Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [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 | */ |
| 16 | |
| 17 | package device |
| 18 | |
| 19 | import ( |
| 20 | "context" |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 21 | "github.com/opencord/voltha-protos/v4/go/common" |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 22 | |
| 23 | "github.com/gogo/protobuf/proto" |
| 24 | "github.com/golang/protobuf/ptypes" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 26 | "github.com/opencord/voltha-protos/v4/go/voltha" |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 27 | "google.golang.org/grpc/codes" |
| 28 | "google.golang.org/grpc/status" |
| 29 | ) |
| 30 | |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 31 | func (agent *Agent) downloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 32 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 33 | return nil, err |
| 34 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 35 | logger.Debugw(ctx, "downloadImage", log.Fields{"device-id": agent.deviceID}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 36 | |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 37 | if agent.device.Root { |
| 38 | return nil, status.Errorf(codes.FailedPrecondition, "device-id:%s, is an OLT. Image update "+ |
| 39 | "not supported by VOLTHA. Use Device Manager or other means", agent.deviceID) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 40 | } |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 41 | |
| 42 | device := agent.cloneDeviceWithoutLock() |
| 43 | if device.ImageDownloads != nil { |
| 44 | for _, image := range device.ImageDownloads { |
| 45 | if image.DownloadState == voltha.ImageDownload_DOWNLOAD_REQUESTED { |
| 46 | return nil, status.Errorf(codes.FailedPrecondition, "device-id:%s, already downloading image:%s", |
| 47 | agent.deviceID, image.Name) |
| 48 | } |
| 49 | } |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 50 | } |
| 51 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 52 | // Save the image |
| 53 | clonedImg := proto.Clone(img).(*voltha.ImageDownload) |
| 54 | clonedImg.DownloadState = voltha.ImageDownload_DOWNLOAD_REQUESTED |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 55 | cloned := agent.cloneDeviceWithoutLock() |
| 56 | cloned.ImageDownloads = append(device.ImageDownloads, clonedImg) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 57 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 58 | cloned.AdminState = voltha.AdminState_DOWNLOADING_IMAGE |
| 59 | if err := agent.updateDeviceAndReleaseLock(ctx, cloned); err != nil { |
| 60 | return nil, err |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 61 | } |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 62 | |
| 63 | // Send the request to the adapter |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 64 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 65 | ch, err := agent.adapterProxy.DownloadImage(subCtx, cloned, clonedImg) |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 66 | if err != nil { |
| 67 | cancel() |
| 68 | return nil, err |
| 69 | } |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 70 | go agent.waitForAdapterResponse(subCtx, cancel, "downloadImage", ch, agent.onImageSuccess, agent.onImageFailure) |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 71 | |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 72 | return &common.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 75 | // getImage is a helper method to figure out if an image is already registered |
| 76 | func getImage(img *voltha.ImageDownload, device *voltha.Device) (*voltha.ImageDownload, int, error) { |
| 77 | for pos, image := range device.ImageDownloads { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 78 | if image.Id == img.Id && image.Name == img.Name { |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 79 | return image, pos, nil |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 82 | return nil, -1, status.Errorf(codes.FailedPrecondition, "device-id:%s, image-not-registered:%s", |
| 83 | device.Id, img.Name) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 86 | func (agent *Agent) cancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 87 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 88 | return nil, err |
| 89 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 90 | logger.Debugw(ctx, "cancelImageDownload", log.Fields{"device-id": agent.deviceID}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 91 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 92 | // Update image download state |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 93 | cloned := agent.cloneDeviceWithoutLock() |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 94 | _, index, err := getImage(img, cloned) |
| 95 | if err != nil { |
| 96 | agent.requestQueue.RequestComplete() |
| 97 | return nil, err |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 100 | cloned.ImageDownloads[index].DownloadState = voltha.ImageDownload_DOWNLOAD_CANCELLED |
| 101 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 102 | if cloned.AdminState != voltha.AdminState_DOWNLOADING_IMAGE { |
| 103 | agent.requestQueue.RequestComplete() |
| 104 | } else { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 105 | // Set the device to Enabled |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 106 | cloned.AdminState = voltha.AdminState_ENABLED |
| 107 | if err := agent.updateDeviceAndReleaseLock(ctx, cloned); err != nil { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 108 | return nil, err |
| 109 | } |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 110 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 111 | ch, err := agent.adapterProxy.CancelImageDownload(subCtx, cloned, img) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 112 | if err != nil { |
| 113 | cancel() |
| 114 | return nil, err |
| 115 | } |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 116 | go agent.waitForAdapterResponse(subCtx, cancel, "cancelImageDownload", ch, agent.onImageSuccess, |
| 117 | agent.onImageFailure) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 118 | } |
| 119 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
| 120 | } |
| 121 | |
| 122 | func (agent *Agent) activateImage(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
| 123 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 124 | return nil, err |
| 125 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 126 | logger.Debugw(ctx, "activateImage", log.Fields{"device-id": agent.deviceID}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 127 | |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 128 | // Update image download state |
| 129 | cloned := agent.cloneDeviceWithoutLock() |
| 130 | image, index, err := getImage(img, cloned) |
| 131 | if err != nil { |
| 132 | agent.requestQueue.RequestComplete() |
| 133 | return nil, err |
| 134 | } |
| 135 | |
| 136 | if err != nil { |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 137 | agent.requestQueue.RequestComplete() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 138 | return nil, status.Errorf(codes.FailedPrecondition, "device-id:%s, image-not-registered:%s", agent.deviceID, img.Name) |
| 139 | } |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 140 | |
| 141 | if image.DownloadState != voltha.ImageDownload_DOWNLOAD_SUCCEEDED { |
| 142 | agent.requestQueue.RequestComplete() |
| 143 | return nil, status.Errorf(codes.FailedPrecondition, "device-id:%s, device-has-not-downloaded-image:%s", agent.deviceID, img.Name) |
| 144 | } |
| 145 | |
| 146 | //TODO does this need to be removed ? |
| 147 | if cloned.AdminState == voltha.AdminState_DOWNLOADING_IMAGE { |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 148 | agent.requestQueue.RequestComplete() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 149 | return nil, status.Errorf(codes.FailedPrecondition, "device-id:%s, device-in-downloading-state:%s", agent.deviceID, img.Name) |
| 150 | } |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 151 | |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 152 | // Save the image |
| 153 | cloned.ImageDownloads[index].ImageState = voltha.ImageDownload_IMAGE_ACTIVATING |
| 154 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 155 | cloned.AdminState = voltha.AdminState_DOWNLOADING_IMAGE |
| 156 | if err := agent.updateDeviceAndReleaseLock(ctx, cloned); err != nil { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 157 | return nil, err |
| 158 | } |
| 159 | |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 160 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 161 | ch, err := agent.adapterProxy.ActivateImageUpdate(subCtx, cloned, img) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 162 | if err != nil { |
| 163 | cancel() |
| 164 | return nil, err |
| 165 | } |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 166 | go agent.waitForAdapterResponse(subCtx, cancel, "activateImageUpdate", ch, agent.onImageSuccess, agent.onFailure) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 167 | |
| 168 | // The status of the AdminState will be changed following the update_download_status response from the adapter |
| 169 | // The image name will also be removed from the device list |
| 170 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
| 171 | } |
| 172 | |
| 173 | func (agent *Agent) revertImage(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
| 174 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 175 | return nil, err |
| 176 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 177 | logger.Debugw(ctx, "revertImage", log.Fields{"device-id": agent.deviceID}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 178 | |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 179 | // Update image download state |
| 180 | cloned := agent.cloneDeviceWithoutLock() |
| 181 | _, index, err := getImage(img, cloned) |
| 182 | if err != nil { |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 183 | agent.requestQueue.RequestComplete() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 184 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, image-not-registered:%s", agent.deviceID, img.Name) |
| 185 | } |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 186 | if cloned.AdminState != voltha.AdminState_ENABLED { |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 187 | agent.requestQueue.RequestComplete() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 188 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, device-not-enabled-state:%s", agent.deviceID, img.Name) |
| 189 | } |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 190 | |
| 191 | cloned.ImageDownloads[index].ImageState = voltha.ImageDownload_IMAGE_REVERTING |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 192 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 193 | if err := agent.updateDeviceAndReleaseLock(ctx, cloned); err != nil { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 194 | return nil, err |
| 195 | } |
| 196 | |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 197 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 198 | ch, err := agent.adapterProxy.RevertImageUpdate(subCtx, cloned, img) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 199 | if err != nil { |
| 200 | cancel() |
| 201 | return nil, err |
| 202 | } |
| 203 | go agent.waitForAdapterResponse(subCtx, cancel, "revertImageUpdate", ch, agent.onSuccess, agent.onFailure) |
| 204 | |
| 205 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
| 206 | } |
| 207 | |
| 208 | func (agent *Agent) getImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 209 | logger.Debugw(ctx, "getImageDownloadStatus", log.Fields{"device-id": agent.deviceID}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 210 | |
| 211 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 212 | return nil, err |
| 213 | } |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 214 | device := agent.getDeviceReadOnlyWithoutLock() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 215 | ch, err := agent.adapterProxy.GetImageDownloadStatus(ctx, device, img) |
| 216 | agent.requestQueue.RequestComplete() |
| 217 | if err != nil { |
| 218 | return nil, err |
| 219 | } |
| 220 | // Wait for the adapter response |
| 221 | rpcResponse, ok := <-ch |
| 222 | if !ok { |
| 223 | return nil, status.Errorf(codes.Aborted, "channel-closed-device-id-%s", agent.deviceID) |
| 224 | } |
| 225 | if rpcResponse.Err != nil { |
| 226 | return nil, rpcResponse.Err |
| 227 | } |
| 228 | // Successful response |
| 229 | imgDownload := &voltha.ImageDownload{} |
| 230 | if err := ptypes.UnmarshalAny(rpcResponse.Reply, imgDownload); err != nil { |
| 231 | return nil, status.Errorf(codes.InvalidArgument, "%s", err.Error()) |
| 232 | } |
| 233 | return imgDownload, nil |
| 234 | } |
| 235 | |
| 236 | func (agent *Agent) updateImageDownload(ctx context.Context, img *voltha.ImageDownload) error { |
| 237 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 238 | return err |
| 239 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 240 | logger.Debugw(ctx, "updating-image-download", log.Fields{"device-id": agent.deviceID, "img": img}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 241 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 242 | // Update the image as well as remove it if the download was cancelled |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 243 | cloned := agent.cloneDeviceWithoutLock() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 244 | clonedImages := make([]*voltha.ImageDownload, len(cloned.ImageDownloads)) |
| 245 | for _, image := range cloned.ImageDownloads { |
| 246 | if image.Id == img.Id && image.Name == img.Name { |
| 247 | if image.DownloadState != voltha.ImageDownload_DOWNLOAD_CANCELLED { |
| 248 | clonedImages = append(clonedImages, img) |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | cloned.ImageDownloads = clonedImages |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 253 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 254 | // Set the Admin state to enabled if required |
| 255 | if (img.DownloadState != voltha.ImageDownload_DOWNLOAD_REQUESTED && |
| 256 | img.DownloadState != voltha.ImageDownload_DOWNLOAD_STARTED) || |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 257 | img.ImageState != voltha.ImageDownload_IMAGE_ACTIVATING { |
| 258 | cloned.AdminState = voltha.AdminState_ENABLED |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 259 | } |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 260 | return agent.updateDeviceAndReleaseLock(ctx, cloned) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | func (agent *Agent) getImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 264 | logger.Debugw(ctx, "getImageDownload", log.Fields{"device-id": agent.deviceID}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 265 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 266 | device, err := agent.getDeviceReadOnly(ctx) |
| 267 | if err != nil { |
| 268 | return nil, status.Errorf(codes.Aborted, "%s", err) |
| 269 | } |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 270 | for _, image := range device.ImageDownloads { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 271 | if image.Id == img.Id && image.Name == img.Name { |
| 272 | return image, nil |
| 273 | } |
| 274 | } |
| 275 | return nil, status.Errorf(codes.NotFound, "image-not-found:%s", img.Name) |
| 276 | } |
| 277 | |
| 278 | func (agent *Agent) listImageDownloads(ctx context.Context, deviceID string) (*voltha.ImageDownloads, error) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 279 | logger.Debugw(ctx, "listImageDownloads", log.Fields{"device-id": agent.deviceID}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 280 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 281 | device, err := agent.getDeviceReadOnly(ctx) |
| 282 | if err != nil { |
| 283 | return nil, status.Errorf(codes.Aborted, "%s", err) |
| 284 | } |
| 285 | return &voltha.ImageDownloads{Items: device.ImageDownloads}, nil |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 286 | } |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 287 | |
| 288 | // onImageFailure brings back the device to Enabled state and sets the image to image download_failed. |
| 289 | func (agent *Agent) onImageFailure(ctx context.Context, rpc string, response interface{}, reqArgs ...interface{}) { |
| 290 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 291 | logger.Errorw(ctx, "can't obtain lock", log.Fields{"rpc": rpc, "device-id": agent.deviceID, "error": err, "args": reqArgs}) |
| 292 | return |
| 293 | } |
| 294 | if res, ok := response.(error); ok { |
| 295 | logger.Errorw(ctx, "rpc-failed", log.Fields{"rpc": rpc, "device-id": agent.deviceID, "error": res, "args": reqArgs}) |
| 296 | device := agent.cloneDeviceWithoutLock() |
| 297 | //TODO base this on IMAGE ID when created |
| 298 | var imageFailed *voltha.ImageDownload |
| 299 | var index int |
| 300 | if device.ImageDownloads != nil { |
| 301 | for pos, image := range device.ImageDownloads { |
| 302 | if image.DownloadState == voltha.ImageDownload_DOWNLOAD_REQUESTED || |
| 303 | image.ImageState == voltha.ImageDownload_IMAGE_ACTIVATING { |
| 304 | imageFailed = image |
| 305 | index = pos |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | if imageFailed == nil { |
| 311 | logger.Errorw(ctx, "can't find image", log.Fields{"rpc": rpc, "device-id": agent.deviceID, "args": reqArgs}) |
| 312 | return |
| 313 | } |
| 314 | |
| 315 | updatedImages := removeImage(device.ImageDownloads, index) |
| 316 | |
| 317 | // Save the image |
| 318 | clonedImg := proto.Clone(imageFailed).(*voltha.ImageDownload) |
| 319 | if imageFailed.DownloadState == voltha.ImageDownload_DOWNLOAD_REQUESTED { |
| 320 | clonedImg.DownloadState = voltha.ImageDownload_DOWNLOAD_FAILED |
| 321 | } else if imageFailed.ImageState == voltha.ImageDownload_IMAGE_ACTIVATING { |
| 322 | clonedImg.ImageState = voltha.ImageDownload_IMAGE_INACTIVE |
| 323 | } |
| 324 | cloned := agent.cloneDeviceWithoutLock() |
| 325 | cloned.ImageDownloads = append(updatedImages, clonedImg) |
| 326 | //Enabled is the only state we can go back to. |
| 327 | cloned.AdminState = voltha.AdminState_ENABLED |
| 328 | if err := agent.updateDeviceAndReleaseLock(ctx, cloned); err != nil { |
| 329 | logger.Errorw(ctx, "failed-enable-device-after-image-failure", |
| 330 | log.Fields{"rpc": rpc, "device-id": agent.deviceID, "error": res, "args": reqArgs}) |
| 331 | } |
| 332 | } else { |
| 333 | logger.Errorw(ctx, "rpc-failed-invalid-error", log.Fields{"rpc": rpc, "device-id": agent.deviceID, "args": reqArgs}) |
| 334 | } |
| 335 | // TODO: Post failure message onto kafka |
| 336 | } |
| 337 | |
| 338 | // onImageSuccess brings back the device to Enabled state and sets the image to image download_failed. |
| 339 | func (agent *Agent) onImageSuccess(ctx context.Context, rpc string, response interface{}, reqArgs ...interface{}) { |
| 340 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 341 | logger.Errorw(ctx, "can't obtain lock", log.Fields{"rpc": rpc, "device-id": agent.deviceID, "error": err, "args": reqArgs}) |
| 342 | return |
| 343 | } |
| 344 | logger.Errorw(ctx, "rpc-successful", log.Fields{"rpc": rpc, "device-id": agent.deviceID, "response": response, "args": reqArgs}) |
| 345 | device := agent.cloneDeviceWithoutLock() |
| 346 | //TODO base this on IMAGE ID when created |
| 347 | var imageSucceeded *voltha.ImageDownload |
| 348 | var index int |
| 349 | if device.ImageDownloads != nil { |
| 350 | for pos, image := range device.ImageDownloads { |
| 351 | if image.DownloadState == voltha.ImageDownload_DOWNLOAD_REQUESTED || |
| 352 | image.ImageState == voltha.ImageDownload_IMAGE_ACTIVATING { |
| 353 | imageSucceeded = image |
| 354 | index = pos |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | if imageSucceeded == nil { |
| 360 | logger.Errorw(ctx, "can't find image", log.Fields{"rpc": rpc, "device-id": agent.deviceID, "args": reqArgs}) |
| 361 | return |
| 362 | } |
| 363 | |
| 364 | updatedImages := removeImage(device.ImageDownloads, index) |
| 365 | |
| 366 | // Save the image |
| 367 | clonedImg := proto.Clone(imageSucceeded).(*voltha.ImageDownload) |
| 368 | if imageSucceeded.DownloadState == voltha.ImageDownload_DOWNLOAD_REQUESTED { |
| 369 | clonedImg.DownloadState = voltha.ImageDownload_DOWNLOAD_SUCCEEDED |
| 370 | } else if imageSucceeded.ImageState == voltha.ImageDownload_IMAGE_ACTIVATING { |
| 371 | clonedImg.ImageState = voltha.ImageDownload_IMAGE_ACTIVE |
| 372 | |
| 373 | } |
| 374 | cloned := agent.cloneDeviceWithoutLock() |
| 375 | cloned.ImageDownloads = append(updatedImages, clonedImg) |
| 376 | |
| 377 | //Enabled is the only state we can go back to. |
| 378 | cloned.AdminState = voltha.AdminState_ENABLED |
| 379 | if err := agent.updateDeviceAndReleaseLock(ctx, cloned); err != nil { |
| 380 | logger.Errorw(ctx, "failed-enable-device-after-image-download-success", |
| 381 | log.Fields{"rpc": rpc, "device-id": agent.deviceID, "response": response, "args": reqArgs}) |
| 382 | } |
| 383 | |
| 384 | } |
| 385 | |
| 386 | func removeImage(s []*voltha.ImageDownload, i int) []*voltha.ImageDownload { |
| 387 | s[i] = s[len(s)-1] |
| 388 | // We do not need to put s[i] at the end, as it will be discarded anyway |
| 389 | return s[:len(s)-1] |
| 390 | } |