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