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 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 17 | package device |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 18 | |
| 19 | import ( |
| 20 | "context" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 21 | "fmt" |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 22 | "sync" |
| 23 | "time" |
| 24 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 25 | "github.com/opencord/voltha-go/rw_core/config" |
| 26 | "github.com/opencord/voltha-lib-go/v7/pkg/probe" |
| 27 | "github.com/opencord/voltha-protos/v5/go/core" |
| 28 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 29 | "github.com/opencord/voltha-go/db/model" |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 30 | "github.com/opencord/voltha-go/rw_core/core/adapter" |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 31 | "github.com/opencord/voltha-go/rw_core/core/device/event" |
Kent Hagerman | 6031aad | 2020-07-29 16:36:33 -0400 | [diff] [blame] | 32 | "github.com/opencord/voltha-go/rw_core/core/device/state" |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 33 | "github.com/opencord/voltha-go/rw_core/utils" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 34 | "github.com/opencord/voltha-lib-go/v7/pkg/events" |
| 35 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame^] | 36 | ca "github.com/opencord/voltha-protos/v5/go/core_adapter" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 37 | ofp "github.com/opencord/voltha-protos/v5/go/openflow_13" |
| 38 | "github.com/opencord/voltha-protos/v5/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 39 | "google.golang.org/grpc/codes" |
| 40 | "google.golang.org/grpc/status" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 41 | ) |
| 42 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 43 | // Manager represent device manager attributes |
| 44 | type Manager struct { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 45 | deviceAgents sync.Map |
| 46 | rootDevices map[string]bool |
| 47 | lockRootDeviceMap sync.RWMutex |
Mahir Gunyel | b0343bf | 2021-05-11 14:14:26 -0700 | [diff] [blame] | 48 | *event.Agent |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 49 | adapterMgr *adapter.Manager |
| 50 | logicalDeviceMgr *LogicalManager |
Kent Hagerman | 6031aad | 2020-07-29 16:36:33 -0400 | [diff] [blame] | 51 | stateTransitions *state.TransitionMap |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 52 | dbPath *model.Path |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 53 | dProxy *model.Proxy |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 54 | coreInstanceID string |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 55 | internalTimeout time.Duration |
| 56 | rpcTimeout time.Duration |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 57 | devicesLoadingLock sync.RWMutex |
| 58 | deviceLoadingInProgress map[string][]chan int |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 59 | config *config.RWCoreFlags |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 60 | } |
| 61 | |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 62 | //NewManagers creates the Manager and the Logical Manager. |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 63 | func NewManagers(dbPath *model.Path, adapterMgr *adapter.Manager, cf *config.RWCoreFlags, coreInstanceID string, eventProxy *events.EventProxy) (*Manager, *LogicalManager) { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 64 | deviceMgr := &Manager{ |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 65 | rootDevices: make(map[string]bool), |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 66 | coreInstanceID: coreInstanceID, |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 67 | dbPath: dbPath, |
| 68 | dProxy: dbPath.Proxy("devices"), |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 69 | adapterMgr: adapterMgr, |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 70 | internalTimeout: cf.InternalTimeout, |
| 71 | rpcTimeout: cf.RPCTimeout, |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 72 | Agent: event.NewAgent(eventProxy, coreInstanceID, cf.VolthaStackID), |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 73 | deviceLoadingInProgress: make(map[string][]chan int), |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 74 | config: cf, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 75 | } |
Kent Hagerman | 6031aad | 2020-07-29 16:36:33 -0400 | [diff] [blame] | 76 | deviceMgr.stateTransitions = state.NewTransitionMap(deviceMgr) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 77 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 78 | logicalDeviceMgr := &LogicalManager{ |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 79 | Manager: event.NewManager(eventProxy, coreInstanceID, cf.VolthaStackID), |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 80 | deviceMgr: deviceMgr, |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 81 | dbPath: dbPath, |
| 82 | ldProxy: dbPath.Proxy("logical_devices"), |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 83 | internalTimeout: cf.InternalTimeout, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 84 | logicalDeviceLoadingInProgress: make(map[string][]chan int), |
| 85 | } |
| 86 | deviceMgr.logicalDeviceMgr = logicalDeviceMgr |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 87 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 88 | adapterMgr.SetAdapterRestartedCallback(deviceMgr.adapterRestartedHandler) |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 89 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 90 | return deviceMgr, logicalDeviceMgr |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 91 | } |
| 92 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 93 | func (dMgr *Manager) Start(ctx context.Context, serviceName string) error { |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 94 | logger.Info(ctx, "starting-device-manager") |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 95 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusPreparing) |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 96 | |
| 97 | // Load all the devices from the dB |
| 98 | var devices []*voltha.Device |
| 99 | if err := dMgr.dProxy.List(ctx, &devices); err != nil { |
| 100 | // Any error from the dB means if we proceed we may end up with corrupted data |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 101 | logger.Errorw(ctx, "failed-to-list-devices-from-KV", log.Fields{"error": err, "service-name": serviceName}) |
| 102 | return err |
| 103 | } |
| 104 | |
| 105 | defer probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning) |
| 106 | |
| 107 | if len(devices) == 0 { |
| 108 | logger.Info(ctx, "no-device-to-load") |
| 109 | return nil |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | for _, device := range devices { |
| 113 | // Create an agent for each device |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 114 | agent := newAgent(device, dMgr, dMgr.dbPath, dMgr.dProxy, dMgr.internalTimeout, dMgr.rpcTimeout) |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 115 | if _, err := agent.start(ctx, true, device); err != nil { |
| 116 | logger.Warnw(ctx, "failure-starting-agent", log.Fields{"device-id": device.Id}) |
| 117 | } else { |
| 118 | dMgr.addDeviceAgentToMap(agent) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // TODO: Need to trigger a reconcile at this point |
| 123 | |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 124 | logger.Info(ctx, "device-manager-started") |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 125 | |
| 126 | return nil |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 129 | func (dMgr *Manager) addDeviceAgentToMap(agent *Agent) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 130 | if _, exist := dMgr.deviceAgents.Load(agent.deviceID); !exist { |
| 131 | dMgr.deviceAgents.Store(agent.deviceID, agent) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 132 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 133 | dMgr.lockRootDeviceMap.Lock() |
| 134 | defer dMgr.lockRootDeviceMap.Unlock() |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 135 | dMgr.rootDevices[agent.deviceID] = agent.isRootDevice |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 136 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 139 | func (dMgr *Manager) deleteDeviceAgentFromMap(agent *Agent) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 140 | dMgr.deviceAgents.Delete(agent.deviceID) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 141 | dMgr.lockRootDeviceMap.Lock() |
| 142 | defer dMgr.lockRootDeviceMap.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 143 | delete(dMgr.rootDevices, agent.deviceID) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 144 | } |
| 145 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 146 | // getDeviceAgent returns the agent managing the device. If the device is not in memory, it will loads it, if it exists |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 147 | func (dMgr *Manager) getDeviceAgent(ctx context.Context, deviceID string) *Agent { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 148 | agent, ok := dMgr.deviceAgents.Load(deviceID) |
| 149 | if ok { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 150 | return agent.(*Agent) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 151 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 152 | // 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] | 153 | err := dMgr.load(ctx, deviceID) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 154 | if err == nil { |
| 155 | agent, ok = dMgr.deviceAgents.Load(deviceID) |
| 156 | if !ok { |
| 157 | return nil |
| 158 | } |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 159 | return agent.(*Agent) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 160 | } |
| 161 | //TODO: Change the return params to return an error as well |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 162 | logger.Errorw(ctx, "loading-device-failed", log.Fields{"device-id": deviceID, "error": err}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 163 | return nil |
| 164 | } |
| 165 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 166 | // listDeviceIdsFromMap returns the list of device IDs that are in memory |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 167 | func (dMgr *Manager) listDeviceIdsFromMap() *voltha.IDs { |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 168 | result := &voltha.IDs{Items: make([]*voltha.ID, 0)} |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 169 | |
| 170 | dMgr.deviceAgents.Range(func(key, value interface{}) bool { |
| 171 | result.Items = append(result.Items, &voltha.ID{Id: key.(string)}) |
| 172 | return true |
| 173 | }) |
| 174 | |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 175 | return result |
| 176 | } |
| 177 | |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 178 | // stopManagingDevice stops the management of the device as well as any of its reference device and logical device. |
| 179 | // This function is called only in the Core that does not own this device. In the Core that owns this device then a |
| 180 | // deletion deletion also includes removal of any reference of this device. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 181 | func (dMgr *Manager) stopManagingDevice(ctx context.Context, id string) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 182 | logger.Infow(ctx, "stop-managing-device", log.Fields{"device-id": id}) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 183 | if dMgr.IsDeviceInCache(id) { // Proceed only if an agent is present for this device |
Kent Hagerman | 6031aad | 2020-07-29 16:36:33 -0400 | [diff] [blame] | 184 | if device, err := dMgr.getDeviceReadOnly(ctx, id); err == nil && device.Root { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 185 | // stop managing the logical device |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 186 | _ = dMgr.logicalDeviceMgr.stopManagingLogicalDeviceWithDeviceID(ctx, id) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 187 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 188 | if agent := dMgr.getDeviceAgent(ctx, id); agent != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 189 | if err := agent.stop(ctx); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 190 | logger.Warnw(ctx, "unable-to-stop-device-agent", log.Fields{"device-id": agent.deviceID, "error": err}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 191 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 192 | dMgr.deleteDeviceAgentFromMap(agent) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 197 | // getDeviceReadOnly will returns a device, either from memory or from the dB, if present |
| 198 | func (dMgr *Manager) getDeviceReadOnly(ctx context.Context, id string) (*voltha.Device, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 199 | logger.Debugw(ctx, "get-device-read-only", log.Fields{"device-id": id}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 200 | if agent := dMgr.getDeviceAgent(ctx, id); agent != nil { |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 201 | return agent.getDeviceReadOnly(ctx) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 202 | } |
| 203 | return nil, status.Errorf(codes.NotFound, "%s", id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 204 | } |
| 205 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 206 | func (dMgr *Manager) listDevicePorts(ctx context.Context, id string) (map[uint32]*voltha.Port, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 207 | logger.Debugw(ctx, "list-device-ports", log.Fields{"device-id": id}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 208 | agent := dMgr.getDeviceAgent(ctx, id) |
| 209 | if agent == nil { |
| 210 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 211 | } |
| 212 | return agent.listDevicePorts(), nil |
| 213 | } |
| 214 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 215 | // IsDeviceInCache returns true if device is found in the map |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 216 | func (dMgr *Manager) IsDeviceInCache(id string) bool { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 217 | _, exist := dMgr.deviceAgents.Load(id) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 218 | return exist |
| 219 | } |
| 220 | |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 221 | //isParentDeviceExist checks whether device is already preprovisioned. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 222 | func (dMgr *Manager) isParentDeviceExist(ctx context.Context, newDevice *voltha.Device) (bool, error) { |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 223 | hostPort := newDevice.GetHostAndPort() |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 224 | var devices []*voltha.Device |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 225 | if err := dMgr.dProxy.List(ctx, &devices); err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 226 | logger.Errorw(ctx, "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] | 227 | return false, err |
| 228 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 229 | for _, device := range devices { |
| 230 | if !device.Root { |
| 231 | continue |
| 232 | } |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 233 | |
| 234 | if hostPort != "" && hostPort == device.GetHostAndPort() { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 235 | return true, nil |
| 236 | } |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 237 | if newDevice.MacAddress != "" && newDevice.MacAddress == device.MacAddress { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 238 | return true, nil |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 239 | } |
| 240 | } |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 241 | return false, nil |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 242 | } |
| 243 | |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 244 | //getDeviceFromModelretrieves the device data from the model. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 245 | func (dMgr *Manager) getDeviceFromModel(ctx context.Context, deviceID string) (*voltha.Device, error) { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 246 | device := &voltha.Device{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 247 | if have, err := dMgr.dProxy.Get(ctx, deviceID, device); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 248 | logger.Errorw(ctx, "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] | 249 | return nil, err |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 250 | } else if !have { |
| 251 | return nil, status.Error(codes.NotFound, deviceID) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 252 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 253 | |
| 254 | return device, nil |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 255 | } |
| 256 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 257 | // loadDevice loads the deviceID in memory, if not present |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 258 | func (dMgr *Manager) loadDevice(ctx context.Context, deviceID string) (*Agent, error) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 259 | if deviceID == "" { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 260 | return nil, status.Error(codes.InvalidArgument, "deviceId empty") |
| 261 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 262 | var err error |
| 263 | var device *voltha.Device |
| 264 | dMgr.devicesLoadingLock.Lock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 265 | if _, exist := dMgr.deviceLoadingInProgress[deviceID]; !exist { |
| 266 | if !dMgr.IsDeviceInCache(deviceID) { |
| 267 | dMgr.deviceLoadingInProgress[deviceID] = []chan int{make(chan int, 1)} |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 268 | dMgr.devicesLoadingLock.Unlock() |
| 269 | // 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] | 270 | if device, err = dMgr.getDeviceFromModel(ctx, deviceID); err == nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 271 | logger.Debugw(ctx, "loading-device", log.Fields{"device-id": deviceID}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 272 | agent := newAgent(device, dMgr, dMgr.dbPath, dMgr.dProxy, dMgr.internalTimeout, dMgr.rpcTimeout) |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 273 | if _, err = agent.start(ctx, true, device); err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 274 | logger.Warnw(ctx, "failure-loading-device", log.Fields{"device-id": deviceID, "error": err}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 275 | } else { |
| 276 | dMgr.addDeviceAgentToMap(agent) |
| 277 | } |
| 278 | } else { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 279 | logger.Debugw(ctx, "device-is-not-in-model", log.Fields{"device-id": deviceID}) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 280 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 281 | // announce completion of task to any number of waiting channels |
| 282 | dMgr.devicesLoadingLock.Lock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 283 | if v, ok := dMgr.deviceLoadingInProgress[deviceID]; ok { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 284 | for _, ch := range v { |
| 285 | close(ch) |
| 286 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 287 | delete(dMgr.deviceLoadingInProgress, deviceID) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 288 | } |
| 289 | dMgr.devicesLoadingLock.Unlock() |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 290 | } else { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 291 | dMgr.devicesLoadingLock.Unlock() |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 292 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 293 | } else { |
| 294 | ch := make(chan int, 1) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 295 | dMgr.deviceLoadingInProgress[deviceID] = append(dMgr.deviceLoadingInProgress[deviceID], ch) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 296 | dMgr.devicesLoadingLock.Unlock() |
| 297 | // Wait for the channel to be closed, implying the process loading this device is done. |
| 298 | <-ch |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 299 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 300 | if agent, ok := dMgr.deviceAgents.Load(deviceID); ok { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 301 | return agent.(*Agent), nil |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 302 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 303 | return nil, status.Errorf(codes.Aborted, "Error loading device %s", deviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | // loadRootDeviceParentAndChildren loads the children and parents of a root device in memory |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 307 | func (dMgr *Manager) loadRootDeviceParentAndChildren(ctx context.Context, device *voltha.Device, devicePorts map[uint32]*voltha.Port) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 308 | logger.Debugw(ctx, "loading-parent-and-children", log.Fields{"device-id": device.Id}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 309 | if device.Root { |
| 310 | // Scenario A |
| 311 | if device.ParentId != "" { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 312 | // Load logical device if needed. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 313 | if err := dMgr.logicalDeviceMgr.load(ctx, device.ParentId); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 314 | logger.Warnw(ctx, "failure-loading-logical-device", log.Fields{"logical-device-id": device.ParentId}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 315 | } |
| 316 | } else { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 317 | logger.Debugw(ctx, "no-parent-to-load", log.Fields{"device-id": device.Id}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 318 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 319 | // Load all child devices, if needed |
| 320 | childDeviceIds := dMgr.getAllChildDeviceIds(ctx, devicePorts) |
| 321 | for childDeviceID := range childDeviceIds { |
| 322 | if _, err := dMgr.loadDevice(ctx, childDeviceID); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 323 | logger.Warnw(ctx, "failure-loading-device", log.Fields{"device-id": childDeviceID, "error": err}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 324 | return err |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 325 | } |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 326 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 327 | logger.Debugw(ctx, "loaded-children", log.Fields{"device-id": device.Id, "num-children": len(childDeviceIds)}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 328 | } |
| 329 | return nil |
| 330 | } |
| 331 | |
| 332 | // load loads the deviceId in memory, if not present, and also loads its accompanying parents and children. Loading |
| 333 | // in memory is for improved performance. It is not imperative that a device needs to be in memory when a request |
| 334 | // acting on the device is received by the core. In such a scenario, the Core will load the device in memory first |
| 335 | // and the proceed with the request. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 336 | func (dMgr *Manager) load(ctx context.Context, deviceID string) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 337 | logger.Debug(ctx, "load...") |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 338 | // First load the device - this may fail in case the device was deleted intentionally by the other core |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 339 | var dAgent *Agent |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 340 | var err error |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 341 | if dAgent, err = dMgr.loadDevice(ctx, deviceID); err != nil { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 342 | return err |
| 343 | } |
| 344 | // Get the loaded device details |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 345 | device, err := dAgent.getDeviceReadOnly(ctx) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 346 | if err != nil { |
| 347 | return err |
| 348 | } |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 349 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 350 | // If the device is in Pre-provisioning or getting deleted state stop here |
| 351 | if device.AdminState == voltha.AdminState_PREPROVISIONED || dAgent.isDeletionInProgress() { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 352 | return nil |
| 353 | } |
| 354 | |
| 355 | // Now we face two scenarios |
| 356 | if device.Root { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 357 | devicePorts := dAgent.listDevicePorts() |
| 358 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 359 | // Load all children as well as the parent of this device (logical_device) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 360 | if err := dMgr.loadRootDeviceParentAndChildren(ctx, device, devicePorts); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 361 | logger.Warnw(ctx, "failure-loading-device-parent-and-children", log.Fields{"device-id": deviceID}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 362 | return err |
| 363 | } |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 364 | logger.Debugw(ctx, "successfully-loaded-parent-and-children", log.Fields{"device-id": deviceID}) |
Andrey Pozolotin | 34dd63f | 2021-05-31 21:26:40 +0300 | [diff] [blame] | 365 | } else if device.ParentId != "" { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 366 | // Scenario B - use the parentId of that device (root device) to trigger the loading |
Andrey Pozolotin | 34dd63f | 2021-05-31 21:26:40 +0300 | [diff] [blame] | 367 | return dMgr.load(ctx, device.ParentId) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 368 | } |
Andrey Pozolotin | 34dd63f | 2021-05-31 21:26:40 +0300 | [diff] [blame] | 369 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 370 | return nil |
| 371 | } |
| 372 | |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 373 | // adapterRestarted is invoked whenever an adapter is restarted |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 374 | func (dMgr *Manager) adapterRestarted(ctx context.Context, adapter *voltha.Adapter) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 375 | logger.Debugw(ctx, "adapter-restarted", log.Fields{"adapter-id": adapter.Id, "vendor": adapter.Vendor, |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 376 | "current-replica": adapter.CurrentReplica, "total-replicas": adapter.TotalReplicas, "restarted-endpoint": adapter.Endpoint}) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 377 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 378 | numberOfDevicesToReconcile := 0 |
| 379 | dMgr.deviceAgents.Range(func(key, value interface{}) bool { |
| 380 | deviceAgent, ok := value.(*Agent) |
| 381 | if ok && deviceAgent.adapterEndpoint == adapter.Endpoint { |
| 382 | // Before reconciling, abort in-process request |
| 383 | if err := deviceAgent.abortAllProcessing(utils.WithNewSpanAndRPCMetadataContext(ctx, "AbortProcessingOnRestart")); err == nil { |
| 384 | logger.Debugw(ctx, "reconciling-device", |
| 385 | log.Fields{ |
| 386 | "device-id": deviceAgent.deviceID, |
| 387 | "root-device": deviceAgent.isRootDevice, |
| 388 | "restarted-endpoint": adapter.Endpoint, |
| 389 | "device-type": deviceAgent.deviceType, |
| 390 | "adapter-type": adapter.Type, |
| 391 | }) |
| 392 | go deviceAgent.ReconcileDevice(utils.WithNewSpanAndRPCMetadataContext(ctx, "ReconcileDevice")) |
| 393 | numberOfDevicesToReconcile++ |
| 394 | } else { |
| 395 | logger.Errorw(ctx, "failed-aborting-exisiting-processing", log.Fields{"error": err}) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 396 | } |
| 397 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 398 | return true |
| 399 | }) |
| 400 | logger.Debugw(ctx, "reconciling-on-adapter-restart-initiated", log.Fields{"adapter-endpoint": adapter.Endpoint, "number-of-devices-to-reconcile": numberOfDevicesToReconcile}) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 401 | return nil |
| 402 | } |
| 403 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 404 | func (dMgr *Manager) UpdateDeviceUsingAdapterData(ctx context.Context, device *voltha.Device) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 405 | logger.Debugw(ctx, "update-device-using-adapter-data", log.Fields{"device-id": device.Id, "device": device}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 406 | if agent := dMgr.getDeviceAgent(ctx, device.Id); agent != nil { |
| 407 | return agent.updateDeviceUsingAdapterData(ctx, device) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 408 | } |
| 409 | return status.Errorf(codes.NotFound, "%s", device.Id) |
| 410 | } |
| 411 | |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 412 | func (dMgr *Manager) addPeerPort(ctx context.Context, deviceID string, port *voltha.Port) error { |
| 413 | meAsPeer := &voltha.Port_PeerPort{DeviceId: deviceID, PortNo: port.PortNo} |
| 414 | for _, peerPort := range port.Peers { |
| 415 | if agent := dMgr.getDeviceAgent(ctx, peerPort.DeviceId); agent != nil { |
| 416 | if err := agent.addPeerPort(ctx, meAsPeer); err != nil { |
| 417 | return err |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | // Notify the logical device manager to setup a logical port, if needed. If the added port is an NNI or UNI |
| 422 | // then a logical port will be added to the logical device and the device route generated. If the port is a |
| 423 | // PON port then only the device graph will be generated. |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 424 | device, err := dMgr.getDeviceReadOnly(ctx, deviceID) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 425 | if err != nil { |
| 426 | return err |
| 427 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 428 | ports, err := dMgr.listDevicePorts(ctx, deviceID) |
| 429 | if err != nil { |
| 430 | return err |
| 431 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 432 | subCtx := utils.WithSpanAndRPCMetadataFromContext(ctx) |
| 433 | |
| 434 | if err = dMgr.logicalDeviceMgr.updateLogicalPort(subCtx, device, ports, port); err != nil { |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 435 | return err |
| 436 | } |
| 437 | return nil |
| 438 | } |
| 439 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 440 | func (dMgr *Manager) AddPort(ctx context.Context, deviceID string, port *voltha.Port) error { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 441 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 442 | if agent != nil { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 443 | if err := agent.addPort(ctx, port); err != nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 444 | return err |
| 445 | } |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 446 | // Setup peer ports in its own routine |
| 447 | go func() { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 448 | subCtx := utils.WithSpanAndRPCMetadataFromContext(ctx) |
| 449 | if err := dMgr.addPeerPort(subCtx, deviceID, port); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 450 | logger.Errorw(ctx, "unable-to-add-peer-port", log.Fields{"error": err, "device-id": deviceID}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 451 | } |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 452 | }() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 453 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 454 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 455 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 456 | } |
| 457 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 458 | func (dMgr *Manager) canMultipleAdapterRequestProceed(ctx context.Context, deviceIDs []string) error { |
| 459 | ready := len(deviceIDs) > 0 |
| 460 | for _, deviceID := range deviceIDs { |
| 461 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
| 462 | if agent == nil { |
| 463 | logger.Errorw(ctx, "adapter-nil", log.Fields{"device-id": deviceID}) |
| 464 | return status.Errorf(codes.Unavailable, "adapter-nil-for-%s", deviceID) |
| 465 | } |
| 466 | ready = ready && agent.isAdapterConnectionUp(ctx) |
| 467 | if !ready { |
| 468 | return status.Errorf(codes.Unavailable, "adapter-connection-down-for-%s", deviceID) |
| 469 | } |
| 470 | if err := agent.canDeviceRequestProceed(ctx); err != nil { |
| 471 | return err |
| 472 | } |
| 473 | } |
| 474 | if !ready { |
| 475 | return status.Error(codes.Unavailable, "adapter(s)-not-ready") |
| 476 | } |
| 477 | return nil |
| 478 | } |
| 479 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame^] | 480 | func (dMgr *Manager) addFlowsAndGroups(ctx context.Context, deviceID string, flows []*ofp.OfpFlowStats, groups []*ofp.OfpGroupEntry, flowMetadata *ofp.FlowMetadata) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 481 | logger.Debugw(ctx, "add-flows-and-groups", log.Fields{"device-id": deviceID, "groups:": groups, "flow-metadata": flowMetadata}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 482 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 483 | return agent.addFlowsAndGroups(ctx, flows, groups, flowMetadata) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 484 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 485 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 486 | } |
| 487 | |
khenaidoo | 787224a | 2020-04-16 18:08:47 -0400 | [diff] [blame] | 488 | // deleteParentFlows removes flows from the parent device based on specific attributes |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame^] | 489 | func (dMgr *Manager) deleteParentFlows(ctx context.Context, deviceID string, uniPort uint32, metadata *ofp.FlowMetadata) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 490 | logger.Debugw(ctx, "delete-parent-flows", log.Fields{"device-id": deviceID, "uni-port": uniPort, "metadata": metadata}) |
khenaidoo | 787224a | 2020-04-16 18:08:47 -0400 | [diff] [blame] | 491 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 492 | if !agent.isRootDevice { |
khenaidoo | 787224a | 2020-04-16 18:08:47 -0400 | [diff] [blame] | 493 | return status.Errorf(codes.FailedPrecondition, "not-a-parent-device-%s", deviceID) |
| 494 | } |
| 495 | return agent.filterOutFlows(ctx, uniPort, metadata) |
| 496 | } |
| 497 | return status.Errorf(codes.NotFound, "%s", deviceID) |
| 498 | } |
| 499 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame^] | 500 | func (dMgr *Manager) deleteFlowsAndGroups(ctx context.Context, deviceID string, flows []*ofp.OfpFlowStats, groups []*ofp.OfpGroupEntry, flowMetadata *ofp.FlowMetadata) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 501 | logger.Debugw(ctx, "delete-flows-and-groups", log.Fields{"device-id": deviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 502 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 503 | return agent.deleteFlowsAndGroups(ctx, flows, groups, flowMetadata) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 504 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 505 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 506 | } |
| 507 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame^] | 508 | func (dMgr *Manager) updateFlowsAndGroups(ctx context.Context, deviceID string, flows []*ofp.OfpFlowStats, groups []*ofp.OfpGroupEntry, flowMetadata *ofp.FlowMetadata) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 509 | logger.Debugw(ctx, "update-flows-and-groups", log.Fields{"device-id": deviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 510 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 511 | return agent.updateFlowsAndGroups(ctx, flows, groups, flowMetadata) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 512 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 513 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 514 | } |
| 515 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 516 | // InitPmConfigs initialize the pm configs as defined by the adapter. |
| 517 | func (dMgr *Manager) InitPmConfigs(ctx context.Context, deviceID string, pmConfigs *voltha.PmConfigs) error { |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 518 | if pmConfigs.Id == "" { |
| 519 | return status.Errorf(codes.FailedPrecondition, "invalid-device-Id") |
| 520 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 521 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 522 | return agent.initPmConfigs(ctx, pmConfigs) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 523 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 524 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 525 | } |
| 526 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame^] | 527 | func (dMgr *Manager) getSwitchCapability(ctx context.Context, deviceID string) (*ca.SwitchCapability, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 528 | logger.Debugw(ctx, "get-switch-capability", log.Fields{"device-id": deviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 529 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 530 | return agent.getSwitchCapability(ctx) |
| 531 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 532 | return nil, status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 533 | } |
| 534 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 535 | func (dMgr *Manager) UpdateDeviceStatus(ctx context.Context, deviceID string, operStatus voltha.OperStatus_Types, connStatus voltha.ConnectStatus_Types) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 536 | logger.Debugw(ctx, "update-device-status", log.Fields{"device-id": deviceID, "oper-status": operStatus, "conn-status": connStatus}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 537 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 538 | return agent.updateDeviceStatus(ctx, operStatus, connStatus) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 539 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 540 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 541 | } |
| 542 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 543 | func (dMgr *Manager) UpdateChildrenStatus(ctx context.Context, deviceID string, operStatus voltha.OperStatus_Types, connStatus voltha.ConnectStatus_Types) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 544 | logger.Debugw(ctx, "update-children-status", log.Fields{"parent-device-id": deviceID, "oper-status": operStatus, "conn-status": connStatus}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 545 | parentDevicePorts, err := dMgr.listDevicePorts(ctx, deviceID) |
| 546 | if err != nil { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 547 | return status.Errorf(codes.Aborted, "%s", err.Error()) |
| 548 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 549 | for childDeviceID := range dMgr.getAllChildDeviceIds(ctx, parentDevicePorts) { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 550 | if agent := dMgr.getDeviceAgent(ctx, childDeviceID); agent != nil { |
| 551 | if err = agent.updateDeviceStatus(ctx, operStatus, connStatus); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 552 | return status.Errorf(codes.Aborted, "childDevice:%s, error:%s", childDeviceID, err.Error()) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | } |
| 556 | return nil |
| 557 | } |
| 558 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 559 | func (dMgr *Manager) UpdatePortState(ctx context.Context, deviceID string, portType voltha.Port_PortType, portNo uint32, operStatus voltha.OperStatus_Types) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 560 | logger.Debugw(ctx, "update-port-state", log.Fields{"device-id": deviceID, "port-type": portType, "port-no": portNo, "oper-status": operStatus}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 561 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 562 | if err := agent.updatePortState(ctx, portType, portNo, operStatus); err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 563 | logger.Errorw(ctx, "updating-port-state-failed", log.Fields{"device-id": deviceID, "port-no": portNo, "error": err}) |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 564 | return err |
| 565 | } |
| 566 | // Notify the logical device manager to change the port state |
Mahir Gunyel | 18fa0c9 | 2020-03-06 13:34:04 -0800 | [diff] [blame] | 567 | // Do this for NNI and UNIs only. PON ports are not known by logical device |
| 568 | if portType == voltha.Port_ETHERNET_NNI || portType == voltha.Port_ETHERNET_UNI { |
| 569 | go func() { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 570 | subCtx := utils.WithSpanAndRPCMetadataFromContext(ctx) |
| 571 | err := dMgr.logicalDeviceMgr.updatePortState(subCtx, deviceID, portNo, operStatus) |
Mahir Gunyel | 18fa0c9 | 2020-03-06 13:34:04 -0800 | [diff] [blame] | 572 | if err != nil { |
| 573 | // While we want to handle (catch) and log when |
| 574 | // an update to a port was not able to be |
| 575 | // propagated to the logical port, we can report |
| 576 | // it as a warning and not an error because it |
| 577 | // doesn't stop or modify processing. |
| 578 | // TODO: VOL-2707 |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 579 | logger.Warnw(ctx, "unable-to-update-logical-port-state", log.Fields{"error": err}) |
Mahir Gunyel | 18fa0c9 | 2020-03-06 13:34:04 -0800 | [diff] [blame] | 580 | } |
| 581 | }() |
| 582 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 583 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 584 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 585 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 586 | } |
| 587 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 588 | //UpdatePortsState updates all ports on the device |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 589 | func (dMgr *Manager) UpdatePortsState(ctx context.Context, deviceID string, portTypeFilter uint32, state voltha.OperStatus_Types) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 590 | logger.Debugw(ctx, "update-ports-state", log.Fields{"device-id": deviceID}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 591 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
| 592 | if agent == nil { |
| 593 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 594 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 595 | if state != voltha.OperStatus_ACTIVE && state != voltha.OperStatus_UNKNOWN { |
| 596 | return status.Error(codes.Unimplemented, "state-change-not-implemented") |
| 597 | } |
| 598 | if err := agent.updatePortsOperState(ctx, portTypeFilter, state); err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 599 | logger.Warnw(ctx, "update-ports-state-failed", log.Fields{"device-id": deviceID, "error": err}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 600 | return err |
| 601 | } |
| 602 | return nil |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 603 | } |
| 604 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 605 | func (dMgr *Manager) packetOut(ctx context.Context, deviceID string, outPort uint32, packet *ofp.OfpPacketOut) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 606 | logger.Debugw(ctx, "packet-out", log.Fields{"device-id": deviceID, "out-port": outPort}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 607 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 608 | return agent.packetOut(ctx, outPort, packet) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 609 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 610 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 611 | } |
| 612 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 613 | // PacketIn receives packet from adapter |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 614 | func (dMgr *Manager) PacketIn(ctx context.Context, deviceID string, port uint32, transactionID string, packet []byte) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 615 | logger.Debugw(ctx, "packet-in", log.Fields{"device-id": deviceID, "port": port}) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 616 | // Get the logical device Id based on the deviceId |
| 617 | var device *voltha.Device |
| 618 | var err error |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 619 | if device, err = dMgr.getDeviceReadOnly(ctx, deviceID); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 620 | logger.Errorw(ctx, "device-not-found", log.Fields{"device-id": deviceID}) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 621 | return err |
| 622 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 623 | if !device.Root { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 624 | logger.Errorw(ctx, "device-not-root", log.Fields{"device-id": deviceID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 625 | return status.Errorf(codes.FailedPrecondition, "%s", deviceID) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 626 | } |
| 627 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 628 | if err := dMgr.logicalDeviceMgr.packetIn(ctx, device.ParentId, port, packet); err != nil { |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 629 | return err |
| 630 | } |
| 631 | return nil |
| 632 | } |
| 633 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 634 | func (dMgr *Manager) setParentID(ctx context.Context, device *voltha.Device, parentID string) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 635 | logger.Debugw(ctx, "set-parent-id", log.Fields{"device-id": device.Id, "parent-id": parentID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 636 | if agent := dMgr.getDeviceAgent(ctx, device.Id); agent != nil { |
| 637 | return agent.setParentID(ctx, device, parentID) |
khenaidoo | ad06fd7 | 2019-10-28 12:26:05 -0400 | [diff] [blame] | 638 | } |
| 639 | return status.Errorf(codes.NotFound, "%s", device.Id) |
| 640 | } |
| 641 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 642 | func (dMgr *Manager) getParentDevice(ctx context.Context, childDevice *voltha.Device) *voltha.Device { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 643 | // Sanity check |
| 644 | if childDevice.Root { |
| 645 | // childDevice is the parent device |
| 646 | return childDevice |
| 647 | } |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 648 | parentDevice, _ := dMgr.getDeviceReadOnly(ctx, childDevice.ParentId) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 649 | return parentDevice |
| 650 | } |
| 651 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 652 | /* |
| 653 | All the functions below are callback functions where they are invoked with the latest and previous data. We can |
| 654 | therefore use the data as is without trying to get the latest from the model. |
| 655 | */ |
| 656 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 657 | //DisableAllChildDevices is invoked as a callback when the parent device is disabled |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 658 | func (dMgr *Manager) DisableAllChildDevices(ctx context.Context, parentCurrDevice *voltha.Device) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 659 | logger.Debug(ctx, "disable-all-child-devices") |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 660 | ports, _ := dMgr.listDevicePorts(ctx, parentCurrDevice.Id) |
| 661 | for childDeviceID := range dMgr.getAllChildDeviceIds(ctx, ports) { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 662 | if agent := dMgr.getDeviceAgent(ctx, childDeviceID); agent != nil { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 663 | if err := agent.disableDevice(ctx); err != nil { |
khenaidoo | e132f52 | 2020-03-20 15:23:15 -0400 | [diff] [blame] | 664 | // Just log the error - this error happens only if the child device was already in deleted state. |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 665 | logger.Errorw(ctx, "failure-disable-device", log.Fields{"device-id": childDeviceID, "error": err.Error()}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | } |
| 669 | return nil |
| 670 | } |
| 671 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 672 | //getAllChildDeviceIds is a helper method to get all the child device IDs from the device passed as parameter |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 673 | func (dMgr *Manager) getAllChildDeviceIds(ctx context.Context, parentDevicePorts map[uint32]*voltha.Port) map[string]struct{} { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 674 | logger.Debug(ctx, "get-all-child-device-ids") |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 675 | childDeviceIds := make(map[string]struct{}, len(parentDevicePorts)) |
| 676 | for _, port := range parentDevicePorts { |
| 677 | for _, peer := range port.Peers { |
| 678 | childDeviceIds[peer.DeviceId] = struct{}{} |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 679 | } |
| 680 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 681 | logger.Debugw(ctx, "returning-getAllChildDeviceIds", log.Fields{"childDeviceIds": childDeviceIds}) |
| 682 | return childDeviceIds |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 683 | } |
| 684 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 685 | //GgtAllChildDevices is a helper method to get all the child device IDs from the device passed as parameter |
| 686 | func (dMgr *Manager) getAllChildDevices(ctx context.Context, parentDeviceID string) (*voltha.Devices, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 687 | logger.Debugw(ctx, "get-all-child-devices", log.Fields{"parent-device-id": parentDeviceID}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 688 | if parentDevicePorts, err := dMgr.listDevicePorts(ctx, parentDeviceID); err == nil { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 689 | childDevices := make([]*voltha.Device, 0) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 690 | for deviceID := range dMgr.getAllChildDeviceIds(ctx, parentDevicePorts) { |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 691 | if d, e := dMgr.getDeviceReadOnly(ctx, deviceID); e == nil && d != nil { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 692 | childDevices = append(childDevices, d) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | return &voltha.Devices{Items: childDevices}, nil |
| 696 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 697 | return nil, status.Errorf(codes.NotFound, "%s", parentDeviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 698 | } |
| 699 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 700 | func (dMgr *Manager) NotifyInvalidTransition(ctx context.Context, device *voltha.Device) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 701 | logger.Errorw(ctx, "notify-invalid-transition", log.Fields{ |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 702 | "device": device.Id, |
| 703 | "curr-admin-state": device.AdminState, |
| 704 | "curr-oper-state": device.OperStatus, |
| 705 | "curr-conn-state": device.ConnectStatus, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 706 | }) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 707 | //TODO: notify over kafka? |
| 708 | return nil |
| 709 | } |
| 710 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 711 | // UpdateDeviceAttribute updates value of particular device attribute |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 712 | func (dMgr *Manager) UpdateDeviceAttribute(ctx context.Context, deviceID string, attribute string, value interface{}) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 713 | if agent, ok := dMgr.deviceAgents.Load(deviceID); ok { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 714 | agent.(*Agent).updateDeviceAttribute(ctx, attribute, value) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 718 | // GetParentDeviceID returns parent device id, either from memory or from the dB, if present |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 719 | func (dMgr *Manager) GetParentDeviceID(ctx context.Context, deviceID string) string { |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 720 | if device, _ := dMgr.getDeviceReadOnly(ctx, deviceID); device != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 721 | logger.Infow(ctx, "get-parent-device-id", log.Fields{"device-id": device.Id, "parent-id": device.ParentId}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 722 | return device.ParentId |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 723 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 724 | return "" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 725 | } |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 726 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 727 | func (dMgr *Manager) UpdateDeviceReason(ctx context.Context, deviceID string, reason string) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 728 | logger.Debugw(ctx, "update-device-reason", log.Fields{"device-id": deviceID, "reason": reason}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 729 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 730 | return agent.updateDeviceReason(ctx, reason) |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 731 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 732 | return status.Errorf(codes.NotFound, "%s", deviceID) |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 733 | } |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 734 | |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 735 | func (dMgr *Manager) SendRPCEvent(ctx context.Context, id string, rpcEvent *voltha.RPCEvent, |
| 736 | category voltha.EventCategory_Types, subCategory *voltha.EventSubCategory_Types, raisedTs int64) { |
| 737 | //TODO Instead of directly sending to the kafka bus, queue the message and send it asynchronously |
Mahir Gunyel | b0343bf | 2021-05-11 14:14:26 -0700 | [diff] [blame] | 738 | dMgr.Agent.SendRPCEvent(ctx, id, rpcEvent, category, subCategory, raisedTs) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 739 | } |
Maninder | a496f85 | 2021-02-22 09:57:56 +0530 | [diff] [blame] | 740 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 741 | func (dMgr *Manager) GetTransientState(ctx context.Context, id string) (core.DeviceTransientState_Types, error) { |
Maninder | a496f85 | 2021-02-22 09:57:56 +0530 | [diff] [blame] | 742 | agent := dMgr.getDeviceAgent(ctx, id) |
| 743 | if agent == nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 744 | return core.DeviceTransientState_NONE, status.Errorf(codes.NotFound, "%s", id) |
Maninder | a496f85 | 2021-02-22 09:57:56 +0530 | [diff] [blame] | 745 | } |
| 746 | return agent.getTransientState(), nil |
| 747 | } |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 748 | |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 749 | func (dMgr *Manager) validateImageDownloadRequest(request *voltha.DeviceImageDownloadRequest) error { |
| 750 | if request == nil || request.Image == nil || len(request.DeviceId) == 0 { |
| 751 | return status.Errorf(codes.InvalidArgument, "invalid argument") |
| 752 | } |
| 753 | |
| 754 | for _, deviceID := range request.DeviceId { |
| 755 | if deviceID == nil { |
| 756 | return status.Errorf(codes.InvalidArgument, "id is nil") |
| 757 | } |
| 758 | } |
| 759 | return nil |
| 760 | } |
| 761 | |
| 762 | func (dMgr *Manager) validateImageRequest(request *voltha.DeviceImageRequest) error { |
| 763 | if request == nil || len(request.DeviceId) == 0 || request.DeviceId[0] == nil { |
| 764 | return status.Errorf(codes.InvalidArgument, "invalid argument") |
| 765 | } |
| 766 | |
| 767 | for _, deviceID := range request.DeviceId { |
| 768 | if deviceID == nil { |
| 769 | return status.Errorf(codes.InvalidArgument, "id is nil") |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | return nil |
| 774 | } |
| 775 | |
| 776 | func (dMgr *Manager) validateDeviceImageResponse(response *voltha.DeviceImageResponse) error { |
| 777 | if response == nil || len(response.GetDeviceImageStates()) == 0 || response.GetDeviceImageStates()[0] == nil { |
| 778 | return status.Errorf(codes.Internal, "invalid-response-from-adapter") |
| 779 | } |
| 780 | |
| 781 | return nil |
| 782 | } |
| 783 | |
| 784 | func (dMgr *Manager) waitForAllResponses(ctx context.Context, opName string, respCh chan []*voltha.DeviceImageState, expectedResps int) (*voltha.DeviceImageResponse, error) { |
| 785 | response := &voltha.DeviceImageResponse{} |
| 786 | respCount := 0 |
| 787 | for { |
| 788 | select { |
| 789 | case resp, ok := <-respCh: |
| 790 | if !ok { |
| 791 | logger.Errorw(ctx, opName+"-failed", log.Fields{"error": "channel-closed"}) |
| 792 | return response, status.Errorf(codes.Aborted, "channel-closed") |
| 793 | } |
| 794 | |
| 795 | if resp != nil { |
| 796 | logger.Debugw(ctx, opName+"-result", log.Fields{"image-state": resp[0].GetImageState(), "device-id": resp[0].GetDeviceId()}) |
| 797 | response.DeviceImageStates = append(response.DeviceImageStates, resp...) |
| 798 | } |
| 799 | |
| 800 | respCount++ |
| 801 | |
| 802 | //check whether all responses received, if so, sent back the collated response |
| 803 | if respCount == expectedResps { |
| 804 | return response, nil |
| 805 | } |
| 806 | continue |
| 807 | case <-ctx.Done(): |
| 808 | return nil, status.Errorf(codes.Aborted, opName+"-failed-%s", ctx.Err()) |
| 809 | } |
| 810 | } |
| 811 | } |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 812 | |
| 813 | func (dMgr *Manager) ReconcilingCleanup(ctx context.Context, device *voltha.Device) error { |
| 814 | agent := dMgr.getDeviceAgent(ctx, device.Id) |
| 815 | if agent == nil { |
| 816 | logger.Errorf(ctx, "Not able to get device agent.") |
| 817 | return status.Errorf(codes.NotFound, "Not able to get device agent for device : %s", device.Id) |
| 818 | } |
| 819 | err := agent.reconcilingCleanup(ctx) |
| 820 | if err != nil { |
| 821 | logger.Errorf(ctx, err.Error()) |
| 822 | return status.Errorf(codes.Internal, err.Error()) |
| 823 | } |
| 824 | return nil |
| 825 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 826 | |
| 827 | func (dMgr *Manager) adapterRestartedHandler(ctx context.Context, endpoint string) error { |
| 828 | // Get the adapter corresponding to that endpoint |
| 829 | if a, _ := dMgr.adapterMgr.GetAdapterWithEndpoint(ctx, endpoint); a != nil { |
| 830 | return dMgr.adapterRestarted(ctx, a) |
| 831 | } |
| 832 | logger.Errorw(ctx, "restarted-adapter-not-found", log.Fields{"endpoint": endpoint}) |
| 833 | return fmt.Errorf("restarted adapter at endpoint %s not found", endpoint) |
| 834 | } |