blob: ba3295f9e787982b0ce9bc5eedd43e2bc4a05531 [file] [log] [blame]
mpagenko80622a52021-02-09 16:53:23 +00001/*
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 Hildebrandt4b5e73f2021-08-19 06:51:21 +000017//Package swupg provides the utilities for onu sw upgrade
18package swupg
mpagenko80622a52021-02-09 16:53:23 +000019
20import (
21 "context"
mpagenkoc26d4c02021-05-06 14:27:57 +000022 "encoding/binary"
mpagenko80622a52021-02-09 16:53:23 +000023 "fmt"
24 "strconv"
mpagenkoc26d4c02021-05-06 14:27:57 +000025 "sync"
mpagenko80622a52021-02-09 16:53:23 +000026 "time"
27
28 "github.com/boguslaw-wojcik/crc32a"
29 "github.com/looplab/fsm"
mpagenko836a1fd2021-11-01 16:12:42 +000030 "github.com/opencord/omci-lib-go/v2"
31 me "github.com/opencord/omci-lib-go/v2/generated"
khenaidoo7d3c5582021-08-11 18:09:44 -040032 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000033 cmn "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/common"
34 "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/devdb"
kesavand011d5162021-11-25 19:21:06 +053035 ia "github.com/opencord/voltha-protos/v5/go/inter_adapter"
khenaidoo7d3c5582021-08-11 18:09:44 -040036 "github.com/opencord/voltha-protos/v5/go/voltha"
mpagenko80622a52021-02-09 16:53:23 +000037)
38
39const cMaxUint32 = ^uint32(0)
40
41const (
42 // internal predefined values - some off them should later be configurable (perhaps with theses as defaults)
Holger Hildebrandt5458d892022-05-31 09:52:06 +000043 cOmciDownloadSectionSizeBaseLine = 31 //in bytes
44 cOmciDownloadWindowSizeLimitBaseLine = 31 //in sections for window offset (windowSize(32)-1)
45 cOmciDownloadSectionSizeExtLine = 1965 //in bytes
46 cOmciDownloadWindowSizeLimitExtLine = 7 //in sections for window offset (windowSize(8)-1)
mpagenko80622a52021-02-09 16:53:23 +000047 //cOmciDownloadWindowRetryMax = 2 // max attempts for a specific window
mpagenkoc26d4c02021-05-06 14:27:57 +000048 cOmciSectionInterleaveMilliseconds = 0 //DownloadSection interleave time in milliseconds (0 for no delay)
49 cOmciEndSwDlDelaySeconds = 1 //End Software Download delay after last section (may be also configurable?)
50 cWaitCountEndSwDl = 6 //maximum number of EndSwDl requests
51 cWaitDelayEndSwDlSeconds = 10 //duration, how long is waited before next request on EndSwDl
mpagenko80622a52021-02-09 16:53:23 +000052 //cOmciDownloadCompleteTimeout = 5400 //in s for the complete timeout (may be better scale to image size/ noOfWindows)
53)
54
mpagenko45586762021-10-01 08:30:22 +000055// tEndSwDlResponseResult - Response result from EndSwDownload as used in channel indication
56type tUpgradePhase uint8
57
58const (
59 // undefined phase
60 cUpgradeUndefined tUpgradePhase = iota
61 // downloading image
62 cUpgradeDownloading
63 // image downloaded
64 cUpgradeDownloaded
65 // activating image
66 cUpgradeActivating
67 // image activated
68 cUpgradeActivated
69 // committing image
70 cUpgradeCommitting
71 // image committed
72 cUpgradeCommitted
73)
74
75// tEndSwDlResponseResult - Response result from EndSwDownload as used in channel indication
76type tEndSwDlResponseResult uint8
77
78const (
79 // response success
80 cEndSwDlResponseSuccess tEndSwDlResponseResult = iota
81 // response busy (repeat)
82 cEndSwDlResponseBusy
83 // response error or abort waiting for response
84 cEndSwDlResponseAbort
85)
86
87// upgrade FSM related events
mpagenko80622a52021-02-09 16:53:23 +000088const (
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000089 UpgradeEvStart = "UpgradeEvStart"
90 UpgradeEvDisable = "UpgradeEvDisable"
91 UpgradeEvAdapterDownload = "UpgradeEvAdapterDownload"
92 UpgradeEvPrepareSwDownload = "UpgradeEvPrepareSwDownload"
93 UpgradeEvRxStartSwDownload = "UpgradeEvRxStartSwDownload"
94 UpgradeEvWaitWindowAck = "UpgradeEvWaitWindowAck"
95 UpgradeEvContinueNextWindow = "UpgradeEvContinueNextWindow"
96 UpgradeEvEndSwDownload = "UpgradeEvEndSwDownload"
97 UpgradeEvWaitEndDownload = "UpgradeEvWaitEndDownload"
98 UpgradeEvContinueFinalize = "UpgradeEvContinueFinalize"
99 UpgradeEvCheckImageName = "UpgradeEvCheckImageName"
100 UpgradeEvWaitForActivate = "UpgradeEvWaitForActivate"
101 UpgradeEvRequestActivate = "UpgradeEvRequestActivate"
102 UpgradeEvActivationDone = "UpgradeEvActivationDone"
103 UpgradeEvWaitForCommit = "UpgradeEvWaitForCommit"
104 UpgradeEvCommitSw = "UpgradeEvCommitSw"
105 UpgradeEvCheckCommitted = "UpgradeEvCheckCommitted"
mpagenko80622a52021-02-09 16:53:23 +0000106
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000107 //UpgradeEvTimeoutSimple = "UpgradeEvTimeoutSimple"
108 //UpgradeEvTimeoutMids = "UpgradeEvTimeoutMids"
109 UpgradeEvReset = "UpgradeEvReset"
110 UpgradeEvAbort = "UpgradeEvAbort"
111 UpgradeEvRestart = "UpgradeEvRestart"
112 UpgradeEvAbortSwDownload = "UpgradeEvAbortSwDownload"
mpagenko80622a52021-02-09 16:53:23 +0000113)
114
mpagenko45586762021-10-01 08:30:22 +0000115// upgrade FSM related states
mpagenko80622a52021-02-09 16:53:23 +0000116const (
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000117 UpgradeStDisabled = "UpgradeStDisabled"
118 UpgradeStStarting = "UpgradeStStarting"
119 UpgradeStWaitingAdapterDL = "UpgradeStWaitingAdapterDL"
120 UpgradeStPreparingDL = "UpgradeStPreparingDL"
121 UpgradeStDLSection = "UpgradeStDLSection"
122 UpgradeStVerifyWindow = "UpgradeStVerifyWindow"
123 UpgradeStFinalizeDL = "UpgradeStFinalizeDL"
124 UpgradeStWaitEndDL = "UpgradeStWaitEndDL"
125 UpgradeStCheckImageName = "UpgradeStCheckImageName"
126 UpgradeStWaitForActivate = "UpgradeStWaitForActivate"
127 UpgradeStRequestingActivate = "UpgradeStRequestingActivate"
128 UpgradeStActivated = "UpgradeStActivated"
129 UpgradeStWaitForCommit = "UpgradeStWaitForCommit"
130 UpgradeStCommitSw = "UpgradeStCommitSw"
131 UpgradeStCheckCommitted = "UpgradeStCheckCommitted"
132 UpgradeStResetting = "UpgradeStResetting"
133 UpgradeStRestarting = "UpgradeStRestarting"
134 UpgradeStAbortingDL = "UpgradeStAbortingDL"
mpagenko80622a52021-02-09 16:53:23 +0000135)
136
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000137//COnuUpgradeFsmIdleState - required definition for IdleState detection for activities on OMCI
138const COnuUpgradeFsmIdleState = UpgradeStWaitForCommit
mpagenko80622a52021-02-09 16:53:23 +0000139
140//OnuUpgradeFsm defines the structure for the state machine to config the PON ANI ports of ONU UNI ports via OMCI
141type OnuUpgradeFsm struct {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000142 pDeviceHandler cmn.IdeviceHandler
143 pDownloadManager *AdapterDownloadManager
144 pFileManager *FileDownloadManager //used from R2.8 with new API version
mpagenko80622a52021-02-09 16:53:23 +0000145 deviceID string
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000146 pDevEntry cmn.IonuDeviceEntry
147 pOmciCC *cmn.OmciCC
148 pOnuDB *devdb.OnuDeviceDB
149 requestEvent cmn.OnuDeviceEvent
mpagenko80622a52021-02-09 16:53:23 +0000150 //omciMIdsResponseReceived chan bool //seperate channel needed for checking multiInstance OMCI message responses
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000151 PAdaptFsm *cmn.AdapterFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000152 pImageDsc *voltha.ImageDownload
153 imageBuffer []byte
154 origImageLength uint32 //as also limited by OMCI
155 imageCRC uint32 //as per OMCI - ITU I.363.5 crc
156 imageLength uint32 //including last bytes padding
157 omciDownloadWindowSizeLimit uint8 //windowSize-1 in sections
158 omciDownloadWindowSizeLast uint8 //number of sections in last window
159 noOfSections uint32 //uint32 range for sections should be sufficient for very long images
160 nextDownloadSectionsAbsolute uint32 //number of next section to download in overall image
161 nextDownloadSectionsWindow uint8 //number of next section to download within current window
162 noOfWindows uint32 //uint32 range for windows should be sufficient for very long images
163 nextDownloadWindow uint32 //number of next window to download
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000164 InactiveImageMeID uint16 //ME-ID of the inactive image
mpagenkoc26d4c02021-05-06 14:27:57 +0000165 downloadToOnuTimeout4MB time.Duration //timeout for downloading the image to the ONU for a 4MB image slice
166 omciSectionInterleaveDelay time.Duration //DownloadSectionInterleave delay in milliseconds
167 delayEndSwDl bool //flag to provide a delay between last section and EndSwDl
mpagenko45586762021-10-01 08:30:22 +0000168 repeatAbort bool //flag to indicate if OMCI EndSwDownload (abort) is to be repeated
mpagenkoc26d4c02021-05-06 14:27:57 +0000169 pLastTxMeInstance *me.ManagedEntity
170 waitCountEndSwDl uint8 //number, how often is waited for EndSwDl at maximum
171 waitDelayEndSwDl time.Duration //duration, how long is waited before next request on EndSwDl
172 chReceiveExpectedResponse chan bool
mpagenkoaa3afe92021-05-21 16:20:58 +0000173 useAPIVersion43 bool //flag for indication on which API version is used (and accordingly which specific methods)
174 mutexUpgradeParams sync.RWMutex //mutex to protect members for parallel function requests and omci response processing
175 imageVersion string //name of the image as used within OMCI (and on extrenal API interface)
176 imageIdentifier string //name of the image as used in the adapter
mpagenkoc26d4c02021-05-06 14:27:57 +0000177 mutexIsAwaitingAdapterDlResponse sync.RWMutex
178 chAdapterDlReady chan bool
mpagenko609a69a2021-12-02 08:12:58 +0000179 chAbortDelayEndSwDl chan struct{}
mpagenkoc26d4c02021-05-06 14:27:57 +0000180 isWaitingForAdapterDlResponse bool
mpagenkoc26d4c02021-05-06 14:27:57 +0000181 chOnuDlReady chan bool
mpagenkoc26d4c02021-05-06 14:27:57 +0000182 activateImage bool
183 commitImage bool
mpagenko38662d02021-08-11 09:45:19 +0000184 mutexAbortRequest sync.RWMutex
185 abortRequested voltha.ImageState_ImageFailureReason
186 conditionalCancelRequested bool
mpagenko45586762021-10-01 08:30:22 +0000187 upgradePhase tUpgradePhase
mpagenkoaa3afe92021-05-21 16:20:58 +0000188 volthaDownloadState voltha.ImageState_ImageDownloadState
189 volthaDownloadReason voltha.ImageState_ImageFailureReason
190 volthaImageState voltha.ImageState_ImageActivationState
Holger Hildebrandt288d9472021-09-06 10:47:53 +0000191 isEndSwDlOpen bool
mpagenko45586762021-10-01 08:30:22 +0000192 chReceiveAbortEndSwDlResponse chan tEndSwDlResponseResult
Holger Hildebrandt5458d892022-05-31 09:52:06 +0000193 isExtendedOmci bool
194 omciDownloadSectionSize int64
mpagenko80622a52021-02-09 16:53:23 +0000195}
196
197//NewOnuUpgradeFsm is the 'constructor' for the state machine to config the PON ANI ports
198// of ONU UNI ports via OMCI
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000199func NewOnuUpgradeFsm(ctx context.Context, apDeviceHandler cmn.IdeviceHandler,
200 apDevEntry cmn.IonuDeviceEntry, apOnuDB *devdb.OnuDeviceDB,
201 aRequestEvent cmn.OnuDeviceEvent, aName string, aCommChannel chan cmn.Message) *OnuUpgradeFsm {
mpagenko80622a52021-02-09 16:53:23 +0000202 instFsm := &OnuUpgradeFsm{
Holger Hildebrandt5458d892022-05-31 09:52:06 +0000203 pDeviceHandler: apDeviceHandler,
204 deviceID: apDeviceHandler.GetDeviceID(),
205 pDevEntry: apDevEntry,
206 pOmciCC: apDevEntry.GetDevOmciCC(),
207 pOnuDB: apOnuDB,
208 requestEvent: aRequestEvent,
209 omciSectionInterleaveDelay: cOmciSectionInterleaveMilliseconds,
210 downloadToOnuTimeout4MB: apDeviceHandler.GetDlToOnuTimeout4M(),
211 waitCountEndSwDl: cWaitCountEndSwDl,
212 waitDelayEndSwDl: cWaitDelayEndSwDlSeconds,
213 upgradePhase: cUpgradeUndefined,
214 volthaDownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN,
215 volthaDownloadReason: voltha.ImageState_NO_ERROR,
216 volthaImageState: voltha.ImageState_IMAGE_UNKNOWN,
217 abortRequested: voltha.ImageState_NO_ERROR,
mpagenko80622a52021-02-09 16:53:23 +0000218 }
mpagenko59498c12021-03-18 14:15:15 +0000219 instFsm.chReceiveExpectedResponse = make(chan bool)
mpagenkoc26d4c02021-05-06 14:27:57 +0000220 instFsm.chAdapterDlReady = make(chan bool)
mpagenko609a69a2021-12-02 08:12:58 +0000221 instFsm.chAbortDelayEndSwDl = make(chan struct{})
mpagenkoc26d4c02021-05-06 14:27:57 +0000222 instFsm.chOnuDlReady = make(chan bool)
mpagenko45586762021-10-01 08:30:22 +0000223 instFsm.chReceiveAbortEndSwDlResponse = make(chan tEndSwDlResponseResult)
mpagenko80622a52021-02-09 16:53:23 +0000224
Holger Hildebrandt5458d892022-05-31 09:52:06 +0000225 instFsm.isExtendedOmci = instFsm.pDevEntry.GetPersIsExtOmciSupported()
226 if instFsm.isExtendedOmci {
227 instFsm.omciDownloadSectionSize = cOmciDownloadSectionSizeExtLine
228 instFsm.omciDownloadWindowSizeLimit = cOmciDownloadWindowSizeLimitExtLine
229 } else {
230 instFsm.omciDownloadSectionSize = cOmciDownloadSectionSizeBaseLine
231 instFsm.omciDownloadWindowSizeLimit = cOmciDownloadWindowSizeLimitBaseLine
232 }
233
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000234 instFsm.PAdaptFsm = cmn.NewAdapterFsm(aName, instFsm.deviceID, aCommChannel)
235 if instFsm.PAdaptFsm == nil {
mpagenko80622a52021-02-09 16:53:23 +0000236 logger.Errorw(ctx, "OnuUpgradeFsm's AdapterFsm could not be instantiated!!", log.Fields{
237 "device-id": instFsm.deviceID})
238 return nil
239 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000240 instFsm.PAdaptFsm.PFsm = fsm.NewFSM(
241 UpgradeStDisabled,
mpagenko80622a52021-02-09 16:53:23 +0000242 fsm.Events{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000243 {Name: UpgradeEvStart, Src: []string{UpgradeStDisabled}, Dst: UpgradeStStarting},
244 {Name: UpgradeEvAdapterDownload, Src: []string{UpgradeStStarting}, Dst: UpgradeStWaitingAdapterDL},
245 {Name: UpgradeEvPrepareSwDownload, Src: []string{UpgradeStStarting, UpgradeStWaitingAdapterDL}, Dst: UpgradeStPreparingDL},
246 {Name: UpgradeEvRxStartSwDownload, Src: []string{UpgradeStPreparingDL}, Dst: UpgradeStDLSection},
247 {Name: UpgradeEvWaitWindowAck, Src: []string{UpgradeStDLSection}, Dst: UpgradeStVerifyWindow},
248 {Name: UpgradeEvContinueNextWindow, Src: []string{UpgradeStVerifyWindow}, Dst: UpgradeStDLSection},
249 {Name: UpgradeEvEndSwDownload, Src: []string{UpgradeStVerifyWindow}, Dst: UpgradeStFinalizeDL},
250 {Name: UpgradeEvWaitEndDownload, Src: []string{UpgradeStFinalizeDL}, Dst: UpgradeStWaitEndDL},
251 {Name: UpgradeEvContinueFinalize, Src: []string{UpgradeStWaitEndDL}, Dst: UpgradeStFinalizeDL},
252 //UpgradeStCheckImageName only used with useAPIVersion43
253 {Name: UpgradeEvCheckImageName, Src: []string{UpgradeStWaitEndDL}, Dst: UpgradeStCheckImageName},
254 //UpgradeEvWaitForActivate state transitions depend on useAPIVersion43
255 {Name: UpgradeEvWaitForActivate, Src: []string{UpgradeStWaitEndDL, UpgradeStCheckImageName}, Dst: UpgradeStWaitForActivate},
256 //UpgradeEvRequestActivate state transitions depend on useAPIVersion43
257 {Name: UpgradeEvRequestActivate, Src: []string{UpgradeStStarting, UpgradeStWaitEndDL, UpgradeStCheckImageName,
258 UpgradeStWaitForActivate}, Dst: UpgradeStRequestingActivate}, //allows also for direct activation (without download) [TODO!!!]
259 {Name: UpgradeEvActivationDone, Src: []string{UpgradeStRequestingActivate}, Dst: UpgradeStActivated},
260 {Name: UpgradeEvWaitForCommit, Src: []string{UpgradeStRequestingActivate}, Dst: UpgradeStWaitForCommit},
261 {Name: UpgradeEvCommitSw, Src: []string{UpgradeStStarting, UpgradeStRequestingActivate, UpgradeStWaitForCommit,
262 UpgradeStActivated}, Dst: UpgradeStCommitSw}, //allows also for direct commitment (without download) [TODO!!!]
263 {Name: UpgradeEvCheckCommitted, Src: []string{UpgradeStCommitSw}, Dst: UpgradeStCheckCommitted},
mpagenko80622a52021-02-09 16:53:23 +0000264
265 /*
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000266 {Name: UpgradeEvTimeoutSimple, Src: []string{
267 UpgradeStCreatingDot1PMapper, UpgradeStCreatingMBPCD, UpgradeStSettingTconts, UpgradeStSettingDot1PMapper}, Dst: UpgradeStStarting},
268 {Name: UpgradeEvTimeoutMids, Src: []string{
269 UpgradeStCreatingGemNCTPs, UpgradeStCreatingGemIWs, UpgradeStSettingPQs}, Dst: UpgradeStStarting},
mpagenko80622a52021-02-09 16:53:23 +0000270 */
271 // exceptional treatments
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000272 //on UpgradeEvReset: UpgradeStRequestingActivate, UpgradeStWaitForCommit and UpgradeStActivated are not reset
mpagenko1f8e8822021-06-25 14:10:21 +0000273 // (to let the FSM survive the expected OnuDown indication)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000274 {Name: UpgradeEvReset, Src: []string{UpgradeStStarting, UpgradeStWaitingAdapterDL, UpgradeStPreparingDL, UpgradeStDLSection,
275 UpgradeStVerifyWindow, UpgradeStDLSection, UpgradeStFinalizeDL, UpgradeStWaitEndDL, UpgradeStCheckImageName,
276 UpgradeStWaitForActivate,
mpagenko45586762021-10-01 08:30:22 +0000277 UpgradeStCommitSw, UpgradeStCheckCommitted, UpgradeStAbortingDL},
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000278 Dst: UpgradeStResetting},
279 {Name: UpgradeEvAbort, Src: []string{UpgradeStStarting, UpgradeStWaitingAdapterDL, UpgradeStPreparingDL, UpgradeStDLSection,
280 UpgradeStVerifyWindow, UpgradeStDLSection, UpgradeStFinalizeDL, UpgradeStWaitEndDL, UpgradeStCheckImageName,
281 UpgradeStWaitForActivate,
282 UpgradeStRequestingActivate, UpgradeStActivated, UpgradeStWaitForCommit,
283 UpgradeStCommitSw, UpgradeStCheckCommitted},
284 Dst: UpgradeStResetting},
285 {Name: UpgradeEvAbortSwDownload, Src: []string{UpgradeStResetting}, Dst: UpgradeStAbortingDL},
286 {Name: UpgradeEvRestart, Src: []string{UpgradeStResetting, UpgradeStAbortingDL}, Dst: UpgradeStRestarting},
287 {Name: UpgradeEvDisable, Src: []string{UpgradeStRestarting}, Dst: UpgradeStDisabled},
mpagenko80622a52021-02-09 16:53:23 +0000288 },
289 fsm.Callbacks{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000290 "enter_state": func(e *fsm.Event) { instFsm.PAdaptFsm.LogFsmStateChange(ctx, e) },
291 "enter_" + UpgradeStStarting: func(e *fsm.Event) { instFsm.enterStarting(ctx, e) },
292 "enter_" + UpgradeStWaitingAdapterDL: func(e *fsm.Event) { instFsm.enterWaitingAdapterDL(ctx, e) },
293 "enter_" + UpgradeStPreparingDL: func(e *fsm.Event) { instFsm.enterPreparingDL(ctx, e) },
294 "enter_" + UpgradeStDLSection: func(e *fsm.Event) { instFsm.enterDownloadSection(ctx, e) },
295 "enter_" + UpgradeStVerifyWindow: func(e *fsm.Event) { instFsm.enterVerifyWindow(ctx, e) },
296 "enter_" + UpgradeStFinalizeDL: func(e *fsm.Event) { instFsm.enterFinalizeDL(ctx, e) },
297 "enter_" + UpgradeStWaitEndDL: func(e *fsm.Event) { instFsm.enterWaitEndDL(ctx, e) },
298 "enter_" + UpgradeStCheckImageName: func(e *fsm.Event) { instFsm.enterCheckImageName(ctx, e) },
299 "enter_" + UpgradeStRequestingActivate: func(e *fsm.Event) { instFsm.enterActivateSw(ctx, e) },
300 "enter_" + UpgradeStCommitSw: func(e *fsm.Event) { instFsm.enterCommitSw(ctx, e) },
301 "enter_" + UpgradeStCheckCommitted: func(e *fsm.Event) { instFsm.enterCheckCommitted(ctx, e) },
302 "enter_" + UpgradeStResetting: func(e *fsm.Event) { instFsm.enterResetting(ctx, e) },
303 "enter_" + UpgradeStAbortingDL: func(e *fsm.Event) { instFsm.enterAbortingDL(ctx, e) },
304 "enter_" + UpgradeStRestarting: func(e *fsm.Event) { instFsm.enterRestarting(ctx, e) },
305 "enter_" + UpgradeStDisabled: func(e *fsm.Event) { instFsm.enterDisabled(ctx, e) },
mpagenko80622a52021-02-09 16:53:23 +0000306 },
307 )
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000308 if instFsm.PAdaptFsm.PFsm == nil {
mpagenko80622a52021-02-09 16:53:23 +0000309 logger.Errorw(ctx, "OnuUpgradeFsm's Base FSM could not be instantiated!!", log.Fields{
310 "device-id": instFsm.deviceID})
311 return nil
312 }
313
314 logger.Debugw(ctx, "OnuUpgradeFsm created", log.Fields{"device-id": instFsm.deviceID})
315 return instFsm
316}
317
318//SetDownloadParams configures the needed parameters for a specific download to the ONU
mpagenkoc26d4c02021-05-06 14:27:57 +0000319// called from 'old' API Activate_image_update()
mpagenko15ff4a52021-03-02 10:09:20 +0000320func (oFsm *OnuUpgradeFsm) SetDownloadParams(ctx context.Context, aInactiveImageID uint16,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000321 apImageDsc *voltha.ImageDownload, apDownloadManager *AdapterDownloadManager) error {
322 pBaseFsm := oFsm.PAdaptFsm.PFsm
323 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoaa3afe92021-05-21 16:20:58 +0000324 oFsm.mutexUpgradeParams.Lock()
mpagenko80622a52021-02-09 16:53:23 +0000325 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting", log.Fields{
326 "device-id": oFsm.deviceID, "image-description": apImageDsc})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000327 oFsm.InactiveImageMeID = aInactiveImageID //upgrade state machines run on configured inactive ImageId
mpagenko80622a52021-02-09 16:53:23 +0000328 oFsm.pImageDsc = apImageDsc
329 oFsm.pDownloadManager = apDownloadManager
Holger Hildebrandtac1e0592021-06-03 15:16:49 +0000330 oFsm.activateImage = true
331 oFsm.commitImage = true
mpagenkoaa3afe92021-05-21 16:20:58 +0000332 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000333
334 go func(aPBaseFsm *fsm.FSM) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000335 // let the upgrade FSM proceed to PreparingDL
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000336 _ = aPBaseFsm.Event(UpgradeEvPrepareSwDownload)
mpagenko80622a52021-02-09 16:53:23 +0000337 }(pBaseFsm)
338 return nil
339 }
340 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
341 "device-id": oFsm.deviceID})
342 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
343}
344
mpagenkoc26d4c02021-05-06 14:27:57 +0000345//SetDownloadParamsAfterDownload configures the needed parameters for a specific download to the ONU according to
346// updated API interface with R2.8: start download to ONU if the image is downloaded to the adapter
347// called from 'new' API Download_onu_image
348func (oFsm *OnuUpgradeFsm) SetDownloadParamsAfterDownload(ctx context.Context, aInactiveImageID uint16,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000349 apImageRequest *voltha.DeviceImageDownloadRequest, apDownloadManager *FileDownloadManager,
Holger Hildebrandtac010732021-06-02 13:35:39 +0000350 aImageIdentifier string) error {
mpagenkoc26d4c02021-05-06 14:27:57 +0000351 oFsm.mutexUpgradeParams.Lock()
352 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000353 if oFsm.PAdaptFsm != nil {
354 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000355 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000356 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000357 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting", log.Fields{
358 "device-id": oFsm.deviceID, "image-description": apImageRequest})
359 oFsm.useAPIVersion43 = true
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000360 oFsm.InactiveImageMeID = aInactiveImageID //upgrade state machines run on configured inactive ImageId
mpagenkoc26d4c02021-05-06 14:27:57 +0000361 oFsm.pFileManager = apDownloadManager
362 oFsm.imageIdentifier = aImageIdentifier
363 oFsm.imageVersion = apImageRequest.Image.Version
364 oFsm.activateImage = apImageRequest.ActivateOnSuccess
365 oFsm.commitImage = apImageRequest.CommitOnSuccess
mpagenko45586762021-10-01 08:30:22 +0000366 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_STARTED //state change indication for download request
mpagenkoc26d4c02021-05-06 14:27:57 +0000367 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000368 _ = pBaseFsm.Event(UpgradeEvAdapterDownload) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000369 return nil
370 }
371 oFsm.mutexUpgradeParams.Unlock()
372 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
373 "device-id": oFsm.deviceID})
374 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
375}
376
377//SetActivationParamsRunning sets the activate and commit flags for a running download to the ONU according to adapters rpc call
378// called from 'new' API Activate_onu_image
379func (oFsm *OnuUpgradeFsm) SetActivationParamsRunning(ctx context.Context,
380 aImageIdentifier string, aCommit bool) error {
mpagenko45586762021-10-01 08:30:22 +0000381 logger.Debugw(ctx, "OnuUpgradeFsm activate/commit parameter setting", log.Fields{
382 "device-id": oFsm.deviceID, "image-id": aImageIdentifier, "commit": aCommit})
mpagenkoc26d4c02021-05-06 14:27:57 +0000383 oFsm.mutexUpgradeParams.Lock()
384 //set activate/commit independent from state, if FSM is already beyond concerned states, then it does not matter anyway
385 // (as long as the Imageidentifier is correct)
mpagenkoc26d4c02021-05-06 14:27:57 +0000386 if aImageIdentifier != oFsm.imageIdentifier {
387 logger.Errorw(ctx, "OnuUpgradeFsm abort: mismatching upgrade image", log.Fields{
388 "device-id": oFsm.deviceID, "request-image": aImageIdentifier, "fsm-image": oFsm.imageIdentifier})
389 oFsm.mutexUpgradeParams.Unlock()
390 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm params ignored: requested image-name not used in current upgrade for device-id: %s",
391 oFsm.deviceID))
392 }
393 oFsm.activateImage = true
394 oFsm.commitImage = aCommit
mpagenko45586762021-10-01 08:30:22 +0000395 oFsm.mutexUpgradeParams.Unlock()
mpagenkoc26d4c02021-05-06 14:27:57 +0000396 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000397 if oFsm.PAdaptFsm != nil {
398 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000399 }
400 if pBaseFsm != nil {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000401 if pBaseFsm.Is(UpgradeStWaitForActivate) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000402 logger.Debugw(ctx, "OnuUpgradeFsm finish waiting for activate", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000403 _ = pBaseFsm.Event(UpgradeEvRequestActivate) //no need to call the FSM event in background here
mpagenko38662d02021-08-11 09:45:19 +0000404 } else {
mpagenko38662d02021-08-11 09:45:19 +0000405 logger.Debugw(ctx, "OnuUpgradeFsm not (yet?) waiting for activate", log.Fields{
406 "device-id": oFsm.deviceID, "current FsmState": pBaseFsm.Current()})
mpagenkoc26d4c02021-05-06 14:27:57 +0000407 }
408 return nil
409 }
410 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer", log.Fields{
411 "device-id": oFsm.deviceID})
412 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer for device-id: %s", oFsm.deviceID))
413}
414
415//SetActivationParamsStart starts upgrade processing with immediate activation
416// called from 'new' API Activate_onu_image
417func (oFsm *OnuUpgradeFsm) SetActivationParamsStart(ctx context.Context, aImageVersion string, aInactiveImageID uint16, aCommit bool) error {
418 oFsm.mutexUpgradeParams.Lock()
419 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000420 if oFsm.PAdaptFsm != nil {
421 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000422 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000423 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000424 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting to start with activation", log.Fields{
425 "device-id": oFsm.deviceID, "image-version": aImageVersion})
426 oFsm.useAPIVersion43 = true
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000427 oFsm.InactiveImageMeID = aInactiveImageID //upgrade state machines run on configured inactive ImageId
mpagenkoc26d4c02021-05-06 14:27:57 +0000428 oFsm.imageVersion = aImageVersion
429 oFsm.activateImage = true
430 oFsm.commitImage = aCommit
mpagenko38662d02021-08-11 09:45:19 +0000431 // indicate start of the upgrade activity
mpagenko45586762021-10-01 08:30:22 +0000432 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATING //state change indication for activate request
mpagenkoc26d4c02021-05-06 14:27:57 +0000433 oFsm.mutexUpgradeParams.Unlock()
434 //directly request the FSM to activate the image
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000435 _ = pBaseFsm.Event(UpgradeEvRequestActivate) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000436 return nil
437 }
438 oFsm.mutexUpgradeParams.Unlock()
439 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
440 "device-id": oFsm.deviceID})
441 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
442}
443
444//SetCommitmentParamsRunning sets the commit flag for a running download to the ONU according to adapters rpc call
445// called from 'new' API Commit_onu_image
mpagenko38662d02021-08-11 09:45:19 +0000446func (oFsm *OnuUpgradeFsm) SetCommitmentParamsRunning(ctx context.Context,
447 aImageIdentifier string, aImageVersion string) error {
mpagenkoc26d4c02021-05-06 14:27:57 +0000448 oFsm.mutexUpgradeParams.Lock()
449 //set commit independent from state, if FSM is already beyond commit state (just ready), then it does not matter anyway
450 // (as long as the Imageidentifier is correct)
451 logger.Debugw(ctx, "OnuUpgradeFsm commit parameter setting", log.Fields{
mpagenkoa2b288f2021-10-21 11:25:27 +0000452 "device-id": oFsm.deviceID, "image-id": aImageIdentifier, "image-version": aImageVersion})
mpagenko38662d02021-08-11 09:45:19 +0000453 if (aImageIdentifier != oFsm.imageIdentifier) && (aImageVersion != oFsm.imageVersion) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000454 logger.Errorw(ctx, "OnuUpgradeFsm abort: mismatching upgrade image", log.Fields{
mpagenko38662d02021-08-11 09:45:19 +0000455 "device-id": oFsm.deviceID, "request-identifier": aImageIdentifier, "fsm-identifier": oFsm.imageIdentifier,
456 "request-version": aImageVersion, "fsm-version": oFsm.imageVersion})
mpagenkoc26d4c02021-05-06 14:27:57 +0000457 oFsm.mutexUpgradeParams.Unlock()
458 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm params ignored: requested image-name not used in current upgrade for device-id: %s",
459 oFsm.deviceID))
460 }
461 oFsm.commitImage = true
462 oFsm.mutexUpgradeParams.Unlock()
463 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000464 if oFsm.PAdaptFsm != nil {
465 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000466 }
467 if pBaseFsm != nil {
mpagenko183647c2021-06-08 15:25:04 +0000468 //let the FSM decide if it is ready to process the event
469 logger.Debugw(ctx, "OnuUpgradeFsm requesting commit",
470 log.Fields{"device-id": oFsm.deviceID, "current FsmState": pBaseFsm.Current()})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000471 _ = pBaseFsm.Event(UpgradeEvCommitSw) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000472 return nil
473 }
mpagenko38662d02021-08-11 09:45:19 +0000474 //should never occur
mpagenkoc26d4c02021-05-06 14:27:57 +0000475 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer", log.Fields{
476 "device-id": oFsm.deviceID})
477 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer for device-id: %s", oFsm.deviceID))
478}
479
480//SetCommitmentParamsStart starts upgrade processing with immediate commitment
481// called from 'new' API Commit_onu_image
482func (oFsm *OnuUpgradeFsm) SetCommitmentParamsStart(ctx context.Context, aImageVersion string, aActiveImageID uint16) error {
483 oFsm.mutexUpgradeParams.Lock()
484 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000485 if oFsm.PAdaptFsm != nil {
486 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000487 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000488 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000489 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting to start with commitment", log.Fields{
490 "device-id": oFsm.deviceID, "image-version": aImageVersion})
491 oFsm.useAPIVersion43 = true
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000492 oFsm.InactiveImageMeID = aActiveImageID //upgrade state machines inactive ImageId is the new active ImageId
mpagenkoc26d4c02021-05-06 14:27:57 +0000493 oFsm.imageVersion = aImageVersion
494 oFsm.commitImage = true
mpagenko45586762021-10-01 08:30:22 +0000495 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTING //state change indication for activate request
mpagenkoc26d4c02021-05-06 14:27:57 +0000496 oFsm.mutexUpgradeParams.Unlock()
mpagenko38662d02021-08-11 09:45:19 +0000497 //directly request the FSM to commit the image
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000498 _ = pBaseFsm.Event(UpgradeEvCommitSw) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000499 return nil
500 }
501 oFsm.mutexUpgradeParams.Unlock()
502 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
503 "device-id": oFsm.deviceID})
504 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
505}
506
mpagenko1f8e8822021-06-25 14:10:21 +0000507//GetCommitFlag delivers the commit flag that was configured here
508func (oFsm *OnuUpgradeFsm) GetCommitFlag(ctx context.Context) bool {
509 oFsm.mutexUpgradeParams.RLock()
510 defer oFsm.mutexUpgradeParams.RUnlock()
511 return oFsm.commitImage
512}
513
mpagenko38662d02021-08-11 09:45:19 +0000514//GetImageStates delivers the download/image states as per device proto buf definition
mpagenkoaa3afe92021-05-21 16:20:58 +0000515func (oFsm *OnuUpgradeFsm) GetImageStates(ctx context.Context,
mpagenko38662d02021-08-11 09:45:19 +0000516 aImageIdentifier string, aVersion string) *voltha.ImageState {
mpagenkoaa3afe92021-05-21 16:20:58 +0000517 pImageState := &voltha.ImageState{}
mpagenko38662d02021-08-11 09:45:19 +0000518 pImageState.Version = aVersion //version as requested
mpagenkoaa3afe92021-05-21 16:20:58 +0000519 // check if the request refers to some active image/version of the processing
520 oFsm.mutexUpgradeParams.RLock()
521 if (aImageIdentifier == oFsm.imageIdentifier) || (aVersion == oFsm.imageVersion) {
522 pImageState.DownloadState = oFsm.volthaDownloadState
523 pImageState.Reason = oFsm.volthaDownloadReason
524 pImageState.ImageState = oFsm.volthaImageState
525 } else {
526 pImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
527 pImageState.Reason = voltha.ImageState_NO_ERROR
528 pImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
529 }
530 oFsm.mutexUpgradeParams.RUnlock()
mpagenko38662d02021-08-11 09:45:19 +0000531 return pImageState
mpagenkoaa3afe92021-05-21 16:20:58 +0000532}
533
mpagenko38662d02021-08-11 09:45:19 +0000534//SetImageStateActive sets the FSM internal volthaImageState to ImageState_IMAGE_ACTIVE
535func (oFsm *OnuUpgradeFsm) SetImageStateActive(ctx context.Context) {
mpagenko183647c2021-06-08 15:25:04 +0000536 oFsm.mutexUpgradeParams.Lock()
537 defer oFsm.mutexUpgradeParams.Unlock()
mpagenko45586762021-10-01 08:30:22 +0000538 oFsm.upgradePhase = cUpgradeActivated
mpagenko38662d02021-08-11 09:45:19 +0000539 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVE
mpagenko38662d02021-08-11 09:45:19 +0000540}
541
mpagenkoc26d4c02021-05-06 14:27:57 +0000542//CancelProcessing ensures that suspended processing at waiting on some response is aborted and reset of FSM
mpagenko38662d02021-08-11 09:45:19 +0000543func (oFsm *OnuUpgradeFsm) CancelProcessing(ctx context.Context, abCompleteAbort bool,
544 aReason voltha.ImageState_ImageFailureReason) {
mpagenkoa2b288f2021-10-21 11:25:27 +0000545 pAdaptFsm := oFsm.PAdaptFsm
Holger Hildebrandt12609a12022-03-25 13:23:25 +0000546 logger.Debugw(ctx, "CancelProcessing entered", log.Fields{"device-id": oFsm.deviceID})
mpagenkoa2b288f2021-10-21 11:25:27 +0000547 if pAdaptFsm == nil || pAdaptFsm.PFsm == nil {
548 logger.Warnw(ctx, "OnuUpgradeFsm cancel, but FSM invalid", log.Fields{
549 "device-id": oFsm.deviceID})
550 return
551 }
552 logger.Debugw(ctx, "OnuUpgradeFsm start canceling", log.Fields{
553 "device-id": oFsm.deviceID, "in fsm-state": pAdaptFsm.PFsm.Current()})
mpagenko38662d02021-08-11 09:45:19 +0000554 oFsm.mutexAbortRequest.Lock()
555 oFsm.abortRequested = aReason //possibly abort the sectionDownload loop
556 oFsm.mutexAbortRequest.Unlock()
mpagenkoc26d4c02021-05-06 14:27:57 +0000557 //mutex protection is required for possible concurrent access to FSM members
558 //attention: for an unbuffered channel the sender is blocked until the value is received (processed)!
559 // accordingly the mutex must be released before sending to channel here (mutex acquired in receiver)
560 oFsm.mutexIsAwaitingAdapterDlResponse.RLock()
561 if oFsm.isWaitingForAdapterDlResponse {
562 oFsm.mutexIsAwaitingAdapterDlResponse.RUnlock()
563 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
564 oFsm.chAdapterDlReady <- false
565 } else {
566 oFsm.mutexIsAwaitingAdapterDlResponse.RUnlock()
567 }
mpagenko609a69a2021-12-02 08:12:58 +0000568
569 //abort a possible delaying of sending EndSwDl
570 //use asynchronous channel sending to avoid blocking here in case no receiver is waiting
571 select {
572 case oFsm.chAbortDelayEndSwDl <- struct{}{}:
573 default:
574 }
575
mpagenkoc26d4c02021-05-06 14:27:57 +0000576 //chOnuDlReady is cleared as part of the FSM reset processing (from enterResetting())
577
578 // in any case (even if it might be automatically requested by above cancellation of waiting) ensure resetting the FSM
mpagenko1f8e8822021-06-25 14:10:21 +0000579 // specific here: See definition of state changes: some states are excluded from reset for possible later commit
mpagenkoa2b288f2021-10-21 11:25:27 +0000580 pAdaptFsm = oFsm.PAdaptFsm
mpagenko59862f02021-10-11 08:53:18 +0000581 if pAdaptFsm != nil && pAdaptFsm.PFsm != nil {
mpagenkoc26d4c02021-05-06 14:27:57 +0000582 // calling FSM events in background to avoid blocking of the caller
mpagenko59862f02021-10-11 08:53:18 +0000583 go func(apFsm *fsm.FSM) {
584 if apFsm.Is(UpgradeStWaitEndDL) {
585 oFsm.chReceiveExpectedResponse <- false //which aborts the FSM in WaitEndDL state
586 } else if apFsm.Is(UpgradeStAbortingDL) {
587 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseAbort //abort waiting on EndDownloadResponse
588 }
mpagenko38662d02021-08-11 09:45:19 +0000589
mpagenko59862f02021-10-11 08:53:18 +0000590 var err error
591 if abCompleteAbort {
592 // in case of unconditional abort request the ImageState is set immediately
593 oFsm.mutexUpgradeParams.Lock()
594 //any previous lingering conditional cancelRequest is superseded by this abortion
595 oFsm.conditionalCancelRequested = false
596 oFsm.volthaDownloadReason = aReason
597 oFsm.mutexUpgradeParams.Unlock()
598 err = apFsm.Event(UpgradeEvAbort) //as unconditional default FSM cancellation
599 } else {
600 //at conditional abort request the image states are set when reaching the reset state
mpagenkoa2b288f2021-10-21 11:25:27 +0000601 oFsm.mutexUpgradeParams.Lock()
mpagenko59862f02021-10-11 08:53:18 +0000602 oFsm.conditionalCancelRequested = true
mpagenkoa2b288f2021-10-21 11:25:27 +0000603 oFsm.mutexUpgradeParams.Unlock()
mpagenko59862f02021-10-11 08:53:18 +0000604 err = apFsm.Event(UpgradeEvReset) //as state-conditional default FSM cleanup
605 }
606 if err != nil {
607 //error return is expected in case of conditional request and no state transition
608 logger.Debugw(ctx, "onu upgrade fsm could not cancel with abort/reset event", log.Fields{
609 "device-id": oFsm.deviceID, "error": err})
610 }
mpagenkoa2b288f2021-10-21 11:25:27 +0000611 logger.Debugw(ctx, "OnuUpgradeFsm canceling done", log.Fields{
612 "device-id": oFsm.deviceID})
mpagenko59862f02021-10-11 08:53:18 +0000613 }(pAdaptFsm.PFsm)
mpagenkoa2b288f2021-10-21 11:25:27 +0000614 } else { //the FSM seems already to be in some released state
615 logger.Warnw(ctx, "OnuUpgradeFsm canceling without FSM event", log.Fields{
616 "device-id": oFsm.deviceID})
617 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000618}
619
mpagenko80622a52021-02-09 16:53:23 +0000620func (oFsm *OnuUpgradeFsm) enterStarting(ctx context.Context, e *fsm.Event) {
621 logger.Debugw(ctx, "OnuUpgradeFsm start", log.Fields{"in state": e.FSM.Current(),
622 "device-id": oFsm.deviceID})
623
624 // start go routine for processing of LockState messages
625 go oFsm.processOmciUpgradeMessages(ctx)
626}
627
mpagenkoc26d4c02021-05-06 14:27:57 +0000628//enterWaitingAdapterDL state can only be reached with useAPIVersion43
629func (oFsm *OnuUpgradeFsm) enterWaitingAdapterDL(ctx context.Context, e *fsm.Event) {
630 logger.Debugw(ctx, "OnuUpgradeFsm waiting for adapter download", log.Fields{"in state": e.FSM.Current(),
631 "device-id": oFsm.deviceID})
mpagenko38662d02021-08-11 09:45:19 +0000632 syncChannel := make(chan struct{})
633 go oFsm.waitOnDownloadToAdapterReady(ctx, syncChannel, oFsm.chAdapterDlReady)
634 //block until the wait routine is really blocked on chAdapterDlReady
635 <-syncChannel
mpagenkoc26d4c02021-05-06 14:27:57 +0000636 go oFsm.pFileManager.RequestDownloadReady(ctx, oFsm.imageIdentifier, oFsm.chAdapterDlReady)
637}
638
mpagenko80622a52021-02-09 16:53:23 +0000639func (oFsm *OnuUpgradeFsm) enterPreparingDL(ctx context.Context, e *fsm.Event) {
640 logger.Debugw(ctx, "OnuUpgradeFsm prepare Download to Onu", log.Fields{"in state": e.FSM.Current(),
641 "device-id": oFsm.deviceID})
642
mpagenkoc26d4c02021-05-06 14:27:57 +0000643 var fileLen int64
644 var err error
mpagenkoaa3afe92021-05-21 16:20:58 +0000645 oFsm.mutexUpgradeParams.Lock()
mpagenkoc26d4c02021-05-06 14:27:57 +0000646 if oFsm.useAPIVersion43 {
647 //with the new API structure download to adapter is implicit and we have to wait until the image is available
648 fileLen, err = oFsm.pFileManager.GetImageBufferLen(ctx, oFsm.imageIdentifier)
649 } else {
650 fileLen, err = oFsm.pDownloadManager.getImageBufferLen(ctx, oFsm.pImageDsc.Name, oFsm.pImageDsc.LocalDir)
651 }
mpagenkoc497ee32021-11-10 17:30:20 +0000652 if err != nil || fileLen == 0 || fileLen > int64(cMaxUint32) {
mpagenko39b703e2021-08-25 13:38:40 +0000653 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto)
mpagenkoaa3afe92021-05-21 16:20:58 +0000654 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000655 logger.Errorw(ctx, "OnuUpgradeFsm abort: problems getting image buffer length", log.Fields{
656 "device-id": oFsm.deviceID, "error": err, "length": fileLen})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000657 pBaseFsm := oFsm.PAdaptFsm
mpagenko80622a52021-02-09 16:53:23 +0000658 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000659 go func(a_pAFsm *cmn.AdapterFsm) {
660 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko80622a52021-02-09 16:53:23 +0000661 }(pBaseFsm)
662 return
663 }
664
mpagenkoc26d4c02021-05-06 14:27:57 +0000665 //copy file content to buffer
mpagenkoc497ee32021-11-10 17:30:20 +0000666 var imageBuffer []byte
mpagenkoc26d4c02021-05-06 14:27:57 +0000667 if oFsm.useAPIVersion43 {
mpagenkoc497ee32021-11-10 17:30:20 +0000668 imageBuffer, err = oFsm.pFileManager.GetDownloadImageBuffer(ctx, oFsm.imageIdentifier)
mpagenkoc26d4c02021-05-06 14:27:57 +0000669 } else {
mpagenkoc497ee32021-11-10 17:30:20 +0000670 imageBuffer, err = oFsm.pDownloadManager.getDownloadImageBuffer(ctx, oFsm.pImageDsc.Name, oFsm.pImageDsc.LocalDir)
mpagenkoc26d4c02021-05-06 14:27:57 +0000671 }
mpagenko80622a52021-02-09 16:53:23 +0000672 if err != nil {
mpagenko39b703e2021-08-25 13:38:40 +0000673 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto)
mpagenkoaa3afe92021-05-21 16:20:58 +0000674 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000675 logger.Errorw(ctx, "OnuUpgradeFsm abort: can't get image buffer", log.Fields{
676 "device-id": oFsm.deviceID, "error": err})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000677 pBaseFsm := oFsm.PAdaptFsm
mpagenko80622a52021-02-09 16:53:23 +0000678 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000679 go func(a_pAFsm *cmn.AdapterFsm) {
680 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko80622a52021-02-09 16:53:23 +0000681 }(pBaseFsm)
682 return
683 }
mpagenkoc497ee32021-11-10 17:30:20 +0000684 //provide slice capacity already with the reserve of one section to avoid inflation of the slice to double size at append
Holger Hildebrandt5458d892022-05-31 09:52:06 +0000685 oFsm.imageBuffer = make([]byte, fileLen, fileLen+oFsm.omciDownloadSectionSize)
mpagenkoc497ee32021-11-10 17:30:20 +0000686 //better use a copy of the read image buffer in case the buffer/file is modified from outside,
687 // this also limits the slice len to the expected maximum fileLen
688 copy(oFsm.imageBuffer, imageBuffer)
mpagenko80622a52021-02-09 16:53:23 +0000689
Holger Hildebrandt5458d892022-05-31 09:52:06 +0000690 oFsm.noOfSections = uint32(fileLen / oFsm.omciDownloadSectionSize)
691 if fileLen%oFsm.omciDownloadSectionSize > 0 {
692 // Because the extended message format allows for variable length,
693 // software image sections are only padded in baseline message format
694 if !oFsm.isExtendedOmci {
695 bufferPadding := make([]byte, oFsm.omciDownloadSectionSize-fileLen%oFsm.omciDownloadSectionSize)
696 //expand the imageBuffer to exactly fit multiples of cOmciDownloadSectionSize with padding
697 oFsm.imageBuffer = append(oFsm.imageBuffer, bufferPadding...)
698 }
mpagenko80622a52021-02-09 16:53:23 +0000699 oFsm.noOfSections++
700 }
701 oFsm.origImageLength = uint32(fileLen)
702 oFsm.imageLength = uint32(len(oFsm.imageBuffer))
mpagenko80622a52021-02-09 16:53:23 +0000703 logger.Infow(ctx, "OnuUpgradeFsm starts with StartSwDl values", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000704 "MeId": oFsm.InactiveImageMeID, "windowSizeLimit": oFsm.omciDownloadWindowSizeLimit,
mpagenko80622a52021-02-09 16:53:23 +0000705 "ImageSize": oFsm.imageLength, "original file size": fileLen})
706 //"NumberOfCircuitPacks": oFsm.numberCircuitPacks, "CircuitPacks MeId": 0}) //parallel circuit packs download not supported
mpagenkoaa3afe92021-05-21 16:20:58 +0000707
708 oFsm.mutexUpgradeParams.Unlock()
mpagenko39b703e2021-08-25 13:38:40 +0000709
mpagenkoa2b288f2021-10-21 11:25:27 +0000710 // flush chOnuDlReady
mpagenko39b703e2021-08-25 13:38:40 +0000711 select {
712 case <-oFsm.chOnuDlReady:
713 logger.Debug(ctx, "flushed OnuDlReady channel")
714 default:
715 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000716 go oFsm.waitOnDownloadToOnuReady(ctx, oFsm.chOnuDlReady) // start supervision of the complete download-to-ONU procedure
717
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000718 err = oFsm.pOmciCC.SendStartSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
Holger Hildebrandt5458d892022-05-31 09:52:06 +0000719 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, oFsm.omciDownloadWindowSizeLimit, oFsm.origImageLength, oFsm.isExtendedOmci)
mpagenko80622a52021-02-09 16:53:23 +0000720 if err != nil {
721 logger.Errorw(ctx, "StartSwDl abort: can't send section", log.Fields{
722 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +0000723 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +0000724 return
725 }
Holger Hildebrandt288d9472021-09-06 10:47:53 +0000726 oFsm.isEndSwDlOpen = true
mpagenko80622a52021-02-09 16:53:23 +0000727}
728
729func (oFsm *OnuUpgradeFsm) enterDownloadSection(ctx context.Context, e *fsm.Event) {
730 logger.Debugw(ctx, "OnuUpgradeFsm start downloading sections", log.Fields{
731 "device-id": oFsm.deviceID, "absolute window": oFsm.nextDownloadWindow})
mpagenkoa2b288f2021-10-21 11:25:27 +0000732 //use a background routine to send the multiple download sections frames in a loop
mpagenko609a69a2021-12-02 08:12:58 +0000733 // in order to avoid blocking on synchronous event calls for the entire (long) processing time
mpagenkoa2b288f2021-10-21 11:25:27 +0000734 go oFsm.runSwDlSectionWindow(ctx)
735}
mpagenko80622a52021-02-09 16:53:23 +0000736
mpagenkoa2b288f2021-10-21 11:25:27 +0000737//runSwDlSectionWindow runs a loop to send all DlSection frames of one window in background
738// may be aborted by parallel change of abortRequested
739func (oFsm *OnuUpgradeFsm) runSwDlSectionWindow(ctx context.Context) {
mpagenko80622a52021-02-09 16:53:23 +0000740 var windowAckRequest uint8 = 0
741 var bufferStartOffset uint32
742 var bufferEndOffset uint32
743 var downloadSection []byte
kesavand011d5162021-11-25 19:21:06 +0530744
mpagenkoaa3afe92021-05-21 16:20:58 +0000745 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +0000746 oFsm.upgradePhase = cUpgradeDownloading //start of downloading image to ONU
mpagenko80622a52021-02-09 16:53:23 +0000747 if oFsm.nextDownloadSectionsAbsolute == 0 {
mpagenkoaa3afe92021-05-21 16:20:58 +0000748 oFsm.volthaImageState = voltha.ImageState_IMAGE_DOWNLOADING
mpagenko80622a52021-02-09 16:53:23 +0000749 }
kesavand011d5162021-11-25 19:21:06 +0530750 //var omuTxSecPerWindow []*common.OmciTransferStructure
751 omciMsgsPerSection := &ia.OmciMessages{}
752 //take the TID mutex
753 //To ensure the insequesnce of TIDS for all the onusw sections within a window
754 oFsm.pOmciCC.LockMutexTID()
755 defer oFsm.pOmciCC.UnLockMutexTID()
mpagenko80622a52021-02-09 16:53:23 +0000756 for {
mpagenko38662d02021-08-11 09:45:19 +0000757 oFsm.mutexAbortRequest.RLock()
758 // this way out of the section download loop on abort request
759 if oFsm.abortRequested != voltha.ImageState_NO_ERROR {
mpagenko45586762021-10-01 08:30:22 +0000760 //states are updated when entering the reset state ...
mpagenko38662d02021-08-11 09:45:19 +0000761 oFsm.volthaDownloadReason = oFsm.abortRequested
762 oFsm.mutexAbortRequest.RUnlock()
763 oFsm.mutexUpgradeParams.Unlock()
mpagenkoa2b288f2021-10-21 11:25:27 +0000764 pUpgradeFsm := oFsm.PAdaptFsm
765 if pUpgradeFsm != nil {
766 _ = pUpgradeFsm.PFsm.Event(UpgradeEvAbort)
767 logger.Debugw(ctx, "aborting runSwDlSectionWindow", log.Fields{
768 "device-id": oFsm.deviceID, "reason": oFsm.volthaDownloadReason})
769 return
770 }
771 logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
mpagenko38662d02021-08-11 09:45:19 +0000772 return
773 }
774 oFsm.mutexAbortRequest.RUnlock()
775
Holger Hildebrandt5458d892022-05-31 09:52:06 +0000776 bufferStartOffset = oFsm.nextDownloadSectionsAbsolute * uint32(oFsm.omciDownloadSectionSize)
777 bufferEndOffset = bufferStartOffset + uint32(oFsm.omciDownloadSectionSize) - 1
778 if oFsm.isExtendedOmci {
779 if bufferEndOffset > oFsm.origImageLength-1 {
780 bufferEndOffset = oFsm.origImageLength - 1
781 }
782 }
mpagenko80622a52021-02-09 16:53:23 +0000783 logger.Debugw(ctx, "DlSection values are", log.Fields{
784 "DlSectionNoAbsolute": oFsm.nextDownloadSectionsAbsolute,
785 "DlSectionWindow": oFsm.nextDownloadSectionsWindow,
786 "startOffset": bufferStartOffset, "endOffset": bufferEndOffset})
787 if bufferStartOffset+1 > oFsm.imageLength || bufferEndOffset+1 > oFsm.imageLength { //should never occur in this state
788 logger.Errorw(ctx, "OnuUpgradeFsm buffer error: exceeded length", log.Fields{
789 "device-id": oFsm.deviceID, "bufferStartOffset": bufferStartOffset,
790 "bufferEndOffset": bufferEndOffset, "imageLength": oFsm.imageLength})
mpagenko39b703e2021-08-25 13:38:40 +0000791 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto)
mpagenkoaa3afe92021-05-21 16:20:58 +0000792 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000793 //logical error -- reset the FSM
mpagenkoa2b288f2021-10-21 11:25:27 +0000794 pUpgradeFsm := oFsm.PAdaptFsm
795 if pUpgradeFsm != nil {
796 _ = pUpgradeFsm.PFsm.Event(UpgradeEvAbort)
797 return
798 }
799 logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
mpagenko80622a52021-02-09 16:53:23 +0000800 return
801 }
802 downloadSection = oFsm.imageBuffer[bufferStartOffset : bufferEndOffset+1]
803 if oFsm.nextDownloadSectionsWindow == oFsm.omciDownloadWindowSizeLimit {
804 windowAckRequest = 1
805 logger.Debugw(ctx, "DlSection expect Response for complete window", log.Fields{
806 "device-id": oFsm.deviceID, "in window": oFsm.nextDownloadWindow})
807 }
808 if oFsm.nextDownloadSectionsAbsolute+1 >= oFsm.noOfSections {
809 windowAckRequest = 1
kesavand011d5162021-11-25 19:21:06 +0530810
mpagenko15ff4a52021-03-02 10:09:20 +0000811 oFsm.omciDownloadWindowSizeLast = oFsm.nextDownloadSectionsWindow
812 logger.Infow(ctx, "DlSection expect Response for last window (section)", log.Fields{
mpagenko80622a52021-02-09 16:53:23 +0000813 "device-id": oFsm.deviceID, "DlSectionNoAbsolute": oFsm.nextDownloadSectionsAbsolute})
814 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000815 oFsm.mutexUpgradeParams.Unlock() //unlock here to give other functions some chance to process during/after the send request
kesavand011d5162021-11-25 19:21:06 +0530816 omciTxReq, err := oFsm.pOmciCC.PrepareOnuSectionsOfWindow(log.WithSpanFromContext(context.Background(), ctx),
Holger Hildebrandt5458d892022-05-31 09:52:06 +0000817 oFsm.InactiveImageMeID, windowAckRequest, oFsm.nextDownloadSectionsWindow, downloadSection, omciMsgsPerSection, oFsm.isExtendedOmci)
mpagenko80622a52021-02-09 16:53:23 +0000818 if err != nil {
819 logger.Errorw(ctx, "DlSection abort: can't send section", log.Fields{
mpagenko15ff4a52021-03-02 10:09:20 +0000820 "device-id": oFsm.deviceID, "section absolute": oFsm.nextDownloadSectionsAbsolute, "error": err})
mpagenkoa2b288f2021-10-21 11:25:27 +0000821 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +0000822 return
823 }
kesavand011d5162021-11-25 19:21:06 +0530824
mpagenkoaa3afe92021-05-21 16:20:58 +0000825 oFsm.mutexUpgradeParams.Lock()
mpagenko80622a52021-02-09 16:53:23 +0000826 oFsm.nextDownloadSectionsAbsolute++ //always increase the absolute section counter after having sent one
827 if windowAckRequest == 1 {
mpagenkoaa3afe92021-05-21 16:20:58 +0000828 oFsm.mutexUpgradeParams.Unlock()
kesavand011d5162021-11-25 19:21:06 +0530829
830 if omciTxReq.OnuSwWindow == nil {
831 logger.Errorw(ctx, "fail to send sections in a window", log.Fields{
832 "device-id": oFsm.deviceID, "section absolute": oFsm.nextDownloadSectionsAbsolute, "error": err})
833 oFsm.abortOnOmciError(ctx, false)
834 return
835 }
836
mpagenkoa2b288f2021-10-21 11:25:27 +0000837 pUpgradeFsm := oFsm.PAdaptFsm
838 if pUpgradeFsm != nil {
839 _ = pUpgradeFsm.PFsm.Event(UpgradeEvWaitWindowAck) //state transition to upgradeStVerifyWindow
kesavand011d5162021-11-25 19:21:06 +0530840 oFsm.pOmciCC.SendOnuSwSectionsWindowWithRxSupervision(ctx, omciTxReq, oFsm.pDeviceHandler.GetOmciTimeout(), oFsm.PAdaptFsm.CommChan)
mpagenkoa2b288f2021-10-21 11:25:27 +0000841 return
842 }
843 logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
mpagenko80622a52021-02-09 16:53:23 +0000844 return
845 }
kesavand011d5162021-11-25 19:21:06 +0530846
mpagenko80622a52021-02-09 16:53:23 +0000847 oFsm.nextDownloadSectionsWindow++ //increase the window related section counter only if not in the last section
mpagenko59498c12021-03-18 14:15:15 +0000848 if oFsm.omciSectionInterleaveDelay > 0 {
mpagenko80622a52021-02-09 16:53:23 +0000849 //ensure a defined intersection-time-gap to leave space for further processing, other ONU's ...
mpagenkoaa3afe92021-05-21 16:20:58 +0000850 oFsm.mutexUpgradeParams.Unlock() //unlock here to give other functions some chance to process during/after the send request
mpagenko59498c12021-03-18 14:15:15 +0000851 time.Sleep(oFsm.omciSectionInterleaveDelay * time.Millisecond)
mpagenkoaa3afe92021-05-21 16:20:58 +0000852 oFsm.mutexUpgradeParams.Lock()
mpagenko80622a52021-02-09 16:53:23 +0000853 }
854 }
mpagenkoa2b288f2021-10-21 11:25:27 +0000855} //runSwDlSectionWindow
mpagenko80622a52021-02-09 16:53:23 +0000856
857func (oFsm *OnuUpgradeFsm) enterVerifyWindow(ctx context.Context, e *fsm.Event) {
858 logger.Debugw(ctx, "OnuUpgradeFsm verify DL window ack", log.Fields{
859 "for window": oFsm.nextDownloadWindow, "device-id": oFsm.deviceID})
860}
861
862func (oFsm *OnuUpgradeFsm) enterFinalizeDL(ctx context.Context, e *fsm.Event) {
mpagenko80622a52021-02-09 16:53:23 +0000863 logger.Infow(ctx, "OnuUpgradeFsm finalize DL", log.Fields{
mpagenko59498c12021-03-18 14:15:15 +0000864 "device-id": oFsm.deviceID, "crc": strconv.FormatInt(int64(oFsm.imageCRC), 16), "delay": oFsm.delayEndSwDl})
mpagenko609a69a2021-12-02 08:12:58 +0000865 //use a background routine to wait EndSwDlDelay and then send the EndSwDl request
866 // in order to avoid blocking on synchronous event calls for the complete wait time
867 // and to allow for state transition before sending the EndSwDl request
868 go oFsm.delayAndSendEndSwDl(ctx)
869}
mpagenko80622a52021-02-09 16:53:23 +0000870
mpagenko609a69a2021-12-02 08:12:58 +0000871//delayAndSendEndSwDl ensures a delay before sending the EndSwDl request
872// may also be aborted by parallel channel reception on chAbortEndSwDl
873func (oFsm *OnuUpgradeFsm) delayAndSendEndSwDl(ctx context.Context) {
mpagenkoaa3afe92021-05-21 16:20:58 +0000874 oFsm.mutexUpgradeParams.RLock()
mpagenko80622a52021-02-09 16:53:23 +0000875 if oFsm.delayEndSwDl {
mpagenkoaa3afe92021-05-21 16:20:58 +0000876 oFsm.mutexUpgradeParams.RUnlock()
mpagenko609a69a2021-12-02 08:12:58 +0000877 //give the ONU some time for image evaluation (hoping it does not only start this evaluation on first EndSwDl itself)
878 logger.Debugw(ctx, "OnuUpgradeFsm delay EndSwDl", log.Fields{"device-id": oFsm.deviceID,
879 "duration_s": cOmciEndSwDlDelaySeconds})
880 select {
881 case <-time.After(cOmciEndSwDlDelaySeconds * time.Second):
882 logger.Warnw(ctx, "OnuUpgradeFsm start sending EndSwDl", log.Fields{
883 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier})
884 case <-oFsm.chAbortDelayEndSwDl:
885 logger.Debugw(ctx, "OnuUpgradeFsm abort request to send EndSwDl", log.Fields{"device-id": oFsm.deviceID})
886 //according further state transition is ensured by the entity that sent the abort
887 return
888 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000889 } else {
890 oFsm.mutexUpgradeParams.RUnlock()
mpagenko80622a52021-02-09 16:53:23 +0000891 }
892
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000893 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000894 if pBaseFsm == nil {
mpagenko38662d02021-08-11 09:45:19 +0000895 logger.Errorw(ctx, "EndSwDl abort: BaseFsm invalid", log.Fields{"device-id": oFsm.deviceID})
896 oFsm.mutexUpgradeParams.Lock()
mpagenko38662d02021-08-11 09:45:19 +0000897 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR
898 oFsm.mutexUpgradeParams.Unlock()
mpagenko59498c12021-03-18 14:15:15 +0000899 // Can't call FSM Event directly, decoupling it
mpagenko609a69a2021-12-02 08:12:58 +0000900 _ = pBaseFsm.PFsm.Event(UpgradeEvAbort)
mpagenko59498c12021-03-18 14:15:15 +0000901 return
902 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000903 err := oFsm.pOmciCC.SendEndSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
Holger Hildebrandt5458d892022-05-31 09:52:06 +0000904 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, oFsm.origImageLength, oFsm.imageCRC, oFsm.isExtendedOmci)
Holger Hildebrandt288d9472021-09-06 10:47:53 +0000905
mpagenko80622a52021-02-09 16:53:23 +0000906 if err != nil {
mpagenkoa2b288f2021-10-21 11:25:27 +0000907 logger.Errorw(ctx, "EndSwDl abort: error sending EndSwDl", log.Fields{
mpagenko80622a52021-02-09 16:53:23 +0000908 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +0000909 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +0000910 return
911 }
mpagenko609a69a2021-12-02 08:12:58 +0000912 //from here on sending of EndSwDl(Abort) is not needed anymore (even though response is not yet received)
913 // this avoids sending of both EndSwDl(Success) and EndSwDl(Abort) when cancellation falls just into this window
914 // the ONU must theoretically be prepared to receive none of them (in case of OMCI transfer issues) e.g. by timeout
915 oFsm.isEndSwDlOpen = false
916 // wait for the EndSwDLResponse and check, if the ONU is ready for activation
917 _ = pBaseFsm.PFsm.Event(UpgradeEvWaitEndDownload)
918} //delayAndSendEndSwDl
mpagenko59498c12021-03-18 14:15:15 +0000919
920func (oFsm *OnuUpgradeFsm) enterWaitEndDL(ctx context.Context, e *fsm.Event) {
921 logger.Infow(ctx, "OnuUpgradeFsm WaitEndDl", log.Fields{
922 "device-id": oFsm.deviceID, "wait delay": oFsm.waitDelayEndSwDl * time.Second, "wait count": oFsm.waitCountEndSwDl})
923 if oFsm.waitCountEndSwDl == 0 {
924 logger.Errorw(ctx, "WaitEndDl abort: max limit of EndSwDL reached", log.Fields{
925 "device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000926 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000927 if pBaseFsm == nil {
928 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
929 "device-id": oFsm.deviceID})
930 return
931 }
mpagenko38662d02021-08-11 09:45:19 +0000932 oFsm.mutexUpgradeParams.Lock()
mpagenko39b703e2021-08-25 13:38:40 +0000933 oFsm.volthaDownloadReason = voltha.ImageState_IMAGE_REFUSED_BY_ONU //something like 'END_DOWNLOAD_TIMEOUT' would be better (proto)
mpagenko38662d02021-08-11 09:45:19 +0000934 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000935 go func(a_pAFsm *cmn.AdapterFsm) {
936 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko59498c12021-03-18 14:15:15 +0000937 }(pBaseFsm)
938 return
939 }
940
941 oFsm.waitCountEndSwDl--
942 select {
943 case <-time.After(oFsm.waitDelayEndSwDl * time.Second):
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000944 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000945 if pBaseFsm == nil {
946 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
947 "device-id": oFsm.deviceID})
948 //FSM may be reset already from somewhere else, nothing we can do here anymore
949 return
950 }
951 //retry End SW DL
mpagenkoaa3afe92021-05-21 16:20:58 +0000952 oFsm.mutexUpgradeParams.Lock()
mpagenko59498c12021-03-18 14:15:15 +0000953 oFsm.delayEndSwDl = false //no more extra delay for the request
mpagenkoaa3afe92021-05-21 16:20:58 +0000954 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000955 go func(a_pAFsm *cmn.AdapterFsm) {
956 _ = a_pAFsm.PFsm.Event(UpgradeEvContinueFinalize)
mpagenko59498c12021-03-18 14:15:15 +0000957 }(pBaseFsm)
958 return
959 case success := <-oFsm.chReceiveExpectedResponse:
960 logger.Debugw(ctx, "WaitEndDl stop wait timer", log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +0000961 oFsm.isEndSwDlOpen = false //no request to abort of download (already finished or immediate abort)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000962 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000963 if pBaseFsm == nil {
964 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
965 "device-id": oFsm.deviceID})
966 //FSM may be reset already from somewhere else, nothing we can do here anymore
967 return
968 }
969 if success {
970 //answer received with ready indication
mpagenko38662d02021-08-11 09:45:19 +0000971 //useAPIVersion43 may not conflict in concurrency in this state function
972 if oFsm.useAPIVersion43 { // newer API usage requires verification of downloaded image version
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000973 go func(a_pAFsm *cmn.AdapterFsm) {
974 _ = a_pAFsm.PFsm.Event(UpgradeEvCheckImageName)
mpagenkoc26d4c02021-05-06 14:27:57 +0000975 }(pBaseFsm)
mpagenko38662d02021-08-11 09:45:19 +0000976 } else { // elder API usage does not support image version check -immediately consider download as successful
977 if oFsm.activateImage {
978 //immediate activation requested
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000979 go func(a_pAFsm *cmn.AdapterFsm) {
980 _ = a_pAFsm.PFsm.Event(UpgradeEvRequestActivate)
mpagenko38662d02021-08-11 09:45:19 +0000981 }(pBaseFsm)
982 } else {
983 //have to wait on explicit activation request
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000984 go func(a_pAFsm *cmn.AdapterFsm) {
985 _ = a_pAFsm.PFsm.Event(UpgradeEvWaitForActivate)
mpagenko38662d02021-08-11 09:45:19 +0000986 }(pBaseFsm)
987 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000988 }
mpagenko59498c12021-03-18 14:15:15 +0000989 return
990 }
991 //timer was aborted
mpagenko45586762021-10-01 08:30:22 +0000992 oFsm.abortOnOmciError(ctx, true)
mpagenko59498c12021-03-18 14:15:15 +0000993 return
994 }
mpagenko80622a52021-02-09 16:53:23 +0000995}
996
mpagenko38662d02021-08-11 09:45:19 +0000997func (oFsm *OnuUpgradeFsm) enterCheckImageName(ctx context.Context, e *fsm.Event) {
998 logger.Debugw(ctx, "OnuUpgradeFsm checking downloaded image name", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000999 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
Holger Hildebrandt3ac49bd2022-02-07 17:46:43 +00001000 requestedAttributes := me.AttributeValueMap{me.SoftwareImage_IsCommitted: 0, me.SoftwareImage_IsActive: 0, me.SoftwareImage_Version: ""}
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001001 meInstance, err := oFsm.pOmciCC.SendGetMe(log.WithSpanFromContext(context.Background(), ctx),
1002 me.SoftwareImageClassID, oFsm.InactiveImageMeID, requestedAttributes, oFsm.pDeviceHandler.GetOmciTimeout(),
Holger Hildebrandtd930cb22022-06-17 09:24:50 +00001003 false, oFsm.PAdaptFsm.CommChan, oFsm.isExtendedOmci)
mpagenko38662d02021-08-11 09:45:19 +00001004 if err != nil {
1005 logger.Errorw(ctx, "OnuUpgradeFsm get Software Image ME result error",
1006 log.Fields{"device-id": oFsm.deviceID, "Error": err})
mpagenko45586762021-10-01 08:30:22 +00001007 oFsm.abortOnOmciError(ctx, true)
mpagenko38662d02021-08-11 09:45:19 +00001008 return
1009 }
1010 oFsm.pLastTxMeInstance = meInstance
1011}
1012
mpagenko80622a52021-02-09 16:53:23 +00001013func (oFsm *OnuUpgradeFsm) enterActivateSw(ctx context.Context, e *fsm.Event) {
1014 logger.Infow(ctx, "OnuUpgradeFsm activate SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001015 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
mpagenko80622a52021-02-09 16:53:23 +00001016
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001017 err := oFsm.pOmciCC.SendActivateSoftware(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
Holger Hildebrandt5458d892022-05-31 09:52:06 +00001018 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, oFsm.isExtendedOmci)
mpagenko80622a52021-02-09 16:53:23 +00001019 if err != nil {
1020 logger.Errorw(ctx, "ActivateSw abort: can't send activate frame", log.Fields{
1021 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +00001022 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +00001023 return
1024 }
mpagenko38662d02021-08-11 09:45:19 +00001025 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +00001026 oFsm.upgradePhase = cUpgradeActivating //start of image activation for ONU
mpagenko38662d02021-08-11 09:45:19 +00001027 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATING
1028 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +00001029}
1030
1031func (oFsm *OnuUpgradeFsm) enterCommitSw(ctx context.Context, e *fsm.Event) {
mpagenko38662d02021-08-11 09:45:19 +00001032 logger.Debugw(ctx, "OnuUpgradeFsm start commit SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001033 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
mpagenko38662d02021-08-11 09:45:19 +00001034 //any abort request (also conditional) is still regarded as valid as the commit indication might not be possible to verify
1035 // (which is a bit problematic as the ONU might already be in committed state,
1036 // in this case (committing failed) always 'onuimage list' should be used to verify the real state (if ONU is reachable))
1037 if activeImageID, err := oFsm.pDevEntry.GetActiveImageMeID(ctx); err == nil {
mpagenkoaa3afe92021-05-21 16:20:58 +00001038 oFsm.mutexUpgradeParams.Lock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001039 if activeImageID == oFsm.InactiveImageMeID {
1040 inactiveImageID := oFsm.InactiveImageMeID
mpagenko15ff4a52021-03-02 10:09:20 +00001041 logger.Infow(ctx, "OnuUpgradeFsm commit SW", log.Fields{
mpagenkoaa3afe92021-05-21 16:20:58 +00001042 "device-id": oFsm.deviceID, "me-id": inactiveImageID}) //more efficient activeImageID with above check
1043 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTING
mpagenko45586762021-10-01 08:30:22 +00001044 oFsm.upgradePhase = cUpgradeCommitting //start of image commitment for ONU
mpagenkoaa3afe92021-05-21 16:20:58 +00001045 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001046 err := oFsm.pOmciCC.SendCommitSoftware(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
Holger Hildebrandt5458d892022-05-31 09:52:06 +00001047 oFsm.PAdaptFsm.CommChan, inactiveImageID, oFsm.isExtendedOmci) //more efficient activeImageID with above check
mpagenko15ff4a52021-03-02 10:09:20 +00001048 if err != nil {
1049 logger.Errorw(ctx, "CommitSw abort: can't send commit sw frame", log.Fields{
1050 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +00001051 oFsm.abortOnOmciError(ctx, true)
mpagenko15ff4a52021-03-02 10:09:20 +00001052 return
1053 }
1054 return
1055 }
mpagenko38662d02021-08-11 09:45:19 +00001056 oFsm.mutexUpgradeParams.Unlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001057 logger.Errorw(ctx, "OnuUpgradeFsm active ImageId <> IdToCommit", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001058 "device-id": oFsm.deviceID, "active ID": activeImageID, "to commit ID": oFsm.InactiveImageMeID})
mpagenko38662d02021-08-11 09:45:19 +00001059 } else {
1060 logger.Errorw(ctx, "OnuUpgradeFsm can't commit, no valid active image", log.Fields{
1061 "device-id": oFsm.deviceID})
mpagenko15ff4a52021-03-02 10:09:20 +00001062 }
mpagenko38662d02021-08-11 09:45:19 +00001063 oFsm.mutexUpgradeParams.Lock()
1064 oFsm.conditionalCancelRequested = false //any lingering conditional cancelRequest is superseded by this error
mpagenko38662d02021-08-11 09:45:19 +00001065 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
mpagenko38662d02021-08-11 09:45:19 +00001066 oFsm.mutexUpgradeParams.Unlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001067 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001068 pBaseFsm := oFsm.PAdaptFsm
mpagenko15ff4a52021-03-02 10:09:20 +00001069 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001070 go func(a_pAFsm *cmn.AdapterFsm) {
1071 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko15ff4a52021-03-02 10:09:20 +00001072 }(pBaseFsm)
1073}
1074
1075func (oFsm *OnuUpgradeFsm) enterCheckCommitted(ctx context.Context, e *fsm.Event) {
mpagenko38662d02021-08-11 09:45:19 +00001076 logger.Debugw(ctx, "OnuUpgradeFsm checking committed SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001077 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
Holger Hildebrandt3ac49bd2022-02-07 17:46:43 +00001078 requestedAttributes := me.AttributeValueMap{me.SoftwareImage_IsCommitted: 0, me.SoftwareImage_IsActive: 0, me.SoftwareImage_Version: ""}
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001079 meInstance, err := oFsm.pOmciCC.SendGetMe(log.WithSpanFromContext(context.Background(), ctx),
Holger Hildebrandtd930cb22022-06-17 09:24:50 +00001080 me.SoftwareImageClassID, oFsm.InactiveImageMeID, requestedAttributes,
1081 oFsm.pDeviceHandler.GetOmciTimeout(), false, oFsm.PAdaptFsm.CommChan, oFsm.isExtendedOmci)
ozgecanetsiab36ed572021-04-01 10:38:48 +03001082 if err != nil {
1083 logger.Errorw(ctx, "OnuUpgradeFsm get Software Image ME result error",
1084 log.Fields{"device-id": oFsm.deviceID, "Error": err})
mpagenko45586762021-10-01 08:30:22 +00001085 oFsm.abortOnOmciError(ctx, true)
ozgecanetsiab36ed572021-04-01 10:38:48 +03001086 return
1087 }
mpagenko15ff4a52021-03-02 10:09:20 +00001088 oFsm.pLastTxMeInstance = meInstance
mpagenko80622a52021-02-09 16:53:23 +00001089}
1090
1091func (oFsm *OnuUpgradeFsm) enterResetting(ctx context.Context, e *fsm.Event) {
1092 logger.Debugw(ctx, "OnuUpgradeFsm resetting", log.Fields{"device-id": oFsm.deviceID})
1093
mpagenko45586762021-10-01 08:30:22 +00001094 oFsm.stateUpdateOnReset(ctx)
mpagenko38662d02021-08-11 09:45:19 +00001095
mpagenkoa2b288f2021-10-21 11:25:27 +00001096 oFsm.mutexAbortRequest.Lock()
1097 //to be sure to abort a possibly still running runSwDlSectionWindow()
1098 // in case the reset was not received from cancel() and download not finished correctly
1099 oFsm.abortRequested = oFsm.volthaDownloadReason
1100 oFsm.mutexAbortRequest.Unlock()
1101
mpagenkoc26d4c02021-05-06 14:27:57 +00001102 // in case the download-to-ONU timer is still running - cancel it
mpagenko39b703e2021-08-25 13:38:40 +00001103 //use non-blocking channel (to be independent from receiver state)
1104 select {
1105 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
1106 case oFsm.chOnuDlReady <- false:
1107 default:
mpagenkoc26d4c02021-05-06 14:27:57 +00001108 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001109 pConfigUpgradeStateAFsm := oFsm.PAdaptFsm
1110 if pConfigUpgradeStateAFsm != nil {
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001111 var nextEvent string
1112 if oFsm.isEndSwDlOpen {
mpagenko45586762021-10-01 08:30:22 +00001113 if oFsm.repeatAbort {
1114 oFsm.delayEndSwDl = true //run next abort with delay
1115 } else { //initial request
1116 oFsm.delayEndSwDl = false //run next abort with no delay
1117 oFsm.waitCountEndSwDl = cWaitCountEndSwDl //init for possible repetitions
1118 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001119 nextEvent = UpgradeEvAbortSwDownload
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001120 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001121 nextEvent = UpgradeEvRestart
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001122 }
1123 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001124 go func(a_pAFsm *cmn.AdapterFsm) {
1125 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1126 _ = a_pAFsm.PFsm.Event(nextEvent)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001127 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001128 }(pConfigUpgradeStateAFsm)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001129 }
1130}
mpagenkoc26d4c02021-05-06 14:27:57 +00001131
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001132func (oFsm *OnuUpgradeFsm) enterAbortingDL(ctx context.Context, e *fsm.Event) {
1133 logger.Debugw(ctx, "OnuUpgradeFsm aborting download to ONU", log.Fields{"device-id": oFsm.deviceID})
1134
mpagenko45586762021-10-01 08:30:22 +00001135 oFsm.mutexUpgradeParams.RLock()
1136 if oFsm.delayEndSwDl {
1137 oFsm.mutexUpgradeParams.RUnlock()
1138 //give the ONU some time for image discard activities
1139 time.Sleep(cOmciEndSwDlDelaySeconds * time.Second)
1140 } else {
1141 oFsm.mutexUpgradeParams.RUnlock()
1142 }
1143
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001144 pBaseFsm := oFsm.PAdaptFsm
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001145 if pBaseFsm == nil {
1146 logger.Errorw(ctx, "OnuUpgradeFsm aborting download: BaseFsm invalid", log.Fields{"device-id": oFsm.deviceID})
1147 return
1148 }
1149 // abort the download operation by sending an end software download message with invalid CRC and image size
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001150 err := oFsm.pOmciCC.SendEndSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
Holger Hildebrandt5458d892022-05-31 09:52:06 +00001151 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, 0, 0xFFFFFFFF, oFsm.isExtendedOmci)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001152
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001153 if err != nil {
1154 logger.Errorw(ctx, "OnuUpgradeFsm aborting download: can't send EndSwDl request", log.Fields{"device-id": oFsm.deviceID})
1155 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001156 go func(a_pAFsm *cmn.AdapterFsm) {
1157 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1158 _ = a_pAFsm.PFsm.Event(UpgradeEvRestart)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001159 }
1160 }(pBaseFsm)
1161 return
1162 }
mpagenkoa2b288f2021-10-21 11:25:27 +00001163
1164 //avoid waiting in the enterXXX function here,
1165 // otherwise synchronous event calls (like from RxMessage processing) may block and block complete Rx processing then
1166 go oFsm.waitOnAbortEndSwDlResponse(ctx)
mpagenko45586762021-10-01 08:30:22 +00001167}
1168
1169//abortingDlEvaluateResponse waits for a channel indication with decision to proceed the FSM processing
1170func (oFsm *OnuUpgradeFsm) abortingDlEvaluateResponse(ctx context.Context,
1171 pBaseFsm *cmn.AdapterFsm, aResponseResult tEndSwDlResponseResult) bool {
1172 switch aResponseResult {
1173 case cEndSwDlResponseBusy: // indication for device busy, needs repetition
1174 if oFsm.waitCountEndSwDl == 0 {
1175 logger.Errorw(ctx, "aborting download: max limit of EndSwDl reached", log.Fields{
1176 "device-id": oFsm.deviceID})
1177 go func(a_pAFsm *cmn.AdapterFsm) {
1178 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1179 _ = a_pAFsm.PFsm.Event(UpgradeEvRestart) //give up and let FSM terminate
1180 }
1181 }(pBaseFsm)
1182 } else {
1183 logger.Debugw(ctx, "aborting download: re-trigger sending abort SwDl", log.Fields{
1184 "device-id": oFsm.deviceID, "counter": oFsm.waitCountEndSwDl})
1185 oFsm.waitCountEndSwDl--
1186 oFsm.repeatAbort = true //repeated request in next round
1187 go func(a_pAFsm *cmn.AdapterFsm) {
1188 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1189 _ = a_pAFsm.PFsm.Event(UpgradeEvReset) //which then re-triggers sending AbortSwDL
1190 }
1191 }(pBaseFsm)
1192 }
1193 return true
1194 case cEndSwDlResponseSuccess: // indication for success response
1195 logger.Infow(ctx, "aborting download: success response, terminating FSM", log.Fields{
1196 "device-id": oFsm.deviceID})
1197 case cEndSwDlResponseAbort: // indication for request to abort waiting for response
1198 logger.Infow(ctx, "aborting download: request to abort waiting, terminating FSM", log.Fields{
1199 "device-id": oFsm.deviceID})
1200 default:
1201 logger.Errorw(ctx, "aborting download: unknown channel indication, terminating FSM", log.Fields{
1202 "device-id": oFsm.deviceID})
1203 } //switch
1204 return false
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001205}
1206
1207func (oFsm *OnuUpgradeFsm) enterRestarting(ctx context.Context, e *fsm.Event) {
1208 logger.Debugw(ctx, "OnuUpgradeFsm restarting", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001209 pConfigUpgradeStateAFsm := oFsm.PAdaptFsm
1210 if pConfigUpgradeStateAFsm != nil {
mpagenko80622a52021-02-09 16:53:23 +00001211 // abort running message processing
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001212 fsmAbortMsg := cmn.Message{
1213 Type: cmn.TestMsg,
1214 Data: cmn.TestMessage{
1215 TestMessageVal: cmn.AbortMessageProcessing,
mpagenko80622a52021-02-09 16:53:23 +00001216 },
1217 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001218 pConfigUpgradeStateAFsm.CommChan <- fsmAbortMsg
mpagenko80622a52021-02-09 16:53:23 +00001219
1220 //try to restart the FSM to 'disabled'
1221 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001222 go func(a_pAFsm *cmn.AdapterFsm) {
1223 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1224 _ = a_pAFsm.PFsm.Event(UpgradeEvDisable)
mpagenko80622a52021-02-09 16:53:23 +00001225 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001226 }(pConfigUpgradeStateAFsm)
mpagenko80622a52021-02-09 16:53:23 +00001227 }
1228}
1229
1230func (oFsm *OnuUpgradeFsm) enterDisabled(ctx context.Context, e *fsm.Event) {
1231 logger.Debugw(ctx, "OnuUpgradeFsm enters disabled state", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001232 // no need to flush possible channels here, Upgrade FSM will be completely removed, garbage collector should find its way
mpagenko80622a52021-02-09 16:53:23 +00001233 if oFsm.pDeviceHandler != nil {
1234 //request removal of 'reference' in the Handler (completely clear the FSM and its data)
mpagenko38662d02021-08-11 09:45:19 +00001235 pLastUpgradeImageState := &voltha.ImageState{
1236 Version: oFsm.imageVersion,
1237 DownloadState: oFsm.volthaDownloadState,
1238 Reason: oFsm.volthaDownloadReason,
1239 ImageState: oFsm.volthaImageState,
1240 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001241 go oFsm.pDeviceHandler.RemoveOnuUpgradeFsm(ctx, pLastUpgradeImageState)
mpagenko80622a52021-02-09 16:53:23 +00001242 }
1243}
1244
1245func (oFsm *OnuUpgradeFsm) processOmciUpgradeMessages(ctx context.Context) { //ctx context.Context?
1246 logger.Debugw(ctx, "Start OnuUpgradeFsm Msg processing", log.Fields{"for device-id": oFsm.deviceID})
1247loop:
1248 for {
1249 // case <-ctx.Done():
1250 // logger.Info(ctx,"MibSync Msg", log.Fields{"Message handling canceled via context for device-id": oFsm.deviceID})
1251 // break loop
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001252 message, ok := <-oFsm.PAdaptFsm.CommChan
mpagenko80622a52021-02-09 16:53:23 +00001253 if !ok {
1254 logger.Info(ctx, "OnuUpgradeFsm Rx Msg - could not read from channel", log.Fields{"device-id": oFsm.deviceID})
1255 // but then we have to ensure a restart of the FSM as well - as exceptional procedure
mpagenko45586762021-10-01 08:30:22 +00001256 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +00001257 break loop
1258 }
1259 logger.Debugw(ctx, "OnuUpgradeFsm Rx Msg", log.Fields{"device-id": oFsm.deviceID})
1260
1261 switch message.Type {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001262 case cmn.TestMsg:
1263 msg, _ := message.Data.(cmn.TestMessage)
1264 if msg.TestMessageVal == cmn.AbortMessageProcessing {
mpagenko80622a52021-02-09 16:53:23 +00001265 logger.Infow(ctx, "OnuUpgradeFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.deviceID})
1266 break loop
1267 }
1268 logger.Warnw(ctx, "OnuUpgradeFsm unknown TestMessage", log.Fields{"device-id": oFsm.deviceID, "MessageVal": msg.TestMessageVal})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001269 case cmn.OMCI:
1270 msg, _ := message.Data.(cmn.OmciMessage)
mpagenko80622a52021-02-09 16:53:23 +00001271 oFsm.handleOmciOnuUpgradeMessage(ctx, msg)
1272 default:
1273 logger.Warn(ctx, "OnuUpgradeFsm Rx unknown message", log.Fields{"device-id": oFsm.deviceID,
1274 "message.Type": message.Type})
1275 }
1276 }
1277 logger.Infow(ctx, "End OnuUpgradeFsm Msg processing", log.Fields{"device-id": oFsm.deviceID})
1278}
1279
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001280func (oFsm *OnuUpgradeFsm) handleOmciOnuUpgradeMessage(ctx context.Context, msg cmn.OmciMessage) {
mpagenko80622a52021-02-09 16:53:23 +00001281 logger.Debugw(ctx, "Rx OMCI OnuUpgradeFsm Msg", log.Fields{"device-id": oFsm.deviceID,
1282 "msgType": msg.OmciMsg.MessageType})
1283
1284 switch msg.OmciMsg.MessageType {
1285 case omci.StartSoftwareDownloadResponseType:
1286 {
mpagenko45586762021-10-01 08:30:22 +00001287 oFsm.handleRxStartSwDownloadResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001288 return
1289 } //StartSoftwareDownloadResponseType
1290 case omci.DownloadSectionResponseType:
1291 {
mpagenko45586762021-10-01 08:30:22 +00001292 oFsm.handleRxSwSectionResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001293 return
mpagenko80622a52021-02-09 16:53:23 +00001294 } //DownloadSectionResponseType
1295 case omci.EndSoftwareDownloadResponseType:
1296 {
mpagenko45586762021-10-01 08:30:22 +00001297 oFsm.handleRxEndSwDownloadResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001298 return
1299 } //EndSoftwareDownloadResponseType
1300 case omci.ActivateSoftwareResponseType:
1301 {
1302 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeActivateSoftwareResponse)
1303 if msgLayer == nil {
1304 logger.Errorw(ctx, "Omci Msg layer could not be detected for ActivateSw",
1305 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001306 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001307 return
1308 }
1309 msgObj, msgOk := msgLayer.(*omci.ActivateSoftwareResponse)
1310 if !msgOk {
1311 logger.Errorw(ctx, "Omci Msg layer could not be assigned for ActivateSw",
1312 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001313 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001314 return
1315 }
1316 logger.Debugw(ctx, "OnuUpgradeFsm ActivateSwResponse data", log.Fields{
1317 "device-id": oFsm.deviceID, "data-fields": msgObj})
1318 if msgObj.Result != me.Success {
1319 logger.Errorw(ctx, "OnuUpgradeFsm ActivateSwResponse result error - later: drive FSM to abort state ?",
1320 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
mpagenko45586762021-10-01 08:30:22 +00001321 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001322 return
1323 }
mpagenko183647c2021-06-08 15:25:04 +00001324 oFsm.mutexUpgradeParams.Lock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001325 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
mpagenko38662d02021-08-11 09:45:19 +00001326 // the image is regarded as active really only after ONU reboot and according indication (ONU down/up procedure)
mpagenko183647c2021-06-08 15:25:04 +00001327 oFsm.mutexUpgradeParams.Unlock()
1328 logger.Infow(ctx, "Expected ActivateSwResponse received",
1329 log.Fields{"device-id": oFsm.deviceID, "commit": oFsm.commitImage})
1330 if oFsm.commitImage {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001331 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvWaitForCommit)
mpagenko183647c2021-06-08 15:25:04 +00001332 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001333 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvActivationDone) // let the FSM wait for external commit request
mpagenko183647c2021-06-08 15:25:04 +00001334 }
mpagenko80622a52021-02-09 16:53:23 +00001335 return
1336 }
mpagenko183647c2021-06-08 15:25:04 +00001337 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +00001338 logger.Errorw(ctx, "OnuUpgradeFsm ActivateSwResponse wrong ME instance: abort",
1339 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
mpagenko45586762021-10-01 08:30:22 +00001340 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001341 return
1342 } //ActivateSoftwareResponseType
mpagenko15ff4a52021-03-02 10:09:20 +00001343 case omci.CommitSoftwareResponseType:
1344 {
1345 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeCommitSoftwareResponse)
1346 if msgLayer == nil {
1347 logger.Errorw(ctx, "Omci Msg layer could not be detected for CommitResponse",
1348 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001349 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001350 return
1351 }
1352 msgObj, msgOk := msgLayer.(*omci.CommitSoftwareResponse)
1353 if !msgOk {
1354 logger.Errorw(ctx, "Omci Msg layer could not be assigned for CommitResponse",
1355 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001356 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001357 return
1358 }
mpagenkobf67a092021-03-17 09:52:28 +00001359 if msgObj.Result != me.Success {
mpagenko15ff4a52021-03-02 10:09:20 +00001360 logger.Errorw(ctx, "OnuUpgradeFsm SwImage CommitResponse result error - later: drive FSM to abort state ?",
1361 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
mpagenko45586762021-10-01 08:30:22 +00001362 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001363 return
mpagenkobf67a092021-03-17 09:52:28 +00001364 }
mpagenkoaa3afe92021-05-21 16:20:58 +00001365 oFsm.mutexUpgradeParams.RLock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001366 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
mpagenkoaa3afe92021-05-21 16:20:58 +00001367 oFsm.mutexUpgradeParams.RUnlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001368 logger.Debugw(ctx, "OnuUpgradeFsm Expected SwImage CommitResponse received", log.Fields{"device-id": oFsm.deviceID})
1369 //verifying committed image
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001370 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvCheckCommitted)
mpagenko15ff4a52021-03-02 10:09:20 +00001371 return
1372 }
mpagenkoaa3afe92021-05-21 16:20:58 +00001373 oFsm.mutexUpgradeParams.RUnlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001374 logger.Errorw(ctx, "OnuUpgradeFsm SwImage CommitResponse wrong ME instance: abort",
1375 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
mpagenko45586762021-10-01 08:30:22 +00001376 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001377 return
1378 } //CommitSoftwareResponseType
1379 case omci.GetResponseType:
1380 {
mpagenko45586762021-10-01 08:30:22 +00001381 oFsm.handleRxSwGetResponse(ctx, msg)
mpagenko15ff4a52021-03-02 10:09:20 +00001382 return
1383 } //GetResponseType
mpagenko80622a52021-02-09 16:53:23 +00001384 default:
1385 {
1386 logger.Errorw(ctx, "Rx OMCI unhandled MsgType",
1387 log.Fields{"omciMsgType": msg.OmciMsg.MessageType, "device-id": oFsm.deviceID})
1388 return
1389 }
1390 }
1391}
1392
mpagenko45586762021-10-01 08:30:22 +00001393func (oFsm *OnuUpgradeFsm) handleRxStartSwDownloadResponse(ctx context.Context, msg cmn.OmciMessage) {
1394 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeStartSoftwareDownloadResponse)
1395 if msgLayer == nil {
1396 logger.Errorw(ctx, "Omci Msg layer could not be detected for StartSwDlResponse",
1397 log.Fields{"device-id": oFsm.deviceID})
1398 oFsm.abortOnOmciError(ctx, false)
1399 return
1400 }
1401 msgObj, msgOk := msgLayer.(*omci.StartSoftwareDownloadResponse)
1402 if !msgOk {
1403 logger.Errorw(ctx, "Omci Msg layer could not be assigned for StartSwDlResponse",
1404 log.Fields{"device-id": oFsm.deviceID})
1405 oFsm.abortOnOmciError(ctx, false)
1406 return
1407 }
1408 logger.Debugw(ctx, "OnuUpgradeFsm StartSwDlResponse data", log.Fields{
1409 "device-id": oFsm.deviceID, "data-fields": msgObj})
1410 if msgObj.Result != me.Success {
1411 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse result error - later: drive FSM to abort state ?",
1412 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1413 oFsm.abortOnOmciError(ctx, false)
1414 return
1415 }
1416
1417 oFsm.mutexUpgradeParams.Lock()
1418 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1419 logger.Debugw(ctx, "Expected StartSwDlResponse received", log.Fields{"device-id": oFsm.deviceID})
1420 if msgObj.WindowSize != oFsm.omciDownloadWindowSizeLimit {
1421 // also response WindowSize = 0 is a valid number for used Window size 1
1422 logger.Debugw(ctx, "different StartSwDlResponse window size requested by ONU", log.Fields{
1423 "acceptedOnuWindowSizeLimit": msgObj.WindowSize, "device-id": oFsm.deviceID})
1424 oFsm.omciDownloadWindowSizeLimit = msgObj.WindowSize
1425 }
1426 oFsm.noOfWindows = oFsm.noOfSections / uint32(oFsm.omciDownloadWindowSizeLimit+1)
1427 if oFsm.noOfSections%uint32(oFsm.omciDownloadWindowSizeLimit+1) > 0 {
1428 oFsm.noOfWindows++
1429 }
1430 logger.Debugw(ctx, "OnuUpgradeFsm will use", log.Fields{
1431 "windows": oFsm.noOfWindows, "sections": oFsm.noOfSections,
1432 "at WindowSizeLimit": oFsm.omciDownloadWindowSizeLimit})
1433 oFsm.nextDownloadSectionsAbsolute = 0
1434 oFsm.nextDownloadSectionsWindow = 0
1435 oFsm.nextDownloadWindow = 0
1436
1437 oFsm.mutexUpgradeParams.Unlock()
1438 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvRxStartSwDownload)
1439 return
1440 }
1441 oFsm.mutexUpgradeParams.Unlock()
1442 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse wrong ME instance: try again (later)?",
1443 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1444 oFsm.abortOnOmciError(ctx, false)
1445} //handleRxStartSwDownloadResponse
1446
1447func (oFsm *OnuUpgradeFsm) handleRxSwSectionResponse(ctx context.Context, msg cmn.OmciMessage) {
mpagenkoa2b288f2021-10-21 11:25:27 +00001448 if oFsm.PAdaptFsm == nil {
1449 logger.Infow(ctx, "DlSectionResponse received - but FSM not really valid anymore", log.Fields{
1450 "device-id": oFsm.deviceID})
1451 return
1452 }
1453 if !oFsm.PAdaptFsm.PFsm.Is(UpgradeStVerifyWindow) {
1454 //all the processing here is only relevant if the FSM is in state upgradeStVerifyWindow
1455 // otherwise this response can be ignored (may stem from a long-processing window send activity,
1456 // which is not anymore relevant based on intermediate (cancel) state transitions)
1457 logger.Infow(ctx, "DlSectionResponse received - but ignored", log.Fields{
1458 "device-id": oFsm.deviceID, "fsm-state": oFsm.PAdaptFsm.PFsm.Current()})
1459 return
1460 }
mpagenko45586762021-10-01 08:30:22 +00001461 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeDownloadSectionResponse)
1462 if msgLayer == nil {
1463 logger.Errorw(ctx, "Omci Msg layer could not be detected for DlSectionResponse",
1464 log.Fields{"device-id": oFsm.deviceID, "omci-message": msg.OmciMsg})
1465 oFsm.abortOnOmciError(ctx, false)
1466 return
1467 }
1468 msgObj, msgOk := msgLayer.(*omci.DownloadSectionResponse)
1469 if !msgOk {
1470 logger.Errorw(ctx, "Omci Msg layer could not be assigned for DlSectionResponse",
1471 log.Fields{"device-id": oFsm.deviceID})
1472 oFsm.abortOnOmciError(ctx, false)
1473 return
1474 }
1475 logger.Debugw(ctx, "OnuUpgradeFsm DlSectionResponse Data", log.Fields{
1476 "device-id": oFsm.deviceID, "data-fields": msgObj})
1477 if msgObj.Result != me.Success {
1478 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse result error - later: repeat window once?", //TODO!!!
1479 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1480 oFsm.abortOnOmciError(ctx, false)
1481 return
1482 }
1483 oFsm.mutexUpgradeParams.Lock()
1484 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1485 sectionNumber := msgObj.SectionNumber
1486 logger.Infow(ctx, "DlSectionResponse received", log.Fields{
1487 "window section-number": sectionNumber, "window": oFsm.nextDownloadWindow, "device-id": oFsm.deviceID})
1488
1489 oFsm.nextDownloadWindow++
1490 if oFsm.nextDownloadWindow >= oFsm.noOfWindows {
1491 if sectionNumber != oFsm.omciDownloadWindowSizeLast {
1492 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse section error last window - later: repeat window once?", //TODO!!!
1493 log.Fields{"device-id": oFsm.deviceID, "actual section": sectionNumber,
1494 "expected section": oFsm.omciDownloadWindowSizeLast})
1495 oFsm.mutexUpgradeParams.Unlock()
1496 oFsm.abortOnOmciError(ctx, false)
1497 return
1498 }
1499 oFsm.delayEndSwDl = true //ensure a delay for the EndSwDl message
1500 //CRC computation for all data bytes of the file
1501 imageCRC := crc32a.Checksum(oFsm.imageBuffer[:int(oFsm.origImageLength)]) //store internal for multiple usage
1502 //revert the retrieved CRC Byte Order (seems not to deliver NetworkByteOrder)
1503 var byteSlice []byte = make([]byte, 4)
1504 binary.LittleEndian.PutUint32(byteSlice, uint32(imageCRC))
1505 oFsm.imageCRC = binary.BigEndian.Uint32(byteSlice)
1506 oFsm.mutexUpgradeParams.Unlock()
1507 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvEndSwDownload)
1508 return
1509 }
1510 if sectionNumber != oFsm.omciDownloadWindowSizeLimit {
1511 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse section error - later: repeat window once?", //TODO!!!
1512 log.Fields{"device-id": oFsm.deviceID, "actual-section": sectionNumber,
1513 "expected section": oFsm.omciDownloadWindowSizeLimit})
1514 oFsm.mutexUpgradeParams.Unlock()
1515 oFsm.abortOnOmciError(ctx, false)
1516 return
1517 }
1518 oFsm.nextDownloadSectionsWindow = 0
1519 oFsm.mutexUpgradeParams.Unlock()
1520 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvContinueNextWindow)
1521 return
1522 }
1523 oFsm.mutexUpgradeParams.Unlock()
1524 logger.Errorw(ctx, "OnuUpgradeFsm Omci StartSwDlResponse wrong ME instance: try again (later)?",
1525 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1526 oFsm.abortOnOmciError(ctx, false)
1527} //handleRxSwSectionResponse
1528
1529func (oFsm *OnuUpgradeFsm) handleRxEndSwDownloadResponse(ctx context.Context, msg cmn.OmciMessage) {
1530 inAbortingState := oFsm.PAdaptFsm.PFsm.Is(UpgradeStAbortingDL)
1531
1532 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeEndSoftwareDownloadResponse)
1533 if msgLayer == nil {
1534 logger.Errorw(ctx, "Omci Msg layer could not be detected for EndSwDlResponse",
1535 log.Fields{"device-id": oFsm.deviceID})
1536 if !inAbortingState {
1537 oFsm.abortOnOmciError(ctx, false)
1538 } //else using error log and wait for another response or 'aborting' state timeout
1539 return
1540 }
1541 msgObj, msgOk := msgLayer.(*omci.EndSoftwareDownloadResponse)
1542 if !msgOk {
1543 logger.Errorw(ctx, "Omci Msg layer could not be assigned for EndSwDlResponse",
1544 log.Fields{"device-id": oFsm.deviceID})
1545 if !inAbortingState {
1546 oFsm.abortOnOmciError(ctx, false)
1547 } //else using error log and wait for another response or 'aborting' state timeout
1548 return
1549 }
1550 logger.Debugw(ctx, "OnuUpgradeFsm EndSwDlResponse data", log.Fields{
1551 "device-id": oFsm.deviceID, "data-fields": msgObj})
1552 if msgObj.Result != me.Success {
1553 if msgObj.Result == me.DeviceBusy {
1554 //ONU indicates it is still processing the image - let the FSM just wait and then repeat the request
1555 logger.Debugw(ctx, "OnuUpgradeFsm EndSwDlResponse busy: waiting before sending new request", log.Fields{
1556 "device-id": oFsm.deviceID})
1557 if inAbortingState {
1558 //if the EndSwDl was requested from state AbortingDL then use channel to indicate ONU busy/repeat indication
1559 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseBusy //repeat abort request
1560 }
mpagenko45586762021-10-01 08:30:22 +00001561 return
1562 }
mpagenko45586762021-10-01 08:30:22 +00001563 if inAbortingState {
1564 //if the EndSwDl was requested from state AbortingDL and response is error indication
mpagenkoa2b288f2021-10-21 11:25:27 +00001565 // that would be quite strange ONU behavior, no resolution from OnuAdapter, just let the FSM go on to disabled
1566 logger.Errorw(ctx, "OnuUpgradeFsm EndSwDlResponse result error - aborting anyway",
1567 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1568 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseAbort //error indication to stop waiting on EndDownloadResponse(abort)
1569 return
1570 }
1571 //else: ONU rejects the previous download, complete upgrade is immediately aborted with error
1572 logger.Errorw(ctx, "OnuUpgradeFsm EndSwDlResponse result error - abort upgrade",
1573 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1574 oFsm.mutexUpgradeParams.Lock()
1575 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
1576 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1577 oFsm.mutexUpgradeParams.Unlock()
1578 select {
1579 case oFsm.chOnuDlReady <- false:
1580 default:
1581 }
1582 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko45586762021-10-01 08:30:22 +00001583 return
1584 }
1585 oFsm.mutexUpgradeParams.Lock()
1586 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1587 logger.Debugw(ctx, "Expected EndSwDlResponse received", log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001588 if inAbortingState {
1589 oFsm.mutexUpgradeParams.Unlock()
1590 //if the EndSwDl was requested from state AbortingDL then use channel to indicate abort acceptance
1591 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseSuccess //success
1592 return
1593 }
1594 if !oFsm.useAPIVersion43 {
1595 //in the older API version the image version check was not possible
1596 // - assume new loaded image as valid-inactive immediately
1597 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_SUCCEEDED
1598 oFsm.volthaImageState = voltha.ImageState_IMAGE_INACTIVE
1599 oFsm.mutexUpgradeParams.Unlock()
mpagenkoa2b288f2021-10-21 11:25:27 +00001600 //use non-blocking channel (to be independent from receiver state) to indicate that the download to ONU was successful
mpagenko45586762021-10-01 08:30:22 +00001601 select {
mpagenko45586762021-10-01 08:30:22 +00001602 case oFsm.chOnuDlReady <- true:
1603 default:
1604 }
1605 } else {
1606 oFsm.mutexUpgradeParams.Unlock()
1607 }
1608 //use asynchronous channel sending to let the FSM proceed
1609 select {
1610 case oFsm.chReceiveExpectedResponse <- true:
1611 default:
1612 }
1613 return
1614 }
1615 oFsm.mutexUpgradeParams.Unlock()
1616 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse wrong ME instance: ignoring",
1617 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1618 // no state abort in case of unexpected ImageId, just keep waiting for the correct one
1619} //handleRxEndSwDownloadResponse
1620
1621func (oFsm *OnuUpgradeFsm) handleRxSwGetResponse(ctx context.Context, msg cmn.OmciMessage) {
1622 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeGetResponse)
1623 if msgLayer == nil {
1624 logger.Errorw(ctx, "Omci Msg layer could not be detected for SwImage GetResponse",
1625 log.Fields{"device-id": oFsm.deviceID})
1626 oFsm.abortOnOmciError(ctx, false)
1627 return
1628 }
1629 msgObj, msgOk := msgLayer.(*omci.GetResponse)
1630 if !msgOk {
1631 logger.Errorw(ctx, "Omci Msg layer could not be assigned for SwImage GetResponse",
1632 log.Fields{"device-id": oFsm.deviceID})
1633 oFsm.abortOnOmciError(ctx, false)
1634 return
1635 }
1636 logger.Debugw(ctx, "OnuUpgradeFsm SwImage GetResponse data", log.Fields{
1637 "device-id": oFsm.deviceID, "data-fields": msgObj})
1638 if msgObj.EntityClass == oFsm.pLastTxMeInstance.GetClassID() &&
1639 msgObj.EntityInstance == oFsm.pLastTxMeInstance.GetEntityID() {
1640 if msgObj.Result != me.Success {
1641 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse result error - later: drive FSM to abort state ?",
1642 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1643 oFsm.abortOnOmciError(ctx, false)
1644 return
1645 }
1646 } else {
1647 logger.Warnw(ctx, "OnuUpgradeFsm SwImage unexpected Entity GetResponse data - ignore",
1648 log.Fields{"device-id": oFsm.deviceID})
1649 return
1650 }
mpagenko45586762021-10-01 08:30:22 +00001651 meAttributes := msgObj.Attributes
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +00001652 var imageVersion string
1653 var imageIsCommitted, imageIsActive uint8
mpagenko45586762021-10-01 08:30:22 +00001654
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +00001655 if softwareImageIsCommitted, ok := meAttributes[me.SoftwareImage_IsCommitted]; ok {
1656 if softwareImageIsActiveimage, ok := meAttributes[me.SoftwareImage_IsActive]; ok {
1657 if softwareImageVersion, ok := meAttributes[me.SoftwareImage_Version]; ok {
1658 imageIsCommitted = softwareImageIsCommitted.(uint8)
1659 imageIsActive = softwareImageIsActiveimage.(uint8)
1660 imageVersion = cmn.TrimStringFromMeOctet(softwareImageVersion)
1661 logger.Debugw(ctx, "OnuUpgradeFsm - GetResponse Data for SoftwareImage",
1662 log.Fields{"device-id": oFsm.deviceID, "entityID": msgObj.EntityInstance,
1663 "version": imageVersion, "isActive": imageIsActive, "isCommitted": imageIsCommitted})
1664 } else {
1665 logger.Errorw(ctx,
1666 "OnuUpgradeFsm - Not all mandatory attributes present in in SoftwareImage instance - handling stopped!",
1667 log.Fields{"device-id": oFsm.deviceID})
1668 oFsm.abortOnOmciError(ctx, false)
1669 return
1670 }
1671 }
1672 }
mpagenko45586762021-10-01 08:30:22 +00001673 if oFsm.PAdaptFsm.PFsm.Current() == UpgradeStCheckImageName {
1674 //image name check after EndSwDownload, this state (and block) can only be taken if APIVersion43 is used
1675 oFsm.verifyOnuSwStatusAfterDownload(ctx, msgObj.EntityInstance, imageVersion, imageIsActive, imageIsCommitted)
1676 return
1677 }
1678
1679 //assumed only relevant state here is upgradeStCheckCommitted
1680 oFsm.mutexUpgradeParams.Lock()
1681 oFsm.conditionalCancelRequested = false //getting here any set (conditional) cancelRequest is not relevant anymore
1682 if msgObj.EntityInstance == oFsm.InactiveImageMeID && imageIsActive == cmn.SwIsActive {
1683 //a check on the delivered image version is not done, the ONU delivered version might be different from what might have been
1684 // indicated in the download image version string (version must be part of the image content itself)
1685 // so checking that might be quite unreliable
1686 //but with new API this was changed, assumption is that omci image version is known at download request and exactly that is used
1687 // in all the API references, so it can and should be checked here now
1688 if oFsm.useAPIVersion43 {
1689 if imageVersion != oFsm.imageVersion {
1690 //new active version indicated on OMCI from ONU is not the expected version
1691 logger.Errorw(ctx, "OnuUpgradeFsm image-version not matching the requested upgrade",
1692 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance,
1693 "onu-version": imageVersion, "expected-version": oFsm.imageVersion})
1694 // TODO!!!: error treatment?
1695 //TODO!!!: possibly send event information for aborted upgrade (aborted by wrong version)?
1696 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE //something like 'UNEXPECTED_VERSION' would be better - proto def
1697 oFsm.mutexUpgradeParams.Unlock()
1698 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1699 return
1700 }
1701 logger.Debugw(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU",
1702 log.Fields{"device-id": oFsm.deviceID})
1703 }
1704 if imageIsCommitted == cmn.SwIsCommitted {
1705 oFsm.upgradePhase = cUpgradeCommitted
1706 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTED
1707 //store the new commit flag to onuSwImageIndications (to keep them in sync)
1708 oFsm.pDevEntry.ModifySwImageActiveCommit(ctx, imageIsCommitted)
1709 logger.Infow(ctx, "requested SW image committed, releasing OnuUpgrade", log.Fields{"device-id": oFsm.deviceID})
1710 //deviceProcStatusUpdate not used anymore,
1711 // replaced by transferring the last (more) upgrade state information within removeOnuUpgradeFsm
1712 oFsm.mutexUpgradeParams.Unlock()
1713 //releasing the upgrade FSM on success
1714 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1715 return
1716 }
1717 //if not committed, abort upgrade as failed. There is no implementation here that would trigger this test again
1718 }
1719 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1720 oFsm.mutexUpgradeParams.Unlock()
1721 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse indications not matching requested upgrade",
1722 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1723 // TODO!!!: error treatment?
1724 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
1725 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1726} //handleRxSwGetResponse
1727
1728func (oFsm *OnuUpgradeFsm) verifyOnuSwStatusAfterDownload(ctx context.Context, aInstanceID uint16,
1729 aImageVersion string, aImageIsActive uint8, aImageIsCommitted uint8) {
1730 oFsm.mutexUpgradeParams.Lock()
1731 if aInstanceID == oFsm.InactiveImageMeID && aImageIsActive == cmn.SwIsInactive &&
1732 aImageIsCommitted == cmn.SwIsUncommitted {
1733 if aImageVersion != oFsm.imageVersion {
1734 //new stored inactive version indicated on OMCI from ONU is not the expected version
1735 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse version indication not matching requested upgrade",
1736 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": aInstanceID,
1737 "onu-version": aImageVersion, "expected-version": oFsm.imageVersion})
1738 //download state is set when entering the reset state
1739 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE //something like 'UNEXPECTED_VERSION' would be better - proto def
1740 oFsm.mutexUpgradeParams.Unlock()
1741 //stop the running ONU download timer
1742 //use non-blocking channel (to be independent from receiver state)
1743 select {
1744 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
1745 case oFsm.chOnuDlReady <- false:
1746 default:
1747 }
1748 // TODO!!!: error treatment?
1749 //TODO!!!: possibly send event information for aborted upgrade (aborted by wrong version)?
1750 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1751 return
1752 }
1753 //with APIVersion43 this is the point to consider the newly loaded image as valid (and inactive)
1754 oFsm.upgradePhase = cUpgradeDownloaded
1755 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_SUCCEEDED
1756 oFsm.volthaImageState = voltha.ImageState_IMAGE_INACTIVE
1757 //store the new inactive version to onuSwImageIndications (to keep them in sync)
1758 oFsm.pDevEntry.ModifySwImageInactiveVersion(ctx, oFsm.imageVersion)
1759 //proceed within upgrade FSM
1760 if oFsm.activateImage {
1761 //immediate activation requested
1762 oFsm.mutexUpgradeParams.Unlock()
1763 logger.Debugw(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU, continue with activation",
1764 log.Fields{"device-id": oFsm.deviceID})
1765 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvRequestActivate)
1766 } else {
1767 //have to wait on explicit activation request
1768 oFsm.mutexUpgradeParams.Unlock()
1769 logger.Infow(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU, wait for activate request",
1770 log.Fields{"device-id": oFsm.deviceID})
1771 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvWaitForActivate)
1772 }
1773 //use non-blocking channel (to be independent from receiver state)
1774 select {
1775 //use non-blocking channel to indicate that the download to ONU was successful
1776 case oFsm.chOnuDlReady <- true:
1777 default:
1778 }
1779 return
1780 }
1781 //not the expected image/image state
1782 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1783 oFsm.mutexUpgradeParams.Unlock()
1784 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse indications not matching requested upgrade",
1785 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": aInstanceID})
1786 // TODO!!!: error treatment?
1787 //TODO!!!: possibly send event information for aborted upgrade (aborted by ONU state indication)?
1788 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1789} //verifyOnuSwStatusAfterDownload
1790
1791//abortOnOmciError aborts the upgrade processing with evAbort
1792// asynchronous/synchronous based on parameter aAsync
1793func (oFsm *OnuUpgradeFsm) abortOnOmciError(ctx context.Context, aAsync bool) {
mpagenko38662d02021-08-11 09:45:19 +00001794 oFsm.mutexUpgradeParams.Lock()
1795 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
mpagenko38662d02021-08-11 09:45:19 +00001796 oFsm.volthaDownloadReason = voltha.ImageState_OMCI_TRANSFER_ERROR
mpagenko38662d02021-08-11 09:45:19 +00001797 oFsm.mutexUpgradeParams.Unlock()
1798 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001799 if oFsm.PAdaptFsm != nil {
mpagenko38662d02021-08-11 09:45:19 +00001800 var err error
1801 if aAsync { //asynchronous call requested to ensure state transition
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001802 go func(a_pAFsm *cmn.AdapterFsm) {
1803 if a_pAFsm.PFsm != nil {
1804 err = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko38662d02021-08-11 09:45:19 +00001805 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001806 }(oFsm.PAdaptFsm)
mpagenko38662d02021-08-11 09:45:19 +00001807 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001808 if oFsm.PAdaptFsm.PFsm != nil {
1809 err = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko38662d02021-08-11 09:45:19 +00001810 }
1811 }
1812 if err != nil {
1813 logger.Warnw(ctx, "onu upgrade fsm could not abort on omci error", log.Fields{
1814 "device-id": oFsm.deviceID, "error": err})
1815 }
1816 }
1817}
1818
mpagenkoc26d4c02021-05-06 14:27:57 +00001819//waitOnDownloadToAdapterReady state can only be reached with useAPIVersion43 (usage of pFileManager)
mpagenko38662d02021-08-11 09:45:19 +00001820// precondition: mutexIsAwaitingAdapterDlResponse is lockek on call
1821func (oFsm *OnuUpgradeFsm) waitOnDownloadToAdapterReady(ctx context.Context, aSyncChannel chan<- struct{},
1822 aWaitChannel chan bool) {
mpagenkoc26d4c02021-05-06 14:27:57 +00001823 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
mpagenko38662d02021-08-11 09:45:19 +00001824 downloadToAdapterTimeout := oFsm.pFileManager.GetDownloadTimeout(ctx)
mpagenkoc26d4c02021-05-06 14:27:57 +00001825 oFsm.isWaitingForAdapterDlResponse = true
1826 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenko38662d02021-08-11 09:45:19 +00001827 aSyncChannel <- struct{}{}
mpagenko80622a52021-02-09 16:53:23 +00001828 select {
1829 // maybe be also some outside cancel (but no context modeled for the moment ...)
1830 // case <-ctx.Done():
mpagenkoc26d4c02021-05-06 14:27:57 +00001831 // logger.Infow("OnuUpgradeFsm-waitOnDownloadToAdapterReady canceled", log.Fields{"for device-id": oFsm.deviceID})
1832 case <-time.After(downloadToAdapterTimeout): //10s should be enough for downloading some image to the adapter
1833 logger.Warnw(ctx, "OnuUpgradeFsm Waiting-adapter-download timeout", log.Fields{
1834 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier, "timeout": downloadToAdapterTimeout})
1835 oFsm.pFileManager.RemoveReadyRequest(ctx, oFsm.imageIdentifier, aWaitChannel)
mpagenko39b703e2021-08-25 13:38:40 +00001836 //running into timeout here may still have the download to adapter active -> abort
1837 oFsm.pFileManager.CancelDownload(ctx, oFsm.imageIdentifier)
mpagenkoc26d4c02021-05-06 14:27:57 +00001838 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1839 oFsm.isWaitingForAdapterDlResponse = false
1840 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenko39b703e2021-08-25 13:38:40 +00001841 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +00001842 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
mpagenko39b703e2021-08-25 13:38:40 +00001843 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'DOWNLOAD_TO_ADAPTER_TIMEOUT' would be better (proto)
mpagenko39b703e2021-08-25 13:38:40 +00001844 oFsm.mutexUpgradeParams.Unlock()
1845 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001846 if oFsm.PAdaptFsm != nil && oFsm.PAdaptFsm.PFsm != nil {
1847 err := oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko39b703e2021-08-25 13:38:40 +00001848 if err != nil {
1849 logger.Warnw(ctx, "onu upgrade fsm could not abort on omci error", log.Fields{
1850 "device-id": oFsm.deviceID, "error": err})
1851 }
1852 }
mpagenkoc26d4c02021-05-06 14:27:57 +00001853 return
1854
1855 case success := <-aWaitChannel:
1856 if success {
1857 logger.Debugw(ctx, "OnuUpgradeFsm image-downloaded received", log.Fields{"device-id": oFsm.deviceID})
1858 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1859 oFsm.isWaitingForAdapterDlResponse = false
1860 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
1861 //let the upgrade process proceed
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001862 pUpgradeFsm := oFsm.PAdaptFsm
mpagenkoc26d4c02021-05-06 14:27:57 +00001863 if pUpgradeFsm != nil {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001864 _ = pUpgradeFsm.PFsm.Event(UpgradeEvPrepareSwDownload)
mpagenkoc26d4c02021-05-06 14:27:57 +00001865 } else {
1866 logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
1867 }
1868 return
1869 }
mpagenko39b703e2021-08-25 13:38:40 +00001870 // waiting was aborted (assumed here to be caused by
1871 // error detection or cancel at download after upgrade FSM reset/abort with according image states set there)
mpagenkoc26d4c02021-05-06 14:27:57 +00001872 logger.Debugw(ctx, "OnuUpgradeFsm Waiting-adapter-download aborted", log.Fields{"device-id": oFsm.deviceID})
1873 oFsm.pFileManager.RemoveReadyRequest(ctx, oFsm.imageIdentifier, aWaitChannel)
1874 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1875 oFsm.isWaitingForAdapterDlResponse = false
1876 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenkoc26d4c02021-05-06 14:27:57 +00001877 return
mpagenko80622a52021-02-09 16:53:23 +00001878 }
1879}
mpagenkoc26d4c02021-05-06 14:27:57 +00001880
1881//waitOnDownloadToOnuReady state can only be reached with useAPIVersion43 (usage of pFileManager)
1882func (oFsm *OnuUpgradeFsm) waitOnDownloadToOnuReady(ctx context.Context, aWaitChannel chan bool) {
1883 downloadToOnuTimeout := time.Duration(1+(oFsm.imageLength/0x400000)) * oFsm.downloadToOnuTimeout4MB
1884 logger.Debugw(ctx, "OnuUpgradeFsm start download-to-ONU timer", log.Fields{"device-id": oFsm.deviceID,
1885 "duration": downloadToOnuTimeout})
mpagenkoc26d4c02021-05-06 14:27:57 +00001886 select {
1887 // maybe be also some outside cancel (but no context modeled for the moment ...)
1888 // case <-ctx.Done():
1889 // logger.Infow("OnuUpgradeFsm-waitOnDownloadToOnuReady canceled", log.Fields{"for device-id": oFsm.deviceID})
1890 case <-time.After(downloadToOnuTimeout): //using an image-size depending timout (in minutes)
1891 logger.Warnw(ctx, "OnuUpgradeFsm Waiting-ONU-download timeout", log.Fields{
1892 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier, "timeout": downloadToOnuTimeout})
mpagenkoc26d4c02021-05-06 14:27:57 +00001893 //the upgrade process has to be aborted
mpagenko45586762021-10-01 08:30:22 +00001894 oFsm.abortOnOmciError(ctx, false)
mpagenkoc26d4c02021-05-06 14:27:57 +00001895 return
1896
1897 case success := <-aWaitChannel:
1898 if success {
1899 logger.Debugw(ctx, "OnuUpgradeFsm image-downloaded on ONU received", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001900 //all fine, let the FSM proceed like defined from the sender of this event
1901 return
1902 }
1903 // waiting was aborted (assumed here to be caused by
mpagenko39b703e2021-08-25 13:38:40 +00001904 // error detection or cancel at download after upgrade FSM reset/abort with according image states set there)
mpagenkoc26d4c02021-05-06 14:27:57 +00001905 logger.Debugw(ctx, "OnuUpgradeFsm Waiting-ONU-download aborted", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001906 return
1907 }
1908}
mpagenko45586762021-10-01 08:30:22 +00001909
mpagenkoa2b288f2021-10-21 11:25:27 +00001910//waitOnAbortEndSwDlResponse waits for either abort/success or timeout of EndSwDownload (for abortion)
1911func (oFsm *OnuUpgradeFsm) waitOnAbortEndSwDlResponse(ctx context.Context) {
1912 logger.Debugw(ctx, "OnuUpgradeFsm start wait for EndSwDl response (abort)", log.Fields{"device-id": oFsm.deviceID})
1913 select {
1914 case <-time.After(oFsm.pOmciCC.GetMaxOmciTimeoutWithRetries() * time.Second):
1915 logger.Warnw(ctx, "OnuUpgradeFsm aborting download: timeout - no response received", log.Fields{"device-id": oFsm.deviceID})
1916 pUpgradeFsm := oFsm.PAdaptFsm
1917 if pUpgradeFsm != nil {
1918 _ = pUpgradeFsm.PFsm.Event(UpgradeEvRestart)
1919 } else {
1920 logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
1921 }
1922 return
1923 case response := <-oFsm.chReceiveAbortEndSwDlResponse:
1924 logger.Debugw(ctx, "OnuUpgradeFsm aborting download: response received",
1925 log.Fields{"device-id": oFsm.deviceID, "response": response})
1926 pUpgradeFsm := oFsm.PAdaptFsm
1927 if pUpgradeFsm != nil {
1928 if oFsm.abortingDlEvaluateResponse(ctx, pUpgradeFsm, response) {
1929 return //event sent from function already
1930 }
1931 _ = pUpgradeFsm.PFsm.Event(UpgradeEvRestart)
1932 } else {
1933 logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
1934 }
1935 return
1936 } //select
1937}
1938
mpagenko45586762021-10-01 08:30:22 +00001939//stateUpdateOnReset writes the download and/or image state on entering the reset state according to FSM internal indications
1940func (oFsm *OnuUpgradeFsm) stateUpdateOnReset(ctx context.Context) {
1941 oFsm.mutexUpgradeParams.Lock()
1942 defer oFsm.mutexUpgradeParams.Unlock()
1943 if !oFsm.conditionalCancelRequested {
1944 switch oFsm.upgradePhase {
1945 case cUpgradeUndefined, cUpgradeDownloading: //coming from downloading
1946 //make sure the download state is only changed in case the device has still been downloading
1947 if oFsm.volthaDownloadReason == voltha.ImageState_CANCELLED_ON_REQUEST {
1948 // indication for termination on request
1949 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_CANCELLED
1950 } else if oFsm.volthaDownloadReason != voltha.ImageState_NO_ERROR {
1951 // indication for termination on failure
1952 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_FAILED
1953 }
1954 //reset the image state from Downloading in this case
1955 oFsm.volthaImageState = voltha.ImageState_IMAGE_UNKNOWN //something like 'IMAGE_DOWNLOAD_ABORTED' would be better (proto)
1956 //in all other upgrade phases the last set download state remains valid
1957 case cUpgradeActivating:
1958 //reset the image state from Activating in this case
1959 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATION_ABORTED
1960 case cUpgradeCommitting: // indication for request to abort waiting for response
1961 //reset the image state from Activating in this case
1962 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMIT_ABORTED
1963 //default: in all other upgrade phases keep the last set imageState
1964 } //switch
1965 } else {
1966 //when reaching reset state with conditional cancel that can only result from ONU related problems
1967 // (mostly ONU down indication) - derived from resetFsms call
1968 // and it can only be related to the downloading-to-ONU phase (no need to check that additionally)
1969 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_FAILED
1970 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1971 //reset the image state from Downloading in this case
1972 oFsm.volthaImageState = voltha.ImageState_IMAGE_UNKNOWN //something like 'IMAGE_DOWNLOAD_ABORTED' would be better (proto)
1973 }
1974}
Holger Hildebrandte7cc6092022-02-01 11:37:03 +00001975
1976// PrepareForGarbageCollection - remove references to prepare for garbage collection
1977func (oFsm *OnuUpgradeFsm) PrepareForGarbageCollection(ctx context.Context, aDeviceID string) {
1978 logger.Debugw(ctx, "prepare for garbage collection", log.Fields{"device-id": aDeviceID})
1979 oFsm.pDeviceHandler = nil
1980 oFsm.pDownloadManager = nil
1981 oFsm.pFileManager = nil
1982 oFsm.pDevEntry = nil
1983 oFsm.pOmciCC = nil
1984}