blob: 64b529981ce0275333289bd608a79c4e1e692245 [file] [log] [blame]
mpagenko80622a52021-02-09 16:53:23 +00001/*
Joey Armstrong89c812c2024-01-12 19:00:20 -05002 * Copyright 2020-2024 Open Networking Foundation (ONF) and the ONF Contributors
mpagenko80622a52021-02-09 16:53:23 +00003 *
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
Joey Armstrong89c812c2024-01-12 19:00:20 -050017// Package swupg provides the utilities for onu sw upgrade
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000018package 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
Joey Armstrong89c812c2024-01-12 19:00:20 -0500137// COnuUpgradeFsmIdleState - required definition for IdleState detection for activities on OMCI
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000138const COnuUpgradeFsmIdleState = UpgradeStWaitForCommit
mpagenko80622a52021-02-09 16:53:23 +0000139
Joey Armstrong89c812c2024-01-12 19:00:20 -0500140// OnuUpgradeFsm defines the structure for the state machine to config the PON ANI ports of ONU UNI ports via OMCI
mpagenko80622a52021-02-09 16:53:23 +0000141type 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
Joey Armstrong89c812c2024-01-12 19:00:20 -0500197// NewOnuUpgradeFsm is the 'constructor' for the state machine to config the PON ANI ports
198//
199// of ONU UNI ports via OMCI
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000200func NewOnuUpgradeFsm(ctx context.Context, apDeviceHandler cmn.IdeviceHandler,
201 apDevEntry cmn.IonuDeviceEntry, apOnuDB *devdb.OnuDeviceDB,
202 aRequestEvent cmn.OnuDeviceEvent, aName string, aCommChannel chan cmn.Message) *OnuUpgradeFsm {
mpagenko80622a52021-02-09 16:53:23 +0000203 instFsm := &OnuUpgradeFsm{
Holger Hildebrandt5458d892022-05-31 09:52:06 +0000204 pDeviceHandler: apDeviceHandler,
205 deviceID: apDeviceHandler.GetDeviceID(),
206 pDevEntry: apDevEntry,
207 pOmciCC: apDevEntry.GetDevOmciCC(),
208 pOnuDB: apOnuDB,
209 requestEvent: aRequestEvent,
210 omciSectionInterleaveDelay: cOmciSectionInterleaveMilliseconds,
211 downloadToOnuTimeout4MB: apDeviceHandler.GetDlToOnuTimeout4M(),
212 waitCountEndSwDl: cWaitCountEndSwDl,
213 waitDelayEndSwDl: cWaitDelayEndSwDlSeconds,
214 upgradePhase: cUpgradeUndefined,
215 volthaDownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN,
216 volthaDownloadReason: voltha.ImageState_NO_ERROR,
217 volthaImageState: voltha.ImageState_IMAGE_UNKNOWN,
218 abortRequested: voltha.ImageState_NO_ERROR,
mpagenko80622a52021-02-09 16:53:23 +0000219 }
mpagenko59498c12021-03-18 14:15:15 +0000220 instFsm.chReceiveExpectedResponse = make(chan bool)
mpagenkoc26d4c02021-05-06 14:27:57 +0000221 instFsm.chAdapterDlReady = make(chan bool)
mpagenko609a69a2021-12-02 08:12:58 +0000222 instFsm.chAbortDelayEndSwDl = make(chan struct{})
mpagenkoc26d4c02021-05-06 14:27:57 +0000223 instFsm.chOnuDlReady = make(chan bool)
mpagenko45586762021-10-01 08:30:22 +0000224 instFsm.chReceiveAbortEndSwDlResponse = make(chan tEndSwDlResponseResult)
mpagenko80622a52021-02-09 16:53:23 +0000225
Holger Hildebrandt5458d892022-05-31 09:52:06 +0000226 instFsm.isExtendedOmci = instFsm.pDevEntry.GetPersIsExtOmciSupported()
227 if instFsm.isExtendedOmci {
228 instFsm.omciDownloadSectionSize = cOmciDownloadSectionSizeExtLine
229 instFsm.omciDownloadWindowSizeLimit = cOmciDownloadWindowSizeLimitExtLine
230 } else {
231 instFsm.omciDownloadSectionSize = cOmciDownloadSectionSizeBaseLine
232 instFsm.omciDownloadWindowSizeLimit = cOmciDownloadWindowSizeLimitBaseLine
233 }
234
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000235 instFsm.PAdaptFsm = cmn.NewAdapterFsm(aName, instFsm.deviceID, aCommChannel)
236 if instFsm.PAdaptFsm == nil {
mpagenko80622a52021-02-09 16:53:23 +0000237 logger.Errorw(ctx, "OnuUpgradeFsm's AdapterFsm could not be instantiated!!", log.Fields{
238 "device-id": instFsm.deviceID})
239 return nil
240 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000241 instFsm.PAdaptFsm.PFsm = fsm.NewFSM(
242 UpgradeStDisabled,
mpagenko80622a52021-02-09 16:53:23 +0000243 fsm.Events{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000244 {Name: UpgradeEvStart, Src: []string{UpgradeStDisabled}, Dst: UpgradeStStarting},
245 {Name: UpgradeEvAdapterDownload, Src: []string{UpgradeStStarting}, Dst: UpgradeStWaitingAdapterDL},
246 {Name: UpgradeEvPrepareSwDownload, Src: []string{UpgradeStStarting, UpgradeStWaitingAdapterDL}, Dst: UpgradeStPreparingDL},
247 {Name: UpgradeEvRxStartSwDownload, Src: []string{UpgradeStPreparingDL}, Dst: UpgradeStDLSection},
248 {Name: UpgradeEvWaitWindowAck, Src: []string{UpgradeStDLSection}, Dst: UpgradeStVerifyWindow},
249 {Name: UpgradeEvContinueNextWindow, Src: []string{UpgradeStVerifyWindow}, Dst: UpgradeStDLSection},
250 {Name: UpgradeEvEndSwDownload, Src: []string{UpgradeStVerifyWindow}, Dst: UpgradeStFinalizeDL},
251 {Name: UpgradeEvWaitEndDownload, Src: []string{UpgradeStFinalizeDL}, Dst: UpgradeStWaitEndDL},
252 {Name: UpgradeEvContinueFinalize, Src: []string{UpgradeStWaitEndDL}, Dst: UpgradeStFinalizeDL},
253 //UpgradeStCheckImageName only used with useAPIVersion43
254 {Name: UpgradeEvCheckImageName, Src: []string{UpgradeStWaitEndDL}, Dst: UpgradeStCheckImageName},
255 //UpgradeEvWaitForActivate state transitions depend on useAPIVersion43
256 {Name: UpgradeEvWaitForActivate, Src: []string{UpgradeStWaitEndDL, UpgradeStCheckImageName}, Dst: UpgradeStWaitForActivate},
257 //UpgradeEvRequestActivate state transitions depend on useAPIVersion43
258 {Name: UpgradeEvRequestActivate, Src: []string{UpgradeStStarting, UpgradeStWaitEndDL, UpgradeStCheckImageName,
259 UpgradeStWaitForActivate}, Dst: UpgradeStRequestingActivate}, //allows also for direct activation (without download) [TODO!!!]
260 {Name: UpgradeEvActivationDone, Src: []string{UpgradeStRequestingActivate}, Dst: UpgradeStActivated},
261 {Name: UpgradeEvWaitForCommit, Src: []string{UpgradeStRequestingActivate}, Dst: UpgradeStWaitForCommit},
262 {Name: UpgradeEvCommitSw, Src: []string{UpgradeStStarting, UpgradeStRequestingActivate, UpgradeStWaitForCommit,
263 UpgradeStActivated}, Dst: UpgradeStCommitSw}, //allows also for direct commitment (without download) [TODO!!!]
264 {Name: UpgradeEvCheckCommitted, Src: []string{UpgradeStCommitSw}, Dst: UpgradeStCheckCommitted},
mpagenko80622a52021-02-09 16:53:23 +0000265
266 /*
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000267 {Name: UpgradeEvTimeoutSimple, Src: []string{
268 UpgradeStCreatingDot1PMapper, UpgradeStCreatingMBPCD, UpgradeStSettingTconts, UpgradeStSettingDot1PMapper}, Dst: UpgradeStStarting},
269 {Name: UpgradeEvTimeoutMids, Src: []string{
270 UpgradeStCreatingGemNCTPs, UpgradeStCreatingGemIWs, UpgradeStSettingPQs}, Dst: UpgradeStStarting},
mpagenko80622a52021-02-09 16:53:23 +0000271 */
272 // exceptional treatments
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000273 //on UpgradeEvReset: UpgradeStRequestingActivate, UpgradeStWaitForCommit and UpgradeStActivated are not reset
mpagenko1f8e8822021-06-25 14:10:21 +0000274 // (to let the FSM survive the expected OnuDown indication)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000275 {Name: UpgradeEvReset, Src: []string{UpgradeStStarting, UpgradeStWaitingAdapterDL, UpgradeStPreparingDL, UpgradeStDLSection,
276 UpgradeStVerifyWindow, UpgradeStDLSection, UpgradeStFinalizeDL, UpgradeStWaitEndDL, UpgradeStCheckImageName,
277 UpgradeStWaitForActivate,
mpagenko45586762021-10-01 08:30:22 +0000278 UpgradeStCommitSw, UpgradeStCheckCommitted, UpgradeStAbortingDL},
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000279 Dst: UpgradeStResetting},
280 {Name: UpgradeEvAbort, Src: []string{UpgradeStStarting, UpgradeStWaitingAdapterDL, UpgradeStPreparingDL, UpgradeStDLSection,
281 UpgradeStVerifyWindow, UpgradeStDLSection, UpgradeStFinalizeDL, UpgradeStWaitEndDL, UpgradeStCheckImageName,
282 UpgradeStWaitForActivate,
283 UpgradeStRequestingActivate, UpgradeStActivated, UpgradeStWaitForCommit,
284 UpgradeStCommitSw, UpgradeStCheckCommitted},
285 Dst: UpgradeStResetting},
286 {Name: UpgradeEvAbortSwDownload, Src: []string{UpgradeStResetting}, Dst: UpgradeStAbortingDL},
287 {Name: UpgradeEvRestart, Src: []string{UpgradeStResetting, UpgradeStAbortingDL}, Dst: UpgradeStRestarting},
288 {Name: UpgradeEvDisable, Src: []string{UpgradeStRestarting}, Dst: UpgradeStDisabled},
mpagenko80622a52021-02-09 16:53:23 +0000289 },
290 fsm.Callbacks{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000291 "enter_state": func(e *fsm.Event) { instFsm.PAdaptFsm.LogFsmStateChange(ctx, e) },
292 "enter_" + UpgradeStStarting: func(e *fsm.Event) { instFsm.enterStarting(ctx, e) },
293 "enter_" + UpgradeStWaitingAdapterDL: func(e *fsm.Event) { instFsm.enterWaitingAdapterDL(ctx, e) },
294 "enter_" + UpgradeStPreparingDL: func(e *fsm.Event) { instFsm.enterPreparingDL(ctx, e) },
295 "enter_" + UpgradeStDLSection: func(e *fsm.Event) { instFsm.enterDownloadSection(ctx, e) },
296 "enter_" + UpgradeStVerifyWindow: func(e *fsm.Event) { instFsm.enterVerifyWindow(ctx, e) },
297 "enter_" + UpgradeStFinalizeDL: func(e *fsm.Event) { instFsm.enterFinalizeDL(ctx, e) },
298 "enter_" + UpgradeStWaitEndDL: func(e *fsm.Event) { instFsm.enterWaitEndDL(ctx, e) },
299 "enter_" + UpgradeStCheckImageName: func(e *fsm.Event) { instFsm.enterCheckImageName(ctx, e) },
300 "enter_" + UpgradeStRequestingActivate: func(e *fsm.Event) { instFsm.enterActivateSw(ctx, e) },
301 "enter_" + UpgradeStCommitSw: func(e *fsm.Event) { instFsm.enterCommitSw(ctx, e) },
302 "enter_" + UpgradeStCheckCommitted: func(e *fsm.Event) { instFsm.enterCheckCommitted(ctx, e) },
303 "enter_" + UpgradeStResetting: func(e *fsm.Event) { instFsm.enterResetting(ctx, e) },
304 "enter_" + UpgradeStAbortingDL: func(e *fsm.Event) { instFsm.enterAbortingDL(ctx, e) },
305 "enter_" + UpgradeStRestarting: func(e *fsm.Event) { instFsm.enterRestarting(ctx, e) },
306 "enter_" + UpgradeStDisabled: func(e *fsm.Event) { instFsm.enterDisabled(ctx, e) },
mpagenko80622a52021-02-09 16:53:23 +0000307 },
308 )
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000309 if instFsm.PAdaptFsm.PFsm == nil {
mpagenko80622a52021-02-09 16:53:23 +0000310 logger.Errorw(ctx, "OnuUpgradeFsm's Base FSM could not be instantiated!!", log.Fields{
311 "device-id": instFsm.deviceID})
312 return nil
313 }
314
315 logger.Debugw(ctx, "OnuUpgradeFsm created", log.Fields{"device-id": instFsm.deviceID})
316 return instFsm
317}
318
Joey Armstrong89c812c2024-01-12 19:00:20 -0500319// SetDownloadParams configures the needed parameters for a specific download to the ONU
320//
321// called from 'old' API Activate_image_update()
mpagenko15ff4a52021-03-02 10:09:20 +0000322func (oFsm *OnuUpgradeFsm) SetDownloadParams(ctx context.Context, aInactiveImageID uint16,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000323 apImageDsc *voltha.ImageDownload, apDownloadManager *AdapterDownloadManager) error {
324 pBaseFsm := oFsm.PAdaptFsm.PFsm
325 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoaa3afe92021-05-21 16:20:58 +0000326 oFsm.mutexUpgradeParams.Lock()
mpagenko80622a52021-02-09 16:53:23 +0000327 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting", log.Fields{
328 "device-id": oFsm.deviceID, "image-description": apImageDsc})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000329 oFsm.InactiveImageMeID = aInactiveImageID //upgrade state machines run on configured inactive ImageId
mpagenko80622a52021-02-09 16:53:23 +0000330 oFsm.pImageDsc = apImageDsc
331 oFsm.pDownloadManager = apDownloadManager
Holger Hildebrandtac1e0592021-06-03 15:16:49 +0000332 oFsm.activateImage = true
333 oFsm.commitImage = true
mpagenkoaa3afe92021-05-21 16:20:58 +0000334 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000335
336 go func(aPBaseFsm *fsm.FSM) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000337 // let the upgrade FSM proceed to PreparingDL
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000338 _ = aPBaseFsm.Event(UpgradeEvPrepareSwDownload)
mpagenko80622a52021-02-09 16:53:23 +0000339 }(pBaseFsm)
340 return nil
341 }
342 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
343 "device-id": oFsm.deviceID})
344 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
345}
346
Joey Armstrong89c812c2024-01-12 19:00:20 -0500347// SetDownloadParamsAfterDownload configures the needed parameters for a specific download to the ONU according to
348//
349// updated API interface with R2.8: start download to ONU if the image is downloaded to the adapter
350// called from 'new' API Download_onu_image
mpagenkoc26d4c02021-05-06 14:27:57 +0000351func (oFsm *OnuUpgradeFsm) SetDownloadParamsAfterDownload(ctx context.Context, aInactiveImageID uint16,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000352 apImageRequest *voltha.DeviceImageDownloadRequest, apDownloadManager *FileDownloadManager,
Holger Hildebrandtac010732021-06-02 13:35:39 +0000353 aImageIdentifier string) error {
mpagenkoc26d4c02021-05-06 14:27:57 +0000354 oFsm.mutexUpgradeParams.Lock()
355 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000356 if oFsm.PAdaptFsm != nil {
357 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000358 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000359 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000360 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting", log.Fields{
361 "device-id": oFsm.deviceID, "image-description": apImageRequest})
362 oFsm.useAPIVersion43 = true
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000363 oFsm.InactiveImageMeID = aInactiveImageID //upgrade state machines run on configured inactive ImageId
mpagenkoc26d4c02021-05-06 14:27:57 +0000364 oFsm.pFileManager = apDownloadManager
365 oFsm.imageIdentifier = aImageIdentifier
366 oFsm.imageVersion = apImageRequest.Image.Version
367 oFsm.activateImage = apImageRequest.ActivateOnSuccess
368 oFsm.commitImage = apImageRequest.CommitOnSuccess
mpagenko45586762021-10-01 08:30:22 +0000369 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_STARTED //state change indication for download request
mpagenkoc26d4c02021-05-06 14:27:57 +0000370 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000371 _ = pBaseFsm.Event(UpgradeEvAdapterDownload) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000372 return nil
373 }
374 oFsm.mutexUpgradeParams.Unlock()
375 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
376 "device-id": oFsm.deviceID})
377 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
378}
379
Joey Armstrong89c812c2024-01-12 19:00:20 -0500380// SetActivationParamsRunning sets the activate and commit flags for a running download to the ONU according to adapters rpc call
381//
382// called from 'new' API Activate_onu_image
mpagenkoc26d4c02021-05-06 14:27:57 +0000383func (oFsm *OnuUpgradeFsm) SetActivationParamsRunning(ctx context.Context,
384 aImageIdentifier string, aCommit bool) error {
mpagenko45586762021-10-01 08:30:22 +0000385 logger.Debugw(ctx, "OnuUpgradeFsm activate/commit parameter setting", log.Fields{
386 "device-id": oFsm.deviceID, "image-id": aImageIdentifier, "commit": aCommit})
mpagenkoc26d4c02021-05-06 14:27:57 +0000387 oFsm.mutexUpgradeParams.Lock()
388 //set activate/commit independent from state, if FSM is already beyond concerned states, then it does not matter anyway
389 // (as long as the Imageidentifier is correct)
mpagenkoc26d4c02021-05-06 14:27:57 +0000390 if aImageIdentifier != oFsm.imageIdentifier {
391 logger.Errorw(ctx, "OnuUpgradeFsm abort: mismatching upgrade image", log.Fields{
392 "device-id": oFsm.deviceID, "request-image": aImageIdentifier, "fsm-image": oFsm.imageIdentifier})
393 oFsm.mutexUpgradeParams.Unlock()
394 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm params ignored: requested image-name not used in current upgrade for device-id: %s",
395 oFsm.deviceID))
396 }
397 oFsm.activateImage = true
398 oFsm.commitImage = aCommit
mpagenko45586762021-10-01 08:30:22 +0000399 oFsm.mutexUpgradeParams.Unlock()
mpagenkoc26d4c02021-05-06 14:27:57 +0000400 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000401 if oFsm.PAdaptFsm != nil {
402 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000403 }
404 if pBaseFsm != nil {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000405 if pBaseFsm.Is(UpgradeStWaitForActivate) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000406 logger.Debugw(ctx, "OnuUpgradeFsm finish waiting for activate", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000407 _ = pBaseFsm.Event(UpgradeEvRequestActivate) //no need to call the FSM event in background here
mpagenko38662d02021-08-11 09:45:19 +0000408 } else {
mpagenko38662d02021-08-11 09:45:19 +0000409 logger.Debugw(ctx, "OnuUpgradeFsm not (yet?) waiting for activate", log.Fields{
410 "device-id": oFsm.deviceID, "current FsmState": pBaseFsm.Current()})
mpagenkoc26d4c02021-05-06 14:27:57 +0000411 }
412 return nil
413 }
414 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer", log.Fields{
415 "device-id": oFsm.deviceID})
416 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer for device-id: %s", oFsm.deviceID))
417}
418
Joey Armstrong89c812c2024-01-12 19:00:20 -0500419// SetActivationParamsStart starts upgrade processing with immediate activation
420//
421// called from 'new' API Activate_onu_image
mpagenkoc26d4c02021-05-06 14:27:57 +0000422func (oFsm *OnuUpgradeFsm) SetActivationParamsStart(ctx context.Context, aImageVersion string, aInactiveImageID uint16, aCommit bool) error {
423 oFsm.mutexUpgradeParams.Lock()
424 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000425 if oFsm.PAdaptFsm != nil {
426 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000427 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000428 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000429 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting to start with activation", log.Fields{
430 "device-id": oFsm.deviceID, "image-version": aImageVersion})
431 oFsm.useAPIVersion43 = true
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000432 oFsm.InactiveImageMeID = aInactiveImageID //upgrade state machines run on configured inactive ImageId
mpagenkoc26d4c02021-05-06 14:27:57 +0000433 oFsm.imageVersion = aImageVersion
434 oFsm.activateImage = true
435 oFsm.commitImage = aCommit
mpagenko38662d02021-08-11 09:45:19 +0000436 // indicate start of the upgrade activity
mpagenko45586762021-10-01 08:30:22 +0000437 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATING //state change indication for activate request
mpagenkoc26d4c02021-05-06 14:27:57 +0000438 oFsm.mutexUpgradeParams.Unlock()
439 //directly request the FSM to activate the image
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000440 _ = pBaseFsm.Event(UpgradeEvRequestActivate) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000441 return nil
442 }
443 oFsm.mutexUpgradeParams.Unlock()
444 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
445 "device-id": oFsm.deviceID})
446 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
447}
448
Joey Armstrong89c812c2024-01-12 19:00:20 -0500449// SetCommitmentParamsRunning sets the commit flag for a running download to the ONU according to adapters rpc call
450//
451// called from 'new' API Commit_onu_image
mpagenko38662d02021-08-11 09:45:19 +0000452func (oFsm *OnuUpgradeFsm) SetCommitmentParamsRunning(ctx context.Context,
453 aImageIdentifier string, aImageVersion string) error {
mpagenkoc26d4c02021-05-06 14:27:57 +0000454 oFsm.mutexUpgradeParams.Lock()
455 //set commit independent from state, if FSM is already beyond commit state (just ready), then it does not matter anyway
456 // (as long as the Imageidentifier is correct)
457 logger.Debugw(ctx, "OnuUpgradeFsm commit parameter setting", log.Fields{
mpagenkoa2b288f2021-10-21 11:25:27 +0000458 "device-id": oFsm.deviceID, "image-id": aImageIdentifier, "image-version": aImageVersion})
mpagenko38662d02021-08-11 09:45:19 +0000459 if (aImageIdentifier != oFsm.imageIdentifier) && (aImageVersion != oFsm.imageVersion) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000460 logger.Errorw(ctx, "OnuUpgradeFsm abort: mismatching upgrade image", log.Fields{
mpagenko38662d02021-08-11 09:45:19 +0000461 "device-id": oFsm.deviceID, "request-identifier": aImageIdentifier, "fsm-identifier": oFsm.imageIdentifier,
462 "request-version": aImageVersion, "fsm-version": oFsm.imageVersion})
mpagenkoc26d4c02021-05-06 14:27:57 +0000463 oFsm.mutexUpgradeParams.Unlock()
464 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm params ignored: requested image-name not used in current upgrade for device-id: %s",
465 oFsm.deviceID))
466 }
467 oFsm.commitImage = true
468 oFsm.mutexUpgradeParams.Unlock()
469 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000470 if oFsm.PAdaptFsm != nil {
471 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000472 }
473 if pBaseFsm != nil {
mpagenko183647c2021-06-08 15:25:04 +0000474 //let the FSM decide if it is ready to process the event
475 logger.Debugw(ctx, "OnuUpgradeFsm requesting commit",
476 log.Fields{"device-id": oFsm.deviceID, "current FsmState": pBaseFsm.Current()})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000477 _ = pBaseFsm.Event(UpgradeEvCommitSw) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000478 return nil
479 }
mpagenko38662d02021-08-11 09:45:19 +0000480 //should never occur
mpagenkoc26d4c02021-05-06 14:27:57 +0000481 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer", log.Fields{
482 "device-id": oFsm.deviceID})
483 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer for device-id: %s", oFsm.deviceID))
484}
485
Joey Armstrong89c812c2024-01-12 19:00:20 -0500486// SetCommitmentParamsStart starts upgrade processing with immediate commitment
487//
488// called from 'new' API Commit_onu_image
mpagenkoc26d4c02021-05-06 14:27:57 +0000489func (oFsm *OnuUpgradeFsm) SetCommitmentParamsStart(ctx context.Context, aImageVersion string, aActiveImageID uint16) error {
490 oFsm.mutexUpgradeParams.Lock()
491 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000492 if oFsm.PAdaptFsm != nil {
493 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000494 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000495 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000496 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting to start with commitment", log.Fields{
497 "device-id": oFsm.deviceID, "image-version": aImageVersion})
498 oFsm.useAPIVersion43 = true
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000499 oFsm.InactiveImageMeID = aActiveImageID //upgrade state machines inactive ImageId is the new active ImageId
mpagenkoc26d4c02021-05-06 14:27:57 +0000500 oFsm.imageVersion = aImageVersion
501 oFsm.commitImage = true
mpagenko45586762021-10-01 08:30:22 +0000502 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTING //state change indication for activate request
mpagenkoc26d4c02021-05-06 14:27:57 +0000503 oFsm.mutexUpgradeParams.Unlock()
mpagenko38662d02021-08-11 09:45:19 +0000504 //directly request the FSM to commit the image
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000505 _ = pBaseFsm.Event(UpgradeEvCommitSw) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000506 return nil
507 }
508 oFsm.mutexUpgradeParams.Unlock()
509 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
510 "device-id": oFsm.deviceID})
511 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
512}
513
Joey Armstrong89c812c2024-01-12 19:00:20 -0500514// GetCommitFlag delivers the commit flag that was configured here
mpagenko1f8e8822021-06-25 14:10:21 +0000515func (oFsm *OnuUpgradeFsm) GetCommitFlag(ctx context.Context) bool {
516 oFsm.mutexUpgradeParams.RLock()
517 defer oFsm.mutexUpgradeParams.RUnlock()
518 return oFsm.commitImage
519}
520
Joey Armstrong89c812c2024-01-12 19:00:20 -0500521// GetImageStates delivers the download/image states as per device proto buf definition
mpagenkoaa3afe92021-05-21 16:20:58 +0000522func (oFsm *OnuUpgradeFsm) GetImageStates(ctx context.Context,
mpagenko38662d02021-08-11 09:45:19 +0000523 aImageIdentifier string, aVersion string) *voltha.ImageState {
mpagenkoaa3afe92021-05-21 16:20:58 +0000524 pImageState := &voltha.ImageState{}
mpagenko38662d02021-08-11 09:45:19 +0000525 pImageState.Version = aVersion //version as requested
mpagenkoaa3afe92021-05-21 16:20:58 +0000526 // check if the request refers to some active image/version of the processing
527 oFsm.mutexUpgradeParams.RLock()
528 if (aImageIdentifier == oFsm.imageIdentifier) || (aVersion == oFsm.imageVersion) {
529 pImageState.DownloadState = oFsm.volthaDownloadState
530 pImageState.Reason = oFsm.volthaDownloadReason
531 pImageState.ImageState = oFsm.volthaImageState
532 } else {
533 pImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
534 pImageState.Reason = voltha.ImageState_NO_ERROR
535 pImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
536 }
537 oFsm.mutexUpgradeParams.RUnlock()
mpagenko38662d02021-08-11 09:45:19 +0000538 return pImageState
mpagenkoaa3afe92021-05-21 16:20:58 +0000539}
540
Joey Armstrong89c812c2024-01-12 19:00:20 -0500541// SetImageStateActive sets the FSM internal volthaImageState to ImageState_IMAGE_ACTIVE
mpagenko38662d02021-08-11 09:45:19 +0000542func (oFsm *OnuUpgradeFsm) SetImageStateActive(ctx context.Context) {
mpagenko183647c2021-06-08 15:25:04 +0000543 oFsm.mutexUpgradeParams.Lock()
544 defer oFsm.mutexUpgradeParams.Unlock()
mpagenko45586762021-10-01 08:30:22 +0000545 oFsm.upgradePhase = cUpgradeActivated
mpagenko38662d02021-08-11 09:45:19 +0000546 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVE
mpagenko38662d02021-08-11 09:45:19 +0000547}
548
Joey Armstrong89c812c2024-01-12 19:00:20 -0500549// CancelProcessing ensures that suspended processing at waiting on some response is aborted and reset of FSM
mpagenko38662d02021-08-11 09:45:19 +0000550func (oFsm *OnuUpgradeFsm) CancelProcessing(ctx context.Context, abCompleteAbort bool,
551 aReason voltha.ImageState_ImageFailureReason) {
mpagenkoa2b288f2021-10-21 11:25:27 +0000552 pAdaptFsm := oFsm.PAdaptFsm
Holger Hildebrandt12609a12022-03-25 13:23:25 +0000553 logger.Debugw(ctx, "CancelProcessing entered", log.Fields{"device-id": oFsm.deviceID})
mpagenkoa2b288f2021-10-21 11:25:27 +0000554 if pAdaptFsm == nil || pAdaptFsm.PFsm == nil {
555 logger.Warnw(ctx, "OnuUpgradeFsm cancel, but FSM invalid", log.Fields{
556 "device-id": oFsm.deviceID})
557 return
558 }
559 logger.Debugw(ctx, "OnuUpgradeFsm start canceling", log.Fields{
560 "device-id": oFsm.deviceID, "in fsm-state": pAdaptFsm.PFsm.Current()})
mpagenko38662d02021-08-11 09:45:19 +0000561 oFsm.mutexAbortRequest.Lock()
562 oFsm.abortRequested = aReason //possibly abort the sectionDownload loop
563 oFsm.mutexAbortRequest.Unlock()
mpagenkoc26d4c02021-05-06 14:27:57 +0000564 //mutex protection is required for possible concurrent access to FSM members
565 //attention: for an unbuffered channel the sender is blocked until the value is received (processed)!
566 // accordingly the mutex must be released before sending to channel here (mutex acquired in receiver)
567 oFsm.mutexIsAwaitingAdapterDlResponse.RLock()
568 if oFsm.isWaitingForAdapterDlResponse {
569 oFsm.mutexIsAwaitingAdapterDlResponse.RUnlock()
570 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
571 oFsm.chAdapterDlReady <- false
572 } else {
573 oFsm.mutexIsAwaitingAdapterDlResponse.RUnlock()
574 }
mpagenko609a69a2021-12-02 08:12:58 +0000575
576 //abort a possible delaying of sending EndSwDl
577 //use asynchronous channel sending to avoid blocking here in case no receiver is waiting
578 select {
579 case oFsm.chAbortDelayEndSwDl <- struct{}{}:
580 default:
581 }
582
mpagenkoc26d4c02021-05-06 14:27:57 +0000583 //chOnuDlReady is cleared as part of the FSM reset processing (from enterResetting())
584
585 // 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 +0000586 // specific here: See definition of state changes: some states are excluded from reset for possible later commit
mpagenkoa2b288f2021-10-21 11:25:27 +0000587 pAdaptFsm = oFsm.PAdaptFsm
mpagenko59862f02021-10-11 08:53:18 +0000588 if pAdaptFsm != nil && pAdaptFsm.PFsm != nil {
mpagenkoc26d4c02021-05-06 14:27:57 +0000589 // calling FSM events in background to avoid blocking of the caller
mpagenko59862f02021-10-11 08:53:18 +0000590 go func(apFsm *fsm.FSM) {
591 if apFsm.Is(UpgradeStWaitEndDL) {
592 oFsm.chReceiveExpectedResponse <- false //which aborts the FSM in WaitEndDL state
593 } else if apFsm.Is(UpgradeStAbortingDL) {
594 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseAbort //abort waiting on EndDownloadResponse
595 }
mpagenko38662d02021-08-11 09:45:19 +0000596
mpagenko59862f02021-10-11 08:53:18 +0000597 var err error
598 if abCompleteAbort {
599 // in case of unconditional abort request the ImageState is set immediately
600 oFsm.mutexUpgradeParams.Lock()
601 //any previous lingering conditional cancelRequest is superseded by this abortion
602 oFsm.conditionalCancelRequested = false
603 oFsm.volthaDownloadReason = aReason
604 oFsm.mutexUpgradeParams.Unlock()
605 err = apFsm.Event(UpgradeEvAbort) //as unconditional default FSM cancellation
606 } else {
607 //at conditional abort request the image states are set when reaching the reset state
mpagenkoa2b288f2021-10-21 11:25:27 +0000608 oFsm.mutexUpgradeParams.Lock()
mpagenko59862f02021-10-11 08:53:18 +0000609 oFsm.conditionalCancelRequested = true
mpagenkoa2b288f2021-10-21 11:25:27 +0000610 oFsm.mutexUpgradeParams.Unlock()
mpagenko59862f02021-10-11 08:53:18 +0000611 err = apFsm.Event(UpgradeEvReset) //as state-conditional default FSM cleanup
612 }
613 if err != nil {
614 //error return is expected in case of conditional request and no state transition
615 logger.Debugw(ctx, "onu upgrade fsm could not cancel with abort/reset event", log.Fields{
616 "device-id": oFsm.deviceID, "error": err})
617 }
mpagenkoa2b288f2021-10-21 11:25:27 +0000618 logger.Debugw(ctx, "OnuUpgradeFsm canceling done", log.Fields{
619 "device-id": oFsm.deviceID})
mpagenko59862f02021-10-11 08:53:18 +0000620 }(pAdaptFsm.PFsm)
mpagenkoa2b288f2021-10-21 11:25:27 +0000621 } else { //the FSM seems already to be in some released state
622 logger.Warnw(ctx, "OnuUpgradeFsm canceling without FSM event", log.Fields{
623 "device-id": oFsm.deviceID})
624 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000625}
626
mpagenko80622a52021-02-09 16:53:23 +0000627func (oFsm *OnuUpgradeFsm) enterStarting(ctx context.Context, e *fsm.Event) {
628 logger.Debugw(ctx, "OnuUpgradeFsm start", log.Fields{"in state": e.FSM.Current(),
629 "device-id": oFsm.deviceID})
630
631 // start go routine for processing of LockState messages
632 go oFsm.processOmciUpgradeMessages(ctx)
633}
634
Joey Armstrong89c812c2024-01-12 19:00:20 -0500635// enterWaitingAdapterDL state can only be reached with useAPIVersion43
mpagenkoc26d4c02021-05-06 14:27:57 +0000636func (oFsm *OnuUpgradeFsm) enterWaitingAdapterDL(ctx context.Context, e *fsm.Event) {
637 logger.Debugw(ctx, "OnuUpgradeFsm waiting for adapter download", log.Fields{"in state": e.FSM.Current(),
638 "device-id": oFsm.deviceID})
mpagenko38662d02021-08-11 09:45:19 +0000639 syncChannel := make(chan struct{})
640 go oFsm.waitOnDownloadToAdapterReady(ctx, syncChannel, oFsm.chAdapterDlReady)
641 //block until the wait routine is really blocked on chAdapterDlReady
642 <-syncChannel
mpagenkoc26d4c02021-05-06 14:27:57 +0000643 go oFsm.pFileManager.RequestDownloadReady(ctx, oFsm.imageIdentifier, oFsm.chAdapterDlReady)
644}
645
mpagenko80622a52021-02-09 16:53:23 +0000646func (oFsm *OnuUpgradeFsm) enterPreparingDL(ctx context.Context, e *fsm.Event) {
647 logger.Debugw(ctx, "OnuUpgradeFsm prepare Download to Onu", log.Fields{"in state": e.FSM.Current(),
648 "device-id": oFsm.deviceID})
649
mpagenkoc26d4c02021-05-06 14:27:57 +0000650 var fileLen int64
651 var err error
mpagenkoaa3afe92021-05-21 16:20:58 +0000652 oFsm.mutexUpgradeParams.Lock()
mpagenkoc26d4c02021-05-06 14:27:57 +0000653 if oFsm.useAPIVersion43 {
654 //with the new API structure download to adapter is implicit and we have to wait until the image is available
655 fileLen, err = oFsm.pFileManager.GetImageBufferLen(ctx, oFsm.imageIdentifier)
656 } else {
657 fileLen, err = oFsm.pDownloadManager.getImageBufferLen(ctx, oFsm.pImageDsc.Name, oFsm.pImageDsc.LocalDir)
658 }
mpagenkoc497ee32021-11-10 17:30:20 +0000659 if err != nil || fileLen == 0 || fileLen > int64(cMaxUint32) {
mpagenko39b703e2021-08-25 13:38:40 +0000660 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto)
mpagenkoaa3afe92021-05-21 16:20:58 +0000661 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000662 logger.Errorw(ctx, "OnuUpgradeFsm abort: problems getting image buffer length", log.Fields{
663 "device-id": oFsm.deviceID, "error": err, "length": fileLen})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000664 pBaseFsm := oFsm.PAdaptFsm
mpagenko80622a52021-02-09 16:53:23 +0000665 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000666 go func(a_pAFsm *cmn.AdapterFsm) {
667 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko80622a52021-02-09 16:53:23 +0000668 }(pBaseFsm)
669 return
670 }
671
mpagenkoc26d4c02021-05-06 14:27:57 +0000672 //copy file content to buffer
mpagenkoc497ee32021-11-10 17:30:20 +0000673 var imageBuffer []byte
mpagenkoc26d4c02021-05-06 14:27:57 +0000674 if oFsm.useAPIVersion43 {
mpagenkoc497ee32021-11-10 17:30:20 +0000675 imageBuffer, err = oFsm.pFileManager.GetDownloadImageBuffer(ctx, oFsm.imageIdentifier)
mpagenkoc26d4c02021-05-06 14:27:57 +0000676 } else {
mpagenkoc497ee32021-11-10 17:30:20 +0000677 imageBuffer, err = oFsm.pDownloadManager.getDownloadImageBuffer(ctx, oFsm.pImageDsc.Name, oFsm.pImageDsc.LocalDir)
mpagenkoc26d4c02021-05-06 14:27:57 +0000678 }
mpagenko80622a52021-02-09 16:53:23 +0000679 if err != nil {
mpagenko39b703e2021-08-25 13:38:40 +0000680 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto)
mpagenkoaa3afe92021-05-21 16:20:58 +0000681 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000682 logger.Errorw(ctx, "OnuUpgradeFsm abort: can't get image buffer", log.Fields{
683 "device-id": oFsm.deviceID, "error": err})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000684 pBaseFsm := oFsm.PAdaptFsm
mpagenko80622a52021-02-09 16:53:23 +0000685 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000686 go func(a_pAFsm *cmn.AdapterFsm) {
687 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko80622a52021-02-09 16:53:23 +0000688 }(pBaseFsm)
689 return
690 }
mpagenkoc497ee32021-11-10 17:30:20 +0000691 //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 +0000692 oFsm.imageBuffer = make([]byte, fileLen, fileLen+oFsm.omciDownloadSectionSize)
mpagenkoc497ee32021-11-10 17:30:20 +0000693 //better use a copy of the read image buffer in case the buffer/file is modified from outside,
694 // this also limits the slice len to the expected maximum fileLen
695 copy(oFsm.imageBuffer, imageBuffer)
mpagenko80622a52021-02-09 16:53:23 +0000696
Holger Hildebrandt5458d892022-05-31 09:52:06 +0000697 oFsm.noOfSections = uint32(fileLen / oFsm.omciDownloadSectionSize)
698 if fileLen%oFsm.omciDownloadSectionSize > 0 {
699 // Because the extended message format allows for variable length,
700 // software image sections are only padded in baseline message format
701 if !oFsm.isExtendedOmci {
702 bufferPadding := make([]byte, oFsm.omciDownloadSectionSize-fileLen%oFsm.omciDownloadSectionSize)
703 //expand the imageBuffer to exactly fit multiples of cOmciDownloadSectionSize with padding
704 oFsm.imageBuffer = append(oFsm.imageBuffer, bufferPadding...)
705 }
mpagenko80622a52021-02-09 16:53:23 +0000706 oFsm.noOfSections++
707 }
708 oFsm.origImageLength = uint32(fileLen)
709 oFsm.imageLength = uint32(len(oFsm.imageBuffer))
mpagenko80622a52021-02-09 16:53:23 +0000710 logger.Infow(ctx, "OnuUpgradeFsm starts with StartSwDl values", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000711 "MeId": oFsm.InactiveImageMeID, "windowSizeLimit": oFsm.omciDownloadWindowSizeLimit,
mpagenko80622a52021-02-09 16:53:23 +0000712 "ImageSize": oFsm.imageLength, "original file size": fileLen})
713 //"NumberOfCircuitPacks": oFsm.numberCircuitPacks, "CircuitPacks MeId": 0}) //parallel circuit packs download not supported
mpagenkoaa3afe92021-05-21 16:20:58 +0000714
715 oFsm.mutexUpgradeParams.Unlock()
mpagenko39b703e2021-08-25 13:38:40 +0000716
mpagenkoa2b288f2021-10-21 11:25:27 +0000717 // flush chOnuDlReady
mpagenko39b703e2021-08-25 13:38:40 +0000718 select {
719 case <-oFsm.chOnuDlReady:
720 logger.Debug(ctx, "flushed OnuDlReady channel")
721 default:
722 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000723 go oFsm.waitOnDownloadToOnuReady(ctx, oFsm.chOnuDlReady) // start supervision of the complete download-to-ONU procedure
724
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000725 err = oFsm.pOmciCC.SendStartSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
Holger Hildebrandt5458d892022-05-31 09:52:06 +0000726 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, oFsm.omciDownloadWindowSizeLimit, oFsm.origImageLength, oFsm.isExtendedOmci)
mpagenko80622a52021-02-09 16:53:23 +0000727 if err != nil {
728 logger.Errorw(ctx, "StartSwDl abort: can't send section", log.Fields{
729 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +0000730 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +0000731 return
732 }
Holger Hildebrandt288d9472021-09-06 10:47:53 +0000733 oFsm.isEndSwDlOpen = true
mpagenko80622a52021-02-09 16:53:23 +0000734}
735
736func (oFsm *OnuUpgradeFsm) enterDownloadSection(ctx context.Context, e *fsm.Event) {
737 logger.Debugw(ctx, "OnuUpgradeFsm start downloading sections", log.Fields{
738 "device-id": oFsm.deviceID, "absolute window": oFsm.nextDownloadWindow})
mpagenkoa2b288f2021-10-21 11:25:27 +0000739 //use a background routine to send the multiple download sections frames in a loop
mpagenko609a69a2021-12-02 08:12:58 +0000740 // in order to avoid blocking on synchronous event calls for the entire (long) processing time
mpagenkoa2b288f2021-10-21 11:25:27 +0000741 go oFsm.runSwDlSectionWindow(ctx)
742}
mpagenko80622a52021-02-09 16:53:23 +0000743
Joey Armstrong89c812c2024-01-12 19:00:20 -0500744// runSwDlSectionWindow runs a loop to send all DlSection frames of one window in background
745//
746// may be aborted by parallel change of abortRequested
mpagenkoa2b288f2021-10-21 11:25:27 +0000747func (oFsm *OnuUpgradeFsm) runSwDlSectionWindow(ctx context.Context) {
mpagenko80622a52021-02-09 16:53:23 +0000748 var windowAckRequest uint8 = 0
749 var bufferStartOffset uint32
750 var bufferEndOffset uint32
751 var downloadSection []byte
kesavand011d5162021-11-25 19:21:06 +0530752
mpagenkoaa3afe92021-05-21 16:20:58 +0000753 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +0000754 oFsm.upgradePhase = cUpgradeDownloading //start of downloading image to ONU
mpagenko80622a52021-02-09 16:53:23 +0000755 if oFsm.nextDownloadSectionsAbsolute == 0 {
mpagenkoaa3afe92021-05-21 16:20:58 +0000756 oFsm.volthaImageState = voltha.ImageState_IMAGE_DOWNLOADING
mpagenko80622a52021-02-09 16:53:23 +0000757 }
kesavand011d5162021-11-25 19:21:06 +0530758 //var omuTxSecPerWindow []*common.OmciTransferStructure
759 omciMsgsPerSection := &ia.OmciMessages{}
760 //take the TID mutex
761 //To ensure the insequesnce of TIDS for all the onusw sections within a window
762 oFsm.pOmciCC.LockMutexTID()
763 defer oFsm.pOmciCC.UnLockMutexTID()
mpagenko80622a52021-02-09 16:53:23 +0000764 for {
mpagenko38662d02021-08-11 09:45:19 +0000765 oFsm.mutexAbortRequest.RLock()
766 // this way out of the section download loop on abort request
767 if oFsm.abortRequested != voltha.ImageState_NO_ERROR {
mpagenko45586762021-10-01 08:30:22 +0000768 //states are updated when entering the reset state ...
mpagenko38662d02021-08-11 09:45:19 +0000769 oFsm.volthaDownloadReason = oFsm.abortRequested
770 oFsm.mutexAbortRequest.RUnlock()
771 oFsm.mutexUpgradeParams.Unlock()
mpagenkoa2b288f2021-10-21 11:25:27 +0000772 pUpgradeFsm := oFsm.PAdaptFsm
773 if pUpgradeFsm != nil {
774 _ = pUpgradeFsm.PFsm.Event(UpgradeEvAbort)
775 logger.Debugw(ctx, "aborting runSwDlSectionWindow", log.Fields{
776 "device-id": oFsm.deviceID, "reason": oFsm.volthaDownloadReason})
777 return
778 }
779 logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
mpagenko38662d02021-08-11 09:45:19 +0000780 return
781 }
782 oFsm.mutexAbortRequest.RUnlock()
783
Holger Hildebrandt5458d892022-05-31 09:52:06 +0000784 bufferStartOffset = oFsm.nextDownloadSectionsAbsolute * uint32(oFsm.omciDownloadSectionSize)
785 bufferEndOffset = bufferStartOffset + uint32(oFsm.omciDownloadSectionSize) - 1
786 if oFsm.isExtendedOmci {
787 if bufferEndOffset > oFsm.origImageLength-1 {
788 bufferEndOffset = oFsm.origImageLength - 1
789 }
790 }
mpagenko80622a52021-02-09 16:53:23 +0000791 logger.Debugw(ctx, "DlSection values are", log.Fields{
792 "DlSectionNoAbsolute": oFsm.nextDownloadSectionsAbsolute,
793 "DlSectionWindow": oFsm.nextDownloadSectionsWindow,
794 "startOffset": bufferStartOffset, "endOffset": bufferEndOffset})
795 if bufferStartOffset+1 > oFsm.imageLength || bufferEndOffset+1 > oFsm.imageLength { //should never occur in this state
796 logger.Errorw(ctx, "OnuUpgradeFsm buffer error: exceeded length", log.Fields{
797 "device-id": oFsm.deviceID, "bufferStartOffset": bufferStartOffset,
798 "bufferEndOffset": bufferEndOffset, "imageLength": oFsm.imageLength})
mpagenko39b703e2021-08-25 13:38:40 +0000799 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto)
mpagenkoaa3afe92021-05-21 16:20:58 +0000800 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000801 //logical error -- reset the FSM
mpagenkoa2b288f2021-10-21 11:25:27 +0000802 pUpgradeFsm := oFsm.PAdaptFsm
803 if pUpgradeFsm != nil {
804 _ = pUpgradeFsm.PFsm.Event(UpgradeEvAbort)
805 return
806 }
807 logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
mpagenko80622a52021-02-09 16:53:23 +0000808 return
809 }
810 downloadSection = oFsm.imageBuffer[bufferStartOffset : bufferEndOffset+1]
811 if oFsm.nextDownloadSectionsWindow == oFsm.omciDownloadWindowSizeLimit {
812 windowAckRequest = 1
813 logger.Debugw(ctx, "DlSection expect Response for complete window", log.Fields{
814 "device-id": oFsm.deviceID, "in window": oFsm.nextDownloadWindow})
815 }
816 if oFsm.nextDownloadSectionsAbsolute+1 >= oFsm.noOfSections {
817 windowAckRequest = 1
kesavand011d5162021-11-25 19:21:06 +0530818
mpagenko15ff4a52021-03-02 10:09:20 +0000819 oFsm.omciDownloadWindowSizeLast = oFsm.nextDownloadSectionsWindow
820 logger.Infow(ctx, "DlSection expect Response for last window (section)", log.Fields{
mpagenko80622a52021-02-09 16:53:23 +0000821 "device-id": oFsm.deviceID, "DlSectionNoAbsolute": oFsm.nextDownloadSectionsAbsolute})
822 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000823 oFsm.mutexUpgradeParams.Unlock() //unlock here to give other functions some chance to process during/after the send request
kesavand011d5162021-11-25 19:21:06 +0530824 omciTxReq, err := oFsm.pOmciCC.PrepareOnuSectionsOfWindow(log.WithSpanFromContext(context.Background(), ctx),
Holger Hildebrandt5458d892022-05-31 09:52:06 +0000825 oFsm.InactiveImageMeID, windowAckRequest, oFsm.nextDownloadSectionsWindow, downloadSection, omciMsgsPerSection, oFsm.isExtendedOmci)
mpagenko80622a52021-02-09 16:53:23 +0000826 if err != nil {
827 logger.Errorw(ctx, "DlSection abort: can't send section", log.Fields{
mpagenko15ff4a52021-03-02 10:09:20 +0000828 "device-id": oFsm.deviceID, "section absolute": oFsm.nextDownloadSectionsAbsolute, "error": err})
mpagenkoa2b288f2021-10-21 11:25:27 +0000829 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +0000830 return
831 }
kesavand011d5162021-11-25 19:21:06 +0530832
mpagenkoaa3afe92021-05-21 16:20:58 +0000833 oFsm.mutexUpgradeParams.Lock()
mpagenko80622a52021-02-09 16:53:23 +0000834 oFsm.nextDownloadSectionsAbsolute++ //always increase the absolute section counter after having sent one
835 if windowAckRequest == 1 {
mpagenkoaa3afe92021-05-21 16:20:58 +0000836 oFsm.mutexUpgradeParams.Unlock()
kesavand011d5162021-11-25 19:21:06 +0530837
838 if omciTxReq.OnuSwWindow == nil {
839 logger.Errorw(ctx, "fail to send sections in a window", log.Fields{
840 "device-id": oFsm.deviceID, "section absolute": oFsm.nextDownloadSectionsAbsolute, "error": err})
841 oFsm.abortOnOmciError(ctx, false)
842 return
843 }
844
mpagenkoa2b288f2021-10-21 11:25:27 +0000845 pUpgradeFsm := oFsm.PAdaptFsm
846 if pUpgradeFsm != nil {
847 _ = pUpgradeFsm.PFsm.Event(UpgradeEvWaitWindowAck) //state transition to upgradeStVerifyWindow
kesavand011d5162021-11-25 19:21:06 +0530848 oFsm.pOmciCC.SendOnuSwSectionsWindowWithRxSupervision(ctx, omciTxReq, oFsm.pDeviceHandler.GetOmciTimeout(), oFsm.PAdaptFsm.CommChan)
mpagenkoa2b288f2021-10-21 11:25:27 +0000849 return
850 }
851 logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
mpagenko80622a52021-02-09 16:53:23 +0000852 return
853 }
kesavand011d5162021-11-25 19:21:06 +0530854
mpagenko80622a52021-02-09 16:53:23 +0000855 oFsm.nextDownloadSectionsWindow++ //increase the window related section counter only if not in the last section
mpagenko59498c12021-03-18 14:15:15 +0000856 if oFsm.omciSectionInterleaveDelay > 0 {
mpagenko80622a52021-02-09 16:53:23 +0000857 //ensure a defined intersection-time-gap to leave space for further processing, other ONU's ...
mpagenkoaa3afe92021-05-21 16:20:58 +0000858 oFsm.mutexUpgradeParams.Unlock() //unlock here to give other functions some chance to process during/after the send request
mpagenko59498c12021-03-18 14:15:15 +0000859 time.Sleep(oFsm.omciSectionInterleaveDelay * time.Millisecond)
mpagenkoaa3afe92021-05-21 16:20:58 +0000860 oFsm.mutexUpgradeParams.Lock()
mpagenko80622a52021-02-09 16:53:23 +0000861 }
862 }
mpagenkoa2b288f2021-10-21 11:25:27 +0000863} //runSwDlSectionWindow
mpagenko80622a52021-02-09 16:53:23 +0000864
865func (oFsm *OnuUpgradeFsm) enterVerifyWindow(ctx context.Context, e *fsm.Event) {
866 logger.Debugw(ctx, "OnuUpgradeFsm verify DL window ack", log.Fields{
867 "for window": oFsm.nextDownloadWindow, "device-id": oFsm.deviceID})
868}
869
870func (oFsm *OnuUpgradeFsm) enterFinalizeDL(ctx context.Context, e *fsm.Event) {
mpagenko80622a52021-02-09 16:53:23 +0000871 logger.Infow(ctx, "OnuUpgradeFsm finalize DL", log.Fields{
mpagenko59498c12021-03-18 14:15:15 +0000872 "device-id": oFsm.deviceID, "crc": strconv.FormatInt(int64(oFsm.imageCRC), 16), "delay": oFsm.delayEndSwDl})
mpagenko609a69a2021-12-02 08:12:58 +0000873 //use a background routine to wait EndSwDlDelay and then send the EndSwDl request
874 // in order to avoid blocking on synchronous event calls for the complete wait time
875 // and to allow for state transition before sending the EndSwDl request
876 go oFsm.delayAndSendEndSwDl(ctx)
877}
mpagenko80622a52021-02-09 16:53:23 +0000878
Joey Armstrong89c812c2024-01-12 19:00:20 -0500879// delayAndSendEndSwDl ensures a delay before sending the EndSwDl request
880//
881// may also be aborted by parallel channel reception on chAbortEndSwDl
mpagenko609a69a2021-12-02 08:12:58 +0000882func (oFsm *OnuUpgradeFsm) delayAndSendEndSwDl(ctx context.Context) {
mpagenkoaa3afe92021-05-21 16:20:58 +0000883 oFsm.mutexUpgradeParams.RLock()
mpagenko80622a52021-02-09 16:53:23 +0000884 if oFsm.delayEndSwDl {
mpagenkoaa3afe92021-05-21 16:20:58 +0000885 oFsm.mutexUpgradeParams.RUnlock()
mpagenko609a69a2021-12-02 08:12:58 +0000886 //give the ONU some time for image evaluation (hoping it does not only start this evaluation on first EndSwDl itself)
887 logger.Debugw(ctx, "OnuUpgradeFsm delay EndSwDl", log.Fields{"device-id": oFsm.deviceID,
888 "duration_s": cOmciEndSwDlDelaySeconds})
889 select {
890 case <-time.After(cOmciEndSwDlDelaySeconds * time.Second):
891 logger.Warnw(ctx, "OnuUpgradeFsm start sending EndSwDl", log.Fields{
892 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier})
893 case <-oFsm.chAbortDelayEndSwDl:
894 logger.Debugw(ctx, "OnuUpgradeFsm abort request to send EndSwDl", log.Fields{"device-id": oFsm.deviceID})
895 //according further state transition is ensured by the entity that sent the abort
896 return
897 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000898 } else {
899 oFsm.mutexUpgradeParams.RUnlock()
mpagenko80622a52021-02-09 16:53:23 +0000900 }
901
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000902 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000903 if pBaseFsm == nil {
mpagenko38662d02021-08-11 09:45:19 +0000904 logger.Errorw(ctx, "EndSwDl abort: BaseFsm invalid", log.Fields{"device-id": oFsm.deviceID})
905 oFsm.mutexUpgradeParams.Lock()
mpagenko38662d02021-08-11 09:45:19 +0000906 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR
907 oFsm.mutexUpgradeParams.Unlock()
mpagenko59498c12021-03-18 14:15:15 +0000908 // Can't call FSM Event directly, decoupling it
mpagenko609a69a2021-12-02 08:12:58 +0000909 _ = pBaseFsm.PFsm.Event(UpgradeEvAbort)
mpagenko59498c12021-03-18 14:15:15 +0000910 return
911 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000912 err := oFsm.pOmciCC.SendEndSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
Holger Hildebrandt5458d892022-05-31 09:52:06 +0000913 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, oFsm.origImageLength, oFsm.imageCRC, oFsm.isExtendedOmci)
Holger Hildebrandt288d9472021-09-06 10:47:53 +0000914
mpagenko80622a52021-02-09 16:53:23 +0000915 if err != nil {
mpagenkoa2b288f2021-10-21 11:25:27 +0000916 logger.Errorw(ctx, "EndSwDl abort: error sending EndSwDl", log.Fields{
mpagenko80622a52021-02-09 16:53:23 +0000917 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +0000918 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +0000919 return
920 }
mpagenko609a69a2021-12-02 08:12:58 +0000921 //from here on sending of EndSwDl(Abort) is not needed anymore (even though response is not yet received)
922 // this avoids sending of both EndSwDl(Success) and EndSwDl(Abort) when cancellation falls just into this window
923 // the ONU must theoretically be prepared to receive none of them (in case of OMCI transfer issues) e.g. by timeout
924 oFsm.isEndSwDlOpen = false
925 // wait for the EndSwDLResponse and check, if the ONU is ready for activation
926 _ = pBaseFsm.PFsm.Event(UpgradeEvWaitEndDownload)
927} //delayAndSendEndSwDl
mpagenko59498c12021-03-18 14:15:15 +0000928
929func (oFsm *OnuUpgradeFsm) enterWaitEndDL(ctx context.Context, e *fsm.Event) {
930 logger.Infow(ctx, "OnuUpgradeFsm WaitEndDl", log.Fields{
931 "device-id": oFsm.deviceID, "wait delay": oFsm.waitDelayEndSwDl * time.Second, "wait count": oFsm.waitCountEndSwDl})
932 if oFsm.waitCountEndSwDl == 0 {
933 logger.Errorw(ctx, "WaitEndDl abort: max limit of EndSwDL reached", log.Fields{
934 "device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000935 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000936 if pBaseFsm == nil {
937 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
938 "device-id": oFsm.deviceID})
939 return
940 }
mpagenko38662d02021-08-11 09:45:19 +0000941 oFsm.mutexUpgradeParams.Lock()
mpagenko39b703e2021-08-25 13:38:40 +0000942 oFsm.volthaDownloadReason = voltha.ImageState_IMAGE_REFUSED_BY_ONU //something like 'END_DOWNLOAD_TIMEOUT' would be better (proto)
mpagenko38662d02021-08-11 09:45:19 +0000943 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000944 go func(a_pAFsm *cmn.AdapterFsm) {
945 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko59498c12021-03-18 14:15:15 +0000946 }(pBaseFsm)
947 return
948 }
949
950 oFsm.waitCountEndSwDl--
951 select {
952 case <-time.After(oFsm.waitDelayEndSwDl * time.Second):
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000953 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000954 if pBaseFsm == nil {
955 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
956 "device-id": oFsm.deviceID})
957 //FSM may be reset already from somewhere else, nothing we can do here anymore
958 return
959 }
960 //retry End SW DL
mpagenkoaa3afe92021-05-21 16:20:58 +0000961 oFsm.mutexUpgradeParams.Lock()
mpagenko59498c12021-03-18 14:15:15 +0000962 oFsm.delayEndSwDl = false //no more extra delay for the request
mpagenkoaa3afe92021-05-21 16:20:58 +0000963 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000964 go func(a_pAFsm *cmn.AdapterFsm) {
965 _ = a_pAFsm.PFsm.Event(UpgradeEvContinueFinalize)
mpagenko59498c12021-03-18 14:15:15 +0000966 }(pBaseFsm)
967 return
968 case success := <-oFsm.chReceiveExpectedResponse:
969 logger.Debugw(ctx, "WaitEndDl stop wait timer", log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +0000970 oFsm.isEndSwDlOpen = false //no request to abort of download (already finished or immediate abort)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000971 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000972 if pBaseFsm == nil {
973 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
974 "device-id": oFsm.deviceID})
975 //FSM may be reset already from somewhere else, nothing we can do here anymore
976 return
977 }
978 if success {
979 //answer received with ready indication
mpagenko38662d02021-08-11 09:45:19 +0000980 //useAPIVersion43 may not conflict in concurrency in this state function
981 if oFsm.useAPIVersion43 { // newer API usage requires verification of downloaded image version
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000982 go func(a_pAFsm *cmn.AdapterFsm) {
983 _ = a_pAFsm.PFsm.Event(UpgradeEvCheckImageName)
mpagenkoc26d4c02021-05-06 14:27:57 +0000984 }(pBaseFsm)
mpagenko38662d02021-08-11 09:45:19 +0000985 } else { // elder API usage does not support image version check -immediately consider download as successful
986 if oFsm.activateImage {
987 //immediate activation requested
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000988 go func(a_pAFsm *cmn.AdapterFsm) {
989 _ = a_pAFsm.PFsm.Event(UpgradeEvRequestActivate)
mpagenko38662d02021-08-11 09:45:19 +0000990 }(pBaseFsm)
991 } else {
992 //have to wait on explicit activation request
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000993 go func(a_pAFsm *cmn.AdapterFsm) {
994 _ = a_pAFsm.PFsm.Event(UpgradeEvWaitForActivate)
mpagenko38662d02021-08-11 09:45:19 +0000995 }(pBaseFsm)
996 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000997 }
mpagenko59498c12021-03-18 14:15:15 +0000998 return
999 }
1000 //timer was aborted
mpagenko45586762021-10-01 08:30:22 +00001001 oFsm.abortOnOmciError(ctx, true)
mpagenko59498c12021-03-18 14:15:15 +00001002 return
1003 }
mpagenko80622a52021-02-09 16:53:23 +00001004}
1005
mpagenko38662d02021-08-11 09:45:19 +00001006func (oFsm *OnuUpgradeFsm) enterCheckImageName(ctx context.Context, e *fsm.Event) {
1007 logger.Debugw(ctx, "OnuUpgradeFsm checking downloaded image name", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001008 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
Holger Hildebrandt3ac49bd2022-02-07 17:46:43 +00001009 requestedAttributes := me.AttributeValueMap{me.SoftwareImage_IsCommitted: 0, me.SoftwareImage_IsActive: 0, me.SoftwareImage_Version: ""}
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001010 meInstance, err := oFsm.pOmciCC.SendGetMe(log.WithSpanFromContext(context.Background(), ctx),
1011 me.SoftwareImageClassID, oFsm.InactiveImageMeID, requestedAttributes, oFsm.pDeviceHandler.GetOmciTimeout(),
Holger Hildebrandtd930cb22022-06-17 09:24:50 +00001012 false, oFsm.PAdaptFsm.CommChan, oFsm.isExtendedOmci)
mpagenko38662d02021-08-11 09:45:19 +00001013 if err != nil {
1014 logger.Errorw(ctx, "OnuUpgradeFsm get Software Image ME result error",
1015 log.Fields{"device-id": oFsm.deviceID, "Error": err})
mpagenko45586762021-10-01 08:30:22 +00001016 oFsm.abortOnOmciError(ctx, true)
mpagenko38662d02021-08-11 09:45:19 +00001017 return
1018 }
1019 oFsm.pLastTxMeInstance = meInstance
1020}
1021
mpagenko80622a52021-02-09 16:53:23 +00001022func (oFsm *OnuUpgradeFsm) enterActivateSw(ctx context.Context, e *fsm.Event) {
1023 logger.Infow(ctx, "OnuUpgradeFsm activate SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001024 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
mpagenko80622a52021-02-09 16:53:23 +00001025
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001026 err := oFsm.pOmciCC.SendActivateSoftware(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
Holger Hildebrandt5458d892022-05-31 09:52:06 +00001027 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, oFsm.isExtendedOmci)
mpagenko80622a52021-02-09 16:53:23 +00001028 if err != nil {
1029 logger.Errorw(ctx, "ActivateSw abort: can't send activate frame", log.Fields{
1030 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +00001031 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +00001032 return
1033 }
mpagenko38662d02021-08-11 09:45:19 +00001034 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +00001035 oFsm.upgradePhase = cUpgradeActivating //start of image activation for ONU
mpagenko38662d02021-08-11 09:45:19 +00001036 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATING
1037 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +00001038}
1039
1040func (oFsm *OnuUpgradeFsm) enterCommitSw(ctx context.Context, e *fsm.Event) {
mpagenko38662d02021-08-11 09:45:19 +00001041 logger.Debugw(ctx, "OnuUpgradeFsm start commit SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001042 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
mpagenko38662d02021-08-11 09:45:19 +00001043 //any abort request (also conditional) is still regarded as valid as the commit indication might not be possible to verify
1044 // (which is a bit problematic as the ONU might already be in committed state,
1045 // in this case (committing failed) always 'onuimage list' should be used to verify the real state (if ONU is reachable))
1046 if activeImageID, err := oFsm.pDevEntry.GetActiveImageMeID(ctx); err == nil {
mpagenkoaa3afe92021-05-21 16:20:58 +00001047 oFsm.mutexUpgradeParams.Lock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001048 if activeImageID == oFsm.InactiveImageMeID {
1049 inactiveImageID := oFsm.InactiveImageMeID
mpagenko15ff4a52021-03-02 10:09:20 +00001050 logger.Infow(ctx, "OnuUpgradeFsm commit SW", log.Fields{
mpagenkoaa3afe92021-05-21 16:20:58 +00001051 "device-id": oFsm.deviceID, "me-id": inactiveImageID}) //more efficient activeImageID with above check
1052 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTING
mpagenko45586762021-10-01 08:30:22 +00001053 oFsm.upgradePhase = cUpgradeCommitting //start of image commitment for ONU
mpagenkoaa3afe92021-05-21 16:20:58 +00001054 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001055 err := oFsm.pOmciCC.SendCommitSoftware(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
Holger Hildebrandt5458d892022-05-31 09:52:06 +00001056 oFsm.PAdaptFsm.CommChan, inactiveImageID, oFsm.isExtendedOmci) //more efficient activeImageID with above check
mpagenko15ff4a52021-03-02 10:09:20 +00001057 if err != nil {
1058 logger.Errorw(ctx, "CommitSw abort: can't send commit sw frame", log.Fields{
1059 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +00001060 oFsm.abortOnOmciError(ctx, true)
mpagenko15ff4a52021-03-02 10:09:20 +00001061 return
1062 }
1063 return
1064 }
mpagenko38662d02021-08-11 09:45:19 +00001065 oFsm.mutexUpgradeParams.Unlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001066 logger.Errorw(ctx, "OnuUpgradeFsm active ImageId <> IdToCommit", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001067 "device-id": oFsm.deviceID, "active ID": activeImageID, "to commit ID": oFsm.InactiveImageMeID})
mpagenko38662d02021-08-11 09:45:19 +00001068 } else {
1069 logger.Errorw(ctx, "OnuUpgradeFsm can't commit, no valid active image", log.Fields{
1070 "device-id": oFsm.deviceID})
mpagenko15ff4a52021-03-02 10:09:20 +00001071 }
mpagenko38662d02021-08-11 09:45:19 +00001072 oFsm.mutexUpgradeParams.Lock()
1073 oFsm.conditionalCancelRequested = false //any lingering conditional cancelRequest is superseded by this error
mpagenko38662d02021-08-11 09:45:19 +00001074 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
mpagenko38662d02021-08-11 09:45:19 +00001075 oFsm.mutexUpgradeParams.Unlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001076 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001077 pBaseFsm := oFsm.PAdaptFsm
mpagenko15ff4a52021-03-02 10:09:20 +00001078 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001079 go func(a_pAFsm *cmn.AdapterFsm) {
1080 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko15ff4a52021-03-02 10:09:20 +00001081 }(pBaseFsm)
1082}
1083
1084func (oFsm *OnuUpgradeFsm) enterCheckCommitted(ctx context.Context, e *fsm.Event) {
mpagenko38662d02021-08-11 09:45:19 +00001085 logger.Debugw(ctx, "OnuUpgradeFsm checking committed SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001086 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
Holger Hildebrandt3ac49bd2022-02-07 17:46:43 +00001087 requestedAttributes := me.AttributeValueMap{me.SoftwareImage_IsCommitted: 0, me.SoftwareImage_IsActive: 0, me.SoftwareImage_Version: ""}
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001088 meInstance, err := oFsm.pOmciCC.SendGetMe(log.WithSpanFromContext(context.Background(), ctx),
Holger Hildebrandtd930cb22022-06-17 09:24:50 +00001089 me.SoftwareImageClassID, oFsm.InactiveImageMeID, requestedAttributes,
1090 oFsm.pDeviceHandler.GetOmciTimeout(), false, oFsm.PAdaptFsm.CommChan, oFsm.isExtendedOmci)
ozgecanetsiab36ed572021-04-01 10:38:48 +03001091 if err != nil {
1092 logger.Errorw(ctx, "OnuUpgradeFsm get Software Image ME result error",
1093 log.Fields{"device-id": oFsm.deviceID, "Error": err})
mpagenko45586762021-10-01 08:30:22 +00001094 oFsm.abortOnOmciError(ctx, true)
ozgecanetsiab36ed572021-04-01 10:38:48 +03001095 return
1096 }
mpagenko15ff4a52021-03-02 10:09:20 +00001097 oFsm.pLastTxMeInstance = meInstance
mpagenko80622a52021-02-09 16:53:23 +00001098}
1099
1100func (oFsm *OnuUpgradeFsm) enterResetting(ctx context.Context, e *fsm.Event) {
1101 logger.Debugw(ctx, "OnuUpgradeFsm resetting", log.Fields{"device-id": oFsm.deviceID})
1102
mpagenko45586762021-10-01 08:30:22 +00001103 oFsm.stateUpdateOnReset(ctx)
mpagenko38662d02021-08-11 09:45:19 +00001104
mpagenkoa2b288f2021-10-21 11:25:27 +00001105 oFsm.mutexAbortRequest.Lock()
1106 //to be sure to abort a possibly still running runSwDlSectionWindow()
1107 // in case the reset was not received from cancel() and download not finished correctly
1108 oFsm.abortRequested = oFsm.volthaDownloadReason
1109 oFsm.mutexAbortRequest.Unlock()
1110
mpagenkoc26d4c02021-05-06 14:27:57 +00001111 // in case the download-to-ONU timer is still running - cancel it
mpagenko39b703e2021-08-25 13:38:40 +00001112 //use non-blocking channel (to be independent from receiver state)
1113 select {
1114 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
1115 case oFsm.chOnuDlReady <- false:
1116 default:
mpagenkoc26d4c02021-05-06 14:27:57 +00001117 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001118 pConfigUpgradeStateAFsm := oFsm.PAdaptFsm
1119 if pConfigUpgradeStateAFsm != nil {
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001120 var nextEvent string
1121 if oFsm.isEndSwDlOpen {
mpagenko45586762021-10-01 08:30:22 +00001122 if oFsm.repeatAbort {
1123 oFsm.delayEndSwDl = true //run next abort with delay
1124 } else { //initial request
1125 oFsm.delayEndSwDl = false //run next abort with no delay
1126 oFsm.waitCountEndSwDl = cWaitCountEndSwDl //init for possible repetitions
1127 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001128 nextEvent = UpgradeEvAbortSwDownload
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001129 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001130 nextEvent = UpgradeEvRestart
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001131 }
1132 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001133 go func(a_pAFsm *cmn.AdapterFsm) {
1134 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1135 _ = a_pAFsm.PFsm.Event(nextEvent)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001136 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001137 }(pConfigUpgradeStateAFsm)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001138 }
1139}
mpagenkoc26d4c02021-05-06 14:27:57 +00001140
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001141func (oFsm *OnuUpgradeFsm) enterAbortingDL(ctx context.Context, e *fsm.Event) {
1142 logger.Debugw(ctx, "OnuUpgradeFsm aborting download to ONU", log.Fields{"device-id": oFsm.deviceID})
1143
mpagenko45586762021-10-01 08:30:22 +00001144 oFsm.mutexUpgradeParams.RLock()
1145 if oFsm.delayEndSwDl {
1146 oFsm.mutexUpgradeParams.RUnlock()
1147 //give the ONU some time for image discard activities
1148 time.Sleep(cOmciEndSwDlDelaySeconds * time.Second)
1149 } else {
1150 oFsm.mutexUpgradeParams.RUnlock()
1151 }
1152
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001153 pBaseFsm := oFsm.PAdaptFsm
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001154 if pBaseFsm == nil {
1155 logger.Errorw(ctx, "OnuUpgradeFsm aborting download: BaseFsm invalid", log.Fields{"device-id": oFsm.deviceID})
1156 return
1157 }
1158 // abort the download operation by sending an end software download message with invalid CRC and image size
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001159 err := oFsm.pOmciCC.SendEndSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
Holger Hildebrandt5458d892022-05-31 09:52:06 +00001160 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, 0, 0xFFFFFFFF, oFsm.isExtendedOmci)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001161
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001162 if err != nil {
1163 logger.Errorw(ctx, "OnuUpgradeFsm aborting download: can't send EndSwDl request", log.Fields{"device-id": oFsm.deviceID})
1164 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001165 go func(a_pAFsm *cmn.AdapterFsm) {
1166 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1167 _ = a_pAFsm.PFsm.Event(UpgradeEvRestart)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001168 }
1169 }(pBaseFsm)
1170 return
1171 }
mpagenkoa2b288f2021-10-21 11:25:27 +00001172
1173 //avoid waiting in the enterXXX function here,
1174 // otherwise synchronous event calls (like from RxMessage processing) may block and block complete Rx processing then
1175 go oFsm.waitOnAbortEndSwDlResponse(ctx)
mpagenko45586762021-10-01 08:30:22 +00001176}
1177
Joey Armstrong89c812c2024-01-12 19:00:20 -05001178// abortingDlEvaluateResponse waits for a channel indication with decision to proceed the FSM processing
mpagenko45586762021-10-01 08:30:22 +00001179func (oFsm *OnuUpgradeFsm) abortingDlEvaluateResponse(ctx context.Context,
1180 pBaseFsm *cmn.AdapterFsm, aResponseResult tEndSwDlResponseResult) bool {
1181 switch aResponseResult {
1182 case cEndSwDlResponseBusy: // indication for device busy, needs repetition
1183 if oFsm.waitCountEndSwDl == 0 {
1184 logger.Errorw(ctx, "aborting download: max limit of EndSwDl reached", log.Fields{
1185 "device-id": oFsm.deviceID})
1186 go func(a_pAFsm *cmn.AdapterFsm) {
1187 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1188 _ = a_pAFsm.PFsm.Event(UpgradeEvRestart) //give up and let FSM terminate
1189 }
1190 }(pBaseFsm)
1191 } else {
1192 logger.Debugw(ctx, "aborting download: re-trigger sending abort SwDl", log.Fields{
1193 "device-id": oFsm.deviceID, "counter": oFsm.waitCountEndSwDl})
1194 oFsm.waitCountEndSwDl--
1195 oFsm.repeatAbort = true //repeated request in next round
1196 go func(a_pAFsm *cmn.AdapterFsm) {
1197 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1198 _ = a_pAFsm.PFsm.Event(UpgradeEvReset) //which then re-triggers sending AbortSwDL
1199 }
1200 }(pBaseFsm)
1201 }
1202 return true
1203 case cEndSwDlResponseSuccess: // indication for success response
1204 logger.Infow(ctx, "aborting download: success response, terminating FSM", log.Fields{
1205 "device-id": oFsm.deviceID})
1206 case cEndSwDlResponseAbort: // indication for request to abort waiting for response
1207 logger.Infow(ctx, "aborting download: request to abort waiting, terminating FSM", log.Fields{
1208 "device-id": oFsm.deviceID})
1209 default:
1210 logger.Errorw(ctx, "aborting download: unknown channel indication, terminating FSM", log.Fields{
1211 "device-id": oFsm.deviceID})
1212 } //switch
1213 return false
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001214}
1215
1216func (oFsm *OnuUpgradeFsm) enterRestarting(ctx context.Context, e *fsm.Event) {
1217 logger.Debugw(ctx, "OnuUpgradeFsm restarting", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001218 pConfigUpgradeStateAFsm := oFsm.PAdaptFsm
1219 if pConfigUpgradeStateAFsm != nil {
mpagenko80622a52021-02-09 16:53:23 +00001220 // abort running message processing
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001221 fsmAbortMsg := cmn.Message{
1222 Type: cmn.TestMsg,
1223 Data: cmn.TestMessage{
1224 TestMessageVal: cmn.AbortMessageProcessing,
mpagenko80622a52021-02-09 16:53:23 +00001225 },
1226 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001227 pConfigUpgradeStateAFsm.CommChan <- fsmAbortMsg
mpagenko80622a52021-02-09 16:53:23 +00001228
1229 //try to restart the FSM to 'disabled'
1230 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001231 go func(a_pAFsm *cmn.AdapterFsm) {
1232 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1233 _ = a_pAFsm.PFsm.Event(UpgradeEvDisable)
mpagenko80622a52021-02-09 16:53:23 +00001234 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001235 }(pConfigUpgradeStateAFsm)
mpagenko80622a52021-02-09 16:53:23 +00001236 }
1237}
1238
1239func (oFsm *OnuUpgradeFsm) enterDisabled(ctx context.Context, e *fsm.Event) {
1240 logger.Debugw(ctx, "OnuUpgradeFsm enters disabled state", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001241 // 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 +00001242 if oFsm.pDeviceHandler != nil {
1243 //request removal of 'reference' in the Handler (completely clear the FSM and its data)
mpagenko38662d02021-08-11 09:45:19 +00001244 pLastUpgradeImageState := &voltha.ImageState{
1245 Version: oFsm.imageVersion,
1246 DownloadState: oFsm.volthaDownloadState,
1247 Reason: oFsm.volthaDownloadReason,
1248 ImageState: oFsm.volthaImageState,
1249 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001250 go oFsm.pDeviceHandler.RemoveOnuUpgradeFsm(ctx, pLastUpgradeImageState)
mpagenko80622a52021-02-09 16:53:23 +00001251 }
1252}
1253
1254func (oFsm *OnuUpgradeFsm) processOmciUpgradeMessages(ctx context.Context) { //ctx context.Context?
1255 logger.Debugw(ctx, "Start OnuUpgradeFsm Msg processing", log.Fields{"for device-id": oFsm.deviceID})
1256loop:
1257 for {
1258 // case <-ctx.Done():
1259 // logger.Info(ctx,"MibSync Msg", log.Fields{"Message handling canceled via context for device-id": oFsm.deviceID})
1260 // break loop
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001261 message, ok := <-oFsm.PAdaptFsm.CommChan
mpagenko80622a52021-02-09 16:53:23 +00001262 if !ok {
1263 logger.Info(ctx, "OnuUpgradeFsm Rx Msg - could not read from channel", log.Fields{"device-id": oFsm.deviceID})
1264 // but then we have to ensure a restart of the FSM as well - as exceptional procedure
mpagenko45586762021-10-01 08:30:22 +00001265 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +00001266 break loop
1267 }
1268 logger.Debugw(ctx, "OnuUpgradeFsm Rx Msg", log.Fields{"device-id": oFsm.deviceID})
1269
1270 switch message.Type {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001271 case cmn.TestMsg:
1272 msg, _ := message.Data.(cmn.TestMessage)
1273 if msg.TestMessageVal == cmn.AbortMessageProcessing {
mpagenko80622a52021-02-09 16:53:23 +00001274 logger.Infow(ctx, "OnuUpgradeFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.deviceID})
1275 break loop
1276 }
1277 logger.Warnw(ctx, "OnuUpgradeFsm unknown TestMessage", log.Fields{"device-id": oFsm.deviceID, "MessageVal": msg.TestMessageVal})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001278 case cmn.OMCI:
1279 msg, _ := message.Data.(cmn.OmciMessage)
mpagenko80622a52021-02-09 16:53:23 +00001280 oFsm.handleOmciOnuUpgradeMessage(ctx, msg)
1281 default:
1282 logger.Warn(ctx, "OnuUpgradeFsm Rx unknown message", log.Fields{"device-id": oFsm.deviceID,
1283 "message.Type": message.Type})
1284 }
1285 }
1286 logger.Infow(ctx, "End OnuUpgradeFsm Msg processing", log.Fields{"device-id": oFsm.deviceID})
1287}
1288
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001289func (oFsm *OnuUpgradeFsm) handleOmciOnuUpgradeMessage(ctx context.Context, msg cmn.OmciMessage) {
mpagenko80622a52021-02-09 16:53:23 +00001290 logger.Debugw(ctx, "Rx OMCI OnuUpgradeFsm Msg", log.Fields{"device-id": oFsm.deviceID,
1291 "msgType": msg.OmciMsg.MessageType})
1292
1293 switch msg.OmciMsg.MessageType {
1294 case omci.StartSoftwareDownloadResponseType:
1295 {
mpagenko45586762021-10-01 08:30:22 +00001296 oFsm.handleRxStartSwDownloadResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001297 return
1298 } //StartSoftwareDownloadResponseType
1299 case omci.DownloadSectionResponseType:
1300 {
mpagenko45586762021-10-01 08:30:22 +00001301 oFsm.handleRxSwSectionResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001302 return
mpagenko80622a52021-02-09 16:53:23 +00001303 } //DownloadSectionResponseType
1304 case omci.EndSoftwareDownloadResponseType:
1305 {
mpagenko45586762021-10-01 08:30:22 +00001306 oFsm.handleRxEndSwDownloadResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001307 return
1308 } //EndSoftwareDownloadResponseType
1309 case omci.ActivateSoftwareResponseType:
1310 {
1311 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeActivateSoftwareResponse)
1312 if msgLayer == nil {
1313 logger.Errorw(ctx, "Omci Msg layer could not be detected for ActivateSw",
1314 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001315 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001316 return
1317 }
1318 msgObj, msgOk := msgLayer.(*omci.ActivateSoftwareResponse)
1319 if !msgOk {
1320 logger.Errorw(ctx, "Omci Msg layer could not be assigned for ActivateSw",
1321 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001322 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001323 return
1324 }
1325 logger.Debugw(ctx, "OnuUpgradeFsm ActivateSwResponse data", log.Fields{
1326 "device-id": oFsm.deviceID, "data-fields": msgObj})
1327 if msgObj.Result != me.Success {
1328 logger.Errorw(ctx, "OnuUpgradeFsm ActivateSwResponse result error - later: drive FSM to abort state ?",
1329 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
mpagenko45586762021-10-01 08:30:22 +00001330 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001331 return
1332 }
mpagenko183647c2021-06-08 15:25:04 +00001333 oFsm.mutexUpgradeParams.Lock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001334 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
mpagenko38662d02021-08-11 09:45:19 +00001335 // the image is regarded as active really only after ONU reboot and according indication (ONU down/up procedure)
mpagenko183647c2021-06-08 15:25:04 +00001336 oFsm.mutexUpgradeParams.Unlock()
1337 logger.Infow(ctx, "Expected ActivateSwResponse received",
1338 log.Fields{"device-id": oFsm.deviceID, "commit": oFsm.commitImage})
1339 if oFsm.commitImage {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001340 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvWaitForCommit)
mpagenko183647c2021-06-08 15:25:04 +00001341 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001342 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvActivationDone) // let the FSM wait for external commit request
mpagenko183647c2021-06-08 15:25:04 +00001343 }
mpagenko80622a52021-02-09 16:53:23 +00001344 return
1345 }
mpagenko183647c2021-06-08 15:25:04 +00001346 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +00001347 logger.Errorw(ctx, "OnuUpgradeFsm ActivateSwResponse wrong ME instance: abort",
1348 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
mpagenko45586762021-10-01 08:30:22 +00001349 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001350 return
1351 } //ActivateSoftwareResponseType
mpagenko15ff4a52021-03-02 10:09:20 +00001352 case omci.CommitSoftwareResponseType:
1353 {
1354 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeCommitSoftwareResponse)
1355 if msgLayer == nil {
1356 logger.Errorw(ctx, "Omci Msg layer could not be detected for CommitResponse",
1357 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001358 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001359 return
1360 }
1361 msgObj, msgOk := msgLayer.(*omci.CommitSoftwareResponse)
1362 if !msgOk {
1363 logger.Errorw(ctx, "Omci Msg layer could not be assigned for CommitResponse",
1364 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001365 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001366 return
1367 }
mpagenkobf67a092021-03-17 09:52:28 +00001368 if msgObj.Result != me.Success {
mpagenko15ff4a52021-03-02 10:09:20 +00001369 logger.Errorw(ctx, "OnuUpgradeFsm SwImage CommitResponse result error - later: drive FSM to abort state ?",
1370 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
mpagenko45586762021-10-01 08:30:22 +00001371 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001372 return
mpagenkobf67a092021-03-17 09:52:28 +00001373 }
mpagenkoaa3afe92021-05-21 16:20:58 +00001374 oFsm.mutexUpgradeParams.RLock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001375 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
mpagenkoaa3afe92021-05-21 16:20:58 +00001376 oFsm.mutexUpgradeParams.RUnlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001377 logger.Debugw(ctx, "OnuUpgradeFsm Expected SwImage CommitResponse received", log.Fields{"device-id": oFsm.deviceID})
1378 //verifying committed image
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001379 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvCheckCommitted)
mpagenko15ff4a52021-03-02 10:09:20 +00001380 return
1381 }
mpagenkoaa3afe92021-05-21 16:20:58 +00001382 oFsm.mutexUpgradeParams.RUnlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001383 logger.Errorw(ctx, "OnuUpgradeFsm SwImage CommitResponse wrong ME instance: abort",
1384 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
mpagenko45586762021-10-01 08:30:22 +00001385 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001386 return
1387 } //CommitSoftwareResponseType
1388 case omci.GetResponseType:
1389 {
mpagenko45586762021-10-01 08:30:22 +00001390 oFsm.handleRxSwGetResponse(ctx, msg)
mpagenko15ff4a52021-03-02 10:09:20 +00001391 return
1392 } //GetResponseType
mpagenko80622a52021-02-09 16:53:23 +00001393 default:
1394 {
1395 logger.Errorw(ctx, "Rx OMCI unhandled MsgType",
1396 log.Fields{"omciMsgType": msg.OmciMsg.MessageType, "device-id": oFsm.deviceID})
1397 return
1398 }
1399 }
1400}
1401
mpagenko45586762021-10-01 08:30:22 +00001402func (oFsm *OnuUpgradeFsm) handleRxStartSwDownloadResponse(ctx context.Context, msg cmn.OmciMessage) {
1403 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeStartSoftwareDownloadResponse)
1404 if msgLayer == nil {
1405 logger.Errorw(ctx, "Omci Msg layer could not be detected for StartSwDlResponse",
1406 log.Fields{"device-id": oFsm.deviceID})
1407 oFsm.abortOnOmciError(ctx, false)
1408 return
1409 }
1410 msgObj, msgOk := msgLayer.(*omci.StartSoftwareDownloadResponse)
1411 if !msgOk {
1412 logger.Errorw(ctx, "Omci Msg layer could not be assigned for StartSwDlResponse",
1413 log.Fields{"device-id": oFsm.deviceID})
1414 oFsm.abortOnOmciError(ctx, false)
1415 return
1416 }
1417 logger.Debugw(ctx, "OnuUpgradeFsm StartSwDlResponse data", log.Fields{
1418 "device-id": oFsm.deviceID, "data-fields": msgObj})
1419 if msgObj.Result != me.Success {
1420 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse result error - later: drive FSM to abort state ?",
1421 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1422 oFsm.abortOnOmciError(ctx, false)
1423 return
1424 }
1425
1426 oFsm.mutexUpgradeParams.Lock()
1427 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1428 logger.Debugw(ctx, "Expected StartSwDlResponse received", log.Fields{"device-id": oFsm.deviceID})
1429 if msgObj.WindowSize != oFsm.omciDownloadWindowSizeLimit {
1430 // also response WindowSize = 0 is a valid number for used Window size 1
1431 logger.Debugw(ctx, "different StartSwDlResponse window size requested by ONU", log.Fields{
1432 "acceptedOnuWindowSizeLimit": msgObj.WindowSize, "device-id": oFsm.deviceID})
1433 oFsm.omciDownloadWindowSizeLimit = msgObj.WindowSize
1434 }
1435 oFsm.noOfWindows = oFsm.noOfSections / uint32(oFsm.omciDownloadWindowSizeLimit+1)
1436 if oFsm.noOfSections%uint32(oFsm.omciDownloadWindowSizeLimit+1) > 0 {
1437 oFsm.noOfWindows++
1438 }
1439 logger.Debugw(ctx, "OnuUpgradeFsm will use", log.Fields{
1440 "windows": oFsm.noOfWindows, "sections": oFsm.noOfSections,
1441 "at WindowSizeLimit": oFsm.omciDownloadWindowSizeLimit})
1442 oFsm.nextDownloadSectionsAbsolute = 0
1443 oFsm.nextDownloadSectionsWindow = 0
1444 oFsm.nextDownloadWindow = 0
1445
1446 oFsm.mutexUpgradeParams.Unlock()
1447 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvRxStartSwDownload)
1448 return
1449 }
1450 oFsm.mutexUpgradeParams.Unlock()
1451 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse wrong ME instance: try again (later)?",
1452 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1453 oFsm.abortOnOmciError(ctx, false)
1454} //handleRxStartSwDownloadResponse
1455
1456func (oFsm *OnuUpgradeFsm) handleRxSwSectionResponse(ctx context.Context, msg cmn.OmciMessage) {
mpagenkoa2b288f2021-10-21 11:25:27 +00001457 if oFsm.PAdaptFsm == nil {
1458 logger.Infow(ctx, "DlSectionResponse received - but FSM not really valid anymore", log.Fields{
1459 "device-id": oFsm.deviceID})
1460 return
1461 }
1462 if !oFsm.PAdaptFsm.PFsm.Is(UpgradeStVerifyWindow) {
1463 //all the processing here is only relevant if the FSM is in state upgradeStVerifyWindow
1464 // otherwise this response can be ignored (may stem from a long-processing window send activity,
1465 // which is not anymore relevant based on intermediate (cancel) state transitions)
1466 logger.Infow(ctx, "DlSectionResponse received - but ignored", log.Fields{
1467 "device-id": oFsm.deviceID, "fsm-state": oFsm.PAdaptFsm.PFsm.Current()})
1468 return
1469 }
mpagenko45586762021-10-01 08:30:22 +00001470 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeDownloadSectionResponse)
1471 if msgLayer == nil {
1472 logger.Errorw(ctx, "Omci Msg layer could not be detected for DlSectionResponse",
1473 log.Fields{"device-id": oFsm.deviceID, "omci-message": msg.OmciMsg})
1474 oFsm.abortOnOmciError(ctx, false)
1475 return
1476 }
1477 msgObj, msgOk := msgLayer.(*omci.DownloadSectionResponse)
1478 if !msgOk {
1479 logger.Errorw(ctx, "Omci Msg layer could not be assigned for DlSectionResponse",
1480 log.Fields{"device-id": oFsm.deviceID})
1481 oFsm.abortOnOmciError(ctx, false)
1482 return
1483 }
1484 logger.Debugw(ctx, "OnuUpgradeFsm DlSectionResponse Data", log.Fields{
1485 "device-id": oFsm.deviceID, "data-fields": msgObj})
1486 if msgObj.Result != me.Success {
1487 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse result error - later: repeat window once?", //TODO!!!
1488 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1489 oFsm.abortOnOmciError(ctx, false)
1490 return
1491 }
1492 oFsm.mutexUpgradeParams.Lock()
1493 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1494 sectionNumber := msgObj.SectionNumber
1495 logger.Infow(ctx, "DlSectionResponse received", log.Fields{
1496 "window section-number": sectionNumber, "window": oFsm.nextDownloadWindow, "device-id": oFsm.deviceID})
1497
1498 oFsm.nextDownloadWindow++
1499 if oFsm.nextDownloadWindow >= oFsm.noOfWindows {
1500 if sectionNumber != oFsm.omciDownloadWindowSizeLast {
1501 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse section error last window - later: repeat window once?", //TODO!!!
1502 log.Fields{"device-id": oFsm.deviceID, "actual section": sectionNumber,
1503 "expected section": oFsm.omciDownloadWindowSizeLast})
1504 oFsm.mutexUpgradeParams.Unlock()
1505 oFsm.abortOnOmciError(ctx, false)
1506 return
1507 }
1508 oFsm.delayEndSwDl = true //ensure a delay for the EndSwDl message
1509 //CRC computation for all data bytes of the file
1510 imageCRC := crc32a.Checksum(oFsm.imageBuffer[:int(oFsm.origImageLength)]) //store internal for multiple usage
1511 //revert the retrieved CRC Byte Order (seems not to deliver NetworkByteOrder)
1512 var byteSlice []byte = make([]byte, 4)
1513 binary.LittleEndian.PutUint32(byteSlice, uint32(imageCRC))
1514 oFsm.imageCRC = binary.BigEndian.Uint32(byteSlice)
1515 oFsm.mutexUpgradeParams.Unlock()
1516 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvEndSwDownload)
1517 return
1518 }
1519 if sectionNumber != oFsm.omciDownloadWindowSizeLimit {
1520 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse section error - later: repeat window once?", //TODO!!!
1521 log.Fields{"device-id": oFsm.deviceID, "actual-section": sectionNumber,
1522 "expected section": oFsm.omciDownloadWindowSizeLimit})
1523 oFsm.mutexUpgradeParams.Unlock()
1524 oFsm.abortOnOmciError(ctx, false)
1525 return
1526 }
1527 oFsm.nextDownloadSectionsWindow = 0
1528 oFsm.mutexUpgradeParams.Unlock()
1529 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvContinueNextWindow)
1530 return
1531 }
1532 oFsm.mutexUpgradeParams.Unlock()
1533 logger.Errorw(ctx, "OnuUpgradeFsm Omci StartSwDlResponse wrong ME instance: try again (later)?",
1534 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1535 oFsm.abortOnOmciError(ctx, false)
1536} //handleRxSwSectionResponse
1537
1538func (oFsm *OnuUpgradeFsm) handleRxEndSwDownloadResponse(ctx context.Context, msg cmn.OmciMessage) {
1539 inAbortingState := oFsm.PAdaptFsm.PFsm.Is(UpgradeStAbortingDL)
1540
1541 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeEndSoftwareDownloadResponse)
1542 if msgLayer == nil {
1543 logger.Errorw(ctx, "Omci Msg layer could not be detected 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 msgObj, msgOk := msgLayer.(*omci.EndSoftwareDownloadResponse)
1551 if !msgOk {
1552 logger.Errorw(ctx, "Omci Msg layer could not be assigned for EndSwDlResponse",
1553 log.Fields{"device-id": oFsm.deviceID})
1554 if !inAbortingState {
1555 oFsm.abortOnOmciError(ctx, false)
1556 } //else using error log and wait for another response or 'aborting' state timeout
1557 return
1558 }
1559 logger.Debugw(ctx, "OnuUpgradeFsm EndSwDlResponse data", log.Fields{
1560 "device-id": oFsm.deviceID, "data-fields": msgObj})
1561 if msgObj.Result != me.Success {
1562 if msgObj.Result == me.DeviceBusy {
1563 //ONU indicates it is still processing the image - let the FSM just wait and then repeat the request
1564 logger.Debugw(ctx, "OnuUpgradeFsm EndSwDlResponse busy: waiting before sending new request", log.Fields{
1565 "device-id": oFsm.deviceID})
1566 if inAbortingState {
1567 //if the EndSwDl was requested from state AbortingDL then use channel to indicate ONU busy/repeat indication
1568 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseBusy //repeat abort request
1569 }
mpagenko45586762021-10-01 08:30:22 +00001570 return
1571 }
mpagenko45586762021-10-01 08:30:22 +00001572 if inAbortingState {
1573 //if the EndSwDl was requested from state AbortingDL and response is error indication
mpagenkoa2b288f2021-10-21 11:25:27 +00001574 // that would be quite strange ONU behavior, no resolution from OnuAdapter, just let the FSM go on to disabled
1575 logger.Errorw(ctx, "OnuUpgradeFsm EndSwDlResponse result error - aborting anyway",
1576 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1577 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseAbort //error indication to stop waiting on EndDownloadResponse(abort)
1578 return
1579 }
1580 //else: ONU rejects the previous download, complete upgrade is immediately aborted with error
1581 logger.Errorw(ctx, "OnuUpgradeFsm EndSwDlResponse result error - abort upgrade",
1582 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1583 oFsm.mutexUpgradeParams.Lock()
1584 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
1585 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1586 oFsm.mutexUpgradeParams.Unlock()
1587 select {
1588 case oFsm.chOnuDlReady <- false:
1589 default:
1590 }
1591 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko45586762021-10-01 08:30:22 +00001592 return
1593 }
1594 oFsm.mutexUpgradeParams.Lock()
1595 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1596 logger.Debugw(ctx, "Expected EndSwDlResponse received", log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001597 if inAbortingState {
1598 oFsm.mutexUpgradeParams.Unlock()
1599 //if the EndSwDl was requested from state AbortingDL then use channel to indicate abort acceptance
1600 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseSuccess //success
1601 return
1602 }
1603 if !oFsm.useAPIVersion43 {
1604 //in the older API version the image version check was not possible
1605 // - assume new loaded image as valid-inactive immediately
1606 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_SUCCEEDED
1607 oFsm.volthaImageState = voltha.ImageState_IMAGE_INACTIVE
1608 oFsm.mutexUpgradeParams.Unlock()
mpagenkoa2b288f2021-10-21 11:25:27 +00001609 //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 +00001610 select {
mpagenko45586762021-10-01 08:30:22 +00001611 case oFsm.chOnuDlReady <- true:
1612 default:
1613 }
1614 } else {
1615 oFsm.mutexUpgradeParams.Unlock()
1616 }
1617 //use asynchronous channel sending to let the FSM proceed
1618 select {
1619 case oFsm.chReceiveExpectedResponse <- true:
1620 default:
1621 }
1622 return
1623 }
1624 oFsm.mutexUpgradeParams.Unlock()
1625 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse wrong ME instance: ignoring",
1626 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1627 // no state abort in case of unexpected ImageId, just keep waiting for the correct one
1628} //handleRxEndSwDownloadResponse
1629
1630func (oFsm *OnuUpgradeFsm) handleRxSwGetResponse(ctx context.Context, msg cmn.OmciMessage) {
1631 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeGetResponse)
1632 if msgLayer == nil {
1633 logger.Errorw(ctx, "Omci Msg layer could not be detected for SwImage GetResponse",
1634 log.Fields{"device-id": oFsm.deviceID})
1635 oFsm.abortOnOmciError(ctx, false)
1636 return
1637 }
1638 msgObj, msgOk := msgLayer.(*omci.GetResponse)
1639 if !msgOk {
1640 logger.Errorw(ctx, "Omci Msg layer could not be assigned for SwImage GetResponse",
1641 log.Fields{"device-id": oFsm.deviceID})
1642 oFsm.abortOnOmciError(ctx, false)
1643 return
1644 }
1645 logger.Debugw(ctx, "OnuUpgradeFsm SwImage GetResponse data", log.Fields{
1646 "device-id": oFsm.deviceID, "data-fields": msgObj})
1647 if msgObj.EntityClass == oFsm.pLastTxMeInstance.GetClassID() &&
1648 msgObj.EntityInstance == oFsm.pLastTxMeInstance.GetEntityID() {
1649 if msgObj.Result != me.Success {
1650 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse result error - later: drive FSM to abort state ?",
1651 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1652 oFsm.abortOnOmciError(ctx, false)
1653 return
1654 }
1655 } else {
1656 logger.Warnw(ctx, "OnuUpgradeFsm SwImage unexpected Entity GetResponse data - ignore",
1657 log.Fields{"device-id": oFsm.deviceID})
1658 return
1659 }
mpagenko45586762021-10-01 08:30:22 +00001660 meAttributes := msgObj.Attributes
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +00001661 var imageVersion string
1662 var imageIsCommitted, imageIsActive uint8
mpagenko45586762021-10-01 08:30:22 +00001663
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +00001664 if softwareImageIsCommitted, ok := meAttributes[me.SoftwareImage_IsCommitted]; ok {
1665 if softwareImageIsActiveimage, ok := meAttributes[me.SoftwareImage_IsActive]; ok {
1666 if softwareImageVersion, ok := meAttributes[me.SoftwareImage_Version]; ok {
1667 imageIsCommitted = softwareImageIsCommitted.(uint8)
1668 imageIsActive = softwareImageIsActiveimage.(uint8)
1669 imageVersion = cmn.TrimStringFromMeOctet(softwareImageVersion)
1670 logger.Debugw(ctx, "OnuUpgradeFsm - GetResponse Data for SoftwareImage",
1671 log.Fields{"device-id": oFsm.deviceID, "entityID": msgObj.EntityInstance,
1672 "version": imageVersion, "isActive": imageIsActive, "isCommitted": imageIsCommitted})
1673 } else {
1674 logger.Errorw(ctx,
1675 "OnuUpgradeFsm - Not all mandatory attributes present in in SoftwareImage instance - handling stopped!",
1676 log.Fields{"device-id": oFsm.deviceID})
1677 oFsm.abortOnOmciError(ctx, false)
1678 return
1679 }
1680 }
1681 }
mpagenko45586762021-10-01 08:30:22 +00001682 if oFsm.PAdaptFsm.PFsm.Current() == UpgradeStCheckImageName {
1683 //image name check after EndSwDownload, this state (and block) can only be taken if APIVersion43 is used
1684 oFsm.verifyOnuSwStatusAfterDownload(ctx, msgObj.EntityInstance, imageVersion, imageIsActive, imageIsCommitted)
1685 return
1686 }
1687
1688 //assumed only relevant state here is upgradeStCheckCommitted
1689 oFsm.mutexUpgradeParams.Lock()
1690 oFsm.conditionalCancelRequested = false //getting here any set (conditional) cancelRequest is not relevant anymore
1691 if msgObj.EntityInstance == oFsm.InactiveImageMeID && imageIsActive == cmn.SwIsActive {
1692 //a check on the delivered image version is not done, the ONU delivered version might be different from what might have been
1693 // indicated in the download image version string (version must be part of the image content itself)
1694 // so checking that might be quite unreliable
1695 //but with new API this was changed, assumption is that omci image version is known at download request and exactly that is used
1696 // in all the API references, so it can and should be checked here now
1697 if oFsm.useAPIVersion43 {
1698 if imageVersion != oFsm.imageVersion {
1699 //new active version indicated on OMCI from ONU is not the expected version
1700 logger.Errorw(ctx, "OnuUpgradeFsm image-version not matching the requested upgrade",
1701 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance,
1702 "onu-version": imageVersion, "expected-version": oFsm.imageVersion})
1703 // TODO!!!: error treatment?
1704 //TODO!!!: possibly send event information for aborted upgrade (aborted by wrong version)?
1705 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE //something like 'UNEXPECTED_VERSION' would be better - proto def
1706 oFsm.mutexUpgradeParams.Unlock()
1707 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1708 return
1709 }
1710 logger.Debugw(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU",
1711 log.Fields{"device-id": oFsm.deviceID})
1712 }
1713 if imageIsCommitted == cmn.SwIsCommitted {
1714 oFsm.upgradePhase = cUpgradeCommitted
1715 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTED
1716 //store the new commit flag to onuSwImageIndications (to keep them in sync)
1717 oFsm.pDevEntry.ModifySwImageActiveCommit(ctx, imageIsCommitted)
1718 logger.Infow(ctx, "requested SW image committed, releasing OnuUpgrade", log.Fields{"device-id": oFsm.deviceID})
1719 //deviceProcStatusUpdate not used anymore,
1720 // replaced by transferring the last (more) upgrade state information within removeOnuUpgradeFsm
1721 oFsm.mutexUpgradeParams.Unlock()
1722 //releasing the upgrade FSM on success
1723 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1724 return
1725 }
1726 //if not committed, abort upgrade as failed. There is no implementation here that would trigger this test again
1727 }
1728 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1729 oFsm.mutexUpgradeParams.Unlock()
1730 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse indications not matching requested upgrade",
1731 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1732 // TODO!!!: error treatment?
1733 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
1734 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1735} //handleRxSwGetResponse
1736
1737func (oFsm *OnuUpgradeFsm) verifyOnuSwStatusAfterDownload(ctx context.Context, aInstanceID uint16,
1738 aImageVersion string, aImageIsActive uint8, aImageIsCommitted uint8) {
1739 oFsm.mutexUpgradeParams.Lock()
1740 if aInstanceID == oFsm.InactiveImageMeID && aImageIsActive == cmn.SwIsInactive &&
1741 aImageIsCommitted == cmn.SwIsUncommitted {
1742 if aImageVersion != oFsm.imageVersion {
1743 //new stored inactive version indicated on OMCI from ONU is not the expected version
1744 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse version indication not matching requested upgrade",
1745 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": aInstanceID,
1746 "onu-version": aImageVersion, "expected-version": oFsm.imageVersion})
1747 //download state is set when entering the reset state
1748 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE //something like 'UNEXPECTED_VERSION' would be better - proto def
1749 oFsm.mutexUpgradeParams.Unlock()
1750 //stop the running ONU download timer
1751 //use non-blocking channel (to be independent from receiver state)
1752 select {
1753 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
1754 case oFsm.chOnuDlReady <- false:
1755 default:
1756 }
1757 // TODO!!!: error treatment?
1758 //TODO!!!: possibly send event information for aborted upgrade (aborted by wrong version)?
1759 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1760 return
1761 }
1762 //with APIVersion43 this is the point to consider the newly loaded image as valid (and inactive)
1763 oFsm.upgradePhase = cUpgradeDownloaded
1764 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_SUCCEEDED
1765 oFsm.volthaImageState = voltha.ImageState_IMAGE_INACTIVE
1766 //store the new inactive version to onuSwImageIndications (to keep them in sync)
1767 oFsm.pDevEntry.ModifySwImageInactiveVersion(ctx, oFsm.imageVersion)
1768 //proceed within upgrade FSM
1769 if oFsm.activateImage {
1770 //immediate activation requested
1771 oFsm.mutexUpgradeParams.Unlock()
1772 logger.Debugw(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU, continue with activation",
1773 log.Fields{"device-id": oFsm.deviceID})
1774 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvRequestActivate)
1775 } else {
1776 //have to wait on explicit activation request
1777 oFsm.mutexUpgradeParams.Unlock()
1778 logger.Infow(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU, wait for activate request",
1779 log.Fields{"device-id": oFsm.deviceID})
1780 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvWaitForActivate)
1781 }
1782 //use non-blocking channel (to be independent from receiver state)
1783 select {
1784 //use non-blocking channel to indicate that the download to ONU was successful
1785 case oFsm.chOnuDlReady <- true:
1786 default:
1787 }
1788 return
1789 }
1790 //not the expected image/image state
1791 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1792 oFsm.mutexUpgradeParams.Unlock()
1793 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse indications not matching requested upgrade",
1794 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": aInstanceID})
1795 // TODO!!!: error treatment?
1796 //TODO!!!: possibly send event information for aborted upgrade (aborted by ONU state indication)?
1797 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1798} //verifyOnuSwStatusAfterDownload
1799
Joey Armstrong89c812c2024-01-12 19:00:20 -05001800// abortOnOmciError aborts the upgrade processing with evAbort
1801//
1802// asynchronous/synchronous based on parameter aAsync
mpagenko45586762021-10-01 08:30:22 +00001803func (oFsm *OnuUpgradeFsm) abortOnOmciError(ctx context.Context, aAsync bool) {
mpagenko38662d02021-08-11 09:45:19 +00001804 oFsm.mutexUpgradeParams.Lock()
1805 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
mpagenko38662d02021-08-11 09:45:19 +00001806 oFsm.volthaDownloadReason = voltha.ImageState_OMCI_TRANSFER_ERROR
mpagenko38662d02021-08-11 09:45:19 +00001807 oFsm.mutexUpgradeParams.Unlock()
1808 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001809 if oFsm.PAdaptFsm != nil {
mpagenko38662d02021-08-11 09:45:19 +00001810 var err error
1811 if aAsync { //asynchronous call requested to ensure state transition
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001812 go func(a_pAFsm *cmn.AdapterFsm) {
1813 if a_pAFsm.PFsm != nil {
1814 err = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko38662d02021-08-11 09:45:19 +00001815 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001816 }(oFsm.PAdaptFsm)
mpagenko38662d02021-08-11 09:45:19 +00001817 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001818 if oFsm.PAdaptFsm.PFsm != nil {
1819 err = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko38662d02021-08-11 09:45:19 +00001820 }
1821 }
1822 if err != nil {
1823 logger.Warnw(ctx, "onu upgrade fsm could not abort on omci error", log.Fields{
1824 "device-id": oFsm.deviceID, "error": err})
1825 }
1826 }
1827}
1828
Joey Armstrong89c812c2024-01-12 19:00:20 -05001829// waitOnDownloadToAdapterReady state can only be reached with useAPIVersion43 (usage of pFileManager)
1830//
1831// precondition: mutexIsAwaitingAdapterDlResponse is lockek on call
mpagenko38662d02021-08-11 09:45:19 +00001832func (oFsm *OnuUpgradeFsm) waitOnDownloadToAdapterReady(ctx context.Context, aSyncChannel chan<- struct{},
1833 aWaitChannel chan bool) {
mpagenkoc26d4c02021-05-06 14:27:57 +00001834 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
mpagenko38662d02021-08-11 09:45:19 +00001835 downloadToAdapterTimeout := oFsm.pFileManager.GetDownloadTimeout(ctx)
mpagenkoc26d4c02021-05-06 14:27:57 +00001836 oFsm.isWaitingForAdapterDlResponse = true
1837 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenko38662d02021-08-11 09:45:19 +00001838 aSyncChannel <- struct{}{}
mpagenko80622a52021-02-09 16:53:23 +00001839 select {
1840 // maybe be also some outside cancel (but no context modeled for the moment ...)
1841 // case <-ctx.Done():
mpagenkoc26d4c02021-05-06 14:27:57 +00001842 // logger.Infow("OnuUpgradeFsm-waitOnDownloadToAdapterReady canceled", log.Fields{"for device-id": oFsm.deviceID})
1843 case <-time.After(downloadToAdapterTimeout): //10s should be enough for downloading some image to the adapter
1844 logger.Warnw(ctx, "OnuUpgradeFsm Waiting-adapter-download timeout", log.Fields{
1845 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier, "timeout": downloadToAdapterTimeout})
1846 oFsm.pFileManager.RemoveReadyRequest(ctx, oFsm.imageIdentifier, aWaitChannel)
mpagenko39b703e2021-08-25 13:38:40 +00001847 //running into timeout here may still have the download to adapter active -> abort
1848 oFsm.pFileManager.CancelDownload(ctx, oFsm.imageIdentifier)
mpagenkoc26d4c02021-05-06 14:27:57 +00001849 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1850 oFsm.isWaitingForAdapterDlResponse = false
1851 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenko39b703e2021-08-25 13:38:40 +00001852 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +00001853 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
mpagenko39b703e2021-08-25 13:38:40 +00001854 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'DOWNLOAD_TO_ADAPTER_TIMEOUT' would be better (proto)
mpagenko39b703e2021-08-25 13:38:40 +00001855 oFsm.mutexUpgradeParams.Unlock()
1856 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001857 if oFsm.PAdaptFsm != nil && oFsm.PAdaptFsm.PFsm != nil {
1858 err := oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko39b703e2021-08-25 13:38:40 +00001859 if err != nil {
1860 logger.Warnw(ctx, "onu upgrade fsm could not abort on omci error", log.Fields{
1861 "device-id": oFsm.deviceID, "error": err})
1862 }
1863 }
mpagenkoc26d4c02021-05-06 14:27:57 +00001864 return
1865
1866 case success := <-aWaitChannel:
1867 if success {
1868 logger.Debugw(ctx, "OnuUpgradeFsm image-downloaded received", log.Fields{"device-id": oFsm.deviceID})
1869 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1870 oFsm.isWaitingForAdapterDlResponse = false
1871 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
1872 //let the upgrade process proceed
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001873 pUpgradeFsm := oFsm.PAdaptFsm
mpagenkoc26d4c02021-05-06 14:27:57 +00001874 if pUpgradeFsm != nil {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001875 _ = pUpgradeFsm.PFsm.Event(UpgradeEvPrepareSwDownload)
mpagenkoc26d4c02021-05-06 14:27:57 +00001876 } else {
1877 logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
1878 }
1879 return
1880 }
mpagenko39b703e2021-08-25 13:38:40 +00001881 // waiting was aborted (assumed here to be caused by
1882 // error detection or cancel at download after upgrade FSM reset/abort with according image states set there)
mpagenkoc26d4c02021-05-06 14:27:57 +00001883 logger.Debugw(ctx, "OnuUpgradeFsm Waiting-adapter-download aborted", log.Fields{"device-id": oFsm.deviceID})
1884 oFsm.pFileManager.RemoveReadyRequest(ctx, oFsm.imageIdentifier, aWaitChannel)
1885 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1886 oFsm.isWaitingForAdapterDlResponse = false
1887 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenkoc26d4c02021-05-06 14:27:57 +00001888 return
mpagenko80622a52021-02-09 16:53:23 +00001889 }
1890}
mpagenkoc26d4c02021-05-06 14:27:57 +00001891
Joey Armstrong89c812c2024-01-12 19:00:20 -05001892// waitOnDownloadToOnuReady state can only be reached with useAPIVersion43 (usage of pFileManager)
mpagenkoc26d4c02021-05-06 14:27:57 +00001893func (oFsm *OnuUpgradeFsm) waitOnDownloadToOnuReady(ctx context.Context, aWaitChannel chan bool) {
1894 downloadToOnuTimeout := time.Duration(1+(oFsm.imageLength/0x400000)) * oFsm.downloadToOnuTimeout4MB
1895 logger.Debugw(ctx, "OnuUpgradeFsm start download-to-ONU timer", log.Fields{"device-id": oFsm.deviceID,
1896 "duration": downloadToOnuTimeout})
mpagenkoc26d4c02021-05-06 14:27:57 +00001897 select {
1898 // maybe be also some outside cancel (but no context modeled for the moment ...)
1899 // case <-ctx.Done():
1900 // logger.Infow("OnuUpgradeFsm-waitOnDownloadToOnuReady canceled", log.Fields{"for device-id": oFsm.deviceID})
1901 case <-time.After(downloadToOnuTimeout): //using an image-size depending timout (in minutes)
1902 logger.Warnw(ctx, "OnuUpgradeFsm Waiting-ONU-download timeout", log.Fields{
1903 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier, "timeout": downloadToOnuTimeout})
mpagenkoc26d4c02021-05-06 14:27:57 +00001904 //the upgrade process has to be aborted
mpagenko45586762021-10-01 08:30:22 +00001905 oFsm.abortOnOmciError(ctx, false)
mpagenkoc26d4c02021-05-06 14:27:57 +00001906 return
1907
1908 case success := <-aWaitChannel:
1909 if success {
1910 logger.Debugw(ctx, "OnuUpgradeFsm image-downloaded on ONU received", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001911 //all fine, let the FSM proceed like defined from the sender of this event
1912 return
1913 }
1914 // waiting was aborted (assumed here to be caused by
mpagenko39b703e2021-08-25 13:38:40 +00001915 // error detection or cancel at download after upgrade FSM reset/abort with according image states set there)
mpagenkoc26d4c02021-05-06 14:27:57 +00001916 logger.Debugw(ctx, "OnuUpgradeFsm Waiting-ONU-download aborted", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001917 return
1918 }
1919}
mpagenko45586762021-10-01 08:30:22 +00001920
Joey Armstrong89c812c2024-01-12 19:00:20 -05001921// waitOnAbortEndSwDlResponse waits for either abort/success or timeout of EndSwDownload (for abortion)
mpagenkoa2b288f2021-10-21 11:25:27 +00001922func (oFsm *OnuUpgradeFsm) waitOnAbortEndSwDlResponse(ctx context.Context) {
1923 logger.Debugw(ctx, "OnuUpgradeFsm start wait for EndSwDl response (abort)", log.Fields{"device-id": oFsm.deviceID})
1924 select {
1925 case <-time.After(oFsm.pOmciCC.GetMaxOmciTimeoutWithRetries() * time.Second):
1926 logger.Warnw(ctx, "OnuUpgradeFsm aborting download: timeout - no response received", log.Fields{"device-id": oFsm.deviceID})
1927 pUpgradeFsm := oFsm.PAdaptFsm
1928 if pUpgradeFsm != nil {
1929 _ = pUpgradeFsm.PFsm.Event(UpgradeEvRestart)
1930 } else {
1931 logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
1932 }
1933 return
1934 case response := <-oFsm.chReceiveAbortEndSwDlResponse:
1935 logger.Debugw(ctx, "OnuUpgradeFsm aborting download: response received",
1936 log.Fields{"device-id": oFsm.deviceID, "response": response})
1937 pUpgradeFsm := oFsm.PAdaptFsm
1938 if pUpgradeFsm != nil {
1939 if oFsm.abortingDlEvaluateResponse(ctx, pUpgradeFsm, response) {
1940 return //event sent from function already
1941 }
1942 _ = pUpgradeFsm.PFsm.Event(UpgradeEvRestart)
1943 } else {
1944 logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
1945 }
1946 return
1947 } //select
1948}
1949
Joey Armstrong89c812c2024-01-12 19:00:20 -05001950// stateUpdateOnReset writes the download and/or image state on entering the reset state according to FSM internal indications
mpagenko45586762021-10-01 08:30:22 +00001951func (oFsm *OnuUpgradeFsm) stateUpdateOnReset(ctx context.Context) {
1952 oFsm.mutexUpgradeParams.Lock()
1953 defer oFsm.mutexUpgradeParams.Unlock()
1954 if !oFsm.conditionalCancelRequested {
1955 switch oFsm.upgradePhase {
1956 case cUpgradeUndefined, cUpgradeDownloading: //coming from downloading
1957 //make sure the download state is only changed in case the device has still been downloading
1958 if oFsm.volthaDownloadReason == voltha.ImageState_CANCELLED_ON_REQUEST {
1959 // indication for termination on request
1960 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_CANCELLED
1961 } else if oFsm.volthaDownloadReason != voltha.ImageState_NO_ERROR {
1962 // indication for termination on failure
1963 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_FAILED
1964 }
1965 //reset the image state from Downloading in this case
1966 oFsm.volthaImageState = voltha.ImageState_IMAGE_UNKNOWN //something like 'IMAGE_DOWNLOAD_ABORTED' would be better (proto)
1967 //in all other upgrade phases the last set download state remains valid
1968 case cUpgradeActivating:
1969 //reset the image state from Activating in this case
1970 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATION_ABORTED
1971 case cUpgradeCommitting: // indication for request to abort waiting for response
1972 //reset the image state from Activating in this case
1973 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMIT_ABORTED
1974 //default: in all other upgrade phases keep the last set imageState
1975 } //switch
1976 } else {
1977 //when reaching reset state with conditional cancel that can only result from ONU related problems
1978 // (mostly ONU down indication) - derived from resetFsms call
1979 // and it can only be related to the downloading-to-ONU phase (no need to check that additionally)
1980 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_FAILED
1981 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1982 //reset the image state from Downloading in this case
1983 oFsm.volthaImageState = voltha.ImageState_IMAGE_UNKNOWN //something like 'IMAGE_DOWNLOAD_ABORTED' would be better (proto)
1984 }
1985}
Holger Hildebrandte7cc6092022-02-01 11:37:03 +00001986
1987// PrepareForGarbageCollection - remove references to prepare for garbage collection
1988func (oFsm *OnuUpgradeFsm) PrepareForGarbageCollection(ctx context.Context, aDeviceID string) {
1989 logger.Debugw(ctx, "prepare for garbage collection", log.Fields{"device-id": aDeviceID})
1990 oFsm.pDeviceHandler = nil
1991 oFsm.pDownloadManager = nil
1992 oFsm.pFileManager = nil
1993 oFsm.pDevEntry = nil
1994 oFsm.pOmciCC = nil
1995}