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