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