blob: 369890ac171db90b893711787dc4900d1793b1bf [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)
43 cOmciDownloadSectionSize = 31 //in bytes
44 cOmciDownloadWindowSizeLimit = 31 //in sections for window offset (windowSize(32)-1)
45 //cOmciDownloadWindowRetryMax = 2 // max attempts for a specific window
mpagenkoc26d4c02021-05-06 14:27:57 +000046 cOmciSectionInterleaveMilliseconds = 0 //DownloadSection interleave time in milliseconds (0 for no delay)
47 cOmciEndSwDlDelaySeconds = 1 //End Software Download delay after last section (may be also configurable?)
48 cWaitCountEndSwDl = 6 //maximum number of EndSwDl requests
49 cWaitDelayEndSwDlSeconds = 10 //duration, how long is waited before next request on EndSwDl
mpagenko80622a52021-02-09 16:53:23 +000050 //cOmciDownloadCompleteTimeout = 5400 //in s for the complete timeout (may be better scale to image size/ noOfWindows)
51)
52
mpagenko45586762021-10-01 08:30:22 +000053// tEndSwDlResponseResult - Response result from EndSwDownload as used in channel indication
54type tUpgradePhase uint8
55
56const (
57 // undefined phase
58 cUpgradeUndefined tUpgradePhase = iota
59 // downloading image
60 cUpgradeDownloading
61 // image downloaded
62 cUpgradeDownloaded
63 // activating image
64 cUpgradeActivating
65 // image activated
66 cUpgradeActivated
67 // committing image
68 cUpgradeCommitting
69 // image committed
70 cUpgradeCommitted
71)
72
73// tEndSwDlResponseResult - Response result from EndSwDownload as used in channel indication
74type tEndSwDlResponseResult uint8
75
76const (
77 // response success
78 cEndSwDlResponseSuccess tEndSwDlResponseResult = iota
79 // response busy (repeat)
80 cEndSwDlResponseBusy
81 // response error or abort waiting for response
82 cEndSwDlResponseAbort
83)
84
85// upgrade FSM related events
mpagenko80622a52021-02-09 16:53:23 +000086const (
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000087 UpgradeEvStart = "UpgradeEvStart"
88 UpgradeEvDisable = "UpgradeEvDisable"
89 UpgradeEvAdapterDownload = "UpgradeEvAdapterDownload"
90 UpgradeEvPrepareSwDownload = "UpgradeEvPrepareSwDownload"
91 UpgradeEvRxStartSwDownload = "UpgradeEvRxStartSwDownload"
92 UpgradeEvWaitWindowAck = "UpgradeEvWaitWindowAck"
93 UpgradeEvContinueNextWindow = "UpgradeEvContinueNextWindow"
94 UpgradeEvEndSwDownload = "UpgradeEvEndSwDownload"
95 UpgradeEvWaitEndDownload = "UpgradeEvWaitEndDownload"
96 UpgradeEvContinueFinalize = "UpgradeEvContinueFinalize"
97 UpgradeEvCheckImageName = "UpgradeEvCheckImageName"
98 UpgradeEvWaitForActivate = "UpgradeEvWaitForActivate"
99 UpgradeEvRequestActivate = "UpgradeEvRequestActivate"
100 UpgradeEvActivationDone = "UpgradeEvActivationDone"
101 UpgradeEvWaitForCommit = "UpgradeEvWaitForCommit"
102 UpgradeEvCommitSw = "UpgradeEvCommitSw"
103 UpgradeEvCheckCommitted = "UpgradeEvCheckCommitted"
mpagenko80622a52021-02-09 16:53:23 +0000104
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000105 //UpgradeEvTimeoutSimple = "UpgradeEvTimeoutSimple"
106 //UpgradeEvTimeoutMids = "UpgradeEvTimeoutMids"
107 UpgradeEvReset = "UpgradeEvReset"
108 UpgradeEvAbort = "UpgradeEvAbort"
109 UpgradeEvRestart = "UpgradeEvRestart"
110 UpgradeEvAbortSwDownload = "UpgradeEvAbortSwDownload"
mpagenko80622a52021-02-09 16:53:23 +0000111)
112
mpagenko45586762021-10-01 08:30:22 +0000113// upgrade FSM related states
mpagenko80622a52021-02-09 16:53:23 +0000114const (
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000115 UpgradeStDisabled = "UpgradeStDisabled"
116 UpgradeStStarting = "UpgradeStStarting"
117 UpgradeStWaitingAdapterDL = "UpgradeStWaitingAdapterDL"
118 UpgradeStPreparingDL = "UpgradeStPreparingDL"
119 UpgradeStDLSection = "UpgradeStDLSection"
120 UpgradeStVerifyWindow = "UpgradeStVerifyWindow"
121 UpgradeStFinalizeDL = "UpgradeStFinalizeDL"
122 UpgradeStWaitEndDL = "UpgradeStWaitEndDL"
123 UpgradeStCheckImageName = "UpgradeStCheckImageName"
124 UpgradeStWaitForActivate = "UpgradeStWaitForActivate"
125 UpgradeStRequestingActivate = "UpgradeStRequestingActivate"
126 UpgradeStActivated = "UpgradeStActivated"
127 UpgradeStWaitForCommit = "UpgradeStWaitForCommit"
128 UpgradeStCommitSw = "UpgradeStCommitSw"
129 UpgradeStCheckCommitted = "UpgradeStCheckCommitted"
130 UpgradeStResetting = "UpgradeStResetting"
131 UpgradeStRestarting = "UpgradeStRestarting"
132 UpgradeStAbortingDL = "UpgradeStAbortingDL"
mpagenko80622a52021-02-09 16:53:23 +0000133)
134
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000135//COnuUpgradeFsmIdleState - required definition for IdleState detection for activities on OMCI
136const COnuUpgradeFsmIdleState = UpgradeStWaitForCommit
mpagenko80622a52021-02-09 16:53:23 +0000137
138//OnuUpgradeFsm defines the structure for the state machine to config the PON ANI ports of ONU UNI ports via OMCI
139type OnuUpgradeFsm struct {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000140 pDeviceHandler cmn.IdeviceHandler
141 pDownloadManager *AdapterDownloadManager
142 pFileManager *FileDownloadManager //used from R2.8 with new API version
mpagenko80622a52021-02-09 16:53:23 +0000143 deviceID string
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000144 pDevEntry cmn.IonuDeviceEntry
145 pOmciCC *cmn.OmciCC
146 pOnuDB *devdb.OnuDeviceDB
147 requestEvent cmn.OnuDeviceEvent
mpagenko80622a52021-02-09 16:53:23 +0000148 //omciMIdsResponseReceived chan bool //seperate channel needed for checking multiInstance OMCI message responses
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000149 PAdaptFsm *cmn.AdapterFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000150 pImageDsc *voltha.ImageDownload
151 imageBuffer []byte
152 origImageLength uint32 //as also limited by OMCI
153 imageCRC uint32 //as per OMCI - ITU I.363.5 crc
154 imageLength uint32 //including last bytes padding
155 omciDownloadWindowSizeLimit uint8 //windowSize-1 in sections
156 omciDownloadWindowSizeLast uint8 //number of sections in last window
157 noOfSections uint32 //uint32 range for sections should be sufficient for very long images
158 nextDownloadSectionsAbsolute uint32 //number of next section to download in overall image
159 nextDownloadSectionsWindow uint8 //number of next section to download within current window
160 noOfWindows uint32 //uint32 range for windows should be sufficient for very long images
161 nextDownloadWindow uint32 //number of next window to download
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000162 InactiveImageMeID uint16 //ME-ID of the inactive image
mpagenkoc26d4c02021-05-06 14:27:57 +0000163 downloadToOnuTimeout4MB time.Duration //timeout for downloading the image to the ONU for a 4MB image slice
164 omciSectionInterleaveDelay time.Duration //DownloadSectionInterleave delay in milliseconds
165 delayEndSwDl bool //flag to provide a delay between last section and EndSwDl
mpagenko45586762021-10-01 08:30:22 +0000166 repeatAbort bool //flag to indicate if OMCI EndSwDownload (abort) is to be repeated
mpagenkoc26d4c02021-05-06 14:27:57 +0000167 pLastTxMeInstance *me.ManagedEntity
168 waitCountEndSwDl uint8 //number, how often is waited for EndSwDl at maximum
169 waitDelayEndSwDl time.Duration //duration, how long is waited before next request on EndSwDl
170 chReceiveExpectedResponse chan bool
mpagenkoaa3afe92021-05-21 16:20:58 +0000171 useAPIVersion43 bool //flag for indication on which API version is used (and accordingly which specific methods)
172 mutexUpgradeParams sync.RWMutex //mutex to protect members for parallel function requests and omci response processing
173 imageVersion string //name of the image as used within OMCI (and on extrenal API interface)
174 imageIdentifier string //name of the image as used in the adapter
mpagenkoc26d4c02021-05-06 14:27:57 +0000175 mutexIsAwaitingAdapterDlResponse sync.RWMutex
176 chAdapterDlReady chan bool
mpagenko609a69a2021-12-02 08:12:58 +0000177 chAbortDelayEndSwDl chan struct{}
mpagenkoc26d4c02021-05-06 14:27:57 +0000178 isWaitingForAdapterDlResponse bool
mpagenkoc26d4c02021-05-06 14:27:57 +0000179 chOnuDlReady chan bool
mpagenkoc26d4c02021-05-06 14:27:57 +0000180 activateImage bool
181 commitImage bool
mpagenko38662d02021-08-11 09:45:19 +0000182 mutexAbortRequest sync.RWMutex
183 abortRequested voltha.ImageState_ImageFailureReason
184 conditionalCancelRequested bool
mpagenko45586762021-10-01 08:30:22 +0000185 upgradePhase tUpgradePhase
mpagenkoaa3afe92021-05-21 16:20:58 +0000186 volthaDownloadState voltha.ImageState_ImageDownloadState
187 volthaDownloadReason voltha.ImageState_ImageFailureReason
188 volthaImageState voltha.ImageState_ImageActivationState
Holger Hildebrandt288d9472021-09-06 10:47:53 +0000189 isEndSwDlOpen bool
mpagenko45586762021-10-01 08:30:22 +0000190 chReceiveAbortEndSwDlResponse chan tEndSwDlResponseResult
mpagenko80622a52021-02-09 16:53:23 +0000191}
192
193//NewOnuUpgradeFsm is the 'constructor' for the state machine to config the PON ANI ports
194// of ONU UNI ports via OMCI
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000195func NewOnuUpgradeFsm(ctx context.Context, apDeviceHandler cmn.IdeviceHandler,
196 apDevEntry cmn.IonuDeviceEntry, apOnuDB *devdb.OnuDeviceDB,
197 aRequestEvent cmn.OnuDeviceEvent, aName string, aCommChannel chan cmn.Message) *OnuUpgradeFsm {
mpagenko80622a52021-02-09 16:53:23 +0000198 instFsm := &OnuUpgradeFsm{
mpagenko59498c12021-03-18 14:15:15 +0000199 pDeviceHandler: apDeviceHandler,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000200 deviceID: apDeviceHandler.GetDeviceID(),
mpagenko38662d02021-08-11 09:45:19 +0000201 pDevEntry: apDevEntry,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000202 pOmciCC: apDevEntry.GetDevOmciCC(),
mpagenko59498c12021-03-18 14:15:15 +0000203 pOnuDB: apOnuDB,
204 requestEvent: aRequestEvent,
205 omciDownloadWindowSizeLimit: cOmciDownloadWindowSizeLimit,
206 omciSectionInterleaveDelay: cOmciSectionInterleaveMilliseconds,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000207 downloadToOnuTimeout4MB: apDeviceHandler.GetDlToOnuTimeout4M(),
mpagenko59498c12021-03-18 14:15:15 +0000208 waitCountEndSwDl: cWaitCountEndSwDl,
209 waitDelayEndSwDl: cWaitDelayEndSwDlSeconds,
mpagenko45586762021-10-01 08:30:22 +0000210 upgradePhase: cUpgradeUndefined,
211 volthaDownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN,
mpagenkoaa3afe92021-05-21 16:20:58 +0000212 volthaDownloadReason: voltha.ImageState_NO_ERROR,
213 volthaImageState: voltha.ImageState_IMAGE_UNKNOWN,
mpagenko38662d02021-08-11 09:45:19 +0000214 abortRequested: voltha.ImageState_NO_ERROR,
mpagenko80622a52021-02-09 16:53:23 +0000215 }
mpagenko59498c12021-03-18 14:15:15 +0000216 instFsm.chReceiveExpectedResponse = make(chan bool)
mpagenkoc26d4c02021-05-06 14:27:57 +0000217 instFsm.chAdapterDlReady = make(chan bool)
mpagenko609a69a2021-12-02 08:12:58 +0000218 instFsm.chAbortDelayEndSwDl = make(chan struct{})
mpagenkoc26d4c02021-05-06 14:27:57 +0000219 instFsm.chOnuDlReady = make(chan bool)
mpagenko45586762021-10-01 08:30:22 +0000220 instFsm.chReceiveAbortEndSwDlResponse = make(chan tEndSwDlResponseResult)
mpagenko80622a52021-02-09 16:53:23 +0000221
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000222 instFsm.PAdaptFsm = cmn.NewAdapterFsm(aName, instFsm.deviceID, aCommChannel)
223 if instFsm.PAdaptFsm == nil {
mpagenko80622a52021-02-09 16:53:23 +0000224 logger.Errorw(ctx, "OnuUpgradeFsm's AdapterFsm could not be instantiated!!", log.Fields{
225 "device-id": instFsm.deviceID})
226 return nil
227 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000228 instFsm.PAdaptFsm.PFsm = fsm.NewFSM(
229 UpgradeStDisabled,
mpagenko80622a52021-02-09 16:53:23 +0000230 fsm.Events{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000231 {Name: UpgradeEvStart, Src: []string{UpgradeStDisabled}, Dst: UpgradeStStarting},
232 {Name: UpgradeEvAdapterDownload, Src: []string{UpgradeStStarting}, Dst: UpgradeStWaitingAdapterDL},
233 {Name: UpgradeEvPrepareSwDownload, Src: []string{UpgradeStStarting, UpgradeStWaitingAdapterDL}, Dst: UpgradeStPreparingDL},
234 {Name: UpgradeEvRxStartSwDownload, Src: []string{UpgradeStPreparingDL}, Dst: UpgradeStDLSection},
235 {Name: UpgradeEvWaitWindowAck, Src: []string{UpgradeStDLSection}, Dst: UpgradeStVerifyWindow},
236 {Name: UpgradeEvContinueNextWindow, Src: []string{UpgradeStVerifyWindow}, Dst: UpgradeStDLSection},
237 {Name: UpgradeEvEndSwDownload, Src: []string{UpgradeStVerifyWindow}, Dst: UpgradeStFinalizeDL},
238 {Name: UpgradeEvWaitEndDownload, Src: []string{UpgradeStFinalizeDL}, Dst: UpgradeStWaitEndDL},
239 {Name: UpgradeEvContinueFinalize, Src: []string{UpgradeStWaitEndDL}, Dst: UpgradeStFinalizeDL},
240 //UpgradeStCheckImageName only used with useAPIVersion43
241 {Name: UpgradeEvCheckImageName, Src: []string{UpgradeStWaitEndDL}, Dst: UpgradeStCheckImageName},
242 //UpgradeEvWaitForActivate state transitions depend on useAPIVersion43
243 {Name: UpgradeEvWaitForActivate, Src: []string{UpgradeStWaitEndDL, UpgradeStCheckImageName}, Dst: UpgradeStWaitForActivate},
244 //UpgradeEvRequestActivate state transitions depend on useAPIVersion43
245 {Name: UpgradeEvRequestActivate, Src: []string{UpgradeStStarting, UpgradeStWaitEndDL, UpgradeStCheckImageName,
246 UpgradeStWaitForActivate}, Dst: UpgradeStRequestingActivate}, //allows also for direct activation (without download) [TODO!!!]
247 {Name: UpgradeEvActivationDone, Src: []string{UpgradeStRequestingActivate}, Dst: UpgradeStActivated},
248 {Name: UpgradeEvWaitForCommit, Src: []string{UpgradeStRequestingActivate}, Dst: UpgradeStWaitForCommit},
249 {Name: UpgradeEvCommitSw, Src: []string{UpgradeStStarting, UpgradeStRequestingActivate, UpgradeStWaitForCommit,
250 UpgradeStActivated}, Dst: UpgradeStCommitSw}, //allows also for direct commitment (without download) [TODO!!!]
251 {Name: UpgradeEvCheckCommitted, Src: []string{UpgradeStCommitSw}, Dst: UpgradeStCheckCommitted},
mpagenko80622a52021-02-09 16:53:23 +0000252
253 /*
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000254 {Name: UpgradeEvTimeoutSimple, Src: []string{
255 UpgradeStCreatingDot1PMapper, UpgradeStCreatingMBPCD, UpgradeStSettingTconts, UpgradeStSettingDot1PMapper}, Dst: UpgradeStStarting},
256 {Name: UpgradeEvTimeoutMids, Src: []string{
257 UpgradeStCreatingGemNCTPs, UpgradeStCreatingGemIWs, UpgradeStSettingPQs}, Dst: UpgradeStStarting},
mpagenko80622a52021-02-09 16:53:23 +0000258 */
259 // exceptional treatments
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000260 //on UpgradeEvReset: UpgradeStRequestingActivate, UpgradeStWaitForCommit and UpgradeStActivated are not reset
mpagenko1f8e8822021-06-25 14:10:21 +0000261 // (to let the FSM survive the expected OnuDown indication)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000262 {Name: UpgradeEvReset, Src: []string{UpgradeStStarting, UpgradeStWaitingAdapterDL, UpgradeStPreparingDL, UpgradeStDLSection,
263 UpgradeStVerifyWindow, UpgradeStDLSection, UpgradeStFinalizeDL, UpgradeStWaitEndDL, UpgradeStCheckImageName,
264 UpgradeStWaitForActivate,
mpagenko45586762021-10-01 08:30:22 +0000265 UpgradeStCommitSw, UpgradeStCheckCommitted, UpgradeStAbortingDL},
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000266 Dst: UpgradeStResetting},
267 {Name: UpgradeEvAbort, Src: []string{UpgradeStStarting, UpgradeStWaitingAdapterDL, UpgradeStPreparingDL, UpgradeStDLSection,
268 UpgradeStVerifyWindow, UpgradeStDLSection, UpgradeStFinalizeDL, UpgradeStWaitEndDL, UpgradeStCheckImageName,
269 UpgradeStWaitForActivate,
270 UpgradeStRequestingActivate, UpgradeStActivated, UpgradeStWaitForCommit,
271 UpgradeStCommitSw, UpgradeStCheckCommitted},
272 Dst: UpgradeStResetting},
273 {Name: UpgradeEvAbortSwDownload, Src: []string{UpgradeStResetting}, Dst: UpgradeStAbortingDL},
274 {Name: UpgradeEvRestart, Src: []string{UpgradeStResetting, UpgradeStAbortingDL}, Dst: UpgradeStRestarting},
275 {Name: UpgradeEvDisable, Src: []string{UpgradeStRestarting}, Dst: UpgradeStDisabled},
mpagenko80622a52021-02-09 16:53:23 +0000276 },
277 fsm.Callbacks{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000278 "enter_state": func(e *fsm.Event) { instFsm.PAdaptFsm.LogFsmStateChange(ctx, e) },
279 "enter_" + UpgradeStStarting: func(e *fsm.Event) { instFsm.enterStarting(ctx, e) },
280 "enter_" + UpgradeStWaitingAdapterDL: func(e *fsm.Event) { instFsm.enterWaitingAdapterDL(ctx, e) },
281 "enter_" + UpgradeStPreparingDL: func(e *fsm.Event) { instFsm.enterPreparingDL(ctx, e) },
282 "enter_" + UpgradeStDLSection: func(e *fsm.Event) { instFsm.enterDownloadSection(ctx, e) },
283 "enter_" + UpgradeStVerifyWindow: func(e *fsm.Event) { instFsm.enterVerifyWindow(ctx, e) },
284 "enter_" + UpgradeStFinalizeDL: func(e *fsm.Event) { instFsm.enterFinalizeDL(ctx, e) },
285 "enter_" + UpgradeStWaitEndDL: func(e *fsm.Event) { instFsm.enterWaitEndDL(ctx, e) },
286 "enter_" + UpgradeStCheckImageName: func(e *fsm.Event) { instFsm.enterCheckImageName(ctx, e) },
287 "enter_" + UpgradeStRequestingActivate: func(e *fsm.Event) { instFsm.enterActivateSw(ctx, e) },
288 "enter_" + UpgradeStCommitSw: func(e *fsm.Event) { instFsm.enterCommitSw(ctx, e) },
289 "enter_" + UpgradeStCheckCommitted: func(e *fsm.Event) { instFsm.enterCheckCommitted(ctx, e) },
290 "enter_" + UpgradeStResetting: func(e *fsm.Event) { instFsm.enterResetting(ctx, e) },
291 "enter_" + UpgradeStAbortingDL: func(e *fsm.Event) { instFsm.enterAbortingDL(ctx, e) },
292 "enter_" + UpgradeStRestarting: func(e *fsm.Event) { instFsm.enterRestarting(ctx, e) },
293 "enter_" + UpgradeStDisabled: func(e *fsm.Event) { instFsm.enterDisabled(ctx, e) },
mpagenko80622a52021-02-09 16:53:23 +0000294 },
295 )
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000296 if instFsm.PAdaptFsm.PFsm == nil {
mpagenko80622a52021-02-09 16:53:23 +0000297 logger.Errorw(ctx, "OnuUpgradeFsm's Base FSM could not be instantiated!!", log.Fields{
298 "device-id": instFsm.deviceID})
299 return nil
300 }
301
302 logger.Debugw(ctx, "OnuUpgradeFsm created", log.Fields{"device-id": instFsm.deviceID})
303 return instFsm
304}
305
306//SetDownloadParams configures the needed parameters for a specific download to the ONU
mpagenkoc26d4c02021-05-06 14:27:57 +0000307// called from 'old' API Activate_image_update()
mpagenko15ff4a52021-03-02 10:09:20 +0000308func (oFsm *OnuUpgradeFsm) SetDownloadParams(ctx context.Context, aInactiveImageID uint16,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000309 apImageDsc *voltha.ImageDownload, apDownloadManager *AdapterDownloadManager) error {
310 pBaseFsm := oFsm.PAdaptFsm.PFsm
311 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoaa3afe92021-05-21 16:20:58 +0000312 oFsm.mutexUpgradeParams.Lock()
mpagenko80622a52021-02-09 16:53:23 +0000313 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting", log.Fields{
314 "device-id": oFsm.deviceID, "image-description": apImageDsc})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000315 oFsm.InactiveImageMeID = aInactiveImageID //upgrade state machines run on configured inactive ImageId
mpagenko80622a52021-02-09 16:53:23 +0000316 oFsm.pImageDsc = apImageDsc
317 oFsm.pDownloadManager = apDownloadManager
Holger Hildebrandtac1e0592021-06-03 15:16:49 +0000318 oFsm.activateImage = true
319 oFsm.commitImage = true
mpagenkoaa3afe92021-05-21 16:20:58 +0000320 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000321
322 go func(aPBaseFsm *fsm.FSM) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000323 // let the upgrade FSM proceed to PreparingDL
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000324 _ = aPBaseFsm.Event(UpgradeEvPrepareSwDownload)
mpagenko80622a52021-02-09 16:53:23 +0000325 }(pBaseFsm)
326 return nil
327 }
328 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
329 "device-id": oFsm.deviceID})
330 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
331}
332
mpagenkoc26d4c02021-05-06 14:27:57 +0000333//SetDownloadParamsAfterDownload configures the needed parameters for a specific download to the ONU according to
334// updated API interface with R2.8: start download to ONU if the image is downloaded to the adapter
335// called from 'new' API Download_onu_image
336func (oFsm *OnuUpgradeFsm) SetDownloadParamsAfterDownload(ctx context.Context, aInactiveImageID uint16,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000337 apImageRequest *voltha.DeviceImageDownloadRequest, apDownloadManager *FileDownloadManager,
Holger Hildebrandtac010732021-06-02 13:35:39 +0000338 aImageIdentifier string) error {
mpagenkoc26d4c02021-05-06 14:27:57 +0000339 oFsm.mutexUpgradeParams.Lock()
340 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000341 if oFsm.PAdaptFsm != nil {
342 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000343 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000344 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000345 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting", log.Fields{
346 "device-id": oFsm.deviceID, "image-description": apImageRequest})
347 oFsm.useAPIVersion43 = true
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000348 oFsm.InactiveImageMeID = aInactiveImageID //upgrade state machines run on configured inactive ImageId
mpagenkoc26d4c02021-05-06 14:27:57 +0000349 oFsm.pFileManager = apDownloadManager
350 oFsm.imageIdentifier = aImageIdentifier
351 oFsm.imageVersion = apImageRequest.Image.Version
352 oFsm.activateImage = apImageRequest.ActivateOnSuccess
353 oFsm.commitImage = apImageRequest.CommitOnSuccess
mpagenko45586762021-10-01 08:30:22 +0000354 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_STARTED //state change indication for download request
mpagenkoc26d4c02021-05-06 14:27:57 +0000355 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000356 _ = pBaseFsm.Event(UpgradeEvAdapterDownload) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000357 return nil
358 }
359 oFsm.mutexUpgradeParams.Unlock()
360 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
361 "device-id": oFsm.deviceID})
362 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
363}
364
365//SetActivationParamsRunning sets the activate and commit flags for a running download to the ONU according to adapters rpc call
366// called from 'new' API Activate_onu_image
367func (oFsm *OnuUpgradeFsm) SetActivationParamsRunning(ctx context.Context,
368 aImageIdentifier string, aCommit bool) error {
mpagenko45586762021-10-01 08:30:22 +0000369 logger.Debugw(ctx, "OnuUpgradeFsm activate/commit parameter setting", log.Fields{
370 "device-id": oFsm.deviceID, "image-id": aImageIdentifier, "commit": aCommit})
mpagenkoc26d4c02021-05-06 14:27:57 +0000371 oFsm.mutexUpgradeParams.Lock()
372 //set activate/commit independent from state, if FSM is already beyond concerned states, then it does not matter anyway
373 // (as long as the Imageidentifier is correct)
mpagenkoc26d4c02021-05-06 14:27:57 +0000374 if aImageIdentifier != oFsm.imageIdentifier {
375 logger.Errorw(ctx, "OnuUpgradeFsm abort: mismatching upgrade image", log.Fields{
376 "device-id": oFsm.deviceID, "request-image": aImageIdentifier, "fsm-image": oFsm.imageIdentifier})
377 oFsm.mutexUpgradeParams.Unlock()
378 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm params ignored: requested image-name not used in current upgrade for device-id: %s",
379 oFsm.deviceID))
380 }
381 oFsm.activateImage = true
382 oFsm.commitImage = aCommit
mpagenko45586762021-10-01 08:30:22 +0000383 oFsm.mutexUpgradeParams.Unlock()
mpagenkoc26d4c02021-05-06 14:27:57 +0000384 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000385 if oFsm.PAdaptFsm != nil {
386 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000387 }
388 if pBaseFsm != nil {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000389 if pBaseFsm.Is(UpgradeStWaitForActivate) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000390 logger.Debugw(ctx, "OnuUpgradeFsm finish waiting for activate", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000391 _ = pBaseFsm.Event(UpgradeEvRequestActivate) //no need to call the FSM event in background here
mpagenko38662d02021-08-11 09:45:19 +0000392 } else {
mpagenko38662d02021-08-11 09:45:19 +0000393 logger.Debugw(ctx, "OnuUpgradeFsm not (yet?) waiting for activate", log.Fields{
394 "device-id": oFsm.deviceID, "current FsmState": pBaseFsm.Current()})
mpagenkoc26d4c02021-05-06 14:27:57 +0000395 }
396 return nil
397 }
398 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer", log.Fields{
399 "device-id": oFsm.deviceID})
400 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer for device-id: %s", oFsm.deviceID))
401}
402
403//SetActivationParamsStart starts upgrade processing with immediate activation
404// called from 'new' API Activate_onu_image
405func (oFsm *OnuUpgradeFsm) SetActivationParamsStart(ctx context.Context, aImageVersion string, aInactiveImageID uint16, aCommit bool) error {
406 oFsm.mutexUpgradeParams.Lock()
407 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000408 if oFsm.PAdaptFsm != nil {
409 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000410 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000411 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000412 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting to start with activation", log.Fields{
413 "device-id": oFsm.deviceID, "image-version": aImageVersion})
414 oFsm.useAPIVersion43 = true
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000415 oFsm.InactiveImageMeID = aInactiveImageID //upgrade state machines run on configured inactive ImageId
mpagenkoc26d4c02021-05-06 14:27:57 +0000416 oFsm.imageVersion = aImageVersion
417 oFsm.activateImage = true
418 oFsm.commitImage = aCommit
mpagenko38662d02021-08-11 09:45:19 +0000419 // indicate start of the upgrade activity
mpagenko45586762021-10-01 08:30:22 +0000420 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATING //state change indication for activate request
mpagenkoc26d4c02021-05-06 14:27:57 +0000421 oFsm.mutexUpgradeParams.Unlock()
422 //directly request the FSM to activate the image
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000423 _ = pBaseFsm.Event(UpgradeEvRequestActivate) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000424 return nil
425 }
426 oFsm.mutexUpgradeParams.Unlock()
427 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
428 "device-id": oFsm.deviceID})
429 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
430}
431
432//SetCommitmentParamsRunning sets the commit flag for a running download to the ONU according to adapters rpc call
433// called from 'new' API Commit_onu_image
mpagenko38662d02021-08-11 09:45:19 +0000434func (oFsm *OnuUpgradeFsm) SetCommitmentParamsRunning(ctx context.Context,
435 aImageIdentifier string, aImageVersion string) error {
mpagenkoc26d4c02021-05-06 14:27:57 +0000436 oFsm.mutexUpgradeParams.Lock()
437 //set commit independent from state, if FSM is already beyond commit state (just ready), then it does not matter anyway
438 // (as long as the Imageidentifier is correct)
439 logger.Debugw(ctx, "OnuUpgradeFsm commit parameter setting", log.Fields{
mpagenkoa2b288f2021-10-21 11:25:27 +0000440 "device-id": oFsm.deviceID, "image-id": aImageIdentifier, "image-version": aImageVersion})
mpagenko38662d02021-08-11 09:45:19 +0000441 if (aImageIdentifier != oFsm.imageIdentifier) && (aImageVersion != oFsm.imageVersion) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000442 logger.Errorw(ctx, "OnuUpgradeFsm abort: mismatching upgrade image", log.Fields{
mpagenko38662d02021-08-11 09:45:19 +0000443 "device-id": oFsm.deviceID, "request-identifier": aImageIdentifier, "fsm-identifier": oFsm.imageIdentifier,
444 "request-version": aImageVersion, "fsm-version": oFsm.imageVersion})
mpagenkoc26d4c02021-05-06 14:27:57 +0000445 oFsm.mutexUpgradeParams.Unlock()
446 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm params ignored: requested image-name not used in current upgrade for device-id: %s",
447 oFsm.deviceID))
448 }
449 oFsm.commitImage = true
450 oFsm.mutexUpgradeParams.Unlock()
451 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000452 if oFsm.PAdaptFsm != nil {
453 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000454 }
455 if pBaseFsm != nil {
mpagenko183647c2021-06-08 15:25:04 +0000456 //let the FSM decide if it is ready to process the event
457 logger.Debugw(ctx, "OnuUpgradeFsm requesting commit",
458 log.Fields{"device-id": oFsm.deviceID, "current FsmState": pBaseFsm.Current()})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000459 _ = pBaseFsm.Event(UpgradeEvCommitSw) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000460 return nil
461 }
mpagenko38662d02021-08-11 09:45:19 +0000462 //should never occur
mpagenkoc26d4c02021-05-06 14:27:57 +0000463 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer", log.Fields{
464 "device-id": oFsm.deviceID})
465 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer for device-id: %s", oFsm.deviceID))
466}
467
468//SetCommitmentParamsStart starts upgrade processing with immediate commitment
469// called from 'new' API Commit_onu_image
470func (oFsm *OnuUpgradeFsm) SetCommitmentParamsStart(ctx context.Context, aImageVersion string, aActiveImageID uint16) error {
471 oFsm.mutexUpgradeParams.Lock()
472 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000473 if oFsm.PAdaptFsm != nil {
474 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000475 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000476 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000477 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting to start with commitment", log.Fields{
478 "device-id": oFsm.deviceID, "image-version": aImageVersion})
479 oFsm.useAPIVersion43 = true
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000480 oFsm.InactiveImageMeID = aActiveImageID //upgrade state machines inactive ImageId is the new active ImageId
mpagenkoc26d4c02021-05-06 14:27:57 +0000481 oFsm.imageVersion = aImageVersion
482 oFsm.commitImage = true
mpagenko45586762021-10-01 08:30:22 +0000483 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTING //state change indication for activate request
mpagenkoc26d4c02021-05-06 14:27:57 +0000484 oFsm.mutexUpgradeParams.Unlock()
mpagenko38662d02021-08-11 09:45:19 +0000485 //directly request the FSM to commit the image
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000486 _ = pBaseFsm.Event(UpgradeEvCommitSw) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000487 return nil
488 }
489 oFsm.mutexUpgradeParams.Unlock()
490 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
491 "device-id": oFsm.deviceID})
492 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
493}
494
mpagenko1f8e8822021-06-25 14:10:21 +0000495//GetCommitFlag delivers the commit flag that was configured here
496func (oFsm *OnuUpgradeFsm) GetCommitFlag(ctx context.Context) bool {
497 oFsm.mutexUpgradeParams.RLock()
498 defer oFsm.mutexUpgradeParams.RUnlock()
499 return oFsm.commitImage
500}
501
mpagenko38662d02021-08-11 09:45:19 +0000502//GetImageStates delivers the download/image states as per device proto buf definition
mpagenkoaa3afe92021-05-21 16:20:58 +0000503func (oFsm *OnuUpgradeFsm) GetImageStates(ctx context.Context,
mpagenko38662d02021-08-11 09:45:19 +0000504 aImageIdentifier string, aVersion string) *voltha.ImageState {
mpagenkoaa3afe92021-05-21 16:20:58 +0000505 pImageState := &voltha.ImageState{}
mpagenko38662d02021-08-11 09:45:19 +0000506 pImageState.Version = aVersion //version as requested
mpagenkoaa3afe92021-05-21 16:20:58 +0000507 // check if the request refers to some active image/version of the processing
508 oFsm.mutexUpgradeParams.RLock()
509 if (aImageIdentifier == oFsm.imageIdentifier) || (aVersion == oFsm.imageVersion) {
510 pImageState.DownloadState = oFsm.volthaDownloadState
511 pImageState.Reason = oFsm.volthaDownloadReason
512 pImageState.ImageState = oFsm.volthaImageState
513 } else {
514 pImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
515 pImageState.Reason = voltha.ImageState_NO_ERROR
516 pImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
517 }
518 oFsm.mutexUpgradeParams.RUnlock()
mpagenko38662d02021-08-11 09:45:19 +0000519 return pImageState
mpagenkoaa3afe92021-05-21 16:20:58 +0000520}
521
mpagenko38662d02021-08-11 09:45:19 +0000522//SetImageStateActive sets the FSM internal volthaImageState to ImageState_IMAGE_ACTIVE
523func (oFsm *OnuUpgradeFsm) SetImageStateActive(ctx context.Context) {
mpagenko183647c2021-06-08 15:25:04 +0000524 oFsm.mutexUpgradeParams.Lock()
525 defer oFsm.mutexUpgradeParams.Unlock()
mpagenko45586762021-10-01 08:30:22 +0000526 oFsm.upgradePhase = cUpgradeActivated
mpagenko38662d02021-08-11 09:45:19 +0000527 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVE
mpagenko38662d02021-08-11 09:45:19 +0000528}
529
mpagenkoc26d4c02021-05-06 14:27:57 +0000530//CancelProcessing ensures that suspended processing at waiting on some response is aborted and reset of FSM
mpagenko38662d02021-08-11 09:45:19 +0000531func (oFsm *OnuUpgradeFsm) CancelProcessing(ctx context.Context, abCompleteAbort bool,
532 aReason voltha.ImageState_ImageFailureReason) {
mpagenkoa2b288f2021-10-21 11:25:27 +0000533 pAdaptFsm := oFsm.PAdaptFsm
534 if pAdaptFsm == nil || pAdaptFsm.PFsm == nil {
535 logger.Warnw(ctx, "OnuUpgradeFsm cancel, but FSM invalid", log.Fields{
536 "device-id": oFsm.deviceID})
537 return
538 }
539 logger.Debugw(ctx, "OnuUpgradeFsm start canceling", log.Fields{
540 "device-id": oFsm.deviceID, "in fsm-state": pAdaptFsm.PFsm.Current()})
mpagenko38662d02021-08-11 09:45:19 +0000541 oFsm.mutexAbortRequest.Lock()
542 oFsm.abortRequested = aReason //possibly abort the sectionDownload loop
543 oFsm.mutexAbortRequest.Unlock()
mpagenkoc26d4c02021-05-06 14:27:57 +0000544 //mutex protection is required for possible concurrent access to FSM members
545 //attention: for an unbuffered channel the sender is blocked until the value is received (processed)!
546 // accordingly the mutex must be released before sending to channel here (mutex acquired in receiver)
547 oFsm.mutexIsAwaitingAdapterDlResponse.RLock()
548 if oFsm.isWaitingForAdapterDlResponse {
549 oFsm.mutexIsAwaitingAdapterDlResponse.RUnlock()
550 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
551 oFsm.chAdapterDlReady <- false
552 } else {
553 oFsm.mutexIsAwaitingAdapterDlResponse.RUnlock()
554 }
mpagenko609a69a2021-12-02 08:12:58 +0000555
556 //abort a possible delaying of sending EndSwDl
557 //use asynchronous channel sending to avoid blocking here in case no receiver is waiting
558 select {
559 case oFsm.chAbortDelayEndSwDl <- struct{}{}:
560 default:
561 }
562
mpagenkoc26d4c02021-05-06 14:27:57 +0000563 //chOnuDlReady is cleared as part of the FSM reset processing (from enterResetting())
564
565 // 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 +0000566 // specific here: See definition of state changes: some states are excluded from reset for possible later commit
mpagenkoa2b288f2021-10-21 11:25:27 +0000567 pAdaptFsm = oFsm.PAdaptFsm
mpagenko59862f02021-10-11 08:53:18 +0000568 if pAdaptFsm != nil && pAdaptFsm.PFsm != nil {
mpagenkoc26d4c02021-05-06 14:27:57 +0000569 // calling FSM events in background to avoid blocking of the caller
mpagenko59862f02021-10-11 08:53:18 +0000570 go func(apFsm *fsm.FSM) {
571 if apFsm.Is(UpgradeStWaitEndDL) {
572 oFsm.chReceiveExpectedResponse <- false //which aborts the FSM in WaitEndDL state
573 } else if apFsm.Is(UpgradeStAbortingDL) {
574 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseAbort //abort waiting on EndDownloadResponse
575 }
mpagenko38662d02021-08-11 09:45:19 +0000576
mpagenko59862f02021-10-11 08:53:18 +0000577 var err error
578 if abCompleteAbort {
579 // in case of unconditional abort request the ImageState is set immediately
580 oFsm.mutexUpgradeParams.Lock()
581 //any previous lingering conditional cancelRequest is superseded by this abortion
582 oFsm.conditionalCancelRequested = false
583 oFsm.volthaDownloadReason = aReason
584 oFsm.mutexUpgradeParams.Unlock()
585 err = apFsm.Event(UpgradeEvAbort) //as unconditional default FSM cancellation
586 } else {
587 //at conditional abort request the image states are set when reaching the reset state
mpagenkoa2b288f2021-10-21 11:25:27 +0000588 oFsm.mutexUpgradeParams.Lock()
mpagenko59862f02021-10-11 08:53:18 +0000589 oFsm.conditionalCancelRequested = true
mpagenkoa2b288f2021-10-21 11:25:27 +0000590 oFsm.mutexUpgradeParams.Unlock()
mpagenko59862f02021-10-11 08:53:18 +0000591 err = apFsm.Event(UpgradeEvReset) //as state-conditional default FSM cleanup
592 }
593 if err != nil {
594 //error return is expected in case of conditional request and no state transition
595 logger.Debugw(ctx, "onu upgrade fsm could not cancel with abort/reset event", log.Fields{
596 "device-id": oFsm.deviceID, "error": err})
597 }
mpagenkoa2b288f2021-10-21 11:25:27 +0000598 logger.Debugw(ctx, "OnuUpgradeFsm canceling done", log.Fields{
599 "device-id": oFsm.deviceID})
mpagenko59862f02021-10-11 08:53:18 +0000600 }(pAdaptFsm.PFsm)
mpagenkoa2b288f2021-10-21 11:25:27 +0000601 } else { //the FSM seems already to be in some released state
602 logger.Warnw(ctx, "OnuUpgradeFsm canceling without FSM event", log.Fields{
603 "device-id": oFsm.deviceID})
604 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000605}
606
mpagenko80622a52021-02-09 16:53:23 +0000607func (oFsm *OnuUpgradeFsm) enterStarting(ctx context.Context, e *fsm.Event) {
608 logger.Debugw(ctx, "OnuUpgradeFsm start", log.Fields{"in state": e.FSM.Current(),
609 "device-id": oFsm.deviceID})
610
611 // start go routine for processing of LockState messages
612 go oFsm.processOmciUpgradeMessages(ctx)
613}
614
mpagenkoc26d4c02021-05-06 14:27:57 +0000615//enterWaitingAdapterDL state can only be reached with useAPIVersion43
616func (oFsm *OnuUpgradeFsm) enterWaitingAdapterDL(ctx context.Context, e *fsm.Event) {
617 logger.Debugw(ctx, "OnuUpgradeFsm waiting for adapter download", log.Fields{"in state": e.FSM.Current(),
618 "device-id": oFsm.deviceID})
mpagenko38662d02021-08-11 09:45:19 +0000619 syncChannel := make(chan struct{})
620 go oFsm.waitOnDownloadToAdapterReady(ctx, syncChannel, oFsm.chAdapterDlReady)
621 //block until the wait routine is really blocked on chAdapterDlReady
622 <-syncChannel
mpagenkoc26d4c02021-05-06 14:27:57 +0000623 go oFsm.pFileManager.RequestDownloadReady(ctx, oFsm.imageIdentifier, oFsm.chAdapterDlReady)
624}
625
mpagenko80622a52021-02-09 16:53:23 +0000626func (oFsm *OnuUpgradeFsm) enterPreparingDL(ctx context.Context, e *fsm.Event) {
627 logger.Debugw(ctx, "OnuUpgradeFsm prepare Download to Onu", log.Fields{"in state": e.FSM.Current(),
628 "device-id": oFsm.deviceID})
629
mpagenkoc26d4c02021-05-06 14:27:57 +0000630 var fileLen int64
631 var err error
mpagenkoaa3afe92021-05-21 16:20:58 +0000632 oFsm.mutexUpgradeParams.Lock()
mpagenkoc26d4c02021-05-06 14:27:57 +0000633 if oFsm.useAPIVersion43 {
634 //with the new API structure download to adapter is implicit and we have to wait until the image is available
635 fileLen, err = oFsm.pFileManager.GetImageBufferLen(ctx, oFsm.imageIdentifier)
636 } else {
637 fileLen, err = oFsm.pDownloadManager.getImageBufferLen(ctx, oFsm.pImageDsc.Name, oFsm.pImageDsc.LocalDir)
638 }
mpagenkoc497ee32021-11-10 17:30:20 +0000639 if err != nil || fileLen == 0 || fileLen > int64(cMaxUint32) {
mpagenko39b703e2021-08-25 13:38:40 +0000640 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto)
mpagenkoaa3afe92021-05-21 16:20:58 +0000641 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000642 logger.Errorw(ctx, "OnuUpgradeFsm abort: problems getting image buffer length", log.Fields{
643 "device-id": oFsm.deviceID, "error": err, "length": fileLen})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000644 pBaseFsm := oFsm.PAdaptFsm
mpagenko80622a52021-02-09 16:53:23 +0000645 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000646 go func(a_pAFsm *cmn.AdapterFsm) {
647 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko80622a52021-02-09 16:53:23 +0000648 }(pBaseFsm)
649 return
650 }
651
mpagenkoc26d4c02021-05-06 14:27:57 +0000652 //copy file content to buffer
mpagenkoc497ee32021-11-10 17:30:20 +0000653 var imageBuffer []byte
mpagenkoc26d4c02021-05-06 14:27:57 +0000654 if oFsm.useAPIVersion43 {
mpagenkoc497ee32021-11-10 17:30:20 +0000655 imageBuffer, err = oFsm.pFileManager.GetDownloadImageBuffer(ctx, oFsm.imageIdentifier)
mpagenkoc26d4c02021-05-06 14:27:57 +0000656 } else {
mpagenkoc497ee32021-11-10 17:30:20 +0000657 imageBuffer, err = oFsm.pDownloadManager.getDownloadImageBuffer(ctx, oFsm.pImageDsc.Name, oFsm.pImageDsc.LocalDir)
mpagenkoc26d4c02021-05-06 14:27:57 +0000658 }
mpagenko80622a52021-02-09 16:53:23 +0000659 if err != nil {
mpagenko39b703e2021-08-25 13:38:40 +0000660 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto)
mpagenkoaa3afe92021-05-21 16:20:58 +0000661 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000662 logger.Errorw(ctx, "OnuUpgradeFsm abort: can't get image buffer", log.Fields{
663 "device-id": oFsm.deviceID, "error": err})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000664 pBaseFsm := oFsm.PAdaptFsm
mpagenko80622a52021-02-09 16:53:23 +0000665 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000666 go func(a_pAFsm *cmn.AdapterFsm) {
667 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko80622a52021-02-09 16:53:23 +0000668 }(pBaseFsm)
669 return
670 }
mpagenkoc497ee32021-11-10 17:30:20 +0000671 //provide slice capacity already with the reserve of one section to avoid inflation of the slice to double size at append
672 oFsm.imageBuffer = make([]byte, fileLen, fileLen+cOmciDownloadSectionSize)
673 //better use a copy of the read image buffer in case the buffer/file is modified from outside,
674 // this also limits the slice len to the expected maximum fileLen
675 copy(oFsm.imageBuffer, imageBuffer)
mpagenko80622a52021-02-09 16:53:23 +0000676
677 oFsm.noOfSections = uint32(fileLen / cOmciDownloadSectionSize)
678 if fileLen%cOmciDownloadSectionSize > 0 {
mpagenkoc26d4c02021-05-06 14:27:57 +0000679 bufferPadding := make([]byte, cOmciDownloadSectionSize-uint32((fileLen)%cOmciDownloadSectionSize))
mpagenko80622a52021-02-09 16:53:23 +0000680 //expand the imageBuffer to exactly fit multiples of cOmciDownloadSectionSize with padding
mpagenkoc497ee32021-11-10 17:30:20 +0000681 oFsm.imageBuffer = append(oFsm.imageBuffer, bufferPadding...)
mpagenko80622a52021-02-09 16:53:23 +0000682 oFsm.noOfSections++
683 }
684 oFsm.origImageLength = uint32(fileLen)
685 oFsm.imageLength = uint32(len(oFsm.imageBuffer))
mpagenko80622a52021-02-09 16:53:23 +0000686 logger.Infow(ctx, "OnuUpgradeFsm starts with StartSwDl values", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000687 "MeId": oFsm.InactiveImageMeID, "windowSizeLimit": oFsm.omciDownloadWindowSizeLimit,
mpagenko80622a52021-02-09 16:53:23 +0000688 "ImageSize": oFsm.imageLength, "original file size": fileLen})
689 //"NumberOfCircuitPacks": oFsm.numberCircuitPacks, "CircuitPacks MeId": 0}) //parallel circuit packs download not supported
mpagenkoaa3afe92021-05-21 16:20:58 +0000690
691 oFsm.mutexUpgradeParams.Unlock()
mpagenko39b703e2021-08-25 13:38:40 +0000692
mpagenkoa2b288f2021-10-21 11:25:27 +0000693 // flush chOnuDlReady
mpagenko39b703e2021-08-25 13:38:40 +0000694 select {
695 case <-oFsm.chOnuDlReady:
696 logger.Debug(ctx, "flushed OnuDlReady channel")
697 default:
698 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000699 go oFsm.waitOnDownloadToOnuReady(ctx, oFsm.chOnuDlReady) // start supervision of the complete download-to-ONU procedure
700
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000701 err = oFsm.pOmciCC.SendStartSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
702 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, oFsm.omciDownloadWindowSizeLimit, oFsm.origImageLength)
mpagenko80622a52021-02-09 16:53:23 +0000703 if err != nil {
704 logger.Errorw(ctx, "StartSwDl abort: can't send section", log.Fields{
705 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +0000706 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +0000707 return
708 }
Holger Hildebrandt288d9472021-09-06 10:47:53 +0000709 oFsm.isEndSwDlOpen = true
mpagenko80622a52021-02-09 16:53:23 +0000710}
711
712func (oFsm *OnuUpgradeFsm) enterDownloadSection(ctx context.Context, e *fsm.Event) {
713 logger.Debugw(ctx, "OnuUpgradeFsm start downloading sections", log.Fields{
714 "device-id": oFsm.deviceID, "absolute window": oFsm.nextDownloadWindow})
mpagenkoa2b288f2021-10-21 11:25:27 +0000715 //use a background routine to send the multiple download sections frames in a loop
mpagenko609a69a2021-12-02 08:12:58 +0000716 // in order to avoid blocking on synchronous event calls for the entire (long) processing time
mpagenkoa2b288f2021-10-21 11:25:27 +0000717 go oFsm.runSwDlSectionWindow(ctx)
718}
mpagenko80622a52021-02-09 16:53:23 +0000719
mpagenkoa2b288f2021-10-21 11:25:27 +0000720//runSwDlSectionWindow runs a loop to send all DlSection frames of one window in background
721// may be aborted by parallel change of abortRequested
722func (oFsm *OnuUpgradeFsm) runSwDlSectionWindow(ctx context.Context) {
mpagenko80622a52021-02-09 16:53:23 +0000723 var windowAckRequest uint8 = 0
724 var bufferStartOffset uint32
725 var bufferEndOffset uint32
726 var downloadSection []byte
kesavand011d5162021-11-25 19:21:06 +0530727
mpagenkoaa3afe92021-05-21 16:20:58 +0000728 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +0000729 oFsm.upgradePhase = cUpgradeDownloading //start of downloading image to ONU
mpagenko80622a52021-02-09 16:53:23 +0000730 if oFsm.nextDownloadSectionsAbsolute == 0 {
mpagenkoaa3afe92021-05-21 16:20:58 +0000731 oFsm.volthaImageState = voltha.ImageState_IMAGE_DOWNLOADING
mpagenko80622a52021-02-09 16:53:23 +0000732 }
kesavand011d5162021-11-25 19:21:06 +0530733 //var omuTxSecPerWindow []*common.OmciTransferStructure
734 omciMsgsPerSection := &ia.OmciMessages{}
735 //take the TID mutex
736 //To ensure the insequesnce of TIDS for all the onusw sections within a window
737 oFsm.pOmciCC.LockMutexTID()
738 defer oFsm.pOmciCC.UnLockMutexTID()
mpagenko80622a52021-02-09 16:53:23 +0000739 for {
mpagenko38662d02021-08-11 09:45:19 +0000740 oFsm.mutexAbortRequest.RLock()
741 // this way out of the section download loop on abort request
742 if oFsm.abortRequested != voltha.ImageState_NO_ERROR {
mpagenko45586762021-10-01 08:30:22 +0000743 //states are updated when entering the reset state ...
mpagenko38662d02021-08-11 09:45:19 +0000744 oFsm.volthaDownloadReason = oFsm.abortRequested
745 oFsm.mutexAbortRequest.RUnlock()
746 oFsm.mutexUpgradeParams.Unlock()
mpagenkoa2b288f2021-10-21 11:25:27 +0000747 pUpgradeFsm := oFsm.PAdaptFsm
748 if pUpgradeFsm != nil {
749 _ = pUpgradeFsm.PFsm.Event(UpgradeEvAbort)
750 logger.Debugw(ctx, "aborting runSwDlSectionWindow", log.Fields{
751 "device-id": oFsm.deviceID, "reason": oFsm.volthaDownloadReason})
752 return
753 }
754 logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
mpagenko38662d02021-08-11 09:45:19 +0000755 return
756 }
757 oFsm.mutexAbortRequest.RUnlock()
758
mpagenko80622a52021-02-09 16:53:23 +0000759 bufferStartOffset = oFsm.nextDownloadSectionsAbsolute * cOmciDownloadSectionSize
760 bufferEndOffset = bufferStartOffset + cOmciDownloadSectionSize - 1 //for representing cOmciDownloadSectionSizeLimit values
761 logger.Debugw(ctx, "DlSection values are", log.Fields{
762 "DlSectionNoAbsolute": oFsm.nextDownloadSectionsAbsolute,
763 "DlSectionWindow": oFsm.nextDownloadSectionsWindow,
764 "startOffset": bufferStartOffset, "endOffset": bufferEndOffset})
765 if bufferStartOffset+1 > oFsm.imageLength || bufferEndOffset+1 > oFsm.imageLength { //should never occur in this state
766 logger.Errorw(ctx, "OnuUpgradeFsm buffer error: exceeded length", log.Fields{
767 "device-id": oFsm.deviceID, "bufferStartOffset": bufferStartOffset,
768 "bufferEndOffset": bufferEndOffset, "imageLength": oFsm.imageLength})
mpagenko39b703e2021-08-25 13:38:40 +0000769 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto)
mpagenkoaa3afe92021-05-21 16:20:58 +0000770 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000771 //logical error -- reset the FSM
mpagenkoa2b288f2021-10-21 11:25:27 +0000772 pUpgradeFsm := oFsm.PAdaptFsm
773 if pUpgradeFsm != nil {
774 _ = pUpgradeFsm.PFsm.Event(UpgradeEvAbort)
775 return
776 }
777 logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
mpagenko80622a52021-02-09 16:53:23 +0000778 return
779 }
780 downloadSection = oFsm.imageBuffer[bufferStartOffset : bufferEndOffset+1]
781 if oFsm.nextDownloadSectionsWindow == oFsm.omciDownloadWindowSizeLimit {
782 windowAckRequest = 1
783 logger.Debugw(ctx, "DlSection expect Response for complete window", log.Fields{
784 "device-id": oFsm.deviceID, "in window": oFsm.nextDownloadWindow})
785 }
786 if oFsm.nextDownloadSectionsAbsolute+1 >= oFsm.noOfSections {
787 windowAckRequest = 1
kesavand011d5162021-11-25 19:21:06 +0530788
mpagenko15ff4a52021-03-02 10:09:20 +0000789 oFsm.omciDownloadWindowSizeLast = oFsm.nextDownloadSectionsWindow
790 logger.Infow(ctx, "DlSection expect Response for last window (section)", log.Fields{
mpagenko80622a52021-02-09 16:53:23 +0000791 "device-id": oFsm.deviceID, "DlSectionNoAbsolute": oFsm.nextDownloadSectionsAbsolute})
792 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000793 oFsm.mutexUpgradeParams.Unlock() //unlock here to give other functions some chance to process during/after the send request
kesavand011d5162021-11-25 19:21:06 +0530794 omciTxReq, err := oFsm.pOmciCC.PrepareOnuSectionsOfWindow(log.WithSpanFromContext(context.Background(), ctx),
795 oFsm.InactiveImageMeID, windowAckRequest, oFsm.nextDownloadSectionsWindow, downloadSection, omciMsgsPerSection)
mpagenko80622a52021-02-09 16:53:23 +0000796 if err != nil {
797 logger.Errorw(ctx, "DlSection abort: can't send section", log.Fields{
mpagenko15ff4a52021-03-02 10:09:20 +0000798 "device-id": oFsm.deviceID, "section absolute": oFsm.nextDownloadSectionsAbsolute, "error": err})
mpagenkoa2b288f2021-10-21 11:25:27 +0000799 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +0000800 return
801 }
kesavand011d5162021-11-25 19:21:06 +0530802
mpagenkoaa3afe92021-05-21 16:20:58 +0000803 oFsm.mutexUpgradeParams.Lock()
mpagenko80622a52021-02-09 16:53:23 +0000804 oFsm.nextDownloadSectionsAbsolute++ //always increase the absolute section counter after having sent one
805 if windowAckRequest == 1 {
mpagenkoaa3afe92021-05-21 16:20:58 +0000806 oFsm.mutexUpgradeParams.Unlock()
kesavand011d5162021-11-25 19:21:06 +0530807
808 if omciTxReq.OnuSwWindow == nil {
809 logger.Errorw(ctx, "fail to send sections in a window", log.Fields{
810 "device-id": oFsm.deviceID, "section absolute": oFsm.nextDownloadSectionsAbsolute, "error": err})
811 oFsm.abortOnOmciError(ctx, false)
812 return
813 }
814
mpagenkoa2b288f2021-10-21 11:25:27 +0000815 pUpgradeFsm := oFsm.PAdaptFsm
816 if pUpgradeFsm != nil {
817 _ = pUpgradeFsm.PFsm.Event(UpgradeEvWaitWindowAck) //state transition to upgradeStVerifyWindow
kesavand011d5162021-11-25 19:21:06 +0530818 oFsm.pOmciCC.SendOnuSwSectionsWindowWithRxSupervision(ctx, omciTxReq, oFsm.pDeviceHandler.GetOmciTimeout(), oFsm.PAdaptFsm.CommChan)
mpagenkoa2b288f2021-10-21 11:25:27 +0000819 return
820 }
821 logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
mpagenko80622a52021-02-09 16:53:23 +0000822 return
823 }
kesavand011d5162021-11-25 19:21:06 +0530824
mpagenko80622a52021-02-09 16:53:23 +0000825 oFsm.nextDownloadSectionsWindow++ //increase the window related section counter only if not in the last section
mpagenko59498c12021-03-18 14:15:15 +0000826 if oFsm.omciSectionInterleaveDelay > 0 {
mpagenko80622a52021-02-09 16:53:23 +0000827 //ensure a defined intersection-time-gap to leave space for further processing, other ONU's ...
mpagenkoaa3afe92021-05-21 16:20:58 +0000828 oFsm.mutexUpgradeParams.Unlock() //unlock here to give other functions some chance to process during/after the send request
mpagenko59498c12021-03-18 14:15:15 +0000829 time.Sleep(oFsm.omciSectionInterleaveDelay * time.Millisecond)
mpagenkoaa3afe92021-05-21 16:20:58 +0000830 oFsm.mutexUpgradeParams.Lock()
mpagenko80622a52021-02-09 16:53:23 +0000831 }
832 }
mpagenkoa2b288f2021-10-21 11:25:27 +0000833} //runSwDlSectionWindow
mpagenko80622a52021-02-09 16:53:23 +0000834
835func (oFsm *OnuUpgradeFsm) enterVerifyWindow(ctx context.Context, e *fsm.Event) {
836 logger.Debugw(ctx, "OnuUpgradeFsm verify DL window ack", log.Fields{
837 "for window": oFsm.nextDownloadWindow, "device-id": oFsm.deviceID})
838}
839
840func (oFsm *OnuUpgradeFsm) enterFinalizeDL(ctx context.Context, e *fsm.Event) {
mpagenko80622a52021-02-09 16:53:23 +0000841 logger.Infow(ctx, "OnuUpgradeFsm finalize DL", log.Fields{
mpagenko59498c12021-03-18 14:15:15 +0000842 "device-id": oFsm.deviceID, "crc": strconv.FormatInt(int64(oFsm.imageCRC), 16), "delay": oFsm.delayEndSwDl})
mpagenko609a69a2021-12-02 08:12:58 +0000843 //use a background routine to wait EndSwDlDelay and then send the EndSwDl request
844 // in order to avoid blocking on synchronous event calls for the complete wait time
845 // and to allow for state transition before sending the EndSwDl request
846 go oFsm.delayAndSendEndSwDl(ctx)
847}
mpagenko80622a52021-02-09 16:53:23 +0000848
mpagenko609a69a2021-12-02 08:12:58 +0000849//delayAndSendEndSwDl ensures a delay before sending the EndSwDl request
850// may also be aborted by parallel channel reception on chAbortEndSwDl
851func (oFsm *OnuUpgradeFsm) delayAndSendEndSwDl(ctx context.Context) {
mpagenkoaa3afe92021-05-21 16:20:58 +0000852 oFsm.mutexUpgradeParams.RLock()
mpagenko80622a52021-02-09 16:53:23 +0000853 if oFsm.delayEndSwDl {
mpagenkoaa3afe92021-05-21 16:20:58 +0000854 oFsm.mutexUpgradeParams.RUnlock()
mpagenko609a69a2021-12-02 08:12:58 +0000855 //give the ONU some time for image evaluation (hoping it does not only start this evaluation on first EndSwDl itself)
856 logger.Debugw(ctx, "OnuUpgradeFsm delay EndSwDl", log.Fields{"device-id": oFsm.deviceID,
857 "duration_s": cOmciEndSwDlDelaySeconds})
858 select {
859 case <-time.After(cOmciEndSwDlDelaySeconds * time.Second):
860 logger.Warnw(ctx, "OnuUpgradeFsm start sending EndSwDl", log.Fields{
861 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier})
862 case <-oFsm.chAbortDelayEndSwDl:
863 logger.Debugw(ctx, "OnuUpgradeFsm abort request to send EndSwDl", log.Fields{"device-id": oFsm.deviceID})
864 //according further state transition is ensured by the entity that sent the abort
865 return
866 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000867 } else {
868 oFsm.mutexUpgradeParams.RUnlock()
mpagenko80622a52021-02-09 16:53:23 +0000869 }
870
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000871 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000872 if pBaseFsm == nil {
mpagenko38662d02021-08-11 09:45:19 +0000873 logger.Errorw(ctx, "EndSwDl abort: BaseFsm invalid", log.Fields{"device-id": oFsm.deviceID})
874 oFsm.mutexUpgradeParams.Lock()
mpagenko38662d02021-08-11 09:45:19 +0000875 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR
876 oFsm.mutexUpgradeParams.Unlock()
mpagenko59498c12021-03-18 14:15:15 +0000877 // Can't call FSM Event directly, decoupling it
mpagenko609a69a2021-12-02 08:12:58 +0000878 _ = pBaseFsm.PFsm.Event(UpgradeEvAbort)
mpagenko59498c12021-03-18 14:15:15 +0000879 return
880 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000881 err := oFsm.pOmciCC.SendEndSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
882 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, oFsm.origImageLength, oFsm.imageCRC)
Holger Hildebrandt288d9472021-09-06 10:47:53 +0000883
mpagenko80622a52021-02-09 16:53:23 +0000884 if err != nil {
mpagenkoa2b288f2021-10-21 11:25:27 +0000885 logger.Errorw(ctx, "EndSwDl abort: error sending EndSwDl", log.Fields{
mpagenko80622a52021-02-09 16:53:23 +0000886 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +0000887 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +0000888 return
889 }
mpagenko609a69a2021-12-02 08:12:58 +0000890 //from here on sending of EndSwDl(Abort) is not needed anymore (even though response is not yet received)
891 // this avoids sending of both EndSwDl(Success) and EndSwDl(Abort) when cancellation falls just into this window
892 // the ONU must theoretically be prepared to receive none of them (in case of OMCI transfer issues) e.g. by timeout
893 oFsm.isEndSwDlOpen = false
894 // wait for the EndSwDLResponse and check, if the ONU is ready for activation
895 _ = pBaseFsm.PFsm.Event(UpgradeEvWaitEndDownload)
896} //delayAndSendEndSwDl
mpagenko59498c12021-03-18 14:15:15 +0000897
898func (oFsm *OnuUpgradeFsm) enterWaitEndDL(ctx context.Context, e *fsm.Event) {
899 logger.Infow(ctx, "OnuUpgradeFsm WaitEndDl", log.Fields{
900 "device-id": oFsm.deviceID, "wait delay": oFsm.waitDelayEndSwDl * time.Second, "wait count": oFsm.waitCountEndSwDl})
901 if oFsm.waitCountEndSwDl == 0 {
902 logger.Errorw(ctx, "WaitEndDl abort: max limit of EndSwDL reached", log.Fields{
903 "device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000904 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000905 if pBaseFsm == nil {
906 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
907 "device-id": oFsm.deviceID})
908 return
909 }
mpagenko38662d02021-08-11 09:45:19 +0000910 oFsm.mutexUpgradeParams.Lock()
mpagenko39b703e2021-08-25 13:38:40 +0000911 oFsm.volthaDownloadReason = voltha.ImageState_IMAGE_REFUSED_BY_ONU //something like 'END_DOWNLOAD_TIMEOUT' would be better (proto)
mpagenko38662d02021-08-11 09:45:19 +0000912 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000913 go func(a_pAFsm *cmn.AdapterFsm) {
914 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko59498c12021-03-18 14:15:15 +0000915 }(pBaseFsm)
916 return
917 }
918
919 oFsm.waitCountEndSwDl--
920 select {
921 case <-time.After(oFsm.waitDelayEndSwDl * time.Second):
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000922 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000923 if pBaseFsm == nil {
924 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
925 "device-id": oFsm.deviceID})
926 //FSM may be reset already from somewhere else, nothing we can do here anymore
927 return
928 }
929 //retry End SW DL
mpagenkoaa3afe92021-05-21 16:20:58 +0000930 oFsm.mutexUpgradeParams.Lock()
mpagenko59498c12021-03-18 14:15:15 +0000931 oFsm.delayEndSwDl = false //no more extra delay for the request
mpagenkoaa3afe92021-05-21 16:20:58 +0000932 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000933 go func(a_pAFsm *cmn.AdapterFsm) {
934 _ = a_pAFsm.PFsm.Event(UpgradeEvContinueFinalize)
mpagenko59498c12021-03-18 14:15:15 +0000935 }(pBaseFsm)
936 return
937 case success := <-oFsm.chReceiveExpectedResponse:
938 logger.Debugw(ctx, "WaitEndDl stop wait timer", log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +0000939 oFsm.isEndSwDlOpen = false //no request to abort of download (already finished or immediate abort)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000940 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000941 if pBaseFsm == nil {
942 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
943 "device-id": oFsm.deviceID})
944 //FSM may be reset already from somewhere else, nothing we can do here anymore
945 return
946 }
947 if success {
948 //answer received with ready indication
mpagenko38662d02021-08-11 09:45:19 +0000949 //useAPIVersion43 may not conflict in concurrency in this state function
950 if oFsm.useAPIVersion43 { // newer API usage requires verification of downloaded image version
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000951 go func(a_pAFsm *cmn.AdapterFsm) {
952 _ = a_pAFsm.PFsm.Event(UpgradeEvCheckImageName)
mpagenkoc26d4c02021-05-06 14:27:57 +0000953 }(pBaseFsm)
mpagenko38662d02021-08-11 09:45:19 +0000954 } else { // elder API usage does not support image version check -immediately consider download as successful
955 if oFsm.activateImage {
956 //immediate activation requested
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000957 go func(a_pAFsm *cmn.AdapterFsm) {
958 _ = a_pAFsm.PFsm.Event(UpgradeEvRequestActivate)
mpagenko38662d02021-08-11 09:45:19 +0000959 }(pBaseFsm)
960 } else {
961 //have to wait on explicit activation request
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000962 go func(a_pAFsm *cmn.AdapterFsm) {
963 _ = a_pAFsm.PFsm.Event(UpgradeEvWaitForActivate)
mpagenko38662d02021-08-11 09:45:19 +0000964 }(pBaseFsm)
965 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000966 }
mpagenko59498c12021-03-18 14:15:15 +0000967 return
968 }
969 //timer was aborted
mpagenko45586762021-10-01 08:30:22 +0000970 oFsm.abortOnOmciError(ctx, true)
mpagenko59498c12021-03-18 14:15:15 +0000971 return
972 }
mpagenko80622a52021-02-09 16:53:23 +0000973}
974
mpagenko38662d02021-08-11 09:45:19 +0000975func (oFsm *OnuUpgradeFsm) enterCheckImageName(ctx context.Context, e *fsm.Event) {
976 logger.Debugw(ctx, "OnuUpgradeFsm checking downloaded image name", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000977 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
Holger Hildebrandt3ac49bd2022-02-07 17:46:43 +0000978 requestedAttributes := me.AttributeValueMap{me.SoftwareImage_IsCommitted: 0, me.SoftwareImage_IsActive: 0, me.SoftwareImage_Version: ""}
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000979 meInstance, err := oFsm.pOmciCC.SendGetMe(log.WithSpanFromContext(context.Background(), ctx),
980 me.SoftwareImageClassID, oFsm.InactiveImageMeID, requestedAttributes, oFsm.pDeviceHandler.GetOmciTimeout(),
981 false, oFsm.PAdaptFsm.CommChan)
mpagenko38662d02021-08-11 09:45:19 +0000982 if err != nil {
983 logger.Errorw(ctx, "OnuUpgradeFsm get Software Image ME result error",
984 log.Fields{"device-id": oFsm.deviceID, "Error": err})
mpagenko45586762021-10-01 08:30:22 +0000985 oFsm.abortOnOmciError(ctx, true)
mpagenko38662d02021-08-11 09:45:19 +0000986 return
987 }
988 oFsm.pLastTxMeInstance = meInstance
989}
990
mpagenko80622a52021-02-09 16:53:23 +0000991func (oFsm *OnuUpgradeFsm) enterActivateSw(ctx context.Context, e *fsm.Event) {
992 logger.Infow(ctx, "OnuUpgradeFsm activate SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000993 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
mpagenko80622a52021-02-09 16:53:23 +0000994
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000995 err := oFsm.pOmciCC.SendActivateSoftware(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
996 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID)
mpagenko80622a52021-02-09 16:53:23 +0000997 if err != nil {
998 logger.Errorw(ctx, "ActivateSw abort: can't send activate frame", log.Fields{
999 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +00001000 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +00001001 return
1002 }
mpagenko38662d02021-08-11 09:45:19 +00001003 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +00001004 oFsm.upgradePhase = cUpgradeActivating //start of image activation for ONU
mpagenko38662d02021-08-11 09:45:19 +00001005 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATING
1006 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +00001007}
1008
1009func (oFsm *OnuUpgradeFsm) enterCommitSw(ctx context.Context, e *fsm.Event) {
mpagenko38662d02021-08-11 09:45:19 +00001010 logger.Debugw(ctx, "OnuUpgradeFsm start commit SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001011 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
mpagenko38662d02021-08-11 09:45:19 +00001012 //any abort request (also conditional) is still regarded as valid as the commit indication might not be possible to verify
1013 // (which is a bit problematic as the ONU might already be in committed state,
1014 // in this case (committing failed) always 'onuimage list' should be used to verify the real state (if ONU is reachable))
1015 if activeImageID, err := oFsm.pDevEntry.GetActiveImageMeID(ctx); err == nil {
mpagenkoaa3afe92021-05-21 16:20:58 +00001016 oFsm.mutexUpgradeParams.Lock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001017 if activeImageID == oFsm.InactiveImageMeID {
1018 inactiveImageID := oFsm.InactiveImageMeID
mpagenko15ff4a52021-03-02 10:09:20 +00001019 logger.Infow(ctx, "OnuUpgradeFsm commit SW", log.Fields{
mpagenkoaa3afe92021-05-21 16:20:58 +00001020 "device-id": oFsm.deviceID, "me-id": inactiveImageID}) //more efficient activeImageID with above check
1021 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTING
mpagenko45586762021-10-01 08:30:22 +00001022 oFsm.upgradePhase = cUpgradeCommitting //start of image commitment for ONU
mpagenkoaa3afe92021-05-21 16:20:58 +00001023 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001024 err := oFsm.pOmciCC.SendCommitSoftware(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
1025 oFsm.PAdaptFsm.CommChan, inactiveImageID) //more efficient activeImageID with above check
mpagenko15ff4a52021-03-02 10:09:20 +00001026 if err != nil {
1027 logger.Errorw(ctx, "CommitSw abort: can't send commit sw frame", log.Fields{
1028 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +00001029 oFsm.abortOnOmciError(ctx, true)
mpagenko15ff4a52021-03-02 10:09:20 +00001030 return
1031 }
1032 return
1033 }
mpagenko38662d02021-08-11 09:45:19 +00001034 oFsm.mutexUpgradeParams.Unlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001035 logger.Errorw(ctx, "OnuUpgradeFsm active ImageId <> IdToCommit", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001036 "device-id": oFsm.deviceID, "active ID": activeImageID, "to commit ID": oFsm.InactiveImageMeID})
mpagenko38662d02021-08-11 09:45:19 +00001037 } else {
1038 logger.Errorw(ctx, "OnuUpgradeFsm can't commit, no valid active image", log.Fields{
1039 "device-id": oFsm.deviceID})
mpagenko15ff4a52021-03-02 10:09:20 +00001040 }
mpagenko38662d02021-08-11 09:45:19 +00001041 oFsm.mutexUpgradeParams.Lock()
1042 oFsm.conditionalCancelRequested = false //any lingering conditional cancelRequest is superseded by this error
mpagenko38662d02021-08-11 09:45:19 +00001043 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
mpagenko38662d02021-08-11 09:45:19 +00001044 oFsm.mutexUpgradeParams.Unlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001045 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001046 pBaseFsm := oFsm.PAdaptFsm
mpagenko15ff4a52021-03-02 10:09:20 +00001047 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001048 go func(a_pAFsm *cmn.AdapterFsm) {
1049 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko15ff4a52021-03-02 10:09:20 +00001050 }(pBaseFsm)
1051}
1052
1053func (oFsm *OnuUpgradeFsm) enterCheckCommitted(ctx context.Context, e *fsm.Event) {
mpagenko38662d02021-08-11 09:45:19 +00001054 logger.Debugw(ctx, "OnuUpgradeFsm checking committed SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001055 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
Holger Hildebrandt3ac49bd2022-02-07 17:46:43 +00001056 requestedAttributes := me.AttributeValueMap{me.SoftwareImage_IsCommitted: 0, me.SoftwareImage_IsActive: 0, me.SoftwareImage_Version: ""}
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001057 meInstance, err := oFsm.pOmciCC.SendGetMe(log.WithSpanFromContext(context.Background(), ctx),
1058 me.SoftwareImageClassID, oFsm.InactiveImageMeID, requestedAttributes, oFsm.pDeviceHandler.GetOmciTimeout(), false, oFsm.PAdaptFsm.CommChan)
ozgecanetsiab36ed572021-04-01 10:38:48 +03001059 if err != nil {
1060 logger.Errorw(ctx, "OnuUpgradeFsm get Software Image ME result error",
1061 log.Fields{"device-id": oFsm.deviceID, "Error": err})
mpagenko45586762021-10-01 08:30:22 +00001062 oFsm.abortOnOmciError(ctx, true)
ozgecanetsiab36ed572021-04-01 10:38:48 +03001063 return
1064 }
mpagenko15ff4a52021-03-02 10:09:20 +00001065 oFsm.pLastTxMeInstance = meInstance
mpagenko80622a52021-02-09 16:53:23 +00001066}
1067
1068func (oFsm *OnuUpgradeFsm) enterResetting(ctx context.Context, e *fsm.Event) {
1069 logger.Debugw(ctx, "OnuUpgradeFsm resetting", log.Fields{"device-id": oFsm.deviceID})
1070
mpagenko45586762021-10-01 08:30:22 +00001071 oFsm.stateUpdateOnReset(ctx)
mpagenko38662d02021-08-11 09:45:19 +00001072
mpagenkoa2b288f2021-10-21 11:25:27 +00001073 oFsm.mutexAbortRequest.Lock()
1074 //to be sure to abort a possibly still running runSwDlSectionWindow()
1075 // in case the reset was not received from cancel() and download not finished correctly
1076 oFsm.abortRequested = oFsm.volthaDownloadReason
1077 oFsm.mutexAbortRequest.Unlock()
1078
mpagenkoc26d4c02021-05-06 14:27:57 +00001079 // in case the download-to-ONU timer is still running - cancel it
mpagenko39b703e2021-08-25 13:38:40 +00001080 //use non-blocking channel (to be independent from receiver state)
1081 select {
1082 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
1083 case oFsm.chOnuDlReady <- false:
1084 default:
mpagenkoc26d4c02021-05-06 14:27:57 +00001085 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001086 pConfigUpgradeStateAFsm := oFsm.PAdaptFsm
1087 if pConfigUpgradeStateAFsm != nil {
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001088 var nextEvent string
1089 if oFsm.isEndSwDlOpen {
mpagenko45586762021-10-01 08:30:22 +00001090 if oFsm.repeatAbort {
1091 oFsm.delayEndSwDl = true //run next abort with delay
1092 } else { //initial request
1093 oFsm.delayEndSwDl = false //run next abort with no delay
1094 oFsm.waitCountEndSwDl = cWaitCountEndSwDl //init for possible repetitions
1095 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001096 nextEvent = UpgradeEvAbortSwDownload
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001097 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001098 nextEvent = UpgradeEvRestart
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001099 }
1100 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001101 go func(a_pAFsm *cmn.AdapterFsm) {
1102 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1103 _ = a_pAFsm.PFsm.Event(nextEvent)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001104 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001105 }(pConfigUpgradeStateAFsm)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001106 }
1107}
mpagenkoc26d4c02021-05-06 14:27:57 +00001108
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001109func (oFsm *OnuUpgradeFsm) enterAbortingDL(ctx context.Context, e *fsm.Event) {
1110 logger.Debugw(ctx, "OnuUpgradeFsm aborting download to ONU", log.Fields{"device-id": oFsm.deviceID})
1111
mpagenko45586762021-10-01 08:30:22 +00001112 oFsm.mutexUpgradeParams.RLock()
1113 if oFsm.delayEndSwDl {
1114 oFsm.mutexUpgradeParams.RUnlock()
1115 //give the ONU some time for image discard activities
1116 time.Sleep(cOmciEndSwDlDelaySeconds * time.Second)
1117 } else {
1118 oFsm.mutexUpgradeParams.RUnlock()
1119 }
1120
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001121 pBaseFsm := oFsm.PAdaptFsm
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001122 if pBaseFsm == nil {
1123 logger.Errorw(ctx, "OnuUpgradeFsm aborting download: BaseFsm invalid", log.Fields{"device-id": oFsm.deviceID})
1124 return
1125 }
1126 // abort the download operation by sending an end software download message with invalid CRC and image size
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001127 err := oFsm.pOmciCC.SendEndSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
1128 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, 0, 0xFFFFFFFF)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001129
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001130 if err != nil {
1131 logger.Errorw(ctx, "OnuUpgradeFsm aborting download: can't send EndSwDl request", log.Fields{"device-id": oFsm.deviceID})
1132 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001133 go func(a_pAFsm *cmn.AdapterFsm) {
1134 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1135 _ = a_pAFsm.PFsm.Event(UpgradeEvRestart)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001136 }
1137 }(pBaseFsm)
1138 return
1139 }
mpagenkoa2b288f2021-10-21 11:25:27 +00001140
1141 //avoid waiting in the enterXXX function here,
1142 // otherwise synchronous event calls (like from RxMessage processing) may block and block complete Rx processing then
1143 go oFsm.waitOnAbortEndSwDlResponse(ctx)
mpagenko45586762021-10-01 08:30:22 +00001144}
1145
1146//abortingDlEvaluateResponse waits for a channel indication with decision to proceed the FSM processing
1147func (oFsm *OnuUpgradeFsm) abortingDlEvaluateResponse(ctx context.Context,
1148 pBaseFsm *cmn.AdapterFsm, aResponseResult tEndSwDlResponseResult) bool {
1149 switch aResponseResult {
1150 case cEndSwDlResponseBusy: // indication for device busy, needs repetition
1151 if oFsm.waitCountEndSwDl == 0 {
1152 logger.Errorw(ctx, "aborting download: max limit of EndSwDl reached", log.Fields{
1153 "device-id": oFsm.deviceID})
1154 go func(a_pAFsm *cmn.AdapterFsm) {
1155 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1156 _ = a_pAFsm.PFsm.Event(UpgradeEvRestart) //give up and let FSM terminate
1157 }
1158 }(pBaseFsm)
1159 } else {
1160 logger.Debugw(ctx, "aborting download: re-trigger sending abort SwDl", log.Fields{
1161 "device-id": oFsm.deviceID, "counter": oFsm.waitCountEndSwDl})
1162 oFsm.waitCountEndSwDl--
1163 oFsm.repeatAbort = true //repeated request in next round
1164 go func(a_pAFsm *cmn.AdapterFsm) {
1165 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1166 _ = a_pAFsm.PFsm.Event(UpgradeEvReset) //which then re-triggers sending AbortSwDL
1167 }
1168 }(pBaseFsm)
1169 }
1170 return true
1171 case cEndSwDlResponseSuccess: // indication for success response
1172 logger.Infow(ctx, "aborting download: success response, terminating FSM", log.Fields{
1173 "device-id": oFsm.deviceID})
1174 case cEndSwDlResponseAbort: // indication for request to abort waiting for response
1175 logger.Infow(ctx, "aborting download: request to abort waiting, terminating FSM", log.Fields{
1176 "device-id": oFsm.deviceID})
1177 default:
1178 logger.Errorw(ctx, "aborting download: unknown channel indication, terminating FSM", log.Fields{
1179 "device-id": oFsm.deviceID})
1180 } //switch
1181 return false
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001182}
1183
1184func (oFsm *OnuUpgradeFsm) enterRestarting(ctx context.Context, e *fsm.Event) {
1185 logger.Debugw(ctx, "OnuUpgradeFsm restarting", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001186 pConfigUpgradeStateAFsm := oFsm.PAdaptFsm
1187 if pConfigUpgradeStateAFsm != nil {
mpagenko80622a52021-02-09 16:53:23 +00001188 // abort running message processing
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001189 fsmAbortMsg := cmn.Message{
1190 Type: cmn.TestMsg,
1191 Data: cmn.TestMessage{
1192 TestMessageVal: cmn.AbortMessageProcessing,
mpagenko80622a52021-02-09 16:53:23 +00001193 },
1194 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001195 pConfigUpgradeStateAFsm.CommChan <- fsmAbortMsg
mpagenko80622a52021-02-09 16:53:23 +00001196
1197 //try to restart the FSM to 'disabled'
1198 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001199 go func(a_pAFsm *cmn.AdapterFsm) {
1200 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1201 _ = a_pAFsm.PFsm.Event(UpgradeEvDisable)
mpagenko80622a52021-02-09 16:53:23 +00001202 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001203 }(pConfigUpgradeStateAFsm)
mpagenko80622a52021-02-09 16:53:23 +00001204 }
1205}
1206
1207func (oFsm *OnuUpgradeFsm) enterDisabled(ctx context.Context, e *fsm.Event) {
1208 logger.Debugw(ctx, "OnuUpgradeFsm enters disabled state", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001209 // 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 +00001210 if oFsm.pDeviceHandler != nil {
1211 //request removal of 'reference' in the Handler (completely clear the FSM and its data)
mpagenko38662d02021-08-11 09:45:19 +00001212 pLastUpgradeImageState := &voltha.ImageState{
1213 Version: oFsm.imageVersion,
1214 DownloadState: oFsm.volthaDownloadState,
1215 Reason: oFsm.volthaDownloadReason,
1216 ImageState: oFsm.volthaImageState,
1217 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001218 go oFsm.pDeviceHandler.RemoveOnuUpgradeFsm(ctx, pLastUpgradeImageState)
mpagenko80622a52021-02-09 16:53:23 +00001219 }
1220}
1221
1222func (oFsm *OnuUpgradeFsm) processOmciUpgradeMessages(ctx context.Context) { //ctx context.Context?
1223 logger.Debugw(ctx, "Start OnuUpgradeFsm Msg processing", log.Fields{"for device-id": oFsm.deviceID})
1224loop:
1225 for {
1226 // case <-ctx.Done():
1227 // logger.Info(ctx,"MibSync Msg", log.Fields{"Message handling canceled via context for device-id": oFsm.deviceID})
1228 // break loop
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001229 message, ok := <-oFsm.PAdaptFsm.CommChan
mpagenko80622a52021-02-09 16:53:23 +00001230 if !ok {
1231 logger.Info(ctx, "OnuUpgradeFsm Rx Msg - could not read from channel", log.Fields{"device-id": oFsm.deviceID})
1232 // but then we have to ensure a restart of the FSM as well - as exceptional procedure
mpagenko45586762021-10-01 08:30:22 +00001233 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +00001234 break loop
1235 }
1236 logger.Debugw(ctx, "OnuUpgradeFsm Rx Msg", log.Fields{"device-id": oFsm.deviceID})
1237
1238 switch message.Type {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001239 case cmn.TestMsg:
1240 msg, _ := message.Data.(cmn.TestMessage)
1241 if msg.TestMessageVal == cmn.AbortMessageProcessing {
mpagenko80622a52021-02-09 16:53:23 +00001242 logger.Infow(ctx, "OnuUpgradeFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.deviceID})
1243 break loop
1244 }
1245 logger.Warnw(ctx, "OnuUpgradeFsm unknown TestMessage", log.Fields{"device-id": oFsm.deviceID, "MessageVal": msg.TestMessageVal})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001246 case cmn.OMCI:
1247 msg, _ := message.Data.(cmn.OmciMessage)
mpagenko80622a52021-02-09 16:53:23 +00001248 oFsm.handleOmciOnuUpgradeMessage(ctx, msg)
1249 default:
1250 logger.Warn(ctx, "OnuUpgradeFsm Rx unknown message", log.Fields{"device-id": oFsm.deviceID,
1251 "message.Type": message.Type})
1252 }
1253 }
1254 logger.Infow(ctx, "End OnuUpgradeFsm Msg processing", log.Fields{"device-id": oFsm.deviceID})
1255}
1256
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001257func (oFsm *OnuUpgradeFsm) handleOmciOnuUpgradeMessage(ctx context.Context, msg cmn.OmciMessage) {
mpagenko80622a52021-02-09 16:53:23 +00001258 logger.Debugw(ctx, "Rx OMCI OnuUpgradeFsm Msg", log.Fields{"device-id": oFsm.deviceID,
1259 "msgType": msg.OmciMsg.MessageType})
1260
1261 switch msg.OmciMsg.MessageType {
1262 case omci.StartSoftwareDownloadResponseType:
1263 {
mpagenko45586762021-10-01 08:30:22 +00001264 oFsm.handleRxStartSwDownloadResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001265 return
1266 } //StartSoftwareDownloadResponseType
1267 case omci.DownloadSectionResponseType:
1268 {
mpagenko45586762021-10-01 08:30:22 +00001269 oFsm.handleRxSwSectionResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001270 return
mpagenko80622a52021-02-09 16:53:23 +00001271 } //DownloadSectionResponseType
1272 case omci.EndSoftwareDownloadResponseType:
1273 {
mpagenko45586762021-10-01 08:30:22 +00001274 oFsm.handleRxEndSwDownloadResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001275 return
1276 } //EndSoftwareDownloadResponseType
1277 case omci.ActivateSoftwareResponseType:
1278 {
1279 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeActivateSoftwareResponse)
1280 if msgLayer == nil {
1281 logger.Errorw(ctx, "Omci Msg layer could not be detected for ActivateSw",
1282 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001283 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001284 return
1285 }
1286 msgObj, msgOk := msgLayer.(*omci.ActivateSoftwareResponse)
1287 if !msgOk {
1288 logger.Errorw(ctx, "Omci Msg layer could not be assigned for ActivateSw",
1289 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001290 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001291 return
1292 }
1293 logger.Debugw(ctx, "OnuUpgradeFsm ActivateSwResponse data", log.Fields{
1294 "device-id": oFsm.deviceID, "data-fields": msgObj})
1295 if msgObj.Result != me.Success {
1296 logger.Errorw(ctx, "OnuUpgradeFsm ActivateSwResponse result error - later: drive FSM to abort state ?",
1297 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
mpagenko45586762021-10-01 08:30:22 +00001298 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001299 return
1300 }
mpagenko183647c2021-06-08 15:25:04 +00001301 oFsm.mutexUpgradeParams.Lock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001302 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
mpagenko38662d02021-08-11 09:45:19 +00001303 // the image is regarded as active really only after ONU reboot and according indication (ONU down/up procedure)
mpagenko183647c2021-06-08 15:25:04 +00001304 oFsm.mutexUpgradeParams.Unlock()
1305 logger.Infow(ctx, "Expected ActivateSwResponse received",
1306 log.Fields{"device-id": oFsm.deviceID, "commit": oFsm.commitImage})
1307 if oFsm.commitImage {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001308 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvWaitForCommit)
mpagenko183647c2021-06-08 15:25:04 +00001309 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001310 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvActivationDone) // let the FSM wait for external commit request
mpagenko183647c2021-06-08 15:25:04 +00001311 }
mpagenko80622a52021-02-09 16:53:23 +00001312 return
1313 }
mpagenko183647c2021-06-08 15:25:04 +00001314 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +00001315 logger.Errorw(ctx, "OnuUpgradeFsm ActivateSwResponse wrong ME instance: abort",
1316 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
mpagenko45586762021-10-01 08:30:22 +00001317 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001318 return
1319 } //ActivateSoftwareResponseType
mpagenko15ff4a52021-03-02 10:09:20 +00001320 case omci.CommitSoftwareResponseType:
1321 {
1322 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeCommitSoftwareResponse)
1323 if msgLayer == nil {
1324 logger.Errorw(ctx, "Omci Msg layer could not be detected for CommitResponse",
1325 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001326 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001327 return
1328 }
1329 msgObj, msgOk := msgLayer.(*omci.CommitSoftwareResponse)
1330 if !msgOk {
1331 logger.Errorw(ctx, "Omci Msg layer could not be assigned for CommitResponse",
1332 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001333 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001334 return
1335 }
mpagenkobf67a092021-03-17 09:52:28 +00001336 if msgObj.Result != me.Success {
mpagenko15ff4a52021-03-02 10:09:20 +00001337 logger.Errorw(ctx, "OnuUpgradeFsm SwImage CommitResponse result error - later: drive FSM to abort state ?",
1338 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
mpagenko45586762021-10-01 08:30:22 +00001339 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001340 return
mpagenkobf67a092021-03-17 09:52:28 +00001341 }
mpagenkoaa3afe92021-05-21 16:20:58 +00001342 oFsm.mutexUpgradeParams.RLock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001343 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
mpagenkoaa3afe92021-05-21 16:20:58 +00001344 oFsm.mutexUpgradeParams.RUnlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001345 logger.Debugw(ctx, "OnuUpgradeFsm Expected SwImage CommitResponse received", log.Fields{"device-id": oFsm.deviceID})
1346 //verifying committed image
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001347 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvCheckCommitted)
mpagenko15ff4a52021-03-02 10:09:20 +00001348 return
1349 }
mpagenkoaa3afe92021-05-21 16:20:58 +00001350 oFsm.mutexUpgradeParams.RUnlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001351 logger.Errorw(ctx, "OnuUpgradeFsm SwImage CommitResponse wrong ME instance: abort",
1352 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
mpagenko45586762021-10-01 08:30:22 +00001353 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001354 return
1355 } //CommitSoftwareResponseType
1356 case omci.GetResponseType:
1357 {
mpagenko45586762021-10-01 08:30:22 +00001358 oFsm.handleRxSwGetResponse(ctx, msg)
mpagenko15ff4a52021-03-02 10:09:20 +00001359 return
1360 } //GetResponseType
mpagenko80622a52021-02-09 16:53:23 +00001361 default:
1362 {
1363 logger.Errorw(ctx, "Rx OMCI unhandled MsgType",
1364 log.Fields{"omciMsgType": msg.OmciMsg.MessageType, "device-id": oFsm.deviceID})
1365 return
1366 }
1367 }
1368}
1369
mpagenko45586762021-10-01 08:30:22 +00001370func (oFsm *OnuUpgradeFsm) handleRxStartSwDownloadResponse(ctx context.Context, msg cmn.OmciMessage) {
1371 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeStartSoftwareDownloadResponse)
1372 if msgLayer == nil {
1373 logger.Errorw(ctx, "Omci Msg layer could not be detected for StartSwDlResponse",
1374 log.Fields{"device-id": oFsm.deviceID})
1375 oFsm.abortOnOmciError(ctx, false)
1376 return
1377 }
1378 msgObj, msgOk := msgLayer.(*omci.StartSoftwareDownloadResponse)
1379 if !msgOk {
1380 logger.Errorw(ctx, "Omci Msg layer could not be assigned for StartSwDlResponse",
1381 log.Fields{"device-id": oFsm.deviceID})
1382 oFsm.abortOnOmciError(ctx, false)
1383 return
1384 }
1385 logger.Debugw(ctx, "OnuUpgradeFsm StartSwDlResponse data", log.Fields{
1386 "device-id": oFsm.deviceID, "data-fields": msgObj})
1387 if msgObj.Result != me.Success {
1388 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse result error - later: drive FSM to abort state ?",
1389 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1390 oFsm.abortOnOmciError(ctx, false)
1391 return
1392 }
1393
1394 oFsm.mutexUpgradeParams.Lock()
1395 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1396 logger.Debugw(ctx, "Expected StartSwDlResponse received", log.Fields{"device-id": oFsm.deviceID})
1397 if msgObj.WindowSize != oFsm.omciDownloadWindowSizeLimit {
1398 // also response WindowSize = 0 is a valid number for used Window size 1
1399 logger.Debugw(ctx, "different StartSwDlResponse window size requested by ONU", log.Fields{
1400 "acceptedOnuWindowSizeLimit": msgObj.WindowSize, "device-id": oFsm.deviceID})
1401 oFsm.omciDownloadWindowSizeLimit = msgObj.WindowSize
1402 }
1403 oFsm.noOfWindows = oFsm.noOfSections / uint32(oFsm.omciDownloadWindowSizeLimit+1)
1404 if oFsm.noOfSections%uint32(oFsm.omciDownloadWindowSizeLimit+1) > 0 {
1405 oFsm.noOfWindows++
1406 }
1407 logger.Debugw(ctx, "OnuUpgradeFsm will use", log.Fields{
1408 "windows": oFsm.noOfWindows, "sections": oFsm.noOfSections,
1409 "at WindowSizeLimit": oFsm.omciDownloadWindowSizeLimit})
1410 oFsm.nextDownloadSectionsAbsolute = 0
1411 oFsm.nextDownloadSectionsWindow = 0
1412 oFsm.nextDownloadWindow = 0
1413
1414 oFsm.mutexUpgradeParams.Unlock()
1415 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvRxStartSwDownload)
1416 return
1417 }
1418 oFsm.mutexUpgradeParams.Unlock()
1419 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse wrong ME instance: try again (later)?",
1420 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1421 oFsm.abortOnOmciError(ctx, false)
1422} //handleRxStartSwDownloadResponse
1423
1424func (oFsm *OnuUpgradeFsm) handleRxSwSectionResponse(ctx context.Context, msg cmn.OmciMessage) {
mpagenkoa2b288f2021-10-21 11:25:27 +00001425 if oFsm.PAdaptFsm == nil {
1426 logger.Infow(ctx, "DlSectionResponse received - but FSM not really valid anymore", log.Fields{
1427 "device-id": oFsm.deviceID})
1428 return
1429 }
1430 if !oFsm.PAdaptFsm.PFsm.Is(UpgradeStVerifyWindow) {
1431 //all the processing here is only relevant if the FSM is in state upgradeStVerifyWindow
1432 // otherwise this response can be ignored (may stem from a long-processing window send activity,
1433 // which is not anymore relevant based on intermediate (cancel) state transitions)
1434 logger.Infow(ctx, "DlSectionResponse received - but ignored", log.Fields{
1435 "device-id": oFsm.deviceID, "fsm-state": oFsm.PAdaptFsm.PFsm.Current()})
1436 return
1437 }
mpagenko45586762021-10-01 08:30:22 +00001438 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeDownloadSectionResponse)
1439 if msgLayer == nil {
1440 logger.Errorw(ctx, "Omci Msg layer could not be detected for DlSectionResponse",
1441 log.Fields{"device-id": oFsm.deviceID, "omci-message": msg.OmciMsg})
1442 oFsm.abortOnOmciError(ctx, false)
1443 return
1444 }
1445 msgObj, msgOk := msgLayer.(*omci.DownloadSectionResponse)
1446 if !msgOk {
1447 logger.Errorw(ctx, "Omci Msg layer could not be assigned for DlSectionResponse",
1448 log.Fields{"device-id": oFsm.deviceID})
1449 oFsm.abortOnOmciError(ctx, false)
1450 return
1451 }
1452 logger.Debugw(ctx, "OnuUpgradeFsm DlSectionResponse Data", log.Fields{
1453 "device-id": oFsm.deviceID, "data-fields": msgObj})
1454 if msgObj.Result != me.Success {
1455 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse result error - later: repeat window once?", //TODO!!!
1456 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1457 oFsm.abortOnOmciError(ctx, false)
1458 return
1459 }
1460 oFsm.mutexUpgradeParams.Lock()
1461 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1462 sectionNumber := msgObj.SectionNumber
1463 logger.Infow(ctx, "DlSectionResponse received", log.Fields{
1464 "window section-number": sectionNumber, "window": oFsm.nextDownloadWindow, "device-id": oFsm.deviceID})
1465
1466 oFsm.nextDownloadWindow++
1467 if oFsm.nextDownloadWindow >= oFsm.noOfWindows {
1468 if sectionNumber != oFsm.omciDownloadWindowSizeLast {
1469 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse section error last window - later: repeat window once?", //TODO!!!
1470 log.Fields{"device-id": oFsm.deviceID, "actual section": sectionNumber,
1471 "expected section": oFsm.omciDownloadWindowSizeLast})
1472 oFsm.mutexUpgradeParams.Unlock()
1473 oFsm.abortOnOmciError(ctx, false)
1474 return
1475 }
1476 oFsm.delayEndSwDl = true //ensure a delay for the EndSwDl message
1477 //CRC computation for all data bytes of the file
1478 imageCRC := crc32a.Checksum(oFsm.imageBuffer[:int(oFsm.origImageLength)]) //store internal for multiple usage
1479 //revert the retrieved CRC Byte Order (seems not to deliver NetworkByteOrder)
1480 var byteSlice []byte = make([]byte, 4)
1481 binary.LittleEndian.PutUint32(byteSlice, uint32(imageCRC))
1482 oFsm.imageCRC = binary.BigEndian.Uint32(byteSlice)
1483 oFsm.mutexUpgradeParams.Unlock()
1484 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvEndSwDownload)
1485 return
1486 }
1487 if sectionNumber != oFsm.omciDownloadWindowSizeLimit {
1488 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse section error - later: repeat window once?", //TODO!!!
1489 log.Fields{"device-id": oFsm.deviceID, "actual-section": sectionNumber,
1490 "expected section": oFsm.omciDownloadWindowSizeLimit})
1491 oFsm.mutexUpgradeParams.Unlock()
1492 oFsm.abortOnOmciError(ctx, false)
1493 return
1494 }
1495 oFsm.nextDownloadSectionsWindow = 0
1496 oFsm.mutexUpgradeParams.Unlock()
1497 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvContinueNextWindow)
1498 return
1499 }
1500 oFsm.mutexUpgradeParams.Unlock()
1501 logger.Errorw(ctx, "OnuUpgradeFsm Omci StartSwDlResponse wrong ME instance: try again (later)?",
1502 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1503 oFsm.abortOnOmciError(ctx, false)
1504} //handleRxSwSectionResponse
1505
1506func (oFsm *OnuUpgradeFsm) handleRxEndSwDownloadResponse(ctx context.Context, msg cmn.OmciMessage) {
1507 inAbortingState := oFsm.PAdaptFsm.PFsm.Is(UpgradeStAbortingDL)
1508
1509 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeEndSoftwareDownloadResponse)
1510 if msgLayer == nil {
1511 logger.Errorw(ctx, "Omci Msg layer could not be detected for EndSwDlResponse",
1512 log.Fields{"device-id": oFsm.deviceID})
1513 if !inAbortingState {
1514 oFsm.abortOnOmciError(ctx, false)
1515 } //else using error log and wait for another response or 'aborting' state timeout
1516 return
1517 }
1518 msgObj, msgOk := msgLayer.(*omci.EndSoftwareDownloadResponse)
1519 if !msgOk {
1520 logger.Errorw(ctx, "Omci Msg layer could not be assigned for EndSwDlResponse",
1521 log.Fields{"device-id": oFsm.deviceID})
1522 if !inAbortingState {
1523 oFsm.abortOnOmciError(ctx, false)
1524 } //else using error log and wait for another response or 'aborting' state timeout
1525 return
1526 }
1527 logger.Debugw(ctx, "OnuUpgradeFsm EndSwDlResponse data", log.Fields{
1528 "device-id": oFsm.deviceID, "data-fields": msgObj})
1529 if msgObj.Result != me.Success {
1530 if msgObj.Result == me.DeviceBusy {
1531 //ONU indicates it is still processing the image - let the FSM just wait and then repeat the request
1532 logger.Debugw(ctx, "OnuUpgradeFsm EndSwDlResponse busy: waiting before sending new request", log.Fields{
1533 "device-id": oFsm.deviceID})
1534 if inAbortingState {
1535 //if the EndSwDl was requested from state AbortingDL then use channel to indicate ONU busy/repeat indication
1536 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseBusy //repeat abort request
1537 }
mpagenko45586762021-10-01 08:30:22 +00001538 return
1539 }
mpagenko45586762021-10-01 08:30:22 +00001540 if inAbortingState {
1541 //if the EndSwDl was requested from state AbortingDL and response is error indication
mpagenkoa2b288f2021-10-21 11:25:27 +00001542 // that would be quite strange ONU behavior, no resolution from OnuAdapter, just let the FSM go on to disabled
1543 logger.Errorw(ctx, "OnuUpgradeFsm EndSwDlResponse result error - aborting anyway",
1544 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1545 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseAbort //error indication to stop waiting on EndDownloadResponse(abort)
1546 return
1547 }
1548 //else: ONU rejects the previous download, complete upgrade is immediately aborted with error
1549 logger.Errorw(ctx, "OnuUpgradeFsm EndSwDlResponse result error - abort upgrade",
1550 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1551 oFsm.mutexUpgradeParams.Lock()
1552 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
1553 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1554 oFsm.mutexUpgradeParams.Unlock()
1555 select {
1556 case oFsm.chOnuDlReady <- false:
1557 default:
1558 }
1559 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko45586762021-10-01 08:30:22 +00001560 return
1561 }
1562 oFsm.mutexUpgradeParams.Lock()
1563 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1564 logger.Debugw(ctx, "Expected EndSwDlResponse received", log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001565 if inAbortingState {
1566 oFsm.mutexUpgradeParams.Unlock()
1567 //if the EndSwDl was requested from state AbortingDL then use channel to indicate abort acceptance
1568 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseSuccess //success
1569 return
1570 }
1571 if !oFsm.useAPIVersion43 {
1572 //in the older API version the image version check was not possible
1573 // - assume new loaded image as valid-inactive immediately
1574 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_SUCCEEDED
1575 oFsm.volthaImageState = voltha.ImageState_IMAGE_INACTIVE
1576 oFsm.mutexUpgradeParams.Unlock()
mpagenkoa2b288f2021-10-21 11:25:27 +00001577 //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 +00001578 select {
mpagenko45586762021-10-01 08:30:22 +00001579 case oFsm.chOnuDlReady <- true:
1580 default:
1581 }
1582 } else {
1583 oFsm.mutexUpgradeParams.Unlock()
1584 }
1585 //use asynchronous channel sending to let the FSM proceed
1586 select {
1587 case oFsm.chReceiveExpectedResponse <- true:
1588 default:
1589 }
1590 return
1591 }
1592 oFsm.mutexUpgradeParams.Unlock()
1593 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse wrong ME instance: ignoring",
1594 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1595 // no state abort in case of unexpected ImageId, just keep waiting for the correct one
1596} //handleRxEndSwDownloadResponse
1597
1598func (oFsm *OnuUpgradeFsm) handleRxSwGetResponse(ctx context.Context, msg cmn.OmciMessage) {
1599 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeGetResponse)
1600 if msgLayer == nil {
1601 logger.Errorw(ctx, "Omci Msg layer could not be detected for SwImage GetResponse",
1602 log.Fields{"device-id": oFsm.deviceID})
1603 oFsm.abortOnOmciError(ctx, false)
1604 return
1605 }
1606 msgObj, msgOk := msgLayer.(*omci.GetResponse)
1607 if !msgOk {
1608 logger.Errorw(ctx, "Omci Msg layer could not be assigned for SwImage GetResponse",
1609 log.Fields{"device-id": oFsm.deviceID})
1610 oFsm.abortOnOmciError(ctx, false)
1611 return
1612 }
1613 logger.Debugw(ctx, "OnuUpgradeFsm SwImage GetResponse data", log.Fields{
1614 "device-id": oFsm.deviceID, "data-fields": msgObj})
1615 if msgObj.EntityClass == oFsm.pLastTxMeInstance.GetClassID() &&
1616 msgObj.EntityInstance == oFsm.pLastTxMeInstance.GetEntityID() {
1617 if msgObj.Result != me.Success {
1618 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse result error - later: drive FSM to abort state ?",
1619 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1620 oFsm.abortOnOmciError(ctx, false)
1621 return
1622 }
1623 } else {
1624 logger.Warnw(ctx, "OnuUpgradeFsm SwImage unexpected Entity GetResponse data - ignore",
1625 log.Fields{"device-id": oFsm.deviceID})
1626 return
1627 }
1628
1629 meAttributes := msgObj.Attributes
Holger Hildebrandt3ac49bd2022-02-07 17:46:43 +00001630 imageIsCommitted := meAttributes[me.SoftwareImage_IsCommitted].(uint8)
1631 imageIsActive := meAttributes[me.SoftwareImage_IsActive].(uint8)
1632 imageVersion := cmn.TrimStringFromMeOctet(meAttributes[me.SoftwareImage_Version])
mpagenko45586762021-10-01 08:30:22 +00001633 logger.Debugw(ctx, "OnuUpgradeFsm - GetResponse Data for SoftwareImage",
1634 log.Fields{"device-id": oFsm.deviceID, "entityID": msgObj.EntityInstance,
1635 "version": imageVersion, "isActive": imageIsActive, "isCommitted": imageIsCommitted})
1636
1637 if oFsm.PAdaptFsm.PFsm.Current() == UpgradeStCheckImageName {
1638 //image name check after EndSwDownload, this state (and block) can only be taken if APIVersion43 is used
1639 oFsm.verifyOnuSwStatusAfterDownload(ctx, msgObj.EntityInstance, imageVersion, imageIsActive, imageIsCommitted)
1640 return
1641 }
1642
1643 //assumed only relevant state here is upgradeStCheckCommitted
1644 oFsm.mutexUpgradeParams.Lock()
1645 oFsm.conditionalCancelRequested = false //getting here any set (conditional) cancelRequest is not relevant anymore
1646 if msgObj.EntityInstance == oFsm.InactiveImageMeID && imageIsActive == cmn.SwIsActive {
1647 //a check on the delivered image version is not done, the ONU delivered version might be different from what might have been
1648 // indicated in the download image version string (version must be part of the image content itself)
1649 // so checking that might be quite unreliable
1650 //but with new API this was changed, assumption is that omci image version is known at download request and exactly that is used
1651 // in all the API references, so it can and should be checked here now
1652 if oFsm.useAPIVersion43 {
1653 if imageVersion != oFsm.imageVersion {
1654 //new active version indicated on OMCI from ONU is not the expected version
1655 logger.Errorw(ctx, "OnuUpgradeFsm image-version not matching the requested upgrade",
1656 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance,
1657 "onu-version": imageVersion, "expected-version": oFsm.imageVersion})
1658 // TODO!!!: error treatment?
1659 //TODO!!!: possibly send event information for aborted upgrade (aborted by wrong version)?
1660 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE //something like 'UNEXPECTED_VERSION' would be better - proto def
1661 oFsm.mutexUpgradeParams.Unlock()
1662 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1663 return
1664 }
1665 logger.Debugw(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU",
1666 log.Fields{"device-id": oFsm.deviceID})
1667 }
1668 if imageIsCommitted == cmn.SwIsCommitted {
1669 oFsm.upgradePhase = cUpgradeCommitted
1670 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTED
1671 //store the new commit flag to onuSwImageIndications (to keep them in sync)
1672 oFsm.pDevEntry.ModifySwImageActiveCommit(ctx, imageIsCommitted)
1673 logger.Infow(ctx, "requested SW image committed, releasing OnuUpgrade", log.Fields{"device-id": oFsm.deviceID})
1674 //deviceProcStatusUpdate not used anymore,
1675 // replaced by transferring the last (more) upgrade state information within removeOnuUpgradeFsm
1676 oFsm.mutexUpgradeParams.Unlock()
1677 //releasing the upgrade FSM on success
1678 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1679 return
1680 }
1681 //if not committed, abort upgrade as failed. There is no implementation here that would trigger this test again
1682 }
1683 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1684 oFsm.mutexUpgradeParams.Unlock()
1685 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse indications not matching requested upgrade",
1686 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1687 // TODO!!!: error treatment?
1688 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
1689 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1690} //handleRxSwGetResponse
1691
1692func (oFsm *OnuUpgradeFsm) verifyOnuSwStatusAfterDownload(ctx context.Context, aInstanceID uint16,
1693 aImageVersion string, aImageIsActive uint8, aImageIsCommitted uint8) {
1694 oFsm.mutexUpgradeParams.Lock()
1695 if aInstanceID == oFsm.InactiveImageMeID && aImageIsActive == cmn.SwIsInactive &&
1696 aImageIsCommitted == cmn.SwIsUncommitted {
1697 if aImageVersion != oFsm.imageVersion {
1698 //new stored inactive version indicated on OMCI from ONU is not the expected version
1699 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse version indication not matching requested upgrade",
1700 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": aInstanceID,
1701 "onu-version": aImageVersion, "expected-version": oFsm.imageVersion})
1702 //download state is set when entering the reset state
1703 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE //something like 'UNEXPECTED_VERSION' would be better - proto def
1704 oFsm.mutexUpgradeParams.Unlock()
1705 //stop the running ONU download timer
1706 //use non-blocking channel (to be independent from receiver state)
1707 select {
1708 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
1709 case oFsm.chOnuDlReady <- false:
1710 default:
1711 }
1712 // TODO!!!: error treatment?
1713 //TODO!!!: possibly send event information for aborted upgrade (aborted by wrong version)?
1714 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1715 return
1716 }
1717 //with APIVersion43 this is the point to consider the newly loaded image as valid (and inactive)
1718 oFsm.upgradePhase = cUpgradeDownloaded
1719 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_SUCCEEDED
1720 oFsm.volthaImageState = voltha.ImageState_IMAGE_INACTIVE
1721 //store the new inactive version to onuSwImageIndications (to keep them in sync)
1722 oFsm.pDevEntry.ModifySwImageInactiveVersion(ctx, oFsm.imageVersion)
1723 //proceed within upgrade FSM
1724 if oFsm.activateImage {
1725 //immediate activation requested
1726 oFsm.mutexUpgradeParams.Unlock()
1727 logger.Debugw(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU, continue with activation",
1728 log.Fields{"device-id": oFsm.deviceID})
1729 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvRequestActivate)
1730 } else {
1731 //have to wait on explicit activation request
1732 oFsm.mutexUpgradeParams.Unlock()
1733 logger.Infow(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU, wait for activate request",
1734 log.Fields{"device-id": oFsm.deviceID})
1735 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvWaitForActivate)
1736 }
1737 //use non-blocking channel (to be independent from receiver state)
1738 select {
1739 //use non-blocking channel to indicate that the download to ONU was successful
1740 case oFsm.chOnuDlReady <- true:
1741 default:
1742 }
1743 return
1744 }
1745 //not the expected image/image state
1746 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1747 oFsm.mutexUpgradeParams.Unlock()
1748 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse indications not matching requested upgrade",
1749 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": aInstanceID})
1750 // TODO!!!: error treatment?
1751 //TODO!!!: possibly send event information for aborted upgrade (aborted by ONU state indication)?
1752 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1753} //verifyOnuSwStatusAfterDownload
1754
1755//abortOnOmciError aborts the upgrade processing with evAbort
1756// asynchronous/synchronous based on parameter aAsync
1757func (oFsm *OnuUpgradeFsm) abortOnOmciError(ctx context.Context, aAsync bool) {
mpagenko38662d02021-08-11 09:45:19 +00001758 oFsm.mutexUpgradeParams.Lock()
1759 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
mpagenko38662d02021-08-11 09:45:19 +00001760 oFsm.volthaDownloadReason = voltha.ImageState_OMCI_TRANSFER_ERROR
mpagenko38662d02021-08-11 09:45:19 +00001761 oFsm.mutexUpgradeParams.Unlock()
1762 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001763 if oFsm.PAdaptFsm != nil {
mpagenko38662d02021-08-11 09:45:19 +00001764 var err error
1765 if aAsync { //asynchronous call requested to ensure state transition
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001766 go func(a_pAFsm *cmn.AdapterFsm) {
1767 if a_pAFsm.PFsm != nil {
1768 err = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko38662d02021-08-11 09:45:19 +00001769 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001770 }(oFsm.PAdaptFsm)
mpagenko38662d02021-08-11 09:45:19 +00001771 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001772 if oFsm.PAdaptFsm.PFsm != nil {
1773 err = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko38662d02021-08-11 09:45:19 +00001774 }
1775 }
1776 if err != nil {
1777 logger.Warnw(ctx, "onu upgrade fsm could not abort on omci error", log.Fields{
1778 "device-id": oFsm.deviceID, "error": err})
1779 }
1780 }
1781}
1782
mpagenkoc26d4c02021-05-06 14:27:57 +00001783//waitOnDownloadToAdapterReady state can only be reached with useAPIVersion43 (usage of pFileManager)
mpagenko38662d02021-08-11 09:45:19 +00001784// precondition: mutexIsAwaitingAdapterDlResponse is lockek on call
1785func (oFsm *OnuUpgradeFsm) waitOnDownloadToAdapterReady(ctx context.Context, aSyncChannel chan<- struct{},
1786 aWaitChannel chan bool) {
mpagenkoc26d4c02021-05-06 14:27:57 +00001787 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
mpagenko38662d02021-08-11 09:45:19 +00001788 downloadToAdapterTimeout := oFsm.pFileManager.GetDownloadTimeout(ctx)
mpagenkoc26d4c02021-05-06 14:27:57 +00001789 oFsm.isWaitingForAdapterDlResponse = true
1790 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenko38662d02021-08-11 09:45:19 +00001791 aSyncChannel <- struct{}{}
mpagenko80622a52021-02-09 16:53:23 +00001792 select {
1793 // maybe be also some outside cancel (but no context modeled for the moment ...)
1794 // case <-ctx.Done():
mpagenkoc26d4c02021-05-06 14:27:57 +00001795 // logger.Infow("OnuUpgradeFsm-waitOnDownloadToAdapterReady canceled", log.Fields{"for device-id": oFsm.deviceID})
1796 case <-time.After(downloadToAdapterTimeout): //10s should be enough for downloading some image to the adapter
1797 logger.Warnw(ctx, "OnuUpgradeFsm Waiting-adapter-download timeout", log.Fields{
1798 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier, "timeout": downloadToAdapterTimeout})
1799 oFsm.pFileManager.RemoveReadyRequest(ctx, oFsm.imageIdentifier, aWaitChannel)
mpagenko39b703e2021-08-25 13:38:40 +00001800 //running into timeout here may still have the download to adapter active -> abort
1801 oFsm.pFileManager.CancelDownload(ctx, oFsm.imageIdentifier)
mpagenkoc26d4c02021-05-06 14:27:57 +00001802 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1803 oFsm.isWaitingForAdapterDlResponse = false
1804 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenko39b703e2021-08-25 13:38:40 +00001805 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +00001806 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
mpagenko39b703e2021-08-25 13:38:40 +00001807 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'DOWNLOAD_TO_ADAPTER_TIMEOUT' would be better (proto)
mpagenko39b703e2021-08-25 13:38:40 +00001808 oFsm.mutexUpgradeParams.Unlock()
1809 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001810 if oFsm.PAdaptFsm != nil && oFsm.PAdaptFsm.PFsm != nil {
1811 err := oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko39b703e2021-08-25 13:38:40 +00001812 if err != nil {
1813 logger.Warnw(ctx, "onu upgrade fsm could not abort on omci error", log.Fields{
1814 "device-id": oFsm.deviceID, "error": err})
1815 }
1816 }
mpagenkoc26d4c02021-05-06 14:27:57 +00001817 return
1818
1819 case success := <-aWaitChannel:
1820 if success {
1821 logger.Debugw(ctx, "OnuUpgradeFsm image-downloaded received", log.Fields{"device-id": oFsm.deviceID})
1822 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1823 oFsm.isWaitingForAdapterDlResponse = false
1824 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
1825 //let the upgrade process proceed
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001826 pUpgradeFsm := oFsm.PAdaptFsm
mpagenkoc26d4c02021-05-06 14:27:57 +00001827 if pUpgradeFsm != nil {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001828 _ = pUpgradeFsm.PFsm.Event(UpgradeEvPrepareSwDownload)
mpagenkoc26d4c02021-05-06 14:27:57 +00001829 } else {
1830 logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
1831 }
1832 return
1833 }
mpagenko39b703e2021-08-25 13:38:40 +00001834 // waiting was aborted (assumed here to be caused by
1835 // error detection or cancel at download after upgrade FSM reset/abort with according image states set there)
mpagenkoc26d4c02021-05-06 14:27:57 +00001836 logger.Debugw(ctx, "OnuUpgradeFsm Waiting-adapter-download aborted", log.Fields{"device-id": oFsm.deviceID})
1837 oFsm.pFileManager.RemoveReadyRequest(ctx, oFsm.imageIdentifier, aWaitChannel)
1838 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1839 oFsm.isWaitingForAdapterDlResponse = false
1840 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenkoc26d4c02021-05-06 14:27:57 +00001841 return
mpagenko80622a52021-02-09 16:53:23 +00001842 }
1843}
mpagenkoc26d4c02021-05-06 14:27:57 +00001844
1845//waitOnDownloadToOnuReady state can only be reached with useAPIVersion43 (usage of pFileManager)
1846func (oFsm *OnuUpgradeFsm) waitOnDownloadToOnuReady(ctx context.Context, aWaitChannel chan bool) {
1847 downloadToOnuTimeout := time.Duration(1+(oFsm.imageLength/0x400000)) * oFsm.downloadToOnuTimeout4MB
1848 logger.Debugw(ctx, "OnuUpgradeFsm start download-to-ONU timer", log.Fields{"device-id": oFsm.deviceID,
1849 "duration": downloadToOnuTimeout})
mpagenkoc26d4c02021-05-06 14:27:57 +00001850 select {
1851 // maybe be also some outside cancel (but no context modeled for the moment ...)
1852 // case <-ctx.Done():
1853 // logger.Infow("OnuUpgradeFsm-waitOnDownloadToOnuReady canceled", log.Fields{"for device-id": oFsm.deviceID})
1854 case <-time.After(downloadToOnuTimeout): //using an image-size depending timout (in minutes)
1855 logger.Warnw(ctx, "OnuUpgradeFsm Waiting-ONU-download timeout", log.Fields{
1856 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier, "timeout": downloadToOnuTimeout})
mpagenkoc26d4c02021-05-06 14:27:57 +00001857 //the upgrade process has to be aborted
mpagenko45586762021-10-01 08:30:22 +00001858 oFsm.abortOnOmciError(ctx, false)
mpagenkoc26d4c02021-05-06 14:27:57 +00001859 return
1860
1861 case success := <-aWaitChannel:
1862 if success {
1863 logger.Debugw(ctx, "OnuUpgradeFsm image-downloaded on ONU received", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001864 //all fine, let the FSM proceed like defined from the sender of this event
1865 return
1866 }
1867 // waiting was aborted (assumed here to be caused by
mpagenko39b703e2021-08-25 13:38:40 +00001868 // error detection or cancel at download after upgrade FSM reset/abort with according image states set there)
mpagenkoc26d4c02021-05-06 14:27:57 +00001869 logger.Debugw(ctx, "OnuUpgradeFsm Waiting-ONU-download aborted", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001870 return
1871 }
1872}
mpagenko45586762021-10-01 08:30:22 +00001873
mpagenkoa2b288f2021-10-21 11:25:27 +00001874//waitOnAbortEndSwDlResponse waits for either abort/success or timeout of EndSwDownload (for abortion)
1875func (oFsm *OnuUpgradeFsm) waitOnAbortEndSwDlResponse(ctx context.Context) {
1876 logger.Debugw(ctx, "OnuUpgradeFsm start wait for EndSwDl response (abort)", log.Fields{"device-id": oFsm.deviceID})
1877 select {
1878 case <-time.After(oFsm.pOmciCC.GetMaxOmciTimeoutWithRetries() * time.Second):
1879 logger.Warnw(ctx, "OnuUpgradeFsm aborting download: timeout - no response received", log.Fields{"device-id": oFsm.deviceID})
1880 pUpgradeFsm := oFsm.PAdaptFsm
1881 if pUpgradeFsm != nil {
1882 _ = pUpgradeFsm.PFsm.Event(UpgradeEvRestart)
1883 } else {
1884 logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
1885 }
1886 return
1887 case response := <-oFsm.chReceiveAbortEndSwDlResponse:
1888 logger.Debugw(ctx, "OnuUpgradeFsm aborting download: response received",
1889 log.Fields{"device-id": oFsm.deviceID, "response": response})
1890 pUpgradeFsm := oFsm.PAdaptFsm
1891 if pUpgradeFsm != nil {
1892 if oFsm.abortingDlEvaluateResponse(ctx, pUpgradeFsm, response) {
1893 return //event sent from function already
1894 }
1895 _ = pUpgradeFsm.PFsm.Event(UpgradeEvRestart)
1896 } else {
1897 logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
1898 }
1899 return
1900 } //select
1901}
1902
mpagenko45586762021-10-01 08:30:22 +00001903//stateUpdateOnReset writes the download and/or image state on entering the reset state according to FSM internal indications
1904func (oFsm *OnuUpgradeFsm) stateUpdateOnReset(ctx context.Context) {
1905 oFsm.mutexUpgradeParams.Lock()
1906 defer oFsm.mutexUpgradeParams.Unlock()
1907 if !oFsm.conditionalCancelRequested {
1908 switch oFsm.upgradePhase {
1909 case cUpgradeUndefined, cUpgradeDownloading: //coming from downloading
1910 //make sure the download state is only changed in case the device has still been downloading
1911 if oFsm.volthaDownloadReason == voltha.ImageState_CANCELLED_ON_REQUEST {
1912 // indication for termination on request
1913 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_CANCELLED
1914 } else if oFsm.volthaDownloadReason != voltha.ImageState_NO_ERROR {
1915 // indication for termination on failure
1916 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_FAILED
1917 }
1918 //reset the image state from Downloading in this case
1919 oFsm.volthaImageState = voltha.ImageState_IMAGE_UNKNOWN //something like 'IMAGE_DOWNLOAD_ABORTED' would be better (proto)
1920 //in all other upgrade phases the last set download state remains valid
1921 case cUpgradeActivating:
1922 //reset the image state from Activating in this case
1923 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATION_ABORTED
1924 case cUpgradeCommitting: // indication for request to abort waiting for response
1925 //reset the image state from Activating in this case
1926 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMIT_ABORTED
1927 //default: in all other upgrade phases keep the last set imageState
1928 } //switch
1929 } else {
1930 //when reaching reset state with conditional cancel that can only result from ONU related problems
1931 // (mostly ONU down indication) - derived from resetFsms call
1932 // and it can only be related to the downloading-to-ONU phase (no need to check that additionally)
1933 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_FAILED
1934 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1935 //reset the image state from Downloading in this case
1936 oFsm.volthaImageState = voltha.ImageState_IMAGE_UNKNOWN //something like 'IMAGE_DOWNLOAD_ABORTED' would be better (proto)
1937 }
1938}
Holger Hildebrandte7cc6092022-02-01 11:37:03 +00001939
1940// PrepareForGarbageCollection - remove references to prepare for garbage collection
1941func (oFsm *OnuUpgradeFsm) PrepareForGarbageCollection(ctx context.Context, aDeviceID string) {
1942 logger.Debugw(ctx, "prepare for garbage collection", log.Fields{"device-id": aDeviceID})
1943 oFsm.pDeviceHandler = nil
1944 oFsm.pDownloadManager = nil
1945 oFsm.pFileManager = nil
1946 oFsm.pDevEntry = nil
1947 oFsm.pOmciCC = nil
1948}