Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NOISSUE - Remove thing related code from twins service #1169

Merged
merged 1 commit into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 18 additions & 25 deletions twins/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Twins

Service twins is used for CRUD and update of digital twins. Twin is a semantic
representation of a real world entity, be it device, application or something
else. It holds the sequence of attribute based definitions of a real world thing
and refers to the time series of definition based states that hold the
historical data about the represented real world thing.
representation of a real world data system consisting of data producers and
consumers. It stores the sequence of attribute based definitions of a system and
refers to a time series of definition based states that store the system
historical data.

## Configuration

Expand All @@ -27,8 +27,6 @@ default values.
| MF_TWINS_CLIENT_TLS | Flag that indicates if TLS should be turned on | false |
| MF_TWINS_CA_CERTS | Path to trusted CAs in PEM format | |
| MF_TWINS_MQTT_URL | Mqtt broker URL for twin CRUD and states update notifications | tcp://localhost:1883 |
| MF_TWINS_THING_ID | ID of thing representing twins service & mqtt user | |
| MF_TWINS_THING_KEY | Key of thing representing twins service & mqtt pass | |
| MF_TWINS_CHANNEL_ID | Mqtt notifications topic | |
| MF_NATS_URL | Mainflux NATS broker URL | nats://localhost:4222 |
| MF_AUTHN_GRPC_URL | AuthN service gRPC URL | localhost:8181 |
Expand All @@ -37,8 +35,8 @@ default values.
## Deployment

The service itself is distributed as Docker container. The following snippet
provides a compose file template that can be used to deploy the service container
locally:
provides a compose file template that can be used to deploy the service
container locally:

```yaml
version: "3"
Expand All @@ -62,15 +60,14 @@ services:
MF_TWINS_CLIENT_TLS: [Flag that indicates if TLS should be turned on]
MF_TWINS_CA_CERTS: [Path to trusted CAs in PEM format]
MF_TWINS_MQTT_URL: [Mqtt broker URL for twin CRUD and states]
MF_TWINS_THING_ID: [ID of thing representing twins service]
MF_TWINS_THING_KEY: [Key of thing representing twins service]
MF_TWINS_CHANNEL_ID: [Mqtt notifications topic]
MF_NATS_URL: [Mainflux NATS broker URL]
MF_AUTHN_GRPC_URL: [AuthN service gRPC URL]
MF_AUTHN_GRPC_TIMEOUT: [AuthN service gRPC request timeout in seconds]
```

To start the service outside of the container, execute the following shell script:
To start the service outside of the container, execute the following shell
script:

