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