blob: 3da4913a956ef90846c62d26a916e6bf1f54ad8e [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
Balaji Seenivasana52fb0c2024-12-18 07:50:42 +0530848 err := oFsm.pOmciCC.SendOnuSwSectionsWindowWithRxSupervision(ctx, omciTxReq, oFsm.pDeviceHandler.GetOmciTimeout(), oFsm.PAdaptFsm.CommChan)
849 if err != nil {
850 logger.Errorw(ctx, "DlSection abort: can't send window acknowledgement request to ONU ", log.Fields{
851 "device-id": oFsm.deviceID, "section absolute": oFsm.nextDownloadSectionsAbsolute, "error": err})
852 oFsm.abortOnOmciError(ctx, false)
853 }
mpagenkoa2b288f2021-10-21 11:25:27 +0000854 return
855 }
856 logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
mpagenko80622a52021-02-09 16:53:23 +0000857 return
858 }
kesavand011d5162021-11-25 19:21:06 +0530859
mpagenko80622a52021-02-09 16:53:23 +0000860 oFsm.nextDownloadSectionsWindow++ //increase the window related section counter only if not in the last section
mpagenko59498c12021-03-18 14:15:15 +0000861 if oFsm.omciSectionInterleaveDelay > 0 {
mpagenko80622a52021-02-09 16:53:23 +0000862 //ensure a defined intersection-time-gap to leave space for further processing, other ONU's ...
mpagenkoaa3afe92021-05-21 16:20:58 +0000863 oFsm.mutexUpgradeParams.Unlock() //unlock here to give other functions some chance to process during/after the send request
mpagenko59498c12021-03-18 14:15:15 +0000864 time.Sleep(oFsm.omciSectionInterleaveDelay * time.Millisecond)
mpagenkoaa3afe92021-05-21 16:20:58 +0000865 oFsm.mutexUpgradeParams.Lock()
mpagenko80622a52021-02-09 16:53:23 +0000866 }
867 }
mpagenkoa2b288f2021-10-21 11:25:27 +0000868} //runSwDlSectionWindow
mpagenko80622a52021-02-09 16:53:23 +0000869
870func (oFsm *OnuUpgradeFsm) enterVerifyWindow(ctx context.Context, e *fsm.Event) {
871 logger.Debugw(ctx, "OnuUpgradeFsm verify DL window ack", log.Fields{
872 "for window": oFsm.nextDownloadWindow, "device-id": oFsm.deviceID})
873}
874
875func (oFsm *OnuUpgradeFsm) enterFinalizeDL(ctx context.Context, e *fsm.Event) {
mpagenko80622a52021-02-09 16:53:23 +0000876 logger.Infow(ctx, "OnuUpgradeFsm finalize DL", log.Fields{
mpagenko59498c12021-03-18 14:15:15 +0000877 "device-id": oFsm.deviceID, "crc": strconv.FormatInt(int64(oFsm.imageCRC), 16), "delay": oFsm.delayEndSwDl})
mpagenko609a69a2021-12-02 08:12:58 +0000878 //use a background routine to wait EndSwDlDelay and then send the EndSwDl request
879 // in order to avoid blocking on synchronous event calls for the complete wait time
880 // and to allow for state transition before sending the EndSwDl request
881 go oFsm.delayAndSendEndSwDl(ctx)
882}
mpagenko80622a52021-02-09 16:53:23 +0000883
Joey Armstrong89c812c2024-01-12 19:00:20 -0500884// delayAndSendEndSwDl ensures a delay before sending the EndSwDl request
885//
886// may also be aborted by parallel channel reception on chAbortEndSwDl
mpagenko609a69a2021-12-02 08:12:58 +0000887func (oFsm *OnuUpgradeFsm) delayAndSendEndSwDl(ctx context.Context) {
mpagenkoaa3afe92021-05-21 16:20:58 +0000888 oFsm.mutexUpgradeParams.RLock()
mpagenko80622a52021-02-09 16:53:23 +0000889 if oFsm.delayEndSwDl {
mpagenkoaa3afe92021-05-21 16:20:58 +0000890 oFsm.mutexUpgradeParams.RUnlock()
mpagenko609a69a2021-12-02 08:12:58 +0000891 //give the ONU some time for image evaluation (hoping it does not only start this evaluation on first EndSwDl itself)
892 logger.Debugw(ctx, "OnuUpgradeFsm delay EndSwDl", log.Fields{"device-id": oFsm.deviceID,
893 "duration_s": cOmciEndSwDlDelaySeconds})
894 select {
895 case <-time.After(cOmciEndSwDlDelaySeconds * time.Second):
896 logger.Warnw(ctx, "OnuUpgradeFsm start sending EndSwDl", log.Fields{
897 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier})
898 case <-oFsm.chAbortDelayEndSwDl:
899 logger.Debugw(ctx, "OnuUpgradeFsm abort request to send EndSwDl", log.Fields{"device-id": oFsm.deviceID})
900 //according further state transition is ensured by the entity that sent the abort
901 return
902 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000903 } else {
904 oFsm.mutexUpgradeParams.RUnlock()
mpagenko80622a52021-02-09 16:53:23 +0000905 }
906
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000907 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000908 if pBaseFsm == nil {
mpagenko38662d02021-08-11 09:45:19 +0000909 logger.Errorw(ctx, "EndSwDl abort: BaseFsm invalid", log.Fields{"device-id": oFsm.deviceID})
910 oFsm.mutexUpgradeParams.Lock()
mpagenko38662d02021-08-11 09:45:19 +0000911 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR
912 oFsm.mutexUpgradeParams.Unlock()
mpagenko59498c12021-03-18 14:15:15 +0000913 // Can't call FSM Event directly, decoupling it
mpagenko609a69a2021-12-02 08:12:58 +0000914 _ = pBaseFsm.PFsm.Event(UpgradeEvAbort)
mpagenko59498c12021-03-18 14:15:15 +0000915 return
916 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000917 err := oFsm.pOmciCC.SendEndSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
Holger Hildebrandt5458d892022-05-31 09:52:06 +0000918 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, oFsm.origImageLength, oFsm.imageCRC, oFsm.isExtendedOmci)
Holger Hildebrandt288d9472021-09-06 10:47:53 +0000919
mpagenko80622a52021-02-09 16:53:23 +0000920 if err != nil {
mpagenkoa2b288f2021-10-21 11:25:27 +0000921 logger.Errorw(ctx, "EndSwDl abort: error sending EndSwDl", log.Fields{
mpagenko80622a52021-02-09 16:53:23 +0000922 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +0000923 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +0000924 return
925 }
mpagenko609a69a2021-12-02 08:12:58 +0000926 //from here on sending of EndSwDl(Abort) is not needed anymore (even though response is not yet received)
927 // this avoids sending of both EndSwDl(Success) and EndSwDl(Abort) when cancellation falls just into this window
928 // the ONU must theoretically be prepared to receive none of them (in case of OMCI transfer issues) e.g. by timeout
929 oFsm.isEndSwDlOpen = false
930 // wait for the EndSwDLResponse and check, if the ONU is ready for activation
931 _ = pBaseFsm.PFsm.Event(UpgradeEvWaitEndDownload)
932} //delayAndSendEndSwDl
mpagenko59498c12021-03-18 14:15:15 +0000933
934func (oFsm *OnuUpgradeFsm) enterWaitEndDL(ctx context.Context, e *fsm.Event) {
935 logger.Infow(ctx, "OnuUpgradeFsm WaitEndDl", log.Fields{
936 "device-id": oFsm.deviceID, "wait delay": oFsm.waitDelayEndSwDl * time.Second, "wait count": oFsm.waitCountEndSwDl})
937 if oFsm.waitCountEndSwDl == 0 {
938 logger.Errorw(ctx, "WaitEndDl abort: max limit of EndSwDL reached", log.Fields{
939 "device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000940 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000941 if pBaseFsm == nil {
942 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
943 "device-id": oFsm.deviceID})
944 return
945 }
mpagenko38662d02021-08-11 09:45:19 +0000946 oFsm.mutexUpgradeParams.Lock()
mpagenko39b703e2021-08-25 13:38:40 +0000947 oFsm.volthaDownloadReason = voltha.ImageState_IMAGE_REFUSED_BY_ONU //something like 'END_DOWNLOAD_TIMEOUT' would be better (proto)
mpagenko38662d02021-08-11 09:45:19 +0000948 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000949 go func(a_pAFsm *cmn.AdapterFsm) {
950 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko59498c12021-03-18 14:15:15 +0000951 }(pBaseFsm)
952 return
953 }
954
955 oFsm.waitCountEndSwDl--
956 select {
957 case <-time.After(oFsm.waitDelayEndSwDl * time.Second):
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000958 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000959 if pBaseFsm == nil {
960 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
961 "device-id": oFsm.deviceID})
962 //FSM may be reset already from somewhere else, nothing we can do here anymore
963 return
964 }
965 //retry End SW DL
mpagenkoaa3afe92021-05-21 16:20:58 +0000966 oFsm.mutexUpgradeParams.Lock()
mpagenko59498c12021-03-18 14:15:15 +0000967 oFsm.delayEndSwDl = false //no more extra delay for the request
mpagenkoaa3afe92021-05-21 16:20:58 +0000968 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000969 go func(a_pAFsm *cmn.AdapterFsm) {
970 _ = a_pAFsm.PFsm.Event(UpgradeEvContinueFinalize)
mpagenko59498c12021-03-18 14:15:15 +0000971 }(pBaseFsm)
972 return
973 case success := <-oFsm.chReceiveExpectedResponse:
974 logger.Debugw(ctx, "WaitEndDl stop wait timer", log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +0000975 oFsm.isEndSwDlOpen = false //no request to abort of download (already finished or immediate abort)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000976 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000977 if pBaseFsm == nil {
978 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
979 "device-id": oFsm.deviceID})
980 //FSM may be reset already from somewhere else, nothing we can do here anymore
981 return
982 }
983 if success {
984 //answer received with ready indication
mpagenko38662d02021-08-11 09:45:19 +0000985 //useAPIVersion43 may not conflict in concurrency in this state function
986 if oFsm.useAPIVersion43 { // newer API usage requires verification of downloaded image version
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000987 go func(a_pAFsm *cmn.AdapterFsm) {
988 _ = a_pAFsm.PFsm.Event(UpgradeEvCheckImageName)
mpagenkoc26d4c02021-05-06 14:27:57 +0000989 }(pBaseFsm)
mpagenko38662d02021-08-11 09:45:19 +0000990 } else { // elder API usage does not support image version check -immediately consider download as successful
991 if oFsm.activateImage {
992 //immediate activation requested
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000993 go func(a_pAFsm *cmn.AdapterFsm) {
994 _ = a_pAFsm.PFsm.Event(UpgradeEvRequestActivate)
mpagenko38662d02021-08-11 09:45:19 +0000995 }(pBaseFsm)
996 } else {
997 //have to wait on explicit activation request
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000998 go func(a_pAFsm *cmn.AdapterFsm) {
999 _ = a_pAFsm.PFsm.Event(UpgradeEvWaitForActivate)
mpagenko38662d02021-08-11 09:45:19 +00001000 }(pBaseFsm)
1001 }
mpagenkoc26d4c02021-05-06 14:27:57 +00001002 }
mpagenko59498c12021-03-18 14:15:15 +00001003 return
1004 }
1005 //timer was aborted
mpagenko45586762021-10-01 08:30:22 +00001006 oFsm.abortOnOmciError(ctx, true)
mpagenko59498c12021-03-18 14:15:15 +00001007 return
1008 }
mpagenko80622a52021-02-09 16:53:23 +00001009}
1010
mpagenko38662d02021-08-11 09:45:19 +00001011func (oFsm *OnuUpgradeFsm) enterCheckImageName(ctx context.Context, e *fsm.Event) {
1012 logger.Debugw(ctx, "OnuUpgradeFsm checking downloaded image name", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001013 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
Holger Hildebrandt3ac49bd2022-02-07 17:46:43 +00001014 requestedAttributes := me.AttributeValueMap{me.SoftwareImage_IsCommitted: 0, me.SoftwareImage_IsActive: 0, me.SoftwareImage_Version: ""}
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001015 meInstance, err := oFsm.pOmciCC.SendGetMe(log.WithSpanFromContext(context.Background(), ctx),
1016 me.SoftwareImageClassID, oFsm.InactiveImageMeID, requestedAttributes, oFsm.pDeviceHandler.GetOmciTimeout(),
Holger Hildebrandtd930cb22022-06-17 09:24:50 +00001017 false, oFsm.PAdaptFsm.CommChan, oFsm.isExtendedOmci)
mpagenko38662d02021-08-11 09:45:19 +00001018 if err != nil {
1019 logger.Errorw(ctx, "OnuUpgradeFsm get Software Image ME result error",
1020 log.Fields{"device-id": oFsm.deviceID, "Error": err})
mpagenko45586762021-10-01 08:30:22 +00001021 oFsm.abortOnOmciError(ctx, true)
mpagenko38662d02021-08-11 09:45:19 +00001022 return
1023 }
1024 oFsm.pLastTxMeInstance = meInstance
1025}
1026
mpagenko80622a52021-02-09 16:53:23 +00001027func (oFsm *OnuUpgradeFsm) enterActivateSw(ctx context.Context, e *fsm.Event) {
1028 logger.Infow(ctx, "OnuUpgradeFsm activate SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001029 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
mpagenko80622a52021-02-09 16:53:23 +00001030
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001031 err := oFsm.pOmciCC.SendActivateSoftware(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
Holger Hildebrandt5458d892022-05-31 09:52:06 +00001032 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, oFsm.isExtendedOmci)
mpagenko80622a52021-02-09 16:53:23 +00001033 if err != nil {
1034 logger.Errorw(ctx, "ActivateSw abort: can't send activate frame", log.Fields{
1035 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +00001036 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +00001037 return
1038 }
mpagenko38662d02021-08-11 09:45:19 +00001039 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +00001040 oFsm.upgradePhase = cUpgradeActivating //start of image activation for ONU
mpagenko38662d02021-08-11 09:45:19 +00001041 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATING
1042 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +00001043}
1044
1045func (oFsm *OnuUpgradeFsm) enterCommitSw(ctx context.Context, e *fsm.Event) {
mpagenko38662d02021-08-11 09:45:19 +00001046 logger.Debugw(ctx, "OnuUpgradeFsm start commit SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001047 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
mpagenko38662d02021-08-11 09:45:19 +00001048 //any abort request (also conditional) is still regarded as valid as the commit indication might not be possible to verify
1049 // (which is a bit problematic as the ONU might already be in committed state,
1050 // in this case (committing failed) always 'onuimage list' should be used to verify the real state (if ONU is reachable))
1051 if activeImageID, err := oFsm.pDevEntry.GetActiveImageMeID(ctx); err == nil {
mpagenkoaa3afe92021-05-21 16:20:58 +00001052 oFsm.mutexUpgradeParams.Lock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001053 if activeImageID == oFsm.InactiveImageMeID {
1054 inactiveImageID := oFsm.InactiveImageMeID
mpagenko15ff4a52021-03-02 10:09:20 +00001055 logger.Infow(ctx, "OnuUpgradeFsm commit SW", log.Fields{
mpagenkoaa3afe92021-05-21 16:20:58 +00001056 "device-id": oFsm.deviceID, "me-id": inactiveImageID}) //more efficient activeImageID with above check
1057 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTING
mpagenko45586762021-10-01 08:30:22 +00001058 oFsm.upgradePhase = cUpgradeCommitting //start of image commitment for ONU
mpagenkoaa3afe92021-05-21 16:20:58 +00001059 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001060 err := oFsm.pOmciCC.SendCommitSoftware(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
Holger Hildebrandt5458d892022-05-31 09:52:06 +00001061 oFsm.PAdaptFsm.CommChan, inactiveImageID, oFsm.isExtendedOmci) //more efficient activeImageID with above check
mpagenko15ff4a52021-03-02 10:09:20 +00001062 if err != nil {
1063 logger.Errorw(ctx, "CommitSw abort: can't send commit sw frame", log.Fields{
1064 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +00001065 oFsm.abortOnOmciError(ctx, true)
mpagenko15ff4a52021-03-02 10:09:20 +00001066 return
1067 }
1068 return
1069 }
mpagenko38662d02021-08-11 09:45:19 +00001070 oFsm.mutexUpgradeParams.Unlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001071 logger.Errorw(ctx, "OnuUpgradeFsm active ImageId <> IdToCommit", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001072 "device-id": oFsm.deviceID, "active ID": activeImageID, "to commit ID": oFsm.InactiveImageMeID})
mpagenko38662d02021-08-11 09:45:19 +00001073 } else {
1074 logger.Errorw(ctx, "OnuUpgradeFsm can't commit, no valid active image", log.Fields{
1075 "device-id": oFsm.deviceID})
mpagenko15ff4a52021-03-02 10:09:20 +00001076 }
mpagenko38662d02021-08-11 09:45:19 +00001077 oFsm.mutexUpgradeParams.Lock()
1078 oFsm.conditionalCancelRequested = false //any lingering conditional cancelRequest is superseded by this error
mpagenko38662d02021-08-11 09:45:19 +00001079 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
mpagenko38662d02021-08-11 09:45:19 +00001080 oFsm.mutexUpgradeParams.Unlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001081 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001082 pBaseFsm := oFsm.PAdaptFsm
mpagenko15ff4a52021-03-02 10:09:20 +00001083 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001084 go func(a_pAFsm *cmn.AdapterFsm) {
1085 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko15ff4a52021-03-02 10:09:20 +00001086 }(pBaseFsm)
1087}
1088
1089func (oFsm *OnuUpgradeFsm) enterCheckCommitted(ctx context.Context, e *fsm.Event) {
mpagenko38662d02021-08-11 09:45:19 +00001090 logger.Debugw(ctx, "OnuUpgradeFsm checking committed SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001091 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
Holger Hildebrandt3ac49bd2022-02-07 17:46:43 +00001092 requestedAttributes := me.AttributeValueMap{me.SoftwareImage_IsCommitted: 0, me.SoftwareImage_IsActive: 0, me.SoftwareImage_Version: ""}
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001093 meInstance, err := oFsm.pOmciCC.SendGetMe(log.WithSpanFromContext(context.Background(), ctx),
Holger Hildebrandtd930cb22022-06-17 09:24:50 +00001094 me.SoftwareImageClassID, oFsm.InactiveImageMeID, requestedAttributes,
1095 oFsm.pDeviceHandler.GetOmciTimeout(), false, oFsm.PAdaptFsm.CommChan, oFsm.isExtendedOmci)
ozgecanetsiab36ed572021-04-01 10:38:48 +03001096 if err != nil {
1097 logger.Errorw(ctx, "OnuUpgradeFsm get Software Image ME result error",
1098 log.Fields{"device-id": oFsm.deviceID, "Error": err})
mpagenko45586762021-10-01 08:30:22 +00001099 oFsm.abortOnOmciError(ctx, true)
ozgecanetsiab36ed572021-04-01 10:38:48 +03001100 return
1101 }
mpagenko15ff4a52021-03-02 10:09:20 +00001102 oFsm.pLastTxMeInstance = meInstance
mpagenko80622a52021-02-09 16:53:23 +00001103}
1104
1105func (oFsm *OnuUpgradeFsm) enterResetting(ctx context.Context, e *fsm.Event) {
1106 logger.Debugw(ctx, "OnuUpgradeFsm resetting", log.Fields{"device-id": oFsm.deviceID})
1107
mpagenko45586762021-10-01 08:30:22 +00001108 oFsm.stateUpdateOnReset(ctx)
mpagenko38662d02021-08-11 09:45:19 +00001109
mpagenkoa2b288f2021-10-21 11:25:27 +00001110 oFsm.mutexAbortRequest.Lock()
1111 //to be sure to abort a possibly still running runSwDlSectionWindow()
1112 // in case the reset was not received from cancel() and download not finished correctly
1113 oFsm.abortRequested = oFsm.volthaDownloadReason
1114 oFsm.mutexAbortRequest.Unlock()
1115
mpagenkoc26d4c02021-05-06 14:27:57 +00001116 // in case the download-to-ONU timer is still running - cancel it
mpagenko39b703e2021-08-25 13:38:40 +00001117 //use non-blocking channel (to be independent from receiver state)
1118 select {
1119 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
1120 case oFsm.chOnuDlReady <- false:
1121 default:
mpagenkoc26d4c02021-05-06 14:27:57 +00001122 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001123 pConfigUpgradeStateAFsm := oFsm.PAdaptFsm
1124 if pConfigUpgradeStateAFsm != nil {
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001125 var nextEvent string
1126 if oFsm.isEndSwDlOpen {
mpagenko45586762021-10-01 08:30:22 +00001127 if oFsm.repeatAbort {
1128 oFsm.delayEndSwDl = true //run next abort with delay
1129 } else { //initial request
1130 oFsm.delayEndSwDl = false //run next abort with no delay
1131 oFsm.waitCountEndSwDl = cWaitCountEndSwDl //init for possible repetitions
1132 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001133 nextEvent = UpgradeEvAbortSwDownload
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001134 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001135 nextEvent = UpgradeEvRestart
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001136 }
1137 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001138 go func(a_pAFsm *cmn.AdapterFsm) {
1139 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1140 _ = a_pAFsm.PFsm.Event(nextEvent)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001141 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001142 }(pConfigUpgradeStateAFsm)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001143 }
1144}
mpagenkoc26d4c02021-05-06 14:27:57 +00001145
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001146func (oFsm *OnuUpgradeFsm) enterAbortingDL(ctx context.Context, e *fsm.Event) {
1147 logger.Debugw(ctx, "OnuUpgradeFsm aborting download to ONU", log.Fields{"device-id": oFsm.deviceID})
1148
mpagenko45586762021-10-01 08:30:22 +00001149 oFsm.mutexUpgradeParams.RLock()
1150 if oFsm.delayEndSwDl {
1151 oFsm.mutexUpgradeParams.RUnlock()
1152 //give the ONU some time for image discard activities
1153 time.Sleep(cOmciEndSwDlDelaySeconds * time.Second)
1154 } else {
1155 oFsm.mutexUpgradeParams.RUnlock()
1156 }
1157
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001158 pBaseFsm := oFsm.PAdaptFsm
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001159 if pBaseFsm == nil {
1160 logger.Errorw(ctx, "OnuUpgradeFsm aborting download: BaseFsm invalid", log.Fields{"device-id": oFsm.deviceID})
1161 return
1162 }
1163 // abort the download operation by sending an end software download message with invalid CRC and image size
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001164 err := oFsm.pOmciCC.SendEndSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
Holger Hildebrandt5458d892022-05-31 09:52:06 +00001165 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, 0, 0xFFFFFFFF, oFsm.isExtendedOmci)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001166
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001167 if err != nil {
1168 logger.Errorw(ctx, "OnuUpgradeFsm aborting download: can't send EndSwDl request", log.Fields{"device-id": oFsm.deviceID})
1169 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001170 go func(a_pAFsm *cmn.AdapterFsm) {
1171 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1172 _ = a_pAFsm.PFsm.Event(UpgradeEvRestart)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001173 }
1174 }(pBaseFsm)
1175 return
1176 }
mpagenkoa2b288f2021-10-21 11:25:27 +00001177
1178 //avoid waiting in the enterXXX function here,
1179 // otherwise synchronous event calls (like from RxMessage processing) may block and block complete Rx processing then
1180 go oFsm.waitOnAbortEndSwDlResponse(ctx)
mpagenko45586762021-10-01 08:30:22 +00001181}
1182
Joey Armstrong89c812c2024-01-12 19:00:20 -05001183// abortingDlEvaluateResponse waits for a channel indication with decision to proceed the FSM processing
mpagenko45586762021-10-01 08:30:22 +00001184func (oFsm *OnuUpgradeFsm) abortingDlEvaluateResponse(ctx context.Context,
1185 pBaseFsm *cmn.AdapterFsm, aResponseResult tEndSwDlResponseResult) bool {
1186 switch aResponseResult {
1187 case cEndSwDlResponseBusy: // indication for device busy, needs repetition
1188 if oFsm.waitCountEndSwDl == 0 {
1189 logger.Errorw(ctx, "aborting download: max limit of EndSwDl reached", log.Fields{
1190 "device-id": oFsm.deviceID})
1191 go func(a_pAFsm *cmn.AdapterFsm) {
1192 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1193 _ = a_pAFsm.PFsm.Event(UpgradeEvRestart) //give up and let FSM terminate
1194 }
1195 }(pBaseFsm)
1196 } else {
1197 logger.Debugw(ctx, "aborting download: re-trigger sending abort SwDl", log.Fields{
1198 "device-id": oFsm.deviceID, "counter": oFsm.waitCountEndSwDl})
1199 oFsm.waitCountEndSwDl--
1200 oFsm.repeatAbort = true //repeated request in next round
1201 go func(a_pAFsm *cmn.AdapterFsm) {
1202 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1203 _ = a_pAFsm.PFsm.Event(UpgradeEvReset) //which then re-triggers sending AbortSwDL
1204 }
1205 }(pBaseFsm)
1206 }
1207 return true
1208 case cEndSwDlResponseSuccess: // indication for success response
1209 logger.Infow(ctx, "aborting download: success response, terminating FSM", log.Fields{
1210 "device-id": oFsm.deviceID})
1211 case cEndSwDlResponseAbort: // indication for request to abort waiting for response
1212 logger.Infow(ctx, "aborting download: request to abort waiting, terminating FSM", log.Fields{
1213 "device-id": oFsm.deviceID})
1214 default:
1215 logger.Errorw(ctx, "aborting download: unknown channel indication, terminating FSM", log.Fields{
1216 "device-id": oFsm.deviceID})
1217 } //switch
1218 return false
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001219}
1220
1221func (oFsm *OnuUpgradeFsm) enterRestarting(ctx context.Context, e *fsm.Event) {
1222 logger.Debugw(ctx, "OnuUpgradeFsm restarting", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001223 pConfigUpgradeStateAFsm := oFsm.PAdaptFsm
1224 if pConfigUpgradeStateAFsm != nil {
mpagenko80622a52021-02-09 16:53:23 +00001225 // abort running message processing
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001226 fsmAbortMsg := cmn.Message{
1227 Type: cmn.TestMsg,
1228 Data: cmn.TestMessage{
1229 TestMessageVal: cmn.AbortMessageProcessing,
mpagenko80622a52021-02-09 16:53:23 +00001230 },
1231 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001232 pConfigUpgradeStateAFsm.CommChan <- fsmAbortMsg
mpagenko80622a52021-02-09 16:53:23 +00001233
1234 //try to restart the FSM to 'disabled'
1235 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001236 go func(a_pAFsm *cmn.AdapterFsm) {
1237 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1238 _ = a_pAFsm.PFsm.Event(UpgradeEvDisable)
mpagenko80622a52021-02-09 16:53:23 +00001239 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001240 }(pConfigUpgradeStateAFsm)
mpagenko80622a52021-02-09 16:53:23 +00001241 }
1242}
1243
1244func (oFsm *OnuUpgradeFsm) enterDisabled(ctx context.Context, e *fsm.Event) {
1245 logger.Debugw(ctx, "OnuUpgradeFsm enters disabled state", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001246 // 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 +00001247 if oFsm.pDeviceHandler != nil {
1248 //request removal of 'reference' in the Handler (completely clear the FSM and its data)
mpagenko38662d02021-08-11 09:45:19 +00001249 pLastUpgradeImageState := &voltha.ImageState{
1250 Version: oFsm.imageVersion,
1251 DownloadState: oFsm.volthaDownloadState,
1252 Reason: oFsm.volthaDownloadReason,
1253 ImageState: oFsm.volthaImageState,
1254 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001255 go oFsm.pDeviceHandler.RemoveOnuUpgradeFsm(ctx, pLastUpgradeImageState)
mpagenko80622a52021-02-09 16:53:23 +00001256 }
1257}
1258
1259func (oFsm *OnuUpgradeFsm) processOmciUpgradeMessages(ctx context.Context) { //ctx context.Context?
1260 logger.Debugw(ctx, "Start OnuUpgradeFsm Msg processing", log.Fields{"for device-id": oFsm.deviceID})
1261loop:
1262 for {
1263 // case <-ctx.Done():
1264 // logger.Info(ctx,"MibSync Msg", log.Fields{"Message handling canceled via context for device-id": oFsm.deviceID})
1265 // break loop
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001266 message, ok := <-oFsm.PAdaptFsm.CommChan
mpagenko80622a52021-02-09 16:53:23 +00001267 if !ok {
1268 logger.Info(ctx, "OnuUpgradeFsm Rx Msg - could not read from channel", log.Fields{"device-id": oFsm.deviceID})
1269 // but then we have to ensure a restart of the FSM as well - as exceptional procedure
mpagenko45586762021-10-01 08:30:22 +00001270 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +00001271 break loop
1272 }
1273 logger.Debugw(ctx, "OnuUpgradeFsm Rx Msg", log.Fields{"device-id": oFsm.deviceID})
1274
1275 switch message.Type {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001276 case cmn.TestMsg:
1277 msg, _ := message.Data.(cmn.TestMessage)
1278 if msg.TestMessageVal == cmn.AbortMessageProcessing {
mpagenko80622a52021-02-09 16:53:23 +00001279 logger.Infow(ctx, "OnuUpgradeFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.deviceID})
1280 break loop
1281 }
1282 logger.Warnw(ctx, "OnuUpgradeFsm unknown TestMessage", log.Fields{"device-id": oFsm.deviceID, "MessageVal": msg.TestMessageVal})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001283 case cmn.OMCI:
1284 msg, _ := message.Data.(cmn.OmciMessage)
mpagenko80622a52021-02-09 16:53:23 +00001285 oFsm.handleOmciOnuUpgradeMessage(ctx, msg)
1286 default:
1287 logger.Warn(ctx, "OnuUpgradeFsm Rx unknown message", log.Fields{"device-id": oFsm.deviceID,
1288 "message.Type": message.Type})
1289 }
1290 }
1291 logger.Infow(ctx, "End OnuUpgradeFsm Msg processing", log.Fields{"device-id": oFsm.deviceID})
1292}
1293
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001294func (oFsm *OnuUpgradeFsm) handleOmciOnuUpgradeMessage(ctx context.Context, msg cmn.OmciMessage) {
mpagenko80622a52021-02-09 16:53:23 +00001295 logger.Debugw(ctx, "Rx OMCI OnuUpgradeFsm Msg", log.Fields{"device-id": oFsm.deviceID,
1296 "msgType": msg.OmciMsg.MessageType})
1297
1298 switch msg.OmciMsg.MessageType {
1299 case omci.StartSoftwareDownloadResponseType:
1300 {
mpagenko45586762021-10-01 08:30:22 +00001301 oFsm.handleRxStartSwDownloadResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001302 return
1303 } //StartSoftwareDownloadResponseType
1304 case omci.DownloadSectionResponseType:
1305 {
mpagenko45586762021-10-01 08:30:22 +00001306 oFsm.handleRxSwSectionResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001307 return
mpagenko80622a52021-02-09 16:53:23 +00001308 } //DownloadSectionResponseType
1309 case omci.EndSoftwareDownloadResponseType:
1310 {
mpagenko45586762021-10-01 08:30:22 +00001311 oFsm.handleRxEndSwDownloadResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001312 return
1313 } //EndSoftwareDownloadResponseType
1314 case omci.ActivateSoftwareResponseType:
1315 {
1316 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeActivateSoftwareResponse)
1317 if msgLayer == nil {
1318 logger.Errorw(ctx, "Omci Msg layer could not be detected for ActivateSw",
1319 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001320 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001321 return
1322 }
1323 msgObj, msgOk := msgLayer.(*omci.ActivateSoftwareResponse)
1324 if !msgOk {
1325 logger.Errorw(ctx, "Omci Msg layer could not be assigned for ActivateSw",
1326 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001327 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001328 return
1329 }
1330 logger.Debugw(ctx, "OnuUpgradeFsm ActivateSwResponse data", log.Fields{
1331 "device-id": oFsm.deviceID, "data-fields": msgObj})
1332 if msgObj.Result != me.Success {
1333 logger.Errorw(ctx, "OnuUpgradeFsm ActivateSwResponse result error - later: drive FSM to abort state ?",
1334 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
mpagenko45586762021-10-01 08:30:22 +00001335 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001336 return
1337 }
mpagenko183647c2021-06-08 15:25:04 +00001338 oFsm.mutexUpgradeParams.Lock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001339 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
mpagenko38662d02021-08-11 09:45:19 +00001340 // the image is regarded as active really only after ONU reboot and according indication (ONU down/up procedure)
mpagenko183647c2021-06-08 15:25:04 +00001341 oFsm.mutexUpgradeParams.Unlock()
1342 logger.Infow(ctx, "Expected ActivateSwResponse received",
1343 log.Fields{"device-id": oFsm.deviceID, "commit": oFsm.commitImage})
1344 if oFsm.commitImage {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001345 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvWaitForCommit)
mpagenko183647c2021-06-08 15:25:04 +00001346 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001347 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvActivationDone) // let the FSM wait for external commit request
mpagenko183647c2021-06-08 15:25:04 +00001348 }
mpagenko80622a52021-02-09 16:53:23 +00001349 return
1350 }
mpagenko183647c2021-06-08 15:25:04 +00001351 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +00001352 logger.Errorw(ctx, "OnuUpgradeFsm ActivateSwResponse wrong ME instance: abort",
1353 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
mpagenko45586762021-10-01 08:30:22 +00001354 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001355 return
1356 } //ActivateSoftwareResponseType
mpagenko15ff4a52021-03-02 10:09:20 +00001357 case omci.CommitSoftwareResponseType:
1358 {
1359 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeCommitSoftwareResponse)
1360 if msgLayer == nil {
1361 logger.Errorw(ctx, "Omci Msg layer could not be detected for CommitResponse",
1362 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001363 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001364 return
1365 }
1366 msgObj, msgOk := msgLayer.(*omci.CommitSoftwareResponse)
1367 if !msgOk {
1368 logger.Errorw(ctx, "Omci Msg layer could not be assigned for CommitResponse",
1369 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001370 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001371 return
1372 }
mpagenkobf67a092021-03-17 09:52:28 +00001373 if msgObj.Result != me.Success {
mpagenko15ff4a52021-03-02 10:09:20 +00001374 logger.Errorw(ctx, "OnuUpgradeFsm SwImage CommitResponse result error - later: drive FSM to abort state ?",
1375 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
mpagenko45586762021-10-01 08:30:22 +00001376 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001377 return
mpagenkobf67a092021-03-17 09:52:28 +00001378 }
mpagenkoaa3afe92021-05-21 16:20:58 +00001379 oFsm.mutexUpgradeParams.RLock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001380 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
mpagenkoaa3afe92021-05-21 16:20:58 +00001381 oFsm.mutexUpgradeParams.RUnlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001382 logger.Debugw(ctx, "OnuUpgradeFsm Expected SwImage CommitResponse received", log.Fields{"device-id": oFsm.deviceID})
1383 //verifying committed image
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001384 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvCheckCommitted)
mpagenko15ff4a52021-03-02 10:09:20 +00001385 return
1386 }
mpagenkoaa3afe92021-05-21 16:20:58 +00001387 oFsm.mutexUpgradeParams.RUnlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001388 logger.Errorw(ctx, "OnuUpgradeFsm SwImage CommitResponse wrong ME instance: abort",
1389 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
mpagenko45586762021-10-01 08:30:22 +00001390 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001391 return
1392 } //CommitSoftwareResponseType
1393 case omci.GetResponseType:
1394 {
mpagenko45586762021-10-01 08:30:22 +00001395 oFsm.handleRxSwGetResponse(ctx, msg)
mpagenko15ff4a52021-03-02 10:09:20 +00001396 return
1397 } //GetResponseType
mpagenko80622a52021-02-09 16:53:23 +00001398 default:
1399 {
1400 logger.Errorw(ctx, "Rx OMCI unhandled MsgType",
1401 log.Fields{"omciMsgType": msg.OmciMsg.MessageType, "device-id": oFsm.deviceID})
1402 return
1403 }
1404 }
1405}
1406
mpagenko45586762021-10-01 08:30:22 +00001407func (oFsm *OnuUpgradeFsm) handleRxStartSwDownloadResponse(ctx context.Context, msg cmn.OmciMessage) {
1408 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeStartSoftwareDownloadResponse)
1409 if msgLayer == nil {
1410 logger.Errorw(ctx, "Omci Msg layer could not be detected for StartSwDlResponse",
1411 log.Fields{"device-id": oFsm.deviceID})
1412 oFsm.abortOnOmciError(ctx, false)
1413 return
1414 }
1415 msgObj, msgOk := msgLayer.(*omci.StartSoftwareDownloadResponse)
1416 if !msgOk {
1417 logger.Errorw(ctx, "Omci Msg layer could not be assigned for StartSwDlResponse",
1418 log.Fields{"device-id": oFsm.deviceID})
1419 oFsm.abortOnOmciError(ctx, false)
1420 return
1421 }
1422 logger.Debugw(ctx, "OnuUpgradeFsm StartSwDlResponse data", log.Fields{
1423 "device-id": oFsm.deviceID, "data-fields": msgObj})
1424 if msgObj.Result != me.Success {
1425 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse result error - later: drive FSM to abort state ?",
1426 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1427 oFsm.abortOnOmciError(ctx, false)
1428 return
1429 }
1430
1431 oFsm.mutexUpgradeParams.Lock()
1432 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1433 logger.Debugw(ctx, "Expected StartSwDlResponse received", log.Fields{"device-id": oFsm.deviceID})
1434 if msgObj.WindowSize != oFsm.omciDownloadWindowSizeLimit {
1435 // also response WindowSize = 0 is a valid number for used Window size 1
1436 logger.Debugw(ctx, "different StartSwDlResponse window size requested by ONU", log.Fields{
1437 "acceptedOnuWindowSizeLimit": msgObj.WindowSize, "device-id": oFsm.deviceID})
1438 oFsm.omciDownloadWindowSizeLimit = msgObj.WindowSize
1439 }
1440 oFsm.noOfWindows = oFsm.noOfSections / uint32(oFsm.omciDownloadWindowSizeLimit+1)
1441 if oFsm.noOfSections%uint32(oFsm.omciDownloadWindowSizeLimit+1) > 0 {
1442 oFsm.noOfWindows++
1443 }
1444 logger.Debugw(ctx, "OnuUpgradeFsm will use", log.Fields{
1445 "windows": oFsm.noOfWindows, "sections": oFsm.noOfSections,
1446 "at WindowSizeLimit": oFsm.omciDownloadWindowSizeLimit})
1447 oFsm.nextDownloadSectionsAbsolute = 0
1448 oFsm.nextDownloadSectionsWindow = 0
1449 oFsm.nextDownloadWindow = 0
1450
1451 oFsm.mutexUpgradeParams.Unlock()
1452 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvRxStartSwDownload)
1453 return
1454 }
1455 oFsm.mutexUpgradeParams.Unlock()
1456 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse wrong ME instance: try again (later)?",
1457 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1458 oFsm.abortOnOmciError(ctx, false)
1459} //handleRxStartSwDownloadResponse
1460
1461func (oFsm *OnuUpgradeFsm) handleRxSwSectionResponse(ctx context.Context, msg cmn.OmciMessage) {
mpagenkoa2b288f2021-10-21 11:25:27 +00001462 if oFsm.PAdaptFsm == nil {
1463 logger.Infow(ctx, "DlSectionResponse received - but FSM not really valid anymore", log.Fields{
1464 "device-id": oFsm.deviceID})
1465 return
1466 }
1467 if !oFsm.PAdaptFsm.PFsm.Is(UpgradeStVerifyWindow) {
1468 //all the processing here is only relevant if the FSM is in state upgradeStVerifyWindow
1469 // otherwise this response can be ignored (may stem from a long-processing window send activity,
1470 // which is not anymore relevant based on intermediate (cancel) state transitions)
1471 logger.Infow(ctx, "DlSectionResponse received - but ignored", log.Fields{
1472 "device-id": oFsm.deviceID, "fsm-state": oFsm.PAdaptFsm.PFsm.Current()})
1473 return
1474 }
mpagenko45586762021-10-01 08:30:22 +00001475 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeDownloadSectionResponse)
1476 if msgLayer == nil {
1477 logger.Errorw(ctx, "Omci Msg layer could not be detected for DlSectionResponse",
1478 log.Fields{"device-id": oFsm.deviceID, "omci-message": msg.OmciMsg})
1479 oFsm.abortOnOmciError(ctx, false)
1480 return
1481 }
1482 msgObj, msgOk := msgLayer.(*omci.DownloadSectionResponse)
1483 if !msgOk {
1484 logger.Errorw(ctx, "Omci Msg layer could not be assigned for DlSectionResponse",
1485 log.Fields{"device-id": oFsm.deviceID})
1486 oFsm.abortOnOmciError(ctx, false)
1487 return
1488 }
1489 logger.Debugw(ctx, "OnuUpgradeFsm DlSectionResponse Data", log.Fields{
1490 "device-id": oFsm.deviceID, "data-fields": msgObj})
1491 if msgObj.Result != me.Success {
1492 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse result error - later: repeat window once?", //TODO!!!
1493 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1494 oFsm.abortOnOmciError(ctx, false)
1495 return
1496 }
1497 oFsm.mutexUpgradeParams.Lock()
1498 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1499 sectionNumber := msgObj.SectionNumber
1500 logger.Infow(ctx, "DlSectionResponse received", log.Fields{
1501 "window section-number": sectionNumber, "window": oFsm.nextDownloadWindow, "device-id": oFsm.deviceID})
1502
1503 oFsm.nextDownloadWindow++
1504 if oFsm.nextDownloadWindow >= oFsm.noOfWindows {
1505 if sectionNumber != oFsm.omciDownloadWindowSizeLast {
1506 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse section error last window - later: repeat window once?", //TODO!!!
1507 log.Fields{"device-id": oFsm.deviceID, "actual section": sectionNumber,
1508 "expected section": oFsm.omciDownloadWindowSizeLast})
1509 oFsm.mutexUpgradeParams.Unlock()
1510 oFsm.abortOnOmciError(ctx, false)
1511 return
1512 }
1513 oFsm.delayEndSwDl = true //ensure a delay for the EndSwDl message
1514 //CRC computation for all data bytes of the file
1515 imageCRC := crc32a.Checksum(oFsm.imageBuffer[:int(oFsm.origImageLength)]) //store internal for multiple usage
1516 //revert the retrieved CRC Byte Order (seems not to deliver NetworkByteOrder)
1517 var byteSlice []byte = make([]byte, 4)
1518 binary.LittleEndian.PutUint32(byteSlice, uint32(imageCRC))
1519 oFsm.imageCRC = binary.BigEndian.Uint32(byteSlice)
1520 oFsm.mutexUpgradeParams.Unlock()
1521 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvEndSwDownload)
1522 return
1523 }
1524 if sectionNumber != oFsm.omciDownloadWindowSizeLimit {
1525 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse section error - later: repeat window once?", //TODO!!!
1526 log.Fields{"device-id": oFsm.deviceID, "actual-section": sectionNumber,
1527 "expected section": oFsm.omciDownloadWindowSizeLimit})
1528 oFsm.mutexUpgradeParams.Unlock()
1529 oFsm.abortOnOmciError(ctx, false)
1530 return
1531 }
1532 oFsm.nextDownloadSectionsWindow = 0
1533 oFsm.mutexUpgradeParams.Unlock()
1534 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvContinueNextWindow)
1535 return
1536 }
1537 oFsm.mutexUpgradeParams.Unlock()
1538 logger.Errorw(ctx, "OnuUpgradeFsm Omci StartSwDlResponse wrong ME instance: try again (later)?",
1539 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1540 oFsm.abortOnOmciError(ctx, false)
1541} //handleRxSwSectionResponse
1542
1543func (oFsm *OnuUpgradeFsm) handleRxEndSwDownloadResponse(ctx context.Context, msg cmn.OmciMessage) {
1544 inAbortingState := oFsm.PAdaptFsm.PFsm.Is(UpgradeStAbortingDL)
1545
1546 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeEndSoftwareDownloadResponse)
1547 if msgLayer == nil {
1548 logger.Errorw(ctx, "Omci Msg layer could not be detected for EndSwDlResponse",
1549 log.Fields{"device-id": oFsm.deviceID})
1550 if !inAbortingState {
1551 oFsm.abortOnOmciError(ctx, false)
1552 } //else using error log and wait for another response or 'aborting' state timeout
1553 return
1554 }
1555 msgObj, msgOk := msgLayer.(*omci.EndSoftwareDownloadResponse)
1556 if !msgOk {
1557 logger.Errorw(ctx, "Omci Msg layer could not be assigned for EndSwDlResponse",
1558 log.Fields{"device-id": oFsm.deviceID})
1559 if !inAbortingState {
1560 oFsm.abortOnOmciError(ctx, false)
1561 } //else using error log and wait for another response or 'aborting' state timeout
1562 return
1563 }
1564 logger.Debugw(ctx, "OnuUpgradeFsm EndSwDlResponse data", log.Fields{
1565 "device-id": oFsm.deviceID, "data-fields": msgObj})
1566 if msgObj.Result != me.Success {
1567 if msgObj.Result == me.DeviceBusy {
1568 //ONU indicates it is still processing the image - let the FSM just wait and then repeat the request
1569 logger.Debugw(ctx, "OnuUpgradeFsm EndSwDlResponse busy: waiting before sending new request", log.Fields{
1570 "device-id": oFsm.deviceID})
1571 if inAbortingState {
1572 //if the EndSwDl was requested from state AbortingDL then use channel to indicate ONU busy/repeat indication
1573 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseBusy //repeat abort request
1574 }
mpagenko45586762021-10-01 08:30:22 +00001575 return
1576 }
mpagenko45586762021-10-01 08:30:22 +00001577 if inAbortingState {
1578 //if the EndSwDl was requested from state AbortingDL and response is error indication
mpagenkoa2b288f2021-10-21 11:25:27 +00001579 // that would be quite strange ONU behavior, no resolution from OnuAdapter, just let the FSM go on to disabled
1580 logger.Errorw(ctx, "OnuUpgradeFsm EndSwDlResponse result error - aborting anyway",
1581 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1582 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseAbort //error indication to stop waiting on EndDownloadResponse(abort)
1583 return
1584 }
1585 //else: ONU rejects the previous download, complete upgrade is immediately aborted with error
1586 logger.Errorw(ctx, "OnuUpgradeFsm EndSwDlResponse result error - abort upgrade",
1587 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1588 oFsm.mutexUpgradeParams.Lock()
1589 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
1590 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1591 oFsm.mutexUpgradeParams.Unlock()
1592 select {
1593 case oFsm.chOnuDlReady <- false:
1594 default:
1595 }
1596 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko45586762021-10-01 08:30:22 +00001597 return
1598 }
1599 oFsm.mutexUpgradeParams.Lock()
1600 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1601 logger.Debugw(ctx, "Expected EndSwDlResponse received", log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001602 if inAbortingState {
1603 oFsm.mutexUpgradeParams.Unlock()
1604 //if the EndSwDl was requested from state AbortingDL then use channel to indicate abort acceptance
1605 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseSuccess //success
1606 return
1607 }
1608 if !oFsm.useAPIVersion43 {
1609 //in the older API version the image version check was not possible
1610 // - assume new loaded image as valid-inactive immediately
1611 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_SUCCEEDED
1612 oFsm.volthaImageState = voltha.ImageState_IMAGE_INACTIVE
1613 oFsm.mutexUpgradeParams.Unlock()
mpagenkoa2b288f2021-10-21 11:25:27 +00001614 //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 +00001615 select {
mpagenko45586762021-10-01 08:30:22 +00001616 case oFsm.chOnuDlReady <- true:
1617 default:
1618 }
1619 } else {
1620 oFsm.mutexUpgradeParams.Unlock()
1621 }
1622 //use asynchronous channel sending to let the FSM proceed
1623 select {
1624 case oFsm.chReceiveExpectedResponse <- true:
1625 default:
1626 }
1627 return
1628 }
1629 oFsm.mutexUpgradeParams.Unlock()
1630 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse wrong ME instance: ignoring",
1631 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1632 // no state abort in case of unexpected ImageId, just keep waiting for the correct one
1633} //handleRxEndSwDownloadResponse
1634
1635func (oFsm *OnuUpgradeFsm) handleRxSwGetResponse(ctx context.Context, msg cmn.OmciMessage) {
1636 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeGetResponse)
1637 if msgLayer == nil {
1638 logger.Errorw(ctx, "Omci Msg layer could not be detected for SwImage GetResponse",
1639 log.Fields{"device-id": oFsm.deviceID})
1640 oFsm.abortOnOmciError(ctx, false)
1641 return
1642 }
1643 msgObj, msgOk := msgLayer.(*omci.GetResponse)
1644 if !msgOk {
1645 logger.Errorw(ctx, "Omci Msg layer could not be assigned for SwImage GetResponse",
1646 log.Fields{"device-id": oFsm.deviceID})
1647 oFsm.abortOnOmciError(ctx, false)
1648 return
1649 }
Akash Sonif80a6e62024-12-11 10:48:53 +05301650 if msgObj.EntityClass != me.SoftwareImageClassID {
1651 logger.Errorw(ctx, "Class id mismatch for software image upgrade",
1652 log.Fields{"device-id": oFsm.deviceID, "received ClassId": msgObj.EntityClass, "expected ClassId": me.SoftwareImageClassID})
1653 oFsm.abortOnOmciError(ctx, false)
1654 return
1655 }
mpagenko45586762021-10-01 08:30:22 +00001656 logger.Debugw(ctx, "OnuUpgradeFsm SwImage GetResponse data", log.Fields{
1657 "device-id": oFsm.deviceID, "data-fields": msgObj})
1658 if msgObj.EntityClass == oFsm.pLastTxMeInstance.GetClassID() &&
1659 msgObj.EntityInstance == oFsm.pLastTxMeInstance.GetEntityID() {
1660 if msgObj.Result != me.Success {
1661 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse result error - later: drive FSM to abort state ?",
1662 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1663 oFsm.abortOnOmciError(ctx, false)
1664 return
1665 }
1666 } else {
1667 logger.Warnw(ctx, "OnuUpgradeFsm SwImage unexpected Entity GetResponse data - ignore",
1668 log.Fields{"device-id": oFsm.deviceID})
1669 return
1670 }
mpagenko45586762021-10-01 08:30:22 +00001671 meAttributes := msgObj.Attributes
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +00001672 var imageVersion string
1673 var imageIsCommitted, imageIsActive uint8
mpagenko45586762021-10-01 08:30:22 +00001674
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +00001675 if softwareImageIsCommitted, ok := meAttributes[me.SoftwareImage_IsCommitted]; ok {
1676 if softwareImageIsActiveimage, ok := meAttributes[me.SoftwareImage_IsActive]; ok {
1677 if softwareImageVersion, ok := meAttributes[me.SoftwareImage_Version]; ok {
1678 imageIsCommitted = softwareImageIsCommitted.(uint8)
1679 imageIsActive = softwareImageIsActiveimage.(uint8)
1680 imageVersion = cmn.TrimStringFromMeOctet(softwareImageVersion)
1681 logger.Debugw(ctx, "OnuUpgradeFsm - GetResponse Data for SoftwareImage",
1682 log.Fields{"device-id": oFsm.deviceID, "entityID": msgObj.EntityInstance,
1683 "version": imageVersion, "isActive": imageIsActive, "isCommitted": imageIsCommitted})
1684 } else {
1685 logger.Errorw(ctx,
1686 "OnuUpgradeFsm - Not all mandatory attributes present in in SoftwareImage instance - handling stopped!",
1687 log.Fields{"device-id": oFsm.deviceID})
1688 oFsm.abortOnOmciError(ctx, false)
1689 return
1690 }
1691 }
1692 }
mpagenko45586762021-10-01 08:30:22 +00001693 if oFsm.PAdaptFsm.PFsm.Current() == UpgradeStCheckImageName {
1694 //image name check after EndSwDownload, this state (and block) can only be taken if APIVersion43 is used
1695 oFsm.verifyOnuSwStatusAfterDownload(ctx, msgObj.EntityInstance, imageVersion, imageIsActive, imageIsCommitted)
1696 return
1697 }
1698
1699 //assumed only relevant state here is upgradeStCheckCommitted
1700 oFsm.mutexUpgradeParams.Lock()
1701 oFsm.conditionalCancelRequested = false //getting here any set (conditional) cancelRequest is not relevant anymore
1702 if msgObj.EntityInstance == oFsm.InactiveImageMeID && imageIsActive == cmn.SwIsActive {
1703 //a check on the delivered image version is not done, the ONU delivered version might be different from what might have been
1704 // indicated in the download image version string (version must be part of the image content itself)
1705 // so checking that might be quite unreliable
1706 //but with new API this was changed, assumption is that omci image version is known at download request and exactly that is used
1707 // in all the API references, so it can and should be checked here now
1708 if oFsm.useAPIVersion43 {
1709 if imageVersion != oFsm.imageVersion {
1710 //new active version indicated on OMCI from ONU is not the expected version
1711 logger.Errorw(ctx, "OnuUpgradeFsm image-version not matching the requested upgrade",
1712 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance,
1713 "onu-version": imageVersion, "expected-version": oFsm.imageVersion})
1714 // TODO!!!: error treatment?
1715 //TODO!!!: possibly send event information for aborted upgrade (aborted by wrong version)?
1716 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE //something like 'UNEXPECTED_VERSION' would be better - proto def
1717 oFsm.mutexUpgradeParams.Unlock()
1718 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1719 return
1720 }
1721 logger.Debugw(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU",
1722 log.Fields{"device-id": oFsm.deviceID})
1723 }
1724 if imageIsCommitted == cmn.SwIsCommitted {
1725 oFsm.upgradePhase = cUpgradeCommitted
1726 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTED
1727 //store the new commit flag to onuSwImageIndications (to keep them in sync)
1728 oFsm.pDevEntry.ModifySwImageActiveCommit(ctx, imageIsCommitted)
1729 logger.Infow(ctx, "requested SW image committed, releasing OnuUpgrade", log.Fields{"device-id": oFsm.deviceID})
1730 //deviceProcStatusUpdate not used anymore,
1731 // replaced by transferring the last (more) upgrade state information within removeOnuUpgradeFsm
1732 oFsm.mutexUpgradeParams.Unlock()
1733 //releasing the upgrade FSM on success
1734 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1735 return
1736 }
1737 //if not committed, abort upgrade as failed. There is no implementation here that would trigger this test again
1738 }
1739 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1740 oFsm.mutexUpgradeParams.Unlock()
1741 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse indications not matching requested upgrade",
1742 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1743 // TODO!!!: error treatment?
1744 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
1745 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1746} //handleRxSwGetResponse
1747
1748func (oFsm *OnuUpgradeFsm) verifyOnuSwStatusAfterDownload(ctx context.Context, aInstanceID uint16,
1749 aImageVersion string, aImageIsActive uint8, aImageIsCommitted uint8) {
1750 oFsm.mutexUpgradeParams.Lock()
1751 if aInstanceID == oFsm.InactiveImageMeID && aImageIsActive == cmn.SwIsInactive &&
1752 aImageIsCommitted == cmn.SwIsUncommitted {
1753 if aImageVersion != oFsm.imageVersion {
1754 //new stored inactive version indicated on OMCI from ONU is not the expected version
1755 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse version indication not matching requested upgrade",
1756 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": aInstanceID,
1757 "onu-version": aImageVersion, "expected-version": oFsm.imageVersion})
1758 //download state is set when entering the reset state
1759 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE //something like 'UNEXPECTED_VERSION' would be better - proto def
1760 oFsm.mutexUpgradeParams.Unlock()
1761 //stop the running ONU download timer
1762 //use non-blocking channel (to be independent from receiver state)
1763 select {
1764 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
1765 case oFsm.chOnuDlReady <- false:
1766 default:
1767 }
1768 // TODO!!!: error treatment?
1769 //TODO!!!: possibly send event information for aborted upgrade (aborted by wrong version)?
1770 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1771 return
1772 }
1773 //with APIVersion43 this is the point to consider the newly loaded image as valid (and inactive)
1774 oFsm.upgradePhase = cUpgradeDownloaded
1775 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_SUCCEEDED
1776 oFsm.volthaImageState = voltha.ImageState_IMAGE_INACTIVE
1777 //store the new inactive version to onuSwImageIndications (to keep them in sync)
1778 oFsm.pDevEntry.ModifySwImageInactiveVersion(ctx, oFsm.imageVersion)
1779 //proceed within upgrade FSM
1780 if oFsm.activateImage {
1781 //immediate activation requested
1782 oFsm.mutexUpgradeParams.Unlock()
1783 logger.Debugw(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU, continue with activation",
1784 log.Fields{"device-id": oFsm.deviceID})
1785 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvRequestActivate)
1786 } else {
1787 //have to wait on explicit activation request
1788 oFsm.mutexUpgradeParams.Unlock()
1789 logger.Infow(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU, wait for activate request",
1790 log.Fields{"device-id": oFsm.deviceID})
1791 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvWaitForActivate)
1792 }
1793 //use non-blocking channel (to be independent from receiver state)
1794 select {
1795 //use non-blocking channel to indicate that the download to ONU was successful
1796 case oFsm.chOnuDlReady <- true:
1797 default:
1798 }
1799 return
1800 }
1801 //not the expected image/image state
1802 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1803 oFsm.mutexUpgradeParams.Unlock()
1804 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse indications not matching requested upgrade",
1805 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": aInstanceID})
1806 // TODO!!!: error treatment?
1807 //TODO!!!: possibly send event information for aborted upgrade (aborted by ONU state indication)?
1808 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1809} //verifyOnuSwStatusAfterDownload
1810
Joey Armstrong89c812c2024-01-12 19:00:20 -05001811// abortOnOmciError aborts the upgrade processing with evAbort
1812//
1813// asynchronous/synchronous based on parameter aAsync
mpagenko45586762021-10-01 08:30:22 +00001814func (oFsm *OnuUpgradeFsm) abortOnOmciError(ctx context.Context, aAsync bool) {
mpagenko38662d02021-08-11 09:45:19 +00001815 oFsm.mutexUpgradeParams.Lock()
1816 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
mpagenko38662d02021-08-11 09:45:19 +00001817 oFsm.volthaDownloadReason = voltha.ImageState_OMCI_TRANSFER_ERROR
mpagenko38662d02021-08-11 09:45:19 +00001818 oFsm.mutexUpgradeParams.Unlock()
1819 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001820 if oFsm.PAdaptFsm != nil {
mpagenko38662d02021-08-11 09:45:19 +00001821 var err error
1822 if aAsync { //asynchronous call requested to ensure state transition
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001823 go func(a_pAFsm *cmn.AdapterFsm) {
1824 if a_pAFsm.PFsm != nil {
1825 err = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko38662d02021-08-11 09:45:19 +00001826 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001827 }(oFsm.PAdaptFsm)
mpagenko38662d02021-08-11 09:45:19 +00001828 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001829 if oFsm.PAdaptFsm.PFsm != nil {
1830 err = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko38662d02021-08-11 09:45:19 +00001831 }
1832 }
1833 if err != nil {
1834 logger.Warnw(ctx, "onu upgrade fsm could not abort on omci error", log.Fields{
1835 "device-id": oFsm.deviceID, "error": err})
1836 }
1837 }
1838}
1839
Joey Armstrong89c812c2024-01-12 19:00:20 -05001840// waitOnDownloadToAdapterReady state can only be reached with useAPIVersion43 (usage of pFileManager)
1841//
1842// precondition: mutexIsAwaitingAdapterDlResponse is lockek on call
mpagenko38662d02021-08-11 09:45:19 +00001843func (oFsm *OnuUpgradeFsm) waitOnDownloadToAdapterReady(ctx context.Context, aSyncChannel chan<- struct{},
1844 aWaitChannel chan bool) {
mpagenkoc26d4c02021-05-06 14:27:57 +00001845 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
mpagenko38662d02021-08-11 09:45:19 +00001846 downloadToAdapterTimeout := oFsm.pFileManager.GetDownloadTimeout(ctx)
mpagenkoc26d4c02021-05-06 14:27:57 +00001847 oFsm.isWaitingForAdapterDlResponse = true
1848 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenko38662d02021-08-11 09:45:19 +00001849 aSyncChannel <- struct{}{}
mpagenko80622a52021-02-09 16:53:23 +00001850 select {
1851 // maybe be also some outside cancel (but no context modeled for the moment ...)
1852 // case <-ctx.Done():
mpagenkoc26d4c02021-05-06 14:27:57 +00001853 // logger.Infow("OnuUpgradeFsm-waitOnDownloadToAdapterReady canceled", log.Fields{"for device-id": oFsm.deviceID})
1854 case <-time.After(downloadToAdapterTimeout): //10s should be enough for downloading some image to the adapter
1855 logger.Warnw(ctx, "OnuUpgradeFsm Waiting-adapter-download timeout", log.Fields{
1856 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier, "timeout": downloadToAdapterTimeout})
1857 oFsm.pFileManager.RemoveReadyRequest(ctx, oFsm.imageIdentifier, aWaitChannel)
mpagenko39b703e2021-08-25 13:38:40 +00001858 //running into timeout here may still have the download to adapter active -> abort
1859 oFsm.pFileManager.CancelDownload(ctx, oFsm.imageIdentifier)
mpagenkoc26d4c02021-05-06 14:27:57 +00001860 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1861 oFsm.isWaitingForAdapterDlResponse = false
1862 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenko39b703e2021-08-25 13:38:40 +00001863 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +00001864 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
mpagenko39b703e2021-08-25 13:38:40 +00001865 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'DOWNLOAD_TO_ADAPTER_TIMEOUT' would be better (proto)
mpagenko39b703e2021-08-25 13:38:40 +00001866 oFsm.mutexUpgradeParams.Unlock()
1867 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001868 if oFsm.PAdaptFsm != nil && oFsm.PAdaptFsm.PFsm != nil {
1869 err := oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko39b703e2021-08-25 13:38:40 +00001870 if err != nil {
1871 logger.Warnw(ctx, "onu upgrade fsm could not abort on omci error", log.Fields{
1872 "device-id": oFsm.deviceID, "error": err})
1873 }
1874 }
mpagenkoc26d4c02021-05-06 14:27:57 +00001875 return
1876
1877 case success := <-aWaitChannel:
1878 if success {
1879 logger.Debugw(ctx, "OnuUpgradeFsm image-downloaded received", log.Fields{"device-id": oFsm.deviceID})
1880 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1881 oFsm.isWaitingForAdapterDlResponse = false
1882 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
1883 //let the upgrade process proceed
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001884 pUpgradeFsm := oFsm.PAdaptFsm
mpagenkoc26d4c02021-05-06 14:27:57 +00001885 if pUpgradeFsm != nil {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001886 _ = pUpgradeFsm.PFsm.Event(UpgradeEvPrepareSwDownload)
mpagenkoc26d4c02021-05-06 14:27:57 +00001887 } else {
1888 logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
1889 }
1890 return
1891 }
mpagenko39b703e2021-08-25 13:38:40 +00001892 // waiting was aborted (assumed here to be caused by
1893 // error detection or cancel at download after upgrade FSM reset/abort with according image states set there)
mpagenkoc26d4c02021-05-06 14:27:57 +00001894 logger.Debugw(ctx, "OnuUpgradeFsm Waiting-adapter-download aborted", log.Fields{"device-id": oFsm.deviceID})
1895 oFsm.pFileManager.RemoveReadyRequest(ctx, oFsm.imageIdentifier, aWaitChannel)
1896 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1897 oFsm.isWaitingForAdapterDlResponse = false
1898 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenkoc26d4c02021-05-06 14:27:57 +00001899 return
mpagenko80622a52021-02-09 16:53:23 +00001900 }
1901}
mpagenkoc26d4c02021-05-06 14:27:57 +00001902
Joey Armstrong89c812c2024-01-12 19:00:20 -05001903// waitOnDownloadToOnuReady state can only be reached with useAPIVersion43 (usage of pFileManager)
mpagenkoc26d4c02021-05-06 14:27:57 +00001904func (oFsm *OnuUpgradeFsm) waitOnDownloadToOnuReady(ctx context.Context, aWaitChannel chan bool) {
1905 downloadToOnuTimeout := time.Duration(1+(oFsm.imageLength/0x400000)) * oFsm.downloadToOnuTimeout4MB
1906 logger.Debugw(ctx, "OnuUpgradeFsm start download-to-ONU timer", log.Fields{"device-id": oFsm.deviceID,
1907 "duration": downloadToOnuTimeout})
mpagenkoc26d4c02021-05-06 14:27:57 +00001908 select {
1909 // maybe be also some outside cancel (but no context modeled for the moment ...)
1910 // case <-ctx.Done():
1911 // logger.Infow("OnuUpgradeFsm-waitOnDownloadToOnuReady canceled", log.Fields{"for device-id": oFsm.deviceID})
1912 case <-time.After(downloadToOnuTimeout): //using an image-size depending timout (in minutes)
1913 logger.Warnw(ctx, "OnuUpgradeFsm Waiting-ONU-download timeout", log.Fields{
1914 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier, "timeout": downloadToOnuTimeout})
mpagenkoc26d4c02021-05-06 14:27:57 +00001915 //the upgrade process has to be aborted
mpagenko45586762021-10-01 08:30:22 +00001916 oFsm.abortOnOmciError(ctx, false)
mpagenkoc26d4c02021-05-06 14:27:57 +00001917 return
1918
1919 case success := <-aWaitChannel:
1920 if success {
1921 logger.Debugw(ctx, "OnuUpgradeFsm image-downloaded on ONU received", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001922 //all fine, let the FSM proceed like defined from the sender of this event
1923 return
1924 }
1925 // waiting was aborted (assumed here to be caused by
mpagenko39b703e2021-08-25 13:38:40 +00001926 // error detection or cancel at download after upgrade FSM reset/abort with according image states set there)
mpagenkoc26d4c02021-05-06 14:27:57 +00001927 logger.Debugw(ctx, "OnuUpgradeFsm Waiting-ONU-download aborted", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001928 return
1929 }
1930}
mpagenko45586762021-10-01 08:30:22 +00001931
Joey Armstrong89c812c2024-01-12 19:00:20 -05001932// waitOnAbortEndSwDlResponse waits for either abort/success or timeout of EndSwDownload (for abortion)
mpagenkoa2b288f2021-10-21 11:25:27 +00001933func (oFsm *OnuUpgradeFsm) waitOnAbortEndSwDlResponse(ctx context.Context) {
1934 logger.Debugw(ctx, "OnuUpgradeFsm start wait for EndSwDl response (abort)", log.Fields{"device-id": oFsm.deviceID})
1935 select {
1936 case <-time.After(oFsm.pOmciCC.GetMaxOmciTimeoutWithRetries() * time.Second):
1937 logger.Warnw(ctx, "OnuUpgradeFsm aborting download: timeout - no response received", log.Fields{"device-id": oFsm.deviceID})
1938 pUpgradeFsm := oFsm.PAdaptFsm
1939 if pUpgradeFsm != nil {
1940 _ = pUpgradeFsm.PFsm.Event(UpgradeEvRestart)
1941 } else {
1942 logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
1943 }
1944 return
1945 case response := <-oFsm.chReceiveAbortEndSwDlResponse:
1946 logger.Debugw(ctx, "OnuUpgradeFsm aborting download: response received",
1947 log.Fields{"device-id": oFsm.deviceID, "response": response})
1948 pUpgradeFsm := oFsm.PAdaptFsm
1949 if pUpgradeFsm != nil {
1950 if oFsm.abortingDlEvaluateResponse(ctx, pUpgradeFsm, response) {
1951 return //event sent from function already
1952 }
1953 _ = pUpgradeFsm.PFsm.Event(UpgradeEvRestart)
1954 } else {
1955 logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
1956 }
1957 return
1958 } //select
1959}
1960
Joey Armstrong89c812c2024-01-12 19:00:20 -05001961// stateUpdateOnReset writes the download and/or image state on entering the reset state according to FSM internal indications
mpagenko45586762021-10-01 08:30:22 +00001962func (oFsm *OnuUpgradeFsm) stateUpdateOnReset(ctx context.Context) {
1963 oFsm.mutexUpgradeParams.Lock()
1964 defer oFsm.mutexUpgradeParams.Unlock()
1965 if !oFsm.conditionalCancelRequested {
1966 switch oFsm.upgradePhase {
1967 case cUpgradeUndefined, cUpgradeDownloading: //coming from downloading
1968 //make sure the download state is only changed in case the device has still been downloading
1969 if oFsm.volthaDownloadReason == voltha.ImageState_CANCELLED_ON_REQUEST {
1970 // indication for termination on request
1971 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_CANCELLED
1972 } else if oFsm.volthaDownloadReason != voltha.ImageState_NO_ERROR {
1973 // indication for termination on failure
1974 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_FAILED
1975 }
1976 //reset the image state from Downloading in this case
Balaji Seenivasana52fb0c2024-12-18 07:50:42 +05301977 oFsm.volthaImageState = voltha.ImageState_IMAGE_DOWNLOADING_ABORTED
mpagenko45586762021-10-01 08:30:22 +00001978 //in all other upgrade phases the last set download state remains valid
1979 case cUpgradeActivating:
1980 //reset the image state from Activating in this case
1981 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATION_ABORTED
1982 case cUpgradeCommitting: // indication for request to abort waiting for response
1983 //reset the image state from Activating in this case
1984 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMIT_ABORTED
1985 //default: in all other upgrade phases keep the last set imageState
1986 } //switch
1987 } else {
1988 //when reaching reset state with conditional cancel that can only result from ONU related problems
1989 // (mostly ONU down indication) - derived from resetFsms call
1990 // and it can only be related to the downloading-to-ONU phase (no need to check that additionally)
1991 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_FAILED
1992 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1993 //reset the image state from Downloading in this case
Balaji Seenivasana52fb0c2024-12-18 07:50:42 +05301994 oFsm.volthaImageState = voltha.ImageState_IMAGE_DOWNLOADING_ABORTED
mpagenko45586762021-10-01 08:30:22 +00001995 }
1996}
Holger Hildebrandte7cc6092022-02-01 11:37:03 +00001997
1998// PrepareForGarbageCollection - remove references to prepare for garbage collection
1999func (oFsm *OnuUpgradeFsm) PrepareForGarbageCollection(ctx context.Context, aDeviceID string) {
2000 logger.Debugw(ctx, "prepare for garbage collection", log.Fields{"device-id": aDeviceID})
2001 oFsm.pDeviceHandler = nil
2002 oFsm.pDownloadManager = nil
2003 oFsm.pFileManager = nil
2004 oFsm.pDevEntry = nil
2005 oFsm.pOmciCC = nil
2006}