khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1 | /* |
Joey Armstrong | 393daca | 2023-07-06 08:47:54 -0400 | [diff] [blame] | 2 | * Copyright 2021-2023 Open Networking Foundation (ONF) and the ONF Contributors |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 3 | |
Joey Armstrong | 393daca | 2023-07-06 08:47:54 -0400 | [diff] [blame] | 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 |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 7 | |
Joey Armstrong | 393daca | 2023-07-06 08:47:54 -0400 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 9 | |
Joey Armstrong | 393daca | 2023-07-06 08:47:54 -0400 | [diff] [blame] | 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. |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 15 | */ |
| 16 | package device |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "errors" |
| 21 | |
| 22 | "github.com/golang/protobuf/ptypes/empty" |
| 23 | "github.com/opencord/voltha-go/rw_core/utils" |
| 24 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 25 | "github.com/opencord/voltha-protos/v5/go/common" |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 26 | "github.com/opencord/voltha-protos/v5/go/extension" |
| 27 | "github.com/opencord/voltha-protos/v5/go/omci" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 28 | ofp "github.com/opencord/voltha-protos/v5/go/openflow_13" |
yasin sapli | 8fe47e4 | 2023-06-09 21:19:00 +0000 | [diff] [blame] | 29 | "github.com/opencord/voltha-protos/v5/go/voip_system_profile" |
| 30 | "github.com/opencord/voltha-protos/v5/go/voip_user_profile" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 31 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 32 | "google.golang.org/grpc/codes" |
| 33 | "google.golang.org/grpc/status" |
| 34 | ) |
| 35 | |
| 36 | // CreateDevice creates a new parent device in the data model |
| 37 | func (dMgr *Manager) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) { |
| 38 | if device.MacAddress == "" && device.GetHostAndPort() == "" { |
| 39 | logger.Errorf(ctx, "no-device-info-present") |
| 40 | return &voltha.Device{}, errors.New("no-device-info-present; MAC or HOSTIP&PORT") |
| 41 | } |
| 42 | ctx = utils.WithRPCMetadataContext(ctx, "CreateDevice") |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 43 | logger.Info(ctx, "create-device", log.Fields{"device": *device}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 44 | |
| 45 | deviceExist, err := dMgr.isParentDeviceExist(ctx, device) |
| 46 | if err != nil { |
| 47 | logger.Errorf(ctx, "failed-to-fetch-parent-device-info") |
| 48 | return nil, err |
| 49 | } |
| 50 | if deviceExist { |
| 51 | logger.Errorf(ctx, "device-is-pre-provisioned-already-with-same-ip-port-or-mac-address") |
| 52 | return nil, errors.New("device is already pre-provisioned") |
| 53 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 54 | |
| 55 | // Ensure this device is set as root |
| 56 | device.Root = true |
| 57 | // Create and start a device agent for that device |
Himani Chawla | 4b4bd25 | 2021-11-08 15:59:40 +0530 | [diff] [blame] | 58 | agent := newAgent(device, dMgr, dMgr.dbPath, dMgr.dProxy, dMgr.internalTimeout, dMgr.rpcTimeout, dMgr.flowTimeout) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 59 | device, err = agent.start(ctx, false, device) |
| 60 | if err != nil { |
| 61 | logger.Errorw(ctx, "fail-to-start-device", log.Fields{"device-id": agent.deviceID, "error": err}) |
| 62 | return nil, err |
| 63 | } |
| 64 | dMgr.addDeviceAgentToMap(agent) |
| 65 | return device, nil |
| 66 | } |
| 67 | |
| 68 | // EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter |
| 69 | func (dMgr *Manager) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 70 | ctx = utils.WithRPCMetadataContext(ctx, "EnableDevice") |
| 71 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 72 | |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 73 | logger.Info(ctx, "enable-device", log.Fields{"device-id": id.Id}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 74 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 75 | if agent == nil { |
| 76 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
| 77 | } |
| 78 | return &empty.Empty{}, agent.enableDevice(ctx) |
| 79 | } |
| 80 | |
| 81 | // DisableDevice disables a device along with any child device it may have |
| 82 | func (dMgr *Manager) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 83 | ctx = utils.WithRPCMetadataContext(ctx, "DisableDevice") |
| 84 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 85 | |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 86 | logger.Info(ctx, "disable-device", log.Fields{"device-id": id.Id}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 87 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 88 | if agent == nil { |
| 89 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
| 90 | } |
| 91 | return &empty.Empty{}, agent.disableDevice(ctx) |
| 92 | } |
| 93 | |
Joey Armstrong | 393daca | 2023-07-06 08:47:54 -0400 | [diff] [blame] | 94 | // RebootDevice invoked the reboot API to the corresponding adapter |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 95 | func (dMgr *Manager) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 96 | ctx = utils.WithRPCMetadataContext(ctx, "RebootDevice") |
| 97 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 98 | |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 99 | logger.Info(ctx, "reboot-device", log.Fields{"device-id": id.Id}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 100 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 101 | if agent == nil { |
| 102 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
| 103 | } |
| 104 | return &empty.Empty{}, agent.rebootDevice(ctx) |
| 105 | } |
| 106 | |
| 107 | // DeleteDevice removes a device from the data model |
| 108 | func (dMgr *Manager) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 109 | ctx = utils.WithRPCMetadataContext(ctx, "DeleteDevice") |
| 110 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 111 | |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 112 | logger.Info(ctx, "delete-device", log.Fields{"device-id": id.Id}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 113 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 114 | if agent == nil { |
| 115 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
| 116 | } |
| 117 | return &empty.Empty{}, agent.deleteDevice(ctx) |
| 118 | } |
| 119 | |
| 120 | // ForceDeleteDevice removes a device from the data model forcefully without successfully waiting for the adapters. |
| 121 | func (dMgr *Manager) ForceDeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 122 | ctx = utils.WithRPCMetadataContext(ctx, "ForceDeleteDevice") |
| 123 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 124 | |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 125 | logger.Info(ctx, "force-delete-device", log.Fields{"device-id": id.Id}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 126 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 127 | if agent == nil { |
| 128 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
| 129 | } |
| 130 | return &empty.Empty{}, agent.deleteDeviceForce(ctx) |
| 131 | } |
| 132 | |
| 133 | // ListDevices retrieves the latest devices from the data model |
| 134 | func (dMgr *Manager) ListDevices(ctx context.Context, _ *empty.Empty) (*voltha.Devices, error) { |
| 135 | ctx = utils.WithRPCMetadataContext(ctx, "ListDevices") |
| 136 | |
| 137 | logger.Debug(ctx, "list-devices") |
| 138 | result := &voltha.Devices{} |
| 139 | |
| 140 | dMgr.deviceAgents.Range(func(key, value interface{}) bool { |
| 141 | result.Items = append(result.Items, value.(*Agent).device) |
| 142 | return true |
| 143 | }) |
| 144 | |
| 145 | logger.Debugw(ctx, "list-devices-end", log.Fields{"len": len(result.Items)}) |
| 146 | return result, nil |
| 147 | } |
| 148 | |
| 149 | // ListDeviceIds retrieves the latest device IDs information from the data model (memory data only) |
| 150 | func (dMgr *Manager) ListDeviceIds(ctx context.Context, _ *empty.Empty) (*voltha.IDs, error) { |
| 151 | ctx = utils.WithRPCMetadataContext(ctx, "ListDeviceIds") |
| 152 | |
| 153 | logger.Debug(ctx, "list-device-ids") |
| 154 | // Report only device IDs that are in the device agent map |
| 155 | return dMgr.listDeviceIdsFromMap(), nil |
| 156 | } |
| 157 | |
| 158 | // ReconcileDevices is a request to a voltha core to update its list of managed devices. This will |
| 159 | // trigger loading the devices along with their children and parent in memory |
| 160 | func (dMgr *Manager) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) { |
| 161 | ctx = utils.WithRPCMetadataContext(ctx, "ReconcileDevices") |
| 162 | |
| 163 | numDevices := 0 |
| 164 | if ids != nil { |
| 165 | numDevices = len(ids.Items) |
| 166 | } |
| 167 | |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 168 | logger.Info(ctx, "reconcile-devices", log.Fields{"num-devices": numDevices}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 169 | if ids != nil && len(ids.Items) != 0 { |
| 170 | toReconcile := len(ids.Items) |
| 171 | reconciled := 0 |
| 172 | var err error |
| 173 | for _, id := range ids.Items { |
| 174 | if err = dMgr.load(ctx, id.Id); err != nil { |
| 175 | logger.Warnw(ctx, "failure-reconciling-device", log.Fields{"device-id": id.Id, "error": err}) |
| 176 | } else { |
| 177 | reconciled++ |
| 178 | } |
| 179 | } |
| 180 | if toReconcile != reconciled { |
| 181 | return nil, status.Errorf(codes.DataLoss, "less-device-reconciled-than-requested:%d/%d", reconciled, toReconcile) |
| 182 | } |
| 183 | } else { |
| 184 | return nil, status.Errorf(codes.InvalidArgument, "empty-list-of-ids") |
| 185 | } |
| 186 | return &empty.Empty{}, nil |
| 187 | } |
| 188 | |
| 189 | // GetDevice exists primarily to implement the gRPC interface. |
| 190 | // Internal functions should call getDeviceReadOnly instead. |
| 191 | func (dMgr *Manager) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) { |
| 192 | ctx = utils.WithRPCMetadataContext(ctx, "GetDevice") |
| 193 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 194 | |
| 195 | return dMgr.getDeviceReadOnly(ctx, id.Id) |
| 196 | } |
| 197 | |
| 198 | // convenience to avoid redefining |
| 199 | var operationFailureResp = &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE} |
| 200 | |
| 201 | // DownloadImage execute an image download request |
| 202 | func (dMgr *Manager) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
| 203 | ctx = utils.WithRPCMetadataContext(ctx, "DownloadImage") |
| 204 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
| 205 | |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 206 | logger.Info(ctx, "download-image", log.Fields{"device-id": img.Id, "image-name": img.Name}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 207 | agent := dMgr.getDeviceAgent(ctx, img.Id) |
| 208 | if agent == nil { |
| 209 | return operationFailureResp, status.Errorf(codes.NotFound, "%s", img.Id) |
| 210 | } |
| 211 | resp, err := agent.downloadImage(ctx, img) |
| 212 | if err != nil { |
| 213 | return operationFailureResp, err |
| 214 | } |
| 215 | return resp, nil |
| 216 | } |
| 217 | |
| 218 | // CancelImageDownload cancels image download request |
| 219 | func (dMgr *Manager) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
| 220 | ctx = utils.WithRPCMetadataContext(ctx, "CancelImageDownload") |
| 221 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
| 222 | |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 223 | logger.Info(ctx, "cancel-image-download", log.Fields{"device-id": img.Id, "image-name": img.Name}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 224 | agent := dMgr.getDeviceAgent(ctx, img.Id) |
| 225 | if agent == nil { |
| 226 | return operationFailureResp, status.Errorf(codes.NotFound, "%s", img.Id) |
| 227 | } |
| 228 | resp, err := agent.cancelImageDownload(ctx, img) |
| 229 | if err != nil { |
| 230 | return operationFailureResp, err |
| 231 | } |
| 232 | return resp, nil |
| 233 | } |
| 234 | |
| 235 | // ActivateImageUpdate activates image update request |
| 236 | func (dMgr *Manager) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
| 237 | ctx = utils.WithRPCMetadataContext(ctx, "ActivateImageUpdate") |
| 238 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
| 239 | |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 240 | logger.Info(ctx, "activate-image-update", log.Fields{"device-id": img.Id, "image-name": img.Name}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 241 | agent := dMgr.getDeviceAgent(ctx, img.Id) |
| 242 | if agent == nil { |
| 243 | return operationFailureResp, status.Errorf(codes.NotFound, "%s", img.Id) |
| 244 | } |
| 245 | resp, err := agent.activateImage(ctx, img) |
| 246 | if err != nil { |
| 247 | return operationFailureResp, err |
| 248 | } |
| 249 | return resp, nil |
| 250 | } |
| 251 | |
| 252 | // RevertImageUpdate reverts image update |
| 253 | func (dMgr *Manager) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
| 254 | ctx = utils.WithRPCMetadataContext(ctx, "RevertImageUpdate") |
| 255 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
| 256 | |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 257 | logger.Info(ctx, "rever-image-update", log.Fields{"device-id": img.Id, "image-name": img.Name}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 258 | agent := dMgr.getDeviceAgent(ctx, img.Id) |
| 259 | if agent == nil { |
| 260 | return operationFailureResp, status.Errorf(codes.NotFound, "%s", img.Id) |
| 261 | } |
| 262 | resp, err := agent.revertImage(ctx, img) |
| 263 | if err != nil { |
| 264 | return operationFailureResp, err |
| 265 | } |
| 266 | return resp, nil |
| 267 | } |
| 268 | |
| 269 | func (dMgr *Manager) DownloadImageToDevice(ctx context.Context, request *voltha.DeviceImageDownloadRequest) (*voltha.DeviceImageResponse, error) { |
| 270 | if err := dMgr.validateImageDownloadRequest(request); err != nil { |
| 271 | return nil, err |
| 272 | } |
| 273 | |
| 274 | ctx = utils.WithRPCMetadataContext(ctx, "DownloadImageToDevice") |
| 275 | respCh := make(chan []*voltha.DeviceImageState, len(request.GetDeviceId())) |
| 276 | |
| 277 | for index, deviceID := range request.DeviceId { |
| 278 | // Create download request per device |
| 279 | downloadReq := &voltha.DeviceImageDownloadRequest{ |
| 280 | Image: request.Image, |
| 281 | ActivateOnSuccess: request.ActivateOnSuccess, |
| 282 | CommitOnSuccess: request.CommitOnSuccess, |
| 283 | } |
| 284 | |
| 285 | //slice-out only single deviceID from the request |
| 286 | downloadReq.DeviceId = request.DeviceId[index : index+1] |
| 287 | |
| 288 | go func(deviceID string, req *voltha.DeviceImageDownloadRequest, ch chan []*voltha.DeviceImageState) { |
| 289 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
| 290 | if agent == nil { |
ssiddiqui | 9e120e9 | 2021-10-11 12:33:05 +0530 | [diff] [blame] | 291 | logger.Errorw(ctx, "Device-agent-not-found", log.Fields{"device-id": deviceID}) |
| 292 | ch <- []*voltha.DeviceImageState{{ |
| 293 | DeviceId: deviceID, |
| 294 | ImageState: &voltha.ImageState{ |
| 295 | Version: req.GetImage().GetVersion(), |
| 296 | DownloadState: voltha.ImageState_DOWNLOAD_FAILED, |
| 297 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 298 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 299 | }, |
| 300 | }} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 301 | return |
| 302 | } |
| 303 | |
| 304 | resp, err := agent.downloadImageToDevice(ctx, req) |
| 305 | if err != nil { |
| 306 | logger.Errorw(ctx, "download-image-to-device-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | 9e120e9 | 2021-10-11 12:33:05 +0530 | [diff] [blame] | 307 | ch <- []*voltha.DeviceImageState{{ |
| 308 | DeviceId: deviceID, |
| 309 | ImageState: &voltha.ImageState{ |
| 310 | Version: req.GetImage().GetVersion(), |
| 311 | DownloadState: voltha.ImageState_DOWNLOAD_FAILED, |
| 312 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 313 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 314 | }, |
| 315 | }} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 316 | return |
| 317 | } |
| 318 | |
| 319 | err = dMgr.validateDeviceImageResponse(resp) |
| 320 | if err != nil { |
| 321 | logger.Errorw(ctx, "download-image-to-device-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | 9e120e9 | 2021-10-11 12:33:05 +0530 | [diff] [blame] | 322 | ch <- []*voltha.DeviceImageState{{ |
| 323 | DeviceId: deviceID, |
| 324 | ImageState: &voltha.ImageState{ |
| 325 | Version: req.GetImage().GetVersion(), |
| 326 | DownloadState: voltha.ImageState_DOWNLOAD_FAILED, |
| 327 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 328 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 329 | }, |
| 330 | }} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 331 | return |
| 332 | } |
| 333 | ch <- resp.GetDeviceImageStates() |
| 334 | }(deviceID.GetId(), downloadReq, respCh) |
| 335 | |
| 336 | } |
| 337 | |
| 338 | return dMgr.waitForAllResponses(ctx, "download-image-to-device", respCh, len(request.GetDeviceId())) |
| 339 | } |
| 340 | |
| 341 | func (dMgr *Manager) GetImageStatus(ctx context.Context, request *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { |
| 342 | if err := dMgr.validateImageRequest(request); err != nil { |
| 343 | return nil, err |
| 344 | } |
| 345 | |
| 346 | ctx = utils.WithRPCMetadataContext(ctx, "GetImageStatus") |
| 347 | |
| 348 | respCh := make(chan []*voltha.DeviceImageState, len(request.GetDeviceId())) |
Elia Battiston | 58d1c06 | 2022-02-08 11:54:27 +0100 | [diff] [blame] | 349 | |
| 350 | if request.DeviceId == nil { |
| 351 | //Reply for every ONU |
| 352 | dMgr.deviceAgents.Range(func(key, value interface{}) bool { |
| 353 | device := value.(*Agent).device |
| 354 | if !device.Root { |
| 355 | request.DeviceId = append(request.DeviceId, &common.ID{Id: value.(*Agent).device.Id}) |
| 356 | } |
| 357 | return true |
| 358 | }) |
| 359 | } |
| 360 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 361 | for index, deviceID := range request.DeviceId { |
| 362 | // Create status request per device |
| 363 | imageStatusReq := &voltha.DeviceImageRequest{ |
| 364 | Version: request.Version, |
| 365 | CommitOnSuccess: request.CommitOnSuccess, |
| 366 | } |
| 367 | |
| 368 | //slice-out only single deviceID from the request |
| 369 | imageStatusReq.DeviceId = request.DeviceId[index : index+1] |
| 370 | |
| 371 | go func(deviceID string, req *voltha.DeviceImageRequest, ch chan []*voltha.DeviceImageState) { |
| 372 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
| 373 | if agent == nil { |
ssiddiqui | 9e120e9 | 2021-10-11 12:33:05 +0530 | [diff] [blame] | 374 | logger.Errorw(ctx, "Device-agent-not-found", log.Fields{"device-id": deviceID}) |
| 375 | ch <- []*voltha.DeviceImageState{{ |
| 376 | DeviceId: deviceID, |
| 377 | ImageState: &voltha.ImageState{ |
| 378 | Version: request.GetVersion(), |
| 379 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 380 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 381 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 382 | }, |
| 383 | }} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 384 | return |
| 385 | } |
| 386 | |
| 387 | resp, err := agent.getImageStatus(ctx, req) |
| 388 | if err != nil { |
| 389 | logger.Errorw(ctx, "get-image-status-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | 9e120e9 | 2021-10-11 12:33:05 +0530 | [diff] [blame] | 390 | ch <- []*voltha.DeviceImageState{{ |
| 391 | DeviceId: deviceID, |
| 392 | ImageState: &voltha.ImageState{ |
| 393 | Version: request.GetVersion(), |
| 394 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 395 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 396 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 397 | }, |
| 398 | }} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 399 | return |
| 400 | } |
| 401 | |
| 402 | err = dMgr.validateDeviceImageResponse(resp) |
| 403 | if err != nil { |
| 404 | logger.Errorw(ctx, "get-image-status-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | 9e120e9 | 2021-10-11 12:33:05 +0530 | [diff] [blame] | 405 | ch <- []*voltha.DeviceImageState{{ |
| 406 | DeviceId: deviceID, |
| 407 | ImageState: &voltha.ImageState{ |
| 408 | Version: request.GetVersion(), |
| 409 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 410 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 411 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 412 | }, |
| 413 | }} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 414 | return |
| 415 | } |
| 416 | ch <- resp.GetDeviceImageStates() |
| 417 | }(deviceID.GetId(), imageStatusReq, respCh) |
| 418 | |
| 419 | } |
| 420 | |
| 421 | return dMgr.waitForAllResponses(ctx, "get-image-status", respCh, len(request.GetDeviceId())) |
| 422 | } |
| 423 | |
| 424 | func (dMgr *Manager) AbortImageUpgradeToDevice(ctx context.Context, request *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { |
| 425 | if err := dMgr.validateImageRequest(request); err != nil { |
| 426 | return nil, err |
| 427 | } |
| 428 | |
| 429 | ctx = utils.WithRPCMetadataContext(ctx, "AbortImageUpgradeToDevice") |
| 430 | respCh := make(chan []*voltha.DeviceImageState, len(request.GetDeviceId())) |
| 431 | |
| 432 | for index, deviceID := range request.DeviceId { |
| 433 | // Create abort request per device |
| 434 | abortImageReq := &voltha.DeviceImageRequest{ |
| 435 | Version: request.Version, |
| 436 | CommitOnSuccess: request.CommitOnSuccess, |
| 437 | } |
| 438 | |
| 439 | //slice-out only single deviceID from the request |
| 440 | abortImageReq.DeviceId = request.DeviceId[index : index+1] |
| 441 | |
| 442 | go func(deviceID string, req *voltha.DeviceImageRequest, ch chan []*voltha.DeviceImageState) { |
| 443 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
| 444 | if agent == nil { |
ssiddiqui | 9e120e9 | 2021-10-11 12:33:05 +0530 | [diff] [blame] | 445 | logger.Errorw(ctx, "Device-agent-not-found", log.Fields{"device-id": deviceID}) |
| 446 | ch <- []*voltha.DeviceImageState{{ |
| 447 | DeviceId: deviceID, |
| 448 | ImageState: &voltha.ImageState{ |
| 449 | Version: request.GetVersion(), |
| 450 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 451 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 452 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 453 | }, |
| 454 | }} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 455 | return |
| 456 | } |
| 457 | |
| 458 | resp, err := agent.abortImageUpgradeToDevice(ctx, req) |
| 459 | if err != nil { |
| 460 | logger.Errorw(ctx, "abort-image-upgrade-to-device-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | 9e120e9 | 2021-10-11 12:33:05 +0530 | [diff] [blame] | 461 | ch <- []*voltha.DeviceImageState{{ |
| 462 | DeviceId: deviceID, |
| 463 | ImageState: &voltha.ImageState{ |
| 464 | Version: request.GetVersion(), |
| 465 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 466 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 467 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 468 | }, |
| 469 | }} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 470 | return |
| 471 | } |
| 472 | |
| 473 | err = dMgr.validateDeviceImageResponse(resp) |
| 474 | if err != nil { |
| 475 | logger.Errorw(ctx, "abort-image-upgrade-to-device-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | 9e120e9 | 2021-10-11 12:33:05 +0530 | [diff] [blame] | 476 | ch <- []*voltha.DeviceImageState{{ |
| 477 | DeviceId: deviceID, |
| 478 | ImageState: &voltha.ImageState{ |
| 479 | Version: request.GetVersion(), |
| 480 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 481 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 482 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 483 | }, |
| 484 | }} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 485 | return |
| 486 | } |
| 487 | ch <- resp.GetDeviceImageStates() |
| 488 | }(deviceID.GetId(), abortImageReq, respCh) |
| 489 | |
| 490 | } |
| 491 | |
| 492 | return dMgr.waitForAllResponses(ctx, "abort-image-upgrade-to-device", respCh, len(request.GetDeviceId())) |
| 493 | } |
| 494 | |
| 495 | func (dMgr *Manager) GetOnuImages(ctx context.Context, id *common.ID) (*voltha.OnuImages, error) { |
| 496 | if id == nil || id.Id == "" { |
| 497 | return nil, status.Errorf(codes.InvalidArgument, "empty device id") |
| 498 | } |
| 499 | |
| 500 | ctx = utils.WithRPCMetadataContext(ctx, "GetOnuImages") |
| 501 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 502 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 503 | if agent == nil { |
| 504 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
| 505 | } |
| 506 | |
| 507 | resp, err := agent.getOnuImages(ctx, id) |
| 508 | if err != nil { |
| 509 | return nil, err |
| 510 | } |
| 511 | |
| 512 | logger.Debugw(ctx, "get-onu-images-result", log.Fields{"onu-image": resp.Items}) |
| 513 | |
| 514 | return resp, nil |
| 515 | } |
| 516 | |
| 517 | func (dMgr *Manager) ActivateImage(ctx context.Context, request *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { |
| 518 | if err := dMgr.validateImageRequest(request); err != nil { |
| 519 | return nil, err |
| 520 | } |
| 521 | |
| 522 | ctx = utils.WithRPCMetadataContext(ctx, "ActivateImage") |
| 523 | respCh := make(chan []*voltha.DeviceImageState, len(request.GetDeviceId())) |
| 524 | |
| 525 | for index, deviceID := range request.DeviceId { |
| 526 | // Create activate request per device |
| 527 | activateImageReq := &voltha.DeviceImageRequest{ |
| 528 | Version: request.Version, |
| 529 | CommitOnSuccess: request.CommitOnSuccess, |
| 530 | } |
| 531 | |
| 532 | //slice-out only single deviceID from the request |
| 533 | activateImageReq.DeviceId = request.DeviceId[index : index+1] |
| 534 | |
| 535 | go func(deviceID string, req *voltha.DeviceImageRequest, ch chan []*voltha.DeviceImageState) { |
| 536 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
| 537 | if agent == nil { |
ssiddiqui | 9e120e9 | 2021-10-11 12:33:05 +0530 | [diff] [blame] | 538 | logger.Errorw(ctx, "Device-agent-not-found", log.Fields{"device-id": deviceID}) |
| 539 | ch <- []*voltha.DeviceImageState{{ |
| 540 | DeviceId: deviceID, |
| 541 | ImageState: &voltha.ImageState{ |
| 542 | Version: request.GetVersion(), |
| 543 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 544 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 545 | ImageState: voltha.ImageState_IMAGE_ACTIVATION_ABORTED, |
| 546 | }, |
| 547 | }} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 548 | return |
| 549 | } |
| 550 | |
| 551 | resp, err := agent.activateImageOnDevice(ctx, req) |
| 552 | if err != nil { |
| 553 | logger.Errorw(ctx, "activate-image-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | 9e120e9 | 2021-10-11 12:33:05 +0530 | [diff] [blame] | 554 | ch <- []*voltha.DeviceImageState{{ |
| 555 | DeviceId: deviceID, |
| 556 | ImageState: &voltha.ImageState{ |
| 557 | Version: request.GetVersion(), |
| 558 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 559 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 560 | ImageState: voltha.ImageState_IMAGE_ACTIVATION_ABORTED, |
| 561 | }, |
| 562 | }} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 563 | return |
| 564 | } |
| 565 | |
| 566 | err = dMgr.validateDeviceImageResponse(resp) |
| 567 | if err != nil { |
| 568 | logger.Errorw(ctx, "activate-image-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | 9e120e9 | 2021-10-11 12:33:05 +0530 | [diff] [blame] | 569 | ch <- []*voltha.DeviceImageState{{ |
| 570 | DeviceId: deviceID, |
| 571 | ImageState: &voltha.ImageState{ |
| 572 | Version: request.GetVersion(), |
| 573 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 574 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 575 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 576 | }, |
| 577 | }} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 578 | return |
| 579 | } |
| 580 | |
| 581 | ch <- resp.GetDeviceImageStates() |
| 582 | }(deviceID.GetId(), activateImageReq, respCh) |
| 583 | |
| 584 | } |
| 585 | |
| 586 | return dMgr.waitForAllResponses(ctx, "activate-image", respCh, len(request.GetDeviceId())) |
| 587 | } |
| 588 | |
| 589 | func (dMgr *Manager) CommitImage(ctx context.Context, request *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { |
| 590 | if err := dMgr.validateImageRequest(request); err != nil { |
| 591 | return nil, err |
| 592 | } |
| 593 | |
| 594 | ctx = utils.WithRPCMetadataContext(ctx, "CommitImage") |
| 595 | respCh := make(chan []*voltha.DeviceImageState, len(request.GetDeviceId())) |
| 596 | |
| 597 | for index, deviceID := range request.DeviceId { |
| 598 | // Create commit request per device |
| 599 | commitImageReq := &voltha.DeviceImageRequest{ |
| 600 | Version: request.Version, |
| 601 | CommitOnSuccess: request.CommitOnSuccess, |
| 602 | } |
| 603 | //slice-out only single deviceID from the request |
| 604 | commitImageReq.DeviceId = request.DeviceId[index : index+1] |
| 605 | |
| 606 | go func(deviceID string, req *voltha.DeviceImageRequest, ch chan []*voltha.DeviceImageState) { |
| 607 | agent := dMgr.getDeviceAgent(ctx, deviceID) |
| 608 | if agent == nil { |
ssiddiqui | 9e120e9 | 2021-10-11 12:33:05 +0530 | [diff] [blame] | 609 | logger.Errorw(ctx, "Device-agent-not-found", log.Fields{"device-id": deviceID}) |
| 610 | ch <- []*voltha.DeviceImageState{{ |
| 611 | DeviceId: deviceID, |
| 612 | ImageState: &voltha.ImageState{ |
| 613 | Version: request.GetVersion(), |
| 614 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 615 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 616 | ImageState: voltha.ImageState_IMAGE_COMMIT_ABORTED, |
| 617 | }, |
| 618 | }} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 619 | return |
| 620 | } |
| 621 | |
| 622 | resp, err := agent.commitImage(ctx, req) |
| 623 | if err != nil { |
| 624 | logger.Errorw(ctx, "commit-image-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | 9e120e9 | 2021-10-11 12:33:05 +0530 | [diff] [blame] | 625 | ch <- []*voltha.DeviceImageState{{ |
| 626 | DeviceId: deviceID, |
| 627 | ImageState: &voltha.ImageState{ |
| 628 | Version: request.GetVersion(), |
| 629 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 630 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 631 | ImageState: voltha.ImageState_IMAGE_COMMIT_ABORTED, |
| 632 | }, |
| 633 | }} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 634 | return |
| 635 | } |
| 636 | |
| 637 | err = dMgr.validateDeviceImageResponse(resp) |
| 638 | if err != nil { |
| 639 | logger.Errorf(ctx, "commit-image-failed", log.Fields{"device-id": deviceID, "error": err}) |
ssiddiqui | 9e120e9 | 2021-10-11 12:33:05 +0530 | [diff] [blame] | 640 | ch <- []*voltha.DeviceImageState{{ |
| 641 | DeviceId: deviceID, |
| 642 | ImageState: &voltha.ImageState{ |
| 643 | Version: request.GetVersion(), |
| 644 | DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
| 645 | Reason: voltha.ImageState_UNKNOWN_ERROR, |
| 646 | ImageState: voltha.ImageState_IMAGE_UNKNOWN, |
| 647 | }, |
| 648 | }} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 649 | return |
| 650 | } |
| 651 | ch <- resp.GetDeviceImageStates() |
| 652 | }(deviceID.GetId(), commitImageReq, respCh) |
| 653 | |
| 654 | } |
| 655 | |
| 656 | return dMgr.waitForAllResponses(ctx, "commit-image", respCh, len(request.GetDeviceId())) |
| 657 | } |
| 658 | |
| 659 | // convenience to avoid redefining |
| 660 | var imageDownloadFailureResp = &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN} |
| 661 | |
| 662 | // GetImageDownloadStatus returns status of image download |
| 663 | func (dMgr *Manager) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 664 | ctx = utils.WithRPCMetadataContext(ctx, "GetImageDownloadStatus") |
| 665 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
| 666 | |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 667 | logger.Info(ctx, "get-image-download-status", log.Fields{"device-id": img.Id, "image-name": img.Name}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 668 | agent := dMgr.getDeviceAgent(ctx, img.Id) |
| 669 | if agent == nil { |
| 670 | return imageDownloadFailureResp, status.Errorf(codes.NotFound, "%s", img.Id) |
| 671 | } |
| 672 | resp, err := agent.getImageDownloadStatus(ctx, img) |
| 673 | if err != nil { |
| 674 | return imageDownloadFailureResp, err |
| 675 | } |
| 676 | return resp, nil |
| 677 | } |
| 678 | |
| 679 | // GetImageDownload returns image download |
| 680 | func (dMgr *Manager) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 681 | ctx = utils.WithRPCMetadataContext(ctx, "GetImageDownload") |
| 682 | log.EnrichSpan(ctx, log.Fields{"device-id": img.Id}) |
| 683 | |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 684 | logger.Info(ctx, "get-image-download", log.Fields{"device-id": img.Id, "image-name": img.Name}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 685 | agent := dMgr.getDeviceAgent(ctx, img.Id) |
| 686 | if agent == nil { |
| 687 | return imageDownloadFailureResp, status.Errorf(codes.NotFound, "%s", img.Id) |
| 688 | } |
| 689 | resp, err := agent.getImageDownload(ctx, img) |
| 690 | if err != nil { |
| 691 | return imageDownloadFailureResp, err |
| 692 | } |
| 693 | return resp, nil |
| 694 | } |
| 695 | |
| 696 | // ListImageDownloads returns image downloads |
| 697 | func (dMgr *Manager) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) { |
| 698 | ctx = utils.WithRPCMetadataContext(ctx, "ListImageDownloads") |
| 699 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 700 | |
| 701 | logger.Debugw(ctx, "list-image-downloads", log.Fields{"device-id": id.Id}) |
| 702 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 703 | if agent == nil { |
| 704 | return &voltha.ImageDownloads{Items: []*voltha.ImageDownload{imageDownloadFailureResp}}, status.Errorf(codes.NotFound, "%s", id.Id) |
| 705 | } |
| 706 | resp, err := agent.listImageDownloads(ctx, id.Id) |
| 707 | if err != nil { |
| 708 | return &voltha.ImageDownloads{Items: []*voltha.ImageDownload{imageDownloadFailureResp}}, err |
| 709 | } |
| 710 | return resp, nil |
| 711 | } |
| 712 | |
| 713 | // GetImages returns all images for a specific device entry |
| 714 | func (dMgr *Manager) GetImages(ctx context.Context, id *voltha.ID) (*voltha.Images, error) { |
| 715 | ctx = utils.WithRPCMetadataContext(ctx, "GetImages") |
| 716 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 717 | |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 718 | logger.Info(ctx, "get-images", log.Fields{"device-id": id.Id}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 719 | device, err := dMgr.getDeviceReadOnly(ctx, id.Id) |
| 720 | if err != nil { |
| 721 | return nil, err |
| 722 | } |
| 723 | return device.Images, nil |
| 724 | } |
| 725 | |
| 726 | // ListDevicePorts returns the ports details for a specific device entry |
| 727 | func (dMgr *Manager) ListDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.Ports, error) { |
| 728 | ctx = utils.WithRPCMetadataContext(ctx, "ListDevicePorts") |
| 729 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 730 | |
| 731 | logger.Debugw(ctx, "list-device-ports", log.Fields{"device-id": id.Id}) |
| 732 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 733 | if agent == nil { |
| 734 | return nil, status.Errorf(codes.NotFound, "device-%s", id.Id) |
| 735 | } |
| 736 | |
| 737 | ports := agent.listDevicePorts() |
| 738 | ctr, ret := 0, make([]*voltha.Port, len(ports)) |
| 739 | for _, port := range ports { |
| 740 | ret[ctr] = port |
| 741 | ctr++ |
| 742 | } |
| 743 | return &voltha.Ports{Items: ret}, nil |
| 744 | } |
| 745 | |
| 746 | // ListDevicePmConfigs returns pm configs of device |
| 747 | func (dMgr *Manager) ListDevicePmConfigs(ctx context.Context, id *voltha.ID) (*voltha.PmConfigs, error) { |
| 748 | ctx = utils.WithRPCMetadataContext(ctx, "ListDevicePmConfigs") |
| 749 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 750 | |
| 751 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 752 | if agent == nil { |
| 753 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
| 754 | } |
| 755 | return agent.listPmConfigs(ctx) |
| 756 | } |
| 757 | |
| 758 | // UpdateDevicePmConfigs updates the PM configs. This is executed when the northbound gRPC API is invoked, typically |
| 759 | // following a user action |
| 760 | func (dMgr *Manager) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) { |
| 761 | ctx = utils.WithRPCMetadataContext(ctx, "UpdateDevicePmConfigs") |
| 762 | log.EnrichSpan(ctx, log.Fields{"device-id": configs.Id}) |
| 763 | |
| 764 | if configs.Id == "" { |
| 765 | return nil, status.Error(codes.FailedPrecondition, "invalid-device-Id") |
| 766 | } |
| 767 | agent := dMgr.getDeviceAgent(ctx, configs.Id) |
| 768 | if agent == nil { |
| 769 | return nil, status.Errorf(codes.NotFound, "%s", configs.Id) |
| 770 | } |
| 771 | return &empty.Empty{}, agent.updatePmConfigs(ctx, configs) |
| 772 | } |
| 773 | |
| 774 | // ListDeviceFlows returns the flow details for a specific device entry |
| 775 | func (dMgr *Manager) ListDeviceFlows(ctx context.Context, id *voltha.ID) (*ofp.Flows, error) { |
| 776 | ctx = utils.WithRPCMetadataContext(ctx, "ListDeviceFlows") |
| 777 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 778 | logger.Debugw(ctx, "list-device-flows", log.Fields{"device-id": id.Id}) |
| 779 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 780 | if agent == nil { |
| 781 | return nil, status.Errorf(codes.NotFound, "device-%s", id.Id) |
| 782 | } |
| 783 | |
| 784 | flows := agent.listDeviceFlows() |
| 785 | ctr, ret := 0, make([]*ofp.OfpFlowStats, len(flows)) |
| 786 | for _, flow := range flows { |
| 787 | ret[ctr] = flow |
| 788 | ctr++ |
| 789 | } |
| 790 | return &ofp.Flows{Items: ret}, nil |
| 791 | } |
| 792 | |
| 793 | // ListDeviceFlowGroups returns the flow group details for a specific device entry |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 794 | func (dMgr *Manager) ListDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*ofp.FlowGroups, error) { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 795 | ctx = utils.WithRPCMetadataContext(ctx, "ListDeviceFlowGroups") |
| 796 | log.EnrichSpan(ctx, log.Fields{"device-id": id.Id}) |
| 797 | logger.Debugw(ctx, "list-device-flow-groups", log.Fields{"device-id": id.Id}) |
| 798 | agent := dMgr.getDeviceAgent(ctx, id.Id) |
| 799 | if agent == nil { |
| 800 | return nil, status.Errorf(codes.NotFound, "device-%s", id.Id) |
| 801 | } |
| 802 | groups := agent.listDeviceGroups() |
| 803 | ctr, ret := 0, make([]*ofp.OfpGroupEntry, len(groups)) |
| 804 | for _, group := range groups { |
| 805 | ret[ctr] = group |
| 806 | ctr++ |
| 807 | } |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 808 | return &ofp.FlowGroups{Items: ret}, nil |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | func (dMgr *Manager) EnablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) { |
| 812 | ctx = utils.WithRPCMetadataContext(ctx, "EnablePort") |
| 813 | log.EnrichSpan(ctx, log.Fields{"device-id": port.DeviceId}) |
| 814 | |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 815 | logger.Info(ctx, "enable-port", log.Fields{"device-id": port.DeviceId, "port-no": port.PortNo}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 816 | agent := dMgr.getDeviceAgent(ctx, port.DeviceId) |
| 817 | if agent == nil { |
| 818 | return nil, status.Errorf(codes.NotFound, "%s", port.DeviceId) |
| 819 | } |
| 820 | return &empty.Empty{}, agent.enablePort(ctx, port.PortNo) |
| 821 | } |
| 822 | |
| 823 | func (dMgr *Manager) DisablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) { |
| 824 | ctx = utils.WithRPCMetadataContext(ctx, "DisablePort") |
| 825 | log.EnrichSpan(ctx, log.Fields{"device-id": port.DeviceId}) |
| 826 | |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 827 | logger.Info(ctx, "disable-port", log.Fields{"device-id": port.DeviceId, "port-no": port.PortNo}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 828 | agent := dMgr.getDeviceAgent(ctx, port.DeviceId) |
| 829 | if agent == nil { |
| 830 | return nil, status.Errorf(codes.NotFound, "%s", port.DeviceId) |
| 831 | } |
| 832 | return &empty.Empty{}, agent.disablePort(ctx, port.PortNo) |
| 833 | } |
| 834 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 835 | func (dMgr *Manager) GetExtValue(ctx context.Context, value *extension.ValueSpecifier) (*extension.ReturnValues, error) { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 836 | ctx = utils.WithRPCMetadataContext(ctx, "GetExtValue") |
| 837 | log.EnrichSpan(ctx, log.Fields{"device-id": value.Id}) |
| 838 | |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 839 | logger.Info(ctx, "get-ext-value", log.Fields{"onu-id": value.Id}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 840 | cDevice, err := dMgr.getDeviceReadOnly(ctx, value.Id) |
| 841 | if err != nil { |
| 842 | return nil, status.Errorf(codes.Aborted, "%s", err.Error()) |
| 843 | } |
| 844 | pDevice, err := dMgr.getDeviceReadOnly(ctx, cDevice.ParentId) |
| 845 | if err != nil { |
| 846 | return nil, status.Errorf(codes.Aborted, "%s", err.Error()) |
| 847 | } |
| 848 | if agent := dMgr.getDeviceAgent(ctx, cDevice.ParentId); agent != nil { |
| 849 | resp, err := agent.getExtValue(ctx, pDevice, cDevice, value) |
| 850 | if err != nil { |
| 851 | return nil, err |
| 852 | } |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 853 | logger.Info(ctx, "get-ext-value-result", log.Fields{"result": resp}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 854 | return resp, nil |
| 855 | } |
| 856 | return nil, status.Errorf(codes.NotFound, "%s", value.Id) |
| 857 | |
| 858 | } |
| 859 | |
| 860 | // SetExtValue set some given configs or value |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 861 | func (dMgr *Manager) SetExtValue(ctx context.Context, value *extension.ValueSet) (*empty.Empty, error) { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 862 | ctx = utils.WithRPCMetadataContext(ctx, "SetExtValue") |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 863 | logger.Info(ctx, "set-ext-value", log.Fields{"onu-id": value.Id}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 864 | |
| 865 | device, err := dMgr.getDeviceReadOnly(ctx, value.Id) |
| 866 | if err != nil { |
| 867 | return nil, status.Errorf(codes.Aborted, "%s", err.Error()) |
| 868 | } |
| 869 | if agent := dMgr.getDeviceAgent(ctx, device.Id); agent != nil { |
| 870 | resp, err := agent.setExtValue(ctx, device, value) |
| 871 | if err != nil { |
| 872 | return nil, err |
| 873 | } |
nikesh.krishnan | df10b2b | 2023-09-29 21:24:52 +0530 | [diff] [blame] | 874 | logger.Info(ctx, "set-ext-value-result", log.Fields{"result": resp}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 875 | return resp, nil |
| 876 | } |
| 877 | return nil, status.Errorf(codes.NotFound, "%s", value.Id) |
| 878 | |
| 879 | } |
| 880 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 881 | func (dMgr *Manager) StartOmciTestAction(ctx context.Context, request *omci.OmciTestRequest) (*omci.TestResponse, error) { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 882 | ctx = utils.WithRPCMetadataContext(ctx, "StartOmciTestAction") |
| 883 | log.EnrichSpan(ctx, log.Fields{"device-id": request.Id}) |
| 884 | |
| 885 | logger.Debugw(ctx, "start-omci-test-action", log.Fields{"device-id": request.Id, "uuid": request.Uuid}) |
| 886 | agent := dMgr.getDeviceAgent(ctx, request.Id) |
| 887 | if agent == nil { |
| 888 | return nil, status.Errorf(codes.NotFound, "%s", request.Id) |
| 889 | } |
| 890 | return agent.startOmciTest(ctx, request) |
| 891 | } |
| 892 | |
| 893 | func (dMgr *Manager) SimulateAlarm(ctx context.Context, simulateReq *voltha.SimulateAlarmRequest) (*common.OperationResp, error) { |
| 894 | ctx = utils.WithRPCMetadataContext(ctx, "SimulateAlarm") |
| 895 | |
| 896 | logger.Debugw(ctx, "simulate-alarm", log.Fields{"id": simulateReq.Id, "indicator": simulateReq.Indicator, "intf-id": simulateReq.IntfId, |
| 897 | "port-type-name": simulateReq.PortTypeName, "onu-device-id": simulateReq.OnuDeviceId, "inverse-bit-error-rate": simulateReq.InverseBitErrorRate, |
| 898 | "drift": simulateReq.Drift, "new-eqd": simulateReq.NewEqd, "onu-serial-number": simulateReq.OnuSerialNumber, "operation": simulateReq.Operation}) |
| 899 | agent := dMgr.getDeviceAgent(ctx, simulateReq.Id) |
| 900 | if agent == nil { |
| 901 | return nil, status.Errorf(codes.NotFound, "%s", simulateReq.Id) |
| 902 | } |
| 903 | if err := agent.simulateAlarm(ctx, simulateReq); err != nil { |
| 904 | return nil, err |
| 905 | } |
| 906 | return &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}, nil |
| 907 | } |
yasin sapli | 8fe47e4 | 2023-06-09 21:19:00 +0000 | [diff] [blame] | 908 | |
| 909 | func (dMgr *Manager) PutVoipUserProfile(ctx context.Context, voipUserProfileRequest *voip_user_profile.VoipUserProfileRequest) (*empty.Empty, error) { |
| 910 | return nil, status.Error(codes.Unimplemented, "put-voip-user-profile-not-implemented") |
| 911 | } |
| 912 | |
| 913 | func (dMgr *Manager) DeleteVoipUserProfile(ctx context.Context, key *common.Key) (*empty.Empty, error) { |
| 914 | return nil, status.Error(codes.Unimplemented, "delete-voip-user-profile-not-implemented") |
| 915 | } |
| 916 | |
| 917 | func (dMgr *Manager) PutVoipSystemProfile(ctx context.Context, voipSystemProfileRequest *voip_system_profile.VoipSystemProfileRequest) (*empty.Empty, error) { |
| 918 | return nil, status.Error(codes.Unimplemented, "put-voip-system-profile-not-implemented") |
| 919 | } |
| 920 | |
| 921 | func (dMgr *Manager) DeleteVoipSystemProfile(ctx context.Context, key *common.Key) (*empty.Empty, error) { |
| 922 | return nil, status.Error(codes.Unimplemented, "delete-voip-system-profile-not-implemented") |
| 923 | } |