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" |
| 21 | "errors" |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 22 | "sync" |
| 23 | "time" |
| 24 | |
serkant.uluderya | e25f648 | 2020-12-17 21:08:38 +0300 | [diff] [blame] | 25 | "github.com/opencord/voltha-go/rw_core/config" |
| 26 | "github.com/opencord/voltha-lib-go/v5/pkg/probe" |
| 27 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 28 | "github.com/golang/protobuf/ptypes/empty" |
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 | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 32 | "github.com/opencord/voltha-go/rw_core/core/device/remote" |
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" |
yasin sapli | 5458a1c | 2021-06-14 22:24:38 +0000 | [diff] [blame] | 35 | "github.com/opencord/voltha-lib-go/v5/pkg/events" |
| 36 | "github.com/opencord/voltha-lib-go/v5/pkg/kafka" |
| 37 | "github.com/opencord/voltha-lib-go/v5/pkg/log" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 38 | "github.com/opencord/voltha-protos/v4/go/common" |
| 39 | ic "github.com/opencord/voltha-protos/v4/go/inter_container" |
| 40 | "github.com/opencord/voltha-protos/v4/go/openflow_13" |
| 41 | ofp "github.com/opencord/voltha-protos/v4/go/openflow_13" |
| 42 | "github.com/opencord/voltha-protos/v4/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 43 | "google.golang.org/grpc/codes" |
| 44 | "google.golang.org/grpc/status" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 45 | ) |
| 46 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 47 | // Manager represent device manager attributes |
| 48 | type Manager struct { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 49 | deviceAgents sync.Map |
| 50 | rootDevices map[string]bool |
| 51 | lockRootDeviceMap sync.RWMutex |
| 52 | adapterProxy *remote.AdapterProxy |
Mahir Gunyel | b0343bf | 2021-05-11 14:14:26 -0700 | [diff] [blame] | 53 | *event.Agent |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 54 | adapterMgr *adapter.Manager |
| 55 | logicalDeviceMgr *LogicalManager |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 56 | kafkaICProxy kafka.InterContainerProxy |
Kent Hagerman | 6031aad | 2020-07-29 16:36:33 -0400 | [diff] [blame] | 57 | stateTransitions *state.TransitionMap |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 58 | dbPath *model.Path |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 59 | dProxy *model.Proxy |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 60 | coreInstanceID string |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 61 | defaultTimeout time.Duration |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 62 | devicesLoadingLock sync.RWMutex |
| 63 | deviceLoadingInProgress map[string][]chan int |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 64 | config *config.RWCoreFlags |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 67 | //NewManagers creates the Manager and the Logical Manager. |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 68 | func NewManagers(dbPath *model.Path, adapterMgr *adapter.Manager, kmp kafka.InterContainerProxy, endpointMgr kafka.EndpointManager, cf *config.RWCoreFlags, coreInstanceID string, eventProxy *events.EventProxy) (*Manager, *LogicalManager) { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 69 | deviceMgr := &Manager{ |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 70 | rootDevices: make(map[string]bool), |
| 71 | kafkaICProxy: kmp, |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 72 | adapterProxy: remote.NewAdapterProxy(kmp, cf.CoreTopic, endpointMgr), |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 73 | coreInstanceID: coreInstanceID, |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 74 | dbPath: dbPath, |
| 75 | dProxy: dbPath.Proxy("devices"), |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 76 | adapterMgr: adapterMgr, |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 77 | defaultTimeout: cf.DefaultCoreTimeout, |
| 78 | Agent: event.NewAgent(eventProxy, coreInstanceID, cf.VolthaStackID), |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 79 | deviceLoadingInProgress: make(map[string][]chan int), |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 80 | config: cf, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 81 | } |
Kent Hagerman | 6031aad | 2020-07-29 16:36:33 -0400 | [diff] [blame] | 82 | deviceMgr.stateTransitions = state.NewTransitionMap(deviceMgr) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 83 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 84 | logicalDeviceMgr := &LogicalManager{ |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 85 | Manager: event.NewManager(eventProxy, coreInstanceID, cf.VolthaStackID), |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 86 | deviceMgr: deviceMgr, |
| 87 | kafkaICProxy: kmp, |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 88 | dbPath: dbPath, |
| 89 | ldProxy: dbPath.Proxy("logical_devices"), |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 90 | defaultTimeout: cf.DefaultCoreTimeout, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 91 | logicalDeviceLoadingInProgress: make(map[string][]chan int), |
| 92 | } |
| 93 | deviceMgr.logicalDeviceMgr = logicalDeviceMgr |
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 | adapterMgr.SetAdapterRestartedCallback(deviceMgr.adapterRestarted) |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 96 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 97 | return deviceMgr, logicalDeviceMgr |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 98 | } |
| 99 | |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 100 | func (dMgr *Manager) Start(ctx context.Context) { |
| 101 | logger.Info(ctx, "starting-device-manager") |
| 102 | probe.UpdateStatusFromContext(ctx, "device-manager", probe.ServiceStatusPreparing) |
| 103 | |
| 104 | // Load all the devices from the dB |
| 105 | var devices []*voltha.Device |
| 106 | if err := dMgr.dProxy.List(ctx, &devices); err != nil { |
| 107 | // Any error from the dB means if we proceed we may end up with corrupted data |
| 108 | logger.Fatalw(ctx, "failed-to-list-devices-from-KV", log.Fields{"error": err}) |
| 109 | } |
| 110 | |
| 111 | for _, device := range devices { |
| 112 | // Create an agent for each device |
| 113 | agent := newAgent(dMgr.adapterProxy, device, dMgr, dMgr.dbPath, dMgr.dProxy, dMgr.defaultTimeout) |
| 114 | if _, err := agent.start(ctx, true, device); err != nil { |
| 115 | logger.Warnw(ctx, "failure-starting-agent", log.Fields{"device-id": device.Id}) |
| 116 | } else { |
| 117 | dMgr.addDeviceAgentToMap(agent) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // TODO: Need to trigger a reconcile at this point |
| 122 | |
| 123 | probe.UpdateStatusFromContext(ctx, "device-manager", probe.ServiceStatusRunning) |
| 124 | logger.Info(ctx, "device-manager-started") |
| 125 | } |
| 126 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 127 | func (dMgr *Manager) addDeviceAgentToMap(agent *Agent) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 128 | if _, exist := dMgr.deviceAgents.Load(agent.deviceID); !exist { |
| 129 | dMgr.deviceAgents.Store(agent.deviceID, agent) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 130 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 131 | dMgr.lockRootDeviceMap.Lock() |
| 132 | defer dMgr.lockRootDeviceMap.Unlock() |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 133 | dMgr.rootDevices[agent.deviceID] = agent.isRootDevice |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 134 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 135 | } |
| 136 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 137 | func (dMgr *Manager) deleteDeviceAgentFromMap(agent *Agent) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 138 | dMgr.deviceAgents.Delete(agent.deviceID) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 139 | dMgr.lockRootDeviceMap.Lock() |
| 140 | defer dMgr.lockRootDeviceMap.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 141 | delete(dMgr.rootDevices, agent.deviceID) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 142 | } |
| 143 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 144 | // 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] | 145 | func (dMgr *Manager) getDeviceAgent(ctx context.Context, deviceID string) *Agent { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 146 | agent, ok := dMgr.deviceAgents.Load(deviceID) |
| 147 | if ok { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 148 | return agent.(*Agent) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 149 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 150 | // 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] | 151 | err := dMgr.load(ctx, deviceID) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 152 | if err == nil { |
| 153 | agent, ok = dMgr.deviceAgents.Load(deviceID) |
| 154 | if !ok { |
| 155 | return nil |
| 156 | } |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 157 | return agent.(*Agent) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 158 | } |
| 159 | //TODO: Change the return params to return an error as well |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 160 | logger.Errorw(ctx, "loading-device-failed", log.Fields{"device-id": deviceID, "error": err}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 161 | return nil |
| 162 | } |
| 163 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 164 | // listDeviceIdsFromMap returns the list of device IDs that are in memory |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 165 | func (dMgr *Manager) listDeviceIdsFromMap() *voltha.IDs { |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 166 | result := &voltha.IDs{Items: make([]*voltha.ID, 0)} |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 167 | |
| 168 | dMgr.deviceAgents.Range(func(key, value interface{}) bool { |
| 169 | result.Items = append(result.Items, &voltha.ID{Id: key.(string)}) |
| 170 | return true |
| 171 | }) |
| 172 | |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 173 | return result |
| 174 | } |
| 175 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 176 | // CreateDevice creates a new parent device in the data model |
| 177 | func (dMgr *Manager) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) { |
| 178 | if device.MacAddress == "" && device.GetHostAndPort() == "" { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 179 | logger.Errorf(ctx, "no-device-info-present") |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 180 | return &voltha.Device{}, errors.New("no-device-info-present; MAC or HOSTIP&PORT") |
| 181 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 182 | ctx = utils.WithRPCMetadataContext(ctx, "CreateDevice") |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 183 | logger.Debugw(ctx, "create-device", log.Fields{"device": *device}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 184 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 185 | deviceExist, err := dMgr.isParentDeviceExist(ctx, device) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 186 | if err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 187 | logger.Errorf(ctx, "failed-to-fetch-parent-device-info") |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 188 | return nil, err |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 189 | } |
| 190 | if deviceExist { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 191 | logger.Errorf(ctx, "device-is-pre-provisioned-already-with-same-ip-port-or-mac-address") |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 192 | return nil, errors.New("device is already pre-provisioned") |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 193 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 194 | logger.Debugw(ctx, "create-device", log.Fields{"device": device, "aproxy": dMgr.adapterProxy}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 195 | |
khenaidoo | 5e677ae | 2019-02-28 17:26:29 -0500 | [diff] [blame] | 196 | // Ensure this device is set as root |
| 197 | device.Root = true |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 198 | // Create and start a device agent for that device |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 199 | agent := newAgent(dMgr.adapterProxy, device, dMgr, dMgr.dbPath, dMgr.dProxy, dMgr.defaultTimeout) |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 200 | device, err = agent.start(ctx, false, device) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 201 | if err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 202 | logger.Errorw(ctx, "fail-to-start-device", log.Fields{"device-id": agent.deviceID, "error": err}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 203 | return nil, err |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 204 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 205 | dMgr.addDeviceAgentToMap(agent) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 206 | return device, nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 207 | } |
| 208 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 209 | // EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter |
| 210 | func (dMgr *Manager) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 211 | ctx = utils.WithRPCMetadataContext(ctx, "EnableDevice") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 212 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 213 | logger.Debugw(ctx, "enable-device", log.Fields{"device-id": id.Id}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 214 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 215 | if agent == nil { |
| 216 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 217 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 218 | return &empty.Empty{}, agent.enableDevice(ctx) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 219 | } |
| 220 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 221 | // DisableDevice disables a device along with any child device it may have |
| 222 | func (dMgr *Manager) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 223 | ctx = utils.WithRPCMetadataContext(ctx, "DisableDevice") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 224 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 225 | logger.Debugw(ctx, "disable-device", log.Fields{"device-id": id.Id}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 226 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 227 | if agent == nil { |
| 228 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 229 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 230 | return &empty.Empty{}, agent.disableDevice(ctx) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 231 | } |
| 232 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 233 | //RebootDevice invoked the reboot API to the corresponding adapter |
| 234 | func (dMgr *Manager) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 235 | ctx = utils.WithRPCMetadataContext(ctx, "RebootDevice") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 236 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 237 | logger.Debugw(ctx, "reboot-device", log.Fields{"device-id": id.Id}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 238 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 239 | if agent == nil { |
| 240 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 241 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 242 | return &empty.Empty{}, agent.rebootDevice(ctx) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 243 | } |
| 244 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 245 | // DeleteDevice removes a device from the data model |
| 246 | func (dMgr *Manager) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 247 | ctx = utils.WithRPCMetadataContext(ctx, "DeleteDevice") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 248 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 249 | logger.Debugw(ctx, "delete-device", log.Fields{"device-id": id.Id}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 250 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 251 | if agent == nil { |
| 252 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 253 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 254 | return &empty.Empty{}, agent.deleteDevice(ctx) |
| 255 | } |
| 256 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 257 | // ForceDeleteDevice removes a device from the data model forcefully without successfully waiting for the adapters. |
| 258 | func (dMgr *Manager) ForceDeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 259 | ctx = utils.WithRPCMetadataContext(ctx, "ForceDeleteDevice") |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 260 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 261 | logger.Debugw(ctx, "force-delete-device", log.Fields{"device-id": id.Id}) |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 262 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 263 | if agent == nil { |
| 264 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
| 265 | } |
| 266 | return &empty.Empty{}, agent.deleteDeviceForce(ctx) |
| 267 | } |
| 268 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 269 | // GetDevicePort returns the port details for a specific device port entry |
| 270 | func (dMgr *Manager) GetDevicePort(ctx context.Context, deviceID string, portID uint32) (*voltha.Port, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 271 | logger.Debugw(ctx, "get-device-port", log.Fields{"device-id": deviceID}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 272 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
| 273 | if agent == nil { |
| 274 | return nil, status.Errorf(codes.NotFound, "device-%s", deviceID) |
| 275 | } |
| 276 | return agent.getDevicePort(portID) |
| 277 | } |
| 278 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 279 | // ListDevicePorts returns the ports details for a specific device entry |
| 280 | func (dMgr *Manager) ListDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.Ports, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 281 | ctx = utils.WithRPCMetadataContext(ctx, "ListDevicePorts") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 282 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 283 | logger.Debugw(ctx, "list-device-ports", log.Fields{"device-id": id.Id}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 284 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 285 | if agent == nil { |
| 286 | return nil, status.Errorf(codes.NotFound, "device-%s", id.Id) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 287 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 288 | |
| 289 | ports := agent.listDevicePorts() |
| 290 | ctr, ret := 0, make([]*voltha.Port, len(ports)) |
| 291 | for _, port := range ports { |
| 292 | ret[ctr] = port |
| 293 | ctr++ |
| 294 | } |
| 295 | return &voltha.Ports{Items: ret}, nil |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | // ListDeviceFlows returns the flow details for a specific device entry |
| 299 | func (dMgr *Manager) ListDeviceFlows(ctx context.Context, id *voltha.ID) (*ofp.Flows, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 300 | ctx = utils.WithRPCMetadataContext(ctx, "ListDeviceFlows") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 301 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 302 | logger.Debugw(ctx, "list-device-flows", log.Fields{"device-id": id.Id}) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 303 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 304 | if agent == nil { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 305 | return nil, status.Errorf(codes.NotFound, "device-%s", id.Id) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 306 | } |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 307 | |
| 308 | flows := agent.listDeviceFlows() |
| 309 | ctr, ret := 0, make([]*ofp.OfpFlowStats, len(flows)) |
| 310 | for _, flow := range flows { |
| 311 | ret[ctr] = flow |
| 312 | ctr++ |
| 313 | } |
| 314 | return &openflow_13.Flows{Items: ret}, nil |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | // ListDeviceFlowGroups returns the flow group details for a specific device entry |
| 318 | func (dMgr *Manager) ListDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*voltha.FlowGroups, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 319 | ctx = utils.WithRPCMetadataContext(ctx, "ListDeviceFlowGroups") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 320 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 321 | logger.Debugw(ctx, "list-device-flow-groups", log.Fields{"device-id": id.Id}) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 322 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 323 | if agent == nil { |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 324 | return nil, status.Errorf(codes.NotFound, "device-%s", id.Id) |
| 325 | } |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 326 | groups := agent.listDeviceGroups() |
| 327 | ctr, ret := 0, make([]*openflow_13.OfpGroupEntry, len(groups)) |
| 328 | for _, group := range groups { |
| 329 | ret[ctr] = group |
| 330 | ctr++ |
| 331 | } |
| 332 | return &voltha.FlowGroups{Items: ret}, nil |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 333 | } |
| 334 | |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 335 | // stopManagingDevice stops the management of the device as well as any of its reference device and logical device. |
| 336 | // This function is called only in the Core that does not own this device. In the Core that owns this device then a |
| 337 | // deletion deletion also includes removal of any reference of this device. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 338 | func (dMgr *Manager) stopManagingDevice(ctx context.Context, id string) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 339 | logger.Infow(ctx, "stop-managing-device", log.Fields{"device-id": id}) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 340 | 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] | 341 | if device, err := dMgr.getDeviceReadOnly(ctx, id); err == nil && device.Root { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 342 | // stop managing the logical device |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 343 | _ = dMgr.logicalDeviceMgr.stopManagingLogicalDeviceWithDeviceID(ctx, id) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 344 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 345 | if agent := dMgr.getDeviceAgent(ctx, id); agent != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 346 | if err := agent.stop(ctx); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 347 | 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] | 348 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 349 | dMgr.deleteDeviceAgentFromMap(agent) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 354 | // RunPostDeviceDelete removes any reference of this device |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 355 | func (dMgr *Manager) RunPostDeviceDelete(ctx context.Context, cDevice *voltha.Device) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 356 | logger.Infow(ctx, "run-post-device-delete", log.Fields{"device-id": cDevice.Id}) |
Andrea Campanella | 408dec6 | 2021-11-10 14:32:10 +0100 | [diff] [blame^] | 357 | //deleting the logical device |
| 358 | logger.Debugw(ctx, "delete-logical-device", log.Fields{"device-id": cDevice.Id}) |
| 359 | if dMgr.logicalDeviceMgr != nil && cDevice.Root { |
| 360 | if err := dMgr.logicalDeviceMgr.deleteLogicalDevice(ctx, cDevice); err != nil { |
| 361 | logger.Warnw(ctx, "failure-delete-logical-device", log.Fields{"device-id": cDevice.Id, "error": err.Error()}) |
| 362 | } |
| 363 | // Remove the logical device Id from the parent device |
| 364 | logicalID := "" |
| 365 | dMgr.UpdateDeviceAttribute(ctx, cDevice.Id, "ParentId", logicalID) |
| 366 | } |
serkant.uluderya | e25f648 | 2020-12-17 21:08:38 +0300 | [diff] [blame] | 367 | if agent := dMgr.getDeviceAgent(ctx, cDevice.Id); agent != nil { |
| 368 | logger.Debugw(ctx, "invoking-delete-device-ports", log.Fields{"device-id": cDevice.Id}) |
| 369 | //delete ports |
| 370 | if err := agent.deleteAllPorts(ctx); err != nil { |
| 371 | logger.Warnw(ctx, "failure-delete-device-ports", log.Fields{"device-id": cDevice.Id, "error": err.Error()}) |
| 372 | } |
| 373 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 374 | dMgr.stopManagingDevice(ctx, cDevice.Id) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 375 | return nil |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 376 | } |
| 377 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 378 | // GetDevice exists primarily to implement the gRPC interface. |
| 379 | // Internal functions should call getDeviceReadOnly instead. |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 380 | func (dMgr *Manager) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 381 | ctx = utils.WithRPCMetadataContext(ctx, "GetDevice") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 382 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 383 | return dMgr.getDeviceReadOnly(ctx, id.Id) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 384 | } |
| 385 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 386 | // getDeviceReadOnly will returns a device, either from memory or from the dB, if present |
| 387 | func (dMgr *Manager) getDeviceReadOnly(ctx context.Context, id string) (*voltha.Device, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 388 | logger.Debugw(ctx, "get-device-read-only", log.Fields{"device-id": id}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 389 | if agent := dMgr.getDeviceAgent(ctx, id); agent != nil { |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 390 | return agent.getDeviceReadOnly(ctx) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 391 | } |
| 392 | return nil, status.Errorf(codes.NotFound, "%s", id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 393 | } |
| 394 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 395 | 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] | 396 | logger.Debugw(ctx, "list-device-ports", log.Fields{"device-id": id}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 397 | agent := dMgr.getDeviceAgent(ctx, id) |
| 398 | if agent == nil { |
| 399 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 400 | } |
| 401 | return agent.listDevicePorts(), nil |
| 402 | } |
| 403 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 404 | // GetChildDevice will return a device, either from memory or from the dB, if present |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 405 | func (dMgr *Manager) GetChildDevice(ctx context.Context, parentDeviceID string, serialNumber string, onuID int64, parentPortNo int64) (*voltha.Device, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 406 | logger.Debugw(ctx, "get-child-device", log.Fields{"parent-device-id": parentDeviceID, "serialNumber": serialNumber, |
| 407 | "parent-port-no": parentPortNo, "onu-id": onuID}) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 408 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 409 | parentDevicePorts, err := dMgr.listDevicePorts(ctx, parentDeviceID) |
| 410 | if err != nil { |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 411 | return nil, status.Errorf(codes.Aborted, "%s", err.Error()) |
| 412 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 413 | childDeviceIds := dMgr.getAllChildDeviceIds(ctx, parentDevicePorts) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 414 | if len(childDeviceIds) == 0 { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 415 | logger.Debugw(ctx, "no-child-devices", log.Fields{"parent-device-id": parentDeviceID, "serial-number": serialNumber, "onu-id": onuID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 416 | return nil, status.Errorf(codes.NotFound, "%s", parentDeviceID) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | var foundChildDevice *voltha.Device |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 420 | for childDeviceID := range childDeviceIds { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 421 | var found bool |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 422 | if searchDevice, err := dMgr.getDeviceReadOnly(ctx, childDeviceID); err == nil { |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 423 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 424 | foundOnuID := false |
| 425 | if searchDevice.ProxyAddress.OnuId == uint32(onuID) { |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 426 | if searchDevice.ParentPortNo == uint32(parentPortNo) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 427 | logger.Debugw(ctx, "found-child-by-onuid", log.Fields{"parent-device-id": parentDeviceID, "onu-id": onuID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 428 | foundOnuID = true |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
| 432 | foundSerialNumber := false |
| 433 | if searchDevice.SerialNumber == serialNumber { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 434 | logger.Debugw(ctx, "found-child-by-serial-number", log.Fields{"parent-device-id": parentDeviceID, "serial-number": serialNumber}) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 435 | foundSerialNumber = true |
| 436 | } |
| 437 | |
| 438 | // if both onuId and serialNumber are provided both must be true for the device to be found |
| 439 | // otherwise whichever one found a match is good enough |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 440 | if onuID > 0 && serialNumber != "" { |
| 441 | found = foundOnuID && foundSerialNumber |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 442 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 443 | found = foundOnuID || foundSerialNumber |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 444 | } |
| 445 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 446 | if found { |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 447 | foundChildDevice = searchDevice |
| 448 | break |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | if foundChildDevice != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 454 | logger.Debugw(ctx, "child-device-found", log.Fields{"parent-device-id": parentDeviceID, "found-child-device": foundChildDevice}) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 455 | return foundChildDevice, nil |
| 456 | } |
| 457 | |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 458 | logger.Debugw(ctx, "child-device-not-found", log.Fields{"parent-device-id": parentDeviceID, |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 459 | "serial-number": serialNumber, "onu-id": onuID, "parent-port-no": parentPortNo}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 460 | return nil, status.Errorf(codes.NotFound, "%s", parentDeviceID) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 461 | } |
| 462 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 463 | // GetChildDeviceWithProxyAddress will return a device based on proxy address |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 464 | func (dMgr *Manager) GetChildDeviceWithProxyAddress(ctx context.Context, proxyAddress *voltha.Device_ProxyAddress) (*voltha.Device, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 465 | logger.Debugw(ctx, "get-child-device-with-proxy-address", log.Fields{"proxy-address": proxyAddress}) |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 466 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 467 | parentDevicePorts, err := dMgr.listDevicePorts(ctx, proxyAddress.DeviceId) |
| 468 | if err != nil { |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 469 | return nil, status.Errorf(codes.Aborted, "%s", err.Error()) |
| 470 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 471 | childDeviceIds := dMgr.getAllChildDeviceIds(ctx, parentDevicePorts) |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 472 | if len(childDeviceIds) == 0 { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 473 | logger.Debugw(ctx, "no-child-devices", log.Fields{"parent-device-id": proxyAddress.DeviceId}) |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 474 | return nil, status.Errorf(codes.NotFound, "%s", proxyAddress) |
| 475 | } |
| 476 | |
| 477 | var foundChildDevice *voltha.Device |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 478 | for childDeviceID := range childDeviceIds { |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 479 | if searchDevice, err := dMgr.getDeviceReadOnly(ctx, childDeviceID); err == nil { |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 480 | if searchDevice.ProxyAddress == proxyAddress { |
| 481 | foundChildDevice = searchDevice |
| 482 | break |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | if foundChildDevice != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 488 | logger.Debugw(ctx, "child-device-found", log.Fields{"proxy-address": proxyAddress}) |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 489 | return foundChildDevice, nil |
| 490 | } |
| 491 | |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 492 | logger.Warnw(ctx, "child-device-not-found", log.Fields{"proxy-address": proxyAddress}) |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 493 | return nil, status.Errorf(codes.NotFound, "%s", proxyAddress) |
| 494 | } |
| 495 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 496 | // IsDeviceInCache returns true if device is found in the map |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 497 | func (dMgr *Manager) IsDeviceInCache(id string) bool { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 498 | _, exist := dMgr.deviceAgents.Load(id) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 499 | return exist |
| 500 | } |
| 501 | |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 502 | // ListDevices retrieves the latest devices from the data model |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 503 | func (dMgr *Manager) ListDevices(ctx context.Context, _ *empty.Empty) (*voltha.Devices, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 504 | ctx = utils.WithRPCMetadataContext(ctx, "ListDevices") |
| 505 | logger.Debug(ctx, "list-devices") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 506 | result := &voltha.Devices{} |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 507 | |
Esin Karaman | a4e4e00 | 2021-03-02 10:42:16 +0000 | [diff] [blame] | 508 | dMgr.deviceAgents.Range(func(key, value interface{}) bool { |
| 509 | result.Items = append(result.Items, value.(*Agent).device) |
| 510 | return true |
| 511 | }) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 512 | |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 513 | logger.Debugw(ctx, "list-devices-end", log.Fields{"len": len(result.Items)}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 514 | return result, nil |
| 515 | } |
| 516 | |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 517 | //isParentDeviceExist checks whether device is already preprovisioned. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 518 | 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] | 519 | hostPort := newDevice.GetHostAndPort() |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 520 | var devices []*voltha.Device |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 521 | if err := dMgr.dProxy.List(ctx, &devices); err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 522 | 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] | 523 | return false, err |
| 524 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 525 | for _, device := range devices { |
| 526 | if !device.Root { |
| 527 | continue |
| 528 | } |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 529 | |
| 530 | if hostPort != "" && hostPort == device.GetHostAndPort() { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 531 | return true, nil |
| 532 | } |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 533 | if newDevice.MacAddress != "" && newDevice.MacAddress == device.MacAddress { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 534 | return true, nil |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 535 | } |
| 536 | } |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 537 | return false, nil |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 538 | } |
| 539 | |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 540 | //getDeviceFromModelretrieves the device data from the model. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 541 | func (dMgr *Manager) getDeviceFromModel(ctx context.Context, deviceID string) (*voltha.Device, error) { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 542 | device := &voltha.Device{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 543 | if have, err := dMgr.dProxy.Get(ctx, deviceID, device); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 544 | 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] | 545 | return nil, err |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 546 | } else if !have { |
| 547 | return nil, status.Error(codes.NotFound, deviceID) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 548 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 549 | |
| 550 | return device, nil |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 551 | } |
| 552 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 553 | // loadDevice loads the deviceID in memory, if not present |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 554 | func (dMgr *Manager) loadDevice(ctx context.Context, deviceID string) (*Agent, error) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 555 | if deviceID == "" { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 556 | return nil, status.Error(codes.InvalidArgument, "deviceId empty") |
| 557 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 558 | var err error |
| 559 | var device *voltha.Device |
| 560 | dMgr.devicesLoadingLock.Lock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 561 | if _, exist := dMgr.deviceLoadingInProgress[deviceID]; !exist { |
| 562 | if !dMgr.IsDeviceInCache(deviceID) { |
| 563 | dMgr.deviceLoadingInProgress[deviceID] = []chan int{make(chan int, 1)} |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 564 | dMgr.devicesLoadingLock.Unlock() |
| 565 | // 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] | 566 | if device, err = dMgr.getDeviceFromModel(ctx, deviceID); err == nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 567 | logger.Debugw(ctx, "loading-device", log.Fields{"device-id": deviceID}) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 568 | agent := newAgent(dMgr.adapterProxy, device, dMgr, dMgr.dbPath, dMgr.dProxy, dMgr.defaultTimeout) |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 569 | if _, err = agent.start(ctx, true, device); err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 570 | logger.Warnw(ctx, "failure-loading-device", log.Fields{"device-id": deviceID, "error": err}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 571 | } else { |
| 572 | dMgr.addDeviceAgentToMap(agent) |
| 573 | } |
| 574 | } else { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 575 | logger.Debugw(ctx, "device-is-not-in-model", log.Fields{"device-id": deviceID}) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 576 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 577 | // announce completion of task to any number of waiting channels |
| 578 | dMgr.devicesLoadingLock.Lock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 579 | if v, ok := dMgr.deviceLoadingInProgress[deviceID]; ok { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 580 | for _, ch := range v { |
| 581 | close(ch) |
| 582 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 583 | delete(dMgr.deviceLoadingInProgress, deviceID) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 584 | } |
| 585 | dMgr.devicesLoadingLock.Unlock() |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 586 | } else { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 587 | dMgr.devicesLoadingLock.Unlock() |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 588 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 589 | } else { |
| 590 | ch := make(chan int, 1) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 591 | dMgr.deviceLoadingInProgress[deviceID] = append(dMgr.deviceLoadingInProgress[deviceID], ch) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 592 | dMgr.devicesLoadingLock.Unlock() |
| 593 | // Wait for the channel to be closed, implying the process loading this device is done. |
| 594 | <-ch |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 595 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 596 | if agent, ok := dMgr.deviceAgents.Load(deviceID); ok { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 597 | return agent.(*Agent), nil |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 598 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 599 | return nil, status.Errorf(codes.Aborted, "Error loading device %s", deviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | // loadRootDeviceParentAndChildren loads the children and parents of a root device in memory |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 603 | 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] | 604 | logger.Debugw(ctx, "loading-parent-and-children", log.Fields{"device-id": device.Id}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 605 | if device.Root { |
| 606 | // Scenario A |
| 607 | if device.ParentId != "" { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 608 | // Load logical device if needed. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 609 | if err := dMgr.logicalDeviceMgr.load(ctx, device.ParentId); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 610 | 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] | 611 | } |
| 612 | } else { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 613 | logger.Debugw(ctx, "no-parent-to-load", log.Fields{"device-id": device.Id}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 614 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 615 | // Load all child devices, if needed |
| 616 | childDeviceIds := dMgr.getAllChildDeviceIds(ctx, devicePorts) |
| 617 | for childDeviceID := range childDeviceIds { |
| 618 | if _, err := dMgr.loadDevice(ctx, childDeviceID); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 619 | 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] | 620 | return err |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 621 | } |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 622 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 623 | 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] | 624 | } |
| 625 | return nil |
| 626 | } |
| 627 | |
| 628 | // load loads the deviceId in memory, if not present, and also loads its accompanying parents and children. Loading |
| 629 | // in memory is for improved performance. It is not imperative that a device needs to be in memory when a request |
| 630 | // acting on the device is received by the core. In such a scenario, the Core will load the device in memory first |
| 631 | // and the proceed with the request. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 632 | func (dMgr *Manager) load(ctx context.Context, deviceID string) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 633 | logger.Debug(ctx, "load...") |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 634 | // 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] | 635 | var dAgent *Agent |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 636 | var err error |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 637 | if dAgent, err = dMgr.loadDevice(ctx, deviceID); err != nil { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 638 | return err |
| 639 | } |
| 640 | // Get the loaded device details |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 641 | device, err := dAgent.getDeviceReadOnly(ctx) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 642 | if err != nil { |
| 643 | return err |
| 644 | } |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 645 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 646 | // If the device is in Pre-provisioning or getting deleted state stop here |
| 647 | if device.AdminState == voltha.AdminState_PREPROVISIONED || dAgent.isDeletionInProgress() { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 648 | return nil |
| 649 | } |
| 650 | |
| 651 | // Now we face two scenarios |
| 652 | if device.Root { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 653 | devicePorts := dAgent.listDevicePorts() |
| 654 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 655 | // 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] | 656 | if err := dMgr.loadRootDeviceParentAndChildren(ctx, device, devicePorts); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 657 | 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] | 658 | return err |
| 659 | } |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 660 | 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] | 661 | } else if device.ParentId != "" { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 662 | // 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] | 663 | return dMgr.load(ctx, device.ParentId) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 664 | } |
Andrey Pozolotin | 34dd63f | 2021-05-31 21:26:40 +0300 | [diff] [blame] | 665 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 666 | return nil |
| 667 | } |
| 668 | |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 669 | // ListDeviceIds retrieves the latest device IDs information from the data model (memory data only) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 670 | func (dMgr *Manager) ListDeviceIds(ctx context.Context, _ *empty.Empty) (*voltha.IDs, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 671 | ctx = utils.WithRPCMetadataContext(ctx, "ListDeviceIds") |
| 672 | logger.Debug(ctx, "list-device-ids") |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 673 | // Report only device IDs that are in the device agent map |
| 674 | return dMgr.listDeviceIdsFromMap(), nil |
| 675 | } |
| 676 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 677 | // ReconcileDevices is a request to a voltha core to update its list of managed devices. This will |
| 678 | // trigger loading the devices along with their children and parent in memory |
| 679 | func (dMgr *Manager) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 680 | ctx = utils.WithRPCMetadataContext(ctx, "ReconcileDevices") |
Andrey Pozolotin | 34dd63f | 2021-05-31 21:26:40 +0300 | [diff] [blame] | 681 | |
| 682 | numDevices := 0 |
| 683 | if ids != nil { |
| 684 | numDevices = len(ids.Items) |
| 685 | } |
| 686 | |
| 687 | logger.Debugw(ctx, "reconcile-devices", log.Fields{"num-devices": numDevices}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 688 | if ids != nil && len(ids.Items) != 0 { |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 689 | toReconcile := len(ids.Items) |
| 690 | reconciled := 0 |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 691 | var err error |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 692 | for _, id := range ids.Items { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 693 | if err = dMgr.load(ctx, id.Id); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 694 | logger.Warnw(ctx, "failure-reconciling-device", log.Fields{"device-id": id.Id, "error": err}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 695 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 696 | reconciled++ |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | if toReconcile != reconciled { |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 700 | return nil, status.Errorf(codes.DataLoss, "less-device-reconciled-than-requested:%d/%d", reconciled, toReconcile) |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 701 | } |
| 702 | } else { |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 703 | return nil, status.Errorf(codes.InvalidArgument, "empty-list-of-ids") |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 704 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 705 | return &empty.Empty{}, nil |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 706 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 707 | |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 708 | // adapterRestarted is invoked whenever an adapter is restarted |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 709 | func (dMgr *Manager) adapterRestarted(ctx context.Context, adapter *voltha.Adapter) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 710 | logger.Debugw(ctx, "adapter-restarted", log.Fields{"adapter-id": adapter.Id, "vendor": adapter.Vendor, |
| 711 | "current-replica": adapter.CurrentReplica, "total-replicas": adapter.TotalReplicas, "endpoint": adapter.Endpoint}) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 712 | |
| 713 | // Let's reconcile the device managed by this Core only |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 714 | if len(dMgr.rootDevices) == 0 { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 715 | logger.Debugw(ctx, "nothing-to-reconcile", log.Fields{"adapter-id": adapter.Id}) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 716 | return nil |
| 717 | } |
| 718 | |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 719 | if len(dMgr.rootDevices) == 0 { |
| 720 | logger.Debugw(ctx, "no-managed-device-to-reconcile", log.Fields{"adapter-id": adapter.Id}) |
| 721 | return nil |
| 722 | } |
| 723 | |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 724 | for rootDeviceID := range dMgr.rootDevices { |
Mahir Gunyel | 12cf55f | 2020-12-16 17:55:03 -0800 | [diff] [blame] | 725 | dAgent := dMgr.getDeviceAgent(ctx, rootDeviceID) |
| 726 | if dAgent == nil { |
| 727 | continue |
| 728 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 729 | logger.Debugw(ctx, "checking-adapter-type", log.Fields{"agentType": dAgent.deviceType, "adapter-type": adapter.Type}) |
Mahir Gunyel | 12cf55f | 2020-12-16 17:55:03 -0800 | [diff] [blame] | 730 | if dAgent.deviceType == adapter.Type { |
| 731 | rootDevice, _ := dAgent.getDeviceReadOnly(ctx) |
| 732 | if rootDevice == nil { |
| 733 | continue |
| 734 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 735 | isDeviceOwnedByService, err := dMgr.adapterProxy.IsDeviceOwnedByService(ctx, rootDeviceID, adapter.Type, adapter.CurrentReplica) |
serkant.uluderya | 7d545aa | 2020-04-09 13:38:45 -0700 | [diff] [blame] | 736 | if err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 737 | logger.Warnw(ctx, "is-device-owned-by-service", log.Fields{"error": err, "root-device-id": rootDeviceID, "adapter-type": adapter.Type, "replica-number": adapter.CurrentReplica}) |
serkant.uluderya | 7d545aa | 2020-04-09 13:38:45 -0700 | [diff] [blame] | 738 | continue |
| 739 | } |
| 740 | if isDeviceOwnedByService { |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 741 | if rootDevice.AdminState != voltha.AdminState_PREPROVISIONED { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 742 | logger.Debugw(ctx, "reconciling-root-device", log.Fields{"rootId": rootDevice.Id}) |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 743 | go dAgent.ReconcileDevice(ctx, rootDevice) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 744 | } else { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 745 | logger.Debugw(ctx, "not-reconciling-root-device", log.Fields{"rootId": rootDevice.Id, "state": rootDevice.AdminState}) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 746 | } |
| 747 | } else { // Should we be reconciling the root's children instead? |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 748 | rootDevicePorts, _ := dMgr.listDevicePorts(ctx, rootDeviceID) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 749 | childManagedByAdapter: |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 750 | for _, port := range rootDevicePorts { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 751 | for _, peer := range port.Peers { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 752 | if childDevice, _ := dMgr.getDeviceFromModel(ctx, peer.DeviceId); childDevice != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 753 | isDeviceOwnedByService, err := dMgr.adapterProxy.IsDeviceOwnedByService(ctx, childDevice.Id, adapter.Type, adapter.CurrentReplica) |
serkant.uluderya | 7d545aa | 2020-04-09 13:38:45 -0700 | [diff] [blame] | 754 | if err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 755 | logger.Warnw(ctx, "is-device-owned-by-service", log.Fields{"error": err, "child-device-id": childDevice.Id, "adapter-type": adapter.Type, "replica-number": adapter.CurrentReplica}) |
serkant.uluderya | 7d545aa | 2020-04-09 13:38:45 -0700 | [diff] [blame] | 756 | } |
| 757 | if isDeviceOwnedByService { |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 758 | if childDevice.AdminState != voltha.AdminState_PREPROVISIONED { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 759 | logger.Debugw(ctx, "reconciling-child-device", log.Fields{"child-device-id": childDevice.Id}) |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 760 | go dAgent.ReconcileDevice(ctx, childDevice) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 761 | } else { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 762 | logger.Debugw(ctx, "not-reconciling-child-device", log.Fields{"child-device-id": childDevice.Id, "state": childDevice.AdminState}) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 763 | } |
| 764 | } else { |
| 765 | // All child devices under a parent device are typically managed by the same adapter type. |
| 766 | // Therefore we only need to check whether the first device we retrieved is managed by that adapter |
| 767 | break childManagedByAdapter |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | } |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 775 | logger.Debugw(ctx, "Reconciling for device on adapter restart is initiated", log.Fields{"adapter-id": adapter.Id}) |
| 776 | |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 777 | return nil |
| 778 | } |
| 779 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 780 | func (dMgr *Manager) ReconcileChildDevices(ctx context.Context, parentDeviceID string) error { |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 781 | dAgent := dMgr.getDeviceAgent(ctx, parentDeviceID) |
| 782 | if dAgent == nil { |
| 783 | return status.Errorf(codes.NotFound, "error-unable to get agent from device") |
| 784 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 785 | if parentDevicePorts, err := dMgr.listDevicePorts(ctx, parentDeviceID); err == nil { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 786 | for _, port := range parentDevicePorts { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 787 | for _, peer := range port.Peers { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 788 | if childDevice, err := dMgr.getDeviceFromModel(ctx, peer.DeviceId); err == nil { |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 789 | go dAgent.ReconcileDevice(ctx, childDevice) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | } |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 793 | logger.Debugw(ctx, "Reconcile initiated for child devices", log.Fields{"parent-device-id": parentDeviceID}) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 794 | } |
| 795 | return nil |
| 796 | } |
| 797 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 798 | func (dMgr *Manager) UpdateDeviceUsingAdapterData(ctx context.Context, device *voltha.Device) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 799 | 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] | 800 | if agent := dMgr.getDeviceAgent(ctx, device.Id); agent != nil { |
| 801 | return agent.updateDeviceUsingAdapterData(ctx, device) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 802 | } |
| 803 | return status.Errorf(codes.NotFound, "%s", device.Id) |
| 804 | } |
| 805 | |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 806 | func (dMgr *Manager) addPeerPort(ctx context.Context, deviceID string, port *voltha.Port) error { |
| 807 | meAsPeer := &voltha.Port_PeerPort{DeviceId: deviceID, PortNo: port.PortNo} |
| 808 | for _, peerPort := range port.Peers { |
| 809 | if agent := dMgr.getDeviceAgent(ctx, peerPort.DeviceId); agent != nil { |
| 810 | if err := agent.addPeerPort(ctx, meAsPeer); err != nil { |
| 811 | return err |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | // Notify the logical device manager to setup a logical port, if needed. If the added port is an NNI or UNI |
| 816 | // then a logical port will be added to the logical device and the device route generated. If the port is a |
| 817 | // PON port then only the device graph will be generated. |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 818 | device, err := dMgr.getDeviceReadOnly(ctx, deviceID) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 819 | if err != nil { |
| 820 | return err |
| 821 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 822 | ports, err := dMgr.listDevicePorts(ctx, deviceID) |
| 823 | if err != nil { |
| 824 | return err |
| 825 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 826 | subCtx := utils.WithSpanAndRPCMetadataFromContext(ctx) |
| 827 | |
| 828 | if err = dMgr.logicalDeviceMgr.updateLogicalPort(subCtx, device, ports, port); err != nil { |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 829 | return err |
| 830 | } |
| 831 | return nil |
| 832 | } |
| 833 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 834 | func (dMgr *Manager) AddPort(ctx context.Context, deviceID string, port *voltha.Port) error { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 835 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 836 | if agent != nil { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 837 | if err := agent.addPort(ctx, port); err != nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 838 | return err |
| 839 | } |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 840 | // Setup peer ports in its own routine |
| 841 | go func() { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 842 | subCtx := utils.WithSpanAndRPCMetadataFromContext(ctx) |
| 843 | if err := dMgr.addPeerPort(subCtx, deviceID, port); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 844 | 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] | 845 | } |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 846 | }() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 847 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 848 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 849 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 850 | } |
| 851 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 852 | func (dMgr *Manager) addFlowsAndGroups(ctx context.Context, deviceID string, flows []*ofp.OfpFlowStats, groups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 853 | 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] | 854 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 855 | return agent.addFlowsAndGroups(ctx, flows, groups, flowMetadata) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 856 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 857 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 858 | } |
| 859 | |
khenaidoo | 787224a | 2020-04-16 18:08:47 -0400 | [diff] [blame] | 860 | // deleteParentFlows removes flows from the parent device based on specific attributes |
| 861 | func (dMgr *Manager) deleteParentFlows(ctx context.Context, deviceID string, uniPort uint32, metadata *voltha.FlowMetadata) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 862 | 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] | 863 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 864 | if !agent.isRootDevice { |
khenaidoo | 787224a | 2020-04-16 18:08:47 -0400 | [diff] [blame] | 865 | return status.Errorf(codes.FailedPrecondition, "not-a-parent-device-%s", deviceID) |
| 866 | } |
| 867 | return agent.filterOutFlows(ctx, uniPort, metadata) |
| 868 | } |
| 869 | return status.Errorf(codes.NotFound, "%s", deviceID) |
| 870 | } |
| 871 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 872 | func (dMgr *Manager) deleteFlowsAndGroups(ctx context.Context, deviceID string, flows []*ofp.OfpFlowStats, groups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 873 | logger.Debugw(ctx, "delete-flows-and-groups", log.Fields{"device-id": deviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 874 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 875 | return agent.deleteFlowsAndGroups(ctx, flows, groups, flowMetadata) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 876 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 877 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 878 | } |
| 879 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 880 | func (dMgr *Manager) updateFlowsAndGroups(ctx context.Context, deviceID string, flows []*ofp.OfpFlowStats, groups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 881 | logger.Debugw(ctx, "update-flows-and-groups", log.Fields{"device-id": deviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 882 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 883 | return agent.updateFlowsAndGroups(ctx, flows, groups, flowMetadata) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 884 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 885 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 886 | } |
| 887 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 888 | // UpdateDevicePmConfigs updates the PM configs. This is executed when the northbound gRPC API is invoked, typically |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 889 | // following a user action |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 890 | func (dMgr *Manager) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 891 | ctx = utils.WithRPCMetadataContext(ctx, "UpdateDevicePmConfigs") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 892 | log.EnrichSpan(ctx, log.Fields{"device-id": configs.Id}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 893 | if configs.Id == "" { |
| 894 | return nil, status.Error(codes.FailedPrecondition, "invalid-device-Id") |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 895 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 896 | agent := dMgr.getDeviceAgent(ctx, configs.Id) |
| 897 | if agent == nil { |
| 898 | return nil, status.Errorf(codes.NotFound, "%s", configs.Id) |
| 899 | } |
| 900 | return &empty.Empty{}, agent.updatePmConfigs(ctx, configs) |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 901 | } |
| 902 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 903 | // InitPmConfigs initialize the pm configs as defined by the adapter. |
| 904 | func (dMgr *Manager) InitPmConfigs(ctx context.Context, deviceID string, pmConfigs *voltha.PmConfigs) error { |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 905 | if pmConfigs.Id == "" { |
| 906 | return status.Errorf(codes.FailedPrecondition, "invalid-device-Id") |
| 907 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 908 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 909 | return agent.initPmConfigs(ctx, pmConfigs) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 910 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 911 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 912 | } |
| 913 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 914 | // ListDevicePmConfigs returns pm configs of device |
| 915 | func (dMgr *Manager) ListDevicePmConfigs(ctx context.Context, id *voltha.ID) (*voltha.PmConfigs, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 916 | ctx = utils.WithRPCMetadataContext(ctx, "ListDevicePmConfigs") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 917 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 918 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 919 | if agent == nil { |
| 920 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 921 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 922 | return agent.listPmConfigs(ctx) |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 923 | } |
| 924 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 925 | func (dMgr *Manager) getSwitchCapability(ctx context.Context, deviceID string) (*ic.SwitchCapability, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 926 | logger.Debugw(ctx, "get-switch-capability", log.Fields{"device-id": deviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 927 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 928 | return agent.getSwitchCapability(ctx) |
| 929 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 930 | return nil, status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 931 | } |
| 932 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 933 | func (dMgr *Manager) GetPorts(ctx context.Context, deviceID string, portType voltha.Port_PortType) (*voltha.Ports, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 934 | logger.Debugw(ctx, "get-ports", log.Fields{"device-id": deviceID, "port-type": portType}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 935 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
| 936 | if agent == nil { |
| 937 | return nil, status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 938 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 939 | return agent.getPorts(ctx, portType), nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 940 | } |
| 941 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 942 | 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] | 943 | 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] | 944 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 945 | return agent.updateDeviceStatus(ctx, operStatus, connStatus) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 946 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 947 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 948 | } |
| 949 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 950 | 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] | 951 | 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] | 952 | parentDevicePorts, err := dMgr.listDevicePorts(ctx, deviceID) |
| 953 | if err != nil { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 954 | return status.Errorf(codes.Aborted, "%s", err.Error()) |
| 955 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 956 | for childDeviceID := range dMgr.getAllChildDeviceIds(ctx, parentDevicePorts) { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 957 | if agent := dMgr.getDeviceAgent(ctx, childDeviceID); agent != nil { |
| 958 | if err = agent.updateDeviceStatus(ctx, operStatus, connStatus); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 959 | return status.Errorf(codes.Aborted, "childDevice:%s, error:%s", childDeviceID, err.Error()) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | } |
| 963 | return nil |
| 964 | } |
| 965 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 966 | 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] | 967 | 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] | 968 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 969 | if err := agent.updatePortState(ctx, portType, portNo, operStatus); err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 970 | 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] | 971 | return err |
| 972 | } |
| 973 | // Notify the logical device manager to change the port state |
Mahir Gunyel | 18fa0c9 | 2020-03-06 13:34:04 -0800 | [diff] [blame] | 974 | // Do this for NNI and UNIs only. PON ports are not known by logical device |
| 975 | if portType == voltha.Port_ETHERNET_NNI || portType == voltha.Port_ETHERNET_UNI { |
| 976 | go func() { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 977 | subCtx := utils.WithSpanAndRPCMetadataFromContext(ctx) |
| 978 | err := dMgr.logicalDeviceMgr.updatePortState(subCtx, deviceID, portNo, operStatus) |
Mahir Gunyel | 18fa0c9 | 2020-03-06 13:34:04 -0800 | [diff] [blame] | 979 | if err != nil { |
| 980 | // While we want to handle (catch) and log when |
| 981 | // an update to a port was not able to be |
| 982 | // propagated to the logical port, we can report |
| 983 | // it as a warning and not an error because it |
| 984 | // doesn't stop or modify processing. |
| 985 | // TODO: VOL-2707 |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 986 | 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] | 987 | } |
| 988 | }() |
| 989 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 990 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 991 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 992 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 993 | } |
| 994 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 995 | func (dMgr *Manager) DeleteAllPorts(ctx context.Context, deviceID string) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 996 | logger.Debugw(ctx, "delete-all-ports", log.Fields{"device-id": deviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 997 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 998 | if err := agent.deleteAllPorts(ctx); err != nil { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 999 | return err |
| 1000 | } |
| 1001 | // Notify the logical device manager to remove all logical ports, if needed. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1002 | // At this stage the device itself may gave been deleted already at a DeleteAllPorts |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1003 | // typically is part of a device deletion phase. |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 1004 | if device, err := dMgr.getDeviceReadOnly(ctx, deviceID); err == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1005 | go func() { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1006 | subCtx := utils.WithSpanAndRPCMetadataFromContext(ctx) |
| 1007 | if err := dMgr.logicalDeviceMgr.deleteAllLogicalPorts(subCtx, device); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1008 | logger.Errorw(ctx, "unable-to-delete-logical-ports", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1009 | } |
| 1010 | }() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1011 | } else { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1012 | logger.Warnw(ctx, "failed-to-retrieve-device", log.Fields{"device-id": deviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1013 | return err |
| 1014 | } |
| 1015 | return nil |
| 1016 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1017 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1018 | } |
| 1019 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1020 | //UpdatePortsState updates all ports on the device |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1021 | 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] | 1022 | logger.Debugw(ctx, "update-ports-state", log.Fields{"device-id": deviceID}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1023 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
| 1024 | if agent == nil { |
| 1025 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 1026 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1027 | if state != voltha.OperStatus_ACTIVE && state != voltha.OperStatus_UNKNOWN { |
| 1028 | return status.Error(codes.Unimplemented, "state-change-not-implemented") |
| 1029 | } |
| 1030 | if err := agent.updatePortsOperState(ctx, portTypeFilter, state); err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1031 | 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] | 1032 | return err |
| 1033 | } |
| 1034 | return nil |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 1035 | } |
| 1036 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1037 | func (dMgr *Manager) ChildDeviceDetected(ctx context.Context, parentDeviceID string, parentPortNo int64, deviceType string, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1038 | channelID int64, vendorID string, serialNumber string, onuID int64) (*voltha.Device, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1039 | logger.Debugw(ctx, "child-device-detected", log.Fields{"parent-device-id": parentDeviceID, "parent-port-no": parentPortNo, "device-type": deviceType, "channel-id": channelID, "vendor-id": vendorID, "serial-number": serialNumber, "onu-id": onuID}) |
Chaitrashree G S | 4b3fada | 2019-07-28 23:55:25 -0700 | [diff] [blame] | 1040 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1041 | if deviceType == "" && vendorID != "" { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1042 | logger.Debug(ctx, "device-type-is-nil-fetching-device-type") |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1043 | deviceTypes, err := dMgr.adapterMgr.ListDeviceTypes(ctx, nil) |
| 1044 | if err != nil { |
| 1045 | return nil, err |
| 1046 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 1047 | OLoop: |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1048 | for _, dType := range deviceTypes.Items { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 1049 | for _, v := range dType.VendorIds { |
| 1050 | if v == vendorID { |
| 1051 | deviceType = dType.Adapter |
| 1052 | break OLoop |
Chaitrashree G S | 4b3fada | 2019-07-28 23:55:25 -0700 | [diff] [blame] | 1053 | } |
| 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | //if no match found for the vendorid,report adapter with the custom error message |
| 1058 | if deviceType == "" { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1059 | logger.Errorw(ctx, "failed-to-fetch-adapter-name ", log.Fields{"vendor-id": vendorID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1060 | return nil, status.Errorf(codes.NotFound, "%s", vendorID) |
Chaitrashree G S | 4b3fada | 2019-07-28 23:55:25 -0700 | [diff] [blame] | 1061 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1062 | |
| 1063 | // Create the ONU device |
| 1064 | childDevice := &voltha.Device{} |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1065 | childDevice.Type = deviceType |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1066 | childDevice.ParentId = parentDeviceID |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1067 | childDevice.ParentPortNo = uint32(parentPortNo) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1068 | childDevice.VendorId = vendorID |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 1069 | childDevice.SerialNumber = serialNumber |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1070 | childDevice.Root = false |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 1071 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1072 | // Get parent device type |
| 1073 | pAgent := dMgr.getDeviceAgent(ctx, parentDeviceID) |
| 1074 | if pAgent == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1075 | return nil, status.Errorf(codes.NotFound, "%s", parentDeviceID) |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 1076 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1077 | if pAgent.deviceType == "" { |
| 1078 | return nil, status.Errorf(codes.FailedPrecondition, "device Type not set %s", parentDeviceID) |
| 1079 | } |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 1080 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1081 | if device, err := dMgr.GetChildDevice(ctx, parentDeviceID, serialNumber, onuID, parentPortNo); err == nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1082 | logger.Warnw(ctx, "child-device-exists", log.Fields{"parent-device-id": parentDeviceID, "serial-number": serialNumber}) |
Mahir Gunyel | 6deaa24 | 2019-06-27 04:53:33 -0700 | [diff] [blame] | 1083 | return device, status.Errorf(codes.AlreadyExists, "%s", serialNumber) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 1084 | } |
| 1085 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1086 | childDevice.ProxyAddress = &voltha.Device_ProxyAddress{DeviceId: parentDeviceID, DeviceType: pAgent.deviceType, ChannelId: uint32(channelID), OnuId: uint32(onuID)} |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1087 | |
| 1088 | // Create and start a device agent for that device |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 1089 | agent := newAgent(dMgr.adapterProxy, childDevice, dMgr, dMgr.dbPath, dMgr.dProxy, dMgr.defaultTimeout) |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 1090 | insertedChildDevice, err := agent.start(ctx, false, childDevice) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 1091 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1092 | logger.Errorw(ctx, "error-starting-child-device", log.Fields{"parent-device-id": childDevice.ParentId, "child-device-id": agent.deviceID, "error": err}) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 1093 | return nil, err |
| 1094 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1095 | dMgr.addDeviceAgentToMap(agent) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1096 | |
| 1097 | // Activate the child device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1098 | if agent = dMgr.getDeviceAgent(ctx, agent.deviceID); agent != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1099 | go func() { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 1100 | subCtx := utils.WithFromTopicMetadataFromContext(utils.WithSpanAndRPCMetadataFromContext(ctx), ctx) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1101 | err := agent.enableDevice(subCtx) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1102 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1103 | logger.Errorw(ctx, "unable-to-enable-device", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1104 | } |
| 1105 | }() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1106 | } |
| 1107 | |
Mahir Gunyel | 23bd8c7 | 2020-09-21 23:20:43 -0700 | [diff] [blame] | 1108 | return insertedChildDevice, nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1109 | } |
| 1110 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1111 | 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] | 1112 | logger.Debugw(ctx, "packet-out", log.Fields{"device-id": deviceID, "out-port": outPort}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1113 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 1114 | return agent.packetOut(ctx, outPort, packet) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1115 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1116 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1117 | } |
| 1118 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1119 | // PacketIn receives packet from adapter |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1120 | 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] | 1121 | logger.Debugw(ctx, "packet-in", log.Fields{"device-id": deviceID, "port": port}) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1122 | // Get the logical device Id based on the deviceId |
| 1123 | var device *voltha.Device |
| 1124 | var err error |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 1125 | if device, err = dMgr.getDeviceReadOnly(ctx, deviceID); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1126 | logger.Errorw(ctx, "device-not-found", log.Fields{"device-id": deviceID}) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1127 | return err |
| 1128 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1129 | if !device.Root { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1130 | logger.Errorw(ctx, "device-not-root", log.Fields{"device-id": deviceID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1131 | return status.Errorf(codes.FailedPrecondition, "%s", deviceID) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1132 | } |
| 1133 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1134 | if err := dMgr.logicalDeviceMgr.packetIn(ctx, device.ParentId, port, transactionID, packet); err != nil { |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1135 | return err |
| 1136 | } |
| 1137 | return nil |
| 1138 | } |
| 1139 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1140 | 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] | 1141 | 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] | 1142 | if agent := dMgr.getDeviceAgent(ctx, device.Id); agent != nil { |
| 1143 | return agent.setParentID(ctx, device, parentID) |
khenaidoo | ad06fd7 | 2019-10-28 12:26:05 -0400 | [diff] [blame] | 1144 | } |
| 1145 | return status.Errorf(codes.NotFound, "%s", device.Id) |
| 1146 | } |
| 1147 | |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1148 | // |
| 1149 | //CreateLogicalDevice creates logical device in core |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1150 | func (dMgr *Manager) CreateLogicalDevice(ctx context.Context, cDevice *voltha.Device) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1151 | logger.Info(ctx, "create-logical-device") |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1152 | // Verify whether the logical device has already been created |
| 1153 | if cDevice.ParentId != "" { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1154 | logger.Debugw(ctx, "parent-device-already-exist", log.Fields{"device-id": cDevice.Id, "logical-device-id": cDevice.Id}) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1155 | return nil |
| 1156 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1157 | var err error |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1158 | if _, err = dMgr.logicalDeviceMgr.createLogicalDevice(ctx, cDevice); err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1159 | logger.Warnw(ctx, "create-logical-device-error", log.Fields{"device": cDevice}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1160 | return err |
| 1161 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1162 | return nil |
| 1163 | } |
| 1164 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1165 | // DeleteLogicalDevice deletes logical device from core |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1166 | func (dMgr *Manager) DeleteLogicalDevice(ctx context.Context, cDevice *voltha.Device) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1167 | logger.Info(ctx, "delete-logical-device") |
serkant.uluderya | e25f648 | 2020-12-17 21:08:38 +0300 | [diff] [blame] | 1168 | if err := dMgr.logicalDeviceMgr.deleteLogicalDevice(ctx, cDevice); err != nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1169 | return err |
| 1170 | } |
| 1171 | // Remove the logical device Id from the parent device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1172 | logicalID := "" |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1173 | dMgr.UpdateDeviceAttribute(ctx, cDevice.Id, "ParentId", logicalID) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1174 | return nil |
| 1175 | } |
| 1176 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1177 | // DeleteLogicalPorts removes the logical ports associated with that deviceId |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1178 | func (dMgr *Manager) DeleteLogicalPorts(ctx context.Context, cDevice *voltha.Device) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1179 | logger.Debugw(ctx, "delete-all-logical-ports", log.Fields{"device-id": cDevice.Id}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1180 | if err := dMgr.logicalDeviceMgr.deleteLogicalPorts(ctx, cDevice.Id); err != nil { |
khenaidoo | e132f52 | 2020-03-20 15:23:15 -0400 | [diff] [blame] | 1181 | // Just log the error. The logical device or port may already have been deleted before this callback is invoked. |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1182 | logger.Warnw(ctx, "delete-logical-ports-error", log.Fields{"device-id": cDevice.Id, "error": err}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1183 | } |
| 1184 | return nil |
| 1185 | } |
| 1186 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1187 | func (dMgr *Manager) getParentDevice(ctx context.Context, childDevice *voltha.Device) *voltha.Device { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1188 | // Sanity check |
| 1189 | if childDevice.Root { |
| 1190 | // childDevice is the parent device |
| 1191 | return childDevice |
| 1192 | } |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 1193 | parentDevice, _ := dMgr.getDeviceReadOnly(ctx, childDevice.ParentId) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1194 | return parentDevice |
| 1195 | } |
| 1196 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1197 | //ChildDevicesLost is invoked by an adapter to indicate that a parent device is in a state (Disabled) where it |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1198 | //cannot manage the child devices. This will trigger the Core to disable all the child devices. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1199 | func (dMgr *Manager) ChildDevicesLost(ctx context.Context, parentDeviceID string) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1200 | logger.Debug(ctx, "child-devices-lost") |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 1201 | parentDevice, err := dMgr.getDeviceReadOnly(ctx, parentDeviceID) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1202 | if err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1203 | logger.Warnw(ctx, "failed-getting-device", log.Fields{"parent-device-id": parentDeviceID, "error": err}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1204 | return err |
| 1205 | } |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 1206 | return dMgr.DisableAllChildDevices(ctx, parentDevice) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1207 | } |
| 1208 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1209 | //ChildDevicesDetected is invoked by an adapter when child devices are found, typically after after a |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1210 | // disable/enable sequence. This will trigger the Core to Enable all the child devices of that parent. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1211 | func (dMgr *Manager) ChildDevicesDetected(ctx context.Context, parentDeviceID string) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1212 | logger.Debug(ctx, "child-devices-detected") |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1213 | parentDevicePorts, err := dMgr.listDevicePorts(ctx, parentDeviceID) |
| 1214 | if err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1215 | logger.Warnw(ctx, "failed-getting-device", log.Fields{"device-id": parentDeviceID, "error": err}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1216 | return err |
| 1217 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1218 | childDeviceIds := dMgr.getAllChildDeviceIds(ctx, parentDevicePorts) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1219 | if len(childDeviceIds) == 0 { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1220 | logger.Debugw(ctx, "no-child-device", log.Fields{"parent-device-id": parentDeviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1221 | } |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1222 | allChildEnableRequestSent := true |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1223 | for childDeviceID := range childDeviceIds { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1224 | if agent := dMgr.getDeviceAgent(ctx, childDeviceID); agent != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1225 | subCtx := utils.WithSpanAndRPCMetadataFromContext(ctx) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1226 | // Run the children re-registration in its own routine |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1227 | go func(ctx context.Context) { |
| 1228 | err = agent.enableDevice(ctx) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1229 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1230 | logger.Errorw(ctx, "unable-to-enable-device", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1231 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1232 | }(subCtx) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1233 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1234 | err = status.Errorf(codes.Unavailable, "no agent for child device %s", childDeviceID) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1235 | logger.Errorw(ctx, "no-child-device-agent", log.Fields{"parent-device-id": parentDeviceID, "child-id": childDeviceID}) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1236 | allChildEnableRequestSent = false |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1237 | } |
| 1238 | } |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1239 | if !allChildEnableRequestSent { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1240 | return err |
| 1241 | } |
| 1242 | return nil |
| 1243 | } |
| 1244 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1245 | /* |
| 1246 | All the functions below are callback functions where they are invoked with the latest and previous data. We can |
| 1247 | therefore use the data as is without trying to get the latest from the model. |
| 1248 | */ |
| 1249 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1250 | //DisableAllChildDevices is invoked as a callback when the parent device is disabled |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1251 | func (dMgr *Manager) DisableAllChildDevices(ctx context.Context, parentCurrDevice *voltha.Device) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1252 | logger.Debug(ctx, "disable-all-child-devices") |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1253 | ports, _ := dMgr.listDevicePorts(ctx, parentCurrDevice.Id) |
| 1254 | for childDeviceID := range dMgr.getAllChildDeviceIds(ctx, ports) { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1255 | if agent := dMgr.getDeviceAgent(ctx, childDeviceID); agent != nil { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1256 | if err := agent.disableDevice(ctx); err != nil { |
khenaidoo | e132f52 | 2020-03-20 15:23:15 -0400 | [diff] [blame] | 1257 | // 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] | 1258 | 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] | 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | return nil |
| 1263 | } |
| 1264 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1265 | //DeleteAllChildDevices is invoked as a callback when the parent device is deleted |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1266 | func (dMgr *Manager) DeleteAllChildDevices(ctx context.Context, parentCurrDevice *voltha.Device) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1267 | logger.Debug(ctx, "delete-all-child-devices") |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1268 | force := false |
| 1269 | // Get the parent device Transient state, if its FORCE_DELETED(go for force delete for child devices) |
| 1270 | // So in cases when this handler is getting called other than DELETE operation, no force option would be used. |
| 1271 | agent := dMgr.getDeviceAgent(ctx, parentCurrDevice.Id) |
| 1272 | if agent == nil { |
| 1273 | return status.Errorf(codes.NotFound, "%s", parentCurrDevice.Id) |
| 1274 | } |
| 1275 | |
| 1276 | force = agent.getTransientState() == voltha.DeviceTransientState_FORCE_DELETING |
| 1277 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1278 | ports, _ := dMgr.listDevicePorts(ctx, parentCurrDevice.Id) |
| 1279 | for childDeviceID := range dMgr.getAllChildDeviceIds(ctx, ports) { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1280 | if agent := dMgr.getDeviceAgent(ctx, childDeviceID); agent != nil { |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1281 | if force { |
| 1282 | if err := agent.deleteDeviceForce(ctx); err != nil { |
| 1283 | logger.Warnw(ctx, "failure-delete-device-force", log.Fields{"device-id": childDeviceID, |
| 1284 | "error": err.Error()}) |
| 1285 | } |
| 1286 | } else { |
| 1287 | if err := agent.deleteDevice(ctx); err != nil { |
| 1288 | logger.Warnw(ctx, "failure-delete-device", log.Fields{"device-id": childDeviceID, |
| 1289 | "error": err.Error()}) |
| 1290 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1291 | } |
khenaidoo | 4908535 | 2020-01-13 19:15:43 -0500 | [diff] [blame] | 1292 | // No further action is required here. The deleteDevice will change the device state where the resulting |
| 1293 | // callback will take care of cleaning the child device agent. |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1294 | } |
| 1295 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1296 | return nil |
| 1297 | } |
| 1298 | |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 1299 | //DeleteAllLogicalPorts is invoked as a callback when the parent device's connection status moves to UNREACHABLE |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1300 | func (dMgr *Manager) DeleteAllLogicalPorts(ctx context.Context, parentDevice *voltha.Device) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1301 | logger.Debugw(ctx, "delete-all-logical-ports", log.Fields{"parent-device-id": parentDevice.Id}) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 1302 | if err := dMgr.logicalDeviceMgr.deleteAllLogicalPorts(ctx, parentDevice); err != nil { |
khenaidoo | e132f52 | 2020-03-20 15:23:15 -0400 | [diff] [blame] | 1303 | // Just log error as logical device may already have been deleted |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1304 | logger.Warnw(ctx, "delete-all-logical-ports-fail", log.Fields{"parent-device-id": parentDevice.Id, "error": err}) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 1305 | } |
| 1306 | return nil |
| 1307 | } |
| 1308 | |
| 1309 | //DeleteAllDeviceFlows is invoked as a callback when the parent device's connection status moves to UNREACHABLE |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1310 | func (dMgr *Manager) DeleteAllDeviceFlows(ctx context.Context, parentDevice *voltha.Device) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1311 | logger.Debugw(ctx, "delete-all-device-flows", log.Fields{"parent-device-id": parentDevice.Id}) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 1312 | if agent := dMgr.getDeviceAgent(ctx, parentDevice.Id); agent != nil { |
| 1313 | if err := agent.deleteAllFlows(ctx); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1314 | logger.Errorw(ctx, "error-deleting-all-device-flows", log.Fields{"parent-device-id": parentDevice.Id}) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 1315 | return err |
| 1316 | } |
| 1317 | return nil |
| 1318 | } |
| 1319 | return status.Errorf(codes.NotFound, "%s", parentDevice.Id) |
| 1320 | } |
| 1321 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1322 | //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] | 1323 | 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] | 1324 | logger.Debug(ctx, "get-all-child-device-ids") |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1325 | childDeviceIds := make(map[string]struct{}, len(parentDevicePorts)) |
| 1326 | for _, port := range parentDevicePorts { |
| 1327 | for _, peer := range port.Peers { |
| 1328 | childDeviceIds[peer.DeviceId] = struct{}{} |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1329 | } |
| 1330 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1331 | logger.Debugw(ctx, "returning-getAllChildDeviceIds", log.Fields{"childDeviceIds": childDeviceIds}) |
| 1332 | return childDeviceIds |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1333 | } |
| 1334 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1335 | //GetAllChildDevices is a helper method to get all the child device IDs from the device passed as parameter |
| 1336 | func (dMgr *Manager) GetAllChildDevices(ctx context.Context, parentDeviceID string) (*voltha.Devices, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1337 | 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] | 1338 | if parentDevicePorts, err := dMgr.listDevicePorts(ctx, parentDeviceID); err == nil { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 1339 | childDevices := make([]*voltha.Device, 0) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1340 | for deviceID := range dMgr.getAllChildDeviceIds(ctx, parentDevicePorts) { |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 1341 | if d, e := dMgr.getDeviceReadOnly(ctx, deviceID); e == nil && d != nil { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1342 | childDevices = append(childDevices, d) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 1343 | } |
| 1344 | } |
| 1345 | return &voltha.Devices{Items: childDevices}, nil |
| 1346 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1347 | return nil, status.Errorf(codes.NotFound, "%s", parentDeviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 1348 | } |
| 1349 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1350 | // SetupUNILogicalPorts creates UNI ports on the logical device that represents a child UNI interface |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1351 | func (dMgr *Manager) SetupUNILogicalPorts(ctx context.Context, cDevice *voltha.Device) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1352 | logger.Info(ctx, "setup-uni-logical-ports") |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1353 | cDevicePorts, err := dMgr.listDevicePorts(ctx, cDevice.Id) |
| 1354 | if err != nil { |
| 1355 | return err |
| 1356 | } |
| 1357 | if err := dMgr.logicalDeviceMgr.setupUNILogicalPorts(ctx, cDevice, cDevicePorts); err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1358 | logger.Warnw(ctx, "setup-uni-logical-ports-error", log.Fields{"device": cDevice, "err": err}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1359 | return err |
| 1360 | } |
| 1361 | return nil |
| 1362 | } |
| 1363 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1364 | // convenience to avoid redefining |
| 1365 | var operationFailureResp = &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE} |
| 1366 | |
| 1367 | // DownloadImage execute an image download request |
| 1368 | func (dMgr *Manager) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1369 | ctx = utils.WithRPCMetadataContext(ctx, "DownloadImage") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1370 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1371 | logger.Debugw(ctx, "download-image", log.Fields{"device-id": img.Id, "image-name": img.Name}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1372 | agent := dMgr.getDeviceAgent(ctx, img.Id) |
| 1373 | if agent == nil { |
| 1374 | return operationFailureResp, status.Errorf(codes.NotFound, "%s", img.Id) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1375 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1376 | resp, err := agent.downloadImage(ctx, img) |
| 1377 | if err != nil { |
| 1378 | return operationFailureResp, err |
| 1379 | } |
| 1380 | return resp, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1381 | } |
| 1382 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1383 | // CancelImageDownload cancels image download request |
| 1384 | func (dMgr *Manager) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1385 | ctx = utils.WithRPCMetadataContext(ctx, "CancelImageDownload") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1386 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1387 | logger.Debugw(ctx, "cancel-image-download", log.Fields{"device-id": img.Id, "image-name": img.Name}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1388 | agent := dMgr.getDeviceAgent(ctx, img.Id) |
| 1389 | if agent == nil { |
| 1390 | return operationFailureResp, status.Errorf(codes.NotFound, "%s", img.Id) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1391 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1392 | resp, err := agent.cancelImageDownload(ctx, img) |
| 1393 | if err != nil { |
| 1394 | return operationFailureResp, err |
| 1395 | } |
| 1396 | return resp, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1397 | } |
| 1398 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1399 | // ActivateImageUpdate activates image update request |
| 1400 | func (dMgr *Manager) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1401 | ctx = utils.WithRPCMetadataContext(ctx, "ActivateImageUpdate") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1402 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1403 | logger.Debugw(ctx, "activate-image-update", log.Fields{"device-id": img.Id, "image-name": img.Name}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1404 | agent := dMgr.getDeviceAgent(ctx, img.Id) |
| 1405 | if agent == nil { |
| 1406 | return operationFailureResp, status.Errorf(codes.NotFound, "%s", img.Id) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1407 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1408 | resp, err := agent.activateImage(ctx, img) |
| 1409 | if err != nil { |
| 1410 | return operationFailureResp, err |
| 1411 | } |
| 1412 | return resp, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1413 | } |
| 1414 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1415 | // RevertImageUpdate reverts image update |
| 1416 | func (dMgr *Manager) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1417 | ctx = utils.WithRPCMetadataContext(ctx, "RevertImageUpdate") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1418 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1419 | logger.Debugw(ctx, "rever-image-update", log.Fields{"device-id": img.Id, "image-name": img.Name}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1420 | agent := dMgr.getDeviceAgent(ctx, img.Id) |
| 1421 | if agent == nil { |
| 1422 | return operationFailureResp, status.Errorf(codes.NotFound, "%s", img.Id) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1423 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1424 | resp, err := agent.revertImage(ctx, img) |
| 1425 | if err != nil { |
| 1426 | return operationFailureResp, err |
| 1427 | } |
| 1428 | return resp, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1429 | } |
| 1430 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1431 | // convenience to avoid redefining |
| 1432 | var imageDownloadFailureResp = &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN} |
| 1433 | |
| 1434 | // GetImageDownloadStatus returns status of image download |
| 1435 | func (dMgr *Manager) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1436 | ctx = utils.WithRPCMetadataContext(ctx, "GetImageDownloadStatus") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1437 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1438 | logger.Debugw(ctx, "get-image-download-status", log.Fields{"device-id": img.Id, "image-name": img.Name}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1439 | agent := dMgr.getDeviceAgent(ctx, img.Id) |
| 1440 | if agent == nil { |
| 1441 | return imageDownloadFailureResp, status.Errorf(codes.NotFound, "%s", img.Id) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1442 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1443 | resp, err := agent.getImageDownloadStatus(ctx, img) |
| 1444 | if err != nil { |
| 1445 | return imageDownloadFailureResp, err |
| 1446 | } |
| 1447 | return resp, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1448 | } |
| 1449 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1450 | func (dMgr *Manager) UpdateImageDownload(ctx context.Context, deviceID string, img *voltha.ImageDownload) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1451 | ctx = utils.WithRPCMetadataContext(ctx, "UpdateImageDownload") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1452 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1453 | logger.Debugw(ctx, "update-image-download", log.Fields{"device-id": img.Id, "image-name": img.Name}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1454 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 1455 | if err := agent.updateImageDownload(ctx, img); err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1456 | logger.Debugw(ctx, "update-image-download-failed", log.Fields{"err": err, "image-name": img.Name}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1457 | return err |
| 1458 | } |
| 1459 | } else { |
| 1460 | return status.Errorf(codes.NotFound, "%s", img.Id) |
| 1461 | } |
| 1462 | return nil |
| 1463 | } |
| 1464 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1465 | // GetImageDownload returns image download |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1466 | func (dMgr *Manager) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1467 | ctx = utils.WithRPCMetadataContext(ctx, "GetImageDownload") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1468 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1469 | logger.Debugw(ctx, "get-image-download", log.Fields{"device-id": img.Id, "image-name": img.Name}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1470 | agent := dMgr.getDeviceAgent(ctx, img.Id) |
| 1471 | if agent == nil { |
| 1472 | return imageDownloadFailureResp, status.Errorf(codes.NotFound, "%s", img.Id) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1473 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1474 | resp, err := agent.getImageDownload(ctx, img) |
| 1475 | if err != nil { |
| 1476 | return imageDownloadFailureResp, err |
| 1477 | } |
| 1478 | return resp, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1479 | } |
| 1480 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1481 | // ListImageDownloads returns image downloads |
| 1482 | func (dMgr *Manager) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1483 | ctx = utils.WithRPCMetadataContext(ctx, "ListImageDownloads") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1484 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1485 | logger.Debugw(ctx, "list-image-downloads", log.Fields{"device-id": id.Id}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1486 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 1487 | if agent == nil { |
| 1488 | return &voltha.ImageDownloads{Items: []*voltha.ImageDownload{imageDownloadFailureResp}}, status.Errorf(codes.NotFound, "%s", id.Id) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1489 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1490 | resp, err := agent.listImageDownloads(ctx, id.Id) |
| 1491 | if err != nil { |
| 1492 | return &voltha.ImageDownloads{Items: []*voltha.ImageDownload{imageDownloadFailureResp}}, err |
| 1493 | } |
| 1494 | return resp, nil |
| 1495 | } |
| 1496 | |
| 1497 | // GetImages returns all images for a specific device entry |
| 1498 | func (dMgr *Manager) GetImages(ctx context.Context, id *voltha.ID) (*voltha.Images, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1499 | ctx = utils.WithRPCMetadataContext(ctx, "GetImages") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1500 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1501 | logger.Debugw(ctx, "get-images", log.Fields{"device-id": id.Id}) |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 1502 | device, err := dMgr.getDeviceReadOnly(ctx, id.Id) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1503 | if err != nil { |
| 1504 | return nil, err |
| 1505 | } |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 1506 | return device.Images, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1507 | } |
| 1508 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1509 | func (dMgr *Manager) NotifyInvalidTransition(ctx context.Context, device *voltha.Device) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1510 | logger.Errorw(ctx, "notify-invalid-transition", log.Fields{ |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 1511 | "device": device.Id, |
| 1512 | "curr-admin-state": device.AdminState, |
| 1513 | "curr-oper-state": device.OperStatus, |
| 1514 | "curr-conn-state": device.ConnectStatus, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 1515 | }) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1516 | //TODO: notify over kafka? |
| 1517 | return nil |
| 1518 | } |
| 1519 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1520 | // UpdateDeviceAttribute updates value of particular device attribute |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1521 | func (dMgr *Manager) UpdateDeviceAttribute(ctx context.Context, deviceID string, attribute string, value interface{}) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1522 | if agent, ok := dMgr.deviceAgents.Load(deviceID); ok { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1523 | agent.(*Agent).updateDeviceAttribute(ctx, attribute, value) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1524 | } |
| 1525 | } |
| 1526 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1527 | // 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] | 1528 | func (dMgr *Manager) GetParentDeviceID(ctx context.Context, deviceID string) string { |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 1529 | if device, _ := dMgr.getDeviceReadOnly(ctx, deviceID); device != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1530 | 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] | 1531 | return device.ParentId |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1532 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 1533 | return "" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1534 | } |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1535 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1536 | func (dMgr *Manager) SimulateAlarm(ctx context.Context, simulateReq *voltha.SimulateAlarmRequest) (*common.OperationResp, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1537 | ctx = utils.WithRPCMetadataContext(ctx, "SimulateAlarm") |
| 1538 | logger.Debugw(ctx, "simulate-alarm", log.Fields{"id": simulateReq.Id, "indicator": simulateReq.Indicator, "intf-id": simulateReq.IntfId, |
| 1539 | "port-type-name": simulateReq.PortTypeName, "onu-device-id": simulateReq.OnuDeviceId, "inverse-bit-error-rate": simulateReq.InverseBitErrorRate, |
| 1540 | "drift": simulateReq.Drift, "new-eqd": simulateReq.NewEqd, "onu-serial-number": simulateReq.OnuSerialNumber, "operation": simulateReq.Operation}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1541 | agent := dMgr.getDeviceAgent(ctx, simulateReq.Id) |
| 1542 | if agent == nil { |
| 1543 | return nil, status.Errorf(codes.NotFound, "%s", simulateReq.Id) |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1544 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1545 | if err := agent.simulateAlarm(ctx, simulateReq); err != nil { |
| 1546 | return nil, err |
| 1547 | } |
| 1548 | return &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}, nil |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1549 | } |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1550 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1551 | func (dMgr *Manager) UpdateDeviceReason(ctx context.Context, deviceID string, reason string) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1552 | logger.Debugw(ctx, "update-device-reason", log.Fields{"device-id": deviceID, "reason": reason}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1553 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 1554 | return agent.updateDeviceReason(ctx, reason) |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1555 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1556 | return status.Errorf(codes.NotFound, "%s", deviceID) |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1557 | } |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1558 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1559 | func (dMgr *Manager) EnablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1560 | ctx = utils.WithRPCMetadataContext(ctx, "EnablePort") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1561 | log.EnrichSpan(ctx, log.Fields{"device-id": port.DeviceId}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1562 | logger.Debugw(ctx, "enable-port", log.Fields{"device-id": port.DeviceId, "port-no": port.PortNo}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1563 | agent := dMgr.getDeviceAgent(ctx, port.DeviceId) |
| 1564 | if agent == nil { |
| 1565 | return nil, status.Errorf(codes.NotFound, "%s", port.DeviceId) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1566 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1567 | return &empty.Empty{}, agent.enablePort(ctx, port.PortNo) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1568 | } |
| 1569 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1570 | func (dMgr *Manager) DisablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1571 | ctx = utils.WithRPCMetadataContext(ctx, "DisablePort") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1572 | log.EnrichSpan(ctx, log.Fields{"device-id": port.DeviceId}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1573 | logger.Debugw(ctx, "disable-port", log.Fields{"device-id": port.DeviceId, "port-no": port.PortNo}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1574 | agent := dMgr.getDeviceAgent(ctx, port.DeviceId) |
| 1575 | if agent == nil { |
| 1576 | return nil, status.Errorf(codes.NotFound, "%s", port.DeviceId) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1577 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1578 | return &empty.Empty{}, agent.disablePort(ctx, port.PortNo) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1579 | } |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1580 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1581 | // ChildDeviceLost calls parent adapter to delete child device and all its references |
| 1582 | func (dMgr *Manager) ChildDeviceLost(ctx context.Context, curr *voltha.Device) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1583 | logger.Debugw(ctx, "child-device-lost", log.Fields{"child-device-id": curr.Id, "parent-device-id": curr.ParentId}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1584 | if parentAgent := dMgr.getDeviceAgent(ctx, curr.ParentId); parentAgent != nil { |
khenaidoo | e132f52 | 2020-03-20 15:23:15 -0400 | [diff] [blame] | 1585 | if err := parentAgent.ChildDeviceLost(ctx, curr); err != nil { |
| 1586 | // Just log the message and let the remaining pipeline proceed. |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1587 | logger.Warnw(ctx, "childDeviceLost", log.Fields{"child-device-id": curr.Id, "parent-device-id": curr.ParentId, "error": err}) |
khenaidoo | e132f52 | 2020-03-20 15:23:15 -0400 | [diff] [blame] | 1588 | } |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1589 | } |
khenaidoo | e132f52 | 2020-03-20 15:23:15 -0400 | [diff] [blame] | 1590 | // Do not return an error as parent device may also have been deleted. Let the remaining pipeline proceed. |
| 1591 | return nil |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1592 | } |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 1593 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1594 | func (dMgr *Manager) StartOmciTestAction(ctx context.Context, request *voltha.OmciTestRequest) (*voltha.TestResponse, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1595 | ctx = utils.WithRPCMetadataContext(ctx, "StartOmciTestAction") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1596 | log.EnrichSpan(ctx, log.Fields{"device-id": request.Id}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1597 | logger.Debugw(ctx, "start-omci-test-action", log.Fields{"device-id": request.Id, "uuid": request.Uuid}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1598 | agent := dMgr.getDeviceAgent(ctx, request.Id) |
| 1599 | if agent == nil { |
| 1600 | return nil, status.Errorf(codes.NotFound, "%s", request.Id) |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 1601 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1602 | return agent.startOmciTest(ctx, request) |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 1603 | } |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 1604 | |
| 1605 | func (dMgr *Manager) GetExtValue(ctx context.Context, value *voltha.ValueSpecifier) (*voltha.ReturnValues, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1606 | ctx = utils.WithRPCMetadataContext(ctx, "GetExtValue") |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1607 | log.EnrichSpan(ctx, log.Fields{"device-id": value.Id}) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1608 | logger.Debugw(ctx, "get-ext-value", log.Fields{"onu-id": value.Id}) |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 1609 | cDevice, err := dMgr.getDeviceReadOnly(ctx, value.Id) |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 1610 | if err != nil { |
| 1611 | return nil, status.Errorf(codes.Aborted, "%s", err.Error()) |
| 1612 | } |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 1613 | pDevice, err := dMgr.getDeviceReadOnly(ctx, cDevice.ParentId) |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 1614 | if err != nil { |
| 1615 | return nil, status.Errorf(codes.Aborted, "%s", err.Error()) |
| 1616 | } |
| 1617 | if agent := dMgr.getDeviceAgent(ctx, cDevice.ParentId); agent != nil { |
| 1618 | resp, err := agent.getExtValue(ctx, pDevice, cDevice, value) |
| 1619 | if err != nil { |
| 1620 | return nil, err |
| 1621 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1622 | logger.Debugw(ctx, "get-ext-value-result", log.Fields{"result": resp}) |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 1623 | return resp, nil |
| 1624 | } |
| 1625 | return nil, status.Errorf(codes.NotFound, "%s", value.Id) |
| 1626 | |
| 1627 | } |
dpaul | 6268631 | 2020-06-23 14:17:36 +0530 | [diff] [blame] | 1628 | |
| 1629 | // SetExtValue set some given configs or value |
| 1630 | func (dMgr *Manager) SetExtValue(ctx context.Context, value *voltha.ValueSet) (*empty.Empty, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1631 | ctx = utils.WithRPCMetadataContext(ctx, "SetExtValue") |
| 1632 | logger.Debugw(ctx, "set-ext-value", log.Fields{"onu-id": value.Id}) |
dpaul | 6268631 | 2020-06-23 14:17:36 +0530 | [diff] [blame] | 1633 | device, err := dMgr.getDeviceReadOnly(ctx, value.Id) |
| 1634 | if err != nil { |
| 1635 | return nil, status.Errorf(codes.Aborted, "%s", err.Error()) |
| 1636 | } |
| 1637 | if agent := dMgr.getDeviceAgent(ctx, device.Id); agent != nil { |
| 1638 | resp, err := agent.setExtValue(ctx, device, value) |
| 1639 | if err != nil { |
| 1640 | return nil, err |
| 1641 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1642 | logger.Debugw(ctx, "set-ext-value-result", log.Fields{"result": resp}) |
dpaul | 6268631 | 2020-06-23 14:17:36 +0530 | [diff] [blame] | 1643 | return resp, nil |
| 1644 | } |
| 1645 | return nil, status.Errorf(codes.NotFound, "%s", value.Id) |
| 1646 | |
| 1647 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1648 | |
| 1649 | func (dMgr *Manager) SendRPCEvent(ctx context.Context, id string, rpcEvent *voltha.RPCEvent, |
| 1650 | category voltha.EventCategory_Types, subCategory *voltha.EventSubCategory_Types, raisedTs int64) { |
| 1651 | //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] | 1652 | dMgr.Agent.SendRPCEvent(ctx, id, rpcEvent, category, subCategory, raisedTs) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1653 | } |
Maninder | a496f85 | 2021-02-22 09:57:56 +0530 | [diff] [blame] | 1654 | |
| 1655 | func (dMgr *Manager) GetTransientState(ctx context.Context, id string) (voltha.DeviceTransientState_Types, error) { |
| 1656 | agent := dMgr.getDeviceAgent(ctx, id) |
| 1657 | if agent == nil { |
| 1658 | return voltha.DeviceTransientState_NONE, status.Errorf(codes.NotFound, "%s", id) |
| 1659 | } |
| 1660 | return agent.getTransientState(), nil |
| 1661 | } |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1662 | |
| 1663 | func (dMgr *Manager) DownloadImageToDevice(ctx context.Context, request *voltha.DeviceImageDownloadRequest) (*voltha.DeviceImageResponse, error) { |
| 1664 | if err := dMgr.validateImageDownloadRequest(request); err != nil { |
| 1665 | return nil, err |
| 1666 | } |
| 1667 | |
| 1668 | ctx = utils.WithRPCMetadataContext(ctx, "DownloadImageToDevice") |
| 1669 | respCh := make(chan []*voltha.DeviceImageState, len(request.GetDeviceId())) |
| 1670 | |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1671 | for index, deviceID := range request.DeviceId { |
ssiddiqui | 6bd4de0 | 2021-06-08 12:04:17 +0530 | [diff] [blame] | 1672 | // Create download request per device |
| 1673 | downloadReq := &voltha.DeviceImageDownloadRequest{ |
| 1674 | Image: request.Image, |
| 1675 | ActivateOnSuccess: request.ActivateOnSuccess, |
| 1676 | CommitOnSuccess: request.CommitOnSuccess, |
| 1677 | } |
| 1678 | |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1679 | //slice-out only single deviceID from the request |
| 1680 | downloadReq.DeviceId = request.DeviceId[index : index+1] |
| 1681 | |
| 1682 | go func(deviceID string, req *voltha.DeviceImageDownloadRequest, ch chan []*voltha.DeviceImageState) { |
| 1683 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
| 1684 | if agent == nil { |
ssiddiqui | b2f526c | 2021-10-11 17:11:25 +0530 | [diff] [blame] | 1685 | logger.Errorw(ctx, "Device-agent-not-found", log.Fields{"device-id": deviceID}) |
| 1686 | ch <- []*voltha.DeviceImageState{{ |
| 1687 | DeviceId: deviceID, |
| 1688 | ImageState: &voltha.ImageState{ |
| 1689 | Version: req.GetImage().GetVersion(), |
| 1690 | DownloadState: voltha.ImageState_DOWNLOAD_FAILED, |
| 1691 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 1692 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 1693 | }, |
| 1694 | }} |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1695 | return |
| 1696 | } |
| 1697 | |
| 1698 | resp, err := agent.downloadImageToDevice(ctx, req) |
| 1699 | if err != nil { |
| 1700 | logger.Errorw(ctx, "download-image-to-device-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | b2f526c | 2021-10-11 17:11:25 +0530 | [diff] [blame] | 1701 | ch <- []*voltha.DeviceImageState{{ |
| 1702 | DeviceId: deviceID, |
| 1703 | ImageState: &voltha.ImageState{ |
| 1704 | Version: req.GetImage().GetVersion(), |
| 1705 | DownloadState: voltha.ImageState_DOWNLOAD_FAILED, |
| 1706 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 1707 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 1708 | }, |
| 1709 | }} |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1710 | return |
| 1711 | } |
| 1712 | |
| 1713 | err = dMgr.validateDeviceImageResponse(resp) |
| 1714 | if err != nil { |
| 1715 | logger.Errorw(ctx, "download-image-to-device-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | b2f526c | 2021-10-11 17:11:25 +0530 | [diff] [blame] | 1716 | ch <- []*voltha.DeviceImageState{{ |
| 1717 | DeviceId: deviceID, |
| 1718 | ImageState: &voltha.ImageState{ |
| 1719 | Version: req.GetImage().GetVersion(), |
| 1720 | DownloadState: voltha.ImageState_DOWNLOAD_FAILED, |
| 1721 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 1722 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 1723 | }, |
| 1724 | }} |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1725 | return |
| 1726 | } |
| 1727 | ch <- resp.GetDeviceImageStates() |
| 1728 | }(deviceID.GetId(), downloadReq, respCh) |
| 1729 | |
| 1730 | } |
| 1731 | |
| 1732 | return dMgr.waitForAllResponses(ctx, "download-image-to-device", respCh, len(request.GetDeviceId())) |
| 1733 | } |
| 1734 | |
| 1735 | func (dMgr *Manager) GetImageStatus(ctx context.Context, request *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { |
| 1736 | if err := dMgr.validateImageRequest(request); err != nil { |
| 1737 | return nil, err |
| 1738 | } |
| 1739 | |
| 1740 | ctx = utils.WithRPCMetadataContext(ctx, "GetImageStatus") |
| 1741 | |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1742 | respCh := make(chan []*voltha.DeviceImageState, len(request.GetDeviceId())) |
| 1743 | for index, deviceID := range request.DeviceId { |
ssiddiqui | 6bd4de0 | 2021-06-08 12:04:17 +0530 | [diff] [blame] | 1744 | // Create status request per device |
| 1745 | imageStatusReq := &voltha.DeviceImageRequest{ |
| 1746 | Version: request.Version, |
| 1747 | CommitOnSuccess: request.CommitOnSuccess, |
| 1748 | } |
| 1749 | |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1750 | //slice-out only single deviceID from the request |
| 1751 | imageStatusReq.DeviceId = request.DeviceId[index : index+1] |
| 1752 | |
| 1753 | go func(deviceID string, req *voltha.DeviceImageRequest, ch chan []*voltha.DeviceImageState) { |
| 1754 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
| 1755 | if agent == nil { |
ssiddiqui | b2f526c | 2021-10-11 17:11:25 +0530 | [diff] [blame] | 1756 | logger.Errorw(ctx, "Device-agent-not-found", log.Fields{"device-id": deviceID}) |
| 1757 | ch <- []*voltha.DeviceImageState{{ |
| 1758 | DeviceId: deviceID, |
| 1759 | ImageState: &voltha.ImageState{ |
| 1760 | Version: request.GetVersion(), |
| 1761 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 1762 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 1763 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 1764 | }, |
| 1765 | }} |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1766 | return |
| 1767 | } |
| 1768 | |
| 1769 | resp, err := agent.getImageStatus(ctx, req) |
| 1770 | if err != nil { |
| 1771 | logger.Errorw(ctx, "get-image-status-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | b2f526c | 2021-10-11 17:11:25 +0530 | [diff] [blame] | 1772 | ch <- []*voltha.DeviceImageState{{ |
| 1773 | DeviceId: deviceID, |
| 1774 | ImageState: &voltha.ImageState{ |
| 1775 | Version: request.GetVersion(), |
| 1776 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 1777 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 1778 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 1779 | }, |
| 1780 | }} |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1781 | return |
| 1782 | } |
| 1783 | |
| 1784 | err = dMgr.validateDeviceImageResponse(resp) |
| 1785 | if err != nil { |
| 1786 | logger.Errorw(ctx, "get-image-status-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | b2f526c | 2021-10-11 17:11:25 +0530 | [diff] [blame] | 1787 | ch <- []*voltha.DeviceImageState{{ |
| 1788 | DeviceId: deviceID, |
| 1789 | ImageState: &voltha.ImageState{ |
| 1790 | Version: request.GetVersion(), |
| 1791 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 1792 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 1793 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 1794 | }, |
| 1795 | }} |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1796 | return |
| 1797 | } |
| 1798 | ch <- resp.GetDeviceImageStates() |
| 1799 | }(deviceID.GetId(), imageStatusReq, respCh) |
| 1800 | |
| 1801 | } |
| 1802 | |
| 1803 | return dMgr.waitForAllResponses(ctx, "get-image-status", respCh, len(request.GetDeviceId())) |
| 1804 | } |
| 1805 | |
| 1806 | func (dMgr *Manager) AbortImageUpgradeToDevice(ctx context.Context, request *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { |
| 1807 | if err := dMgr.validateImageRequest(request); err != nil { |
| 1808 | return nil, err |
| 1809 | } |
| 1810 | |
| 1811 | ctx = utils.WithRPCMetadataContext(ctx, "AbortImageUpgradeToDevice") |
| 1812 | respCh := make(chan []*voltha.DeviceImageState, len(request.GetDeviceId())) |
| 1813 | |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1814 | for index, deviceID := range request.DeviceId { |
ssiddiqui | 6bd4de0 | 2021-06-08 12:04:17 +0530 | [diff] [blame] | 1815 | // Create abort request per device |
| 1816 | abortImageReq := &voltha.DeviceImageRequest{ |
| 1817 | Version: request.Version, |
| 1818 | CommitOnSuccess: request.CommitOnSuccess, |
| 1819 | } |
| 1820 | |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1821 | //slice-out only single deviceID from the request |
| 1822 | abortImageReq.DeviceId = request.DeviceId[index : index+1] |
| 1823 | |
| 1824 | go func(deviceID string, req *voltha.DeviceImageRequest, ch chan []*voltha.DeviceImageState) { |
| 1825 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
| 1826 | if agent == nil { |
ssiddiqui | b2f526c | 2021-10-11 17:11:25 +0530 | [diff] [blame] | 1827 | logger.Errorw(ctx, "Device-agent-not-found", log.Fields{"device-id": deviceID}) |
| 1828 | ch <- []*voltha.DeviceImageState{{ |
| 1829 | DeviceId: deviceID, |
| 1830 | ImageState: &voltha.ImageState{ |
| 1831 | Version: request.GetVersion(), |
| 1832 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 1833 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 1834 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 1835 | }, |
| 1836 | }} |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1837 | return |
| 1838 | } |
| 1839 | |
| 1840 | resp, err := agent.abortImageUpgradeToDevice(ctx, req) |
| 1841 | if err != nil { |
| 1842 | logger.Errorw(ctx, "abort-image-upgrade-to-device-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | b2f526c | 2021-10-11 17:11:25 +0530 | [diff] [blame] | 1843 | ch <- []*voltha.DeviceImageState{{ |
| 1844 | DeviceId: deviceID, |
| 1845 | ImageState: &voltha.ImageState{ |
| 1846 | Version: request.GetVersion(), |
| 1847 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 1848 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 1849 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 1850 | }, |
| 1851 | }} |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1852 | return |
| 1853 | } |
| 1854 | |
| 1855 | err = dMgr.validateDeviceImageResponse(resp) |
| 1856 | if err != nil { |
| 1857 | logger.Errorw(ctx, "abort-image-upgrade-to-device-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | b2f526c | 2021-10-11 17:11:25 +0530 | [diff] [blame] | 1858 | ch <- []*voltha.DeviceImageState{{ |
| 1859 | DeviceId: deviceID, |
| 1860 | ImageState: &voltha.ImageState{ |
| 1861 | Version: request.GetVersion(), |
| 1862 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 1863 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 1864 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 1865 | }, |
| 1866 | }} |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1867 | return |
| 1868 | } |
| 1869 | ch <- resp.GetDeviceImageStates() |
| 1870 | }(deviceID.GetId(), abortImageReq, respCh) |
| 1871 | |
| 1872 | } |
| 1873 | |
| 1874 | return dMgr.waitForAllResponses(ctx, "abort-image-upgrade-to-device", respCh, len(request.GetDeviceId())) |
| 1875 | } |
| 1876 | |
| 1877 | func (dMgr *Manager) GetOnuImages(ctx context.Context, id *common.ID) (*voltha.OnuImages, error) { |
| 1878 | if id == nil || id.Id == "" { |
| 1879 | return nil, status.Errorf(codes.InvalidArgument, "empty device id") |
| 1880 | } |
| 1881 | |
| 1882 | ctx = utils.WithRPCMetadataContext(ctx, "GetOnuImages") |
| 1883 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 1884 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 1885 | if agent == nil { |
| 1886 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
| 1887 | } |
| 1888 | |
| 1889 | resp, err := agent.getOnuImages(ctx, id) |
| 1890 | if err != nil { |
| 1891 | return nil, err |
| 1892 | } |
| 1893 | |
| 1894 | logger.Debugw(ctx, "get-onu-images-result", log.Fields{"onu-image": resp.Items}) |
| 1895 | |
| 1896 | return resp, nil |
| 1897 | } |
| 1898 | |
| 1899 | func (dMgr *Manager) ActivateImage(ctx context.Context, request *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { |
| 1900 | if err := dMgr.validateImageRequest(request); err != nil { |
| 1901 | return nil, err |
| 1902 | } |
| 1903 | |
| 1904 | ctx = utils.WithRPCMetadataContext(ctx, "ActivateImage") |
| 1905 | respCh := make(chan []*voltha.DeviceImageState, len(request.GetDeviceId())) |
| 1906 | |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1907 | for index, deviceID := range request.DeviceId { |
ssiddiqui | 6bd4de0 | 2021-06-08 12:04:17 +0530 | [diff] [blame] | 1908 | // Create activate request per device |
| 1909 | activateImageReq := &voltha.DeviceImageRequest{ |
| 1910 | Version: request.Version, |
| 1911 | CommitOnSuccess: request.CommitOnSuccess, |
| 1912 | } |
| 1913 | |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1914 | //slice-out only single deviceID from the request |
| 1915 | activateImageReq.DeviceId = request.DeviceId[index : index+1] |
| 1916 | |
| 1917 | go func(deviceID string, req *voltha.DeviceImageRequest, ch chan []*voltha.DeviceImageState) { |
| 1918 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
| 1919 | if agent == nil { |
ssiddiqui | b2f526c | 2021-10-11 17:11:25 +0530 | [diff] [blame] | 1920 | logger.Errorw(ctx, "Device-agent-not-found", log.Fields{"device-id": deviceID}) |
| 1921 | ch <- []*voltha.DeviceImageState{{ |
| 1922 | DeviceId: deviceID, |
| 1923 | ImageState: &voltha.ImageState{ |
| 1924 | Version: request.GetVersion(), |
| 1925 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 1926 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 1927 | ImageState: voltha.ImageState_IMAGE_ACTIVATION_ABORTED, |
| 1928 | }, |
| 1929 | }} |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1930 | return |
| 1931 | } |
| 1932 | |
| 1933 | resp, err := agent.activateImageOnDevice(ctx, req) |
| 1934 | if err != nil { |
| 1935 | logger.Errorw(ctx, "activate-image-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | b2f526c | 2021-10-11 17:11:25 +0530 | [diff] [blame] | 1936 | ch <- []*voltha.DeviceImageState{{ |
| 1937 | DeviceId: deviceID, |
| 1938 | ImageState: &voltha.ImageState{ |
| 1939 | Version: request.GetVersion(), |
| 1940 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 1941 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 1942 | ImageState: voltha.ImageState_IMAGE_ACTIVATION_ABORTED, |
| 1943 | }, |
| 1944 | }} |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1945 | return |
| 1946 | } |
| 1947 | |
| 1948 | err = dMgr.validateDeviceImageResponse(resp) |
| 1949 | if err != nil { |
| 1950 | logger.Errorw(ctx, "activate-image-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | b2f526c | 2021-10-11 17:11:25 +0530 | [diff] [blame] | 1951 | ch <- []*voltha.DeviceImageState{{ |
| 1952 | DeviceId: deviceID, |
| 1953 | ImageState: &voltha.ImageState{ |
| 1954 | Version: request.GetVersion(), |
| 1955 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 1956 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 1957 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 1958 | }, |
| 1959 | }} |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1960 | return |
| 1961 | } |
| 1962 | |
| 1963 | ch <- resp.GetDeviceImageStates() |
| 1964 | }(deviceID.GetId(), activateImageReq, respCh) |
| 1965 | |
| 1966 | } |
| 1967 | |
| 1968 | return dMgr.waitForAllResponses(ctx, "activate-image", respCh, len(request.GetDeviceId())) |
| 1969 | } |
| 1970 | |
| 1971 | func (dMgr *Manager) CommitImage(ctx context.Context, request *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { |
| 1972 | if err := dMgr.validateImageRequest(request); err != nil { |
| 1973 | return nil, err |
| 1974 | } |
| 1975 | |
| 1976 | ctx = utils.WithRPCMetadataContext(ctx, "CommitImage") |
| 1977 | respCh := make(chan []*voltha.DeviceImageState, len(request.GetDeviceId())) |
| 1978 | |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1979 | for index, deviceID := range request.DeviceId { |
ssiddiqui | 6bd4de0 | 2021-06-08 12:04:17 +0530 | [diff] [blame] | 1980 | // Create commit request per device |
| 1981 | commitImageReq := &voltha.DeviceImageRequest{ |
| 1982 | Version: request.Version, |
| 1983 | CommitOnSuccess: request.CommitOnSuccess, |
| 1984 | } |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 1985 | //slice-out only single deviceID from the request |
| 1986 | commitImageReq.DeviceId = request.DeviceId[index : index+1] |
| 1987 | |
| 1988 | go func(deviceID string, req *voltha.DeviceImageRequest, ch chan []*voltha.DeviceImageState) { |
| 1989 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
| 1990 | if agent == nil { |
ssiddiqui | b2f526c | 2021-10-11 17:11:25 +0530 | [diff] [blame] | 1991 | logger.Errorw(ctx, "Device-agent-not-found", log.Fields{"device-id": deviceID}) |
| 1992 | ch <- []*voltha.DeviceImageState{{ |
| 1993 | DeviceId: deviceID, |
| 1994 | ImageState: &voltha.ImageState{ |
| 1995 | Version: request.GetVersion(), |
| 1996 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 1997 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 1998 | ImageState: voltha.ImageState_IMAGE_COMMIT_ABORTED, |
| 1999 | }, |
| 2000 | }} |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 2001 | return |
| 2002 | } |
| 2003 | |
| 2004 | resp, err := agent.commitImage(ctx, req) |
| 2005 | if err != nil { |
| 2006 | logger.Errorw(ctx, "commit-image-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | b2f526c | 2021-10-11 17:11:25 +0530 | [diff] [blame] | 2007 | ch <- []*voltha.DeviceImageState{{ |
| 2008 | DeviceId: deviceID, |
| 2009 | ImageState: &voltha.ImageState{ |
| 2010 | Version: request.GetVersion(), |
| 2011 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 2012 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 2013 | ImageState: voltha.ImageState_IMAGE_COMMIT_ABORTED, |
| 2014 | }, |
| 2015 | }} |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 2016 | return |
| 2017 | } |
| 2018 | |
| 2019 | err = dMgr.validateDeviceImageResponse(resp) |
| 2020 | if err != nil { |
| 2021 | logger.Errorf(ctx, "commit-image-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | b2f526c | 2021-10-11 17:11:25 +0530 | [diff] [blame] | 2022 | ch <- []*voltha.DeviceImageState{{ |
| 2023 | DeviceId: deviceID, |
| 2024 | ImageState: &voltha.ImageState{ |
| 2025 | Version: request.GetVersion(), |
| 2026 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 2027 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 2028 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 2029 | }, |
| 2030 | }} |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 2031 | return |
| 2032 | } |
| 2033 | ch <- resp.GetDeviceImageStates() |
| 2034 | }(deviceID.GetId(), commitImageReq, respCh) |
| 2035 | |
| 2036 | } |
| 2037 | |
| 2038 | return dMgr.waitForAllResponses(ctx, "commit-image", respCh, len(request.GetDeviceId())) |
| 2039 | } |
| 2040 | |
| 2041 | func (dMgr *Manager) validateImageDownloadRequest(request *voltha.DeviceImageDownloadRequest) error { |
| 2042 | if request == nil || request.Image == nil || len(request.DeviceId) == 0 { |
| 2043 | return status.Errorf(codes.InvalidArgument, "invalid argument") |
| 2044 | } |
| 2045 | |
| 2046 | for _, deviceID := range request.DeviceId { |
| 2047 | if deviceID == nil { |
| 2048 | return status.Errorf(codes.InvalidArgument, "id is nil") |
| 2049 | } |
| 2050 | } |
| 2051 | return nil |
| 2052 | } |
| 2053 | |
| 2054 | func (dMgr *Manager) validateImageRequest(request *voltha.DeviceImageRequest) error { |
| 2055 | if request == nil || len(request.DeviceId) == 0 || request.DeviceId[0] == nil { |
| 2056 | return status.Errorf(codes.InvalidArgument, "invalid argument") |
| 2057 | } |
| 2058 | |
| 2059 | for _, deviceID := range request.DeviceId { |
| 2060 | if deviceID == nil { |
| 2061 | return status.Errorf(codes.InvalidArgument, "id is nil") |
| 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | return nil |
| 2066 | } |
| 2067 | |
| 2068 | func (dMgr *Manager) validateDeviceImageResponse(response *voltha.DeviceImageResponse) error { |
| 2069 | if response == nil || len(response.GetDeviceImageStates()) == 0 || response.GetDeviceImageStates()[0] == nil { |
| 2070 | return status.Errorf(codes.Internal, "invalid-response-from-adapter") |
| 2071 | } |
| 2072 | |
| 2073 | return nil |
| 2074 | } |
| 2075 | |
| 2076 | func (dMgr *Manager) waitForAllResponses(ctx context.Context, opName string, respCh chan []*voltha.DeviceImageState, expectedResps int) (*voltha.DeviceImageResponse, error) { |
| 2077 | response := &voltha.DeviceImageResponse{} |
| 2078 | respCount := 0 |
| 2079 | for { |
| 2080 | select { |
| 2081 | case resp, ok := <-respCh: |
| 2082 | if !ok { |
| 2083 | logger.Errorw(ctx, opName+"-failed", log.Fields{"error": "channel-closed"}) |
| 2084 | return response, status.Errorf(codes.Aborted, "channel-closed") |
| 2085 | } |
| 2086 | |
| 2087 | if resp != nil { |
| 2088 | logger.Debugw(ctx, opName+"-result", log.Fields{"image-state": resp[0].GetImageState(), "device-id": resp[0].GetDeviceId()}) |
| 2089 | response.DeviceImageStates = append(response.DeviceImageStates, resp...) |
| 2090 | } |
| 2091 | |
| 2092 | respCount++ |
| 2093 | |
| 2094 | //check whether all responses received, if so, sent back the collated response |
| 2095 | if respCount == expectedResps { |
| 2096 | return response, nil |
| 2097 | } |
| 2098 | continue |
| 2099 | case <-ctx.Done(): |
| 2100 | return nil, status.Errorf(codes.Aborted, opName+"-failed-%s", ctx.Err()) |
| 2101 | } |
| 2102 | } |
| 2103 | } |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 2104 | |
| 2105 | func (dMgr *Manager) ReconcilingCleanup(ctx context.Context, device *voltha.Device) error { |
| 2106 | agent := dMgr.getDeviceAgent(ctx, device.Id) |
| 2107 | if agent == nil { |
| 2108 | logger.Errorf(ctx, "Not able to get device agent.") |
| 2109 | return status.Errorf(codes.NotFound, "Not able to get device agent for device : %s", device.Id) |
| 2110 | } |
| 2111 | err := agent.reconcilingCleanup(ctx) |
| 2112 | if err != nil { |
| 2113 | logger.Errorf(ctx, err.Error()) |
| 2114 | return status.Errorf(codes.Internal, err.Error()) |
| 2115 | } |
| 2116 | return nil |
| 2117 | } |
serkant.uluderya | e25f648 | 2020-12-17 21:08:38 +0300 | [diff] [blame] | 2118 | |
| 2119 | func (dMgr *Manager) DeleteAllLogicalMeters(ctx context.Context, cDevice *voltha.Device) error { |
| 2120 | logger.Debugw(ctx, "delete-all-logical-device-meters", log.Fields{"device-id": cDevice.Id}) |
| 2121 | if err := dMgr.logicalDeviceMgr.deleteAllLogicalMeters(ctx, cDevice.Id); err != nil { |
| 2122 | // Just log the error. The logical device or port may already have been deleted before this callback is invoked. |
Andrea Campanella | 408dec6 | 2021-11-10 14:32:10 +0100 | [diff] [blame^] | 2123 | logger.Warnw(ctx, "delete-logical-meters-error", log.Fields{"device-id": cDevice.Id, "error": err}) |
serkant.uluderya | e25f648 | 2020-12-17 21:08:38 +0300 | [diff] [blame] | 2124 | } |
| 2125 | return nil |
| 2126 | |
| 2127 | } |