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