```bash
# download the latest version of the service
Expand All @@ -97,8 +94,6 @@ MF_TWINS_SINGLE_USER_TOKEN: [User token for single user mode] \
MF_TWINS_CLIENT_TLS: [Flag that indicates if TLS should be turned on] \
MF_TWINS_CA_CERTS: [Path to trusted CAs in PEM format] \
MF_TWINS_MQTT_URL: [Mqtt broker URL for twin CRUD and states] \
MF_TWINS_THING_ID: [ID of thing representing twins service] \
MF_TWINS_THING_KEY: [Key of thing representing twins service] \
MF_TWINS_CHANNEL_ID: [Mqtt notifications topic] \
MF_NATS_URL: [Mainflux NATS broker URL] \
MF_AUTHN_GRPC_URL: [AuthN service gRPC URL] \
Expand All @@ -110,25 +105,23 @@ $GOBIN/mainflux-twins

### Starting twins service

The twins service publishes notifications on an mqtt topic of the format
`channels/<MF_TWINS_CHANNEL_ID>/messages/<twinID>/<crudOp>`, where `crudOp`
The twins service publishes notifications on a NATS subject of the format
`channels.<MF_TWINS_CHANNEL_ID>.messages.<twinID>.<crudOp>`, where `crudOp`
stands for the crud operation done on twin - create, update, delete or
retrieve - or state - save state. In order to use twin service, one must
inform it - via environment variables - about the Mainflux thing and
channel used for mqtt notification publishing. You can use an already existing
thing and channel - thing has to be connected to channel - or create new ones.
retrieve - or state - save state. In order to use twin service notifications,
one must inform it - via environment variables - about the Mainflux channel used
for notification publishing. You must use an already existing channel, since you
cannot know in advance or set the channel id (Mainflux does it automatically).

To set the environment variables, please go to `.env` file and set the following
variables:
To set the environment variable, please go to `.env` file and set the following
variable:

```
MF_TWINS_THING_ID=
MF_TWINS_THING_KEY=
MF_TWINS_CHANNEL_ID=
```

with the corresponding values of the desired thing and channel. If you are
running mainflux natively, than do the same thing in the corresponding console
with the corresponding values of the desired channel. If you are running
mainflux natively, than do the same thing in the corresponding console
environment.

For more information about service capabilities and its usage, please check out
Expand Down
32 changes: 0 additions & 32 deletions twins/api/http/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func updateTwinEndpoint(svc twins.Service) endpoint.Endpoint {
twin := twins.Twin{
ID: req.id,
Name: req.Name,
ThingID: req.ThingID,
Metadata: req.Metadata,
}

Expand Down Expand Up @@ -76,7 +75,6 @@ func viewTwinEndpoint(svc twins.Service) endpoint.Endpoint {
Owner: twin.Owner,
ID: twin.ID,
Name: twin.Name,
ThingID: twin.ThingID,
Created: twin.Created,
Updated: twin.Updated,
Revision: twin.Revision,
Expand All @@ -87,35 +85,6 @@ func viewTwinEndpoint(svc twins.Service) endpoint.Endpoint {
}
}

func viewTwinByThingEndpoint(svc twins.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(viewTwinReq)

if err := req.validate(); err != nil {
return nil, err
}

twin, err := svc.ViewTwinByThing(ctx, req.token, req.id)
if err != nil {
return nil, err
}

res := viewTwinRes{
Owner: twin.Owner,
ID: twin.ID,
Name: twin.Name,
ThingID: twin.ThingID,
Created: twin.Created,
Updated: twin.Updated,
Revision: twin.Revision,
Definitions: twin.Definitions,
Metadata: twin.Metadata,
}

return res, nil
}
}

func listTwinsEndpoint(svc twins.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(listReq)
Expand All @@ -142,7 +111,6 @@ func listTwinsEndpoint(svc twins.Service) endpoint.Endpoint {
Owner: twin.Owner,
ID: twin.ID,
Name: twin.Name,
ThingID: twin.ThingID,
Created: twin.Created,
Updated: twin.Updated,
Revision: twin.Revision,
Expand Down
13 changes: 4 additions & 9 deletions twins/api/http/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const (
email = "user@example.com"
token = "token"
wrongValue = "wrong_value"
thingID = "5b68df78-86f7-48a6-ac4f-bb24dd75c39e"
wrongID = 0
maxNameSize = 1024
topic = "topic"
Expand Down Expand Up @@ -87,8 +86,7 @@ func TestAddTwin(t *testing.T) {
ts := newServer(svc)
defer ts.Close()

// tw := twins.Twin{ThingID: thingID}
tw := twinReq{ThingID: thingID}
tw := twinReq{}
data := toJSON(tw)

tw.Name = invalidName
Expand Down Expand Up @@ -191,7 +189,7 @@ func TestUpdateTwin(t *testing.T) {
ts := newServer(svc)
defer ts.Close()

twin := twins.Twin{ThingID: thingID}
twin := twins.Twin{}
def := twins.Definition{}
stw, _ := svc.AddTwin(context.Background(), token, twin, def)

Expand Down Expand Up @@ -304,15 +302,14 @@ func TestViewTwin(t *testing.T) {
defer ts.Close()

def := twins.Definition{}
twin := twins.Twin{ThingID: thingID}
twin := twins.Twin{}
stw, err := svc.AddTwin(context.Background(), token, twin, def)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))

twres := twinRes{
Owner: stw.Owner,
Name: stw.Name,
ID: stw.ID,
ThingID: stw.ThingID,
Revision: stw.Revision,
Created: stw.Created,
Updated: stw.Updated,
Expand Down Expand Up @@ -387,7 +384,7 @@ func TestRemoveTwin(t *testing.T) {
defer ts.Close()

def := twins.Definition{}
twin := twins.Twin{ThingID: thingID}
twin := twins.Twin{}
stw, _ := svc.AddTwin(context.Background(), token, twin, def)

cases := []struct {
Expand Down Expand Up @@ -444,7 +441,6 @@ func TestRemoveTwin(t *testing.T) {
type twinReq struct {
token string
Name string `json:"name,omitempty"`
ThingID string `json:"thing_id"`
Definition twins.Definition `json:"definition,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
Expand All @@ -453,7 +449,6 @@ type twinRes struct {
Owner string `json:"owner"`
Name string `json:"name,omitempty"`
ID string `json:"id"`
ThingID string `json:"thing_id"`
Revision int `json:"revision"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
Expand Down
1 change: 0 additions & 1 deletion twins/api/http/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ type updateTwinReq struct {
token string
id string
Name string `json:"name,omitempty"`
ThingID string `json:"thing_id,omitempty"`
Definition twins.Definition `json:"definition,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
Expand Down
1 change: 0 additions & 1 deletion twins/api/http/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func (res twinRes) Empty() bool {
type viewTwinRes struct {
Owner string `json:"owner,omitempty"`
ID string `json:"id"`
ThingID string `json:"thing_id"`
Name string `json:"name,omitempty"`
Revision int `json:"revision"`
Created time.Time `json:"created"`
Expand Down
16 changes: 0 additions & 16 deletions twins/api/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ func MakeHandler(tracer opentracing.Tracer, svc twins.Service) http.Handler {
opts...,
))

r.Get("/things/:id", kithttp.NewServer(
kitot.TraceServer(tracer, "view_twin_by_thing")(viewTwinByThingEndpoint(svc)),
decodeViewTwinByThing,
encodeResponse,
opts...,
))

r.Get("/states/:id", kithttp.NewServer(
kitot.TraceServer(tracer, "list_states")(listStatesEndpoint(svc)),
decodeListStates,
Expand Down Expand Up @@ -192,15 +185,6 @@ func decodeListStates(_ context.Context, r *http.Request) (interface{}, error) {
return req, nil
}

func decodeViewTwinByThing(_ context.Context, r *http.Request) (interface{}, error) {
req := viewTwinReq{
token: r.Header.Get("Authorization"),
id: bone.GetValue(r, "id"),
}

return req, nil
}

func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
w.Header().Set("Content-Type", contentType)

Expand Down
13 changes: 0 additions & 13 deletions twins/api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,6 @@ func (lm *loggingMiddleware) ListStates(ctx context.Context, token string, offse
return lm.svc.ListStates(ctx, token, offset, limit, id)
}

func (lm *loggingMiddleware) ViewTwinByThing(ctx context.Context, token, thingid string) (tw twins.Twin, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method view_twin_by_thing for token %s and thing %s took %s to complete", token, thingid, time.Since(begin))
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
}(time.Now())

return lm.svc.ViewTwinByThing(ctx, token, thingid)
}

func (lm *loggingMiddleware) RemoveTwin(ctx context.Context, token, id string) (err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method remove_twin for token %s and twin %s took %s to complete", token, id, time.Since(begin))
Expand Down
9 changes: 0 additions & 9 deletions twins/api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,6 @@ func (ms *metricsMiddleware) ListStates(ctx context.Context, token string, offse
return ms.svc.ListStates(ctx, token, offset, limit, id)
}

func (ms *metricsMiddleware) ViewTwinByThing(ctx context.Context, token, thingid string) (twins.Twin, error) {
defer func(begin time.Time) {
ms.counter.With("method", "view_twin_by_thing").Add(1)
ms.latency.With("method", "view_twin_by_thing").Observe(time.Since(begin).Seconds())
}(time.Now())

return ms.svc.ViewTwinByThing(ctx, token, thingid)
}

func (ms *metricsMiddleware) RemoveTwin(ctx context.Context, token, id string) (err error) {
defer func(begin time.Time) {
ms.counter.With("method", "remove_twin").Add(1)
Expand Down
9 changes: 4 additions & 5 deletions twins/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

// Package twins contains the domain concept definitions needed to support
// Mainflux twins service functionality. Twin is a semantic representation
// of a real world entity, be it device, application or something else.
// It holds the sequence of attribute based definitions of a real world
// thing and refers to the time series of definition based states that
// hold the historical data about the represented real world thing.
// Mainflux twins service functionality. Twin is a semantic representation of a
// real world data system consisting of data producers and consumers. It stores
// the sequence of attribute based definitions of a data system and refers to a
// time series of definition based states that store the system historical data.
package twins
14 changes: 0 additions & 14 deletions twins/mocks/twins.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,6 @@ func (trm *twinRepositoryMock) RetrieveByAttribute(ctx context.Context, channel,
return ids, nil
}

func (trm *twinRepositoryMock) RetrieveByThing(_ context.Context, thingid string) (twins.Twin, error) {
trm.mu.Lock()
defer trm.mu.Unlock()

for _, twin := range trm.twins {
if twin.ThingID == thingid {
return twin, nil
}
}

return twins.Twin{}, twins.ErrNotFound

}

func (trm *twinRepositoryMock) RetrieveAll(_ context.Context, owner string, offset uint64, limit uint64, name string, metadata twins.Metadata) (twins.Page, error) {
trm.mu.Lock()
defer trm.mu.Unlock()
Expand Down
Loading