khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [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 | */ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 16 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "errors" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 22 | "reflect" |
| 23 | "runtime" |
| 24 | "sync" |
| 25 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 26 | "github.com/opencord/voltha-go/db/model" |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 27 | "github.com/opencord/voltha-go/rw_core/utils" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 28 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 29 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 30 | "github.com/opencord/voltha-lib-go/v3/pkg/probe" |
| 31 | ic "github.com/opencord/voltha-protos/v3/go/inter_container" |
| 32 | ofp "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 33 | "github.com/opencord/voltha-protos/v3/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 34 | "google.golang.org/grpc/codes" |
| 35 | "google.golang.org/grpc/status" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 36 | ) |
| 37 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 38 | // DeviceManager represent device manager attributes |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 39 | type DeviceManager struct { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 40 | deviceAgents sync.Map |
| 41 | rootDevices map[string]bool |
| 42 | lockRootDeviceMap sync.RWMutex |
| 43 | core *Core |
| 44 | adapterProxy *AdapterProxy |
| 45 | adapterMgr *AdapterManager |
| 46 | logicalDeviceMgr *LogicalDeviceManager |
| 47 | kafkaICProxy *kafka.InterContainerProxy |
| 48 | stateTransitions *TransitionMap |
| 49 | clusterDataProxy *model.Proxy |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 50 | coreInstanceID string |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 51 | exitChannel chan int |
| 52 | defaultTimeout int64 |
| 53 | devicesLoadingLock sync.RWMutex |
| 54 | deviceLoadingInProgress map[string][]chan int |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 55 | } |
| 56 | |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 57 | func newDeviceManager(core *Core) *DeviceManager { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 58 | var deviceMgr DeviceManager |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 59 | deviceMgr.core = core |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 60 | deviceMgr.exitChannel = make(chan int, 1) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 61 | deviceMgr.rootDevices = make(map[string]bool) |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 62 | deviceMgr.kafkaICProxy = core.kmp |
Kent Hagerman | a6d0c36 | 2019-07-30 12:50:21 -0400 | [diff] [blame] | 63 | deviceMgr.adapterProxy = NewAdapterProxy(core.kmp, core.config.CorePairTopic) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 64 | deviceMgr.coreInstanceID = core.instanceID |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 65 | deviceMgr.clusterDataProxy = core.clusterDataProxy |
| 66 | deviceMgr.adapterMgr = core.adapterMgr |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 67 | deviceMgr.lockRootDeviceMap = sync.RWMutex{} |
| 68 | deviceMgr.defaultTimeout = core.config.DefaultCoreTimeout |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 69 | deviceMgr.devicesLoadingLock = sync.RWMutex{} |
| 70 | deviceMgr.deviceLoadingInProgress = make(map[string][]chan int) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 71 | return &deviceMgr |
| 72 | } |
| 73 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 74 | func (dMgr *DeviceManager) start(ctx context.Context, logicalDeviceMgr *LogicalDeviceManager) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 75 | log.Info("starting-device-manager") |
| 76 | dMgr.logicalDeviceMgr = logicalDeviceMgr |
| 77 | dMgr.stateTransitions = NewTransitionMap(dMgr) |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 78 | probe.UpdateStatusFromContext(ctx, "device-manager", probe.ServiceStatusRunning) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 79 | log.Info("device-manager-started") |
| 80 | } |
| 81 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 82 | func (dMgr *DeviceManager) stop(ctx context.Context) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 83 | log.Info("stopping-device-manager") |
| 84 | dMgr.exitChannel <- 1 |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 85 | probe.UpdateStatusFromContext(ctx, "device-manager", probe.ServiceStatusStopped) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 86 | log.Info("device-manager-stopped") |
| 87 | } |
| 88 | |
| 89 | func sendResponse(ctx context.Context, ch chan interface{}, result interface{}) { |
| 90 | if ctx.Err() == nil { |
| 91 | // Returned response only of the ctx has not been cancelled/timeout/etc |
| 92 | // Channel is automatically closed when a context is Done |
| 93 | ch <- result |
| 94 | log.Debugw("sendResponse", log.Fields{"result": result}) |
| 95 | } else { |
| 96 | // Should the transaction be reverted back? |
| 97 | log.Debugw("sendResponse-context-error", log.Fields{"context-error": ctx.Err()}) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func (dMgr *DeviceManager) addDeviceAgentToMap(agent *DeviceAgent) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 102 | if _, exist := dMgr.deviceAgents.Load(agent.deviceID); !exist { |
| 103 | dMgr.deviceAgents.Store(agent.deviceID, agent) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 104 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 105 | dMgr.lockRootDeviceMap.Lock() |
| 106 | defer dMgr.lockRootDeviceMap.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 107 | dMgr.rootDevices[agent.deviceID] = agent.isRootdevice |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 108 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 109 | } |
| 110 | |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 111 | func (dMgr *DeviceManager) deleteDeviceAgentFromMap(agent *DeviceAgent) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 112 | dMgr.deviceAgents.Delete(agent.deviceID) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 113 | dMgr.lockRootDeviceMap.Lock() |
| 114 | defer dMgr.lockRootDeviceMap.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 115 | delete(dMgr.rootDevices, agent.deviceID) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 116 | } |
| 117 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 118 | // getDeviceAgent returns the agent managing the device. If the device is not in memory, it will loads it, if it exists |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 119 | func (dMgr *DeviceManager) getDeviceAgent(deviceID string) *DeviceAgent { |
| 120 | agent, ok := dMgr.deviceAgents.Load(deviceID) |
| 121 | if ok { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 122 | return agent.(*DeviceAgent) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 123 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 124 | // Try to load into memory - loading will also create the device agent and set the device ownership |
| 125 | err := dMgr.load(deviceID) |
| 126 | if err == nil { |
| 127 | agent, ok = dMgr.deviceAgents.Load(deviceID) |
| 128 | if !ok { |
| 129 | return nil |
| 130 | } |
| 131 | // Register this device for ownership tracking |
| 132 | go func() { |
| 133 | _, err = dMgr.core.deviceOwnership.OwnedByMe(&utils.DeviceID{ID: deviceID}) |
| 134 | if err != nil { |
| 135 | log.Errorw("unable-to-find-core-instance-active-owns-this-device", log.Fields{"error": err}) |
| 136 | } |
| 137 | }() |
| 138 | return agent.(*DeviceAgent) |
| 139 | } |
| 140 | //TODO: Change the return params to return an error as well |
| 141 | log.Errorw("loading-device-failed", log.Fields{"deviceId": deviceID, "error": err}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 142 | return nil |
| 143 | } |
| 144 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 145 | // listDeviceIdsFromMap returns the list of device IDs that are in memory |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 146 | func (dMgr *DeviceManager) listDeviceIdsFromMap() *voltha.IDs { |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 147 | result := &voltha.IDs{Items: make([]*voltha.ID, 0)} |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 148 | |
| 149 | dMgr.deviceAgents.Range(func(key, value interface{}) bool { |
| 150 | result.Items = append(result.Items, &voltha.ID{Id: key.(string)}) |
| 151 | return true |
| 152 | }) |
| 153 | |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 154 | return result |
| 155 | } |
| 156 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 157 | func (dMgr *DeviceManager) createDevice(ctx context.Context, device *voltha.Device, ch chan interface{}) { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 158 | deviceExist, err := dMgr.isParentDeviceExist(device) |
| 159 | if err != nil { |
| 160 | log.Errorf("Failed to fetch parent device info") |
| 161 | sendResponse(ctx, ch, err) |
| 162 | return |
| 163 | } |
| 164 | if deviceExist { |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 165 | log.Errorf("Device is Pre-provisioned already with same IP-Port or MAC Address") |
| 166 | sendResponse(ctx, ch, errors.New("Device is already pre-provisioned")) |
| 167 | return |
| 168 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 169 | log.Debugw("createDevice", log.Fields{"device": device, "aproxy": dMgr.adapterProxy}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 170 | |
khenaidoo | 5e677ae | 2019-02-28 17:26:29 -0500 | [diff] [blame] | 171 | // Ensure this device is set as root |
| 172 | device.Root = true |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 173 | // Create and start a device agent for that device |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 174 | agent := newDeviceAgent(dMgr.adapterProxy, device, dMgr, dMgr.clusterDataProxy, dMgr.defaultTimeout) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 175 | dMgr.addDeviceAgentToMap(agent) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 176 | device, err = agent.start(ctx, device) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 177 | if err != nil { |
| 178 | log.Errorf("Failed to start device") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 179 | sendResponse(ctx, ch, err) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 180 | return |
| 181 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 182 | |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 183 | sendResponse(ctx, ch, device) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | func (dMgr *DeviceManager) enableDevice(ctx context.Context, id *voltha.ID, ch chan interface{}) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 187 | log.Debugw("enableDevice", log.Fields{"deviceid": id}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 188 | var res interface{} |
| 189 | if agent := dMgr.getDeviceAgent(id.Id); agent != nil { |
| 190 | res = agent.enableDevice(ctx) |
| 191 | log.Debugw("EnableDevice-result", log.Fields{"result": res}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | sendResponse(ctx, ch, res) |
| 195 | } |
| 196 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 197 | func (dMgr *DeviceManager) disableDevice(ctx context.Context, id *voltha.ID, ch chan interface{}) { |
| 198 | log.Debugw("disableDevice", log.Fields{"deviceid": id}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 199 | var res interface{} |
| 200 | if agent := dMgr.getDeviceAgent(id.Id); agent != nil { |
| 201 | res = agent.disableDevice(ctx) |
| 202 | log.Debugw("disableDevice-result", log.Fields{"result": res}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 203 | } else { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 204 | res = status.Errorf(codes.NotFound, "%s", id.Id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 205 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 206 | |
| 207 | sendResponse(ctx, ch, res) |
| 208 | } |
| 209 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 210 | func (dMgr *DeviceManager) rebootDevice(ctx context.Context, id *voltha.ID, ch chan interface{}) { |
| 211 | log.Debugw("rebootDevice", log.Fields{"deviceid": id}) |
| 212 | var res interface{} |
| 213 | if agent := dMgr.getDeviceAgent(id.Id); agent != nil { |
| 214 | res = agent.rebootDevice(ctx) |
| 215 | log.Debugw("rebootDevice-result", log.Fields{"result": res}) |
| 216 | } else { |
| 217 | res = status.Errorf(codes.NotFound, "%s", id.Id) |
| 218 | } |
| 219 | sendResponse(ctx, ch, res) |
| 220 | } |
| 221 | |
| 222 | func (dMgr *DeviceManager) deleteDevice(ctx context.Context, id *voltha.ID, ch chan interface{}) { |
| 223 | log.Debugw("deleteDevice", log.Fields{"deviceid": id}) |
| 224 | var res interface{} |
| 225 | if agent := dMgr.getDeviceAgent(id.Id); agent != nil { |
| 226 | res = agent.deleteDevice(ctx) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 227 | log.Debugw("deleteDevice-result", log.Fields{"result": res}) |
| 228 | } else { |
| 229 | res = status.Errorf(codes.NotFound, "%s", id.Id) |
| 230 | } |
| 231 | sendResponse(ctx, ch, res) |
| 232 | } |
| 233 | |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 234 | // stopManagingDevice stops the management of the device as well as any of its reference device and logical device. |
| 235 | // This function is called only in the Core that does not own this device. In the Core that owns this device then a |
| 236 | // deletion deletion also includes removal of any reference of this device. |
| 237 | func (dMgr *DeviceManager) stopManagingDevice(id string) { |
| 238 | log.Infow("stopManagingDevice", log.Fields{"deviceId": id}) |
| 239 | if dMgr.IsDeviceInCache(id) { // Proceed only if an agent is present for this device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 240 | if root, _ := dMgr.IsRootDevice(id); root { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 241 | // stop managing the logical device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 242 | ldeviceID := dMgr.logicalDeviceMgr.stopManagingLogicalDeviceWithDeviceID(id) |
| 243 | if ldeviceID != "" { // Can happen if logical device agent was already stopped |
| 244 | err := dMgr.core.deviceOwnership.AbandonDevice(ldeviceID) |
| 245 | if err != nil { |
| 246 | log.Errorw("unable-to-abandon-the-device", log.Fields{"error": err}) |
| 247 | } |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 248 | } |
khenaidoo | 4908535 | 2020-01-13 19:15:43 -0500 | [diff] [blame] | 249 | // We do not need to stop the child devices as this is taken care by the state machine. |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 250 | } |
| 251 | if agent := dMgr.getDeviceAgent(id); agent != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 252 | agent.stop(context.TODO()) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 253 | dMgr.deleteDeviceAgentFromMap(agent) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 254 | // Abandon the device ownership |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 255 | err := dMgr.core.deviceOwnership.AbandonDevice(id) |
| 256 | if err != nil { |
| 257 | log.Errorw("unable-to-abandon-device", log.Fields{"error": err}) |
| 258 | } |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 263 | // RunPostDeviceDelete removes any reference of this device |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 264 | func (dMgr *DeviceManager) RunPostDeviceDelete(cDevice *voltha.Device) error { |
| 265 | log.Infow("RunPostDeviceDelete", log.Fields{"deviceId": cDevice.Id}) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 266 | dMgr.stopManagingDevice(cDevice.Id) |
| 267 | return nil |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 268 | } |
| 269 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 270 | // GetDevice will returns a device, either from memory or from the dB, if present |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 271 | func (dMgr *DeviceManager) GetDevice(id string) (*voltha.Device, error) { |
| 272 | log.Debugw("GetDevice", log.Fields{"deviceid": id}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 273 | if agent := dMgr.getDeviceAgent(id); agent != nil { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 274 | return agent.getDevice(), nil |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 275 | } |
| 276 | return nil, status.Errorf(codes.NotFound, "%s", id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 277 | } |
| 278 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 279 | // GetChildDevice will return a device, either from memory or from the dB, if present |
| 280 | func (dMgr *DeviceManager) GetChildDevice(parentDeviceID string, serialNumber string, onuID int64, parentPortNo int64) (*voltha.Device, error) { |
| 281 | log.Debugw("GetChildDevice", log.Fields{"parentDeviceid": parentDeviceID, "serialNumber": serialNumber, |
| 282 | "parentPortNo": parentPortNo, "onuId": onuID}) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 283 | |
| 284 | var parentDevice *voltha.Device |
| 285 | var err error |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 286 | if parentDevice, err = dMgr.GetDevice(parentDeviceID); err != nil { |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 287 | return nil, status.Errorf(codes.Aborted, "%s", err.Error()) |
| 288 | } |
| 289 | var childDeviceIds []string |
| 290 | if childDeviceIds, err = dMgr.getAllChildDeviceIds(parentDevice); err != nil { |
| 291 | return nil, status.Errorf(codes.Aborted, "%s", err.Error()) |
| 292 | } |
| 293 | if len(childDeviceIds) == 0 { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 294 | log.Debugw("no-child-devices", log.Fields{"parentDeviceId": parentDevice.Id, "serialNumber": serialNumber, "onuId": onuID}) |
| 295 | return nil, status.Errorf(codes.NotFound, "%s", parentDeviceID) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | var foundChildDevice *voltha.Device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 299 | for _, childDeviceID := range childDeviceIds { |
| 300 | var found bool |
| 301 | if searchDevice, err := dMgr.GetDevice(childDeviceID); err == nil { |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 302 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 303 | foundOnuID := false |
| 304 | if searchDevice.ProxyAddress.OnuId == uint32(onuID) { |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 305 | if searchDevice.ParentPortNo == uint32(parentPortNo) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 306 | log.Debugw("found-child-by-onuid", log.Fields{"parentDeviceId": parentDevice.Id, "onuId": onuID}) |
| 307 | foundOnuID = true |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | foundSerialNumber := false |
| 312 | if searchDevice.SerialNumber == serialNumber { |
| 313 | log.Debugw("found-child-by-serialnumber", log.Fields{"parentDeviceId": parentDevice.Id, "serialNumber": serialNumber}) |
| 314 | foundSerialNumber = true |
| 315 | } |
| 316 | |
| 317 | // if both onuId and serialNumber are provided both must be true for the device to be found |
| 318 | // otherwise whichever one found a match is good enough |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 319 | if onuID > 0 && serialNumber != "" { |
| 320 | found = foundOnuID && foundSerialNumber |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 321 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 322 | found = foundOnuID || foundSerialNumber |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 323 | } |
| 324 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 325 | if found { |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 326 | foundChildDevice = searchDevice |
| 327 | break |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | if foundChildDevice != nil { |
| 333 | log.Debugw("child-device-found", log.Fields{"parentDeviceId": parentDevice.Id, "foundChildDevice": foundChildDevice}) |
| 334 | return foundChildDevice, nil |
| 335 | } |
| 336 | |
| 337 | log.Warnw("child-device-not-found", log.Fields{"parentDeviceId": parentDevice.Id, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 338 | "serialNumber": serialNumber, "onuId": onuID, "parentPortNo": parentPortNo}) |
| 339 | return nil, status.Errorf(codes.NotFound, "%s", parentDeviceID) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 340 | } |
| 341 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 342 | // GetChildDeviceWithProxyAddress will return a device based on proxy address |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 343 | func (dMgr *DeviceManager) GetChildDeviceWithProxyAddress(proxyAddress *voltha.Device_ProxyAddress) (*voltha.Device, error) { |
| 344 | log.Debugw("GetChildDeviceWithProxyAddress", log.Fields{"proxyAddress": proxyAddress}) |
| 345 | |
| 346 | var parentDevice *voltha.Device |
| 347 | var err error |
| 348 | if parentDevice, err = dMgr.GetDevice(proxyAddress.DeviceId); err != nil { |
| 349 | return nil, status.Errorf(codes.Aborted, "%s", err.Error()) |
| 350 | } |
| 351 | var childDeviceIds []string |
| 352 | if childDeviceIds, err = dMgr.getAllChildDeviceIds(parentDevice); err != nil { |
| 353 | return nil, status.Errorf(codes.Aborted, "%s", err.Error()) |
| 354 | } |
| 355 | if len(childDeviceIds) == 0 { |
| 356 | log.Debugw("no-child-devices", log.Fields{"parentDeviceId": parentDevice.Id}) |
| 357 | return nil, status.Errorf(codes.NotFound, "%s", proxyAddress) |
| 358 | } |
| 359 | |
| 360 | var foundChildDevice *voltha.Device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 361 | for _, childDeviceID := range childDeviceIds { |
| 362 | if searchDevice, err := dMgr.GetDevice(childDeviceID); err == nil { |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 363 | if searchDevice.ProxyAddress == proxyAddress { |
| 364 | foundChildDevice = searchDevice |
| 365 | break |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | if foundChildDevice != nil { |
| 371 | log.Debugw("child-device-found", log.Fields{"proxyAddress": proxyAddress}) |
| 372 | return foundChildDevice, nil |
| 373 | } |
| 374 | |
| 375 | log.Warnw("child-device-not-found", log.Fields{"proxyAddress": proxyAddress}) |
| 376 | return nil, status.Errorf(codes.NotFound, "%s", proxyAddress) |
| 377 | } |
| 378 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 379 | // IsDeviceInCache returns true if device is found in the map |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 380 | func (dMgr *DeviceManager) IsDeviceInCache(id string) bool { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 381 | _, exist := dMgr.deviceAgents.Load(id) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 382 | return exist |
| 383 | } |
| 384 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 385 | // IsRootDevice returns true if root device is found in the map |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 386 | func (dMgr *DeviceManager) IsRootDevice(id string) (bool, error) { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 387 | dMgr.lockRootDeviceMap.RLock() |
| 388 | defer dMgr.lockRootDeviceMap.RUnlock() |
| 389 | if exist := dMgr.rootDevices[id]; exist { |
| 390 | return dMgr.rootDevices[id], nil |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 391 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 392 | return false, nil |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 393 | } |
| 394 | |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 395 | // ListDevices retrieves the latest devices from the data model |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 396 | func (dMgr *DeviceManager) ListDevices() (*voltha.Devices, error) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 397 | log.Debug("ListDevices") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 398 | result := &voltha.Devices{} |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 399 | devices, err := dMgr.clusterDataProxy.List(context.Background(), "/devices", 0, false, "") |
| 400 | if err != nil { |
| 401 | log.Errorw("failed-to-list-devices-from-cluster-proxy", log.Fields{"error": err}) |
| 402 | return nil, err |
| 403 | } |
| 404 | if devices != nil { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 405 | for _, device := range devices.([]interface{}) { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 406 | // If device is not in memory then set it up |
| 407 | if !dMgr.IsDeviceInCache(device.(*voltha.Device).Id) { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 408 | log.Debugw("loading-device-from-Model", log.Fields{"id": device.(*voltha.Device).Id}) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 409 | agent := newDeviceAgent(dMgr.adapterProxy, device.(*voltha.Device), dMgr, dMgr.clusterDataProxy, dMgr.defaultTimeout) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 410 | if _, err := agent.start(context.TODO(), nil); err != nil { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 411 | log.Warnw("failure-starting-agent", log.Fields{"deviceId": device.(*voltha.Device).Id}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 412 | agent.stop(context.TODO()) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 413 | } else { |
| 414 | dMgr.addDeviceAgentToMap(agent) |
| 415 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 416 | } |
| 417 | result.Items = append(result.Items, device.(*voltha.Device)) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 418 | } |
| 419 | } |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 420 | log.Debugw("ListDevices-end", log.Fields{"len": len(result.Items)}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 421 | return result, nil |
| 422 | } |
| 423 | |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 424 | //isParentDeviceExist checks whether device is already preprovisioned. |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 425 | func (dMgr *DeviceManager) isParentDeviceExist(newDevice *voltha.Device) (bool, error) { |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 426 | hostPort := newDevice.GetHostAndPort() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 427 | devices, err := dMgr.clusterDataProxy.List(context.Background(), "/devices", 0, false, "") |
| 428 | if err != nil { |
| 429 | log.Errorw("Failed to list devices from cluster data proxy", log.Fields{"error": err}) |
| 430 | return false, err |
| 431 | } |
| 432 | if devices != nil { |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 433 | for _, device := range devices.([]interface{}) { |
| 434 | if !device.(*voltha.Device).Root { |
| 435 | continue |
| 436 | } |
| 437 | if hostPort != "" && hostPort == device.(*voltha.Device).GetHostAndPort() { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 438 | return true, nil |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 439 | } |
| 440 | if newDevice.MacAddress != "" && newDevice.MacAddress == device.(*voltha.Device).MacAddress { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 441 | return true, nil |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | } |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 445 | return false, nil |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 446 | } |
| 447 | |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 448 | //getDeviceFromModelretrieves the device data from the model. |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 449 | func (dMgr *DeviceManager) getDeviceFromModel(deviceID string) (*voltha.Device, error) { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 450 | device, err := dMgr.clusterDataProxy.Get(context.Background(), "/devices/"+deviceID, 0, false, "") |
| 451 | if err != nil { |
| 452 | log.Errorw("failed-to-get-device-info-from-cluster-proxy", log.Fields{"error": err}) |
| 453 | return nil, err |
| 454 | } |
| 455 | if device != nil { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 456 | if d, ok := device.(*voltha.Device); ok { |
| 457 | return d, nil |
| 458 | } |
| 459 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 460 | return nil, status.Error(codes.NotFound, deviceID) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 461 | } |
| 462 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 463 | // loadDevice loads the deviceID in memory, if not present |
| 464 | func (dMgr *DeviceManager) loadDevice(deviceID string) (*DeviceAgent, error) { |
| 465 | if deviceID == "" { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 466 | return nil, status.Error(codes.InvalidArgument, "deviceId empty") |
| 467 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 468 | var err error |
| 469 | var device *voltha.Device |
| 470 | dMgr.devicesLoadingLock.Lock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 471 | if _, exist := dMgr.deviceLoadingInProgress[deviceID]; !exist { |
| 472 | if !dMgr.IsDeviceInCache(deviceID) { |
| 473 | dMgr.deviceLoadingInProgress[deviceID] = []chan int{make(chan int, 1)} |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 474 | dMgr.devicesLoadingLock.Unlock() |
| 475 | // Proceed with the loading only if the device exist in the Model (could have been deleted) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 476 | if device, err = dMgr.getDeviceFromModel(deviceID); err == nil { |
| 477 | log.Debugw("loading-device", log.Fields{"deviceId": deviceID}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 478 | agent := newDeviceAgent(dMgr.adapterProxy, device, dMgr, dMgr.clusterDataProxy, dMgr.defaultTimeout) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 479 | if _, err = agent.start(context.TODO(), nil); err != nil { |
| 480 | log.Warnw("Failure loading device", log.Fields{"deviceId": deviceID, "error": err}) |
| 481 | agent.stop(context.TODO()) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 482 | } else { |
| 483 | dMgr.addDeviceAgentToMap(agent) |
| 484 | } |
| 485 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 486 | log.Debugw("Device not in model", log.Fields{"deviceId": deviceID}) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 487 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 488 | // announce completion of task to any number of waiting channels |
| 489 | dMgr.devicesLoadingLock.Lock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 490 | if v, ok := dMgr.deviceLoadingInProgress[deviceID]; ok { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 491 | for _, ch := range v { |
| 492 | close(ch) |
| 493 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 494 | delete(dMgr.deviceLoadingInProgress, deviceID) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 495 | } |
| 496 | dMgr.devicesLoadingLock.Unlock() |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 497 | } else { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 498 | dMgr.devicesLoadingLock.Unlock() |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 499 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 500 | } else { |
| 501 | ch := make(chan int, 1) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 502 | dMgr.deviceLoadingInProgress[deviceID] = append(dMgr.deviceLoadingInProgress[deviceID], ch) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 503 | dMgr.devicesLoadingLock.Unlock() |
| 504 | // Wait for the channel to be closed, implying the process loading this device is done. |
| 505 | <-ch |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 506 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 507 | if agent, ok := dMgr.deviceAgents.Load(deviceID); ok { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 508 | return agent.(*DeviceAgent), nil |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 509 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 510 | return nil, status.Errorf(codes.Aborted, "Error loading device %s", deviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | // loadRootDeviceParentAndChildren loads the children and parents of a root device in memory |
| 514 | func (dMgr *DeviceManager) loadRootDeviceParentAndChildren(device *voltha.Device) error { |
| 515 | log.Debugw("loading-parent-and-children", log.Fields{"deviceId": device.Id}) |
| 516 | if device.Root { |
| 517 | // Scenario A |
| 518 | if device.ParentId != "" { |
| 519 | // Load logical device if needed. |
| 520 | if err := dMgr.logicalDeviceMgr.load(device.ParentId); err != nil { |
| 521 | log.Warnw("failure-loading-logical-device", log.Fields{"lDeviceId": device.ParentId}) |
| 522 | } |
| 523 | } else { |
| 524 | log.Debugw("no-parent-to-load", log.Fields{"deviceId": device.Id}) |
| 525 | } |
| 526 | // Load all child devices, if needed |
| 527 | if childDeviceIds, err := dMgr.getAllChildDeviceIds(device); err == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 528 | for _, childDeviceID := range childDeviceIds { |
| 529 | if _, err := dMgr.loadDevice(childDeviceID); err != nil { |
| 530 | log.Warnw("failure-loading-device", log.Fields{"deviceId": childDeviceID, "error": err}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 531 | return err |
| 532 | } |
| 533 | } |
| 534 | log.Debugw("loaded-children", log.Fields{"deviceId": device.Id, "numChildren": len(childDeviceIds)}) |
| 535 | } else { |
| 536 | log.Debugw("no-child-to-load", log.Fields{"deviceId": device.Id}) |
| 537 | } |
| 538 | } |
| 539 | return nil |
| 540 | } |
| 541 | |
| 542 | // load loads the deviceId in memory, if not present, and also loads its accompanying parents and children. Loading |
| 543 | // in memory is for improved performance. It is not imperative that a device needs to be in memory when a request |
| 544 | // acting on the device is received by the core. In such a scenario, the Core will load the device in memory first |
| 545 | // and the proceed with the request. |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 546 | func (dMgr *DeviceManager) load(deviceID string) error { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 547 | log.Debug("load...") |
| 548 | // First load the device - this may fail in case the device was deleted intentionally by the other core |
| 549 | var dAgent *DeviceAgent |
| 550 | var err error |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 551 | if dAgent, err = dMgr.loadDevice(deviceID); err != nil { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 552 | return err |
| 553 | } |
| 554 | // Get the loaded device details |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 555 | device := dAgent.getDevice() |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 556 | |
| 557 | // If the device is in Pre-provisioning or deleted state stop here |
| 558 | if device.AdminState == voltha.AdminState_PREPROVISIONED || device.AdminState == voltha.AdminState_DELETED { |
| 559 | return nil |
| 560 | } |
| 561 | |
| 562 | // Now we face two scenarios |
| 563 | if device.Root { |
| 564 | // Load all children as well as the parent of this device (logical_device) |
| 565 | if err := dMgr.loadRootDeviceParentAndChildren(device); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 566 | log.Warnw("failure-loading-device-parent-and-children", log.Fields{"deviceId": deviceID}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 567 | return err |
| 568 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 569 | log.Debugw("successfully-loaded-parent-and-children", log.Fields{"deviceId": deviceID}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 570 | } else { |
| 571 | // Scenario B - use the parentId of that device (root device) to trigger the loading |
| 572 | if device.ParentId != "" { |
| 573 | return dMgr.load(device.ParentId) |
| 574 | } |
| 575 | } |
| 576 | return nil |
| 577 | } |
| 578 | |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 579 | // ListDeviceIds retrieves the latest device IDs information from the data model (memory data only) |
| 580 | func (dMgr *DeviceManager) ListDeviceIds() (*voltha.IDs, error) { |
| 581 | log.Debug("ListDeviceIDs") |
| 582 | // Report only device IDs that are in the device agent map |
| 583 | return dMgr.listDeviceIdsFromMap(), nil |
| 584 | } |
| 585 | |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 586 | //ReconcileDevices is a request to a voltha core to update its list of managed devices. This will |
| 587 | //trigger loading the devices along with their children and parent in memory |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 588 | func (dMgr *DeviceManager) ReconcileDevices(ctx context.Context, ids *voltha.IDs, ch chan interface{}) { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 589 | log.Debugw("ReconcileDevices", log.Fields{"numDevices": len(ids.Items)}) |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 590 | var res interface{} |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 591 | if ids != nil && len(ids.Items) != 0 { |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 592 | toReconcile := len(ids.Items) |
| 593 | reconciled := 0 |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 594 | var err error |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 595 | for _, id := range ids.Items { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 596 | if err = dMgr.load(id.Id); err != nil { |
| 597 | log.Warnw("failure-reconciling-device", log.Fields{"deviceId": id.Id, "error": err}) |
| 598 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 599 | reconciled++ |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | if toReconcile != reconciled { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 603 | res = status.Errorf(codes.DataLoss, "less-device-reconciled-than-requested:%d/%d", reconciled, toReconcile) |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 604 | } |
| 605 | } else { |
| 606 | res = status.Errorf(codes.InvalidArgument, "empty-list-of-ids") |
| 607 | } |
| 608 | sendResponse(ctx, ch, res) |
| 609 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 610 | |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 611 | // isOkToReconcile validates whether a device is in the correct status to be reconciled |
| 612 | func isOkToReconcile(device *voltha.Device) bool { |
| 613 | if device == nil { |
| 614 | return false |
| 615 | } |
| 616 | return device.AdminState != voltha.AdminState_PREPROVISIONED && device.AdminState != voltha.AdminState_DELETED |
| 617 | } |
| 618 | |
| 619 | // adapterRestarted is invoked whenever an adapter is restarted |
| 620 | func (dMgr *DeviceManager) adapterRestarted(adapter *voltha.Adapter) error { |
| 621 | log.Debugw("adapter-restarted", log.Fields{"adapter": adapter.Id}) |
| 622 | |
| 623 | // Let's reconcile the device managed by this Core only |
| 624 | rootDeviceIds := dMgr.core.deviceOwnership.GetAllDeviceIdsOwnedByMe() |
| 625 | if len(rootDeviceIds) == 0 { |
| 626 | log.Debugw("nothing-to-reconcile", log.Fields{"adapterId": adapter.Id}) |
| 627 | return nil |
| 628 | } |
| 629 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 630 | responses := make([]utils.Response, 0) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 631 | for _, rootDeviceID := range rootDeviceIds { |
| 632 | if rootDevice, _ := dMgr.getDeviceFromModel(rootDeviceID); rootDevice != nil { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 633 | if rootDevice.Adapter == adapter.Id { |
| 634 | if isOkToReconcile(rootDevice) { |
| 635 | log.Debugw("reconciling-root-device", log.Fields{"rootId": rootDevice.Id}) |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 636 | responses = append(responses, dMgr.sendReconcileDeviceRequest(rootDevice)) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 637 | } else { |
| 638 | log.Debugw("not-reconciling-root-device", log.Fields{"rootId": rootDevice.Id, "state": rootDevice.AdminState}) |
| 639 | } |
| 640 | } else { // Should we be reconciling the root's children instead? |
| 641 | childManagedByAdapter: |
| 642 | for _, port := range rootDevice.Ports { |
| 643 | for _, peer := range port.Peers { |
| 644 | if childDevice, _ := dMgr.getDeviceFromModel(peer.DeviceId); childDevice != nil { |
| 645 | if childDevice.Adapter == adapter.Id { |
| 646 | if isOkToReconcile(childDevice) { |
| 647 | log.Debugw("reconciling-child-device", log.Fields{"childId": childDevice.Id}) |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 648 | responses = append(responses, dMgr.sendReconcileDeviceRequest(childDevice)) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 649 | } else { |
| 650 | log.Debugw("not-reconciling-child-device", log.Fields{"childId": childDevice.Id, "state": childDevice.AdminState}) |
| 651 | } |
| 652 | } else { |
| 653 | // All child devices under a parent device are typically managed by the same adapter type. |
| 654 | // Therefore we only need to check whether the first device we retrieved is managed by that adapter |
| 655 | break childManagedByAdapter |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 663 | if len(responses) > 0 { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 664 | // Wait for completion |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 665 | if res := utils.WaitForNilOrErrorResponses(dMgr.defaultTimeout, responses...); res != nil { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 666 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 667 | } |
| 668 | } else { |
| 669 | log.Debugw("no-managed-device-to-reconcile", log.Fields{"adapterId": adapter.Id}) |
| 670 | } |
| 671 | return nil |
| 672 | } |
| 673 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 674 | func (dMgr *DeviceManager) sendReconcileDeviceRequest(device *voltha.Device) utils.Response { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 675 | // Send a reconcile request to the adapter. Since this Core may not be managing this device then there is no |
| 676 | // point of creating a device agent (if the device is not being managed by this Core) before sending the request |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 677 | // to the adapter. We will therefore bypass the adapter adapter and send the request directly to the adapter via |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 678 | // the adapter_proxy. |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 679 | response := utils.NewResponse() |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 680 | go func(device *voltha.Device) { |
| 681 | if err := dMgr.adapterProxy.ReconcileDevice(context.Background(), device); err != nil { |
| 682 | log.Errorw("reconcile-request-failed", log.Fields{"deviceId": device.Id, "error": err}) |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 683 | response.Error(status.Errorf(codes.Internal, "device: %s", device.Id)) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 684 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 685 | response.Done() |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 686 | }(device) |
| 687 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 688 | return response |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 689 | } |
| 690 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 691 | func (dMgr *DeviceManager) reconcileChildDevices(parentDeviceID string) error { |
| 692 | if parentDevice, _ := dMgr.getDeviceFromModel(parentDeviceID); parentDevice != nil { |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 693 | responses := make([]utils.Response, 0) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 694 | for _, port := range parentDevice.Ports { |
| 695 | for _, peer := range port.Peers { |
| 696 | if childDevice, _ := dMgr.getDeviceFromModel(peer.DeviceId); childDevice != nil { |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 697 | responses = append(responses, dMgr.sendReconcileDeviceRequest(childDevice)) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | } |
| 701 | // Wait for completion |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 702 | if res := utils.WaitForNilOrErrorResponses(dMgr.defaultTimeout, responses...); res != nil { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 703 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 704 | } |
| 705 | } |
| 706 | return nil |
| 707 | } |
| 708 | |
Mahir Gunyel | 8e2707d | 2019-07-25 00:36:21 -0700 | [diff] [blame] | 709 | func (dMgr *DeviceManager) updateDeviceUsingAdapterData(device *voltha.Device) error { |
| 710 | log.Debugw("updateDeviceUsingAdapterData", log.Fields{"deviceid": device.Id, "device": device}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 711 | if agent := dMgr.getDeviceAgent(device.Id); agent != nil { |
Mahir Gunyel | 8e2707d | 2019-07-25 00:36:21 -0700 | [diff] [blame] | 712 | return agent.updateDeviceUsingAdapterData(device) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 713 | } |
| 714 | return status.Errorf(codes.NotFound, "%s", device.Id) |
| 715 | } |
| 716 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 717 | func (dMgr *DeviceManager) addPort(deviceID string, port *voltha.Port) error { |
| 718 | agent := dMgr.getDeviceAgent(deviceID) |
| 719 | if agent != nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 720 | if err := agent.addPort(port); err != nil { |
| 721 | return err |
| 722 | } |
| 723 | // Setup peer ports |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 724 | meAsPeer := &voltha.Port_PeerPort{DeviceId: deviceID, PortNo: port.PortNo} |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 725 | for _, peerPort := range port.Peers { |
| 726 | if agent := dMgr.getDeviceAgent(peerPort.DeviceId); agent != nil { |
| 727 | if err := agent.addPeerPort(meAsPeer); err != nil { |
| 728 | log.Errorw("failed-to-add-peer", log.Fields{"peer-device-id": peerPort.DeviceId}) |
| 729 | return err |
| 730 | } |
| 731 | } |
| 732 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 733 | // Notify the logical device manager to setup a logical port, if needed. If the added port is an NNI or UNI |
| 734 | // then a logical port will be added to the logical device and the device graph generated. If the port is a |
| 735 | // PON port then only the device graph will be generated. |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 736 | if device, err := dMgr.GetDevice(deviceID); err == nil { |
| 737 | go func() { |
| 738 | err = dMgr.logicalDeviceMgr.updateLogicalPort(device, port) |
| 739 | if err != nil { |
| 740 | log.Errorw("unable-to-update-logical-port", log.Fields{"error": err}) |
| 741 | } |
| 742 | }() |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 743 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 744 | log.Errorw("failed-to-retrieve-device", log.Fields{"deviceId": deviceID}) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 745 | return err |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 746 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 747 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 748 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 749 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 750 | } |
| 751 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 752 | func (dMgr *DeviceManager) deletePeerPorts(fromDeviceID string, deviceID string) error { |
| 753 | log.Debugw("deletePeerPorts", log.Fields{"fromDeviceId": fromDeviceID, "deviceid": deviceID}) |
| 754 | if agent := dMgr.getDeviceAgent(fromDeviceID); agent != nil { |
| 755 | return agent.deletePeerPorts(deviceID) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 756 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 757 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 758 | } |
| 759 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 760 | func (dMgr *DeviceManager) addFlowsAndGroups(deviceID string, flows []*ofp.OfpFlowStats, groups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
Esin Karaman | 09959ae | 2019-11-29 13:59:58 +0000 | [diff] [blame] | 761 | log.Debugw("addFlowsAndGroups", log.Fields{"deviceid": deviceID, "groups:": groups, "flowMetadata": flowMetadata}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 762 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 763 | return agent.addFlowsAndGroups(flows, groups, flowMetadata) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 764 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 765 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 766 | } |
| 767 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 768 | func (dMgr *DeviceManager) deleteFlowsAndGroups(deviceID string, flows []*ofp.OfpFlowStats, groups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
| 769 | log.Debugw("deleteFlowsAndGroups", log.Fields{"deviceid": deviceID}) |
| 770 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 771 | return agent.deleteFlowsAndGroups(flows, groups, flowMetadata) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 772 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 773 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 774 | } |
| 775 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 776 | func (dMgr *DeviceManager) updateFlowsAndGroups(deviceID string, flows []*ofp.OfpFlowStats, groups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
| 777 | log.Debugw("updateFlowsAndGroups", log.Fields{"deviceid": deviceID}) |
| 778 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 779 | return agent.updateFlowsAndGroups(flows, groups, flowMetadata) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 780 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 781 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 782 | } |
| 783 | |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 784 | // updatePmConfigs updates the PM configs. This is executed when the northbound gRPC API is invoked, typically |
| 785 | // following a user action |
| 786 | func (dMgr *DeviceManager) updatePmConfigs(ctx context.Context, pmConfigs *voltha.PmConfigs, ch chan interface{}) { |
| 787 | var res interface{} |
| 788 | if pmConfigs.Id == "" { |
| 789 | res = status.Errorf(codes.FailedPrecondition, "invalid-device-Id") |
| 790 | } else if agent := dMgr.getDeviceAgent(pmConfigs.Id); agent != nil { |
| 791 | res = agent.updatePmConfigs(ctx, pmConfigs) |
| 792 | } else { |
| 793 | res = status.Errorf(codes.NotFound, "%s", pmConfigs.Id) |
| 794 | } |
| 795 | sendResponse(ctx, ch, res) |
| 796 | } |
| 797 | |
| 798 | // initPmConfigs initialize the pm configs as defined by the adapter. |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 799 | func (dMgr *DeviceManager) initPmConfigs(deviceID string, pmConfigs *voltha.PmConfigs) error { |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 800 | if pmConfigs.Id == "" { |
| 801 | return status.Errorf(codes.FailedPrecondition, "invalid-device-Id") |
| 802 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 803 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 804 | return agent.initPmConfigs(pmConfigs) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 805 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 806 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 807 | } |
| 808 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 809 | func (dMgr *DeviceManager) listPmConfigs(ctx context.Context, deviceID string) (*voltha.PmConfigs, error) { |
| 810 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 811 | return agent.listPmConfigs(ctx) |
| 812 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 813 | return nil, status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 814 | } |
| 815 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 816 | func (dMgr *DeviceManager) getSwitchCapability(ctx context.Context, deviceID string) (*ic.SwitchCapability, error) { |
| 817 | log.Debugw("getSwitchCapability", log.Fields{"deviceid": deviceID}) |
| 818 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 819 | return agent.getSwitchCapability(ctx) |
| 820 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 821 | return nil, status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 822 | } |
| 823 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 824 | func (dMgr *DeviceManager) getPorts(ctx context.Context, deviceID string, portType voltha.Port_PortType) (*voltha.Ports, error) { |
| 825 | log.Debugw("getPorts", log.Fields{"deviceid": deviceID, "portType": portType}) |
| 826 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 827 | return agent.getPorts(ctx, portType), nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 828 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 829 | return nil, status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 830 | } |
| 831 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 832 | func (dMgr *DeviceManager) getPortCapability(ctx context.Context, deviceID string, portNo uint32) (*ic.PortCapability, error) { |
| 833 | log.Debugw("getPortCapability", log.Fields{"deviceid": deviceID}) |
| 834 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 835 | return agent.getPortCapability(ctx, portNo) |
| 836 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 837 | return nil, status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 838 | } |
| 839 | |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 840 | func (dMgr *DeviceManager) updateDeviceStatus(deviceID string, operStatus voltha.OperStatus_Types, connStatus voltha.ConnectStatus_Types) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 841 | log.Debugw("updateDeviceStatus", log.Fields{"deviceid": deviceID, "operStatus": operStatus, "connStatus": connStatus}) |
| 842 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 843 | return agent.updateDeviceStatus(operStatus, connStatus) |
| 844 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 845 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 846 | } |
| 847 | |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 848 | func (dMgr *DeviceManager) updateChildrenStatus(deviceID string, operStatus voltha.OperStatus_Types, connStatus voltha.ConnectStatus_Types) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 849 | log.Debugw("updateChildrenStatus", log.Fields{"parentDeviceid": deviceID, "operStatus": operStatus, "connStatus": connStatus}) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 850 | var parentDevice *voltha.Device |
| 851 | var err error |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 852 | if parentDevice, err = dMgr.GetDevice(deviceID); err != nil { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 853 | return status.Errorf(codes.Aborted, "%s", err.Error()) |
| 854 | } |
| 855 | var childDeviceIds []string |
| 856 | if childDeviceIds, err = dMgr.getAllChildDeviceIds(parentDevice); err != nil { |
| 857 | return status.Errorf(codes.Aborted, "%s", err.Error()) |
| 858 | } |
| 859 | if len(childDeviceIds) == 0 { |
| 860 | log.Debugw("no-child-device", log.Fields{"parentDeviceId": parentDevice.Id}) |
| 861 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 862 | for _, childDeviceID := range childDeviceIds { |
| 863 | if agent := dMgr.getDeviceAgent(childDeviceID); agent != nil { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 864 | if err = agent.updateDeviceStatus(operStatus, connStatus); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 865 | return status.Errorf(codes.Aborted, "childDevice:%s, error:%s", childDeviceID, err.Error()) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | } |
| 869 | return nil |
| 870 | } |
| 871 | |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 872 | func (dMgr *DeviceManager) updatePortState(deviceID string, portType voltha.Port_PortType, portNo uint32, operStatus voltha.OperStatus_Types) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 873 | log.Debugw("updatePortState", log.Fields{"deviceid": deviceID, "portType": portType, "portNo": portNo, "operStatus": operStatus}) |
| 874 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 875 | if err := agent.updatePortState(portType, portNo, operStatus); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 876 | log.Errorw("updating-port-state-failed", log.Fields{"deviceid": deviceID, "portNo": portNo, "error": err}) |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 877 | return err |
| 878 | } |
| 879 | // Notify the logical device manager to change the port state |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 880 | go func() { |
| 881 | err := dMgr.logicalDeviceMgr.updatePortState(deviceID, portNo, operStatus) |
| 882 | if err != nil { |
| 883 | log.Errorw("unable-to-update-port-state", log.Fields{"error": err}) |
| 884 | } |
| 885 | }() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 886 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 887 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 888 | } |
| 889 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 890 | func (dMgr *DeviceManager) deleteAllPorts(deviceID string) error { |
| 891 | log.Debugw("DeleteAllPorts", log.Fields{"deviceid": deviceID}) |
| 892 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 893 | if err := agent.deleteAllPorts(); err != nil { |
| 894 | return err |
| 895 | } |
| 896 | // Notify the logical device manager to remove all logical ports, if needed. |
| 897 | // At this stage the device itself may gave been deleted already at a deleteAllPorts |
| 898 | // typically is part of a device deletion phase. |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 899 | if device, err := dMgr.GetDevice(deviceID); err == nil { |
| 900 | go func() { |
| 901 | err = dMgr.logicalDeviceMgr.deleteAllLogicalPorts(device) |
| 902 | if err != nil { |
| 903 | log.Errorw("unable-to-delete-logical-ports", log.Fields{"error": err}) |
| 904 | } |
| 905 | }() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 906 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 907 | log.Warnw("failed-to-retrieve-device", log.Fields{"deviceId": deviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 908 | return err |
| 909 | } |
| 910 | return nil |
| 911 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 912 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 913 | } |
| 914 | |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 915 | //updatePortsState updates all ports on the device |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 916 | func (dMgr *DeviceManager) updatePortsState(deviceID string, state voltha.OperStatus_Types) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 917 | log.Debugw("updatePortsState", log.Fields{"deviceid": deviceID}) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 918 | |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 919 | var adminState voltha.AdminState_Types |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 920 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 921 | switch state { |
| 922 | case voltha.OperStatus_ACTIVE: |
| 923 | adminState = voltha.AdminState_ENABLED |
| 924 | if err := agent.enablePorts(); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 925 | log.Warnw("enable-all-ports-failed", log.Fields{"deviceId": deviceID, "error": err}) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 926 | return err |
| 927 | } |
| 928 | case voltha.OperStatus_UNKNOWN: |
| 929 | adminState = voltha.AdminState_DISABLED |
| 930 | if err := agent.disablePorts(); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 931 | log.Warnw("disable-all-ports-failed", log.Fields{"deviceId": deviceID, "error": err}) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 932 | return err |
| 933 | } |
| 934 | default: |
| 935 | return status.Error(codes.Unimplemented, "state-change-not-implemented") |
| 936 | } |
| 937 | // Notify the logical device about the state change |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 938 | device, err := dMgr.GetDevice(deviceID) |
| 939 | if err != nil { |
| 940 | log.Warnw("non-existent-device", log.Fields{"deviceId": deviceID, "error": err}) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 941 | return err |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 942 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 943 | if err := dMgr.logicalDeviceMgr.updatePortsState(device, adminState); err != nil { |
| 944 | log.Warnw("failed-updating-ports-state", log.Fields{"deviceId": deviceID, "error": err}) |
| 945 | return err |
| 946 | } |
| 947 | return nil |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 948 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 949 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 950 | } |
| 951 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 952 | func (dMgr *DeviceManager) childDeviceDetected(parentDeviceID string, parentPortNo int64, deviceType string, |
| 953 | channelID int64, vendorID string, serialNumber string, onuID int64) (*voltha.Device, error) { |
| 954 | log.Debugw("childDeviceDetected", log.Fields{"parentDeviceId": parentDeviceID, "parentPortNo": parentPortNo, "deviceType": deviceType, "channelId": channelID, "vendorId": vendorID, "serialNumber": serialNumber, "onuId": onuID}) |
Chaitrashree G S | 4b3fada | 2019-07-28 23:55:25 -0700 | [diff] [blame] | 955 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 956 | if deviceType == "" && vendorID != "" { |
Chaitrashree G S | 4b3fada | 2019-07-28 23:55:25 -0700 | [diff] [blame] | 957 | log.Debug("device-type-is-nil-fetching-device-type") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 958 | deviceTypesIf, err := dMgr.adapterMgr.clusterDataProxy.List(context.Background(), "/device_types", 0, false, "") |
| 959 | if err != nil { |
| 960 | log.Errorw("failed-to-get-device-type-info", log.Fields{"error": err}) |
| 961 | return nil, err |
| 962 | } |
| 963 | if deviceTypesIf != nil { |
Chaitrashree G S | 4b3fada | 2019-07-28 23:55:25 -0700 | [diff] [blame] | 964 | OLoop: |
| 965 | for _, deviceTypeIf := range deviceTypesIf.([]interface{}) { |
| 966 | if dType, ok := deviceTypeIf.(*voltha.DeviceType); ok { |
| 967 | for _, v := range dType.VendorIds { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 968 | if v == vendorID { |
Chaitrashree G S | 4b3fada | 2019-07-28 23:55:25 -0700 | [diff] [blame] | 969 | deviceType = dType.Adapter |
| 970 | break OLoop |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | //if no match found for the vendorid,report adapter with the custom error message |
| 978 | if deviceType == "" { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 979 | log.Errorw("failed-to-fetch-adapter-name ", log.Fields{"vendorId": vendorID}) |
| 980 | return nil, status.Errorf(codes.NotFound, "%s", vendorID) |
Chaitrashree G S | 4b3fada | 2019-07-28 23:55:25 -0700 | [diff] [blame] | 981 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 982 | |
| 983 | // Create the ONU device |
| 984 | childDevice := &voltha.Device{} |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 985 | childDevice.Type = deviceType |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 986 | childDevice.ParentId = parentDeviceID |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 987 | childDevice.ParentPortNo = uint32(parentPortNo) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 988 | childDevice.VendorId = vendorID |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 989 | childDevice.SerialNumber = serialNumber |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 990 | childDevice.Root = false |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 991 | |
| 992 | //Get parent device type |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 993 | parent, err := dMgr.GetDevice(parentDeviceID) |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 994 | if err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 995 | log.Error("no-parent-found", log.Fields{"parentId": parentDeviceID}) |
| 996 | return nil, status.Errorf(codes.NotFound, "%s", parentDeviceID) |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 997 | } |
| 998 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 999 | if device, err := dMgr.GetChildDevice(parentDeviceID, serialNumber, onuID, parentPortNo); err == nil { |
| 1000 | log.Warnw("child-device-exists", log.Fields{"parentId": parentDeviceID, "serialNumber": serialNumber}) |
Mahir Gunyel | 6deaa24 | 2019-06-27 04:53:33 -0700 | [diff] [blame] | 1001 | return device, status.Errorf(codes.AlreadyExists, "%s", serialNumber) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 1002 | } |
| 1003 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1004 | childDevice.ProxyAddress = &voltha.Device_ProxyAddress{DeviceId: parentDeviceID, DeviceType: parent.Type, ChannelId: uint32(channelID), OnuId: uint32(onuID)} |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1005 | |
| 1006 | // Create and start a device agent for that device |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1007 | agent := newDeviceAgent(dMgr.adapterProxy, childDevice, dMgr, dMgr.clusterDataProxy, dMgr.defaultTimeout) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1008 | dMgr.addDeviceAgentToMap(agent) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1009 | childDevice, err = agent.start(context.TODO(), childDevice) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 1010 | if err != nil { |
| 1011 | log.Error("error-starting-child") |
| 1012 | return nil, err |
| 1013 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1014 | |
khenaidoo | 09771ef | 2019-10-11 14:25:02 -0400 | [diff] [blame] | 1015 | // Since this Core has handled this request then it therefore owns this child device. Set the |
| 1016 | // ownership of this device to this Core |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1017 | _, err = dMgr.core.deviceOwnership.OwnedByMe(&utils.DeviceID{ID: agent.deviceID}) |
| 1018 | if err != nil { |
| 1019 | log.Errorw("unable-to-find-core-instance-active-owns-this-device", log.Fields{"error": err}) |
| 1020 | } |
khenaidoo | 09771ef | 2019-10-11 14:25:02 -0400 | [diff] [blame] | 1021 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1022 | // Activate the child device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1023 | if agent = dMgr.getDeviceAgent(agent.deviceID); agent != nil { |
| 1024 | go func() { |
| 1025 | err := agent.enableDevice(context.TODO()) |
| 1026 | if err != nil { |
| 1027 | log.Errorw("unable-to-enable-device", log.Fields{"error": err}) |
| 1028 | } |
| 1029 | }() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1030 | } |
| 1031 | |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 1032 | // Publish on the messaging bus that we have discovered new devices |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1033 | go func() { |
| 1034 | err := dMgr.kafkaICProxy.DeviceDiscovered(agent.deviceID, deviceType, parentDeviceID, dMgr.coreInstanceID) |
| 1035 | if err != nil { |
| 1036 | log.Errorw("unable-to-discover-the-device", log.Fields{"error": err}) |
| 1037 | } |
| 1038 | }() |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 1039 | |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 1040 | return childDevice, nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | func (dMgr *DeviceManager) processTransition(previous *voltha.Device, current *voltha.Device) error { |
| 1044 | // This will be triggered on every update to the device. |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1045 | handlers := dMgr.stateTransitions.GetTransitionHandler(previous, current) |
| 1046 | if handlers == nil { |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1047 | log.Debugw("no-op-transition", log.Fields{"deviceId": current.Id}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1048 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1049 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 1050 | log.Debugw("handler-found", log.Fields{"num-handlers": len(handlers), "isParent": current.Root, "current-data": current}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1051 | for _, handler := range handlers { |
| 1052 | log.Debugw("running-handler", log.Fields{"handler": funcName(handler)}) |
| 1053 | if err := handler(current); err != nil { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 1054 | log.Warnw("handler-failed", log.Fields{"handler": funcName(handler), "error": err}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1055 | return err |
| 1056 | } |
| 1057 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1058 | return nil |
| 1059 | } |
| 1060 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1061 | func (dMgr *DeviceManager) packetOut(deviceID string, outPort uint32, packet *ofp.OfpPacketOut) error { |
| 1062 | log.Debugw("packetOut", log.Fields{"deviceId": deviceID, "outPort": outPort}) |
| 1063 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1064 | return agent.packetOut(outPort, packet) |
| 1065 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1066 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1067 | } |
| 1068 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1069 | // PacketIn receives packet from adapter |
| 1070 | func (dMgr *DeviceManager) PacketIn(deviceID string, port uint32, transactionID string, packet []byte) error { |
| 1071 | log.Debugw("PacketIn", log.Fields{"deviceId": deviceID, "port": port}) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1072 | // Get the logical device Id based on the deviceId |
| 1073 | var device *voltha.Device |
| 1074 | var err error |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1075 | if device, err = dMgr.GetDevice(deviceID); err != nil { |
| 1076 | log.Errorw("device-not-found", log.Fields{"deviceId": deviceID}) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1077 | return err |
| 1078 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1079 | if !device.Root { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1080 | log.Errorw("device-not-root", log.Fields{"deviceId": deviceID}) |
| 1081 | return status.Errorf(codes.FailedPrecondition, "%s", deviceID) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1082 | } |
| 1083 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1084 | if err := dMgr.logicalDeviceMgr.packetIn(device.ParentId, port, transactionID, packet); err != nil { |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1085 | return err |
| 1086 | } |
| 1087 | return nil |
| 1088 | } |
| 1089 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1090 | func (dMgr *DeviceManager) setParentID(device *voltha.Device, parentID string) error { |
| 1091 | log.Debugw("setParentId", log.Fields{"deviceId": device.Id, "parentId": parentID}) |
khenaidoo | ad06fd7 | 2019-10-28 12:26:05 -0400 | [diff] [blame] | 1092 | if agent := dMgr.getDeviceAgent(device.Id); agent != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1093 | return agent.setParentID(device, parentID) |
khenaidoo | ad06fd7 | 2019-10-28 12:26:05 -0400 | [diff] [blame] | 1094 | } |
| 1095 | return status.Errorf(codes.NotFound, "%s", device.Id) |
| 1096 | } |
| 1097 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1098 | // CreateLogicalDevice creates logical device in core |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1099 | func (dMgr *DeviceManager) CreateLogicalDevice(cDevice *voltha.Device) error { |
| 1100 | log.Info("CreateLogicalDevice") |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1101 | // Verify whether the logical device has already been created |
| 1102 | if cDevice.ParentId != "" { |
| 1103 | log.Debugw("Parent device already exist.", log.Fields{"deviceId": cDevice.Id, "logicalDeviceId": cDevice.Id}) |
| 1104 | return nil |
| 1105 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1106 | var err error |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1107 | if _, err = dMgr.logicalDeviceMgr.createLogicalDevice(context.TODO(), cDevice); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1108 | log.Warnw("createlogical-device-error", log.Fields{"device": cDevice}) |
| 1109 | return err |
| 1110 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1111 | return nil |
| 1112 | } |
| 1113 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1114 | // DeleteLogicalDevice deletes logical device from core |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1115 | func (dMgr *DeviceManager) DeleteLogicalDevice(cDevice *voltha.Device) error { |
| 1116 | log.Info("DeleteLogicalDevice") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1117 | var err error |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1118 | if err = dMgr.logicalDeviceMgr.deleteLogicalDevice(context.TODO(), cDevice); err != nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1119 | log.Warnw("deleteLogical-device-error", log.Fields{"deviceId": cDevice.Id}) |
| 1120 | return err |
| 1121 | } |
| 1122 | // Remove the logical device Id from the parent device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1123 | logicalID := "" |
| 1124 | dMgr.UpdateDeviceAttribute(cDevice.Id, "ParentId", logicalID) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1125 | return nil |
| 1126 | } |
| 1127 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1128 | // DeleteLogicalPort removes the logical port associated with a device |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1129 | func (dMgr *DeviceManager) DeleteLogicalPort(device *voltha.Device) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1130 | log.Info("deleteLogicalPort") |
| 1131 | var err error |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1132 | // Get the logical port associated with this device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1133 | var lPortID *voltha.LogicalPortId |
| 1134 | if lPortID, err = dMgr.logicalDeviceMgr.getLogicalPortID(device); err != nil { |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 1135 | log.Warnw("getLogical-port-error", log.Fields{"deviceId": device.Id, "error": err}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1136 | return err |
| 1137 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1138 | if err = dMgr.logicalDeviceMgr.deleteLogicalPort(context.TODO(), lPortID); err != nil { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1139 | log.Warnw("deleteLogical-port-error", log.Fields{"deviceId": device.Id}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1140 | return err |
| 1141 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1142 | return nil |
| 1143 | } |
| 1144 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1145 | // DeleteLogicalPorts removes the logical ports associated with that deviceId |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1146 | func (dMgr *DeviceManager) DeleteLogicalPorts(device *voltha.Device) error { |
| 1147 | log.Info("deleteLogicalPorts") |
| 1148 | if err := dMgr.logicalDeviceMgr.deleteLogicalPorts(device.Id); err != nil { |
| 1149 | log.Warnw("deleteLogical-ports-error", log.Fields{"deviceId": device.Id}) |
| 1150 | return err |
| 1151 | } |
| 1152 | return nil |
| 1153 | } |
| 1154 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1155 | func (dMgr *DeviceManager) getParentDevice(childDevice *voltha.Device) *voltha.Device { |
| 1156 | // Sanity check |
| 1157 | if childDevice.Root { |
| 1158 | // childDevice is the parent device |
| 1159 | return childDevice |
| 1160 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1161 | parentDevice, _ := dMgr.GetDevice(childDevice.ParentId) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1162 | return parentDevice |
| 1163 | } |
| 1164 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1165 | //childDevicesLost is invoked by an adapter to indicate that a parent device is in a state (Disabled) where it |
| 1166 | //cannot manage the child devices. This will trigger the Core to disable all the child devices. |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1167 | func (dMgr *DeviceManager) childDevicesLost(parentDeviceID string) error { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1168 | log.Debug("childDevicesLost") |
| 1169 | var err error |
| 1170 | var parentDevice *voltha.Device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1171 | if parentDevice, err = dMgr.GetDevice(parentDeviceID); err != nil { |
| 1172 | log.Warnw("failed-getting-device", log.Fields{"deviceId": parentDeviceID, "error": err}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1173 | return err |
| 1174 | } |
| 1175 | return dMgr.DisableAllChildDevices(parentDevice) |
| 1176 | } |
| 1177 | |
| 1178 | //childDevicesDetected is invoked by an adapter when child devices are found, typically after after a |
| 1179 | // disable/enable sequence. This will trigger the Core to Enable all the child devices of that parent. |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1180 | func (dMgr *DeviceManager) childDevicesDetected(parentDeviceID string) error { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1181 | log.Debug("childDevicesDetected") |
| 1182 | var err error |
| 1183 | var parentDevice *voltha.Device |
| 1184 | var childDeviceIds []string |
| 1185 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1186 | if parentDevice, err = dMgr.GetDevice(parentDeviceID); err != nil { |
| 1187 | log.Warnw("failed-getting-device", log.Fields{"deviceId": parentDeviceID, "error": err}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1188 | return err |
| 1189 | } |
| 1190 | |
| 1191 | if childDeviceIds, err = dMgr.getAllChildDeviceIds(parentDevice); err != nil { |
| 1192 | return status.Errorf(codes.NotFound, "%s", parentDevice.Id) |
| 1193 | } |
| 1194 | if len(childDeviceIds) == 0 { |
| 1195 | log.Debugw("no-child-device", log.Fields{"parentDeviceId": parentDevice.Id}) |
| 1196 | } |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1197 | allChildEnableRequestSent := true |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1198 | for _, childDeviceID := range childDeviceIds { |
| 1199 | if agent := dMgr.getDeviceAgent(childDeviceID); agent != nil { |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1200 | // Run the children re-registration in its own routine |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1201 | go func() { |
| 1202 | err = agent.enableDevice(context.TODO()) |
| 1203 | if err != nil { |
| 1204 | log.Errorw("unable-to-enable-device", log.Fields{"error": err}) |
| 1205 | } |
| 1206 | }() |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1207 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1208 | err = status.Errorf(codes.Unavailable, "no agent for child device %s", childDeviceID) |
| 1209 | log.Errorw("no-child-device-agent", log.Fields{"parentDeviceId": parentDevice.Id, "childId": childDeviceID}) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1210 | allChildEnableRequestSent = false |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1211 | } |
| 1212 | } |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1213 | if !allChildEnableRequestSent { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1214 | return err |
| 1215 | } |
| 1216 | return nil |
| 1217 | } |
| 1218 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1219 | /* |
| 1220 | All the functions below are callback functions where they are invoked with the latest and previous data. We can |
| 1221 | therefore use the data as is without trying to get the latest from the model. |
| 1222 | */ |
| 1223 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1224 | //DisableAllChildDevices is invoked as a callback when the parent device is disabled |
| 1225 | func (dMgr *DeviceManager) DisableAllChildDevices(parentDevice *voltha.Device) error { |
| 1226 | log.Debug("DisableAllChildDevices") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1227 | var childDeviceIds []string |
| 1228 | var err error |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1229 | if childDeviceIds, err = dMgr.getAllChildDeviceIds(parentDevice); err != nil { |
| 1230 | return status.Errorf(codes.NotFound, "%s", parentDevice.Id) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1231 | } |
| 1232 | if len(childDeviceIds) == 0 { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1233 | log.Debugw("no-child-device", log.Fields{"parentDeviceId": parentDevice.Id}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1234 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1235 | allChildDisable := true |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1236 | for _, childDeviceID := range childDeviceIds { |
| 1237 | if agent := dMgr.getDeviceAgent(childDeviceID); agent != nil { |
| 1238 | if err = agent.disableDevice(context.TODO()); err != nil { |
| 1239 | log.Errorw("failure-disable-device", log.Fields{"deviceId": childDeviceID, "error": err.Error()}) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1240 | allChildDisable = false |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1241 | } |
| 1242 | } |
| 1243 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1244 | if !allChildDisable { |
| 1245 | return err |
| 1246 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1247 | return nil |
| 1248 | } |
| 1249 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1250 | //DeleteAllChildDevices is invoked as a callback when the parent device is deleted |
| 1251 | func (dMgr *DeviceManager) DeleteAllChildDevices(parentDevice *voltha.Device) error { |
| 1252 | log.Debug("DeleteAllChildDevices") |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1253 | var childDeviceIds []string |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1254 | var err error |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1255 | if childDeviceIds, err = dMgr.getAllChildDeviceIds(parentDevice); err != nil { |
| 1256 | return status.Errorf(codes.NotFound, "%s", parentDevice.Id) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1257 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1258 | if len(childDeviceIds) == 0 { |
| 1259 | log.Debugw("no-child-device", log.Fields{"parentDeviceId": parentDevice.Id}) |
| 1260 | } |
| 1261 | allChildDeleted := true |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1262 | for _, childDeviceID := range childDeviceIds { |
| 1263 | if agent := dMgr.getDeviceAgent(childDeviceID); agent != nil { |
| 1264 | if err = agent.deleteDevice(context.TODO()); err != nil { |
| 1265 | log.Errorw("failure-delete-device", log.Fields{"deviceId": childDeviceID, "error": err.Error()}) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1266 | allChildDeleted = false |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1267 | } |
khenaidoo | 4908535 | 2020-01-13 19:15:43 -0500 | [diff] [blame] | 1268 | // No further action is required here. The deleteDevice will change the device state where the resulting |
| 1269 | // callback will take care of cleaning the child device agent. |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | if !allChildDeleted { |
| 1273 | return err |
| 1274 | } |
| 1275 | return nil |
| 1276 | } |
| 1277 | |
| 1278 | //getAllChildDeviceIds is a helper method to get all the child device IDs from the device passed as parameter |
| 1279 | func (dMgr *DeviceManager) getAllChildDeviceIds(parentDevice *voltha.Device) ([]string, error) { |
| 1280 | log.Debugw("getAllChildDeviceIds", log.Fields{"parentDeviceId": parentDevice.Id}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1281 | childDeviceIds := make([]string, 0) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1282 | if parentDevice != nil { |
| 1283 | for _, port := range parentDevice.Ports { |
| 1284 | for _, peer := range port.Peers { |
| 1285 | childDeviceIds = append(childDeviceIds, peer.DeviceId) |
| 1286 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1287 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1288 | log.Debugw("returning-getAllChildDeviceIds", log.Fields{"parentDeviceId": parentDevice.Id, "childDeviceIds": childDeviceIds}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1289 | } |
| 1290 | return childDeviceIds, nil |
| 1291 | } |
| 1292 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 1293 | //getAllChildDevices is a helper method to get all the child device IDs from the device passed as parameter |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1294 | func (dMgr *DeviceManager) getAllChildDevices(parentDeviceID string) (*voltha.Devices, error) { |
| 1295 | log.Debugw("getAllChildDevices", log.Fields{"parentDeviceId": parentDeviceID}) |
| 1296 | if parentDevice, err := dMgr.GetDevice(parentDeviceID); err == nil { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 1297 | childDevices := make([]*voltha.Device, 0) |
| 1298 | if childDeviceIds, er := dMgr.getAllChildDeviceIds(parentDevice); er == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1299 | for _, deviceID := range childDeviceIds { |
| 1300 | if d, e := dMgr.GetDevice(deviceID); e == nil && d != nil { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 1301 | childDevices = append(childDevices, d) |
| 1302 | } |
| 1303 | } |
| 1304 | } |
| 1305 | return &voltha.Devices{Items: childDevices}, nil |
| 1306 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1307 | return nil, status.Errorf(codes.NotFound, "%s", parentDeviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 1308 | } |
| 1309 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1310 | // SetupUNILogicalPorts creates UNI ports on the logical device that represents a child UNI interface |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1311 | func (dMgr *DeviceManager) SetupUNILogicalPorts(cDevice *voltha.Device) error { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1312 | log.Info("addUNILogicalPort") |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1313 | if err := dMgr.logicalDeviceMgr.setupUNILogicalPorts(context.TODO(), cDevice); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1314 | log.Warnw("addUNILogicalPort-error", log.Fields{"device": cDevice, "err": err}) |
| 1315 | return err |
| 1316 | } |
| 1317 | return nil |
| 1318 | } |
| 1319 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1320 | func (dMgr *DeviceManager) downloadImage(ctx context.Context, img *voltha.ImageDownload, ch chan interface{}) { |
| 1321 | log.Debugw("downloadImage", log.Fields{"deviceid": img.Id, "imageName": img.Name}) |
| 1322 | var res interface{} |
| 1323 | var err error |
| 1324 | if agent := dMgr.getDeviceAgent(img.Id); agent != nil { |
| 1325 | if res, err = agent.downloadImage(ctx, img); err != nil { |
| 1326 | log.Debugw("downloadImage-failed", log.Fields{"err": err, "imageName": img.Name}) |
| 1327 | res = err |
| 1328 | } |
| 1329 | } else { |
| 1330 | res = status.Errorf(codes.NotFound, "%s", img.Id) |
| 1331 | } |
| 1332 | sendResponse(ctx, ch, res) |
| 1333 | } |
| 1334 | |
| 1335 | func (dMgr *DeviceManager) cancelImageDownload(ctx context.Context, img *voltha.ImageDownload, ch chan interface{}) { |
| 1336 | log.Debugw("cancelImageDownload", log.Fields{"deviceid": img.Id, "imageName": img.Name}) |
| 1337 | var res interface{} |
| 1338 | var err error |
| 1339 | if agent := dMgr.getDeviceAgent(img.Id); agent != nil { |
| 1340 | if res, err = agent.cancelImageDownload(ctx, img); err != nil { |
| 1341 | log.Debugw("cancelImageDownload-failed", log.Fields{"err": err, "imageName": img.Name}) |
| 1342 | res = err |
| 1343 | } |
| 1344 | } else { |
| 1345 | res = status.Errorf(codes.NotFound, "%s", img.Id) |
| 1346 | } |
| 1347 | sendResponse(ctx, ch, res) |
| 1348 | } |
| 1349 | |
| 1350 | func (dMgr *DeviceManager) activateImage(ctx context.Context, img *voltha.ImageDownload, ch chan interface{}) { |
| 1351 | log.Debugw("activateImage", log.Fields{"deviceid": img.Id, "imageName": img.Name}) |
| 1352 | var res interface{} |
| 1353 | var err error |
| 1354 | if agent := dMgr.getDeviceAgent(img.Id); agent != nil { |
| 1355 | if res, err = agent.activateImage(ctx, img); err != nil { |
| 1356 | log.Debugw("activateImage-failed", log.Fields{"err": err, "imageName": img.Name}) |
| 1357 | res = err |
| 1358 | } |
| 1359 | } else { |
| 1360 | res = status.Errorf(codes.NotFound, "%s", img.Id) |
| 1361 | } |
| 1362 | sendResponse(ctx, ch, res) |
| 1363 | } |
| 1364 | |
| 1365 | func (dMgr *DeviceManager) revertImage(ctx context.Context, img *voltha.ImageDownload, ch chan interface{}) { |
| 1366 | log.Debugw("revertImage", log.Fields{"deviceid": img.Id, "imageName": img.Name}) |
| 1367 | var res interface{} |
| 1368 | var err error |
| 1369 | if agent := dMgr.getDeviceAgent(img.Id); agent != nil { |
| 1370 | if res, err = agent.revertImage(ctx, img); err != nil { |
| 1371 | log.Debugw("revertImage-failed", log.Fields{"err": err, "imageName": img.Name}) |
| 1372 | res = err |
| 1373 | } |
| 1374 | } else { |
| 1375 | res = status.Errorf(codes.NotFound, "%s", img.Id) |
| 1376 | } |
| 1377 | sendResponse(ctx, ch, res) |
| 1378 | } |
| 1379 | |
| 1380 | func (dMgr *DeviceManager) getImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload, ch chan interface{}) { |
| 1381 | log.Debugw("getImageDownloadStatus", log.Fields{"deviceid": img.Id, "imageName": img.Name}) |
| 1382 | var res interface{} |
| 1383 | var err error |
| 1384 | if agent := dMgr.getDeviceAgent(img.Id); agent != nil { |
| 1385 | if res, err = agent.getImageDownloadStatus(ctx, img); err != nil { |
| 1386 | log.Debugw("getImageDownloadStatus-failed", log.Fields{"err": err, "imageName": img.Name}) |
| 1387 | res = err |
| 1388 | } |
| 1389 | } else { |
| 1390 | res = status.Errorf(codes.NotFound, "%s", img.Id) |
| 1391 | } |
| 1392 | sendResponse(ctx, ch, res) |
| 1393 | } |
| 1394 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1395 | func (dMgr *DeviceManager) updateImageDownload(deviceID string, img *voltha.ImageDownload) error { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1396 | log.Debugw("updateImageDownload", log.Fields{"deviceid": img.Id, "imageName": img.Name}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1397 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1398 | if err := agent.updateImageDownload(img); err != nil { |
| 1399 | log.Debugw("updateImageDownload-failed", log.Fields{"err": err, "imageName": img.Name}) |
| 1400 | return err |
| 1401 | } |
| 1402 | } else { |
| 1403 | return status.Errorf(codes.NotFound, "%s", img.Id) |
| 1404 | } |
| 1405 | return nil |
| 1406 | } |
| 1407 | |
| 1408 | func (dMgr *DeviceManager) getImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 1409 | log.Debugw("getImageDownload", log.Fields{"deviceid": img.Id, "imageName": img.Name}) |
| 1410 | if agent := dMgr.getDeviceAgent(img.Id); agent != nil { |
| 1411 | return agent.getImageDownload(ctx, img) |
| 1412 | } |
| 1413 | return nil, status.Errorf(codes.NotFound, "%s", img.Id) |
| 1414 | } |
| 1415 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1416 | func (dMgr *DeviceManager) listImageDownloads(ctx context.Context, deviceID string) (*voltha.ImageDownloads, error) { |
| 1417 | log.Debugw("listImageDownloads", log.Fields{"deviceID": deviceID}) |
| 1418 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
| 1419 | return agent.listImageDownloads(ctx, deviceID) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1420 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1421 | return nil, status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1422 | } |
| 1423 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1424 | // SetAdminStateToEnable sets admin state of device to enabled |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1425 | func (dMgr *DeviceManager) SetAdminStateToEnable(cDevice *voltha.Device) error { |
| 1426 | log.Info("SetAdminStateToEnable") |
| 1427 | if agent := dMgr.getDeviceAgent(cDevice.Id); agent != nil { |
| 1428 | return agent.updateAdminState(voltha.AdminState_ENABLED) |
| 1429 | } |
| 1430 | return status.Errorf(codes.NotFound, "%s", cDevice.Id) |
| 1431 | } |
| 1432 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1433 | // NotifyInvalidTransition notifies about invalid transition |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1434 | func (dMgr *DeviceManager) NotifyInvalidTransition(pcDevice *voltha.Device) error { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 1435 | log.Errorw("NotifyInvalidTransition", log.Fields{ |
| 1436 | "device": pcDevice.Id, |
| 1437 | "adminState": pcDevice.AdminState, |
| 1438 | "operState": pcDevice.OperStatus, |
| 1439 | "connState": pcDevice.ConnectStatus, |
| 1440 | }) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1441 | //TODO: notify over kafka? |
| 1442 | return nil |
| 1443 | } |
| 1444 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1445 | func funcName(f interface{}) string { |
| 1446 | p := reflect.ValueOf(f).Pointer() |
| 1447 | rf := runtime.FuncForPC(p) |
| 1448 | return rf.Name() |
| 1449 | } |
| 1450 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1451 | // UpdateDeviceAttribute updates value of particular device attribute |
| 1452 | func (dMgr *DeviceManager) UpdateDeviceAttribute(deviceID string, attribute string, value interface{}) { |
| 1453 | if agent, ok := dMgr.deviceAgents.Load(deviceID); ok { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 1454 | agent.(*DeviceAgent).updateDeviceAttribute(attribute, value) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1455 | } |
| 1456 | } |
| 1457 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1458 | // GetParentDeviceID returns parent device id, either from memory or from the dB, if present |
| 1459 | func (dMgr *DeviceManager) GetParentDeviceID(deviceID string) string { |
| 1460 | if device, _ := dMgr.GetDevice(deviceID); device != nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1461 | log.Infow("GetParentDeviceId", log.Fields{"deviceId": device.Id, "parentId": device.ParentId}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 1462 | return device.ParentId |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1463 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 1464 | return "" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1465 | } |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1466 | |
| 1467 | func (dMgr *DeviceManager) simulateAlarm(ctx context.Context, simulatereq *voltha.SimulateAlarmRequest, ch chan interface{}) { |
| 1468 | log.Debugw("simulateAlarm", log.Fields{"id": simulatereq.Id, "Indicator": simulatereq.Indicator, "IntfId": simulatereq.IntfId, |
| 1469 | "PortTypeName": simulatereq.PortTypeName, "OnuDeviceId": simulatereq.OnuDeviceId, "InverseBitErrorRate": simulatereq.InverseBitErrorRate, |
| 1470 | "Drift": simulatereq.Drift, "NewEqd": simulatereq.NewEqd, "OnuSerialNumber": simulatereq.OnuSerialNumber, "Operation": simulatereq.Operation}) |
| 1471 | var res interface{} |
| 1472 | if agent := dMgr.getDeviceAgent(simulatereq.Id); agent != nil { |
| 1473 | res = agent.simulateAlarm(ctx, simulatereq) |
| 1474 | log.Debugw("SimulateAlarm-result", log.Fields{"result": res}) |
| 1475 | } |
| 1476 | //TODO CLI always get successful response |
| 1477 | sendResponse(ctx, ch, res) |
| 1478 | } |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1479 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1480 | func (dMgr *DeviceManager) updateDeviceReason(deviceID string, reason string) error { |
| 1481 | log.Debugw("updateDeviceReason", log.Fields{"deviceid": deviceID, "reason": reason}) |
| 1482 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1483 | return agent.updateDeviceReason(reason) |
| 1484 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1485 | return status.Errorf(codes.NotFound, "%s", deviceID) |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1486 | } |