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