/
client_api.go
162 lines (142 loc) · 4.45 KB
/
client_api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
Copyright 2020 The Magma Authors.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package device
import (
"context"
merrors "github.com/go-magma/magma/lib/go/errors"
"github.com/go-magma/magma/lib/go/registry"
"github.com/go-magma/magma/orc8r/cloud/go/serde"
"github.com/go-magma/magma/orc8r/cloud/go/services/device/protos"
"github.com/go-magma/magma/orc8r/cloud/go/storage"
"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/thoas/go-funk"
)
func getDeviceClient() (protos.DeviceClient, error) {
conn, err := registry.GetConnection(ServiceName)
if err != nil {
initErr := merrors.NewInitError(err, ServiceName)
glog.Error(initErr)
return nil, initErr
}
return protos.NewDeviceClient(conn), err
}
func RegisterDevice(networkID, deviceType, deviceKey string, info interface{}) error {
client, err := getDeviceClient()
if err != nil {
return err
}
serializedInfo, err := serde.Serialize(SerdeDomain, deviceType, info)
if err != nil {
return err
}
entity := &protos.PhysicalEntity{
DeviceID: deviceKey,
Type: deviceType,
Info: serializedInfo,
}
req := &protos.RegisterOrUpdateDevicesRequest{
NetworkID: networkID,
Entities: []*protos.PhysicalEntity{entity},
}
_, err = client.RegisterDevices(context.Background(), req)
return err
}
func UpdateDevice(networkID, deviceType, deviceKey string, info interface{}) error {
client, err := getDeviceClient()
if err != nil {
return err
}
serializedInfo, err := serde.Serialize(SerdeDomain, deviceType, info)
if err != nil {
return err
}
entity := &protos.PhysicalEntity{
DeviceID: deviceKey,
Type: deviceType,
Info: serializedInfo,
}
req := &protos.RegisterOrUpdateDevicesRequest{
NetworkID: networkID,
Entities: []*protos.PhysicalEntity{entity},
}
_, err = client.UpdateDevices(context.Background(), req)
return err
}
func DeleteDevices(networkID string, ids []storage.TypeAndKey) error {
client, err := getDeviceClient()
if err != nil {
return err
}
requestIDs := funk.Map(
ids,
func(id storage.TypeAndKey) *protos.DeviceID {
return &protos.DeviceID{Type: id.Type, DeviceID: id.Key}
},
).([]*protos.DeviceID)
req := &protos.DeleteDevicesRequest{NetworkID: networkID, DeviceIDs: requestIDs}
_, err = client.DeleteDevices(context.Background(), req)
return err
}
func DeleteDevice(networkID, deviceType, deviceKey string) error {
return DeleteDevices(networkID, []storage.TypeAndKey{{Type: deviceType, Key: deviceKey}})
}
func GetDevice(networkID, deviceType, deviceKey string) (interface{}, error) {
client, err := getDeviceClient()
if err != nil {
return nil, err
}
deviceID := &protos.DeviceID{Type: deviceType, DeviceID: deviceKey}
req := &protos.GetDeviceInfoRequest{NetworkID: networkID, DeviceIDs: []*protos.DeviceID{deviceID}}
res, err := client.GetDeviceInfo(context.Background(), req)
if err != nil {
return nil, err
}
device, ok := res.DeviceMap[deviceKey]
if !ok {
return nil, merrors.ErrNotFound
}
return serde.Deserialize(SerdeDomain, deviceType, device.Info)
}
func GetDevices(networkID string, deviceType string, deviceIDs []string) (map[string]interface{}, error) {
if len(deviceIDs) == 0 {
return map[string]interface{}{}, nil
}
client, err := getDeviceClient()
if err != nil {
return nil, err
}
requestIDs := funk.Map(
deviceIDs,
func(id string) *protos.DeviceID { return &protos.DeviceID{Type: deviceType, DeviceID: id} },
).([]*protos.DeviceID)
req := &protos.GetDeviceInfoRequest{NetworkID: networkID, DeviceIDs: requestIDs}
res, err := client.GetDeviceInfo(context.Background(), req)
if err != nil {
return map[string]interface{}{}, err
}
ret := make(map[string]interface{}, len(res.DeviceMap))
for k, val := range res.DeviceMap {
iVal, err := serde.Deserialize(SerdeDomain, deviceType, val.Info)
if err != nil {
return map[string]interface{}{}, errors.Wrapf(err, "failed to deserialize device %s", k)
}
ret[k] = iVal
}
return ret, nil
}
func DoesDeviceExist(networkID, deviceType, deviceID string) bool {
_, err := GetDevice(networkID, deviceType, deviceID)
if err != nil {
return false
}
return true
}