Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [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 | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 16 | |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 21 | "sync" |
| 22 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/db/model" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 24 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
| 25 | "github.com/opencord/voltha-lib-go/v2/pkg/probe" |
Scott Baker | 555307d | 2019-11-04 08:58:01 -0800 | [diff] [blame] | 26 | "github.com/opencord/voltha-protos/v2/go/voltha" |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 27 | "google.golang.org/grpc/codes" |
| 28 | "google.golang.org/grpc/status" |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 29 | ) |
| 30 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 31 | // DeviceManager represents device manager related information |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 32 | type DeviceManager struct { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 33 | deviceAgents sync.Map |
| 34 | logicalDeviceMgr *LogicalDeviceManager |
| 35 | clusterDataProxy *model.Proxy |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 36 | coreInstanceID string |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 37 | exitChannel chan int |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 38 | } |
| 39 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 40 | func newDeviceManager(cdProxy *model.Proxy, coreInstanceID string) *DeviceManager { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 41 | var deviceMgr DeviceManager |
| 42 | deviceMgr.exitChannel = make(chan int, 1) |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 43 | deviceMgr.coreInstanceID = coreInstanceID |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 44 | deviceMgr.clusterDataProxy = cdProxy |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 45 | return &deviceMgr |
| 46 | } |
| 47 | |
| 48 | func (dMgr *DeviceManager) start(ctx context.Context, logicalDeviceMgr *LogicalDeviceManager) { |
| 49 | log.Info("starting-device-manager") |
| 50 | dMgr.logicalDeviceMgr = logicalDeviceMgr |
Hardik Windlass | dc63dde | 2019-09-30 07:15:13 +0000 | [diff] [blame] | 51 | probe.UpdateStatusFromContext(ctx, "device-manager", probe.ServiceStatusRunning) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 52 | log.Info("device-manager-started") |
| 53 | } |
| 54 | |
| 55 | func (dMgr *DeviceManager) stop(ctx context.Context) { |
| 56 | log.Info("stopping-device-manager") |
| 57 | dMgr.exitChannel <- 1 |
Hardik Windlass | dc63dde | 2019-09-30 07:15:13 +0000 | [diff] [blame] | 58 | probe.UpdateStatusFromContext(ctx, "device-manager", probe.ServiceStatusStopped) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 59 | log.Info("device-manager-stopped") |
| 60 | } |
| 61 | |
| 62 | func sendResponse(ctx context.Context, ch chan interface{}, result interface{}) { |
| 63 | if ctx.Err() == nil { |
| 64 | // Returned response only of the ctx has not been cancelled/timeout/etc |
| 65 | // Channel is automatically closed when a context is Done |
| 66 | ch <- result |
| 67 | log.Debugw("sendResponse", log.Fields{"result": result}) |
| 68 | } else { |
| 69 | // Should the transaction be reverted back? |
| 70 | log.Debugw("sendResponse-context-error", log.Fields{"context-error": ctx.Err()}) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func (dMgr *DeviceManager) addDeviceAgentToMap(agent *DeviceAgent) { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 75 | if _, exist := dMgr.deviceAgents.Load(agent.deviceID); !exist { |
| 76 | dMgr.deviceAgents.Store(agent.deviceID, agent) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | func (dMgr *DeviceManager) deleteDeviceAgentToMap(agent *DeviceAgent) { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 81 | dMgr.deviceAgents.Delete(agent.deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 82 | } |
| 83 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 84 | func (dMgr *DeviceManager) getDeviceAgent(deviceID string) *DeviceAgent { |
| 85 | if agent, ok := dMgr.deviceAgents.Load(deviceID); ok { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 86 | return agent.(*DeviceAgent) |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 87 | } |
| 88 | // Try to load into memory - loading will also create the device agent |
| 89 | if err := dMgr.load(deviceID); err == nil { |
| 90 | if agent, ok := dMgr.deviceAgents.Load(deviceID); ok { |
| 91 | return agent.(*DeviceAgent) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 92 | } |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 93 | } |
| 94 | return nil |
| 95 | } |
| 96 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 97 | // listDeviceIDsFromMap returns the list of device IDs that are in memory |
| 98 | func (dMgr *DeviceManager) listDeviceIDsFromMap() *voltha.IDs { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 99 | result := &voltha.IDs{Items: make([]*voltha.ID, 0)} |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 100 | dMgr.deviceAgents.Range(func(key, value interface{}) bool { |
| 101 | result.Items = append(result.Items, &voltha.ID{Id: key.(string)}) |
| 102 | return true |
| 103 | }) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 104 | return result |
| 105 | } |
| 106 | |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 107 | // GetDevice will returns a device, either from memory or from the dB, if present |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 108 | func (dMgr *DeviceManager) GetDevice(id string) (*voltha.Device, error) { |
| 109 | log.Debugw("GetDevice", log.Fields{"deviceid": id}) |
| 110 | if agent := dMgr.getDeviceAgent(id); agent != nil { |
| 111 | return agent.getDevice() |
| 112 | } |
| 113 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 114 | } |
| 115 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 116 | // IsDeviceInCache returns true if device exists in cache |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 117 | func (dMgr *DeviceManager) IsDeviceInCache(id string) bool { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 118 | _, exist := dMgr.deviceAgents.Load(id) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 119 | return exist |
| 120 | } |
| 121 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 122 | // IsRootDevice returns true if root device is present in either memory or db |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 123 | func (dMgr *DeviceManager) IsRootDevice(id string) (bool, error) { |
| 124 | device, err := dMgr.GetDevice(id) |
| 125 | if err != nil { |
| 126 | return false, err |
| 127 | } |
| 128 | return device.Root, nil |
| 129 | } |
| 130 | |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 131 | // ListDevices retrieves the latest devices from the data model |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 132 | func (dMgr *DeviceManager) ListDevices() (*voltha.Devices, error) { |
| 133 | log.Debug("ListDevices") |
| 134 | result := &voltha.Devices{} |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 135 | if devices, err := dMgr.clusterDataProxy.List(context.Background(), "/devices", 0, false, ""); err != nil { |
| 136 | log.Errorw("failed-to-list-devices", log.Fields{"error": err}) |
| 137 | return nil, err |
| 138 | } else if devices != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 139 | for _, device := range devices.([]interface{}) { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 140 | // If device is not in memory then set it up |
| 141 | if !dMgr.IsDeviceInCache(device.(*voltha.Device).Id) { |
| 142 | agent := newDeviceAgent(device.(*voltha.Device), dMgr, dMgr.clusterDataProxy) |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 143 | if err := agent.start(context.TODO(), true); err != nil { |
| 144 | log.Warnw("failure-starting-agent", log.Fields{"deviceID": device.(*voltha.Device).Id}) |
| 145 | agent.stop(context.TODO()) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 146 | } else { |
| 147 | dMgr.addDeviceAgentToMap(agent) |
| 148 | } |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 149 | } |
| 150 | result.Items = append(result.Items, device.(*voltha.Device)) |
| 151 | } |
| 152 | } |
| 153 | return result, nil |
| 154 | } |
| 155 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 156 | // loadDevice loads the deviceID in memory, if not present |
| 157 | func (dMgr *DeviceManager) loadDevice(deviceID string) (*DeviceAgent, error) { |
| 158 | log.Debugw("loading-device", log.Fields{"deviceID": deviceID}) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 159 | // Sanity check |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 160 | if deviceID == "" { |
| 161 | return nil, status.Error(codes.InvalidArgument, "deviceID empty") |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 162 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 163 | if !dMgr.IsDeviceInCache(deviceID) { |
| 164 | agent := newDeviceAgent(&voltha.Device{Id: deviceID}, dMgr, dMgr.clusterDataProxy) |
| 165 | if err := agent.start(context.TODO(), true); err != nil { |
| 166 | agent.stop(context.TODO()) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 167 | return nil, err |
| 168 | } |
| 169 | dMgr.addDeviceAgentToMap(agent) |
| 170 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 171 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 172 | return agent, nil |
| 173 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 174 | return nil, status.Error(codes.NotFound, deviceID) // This should nto happen |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // loadRootDeviceParentAndChildren loads the children and parents of a root device in memory |
| 178 | func (dMgr *DeviceManager) loadRootDeviceParentAndChildren(device *voltha.Device) error { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 179 | log.Debugw("loading-parent-and-children", log.Fields{"deviceID": device.Id}) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 180 | if device.Root { |
| 181 | // Scenario A |
| 182 | if device.ParentId != "" { |
| 183 | // Load logical device if needed. |
| 184 | if err := dMgr.logicalDeviceMgr.load(device.ParentId); err != nil { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 185 | log.Warnw("failure-loading-logical-device", log.Fields{"lDeviceID": device.ParentId}) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 186 | } |
| 187 | } else { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 188 | log.Debugw("no-parent-to-load", log.Fields{"deviceID": device.Id}) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 189 | } |
| 190 | // Load all child devices, if needed |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 191 | if childDeviceIDs, err := dMgr.getAllChildDeviceIDs(device); err == nil { |
| 192 | for _, childDeviceID := range childDeviceIDs { |
| 193 | if _, err := dMgr.loadDevice(childDeviceID); err != nil { |
| 194 | log.Warnw("failure-loading-device", log.Fields{"deviceID": childDeviceID}) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 195 | return err |
| 196 | } |
| 197 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 198 | log.Debugw("loaded-children", log.Fields{"deviceID": device.Id, "numChildren": len(childDeviceIDs)}) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 199 | } else { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 200 | log.Debugw("no-child-to-load", log.Fields{"deviceID": device.Id}) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | return nil |
| 204 | } |
| 205 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 206 | // load loads the deviceID in memory, if not present, and also loads its accompanying parents and children. Loading |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 207 | // in memory is for improved performance. It is not imperative that a device needs to be in memory when a request |
| 208 | // acting on the device is received by the core. In such a scenario, the Core will load the device in memory first |
| 209 | // and the proceed with the request. |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 210 | func (dMgr *DeviceManager) load(deviceID string) error { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 211 | log.Debug("load...") |
| 212 | // First load the device - this may fail in case the device was deleted intentionally by the other core |
| 213 | var dAgent *DeviceAgent |
| 214 | var err error |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 215 | if dAgent, err = dMgr.loadDevice(deviceID); err != nil { |
| 216 | log.Warnw("failure-loading-device", log.Fields{"deviceID": deviceID}) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 217 | return err |
| 218 | } |
| 219 | // Get the loaded device details |
| 220 | var device *voltha.Device |
| 221 | if device, err = dAgent.getDevice(); err != nil { |
| 222 | return err |
| 223 | } |
| 224 | |
| 225 | // If the device is in Pre-provisioning or deleted state stop here |
| 226 | if device.AdminState == voltha.AdminState_PREPROVISIONED || device.AdminState == voltha.AdminState_DELETED { |
| 227 | return nil |
| 228 | } |
| 229 | |
| 230 | // Now we face two scenarios |
| 231 | if device.Root { |
| 232 | // Load all children as well as the parent of this device (logical_device) |
| 233 | if err := dMgr.loadRootDeviceParentAndChildren(device); err != nil { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 234 | log.Warnw("failure-loading-device-parent-and-children", log.Fields{"deviceID": deviceID}) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 235 | return err |
| 236 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 237 | log.Debugw("successfully-loaded-parent-and-children", log.Fields{"deviceID": deviceID}) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 238 | } else { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 239 | // Scenario B - use the parentID of that device (root device) to trigger the loading |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 240 | if device.ParentId != "" { |
| 241 | return dMgr.load(device.ParentId) |
| 242 | } |
| 243 | } |
| 244 | return nil |
| 245 | } |
| 246 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 247 | // ListDeviceIDs retrieves the latest device IDs information from the data model (memory data only) |
| 248 | func (dMgr *DeviceManager) ListDeviceIDs() (*voltha.IDs, error) { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 249 | log.Debug("ListDeviceIDs") |
| 250 | // Report only device IDs that are in the device agent map |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 251 | return dMgr.listDeviceIDsFromMap(), nil |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | //ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs |
| 255 | func (dMgr *DeviceManager) ReconcileDevices(ctx context.Context, ids *voltha.IDs, ch chan interface{}) { |
| 256 | log.Debug("ReconcileDevices") |
| 257 | var res interface{} |
| 258 | if ids != nil { |
| 259 | toReconcile := len(ids.Items) |
| 260 | reconciled := 0 |
| 261 | for _, id := range ids.Items { |
| 262 | // Act on the device only if its not present in the agent map |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 263 | if !dMgr.IsDeviceInCache(id.Id) { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 264 | // Device ID not in memory |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 265 | log.Debugw("reconciling-device", log.Fields{"id": id.Id}) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 266 | // Load device from dB |
| 267 | agent := newDeviceAgent(&voltha.Device{Id: id.Id}, dMgr, dMgr.clusterDataProxy) |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 268 | if err := agent.start(context.TODO(), true); err != nil { |
| 269 | log.Warnw("failure-loading-device", log.Fields{"deviceID": id.Id}) |
| 270 | agent.stop(context.TODO()) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 271 | } else { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 272 | dMgr.addDeviceAgentToMap(agent) |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 273 | reconciled++ |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 274 | } |
| 275 | } else { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 276 | reconciled++ |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | if toReconcile != reconciled { |
| 280 | res = status.Errorf(codes.DataLoss, "less-device-reconciled:%d/%d", reconciled, toReconcile) |
| 281 | } |
| 282 | } else { |
| 283 | res = status.Errorf(codes.InvalidArgument, "empty-list-of-ids") |
| 284 | } |
| 285 | sendResponse(ctx, ch, res) |
| 286 | } |
| 287 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 288 | // ListDevicePorts returns ports details for a specific device |
| 289 | func (dMgr *DeviceManager) ListDevicePorts(ctx context.Context, deviceID string) (*voltha.Ports, error) { |
| 290 | log.Debugw("ListDevicePorts", log.Fields{"deviceid": deviceID}) |
| 291 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 292 | return agent.ListDevicePorts(ctx) |
| 293 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 294 | return nil, status.Errorf(codes.NotFound, "%s", deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 295 | |
| 296 | } |
| 297 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 298 | // ListDevicePmConfigs returns PM config details for a specific device |
| 299 | func (dMgr *DeviceManager) ListDevicePmConfigs(ctx context.Context, deviceID string) (*voltha.PmConfigs, error) { |
| 300 | log.Debugw("ListDevicePmConfigs", log.Fields{"deviceid": deviceID}) |
| 301 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 302 | return agent.ListDevicePmConfigs(ctx) |
| 303 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 304 | return nil, status.Errorf(codes.NotFound, "%s", deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 305 | |
| 306 | } |
| 307 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 308 | // ListDeviceFlows returns flow details for a specific device |
| 309 | func (dMgr *DeviceManager) ListDeviceFlows(ctx context.Context, deviceID string) (*voltha.Flows, error) { |
| 310 | log.Debugw("ListDeviceFlows", log.Fields{"deviceid": deviceID}) |
| 311 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 312 | return agent.ListDeviceFlows(ctx) |
| 313 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 314 | return nil, status.Errorf(codes.NotFound, "%s", deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 315 | } |
| 316 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 317 | // ListDeviceFlowGroups returns flow group details for a specific device |
| 318 | func (dMgr *DeviceManager) ListDeviceFlowGroups(ctx context.Context, deviceID string) (*voltha.FlowGroups, error) { |
| 319 | log.Debugw("ListDeviceFlowGroups", log.Fields{"deviceid": deviceID}) |
| 320 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 321 | return agent.ListDeviceFlowGroups(ctx) |
| 322 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 323 | return nil, status.Errorf(codes.NotFound, "%s", deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 324 | |
| 325 | } |
| 326 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 327 | // GetImageDownloadStatus returns the download status of an image of a particular device |
| 328 | func (dMgr *DeviceManager) GetImageDownloadStatus(ctx context.Context, deviceID string, imageName string) (*voltha.ImageDownload, error) { |
| 329 | log.Debugw("GetImageDownloadStatus", log.Fields{"deviceid": deviceID, "imagename": imageName}) |
| 330 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 331 | return agent.GetImageDownloadStatus(ctx, imageName) |
| 332 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 333 | return nil, status.Errorf(codes.NotFound, "%s", deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 334 | |
| 335 | } |
| 336 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 337 | // GetImageDownload return the download details for a specific image entry |
| 338 | func (dMgr *DeviceManager) GetImageDownload(ctx context.Context, deviceID string, imageName string) (*voltha.ImageDownload, error) { |
| 339 | log.Debugw("GetImageDownload", log.Fields{"deviceid": deviceID, "imagename": imageName}) |
| 340 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 341 | return agent.GetImageDownload(ctx, imageName) |
| 342 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 343 | return nil, status.Errorf(codes.NotFound, "%s", deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 344 | |
| 345 | } |
| 346 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 347 | // ListImageDownloads returns all image downloads known to the system |
| 348 | func (dMgr *DeviceManager) ListImageDownloads(ctx context.Context, deviceID string) (*voltha.ImageDownloads, error) { |
| 349 | log.Debugw("ListImageDownloads", log.Fields{"deviceid": deviceID}) |
| 350 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 351 | return agent.ListImageDownloads(ctx) |
| 352 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 353 | return nil, status.Errorf(codes.NotFound, "%s", deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 354 | |
| 355 | } |
| 356 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 357 | // GetImages returns all images for a specific device entry |
| 358 | func (dMgr *DeviceManager) GetImages(ctx context.Context, deviceID string) (*voltha.Images, error) { |
| 359 | log.Debugw("GetImages", log.Fields{"deviceid": deviceID}) |
| 360 | if agent := dMgr.getDeviceAgent(deviceID); agent != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 361 | return agent.GetImages(ctx) |
| 362 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 363 | return nil, status.Errorf(codes.NotFound, "%s", deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 364 | |
| 365 | } |
| 366 | |
| 367 | func (dMgr *DeviceManager) getParentDevice(childDevice *voltha.Device) *voltha.Device { |
| 368 | // Sanity check |
| 369 | if childDevice.Root { |
| 370 | // childDevice is the parent device |
| 371 | return childDevice |
| 372 | } |
| 373 | parentDevice, _ := dMgr.GetDevice(childDevice.ParentId) |
| 374 | return parentDevice |
| 375 | } |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 376 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 377 | //getAllChildDeviceIDs is a helper method to get all the child device IDs from the device passed as parameter |
| 378 | func (dMgr *DeviceManager) getAllChildDeviceIDs(parentDevice *voltha.Device) ([]string, error) { |
| 379 | log.Debugw("getAllChildDeviceIDs", log.Fields{"parentDeviceID": parentDevice.Id}) |
| 380 | childDeviceIDs := make([]string, 0) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 381 | if parentDevice != nil { |
| 382 | for _, port := range parentDevice.Ports { |
| 383 | for _, peer := range port.Peers { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 384 | childDeviceIDs = append(childDeviceIDs, peer.DeviceId) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 388 | return childDeviceIDs, nil |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 389 | } |