blob: 9ea44a12921679bb1c2fe0b1f937cf6337617153 [file] [log] [blame]
khenaidoob9203542018-09-17 22:56:37 -04001/*
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 */
npujar1d86a522019-11-14 17:11:16 +053016
khenaidoob9203542018-09-17 22:56:37 -040017package core
18
19import (
npujar1d86a522019-11-14 17:11:16 +053020 "context"
khenaidoob9203542018-09-17 22:56:37 -040021 "errors"
David Bainbridgefd27f4b2020-03-26 18:27:41 -070022 "time"
23
khenaidoob9203542018-09-17 22:56:37 -040024 "github.com/golang/protobuf/ptypes"
25 "github.com/golang/protobuf/ptypes/empty"
sbarbari17d7e222019-11-05 10:02:29 -050026 "github.com/opencord/voltha-go/db/model"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080027 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
28 "github.com/opencord/voltha-lib-go/v3/pkg/log"
29 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
30 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidoob9203542018-09-17 22:56:37 -040031)
32
npujar1d86a522019-11-14 17:11:16 +053033// AdapterRequestHandlerProxy represent adapter request handler proxy attributes
khenaidoob9203542018-09-17 22:56:37 -040034type AdapterRequestHandlerProxy struct {
npujar1d86a522019-11-14 17:11:16 +053035 coreInstanceID string
khenaidoode93b462019-02-13 22:17:21 -050036 deviceMgr *DeviceManager
37 lDeviceMgr *LogicalDeviceManager
38 adapterMgr *AdapterManager
39 localDataProxy *model.Proxy
40 clusterDataProxy *model.Proxy
khenaidoo442e7c72020-03-10 16:13:48 -040041 defaultRequestTimeout time.Duration
42 longRunningRequestTimeout time.Duration
Richard Jankowski199fd862019-03-18 14:49:51 -040043 core *Core
khenaidoob9203542018-09-17 22:56:37 -040044}
45
npujar1d86a522019-11-14 17:11:16 +053046// NewAdapterRequestHandlerProxy assigns values for adapter request handler proxy attributes and returns the new instance
47func NewAdapterRequestHandlerProxy(core *Core, coreInstanceID string, dMgr *DeviceManager, ldMgr *LogicalDeviceManager,
David Bainbridgefd27f4b2020-03-26 18:27:41 -070048 aMgr *AdapterManager, cdProxy *model.Proxy, ldProxy *model.Proxy, longRunningRequestTimeout time.Duration,
khenaidoo442e7c72020-03-10 16:13:48 -040049 defaultRequestTimeout time.Duration) *AdapterRequestHandlerProxy {
khenaidoob9203542018-09-17 22:56:37 -040050 var proxy AdapterRequestHandlerProxy
Richard Jankowski199fd862019-03-18 14:49:51 -040051 proxy.core = core
npujar1d86a522019-11-14 17:11:16 +053052 proxy.coreInstanceID = coreInstanceID
khenaidoob9203542018-09-17 22:56:37 -040053 proxy.deviceMgr = dMgr
54 proxy.lDeviceMgr = ldMgr
55 proxy.clusterDataProxy = cdProxy
56 proxy.localDataProxy = ldProxy
khenaidoo21d51152019-02-01 13:48:37 -050057 proxy.adapterMgr = aMgr
khenaidoo297cd252019-02-07 22:10:23 -050058 proxy.defaultRequestTimeout = defaultRequestTimeout
59 proxy.longRunningRequestTimeout = longRunningRequestTimeout
khenaidoob9203542018-09-17 22:56:37 -040060 return &proxy
61}
62
khenaidoo79232702018-12-04 11:00:41 -050063func (rhp *AdapterRequestHandlerProxy) Register(args []*ic.Argument) (*voltha.CoreInstance, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -050064 if len(args) < 3 {
Girish Kumarf56a4682020-03-20 20:07:46 +000065 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
khenaidoob9203542018-09-17 22:56:37 -040066 err := errors.New("invalid-number-of-args")
67 return nil, err
68 }
69 adapter := &voltha.Adapter{}
khenaidoo91ecfd62018-11-04 17:13:42 -050070 deviceTypes := &voltha.DeviceTypes{}
npujar1d86a522019-11-14 17:11:16 +053071 transactionID := &ic.StrType{}
khenaidoo91ecfd62018-11-04 17:13:42 -050072 for _, arg := range args {
73 switch arg.Key {
74 case "adapter":
75 if err := ptypes.UnmarshalAny(arg.Value, adapter); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000076 logger.Warnw("cannot-unmarshal-adapter", log.Fields{"error": err})
khenaidoo91ecfd62018-11-04 17:13:42 -050077 return nil, err
78 }
79 case "deviceTypes":
80 if err := ptypes.UnmarshalAny(arg.Value, deviceTypes); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000081 logger.Warnw("cannot-unmarshal-device-types", log.Fields{"error": err})
khenaidoo91ecfd62018-11-04 17:13:42 -050082 return nil, err
83 }
khenaidoo297cd252019-02-07 22:10:23 -050084 case kafka.TransactionKey:
npujar1d86a522019-11-14 17:11:16 +053085 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000086 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
khenaidoo297cd252019-02-07 22:10:23 -050087 return nil, err
88 }
khenaidoo91ecfd62018-11-04 17:13:42 -050089 }
khenaidoob9203542018-09-17 22:56:37 -040090 }
Girish Kumarf56a4682020-03-20 20:07:46 +000091 logger.Debugw("Register", log.Fields{"adapter": *adapter, "device-types": deviceTypes, "transaction-id": transactionID.Val, "core-id": rhp.coreInstanceID})
khenaidoo297cd252019-02-07 22:10:23 -050092
Thomas Lee Se5a44012019-11-07 20:32:24 +053093 return rhp.adapterMgr.registerAdapter(adapter, deviceTypes)
khenaidoob9203542018-09-17 22:56:37 -040094}
95
npujar1d86a522019-11-14 17:11:16 +053096// GetDevice returns device info
khenaidoo79232702018-12-04 11:00:41 -050097func (rhp *AdapterRequestHandlerProxy) GetDevice(args []*ic.Argument) (*voltha.Device, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -050098 if len(args) < 2 {
Girish Kumarf56a4682020-03-20 20:07:46 +000099 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
khenaidoob9203542018-09-17 22:56:37 -0400100 err := errors.New("invalid-number-of-args")
101 return nil, err
102 }
khenaidoo297cd252019-02-07 22:10:23 -0500103
khenaidoob9203542018-09-17 22:56:37 -0400104 pID := &voltha.ID{}
khenaidoo297cd252019-02-07 22:10:23 -0500105 transactionID := &ic.StrType{}
106 for _, arg := range args {
107 switch arg.Key {
108 case "device_id":
109 if err := ptypes.UnmarshalAny(arg.Value, pID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000110 logger.Warnw("cannot-unmarshal-ID", log.Fields{"error": err})
khenaidoo297cd252019-02-07 22:10:23 -0500111 return nil, err
112 }
113 case kafka.TransactionKey:
114 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000115 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
khenaidoo297cd252019-02-07 22:10:23 -0500116 return nil, err
117 }
118 }
khenaidoob9203542018-09-17 22:56:37 -0400119 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000120 logger.Debugw("GetDevice", log.Fields{"deviceID": pID.Id, "transactionID": transactionID.Val})
khenaidoo297cd252019-02-07 22:10:23 -0500121
khenaidoob9203542018-09-17 22:56:37 -0400122 // Get the device via the device manager
npujar467fe752020-01-16 20:17:45 +0530123 device, err := rhp.deviceMgr.GetDevice(context.TODO(), pID.Id)
npujar1d86a522019-11-14 17:11:16 +0530124 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000125 logger.Debugw("get-device-failed", log.Fields{"deviceID": pID.Id, "error": err})
khenaidoob9203542018-09-17 22:56:37 -0400126 }
khenaidoo442e7c72020-03-10 16:13:48 -0400127 return device, err
khenaidoob9203542018-09-17 22:56:37 -0400128}
129
npujar1d86a522019-11-14 17:11:16 +0530130// DeviceUpdate updates device using adapter data
khenaidoo79232702018-12-04 11:00:41 -0500131func (rhp *AdapterRequestHandlerProxy) DeviceUpdate(args []*ic.Argument) (*empty.Empty, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500132 if len(args) < 2 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000133 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
khenaidoob9203542018-09-17 22:56:37 -0400134 err := errors.New("invalid-number-of-args")
135 return nil, err
136 }
khenaidoo297cd252019-02-07 22:10:23 -0500137
khenaidoob9203542018-09-17 22:56:37 -0400138 device := &voltha.Device{}
khenaidoo297cd252019-02-07 22:10:23 -0500139 transactionID := &ic.StrType{}
140 for _, arg := range args {
141 switch arg.Key {
142 case "device":
143 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000144 logger.Warnw("cannot-unmarshal-ID", log.Fields{"error": err})
khenaidoo297cd252019-02-07 22:10:23 -0500145 return nil, err
146 }
147 case kafka.TransactionKey:
148 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000149 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
khenaidoo297cd252019-02-07 22:10:23 -0500150 return nil, err
151 }
152 }
khenaidoob9203542018-09-17 22:56:37 -0400153 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000154 logger.Debugw("DeviceUpdate", log.Fields{"deviceID": device.Id, "transactionID": transactionID.Val})
khenaidoo297cd252019-02-07 22:10:23 -0500155
khenaidoo442e7c72020-03-10 16:13:48 -0400156 if err := rhp.deviceMgr.updateDeviceUsingAdapterData(context.TODO(), device); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000157 logger.Debugw("unable-to-update-device-using-adapter-data", log.Fields{"error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400158 return nil, err
khenaidoob9203542018-09-17 22:56:37 -0400159 }
khenaidoo442e7c72020-03-10 16:13:48 -0400160 return &empty.Empty{}, nil
khenaidoob9203542018-09-17 22:56:37 -0400161}
162
npujar1d86a522019-11-14 17:11:16 +0530163// GetChildDevice returns details of child device
khenaidoo79232702018-12-04 11:00:41 -0500164func (rhp *AdapterRequestHandlerProxy) GetChildDevice(args []*ic.Argument) (*voltha.Device, error) {
Matt Jeanneret4e241952019-02-28 11:16:04 -0500165 if len(args) < 3 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000166 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
khenaidoob9203542018-09-17 22:56:37 -0400167 err := errors.New("invalid-number-of-args")
168 return nil, err
169 }
khenaidoo297cd252019-02-07 22:10:23 -0500170
khenaidoo2c6f1672018-09-20 23:14:41 -0400171 pID := &voltha.ID{}
khenaidoo297cd252019-02-07 22:10:23 -0500172 transactionID := &ic.StrType{}
Matt Jeanneret4e241952019-02-28 11:16:04 -0500173 serialNumber := &ic.StrType{}
npujar1d86a522019-11-14 17:11:16 +0530174 onuID := &ic.IntType{}
Matt Jeanneret4e241952019-02-28 11:16:04 -0500175 parentPortNo := &ic.IntType{}
khenaidoo297cd252019-02-07 22:10:23 -0500176 for _, arg := range args {
177 switch arg.Key {
178 case "device_id":
179 if err := ptypes.UnmarshalAny(arg.Value, pID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000180 logger.Warnw("cannot-unmarshal-ID", log.Fields{"error": err})
khenaidoo297cd252019-02-07 22:10:23 -0500181 return nil, err
182 }
Matt Jeanneret4e241952019-02-28 11:16:04 -0500183 case "serial_number":
184 if err := ptypes.UnmarshalAny(arg.Value, serialNumber); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000185 logger.Warnw("cannot-unmarshal-ID", log.Fields{"error": err})
Matt Jeanneret4e241952019-02-28 11:16:04 -0500186 return nil, err
187 }
188 case "onu_id":
npujar1d86a522019-11-14 17:11:16 +0530189 if err := ptypes.UnmarshalAny(arg.Value, onuID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000190 logger.Warnw("cannot-unmarshal-ID", log.Fields{"error": err})
Matt Jeanneret4e241952019-02-28 11:16:04 -0500191 return nil, err
192 }
193 case "parent_port_no":
194 if err := ptypes.UnmarshalAny(arg.Value, parentPortNo); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000195 logger.Warnw("cannot-unmarshal-ID", log.Fields{"error": err})
Matt Jeanneret4e241952019-02-28 11:16:04 -0500196 return nil, err
197 }
khenaidoo297cd252019-02-07 22:10:23 -0500198 case kafka.TransactionKey:
199 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000200 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
khenaidoo297cd252019-02-07 22:10:23 -0500201 return nil, err
202 }
203 }
khenaidoob9203542018-09-17 22:56:37 -0400204 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000205 logger.Debugw("GetChildDevice", log.Fields{"parentDeviceID": pID.Id, "args": args, "transactionID": transactionID.Val})
khenaidoo297cd252019-02-07 22:10:23 -0500206
npujar467fe752020-01-16 20:17:45 +0530207 return rhp.deviceMgr.GetChildDevice(context.TODO(), pID.Id, serialNumber.Val, onuID.Val, parentPortNo.Val)
khenaidoob9203542018-09-17 22:56:37 -0400208}
209
npujar1d86a522019-11-14 17:11:16 +0530210// GetChildDeviceWithProxyAddress returns details of child device with proxy address
Matt Jeanneret2a20aaa2019-03-05 21:04:02 -0500211func (rhp *AdapterRequestHandlerProxy) GetChildDeviceWithProxyAddress(args []*ic.Argument) (*voltha.Device, error) {
212 if len(args) < 2 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000213 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
Matt Jeanneret2a20aaa2019-03-05 21:04:02 -0500214 err := errors.New("invalid-number-of-args")
215 return nil, err
216 }
217
218 proxyAddress := &voltha.Device_ProxyAddress{}
219 transactionID := &ic.StrType{}
220 for _, arg := range args {
221 switch arg.Key {
222 case "proxy_address":
223 if err := ptypes.UnmarshalAny(arg.Value, proxyAddress); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000224 logger.Warnw("cannot-unmarshal-proxy-address", log.Fields{"error": err})
Matt Jeanneret2a20aaa2019-03-05 21:04:02 -0500225 return nil, err
226 }
227 case kafka.TransactionKey:
228 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000229 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
Matt Jeanneret2a20aaa2019-03-05 21:04:02 -0500230 return nil, err
231 }
232 }
233 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000234 logger.Debugw("GetChildDeviceWithProxyAddress", log.Fields{"proxyAddress": proxyAddress, "transactionID": transactionID.Val})
Matt Jeanneret2a20aaa2019-03-05 21:04:02 -0500235
npujar467fe752020-01-16 20:17:45 +0530236 return rhp.deviceMgr.GetChildDeviceWithProxyAddress(context.TODO(), proxyAddress)
Matt Jeanneret2a20aaa2019-03-05 21:04:02 -0500237}
238
npujar1d86a522019-11-14 17:11:16 +0530239// GetPorts returns the ports information of the device based on the port type.
khenaidoo79232702018-12-04 11:00:41 -0500240func (rhp *AdapterRequestHandlerProxy) GetPorts(args []*ic.Argument) (*voltha.Ports, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500241 if len(args) < 3 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000242 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
khenaidoob9203542018-09-17 22:56:37 -0400243 err := errors.New("invalid-number-of-args")
244 return nil, err
245 }
npujar1d86a522019-11-14 17:11:16 +0530246 deviceID := &voltha.ID{}
khenaidoo79232702018-12-04 11:00:41 -0500247 pt := &ic.IntType{}
khenaidoo297cd252019-02-07 22:10:23 -0500248 transactionID := &ic.StrType{}
khenaidoo92e62c52018-10-03 14:02:54 -0400249 for _, arg := range args {
250 switch arg.Key {
251 case "device_id":
npujar1d86a522019-11-14 17:11:16 +0530252 if err := ptypes.UnmarshalAny(arg.Value, deviceID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000253 logger.Warnw("cannot-unmarshal-device-id", log.Fields{"error": err})
khenaidoo92e62c52018-10-03 14:02:54 -0400254 return nil, err
255 }
256 case "port_type":
257 if err := ptypes.UnmarshalAny(arg.Value, pt); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000258 logger.Warnw("cannot-unmarshal-porttype", log.Fields{"error": err})
khenaidoo92e62c52018-10-03 14:02:54 -0400259 return nil, err
260 }
khenaidoo297cd252019-02-07 22:10:23 -0500261 case kafka.TransactionKey:
262 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000263 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
khenaidoo297cd252019-02-07 22:10:23 -0500264 return nil, err
265 }
khenaidoo92e62c52018-10-03 14:02:54 -0400266 }
khenaidoob9203542018-09-17 22:56:37 -0400267 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000268 logger.Debugw("GetPorts", log.Fields{"deviceID": deviceID.Id, "portype": pt.Val, "transactionID": transactionID.Val})
khenaidoo442e7c72020-03-10 16:13:48 -0400269
npujar1d86a522019-11-14 17:11:16 +0530270 return rhp.deviceMgr.getPorts(context.TODO(), deviceID.Id, voltha.Port_PortType(pt.Val))
khenaidoob9203542018-09-17 22:56:37 -0400271}
272
npujar1d86a522019-11-14 17:11:16 +0530273// GetChildDevices gets all the child device IDs from the device passed as parameter
khenaidoo297cd252019-02-07 22:10:23 -0500274func (rhp *AdapterRequestHandlerProxy) GetChildDevices(args []*ic.Argument) (*voltha.Devices, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500275 if len(args) < 2 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000276 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
khenaidoob9203542018-09-17 22:56:37 -0400277 err := errors.New("invalid-number-of-args")
278 return nil, err
279 }
khenaidoo297cd252019-02-07 22:10:23 -0500280
khenaidoo2c6f1672018-09-20 23:14:41 -0400281 pID := &voltha.ID{}
khenaidoo297cd252019-02-07 22:10:23 -0500282 transactionID := &ic.StrType{}
283 for _, arg := range args {
284 switch arg.Key {
285 case "device_id":
286 if err := ptypes.UnmarshalAny(arg.Value, pID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000287 logger.Warnw("cannot-unmarshal-ID", log.Fields{"error": err})
khenaidoo297cd252019-02-07 22:10:23 -0500288 return nil, err
289 }
290 case kafka.TransactionKey:
291 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000292 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
khenaidoo297cd252019-02-07 22:10:23 -0500293 return nil, err
294 }
295 }
khenaidoob9203542018-09-17 22:56:37 -0400296 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000297 logger.Debugw("GetChildDevices", log.Fields{"deviceID": pID.Id, "transactionID": transactionID.Val})
khenaidoo297cd252019-02-07 22:10:23 -0500298
npujar467fe752020-01-16 20:17:45 +0530299 return rhp.deviceMgr.getAllChildDevices(context.TODO(), pID.Id)
khenaidoob9203542018-09-17 22:56:37 -0400300}
301
khenaidoo442e7c72020-03-10 16:13:48 -0400302// ChildDeviceDetected is invoked when a child device is detected. The following parameters are expected:
Matt Jeanneret4e241952019-02-28 11:16:04 -0500303// {parent_device_id, parent_port_no, child_device_type, channel_id, vendor_id, serial_number)
Mahir Gunyel6deaa242019-06-27 04:53:33 -0700304func (rhp *AdapterRequestHandlerProxy) ChildDeviceDetected(args []*ic.Argument) (*voltha.Device, error) {
khenaidoo297cd252019-02-07 22:10:23 -0500305 if len(args) < 5 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000306 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
khenaidoob9203542018-09-17 22:56:37 -0400307 err := errors.New("invalid-number-of-args")
308 return nil, err
309 }
310
311 pID := &voltha.ID{}
khenaidoo79232702018-12-04 11:00:41 -0500312 portNo := &ic.IntType{}
313 dt := &ic.StrType{}
npujar1d86a522019-11-14 17:11:16 +0530314 chnlID := &ic.IntType{}
khenaidoo297cd252019-02-07 22:10:23 -0500315 transactionID := &ic.StrType{}
Matt Jeanneret4e241952019-02-28 11:16:04 -0500316 serialNumber := &ic.StrType{}
npujar1d86a522019-11-14 17:11:16 +0530317 vendorID := &ic.StrType{}
318 onuID := &ic.IntType{}
khenaidoob9203542018-09-17 22:56:37 -0400319 for _, arg := range args {
320 switch arg.Key {
321 case "parent_device_id":
322 if err := ptypes.UnmarshalAny(arg.Value, pID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000323 logger.Warnw("cannot-unmarshal-parent-device-id", log.Fields{"error": err})
khenaidoob9203542018-09-17 22:56:37 -0400324 return nil, err
325 }
326 case "parent_port_no":
327 if err := ptypes.UnmarshalAny(arg.Value, portNo); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000328 logger.Warnw("cannot-unmarshal-parent-port", log.Fields{"error": err})
khenaidoob9203542018-09-17 22:56:37 -0400329 return nil, err
330 }
331 case "child_device_type":
332 if err := ptypes.UnmarshalAny(arg.Value, dt); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000333 logger.Warnw("cannot-unmarshal-child-device-type", log.Fields{"error": err})
khenaidoob9203542018-09-17 22:56:37 -0400334 return nil, err
335 }
336 case "channel_id":
npujar1d86a522019-11-14 17:11:16 +0530337 if err := ptypes.UnmarshalAny(arg.Value, chnlID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000338 logger.Warnw("cannot-unmarshal-channel-id", log.Fields{"error": err})
khenaidoob9203542018-09-17 22:56:37 -0400339 return nil, err
340 }
Matt Jeanneret4e241952019-02-28 11:16:04 -0500341 case "vendor_id":
npujar1d86a522019-11-14 17:11:16 +0530342 if err := ptypes.UnmarshalAny(arg.Value, vendorID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000343 logger.Warnw("cannot-unmarshal-vendor-id", log.Fields{"error": err})
Matt Jeanneret4e241952019-02-28 11:16:04 -0500344 return nil, err
345 }
346 case "serial_number":
347 if err := ptypes.UnmarshalAny(arg.Value, serialNumber); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000348 logger.Warnw("cannot-unmarshal-serial-number", log.Fields{"error": err})
Matt Jeanneret4e241952019-02-28 11:16:04 -0500349 return nil, err
350 }
351 case "onu_id":
npujar1d86a522019-11-14 17:11:16 +0530352 if err := ptypes.UnmarshalAny(arg.Value, onuID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000353 logger.Warnw("cannot-unmarshal-onu-id", log.Fields{"error": err})
Matt Jeanneret4e241952019-02-28 11:16:04 -0500354 return nil, err
355 }
khenaidoo297cd252019-02-07 22:10:23 -0500356 case kafka.TransactionKey:
357 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000358 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
khenaidoo297cd252019-02-07 22:10:23 -0500359 return nil, err
360 }
khenaidoob9203542018-09-17 22:56:37 -0400361 }
362 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000363 logger.Debugw("ChildDeviceDetected", log.Fields{"parentDeviceID": pID.Id, "parentPortNo": portNo.Val,
npujar1d86a522019-11-14 17:11:16 +0530364 "deviceType": dt.Val, "channelID": chnlID.Val, "serialNumber": serialNumber.Val,
365 "vendorID": vendorID.Val, "onuID": onuID.Val, "transactionID": transactionID.Val})
khenaidoo297cd252019-02-07 22:10:23 -0500366
npujar467fe752020-01-16 20:17:45 +0530367 device, err := rhp.deviceMgr.childDeviceDetected(context.TODO(), pID.Id, portNo.Val, dt.Val, chnlID.Val, vendorID.Val, serialNumber.Val, onuID.Val)
Mahir Gunyel6deaa242019-06-27 04:53:33 -0700368 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000369 logger.Debugw("child-detection-failed", log.Fields{"parentID": pID.Id, "onuID": onuID.Val, "error": err})
khenaidoo59ef7be2019-06-21 12:40:28 -0400370 }
khenaidoo442e7c72020-03-10 16:13:48 -0400371 return device, err
khenaidoob9203542018-09-17 22:56:37 -0400372}
373
npujar1d86a522019-11-14 17:11:16 +0530374// DeviceStateUpdate updates device status
khenaidoo79232702018-12-04 11:00:41 -0500375func (rhp *AdapterRequestHandlerProxy) DeviceStateUpdate(args []*ic.Argument) (*empty.Empty, error) {
khenaidoo297cd252019-02-07 22:10:23 -0500376 if len(args) < 3 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000377 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
khenaidoob9203542018-09-17 22:56:37 -0400378 err := errors.New("invalid-number-of-args")
379 return nil, err
380 }
npujar1d86a522019-11-14 17:11:16 +0530381 deviceID := &voltha.ID{}
khenaidoo79232702018-12-04 11:00:41 -0500382 operStatus := &ic.IntType{}
383 connStatus := &ic.IntType{}
khenaidoo297cd252019-02-07 22:10:23 -0500384 transactionID := &ic.StrType{}
khenaidoob9203542018-09-17 22:56:37 -0400385 for _, arg := range args {
386 switch arg.Key {
387 case "device_id":
npujar1d86a522019-11-14 17:11:16 +0530388 if err := ptypes.UnmarshalAny(arg.Value, deviceID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000389 logger.Warnw("cannot-unmarshal-device-id", log.Fields{"error": err})
khenaidoob9203542018-09-17 22:56:37 -0400390 return nil, err
391 }
392 case "oper_status":
393 if err := ptypes.UnmarshalAny(arg.Value, operStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000394 logger.Warnw("cannot-unmarshal-operStatus", log.Fields{"error": err})
khenaidoob9203542018-09-17 22:56:37 -0400395 return nil, err
396 }
khenaidoob9203542018-09-17 22:56:37 -0400397 case "connect_status":
398 if err := ptypes.UnmarshalAny(arg.Value, connStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000399 logger.Warnw("cannot-unmarshal-connStatus", log.Fields{"error": err})
khenaidoob9203542018-09-17 22:56:37 -0400400 return nil, err
401 }
khenaidoo297cd252019-02-07 22:10:23 -0500402 case kafka.TransactionKey:
403 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000404 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
khenaidoo297cd252019-02-07 22:10:23 -0500405 return nil, err
406 }
khenaidoob9203542018-09-17 22:56:37 -0400407 }
408 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000409 logger.Debugw("DeviceStateUpdate", log.Fields{"deviceID": deviceID.Id, "oper-status": operStatus,
khenaidoo297cd252019-02-07 22:10:23 -0500410 "conn-status": connStatus, "transactionID": transactionID.Val})
411
khenaidoo442e7c72020-03-10 16:13:48 -0400412 if err := rhp.deviceMgr.updateDeviceStatus(context.TODO(), deviceID.Id, voltha.OperStatus_Types(operStatus.Val),
413 voltha.ConnectStatus_Types(connStatus.Val)); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000414 logger.Debugw("unable-to-update-device-status", log.Fields{"error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400415 return nil, err
khenaidoob9203542018-09-17 22:56:37 -0400416 }
khenaidoo442e7c72020-03-10 16:13:48 -0400417 return &empty.Empty{}, nil
khenaidoo92e62c52018-10-03 14:02:54 -0400418}
419
npujar1d86a522019-11-14 17:11:16 +0530420// ChildrenStateUpdate updates child device status
khenaidoo79232702018-12-04 11:00:41 -0500421func (rhp *AdapterRequestHandlerProxy) ChildrenStateUpdate(args []*ic.Argument) (*empty.Empty, error) {
khenaidoo297cd252019-02-07 22:10:23 -0500422 if len(args) < 3 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000423 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
khenaidoo4d4802d2018-10-04 21:59:49 -0400424 err := errors.New("invalid-number-of-args")
425 return nil, err
426 }
npujar1d86a522019-11-14 17:11:16 +0530427 deviceID := &voltha.ID{}
khenaidoo79232702018-12-04 11:00:41 -0500428 operStatus := &ic.IntType{}
429 connStatus := &ic.IntType{}
khenaidoo297cd252019-02-07 22:10:23 -0500430 transactionID := &ic.StrType{}
khenaidoo4d4802d2018-10-04 21:59:49 -0400431 for _, arg := range args {
432 switch arg.Key {
433 case "device_id":
npujar1d86a522019-11-14 17:11:16 +0530434 if err := ptypes.UnmarshalAny(arg.Value, deviceID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000435 logger.Warnw("cannot-unmarshal-device-id", log.Fields{"error": err})
khenaidoo4d4802d2018-10-04 21:59:49 -0400436 return nil, err
437 }
438 case "oper_status":
439 if err := ptypes.UnmarshalAny(arg.Value, operStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000440 logger.Warnw("cannot-unmarshal-operStatus", log.Fields{"error": err})
khenaidoo4d4802d2018-10-04 21:59:49 -0400441 return nil, err
442 }
443 case "connect_status":
444 if err := ptypes.UnmarshalAny(arg.Value, connStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000445 logger.Warnw("cannot-unmarshal-connStatus", log.Fields{"error": err})
khenaidoo4d4802d2018-10-04 21:59:49 -0400446 return nil, err
447 }
khenaidoo297cd252019-02-07 22:10:23 -0500448 case kafka.TransactionKey:
449 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000450 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
khenaidoo297cd252019-02-07 22:10:23 -0500451 return nil, err
452 }
khenaidoo4d4802d2018-10-04 21:59:49 -0400453 }
454 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000455 logger.Debugw("ChildrenStateUpdate", log.Fields{"deviceID": deviceID.Id, "oper-status": operStatus,
khenaidoo297cd252019-02-07 22:10:23 -0500456 "conn-status": connStatus, "transactionID": transactionID.Val})
457
khenaidoo4d4802d2018-10-04 21:59:49 -0400458 // When the enum is not set (i.e. -1), Go still convert to the Enum type with the value being -1
khenaidoo442e7c72020-03-10 16:13:48 -0400459 if err := rhp.deviceMgr.updateChildrenStatus(context.TODO(), deviceID.Id, voltha.OperStatus_Types(operStatus.Val),
460 voltha.ConnectStatus_Types(connStatus.Val)); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000461 logger.Debugw("unable-to-update-children-status", log.Fields{"error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400462 return nil, err
463 }
464 return &empty.Empty{}, nil
khenaidoo4d4802d2018-10-04 21:59:49 -0400465}
466
npujar1d86a522019-11-14 17:11:16 +0530467// PortsStateUpdate updates the ports state related to the device
khenaidoo3ab34882019-05-02 21:33:30 -0400468func (rhp *AdapterRequestHandlerProxy) PortsStateUpdate(args []*ic.Argument) (*empty.Empty, error) {
469 if len(args) < 2 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000470 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
khenaidoo3ab34882019-05-02 21:33:30 -0400471 err := errors.New("invalid-number-of-args")
472 return nil, err
473 }
npujar1d86a522019-11-14 17:11:16 +0530474 deviceID := &voltha.ID{}
khenaidoo3ab34882019-05-02 21:33:30 -0400475 operStatus := &ic.IntType{}
476 transactionID := &ic.StrType{}
477 for _, arg := range args {
478 switch arg.Key {
479 case "device_id":
npujar1d86a522019-11-14 17:11:16 +0530480 if err := ptypes.UnmarshalAny(arg.Value, deviceID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000481 logger.Warnw("cannot-unmarshal-device-id", log.Fields{"error": err})
khenaidoo3ab34882019-05-02 21:33:30 -0400482 return nil, err
483 }
484 case "oper_status":
485 if err := ptypes.UnmarshalAny(arg.Value, operStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000486 logger.Warnw("cannot-unmarshal-operStatus", log.Fields{"error": err})
khenaidoo3ab34882019-05-02 21:33:30 -0400487 return nil, err
488 }
489 case kafka.TransactionKey:
490 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000491 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
khenaidoo3ab34882019-05-02 21:33:30 -0400492 return nil, err
493 }
494 }
495 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000496 logger.Debugw("PortsStateUpdate", log.Fields{"deviceID": deviceID.Id, "operStatus": operStatus, "transactionID": transactionID.Val})
khenaidoo3ab34882019-05-02 21:33:30 -0400497
khenaidoo442e7c72020-03-10 16:13:48 -0400498 if err := rhp.deviceMgr.updatePortsState(context.TODO(), deviceID.Id, voltha.OperStatus_Types(operStatus.Val)); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000499 logger.Debugw("unable-to-update-ports-state", log.Fields{"error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400500 return nil, err
khenaidoo3ab34882019-05-02 21:33:30 -0400501 }
khenaidoo442e7c72020-03-10 16:13:48 -0400502 return &empty.Empty{}, nil
khenaidoo3ab34882019-05-02 21:33:30 -0400503}
504
npujar1d86a522019-11-14 17:11:16 +0530505// PortStateUpdate updates the port state of the device
khenaidoo79232702018-12-04 11:00:41 -0500506func (rhp *AdapterRequestHandlerProxy) PortStateUpdate(args []*ic.Argument) (*empty.Empty, error) {
khenaidoo297cd252019-02-07 22:10:23 -0500507 if len(args) < 3 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000508 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
khenaidoo92e62c52018-10-03 14:02:54 -0400509 err := errors.New("invalid-number-of-args")
510 return nil, err
khenaidoob9203542018-09-17 22:56:37 -0400511 }
npujar1d86a522019-11-14 17:11:16 +0530512 deviceID := &voltha.ID{}
khenaidoo79232702018-12-04 11:00:41 -0500513 portType := &ic.IntType{}
514 portNo := &ic.IntType{}
515 operStatus := &ic.IntType{}
khenaidoo297cd252019-02-07 22:10:23 -0500516 transactionID := &ic.StrType{}
khenaidoo92e62c52018-10-03 14:02:54 -0400517 for _, arg := range args {
518 switch arg.Key {
519 case "device_id":
npujar1d86a522019-11-14 17:11:16 +0530520 if err := ptypes.UnmarshalAny(arg.Value, deviceID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000521 logger.Warnw("cannot-unmarshal-device-id", log.Fields{"error": err})
khenaidoo92e62c52018-10-03 14:02:54 -0400522 return nil, err
523 }
524 case "oper_status":
525 if err := ptypes.UnmarshalAny(arg.Value, operStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000526 logger.Warnw("cannot-unmarshal-operStatus", log.Fields{"error": err})
khenaidoo92e62c52018-10-03 14:02:54 -0400527 return nil, err
528 }
529 case "port_type":
530 if err := ptypes.UnmarshalAny(arg.Value, portType); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000531 logger.Warnw("cannot-unmarshal-porttype", log.Fields{"error": err})
khenaidoo92e62c52018-10-03 14:02:54 -0400532 return nil, err
533 }
534 case "port_no":
535 if err := ptypes.UnmarshalAny(arg.Value, portNo); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000536 logger.Warnw("cannot-unmarshal-portno", log.Fields{"error": err})
khenaidoo92e62c52018-10-03 14:02:54 -0400537 return nil, err
538 }
khenaidoo297cd252019-02-07 22:10:23 -0500539 case kafka.TransactionKey:
540 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000541 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
khenaidoo297cd252019-02-07 22:10:23 -0500542 return nil, err
543 }
khenaidoo92e62c52018-10-03 14:02:54 -0400544 }
545 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000546 logger.Debugw("PortStateUpdate", log.Fields{"deviceID": deviceID.Id, "operStatus": operStatus,
khenaidoo297cd252019-02-07 22:10:23 -0500547 "portType": portType, "portNo": portNo, "transactionID": transactionID.Val})
548
khenaidoo442e7c72020-03-10 16:13:48 -0400549 if err := rhp.deviceMgr.updatePortState(context.TODO(), deviceID.Id, voltha.Port_PortType(portType.Val), uint32(portNo.Val),
550 voltha.OperStatus_Types(operStatus.Val)); err != nil {
551 // If the error doesn't change behavior and is essentially ignored, it is not an error, it is a
552 // warning.
553 // TODO: VOL-2707
Girish Kumarf56a4682020-03-20 20:07:46 +0000554 logger.Debugw("unable-to-update-port-state", log.Fields{"error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400555 return nil, err
khenaidoo92e62c52018-10-03 14:02:54 -0400556 }
khenaidoo442e7c72020-03-10 16:13:48 -0400557 return &empty.Empty{}, nil
khenaidoob9203542018-09-17 22:56:37 -0400558}
559
npujar1d86a522019-11-14 17:11:16 +0530560// DeleteAllPorts deletes all ports of device
khenaidoo0a822f92019-05-08 15:15:57 -0400561func (rhp *AdapterRequestHandlerProxy) DeleteAllPorts(args []*ic.Argument) (*empty.Empty, error) {
562 if len(args) < 3 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000563 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
khenaidoo0a822f92019-05-08 15:15:57 -0400564 err := errors.New("invalid-number-of-args")
565 return nil, err
566 }
npujar1d86a522019-11-14 17:11:16 +0530567 deviceID := &voltha.ID{}
khenaidoo0a822f92019-05-08 15:15:57 -0400568 transactionID := &ic.StrType{}
569 for _, arg := range args {
570 switch arg.Key {
571 case "device_id":
npujar1d86a522019-11-14 17:11:16 +0530572 if err := ptypes.UnmarshalAny(arg.Value, deviceID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000573 logger.Warnw("cannot-unmarshal-device-id", log.Fields{"error": err})
khenaidoo0a822f92019-05-08 15:15:57 -0400574 return nil, err
575 }
576 case kafka.TransactionKey:
577 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000578 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
khenaidoo0a822f92019-05-08 15:15:57 -0400579 return nil, err
580 }
581 }
582 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000583 logger.Debugw("DeleteAllPorts", log.Fields{"deviceID": deviceID.Id, "transactionID": transactionID.Val})
khenaidoo0a822f92019-05-08 15:15:57 -0400584
khenaidoo442e7c72020-03-10 16:13:48 -0400585 if err := rhp.deviceMgr.deleteAllPorts(context.TODO(), deviceID.Id); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000586 logger.Debugw("unable-to-delete-ports", log.Fields{"error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400587 return nil, err
khenaidoo0a822f92019-05-08 15:15:57 -0400588 }
khenaidoo442e7c72020-03-10 16:13:48 -0400589 return &empty.Empty{}, nil
khenaidoo0a822f92019-05-08 15:15:57 -0400590}
591
npujar1d86a522019-11-14 17:11:16 +0530592// ChildDevicesLost indicates that a parent device is in a state (Disabled) where it cannot manage the child devices.
593// This will trigger the Core to disable all the child devices.
khenaidoo0a822f92019-05-08 15:15:57 -0400594func (rhp *AdapterRequestHandlerProxy) ChildDevicesLost(args []*ic.Argument) (*empty.Empty, error) {
595 if len(args) < 2 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000596 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
khenaidoo0a822f92019-05-08 15:15:57 -0400597 err := errors.New("invalid-number-of-args")
598 return nil, err
599 }
npujar1d86a522019-11-14 17:11:16 +0530600 parentDeviceID := &voltha.ID{}
khenaidoo0a822f92019-05-08 15:15:57 -0400601 transactionID := &ic.StrType{}
602 for _, arg := range args {
603 switch arg.Key {
604 case "parent_device_id":
npujar1d86a522019-11-14 17:11:16 +0530605 if err := ptypes.UnmarshalAny(arg.Value, parentDeviceID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000606 logger.Warnw("cannot-unmarshal-device-id", log.Fields{"error": err})
khenaidoo0a822f92019-05-08 15:15:57 -0400607 return nil, err
608 }
609 case kafka.TransactionKey:
610 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000611 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
khenaidoo0a822f92019-05-08 15:15:57 -0400612 return nil, err
613 }
614 }
615 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000616 logger.Debugw("ChildDevicesLost", log.Fields{"deviceID": parentDeviceID.Id, "transactionID": transactionID.Val})
khenaidoo0a822f92019-05-08 15:15:57 -0400617
khenaidoo442e7c72020-03-10 16:13:48 -0400618 if err := rhp.deviceMgr.childDevicesLost(context.TODO(), parentDeviceID.Id); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000619 logger.Debugw("unable-to-disable-child-devices", log.Fields{"error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400620 return nil, err
khenaidoo0a822f92019-05-08 15:15:57 -0400621 }
khenaidoo442e7c72020-03-10 16:13:48 -0400622 return &empty.Empty{}, nil
khenaidoo0a822f92019-05-08 15:15:57 -0400623}
624
npujar1d86a522019-11-14 17:11:16 +0530625// ChildDevicesDetected invoked by an adapter when child devices are found, typically after after a disable/enable sequence.
626// This will trigger the Core to Enable all the child devices of that parent.
khenaidoo0a822f92019-05-08 15:15:57 -0400627func (rhp *AdapterRequestHandlerProxy) ChildDevicesDetected(args []*ic.Argument) (*empty.Empty, error) {
628 if len(args) < 2 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000629 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
khenaidoo0a822f92019-05-08 15:15:57 -0400630 err := errors.New("invalid-number-of-args")
631 return nil, err
632 }
npujar1d86a522019-11-14 17:11:16 +0530633 parentDeviceID := &voltha.ID{}
khenaidoo0a822f92019-05-08 15:15:57 -0400634 transactionID := &ic.StrType{}
635 for _, arg := range args {
636 switch arg.Key {
637 case "parent_device_id":
npujar1d86a522019-11-14 17:11:16 +0530638 if err := ptypes.UnmarshalAny(arg.Value, parentDeviceID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000639 logger.Warnw("cannot-unmarshal-device-id", log.Fields{"error": err})
khenaidoo0a822f92019-05-08 15:15:57 -0400640 return nil, err
641 }
642 case kafka.TransactionKey:
643 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000644 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
khenaidoo0a822f92019-05-08 15:15:57 -0400645 return nil, err
646 }
647 }
648 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000649 logger.Debugw("ChildDevicesDetected", log.Fields{"deviceID": parentDeviceID.Id, "transactionID": transactionID.Val})
khenaidoo0a822f92019-05-08 15:15:57 -0400650
npujar467fe752020-01-16 20:17:45 +0530651 if err := rhp.deviceMgr.childDevicesDetected(context.TODO(), parentDeviceID.Id); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000652 logger.Debugw("child-devices-detection-failed", log.Fields{"parentID": parentDeviceID.Id, "error": err})
khenaidoo59ef7be2019-06-21 12:40:28 -0400653 return nil, err
654 }
khenaidoo442e7c72020-03-10 16:13:48 -0400655 return &empty.Empty{}, nil
khenaidoo0a822f92019-05-08 15:15:57 -0400656}
657
npujar1d86a522019-11-14 17:11:16 +0530658// PortCreated adds port to device
khenaidoo79232702018-12-04 11:00:41 -0500659func (rhp *AdapterRequestHandlerProxy) PortCreated(args []*ic.Argument) (*empty.Empty, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500660 if len(args) < 3 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000661 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
khenaidoob9203542018-09-17 22:56:37 -0400662 err := errors.New("invalid-number-of-args")
663 return nil, err
664 }
npujar1d86a522019-11-14 17:11:16 +0530665 deviceID := &voltha.ID{}
khenaidoob9203542018-09-17 22:56:37 -0400666 port := &voltha.Port{}
khenaidoo297cd252019-02-07 22:10:23 -0500667 transactionID := &ic.StrType{}
khenaidoob9203542018-09-17 22:56:37 -0400668 for _, arg := range args {
669 switch arg.Key {
670 case "device_id":
npujar1d86a522019-11-14 17:11:16 +0530671 if err := ptypes.UnmarshalAny(arg.Value, deviceID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000672 logger.Warnw("cannot-unmarshal-device-id", log.Fields{"error": err})
khenaidoob9203542018-09-17 22:56:37 -0400673 return nil, err
674 }
675 case "port":
676 if err := ptypes.UnmarshalAny(arg.Value, port); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000677 logger.Warnw("cannot-unmarshal-port", log.Fields{"error": err})
khenaidoob9203542018-09-17 22:56:37 -0400678 return nil, err
679 }
khenaidoo297cd252019-02-07 22:10:23 -0500680 case kafka.TransactionKey:
681 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000682 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
khenaidoo297cd252019-02-07 22:10:23 -0500683 return nil, err
684 }
khenaidoob9203542018-09-17 22:56:37 -0400685 }
686 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000687 logger.Debugw("PortCreated", log.Fields{"deviceID": deviceID.Id, "port": port, "transactionID": transactionID.Val})
khenaidoo297cd252019-02-07 22:10:23 -0500688
khenaidoo442e7c72020-03-10 16:13:48 -0400689 if err := rhp.deviceMgr.addPort(context.TODO(), deviceID.Id, port); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000690 logger.Debugw("unable-to-add-port", log.Fields{"error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400691 return nil, err
khenaidoob9203542018-09-17 22:56:37 -0400692 }
khenaidoo442e7c72020-03-10 16:13:48 -0400693 return &empty.Empty{}, nil
khenaidoob9203542018-09-17 22:56:37 -0400694}
695
npujar1d86a522019-11-14 17:11:16 +0530696// DevicePMConfigUpdate initializes the pm configs as defined by the adapter.
khenaidoo79232702018-12-04 11:00:41 -0500697func (rhp *AdapterRequestHandlerProxy) DevicePMConfigUpdate(args []*ic.Argument) (*empty.Empty, error) {
khenaidoob3127472019-07-24 21:04:55 -0400698 if len(args) < 2 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000699 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
khenaidoob9203542018-09-17 22:56:37 -0400700 err := errors.New("invalid-number-of-args")
701 return nil, err
702 }
703 pmConfigs := &voltha.PmConfigs{}
khenaidoo297cd252019-02-07 22:10:23 -0500704 transactionID := &ic.StrType{}
khenaidoob9203542018-09-17 22:56:37 -0400705 for _, arg := range args {
706 switch arg.Key {
707 case "device_pm_config":
708 if err := ptypes.UnmarshalAny(arg.Value, pmConfigs); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000709 logger.Warnw("cannot-unmarshal-pm-config", log.Fields{"error": err})
khenaidoob9203542018-09-17 22:56:37 -0400710 return nil, err
711 }
khenaidoo297cd252019-02-07 22:10:23 -0500712 case kafka.TransactionKey:
713 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000714 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
khenaidoo297cd252019-02-07 22:10:23 -0500715 return nil, err
716 }
khenaidoob9203542018-09-17 22:56:37 -0400717 }
718 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000719 logger.Debugw("DevicePMConfigUpdate", log.Fields{"deviceID": pmConfigs.Id, "configs": pmConfigs,
khenaidoob3127472019-07-24 21:04:55 -0400720 "transactionID": transactionID.Val})
khenaidoo297cd252019-02-07 22:10:23 -0500721
khenaidoo442e7c72020-03-10 16:13:48 -0400722 if err := rhp.deviceMgr.initPmConfigs(context.TODO(), pmConfigs.Id, pmConfigs); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000723 logger.Debugw("unable-to-initialize-pm-configs", log.Fields{"error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400724 return nil, err
khenaidoob9203542018-09-17 22:56:37 -0400725 }
khenaidoo442e7c72020-03-10 16:13:48 -0400726 return &empty.Empty{}, nil
khenaidoob9203542018-09-17 22:56:37 -0400727}
khenaidoofdbad6e2018-11-06 22:26:38 -0500728
npujar1d86a522019-11-14 17:11:16 +0530729// PacketIn sends the incoming packet of device
khenaidoo79232702018-12-04 11:00:41 -0500730func (rhp *AdapterRequestHandlerProxy) PacketIn(args []*ic.Argument) (*empty.Empty, error) {
khenaidoo297cd252019-02-07 22:10:23 -0500731 if len(args) < 4 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000732 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
khenaidoofdbad6e2018-11-06 22:26:38 -0500733 err := errors.New("invalid-number-of-args")
734 return nil, err
735 }
npujar1d86a522019-11-14 17:11:16 +0530736 deviceID := &voltha.ID{}
khenaidoo79232702018-12-04 11:00:41 -0500737 portNo := &ic.IntType{}
738 packet := &ic.Packet{}
khenaidoo297cd252019-02-07 22:10:23 -0500739 transactionID := &ic.StrType{}
khenaidoofdbad6e2018-11-06 22:26:38 -0500740 for _, arg := range args {
741 switch arg.Key {
742 case "device_id":
npujar1d86a522019-11-14 17:11:16 +0530743 if err := ptypes.UnmarshalAny(arg.Value, deviceID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000744 logger.Warnw("cannot-unmarshal-device-id", log.Fields{"error": err})
khenaidoofdbad6e2018-11-06 22:26:38 -0500745 return nil, err
746 }
747 case "port":
748 if err := ptypes.UnmarshalAny(arg.Value, portNo); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000749 logger.Warnw("cannot-unmarshal-port-no", log.Fields{"error": err})
khenaidoofdbad6e2018-11-06 22:26:38 -0500750 return nil, err
751 }
752 case "packet":
753 if err := ptypes.UnmarshalAny(arg.Value, packet); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000754 logger.Warnw("cannot-unmarshal-packet", log.Fields{"error": err})
khenaidoofdbad6e2018-11-06 22:26:38 -0500755 return nil, err
756 }
khenaidoo297cd252019-02-07 22:10:23 -0500757 case kafka.TransactionKey:
758 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000759 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
khenaidoo297cd252019-02-07 22:10:23 -0500760 return nil, err
761 }
khenaidoofdbad6e2018-11-06 22:26:38 -0500762 }
763 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000764 logger.Debugw("PacketIn", log.Fields{"deviceID": deviceID.Id, "port": portNo.Val, "packet": packet,
khenaidoo297cd252019-02-07 22:10:23 -0500765 "transactionID": transactionID.Val})
766
khenaidoo442e7c72020-03-10 16:13:48 -0400767 if err := rhp.deviceMgr.PacketIn(context.TODO(), deviceID.Id, uint32(portNo.Val), transactionID.Val, packet.Payload); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000768 logger.Debugw("unable-to-receive-packet-from-adapter", log.Fields{"error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400769 return nil, err
770
771 }
772 return &empty.Empty{}, nil
khenaidoofdbad6e2018-11-06 22:26:38 -0500773}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500774
npujar1d86a522019-11-14 17:11:16 +0530775// UpdateImageDownload updates image download
khenaidoof5a5bfa2019-01-23 22:20:29 -0500776func (rhp *AdapterRequestHandlerProxy) UpdateImageDownload(args []*ic.Argument) (*empty.Empty, error) {
777 if len(args) < 2 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000778 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
khenaidoof5a5bfa2019-01-23 22:20:29 -0500779 err := errors.New("invalid-number-of-args")
780 return nil, err
781 }
npujar1d86a522019-11-14 17:11:16 +0530782 deviceID := &voltha.ID{}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500783 img := &voltha.ImageDownload{}
khenaidoo297cd252019-02-07 22:10:23 -0500784 transactionID := &ic.StrType{}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500785 for _, arg := range args {
786 switch arg.Key {
787 case "device_id":
npujar1d86a522019-11-14 17:11:16 +0530788 if err := ptypes.UnmarshalAny(arg.Value, deviceID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000789 logger.Warnw("cannot-unmarshal-device-id", log.Fields{"error": err})
khenaidoof5a5bfa2019-01-23 22:20:29 -0500790 return nil, err
791 }
792 case "image_download":
793 if err := ptypes.UnmarshalAny(arg.Value, img); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000794 logger.Warnw("cannot-unmarshal-imgaeDownload", log.Fields{"error": err})
khenaidoof5a5bfa2019-01-23 22:20:29 -0500795 return nil, err
796 }
khenaidoo297cd252019-02-07 22:10:23 -0500797 case kafka.TransactionKey:
798 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000799 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
khenaidoo297cd252019-02-07 22:10:23 -0500800 return nil, err
801 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500802 }
803 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000804 logger.Debugw("UpdateImageDownload", log.Fields{"deviceID": deviceID.Id, "image-download": img,
khenaidoo297cd252019-02-07 22:10:23 -0500805 "transactionID": transactionID.Val})
806
khenaidoo442e7c72020-03-10 16:13:48 -0400807 if err := rhp.deviceMgr.updateImageDownload(context.TODO(), deviceID.Id, img); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000808 logger.Debugw("unable-to-update-image-download", log.Fields{"error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400809 return nil, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500810 }
khenaidoo442e7c72020-03-10 16:13:48 -0400811 return &empty.Empty{}, nil
khenaidoof5a5bfa2019-01-23 22:20:29 -0500812}
khenaidooba6b6c42019-08-02 09:11:56 -0400813
npujar1d86a522019-11-14 17:11:16 +0530814// ReconcileChildDevices reconciles child devices
khenaidooba6b6c42019-08-02 09:11:56 -0400815func (rhp *AdapterRequestHandlerProxy) ReconcileChildDevices(args []*ic.Argument) (*empty.Empty, error) {
816 if len(args) < 2 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000817 logger.Warn("invalid-number-of-args", log.Fields{"args": args})
khenaidooba6b6c42019-08-02 09:11:56 -0400818 err := errors.New("invalid-number-of-args")
819 return nil, err
820 }
npujar1d86a522019-11-14 17:11:16 +0530821 parentDeviceID := &voltha.ID{}
khenaidooba6b6c42019-08-02 09:11:56 -0400822 transactionID := &ic.StrType{}
823 for _, arg := range args {
824 switch arg.Key {
825 case "parent_device_id":
npujar1d86a522019-11-14 17:11:16 +0530826 if err := ptypes.UnmarshalAny(arg.Value, parentDeviceID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000827 logger.Warnw("cannot-unmarshal-device-id", log.Fields{"error": err})
khenaidooba6b6c42019-08-02 09:11:56 -0400828 return nil, err
829 }
830 case kafka.TransactionKey:
831 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000832 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
khenaidooba6b6c42019-08-02 09:11:56 -0400833 return nil, err
834 }
835 }
836 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000837 logger.Debugw("ReconcileChildDevices", log.Fields{"deviceID": parentDeviceID.Id, "transactionID": transactionID.Val})
khenaidooba6b6c42019-08-02 09:11:56 -0400838
khenaidoo442e7c72020-03-10 16:13:48 -0400839 if err := rhp.deviceMgr.reconcileChildDevices(context.TODO(), parentDeviceID.Id); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000840 logger.Debugw("unable-to-reconcile-child-devices", log.Fields{"error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400841 return nil, err
khenaidooba6b6c42019-08-02 09:11:56 -0400842 }
khenaidoo442e7c72020-03-10 16:13:48 -0400843 return &empty.Empty{}, nil
khenaidooba6b6c42019-08-02 09:11:56 -0400844}
Mahir Gunyelfdee9212019-10-16 16:52:21 -0700845
npujar1d86a522019-11-14 17:11:16 +0530846// DeviceReasonUpdate updates device reason
Mahir Gunyelfdee9212019-10-16 16:52:21 -0700847func (rhp *AdapterRequestHandlerProxy) DeviceReasonUpdate(args []*ic.Argument) (*empty.Empty, error) {
848 if len(args) < 2 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000849 logger.Warn("DeviceReasonUpdate: invalid-number-of-args", log.Fields{"args": args})
Mahir Gunyelfdee9212019-10-16 16:52:21 -0700850 err := errors.New("DeviceReasonUpdate: invalid-number-of-args")
851 return nil, err
852 }
npujar1d86a522019-11-14 17:11:16 +0530853 deviceID := &voltha.ID{}
Mahir Gunyelfdee9212019-10-16 16:52:21 -0700854 reason := &ic.StrType{}
855 transactionID := &ic.StrType{}
856 for _, arg := range args {
857 switch arg.Key {
858 case "device_id":
npujar1d86a522019-11-14 17:11:16 +0530859 if err := ptypes.UnmarshalAny(arg.Value, deviceID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000860 logger.Warnw("cannot-unmarshal-device-id", log.Fields{"error": err})
Mahir Gunyelfdee9212019-10-16 16:52:21 -0700861 return nil, err
862 }
863 case "device_reason":
864 if err := ptypes.UnmarshalAny(arg.Value, reason); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000865 logger.Warnw("cannot-unmarshal-reason", log.Fields{"error": err})
Mahir Gunyelfdee9212019-10-16 16:52:21 -0700866 return nil, err
867 }
868 case kafka.TransactionKey:
869 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000870 logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
Mahir Gunyelfdee9212019-10-16 16:52:21 -0700871 return nil, err
872 }
873 }
874 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000875 logger.Debugw("DeviceReasonUpdate", log.Fields{"deviceId": deviceID.Id, "reason": reason.Val,
Mahir Gunyelfdee9212019-10-16 16:52:21 -0700876 "transactionID": transactionID.Val})
877
khenaidoo442e7c72020-03-10 16:13:48 -0400878 if err := rhp.deviceMgr.updateDeviceReason(context.TODO(), deviceID.Id, reason.Val); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000879 logger.Debugw("unable-to-update-device-reason", log.Fields{"error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400880 return nil, err
881
Mahir Gunyelfdee9212019-10-16 16:52:21 -0700882 }
khenaidoo442e7c72020-03-10 16:13:48 -0400883 return &empty.Empty{}, nil
Mahir Gunyelfdee9212019-10-16 16:52:21 -0700884}