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" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 22 | "github.com/opencord/voltha-go/db/model" |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/rw_core/utils" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 24 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 25 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 26 | "github.com/opencord/voltha-lib-go/v3/pkg/probe" |
| 27 | ic "github.com/opencord/voltha-protos/v3/go/inter_container" |
| 28 | ofp "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 29 | "github.com/opencord/voltha-protos/v3/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 30 | "google.golang.org/grpc/codes" |
| 31 | "google.golang.org/grpc/status" |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 32 | "reflect" |
| 33 | "runtime" |
| 34 | "sync" |
| 35 | "time" |
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 |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 47 | kafkaICProxy kafka.InterContainerProxy |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 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 |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 52 | defaultTimeout time.Duration |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 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{} |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 68 | deviceMgr.defaultTimeout = time.Duration(core.config.DefaultCoreTimeout) * time.Millisecond |
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) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 75 | logger.Info("starting-device-manager") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 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) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 79 | logger.Info("device-manager-started") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 80 | } |
| 81 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 82 | func (dMgr *DeviceManager) stop(ctx context.Context) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 83 | logger.Info("stopping-device-manager") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 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) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 86 | logger.Info("device-manager-stopped") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 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 |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 94 | logger.Debugw("sendResponse", log.Fields{"result": result}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 95 | } else { |
| 96 | // Should the transaction be reverted back? |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 97 | logger.Debugw("sendResponse-context-error", log.Fields{"context-error": ctx.Err()}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 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 | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 119 | func (dMgr *DeviceManager) getDeviceAgent(ctx context.Context, deviceID string) *DeviceAgent { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 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 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 124 | // Try to load into memory - loading will also create the device agent and set the device ownership |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 125 | err := dMgr.load(ctx, deviceID) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 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() { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 133 | _, err = dMgr.core.deviceOwnership.OwnedByMe(ctx, &utils.DeviceID{ID: deviceID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 134 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 135 | logger.Errorw("unable-to-find-core-instance-active-owns-this-device", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 136 | } |
| 137 | }() |
| 138 | return agent.(*DeviceAgent) |
| 139 | } |
| 140 | //TODO: Change the return params to return an error as well |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 141 | logger.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{}) { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 158 | deviceExist, err := dMgr.isParentDeviceExist(ctx, device) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 159 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 160 | logger.Errorf("Failed to fetch parent device info") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 161 | sendResponse(ctx, ch, err) |
| 162 | return |
| 163 | } |
| 164 | if deviceExist { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 165 | logger.Errorf("Device is Pre-provisioned already with same IP-Port or MAC Address") |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 166 | sendResponse(ctx, ch, errors.New("Device is already pre-provisioned")) |
| 167 | return |
| 168 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 169 | logger.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) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 175 | device, err = agent.start(ctx, device) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 176 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 177 | logger.Errorw("Fail-to-start-device", log.Fields{"device-id": agent.deviceID, "error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 178 | sendResponse(ctx, ch, err) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 179 | return |
| 180 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 181 | dMgr.addDeviceAgentToMap(agent) |
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{}) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 187 | logger.Debugw("enableDevice", log.Fields{"deviceid": id}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 188 | var res interface{} |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 189 | if agent := dMgr.getDeviceAgent(ctx, id.Id); agent != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 190 | res = agent.enableDevice(ctx) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 191 | logger.Debugw("EnableDevice-result", log.Fields{"result": res}) |
Hardik Windlass | b9cfcb1 | 2020-02-03 15:59:46 +0000 | [diff] [blame] | 192 | } else { |
| 193 | res = status.Errorf(codes.NotFound, "%s", id.Id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | sendResponse(ctx, ch, res) |
| 197 | } |
| 198 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 199 | func (dMgr *DeviceManager) disableDevice(ctx context.Context, id *voltha.ID, ch chan interface{}) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 200 | logger.Debugw("disableDevice", log.Fields{"deviceid": id}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 201 | var res interface{} |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 202 | if agent := dMgr.getDeviceAgent(ctx, id.Id); agent != nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 203 | res = agent.disableDevice(ctx) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 204 | logger.Debugw("disableDevice-result", log.Fields{"result": res}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 205 | } else { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 206 | res = status.Errorf(codes.NotFound, "%s", id.Id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 207 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 208 | |
| 209 | sendResponse(ctx, ch, res) |
| 210 | } |
| 211 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 212 | func (dMgr *DeviceManager) rebootDevice(ctx context.Context, id *voltha.ID, ch chan interface{}) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 213 | logger.Debugw("rebootDevice", log.Fields{"deviceid": id}) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 214 | var res interface{} |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 215 | if agent := dMgr.getDeviceAgent(ctx, id.Id); agent != nil { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 216 | res = agent.rebootDevice(ctx) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 217 | logger.Debugw("rebootDevice-result", log.Fields{"result": res}) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 218 | } else { |
| 219 | res = status.Errorf(codes.NotFound, "%s", id.Id) |
| 220 | } |
| 221 | sendResponse(ctx, ch, res) |
| 222 | } |
| 223 | |
| 224 | func (dMgr *DeviceManager) deleteDevice(ctx context.Context, id *voltha.ID, ch chan interface{}) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 225 | logger.Debugw("deleteDevice", log.Fields{"deviceid": id}) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 226 | var res interface{} |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 227 | if agent := dMgr.getDeviceAgent(ctx, id.Id); agent != nil { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 228 | res = agent.deleteDevice(ctx) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 229 | logger.Debugw("deleteDevice-result", log.Fields{"result": res}) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 230 | } else { |
| 231 | res = status.Errorf(codes.NotFound, "%s", id.Id) |
| 232 | } |
| 233 | sendResponse(ctx, ch, res) |
| 234 | } |
| 235 | |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 236 | // stopManagingDevice stops the management of the device as well as any of its reference device and logical device. |
| 237 | // This function is called only in the Core that does not own this device. In the Core that owns this device then a |
| 238 | // deletion deletion also includes removal of any reference of this device. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 239 | func (dMgr *DeviceManager) stopManagingDevice(ctx context.Context, id string) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 240 | logger.Infow("stopManagingDevice", log.Fields{"deviceId": id}) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 241 | 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] | 242 | if root, _ := dMgr.IsRootDevice(id); root { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 243 | // stop managing the logical device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 244 | ldeviceID := dMgr.logicalDeviceMgr.stopManagingLogicalDeviceWithDeviceID(ctx, id) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 245 | if ldeviceID != "" { // Can happen if logical device agent was already stopped |
| 246 | err := dMgr.core.deviceOwnership.AbandonDevice(ldeviceID) |
| 247 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 248 | logger.Errorw("unable-to-abandon-the-device", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 249 | } |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 250 | } |
khenaidoo | 4908535 | 2020-01-13 19:15:43 -0500 | [diff] [blame] | 251 | // We do not need to stop the child devices as this is taken care by the state machine. |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 252 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 253 | if agent := dMgr.getDeviceAgent(ctx, id); agent != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 254 | if err := agent.stop(ctx); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 255 | logger.Warnw("unable-to-stop-device-agent", log.Fields{"device-id": agent.deviceID, "error": err}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 256 | } |
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 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 261 | logger.Warnw("unable-to-abandon-device", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 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 | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 268 | func (dMgr *DeviceManager) RunPostDeviceDelete(ctx context.Context, cDevice *voltha.Device, pDevice *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 269 | logger.Infow("RunPostDeviceDelete", log.Fields{"deviceId": cDevice.Id}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 270 | dMgr.stopManagingDevice(ctx, cDevice.Id) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 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 |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 275 | func (dMgr *DeviceManager) GetDevice(ctx context.Context, id string) (*voltha.Device, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 276 | logger.Debugw("GetDevice", log.Fields{"deviceid": id}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 277 | if agent := dMgr.getDeviceAgent(ctx, id); agent != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 278 | return agent.getDevice(ctx) |
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 |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 284 | func (dMgr *DeviceManager) GetChildDevice(ctx context.Context, parentDeviceID string, serialNumber string, onuID int64, parentPortNo int64) (*voltha.Device, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 285 | logger.Debugw("GetChildDevice", log.Fields{"parentDeviceid": parentDeviceID, "serialNumber": serialNumber, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 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 | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 290 | if parentDevice, err = dMgr.GetDevice(ctx, 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 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 298 | logger.Debugw("no-child-devices", log.Fields{"parentDeviceId": parentDevice.Id, "serialNumber": serialNumber, "onuId": onuID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 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 |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 305 | if searchDevice, err := dMgr.GetDevice(ctx, 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) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 310 | logger.Debugw("found-child-by-onuid", log.Fields{"parentDeviceId": parentDevice.Id, "onuId": onuID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 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 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 317 | logger.Debugw("found-child-by-serialnumber", log.Fields{"parentDeviceId": parentDevice.Id, "serialNumber": serialNumber}) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 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 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 337 | logger.Debugw("child-device-found", log.Fields{"parentDeviceId": parentDevice.Id, "foundChildDevice": foundChildDevice}) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 338 | return foundChildDevice, nil |
| 339 | } |
| 340 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 341 | logger.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 |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 347 | func (dMgr *DeviceManager) GetChildDeviceWithProxyAddress(ctx context.Context, proxyAddress *voltha.Device_ProxyAddress) (*voltha.Device, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 348 | logger.Debugw("GetChildDeviceWithProxyAddress", log.Fields{"proxyAddress": proxyAddress}) |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 349 | |
| 350 | var parentDevice *voltha.Device |
| 351 | var err error |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 352 | if parentDevice, err = dMgr.GetDevice(ctx, proxyAddress.DeviceId); err != nil { |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 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 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 360 | logger.Debugw("no-child-devices", log.Fields{"parentDeviceId": parentDevice.Id}) |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 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 { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 366 | if searchDevice, err := dMgr.GetDevice(ctx, 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 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 375 | logger.Debugw("child-device-found", log.Fields{"proxyAddress": proxyAddress}) |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 376 | return foundChildDevice, nil |
| 377 | } |
| 378 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 379 | logger.Warnw("child-device-not-found", log.Fields{"proxyAddress": proxyAddress}) |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 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 |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 400 | func (dMgr *DeviceManager) ListDevices(ctx context.Context) (*voltha.Devices, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 401 | logger.Debug("ListDevices") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 402 | result := &voltha.Devices{} |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 403 | devices, err := dMgr.clusterDataProxy.List(ctx, "/devices", 0, false, "") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 404 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 405 | logger.Errorw("failed-to-list-devices-from-cluster-proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 406 | return nil, err |
| 407 | } |
| 408 | if devices != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 409 | for _, d := range devices.([]interface{}) { |
| 410 | device := d.(*voltha.Device) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 411 | // If device is not in memory then set it up |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 412 | if !dMgr.IsDeviceInCache(device.Id) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 413 | logger.Debugw("loading-device-from-Model", log.Fields{"id": device.Id}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 414 | agent := newDeviceAgent(dMgr.adapterProxy, device, dMgr, dMgr.clusterDataProxy, dMgr.defaultTimeout) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 415 | if _, err := agent.start(ctx, nil); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 416 | logger.Warnw("failure-starting-agent", log.Fields{"deviceId": device.Id}) |
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 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 421 | result.Items = append(result.Items, device) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 422 | } |
| 423 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 424 | logger.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. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 429 | func (dMgr *DeviceManager) isParentDeviceExist(ctx context.Context, newDevice *voltha.Device) (bool, error) { |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 430 | hostPort := newDevice.GetHostAndPort() |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 431 | devices, err := dMgr.clusterDataProxy.List(ctx, "/devices", 0, false, "") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 432 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 433 | logger.Errorw("Failed to list devices from cluster data proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 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 | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 453 | func (dMgr *DeviceManager) getDeviceFromModel(ctx context.Context, deviceID string) (*voltha.Device, error) { |
| 454 | device, err := dMgr.clusterDataProxy.Get(ctx, "/devices/"+deviceID, 0, false, "") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 455 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 456 | logger.Errorw("failed-to-get-device-info-from-cluster-proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 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 |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 468 | func (dMgr *DeviceManager) loadDevice(ctx context.Context, deviceID string) (*DeviceAgent, error) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 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 | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 480 | if device, err = dMgr.getDeviceFromModel(ctx, deviceID); err == nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 481 | logger.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 | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 483 | if _, err = agent.start(ctx, nil); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 484 | logger.Warnw("Failure loading device", log.Fields{"deviceId": deviceID, "error": err}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 485 | } else { |
| 486 | dMgr.addDeviceAgentToMap(agent) |
| 487 | } |
| 488 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 489 | logger.Debugw("Device not in model", log.Fields{"deviceId": deviceID}) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 490 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 491 | // announce completion of task to any number of waiting channels |
| 492 | dMgr.devicesLoadingLock.Lock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 493 | if v, ok := dMgr.deviceLoadingInProgress[deviceID]; ok { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 494 | for _, ch := range v { |
| 495 | close(ch) |
| 496 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 497 | delete(dMgr.deviceLoadingInProgress, deviceID) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 498 | } |
| 499 | dMgr.devicesLoadingLock.Unlock() |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 500 | } else { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 501 | dMgr.devicesLoadingLock.Unlock() |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 502 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 503 | } else { |
| 504 | ch := make(chan int, 1) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 505 | dMgr.deviceLoadingInProgress[deviceID] = append(dMgr.deviceLoadingInProgress[deviceID], ch) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 506 | dMgr.devicesLoadingLock.Unlock() |
| 507 | // Wait for the channel to be closed, implying the process loading this device is done. |
| 508 | <-ch |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 509 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 510 | if agent, ok := dMgr.deviceAgents.Load(deviceID); ok { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 511 | return agent.(*DeviceAgent), nil |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 512 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 513 | return nil, status.Errorf(codes.Aborted, "Error loading device %s", deviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | // loadRootDeviceParentAndChildren loads the children and parents of a root device in memory |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 517 | func (dMgr *DeviceManager) loadRootDeviceParentAndChildren(ctx context.Context, device *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 518 | logger.Debugw("loading-parent-and-children", log.Fields{"deviceId": device.Id}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 519 | if device.Root { |
| 520 | // Scenario A |
| 521 | if device.ParentId != "" { |
| 522 | // Load logical device if needed. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 523 | if err := dMgr.logicalDeviceMgr.load(ctx, device.ParentId); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 524 | logger.Warnw("failure-loading-logical-device", log.Fields{"lDeviceId": device.ParentId}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 525 | } |
| 526 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 527 | logger.Debugw("no-parent-to-load", log.Fields{"deviceId": device.Id}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 528 | } |
| 529 | // Load all child devices, if needed |
| 530 | if childDeviceIds, err := dMgr.getAllChildDeviceIds(device); err == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 531 | for _, childDeviceID := range childDeviceIds { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 532 | if _, err := dMgr.loadDevice(ctx, childDeviceID); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 533 | logger.Warnw("failure-loading-device", log.Fields{"deviceId": childDeviceID, "error": err}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 534 | return err |
| 535 | } |
| 536 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 537 | logger.Debugw("loaded-children", log.Fields{"deviceId": device.Id, "numChildren": len(childDeviceIds)}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 538 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 539 | logger.Debugw("no-child-to-load", log.Fields{"deviceId": device.Id}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | return nil |
| 543 | } |
| 544 | |
| 545 | // load loads the deviceId in memory, if not present, and also loads its accompanying parents and children. Loading |
| 546 | // in memory is for improved performance. It is not imperative that a device needs to be in memory when a request |
| 547 | // acting on the device is received by the core. In such a scenario, the Core will load the device in memory first |
| 548 | // and the proceed with the request. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 549 | func (dMgr *DeviceManager) load(ctx context.Context, deviceID string) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 550 | logger.Debug("load...") |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 551 | // First load the device - this may fail in case the device was deleted intentionally by the other core |
| 552 | var dAgent *DeviceAgent |
| 553 | var err error |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 554 | if dAgent, err = dMgr.loadDevice(ctx, deviceID); err != nil { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 555 | return err |
| 556 | } |
| 557 | // Get the loaded device details |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 558 | device, err := dAgent.getDevice(ctx) |
| 559 | if err != nil { |
| 560 | return err |
| 561 | } |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 562 | |
| 563 | // If the device is in Pre-provisioning or deleted state stop here |
| 564 | if device.AdminState == voltha.AdminState_PREPROVISIONED || device.AdminState == voltha.AdminState_DELETED { |
| 565 | return nil |
| 566 | } |
| 567 | |
| 568 | // Now we face two scenarios |
| 569 | if device.Root { |
| 570 | // Load all children as well as the parent of this device (logical_device) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 571 | if err := dMgr.loadRootDeviceParentAndChildren(ctx, device); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 572 | logger.Warnw("failure-loading-device-parent-and-children", log.Fields{"deviceId": deviceID}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 573 | return err |
| 574 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 575 | logger.Debugw("successfully-loaded-parent-and-children", log.Fields{"deviceId": deviceID}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 576 | } else { |
| 577 | // Scenario B - use the parentId of that device (root device) to trigger the loading |
| 578 | if device.ParentId != "" { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 579 | return dMgr.load(ctx, device.ParentId) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | return nil |
| 583 | } |
| 584 | |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 585 | // ListDeviceIds retrieves the latest device IDs information from the data model (memory data only) |
| 586 | func (dMgr *DeviceManager) ListDeviceIds() (*voltha.IDs, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 587 | logger.Debug("ListDeviceIDs") |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 588 | // Report only device IDs that are in the device agent map |
| 589 | return dMgr.listDeviceIdsFromMap(), nil |
| 590 | } |
| 591 | |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 592 | //ReconcileDevices is a request to a voltha core to update its list of managed devices. This will |
| 593 | //trigger loading the devices along with their children and parent in memory |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 594 | func (dMgr *DeviceManager) ReconcileDevices(ctx context.Context, ids *voltha.IDs, ch chan interface{}) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 595 | logger.Debugw("ReconcileDevices", log.Fields{"numDevices": len(ids.Items)}) |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 596 | var res interface{} |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 597 | if ids != nil && len(ids.Items) != 0 { |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 598 | toReconcile := len(ids.Items) |
| 599 | reconciled := 0 |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 600 | var err error |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 601 | for _, id := range ids.Items { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 602 | if err = dMgr.load(ctx, id.Id); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 603 | logger.Warnw("failure-reconciling-device", log.Fields{"deviceId": id.Id, "error": err}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 604 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 605 | reconciled++ |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | if toReconcile != reconciled { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 609 | 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] | 610 | } |
| 611 | } else { |
| 612 | res = status.Errorf(codes.InvalidArgument, "empty-list-of-ids") |
| 613 | } |
| 614 | sendResponse(ctx, ch, res) |
| 615 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 616 | |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 617 | // isOkToReconcile validates whether a device is in the correct status to be reconciled |
| 618 | func isOkToReconcile(device *voltha.Device) bool { |
| 619 | if device == nil { |
| 620 | return false |
| 621 | } |
| 622 | return device.AdminState != voltha.AdminState_PREPROVISIONED && device.AdminState != voltha.AdminState_DELETED |
| 623 | } |
| 624 | |
| 625 | // adapterRestarted is invoked whenever an adapter is restarted |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 626 | func (dMgr *DeviceManager) adapterRestarted(ctx context.Context, adapter *voltha.Adapter) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 627 | logger.Debugw("adapter-restarted", log.Fields{"adapter": adapter.Id}) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 628 | |
| 629 | // Let's reconcile the device managed by this Core only |
| 630 | rootDeviceIds := dMgr.core.deviceOwnership.GetAllDeviceIdsOwnedByMe() |
| 631 | if len(rootDeviceIds) == 0 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 632 | logger.Debugw("nothing-to-reconcile", log.Fields{"adapterId": adapter.Id}) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 633 | return nil |
| 634 | } |
| 635 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 636 | responses := make([]utils.Response, 0) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 637 | for _, rootDeviceID := range rootDeviceIds { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 638 | if rootDevice, _ := dMgr.getDeviceFromModel(ctx, rootDeviceID); rootDevice != nil { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 639 | if rootDevice.Adapter == adapter.Id { |
| 640 | if isOkToReconcile(rootDevice) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 641 | logger.Debugw("reconciling-root-device", log.Fields{"rootId": rootDevice.Id}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 642 | responses = append(responses, dMgr.sendReconcileDeviceRequest(ctx, rootDevice)) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 643 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 644 | logger.Debugw("not-reconciling-root-device", log.Fields{"rootId": rootDevice.Id, "state": rootDevice.AdminState}) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 645 | } |
| 646 | } else { // Should we be reconciling the root's children instead? |
| 647 | childManagedByAdapter: |
| 648 | for _, port := range rootDevice.Ports { |
| 649 | for _, peer := range port.Peers { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 650 | if childDevice, _ := dMgr.getDeviceFromModel(ctx, peer.DeviceId); childDevice != nil { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 651 | if childDevice.Adapter == adapter.Id { |
| 652 | if isOkToReconcile(childDevice) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 653 | logger.Debugw("reconciling-child-device", log.Fields{"childId": childDevice.Id}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 654 | responses = append(responses, dMgr.sendReconcileDeviceRequest(ctx, childDevice)) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 655 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 656 | logger.Debugw("not-reconciling-child-device", log.Fields{"childId": childDevice.Id, "state": childDevice.AdminState}) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 657 | } |
| 658 | } else { |
| 659 | // All child devices under a parent device are typically managed by the same adapter type. |
| 660 | // Therefore we only need to check whether the first device we retrieved is managed by that adapter |
| 661 | break childManagedByAdapter |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 669 | if len(responses) > 0 { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 670 | // Wait for completion |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 671 | if res := utils.WaitForNilOrErrorResponses(dMgr.defaultTimeout, responses...); res != nil { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 672 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 673 | } |
| 674 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 675 | logger.Debugw("no-managed-device-to-reconcile", log.Fields{"adapterId": adapter.Id}) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 676 | } |
| 677 | return nil |
| 678 | } |
| 679 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 680 | func (dMgr *DeviceManager) sendReconcileDeviceRequest(ctx context.Context, device *voltha.Device) utils.Response { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 681 | // Send a reconcile request to the adapter. Since this Core may not be managing this device then there is no |
| 682 | // 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] | 683 | // 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] | 684 | // the adapter_proxy. |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 685 | response := utils.NewResponse() |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 686 | ch, err := dMgr.adapterProxy.reconcileDevice(ctx, device) |
| 687 | if err != nil { |
| 688 | response.Error(err) |
| 689 | } |
| 690 | // Wait for adapter response in its own routine |
| 691 | go func() { |
| 692 | resp, ok := <-ch |
| 693 | if !ok { |
| 694 | response.Error(status.Errorf(codes.Aborted, "channel-closed-device: %s", device.Id)) |
| 695 | } else if resp.Err != nil { |
| 696 | response.Error(resp.Err) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 697 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 698 | response.Done() |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 699 | }() |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 700 | return response |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 701 | } |
| 702 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 703 | func (dMgr *DeviceManager) reconcileChildDevices(ctx context.Context, parentDeviceID string) error { |
| 704 | if parentDevice, _ := dMgr.getDeviceFromModel(ctx, parentDeviceID); parentDevice != nil { |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 705 | responses := make([]utils.Response, 0) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 706 | for _, port := range parentDevice.Ports { |
| 707 | for _, peer := range port.Peers { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 708 | if childDevice, _ := dMgr.getDeviceFromModel(ctx, peer.DeviceId); childDevice != nil { |
| 709 | responses = append(responses, dMgr.sendReconcileDeviceRequest(ctx, childDevice)) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | } |
| 713 | // Wait for completion |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 714 | if res := utils.WaitForNilOrErrorResponses(dMgr.defaultTimeout, responses...); res != nil { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 715 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 716 | } |
| 717 | } |
| 718 | return nil |
| 719 | } |
| 720 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 721 | func (dMgr *DeviceManager) updateDeviceUsingAdapterData(ctx context.Context, device *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 722 | logger.Debugw("updateDeviceUsingAdapterData", log.Fields{"deviceid": device.Id, "device": device}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 723 | if agent := dMgr.getDeviceAgent(ctx, device.Id); agent != nil { |
| 724 | return agent.updateDeviceUsingAdapterData(ctx, device) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 725 | } |
| 726 | return status.Errorf(codes.NotFound, "%s", device.Id) |
| 727 | } |
| 728 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 729 | func (dMgr *DeviceManager) addPort(ctx context.Context, deviceID string, port *voltha.Port) error { |
| 730 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 731 | if agent != nil { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 732 | if err := agent.addPort(ctx, port); err != nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 733 | return err |
| 734 | } |
| 735 | // Setup peer ports |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 736 | meAsPeer := &voltha.Port_PeerPort{DeviceId: deviceID, PortNo: port.PortNo} |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 737 | for _, peerPort := range port.Peers { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 738 | if agent := dMgr.getDeviceAgent(ctx, peerPort.DeviceId); agent != nil { |
| 739 | if err := agent.addPeerPort(ctx, meAsPeer); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 740 | logger.Errorw("failed-to-add-peer", log.Fields{"peer-device-id": peerPort.DeviceId}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 741 | return err |
| 742 | } |
| 743 | } |
| 744 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 745 | // Notify the logical device manager to setup a logical port, if needed. If the added port is an NNI or UNI |
| 746 | // then a logical port will be added to the logical device and the device graph generated. If the port is a |
| 747 | // PON port then only the device graph will be generated. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 748 | if device, err := dMgr.GetDevice(ctx, deviceID); err == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 749 | go func() { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 750 | err = dMgr.logicalDeviceMgr.updateLogicalPort(context.Background(), device, port) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 751 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 752 | logger.Errorw("unable-to-update-logical-port", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 753 | } |
| 754 | }() |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 755 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 756 | logger.Errorw("failed-to-retrieve-device", log.Fields{"deviceId": deviceID}) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 757 | return err |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 758 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 759 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 760 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 761 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 762 | } |
| 763 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 764 | func (dMgr *DeviceManager) addFlowsAndGroups(ctx context.Context, deviceID string, flows []*ofp.OfpFlowStats, groups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 765 | logger.Debugw("addFlowsAndGroups", log.Fields{"deviceid": deviceID, "groups:": groups, "flowMetadata": flowMetadata}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 766 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 767 | return agent.addFlowsAndGroups(ctx, 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 | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 772 | func (dMgr *DeviceManager) deleteFlowsAndGroups(ctx context.Context, deviceID string, flows []*ofp.OfpFlowStats, groups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 773 | logger.Debugw("deleteFlowsAndGroups", log.Fields{"deviceid": deviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 774 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 775 | return agent.deleteFlowsAndGroups(ctx, 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 | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 780 | func (dMgr *DeviceManager) updateFlowsAndGroups(ctx context.Context, deviceID string, flows []*ofp.OfpFlowStats, groups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 781 | logger.Debugw("updateFlowsAndGroups", log.Fields{"deviceid": deviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 782 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 783 | return agent.updateFlowsAndGroups(ctx, 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") |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 794 | } else if agent := dMgr.getDeviceAgent(ctx, pmConfigs.Id); agent != nil { |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 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 | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 803 | func (dMgr *DeviceManager) initPmConfigs(ctx context.Context, 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 | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 807 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 808 | return agent.initPmConfigs(ctx, 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) { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 814 | if agent := dMgr.getDeviceAgent(ctx, 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) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 821 | logger.Debugw("getSwitchCapability", log.Fields{"deviceid": deviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 822 | if agent := dMgr.getDeviceAgent(ctx, 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) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 829 | logger.Debugw("getPorts", log.Fields{"deviceid": deviceID, "portType": portType}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 830 | if agent := dMgr.getDeviceAgent(ctx, 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) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 837 | logger.Debugw("getPortCapability", log.Fields{"deviceid": deviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 838 | if agent := dMgr.getDeviceAgent(ctx, 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 | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 844 | func (dMgr *DeviceManager) updateDeviceStatus(ctx context.Context, deviceID string, operStatus voltha.OperStatus_Types, connStatus voltha.ConnectStatus_Types) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 845 | logger.Debugw("updateDeviceStatus", log.Fields{"deviceid": deviceID, "operStatus": operStatus, "connStatus": connStatus}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 846 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 847 | return agent.updateDeviceStatus(ctx, operStatus, connStatus) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 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 | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 852 | func (dMgr *DeviceManager) updateChildrenStatus(ctx context.Context, deviceID string, operStatus voltha.OperStatus_Types, connStatus voltha.ConnectStatus_Types) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 853 | logger.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 | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 856 | if parentDevice, err = dMgr.GetDevice(ctx, 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 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 864 | logger.Debugw("no-child-device", log.Fields{"parentDeviceId": parentDevice.Id}) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 865 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 866 | for _, childDeviceID := range childDeviceIds { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 867 | if agent := dMgr.getDeviceAgent(ctx, childDeviceID); agent != nil { |
| 868 | if err = agent.updateDeviceStatus(ctx, 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 | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 876 | func (dMgr *DeviceManager) updatePortState(ctx context.Context, deviceID string, portType voltha.Port_PortType, portNo uint32, operStatus voltha.OperStatus_Types) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 877 | logger.Debugw("updatePortState", log.Fields{"deviceid": deviceID, "portType": portType, "portNo": portNo, "operStatus": operStatus}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 878 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 879 | if err := agent.updatePortState(ctx, portType, portNo, operStatus); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 880 | logger.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 |
Mahir Gunyel | 18fa0c9 | 2020-03-06 13:34:04 -0800 | [diff] [blame] | 884 | // Do this for NNI and UNIs only. PON ports are not known by logical device |
| 885 | if portType == voltha.Port_ETHERNET_NNI || portType == voltha.Port_ETHERNET_UNI { |
| 886 | go func() { |
| 887 | err := dMgr.logicalDeviceMgr.updatePortState(context.Background(), deviceID, portNo, operStatus) |
| 888 | if err != nil { |
| 889 | // While we want to handle (catch) and log when |
| 890 | // an update to a port was not able to be |
| 891 | // propagated to the logical port, we can report |
| 892 | // it as a warning and not an error because it |
| 893 | // doesn't stop or modify processing. |
| 894 | // TODO: VOL-2707 |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 895 | logger.Warnw("unable-to-update-logical-port-state", log.Fields{"error": err}) |
Mahir Gunyel | 18fa0c9 | 2020-03-06 13:34:04 -0800 | [diff] [blame] | 896 | } |
| 897 | }() |
| 898 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 899 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 900 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 901 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 902 | } |
| 903 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 904 | func (dMgr *DeviceManager) deleteAllPorts(ctx context.Context, deviceID string) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 905 | logger.Debugw("DeleteAllPorts", log.Fields{"deviceid": deviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 906 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 907 | if err := agent.deleteAllPorts(ctx); err != nil { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 908 | return err |
| 909 | } |
| 910 | // Notify the logical device manager to remove all logical ports, if needed. |
| 911 | // At this stage the device itself may gave been deleted already at a deleteAllPorts |
| 912 | // typically is part of a device deletion phase. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 913 | if device, err := dMgr.GetDevice(ctx, deviceID); err == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 914 | go func() { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 915 | err = dMgr.logicalDeviceMgr.deleteAllLogicalPorts(ctx, device) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 916 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 917 | logger.Errorw("unable-to-delete-logical-ports", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 918 | } |
| 919 | }() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 920 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 921 | logger.Warnw("failed-to-retrieve-device", log.Fields{"deviceId": deviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 922 | return err |
| 923 | } |
| 924 | return nil |
| 925 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 926 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 927 | } |
| 928 | |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 929 | //updatePortsState updates all ports on the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 930 | func (dMgr *DeviceManager) updatePortsState(ctx context.Context, deviceID string, state voltha.OperStatus_Types) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 931 | logger.Debugw("updatePortsState", log.Fields{"deviceid": deviceID}) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 932 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 933 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 934 | switch state { |
| 935 | case voltha.OperStatus_ACTIVE: |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 936 | if err := agent.updatePortsOperState(ctx, state); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 937 | logger.Warnw("updatePortsOperState-failed", log.Fields{"deviceId": deviceID, "error": err}) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 938 | return err |
| 939 | } |
| 940 | case voltha.OperStatus_UNKNOWN: |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 941 | if err := agent.updatePortsOperState(ctx, state); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 942 | logger.Warnw("updatePortsOperState-failed", log.Fields{"deviceId": deviceID, "error": err}) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 943 | return err |
| 944 | } |
| 945 | default: |
| 946 | return status.Error(codes.Unimplemented, "state-change-not-implemented") |
| 947 | } |
| 948 | // Notify the logical device about the state change |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 949 | device, err := dMgr.GetDevice(ctx, deviceID) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 950 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 951 | logger.Warnw("non-existent-device", log.Fields{"deviceId": deviceID, "error": err}) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 952 | return err |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 953 | } |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 954 | if err := dMgr.logicalDeviceMgr.updatePortsState(ctx, device, state); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 955 | logger.Warnw("failed-updating-ports-state", log.Fields{"deviceId": deviceID, "error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 956 | return err |
| 957 | } |
| 958 | return nil |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 959 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 960 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 961 | } |
| 962 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 963 | func (dMgr *DeviceManager) childDeviceDetected(ctx context.Context, parentDeviceID string, parentPortNo int64, deviceType string, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 964 | channelID int64, vendorID string, serialNumber string, onuID int64) (*voltha.Device, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 965 | logger.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] | 966 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 967 | if deviceType == "" && vendorID != "" { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 968 | logger.Debug("device-type-is-nil-fetching-device-type") |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 969 | deviceTypesIf, err := dMgr.adapterMgr.clusterDataProxy.List(ctx, "/device_types", 0, false, "") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 970 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 971 | logger.Errorw("failed-to-get-device-type-info", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 972 | return nil, err |
| 973 | } |
| 974 | if deviceTypesIf != nil { |
Chaitrashree G S | 4b3fada | 2019-07-28 23:55:25 -0700 | [diff] [blame] | 975 | OLoop: |
| 976 | for _, deviceTypeIf := range deviceTypesIf.([]interface{}) { |
| 977 | if dType, ok := deviceTypeIf.(*voltha.DeviceType); ok { |
| 978 | for _, v := range dType.VendorIds { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 979 | if v == vendorID { |
Chaitrashree G S | 4b3fada | 2019-07-28 23:55:25 -0700 | [diff] [blame] | 980 | deviceType = dType.Adapter |
| 981 | break OLoop |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | //if no match found for the vendorid,report adapter with the custom error message |
| 989 | if deviceType == "" { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 990 | logger.Errorw("failed-to-fetch-adapter-name ", log.Fields{"vendorId": vendorID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 991 | return nil, status.Errorf(codes.NotFound, "%s", vendorID) |
Chaitrashree G S | 4b3fada | 2019-07-28 23:55:25 -0700 | [diff] [blame] | 992 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 993 | |
| 994 | // Create the ONU device |
| 995 | childDevice := &voltha.Device{} |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 996 | childDevice.Type = deviceType |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 997 | childDevice.ParentId = parentDeviceID |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 998 | childDevice.ParentPortNo = uint32(parentPortNo) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 999 | childDevice.VendorId = vendorID |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 1000 | childDevice.SerialNumber = serialNumber |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1001 | childDevice.Root = false |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 1002 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1003 | // Get parent device type |
| 1004 | pAgent := dMgr.getDeviceAgent(ctx, parentDeviceID) |
| 1005 | if pAgent == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1006 | return nil, status.Errorf(codes.NotFound, "%s", parentDeviceID) |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 1007 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1008 | if pAgent.deviceType == "" { |
| 1009 | return nil, status.Errorf(codes.FailedPrecondition, "device Type not set %s", parentDeviceID) |
| 1010 | } |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 1011 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1012 | if device, err := dMgr.GetChildDevice(ctx, parentDeviceID, serialNumber, onuID, parentPortNo); err == nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1013 | logger.Warnw("child-device-exists", log.Fields{"parentId": parentDeviceID, "serialNumber": serialNumber}) |
Mahir Gunyel | 6deaa24 | 2019-06-27 04:53:33 -0700 | [diff] [blame] | 1014 | return device, status.Errorf(codes.AlreadyExists, "%s", serialNumber) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 1015 | } |
| 1016 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1017 | childDevice.ProxyAddress = &voltha.Device_ProxyAddress{DeviceId: parentDeviceID, DeviceType: pAgent.deviceType, ChannelId: uint32(channelID), OnuId: uint32(onuID)} |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1018 | |
| 1019 | // Create and start a device agent for that device |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1020 | agent := newDeviceAgent(dMgr.adapterProxy, childDevice, dMgr, dMgr.clusterDataProxy, dMgr.defaultTimeout) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1021 | childDevice, err := agent.start(ctx, childDevice) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 1022 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1023 | logger.Errorw("error-starting-child-device", log.Fields{"parent-device-id": childDevice.ParentId, "child-device-id": agent.deviceID, "error": err}) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 1024 | return nil, err |
| 1025 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1026 | dMgr.addDeviceAgentToMap(agent) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1027 | |
khenaidoo | 09771ef | 2019-10-11 14:25:02 -0400 | [diff] [blame] | 1028 | // Since this Core has handled this request then it therefore owns this child device. Set the |
| 1029 | // ownership of this device to this Core |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1030 | _, err = dMgr.core.deviceOwnership.OwnedByMe(ctx, &utils.DeviceID{ID: agent.deviceID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1031 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1032 | logger.Errorw("unable-to-find-core-instance-active-owns-this-device", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1033 | } |
khenaidoo | 09771ef | 2019-10-11 14:25:02 -0400 | [diff] [blame] | 1034 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1035 | // Activate the child device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1036 | if agent = dMgr.getDeviceAgent(ctx, agent.deviceID); agent != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1037 | go func() { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1038 | err := agent.enableDevice(context.Background()) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1039 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1040 | logger.Errorw("unable-to-enable-device", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1041 | } |
| 1042 | }() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1043 | } |
| 1044 | |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 1045 | // Publish on the messaging bus that we have discovered new devices |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1046 | go func() { |
| 1047 | err := dMgr.kafkaICProxy.DeviceDiscovered(agent.deviceID, deviceType, parentDeviceID, dMgr.coreInstanceID) |
| 1048 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1049 | logger.Errorw("unable-to-discover-the-device", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1050 | } |
| 1051 | }() |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 1052 | |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 1053 | return childDevice, nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1054 | } |
| 1055 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1056 | func (dMgr *DeviceManager) processTransition(ctx context.Context, previous *voltha.Device, current *voltha.Device) error { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1057 | // This will be triggered on every update to the device. |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1058 | handlers := dMgr.stateTransitions.GetTransitionHandler(previous, current) |
| 1059 | if handlers == nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1060 | logger.Debugw("no-op-transition", log.Fields{"deviceId": current.Id}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1061 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1062 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1063 | logger.Debugw("handler-found", log.Fields{"num-expectedHandlers": len(handlers), "isParent": current.Root, "current-data": current}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1064 | for _, handler := range handlers { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1065 | logger.Debugw("running-handler", log.Fields{"handler": funcName(handler)}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1066 | if err := handler(ctx, current, previous); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1067 | logger.Warnw("handler-failed", log.Fields{"handler": funcName(handler), "error": err}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1068 | return err |
| 1069 | } |
| 1070 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1071 | return nil |
| 1072 | } |
| 1073 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1074 | func (dMgr *DeviceManager) packetOut(ctx context.Context, deviceID string, outPort uint32, packet *ofp.OfpPacketOut) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1075 | logger.Debugw("packetOut", log.Fields{"deviceId": deviceID, "outPort": outPort}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1076 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 1077 | return agent.packetOut(ctx, outPort, packet) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1078 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1079 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1080 | } |
| 1081 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1082 | // PacketIn receives packet from adapter |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1083 | func (dMgr *DeviceManager) PacketIn(ctx context.Context, deviceID string, port uint32, transactionID string, packet []byte) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1084 | logger.Debugw("PacketIn", log.Fields{"deviceId": deviceID, "port": port}) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1085 | // Get the logical device Id based on the deviceId |
| 1086 | var device *voltha.Device |
| 1087 | var err error |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1088 | if device, err = dMgr.GetDevice(ctx, deviceID); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1089 | logger.Errorw("device-not-found", log.Fields{"deviceId": deviceID}) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1090 | return err |
| 1091 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1092 | if !device.Root { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1093 | logger.Errorw("device-not-root", log.Fields{"deviceId": deviceID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1094 | return status.Errorf(codes.FailedPrecondition, "%s", deviceID) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1095 | } |
| 1096 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1097 | if err := dMgr.logicalDeviceMgr.packetIn(ctx, device.ParentId, port, transactionID, packet); err != nil { |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1098 | return err |
| 1099 | } |
| 1100 | return nil |
| 1101 | } |
| 1102 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1103 | func (dMgr *DeviceManager) setParentID(ctx context.Context, device *voltha.Device, parentID string) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1104 | logger.Debugw("setParentId", log.Fields{"deviceId": device.Id, "parentId": parentID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1105 | if agent := dMgr.getDeviceAgent(ctx, device.Id); agent != nil { |
| 1106 | return agent.setParentID(ctx, device, parentID) |
khenaidoo | ad06fd7 | 2019-10-28 12:26:05 -0400 | [diff] [blame] | 1107 | } |
| 1108 | return status.Errorf(codes.NotFound, "%s", device.Id) |
| 1109 | } |
| 1110 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1111 | // CreateLogicalDevice creates logical device in core |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1112 | func (dMgr *DeviceManager) CreateLogicalDevice(ctx context.Context, cDevice *voltha.Device, pDevice *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1113 | logger.Info("CreateLogicalDevice") |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1114 | // Verify whether the logical device has already been created |
| 1115 | if cDevice.ParentId != "" { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1116 | logger.Debugw("Parent device already exist.", log.Fields{"deviceId": cDevice.Id, "logicalDeviceId": cDevice.Id}) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1117 | return nil |
| 1118 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1119 | var err error |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1120 | if _, err = dMgr.logicalDeviceMgr.createLogicalDevice(ctx, cDevice); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1121 | logger.Warnw("createlogical-device-error", log.Fields{"device": cDevice}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1122 | return err |
| 1123 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1124 | return nil |
| 1125 | } |
| 1126 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1127 | // DeleteLogicalDevice deletes logical device from core |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1128 | func (dMgr *DeviceManager) DeleteLogicalDevice(ctx context.Context, cDevice *voltha.Device, pDevice *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1129 | logger.Info("DeleteLogicalDevice") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1130 | var err error |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1131 | if err = dMgr.logicalDeviceMgr.deleteLogicalDevice(ctx, cDevice); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1132 | logger.Warnw("deleteLogical-device-error", log.Fields{"deviceId": cDevice.Id}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1133 | return err |
| 1134 | } |
| 1135 | // Remove the logical device Id from the parent device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1136 | logicalID := "" |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1137 | dMgr.UpdateDeviceAttribute(ctx, cDevice.Id, "ParentId", logicalID) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1138 | return nil |
| 1139 | } |
| 1140 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1141 | // DeleteLogicalPort removes the logical port associated with a device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1142 | func (dMgr *DeviceManager) DeleteLogicalPort(ctx context.Context, device *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1143 | logger.Info("deleteLogicalPort") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1144 | var err error |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1145 | // Get the logical port associated with this device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1146 | var lPortID *voltha.LogicalPortId |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1147 | if lPortID, err = dMgr.logicalDeviceMgr.getLogicalPortID(ctx, device); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1148 | logger.Warnw("getLogical-port-error", log.Fields{"deviceId": device.Id, "error": err}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1149 | return err |
| 1150 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1151 | if err = dMgr.logicalDeviceMgr.deleteLogicalPort(ctx, lPortID); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1152 | logger.Warnw("deleteLogical-port-error", log.Fields{"deviceId": device.Id}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1153 | return err |
| 1154 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1155 | return nil |
| 1156 | } |
| 1157 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1158 | // DeleteLogicalPorts removes the logical ports associated with that deviceId |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1159 | func (dMgr *DeviceManager) DeleteLogicalPorts(ctx context.Context, cDevice *voltha.Device, pDevice *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1160 | logger.Debugw("delete-all-logical-ports", log.Fields{"device-id": cDevice.Id}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1161 | if err := dMgr.logicalDeviceMgr.deleteLogicalPorts(ctx, cDevice.Id); err != nil { |
khenaidoo | e132f52 | 2020-03-20 15:23:15 -0400 | [diff] [blame] | 1162 | // Just log the error. The logical device or port may already have been deleted before this callback is invoked. |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1163 | logger.Warnw("deleteLogical-ports-error", log.Fields{"device-id": cDevice.Id, "error": err}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1164 | } |
| 1165 | return nil |
| 1166 | } |
| 1167 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1168 | func (dMgr *DeviceManager) getParentDevice(ctx context.Context, childDevice *voltha.Device) *voltha.Device { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1169 | // Sanity check |
| 1170 | if childDevice.Root { |
| 1171 | // childDevice is the parent device |
| 1172 | return childDevice |
| 1173 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1174 | parentDevice, _ := dMgr.GetDevice(ctx, childDevice.ParentId) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1175 | return parentDevice |
| 1176 | } |
| 1177 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1178 | //childDevicesLost is invoked by an adapter to indicate that a parent device is in a state (Disabled) where it |
| 1179 | //cannot manage the child devices. This will trigger the Core to disable all the child devices. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1180 | func (dMgr *DeviceManager) childDevicesLost(ctx context.Context, parentDeviceID string) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1181 | logger.Debug("childDevicesLost") |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1182 | var err error |
| 1183 | var parentDevice *voltha.Device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1184 | if parentDevice, err = dMgr.GetDevice(ctx, parentDeviceID); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1185 | logger.Warnw("failed-getting-device", log.Fields{"deviceId": parentDeviceID, "error": err}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1186 | return err |
| 1187 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1188 | return dMgr.DisableAllChildDevices(ctx, parentDevice, nil) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | //childDevicesDetected is invoked by an adapter when child devices are found, typically after after a |
| 1192 | // disable/enable sequence. This will trigger the Core to Enable all the child devices of that parent. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1193 | func (dMgr *DeviceManager) childDevicesDetected(ctx context.Context, parentDeviceID string) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1194 | logger.Debug("childDevicesDetected") |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1195 | var err error |
| 1196 | var parentDevice *voltha.Device |
| 1197 | var childDeviceIds []string |
| 1198 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1199 | if parentDevice, err = dMgr.GetDevice(ctx, parentDeviceID); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1200 | logger.Warnw("failed-getting-device", log.Fields{"deviceId": parentDeviceID, "error": err}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1201 | return err |
| 1202 | } |
| 1203 | |
| 1204 | if childDeviceIds, err = dMgr.getAllChildDeviceIds(parentDevice); err != nil { |
| 1205 | return status.Errorf(codes.NotFound, "%s", parentDevice.Id) |
| 1206 | } |
| 1207 | if len(childDeviceIds) == 0 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1208 | logger.Debugw("no-child-device", log.Fields{"parentDeviceId": parentDevice.Id}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1209 | } |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1210 | allChildEnableRequestSent := true |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1211 | for _, childDeviceID := range childDeviceIds { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1212 | if agent := dMgr.getDeviceAgent(ctx, childDeviceID); agent != nil { |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1213 | // Run the children re-registration in its own routine |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1214 | go func() { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1215 | err = agent.enableDevice(ctx) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1216 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1217 | logger.Errorw("unable-to-enable-device", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1218 | } |
| 1219 | }() |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1220 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1221 | err = status.Errorf(codes.Unavailable, "no agent for child device %s", childDeviceID) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1222 | logger.Errorw("no-child-device-agent", log.Fields{"parentDeviceId": parentDevice.Id, "childId": childDeviceID}) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1223 | allChildEnableRequestSent = false |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1224 | } |
| 1225 | } |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1226 | if !allChildEnableRequestSent { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1227 | return err |
| 1228 | } |
| 1229 | return nil |
| 1230 | } |
| 1231 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1232 | /* |
| 1233 | All the functions below are callback functions where they are invoked with the latest and previous data. We can |
| 1234 | therefore use the data as is without trying to get the latest from the model. |
| 1235 | */ |
| 1236 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1237 | //DisableAllChildDevices is invoked as a callback when the parent device is disabled |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1238 | func (dMgr *DeviceManager) DisableAllChildDevices(ctx context.Context, parentCurrDevice *voltha.Device, parentPrevDevice *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1239 | logger.Debug("DisableAllChildDevices") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1240 | var childDeviceIds []string |
| 1241 | var err error |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1242 | if childDeviceIds, err = dMgr.getAllChildDeviceIds(parentCurrDevice); err != nil { |
| 1243 | return status.Errorf(codes.NotFound, "%s", parentCurrDevice.Id) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1244 | } |
| 1245 | if len(childDeviceIds) == 0 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1246 | logger.Debugw("no-child-device", log.Fields{"parentDeviceId": parentCurrDevice.Id}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1247 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1248 | for _, childDeviceID := range childDeviceIds { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1249 | if agent := dMgr.getDeviceAgent(ctx, childDeviceID); agent != nil { |
| 1250 | if err = agent.disableDevice(ctx); err != nil { |
khenaidoo | e132f52 | 2020-03-20 15:23:15 -0400 | [diff] [blame] | 1251 | // Just log the error - this error happens only if the child device was already in deleted state. |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1252 | logger.Errorw("failure-disable-device", log.Fields{"deviceId": childDeviceID, "error": err.Error()}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1253 | } |
| 1254 | } |
| 1255 | } |
| 1256 | return nil |
| 1257 | } |
| 1258 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1259 | //DeleteAllChildDevices is invoked as a callback when the parent device is deleted |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1260 | func (dMgr *DeviceManager) DeleteAllChildDevices(ctx context.Context, parentCurrDevice *voltha.Device, parentPrevDevice *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1261 | logger.Debug("DeleteAllChildDevices") |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1262 | var childDeviceIds []string |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1263 | var err error |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1264 | if childDeviceIds, err = dMgr.getAllChildDeviceIds(parentCurrDevice); err != nil { |
| 1265 | return status.Errorf(codes.NotFound, "%s", parentCurrDevice.Id) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1266 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1267 | if len(childDeviceIds) == 0 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1268 | logger.Debugw("no-child-device", log.Fields{"parentDeviceId": parentCurrDevice.Id}) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1269 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1270 | for _, childDeviceID := range childDeviceIds { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1271 | if agent := dMgr.getDeviceAgent(ctx, childDeviceID); agent != nil { |
| 1272 | if err = agent.deleteDevice(ctx); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1273 | logger.Warnw("failure-delete-device", log.Fields{"deviceId": childDeviceID, "error": err.Error()}) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1274 | } |
khenaidoo | 4908535 | 2020-01-13 19:15:43 -0500 | [diff] [blame] | 1275 | // No further action is required here. The deleteDevice will change the device state where the resulting |
| 1276 | // callback will take care of cleaning the child device agent. |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1277 | } |
| 1278 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1279 | return nil |
| 1280 | } |
| 1281 | |
Hardik Windlass | c704def | 2020-02-26 18:23:19 +0000 | [diff] [blame] | 1282 | //DeleteAllUNILogicalPorts is invoked as a callback when the parent device is deleted |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1283 | func (dMgr *DeviceManager) DeleteAllUNILogicalPorts(ctx context.Context, curr *voltha.Device, prev *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1284 | logger.Debugw("delete-all-uni-logical-ports", log.Fields{"parent-device-id": curr.Id}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1285 | if err := dMgr.logicalDeviceMgr.deleteAllUNILogicalPorts(ctx, curr); err != nil { |
khenaidoo | e132f52 | 2020-03-20 15:23:15 -0400 | [diff] [blame] | 1286 | // Just log the error and let the remaining pipeline proceed - ports may already have been deleted |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1287 | logger.Warnw("delete-all-uni-logical-ports-failed", log.Fields{"parent-device-id": curr.Id, "error": err}) |
Hardik Windlass | c704def | 2020-02-26 18:23:19 +0000 | [diff] [blame] | 1288 | } |
| 1289 | return nil |
| 1290 | } |
| 1291 | |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 1292 | //DeleteAllLogicalPorts is invoked as a callback when the parent device's connection status moves to UNREACHABLE |
| 1293 | func (dMgr *DeviceManager) DeleteAllLogicalPorts(ctx context.Context, parentDevice *voltha.Device, prev *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1294 | logger.Debugw("delete-all-logical-ports", log.Fields{"parent-device-id": parentDevice.Id}) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 1295 | if err := dMgr.logicalDeviceMgr.deleteAllLogicalPorts(ctx, parentDevice); err != nil { |
khenaidoo | e132f52 | 2020-03-20 15:23:15 -0400 | [diff] [blame] | 1296 | // Just log error as logical device may already have been deleted |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1297 | logger.Warnw("delete-all-logical-ports-fail", log.Fields{"parent-device-id": parentDevice.Id, "error": err}) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 1298 | } |
| 1299 | return nil |
| 1300 | } |
| 1301 | |
| 1302 | //DeleteAllDeviceFlows is invoked as a callback when the parent device's connection status moves to UNREACHABLE |
| 1303 | func (dMgr *DeviceManager) DeleteAllDeviceFlows(ctx context.Context, parentDevice *voltha.Device, prev *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1304 | logger.Debugw("delete-all-device-flows", log.Fields{"parent-device-id": parentDevice.Id}) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 1305 | if agent := dMgr.getDeviceAgent(ctx, parentDevice.Id); agent != nil { |
| 1306 | if err := agent.deleteAllFlows(ctx); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1307 | logger.Errorw("error-deleting-all-device-flows", log.Fields{"parent-device-id": parentDevice.Id}) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 1308 | return err |
| 1309 | } |
| 1310 | return nil |
| 1311 | } |
| 1312 | return status.Errorf(codes.NotFound, "%s", parentDevice.Id) |
| 1313 | } |
| 1314 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1315 | //getAllChildDeviceIds is a helper method to get all the child device IDs from the device passed as parameter |
| 1316 | func (dMgr *DeviceManager) getAllChildDeviceIds(parentDevice *voltha.Device) ([]string, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1317 | logger.Debugw("getAllChildDeviceIds", log.Fields{"parentDeviceId": parentDevice.Id}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1318 | childDeviceIds := make([]string, 0) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1319 | if parentDevice != nil { |
| 1320 | for _, port := range parentDevice.Ports { |
| 1321 | for _, peer := range port.Peers { |
| 1322 | childDeviceIds = append(childDeviceIds, peer.DeviceId) |
| 1323 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1324 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1325 | logger.Debugw("returning-getAllChildDeviceIds", log.Fields{"parentDeviceId": parentDevice.Id, "childDeviceIds": childDeviceIds}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1326 | } |
| 1327 | return childDeviceIds, nil |
| 1328 | } |
| 1329 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 1330 | //getAllChildDevices is a helper method to get all the child device IDs from the device passed as parameter |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1331 | func (dMgr *DeviceManager) getAllChildDevices(ctx context.Context, parentDeviceID string) (*voltha.Devices, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1332 | logger.Debugw("getAllChildDevices", log.Fields{"parentDeviceId": parentDeviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1333 | if parentDevice, err := dMgr.GetDevice(ctx, parentDeviceID); err == nil { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 1334 | childDevices := make([]*voltha.Device, 0) |
| 1335 | if childDeviceIds, er := dMgr.getAllChildDeviceIds(parentDevice); er == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1336 | for _, deviceID := range childDeviceIds { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1337 | if d, e := dMgr.GetDevice(ctx, deviceID); e == nil && d != nil { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 1338 | childDevices = append(childDevices, d) |
| 1339 | } |
| 1340 | } |
| 1341 | } |
| 1342 | return &voltha.Devices{Items: childDevices}, nil |
| 1343 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1344 | return nil, status.Errorf(codes.NotFound, "%s", parentDeviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 1345 | } |
| 1346 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1347 | // SetupUNILogicalPorts creates UNI ports on the logical device that represents a child UNI interface |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1348 | func (dMgr *DeviceManager) SetupUNILogicalPorts(ctx context.Context, cDevice *voltha.Device, pDevice *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1349 | logger.Info("addUNILogicalPort") |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1350 | if err := dMgr.logicalDeviceMgr.setupUNILogicalPorts(ctx, cDevice); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1351 | logger.Warnw("addUNILogicalPort-error", log.Fields{"device": cDevice, "err": err}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1352 | return err |
| 1353 | } |
| 1354 | return nil |
| 1355 | } |
| 1356 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1357 | func (dMgr *DeviceManager) downloadImage(ctx context.Context, img *voltha.ImageDownload, ch chan interface{}) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1358 | logger.Debugw("downloadImage", log.Fields{"deviceid": img.Id, "imageName": img.Name}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1359 | var res interface{} |
| 1360 | var err error |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1361 | if agent := dMgr.getDeviceAgent(ctx, img.Id); agent != nil { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1362 | if res, err = agent.downloadImage(ctx, img); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1363 | logger.Debugw("downloadImage-failed", log.Fields{"err": err, "imageName": img.Name}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1364 | res = err |
| 1365 | } |
| 1366 | } else { |
| 1367 | res = status.Errorf(codes.NotFound, "%s", img.Id) |
| 1368 | } |
| 1369 | sendResponse(ctx, ch, res) |
| 1370 | } |
| 1371 | |
| 1372 | func (dMgr *DeviceManager) cancelImageDownload(ctx context.Context, img *voltha.ImageDownload, ch chan interface{}) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1373 | logger.Debugw("cancelImageDownload", log.Fields{"deviceid": img.Id, "imageName": img.Name}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1374 | var res interface{} |
| 1375 | var err error |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1376 | if agent := dMgr.getDeviceAgent(ctx, img.Id); agent != nil { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1377 | if res, err = agent.cancelImageDownload(ctx, img); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1378 | logger.Debugw("cancelImageDownload-failed", log.Fields{"err": err, "imageName": img.Name}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1379 | res = err |
| 1380 | } |
| 1381 | } else { |
| 1382 | res = status.Errorf(codes.NotFound, "%s", img.Id) |
| 1383 | } |
| 1384 | sendResponse(ctx, ch, res) |
| 1385 | } |
| 1386 | |
| 1387 | func (dMgr *DeviceManager) activateImage(ctx context.Context, img *voltha.ImageDownload, ch chan interface{}) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1388 | logger.Debugw("activateImage", log.Fields{"deviceid": img.Id, "imageName": img.Name}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1389 | var res interface{} |
| 1390 | var err error |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1391 | if agent := dMgr.getDeviceAgent(ctx, img.Id); agent != nil { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1392 | if res, err = agent.activateImage(ctx, img); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1393 | logger.Debugw("activateImage-failed", log.Fields{"err": err, "imageName": img.Name}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1394 | res = err |
| 1395 | } |
| 1396 | } else { |
| 1397 | res = status.Errorf(codes.NotFound, "%s", img.Id) |
| 1398 | } |
| 1399 | sendResponse(ctx, ch, res) |
| 1400 | } |
| 1401 | |
| 1402 | func (dMgr *DeviceManager) revertImage(ctx context.Context, img *voltha.ImageDownload, ch chan interface{}) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1403 | logger.Debugw("revertImage", log.Fields{"deviceid": img.Id, "imageName": img.Name}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1404 | var res interface{} |
| 1405 | var err error |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1406 | if agent := dMgr.getDeviceAgent(ctx, img.Id); agent != nil { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1407 | if res, err = agent.revertImage(ctx, img); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1408 | logger.Debugw("revertImage-failed", log.Fields{"err": err, "imageName": img.Name}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1409 | res = err |
| 1410 | } |
| 1411 | } else { |
| 1412 | res = status.Errorf(codes.NotFound, "%s", img.Id) |
| 1413 | } |
| 1414 | sendResponse(ctx, ch, res) |
| 1415 | } |
| 1416 | |
| 1417 | func (dMgr *DeviceManager) getImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload, ch chan interface{}) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1418 | logger.Debugw("getImageDownloadStatus", log.Fields{"deviceid": img.Id, "imageName": img.Name}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1419 | var res interface{} |
| 1420 | var err error |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1421 | if agent := dMgr.getDeviceAgent(ctx, img.Id); agent != nil { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1422 | if res, err = agent.getImageDownloadStatus(ctx, img); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1423 | logger.Debugw("getImageDownloadStatus-failed", log.Fields{"err": err, "imageName": img.Name}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1424 | res = err |
| 1425 | } |
| 1426 | } else { |
| 1427 | res = status.Errorf(codes.NotFound, "%s", img.Id) |
| 1428 | } |
| 1429 | sendResponse(ctx, ch, res) |
| 1430 | } |
| 1431 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1432 | func (dMgr *DeviceManager) updateImageDownload(ctx context.Context, deviceID string, img *voltha.ImageDownload) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1433 | logger.Debugw("updateImageDownload", log.Fields{"deviceid": img.Id, "imageName": img.Name}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1434 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 1435 | if err := agent.updateImageDownload(ctx, img); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1436 | logger.Debugw("updateImageDownload-failed", log.Fields{"err": err, "imageName": img.Name}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1437 | return err |
| 1438 | } |
| 1439 | } else { |
| 1440 | return status.Errorf(codes.NotFound, "%s", img.Id) |
| 1441 | } |
| 1442 | return nil |
| 1443 | } |
| 1444 | |
| 1445 | func (dMgr *DeviceManager) getImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1446 | logger.Debugw("getImageDownload", log.Fields{"deviceid": img.Id, "imageName": img.Name}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1447 | if agent := dMgr.getDeviceAgent(ctx, img.Id); agent != nil { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1448 | return agent.getImageDownload(ctx, img) |
| 1449 | } |
| 1450 | return nil, status.Errorf(codes.NotFound, "%s", img.Id) |
| 1451 | } |
| 1452 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1453 | func (dMgr *DeviceManager) listImageDownloads(ctx context.Context, deviceID string) (*voltha.ImageDownloads, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1454 | logger.Debugw("listImageDownloads", log.Fields{"deviceID": deviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1455 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1456 | return agent.listImageDownloads(ctx, deviceID) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1457 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1458 | return nil, status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1459 | } |
| 1460 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1461 | func (dMgr *DeviceManager) NotifyInvalidTransition(ctx context.Context, cDevice *voltha.Device, pDevice *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1462 | logger.Errorw("NotifyInvalidTransition", log.Fields{ |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1463 | "device": cDevice.Id, |
| 1464 | "prev-admin-state": pDevice.AdminState, |
| 1465 | "prev-oper-state": pDevice.OperStatus, |
| 1466 | "prev-conn-state": pDevice.ConnectStatus, |
| 1467 | "curr-admin-state": cDevice.AdminState, |
| 1468 | "curr-oper-state": cDevice.OperStatus, |
| 1469 | "curr-conn-state": cDevice.ConnectStatus, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 1470 | }) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1471 | //TODO: notify over kafka? |
| 1472 | return nil |
| 1473 | } |
| 1474 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1475 | func funcName(f interface{}) string { |
| 1476 | p := reflect.ValueOf(f).Pointer() |
| 1477 | rf := runtime.FuncForPC(p) |
| 1478 | return rf.Name() |
| 1479 | } |
| 1480 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1481 | // UpdateDeviceAttribute updates value of particular device attribute |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1482 | func (dMgr *DeviceManager) UpdateDeviceAttribute(ctx context.Context, deviceID string, attribute string, value interface{}) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1483 | if agent, ok := dMgr.deviceAgents.Load(deviceID); ok { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1484 | agent.(*DeviceAgent).updateDeviceAttribute(ctx, attribute, value) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1485 | } |
| 1486 | } |
| 1487 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1488 | // GetParentDeviceID returns parent device id, either from memory or from the dB, if present |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1489 | func (dMgr *DeviceManager) GetParentDeviceID(ctx context.Context, deviceID string) string { |
| 1490 | if device, _ := dMgr.GetDevice(ctx, deviceID); device != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1491 | logger.Infow("GetParentDeviceId", log.Fields{"deviceId": device.Id, "parentId": device.ParentId}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 1492 | return device.ParentId |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1493 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 1494 | return "" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1495 | } |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1496 | |
| 1497 | func (dMgr *DeviceManager) simulateAlarm(ctx context.Context, simulatereq *voltha.SimulateAlarmRequest, ch chan interface{}) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1498 | logger.Debugw("simulateAlarm", log.Fields{"id": simulatereq.Id, "Indicator": simulatereq.Indicator, "IntfId": simulatereq.IntfId, |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1499 | "PortTypeName": simulatereq.PortTypeName, "OnuDeviceId": simulatereq.OnuDeviceId, "InverseBitErrorRate": simulatereq.InverseBitErrorRate, |
| 1500 | "Drift": simulatereq.Drift, "NewEqd": simulatereq.NewEqd, "OnuSerialNumber": simulatereq.OnuSerialNumber, "Operation": simulatereq.Operation}) |
| 1501 | var res interface{} |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1502 | if agent := dMgr.getDeviceAgent(ctx, simulatereq.Id); agent != nil { |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1503 | res = agent.simulateAlarm(ctx, simulatereq) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1504 | logger.Debugw("simulateAlarm-result", log.Fields{"result": res}) |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1505 | } |
| 1506 | //TODO CLI always get successful response |
| 1507 | sendResponse(ctx, ch, res) |
| 1508 | } |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1509 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1510 | func (dMgr *DeviceManager) updateDeviceReason(ctx context.Context, deviceID string, reason string) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1511 | logger.Debugw("updateDeviceReason", log.Fields{"deviceid": deviceID, "reason": reason}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1512 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 1513 | return agent.updateDeviceReason(ctx, reason) |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1514 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1515 | return status.Errorf(codes.NotFound, "%s", deviceID) |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1516 | } |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1517 | |
| 1518 | func (dMgr *DeviceManager) enablePort(ctx context.Context, port *voltha.Port, ch chan interface{}) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1519 | logger.Debugw("enablePort", log.Fields{"device-id": port.DeviceId, "port-no": port.PortNo}) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1520 | var res interface{} |
| 1521 | if agent := dMgr.getDeviceAgent(ctx, port.DeviceId); agent != nil { |
| 1522 | res = agent.enablePort(ctx, port) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1523 | logger.Debugw("enablePort-result", log.Fields{"result": res}) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1524 | } else { |
| 1525 | res = status.Errorf(codes.NotFound, "%s", port.DeviceId) |
| 1526 | } |
| 1527 | |
| 1528 | sendResponse(ctx, ch, res) |
| 1529 | } |
| 1530 | |
| 1531 | func (dMgr *DeviceManager) disablePort(ctx context.Context, port *voltha.Port, ch chan interface{}) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1532 | logger.Debugw("disablePort", log.Fields{"device-id": port.DeviceId, "port-no": port.PortNo}) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1533 | var res interface{} |
| 1534 | if agent := dMgr.getDeviceAgent(ctx, port.DeviceId); agent != nil { |
| 1535 | res = agent.disablePort(ctx, port) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1536 | logger.Debugw("disablePort-result", log.Fields{"result": res}) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1537 | } else { |
| 1538 | res = status.Errorf(codes.NotFound, "%s", port.DeviceId) |
| 1539 | } |
| 1540 | |
| 1541 | sendResponse(ctx, ch, res) |
| 1542 | } |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1543 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1544 | // childDeviceLost calls parent adapter to delete child device and all its references |
| 1545 | func (dMgr *DeviceManager) ChildDeviceLost(ctx context.Context, curr *voltha.Device, prev *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1546 | logger.Debugw("childDeviceLost", log.Fields{"child-device-id": curr.Id, "parent-device-id": curr.ParentId}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1547 | if parentAgent := dMgr.getDeviceAgent(ctx, curr.ParentId); parentAgent != nil { |
khenaidoo | e132f52 | 2020-03-20 15:23:15 -0400 | [diff] [blame] | 1548 | if err := parentAgent.ChildDeviceLost(ctx, curr); err != nil { |
| 1549 | // Just log the message and let the remaining pipeline proceed. |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1550 | logger.Warnw("childDeviceLost", log.Fields{"child-device-id": curr.Id, "parent-device-id": curr.ParentId, "error": err}) |
khenaidoo | e132f52 | 2020-03-20 15:23:15 -0400 | [diff] [blame] | 1551 | } |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1552 | } |
khenaidoo | e132f52 | 2020-03-20 15:23:15 -0400 | [diff] [blame] | 1553 | // Do not return an error as parent device may also have been deleted. Let the remaining pipeline proceed. |
| 1554 | return nil |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1555 | } |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 1556 | |
| 1557 | func (dMgr *DeviceManager) startOmciTest(ctx context.Context, omcitestrequest *voltha.OmciTestRequest) (*voltha.TestResponse, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1558 | logger.Debugw("Omci_test_Request", log.Fields{"device-id": omcitestrequest.Id, "uuid": omcitestrequest.Uuid}) |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 1559 | if agent := dMgr.getDeviceAgent(ctx, omcitestrequest.Id); agent != nil { |
| 1560 | res, err := agent.startOmciTest(ctx, omcitestrequest) |
| 1561 | if err != nil { |
| 1562 | return nil, err |
| 1563 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1564 | logger.Debugw("Omci_test_Response_result-device-magnager", log.Fields{"result": res}) |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 1565 | return res, nil |
| 1566 | } |
| 1567 | return nil, status.Errorf(codes.NotFound, "%s", omcitestrequest.Id) |
| 1568 | } |