khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 16 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 17 | package device |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "errors" |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 22 | "sync" |
| 23 | "time" |
| 24 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 25 | "github.com/golang/protobuf/ptypes/empty" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 26 | "github.com/opencord/voltha-go/db/model" |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 27 | "github.com/opencord/voltha-go/rw_core/core/adapter" |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 28 | "github.com/opencord/voltha-go/rw_core/core/device/event" |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 29 | "github.com/opencord/voltha-go/rw_core/core/device/remote" |
Kent Hagerman | 6031aad | 2020-07-29 16:36:33 -0400 | [diff] [blame] | 30 | "github.com/opencord/voltha-go/rw_core/core/device/state" |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 31 | "github.com/opencord/voltha-go/rw_core/utils" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 32 | "github.com/opencord/voltha-lib-go/v4/pkg/kafka" |
| 33 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 34 | "github.com/opencord/voltha-protos/v4/go/common" |
| 35 | ic "github.com/opencord/voltha-protos/v4/go/inter_container" |
| 36 | "github.com/opencord/voltha-protos/v4/go/openflow_13" |
| 37 | ofp "github.com/opencord/voltha-protos/v4/go/openflow_13" |
| 38 | "github.com/opencord/voltha-protos/v4/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 39 | "google.golang.org/grpc/codes" |
| 40 | "google.golang.org/grpc/status" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 41 | ) |
| 42 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 43 | // Manager represent device manager attributes |
| 44 | type Manager struct { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 45 | deviceAgents sync.Map |
| 46 | rootDevices map[string]bool |
| 47 | lockRootDeviceMap sync.RWMutex |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 48 | adapterProxy *remote.AdapterProxy |
| 49 | adapterMgr *adapter.Manager |
| 50 | logicalDeviceMgr *LogicalManager |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 51 | kafkaICProxy kafka.InterContainerProxy |
Kent Hagerman | 6031aad | 2020-07-29 16:36:33 -0400 | [diff] [blame] | 52 | stateTransitions *state.TransitionMap |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 53 | dbPath *model.Path |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 54 | dProxy *model.Proxy |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 55 | coreInstanceID string |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 56 | defaultTimeout time.Duration |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 57 | devicesLoadingLock sync.RWMutex |
| 58 | deviceLoadingInProgress map[string][]chan int |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 59 | } |
| 60 | |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 61 | //NewManagers creates the Manager and the Logical Manager. |
serkant.uluderya | 8ff291d | 2020-05-20 00:58:00 -0700 | [diff] [blame] | 62 | func NewManagers(dbPath *model.Path, adapterMgr *adapter.Manager, kmp kafka.InterContainerProxy, endpointMgr kafka.EndpointManager, coreTopic, coreInstanceID string, defaultCoreTimeout time.Duration) (*Manager, *LogicalManager) { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 63 | deviceMgr := &Manager{ |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 64 | rootDevices: make(map[string]bool), |
| 65 | kafkaICProxy: kmp, |
serkant.uluderya | 8ff291d | 2020-05-20 00:58:00 -0700 | [diff] [blame] | 66 | adapterProxy: remote.NewAdapterProxy(kmp, coreTopic, endpointMgr), |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 67 | coreInstanceID: coreInstanceID, |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 68 | dbPath: dbPath, |
| 69 | dProxy: dbPath.Proxy("devices"), |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 70 | adapterMgr: adapterMgr, |
Kent Hagerman | f4151de | 2020-06-19 15:58:47 -0400 | [diff] [blame] | 71 | defaultTimeout: defaultCoreTimeout, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 72 | deviceLoadingInProgress: make(map[string][]chan int), |
| 73 | } |
Kent Hagerman | 6031aad | 2020-07-29 16:36:33 -0400 | [diff] [blame] | 74 | deviceMgr.stateTransitions = state.NewTransitionMap(deviceMgr) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 75 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 76 | logicalDeviceMgr := &LogicalManager{ |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 77 | Manager: event.NewManager(), |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 78 | deviceMgr: deviceMgr, |
| 79 | kafkaICProxy: kmp, |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 80 | dbPath: dbPath, |
| 81 | ldProxy: dbPath.Proxy("logical_devices"), |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 82 | defaultTimeout: defaultCoreTimeout, |
| 83 | logicalDeviceLoadingInProgress: make(map[string][]chan int), |
| 84 | } |
| 85 | deviceMgr.logicalDeviceMgr = logicalDeviceMgr |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 86 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 87 | adapterMgr.SetAdapterRestartedCallback(deviceMgr.adapterRestarted) |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 88 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 89 | return deviceMgr, logicalDeviceMgr |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 90 | } |
| 91 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 92 | func (dMgr *Manager) addDeviceAgentToMap(agent *Agent) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 93 | if _, exist := dMgr.deviceAgents.Load(agent.deviceID); !exist { |
| 94 | dMgr.deviceAgents.Store(agent.deviceID, agent) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 95 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 96 | dMgr.lockRootDeviceMap.Lock() |
| 97 | defer dMgr.lockRootDeviceMap.Unlock() |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 98 | dMgr.rootDevices[agent.deviceID] = agent.isRootDevice |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 99 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 102 | func (dMgr *Manager) deleteDeviceAgentFromMap(agent *Agent) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 103 | dMgr.deviceAgents.Delete(agent.deviceID) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 104 | dMgr.lockRootDeviceMap.Lock() |
| 105 | defer dMgr.lockRootDeviceMap.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 106 | delete(dMgr.rootDevices, agent.deviceID) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 107 | } |
| 108 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 109 | // 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] | 110 | func (dMgr *Manager) getDeviceAgent(ctx context.Context, deviceID string) *Agent { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 111 | agent, ok := dMgr.deviceAgents.Load(deviceID) |
| 112 | if ok { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 113 | return agent.(*Agent) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 114 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 115 | // 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] | 116 | err := dMgr.load(ctx, deviceID) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 117 | if err == nil { |
| 118 | agent, ok = dMgr.deviceAgents.Load(deviceID) |
| 119 | if !ok { |
| 120 | return nil |
| 121 | } |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 122 | return agent.(*Agent) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 123 | } |
| 124 | //TODO: Change the return params to return an error as well |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 125 | logger.Errorw(ctx, "loading-device-failed", log.Fields{"device-id": deviceID, "error": err}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 126 | return nil |
| 127 | } |
| 128 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 129 | // listDeviceIdsFromMap returns the list of device IDs that are in memory |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 130 | func (dMgr *Manager) listDeviceIdsFromMap() *voltha.IDs { |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 131 | result := &voltha.IDs{Items: make([]*voltha.ID, 0)} |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 132 | |
| 133 | dMgr.deviceAgents.Range(func(key, value interface{}) bool { |
| 134 | result.Items = append(result.Items, &voltha.ID{Id: key.(string)}) |
| 135 | return true |
| 136 | }) |
| 137 | |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 138 | return result |
| 139 | } |
| 140 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 141 | // CreateDevice creates a new parent device in the data model |
| 142 | func (dMgr *Manager) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) { |
| 143 | if device.MacAddress == "" && device.GetHostAndPort() == "" { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 144 | logger.Errorf(ctx, "No Device Info Present") |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 145 | return &voltha.Device{}, errors.New("no-device-info-present; MAC or HOSTIP&PORT") |
| 146 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 147 | logger.Debugw(ctx, "create-device", log.Fields{"device": *device}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 148 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 149 | deviceExist, err := dMgr.isParentDeviceExist(ctx, device) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 150 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 151 | logger.Errorf(ctx, "Failed to fetch parent device info") |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 152 | return nil, err |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 153 | } |
| 154 | if deviceExist { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 155 | 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] | 156 | return nil, errors.New("device is already pre-provisioned") |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 157 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 158 | logger.Debugw(ctx, "CreateDevice", log.Fields{"device": device, "aproxy": dMgr.adapterProxy}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 159 | |
khenaidoo | 5e677ae | 2019-02-28 17:26:29 -0500 | [diff] [blame] | 160 | // Ensure this device is set as root |
| 161 | device.Root = true |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 162 | // Create and start a device agent for that device |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 163 | agent := newAgent(dMgr.adapterProxy, device, dMgr, dMgr.dbPath, dMgr.dProxy, dMgr.defaultTimeout) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 164 | device, err = agent.start(ctx, device) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 165 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 166 | 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] | 167 | return nil, err |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 168 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 169 | dMgr.addDeviceAgentToMap(agent) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 170 | return device, nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 171 | } |
| 172 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 173 | // EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter |
| 174 | func (dMgr *Manager) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 175 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 176 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 177 | logger.Debugw(ctx, "EnableDevice", log.Fields{"device-id": id.Id}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 178 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 179 | if agent == nil { |
| 180 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 181 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 182 | return &empty.Empty{}, agent.enableDevice(ctx) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 185 | // DisableDevice disables a device along with any child device it may have |
| 186 | func (dMgr *Manager) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 187 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 188 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 189 | logger.Debugw(ctx, "DisableDevice", log.Fields{"device-id": id.Id}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 190 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 191 | if agent == nil { |
| 192 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 193 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 194 | return &empty.Empty{}, agent.disableDevice(ctx) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 195 | } |
| 196 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 197 | //RebootDevice invoked the reboot API to the corresponding adapter |
| 198 | func (dMgr *Manager) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 199 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 200 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 201 | logger.Debugw(ctx, "RebootDevice", log.Fields{"device-id": id.Id}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 202 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 203 | if agent == nil { |
| 204 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 205 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 206 | return &empty.Empty{}, agent.rebootDevice(ctx) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 207 | } |
| 208 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 209 | // DeleteDevice removes a device from the data model |
| 210 | func (dMgr *Manager) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 211 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 212 | logger.Debugw(ctx, "DeleteDevice", 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 | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 216 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 217 | return &empty.Empty{}, agent.deleteDevice(ctx) |
| 218 | } |
| 219 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 220 | // ForceDeleteDevice removes a device from the data model forcefully without successfully waiting for the adapters. |
| 221 | func (dMgr *Manager) ForceDeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 222 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 223 | logger.Debugw(ctx, "ForceDeleteDevice", log.Fields{"device-id": id.Id}) |
| 224 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 225 | if agent == nil { |
| 226 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
| 227 | } |
| 228 | return &empty.Empty{}, agent.deleteDeviceForce(ctx) |
| 229 | } |
| 230 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 231 | // GetDevicePort returns the port details for a specific device port entry |
| 232 | func (dMgr *Manager) GetDevicePort(ctx context.Context, deviceID string, portID uint32) (*voltha.Port, error) { |
| 233 | logger.Debugw(ctx, "ListDevicePorts", log.Fields{"device-id": deviceID}) |
| 234 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
| 235 | if agent == nil { |
| 236 | return nil, status.Errorf(codes.NotFound, "device-%s", deviceID) |
| 237 | } |
| 238 | return agent.getDevicePort(portID) |
| 239 | } |
| 240 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 241 | // ListDevicePorts returns the ports details for a specific device entry |
| 242 | func (dMgr *Manager) ListDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.Ports, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 243 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 244 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 245 | logger.Debugw(ctx, "ListDevicePorts", log.Fields{"device-id": id.Id}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 246 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 247 | if agent == nil { |
| 248 | return nil, status.Errorf(codes.NotFound, "device-%s", id.Id) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 249 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 250 | |
| 251 | ports := agent.listDevicePorts() |
| 252 | ctr, ret := 0, make([]*voltha.Port, len(ports)) |
| 253 | for _, port := range ports { |
| 254 | ret[ctr] = port |
| 255 | ctr++ |
| 256 | } |
| 257 | return &voltha.Ports{Items: ret}, nil |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // ListDeviceFlows returns the flow details for a specific device entry |
| 261 | func (dMgr *Manager) ListDeviceFlows(ctx context.Context, id *voltha.ID) (*ofp.Flows, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 262 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 263 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 264 | logger.Debugw(ctx, "ListDeviceFlows", log.Fields{"device-id": id.Id}) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 265 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 266 | if agent == nil { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 267 | return nil, status.Errorf(codes.NotFound, "device-%s", id.Id) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 268 | } |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 269 | |
| 270 | flows := agent.listDeviceFlows() |
| 271 | ctr, ret := 0, make([]*ofp.OfpFlowStats, len(flows)) |
| 272 | for _, flow := range flows { |
| 273 | ret[ctr] = flow |
| 274 | ctr++ |
| 275 | } |
| 276 | return &openflow_13.Flows{Items: ret}, nil |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | // ListDeviceFlowGroups returns the flow group details for a specific device entry |
| 280 | func (dMgr *Manager) ListDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*voltha.FlowGroups, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 281 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 282 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 283 | logger.Debugw(ctx, "ListDeviceFlowGroups", log.Fields{"device-id": id.Id}) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 284 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 285 | if agent == nil { |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 286 | return nil, status.Errorf(codes.NotFound, "device-%s", id.Id) |
| 287 | } |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 288 | groups := agent.listDeviceGroups() |
| 289 | ctr, ret := 0, make([]*openflow_13.OfpGroupEntry, len(groups)) |
| 290 | for _, group := range groups { |
| 291 | ret[ctr] = group |
| 292 | ctr++ |
| 293 | } |
| 294 | return &voltha.FlowGroups{Items: ret}, nil |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 295 | } |
| 296 | |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 297 | // stopManagingDevice stops the management of the device as well as any of its reference device and logical device. |
| 298 | // This function is called only in the Core that does not own this device. In the Core that owns this device then a |
| 299 | // deletion deletion also includes removal of any reference of this device. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 300 | func (dMgr *Manager) stopManagingDevice(ctx context.Context, id string) { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 301 | logger.Infow(ctx, "stopManagingDevice", log.Fields{"device-id": id}) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 302 | 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] | 303 | if device, err := dMgr.getDeviceReadOnly(ctx, id); err == nil && device.Root { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 304 | // stop managing the logical device |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 305 | _ = dMgr.logicalDeviceMgr.stopManagingLogicalDeviceWithDeviceID(ctx, id) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 306 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 307 | if agent := dMgr.getDeviceAgent(ctx, id); agent != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 308 | if err := agent.stop(ctx); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 309 | 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] | 310 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 311 | dMgr.deleteDeviceAgentFromMap(agent) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 316 | // RunPostDeviceDelete removes any reference of this device |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 317 | func (dMgr *Manager) RunPostDeviceDelete(ctx context.Context, cDevice *voltha.Device) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 318 | logger.Infow(ctx, "RunPostDeviceDelete", log.Fields{"device-id": cDevice.Id}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 319 | dMgr.stopManagingDevice(ctx, cDevice.Id) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 320 | return nil |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 321 | } |
| 322 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 323 | // GetDevice exists primarily to implement the gRPC interface. |
| 324 | // Internal functions should call getDeviceReadOnly instead. |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 325 | func (dMgr *Manager) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 326 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 327 | return dMgr.getDeviceReadOnly(ctx, id.Id) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 328 | } |
| 329 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 330 | // getDeviceReadOnly will returns a device, either from memory or from the dB, if present |
| 331 | func (dMgr *Manager) getDeviceReadOnly(ctx context.Context, id string) (*voltha.Device, error) { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 332 | logger.Debugw(ctx, "getDeviceReadOnly", log.Fields{"device-id": id}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 333 | if agent := dMgr.getDeviceAgent(ctx, id); agent != nil { |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 334 | return agent.getDeviceReadOnly(ctx) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 335 | } |
| 336 | return nil, status.Errorf(codes.NotFound, "%s", id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 337 | } |
| 338 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 339 | func (dMgr *Manager) listDevicePorts(ctx context.Context, id string) (map[uint32]*voltha.Port, error) { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 340 | logger.Debugw(ctx, "listDevicePorts", log.Fields{"device-id": id}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 341 | agent := dMgr.getDeviceAgent(ctx, id) |
| 342 | if agent == nil { |
| 343 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 344 | } |
| 345 | return agent.listDevicePorts(), nil |
| 346 | } |
| 347 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 348 | // 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] | 349 | func (dMgr *Manager) GetChildDevice(ctx context.Context, parentDeviceID string, serialNumber string, onuID int64, parentPortNo int64) (*voltha.Device, error) { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 350 | logger.Debugw(ctx, "GetChildDevice", log.Fields{"parent-device-id": parentDeviceID, "serialNumber": serialNumber, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 351 | "parentPortNo": parentPortNo, "onuId": onuID}) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 352 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 353 | parentDevicePorts, err := dMgr.listDevicePorts(ctx, parentDeviceID) |
| 354 | if err != nil { |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 355 | return nil, status.Errorf(codes.Aborted, "%s", err.Error()) |
| 356 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 357 | childDeviceIds := dMgr.getAllChildDeviceIds(ctx, parentDevicePorts) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 358 | if len(childDeviceIds) == 0 { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 359 | logger.Debugw(ctx, "no-child-devices", log.Fields{"parent-device-id": parentDeviceID, "serialNumber": serialNumber, "onuId": onuID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 360 | return nil, status.Errorf(codes.NotFound, "%s", parentDeviceID) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | var foundChildDevice *voltha.Device |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 364 | for childDeviceID := range childDeviceIds { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 365 | var found bool |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 366 | if searchDevice, err := dMgr.getDeviceReadOnly(ctx, childDeviceID); err == nil { |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 367 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 368 | foundOnuID := false |
| 369 | if searchDevice.ProxyAddress.OnuId == uint32(onuID) { |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 370 | if searchDevice.ParentPortNo == uint32(parentPortNo) { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 371 | logger.Debugw(ctx, "found-child-by-onuid", log.Fields{"parent-device-id": parentDeviceID, "onuId": onuID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 372 | foundOnuID = true |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | foundSerialNumber := false |
| 377 | if searchDevice.SerialNumber == serialNumber { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 378 | logger.Debugw(ctx, "found-child-by-serialnumber", log.Fields{"parent-device-id": parentDeviceID, "serialNumber": serialNumber}) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 379 | foundSerialNumber = true |
| 380 | } |
| 381 | |
| 382 | // if both onuId and serialNumber are provided both must be true for the device to be found |
| 383 | // otherwise whichever one found a match is good enough |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 384 | if onuID > 0 && serialNumber != "" { |
| 385 | found = foundOnuID && foundSerialNumber |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 386 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 387 | found = foundOnuID || foundSerialNumber |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 388 | } |
| 389 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 390 | if found { |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 391 | foundChildDevice = searchDevice |
| 392 | break |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | if foundChildDevice != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 398 | logger.Debugw(ctx, "child-device-found", log.Fields{"parent-device-id": parentDeviceID, "foundChildDevice": foundChildDevice}) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 399 | return foundChildDevice, nil |
| 400 | } |
| 401 | |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 402 | logger.Debugw(ctx, "child-device-not-found", log.Fields{"parent-device-id": parentDeviceID, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 403 | "serialNumber": serialNumber, "onuId": onuID, "parentPortNo": parentPortNo}) |
| 404 | return nil, status.Errorf(codes.NotFound, "%s", parentDeviceID) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 405 | } |
| 406 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 407 | // GetChildDeviceWithProxyAddress will return a device based on proxy address |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 408 | func (dMgr *Manager) GetChildDeviceWithProxyAddress(ctx context.Context, proxyAddress *voltha.Device_ProxyAddress) (*voltha.Device, error) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 409 | logger.Debugw(ctx, "GetChildDeviceWithProxyAddress", log.Fields{"proxyAddress": proxyAddress}) |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 410 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 411 | parentDevicePorts, err := dMgr.listDevicePorts(ctx, proxyAddress.DeviceId) |
| 412 | if err != nil { |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 413 | return nil, status.Errorf(codes.Aborted, "%s", err.Error()) |
| 414 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 415 | childDeviceIds := dMgr.getAllChildDeviceIds(ctx, parentDevicePorts) |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 416 | if len(childDeviceIds) == 0 { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 417 | 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] | 418 | return nil, status.Errorf(codes.NotFound, "%s", proxyAddress) |
| 419 | } |
| 420 | |
| 421 | var foundChildDevice *voltha.Device |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 422 | for childDeviceID := range childDeviceIds { |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 423 | if searchDevice, err := dMgr.getDeviceReadOnly(ctx, childDeviceID); err == nil { |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 424 | if searchDevice.ProxyAddress == proxyAddress { |
| 425 | foundChildDevice = searchDevice |
| 426 | break |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | if foundChildDevice != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 432 | logger.Debugw(ctx, "child-device-found", log.Fields{"proxyAddress": proxyAddress}) |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 433 | return foundChildDevice, nil |
| 434 | } |
| 435 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 436 | logger.Warnw(ctx, "child-device-not-found", log.Fields{"proxyAddress": proxyAddress}) |
Matt Jeanneret | 2a20aaa | 2019-03-05 21:04:02 -0500 | [diff] [blame] | 437 | return nil, status.Errorf(codes.NotFound, "%s", proxyAddress) |
| 438 | } |
| 439 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 440 | // IsDeviceInCache returns true if device is found in the map |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 441 | func (dMgr *Manager) IsDeviceInCache(id string) bool { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 442 | _, exist := dMgr.deviceAgents.Load(id) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 443 | return exist |
| 444 | } |
| 445 | |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 446 | // ListDevices retrieves the latest devices from the data model |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 447 | func (dMgr *Manager) ListDevices(ctx context.Context, _ *empty.Empty) (*voltha.Devices, error) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 448 | logger.Debug(ctx, "ListDevices") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 449 | result := &voltha.Devices{} |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 450 | |
| 451 | var devices []*voltha.Device |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 452 | if err := dMgr.dProxy.List(ctx, &devices); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 453 | logger.Errorw(ctx, "failed-to-list-devices-from-cluster-proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 454 | return nil, err |
| 455 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 456 | |
| 457 | for _, device := range devices { |
| 458 | // If device is not in memory then set it up |
| 459 | if !dMgr.IsDeviceInCache(device.Id) { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 460 | logger.Debugw(ctx, "loading-device-from-Model", log.Fields{"device-id": device.Id}) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 461 | agent := newAgent(dMgr.adapterProxy, device, dMgr, dMgr.dbPath, dMgr.dProxy, dMgr.defaultTimeout) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 462 | if _, err := agent.start(ctx, nil); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 463 | logger.Warnw(ctx, "failure-starting-agent", log.Fields{"device-id": device.Id}) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 464 | } else { |
| 465 | dMgr.addDeviceAgentToMap(agent) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 466 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 467 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 468 | result.Items = append(result.Items, device) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 469 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 470 | logger.Debugw(ctx, "ListDevices-end", log.Fields{"len": len(result.Items)}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 471 | return result, nil |
| 472 | } |
| 473 | |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 474 | //isParentDeviceExist checks whether device is already preprovisioned. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 475 | 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] | 476 | hostPort := newDevice.GetHostAndPort() |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 477 | var devices []*voltha.Device |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 478 | if err := dMgr.dProxy.List(ctx, &devices); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 479 | 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] | 480 | return false, err |
| 481 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 482 | for _, device := range devices { |
| 483 | if !device.Root { |
| 484 | continue |
| 485 | } |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 486 | |
| 487 | if hostPort != "" && hostPort == device.GetHostAndPort() { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 488 | return true, nil |
| 489 | } |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 490 | if newDevice.MacAddress != "" && newDevice.MacAddress == device.MacAddress { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 491 | return true, nil |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 492 | } |
| 493 | } |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 494 | return false, nil |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 495 | } |
| 496 | |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 497 | //getDeviceFromModelretrieves the device data from the model. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 498 | func (dMgr *Manager) getDeviceFromModel(ctx context.Context, deviceID string) (*voltha.Device, error) { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 499 | device := &voltha.Device{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 500 | if have, err := dMgr.dProxy.Get(ctx, deviceID, device); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 501 | 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] | 502 | return nil, err |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 503 | } else if !have { |
| 504 | return nil, status.Error(codes.NotFound, deviceID) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 505 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 506 | |
| 507 | return device, nil |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 508 | } |
| 509 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 510 | // loadDevice loads the deviceID in memory, if not present |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 511 | func (dMgr *Manager) loadDevice(ctx context.Context, deviceID string) (*Agent, error) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 512 | if deviceID == "" { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 513 | return nil, status.Error(codes.InvalidArgument, "deviceId empty") |
| 514 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 515 | var err error |
| 516 | var device *voltha.Device |
| 517 | dMgr.devicesLoadingLock.Lock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 518 | if _, exist := dMgr.deviceLoadingInProgress[deviceID]; !exist { |
| 519 | if !dMgr.IsDeviceInCache(deviceID) { |
| 520 | dMgr.deviceLoadingInProgress[deviceID] = []chan int{make(chan int, 1)} |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 521 | dMgr.devicesLoadingLock.Unlock() |
| 522 | // 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] | 523 | if device, err = dMgr.getDeviceFromModel(ctx, deviceID); err == nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 524 | logger.Debugw(ctx, "loading-device", log.Fields{"device-id": deviceID}) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 525 | agent := newAgent(dMgr.adapterProxy, device, dMgr, dMgr.dbPath, dMgr.dProxy, dMgr.defaultTimeout) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 526 | if _, err = agent.start(ctx, nil); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 527 | logger.Warnw(ctx, "Failure loading device", log.Fields{"device-id": deviceID, "error": err}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 528 | } else { |
| 529 | dMgr.addDeviceAgentToMap(agent) |
| 530 | } |
| 531 | } else { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 532 | logger.Debugw(ctx, "Device not in model", log.Fields{"device-id": deviceID}) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 533 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 534 | // announce completion of task to any number of waiting channels |
| 535 | dMgr.devicesLoadingLock.Lock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 536 | if v, ok := dMgr.deviceLoadingInProgress[deviceID]; ok { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 537 | for _, ch := range v { |
| 538 | close(ch) |
| 539 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 540 | delete(dMgr.deviceLoadingInProgress, deviceID) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 541 | } |
| 542 | dMgr.devicesLoadingLock.Unlock() |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 543 | } else { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 544 | dMgr.devicesLoadingLock.Unlock() |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 545 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 546 | } else { |
| 547 | ch := make(chan int, 1) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 548 | dMgr.deviceLoadingInProgress[deviceID] = append(dMgr.deviceLoadingInProgress[deviceID], ch) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 549 | dMgr.devicesLoadingLock.Unlock() |
| 550 | // Wait for the channel to be closed, implying the process loading this device is done. |
| 551 | <-ch |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 552 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 553 | if agent, ok := dMgr.deviceAgents.Load(deviceID); ok { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 554 | return agent.(*Agent), nil |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 555 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 556 | return nil, status.Errorf(codes.Aborted, "Error loading device %s", deviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | // loadRootDeviceParentAndChildren loads the children and parents of a root device in memory |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 560 | 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] | 561 | logger.Debugw(ctx, "loading-parent-and-children", log.Fields{"device-id": device.Id}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 562 | if device.Root { |
| 563 | // Scenario A |
| 564 | if device.ParentId != "" { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 565 | // Load logical device if needed. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 566 | if err := dMgr.logicalDeviceMgr.load(ctx, device.ParentId); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 567 | 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] | 568 | } |
| 569 | } else { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 570 | logger.Debugw(ctx, "no-parent-to-load", log.Fields{"device-id": device.Id}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 571 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 572 | // Load all child devices, if needed |
| 573 | childDeviceIds := dMgr.getAllChildDeviceIds(ctx, devicePorts) |
| 574 | for childDeviceID := range childDeviceIds { |
| 575 | if _, err := dMgr.loadDevice(ctx, childDeviceID); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 576 | 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] | 577 | return err |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 578 | } |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 579 | } |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 580 | logger.Debugw(ctx, "loaded-children", log.Fields{"device-id": device.Id, "numChildren": len(childDeviceIds)}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 581 | } |
| 582 | return nil |
| 583 | } |
| 584 | |
| 585 | // load loads the deviceId in memory, if not present, and also loads its accompanying parents and children. Loading |
| 586 | // in memory is for improved performance. It is not imperative that a device needs to be in memory when a request |
| 587 | // acting on the device is received by the core. In such a scenario, the Core will load the device in memory first |
| 588 | // and the proceed with the request. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 589 | func (dMgr *Manager) load(ctx context.Context, deviceID string) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 590 | logger.Debug(ctx, "load...") |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 591 | // 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] | 592 | var dAgent *Agent |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 593 | var err error |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 594 | if dAgent, err = dMgr.loadDevice(ctx, deviceID); err != nil { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 595 | return err |
| 596 | } |
| 597 | // Get the loaded device details |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 598 | device, err := dAgent.getDeviceReadOnly(ctx) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 599 | if err != nil { |
| 600 | return err |
| 601 | } |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 602 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 603 | // If the device is in Pre-provisioning or getting deleted state stop here |
| 604 | if device.AdminState == voltha.AdminState_PREPROVISIONED || dAgent.isDeletionInProgress() { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 605 | return nil |
| 606 | } |
| 607 | |
| 608 | // Now we face two scenarios |
| 609 | if device.Root { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 610 | devicePorts := dAgent.listDevicePorts() |
| 611 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 612 | // 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] | 613 | if err := dMgr.loadRootDeviceParentAndChildren(ctx, device, devicePorts); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 614 | 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] | 615 | return err |
| 616 | } |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 617 | logger.Debugw(ctx, "successfully-loaded-parent-and-children", log.Fields{"device-id": deviceID}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 618 | } else { |
| 619 | // Scenario B - use the parentId of that device (root device) to trigger the loading |
| 620 | if device.ParentId != "" { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 621 | return dMgr.load(ctx, device.ParentId) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | return nil |
| 625 | } |
| 626 | |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 627 | // 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] | 628 | func (dMgr *Manager) ListDeviceIds(ctx context.Context, _ *empty.Empty) (*voltha.IDs, error) { |
| 629 | logger.Debug(ctx, "ListDeviceIDs") |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 630 | // Report only device IDs that are in the device agent map |
| 631 | return dMgr.listDeviceIdsFromMap(), nil |
| 632 | } |
| 633 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 634 | // ReconcileDevices is a request to a voltha core to update its list of managed devices. This will |
| 635 | // trigger loading the devices along with their children and parent in memory |
| 636 | func (dMgr *Manager) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 637 | logger.Debugw(ctx, "ReconcileDevices", log.Fields{"numDevices": len(ids.Items)}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 638 | if ids != nil && len(ids.Items) != 0 { |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 639 | toReconcile := len(ids.Items) |
| 640 | reconciled := 0 |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 641 | var err error |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 642 | for _, id := range ids.Items { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 643 | if err = dMgr.load(ctx, id.Id); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 644 | 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] | 645 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 646 | reconciled++ |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | if toReconcile != reconciled { |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 650 | 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] | 651 | } |
| 652 | } else { |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 653 | return nil, status.Errorf(codes.InvalidArgument, "empty-list-of-ids") |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 654 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 655 | return &empty.Empty{}, nil |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 656 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 657 | |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 658 | // isOkToReconcile validates whether a device is in the correct status to be reconciled |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 659 | func (dMgr *Manager) isOkToReconcile(ctx context.Context, device *voltha.Device) bool { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 660 | if device == nil { |
| 661 | return false |
| 662 | } |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 663 | if agent := dMgr.getDeviceAgent(ctx, device.Id); agent != nil { |
| 664 | return device.AdminState != voltha.AdminState_PREPROVISIONED && (!agent.isDeletionInProgress()) |
| 665 | } |
| 666 | return false |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | // adapterRestarted is invoked whenever an adapter is restarted |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 670 | func (dMgr *Manager) adapterRestarted(ctx context.Context, adapter *voltha.Adapter) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 671 | logger.Debugw(ctx, "adapter-restarted", log.Fields{"adapterId": adapter.Id, "vendor": adapter.Vendor, |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 672 | "currentReplica": adapter.CurrentReplica, "totalReplicas": adapter.TotalReplicas, "endpoint": adapter.Endpoint}) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 673 | |
| 674 | // Let's reconcile the device managed by this Core only |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 675 | if len(dMgr.rootDevices) == 0 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 676 | logger.Debugw(ctx, "nothing-to-reconcile", log.Fields{"adapterId": adapter.Id}) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 677 | return nil |
| 678 | } |
| 679 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 680 | responses := make([]utils.Response, 0) |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 681 | for rootDeviceID := range dMgr.rootDevices { |
Mahir Gunyel | 12cf55f | 2020-12-16 17:55:03 -0800 | [diff] [blame] | 682 | dAgent := dMgr.getDeviceAgent(ctx, rootDeviceID) |
| 683 | if dAgent == nil { |
| 684 | continue |
| 685 | } |
| 686 | logger.Debugw(ctx, "checking-adapter-type", log.Fields{"agentType": dAgent.deviceType, "adapterType": adapter.Type}) |
| 687 | if dAgent.deviceType == adapter.Type { |
| 688 | rootDevice, _ := dAgent.getDeviceReadOnly(ctx) |
| 689 | if rootDevice == nil { |
| 690 | continue |
| 691 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 692 | isDeviceOwnedByService, err := dMgr.adapterProxy.IsDeviceOwnedByService(ctx, rootDeviceID, adapter.Type, adapter.CurrentReplica) |
serkant.uluderya | 7d545aa | 2020-04-09 13:38:45 -0700 | [diff] [blame] | 693 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 694 | logger.Warnw(ctx, "is-device-owned-by-service", log.Fields{"error": err, "root-device-id": rootDeviceID, "adapterType": adapter.Type, "replica-number": adapter.CurrentReplica}) |
serkant.uluderya | 7d545aa | 2020-04-09 13:38:45 -0700 | [diff] [blame] | 695 | continue |
| 696 | } |
| 697 | if isDeviceOwnedByService { |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 698 | if dMgr.isOkToReconcile(ctx, rootDevice) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 699 | logger.Debugw(ctx, "reconciling-root-device", log.Fields{"rootId": rootDevice.Id}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 700 | responses = append(responses, dMgr.sendReconcileDeviceRequest(ctx, rootDevice)) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 701 | } else { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 702 | 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] | 703 | } |
| 704 | } else { // Should we be reconciling the root's children instead? |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 705 | rootDevicePorts, _ := dMgr.listDevicePorts(ctx, rootDeviceID) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 706 | childManagedByAdapter: |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 707 | for _, port := range rootDevicePorts { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 708 | for _, peer := range port.Peers { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 709 | if childDevice, _ := dMgr.getDeviceFromModel(ctx, peer.DeviceId); childDevice != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 710 | isDeviceOwnedByService, err := dMgr.adapterProxy.IsDeviceOwnedByService(ctx, childDevice.Id, adapter.Type, adapter.CurrentReplica) |
serkant.uluderya | 7d545aa | 2020-04-09 13:38:45 -0700 | [diff] [blame] | 711 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 712 | logger.Warnw(ctx, "is-device-owned-by-service", log.Fields{"error": err, "child-device-id": childDevice.Id, "adapterType": adapter.Type, "replica-number": adapter.CurrentReplica}) |
serkant.uluderya | 7d545aa | 2020-04-09 13:38:45 -0700 | [diff] [blame] | 713 | } |
| 714 | if isDeviceOwnedByService { |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 715 | if dMgr.isOkToReconcile(ctx, childDevice) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 716 | logger.Debugw(ctx, "reconciling-child-device", log.Fields{"child-device-id": childDevice.Id}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 717 | responses = append(responses, dMgr.sendReconcileDeviceRequest(ctx, childDevice)) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 718 | } else { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 719 | 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] | 720 | } |
| 721 | } else { |
| 722 | // All child devices under a parent device are typically managed by the same adapter type. |
| 723 | // Therefore we only need to check whether the first device we retrieved is managed by that adapter |
| 724 | break childManagedByAdapter |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 732 | if len(responses) > 0 { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 733 | // Wait for completion |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 734 | if res := utils.WaitForNilOrErrorResponses(dMgr.defaultTimeout, responses...); res != nil { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 735 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 736 | } |
| 737 | } else { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 738 | logger.Debugw(ctx, "no-managed-device-to-reconcile", log.Fields{"adapterId": adapter.Id}) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 739 | } |
| 740 | return nil |
| 741 | } |
| 742 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 743 | func (dMgr *Manager) sendReconcileDeviceRequest(ctx context.Context, device *voltha.Device) utils.Response { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 744 | // Send a reconcile request to the adapter. Since this Core may not be managing this device then there is no |
| 745 | // point of creating a device agent (if the device is not being managed by this Core) before sending the request |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 746 | // to the adapter. We will therefore bypass the adapter adapter and send the request directly to the adapter via |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 747 | // the adapter proxy. |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 748 | response := utils.NewResponse() |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 749 | ch, err := dMgr.adapterProxy.ReconcileDevice(ctx, device) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 750 | if err != nil { |
| 751 | response.Error(err) |
| 752 | } |
| 753 | // Wait for adapter response in its own routine |
| 754 | go func() { |
| 755 | resp, ok := <-ch |
| 756 | if !ok { |
| 757 | response.Error(status.Errorf(codes.Aborted, "channel-closed-device: %s", device.Id)) |
| 758 | } else if resp.Err != nil { |
| 759 | response.Error(resp.Err) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 760 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 761 | response.Done() |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 762 | }() |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 763 | return response |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 764 | } |
| 765 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 766 | func (dMgr *Manager) ReconcileChildDevices(ctx context.Context, parentDeviceID string) error { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 767 | if parentDevicePorts, err := dMgr.listDevicePorts(ctx, parentDeviceID); err == nil { |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 768 | responses := make([]utils.Response, 0) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 769 | for _, port := range parentDevicePorts { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 770 | for _, peer := range port.Peers { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 771 | if childDevice, err := dMgr.getDeviceFromModel(ctx, peer.DeviceId); err == nil { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 772 | responses = append(responses, dMgr.sendReconcileDeviceRequest(ctx, childDevice)) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | } |
| 776 | // Wait for completion |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 777 | if res := utils.WaitForNilOrErrorResponses(dMgr.defaultTimeout, responses...); res != nil { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 778 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 779 | } |
| 780 | } |
| 781 | return nil |
| 782 | } |
| 783 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 784 | func (dMgr *Manager) UpdateDeviceUsingAdapterData(ctx context.Context, device *voltha.Device) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 785 | logger.Debugw(ctx, "UpdateDeviceUsingAdapterData", log.Fields{"device-id": device.Id, "device": device}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 786 | if agent := dMgr.getDeviceAgent(ctx, device.Id); agent != nil { |
| 787 | return agent.updateDeviceUsingAdapterData(ctx, device) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 788 | } |
| 789 | return status.Errorf(codes.NotFound, "%s", device.Id) |
| 790 | } |
| 791 | |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 792 | func (dMgr *Manager) addPeerPort(ctx context.Context, deviceID string, port *voltha.Port) error { |
| 793 | meAsPeer := &voltha.Port_PeerPort{DeviceId: deviceID, PortNo: port.PortNo} |
| 794 | for _, peerPort := range port.Peers { |
| 795 | if agent := dMgr.getDeviceAgent(ctx, peerPort.DeviceId); agent != nil { |
| 796 | if err := agent.addPeerPort(ctx, meAsPeer); err != nil { |
| 797 | return err |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | // Notify the logical device manager to setup a logical port, if needed. If the added port is an NNI or UNI |
| 802 | // then a logical port will be added to the logical device and the device route generated. If the port is a |
| 803 | // PON port then only the device graph will be generated. |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 804 | device, err := dMgr.getDeviceReadOnly(ctx, deviceID) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 805 | if err != nil { |
| 806 | return err |
| 807 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 808 | ports, err := dMgr.listDevicePorts(ctx, deviceID) |
| 809 | if err != nil { |
| 810 | return err |
| 811 | } |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 812 | if err = dMgr.logicalDeviceMgr.updateLogicalPort(log.WithSpanFromContext(context.Background(), ctx), device, ports, port); err != nil { |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 813 | return err |
| 814 | } |
| 815 | return nil |
| 816 | } |
| 817 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 818 | func (dMgr *Manager) AddPort(ctx context.Context, deviceID string, port *voltha.Port) error { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 819 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 820 | if agent != nil { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 821 | if err := agent.addPort(ctx, port); err != nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 822 | return err |
| 823 | } |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 824 | // Setup peer ports in its own routine |
| 825 | go func() { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 826 | if err := dMgr.addPeerPort(log.WithSpanFromContext(context.Background(), ctx), deviceID, port); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 827 | 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] | 828 | } |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 829 | }() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 830 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 831 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 832 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 833 | } |
| 834 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 835 | func (dMgr *Manager) addFlowsAndGroups(ctx context.Context, deviceID string, flows []*ofp.OfpFlowStats, groups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 836 | logger.Debugw(ctx, "addFlowsAndGroups", log.Fields{"device-id": deviceID, "groups:": groups, "flowMetadata": flowMetadata}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 837 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 838 | return agent.addFlowsAndGroups(ctx, flows, groups, flowMetadata) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 839 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 840 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 841 | } |
| 842 | |
khenaidoo | 787224a | 2020-04-16 18:08:47 -0400 | [diff] [blame] | 843 | // deleteParentFlows removes flows from the parent device based on specific attributes |
| 844 | func (dMgr *Manager) deleteParentFlows(ctx context.Context, deviceID string, uniPort uint32, metadata *voltha.FlowMetadata) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 845 | logger.Debugw(ctx, "deleteParentFlows", log.Fields{"device-id": deviceID, "uni-port": uniPort, "metadata": metadata}) |
khenaidoo | 787224a | 2020-04-16 18:08:47 -0400 | [diff] [blame] | 846 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 847 | if !agent.isRootDevice { |
khenaidoo | 787224a | 2020-04-16 18:08:47 -0400 | [diff] [blame] | 848 | return status.Errorf(codes.FailedPrecondition, "not-a-parent-device-%s", deviceID) |
| 849 | } |
| 850 | return agent.filterOutFlows(ctx, uniPort, metadata) |
| 851 | } |
| 852 | return status.Errorf(codes.NotFound, "%s", deviceID) |
| 853 | } |
| 854 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 855 | func (dMgr *Manager) deleteFlowsAndGroups(ctx context.Context, deviceID string, flows []*ofp.OfpFlowStats, groups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 856 | logger.Debugw(ctx, "deleteFlowsAndGroups", log.Fields{"device-id": deviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 857 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 858 | return agent.deleteFlowsAndGroups(ctx, flows, groups, flowMetadata) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 859 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 860 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 861 | } |
| 862 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 863 | func (dMgr *Manager) updateFlowsAndGroups(ctx context.Context, deviceID string, flows []*ofp.OfpFlowStats, groups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 864 | logger.Debugw(ctx, "updateFlowsAndGroups", log.Fields{"device-id": deviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 865 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 866 | return agent.updateFlowsAndGroups(ctx, flows, groups, flowMetadata) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 867 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 868 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 869 | } |
| 870 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 871 | // 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] | 872 | // following a user action |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 873 | func (dMgr *Manager) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 874 | log.EnrichSpan(ctx, log.Fields{"device-id": configs.Id}) |
| 875 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 876 | if configs.Id == "" { |
| 877 | return nil, status.Error(codes.FailedPrecondition, "invalid-device-Id") |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 878 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 879 | agent := dMgr.getDeviceAgent(ctx, configs.Id) |
| 880 | if agent == nil { |
| 881 | return nil, status.Errorf(codes.NotFound, "%s", configs.Id) |
| 882 | } |
| 883 | return &empty.Empty{}, agent.updatePmConfigs(ctx, configs) |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 884 | } |
| 885 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 886 | // InitPmConfigs initialize the pm configs as defined by the adapter. |
| 887 | func (dMgr *Manager) InitPmConfigs(ctx context.Context, deviceID string, pmConfigs *voltha.PmConfigs) error { |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 888 | if pmConfigs.Id == "" { |
| 889 | return status.Errorf(codes.FailedPrecondition, "invalid-device-Id") |
| 890 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 891 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 892 | return agent.initPmConfigs(ctx, pmConfigs) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 893 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 894 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 895 | } |
| 896 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 897 | // ListDevicePmConfigs returns pm configs of device |
| 898 | func (dMgr *Manager) ListDevicePmConfigs(ctx context.Context, id *voltha.ID) (*voltha.PmConfigs, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 899 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 900 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 901 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 902 | if agent == nil { |
| 903 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 904 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 905 | return agent.listPmConfigs(ctx) |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 906 | } |
| 907 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 908 | func (dMgr *Manager) getSwitchCapability(ctx context.Context, deviceID string) (*ic.SwitchCapability, error) { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 909 | logger.Debugw(ctx, "getSwitchCapability", log.Fields{"device-id": deviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 910 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 911 | return agent.getSwitchCapability(ctx) |
| 912 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 913 | return nil, status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 914 | } |
| 915 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 916 | func (dMgr *Manager) GetPorts(ctx context.Context, deviceID string, portType voltha.Port_PortType) (*voltha.Ports, error) { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 917 | logger.Debugw(ctx, "GetPorts", log.Fields{"device-id": deviceID, "portType": portType}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 918 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
| 919 | if agent == nil { |
| 920 | return nil, status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 921 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 922 | return agent.getPorts(ctx, portType), nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 923 | } |
| 924 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 925 | func (dMgr *Manager) UpdateDeviceStatus(ctx context.Context, deviceID string, operStatus voltha.OperStatus_Types, connStatus voltha.ConnectStatus_Types) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 926 | logger.Debugw(ctx, "UpdateDeviceStatus", log.Fields{"device-id": deviceID, "operStatus": operStatus, "connStatus": connStatus}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 927 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 928 | return agent.updateDeviceStatus(ctx, operStatus, connStatus) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 929 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 930 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 931 | } |
| 932 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 933 | func (dMgr *Manager) UpdateChildrenStatus(ctx context.Context, deviceID string, operStatus voltha.OperStatus_Types, connStatus voltha.ConnectStatus_Types) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 934 | logger.Debugw(ctx, "UpdateChildrenStatus", log.Fields{"parent-device-id": deviceID, "operStatus": operStatus, "connStatus": connStatus}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 935 | parentDevicePorts, err := dMgr.listDevicePorts(ctx, deviceID) |
| 936 | if err != nil { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 937 | return status.Errorf(codes.Aborted, "%s", err.Error()) |
| 938 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 939 | for childDeviceID := range dMgr.getAllChildDeviceIds(ctx, parentDevicePorts) { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 940 | if agent := dMgr.getDeviceAgent(ctx, childDeviceID); agent != nil { |
| 941 | if err = agent.updateDeviceStatus(ctx, operStatus, connStatus); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 942 | return status.Errorf(codes.Aborted, "childDevice:%s, error:%s", childDeviceID, err.Error()) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | } |
| 946 | return nil |
| 947 | } |
| 948 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 949 | func (dMgr *Manager) UpdatePortState(ctx context.Context, deviceID string, portType voltha.Port_PortType, portNo uint32, operStatus voltha.OperStatus_Types) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 950 | logger.Debugw(ctx, "UpdatePortState", log.Fields{"device-id": deviceID, "portType": portType, "portNo": portNo, "operStatus": operStatus}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 951 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 952 | if err := agent.updatePortState(ctx, portType, portNo, operStatus); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 953 | logger.Errorw(ctx, "updating-port-state-failed", log.Fields{"device-id": deviceID, "portNo": portNo, "error": err}) |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 954 | return err |
| 955 | } |
| 956 | // Notify the logical device manager to change the port state |
Mahir Gunyel | 18fa0c9 | 2020-03-06 13:34:04 -0800 | [diff] [blame] | 957 | // Do this for NNI and UNIs only. PON ports are not known by logical device |
| 958 | if portType == voltha.Port_ETHERNET_NNI || portType == voltha.Port_ETHERNET_UNI { |
| 959 | go func() { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 960 | err := dMgr.logicalDeviceMgr.updatePortState(log.WithSpanFromContext(context.Background(), ctx), 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 { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 978 | logger.Debugw(ctx, "DeleteAllPorts", 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() { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 988 | if err := dMgr.logicalDeviceMgr.deleteAllLogicalPorts(log.WithSpanFromContext(context.Background(), ctx), device); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 989 | logger.Errorw(ctx, "unable-to-delete-logical-ports", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 990 | } |
| 991 | }() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 992 | } else { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 993 | logger.Warnw(ctx, "failed-to-retrieve-device", log.Fields{"device-id": deviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 994 | return err |
| 995 | } |
| 996 | return nil |
| 997 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 998 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 999 | } |
| 1000 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1001 | //UpdatePortsState updates all ports on the device |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1002 | func (dMgr *Manager) UpdatePortsState(ctx context.Context, deviceID string, portTypeFilter uint32, state voltha.OperStatus_Types) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1003 | logger.Debugw(ctx, "UpdatePortsState", log.Fields{"device-id": deviceID}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1004 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
| 1005 | if agent == nil { |
| 1006 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 1007 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1008 | if state != voltha.OperStatus_ACTIVE && state != voltha.OperStatus_UNKNOWN { |
| 1009 | return status.Error(codes.Unimplemented, "state-change-not-implemented") |
| 1010 | } |
| 1011 | if err := agent.updatePortsOperState(ctx, portTypeFilter, state); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1012 | logger.Warnw(ctx, "updatePortsOperState-failed", log.Fields{"device-id": deviceID, "error": err}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1013 | return err |
| 1014 | } |
| 1015 | return nil |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 1016 | } |
| 1017 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1018 | func (dMgr *Manager) ChildDeviceDetected(ctx context.Context, parentDeviceID string, parentPortNo int64, deviceType string, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1019 | channelID int64, vendorID string, serialNumber string, onuID int64) (*voltha.Device, error) { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1020 | logger.Debugw(ctx, "ChildDeviceDetected", log.Fields{"parent-device-id": parentDeviceID, "parentPortNo": parentPortNo, "deviceType": deviceType, "channelId": channelID, "vendorId": vendorID, "serialNumber": serialNumber, "onuId": onuID}) |
Chaitrashree G S | 4b3fada | 2019-07-28 23:55:25 -0700 | [diff] [blame] | 1021 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1022 | if deviceType == "" && vendorID != "" { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1023 | logger.Debug(ctx, "device-type-is-nil-fetching-device-type") |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1024 | deviceTypes, err := dMgr.adapterMgr.ListDeviceTypes(ctx, nil) |
| 1025 | if err != nil { |
| 1026 | return nil, err |
| 1027 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 1028 | OLoop: |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1029 | for _, dType := range deviceTypes.Items { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 1030 | for _, v := range dType.VendorIds { |
| 1031 | if v == vendorID { |
| 1032 | deviceType = dType.Adapter |
| 1033 | break OLoop |
Chaitrashree G S | 4b3fada | 2019-07-28 23:55:25 -0700 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | //if no match found for the vendorid,report adapter with the custom error message |
| 1039 | if deviceType == "" { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1040 | logger.Errorw(ctx, "failed-to-fetch-adapter-name ", log.Fields{"vendorId": vendorID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1041 | return nil, status.Errorf(codes.NotFound, "%s", vendorID) |
Chaitrashree G S | 4b3fada | 2019-07-28 23:55:25 -0700 | [diff] [blame] | 1042 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1043 | |
| 1044 | // Create the ONU device |
| 1045 | childDevice := &voltha.Device{} |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1046 | childDevice.Type = deviceType |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1047 | childDevice.ParentId = parentDeviceID |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1048 | childDevice.ParentPortNo = uint32(parentPortNo) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1049 | childDevice.VendorId = vendorID |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 1050 | childDevice.SerialNumber = serialNumber |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1051 | childDevice.Root = false |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 1052 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1053 | // Get parent device type |
| 1054 | pAgent := dMgr.getDeviceAgent(ctx, parentDeviceID) |
| 1055 | if pAgent == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1056 | return nil, status.Errorf(codes.NotFound, "%s", parentDeviceID) |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 1057 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1058 | if pAgent.deviceType == "" { |
| 1059 | return nil, status.Errorf(codes.FailedPrecondition, "device Type not set %s", parentDeviceID) |
| 1060 | } |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 1061 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1062 | if device, err := dMgr.GetChildDevice(ctx, parentDeviceID, serialNumber, onuID, parentPortNo); err == nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1063 | logger.Warnw(ctx, "child-device-exists", log.Fields{"parent-device-id": parentDeviceID, "serialNumber": serialNumber}) |
Mahir Gunyel | 6deaa24 | 2019-06-27 04:53:33 -0700 | [diff] [blame] | 1064 | return device, status.Errorf(codes.AlreadyExists, "%s", serialNumber) |
Matt Jeanneret | 4e24195 | 2019-02-28 11:16:04 -0500 | [diff] [blame] | 1065 | } |
| 1066 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1067 | 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] | 1068 | |
| 1069 | // Create and start a device agent for that device |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 1070 | agent := newAgent(dMgr.adapterProxy, childDevice, dMgr, dMgr.dbPath, dMgr.dProxy, dMgr.defaultTimeout) |
Mahir Gunyel | 23bd8c7 | 2020-09-21 23:20:43 -0700 | [diff] [blame] | 1071 | insertedChildDevice, err := agent.start(ctx, childDevice) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 1072 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1073 | 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] | 1074 | return nil, err |
| 1075 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1076 | dMgr.addDeviceAgentToMap(agent) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1077 | |
| 1078 | // Activate the child device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1079 | if agent = dMgr.getDeviceAgent(ctx, agent.deviceID); agent != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1080 | go func() { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1081 | err := agent.enableDevice(log.WithSpanFromContext(context.Background(), ctx)) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1082 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1083 | logger.Errorw(ctx, "unable-to-enable-device", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1084 | } |
| 1085 | }() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1086 | } |
| 1087 | |
Mahir Gunyel | 23bd8c7 | 2020-09-21 23:20:43 -0700 | [diff] [blame] | 1088 | return insertedChildDevice, nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1089 | } |
| 1090 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1091 | func (dMgr *Manager) packetOut(ctx context.Context, deviceID string, outPort uint32, packet *ofp.OfpPacketOut) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1092 | logger.Debugw(ctx, "packetOut", log.Fields{"device-id": deviceID, "outPort": outPort}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1093 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 1094 | return agent.packetOut(ctx, outPort, packet) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1095 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1096 | return status.Errorf(codes.NotFound, "%s", deviceID) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1097 | } |
| 1098 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1099 | // PacketIn receives packet from adapter |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1100 | func (dMgr *Manager) PacketIn(ctx context.Context, deviceID string, port uint32, transactionID string, packet []byte) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1101 | logger.Debugw(ctx, "PacketIn", log.Fields{"device-id": deviceID, "port": port}) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1102 | // Get the logical device Id based on the deviceId |
| 1103 | var device *voltha.Device |
| 1104 | var err error |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 1105 | if device, err = dMgr.getDeviceReadOnly(ctx, deviceID); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1106 | logger.Errorw(ctx, "device-not-found", log.Fields{"device-id": deviceID}) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1107 | return err |
| 1108 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1109 | if !device.Root { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1110 | logger.Errorw(ctx, "device-not-root", log.Fields{"device-id": deviceID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1111 | return status.Errorf(codes.FailedPrecondition, "%s", deviceID) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1112 | } |
| 1113 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1114 | if err := dMgr.logicalDeviceMgr.packetIn(ctx, device.ParentId, port, transactionID, packet); err != nil { |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1115 | return err |
| 1116 | } |
| 1117 | return nil |
| 1118 | } |
| 1119 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1120 | func (dMgr *Manager) setParentID(ctx context.Context, device *voltha.Device, parentID string) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1121 | logger.Debugw(ctx, "setParentId", log.Fields{"device-id": device.Id, "parentId": parentID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1122 | if agent := dMgr.getDeviceAgent(ctx, device.Id); agent != nil { |
| 1123 | return agent.setParentID(ctx, device, parentID) |
khenaidoo | ad06fd7 | 2019-10-28 12:26:05 -0400 | [diff] [blame] | 1124 | } |
| 1125 | return status.Errorf(codes.NotFound, "%s", device.Id) |
| 1126 | } |
| 1127 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1128 | // CreateLogicalDevice creates logical device in core |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1129 | func (dMgr *Manager) CreateLogicalDevice(ctx context.Context, cDevice *voltha.Device) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1130 | logger.Info(ctx, "CreateLogicalDevice") |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1131 | // Verify whether the logical device has already been created |
| 1132 | if cDevice.ParentId != "" { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1133 | 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] | 1134 | return nil |
| 1135 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1136 | var err error |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1137 | if _, err = dMgr.logicalDeviceMgr.createLogicalDevice(ctx, cDevice); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1138 | logger.Warnw(ctx, "createlogical-device-error", log.Fields{"device": cDevice}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1139 | return err |
| 1140 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1141 | return nil |
| 1142 | } |
| 1143 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1144 | // DeleteLogicalDevice deletes logical device from core |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1145 | func (dMgr *Manager) DeleteLogicalDevice(ctx context.Context, cDevice *voltha.Device) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1146 | logger.Info(ctx, "DeleteLogicalDevice") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1147 | var err error |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1148 | if err = dMgr.logicalDeviceMgr.deleteLogicalDevice(ctx, cDevice); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1149 | logger.Warnw(ctx, "deleteLogical-device-error", log.Fields{"device-id": cDevice.Id}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1150 | return err |
| 1151 | } |
| 1152 | // Remove the logical device Id from the parent device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1153 | logicalID := "" |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1154 | dMgr.UpdateDeviceAttribute(ctx, cDevice.Id, "ParentId", logicalID) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1155 | return nil |
| 1156 | } |
| 1157 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1158 | // DeleteLogicalPorts removes the logical ports associated with that deviceId |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1159 | func (dMgr *Manager) DeleteLogicalPorts(ctx context.Context, cDevice *voltha.Device) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1160 | logger.Debugw(ctx, "delete-all-logical-ports", log.Fields{"device-id": cDevice.Id}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1161 | if err := dMgr.logicalDeviceMgr.deleteLogicalPorts(ctx, cDevice.Id); err != nil { |
khenaidoo | e132f52 | 2020-03-20 15:23:15 -0400 | [diff] [blame] | 1162 | // Just log the error. The logical device or port may already have been deleted before this callback is invoked. |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1163 | logger.Warnw(ctx, "deleteLogical-ports-error", log.Fields{"device-id": cDevice.Id, "error": err}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1164 | } |
| 1165 | return nil |
| 1166 | } |
| 1167 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1168 | func (dMgr *Manager) getParentDevice(ctx context.Context, childDevice *voltha.Device) *voltha.Device { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1169 | // Sanity check |
| 1170 | if childDevice.Root { |
| 1171 | // childDevice is the parent device |
| 1172 | return childDevice |
| 1173 | } |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 1174 | parentDevice, _ := dMgr.getDeviceReadOnly(ctx, childDevice.ParentId) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1175 | return parentDevice |
| 1176 | } |
| 1177 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1178 | //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] | 1179 | //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] | 1180 | func (dMgr *Manager) ChildDevicesLost(ctx context.Context, parentDeviceID string) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1181 | logger.Debug(ctx, "ChildDevicesLost") |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 1182 | parentDevice, err := dMgr.getDeviceReadOnly(ctx, parentDeviceID) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1183 | if err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1184 | 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] | 1185 | return err |
| 1186 | } |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 1187 | return dMgr.DisableAllChildDevices(ctx, parentDevice) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1188 | } |
| 1189 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1190 | //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] | 1191 | // 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] | 1192 | func (dMgr *Manager) ChildDevicesDetected(ctx context.Context, parentDeviceID string) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1193 | logger.Debug(ctx, "ChildDevicesDetected") |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1194 | parentDevicePorts, err := dMgr.listDevicePorts(ctx, parentDeviceID) |
| 1195 | if err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1196 | logger.Warnw(ctx, "failed-getting-device", log.Fields{"device-id": parentDeviceID, "error": err}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1197 | return err |
| 1198 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1199 | childDeviceIds := dMgr.getAllChildDeviceIds(ctx, parentDevicePorts) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1200 | if len(childDeviceIds) == 0 { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1201 | logger.Debugw(ctx, "no-child-device", log.Fields{"parent-device-id": parentDeviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1202 | } |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1203 | allChildEnableRequestSent := true |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1204 | for childDeviceID := range childDeviceIds { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1205 | if agent := dMgr.getDeviceAgent(ctx, childDeviceID); agent != nil { |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1206 | // Run the children re-registration in its own routine |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1207 | go func(ctx context.Context) { |
| 1208 | err = agent.enableDevice(ctx) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1209 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1210 | logger.Errorw(ctx, "unable-to-enable-device", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1211 | } |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1212 | }(log.WithSpanFromContext(context.Background(), ctx)) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1213 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1214 | err = status.Errorf(codes.Unavailable, "no agent for child device %s", childDeviceID) |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1215 | logger.Errorw(ctx, "no-child-device-agent", log.Fields{"parent-device-id": parentDeviceID, "childId": childDeviceID}) |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1216 | allChildEnableRequestSent = false |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1217 | } |
| 1218 | } |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1219 | if !allChildEnableRequestSent { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1220 | return err |
| 1221 | } |
| 1222 | return nil |
| 1223 | } |
| 1224 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1225 | /* |
| 1226 | All the functions below are callback functions where they are invoked with the latest and previous data. We can |
| 1227 | therefore use the data as is without trying to get the latest from the model. |
| 1228 | */ |
| 1229 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1230 | //DisableAllChildDevices is invoked as a callback when the parent device is disabled |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1231 | func (dMgr *Manager) DisableAllChildDevices(ctx context.Context, parentCurrDevice *voltha.Device) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1232 | logger.Debug(ctx, "DisableAllChildDevices") |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1233 | ports, _ := dMgr.listDevicePorts(ctx, parentCurrDevice.Id) |
| 1234 | for childDeviceID := range dMgr.getAllChildDeviceIds(ctx, ports) { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1235 | if agent := dMgr.getDeviceAgent(ctx, childDeviceID); agent != nil { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1236 | if err := agent.disableDevice(ctx); err != nil { |
khenaidoo | e132f52 | 2020-03-20 15:23:15 -0400 | [diff] [blame] | 1237 | // 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] | 1238 | 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] | 1239 | } |
| 1240 | } |
| 1241 | } |
| 1242 | return nil |
| 1243 | } |
| 1244 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1245 | //DeleteAllChildDevices is invoked as a callback when the parent device is deleted |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1246 | func (dMgr *Manager) DeleteAllChildDevices(ctx context.Context, parentCurrDevice *voltha.Device) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1247 | logger.Debug(ctx, "DeleteAllChildDevices") |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1248 | force := false |
| 1249 | // Get the parent device Transient state, if its FORCE_DELETED(go for force delete for child devices) |
| 1250 | // So in cases when this handler is getting called other than DELETE operation, no force option would be used. |
| 1251 | agent := dMgr.getDeviceAgent(ctx, parentCurrDevice.Id) |
| 1252 | if agent == nil { |
| 1253 | return status.Errorf(codes.NotFound, "%s", parentCurrDevice.Id) |
| 1254 | } |
| 1255 | |
| 1256 | force = agent.getTransientState() == voltha.DeviceTransientState_FORCE_DELETING |
| 1257 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1258 | ports, _ := dMgr.listDevicePorts(ctx, parentCurrDevice.Id) |
| 1259 | for childDeviceID := range dMgr.getAllChildDeviceIds(ctx, ports) { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1260 | if agent := dMgr.getDeviceAgent(ctx, childDeviceID); agent != nil { |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1261 | if force { |
| 1262 | if err := agent.deleteDeviceForce(ctx); err != nil { |
| 1263 | logger.Warnw(ctx, "failure-delete-device-force", log.Fields{"device-id": childDeviceID, |
| 1264 | "error": err.Error()}) |
| 1265 | } |
| 1266 | } else { |
| 1267 | if err := agent.deleteDevice(ctx); err != nil { |
| 1268 | logger.Warnw(ctx, "failure-delete-device", log.Fields{"device-id": childDeviceID, |
| 1269 | "error": err.Error()}) |
| 1270 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1271 | } |
khenaidoo | 4908535 | 2020-01-13 19:15:43 -0500 | [diff] [blame] | 1272 | // No further action is required here. The deleteDevice will change the device state where the resulting |
| 1273 | // callback will take care of cleaning the child device agent. |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1274 | } |
| 1275 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1276 | return nil |
| 1277 | } |
| 1278 | |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 1279 | //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] | 1280 | func (dMgr *Manager) DeleteAllLogicalPorts(ctx context.Context, parentDevice *voltha.Device) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1281 | 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] | 1282 | if err := dMgr.logicalDeviceMgr.deleteAllLogicalPorts(ctx, parentDevice); err != nil { |
khenaidoo | e132f52 | 2020-03-20 15:23:15 -0400 | [diff] [blame] | 1283 | // Just log error as logical device may already have been deleted |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1284 | 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] | 1285 | } |
| 1286 | return nil |
| 1287 | } |
| 1288 | |
| 1289 | //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] | 1290 | func (dMgr *Manager) DeleteAllDeviceFlows(ctx context.Context, parentDevice *voltha.Device) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1291 | logger.Debugw(ctx, "delete-all-device-flows", log.Fields{"parent-device-id": parentDevice.Id}) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 1292 | if agent := dMgr.getDeviceAgent(ctx, parentDevice.Id); agent != nil { |
| 1293 | if err := agent.deleteAllFlows(ctx); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1294 | 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] | 1295 | return err |
| 1296 | } |
| 1297 | return nil |
| 1298 | } |
| 1299 | return status.Errorf(codes.NotFound, "%s", parentDevice.Id) |
| 1300 | } |
| 1301 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1302 | //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] | 1303 | func (dMgr *Manager) getAllChildDeviceIds(ctx context.Context, parentDevicePorts map[uint32]*voltha.Port) map[string]struct{} { |
| 1304 | logger.Debug(ctx, "getAllChildDeviceIds") |
| 1305 | childDeviceIds := make(map[string]struct{}, len(parentDevicePorts)) |
| 1306 | for _, port := range parentDevicePorts { |
| 1307 | for _, peer := range port.Peers { |
| 1308 | childDeviceIds[peer.DeviceId] = struct{}{} |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1309 | } |
| 1310 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1311 | logger.Debugw(ctx, "returning-getAllChildDeviceIds", log.Fields{"childDeviceIds": childDeviceIds}) |
| 1312 | return childDeviceIds |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1313 | } |
| 1314 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1315 | //GetAllChildDevices is a helper method to get all the child device IDs from the device passed as parameter |
| 1316 | func (dMgr *Manager) GetAllChildDevices(ctx context.Context, parentDeviceID string) (*voltha.Devices, error) { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1317 | logger.Debugw(ctx, "GetAllChildDevices", log.Fields{"parent-device-id": parentDeviceID}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1318 | if parentDevicePorts, err := dMgr.listDevicePorts(ctx, parentDeviceID); err == nil { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 1319 | childDevices := make([]*voltha.Device, 0) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1320 | for deviceID := range dMgr.getAllChildDeviceIds(ctx, parentDevicePorts) { |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 1321 | if d, e := dMgr.getDeviceReadOnly(ctx, deviceID); e == nil && d != nil { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1322 | childDevices = append(childDevices, d) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 1323 | } |
| 1324 | } |
| 1325 | return &voltha.Devices{Items: childDevices}, nil |
| 1326 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1327 | return nil, status.Errorf(codes.NotFound, "%s", parentDeviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 1328 | } |
| 1329 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1330 | // 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] | 1331 | func (dMgr *Manager) SetupUNILogicalPorts(ctx context.Context, cDevice *voltha.Device) error { |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 1332 | logger.Info(ctx, "SetupUNILogicalPorts") |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1333 | cDevicePorts, err := dMgr.listDevicePorts(ctx, cDevice.Id) |
| 1334 | if err != nil { |
| 1335 | return err |
| 1336 | } |
| 1337 | if err := dMgr.logicalDeviceMgr.setupUNILogicalPorts(ctx, cDevice, cDevicePorts); err != nil { |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 1338 | logger.Warnw(ctx, "setupUNILogicalPorts-error", log.Fields{"device": cDevice, "err": err}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1339 | return err |
| 1340 | } |
| 1341 | return nil |
| 1342 | } |
| 1343 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1344 | // convenience to avoid redefining |
| 1345 | var operationFailureResp = &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE} |
| 1346 | |
| 1347 | // DownloadImage execute an image download request |
| 1348 | func (dMgr *Manager) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1349 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
| 1350 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1351 | logger.Debugw(ctx, "DownloadImage", log.Fields{"device-id": img.Id, "imageName": img.Name}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1352 | agent := dMgr.getDeviceAgent(ctx, img.Id) |
| 1353 | if agent == nil { |
| 1354 | return operationFailureResp, status.Errorf(codes.NotFound, "%s", img.Id) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1355 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1356 | resp, err := agent.downloadImage(ctx, img) |
| 1357 | if err != nil { |
| 1358 | return operationFailureResp, err |
| 1359 | } |
| 1360 | return resp, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1361 | } |
| 1362 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1363 | // CancelImageDownload cancels image download request |
| 1364 | func (dMgr *Manager) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1365 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
| 1366 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1367 | logger.Debugw(ctx, "CancelImageDownload", log.Fields{"device-id": img.Id, "imageName": img.Name}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1368 | agent := dMgr.getDeviceAgent(ctx, img.Id) |
| 1369 | if agent == nil { |
| 1370 | return operationFailureResp, status.Errorf(codes.NotFound, "%s", img.Id) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1371 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1372 | resp, err := agent.cancelImageDownload(ctx, img) |
| 1373 | if err != nil { |
| 1374 | return operationFailureResp, err |
| 1375 | } |
| 1376 | return resp, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1377 | } |
| 1378 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1379 | // ActivateImageUpdate activates image update request |
| 1380 | func (dMgr *Manager) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1381 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
| 1382 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1383 | logger.Debugw(ctx, "ActivateImageUpdate", log.Fields{"device-id": img.Id, "imageName": img.Name}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1384 | agent := dMgr.getDeviceAgent(ctx, img.Id) |
| 1385 | if agent == nil { |
| 1386 | return operationFailureResp, status.Errorf(codes.NotFound, "%s", img.Id) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1387 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1388 | resp, err := agent.activateImage(ctx, img) |
| 1389 | if err != nil { |
| 1390 | return operationFailureResp, err |
| 1391 | } |
| 1392 | return resp, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1393 | } |
| 1394 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1395 | // RevertImageUpdate reverts image update |
| 1396 | func (dMgr *Manager) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1397 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
| 1398 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1399 | logger.Debugw(ctx, "RevertImageUpdate", log.Fields{"device-id": img.Id, "imageName": img.Name}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1400 | agent := dMgr.getDeviceAgent(ctx, img.Id) |
| 1401 | if agent == nil { |
| 1402 | return operationFailureResp, status.Errorf(codes.NotFound, "%s", img.Id) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1403 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1404 | resp, err := agent.revertImage(ctx, img) |
| 1405 | if err != nil { |
| 1406 | return operationFailureResp, err |
| 1407 | } |
| 1408 | return resp, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1409 | } |
| 1410 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1411 | // convenience to avoid redefining |
| 1412 | var imageDownloadFailureResp = &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN} |
| 1413 | |
| 1414 | // GetImageDownloadStatus returns status of image download |
| 1415 | func (dMgr *Manager) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1416 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
| 1417 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1418 | logger.Debugw(ctx, "GetImageDownloadStatus", log.Fields{"device-id": img.Id, "imageName": img.Name}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1419 | agent := dMgr.getDeviceAgent(ctx, img.Id) |
| 1420 | if agent == nil { |
| 1421 | return imageDownloadFailureResp, status.Errorf(codes.NotFound, "%s", img.Id) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1422 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1423 | resp, err := agent.getImageDownloadStatus(ctx, img) |
| 1424 | if err != nil { |
| 1425 | return imageDownloadFailureResp, err |
| 1426 | } |
| 1427 | return resp, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1428 | } |
| 1429 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1430 | func (dMgr *Manager) UpdateImageDownload(ctx context.Context, deviceID string, img *voltha.ImageDownload) error { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1431 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
| 1432 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1433 | logger.Debugw(ctx, "UpdateImageDownload", log.Fields{"device-id": img.Id, "imageName": img.Name}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1434 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 1435 | if err := agent.updateImageDownload(ctx, img); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1436 | logger.Debugw(ctx, "UpdateImageDownload-failed", log.Fields{"err": err, "imageName": img.Name}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1437 | return err |
| 1438 | } |
| 1439 | } else { |
| 1440 | return status.Errorf(codes.NotFound, "%s", img.Id) |
| 1441 | } |
| 1442 | return nil |
| 1443 | } |
| 1444 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1445 | // GetImageDownload returns image download |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1446 | func (dMgr *Manager) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1447 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
| 1448 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1449 | logger.Debugw(ctx, "GetImageDownload", log.Fields{"device-id": img.Id, "imageName": img.Name}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1450 | agent := dMgr.getDeviceAgent(ctx, img.Id) |
| 1451 | if agent == nil { |
| 1452 | return imageDownloadFailureResp, status.Errorf(codes.NotFound, "%s", img.Id) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1453 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1454 | resp, err := agent.getImageDownload(ctx, img) |
| 1455 | if err != nil { |
| 1456 | return imageDownloadFailureResp, err |
| 1457 | } |
| 1458 | return resp, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1459 | } |
| 1460 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1461 | // ListImageDownloads returns image downloads |
| 1462 | func (dMgr *Manager) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1463 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 1464 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1465 | logger.Debugw(ctx, "ListImageDownloads", log.Fields{"device-id": id.Id}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1466 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 1467 | if agent == nil { |
| 1468 | 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] | 1469 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1470 | resp, err := agent.listImageDownloads(ctx, id.Id) |
| 1471 | if err != nil { |
| 1472 | return &voltha.ImageDownloads{Items: []*voltha.ImageDownload{imageDownloadFailureResp}}, err |
| 1473 | } |
| 1474 | return resp, nil |
| 1475 | } |
| 1476 | |
| 1477 | // GetImages returns all images for a specific device entry |
| 1478 | func (dMgr *Manager) GetImages(ctx context.Context, id *voltha.ID) (*voltha.Images, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1479 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 1480 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1481 | logger.Debugw(ctx, "GetImages", log.Fields{"device-id": id.Id}) |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 1482 | device, err := dMgr.getDeviceReadOnly(ctx, id.Id) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1483 | if err != nil { |
| 1484 | return nil, err |
| 1485 | } |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 1486 | return device.Images, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1487 | } |
| 1488 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1489 | func (dMgr *Manager) NotifyInvalidTransition(ctx context.Context, device *voltha.Device) error { |
| 1490 | logger.Errorw(ctx, "NotifyInvalidTransition", log.Fields{ |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 1491 | "device": device.Id, |
| 1492 | "curr-admin-state": device.AdminState, |
| 1493 | "curr-oper-state": device.OperStatus, |
| 1494 | "curr-conn-state": device.ConnectStatus, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 1495 | }) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1496 | //TODO: notify over kafka? |
| 1497 | return nil |
| 1498 | } |
| 1499 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1500 | // UpdateDeviceAttribute updates value of particular device attribute |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1501 | func (dMgr *Manager) UpdateDeviceAttribute(ctx context.Context, deviceID string, attribute string, value interface{}) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1502 | if agent, ok := dMgr.deviceAgents.Load(deviceID); ok { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1503 | agent.(*Agent).updateDeviceAttribute(ctx, attribute, value) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1504 | } |
| 1505 | } |
| 1506 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1507 | // 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] | 1508 | func (dMgr *Manager) GetParentDeviceID(ctx context.Context, deviceID string) string { |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 1509 | if device, _ := dMgr.getDeviceReadOnly(ctx, deviceID); device != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1510 | logger.Infow(ctx, "GetParentDeviceId", log.Fields{"device-id": device.Id, "parentId": device.ParentId}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 1511 | return device.ParentId |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1512 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 1513 | return "" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1514 | } |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1515 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1516 | func (dMgr *Manager) SimulateAlarm(ctx context.Context, simulateReq *voltha.SimulateAlarmRequest) (*common.OperationResp, error) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1517 | logger.Debugw(ctx, "SimulateAlarm", log.Fields{"id": simulateReq.Id, "Indicator": simulateReq.Indicator, "IntfId": simulateReq.IntfId, |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1518 | "PortTypeName": simulateReq.PortTypeName, "OnuDeviceId": simulateReq.OnuDeviceId, "InverseBitErrorRate": simulateReq.InverseBitErrorRate, |
| 1519 | "Drift": simulateReq.Drift, "NewEqd": simulateReq.NewEqd, "OnuSerialNumber": simulateReq.OnuSerialNumber, "Operation": simulateReq.Operation}) |
| 1520 | agent := dMgr.getDeviceAgent(ctx, simulateReq.Id) |
| 1521 | if agent == nil { |
| 1522 | return nil, status.Errorf(codes.NotFound, "%s", simulateReq.Id) |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1523 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1524 | if err := agent.simulateAlarm(ctx, simulateReq); err != nil { |
| 1525 | return nil, err |
| 1526 | } |
| 1527 | return &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}, nil |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1528 | } |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1529 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1530 | func (dMgr *Manager) UpdateDeviceReason(ctx context.Context, deviceID string, reason string) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 1531 | logger.Debugw(ctx, "UpdateDeviceReason", log.Fields{"device-id": deviceID, "reason": reason}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1532 | if agent := dMgr.getDeviceAgent(ctx, deviceID); agent != nil { |
| 1533 | return agent.updateDeviceReason(ctx, reason) |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1534 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1535 | return status.Errorf(codes.NotFound, "%s", deviceID) |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1536 | } |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1537 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1538 | func (dMgr *Manager) EnablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1539 | log.EnrichSpan(ctx, log.Fields{"device-id": port.DeviceId}) |
| 1540 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1541 | logger.Debugw(ctx, "EnablePort", log.Fields{"device-id": port.DeviceId, "port-no": port.PortNo}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1542 | agent := dMgr.getDeviceAgent(ctx, port.DeviceId) |
| 1543 | if agent == nil { |
| 1544 | return nil, status.Errorf(codes.NotFound, "%s", port.DeviceId) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1545 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1546 | return &empty.Empty{}, agent.enablePort(ctx, port.PortNo) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1547 | } |
| 1548 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1549 | func (dMgr *Manager) DisablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1550 | log.EnrichSpan(ctx, log.Fields{"device-id": port.DeviceId}) |
| 1551 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1552 | logger.Debugw(ctx, "DisablePort", log.Fields{"device-id": port.DeviceId, "port-no": port.PortNo}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1553 | agent := dMgr.getDeviceAgent(ctx, port.DeviceId) |
| 1554 | if agent == nil { |
| 1555 | return nil, status.Errorf(codes.NotFound, "%s", port.DeviceId) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1556 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1557 | return &empty.Empty{}, agent.disablePort(ctx, port.PortNo) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1558 | } |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1559 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1560 | // ChildDeviceLost calls parent adapter to delete child device and all its references |
| 1561 | func (dMgr *Manager) ChildDeviceLost(ctx context.Context, curr *voltha.Device) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1562 | logger.Debugw(ctx, "childDeviceLost", log.Fields{"child-device-id": curr.Id, "parent-device-id": curr.ParentId}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1563 | if parentAgent := dMgr.getDeviceAgent(ctx, curr.ParentId); parentAgent != nil { |
khenaidoo | e132f52 | 2020-03-20 15:23:15 -0400 | [diff] [blame] | 1564 | if err := parentAgent.ChildDeviceLost(ctx, curr); err != nil { |
| 1565 | // Just log the message and let the remaining pipeline proceed. |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1566 | 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] | 1567 | } |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1568 | } |
khenaidoo | e132f52 | 2020-03-20 15:23:15 -0400 | [diff] [blame] | 1569 | // Do not return an error as parent device may also have been deleted. Let the remaining pipeline proceed. |
| 1570 | return nil |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1571 | } |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 1572 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1573 | func (dMgr *Manager) StartOmciTestAction(ctx context.Context, request *voltha.OmciTestRequest) (*voltha.TestResponse, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1574 | log.EnrichSpan(ctx, log.Fields{"device-id": request.Id}) |
| 1575 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1576 | logger.Debugw(ctx, "StartOmciTestAction", log.Fields{"device-id": request.Id, "uuid": request.Uuid}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1577 | agent := dMgr.getDeviceAgent(ctx, request.Id) |
| 1578 | if agent == nil { |
| 1579 | return nil, status.Errorf(codes.NotFound, "%s", request.Id) |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 1580 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1581 | return agent.startOmciTest(ctx, request) |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 1582 | } |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 1583 | |
| 1584 | func (dMgr *Manager) GetExtValue(ctx context.Context, value *voltha.ValueSpecifier) (*voltha.ReturnValues, error) { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 1585 | log.EnrichSpan(ctx, log.Fields{"device-id": value.Id}) |
| 1586 | |
Girish Kumar | 3e8ee21 | 2020-08-19 17:50:11 +0000 | [diff] [blame] | 1587 | logger.Debugw(ctx, "getExtValue", log.Fields{"onu-id": value.Id}) |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 1588 | cDevice, err := dMgr.getDeviceReadOnly(ctx, value.Id) |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 1589 | if err != nil { |
| 1590 | return nil, status.Errorf(codes.Aborted, "%s", err.Error()) |
| 1591 | } |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 1592 | pDevice, err := dMgr.getDeviceReadOnly(ctx, cDevice.ParentId) |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 1593 | if err != nil { |
| 1594 | return nil, status.Errorf(codes.Aborted, "%s", err.Error()) |
| 1595 | } |
| 1596 | if agent := dMgr.getDeviceAgent(ctx, cDevice.ParentId); agent != nil { |
| 1597 | resp, err := agent.getExtValue(ctx, pDevice, cDevice, value) |
| 1598 | if err != nil { |
| 1599 | return nil, err |
| 1600 | } |
Girish Kumar | 3e8ee21 | 2020-08-19 17:50:11 +0000 | [diff] [blame] | 1601 | logger.Debugw(ctx, "getExtValue-result", log.Fields{"result": resp}) |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 1602 | return resp, nil |
| 1603 | } |
| 1604 | return nil, status.Errorf(codes.NotFound, "%s", value.Id) |
| 1605 | |
| 1606 | } |
dpaul | 6268631 | 2020-06-23 14:17:36 +0530 | [diff] [blame] | 1607 | |
| 1608 | // SetExtValue set some given configs or value |
| 1609 | func (dMgr *Manager) SetExtValue(ctx context.Context, value *voltha.ValueSet) (*empty.Empty, error) { |
| 1610 | logger.Debugw(ctx, "setExtValue", log.Fields{"onu-id": value.Id}) |
| 1611 | device, err := dMgr.getDeviceReadOnly(ctx, value.Id) |
| 1612 | if err != nil { |
| 1613 | return nil, status.Errorf(codes.Aborted, "%s", err.Error()) |
| 1614 | } |
| 1615 | if agent := dMgr.getDeviceAgent(ctx, device.Id); agent != nil { |
| 1616 | resp, err := agent.setExtValue(ctx, device, value) |
| 1617 | if err != nil { |
| 1618 | return nil, err |
| 1619 | } |
| 1620 | logger.Debugw(ctx, "setExtValue-result", log.Fields{"result": resp}) |
| 1621 | return resp, nil |
| 1622 | } |
| 1623 | return nil, status.Errorf(codes.NotFound, "%s", value.Id) |
| 1624 | |
| 1625 | } |