Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package common |
| 17 | |
| 18 | import ( |
| 19 | "context" |
Chaitrashree G S | 66d4135 | 2020-01-09 20:18:58 -0500 | [diff] [blame] | 20 | "sync" |
| 21 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 22 | "github.com/golang/protobuf/ptypes" |
| 23 | a "github.com/golang/protobuf/ptypes/any" |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 24 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 25 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 26 | ic "github.com/opencord/voltha-protos/v3/go/inter_container" |
| 27 | "github.com/opencord/voltha-protos/v3/go/voltha" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 28 | "google.golang.org/grpc/codes" |
| 29 | "google.golang.org/grpc/status" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | type CoreProxy struct { |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 33 | kafkaICProxy kafka.InterContainerProxy |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 34 | adapterTopic string |
| 35 | coreTopic string |
| 36 | deviceIdCoreMap map[string]string |
| 37 | lockDeviceIdCoreMap sync.RWMutex |
| 38 | } |
| 39 | |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 40 | func NewCoreProxy(kafkaProxy kafka.InterContainerProxy, adapterTopic string, coreTopic string) *CoreProxy { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 41 | var proxy CoreProxy |
| 42 | proxy.kafkaICProxy = kafkaProxy |
| 43 | proxy.adapterTopic = adapterTopic |
| 44 | proxy.coreTopic = coreTopic |
| 45 | proxy.deviceIdCoreMap = make(map[string]string) |
| 46 | proxy.lockDeviceIdCoreMap = sync.RWMutex{} |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 47 | logger.Debugw("TOPICS", log.Fields{"core": proxy.coreTopic, "adapter": proxy.adapterTopic}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 48 | |
| 49 | return &proxy |
| 50 | } |
| 51 | |
| 52 | func unPackResponse(rpc string, deviceId string, success bool, response *a.Any) error { |
| 53 | if success { |
| 54 | return nil |
| 55 | } else { |
| 56 | unpackResult := &ic.Error{} |
| 57 | var err error |
| 58 | if err = ptypes.UnmarshalAny(response, unpackResult); err != nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 59 | logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 60 | } |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 61 | logger.Debugw("response", log.Fields{"rpc": rpc, "deviceId": deviceId, "success": success, "error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 62 | // TODO: Need to get the real error code |
| 63 | return status.Errorf(codes.Canceled, "%s", unpackResult.Reason) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // UpdateCoreReference adds or update a core reference (really the topic name) for a given device Id |
| 68 | func (ap *CoreProxy) UpdateCoreReference(deviceId string, coreReference string) { |
| 69 | ap.lockDeviceIdCoreMap.Lock() |
| 70 | defer ap.lockDeviceIdCoreMap.Unlock() |
| 71 | ap.deviceIdCoreMap[deviceId] = coreReference |
| 72 | } |
| 73 | |
| 74 | // DeleteCoreReference removes a core reference (really the topic name) for a given device Id |
| 75 | func (ap *CoreProxy) DeleteCoreReference(deviceId string) { |
| 76 | ap.lockDeviceIdCoreMap.Lock() |
| 77 | defer ap.lockDeviceIdCoreMap.Unlock() |
| 78 | delete(ap.deviceIdCoreMap, deviceId) |
| 79 | } |
| 80 | |
| 81 | func (ap *CoreProxy) getCoreTopic(deviceId string) kafka.Topic { |
| 82 | ap.lockDeviceIdCoreMap.Lock() |
| 83 | defer ap.lockDeviceIdCoreMap.Unlock() |
| 84 | |
| 85 | if t, exist := ap.deviceIdCoreMap[deviceId]; exist { |
| 86 | return kafka.Topic{Name: t} |
| 87 | } |
| 88 | |
| 89 | return kafka.Topic{Name: ap.coreTopic} |
| 90 | } |
| 91 | |
| 92 | func (ap *CoreProxy) getAdapterTopic(args ...string) kafka.Topic { |
| 93 | return kafka.Topic{Name: ap.adapterTopic} |
| 94 | } |
| 95 | |
| 96 | func (ap *CoreProxy) RegisterAdapter(ctx context.Context, adapter *voltha.Adapter, deviceTypes *voltha.DeviceTypes) error { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 97 | logger.Debugw("registering-adapter", log.Fields{"coreTopic": ap.coreTopic, "adapterTopic": ap.adapterTopic}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 98 | rpc := "Register" |
| 99 | topic := kafka.Topic{Name: ap.coreTopic} |
| 100 | replyToTopic := ap.getAdapterTopic() |
| 101 | args := make([]*kafka.KVArg, 2) |
| 102 | args[0] = &kafka.KVArg{ |
| 103 | Key: "adapter", |
| 104 | Value: adapter, |
| 105 | } |
| 106 | args[1] = &kafka.KVArg{ |
| 107 | Key: "deviceTypes", |
| 108 | Value: deviceTypes, |
| 109 | } |
| 110 | |
| 111 | success, result := ap.kafkaICProxy.InvokeRPC(ctx, rpc, &topic, &replyToTopic, true, "", args...) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 112 | logger.Debugw("Register-Adapter-response", log.Fields{"replyTopic": replyToTopic, "success": success}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 113 | return unPackResponse(rpc, "", success, result) |
| 114 | } |
| 115 | |
| 116 | func (ap *CoreProxy) DeviceUpdate(ctx context.Context, device *voltha.Device) error { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 117 | logger.Debugw("DeviceUpdate", log.Fields{"deviceId": device.Id}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 118 | rpc := "DeviceUpdate" |
| 119 | toTopic := ap.getCoreTopic(device.Id) |
| 120 | args := make([]*kafka.KVArg, 1) |
| 121 | args[0] = &kafka.KVArg{ |
| 122 | Key: "device", |
| 123 | Value: device, |
| 124 | } |
| 125 | // Use a device specific topic as we are the only adaptercore handling requests for this device |
| 126 | replyToTopic := ap.getAdapterTopic() |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 127 | success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, device.Id, args...) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 128 | logger.Debugw("DeviceUpdate-response", log.Fields{"deviceId": device.Id, "success": success}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 129 | return unPackResponse(rpc, device.Id, success, result) |
| 130 | } |
| 131 | |
| 132 | func (ap *CoreProxy) PortCreated(ctx context.Context, deviceId string, port *voltha.Port) error { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 133 | logger.Debugw("PortCreated", log.Fields{"portNo": port.PortNo}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 134 | rpc := "PortCreated" |
| 135 | // Use a device specific topic to send the request. The adapter handling the device creates a device |
| 136 | // specific topic |
| 137 | toTopic := ap.getCoreTopic(deviceId) |
| 138 | args := make([]*kafka.KVArg, 2) |
| 139 | id := &voltha.ID{Id: deviceId} |
| 140 | args[0] = &kafka.KVArg{ |
| 141 | Key: "device_id", |
| 142 | Value: id, |
| 143 | } |
| 144 | args[1] = &kafka.KVArg{ |
| 145 | Key: "port", |
| 146 | Value: port, |
| 147 | } |
| 148 | |
| 149 | // Use a device specific topic as we are the only adaptercore handling requests for this device |
| 150 | replyToTopic := ap.getAdapterTopic() |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 151 | success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, deviceId, args...) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 152 | logger.Debugw("PortCreated-response", log.Fields{"deviceId": deviceId, "success": success}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 153 | return unPackResponse(rpc, deviceId, success, result) |
| 154 | } |
| 155 | |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 156 | func (ap *CoreProxy) PortsStateUpdate(ctx context.Context, deviceId string, operStatus voltha.OperStatus_Types) error { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 157 | logger.Debugw("PortsStateUpdate", log.Fields{"deviceId": deviceId}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 158 | rpc := "PortsStateUpdate" |
| 159 | // Use a device specific topic to send the request. The adapter handling the device creates a device |
| 160 | // specific topic |
| 161 | toTopic := ap.getCoreTopic(deviceId) |
| 162 | args := make([]*kafka.KVArg, 2) |
| 163 | id := &voltha.ID{Id: deviceId} |
| 164 | oStatus := &ic.IntType{Val: int64(operStatus)} |
| 165 | |
| 166 | args[0] = &kafka.KVArg{ |
| 167 | Key: "device_id", |
| 168 | Value: id, |
| 169 | } |
| 170 | args[1] = &kafka.KVArg{ |
| 171 | Key: "oper_status", |
| 172 | Value: oStatus, |
| 173 | } |
| 174 | |
| 175 | // Use a device specific topic as we are the only adaptercore handling requests for this device |
| 176 | replyToTopic := ap.getAdapterTopic() |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 177 | success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, deviceId, args...) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 178 | logger.Debugw("PortsStateUpdate-response", log.Fields{"deviceId": deviceId, "success": success}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 179 | return unPackResponse(rpc, deviceId, success, result) |
| 180 | } |
| 181 | |
| 182 | func (ap *CoreProxy) DeleteAllPorts(ctx context.Context, deviceId string) error { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 183 | logger.Debugw("DeleteAllPorts", log.Fields{"deviceId": deviceId}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 184 | rpc := "DeleteAllPorts" |
| 185 | // Use a device specific topic to send the request. The adapter handling the device creates a device |
| 186 | // specific topic |
| 187 | toTopic := ap.getCoreTopic(deviceId) |
| 188 | args := make([]*kafka.KVArg, 2) |
| 189 | id := &voltha.ID{Id: deviceId} |
| 190 | |
| 191 | args[0] = &kafka.KVArg{ |
| 192 | Key: "device_id", |
| 193 | Value: id, |
| 194 | } |
| 195 | |
| 196 | // Use a device specific topic as we are the only adaptercore handling requests for this device |
| 197 | replyToTopic := ap.getAdapterTopic() |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 198 | success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, deviceId, args...) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 199 | logger.Debugw("DeleteAllPorts-response", log.Fields{"deviceId": deviceId, "success": success}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 200 | return unPackResponse(rpc, deviceId, success, result) |
| 201 | } |
| 202 | |
| 203 | func (ap *CoreProxy) DeviceStateUpdate(ctx context.Context, deviceId string, |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 204 | connStatus voltha.ConnectStatus_Types, operStatus voltha.OperStatus_Types) error { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame^] | 205 | logger.Debugw("DeviceStateUpdate", log.Fields{"deviceId": deviceId}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 206 | rpc := "DeviceStateUpdate" |
| 207 | // Use a device specific topic to send the request. The adapter handling the device creates a device |
| 208 | // specific topic |
| 209 | toTopic := ap.getCoreTopic(deviceId) |
| 210 | args := make([]*kafka.KVArg, 3) |
| 211 | id := &voltha.ID{Id: deviceId} |
| 212 | oStatus := &ic.IntType{Val: int64(operStatus)} |
| 213 | cStatus := &ic.IntType{Val: int64(connStatus)} |
| 214 | |
| 215 | args[0] = &kafka.KVArg{ |
| 216 | Key: "device_id", |
| 217 | Value: id, |
| 218 | } |
| 219 | args[1] = &kafka.KVArg{ |
| 220 | Key: "oper_status", |
| 221 | Value: oStatus, |
| 222 | } |
| 223 | args[2] = &kafka.KVArg{ |
| 224 | Key: "connect_status", |
| 225 | Value: cStatus, |
| 226 | } |
| 227 | // Use a device specific topic as we are the only adaptercore handling requests for this device |
| 228 | replyToTopic := ap.getAdapterTopic() |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 229 | success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, deviceId, args...) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 230 | logger.Debugw("DeviceStateUpdate-response", log.Fields{"deviceId": deviceId, "success": success}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 231 | return unPackResponse(rpc, deviceId, success, result) |
| 232 | } |
| 233 | |
| 234 | func (ap *CoreProxy) ChildDeviceDetected(ctx context.Context, parentDeviceId string, parentPortNo int, |
| 235 | childDeviceType string, channelId int, vendorId string, serialNumber string, onuId int64) (*voltha.Device, error) { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 236 | logger.Debugw("ChildDeviceDetected", log.Fields{"pDeviceId": parentDeviceId, "channelId": channelId}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 237 | rpc := "ChildDeviceDetected" |
| 238 | // Use a device specific topic to send the request. The adapter handling the device creates a device |
| 239 | // specific topic |
| 240 | toTopic := ap.getCoreTopic(parentDeviceId) |
| 241 | replyToTopic := ap.getAdapterTopic() |
| 242 | |
| 243 | args := make([]*kafka.KVArg, 7) |
| 244 | id := &voltha.ID{Id: parentDeviceId} |
| 245 | args[0] = &kafka.KVArg{ |
| 246 | Key: "parent_device_id", |
| 247 | Value: id, |
| 248 | } |
| 249 | ppn := &ic.IntType{Val: int64(parentPortNo)} |
| 250 | args[1] = &kafka.KVArg{ |
| 251 | Key: "parent_port_no", |
| 252 | Value: ppn, |
| 253 | } |
| 254 | cdt := &ic.StrType{Val: childDeviceType} |
| 255 | args[2] = &kafka.KVArg{ |
| 256 | Key: "child_device_type", |
| 257 | Value: cdt, |
| 258 | } |
| 259 | channel := &ic.IntType{Val: int64(channelId)} |
| 260 | args[3] = &kafka.KVArg{ |
| 261 | Key: "channel_id", |
| 262 | Value: channel, |
| 263 | } |
| 264 | vId := &ic.StrType{Val: vendorId} |
| 265 | args[4] = &kafka.KVArg{ |
| 266 | Key: "vendor_id", |
| 267 | Value: vId, |
| 268 | } |
| 269 | sNo := &ic.StrType{Val: serialNumber} |
| 270 | args[5] = &kafka.KVArg{ |
| 271 | Key: "serial_number", |
| 272 | Value: sNo, |
| 273 | } |
| 274 | oId := &ic.IntType{Val: int64(onuId)} |
| 275 | args[6] = &kafka.KVArg{ |
| 276 | Key: "onu_id", |
| 277 | Value: oId, |
| 278 | } |
| 279 | |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 280 | success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, parentDeviceId, args...) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 281 | logger.Debugw("ChildDeviceDetected-response", log.Fields{"pDeviceId": parentDeviceId, "success": success}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 282 | |
| 283 | if success { |
| 284 | volthaDevice := &voltha.Device{} |
| 285 | if err := ptypes.UnmarshalAny(result, volthaDevice); err != nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 286 | logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
Matteo Scandolo | 1530d41 | 2020-01-30 14:57:46 -0800 | [diff] [blame] | 287 | return nil, status.Error(codes.InvalidArgument, err.Error()) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 288 | } |
| 289 | return volthaDevice, nil |
| 290 | } else { |
| 291 | unpackResult := &ic.Error{} |
| 292 | var err error |
| 293 | if err = ptypes.UnmarshalAny(result, unpackResult); err != nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 294 | logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 295 | } |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 296 | logger.Debugw("ChildDeviceDetected-return", log.Fields{"deviceid": parentDeviceId, "success": success, "error": err}) |
Matteo Scandolo | 1530d41 | 2020-01-30 14:57:46 -0800 | [diff] [blame] | 297 | |
| 298 | return nil, status.Error(ICProxyErrorCodeToGrpcErrorCode(unpackResult.Code), unpackResult.Reason) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | } |
| 302 | |
| 303 | func (ap *CoreProxy) ChildDevicesLost(ctx context.Context, parentDeviceId string) error { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 304 | logger.Debugw("ChildDevicesLost", log.Fields{"pDeviceId": parentDeviceId}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 305 | rpc := "ChildDevicesLost" |
| 306 | // Use a device specific topic to send the request. The adapter handling the device creates a device |
| 307 | // specific topic |
| 308 | toTopic := ap.getCoreTopic(parentDeviceId) |
| 309 | replyToTopic := ap.getAdapterTopic() |
| 310 | |
| 311 | args := make([]*kafka.KVArg, 1) |
| 312 | id := &voltha.ID{Id: parentDeviceId} |
| 313 | args[0] = &kafka.KVArg{ |
| 314 | Key: "parent_device_id", |
| 315 | Value: id, |
| 316 | } |
| 317 | |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 318 | success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, parentDeviceId, args...) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 319 | logger.Debugw("ChildDevicesLost-response", log.Fields{"pDeviceId": parentDeviceId, "success": success}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 320 | return unPackResponse(rpc, parentDeviceId, success, result) |
| 321 | } |
| 322 | |
| 323 | func (ap *CoreProxy) ChildDevicesDetected(ctx context.Context, parentDeviceId string) error { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 324 | logger.Debugw("ChildDevicesDetected", log.Fields{"pDeviceId": parentDeviceId}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 325 | rpc := "ChildDevicesDetected" |
| 326 | // Use a device specific topic to send the request. The adapter handling the device creates a device |
| 327 | // specific topic |
| 328 | toTopic := ap.getCoreTopic(parentDeviceId) |
| 329 | replyToTopic := ap.getAdapterTopic() |
| 330 | |
| 331 | args := make([]*kafka.KVArg, 1) |
| 332 | id := &voltha.ID{Id: parentDeviceId} |
| 333 | args[0] = &kafka.KVArg{ |
| 334 | Key: "parent_device_id", |
| 335 | Value: id, |
| 336 | } |
| 337 | |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 338 | success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, parentDeviceId, args...) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 339 | logger.Debugw("ChildDevicesDetected-response", log.Fields{"pDeviceId": parentDeviceId, "success": success}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 340 | return unPackResponse(rpc, parentDeviceId, success, result) |
| 341 | } |
| 342 | |
| 343 | func (ap *CoreProxy) GetDevice(ctx context.Context, parentDeviceId string, deviceId string) (*voltha.Device, error) { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 344 | logger.Debugw("GetDevice", log.Fields{"deviceId": deviceId}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 345 | rpc := "GetDevice" |
| 346 | |
| 347 | toTopic := ap.getCoreTopic(parentDeviceId) |
| 348 | replyToTopic := ap.getAdapterTopic() |
| 349 | |
| 350 | args := make([]*kafka.KVArg, 1) |
| 351 | id := &voltha.ID{Id: deviceId} |
| 352 | args[0] = &kafka.KVArg{ |
| 353 | Key: "device_id", |
| 354 | Value: id, |
| 355 | } |
| 356 | |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 357 | success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, parentDeviceId, args...) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 358 | logger.Debugw("GetDevice-response", log.Fields{"pDeviceId": parentDeviceId, "success": success}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 359 | |
| 360 | if success { |
| 361 | volthaDevice := &voltha.Device{} |
| 362 | if err := ptypes.UnmarshalAny(result, volthaDevice); err != nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 363 | logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
Matteo Scandolo | 1530d41 | 2020-01-30 14:57:46 -0800 | [diff] [blame] | 364 | return nil, status.Error(codes.InvalidArgument, err.Error()) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 365 | } |
| 366 | return volthaDevice, nil |
| 367 | } else { |
| 368 | unpackResult := &ic.Error{} |
| 369 | var err error |
| 370 | if err = ptypes.UnmarshalAny(result, unpackResult); err != nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 371 | logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 372 | } |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 373 | logger.Debugw("GetDevice-return", log.Fields{"deviceid": parentDeviceId, "success": success, "error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 374 | // TODO: Need to get the real error code |
Matteo Scandolo | 1530d41 | 2020-01-30 14:57:46 -0800 | [diff] [blame] | 375 | return nil, status.Error(ICProxyErrorCodeToGrpcErrorCode(unpackResult.Code), unpackResult.Reason) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | func (ap *CoreProxy) GetChildDevice(ctx context.Context, parentDeviceId string, kwargs map[string]interface{}) (*voltha.Device, error) { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 380 | logger.Debugw("GetChildDevice", log.Fields{"parentDeviceId": parentDeviceId, "kwargs": kwargs}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 381 | rpc := "GetChildDevice" |
| 382 | |
| 383 | toTopic := ap.getCoreTopic(parentDeviceId) |
| 384 | replyToTopic := ap.getAdapterTopic() |
| 385 | |
| 386 | args := make([]*kafka.KVArg, 4) |
| 387 | id := &voltha.ID{Id: parentDeviceId} |
| 388 | args[0] = &kafka.KVArg{ |
| 389 | Key: "device_id", |
| 390 | Value: id, |
| 391 | } |
| 392 | |
| 393 | var cnt uint8 = 0 |
| 394 | for k, v := range kwargs { |
| 395 | cnt += 1 |
| 396 | if k == "serial_number" { |
| 397 | val := &ic.StrType{Val: v.(string)} |
| 398 | args[cnt] = &kafka.KVArg{ |
| 399 | Key: k, |
| 400 | Value: val, |
| 401 | } |
| 402 | } else if k == "onu_id" { |
| 403 | val := &ic.IntType{Val: int64(v.(uint32))} |
| 404 | args[cnt] = &kafka.KVArg{ |
| 405 | Key: k, |
| 406 | Value: val, |
| 407 | } |
| 408 | } else if k == "parent_port_no" { |
| 409 | val := &ic.IntType{Val: int64(v.(uint32))} |
| 410 | args[cnt] = &kafka.KVArg{ |
| 411 | Key: k, |
| 412 | Value: val, |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 417 | success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, parentDeviceId, args...) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 418 | logger.Debugw("GetChildDevice-response", log.Fields{"pDeviceId": parentDeviceId, "success": success}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 419 | |
| 420 | if success { |
| 421 | volthaDevice := &voltha.Device{} |
| 422 | if err := ptypes.UnmarshalAny(result, volthaDevice); err != nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 423 | logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
Matteo Scandolo | 1530d41 | 2020-01-30 14:57:46 -0800 | [diff] [blame] | 424 | return nil, status.Error(codes.InvalidArgument, err.Error()) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 425 | } |
| 426 | return volthaDevice, nil |
| 427 | } else { |
| 428 | unpackResult := &ic.Error{} |
| 429 | var err error |
| 430 | if err = ptypes.UnmarshalAny(result, unpackResult); err != nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 431 | logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 432 | } |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 433 | logger.Debugw("GetChildDevice-return", log.Fields{"deviceid": parentDeviceId, "success": success, "error": err}) |
Matteo Scandolo | b45cf59 | 2020-01-21 16:10:56 -0800 | [diff] [blame] | 434 | |
Matteo Scandolo | 1530d41 | 2020-01-30 14:57:46 -0800 | [diff] [blame] | 435 | return nil, status.Error(ICProxyErrorCodeToGrpcErrorCode(unpackResult.Code), unpackResult.Reason) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
| 439 | func (ap *CoreProxy) GetChildDevices(ctx context.Context, parentDeviceId string) (*voltha.Devices, error) { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 440 | logger.Debugw("GetChildDevices", log.Fields{"parentDeviceId": parentDeviceId}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 441 | rpc := "GetChildDevices" |
| 442 | |
| 443 | toTopic := ap.getCoreTopic(parentDeviceId) |
| 444 | replyToTopic := ap.getAdapterTopic() |
| 445 | |
| 446 | args := make([]*kafka.KVArg, 1) |
| 447 | id := &voltha.ID{Id: parentDeviceId} |
| 448 | args[0] = &kafka.KVArg{ |
| 449 | Key: "device_id", |
| 450 | Value: id, |
| 451 | } |
| 452 | |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 453 | success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, parentDeviceId, args...) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 454 | logger.Debugw("GetChildDevices-response", log.Fields{"pDeviceId": parentDeviceId, "success": success}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 455 | |
| 456 | if success { |
| 457 | volthaDevices := &voltha.Devices{} |
| 458 | if err := ptypes.UnmarshalAny(result, volthaDevices); err != nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 459 | logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
Matteo Scandolo | 1530d41 | 2020-01-30 14:57:46 -0800 | [diff] [blame] | 460 | return nil, status.Error(codes.InvalidArgument, err.Error()) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 461 | } |
| 462 | return volthaDevices, nil |
| 463 | } else { |
| 464 | unpackResult := &ic.Error{} |
| 465 | var err error |
| 466 | if err = ptypes.UnmarshalAny(result, unpackResult); err != nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 467 | logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 468 | } |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 469 | logger.Debugw("GetChildDevices-return", log.Fields{"deviceid": parentDeviceId, "success": success, "error": err}) |
Matteo Scandolo | b45cf59 | 2020-01-21 16:10:56 -0800 | [diff] [blame] | 470 | |
Matteo Scandolo | 1530d41 | 2020-01-30 14:57:46 -0800 | [diff] [blame] | 471 | return nil, status.Error(ICProxyErrorCodeToGrpcErrorCode(unpackResult.Code), unpackResult.Reason) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
| 475 | func (ap *CoreProxy) SendPacketIn(ctx context.Context, deviceId string, port uint32, pktPayload []byte) error { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 476 | logger.Debugw("SendPacketIn", log.Fields{"deviceId": deviceId, "port": port, "pktPayload": pktPayload}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 477 | rpc := "PacketIn" |
| 478 | // Use a device specific topic to send the request. The adapter handling the device creates a device |
| 479 | // specific topic |
| 480 | toTopic := ap.getCoreTopic(deviceId) |
| 481 | replyToTopic := ap.getAdapterTopic() |
| 482 | |
| 483 | args := make([]*kafka.KVArg, 3) |
| 484 | id := &voltha.ID{Id: deviceId} |
| 485 | args[0] = &kafka.KVArg{ |
| 486 | Key: "device_id", |
| 487 | Value: id, |
| 488 | } |
| 489 | portNo := &ic.IntType{Val: int64(port)} |
| 490 | args[1] = &kafka.KVArg{ |
| 491 | Key: "port", |
| 492 | Value: portNo, |
| 493 | } |
| 494 | pkt := &ic.Packet{Payload: pktPayload} |
| 495 | args[2] = &kafka.KVArg{ |
| 496 | Key: "packet", |
| 497 | Value: pkt, |
| 498 | } |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 499 | success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, deviceId, args...) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 500 | logger.Debugw("SendPacketIn-response", log.Fields{"pDeviceId": deviceId, "success": success}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 501 | return unPackResponse(rpc, deviceId, success, result) |
| 502 | } |
| 503 | |
David Bainbridge | 36d0b20 | 2019-10-23 18:33:34 +0000 | [diff] [blame] | 504 | func (ap *CoreProxy) DeviceReasonUpdate(ctx context.Context, deviceId string, deviceReason string) error { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 505 | logger.Debugw("DeviceReasonUpdate", log.Fields{"deviceId": deviceId, "deviceReason": deviceReason}) |
David Bainbridge | 36d0b20 | 2019-10-23 18:33:34 +0000 | [diff] [blame] | 506 | rpc := "DeviceReasonUpdate" |
| 507 | // Use a device specific topic to send the request. The adapter handling the device creates a device |
| 508 | // specific topic |
| 509 | toTopic := ap.getCoreTopic(deviceId) |
| 510 | replyToTopic := ap.getAdapterTopic() |
| 511 | |
| 512 | args := make([]*kafka.KVArg, 2) |
| 513 | id := &voltha.ID{Id: deviceId} |
| 514 | args[0] = &kafka.KVArg{ |
| 515 | Key: "device_id", |
| 516 | Value: id, |
| 517 | } |
| 518 | reason := &ic.StrType{Val: deviceReason} |
| 519 | args[1] = &kafka.KVArg{ |
| 520 | Key: "device_reason", |
| 521 | Value: reason, |
| 522 | } |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 523 | success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, deviceId, args...) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 524 | logger.Debugw("DeviceReason-response", log.Fields{"pDeviceId": deviceId, "success": success}) |
David Bainbridge | 36d0b20 | 2019-10-23 18:33:34 +0000 | [diff] [blame] | 525 | return unPackResponse(rpc, deviceId, success, result) |
| 526 | } |
| 527 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 528 | func (ap *CoreProxy) DevicePMConfigUpdate(ctx context.Context, pmConfigs *voltha.PmConfigs) error { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 529 | logger.Debugw("DevicePMConfigUpdate", log.Fields{"pmConfigs": pmConfigs}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 530 | rpc := "DevicePMConfigUpdate" |
| 531 | // Use a device specific topic to send the request. The adapter handling the device creates a device |
| 532 | // specific topic |
| 533 | toTopic := ap.getCoreTopic(pmConfigs.Id) |
| 534 | replyToTopic := ap.getAdapterTopic() |
| 535 | |
| 536 | args := make([]*kafka.KVArg, 1) |
| 537 | args[0] = &kafka.KVArg{ |
| 538 | Key: "device_pm_config", |
| 539 | Value: pmConfigs, |
| 540 | } |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 541 | success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, pmConfigs.Id, args...) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 542 | logger.Debugw("DevicePMConfigUpdate-response", log.Fields{"pDeviceId": pmConfigs.Id, "success": success}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 543 | return unPackResponse(rpc, pmConfigs.Id, success, result) |
| 544 | } |
| 545 | |
| 546 | func (ap *CoreProxy) ReconcileChildDevices(ctx context.Context, parentDeviceId string) error { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 547 | logger.Debugw("ReconcileChildDevices", log.Fields{"parentDeviceId": parentDeviceId}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 548 | rpc := "ReconcileChildDevices" |
| 549 | // Use a device specific topic to send the request. The adapter handling the device creates a device |
| 550 | // specific topic |
| 551 | toTopic := ap.getCoreTopic(parentDeviceId) |
| 552 | replyToTopic := ap.getAdapterTopic() |
| 553 | |
| 554 | args := []*kafka.KVArg{ |
| 555 | {Key: "parent_device_id", Value: &voltha.ID{Id: parentDeviceId}}, |
| 556 | } |
| 557 | |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 558 | success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, parentDeviceId, args...) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 559 | logger.Debugw("ReconcileChildDevices-response", log.Fields{"pDeviceId": parentDeviceId, "success": success}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 560 | return unPackResponse(rpc, parentDeviceId, success, result) |
| 561 | } |
Chaitrashree G S | 66d4135 | 2020-01-09 20:18:58 -0500 | [diff] [blame] | 562 | |
| 563 | func (ap *CoreProxy) PortStateUpdate(ctx context.Context, deviceId string, pType voltha.Port_PortType, portNum uint32, |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 564 | operStatus voltha.OperStatus_Types) error { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 565 | logger.Debugw("PortStateUpdate", log.Fields{"deviceId": deviceId, "portType": pType, "portNo": portNum, "operation_status": operStatus}) |
Chaitrashree G S | 66d4135 | 2020-01-09 20:18:58 -0500 | [diff] [blame] | 566 | rpc := "PortStateUpdate" |
| 567 | // Use a device specific topic to send the request. The adapter handling the device creates a device |
| 568 | // specific topic |
| 569 | toTopic := ap.getCoreTopic(deviceId) |
| 570 | args := make([]*kafka.KVArg, 4) |
| 571 | deviceID := &voltha.ID{Id: deviceId} |
| 572 | portNo := &ic.IntType{Val: int64(portNum)} |
| 573 | portType := &ic.IntType{Val: int64(pType)} |
| 574 | oStatus := &ic.IntType{Val: int64(operStatus)} |
| 575 | |
| 576 | args[0] = &kafka.KVArg{ |
| 577 | Key: "device_id", |
| 578 | Value: deviceID, |
| 579 | } |
| 580 | args[1] = &kafka.KVArg{ |
| 581 | Key: "oper_status", |
| 582 | Value: oStatus, |
| 583 | } |
| 584 | args[2] = &kafka.KVArg{ |
| 585 | Key: "port_type", |
| 586 | Value: portType, |
| 587 | } |
| 588 | args[3] = &kafka.KVArg{ |
| 589 | Key: "port_no", |
| 590 | Value: portNo, |
| 591 | } |
| 592 | |
| 593 | // Use a device specific topic as we are the only adaptercore handling requests for this device |
| 594 | replyToTopic := ap.getAdapterTopic() |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 595 | success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, deviceId, args...) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 596 | logger.Debugw("PortStateUpdate-response", log.Fields{"deviceId": deviceId, "success": success}) |
Chaitrashree G S | 66d4135 | 2020-01-09 20:18:58 -0500 | [diff] [blame] | 597 | return unPackResponse(rpc, deviceId, success, result) |
| 598 | } |