mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 17 | //Package swupg provides the utilities for onu sw upgrade |
| 18 | package swupg |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 19 | |
| 20 | import ( |
| 21 | "context" |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 22 | "encoding/binary" |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 23 | "fmt" |
| 24 | "strconv" |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 25 | "sync" |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 26 | "time" |
| 27 | |
| 28 | "github.com/boguslaw-wojcik/crc32a" |
| 29 | "github.com/looplab/fsm" |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 30 | "github.com/opencord/omci-lib-go/v2" |
| 31 | me "github.com/opencord/omci-lib-go/v2/generated" |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 32 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 33 | cmn "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/common" |
| 34 | "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/devdb" |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 35 | "github.com/opencord/voltha-protos/v5/go/voltha" |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 36 | ) |
| 37 | |
| 38 | const cMaxUint32 = ^uint32(0) |
| 39 | |
| 40 | const ( |
| 41 | // internal predefined values - some off them should later be configurable (perhaps with theses as defaults) |
| 42 | cOmciDownloadSectionSize = 31 //in bytes |
| 43 | cOmciDownloadWindowSizeLimit = 31 //in sections for window offset (windowSize(32)-1) |
| 44 | //cOmciDownloadWindowRetryMax = 2 // max attempts for a specific window |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 45 | cOmciSectionInterleaveMilliseconds = 0 //DownloadSection interleave time in milliseconds (0 for no delay) |
| 46 | cOmciEndSwDlDelaySeconds = 1 //End Software Download delay after last section (may be also configurable?) |
| 47 | cWaitCountEndSwDl = 6 //maximum number of EndSwDl requests |
| 48 | cWaitDelayEndSwDlSeconds = 10 //duration, how long is waited before next request on EndSwDl |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 49 | //cOmciDownloadCompleteTimeout = 5400 //in s for the complete timeout (may be better scale to image size/ noOfWindows) |
| 50 | ) |
| 51 | |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 52 | // tEndSwDlResponseResult - Response result from EndSwDownload as used in channel indication |
| 53 | type tUpgradePhase uint8 |
| 54 | |
| 55 | const ( |
| 56 | // undefined phase |
| 57 | cUpgradeUndefined tUpgradePhase = iota |
| 58 | // downloading image |
| 59 | cUpgradeDownloading |
| 60 | // image downloaded |
| 61 | cUpgradeDownloaded |
| 62 | // activating image |
| 63 | cUpgradeActivating |
| 64 | // image activated |
| 65 | cUpgradeActivated |
| 66 | // committing image |
| 67 | cUpgradeCommitting |
| 68 | // image committed |
| 69 | cUpgradeCommitted |
| 70 | ) |
| 71 | |
| 72 | // tEndSwDlResponseResult - Response result from EndSwDownload as used in channel indication |
| 73 | type tEndSwDlResponseResult uint8 |
| 74 | |
| 75 | const ( |
| 76 | // response success |
| 77 | cEndSwDlResponseSuccess tEndSwDlResponseResult = iota |
| 78 | // response busy (repeat) |
| 79 | cEndSwDlResponseBusy |
| 80 | // response error or abort waiting for response |
| 81 | cEndSwDlResponseAbort |
| 82 | ) |
| 83 | |
| 84 | // upgrade FSM related events |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 85 | const ( |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 86 | UpgradeEvStart = "UpgradeEvStart" |
| 87 | UpgradeEvDisable = "UpgradeEvDisable" |
| 88 | UpgradeEvAdapterDownload = "UpgradeEvAdapterDownload" |
| 89 | UpgradeEvPrepareSwDownload = "UpgradeEvPrepareSwDownload" |
| 90 | UpgradeEvRxStartSwDownload = "UpgradeEvRxStartSwDownload" |
| 91 | UpgradeEvWaitWindowAck = "UpgradeEvWaitWindowAck" |
| 92 | UpgradeEvContinueNextWindow = "UpgradeEvContinueNextWindow" |
| 93 | UpgradeEvEndSwDownload = "UpgradeEvEndSwDownload" |
| 94 | UpgradeEvWaitEndDownload = "UpgradeEvWaitEndDownload" |
| 95 | UpgradeEvContinueFinalize = "UpgradeEvContinueFinalize" |
| 96 | UpgradeEvCheckImageName = "UpgradeEvCheckImageName" |
| 97 | UpgradeEvWaitForActivate = "UpgradeEvWaitForActivate" |
| 98 | UpgradeEvRequestActivate = "UpgradeEvRequestActivate" |
| 99 | UpgradeEvActivationDone = "UpgradeEvActivationDone" |
| 100 | UpgradeEvWaitForCommit = "UpgradeEvWaitForCommit" |
| 101 | UpgradeEvCommitSw = "UpgradeEvCommitSw" |
| 102 | UpgradeEvCheckCommitted = "UpgradeEvCheckCommitted" |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 103 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 104 | //UpgradeEvTimeoutSimple = "UpgradeEvTimeoutSimple" |
| 105 | //UpgradeEvTimeoutMids = "UpgradeEvTimeoutMids" |
| 106 | UpgradeEvReset = "UpgradeEvReset" |
| 107 | UpgradeEvAbort = "UpgradeEvAbort" |
| 108 | UpgradeEvRestart = "UpgradeEvRestart" |
| 109 | UpgradeEvAbortSwDownload = "UpgradeEvAbortSwDownload" |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 110 | ) |
| 111 | |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 112 | // upgrade FSM related states |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 113 | const ( |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 114 | UpgradeStDisabled = "UpgradeStDisabled" |
| 115 | UpgradeStStarting = "UpgradeStStarting" |
| 116 | UpgradeStWaitingAdapterDL = "UpgradeStWaitingAdapterDL" |
| 117 | UpgradeStPreparingDL = "UpgradeStPreparingDL" |
| 118 | UpgradeStDLSection = "UpgradeStDLSection" |
| 119 | UpgradeStVerifyWindow = "UpgradeStVerifyWindow" |
| 120 | UpgradeStFinalizeDL = "UpgradeStFinalizeDL" |
| 121 | UpgradeStWaitEndDL = "UpgradeStWaitEndDL" |
| 122 | UpgradeStCheckImageName = "UpgradeStCheckImageName" |
| 123 | UpgradeStWaitForActivate = "UpgradeStWaitForActivate" |
| 124 | UpgradeStRequestingActivate = "UpgradeStRequestingActivate" |
| 125 | UpgradeStActivated = "UpgradeStActivated" |
| 126 | UpgradeStWaitForCommit = "UpgradeStWaitForCommit" |
| 127 | UpgradeStCommitSw = "UpgradeStCommitSw" |
| 128 | UpgradeStCheckCommitted = "UpgradeStCheckCommitted" |
| 129 | UpgradeStResetting = "UpgradeStResetting" |
| 130 | UpgradeStRestarting = "UpgradeStRestarting" |
| 131 | UpgradeStAbortingDL = "UpgradeStAbortingDL" |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 132 | ) |
| 133 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 134 | //COnuUpgradeFsmIdleState - required definition for IdleState detection for activities on OMCI |
| 135 | const COnuUpgradeFsmIdleState = UpgradeStWaitForCommit |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 136 | |
| 137 | //OnuUpgradeFsm defines the structure for the state machine to config the PON ANI ports of ONU UNI ports via OMCI |
| 138 | type OnuUpgradeFsm struct { |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 139 | pDeviceHandler cmn.IdeviceHandler |
| 140 | pDownloadManager *AdapterDownloadManager |
| 141 | pFileManager *FileDownloadManager //used from R2.8 with new API version |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 142 | deviceID string |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 143 | pDevEntry cmn.IonuDeviceEntry |
| 144 | pOmciCC *cmn.OmciCC |
| 145 | pOnuDB *devdb.OnuDeviceDB |
| 146 | requestEvent cmn.OnuDeviceEvent |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 147 | //omciMIdsResponseReceived chan bool //seperate channel needed for checking multiInstance OMCI message responses |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 148 | PAdaptFsm *cmn.AdapterFsm |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 149 | pImageDsc *voltha.ImageDownload |
| 150 | imageBuffer []byte |
| 151 | origImageLength uint32 //as also limited by OMCI |
| 152 | imageCRC uint32 //as per OMCI - ITU I.363.5 crc |
| 153 | imageLength uint32 //including last bytes padding |
| 154 | omciDownloadWindowSizeLimit uint8 //windowSize-1 in sections |
| 155 | omciDownloadWindowSizeLast uint8 //number of sections in last window |
| 156 | noOfSections uint32 //uint32 range for sections should be sufficient for very long images |
| 157 | nextDownloadSectionsAbsolute uint32 //number of next section to download in overall image |
| 158 | nextDownloadSectionsWindow uint8 //number of next section to download within current window |
| 159 | noOfWindows uint32 //uint32 range for windows should be sufficient for very long images |
| 160 | nextDownloadWindow uint32 //number of next window to download |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 161 | InactiveImageMeID uint16 //ME-ID of the inactive image |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 162 | downloadToOnuTimeout4MB time.Duration //timeout for downloading the image to the ONU for a 4MB image slice |
| 163 | omciSectionInterleaveDelay time.Duration //DownloadSectionInterleave delay in milliseconds |
| 164 | delayEndSwDl bool //flag to provide a delay between last section and EndSwDl |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 165 | repeatAbort bool //flag to indicate if OMCI EndSwDownload (abort) is to be repeated |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 166 | pLastTxMeInstance *me.ManagedEntity |
| 167 | waitCountEndSwDl uint8 //number, how often is waited for EndSwDl at maximum |
| 168 | waitDelayEndSwDl time.Duration //duration, how long is waited before next request on EndSwDl |
| 169 | chReceiveExpectedResponse chan bool |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 170 | useAPIVersion43 bool //flag for indication on which API version is used (and accordingly which specific methods) |
| 171 | mutexUpgradeParams sync.RWMutex //mutex to protect members for parallel function requests and omci response processing |
| 172 | imageVersion string //name of the image as used within OMCI (and on extrenal API interface) |
| 173 | imageIdentifier string //name of the image as used in the adapter |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 174 | mutexIsAwaitingAdapterDlResponse sync.RWMutex |
| 175 | chAdapterDlReady chan bool |
mpagenko | 609a69a | 2021-12-02 08:12:58 +0000 | [diff] [blame] | 176 | chAbortDelayEndSwDl chan struct{} |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 177 | isWaitingForAdapterDlResponse bool |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 178 | chOnuDlReady chan bool |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 179 | activateImage bool |
| 180 | commitImage bool |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 181 | mutexAbortRequest sync.RWMutex |
| 182 | abortRequested voltha.ImageState_ImageFailureReason |
| 183 | conditionalCancelRequested bool |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 184 | upgradePhase tUpgradePhase |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 185 | volthaDownloadState voltha.ImageState_ImageDownloadState |
| 186 | volthaDownloadReason voltha.ImageState_ImageFailureReason |
| 187 | volthaImageState voltha.ImageState_ImageActivationState |
Holger Hildebrandt | 288d947 | 2021-09-06 10:47:53 +0000 | [diff] [blame] | 188 | isEndSwDlOpen bool |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 189 | chReceiveAbortEndSwDlResponse chan tEndSwDlResponseResult |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | //NewOnuUpgradeFsm is the 'constructor' for the state machine to config the PON ANI ports |
| 193 | // of ONU UNI ports via OMCI |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 194 | func NewOnuUpgradeFsm(ctx context.Context, apDeviceHandler cmn.IdeviceHandler, |
| 195 | apDevEntry cmn.IonuDeviceEntry, apOnuDB *devdb.OnuDeviceDB, |
| 196 | aRequestEvent cmn.OnuDeviceEvent, aName string, aCommChannel chan cmn.Message) *OnuUpgradeFsm { |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 197 | instFsm := &OnuUpgradeFsm{ |
mpagenko | 59498c1 | 2021-03-18 14:15:15 +0000 | [diff] [blame] | 198 | pDeviceHandler: apDeviceHandler, |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 199 | deviceID: apDeviceHandler.GetDeviceID(), |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 200 | pDevEntry: apDevEntry, |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 201 | pOmciCC: apDevEntry.GetDevOmciCC(), |
mpagenko | 59498c1 | 2021-03-18 14:15:15 +0000 | [diff] [blame] | 202 | pOnuDB: apOnuDB, |
| 203 | requestEvent: aRequestEvent, |
| 204 | omciDownloadWindowSizeLimit: cOmciDownloadWindowSizeLimit, |
| 205 | omciSectionInterleaveDelay: cOmciSectionInterleaveMilliseconds, |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 206 | downloadToOnuTimeout4MB: apDeviceHandler.GetDlToOnuTimeout4M(), |
mpagenko | 59498c1 | 2021-03-18 14:15:15 +0000 | [diff] [blame] | 207 | waitCountEndSwDl: cWaitCountEndSwDl, |
| 208 | waitDelayEndSwDl: cWaitDelayEndSwDlSeconds, |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 209 | upgradePhase: cUpgradeUndefined, |
| 210 | volthaDownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 211 | volthaDownloadReason: voltha.ImageState_NO_ERROR, |
| 212 | volthaImageState: voltha.ImageState_IMAGE_UNKNOWN, |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 213 | abortRequested: voltha.ImageState_NO_ERROR, |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 214 | } |
mpagenko | 59498c1 | 2021-03-18 14:15:15 +0000 | [diff] [blame] | 215 | instFsm.chReceiveExpectedResponse = make(chan bool) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 216 | instFsm.chAdapterDlReady = make(chan bool) |
mpagenko | 609a69a | 2021-12-02 08:12:58 +0000 | [diff] [blame] | 217 | instFsm.chAbortDelayEndSwDl = make(chan struct{}) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 218 | instFsm.chOnuDlReady = make(chan bool) |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 219 | instFsm.chReceiveAbortEndSwDlResponse = make(chan tEndSwDlResponseResult) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 220 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 221 | instFsm.PAdaptFsm = cmn.NewAdapterFsm(aName, instFsm.deviceID, aCommChannel) |
| 222 | if instFsm.PAdaptFsm == nil { |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 223 | logger.Errorw(ctx, "OnuUpgradeFsm's AdapterFsm could not be instantiated!!", log.Fields{ |
| 224 | "device-id": instFsm.deviceID}) |
| 225 | return nil |
| 226 | } |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 227 | instFsm.PAdaptFsm.PFsm = fsm.NewFSM( |
| 228 | UpgradeStDisabled, |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 229 | fsm.Events{ |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 230 | {Name: UpgradeEvStart, Src: []string{UpgradeStDisabled}, Dst: UpgradeStStarting}, |
| 231 | {Name: UpgradeEvAdapterDownload, Src: []string{UpgradeStStarting}, Dst: UpgradeStWaitingAdapterDL}, |
| 232 | {Name: UpgradeEvPrepareSwDownload, Src: []string{UpgradeStStarting, UpgradeStWaitingAdapterDL}, Dst: UpgradeStPreparingDL}, |
| 233 | {Name: UpgradeEvRxStartSwDownload, Src: []string{UpgradeStPreparingDL}, Dst: UpgradeStDLSection}, |
| 234 | {Name: UpgradeEvWaitWindowAck, Src: []string{UpgradeStDLSection}, Dst: UpgradeStVerifyWindow}, |
| 235 | {Name: UpgradeEvContinueNextWindow, Src: []string{UpgradeStVerifyWindow}, Dst: UpgradeStDLSection}, |
| 236 | {Name: UpgradeEvEndSwDownload, Src: []string{UpgradeStVerifyWindow}, Dst: UpgradeStFinalizeDL}, |
| 237 | {Name: UpgradeEvWaitEndDownload, Src: []string{UpgradeStFinalizeDL}, Dst: UpgradeStWaitEndDL}, |
| 238 | {Name: UpgradeEvContinueFinalize, Src: []string{UpgradeStWaitEndDL}, Dst: UpgradeStFinalizeDL}, |
| 239 | //UpgradeStCheckImageName only used with useAPIVersion43 |
| 240 | {Name: UpgradeEvCheckImageName, Src: []string{UpgradeStWaitEndDL}, Dst: UpgradeStCheckImageName}, |
| 241 | //UpgradeEvWaitForActivate state transitions depend on useAPIVersion43 |
| 242 | {Name: UpgradeEvWaitForActivate, Src: []string{UpgradeStWaitEndDL, UpgradeStCheckImageName}, Dst: UpgradeStWaitForActivate}, |
| 243 | //UpgradeEvRequestActivate state transitions depend on useAPIVersion43 |
| 244 | {Name: UpgradeEvRequestActivate, Src: []string{UpgradeStStarting, UpgradeStWaitEndDL, UpgradeStCheckImageName, |
| 245 | UpgradeStWaitForActivate}, Dst: UpgradeStRequestingActivate}, //allows also for direct activation (without download) [TODO!!!] |
| 246 | {Name: UpgradeEvActivationDone, Src: []string{UpgradeStRequestingActivate}, Dst: UpgradeStActivated}, |
| 247 | {Name: UpgradeEvWaitForCommit, Src: []string{UpgradeStRequestingActivate}, Dst: UpgradeStWaitForCommit}, |
| 248 | {Name: UpgradeEvCommitSw, Src: []string{UpgradeStStarting, UpgradeStRequestingActivate, UpgradeStWaitForCommit, |
| 249 | UpgradeStActivated}, Dst: UpgradeStCommitSw}, //allows also for direct commitment (without download) [TODO!!!] |
| 250 | {Name: UpgradeEvCheckCommitted, Src: []string{UpgradeStCommitSw}, Dst: UpgradeStCheckCommitted}, |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 251 | |
| 252 | /* |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 253 | {Name: UpgradeEvTimeoutSimple, Src: []string{ |
| 254 | UpgradeStCreatingDot1PMapper, UpgradeStCreatingMBPCD, UpgradeStSettingTconts, UpgradeStSettingDot1PMapper}, Dst: UpgradeStStarting}, |
| 255 | {Name: UpgradeEvTimeoutMids, Src: []string{ |
| 256 | UpgradeStCreatingGemNCTPs, UpgradeStCreatingGemIWs, UpgradeStSettingPQs}, Dst: UpgradeStStarting}, |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 257 | */ |
| 258 | // exceptional treatments |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 259 | //on UpgradeEvReset: UpgradeStRequestingActivate, UpgradeStWaitForCommit and UpgradeStActivated are not reset |
mpagenko | 1f8e882 | 2021-06-25 14:10:21 +0000 | [diff] [blame] | 260 | // (to let the FSM survive the expected OnuDown indication) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 261 | {Name: UpgradeEvReset, Src: []string{UpgradeStStarting, UpgradeStWaitingAdapterDL, UpgradeStPreparingDL, UpgradeStDLSection, |
| 262 | UpgradeStVerifyWindow, UpgradeStDLSection, UpgradeStFinalizeDL, UpgradeStWaitEndDL, UpgradeStCheckImageName, |
| 263 | UpgradeStWaitForActivate, |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 264 | UpgradeStCommitSw, UpgradeStCheckCommitted, UpgradeStAbortingDL}, |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 265 | Dst: UpgradeStResetting}, |
| 266 | {Name: UpgradeEvAbort, Src: []string{UpgradeStStarting, UpgradeStWaitingAdapterDL, UpgradeStPreparingDL, UpgradeStDLSection, |
| 267 | UpgradeStVerifyWindow, UpgradeStDLSection, UpgradeStFinalizeDL, UpgradeStWaitEndDL, UpgradeStCheckImageName, |
| 268 | UpgradeStWaitForActivate, |
| 269 | UpgradeStRequestingActivate, UpgradeStActivated, UpgradeStWaitForCommit, |
| 270 | UpgradeStCommitSw, UpgradeStCheckCommitted}, |
| 271 | Dst: UpgradeStResetting}, |
| 272 | {Name: UpgradeEvAbortSwDownload, Src: []string{UpgradeStResetting}, Dst: UpgradeStAbortingDL}, |
| 273 | {Name: UpgradeEvRestart, Src: []string{UpgradeStResetting, UpgradeStAbortingDL}, Dst: UpgradeStRestarting}, |
| 274 | {Name: UpgradeEvDisable, Src: []string{UpgradeStRestarting}, Dst: UpgradeStDisabled}, |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 275 | }, |
| 276 | fsm.Callbacks{ |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 277 | "enter_state": func(e *fsm.Event) { instFsm.PAdaptFsm.LogFsmStateChange(ctx, e) }, |
| 278 | "enter_" + UpgradeStStarting: func(e *fsm.Event) { instFsm.enterStarting(ctx, e) }, |
| 279 | "enter_" + UpgradeStWaitingAdapterDL: func(e *fsm.Event) { instFsm.enterWaitingAdapterDL(ctx, e) }, |
| 280 | "enter_" + UpgradeStPreparingDL: func(e *fsm.Event) { instFsm.enterPreparingDL(ctx, e) }, |
| 281 | "enter_" + UpgradeStDLSection: func(e *fsm.Event) { instFsm.enterDownloadSection(ctx, e) }, |
| 282 | "enter_" + UpgradeStVerifyWindow: func(e *fsm.Event) { instFsm.enterVerifyWindow(ctx, e) }, |
| 283 | "enter_" + UpgradeStFinalizeDL: func(e *fsm.Event) { instFsm.enterFinalizeDL(ctx, e) }, |
| 284 | "enter_" + UpgradeStWaitEndDL: func(e *fsm.Event) { instFsm.enterWaitEndDL(ctx, e) }, |
| 285 | "enter_" + UpgradeStCheckImageName: func(e *fsm.Event) { instFsm.enterCheckImageName(ctx, e) }, |
| 286 | "enter_" + UpgradeStRequestingActivate: func(e *fsm.Event) { instFsm.enterActivateSw(ctx, e) }, |
| 287 | "enter_" + UpgradeStCommitSw: func(e *fsm.Event) { instFsm.enterCommitSw(ctx, e) }, |
| 288 | "enter_" + UpgradeStCheckCommitted: func(e *fsm.Event) { instFsm.enterCheckCommitted(ctx, e) }, |
| 289 | "enter_" + UpgradeStResetting: func(e *fsm.Event) { instFsm.enterResetting(ctx, e) }, |
| 290 | "enter_" + UpgradeStAbortingDL: func(e *fsm.Event) { instFsm.enterAbortingDL(ctx, e) }, |
| 291 | "enter_" + UpgradeStRestarting: func(e *fsm.Event) { instFsm.enterRestarting(ctx, e) }, |
| 292 | "enter_" + UpgradeStDisabled: func(e *fsm.Event) { instFsm.enterDisabled(ctx, e) }, |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 293 | }, |
| 294 | ) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 295 | if instFsm.PAdaptFsm.PFsm == nil { |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 296 | logger.Errorw(ctx, "OnuUpgradeFsm's Base FSM could not be instantiated!!", log.Fields{ |
| 297 | "device-id": instFsm.deviceID}) |
| 298 | return nil |
| 299 | } |
| 300 | |
| 301 | logger.Debugw(ctx, "OnuUpgradeFsm created", log.Fields{"device-id": instFsm.deviceID}) |
| 302 | return instFsm |
| 303 | } |
| 304 | |
| 305 | //SetDownloadParams configures the needed parameters for a specific download to the ONU |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 306 | // called from 'old' API Activate_image_update() |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 307 | func (oFsm *OnuUpgradeFsm) SetDownloadParams(ctx context.Context, aInactiveImageID uint16, |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 308 | apImageDsc *voltha.ImageDownload, apDownloadManager *AdapterDownloadManager) error { |
| 309 | pBaseFsm := oFsm.PAdaptFsm.PFsm |
| 310 | if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) { |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 311 | oFsm.mutexUpgradeParams.Lock() |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 312 | logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting", log.Fields{ |
| 313 | "device-id": oFsm.deviceID, "image-description": apImageDsc}) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 314 | oFsm.InactiveImageMeID = aInactiveImageID //upgrade state machines run on configured inactive ImageId |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 315 | oFsm.pImageDsc = apImageDsc |
| 316 | oFsm.pDownloadManager = apDownloadManager |
Holger Hildebrandt | ac1e059 | 2021-06-03 15:16:49 +0000 | [diff] [blame] | 317 | oFsm.activateImage = true |
| 318 | oFsm.commitImage = true |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 319 | oFsm.mutexUpgradeParams.Unlock() |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 320 | |
| 321 | go func(aPBaseFsm *fsm.FSM) { |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 322 | // let the upgrade FSM proceed to PreparingDL |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 323 | _ = aPBaseFsm.Event(UpgradeEvPrepareSwDownload) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 324 | }(pBaseFsm) |
| 325 | return nil |
| 326 | } |
| 327 | logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{ |
| 328 | "device-id": oFsm.deviceID}) |
| 329 | return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID)) |
| 330 | } |
| 331 | |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 332 | //SetDownloadParamsAfterDownload configures the needed parameters for a specific download to the ONU according to |
| 333 | // updated API interface with R2.8: start download to ONU if the image is downloaded to the adapter |
| 334 | // called from 'new' API Download_onu_image |
| 335 | func (oFsm *OnuUpgradeFsm) SetDownloadParamsAfterDownload(ctx context.Context, aInactiveImageID uint16, |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 336 | apImageRequest *voltha.DeviceImageDownloadRequest, apDownloadManager *FileDownloadManager, |
Holger Hildebrandt | ac01073 | 2021-06-02 13:35:39 +0000 | [diff] [blame] | 337 | aImageIdentifier string) error { |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 338 | oFsm.mutexUpgradeParams.Lock() |
| 339 | var pBaseFsm *fsm.FSM = nil |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 340 | if oFsm.PAdaptFsm != nil { |
| 341 | pBaseFsm = oFsm.PAdaptFsm.PFsm |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 342 | } |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 343 | if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) { |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 344 | logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting", log.Fields{ |
| 345 | "device-id": oFsm.deviceID, "image-description": apImageRequest}) |
| 346 | oFsm.useAPIVersion43 = true |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 347 | oFsm.InactiveImageMeID = aInactiveImageID //upgrade state machines run on configured inactive ImageId |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 348 | oFsm.pFileManager = apDownloadManager |
| 349 | oFsm.imageIdentifier = aImageIdentifier |
| 350 | oFsm.imageVersion = apImageRequest.Image.Version |
| 351 | oFsm.activateImage = apImageRequest.ActivateOnSuccess |
| 352 | oFsm.commitImage = apImageRequest.CommitOnSuccess |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 353 | oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_STARTED //state change indication for download request |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 354 | oFsm.mutexUpgradeParams.Unlock() |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 355 | _ = pBaseFsm.Event(UpgradeEvAdapterDownload) //no need to call the FSM event in background here |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 356 | return nil |
| 357 | } |
| 358 | oFsm.mutexUpgradeParams.Unlock() |
| 359 | logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{ |
| 360 | "device-id": oFsm.deviceID}) |
| 361 | return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID)) |
| 362 | } |
| 363 | |
| 364 | //SetActivationParamsRunning sets the activate and commit flags for a running download to the ONU according to adapters rpc call |
| 365 | // called from 'new' API Activate_onu_image |
| 366 | func (oFsm *OnuUpgradeFsm) SetActivationParamsRunning(ctx context.Context, |
| 367 | aImageIdentifier string, aCommit bool) error { |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 368 | logger.Debugw(ctx, "OnuUpgradeFsm activate/commit parameter setting", log.Fields{ |
| 369 | "device-id": oFsm.deviceID, "image-id": aImageIdentifier, "commit": aCommit}) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 370 | oFsm.mutexUpgradeParams.Lock() |
| 371 | //set activate/commit independent from state, if FSM is already beyond concerned states, then it does not matter anyway |
| 372 | // (as long as the Imageidentifier is correct) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 373 | if aImageIdentifier != oFsm.imageIdentifier { |
| 374 | logger.Errorw(ctx, "OnuUpgradeFsm abort: mismatching upgrade image", log.Fields{ |
| 375 | "device-id": oFsm.deviceID, "request-image": aImageIdentifier, "fsm-image": oFsm.imageIdentifier}) |
| 376 | oFsm.mutexUpgradeParams.Unlock() |
| 377 | return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm params ignored: requested image-name not used in current upgrade for device-id: %s", |
| 378 | oFsm.deviceID)) |
| 379 | } |
| 380 | oFsm.activateImage = true |
| 381 | oFsm.commitImage = aCommit |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 382 | oFsm.mutexUpgradeParams.Unlock() |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 383 | var pBaseFsm *fsm.FSM = nil |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 384 | if oFsm.PAdaptFsm != nil { |
| 385 | pBaseFsm = oFsm.PAdaptFsm.PFsm |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 386 | } |
| 387 | if pBaseFsm != nil { |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 388 | if pBaseFsm.Is(UpgradeStWaitForActivate) { |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 389 | logger.Debugw(ctx, "OnuUpgradeFsm finish waiting for activate", log.Fields{"device-id": oFsm.deviceID}) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 390 | _ = pBaseFsm.Event(UpgradeEvRequestActivate) //no need to call the FSM event in background here |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 391 | } else { |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 392 | logger.Debugw(ctx, "OnuUpgradeFsm not (yet?) waiting for activate", log.Fields{ |
| 393 | "device-id": oFsm.deviceID, "current FsmState": pBaseFsm.Current()}) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 394 | } |
| 395 | return nil |
| 396 | } |
| 397 | logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer", log.Fields{ |
| 398 | "device-id": oFsm.deviceID}) |
| 399 | return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer for device-id: %s", oFsm.deviceID)) |
| 400 | } |
| 401 | |
| 402 | //SetActivationParamsStart starts upgrade processing with immediate activation |
| 403 | // called from 'new' API Activate_onu_image |
| 404 | func (oFsm *OnuUpgradeFsm) SetActivationParamsStart(ctx context.Context, aImageVersion string, aInactiveImageID uint16, aCommit bool) error { |
| 405 | oFsm.mutexUpgradeParams.Lock() |
| 406 | var pBaseFsm *fsm.FSM = nil |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 407 | if oFsm.PAdaptFsm != nil { |
| 408 | pBaseFsm = oFsm.PAdaptFsm.PFsm |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 409 | } |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 410 | if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) { |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 411 | logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting to start with activation", log.Fields{ |
| 412 | "device-id": oFsm.deviceID, "image-version": aImageVersion}) |
| 413 | oFsm.useAPIVersion43 = true |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 414 | oFsm.InactiveImageMeID = aInactiveImageID //upgrade state machines run on configured inactive ImageId |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 415 | oFsm.imageVersion = aImageVersion |
| 416 | oFsm.activateImage = true |
| 417 | oFsm.commitImage = aCommit |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 418 | // indicate start of the upgrade activity |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 419 | oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATING //state change indication for activate request |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 420 | oFsm.mutexUpgradeParams.Unlock() |
| 421 | //directly request the FSM to activate the image |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 422 | _ = pBaseFsm.Event(UpgradeEvRequestActivate) //no need to call the FSM event in background here |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 423 | return nil |
| 424 | } |
| 425 | oFsm.mutexUpgradeParams.Unlock() |
| 426 | logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{ |
| 427 | "device-id": oFsm.deviceID}) |
| 428 | return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID)) |
| 429 | } |
| 430 | |
| 431 | //SetCommitmentParamsRunning sets the commit flag for a running download to the ONU according to adapters rpc call |
| 432 | // called from 'new' API Commit_onu_image |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 433 | func (oFsm *OnuUpgradeFsm) SetCommitmentParamsRunning(ctx context.Context, |
| 434 | aImageIdentifier string, aImageVersion string) error { |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 435 | oFsm.mutexUpgradeParams.Lock() |
| 436 | //set commit independent from state, if FSM is already beyond commit state (just ready), then it does not matter anyway |
| 437 | // (as long as the Imageidentifier is correct) |
| 438 | logger.Debugw(ctx, "OnuUpgradeFsm commit parameter setting", log.Fields{ |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 439 | "device-id": oFsm.deviceID, "image-id": aImageIdentifier, "image-version": aImageVersion}) |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 440 | if (aImageIdentifier != oFsm.imageIdentifier) && (aImageVersion != oFsm.imageVersion) { |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 441 | logger.Errorw(ctx, "OnuUpgradeFsm abort: mismatching upgrade image", log.Fields{ |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 442 | "device-id": oFsm.deviceID, "request-identifier": aImageIdentifier, "fsm-identifier": oFsm.imageIdentifier, |
| 443 | "request-version": aImageVersion, "fsm-version": oFsm.imageVersion}) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 444 | oFsm.mutexUpgradeParams.Unlock() |
| 445 | return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm params ignored: requested image-name not used in current upgrade for device-id: %s", |
| 446 | oFsm.deviceID)) |
| 447 | } |
| 448 | oFsm.commitImage = true |
| 449 | oFsm.mutexUpgradeParams.Unlock() |
| 450 | var pBaseFsm *fsm.FSM = nil |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 451 | if oFsm.PAdaptFsm != nil { |
| 452 | pBaseFsm = oFsm.PAdaptFsm.PFsm |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 453 | } |
| 454 | if pBaseFsm != nil { |
mpagenko | 183647c | 2021-06-08 15:25:04 +0000 | [diff] [blame] | 455 | //let the FSM decide if it is ready to process the event |
| 456 | logger.Debugw(ctx, "OnuUpgradeFsm requesting commit", |
| 457 | log.Fields{"device-id": oFsm.deviceID, "current FsmState": pBaseFsm.Current()}) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 458 | _ = pBaseFsm.Event(UpgradeEvCommitSw) //no need to call the FSM event in background here |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 459 | return nil |
| 460 | } |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 461 | //should never occur |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 462 | logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer", log.Fields{ |
| 463 | "device-id": oFsm.deviceID}) |
| 464 | return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer for device-id: %s", oFsm.deviceID)) |
| 465 | } |
| 466 | |
| 467 | //SetCommitmentParamsStart starts upgrade processing with immediate commitment |
| 468 | // called from 'new' API Commit_onu_image |
| 469 | func (oFsm *OnuUpgradeFsm) SetCommitmentParamsStart(ctx context.Context, aImageVersion string, aActiveImageID uint16) error { |
| 470 | oFsm.mutexUpgradeParams.Lock() |
| 471 | var pBaseFsm *fsm.FSM = nil |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 472 | if oFsm.PAdaptFsm != nil { |
| 473 | pBaseFsm = oFsm.PAdaptFsm.PFsm |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 474 | } |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 475 | if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) { |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 476 | logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting to start with commitment", log.Fields{ |
| 477 | "device-id": oFsm.deviceID, "image-version": aImageVersion}) |
| 478 | oFsm.useAPIVersion43 = true |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 479 | oFsm.InactiveImageMeID = aActiveImageID //upgrade state machines inactive ImageId is the new active ImageId |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 480 | oFsm.imageVersion = aImageVersion |
| 481 | oFsm.commitImage = true |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 482 | oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTING //state change indication for activate request |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 483 | oFsm.mutexUpgradeParams.Unlock() |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 484 | //directly request the FSM to commit the image |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 485 | _ = pBaseFsm.Event(UpgradeEvCommitSw) //no need to call the FSM event in background here |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 486 | return nil |
| 487 | } |
| 488 | oFsm.mutexUpgradeParams.Unlock() |
| 489 | logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{ |
| 490 | "device-id": oFsm.deviceID}) |
| 491 | return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID)) |
| 492 | } |
| 493 | |
mpagenko | 1f8e882 | 2021-06-25 14:10:21 +0000 | [diff] [blame] | 494 | //GetCommitFlag delivers the commit flag that was configured here |
| 495 | func (oFsm *OnuUpgradeFsm) GetCommitFlag(ctx context.Context) bool { |
| 496 | oFsm.mutexUpgradeParams.RLock() |
| 497 | defer oFsm.mutexUpgradeParams.RUnlock() |
| 498 | return oFsm.commitImage |
| 499 | } |
| 500 | |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 501 | //GetImageStates delivers the download/image states as per device proto buf definition |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 502 | func (oFsm *OnuUpgradeFsm) GetImageStates(ctx context.Context, |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 503 | aImageIdentifier string, aVersion string) *voltha.ImageState { |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 504 | pImageState := &voltha.ImageState{} |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 505 | pImageState.Version = aVersion //version as requested |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 506 | // check if the request refers to some active image/version of the processing |
| 507 | oFsm.mutexUpgradeParams.RLock() |
| 508 | if (aImageIdentifier == oFsm.imageIdentifier) || (aVersion == oFsm.imageVersion) { |
| 509 | pImageState.DownloadState = oFsm.volthaDownloadState |
| 510 | pImageState.Reason = oFsm.volthaDownloadReason |
| 511 | pImageState.ImageState = oFsm.volthaImageState |
| 512 | } else { |
| 513 | pImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN |
| 514 | pImageState.Reason = voltha.ImageState_NO_ERROR |
| 515 | pImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN |
| 516 | } |
| 517 | oFsm.mutexUpgradeParams.RUnlock() |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 518 | return pImageState |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 519 | } |
| 520 | |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 521 | //SetImageStateActive sets the FSM internal volthaImageState to ImageState_IMAGE_ACTIVE |
| 522 | func (oFsm *OnuUpgradeFsm) SetImageStateActive(ctx context.Context) { |
mpagenko | 183647c | 2021-06-08 15:25:04 +0000 | [diff] [blame] | 523 | oFsm.mutexUpgradeParams.Lock() |
| 524 | defer oFsm.mutexUpgradeParams.Unlock() |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 525 | oFsm.upgradePhase = cUpgradeActivated |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 526 | oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVE |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 527 | } |
| 528 | |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 529 | //CancelProcessing ensures that suspended processing at waiting on some response is aborted and reset of FSM |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 530 | func (oFsm *OnuUpgradeFsm) CancelProcessing(ctx context.Context, abCompleteAbort bool, |
| 531 | aReason voltha.ImageState_ImageFailureReason) { |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 532 | pAdaptFsm := oFsm.PAdaptFsm |
| 533 | if pAdaptFsm == nil || pAdaptFsm.PFsm == nil { |
| 534 | logger.Warnw(ctx, "OnuUpgradeFsm cancel, but FSM invalid", log.Fields{ |
| 535 | "device-id": oFsm.deviceID}) |
| 536 | return |
| 537 | } |
| 538 | logger.Debugw(ctx, "OnuUpgradeFsm start canceling", log.Fields{ |
| 539 | "device-id": oFsm.deviceID, "in fsm-state": pAdaptFsm.PFsm.Current()}) |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 540 | oFsm.mutexAbortRequest.Lock() |
| 541 | oFsm.abortRequested = aReason //possibly abort the sectionDownload loop |
| 542 | oFsm.mutexAbortRequest.Unlock() |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 543 | //mutex protection is required for possible concurrent access to FSM members |
| 544 | //attention: for an unbuffered channel the sender is blocked until the value is received (processed)! |
| 545 | // accordingly the mutex must be released before sending to channel here (mutex acquired in receiver) |
| 546 | oFsm.mutexIsAwaitingAdapterDlResponse.RLock() |
| 547 | if oFsm.isWaitingForAdapterDlResponse { |
| 548 | oFsm.mutexIsAwaitingAdapterDlResponse.RUnlock() |
| 549 | //use channel to indicate that the download response waiting shall be aborted for this device (channel) |
| 550 | oFsm.chAdapterDlReady <- false |
| 551 | } else { |
| 552 | oFsm.mutexIsAwaitingAdapterDlResponse.RUnlock() |
| 553 | } |
mpagenko | 609a69a | 2021-12-02 08:12:58 +0000 | [diff] [blame] | 554 | |
| 555 | //abort a possible delaying of sending EndSwDl |
| 556 | //use asynchronous channel sending to avoid blocking here in case no receiver is waiting |
| 557 | select { |
| 558 | case oFsm.chAbortDelayEndSwDl <- struct{}{}: |
| 559 | default: |
| 560 | } |
| 561 | |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 562 | //chOnuDlReady is cleared as part of the FSM reset processing (from enterResetting()) |
| 563 | |
| 564 | // in any case (even if it might be automatically requested by above cancellation of waiting) ensure resetting the FSM |
mpagenko | 1f8e882 | 2021-06-25 14:10:21 +0000 | [diff] [blame] | 565 | // specific here: See definition of state changes: some states are excluded from reset for possible later commit |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 566 | pAdaptFsm = oFsm.PAdaptFsm |
mpagenko | 59862f0 | 2021-10-11 08:53:18 +0000 | [diff] [blame] | 567 | if pAdaptFsm != nil && pAdaptFsm.PFsm != nil { |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 568 | // calling FSM events in background to avoid blocking of the caller |
mpagenko | 59862f0 | 2021-10-11 08:53:18 +0000 | [diff] [blame] | 569 | go func(apFsm *fsm.FSM) { |
| 570 | if apFsm.Is(UpgradeStWaitEndDL) { |
| 571 | oFsm.chReceiveExpectedResponse <- false //which aborts the FSM in WaitEndDL state |
| 572 | } else if apFsm.Is(UpgradeStAbortingDL) { |
| 573 | oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseAbort //abort waiting on EndDownloadResponse |
| 574 | } |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 575 | |
mpagenko | 59862f0 | 2021-10-11 08:53:18 +0000 | [diff] [blame] | 576 | var err error |
| 577 | if abCompleteAbort { |
| 578 | // in case of unconditional abort request the ImageState is set immediately |
| 579 | oFsm.mutexUpgradeParams.Lock() |
| 580 | //any previous lingering conditional cancelRequest is superseded by this abortion |
| 581 | oFsm.conditionalCancelRequested = false |
| 582 | oFsm.volthaDownloadReason = aReason |
| 583 | oFsm.mutexUpgradeParams.Unlock() |
| 584 | err = apFsm.Event(UpgradeEvAbort) //as unconditional default FSM cancellation |
| 585 | } else { |
| 586 | //at conditional abort request the image states are set when reaching the reset state |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 587 | oFsm.mutexUpgradeParams.Lock() |
mpagenko | 59862f0 | 2021-10-11 08:53:18 +0000 | [diff] [blame] | 588 | oFsm.conditionalCancelRequested = true |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 589 | oFsm.mutexUpgradeParams.Unlock() |
mpagenko | 59862f0 | 2021-10-11 08:53:18 +0000 | [diff] [blame] | 590 | err = apFsm.Event(UpgradeEvReset) //as state-conditional default FSM cleanup |
| 591 | } |
| 592 | if err != nil { |
| 593 | //error return is expected in case of conditional request and no state transition |
| 594 | logger.Debugw(ctx, "onu upgrade fsm could not cancel with abort/reset event", log.Fields{ |
| 595 | "device-id": oFsm.deviceID, "error": err}) |
| 596 | } |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 597 | logger.Debugw(ctx, "OnuUpgradeFsm canceling done", log.Fields{ |
| 598 | "device-id": oFsm.deviceID}) |
mpagenko | 59862f0 | 2021-10-11 08:53:18 +0000 | [diff] [blame] | 599 | }(pAdaptFsm.PFsm) |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 600 | } else { //the FSM seems already to be in some released state |
| 601 | logger.Warnw(ctx, "OnuUpgradeFsm canceling without FSM event", log.Fields{ |
| 602 | "device-id": oFsm.deviceID}) |
| 603 | } |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 604 | } |
| 605 | |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 606 | func (oFsm *OnuUpgradeFsm) enterStarting(ctx context.Context, e *fsm.Event) { |
| 607 | logger.Debugw(ctx, "OnuUpgradeFsm start", log.Fields{"in state": e.FSM.Current(), |
| 608 | "device-id": oFsm.deviceID}) |
| 609 | |
| 610 | // start go routine for processing of LockState messages |
| 611 | go oFsm.processOmciUpgradeMessages(ctx) |
| 612 | } |
| 613 | |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 614 | //enterWaitingAdapterDL state can only be reached with useAPIVersion43 |
| 615 | func (oFsm *OnuUpgradeFsm) enterWaitingAdapterDL(ctx context.Context, e *fsm.Event) { |
| 616 | logger.Debugw(ctx, "OnuUpgradeFsm waiting for adapter download", log.Fields{"in state": e.FSM.Current(), |
| 617 | "device-id": oFsm.deviceID}) |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 618 | syncChannel := make(chan struct{}) |
| 619 | go oFsm.waitOnDownloadToAdapterReady(ctx, syncChannel, oFsm.chAdapterDlReady) |
| 620 | //block until the wait routine is really blocked on chAdapterDlReady |
| 621 | <-syncChannel |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 622 | go oFsm.pFileManager.RequestDownloadReady(ctx, oFsm.imageIdentifier, oFsm.chAdapterDlReady) |
| 623 | } |
| 624 | |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 625 | func (oFsm *OnuUpgradeFsm) enterPreparingDL(ctx context.Context, e *fsm.Event) { |
| 626 | logger.Debugw(ctx, "OnuUpgradeFsm prepare Download to Onu", log.Fields{"in state": e.FSM.Current(), |
| 627 | "device-id": oFsm.deviceID}) |
| 628 | |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 629 | var fileLen int64 |
| 630 | var err error |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 631 | oFsm.mutexUpgradeParams.Lock() |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 632 | if oFsm.useAPIVersion43 { |
| 633 | //with the new API structure download to adapter is implicit and we have to wait until the image is available |
| 634 | fileLen, err = oFsm.pFileManager.GetImageBufferLen(ctx, oFsm.imageIdentifier) |
| 635 | } else { |
| 636 | fileLen, err = oFsm.pDownloadManager.getImageBufferLen(ctx, oFsm.pImageDsc.Name, oFsm.pImageDsc.LocalDir) |
| 637 | } |
mpagenko | c497ee3 | 2021-11-10 17:30:20 +0000 | [diff] [blame] | 638 | if err != nil || fileLen == 0 || fileLen > int64(cMaxUint32) { |
mpagenko | 39b703e | 2021-08-25 13:38:40 +0000 | [diff] [blame] | 639 | oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto) |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 640 | oFsm.mutexUpgradeParams.Unlock() |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 641 | logger.Errorw(ctx, "OnuUpgradeFsm abort: problems getting image buffer length", log.Fields{ |
| 642 | "device-id": oFsm.deviceID, "error": err, "length": fileLen}) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 643 | pBaseFsm := oFsm.PAdaptFsm |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 644 | // Can't call FSM Event directly, decoupling it |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 645 | go func(a_pAFsm *cmn.AdapterFsm) { |
| 646 | _ = a_pAFsm.PFsm.Event(UpgradeEvAbort) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 647 | }(pBaseFsm) |
| 648 | return |
| 649 | } |
| 650 | |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 651 | //copy file content to buffer |
mpagenko | c497ee3 | 2021-11-10 17:30:20 +0000 | [diff] [blame] | 652 | var imageBuffer []byte |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 653 | if oFsm.useAPIVersion43 { |
mpagenko | c497ee3 | 2021-11-10 17:30:20 +0000 | [diff] [blame] | 654 | imageBuffer, err = oFsm.pFileManager.GetDownloadImageBuffer(ctx, oFsm.imageIdentifier) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 655 | } else { |
mpagenko | c497ee3 | 2021-11-10 17:30:20 +0000 | [diff] [blame] | 656 | imageBuffer, err = oFsm.pDownloadManager.getDownloadImageBuffer(ctx, oFsm.pImageDsc.Name, oFsm.pImageDsc.LocalDir) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 657 | } |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 658 | if err != nil { |
mpagenko | 39b703e | 2021-08-25 13:38:40 +0000 | [diff] [blame] | 659 | oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto) |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 660 | oFsm.mutexUpgradeParams.Unlock() |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 661 | logger.Errorw(ctx, "OnuUpgradeFsm abort: can't get image buffer", log.Fields{ |
| 662 | "device-id": oFsm.deviceID, "error": err}) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 663 | pBaseFsm := oFsm.PAdaptFsm |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 664 | // Can't call FSM Event directly, decoupling it |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 665 | go func(a_pAFsm *cmn.AdapterFsm) { |
| 666 | _ = a_pAFsm.PFsm.Event(UpgradeEvAbort) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 667 | }(pBaseFsm) |
| 668 | return |
| 669 | } |
mpagenko | c497ee3 | 2021-11-10 17:30:20 +0000 | [diff] [blame] | 670 | //provide slice capacity already with the reserve of one section to avoid inflation of the slice to double size at append |
| 671 | oFsm.imageBuffer = make([]byte, fileLen, fileLen+cOmciDownloadSectionSize) |
| 672 | //better use a copy of the read image buffer in case the buffer/file is modified from outside, |
| 673 | // this also limits the slice len to the expected maximum fileLen |
| 674 | copy(oFsm.imageBuffer, imageBuffer) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 675 | |
| 676 | oFsm.noOfSections = uint32(fileLen / cOmciDownloadSectionSize) |
| 677 | if fileLen%cOmciDownloadSectionSize > 0 { |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 678 | bufferPadding := make([]byte, cOmciDownloadSectionSize-uint32((fileLen)%cOmciDownloadSectionSize)) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 679 | //expand the imageBuffer to exactly fit multiples of cOmciDownloadSectionSize with padding |
mpagenko | c497ee3 | 2021-11-10 17:30:20 +0000 | [diff] [blame] | 680 | oFsm.imageBuffer = append(oFsm.imageBuffer, bufferPadding...) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 681 | oFsm.noOfSections++ |
| 682 | } |
| 683 | oFsm.origImageLength = uint32(fileLen) |
| 684 | oFsm.imageLength = uint32(len(oFsm.imageBuffer)) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 685 | logger.Infow(ctx, "OnuUpgradeFsm starts with StartSwDl values", log.Fields{ |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 686 | "MeId": oFsm.InactiveImageMeID, "windowSizeLimit": oFsm.omciDownloadWindowSizeLimit, |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 687 | "ImageSize": oFsm.imageLength, "original file size": fileLen}) |
| 688 | //"NumberOfCircuitPacks": oFsm.numberCircuitPacks, "CircuitPacks MeId": 0}) //parallel circuit packs download not supported |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 689 | |
| 690 | oFsm.mutexUpgradeParams.Unlock() |
mpagenko | 39b703e | 2021-08-25 13:38:40 +0000 | [diff] [blame] | 691 | |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 692 | // flush chOnuDlReady |
mpagenko | 39b703e | 2021-08-25 13:38:40 +0000 | [diff] [blame] | 693 | select { |
| 694 | case <-oFsm.chOnuDlReady: |
| 695 | logger.Debug(ctx, "flushed OnuDlReady channel") |
| 696 | default: |
| 697 | } |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 698 | go oFsm.waitOnDownloadToOnuReady(ctx, oFsm.chOnuDlReady) // start supervision of the complete download-to-ONU procedure |
| 699 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 700 | err = oFsm.pOmciCC.SendStartSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false, |
| 701 | oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, oFsm.omciDownloadWindowSizeLimit, oFsm.origImageLength) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 702 | if err != nil { |
| 703 | logger.Errorw(ctx, "StartSwDl abort: can't send section", log.Fields{ |
| 704 | "device-id": oFsm.deviceID, "error": err}) |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 705 | oFsm.abortOnOmciError(ctx, true) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 706 | return |
| 707 | } |
Holger Hildebrandt | 288d947 | 2021-09-06 10:47:53 +0000 | [diff] [blame] | 708 | oFsm.isEndSwDlOpen = true |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | func (oFsm *OnuUpgradeFsm) enterDownloadSection(ctx context.Context, e *fsm.Event) { |
| 712 | logger.Debugw(ctx, "OnuUpgradeFsm start downloading sections", log.Fields{ |
| 713 | "device-id": oFsm.deviceID, "absolute window": oFsm.nextDownloadWindow}) |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 714 | //use a background routine to send the multiple download sections frames in a loop |
mpagenko | 609a69a | 2021-12-02 08:12:58 +0000 | [diff] [blame] | 715 | // in order to avoid blocking on synchronous event calls for the entire (long) processing time |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 716 | go oFsm.runSwDlSectionWindow(ctx) |
| 717 | } |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 718 | |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 719 | //runSwDlSectionWindow runs a loop to send all DlSection frames of one window in background |
| 720 | // may be aborted by parallel change of abortRequested |
| 721 | func (oFsm *OnuUpgradeFsm) runSwDlSectionWindow(ctx context.Context) { |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 722 | var windowAckRequest uint8 = 0 |
| 723 | var bufferStartOffset uint32 |
| 724 | var bufferEndOffset uint32 |
| 725 | var downloadSection []byte |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 726 | framePrint := false //default no printing of downloadSection frames |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 727 | oFsm.mutexUpgradeParams.Lock() |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 728 | oFsm.upgradePhase = cUpgradeDownloading //start of downloading image to ONU |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 729 | if oFsm.nextDownloadSectionsAbsolute == 0 { |
| 730 | //debug print of first section frame |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 731 | framePrint = true |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 732 | oFsm.volthaImageState = voltha.ImageState_IMAGE_DOWNLOADING |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 733 | } |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 734 | for { |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 735 | oFsm.mutexAbortRequest.RLock() |
| 736 | // this way out of the section download loop on abort request |
| 737 | if oFsm.abortRequested != voltha.ImageState_NO_ERROR { |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 738 | //states are updated when entering the reset state ... |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 739 | oFsm.volthaDownloadReason = oFsm.abortRequested |
| 740 | oFsm.mutexAbortRequest.RUnlock() |
| 741 | oFsm.mutexUpgradeParams.Unlock() |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 742 | pUpgradeFsm := oFsm.PAdaptFsm |
| 743 | if pUpgradeFsm != nil { |
| 744 | _ = pUpgradeFsm.PFsm.Event(UpgradeEvAbort) |
| 745 | logger.Debugw(ctx, "aborting runSwDlSectionWindow", log.Fields{ |
| 746 | "device-id": oFsm.deviceID, "reason": oFsm.volthaDownloadReason}) |
| 747 | return |
| 748 | } |
| 749 | logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID}) |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 750 | return |
| 751 | } |
| 752 | oFsm.mutexAbortRequest.RUnlock() |
| 753 | |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 754 | bufferStartOffset = oFsm.nextDownloadSectionsAbsolute * cOmciDownloadSectionSize |
| 755 | bufferEndOffset = bufferStartOffset + cOmciDownloadSectionSize - 1 //for representing cOmciDownloadSectionSizeLimit values |
| 756 | logger.Debugw(ctx, "DlSection values are", log.Fields{ |
| 757 | "DlSectionNoAbsolute": oFsm.nextDownloadSectionsAbsolute, |
| 758 | "DlSectionWindow": oFsm.nextDownloadSectionsWindow, |
| 759 | "startOffset": bufferStartOffset, "endOffset": bufferEndOffset}) |
| 760 | if bufferStartOffset+1 > oFsm.imageLength || bufferEndOffset+1 > oFsm.imageLength { //should never occur in this state |
| 761 | logger.Errorw(ctx, "OnuUpgradeFsm buffer error: exceeded length", log.Fields{ |
| 762 | "device-id": oFsm.deviceID, "bufferStartOffset": bufferStartOffset, |
| 763 | "bufferEndOffset": bufferEndOffset, "imageLength": oFsm.imageLength}) |
mpagenko | 39b703e | 2021-08-25 13:38:40 +0000 | [diff] [blame] | 764 | oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto) |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 765 | oFsm.mutexUpgradeParams.Unlock() |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 766 | //logical error -- reset the FSM |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 767 | pUpgradeFsm := oFsm.PAdaptFsm |
| 768 | if pUpgradeFsm != nil { |
| 769 | _ = pUpgradeFsm.PFsm.Event(UpgradeEvAbort) |
| 770 | return |
| 771 | } |
| 772 | logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID}) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 773 | return |
| 774 | } |
| 775 | downloadSection = oFsm.imageBuffer[bufferStartOffset : bufferEndOffset+1] |
| 776 | if oFsm.nextDownloadSectionsWindow == oFsm.omciDownloadWindowSizeLimit { |
| 777 | windowAckRequest = 1 |
| 778 | logger.Debugw(ctx, "DlSection expect Response for complete window", log.Fields{ |
| 779 | "device-id": oFsm.deviceID, "in window": oFsm.nextDownloadWindow}) |
| 780 | } |
| 781 | if oFsm.nextDownloadSectionsAbsolute+1 >= oFsm.noOfSections { |
| 782 | windowAckRequest = 1 |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 783 | framePrint = true //debug print of last frame |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 784 | oFsm.omciDownloadWindowSizeLast = oFsm.nextDownloadSectionsWindow |
| 785 | logger.Infow(ctx, "DlSection expect Response for last window (section)", log.Fields{ |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 786 | "device-id": oFsm.deviceID, "DlSectionNoAbsolute": oFsm.nextDownloadSectionsAbsolute}) |
| 787 | } |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 788 | oFsm.mutexUpgradeParams.Unlock() //unlock here to give other functions some chance to process during/after the send request |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 789 | err := oFsm.pOmciCC.SendDownloadSection(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false, |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 790 | oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, windowAckRequest, oFsm.nextDownloadSectionsWindow, downloadSection, framePrint) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 791 | if err != nil { |
| 792 | logger.Errorw(ctx, "DlSection abort: can't send section", log.Fields{ |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 793 | "device-id": oFsm.deviceID, "section absolute": oFsm.nextDownloadSectionsAbsolute, "error": err}) |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 794 | oFsm.abortOnOmciError(ctx, false) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 795 | return |
| 796 | } |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 797 | oFsm.mutexUpgradeParams.Lock() |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 798 | oFsm.nextDownloadSectionsAbsolute++ //always increase the absolute section counter after having sent one |
| 799 | if windowAckRequest == 1 { |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 800 | oFsm.mutexUpgradeParams.Unlock() |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 801 | pUpgradeFsm := oFsm.PAdaptFsm |
| 802 | if pUpgradeFsm != nil { |
| 803 | _ = pUpgradeFsm.PFsm.Event(UpgradeEvWaitWindowAck) //state transition to upgradeStVerifyWindow |
| 804 | return |
| 805 | } |
| 806 | logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID}) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 807 | return |
| 808 | } |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 809 | framePrint = false //for the next Section frame (if wanted, can be enabled in logic before sendXXX()) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 810 | oFsm.nextDownloadSectionsWindow++ //increase the window related section counter only if not in the last section |
mpagenko | 59498c1 | 2021-03-18 14:15:15 +0000 | [diff] [blame] | 811 | if oFsm.omciSectionInterleaveDelay > 0 { |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 812 | //ensure a defined intersection-time-gap to leave space for further processing, other ONU's ... |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 813 | oFsm.mutexUpgradeParams.Unlock() //unlock here to give other functions some chance to process during/after the send request |
mpagenko | 59498c1 | 2021-03-18 14:15:15 +0000 | [diff] [blame] | 814 | time.Sleep(oFsm.omciSectionInterleaveDelay * time.Millisecond) |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 815 | oFsm.mutexUpgradeParams.Lock() |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 816 | } |
| 817 | } |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 818 | } //runSwDlSectionWindow |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 819 | |
| 820 | func (oFsm *OnuUpgradeFsm) enterVerifyWindow(ctx context.Context, e *fsm.Event) { |
| 821 | logger.Debugw(ctx, "OnuUpgradeFsm verify DL window ack", log.Fields{ |
| 822 | "for window": oFsm.nextDownloadWindow, "device-id": oFsm.deviceID}) |
| 823 | } |
| 824 | |
| 825 | func (oFsm *OnuUpgradeFsm) enterFinalizeDL(ctx context.Context, e *fsm.Event) { |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 826 | logger.Infow(ctx, "OnuUpgradeFsm finalize DL", log.Fields{ |
mpagenko | 59498c1 | 2021-03-18 14:15:15 +0000 | [diff] [blame] | 827 | "device-id": oFsm.deviceID, "crc": strconv.FormatInt(int64(oFsm.imageCRC), 16), "delay": oFsm.delayEndSwDl}) |
mpagenko | 609a69a | 2021-12-02 08:12:58 +0000 | [diff] [blame] | 828 | //use a background routine to wait EndSwDlDelay and then send the EndSwDl request |
| 829 | // in order to avoid blocking on synchronous event calls for the complete wait time |
| 830 | // and to allow for state transition before sending the EndSwDl request |
| 831 | go oFsm.delayAndSendEndSwDl(ctx) |
| 832 | } |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 833 | |
mpagenko | 609a69a | 2021-12-02 08:12:58 +0000 | [diff] [blame] | 834 | //delayAndSendEndSwDl ensures a delay before sending the EndSwDl request |
| 835 | // may also be aborted by parallel channel reception on chAbortEndSwDl |
| 836 | func (oFsm *OnuUpgradeFsm) delayAndSendEndSwDl(ctx context.Context) { |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 837 | oFsm.mutexUpgradeParams.RLock() |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 838 | if oFsm.delayEndSwDl { |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 839 | oFsm.mutexUpgradeParams.RUnlock() |
mpagenko | 609a69a | 2021-12-02 08:12:58 +0000 | [diff] [blame] | 840 | //give the ONU some time for image evaluation (hoping it does not only start this evaluation on first EndSwDl itself) |
| 841 | logger.Debugw(ctx, "OnuUpgradeFsm delay EndSwDl", log.Fields{"device-id": oFsm.deviceID, |
| 842 | "duration_s": cOmciEndSwDlDelaySeconds}) |
| 843 | select { |
| 844 | case <-time.After(cOmciEndSwDlDelaySeconds * time.Second): |
| 845 | logger.Warnw(ctx, "OnuUpgradeFsm start sending EndSwDl", log.Fields{ |
| 846 | "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier}) |
| 847 | case <-oFsm.chAbortDelayEndSwDl: |
| 848 | logger.Debugw(ctx, "OnuUpgradeFsm abort request to send EndSwDl", log.Fields{"device-id": oFsm.deviceID}) |
| 849 | //according further state transition is ensured by the entity that sent the abort |
| 850 | return |
| 851 | } |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 852 | } else { |
| 853 | oFsm.mutexUpgradeParams.RUnlock() |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 856 | pBaseFsm := oFsm.PAdaptFsm |
mpagenko | 59498c1 | 2021-03-18 14:15:15 +0000 | [diff] [blame] | 857 | if pBaseFsm == nil { |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 858 | logger.Errorw(ctx, "EndSwDl abort: BaseFsm invalid", log.Fields{"device-id": oFsm.deviceID}) |
| 859 | oFsm.mutexUpgradeParams.Lock() |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 860 | oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR |
| 861 | oFsm.mutexUpgradeParams.Unlock() |
mpagenko | 59498c1 | 2021-03-18 14:15:15 +0000 | [diff] [blame] | 862 | // Can't call FSM Event directly, decoupling it |
mpagenko | 609a69a | 2021-12-02 08:12:58 +0000 | [diff] [blame] | 863 | _ = pBaseFsm.PFsm.Event(UpgradeEvAbort) |
mpagenko | 59498c1 | 2021-03-18 14:15:15 +0000 | [diff] [blame] | 864 | return |
| 865 | } |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 866 | err := oFsm.pOmciCC.SendEndSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false, |
| 867 | oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, oFsm.origImageLength, oFsm.imageCRC) |
Holger Hildebrandt | 288d947 | 2021-09-06 10:47:53 +0000 | [diff] [blame] | 868 | |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 869 | if err != nil { |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 870 | logger.Errorw(ctx, "EndSwDl abort: error sending EndSwDl", log.Fields{ |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 871 | "device-id": oFsm.deviceID, "error": err}) |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 872 | oFsm.abortOnOmciError(ctx, true) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 873 | return |
| 874 | } |
mpagenko | 609a69a | 2021-12-02 08:12:58 +0000 | [diff] [blame] | 875 | //from here on sending of EndSwDl(Abort) is not needed anymore (even though response is not yet received) |
| 876 | // this avoids sending of both EndSwDl(Success) and EndSwDl(Abort) when cancellation falls just into this window |
| 877 | // the ONU must theoretically be prepared to receive none of them (in case of OMCI transfer issues) e.g. by timeout |
| 878 | oFsm.isEndSwDlOpen = false |
| 879 | // wait for the EndSwDLResponse and check, if the ONU is ready for activation |
| 880 | _ = pBaseFsm.PFsm.Event(UpgradeEvWaitEndDownload) |
| 881 | } //delayAndSendEndSwDl |
mpagenko | 59498c1 | 2021-03-18 14:15:15 +0000 | [diff] [blame] | 882 | |
| 883 | func (oFsm *OnuUpgradeFsm) enterWaitEndDL(ctx context.Context, e *fsm.Event) { |
| 884 | logger.Infow(ctx, "OnuUpgradeFsm WaitEndDl", log.Fields{ |
| 885 | "device-id": oFsm.deviceID, "wait delay": oFsm.waitDelayEndSwDl * time.Second, "wait count": oFsm.waitCountEndSwDl}) |
| 886 | if oFsm.waitCountEndSwDl == 0 { |
| 887 | logger.Errorw(ctx, "WaitEndDl abort: max limit of EndSwDL reached", log.Fields{ |
| 888 | "device-id": oFsm.deviceID}) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 889 | pBaseFsm := oFsm.PAdaptFsm |
mpagenko | 59498c1 | 2021-03-18 14:15:15 +0000 | [diff] [blame] | 890 | if pBaseFsm == nil { |
| 891 | logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{ |
| 892 | "device-id": oFsm.deviceID}) |
| 893 | return |
| 894 | } |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 895 | oFsm.mutexUpgradeParams.Lock() |
mpagenko | 39b703e | 2021-08-25 13:38:40 +0000 | [diff] [blame] | 896 | oFsm.volthaDownloadReason = voltha.ImageState_IMAGE_REFUSED_BY_ONU //something like 'END_DOWNLOAD_TIMEOUT' would be better (proto) |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 897 | oFsm.mutexUpgradeParams.Unlock() |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 898 | go func(a_pAFsm *cmn.AdapterFsm) { |
| 899 | _ = a_pAFsm.PFsm.Event(UpgradeEvAbort) |
mpagenko | 59498c1 | 2021-03-18 14:15:15 +0000 | [diff] [blame] | 900 | }(pBaseFsm) |
| 901 | return |
| 902 | } |
| 903 | |
| 904 | oFsm.waitCountEndSwDl-- |
| 905 | select { |
| 906 | case <-time.After(oFsm.waitDelayEndSwDl * time.Second): |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 907 | pBaseFsm := oFsm.PAdaptFsm |
mpagenko | 59498c1 | 2021-03-18 14:15:15 +0000 | [diff] [blame] | 908 | if pBaseFsm == nil { |
| 909 | logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{ |
| 910 | "device-id": oFsm.deviceID}) |
| 911 | //FSM may be reset already from somewhere else, nothing we can do here anymore |
| 912 | return |
| 913 | } |
| 914 | //retry End SW DL |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 915 | oFsm.mutexUpgradeParams.Lock() |
mpagenko | 59498c1 | 2021-03-18 14:15:15 +0000 | [diff] [blame] | 916 | oFsm.delayEndSwDl = false //no more extra delay for the request |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 917 | oFsm.mutexUpgradeParams.Unlock() |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 918 | go func(a_pAFsm *cmn.AdapterFsm) { |
| 919 | _ = a_pAFsm.PFsm.Event(UpgradeEvContinueFinalize) |
mpagenko | 59498c1 | 2021-03-18 14:15:15 +0000 | [diff] [blame] | 920 | }(pBaseFsm) |
| 921 | return |
| 922 | case success := <-oFsm.chReceiveExpectedResponse: |
| 923 | logger.Debugw(ctx, "WaitEndDl stop wait timer", log.Fields{"device-id": oFsm.deviceID}) |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 924 | oFsm.isEndSwDlOpen = false //no request to abort of download (already finished or immediate abort) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 925 | pBaseFsm := oFsm.PAdaptFsm |
mpagenko | 59498c1 | 2021-03-18 14:15:15 +0000 | [diff] [blame] | 926 | if pBaseFsm == nil { |
| 927 | logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{ |
| 928 | "device-id": oFsm.deviceID}) |
| 929 | //FSM may be reset already from somewhere else, nothing we can do here anymore |
| 930 | return |
| 931 | } |
| 932 | if success { |
| 933 | //answer received with ready indication |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 934 | //useAPIVersion43 may not conflict in concurrency in this state function |
| 935 | if oFsm.useAPIVersion43 { // newer API usage requires verification of downloaded image version |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 936 | go func(a_pAFsm *cmn.AdapterFsm) { |
| 937 | _ = a_pAFsm.PFsm.Event(UpgradeEvCheckImageName) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 938 | }(pBaseFsm) |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 939 | } else { // elder API usage does not support image version check -immediately consider download as successful |
| 940 | if oFsm.activateImage { |
| 941 | //immediate activation requested |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 942 | go func(a_pAFsm *cmn.AdapterFsm) { |
| 943 | _ = a_pAFsm.PFsm.Event(UpgradeEvRequestActivate) |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 944 | }(pBaseFsm) |
| 945 | } else { |
| 946 | //have to wait on explicit activation request |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 947 | go func(a_pAFsm *cmn.AdapterFsm) { |
| 948 | _ = a_pAFsm.PFsm.Event(UpgradeEvWaitForActivate) |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 949 | }(pBaseFsm) |
| 950 | } |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 951 | } |
mpagenko | 59498c1 | 2021-03-18 14:15:15 +0000 | [diff] [blame] | 952 | return |
| 953 | } |
| 954 | //timer was aborted |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 955 | oFsm.abortOnOmciError(ctx, true) |
mpagenko | 59498c1 | 2021-03-18 14:15:15 +0000 | [diff] [blame] | 956 | return |
| 957 | } |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 958 | } |
| 959 | |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 960 | func (oFsm *OnuUpgradeFsm) enterCheckImageName(ctx context.Context, e *fsm.Event) { |
| 961 | logger.Debugw(ctx, "OnuUpgradeFsm checking downloaded image name", log.Fields{ |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 962 | "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID}) |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 963 | requestedAttributes := me.AttributeValueMap{"IsCommitted": 0, "IsActive": 0, "Version": ""} |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 964 | meInstance, err := oFsm.pOmciCC.SendGetMe(log.WithSpanFromContext(context.Background(), ctx), |
| 965 | me.SoftwareImageClassID, oFsm.InactiveImageMeID, requestedAttributes, oFsm.pDeviceHandler.GetOmciTimeout(), |
| 966 | false, oFsm.PAdaptFsm.CommChan) |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 967 | if err != nil { |
| 968 | logger.Errorw(ctx, "OnuUpgradeFsm get Software Image ME result error", |
| 969 | log.Fields{"device-id": oFsm.deviceID, "Error": err}) |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 970 | oFsm.abortOnOmciError(ctx, true) |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 971 | return |
| 972 | } |
| 973 | oFsm.pLastTxMeInstance = meInstance |
| 974 | } |
| 975 | |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 976 | func (oFsm *OnuUpgradeFsm) enterActivateSw(ctx context.Context, e *fsm.Event) { |
| 977 | logger.Infow(ctx, "OnuUpgradeFsm activate SW", log.Fields{ |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 978 | "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID}) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 979 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 980 | err := oFsm.pOmciCC.SendActivateSoftware(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false, |
| 981 | oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 982 | if err != nil { |
| 983 | logger.Errorw(ctx, "ActivateSw abort: can't send activate frame", log.Fields{ |
| 984 | "device-id": oFsm.deviceID, "error": err}) |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 985 | oFsm.abortOnOmciError(ctx, true) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 986 | return |
| 987 | } |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 988 | oFsm.mutexUpgradeParams.Lock() |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 989 | oFsm.upgradePhase = cUpgradeActivating //start of image activation for ONU |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 990 | oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATING |
| 991 | oFsm.mutexUpgradeParams.Unlock() |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | func (oFsm *OnuUpgradeFsm) enterCommitSw(ctx context.Context, e *fsm.Event) { |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 995 | logger.Debugw(ctx, "OnuUpgradeFsm start commit SW", log.Fields{ |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 996 | "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID}) |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 997 | //any abort request (also conditional) is still regarded as valid as the commit indication might not be possible to verify |
| 998 | // (which is a bit problematic as the ONU might already be in committed state, |
| 999 | // in this case (committing failed) always 'onuimage list' should be used to verify the real state (if ONU is reachable)) |
| 1000 | if activeImageID, err := oFsm.pDevEntry.GetActiveImageMeID(ctx); err == nil { |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 1001 | oFsm.mutexUpgradeParams.Lock() |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1002 | if activeImageID == oFsm.InactiveImageMeID { |
| 1003 | inactiveImageID := oFsm.InactiveImageMeID |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1004 | logger.Infow(ctx, "OnuUpgradeFsm commit SW", log.Fields{ |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 1005 | "device-id": oFsm.deviceID, "me-id": inactiveImageID}) //more efficient activeImageID with above check |
| 1006 | oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTING |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1007 | oFsm.upgradePhase = cUpgradeCommitting //start of image commitment for ONU |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 1008 | oFsm.mutexUpgradeParams.Unlock() |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1009 | err := oFsm.pOmciCC.SendCommitSoftware(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false, |
| 1010 | oFsm.PAdaptFsm.CommChan, inactiveImageID) //more efficient activeImageID with above check |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1011 | if err != nil { |
| 1012 | logger.Errorw(ctx, "CommitSw abort: can't send commit sw frame", log.Fields{ |
| 1013 | "device-id": oFsm.deviceID, "error": err}) |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1014 | oFsm.abortOnOmciError(ctx, true) |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1015 | return |
| 1016 | } |
| 1017 | return |
| 1018 | } |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 1019 | oFsm.mutexUpgradeParams.Unlock() |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1020 | logger.Errorw(ctx, "OnuUpgradeFsm active ImageId <> IdToCommit", log.Fields{ |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1021 | "device-id": oFsm.deviceID, "active ID": activeImageID, "to commit ID": oFsm.InactiveImageMeID}) |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 1022 | } else { |
| 1023 | logger.Errorw(ctx, "OnuUpgradeFsm can't commit, no valid active image", log.Fields{ |
| 1024 | "device-id": oFsm.deviceID}) |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1025 | } |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 1026 | oFsm.mutexUpgradeParams.Lock() |
| 1027 | oFsm.conditionalCancelRequested = false //any lingering conditional cancelRequest is superseded by this error |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 1028 | oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 1029 | oFsm.mutexUpgradeParams.Unlock() |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1030 | //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)?? |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1031 | pBaseFsm := oFsm.PAdaptFsm |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1032 | // Can't call FSM Event directly, decoupling it |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1033 | go func(a_pAFsm *cmn.AdapterFsm) { |
| 1034 | _ = a_pAFsm.PFsm.Event(UpgradeEvAbort) |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1035 | }(pBaseFsm) |
| 1036 | } |
| 1037 | |
| 1038 | func (oFsm *OnuUpgradeFsm) enterCheckCommitted(ctx context.Context, e *fsm.Event) { |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 1039 | logger.Debugw(ctx, "OnuUpgradeFsm checking committed SW", log.Fields{ |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1040 | "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID}) |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1041 | requestedAttributes := me.AttributeValueMap{"IsCommitted": 0, "IsActive": 0, "Version": ""} |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1042 | meInstance, err := oFsm.pOmciCC.SendGetMe(log.WithSpanFromContext(context.Background(), ctx), |
| 1043 | me.SoftwareImageClassID, oFsm.InactiveImageMeID, requestedAttributes, oFsm.pDeviceHandler.GetOmciTimeout(), false, oFsm.PAdaptFsm.CommChan) |
ozgecanetsia | b36ed57 | 2021-04-01 10:38:48 +0300 | [diff] [blame] | 1044 | if err != nil { |
| 1045 | logger.Errorw(ctx, "OnuUpgradeFsm get Software Image ME result error", |
| 1046 | log.Fields{"device-id": oFsm.deviceID, "Error": err}) |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1047 | oFsm.abortOnOmciError(ctx, true) |
ozgecanetsia | b36ed57 | 2021-04-01 10:38:48 +0300 | [diff] [blame] | 1048 | return |
| 1049 | } |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1050 | oFsm.pLastTxMeInstance = meInstance |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | func (oFsm *OnuUpgradeFsm) enterResetting(ctx context.Context, e *fsm.Event) { |
| 1054 | logger.Debugw(ctx, "OnuUpgradeFsm resetting", log.Fields{"device-id": oFsm.deviceID}) |
| 1055 | |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1056 | oFsm.stateUpdateOnReset(ctx) |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 1057 | |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 1058 | oFsm.mutexAbortRequest.Lock() |
| 1059 | //to be sure to abort a possibly still running runSwDlSectionWindow() |
| 1060 | // in case the reset was not received from cancel() and download not finished correctly |
| 1061 | oFsm.abortRequested = oFsm.volthaDownloadReason |
| 1062 | oFsm.mutexAbortRequest.Unlock() |
| 1063 | |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1064 | // in case the download-to-ONU timer is still running - cancel it |
mpagenko | 39b703e | 2021-08-25 13:38:40 +0000 | [diff] [blame] | 1065 | //use non-blocking channel (to be independent from receiver state) |
| 1066 | select { |
| 1067 | //use channel to indicate that the download response waiting shall be aborted for this device (channel) |
| 1068 | case oFsm.chOnuDlReady <- false: |
| 1069 | default: |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1070 | } |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1071 | pConfigUpgradeStateAFsm := oFsm.PAdaptFsm |
| 1072 | if pConfigUpgradeStateAFsm != nil { |
Holger Hildebrandt | 288d947 | 2021-09-06 10:47:53 +0000 | [diff] [blame] | 1073 | var nextEvent string |
| 1074 | if oFsm.isEndSwDlOpen { |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1075 | if oFsm.repeatAbort { |
| 1076 | oFsm.delayEndSwDl = true //run next abort with delay |
| 1077 | } else { //initial request |
| 1078 | oFsm.delayEndSwDl = false //run next abort with no delay |
| 1079 | oFsm.waitCountEndSwDl = cWaitCountEndSwDl //init for possible repetitions |
| 1080 | } |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1081 | nextEvent = UpgradeEvAbortSwDownload |
Holger Hildebrandt | 288d947 | 2021-09-06 10:47:53 +0000 | [diff] [blame] | 1082 | } else { |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1083 | nextEvent = UpgradeEvRestart |
Holger Hildebrandt | 288d947 | 2021-09-06 10:47:53 +0000 | [diff] [blame] | 1084 | } |
| 1085 | // Can't call FSM Event directly, decoupling it |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1086 | go func(a_pAFsm *cmn.AdapterFsm) { |
| 1087 | if a_pAFsm != nil && a_pAFsm.PFsm != nil { |
| 1088 | _ = a_pAFsm.PFsm.Event(nextEvent) |
Holger Hildebrandt | 288d947 | 2021-09-06 10:47:53 +0000 | [diff] [blame] | 1089 | } |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1090 | }(pConfigUpgradeStateAFsm) |
Holger Hildebrandt | 288d947 | 2021-09-06 10:47:53 +0000 | [diff] [blame] | 1091 | } |
| 1092 | } |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1093 | |
Holger Hildebrandt | 288d947 | 2021-09-06 10:47:53 +0000 | [diff] [blame] | 1094 | func (oFsm *OnuUpgradeFsm) enterAbortingDL(ctx context.Context, e *fsm.Event) { |
| 1095 | logger.Debugw(ctx, "OnuUpgradeFsm aborting download to ONU", log.Fields{"device-id": oFsm.deviceID}) |
| 1096 | |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1097 | oFsm.mutexUpgradeParams.RLock() |
| 1098 | if oFsm.delayEndSwDl { |
| 1099 | oFsm.mutexUpgradeParams.RUnlock() |
| 1100 | //give the ONU some time for image discard activities |
| 1101 | time.Sleep(cOmciEndSwDlDelaySeconds * time.Second) |
| 1102 | } else { |
| 1103 | oFsm.mutexUpgradeParams.RUnlock() |
| 1104 | } |
| 1105 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1106 | pBaseFsm := oFsm.PAdaptFsm |
Holger Hildebrandt | 288d947 | 2021-09-06 10:47:53 +0000 | [diff] [blame] | 1107 | if pBaseFsm == nil { |
| 1108 | logger.Errorw(ctx, "OnuUpgradeFsm aborting download: BaseFsm invalid", log.Fields{"device-id": oFsm.deviceID}) |
| 1109 | return |
| 1110 | } |
| 1111 | // abort the download operation by sending an end software download message with invalid CRC and image size |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1112 | err := oFsm.pOmciCC.SendEndSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false, |
| 1113 | oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, 0, 0xFFFFFFFF) |
Holger Hildebrandt | 288d947 | 2021-09-06 10:47:53 +0000 | [diff] [blame] | 1114 | |
Holger Hildebrandt | 288d947 | 2021-09-06 10:47:53 +0000 | [diff] [blame] | 1115 | if err != nil { |
| 1116 | logger.Errorw(ctx, "OnuUpgradeFsm aborting download: can't send EndSwDl request", log.Fields{"device-id": oFsm.deviceID}) |
| 1117 | // Can't call FSM Event directly, decoupling it |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1118 | go func(a_pAFsm *cmn.AdapterFsm) { |
| 1119 | if a_pAFsm != nil && a_pAFsm.PFsm != nil { |
| 1120 | _ = a_pAFsm.PFsm.Event(UpgradeEvRestart) |
Holger Hildebrandt | 288d947 | 2021-09-06 10:47:53 +0000 | [diff] [blame] | 1121 | } |
| 1122 | }(pBaseFsm) |
| 1123 | return |
| 1124 | } |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 1125 | |
| 1126 | //avoid waiting in the enterXXX function here, |
| 1127 | // otherwise synchronous event calls (like from RxMessage processing) may block and block complete Rx processing then |
| 1128 | go oFsm.waitOnAbortEndSwDlResponse(ctx) |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | //abortingDlEvaluateResponse waits for a channel indication with decision to proceed the FSM processing |
| 1132 | func (oFsm *OnuUpgradeFsm) abortingDlEvaluateResponse(ctx context.Context, |
| 1133 | pBaseFsm *cmn.AdapterFsm, aResponseResult tEndSwDlResponseResult) bool { |
| 1134 | switch aResponseResult { |
| 1135 | case cEndSwDlResponseBusy: // indication for device busy, needs repetition |
| 1136 | if oFsm.waitCountEndSwDl == 0 { |
| 1137 | logger.Errorw(ctx, "aborting download: max limit of EndSwDl reached", log.Fields{ |
| 1138 | "device-id": oFsm.deviceID}) |
| 1139 | go func(a_pAFsm *cmn.AdapterFsm) { |
| 1140 | if a_pAFsm != nil && a_pAFsm.PFsm != nil { |
| 1141 | _ = a_pAFsm.PFsm.Event(UpgradeEvRestart) //give up and let FSM terminate |
| 1142 | } |
| 1143 | }(pBaseFsm) |
| 1144 | } else { |
| 1145 | logger.Debugw(ctx, "aborting download: re-trigger sending abort SwDl", log.Fields{ |
| 1146 | "device-id": oFsm.deviceID, "counter": oFsm.waitCountEndSwDl}) |
| 1147 | oFsm.waitCountEndSwDl-- |
| 1148 | oFsm.repeatAbort = true //repeated request in next round |
| 1149 | go func(a_pAFsm *cmn.AdapterFsm) { |
| 1150 | if a_pAFsm != nil && a_pAFsm.PFsm != nil { |
| 1151 | _ = a_pAFsm.PFsm.Event(UpgradeEvReset) //which then re-triggers sending AbortSwDL |
| 1152 | } |
| 1153 | }(pBaseFsm) |
| 1154 | } |
| 1155 | return true |
| 1156 | case cEndSwDlResponseSuccess: // indication for success response |
| 1157 | logger.Infow(ctx, "aborting download: success response, terminating FSM", log.Fields{ |
| 1158 | "device-id": oFsm.deviceID}) |
| 1159 | case cEndSwDlResponseAbort: // indication for request to abort waiting for response |
| 1160 | logger.Infow(ctx, "aborting download: request to abort waiting, terminating FSM", log.Fields{ |
| 1161 | "device-id": oFsm.deviceID}) |
| 1162 | default: |
| 1163 | logger.Errorw(ctx, "aborting download: unknown channel indication, terminating FSM", log.Fields{ |
| 1164 | "device-id": oFsm.deviceID}) |
| 1165 | } //switch |
| 1166 | return false |
Holger Hildebrandt | 288d947 | 2021-09-06 10:47:53 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | func (oFsm *OnuUpgradeFsm) enterRestarting(ctx context.Context, e *fsm.Event) { |
| 1170 | logger.Debugw(ctx, "OnuUpgradeFsm restarting", log.Fields{"device-id": oFsm.deviceID}) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1171 | pConfigUpgradeStateAFsm := oFsm.PAdaptFsm |
| 1172 | if pConfigUpgradeStateAFsm != nil { |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1173 | // abort running message processing |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1174 | fsmAbortMsg := cmn.Message{ |
| 1175 | Type: cmn.TestMsg, |
| 1176 | Data: cmn.TestMessage{ |
| 1177 | TestMessageVal: cmn.AbortMessageProcessing, |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1178 | }, |
| 1179 | } |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1180 | pConfigUpgradeStateAFsm.CommChan <- fsmAbortMsg |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1181 | |
| 1182 | //try to restart the FSM to 'disabled' |
| 1183 | // Can't call FSM Event directly, decoupling it |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1184 | go func(a_pAFsm *cmn.AdapterFsm) { |
| 1185 | if a_pAFsm != nil && a_pAFsm.PFsm != nil { |
| 1186 | _ = a_pAFsm.PFsm.Event(UpgradeEvDisable) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1187 | } |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1188 | }(pConfigUpgradeStateAFsm) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | func (oFsm *OnuUpgradeFsm) enterDisabled(ctx context.Context, e *fsm.Event) { |
| 1193 | logger.Debugw(ctx, "OnuUpgradeFsm enters disabled state", log.Fields{"device-id": oFsm.deviceID}) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1194 | // no need to flush possible channels here, Upgrade FSM will be completely removed, garbage collector should find its way |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1195 | if oFsm.pDeviceHandler != nil { |
| 1196 | //request removal of 'reference' in the Handler (completely clear the FSM and its data) |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 1197 | pLastUpgradeImageState := &voltha.ImageState{ |
| 1198 | Version: oFsm.imageVersion, |
| 1199 | DownloadState: oFsm.volthaDownloadState, |
| 1200 | Reason: oFsm.volthaDownloadReason, |
| 1201 | ImageState: oFsm.volthaImageState, |
| 1202 | } |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1203 | go oFsm.pDeviceHandler.RemoveOnuUpgradeFsm(ctx, pLastUpgradeImageState) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | func (oFsm *OnuUpgradeFsm) processOmciUpgradeMessages(ctx context.Context) { //ctx context.Context? |
| 1208 | logger.Debugw(ctx, "Start OnuUpgradeFsm Msg processing", log.Fields{"for device-id": oFsm.deviceID}) |
| 1209 | loop: |
| 1210 | for { |
| 1211 | // case <-ctx.Done(): |
| 1212 | // logger.Info(ctx,"MibSync Msg", log.Fields{"Message handling canceled via context for device-id": oFsm.deviceID}) |
| 1213 | // break loop |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1214 | message, ok := <-oFsm.PAdaptFsm.CommChan |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1215 | if !ok { |
| 1216 | logger.Info(ctx, "OnuUpgradeFsm Rx Msg - could not read from channel", log.Fields{"device-id": oFsm.deviceID}) |
| 1217 | // but then we have to ensure a restart of the FSM as well - as exceptional procedure |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1218 | oFsm.abortOnOmciError(ctx, true) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1219 | break loop |
| 1220 | } |
| 1221 | logger.Debugw(ctx, "OnuUpgradeFsm Rx Msg", log.Fields{"device-id": oFsm.deviceID}) |
| 1222 | |
| 1223 | switch message.Type { |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1224 | case cmn.TestMsg: |
| 1225 | msg, _ := message.Data.(cmn.TestMessage) |
| 1226 | if msg.TestMessageVal == cmn.AbortMessageProcessing { |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1227 | logger.Infow(ctx, "OnuUpgradeFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.deviceID}) |
| 1228 | break loop |
| 1229 | } |
| 1230 | logger.Warnw(ctx, "OnuUpgradeFsm unknown TestMessage", log.Fields{"device-id": oFsm.deviceID, "MessageVal": msg.TestMessageVal}) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1231 | case cmn.OMCI: |
| 1232 | msg, _ := message.Data.(cmn.OmciMessage) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1233 | oFsm.handleOmciOnuUpgradeMessage(ctx, msg) |
| 1234 | default: |
| 1235 | logger.Warn(ctx, "OnuUpgradeFsm Rx unknown message", log.Fields{"device-id": oFsm.deviceID, |
| 1236 | "message.Type": message.Type}) |
| 1237 | } |
| 1238 | } |
| 1239 | logger.Infow(ctx, "End OnuUpgradeFsm Msg processing", log.Fields{"device-id": oFsm.deviceID}) |
| 1240 | } |
| 1241 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1242 | func (oFsm *OnuUpgradeFsm) handleOmciOnuUpgradeMessage(ctx context.Context, msg cmn.OmciMessage) { |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1243 | logger.Debugw(ctx, "Rx OMCI OnuUpgradeFsm Msg", log.Fields{"device-id": oFsm.deviceID, |
| 1244 | "msgType": msg.OmciMsg.MessageType}) |
| 1245 | |
| 1246 | switch msg.OmciMsg.MessageType { |
| 1247 | case omci.StartSoftwareDownloadResponseType: |
| 1248 | { |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1249 | oFsm.handleRxStartSwDownloadResponse(ctx, msg) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1250 | return |
| 1251 | } //StartSoftwareDownloadResponseType |
| 1252 | case omci.DownloadSectionResponseType: |
| 1253 | { |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1254 | oFsm.handleRxSwSectionResponse(ctx, msg) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1255 | return |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1256 | } //DownloadSectionResponseType |
| 1257 | case omci.EndSoftwareDownloadResponseType: |
| 1258 | { |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1259 | oFsm.handleRxEndSwDownloadResponse(ctx, msg) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1260 | return |
| 1261 | } //EndSoftwareDownloadResponseType |
| 1262 | case omci.ActivateSoftwareResponseType: |
| 1263 | { |
| 1264 | msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeActivateSoftwareResponse) |
| 1265 | if msgLayer == nil { |
| 1266 | logger.Errorw(ctx, "Omci Msg layer could not be detected for ActivateSw", |
| 1267 | log.Fields{"device-id": oFsm.deviceID}) |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1268 | oFsm.abortOnOmciError(ctx, false) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1269 | return |
| 1270 | } |
| 1271 | msgObj, msgOk := msgLayer.(*omci.ActivateSoftwareResponse) |
| 1272 | if !msgOk { |
| 1273 | logger.Errorw(ctx, "Omci Msg layer could not be assigned for ActivateSw", |
| 1274 | log.Fields{"device-id": oFsm.deviceID}) |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1275 | oFsm.abortOnOmciError(ctx, false) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1276 | return |
| 1277 | } |
| 1278 | logger.Debugw(ctx, "OnuUpgradeFsm ActivateSwResponse data", log.Fields{ |
| 1279 | "device-id": oFsm.deviceID, "data-fields": msgObj}) |
| 1280 | if msgObj.Result != me.Success { |
| 1281 | logger.Errorw(ctx, "OnuUpgradeFsm ActivateSwResponse result error - later: drive FSM to abort state ?", |
| 1282 | log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result}) |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1283 | oFsm.abortOnOmciError(ctx, false) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1284 | return |
| 1285 | } |
mpagenko | 183647c | 2021-06-08 15:25:04 +0000 | [diff] [blame] | 1286 | oFsm.mutexUpgradeParams.Lock() |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1287 | if msgObj.EntityInstance == oFsm.InactiveImageMeID { |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 1288 | // the image is regarded as active really only after ONU reboot and according indication (ONU down/up procedure) |
mpagenko | 183647c | 2021-06-08 15:25:04 +0000 | [diff] [blame] | 1289 | oFsm.mutexUpgradeParams.Unlock() |
| 1290 | logger.Infow(ctx, "Expected ActivateSwResponse received", |
| 1291 | log.Fields{"device-id": oFsm.deviceID, "commit": oFsm.commitImage}) |
| 1292 | if oFsm.commitImage { |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1293 | _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvWaitForCommit) |
mpagenko | 183647c | 2021-06-08 15:25:04 +0000 | [diff] [blame] | 1294 | } else { |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1295 | _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvActivationDone) // let the FSM wait for external commit request |
mpagenko | 183647c | 2021-06-08 15:25:04 +0000 | [diff] [blame] | 1296 | } |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1297 | return |
| 1298 | } |
mpagenko | 183647c | 2021-06-08 15:25:04 +0000 | [diff] [blame] | 1299 | oFsm.mutexUpgradeParams.Unlock() |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1300 | logger.Errorw(ctx, "OnuUpgradeFsm ActivateSwResponse wrong ME instance: abort", |
| 1301 | log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance}) |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1302 | oFsm.abortOnOmciError(ctx, false) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1303 | return |
| 1304 | } //ActivateSoftwareResponseType |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1305 | case omci.CommitSoftwareResponseType: |
| 1306 | { |
| 1307 | msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeCommitSoftwareResponse) |
| 1308 | if msgLayer == nil { |
| 1309 | logger.Errorw(ctx, "Omci Msg layer could not be detected for CommitResponse", |
| 1310 | log.Fields{"device-id": oFsm.deviceID}) |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1311 | oFsm.abortOnOmciError(ctx, false) |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1312 | return |
| 1313 | } |
| 1314 | msgObj, msgOk := msgLayer.(*omci.CommitSoftwareResponse) |
| 1315 | if !msgOk { |
| 1316 | logger.Errorw(ctx, "Omci Msg layer could not be assigned for CommitResponse", |
| 1317 | log.Fields{"device-id": oFsm.deviceID}) |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1318 | oFsm.abortOnOmciError(ctx, false) |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1319 | return |
| 1320 | } |
mpagenko | bf67a09 | 2021-03-17 09:52:28 +0000 | [diff] [blame] | 1321 | if msgObj.Result != me.Success { |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1322 | logger.Errorw(ctx, "OnuUpgradeFsm SwImage CommitResponse result error - later: drive FSM to abort state ?", |
| 1323 | log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result}) |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1324 | oFsm.abortOnOmciError(ctx, false) |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1325 | return |
mpagenko | bf67a09 | 2021-03-17 09:52:28 +0000 | [diff] [blame] | 1326 | } |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 1327 | oFsm.mutexUpgradeParams.RLock() |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1328 | if msgObj.EntityInstance == oFsm.InactiveImageMeID { |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 1329 | oFsm.mutexUpgradeParams.RUnlock() |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1330 | logger.Debugw(ctx, "OnuUpgradeFsm Expected SwImage CommitResponse received", log.Fields{"device-id": oFsm.deviceID}) |
| 1331 | //verifying committed image |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1332 | _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvCheckCommitted) |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1333 | return |
| 1334 | } |
mpagenko | aa3afe9 | 2021-05-21 16:20:58 +0000 | [diff] [blame] | 1335 | oFsm.mutexUpgradeParams.RUnlock() |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1336 | logger.Errorw(ctx, "OnuUpgradeFsm SwImage CommitResponse wrong ME instance: abort", |
| 1337 | log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance}) |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1338 | oFsm.abortOnOmciError(ctx, false) |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1339 | return |
| 1340 | } //CommitSoftwareResponseType |
| 1341 | case omci.GetResponseType: |
| 1342 | { |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1343 | oFsm.handleRxSwGetResponse(ctx, msg) |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 1344 | return |
| 1345 | } //GetResponseType |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1346 | default: |
| 1347 | { |
| 1348 | logger.Errorw(ctx, "Rx OMCI unhandled MsgType", |
| 1349 | log.Fields{"omciMsgType": msg.OmciMsg.MessageType, "device-id": oFsm.deviceID}) |
| 1350 | return |
| 1351 | } |
| 1352 | } |
| 1353 | } |
| 1354 | |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1355 | func (oFsm *OnuUpgradeFsm) handleRxStartSwDownloadResponse(ctx context.Context, msg cmn.OmciMessage) { |
| 1356 | msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeStartSoftwareDownloadResponse) |
| 1357 | if msgLayer == nil { |
| 1358 | logger.Errorw(ctx, "Omci Msg layer could not be detected for StartSwDlResponse", |
| 1359 | log.Fields{"device-id": oFsm.deviceID}) |
| 1360 | oFsm.abortOnOmciError(ctx, false) |
| 1361 | return |
| 1362 | } |
| 1363 | msgObj, msgOk := msgLayer.(*omci.StartSoftwareDownloadResponse) |
| 1364 | if !msgOk { |
| 1365 | logger.Errorw(ctx, "Omci Msg layer could not be assigned for StartSwDlResponse", |
| 1366 | log.Fields{"device-id": oFsm.deviceID}) |
| 1367 | oFsm.abortOnOmciError(ctx, false) |
| 1368 | return |
| 1369 | } |
| 1370 | logger.Debugw(ctx, "OnuUpgradeFsm StartSwDlResponse data", log.Fields{ |
| 1371 | "device-id": oFsm.deviceID, "data-fields": msgObj}) |
| 1372 | if msgObj.Result != me.Success { |
| 1373 | logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse result error - later: drive FSM to abort state ?", |
| 1374 | log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result}) |
| 1375 | oFsm.abortOnOmciError(ctx, false) |
| 1376 | return |
| 1377 | } |
| 1378 | |
| 1379 | oFsm.mutexUpgradeParams.Lock() |
| 1380 | if msgObj.EntityInstance == oFsm.InactiveImageMeID { |
| 1381 | logger.Debugw(ctx, "Expected StartSwDlResponse received", log.Fields{"device-id": oFsm.deviceID}) |
| 1382 | if msgObj.WindowSize != oFsm.omciDownloadWindowSizeLimit { |
| 1383 | // also response WindowSize = 0 is a valid number for used Window size 1 |
| 1384 | logger.Debugw(ctx, "different StartSwDlResponse window size requested by ONU", log.Fields{ |
| 1385 | "acceptedOnuWindowSizeLimit": msgObj.WindowSize, "device-id": oFsm.deviceID}) |
| 1386 | oFsm.omciDownloadWindowSizeLimit = msgObj.WindowSize |
| 1387 | } |
| 1388 | oFsm.noOfWindows = oFsm.noOfSections / uint32(oFsm.omciDownloadWindowSizeLimit+1) |
| 1389 | if oFsm.noOfSections%uint32(oFsm.omciDownloadWindowSizeLimit+1) > 0 { |
| 1390 | oFsm.noOfWindows++ |
| 1391 | } |
| 1392 | logger.Debugw(ctx, "OnuUpgradeFsm will use", log.Fields{ |
| 1393 | "windows": oFsm.noOfWindows, "sections": oFsm.noOfSections, |
| 1394 | "at WindowSizeLimit": oFsm.omciDownloadWindowSizeLimit}) |
| 1395 | oFsm.nextDownloadSectionsAbsolute = 0 |
| 1396 | oFsm.nextDownloadSectionsWindow = 0 |
| 1397 | oFsm.nextDownloadWindow = 0 |
| 1398 | |
| 1399 | oFsm.mutexUpgradeParams.Unlock() |
| 1400 | _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvRxStartSwDownload) |
| 1401 | return |
| 1402 | } |
| 1403 | oFsm.mutexUpgradeParams.Unlock() |
| 1404 | logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse wrong ME instance: try again (later)?", |
| 1405 | log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance}) |
| 1406 | oFsm.abortOnOmciError(ctx, false) |
| 1407 | } //handleRxStartSwDownloadResponse |
| 1408 | |
| 1409 | func (oFsm *OnuUpgradeFsm) handleRxSwSectionResponse(ctx context.Context, msg cmn.OmciMessage) { |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 1410 | if oFsm.PAdaptFsm == nil { |
| 1411 | logger.Infow(ctx, "DlSectionResponse received - but FSM not really valid anymore", log.Fields{ |
| 1412 | "device-id": oFsm.deviceID}) |
| 1413 | return |
| 1414 | } |
| 1415 | if !oFsm.PAdaptFsm.PFsm.Is(UpgradeStVerifyWindow) { |
| 1416 | //all the processing here is only relevant if the FSM is in state upgradeStVerifyWindow |
| 1417 | // otherwise this response can be ignored (may stem from a long-processing window send activity, |
| 1418 | // which is not anymore relevant based on intermediate (cancel) state transitions) |
| 1419 | logger.Infow(ctx, "DlSectionResponse received - but ignored", log.Fields{ |
| 1420 | "device-id": oFsm.deviceID, "fsm-state": oFsm.PAdaptFsm.PFsm.Current()}) |
| 1421 | return |
| 1422 | } |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1423 | msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeDownloadSectionResponse) |
| 1424 | if msgLayer == nil { |
| 1425 | logger.Errorw(ctx, "Omci Msg layer could not be detected for DlSectionResponse", |
| 1426 | log.Fields{"device-id": oFsm.deviceID, "omci-message": msg.OmciMsg}) |
| 1427 | oFsm.abortOnOmciError(ctx, false) |
| 1428 | return |
| 1429 | } |
| 1430 | msgObj, msgOk := msgLayer.(*omci.DownloadSectionResponse) |
| 1431 | if !msgOk { |
| 1432 | logger.Errorw(ctx, "Omci Msg layer could not be assigned for DlSectionResponse", |
| 1433 | log.Fields{"device-id": oFsm.deviceID}) |
| 1434 | oFsm.abortOnOmciError(ctx, false) |
| 1435 | return |
| 1436 | } |
| 1437 | logger.Debugw(ctx, "OnuUpgradeFsm DlSectionResponse Data", log.Fields{ |
| 1438 | "device-id": oFsm.deviceID, "data-fields": msgObj}) |
| 1439 | if msgObj.Result != me.Success { |
| 1440 | logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse result error - later: repeat window once?", //TODO!!! |
| 1441 | log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result}) |
| 1442 | oFsm.abortOnOmciError(ctx, false) |
| 1443 | return |
| 1444 | } |
| 1445 | oFsm.mutexUpgradeParams.Lock() |
| 1446 | if msgObj.EntityInstance == oFsm.InactiveImageMeID { |
| 1447 | sectionNumber := msgObj.SectionNumber |
| 1448 | logger.Infow(ctx, "DlSectionResponse received", log.Fields{ |
| 1449 | "window section-number": sectionNumber, "window": oFsm.nextDownloadWindow, "device-id": oFsm.deviceID}) |
| 1450 | |
| 1451 | oFsm.nextDownloadWindow++ |
| 1452 | if oFsm.nextDownloadWindow >= oFsm.noOfWindows { |
| 1453 | if sectionNumber != oFsm.omciDownloadWindowSizeLast { |
| 1454 | logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse section error last window - later: repeat window once?", //TODO!!! |
| 1455 | log.Fields{"device-id": oFsm.deviceID, "actual section": sectionNumber, |
| 1456 | "expected section": oFsm.omciDownloadWindowSizeLast}) |
| 1457 | oFsm.mutexUpgradeParams.Unlock() |
| 1458 | oFsm.abortOnOmciError(ctx, false) |
| 1459 | return |
| 1460 | } |
| 1461 | oFsm.delayEndSwDl = true //ensure a delay for the EndSwDl message |
| 1462 | //CRC computation for all data bytes of the file |
| 1463 | imageCRC := crc32a.Checksum(oFsm.imageBuffer[:int(oFsm.origImageLength)]) //store internal for multiple usage |
| 1464 | //revert the retrieved CRC Byte Order (seems not to deliver NetworkByteOrder) |
| 1465 | var byteSlice []byte = make([]byte, 4) |
| 1466 | binary.LittleEndian.PutUint32(byteSlice, uint32(imageCRC)) |
| 1467 | oFsm.imageCRC = binary.BigEndian.Uint32(byteSlice) |
| 1468 | oFsm.mutexUpgradeParams.Unlock() |
| 1469 | _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvEndSwDownload) |
| 1470 | return |
| 1471 | } |
| 1472 | if sectionNumber != oFsm.omciDownloadWindowSizeLimit { |
| 1473 | logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse section error - later: repeat window once?", //TODO!!! |
| 1474 | log.Fields{"device-id": oFsm.deviceID, "actual-section": sectionNumber, |
| 1475 | "expected section": oFsm.omciDownloadWindowSizeLimit}) |
| 1476 | oFsm.mutexUpgradeParams.Unlock() |
| 1477 | oFsm.abortOnOmciError(ctx, false) |
| 1478 | return |
| 1479 | } |
| 1480 | oFsm.nextDownloadSectionsWindow = 0 |
| 1481 | oFsm.mutexUpgradeParams.Unlock() |
| 1482 | _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvContinueNextWindow) |
| 1483 | return |
| 1484 | } |
| 1485 | oFsm.mutexUpgradeParams.Unlock() |
| 1486 | logger.Errorw(ctx, "OnuUpgradeFsm Omci StartSwDlResponse wrong ME instance: try again (later)?", |
| 1487 | log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance}) |
| 1488 | oFsm.abortOnOmciError(ctx, false) |
| 1489 | } //handleRxSwSectionResponse |
| 1490 | |
| 1491 | func (oFsm *OnuUpgradeFsm) handleRxEndSwDownloadResponse(ctx context.Context, msg cmn.OmciMessage) { |
| 1492 | inAbortingState := oFsm.PAdaptFsm.PFsm.Is(UpgradeStAbortingDL) |
| 1493 | |
| 1494 | msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeEndSoftwareDownloadResponse) |
| 1495 | if msgLayer == nil { |
| 1496 | logger.Errorw(ctx, "Omci Msg layer could not be detected for EndSwDlResponse", |
| 1497 | log.Fields{"device-id": oFsm.deviceID}) |
| 1498 | if !inAbortingState { |
| 1499 | oFsm.abortOnOmciError(ctx, false) |
| 1500 | } //else using error log and wait for another response or 'aborting' state timeout |
| 1501 | return |
| 1502 | } |
| 1503 | msgObj, msgOk := msgLayer.(*omci.EndSoftwareDownloadResponse) |
| 1504 | if !msgOk { |
| 1505 | logger.Errorw(ctx, "Omci Msg layer could not be assigned for EndSwDlResponse", |
| 1506 | log.Fields{"device-id": oFsm.deviceID}) |
| 1507 | if !inAbortingState { |
| 1508 | oFsm.abortOnOmciError(ctx, false) |
| 1509 | } //else using error log and wait for another response or 'aborting' state timeout |
| 1510 | return |
| 1511 | } |
| 1512 | logger.Debugw(ctx, "OnuUpgradeFsm EndSwDlResponse data", log.Fields{ |
| 1513 | "device-id": oFsm.deviceID, "data-fields": msgObj}) |
| 1514 | if msgObj.Result != me.Success { |
| 1515 | if msgObj.Result == me.DeviceBusy { |
| 1516 | //ONU indicates it is still processing the image - let the FSM just wait and then repeat the request |
| 1517 | logger.Debugw(ctx, "OnuUpgradeFsm EndSwDlResponse busy: waiting before sending new request", log.Fields{ |
| 1518 | "device-id": oFsm.deviceID}) |
| 1519 | if inAbortingState { |
| 1520 | //if the EndSwDl was requested from state AbortingDL then use channel to indicate ONU busy/repeat indication |
| 1521 | oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseBusy //repeat abort request |
| 1522 | } |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1523 | return |
| 1524 | } |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1525 | if inAbortingState { |
| 1526 | //if the EndSwDl was requested from state AbortingDL and response is error indication |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 1527 | // that would be quite strange ONU behavior, no resolution from OnuAdapter, just let the FSM go on to disabled |
| 1528 | logger.Errorw(ctx, "OnuUpgradeFsm EndSwDlResponse result error - aborting anyway", |
| 1529 | log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result}) |
| 1530 | oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseAbort //error indication to stop waiting on EndDownloadResponse(abort) |
| 1531 | return |
| 1532 | } |
| 1533 | //else: ONU rejects the previous download, complete upgrade is immediately aborted with error |
| 1534 | logger.Errorw(ctx, "OnuUpgradeFsm EndSwDlResponse result error - abort upgrade", |
| 1535 | log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result}) |
| 1536 | oFsm.mutexUpgradeParams.Lock() |
| 1537 | oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion |
| 1538 | oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE |
| 1539 | oFsm.mutexUpgradeParams.Unlock() |
| 1540 | select { |
| 1541 | case oFsm.chOnuDlReady <- false: |
| 1542 | default: |
| 1543 | } |
| 1544 | _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort) |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1545 | return |
| 1546 | } |
| 1547 | oFsm.mutexUpgradeParams.Lock() |
| 1548 | if msgObj.EntityInstance == oFsm.InactiveImageMeID { |
| 1549 | logger.Debugw(ctx, "Expected EndSwDlResponse received", log.Fields{"device-id": oFsm.deviceID}) |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1550 | if inAbortingState { |
| 1551 | oFsm.mutexUpgradeParams.Unlock() |
| 1552 | //if the EndSwDl was requested from state AbortingDL then use channel to indicate abort acceptance |
| 1553 | oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseSuccess //success |
| 1554 | return |
| 1555 | } |
| 1556 | if !oFsm.useAPIVersion43 { |
| 1557 | //in the older API version the image version check was not possible |
| 1558 | // - assume new loaded image as valid-inactive immediately |
| 1559 | oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_SUCCEEDED |
| 1560 | oFsm.volthaImageState = voltha.ImageState_IMAGE_INACTIVE |
| 1561 | oFsm.mutexUpgradeParams.Unlock() |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 1562 | //use non-blocking channel (to be independent from receiver state) to indicate that the download to ONU was successful |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1563 | select { |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1564 | case oFsm.chOnuDlReady <- true: |
| 1565 | default: |
| 1566 | } |
| 1567 | } else { |
| 1568 | oFsm.mutexUpgradeParams.Unlock() |
| 1569 | } |
| 1570 | //use asynchronous channel sending to let the FSM proceed |
| 1571 | select { |
| 1572 | case oFsm.chReceiveExpectedResponse <- true: |
| 1573 | default: |
| 1574 | } |
| 1575 | return |
| 1576 | } |
| 1577 | oFsm.mutexUpgradeParams.Unlock() |
| 1578 | logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse wrong ME instance: ignoring", |
| 1579 | log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance}) |
| 1580 | // no state abort in case of unexpected ImageId, just keep waiting for the correct one |
| 1581 | } //handleRxEndSwDownloadResponse |
| 1582 | |
| 1583 | func (oFsm *OnuUpgradeFsm) handleRxSwGetResponse(ctx context.Context, msg cmn.OmciMessage) { |
| 1584 | msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeGetResponse) |
| 1585 | if msgLayer == nil { |
| 1586 | logger.Errorw(ctx, "Omci Msg layer could not be detected for SwImage GetResponse", |
| 1587 | log.Fields{"device-id": oFsm.deviceID}) |
| 1588 | oFsm.abortOnOmciError(ctx, false) |
| 1589 | return |
| 1590 | } |
| 1591 | msgObj, msgOk := msgLayer.(*omci.GetResponse) |
| 1592 | if !msgOk { |
| 1593 | logger.Errorw(ctx, "Omci Msg layer could not be assigned for SwImage GetResponse", |
| 1594 | log.Fields{"device-id": oFsm.deviceID}) |
| 1595 | oFsm.abortOnOmciError(ctx, false) |
| 1596 | return |
| 1597 | } |
| 1598 | logger.Debugw(ctx, "OnuUpgradeFsm SwImage GetResponse data", log.Fields{ |
| 1599 | "device-id": oFsm.deviceID, "data-fields": msgObj}) |
| 1600 | if msgObj.EntityClass == oFsm.pLastTxMeInstance.GetClassID() && |
| 1601 | msgObj.EntityInstance == oFsm.pLastTxMeInstance.GetEntityID() { |
| 1602 | if msgObj.Result != me.Success { |
| 1603 | logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse result error - later: drive FSM to abort state ?", |
| 1604 | log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result}) |
| 1605 | oFsm.abortOnOmciError(ctx, false) |
| 1606 | return |
| 1607 | } |
| 1608 | } else { |
| 1609 | logger.Warnw(ctx, "OnuUpgradeFsm SwImage unexpected Entity GetResponse data - ignore", |
| 1610 | log.Fields{"device-id": oFsm.deviceID}) |
| 1611 | return |
| 1612 | } |
| 1613 | |
| 1614 | meAttributes := msgObj.Attributes |
| 1615 | imageIsCommitted := meAttributes["IsCommitted"].(uint8) |
| 1616 | imageIsActive := meAttributes["IsActive"].(uint8) |
| 1617 | imageVersion := cmn.TrimStringFromMeOctet(meAttributes["Version"]) |
| 1618 | logger.Debugw(ctx, "OnuUpgradeFsm - GetResponse Data for SoftwareImage", |
| 1619 | log.Fields{"device-id": oFsm.deviceID, "entityID": msgObj.EntityInstance, |
| 1620 | "version": imageVersion, "isActive": imageIsActive, "isCommitted": imageIsCommitted}) |
| 1621 | |
| 1622 | if oFsm.PAdaptFsm.PFsm.Current() == UpgradeStCheckImageName { |
| 1623 | //image name check after EndSwDownload, this state (and block) can only be taken if APIVersion43 is used |
| 1624 | oFsm.verifyOnuSwStatusAfterDownload(ctx, msgObj.EntityInstance, imageVersion, imageIsActive, imageIsCommitted) |
| 1625 | return |
| 1626 | } |
| 1627 | |
| 1628 | //assumed only relevant state here is upgradeStCheckCommitted |
| 1629 | oFsm.mutexUpgradeParams.Lock() |
| 1630 | oFsm.conditionalCancelRequested = false //getting here any set (conditional) cancelRequest is not relevant anymore |
| 1631 | if msgObj.EntityInstance == oFsm.InactiveImageMeID && imageIsActive == cmn.SwIsActive { |
| 1632 | //a check on the delivered image version is not done, the ONU delivered version might be different from what might have been |
| 1633 | // indicated in the download image version string (version must be part of the image content itself) |
| 1634 | // so checking that might be quite unreliable |
| 1635 | //but with new API this was changed, assumption is that omci image version is known at download request and exactly that is used |
| 1636 | // in all the API references, so it can and should be checked here now |
| 1637 | if oFsm.useAPIVersion43 { |
| 1638 | if imageVersion != oFsm.imageVersion { |
| 1639 | //new active version indicated on OMCI from ONU is not the expected version |
| 1640 | logger.Errorw(ctx, "OnuUpgradeFsm image-version not matching the requested upgrade", |
| 1641 | log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance, |
| 1642 | "onu-version": imageVersion, "expected-version": oFsm.imageVersion}) |
| 1643 | // TODO!!!: error treatment? |
| 1644 | //TODO!!!: possibly send event information for aborted upgrade (aborted by wrong version)? |
| 1645 | oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE //something like 'UNEXPECTED_VERSION' would be better - proto def |
| 1646 | oFsm.mutexUpgradeParams.Unlock() |
| 1647 | _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort) |
| 1648 | return |
| 1649 | } |
| 1650 | logger.Debugw(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU", |
| 1651 | log.Fields{"device-id": oFsm.deviceID}) |
| 1652 | } |
| 1653 | if imageIsCommitted == cmn.SwIsCommitted { |
| 1654 | oFsm.upgradePhase = cUpgradeCommitted |
| 1655 | oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTED |
| 1656 | //store the new commit flag to onuSwImageIndications (to keep them in sync) |
| 1657 | oFsm.pDevEntry.ModifySwImageActiveCommit(ctx, imageIsCommitted) |
| 1658 | logger.Infow(ctx, "requested SW image committed, releasing OnuUpgrade", log.Fields{"device-id": oFsm.deviceID}) |
| 1659 | //deviceProcStatusUpdate not used anymore, |
| 1660 | // replaced by transferring the last (more) upgrade state information within removeOnuUpgradeFsm |
| 1661 | oFsm.mutexUpgradeParams.Unlock() |
| 1662 | //releasing the upgrade FSM on success |
| 1663 | _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort) |
| 1664 | return |
| 1665 | } |
| 1666 | //if not committed, abort upgrade as failed. There is no implementation here that would trigger this test again |
| 1667 | } |
| 1668 | oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE |
| 1669 | oFsm.mutexUpgradeParams.Unlock() |
| 1670 | logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse indications not matching requested upgrade", |
| 1671 | log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance}) |
| 1672 | // TODO!!!: error treatment? |
| 1673 | //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)?? |
| 1674 | _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort) |
| 1675 | } //handleRxSwGetResponse |
| 1676 | |
| 1677 | func (oFsm *OnuUpgradeFsm) verifyOnuSwStatusAfterDownload(ctx context.Context, aInstanceID uint16, |
| 1678 | aImageVersion string, aImageIsActive uint8, aImageIsCommitted uint8) { |
| 1679 | oFsm.mutexUpgradeParams.Lock() |
| 1680 | if aInstanceID == oFsm.InactiveImageMeID && aImageIsActive == cmn.SwIsInactive && |
| 1681 | aImageIsCommitted == cmn.SwIsUncommitted { |
| 1682 | if aImageVersion != oFsm.imageVersion { |
| 1683 | //new stored inactive version indicated on OMCI from ONU is not the expected version |
| 1684 | logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse version indication not matching requested upgrade", |
| 1685 | log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": aInstanceID, |
| 1686 | "onu-version": aImageVersion, "expected-version": oFsm.imageVersion}) |
| 1687 | //download state is set when entering the reset state |
| 1688 | oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE //something like 'UNEXPECTED_VERSION' would be better - proto def |
| 1689 | oFsm.mutexUpgradeParams.Unlock() |
| 1690 | //stop the running ONU download timer |
| 1691 | //use non-blocking channel (to be independent from receiver state) |
| 1692 | select { |
| 1693 | //use channel to indicate that the download response waiting shall be aborted for this device (channel) |
| 1694 | case oFsm.chOnuDlReady <- false: |
| 1695 | default: |
| 1696 | } |
| 1697 | // TODO!!!: error treatment? |
| 1698 | //TODO!!!: possibly send event information for aborted upgrade (aborted by wrong version)? |
| 1699 | _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort) |
| 1700 | return |
| 1701 | } |
| 1702 | //with APIVersion43 this is the point to consider the newly loaded image as valid (and inactive) |
| 1703 | oFsm.upgradePhase = cUpgradeDownloaded |
| 1704 | oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_SUCCEEDED |
| 1705 | oFsm.volthaImageState = voltha.ImageState_IMAGE_INACTIVE |
| 1706 | //store the new inactive version to onuSwImageIndications (to keep them in sync) |
| 1707 | oFsm.pDevEntry.ModifySwImageInactiveVersion(ctx, oFsm.imageVersion) |
| 1708 | //proceed within upgrade FSM |
| 1709 | if oFsm.activateImage { |
| 1710 | //immediate activation requested |
| 1711 | oFsm.mutexUpgradeParams.Unlock() |
| 1712 | logger.Debugw(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU, continue with activation", |
| 1713 | log.Fields{"device-id": oFsm.deviceID}) |
| 1714 | _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvRequestActivate) |
| 1715 | } else { |
| 1716 | //have to wait on explicit activation request |
| 1717 | oFsm.mutexUpgradeParams.Unlock() |
| 1718 | logger.Infow(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU, wait for activate request", |
| 1719 | log.Fields{"device-id": oFsm.deviceID}) |
| 1720 | _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvWaitForActivate) |
| 1721 | } |
| 1722 | //use non-blocking channel (to be independent from receiver state) |
| 1723 | select { |
| 1724 | //use non-blocking channel to indicate that the download to ONU was successful |
| 1725 | case oFsm.chOnuDlReady <- true: |
| 1726 | default: |
| 1727 | } |
| 1728 | return |
| 1729 | } |
| 1730 | //not the expected image/image state |
| 1731 | oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE |
| 1732 | oFsm.mutexUpgradeParams.Unlock() |
| 1733 | logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse indications not matching requested upgrade", |
| 1734 | log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": aInstanceID}) |
| 1735 | // TODO!!!: error treatment? |
| 1736 | //TODO!!!: possibly send event information for aborted upgrade (aborted by ONU state indication)? |
| 1737 | _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort) |
| 1738 | } //verifyOnuSwStatusAfterDownload |
| 1739 | |
| 1740 | //abortOnOmciError aborts the upgrade processing with evAbort |
| 1741 | // asynchronous/synchronous based on parameter aAsync |
| 1742 | func (oFsm *OnuUpgradeFsm) abortOnOmciError(ctx context.Context, aAsync bool) { |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 1743 | oFsm.mutexUpgradeParams.Lock() |
| 1744 | oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 1745 | oFsm.volthaDownloadReason = voltha.ImageState_OMCI_TRANSFER_ERROR |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 1746 | oFsm.mutexUpgradeParams.Unlock() |
| 1747 | //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)?? |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1748 | if oFsm.PAdaptFsm != nil { |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 1749 | var err error |
| 1750 | if aAsync { //asynchronous call requested to ensure state transition |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1751 | go func(a_pAFsm *cmn.AdapterFsm) { |
| 1752 | if a_pAFsm.PFsm != nil { |
| 1753 | err = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort) |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 1754 | } |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1755 | }(oFsm.PAdaptFsm) |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 1756 | } else { |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1757 | if oFsm.PAdaptFsm.PFsm != nil { |
| 1758 | err = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort) |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 1759 | } |
| 1760 | } |
| 1761 | if err != nil { |
| 1762 | logger.Warnw(ctx, "onu upgrade fsm could not abort on omci error", log.Fields{ |
| 1763 | "device-id": oFsm.deviceID, "error": err}) |
| 1764 | } |
| 1765 | } |
| 1766 | } |
| 1767 | |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1768 | //waitOnDownloadToAdapterReady state can only be reached with useAPIVersion43 (usage of pFileManager) |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 1769 | // precondition: mutexIsAwaitingAdapterDlResponse is lockek on call |
| 1770 | func (oFsm *OnuUpgradeFsm) waitOnDownloadToAdapterReady(ctx context.Context, aSyncChannel chan<- struct{}, |
| 1771 | aWaitChannel chan bool) { |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1772 | oFsm.mutexIsAwaitingAdapterDlResponse.Lock() |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 1773 | downloadToAdapterTimeout := oFsm.pFileManager.GetDownloadTimeout(ctx) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1774 | oFsm.isWaitingForAdapterDlResponse = true |
| 1775 | oFsm.mutexIsAwaitingAdapterDlResponse.Unlock() |
mpagenko | 38662d0 | 2021-08-11 09:45:19 +0000 | [diff] [blame] | 1776 | aSyncChannel <- struct{}{} |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1777 | select { |
| 1778 | // maybe be also some outside cancel (but no context modeled for the moment ...) |
| 1779 | // case <-ctx.Done(): |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1780 | // logger.Infow("OnuUpgradeFsm-waitOnDownloadToAdapterReady canceled", log.Fields{"for device-id": oFsm.deviceID}) |
| 1781 | case <-time.After(downloadToAdapterTimeout): //10s should be enough for downloading some image to the adapter |
| 1782 | logger.Warnw(ctx, "OnuUpgradeFsm Waiting-adapter-download timeout", log.Fields{ |
| 1783 | "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier, "timeout": downloadToAdapterTimeout}) |
| 1784 | oFsm.pFileManager.RemoveReadyRequest(ctx, oFsm.imageIdentifier, aWaitChannel) |
mpagenko | 39b703e | 2021-08-25 13:38:40 +0000 | [diff] [blame] | 1785 | //running into timeout here may still have the download to adapter active -> abort |
| 1786 | oFsm.pFileManager.CancelDownload(ctx, oFsm.imageIdentifier) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1787 | oFsm.mutexIsAwaitingAdapterDlResponse.Lock() |
| 1788 | oFsm.isWaitingForAdapterDlResponse = false |
| 1789 | oFsm.mutexIsAwaitingAdapterDlResponse.Unlock() |
mpagenko | 39b703e | 2021-08-25 13:38:40 +0000 | [diff] [blame] | 1790 | oFsm.mutexUpgradeParams.Lock() |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1791 | oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion |
mpagenko | 39b703e | 2021-08-25 13:38:40 +0000 | [diff] [blame] | 1792 | oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'DOWNLOAD_TO_ADAPTER_TIMEOUT' would be better (proto) |
mpagenko | 39b703e | 2021-08-25 13:38:40 +0000 | [diff] [blame] | 1793 | oFsm.mutexUpgradeParams.Unlock() |
| 1794 | //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)?? |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1795 | if oFsm.PAdaptFsm != nil && oFsm.PAdaptFsm.PFsm != nil { |
| 1796 | err := oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort) |
mpagenko | 39b703e | 2021-08-25 13:38:40 +0000 | [diff] [blame] | 1797 | if err != nil { |
| 1798 | logger.Warnw(ctx, "onu upgrade fsm could not abort on omci error", log.Fields{ |
| 1799 | "device-id": oFsm.deviceID, "error": err}) |
| 1800 | } |
| 1801 | } |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1802 | return |
| 1803 | |
| 1804 | case success := <-aWaitChannel: |
| 1805 | if success { |
| 1806 | logger.Debugw(ctx, "OnuUpgradeFsm image-downloaded received", log.Fields{"device-id": oFsm.deviceID}) |
| 1807 | oFsm.mutexIsAwaitingAdapterDlResponse.Lock() |
| 1808 | oFsm.isWaitingForAdapterDlResponse = false |
| 1809 | oFsm.mutexIsAwaitingAdapterDlResponse.Unlock() |
| 1810 | //let the upgrade process proceed |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1811 | pUpgradeFsm := oFsm.PAdaptFsm |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1812 | if pUpgradeFsm != nil { |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1813 | _ = pUpgradeFsm.PFsm.Event(UpgradeEvPrepareSwDownload) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1814 | } else { |
| 1815 | logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID}) |
| 1816 | } |
| 1817 | return |
| 1818 | } |
mpagenko | 39b703e | 2021-08-25 13:38:40 +0000 | [diff] [blame] | 1819 | // waiting was aborted (assumed here to be caused by |
| 1820 | // error detection or cancel at download after upgrade FSM reset/abort with according image states set there) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1821 | logger.Debugw(ctx, "OnuUpgradeFsm Waiting-adapter-download aborted", log.Fields{"device-id": oFsm.deviceID}) |
| 1822 | oFsm.pFileManager.RemoveReadyRequest(ctx, oFsm.imageIdentifier, aWaitChannel) |
| 1823 | oFsm.mutexIsAwaitingAdapterDlResponse.Lock() |
| 1824 | oFsm.isWaitingForAdapterDlResponse = false |
| 1825 | oFsm.mutexIsAwaitingAdapterDlResponse.Unlock() |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1826 | return |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 1827 | } |
| 1828 | } |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1829 | |
| 1830 | //waitOnDownloadToOnuReady state can only be reached with useAPIVersion43 (usage of pFileManager) |
| 1831 | func (oFsm *OnuUpgradeFsm) waitOnDownloadToOnuReady(ctx context.Context, aWaitChannel chan bool) { |
| 1832 | downloadToOnuTimeout := time.Duration(1+(oFsm.imageLength/0x400000)) * oFsm.downloadToOnuTimeout4MB |
| 1833 | logger.Debugw(ctx, "OnuUpgradeFsm start download-to-ONU timer", log.Fields{"device-id": oFsm.deviceID, |
| 1834 | "duration": downloadToOnuTimeout}) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1835 | select { |
| 1836 | // maybe be also some outside cancel (but no context modeled for the moment ...) |
| 1837 | // case <-ctx.Done(): |
| 1838 | // logger.Infow("OnuUpgradeFsm-waitOnDownloadToOnuReady canceled", log.Fields{"for device-id": oFsm.deviceID}) |
| 1839 | case <-time.After(downloadToOnuTimeout): //using an image-size depending timout (in minutes) |
| 1840 | logger.Warnw(ctx, "OnuUpgradeFsm Waiting-ONU-download timeout", log.Fields{ |
| 1841 | "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier, "timeout": downloadToOnuTimeout}) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1842 | //the upgrade process has to be aborted |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1843 | oFsm.abortOnOmciError(ctx, false) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1844 | return |
| 1845 | |
| 1846 | case success := <-aWaitChannel: |
| 1847 | if success { |
| 1848 | logger.Debugw(ctx, "OnuUpgradeFsm image-downloaded on ONU received", log.Fields{"device-id": oFsm.deviceID}) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1849 | //all fine, let the FSM proceed like defined from the sender of this event |
| 1850 | return |
| 1851 | } |
| 1852 | // waiting was aborted (assumed here to be caused by |
mpagenko | 39b703e | 2021-08-25 13:38:40 +0000 | [diff] [blame] | 1853 | // error detection or cancel at download after upgrade FSM reset/abort with according image states set there) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1854 | logger.Debugw(ctx, "OnuUpgradeFsm Waiting-ONU-download aborted", log.Fields{"device-id": oFsm.deviceID}) |
mpagenko | c26d4c0 | 2021-05-06 14:27:57 +0000 | [diff] [blame] | 1855 | return |
| 1856 | } |
| 1857 | } |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1858 | |
mpagenko | a2b288f | 2021-10-21 11:25:27 +0000 | [diff] [blame] | 1859 | //waitOnAbortEndSwDlResponse waits for either abort/success or timeout of EndSwDownload (for abortion) |
| 1860 | func (oFsm *OnuUpgradeFsm) waitOnAbortEndSwDlResponse(ctx context.Context) { |
| 1861 | logger.Debugw(ctx, "OnuUpgradeFsm start wait for EndSwDl response (abort)", log.Fields{"device-id": oFsm.deviceID}) |
| 1862 | select { |
| 1863 | case <-time.After(oFsm.pOmciCC.GetMaxOmciTimeoutWithRetries() * time.Second): |
| 1864 | logger.Warnw(ctx, "OnuUpgradeFsm aborting download: timeout - no response received", log.Fields{"device-id": oFsm.deviceID}) |
| 1865 | pUpgradeFsm := oFsm.PAdaptFsm |
| 1866 | if pUpgradeFsm != nil { |
| 1867 | _ = pUpgradeFsm.PFsm.Event(UpgradeEvRestart) |
| 1868 | } else { |
| 1869 | logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID}) |
| 1870 | } |
| 1871 | return |
| 1872 | case response := <-oFsm.chReceiveAbortEndSwDlResponse: |
| 1873 | logger.Debugw(ctx, "OnuUpgradeFsm aborting download: response received", |
| 1874 | log.Fields{"device-id": oFsm.deviceID, "response": response}) |
| 1875 | pUpgradeFsm := oFsm.PAdaptFsm |
| 1876 | if pUpgradeFsm != nil { |
| 1877 | if oFsm.abortingDlEvaluateResponse(ctx, pUpgradeFsm, response) { |
| 1878 | return //event sent from function already |
| 1879 | } |
| 1880 | _ = pUpgradeFsm.PFsm.Event(UpgradeEvRestart) |
| 1881 | } else { |
| 1882 | logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID}) |
| 1883 | } |
| 1884 | return |
| 1885 | } //select |
| 1886 | } |
| 1887 | |
mpagenko | 4558676 | 2021-10-01 08:30:22 +0000 | [diff] [blame] | 1888 | //stateUpdateOnReset writes the download and/or image state on entering the reset state according to FSM internal indications |
| 1889 | func (oFsm *OnuUpgradeFsm) stateUpdateOnReset(ctx context.Context) { |
| 1890 | oFsm.mutexUpgradeParams.Lock() |
| 1891 | defer oFsm.mutexUpgradeParams.Unlock() |
| 1892 | if !oFsm.conditionalCancelRequested { |
| 1893 | switch oFsm.upgradePhase { |
| 1894 | case cUpgradeUndefined, cUpgradeDownloading: //coming from downloading |
| 1895 | //make sure the download state is only changed in case the device has still been downloading |
| 1896 | if oFsm.volthaDownloadReason == voltha.ImageState_CANCELLED_ON_REQUEST { |
| 1897 | // indication for termination on request |
| 1898 | oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_CANCELLED |
| 1899 | } else if oFsm.volthaDownloadReason != voltha.ImageState_NO_ERROR { |
| 1900 | // indication for termination on failure |
| 1901 | oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_FAILED |
| 1902 | } |
| 1903 | //reset the image state from Downloading in this case |
| 1904 | oFsm.volthaImageState = voltha.ImageState_IMAGE_UNKNOWN //something like 'IMAGE_DOWNLOAD_ABORTED' would be better (proto) |
| 1905 | //in all other upgrade phases the last set download state remains valid |
| 1906 | case cUpgradeActivating: |
| 1907 | //reset the image state from Activating in this case |
| 1908 | oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATION_ABORTED |
| 1909 | case cUpgradeCommitting: // indication for request to abort waiting for response |
| 1910 | //reset the image state from Activating in this case |
| 1911 | oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMIT_ABORTED |
| 1912 | //default: in all other upgrade phases keep the last set imageState |
| 1913 | } //switch |
| 1914 | } else { |
| 1915 | //when reaching reset state with conditional cancel that can only result from ONU related problems |
| 1916 | // (mostly ONU down indication) - derived from resetFsms call |
| 1917 | // and it can only be related to the downloading-to-ONU phase (no need to check that additionally) |
| 1918 | oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_FAILED |
| 1919 | oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE |
| 1920 | //reset the image state from Downloading in this case |
| 1921 | oFsm.volthaImageState = voltha.ImageState_IMAGE_UNKNOWN //something like 'IMAGE_DOWNLOAD_ABORTED' would be better (proto) |
| 1922 | } |
| 1923 | } |