blob: 481f349ba2caaadf2862b33f6f181d8432d1139d [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
Holger Hildebrandt12609a12022-03-25 13:23:25 +0000534 logger.Debugw(ctx, "CancelProcessing entered", log.Fields{"device-id": oFsm.deviceID})
mpagenkoa2b288f2021-10-21 11:25:27 +0000535 if pAdaptFsm == nil || pAdaptFsm.PFsm == nil {
536 logger.Warnw(ctx, "OnuUpgradeFsm cancel, but FSM invalid", log.Fields{
537 "device-id": oFsm.deviceID})
538 return
539 }
540 logger.Debugw(ctx, "OnuUpgradeFsm start canceling", log.Fields{
541 "device-id": oFsm.deviceID, "in fsm-state": pAdaptFsm.PFsm.Current()})
mpagenko38662d02021-08-11 09:45:19 +0000542 oFsm.mutexAbortRequest.Lock()
543 oFsm.abortRequested = aReason //possibly abort the sectionDownload loop
544 oFsm.mutexAbortRequest.Unlock()
mpagenkoc26d4c02021-05-06 14:27:57 +0000545 //mutex protection is required for possible concurrent access to FSM members
546 //attention: for an unbuffered channel the sender is blocked until the value is received (processed)!
547 // accordingly the mutex must be released before sending to channel here (mutex acquired in receiver)
548 oFsm.mutexIsAwaitingAdapterDlResponse.RLock()
549 if oFsm.isWaitingForAdapterDlResponse {
550 oFsm.mutexIsAwaitingAdapterDlResponse.RUnlock()
551 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
552 oFsm.chAdapterDlReady <- false
553 } else {
554 oFsm.mutexIsAwaitingAdapterDlResponse.RUnlock()
555 }
mpagenko609a69a2021-12-02 08:12:58 +0000556
557 //abort a possible delaying of sending EndSwDl
558 //use asynchronous channel sending to avoid blocking here in case no receiver is waiting
559 select {
560 case oFsm.chAbortDelayEndSwDl <- struct{}{}:
561 default:
562 }
563
mpagenkoc26d4c02021-05-06 14:27:57 +0000564 //chOnuDlReady is cleared as part of the FSM reset processing (from enterResetting())
565
566 // 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 +0000567 // specific here: See definition of state changes: some states are excluded from reset for possible later commit
mpagenkoa2b288f2021-10-21 11:25:27 +0000568 pAdaptFsm = oFsm.PAdaptFsm
mpagenko59862f02021-10-11 08:53:18 +0000569 if pAdaptFsm != nil && pAdaptFsm.PFsm != nil {
mpagenkoc26d4c02021-05-06 14:27:57 +0000570 // calling FSM events in background to avoid blocking of the caller
mpagenko59862f02021-10-11 08:53:18 +0000571 go func(apFsm *fsm.FSM) {
572 if apFsm.Is(UpgradeStWaitEndDL) {
573 oFsm.chReceiveExpectedResponse <- false //which aborts the FSM in WaitEndDL state
574 } else if apFsm.Is(UpgradeStAbortingDL) {
575 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseAbort //abort waiting on EndDownloadResponse
576 }
mpagenko38662d02021-08-11 09:45:19 +0000577
mpagenko59862f02021-10-11 08:53:18 +0000578 var err error
579 if abCompleteAbort {
580 // in case of unconditional abort request the ImageState is set immediately
581 oFsm.mutexUpgradeParams.Lock()
582 //any previous lingering conditional cancelRequest is superseded by this abortion
583 oFsm.conditionalCancelRequested = false
584 oFsm.volthaDownloadReason = aReason
585 oFsm.mutexUpgradeParams.Unlock()
586 err = apFsm.Event(UpgradeEvAbort) //as unconditional default FSM cancellation
587 } else {
588 //at conditional abort request the image states are set when reaching the reset state
mpagenkoa2b288f2021-10-21 11:25:27 +0000589 oFsm.mutexUpgradeParams.Lock()
mpagenko59862f02021-10-11 08:53:18 +0000590 oFsm.conditionalCancelRequested = true
mpagenkoa2b288f2021-10-21 11:25:27 +0000591 oFsm.mutexUpgradeParams.Unlock()
mpagenko59862f02021-10-11 08:53:18 +0000592 err = apFsm.Event(UpgradeEvReset) //as state-conditional default FSM cleanup
593 }
594 if err != nil {
595 //error return is expected in case of conditional request and no state transition
596 logger.Debugw(ctx, "onu upgrade fsm could not cancel with abort/reset event", log.Fields{
597 "device-id": oFsm.deviceID, "error": err})
598 }
mpagenkoa2b288f2021-10-21 11:25:27 +0000599 logger.Debugw(ctx, "OnuUpgradeFsm canceling done", log.Fields{
600 "device-id": oFsm.deviceID})
mpagenko59862f02021-10-11 08:53:18 +0000601 }(pAdaptFsm.PFsm)
mpagenkoa2b288f2021-10-21 11:25:27 +0000602 } else { //the FSM seems already to be in some released state
603 logger.Warnw(ctx, "OnuUpgradeFsm canceling without FSM event", log.Fields{
604 "device-id": oFsm.deviceID})
605 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000606}
607
mpagenko80622a52021-02-09 16:53:23 +0000608func (oFsm *OnuUpgradeFsm) enterStarting(ctx context.Context, e *fsm.Event) {
609 logger.Debugw(ctx, "OnuUpgradeFsm start", log.Fields{"in state": e.FSM.Current(),
610 "device-id": oFsm.deviceID})
611
612 // start go routine for processing of LockState messages
613 go oFsm.processOmciUpgradeMessages(ctx)
614}
615
mpagenkoc26d4c02021-05-06 14:27:57 +0000616//enterWaitingAdapterDL state can only be reached with useAPIVersion43
617func (oFsm *OnuUpgradeFsm) enterWaitingAdapterDL(ctx context.Context, e *fsm.Event) {
618 logger.Debugw(ctx, "OnuUpgradeFsm waiting for adapter download", log.Fields{"in state": e.FSM.Current(),
619 "device-id": oFsm.deviceID})
mpagenko38662d02021-08-11 09:45:19 +0000620 syncChannel := make(chan struct{})
621 go oFsm.waitOnDownloadToAdapterReady(ctx, syncChannel, oFsm.chAdapterDlReady)
622 //block until the wait routine is really blocked on chAdapterDlReady
623 <-syncChannel
mpagenkoc26d4c02021-05-06 14:27:57 +0000624 go oFsm.pFileManager.RequestDownloadReady(ctx, oFsm.imageIdentifier, oFsm.chAdapterDlReady)
625}
626
mpagenko80622a52021-02-09 16:53:23 +0000627func (oFsm *OnuUpgradeFsm) enterPreparingDL(ctx context.Context, e *fsm.Event) {
628 logger.Debugw(ctx, "OnuUpgradeFsm prepare Download to Onu", log.Fields{"in state": e.FSM.Current(),
629 "device-id": oFsm.deviceID})
630
mpagenkoc26d4c02021-05-06 14:27:57 +0000631 var fileLen int64
632 var err error
mpagenkoaa3afe92021-05-21 16:20:58 +0000633 oFsm.mutexUpgradeParams.Lock()
mpagenkoc26d4c02021-05-06 14:27:57 +0000634 if oFsm.useAPIVersion43 {
635 //with the new API structure download to adapter is implicit and we have to wait until the image is available
636 fileLen, err = oFsm.pFileManager.GetImageBufferLen(ctx, oFsm.imageIdentifier)
637 } else {
638 fileLen, err = oFsm.pDownloadManager.getImageBufferLen(ctx, oFsm.pImageDsc.Name, oFsm.pImageDsc.LocalDir)
639 }
mpagenkoc497ee32021-11-10 17:30:20 +0000640 if err != nil || fileLen == 0 || fileLen > int64(cMaxUint32) {
mpagenko39b703e2021-08-25 13:38:40 +0000641 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto)
mpagenkoaa3afe92021-05-21 16:20:58 +0000642 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000643 logger.Errorw(ctx, "OnuUpgradeFsm abort: problems getting image buffer length", log.Fields{
644 "device-id": oFsm.deviceID, "error": err, "length": fileLen})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000645 pBaseFsm := oFsm.PAdaptFsm
mpagenko80622a52021-02-09 16:53:23 +0000646 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000647 go func(a_pAFsm *cmn.AdapterFsm) {
648 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko80622a52021-02-09 16:53:23 +0000649 }(pBaseFsm)
650 return
651 }
652
mpagenkoc26d4c02021-05-06 14:27:57 +0000653 //copy file content to buffer
mpagenkoc497ee32021-11-10 17:30:20 +0000654 var imageBuffer []byte
mpagenkoc26d4c02021-05-06 14:27:57 +0000655 if oFsm.useAPIVersion43 {
mpagenkoc497ee32021-11-10 17:30:20 +0000656 imageBuffer, err = oFsm.pFileManager.GetDownloadImageBuffer(ctx, oFsm.imageIdentifier)
mpagenkoc26d4c02021-05-06 14:27:57 +0000657 } else {
mpagenkoc497ee32021-11-10 17:30:20 +0000658 imageBuffer, err = oFsm.pDownloadManager.getDownloadImageBuffer(ctx, oFsm.pImageDsc.Name, oFsm.pImageDsc.LocalDir)
mpagenkoc26d4c02021-05-06 14:27:57 +0000659 }
mpagenko80622a52021-02-09 16:53:23 +0000660 if err != nil {
mpagenko39b703e2021-08-25 13:38:40 +0000661 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto)
mpagenkoaa3afe92021-05-21 16:20:58 +0000662 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000663 logger.Errorw(ctx, "OnuUpgradeFsm abort: can't get image buffer", log.Fields{
664 "device-id": oFsm.deviceID, "error": err})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000665 pBaseFsm := oFsm.PAdaptFsm
mpagenko80622a52021-02-09 16:53:23 +0000666 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000667 go func(a_pAFsm *cmn.AdapterFsm) {
668 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko80622a52021-02-09 16:53:23 +0000669 }(pBaseFsm)
670 return
671 }
mpagenkoc497ee32021-11-10 17:30:20 +0000672 //provide slice capacity already with the reserve of one section to avoid inflation of the slice to double size at append
673 oFsm.imageBuffer = make([]byte, fileLen, fileLen+cOmciDownloadSectionSize)
674 //better use a copy of the read image buffer in case the buffer/file is modified from outside,
675 // this also limits the slice len to the expected maximum fileLen
676 copy(oFsm.imageBuffer, imageBuffer)
mpagenko80622a52021-02-09 16:53:23 +0000677
678 oFsm.noOfSections = uint32(fileLen / cOmciDownloadSectionSize)
679 if fileLen%cOmciDownloadSectionSize > 0 {
mpagenkoc26d4c02021-05-06 14:27:57 +0000680 bufferPadding := make([]byte, cOmciDownloadSectionSize-uint32((fileLen)%cOmciDownloadSectionSize))
mpagenko80622a52021-02-09 16:53:23 +0000681 //expand the imageBuffer to exactly fit multiples of cOmciDownloadSectionSize with padding
mpagenkoc497ee32021-11-10 17:30:20 +0000682 oFsm.imageBuffer = append(oFsm.imageBuffer, bufferPadding...)
mpagenko80622a52021-02-09 16:53:23 +0000683 oFsm.noOfSections++
684 }
685 oFsm.origImageLength = uint32(fileLen)
686 oFsm.imageLength = uint32(len(oFsm.imageBuffer))
mpagenko80622a52021-02-09 16:53:23 +0000687 logger.Infow(ctx, "OnuUpgradeFsm starts with StartSwDl values", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000688 "MeId": oFsm.InactiveImageMeID, "windowSizeLimit": oFsm.omciDownloadWindowSizeLimit,
mpagenko80622a52021-02-09 16:53:23 +0000689 "ImageSize": oFsm.imageLength, "original file size": fileLen})
690 //"NumberOfCircuitPacks": oFsm.numberCircuitPacks, "CircuitPacks MeId": 0}) //parallel circuit packs download not supported
mpagenkoaa3afe92021-05-21 16:20:58 +0000691
692 oFsm.mutexUpgradeParams.Unlock()
mpagenko39b703e2021-08-25 13:38:40 +0000693
mpagenkoa2b288f2021-10-21 11:25:27 +0000694 // flush chOnuDlReady
mpagenko39b703e2021-08-25 13:38:40 +0000695 select {
696 case <-oFsm.chOnuDlReady:
697 logger.Debug(ctx, "flushed OnuDlReady channel")
698 default:
699 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000700 go oFsm.waitOnDownloadToOnuReady(ctx, oFsm.chOnuDlReady) // start supervision of the complete download-to-ONU procedure
701
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000702 err = oFsm.pOmciCC.SendStartSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
703 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, oFsm.omciDownloadWindowSizeLimit, oFsm.origImageLength)
mpagenko80622a52021-02-09 16:53:23 +0000704 if err != nil {
705 logger.Errorw(ctx, "StartSwDl abort: can't send section", log.Fields{
706 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +0000707 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +0000708 return
709 }
Holger Hildebrandt288d9472021-09-06 10:47:53 +0000710 oFsm.isEndSwDlOpen = true
mpagenko80622a52021-02-09 16:53:23 +0000711}
712
713func (oFsm *OnuUpgradeFsm) enterDownloadSection(ctx context.Context, e *fsm.Event) {
714 logger.Debugw(ctx, "OnuUpgradeFsm start downloading sections", log.Fields{
715 "device-id": oFsm.deviceID, "absolute window": oFsm.nextDownloadWindow})
mpagenkoa2b288f2021-10-21 11:25:27 +0000716 //use a background routine to send the multiple download sections frames in a loop
mpagenko609a69a2021-12-02 08:12:58 +0000717 // in order to avoid blocking on synchronous event calls for the entire (long) processing time
mpagenkoa2b288f2021-10-21 11:25:27 +0000718 go oFsm.runSwDlSectionWindow(ctx)
719}
mpagenko80622a52021-02-09 16:53:23 +0000720
mpagenkoa2b288f2021-10-21 11:25:27 +0000721//runSwDlSectionWindow runs a loop to send all DlSection frames of one window in background
722// may be aborted by parallel change of abortRequested
723func (oFsm *OnuUpgradeFsm) runSwDlSectionWindow(ctx context.Context) {
mpagenko80622a52021-02-09 16:53:23 +0000724 var windowAckRequest uint8 = 0
725 var bufferStartOffset uint32
726 var bufferEndOffset uint32
727 var downloadSection []byte
kesavand011d5162021-11-25 19:21:06 +0530728
mpagenkoaa3afe92021-05-21 16:20:58 +0000729 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +0000730 oFsm.upgradePhase = cUpgradeDownloading //start of downloading image to ONU
mpagenko80622a52021-02-09 16:53:23 +0000731 if oFsm.nextDownloadSectionsAbsolute == 0 {
mpagenkoaa3afe92021-05-21 16:20:58 +0000732 oFsm.volthaImageState = voltha.ImageState_IMAGE_DOWNLOADING
mpagenko80622a52021-02-09 16:53:23 +0000733 }
kesavand011d5162021-11-25 19:21:06 +0530734 //var omuTxSecPerWindow []*common.OmciTransferStructure
735 omciMsgsPerSection := &ia.OmciMessages{}
736 //take the TID mutex
737 //To ensure the insequesnce of TIDS for all the onusw sections within a window
738 oFsm.pOmciCC.LockMutexTID()
739 defer oFsm.pOmciCC.UnLockMutexTID()
mpagenko80622a52021-02-09 16:53:23 +0000740 for {
mpagenko38662d02021-08-11 09:45:19 +0000741 oFsm.mutexAbortRequest.RLock()
742 // this way out of the section download loop on abort request
743 if oFsm.abortRequested != voltha.ImageState_NO_ERROR {
mpagenko45586762021-10-01 08:30:22 +0000744 //states are updated when entering the reset state ...
mpagenko38662d02021-08-11 09:45:19 +0000745 oFsm.volthaDownloadReason = oFsm.abortRequested
746 oFsm.mutexAbortRequest.RUnlock()
747 oFsm.mutexUpgradeParams.Unlock()
mpagenkoa2b288f2021-10-21 11:25:27 +0000748 pUpgradeFsm := oFsm.PAdaptFsm
749 if pUpgradeFsm != nil {
750 _ = pUpgradeFsm.PFsm.Event(UpgradeEvAbort)
751 logger.Debugw(ctx, "aborting runSwDlSectionWindow", log.Fields{
752 "device-id": oFsm.deviceID, "reason": oFsm.volthaDownloadReason})
753 return
754 }
755 logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
mpagenko38662d02021-08-11 09:45:19 +0000756 return
757 }
758 oFsm.mutexAbortRequest.RUnlock()
759
mpagenko80622a52021-02-09 16:53:23 +0000760 bufferStartOffset = oFsm.nextDownloadSectionsAbsolute * cOmciDownloadSectionSize
761 bufferEndOffset = bufferStartOffset + cOmciDownloadSectionSize - 1 //for representing cOmciDownloadSectionSizeLimit values
762 logger.Debugw(ctx, "DlSection values are", log.Fields{
763 "DlSectionNoAbsolute": oFsm.nextDownloadSectionsAbsolute,
764 "DlSectionWindow": oFsm.nextDownloadSectionsWindow,
765 "startOffset": bufferStartOffset, "endOffset": bufferEndOffset})
766 if bufferStartOffset+1 > oFsm.imageLength || bufferEndOffset+1 > oFsm.imageLength { //should never occur in this state
767 logger.Errorw(ctx, "OnuUpgradeFsm buffer error: exceeded length", log.Fields{
768 "device-id": oFsm.deviceID, "bufferStartOffset": bufferStartOffset,
769 "bufferEndOffset": bufferEndOffset, "imageLength": oFsm.imageLength})
mpagenko39b703e2021-08-25 13:38:40 +0000770 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto)
mpagenkoaa3afe92021-05-21 16:20:58 +0000771 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000772 //logical error -- reset the FSM
mpagenkoa2b288f2021-10-21 11:25:27 +0000773 pUpgradeFsm := oFsm.PAdaptFsm
774 if pUpgradeFsm != nil {
775 _ = pUpgradeFsm.PFsm.Event(UpgradeEvAbort)
776 return
777 }
778 logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
mpagenko80622a52021-02-09 16:53:23 +0000779 return
780 }
781 downloadSection = oFsm.imageBuffer[bufferStartOffset : bufferEndOffset+1]
782 if oFsm.nextDownloadSectionsWindow == oFsm.omciDownloadWindowSizeLimit {
783 windowAckRequest = 1
784 logger.Debugw(ctx, "DlSection expect Response for complete window", log.Fields{
785 "device-id": oFsm.deviceID, "in window": oFsm.nextDownloadWindow})
786 }
787 if oFsm.nextDownloadSectionsAbsolute+1 >= oFsm.noOfSections {
788 windowAckRequest = 1
kesavand011d5162021-11-25 19:21:06 +0530789
mpagenko15ff4a52021-03-02 10:09:20 +0000790 oFsm.omciDownloadWindowSizeLast = oFsm.nextDownloadSectionsWindow
791 logger.Infow(ctx, "DlSection expect Response for last window (section)", log.Fields{
mpagenko80622a52021-02-09 16:53:23 +0000792 "device-id": oFsm.deviceID, "DlSectionNoAbsolute": oFsm.nextDownloadSectionsAbsolute})
793 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000794 oFsm.mutexUpgradeParams.Unlock() //unlock here to give other functions some chance to process during/after the send request
kesavand011d5162021-11-25 19:21:06 +0530795 omciTxReq, err := oFsm.pOmciCC.PrepareOnuSectionsOfWindow(log.WithSpanFromContext(context.Background(), ctx),
796 oFsm.InactiveImageMeID, windowAckRequest, oFsm.nextDownloadSectionsWindow, downloadSection, omciMsgsPerSection)
mpagenko80622a52021-02-09 16:53:23 +0000797 if err != nil {
798 logger.Errorw(ctx, "DlSection abort: can't send section", log.Fields{
mpagenko15ff4a52021-03-02 10:09:20 +0000799 "device-id": oFsm.deviceID, "section absolute": oFsm.nextDownloadSectionsAbsolute, "error": err})
mpagenkoa2b288f2021-10-21 11:25:27 +0000800 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +0000801 return
802 }
kesavand011d5162021-11-25 19:21:06 +0530803
mpagenkoaa3afe92021-05-21 16:20:58 +0000804 oFsm.mutexUpgradeParams.Lock()
mpagenko80622a52021-02-09 16:53:23 +0000805 oFsm.nextDownloadSectionsAbsolute++ //always increase the absolute section counter after having sent one
806 if windowAckRequest == 1 {
mpagenkoaa3afe92021-05-21 16:20:58 +0000807 oFsm.mutexUpgradeParams.Unlock()
kesavand011d5162021-11-25 19:21:06 +0530808
809 if omciTxReq.OnuSwWindow == nil {
810 logger.Errorw(ctx, "fail to send sections in a window", log.Fields{
811 "device-id": oFsm.deviceID, "section absolute": oFsm.nextDownloadSectionsAbsolute, "error": err})
812 oFsm.abortOnOmciError(ctx, false)
813 return
814 }
815
mpagenkoa2b288f2021-10-21 11:25:27 +0000816 pUpgradeFsm := oFsm.PAdaptFsm
817 if pUpgradeFsm != nil {
818 _ = pUpgradeFsm.PFsm.Event(UpgradeEvWaitWindowAck) //state transition to upgradeStVerifyWindow
kesavand011d5162021-11-25 19:21:06 +0530819 oFsm.pOmciCC.SendOnuSwSectionsWindowWithRxSupervision(ctx, omciTxReq, oFsm.pDeviceHandler.GetOmciTimeout(), oFsm.PAdaptFsm.CommChan)
mpagenkoa2b288f2021-10-21 11:25:27 +0000820 return
821 }
822 logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
mpagenko80622a52021-02-09 16:53:23 +0000823 return
824 }
kesavand011d5162021-11-25 19:21:06 +0530825
mpagenko80622a52021-02-09 16:53:23 +0000826 oFsm.nextDownloadSectionsWindow++ //increase the window related section counter only if not in the last section
mpagenko59498c12021-03-18 14:15:15 +0000827 if oFsm.omciSectionInterleaveDelay > 0 {
mpagenko80622a52021-02-09 16:53:23 +0000828 //ensure a defined intersection-time-gap to leave space for further processing, other ONU's ...
mpagenkoaa3afe92021-05-21 16:20:58 +0000829 oFsm.mutexUpgradeParams.Unlock() //unlock here to give other functions some chance to process during/after the send request
mpagenko59498c12021-03-18 14:15:15 +0000830 time.Sleep(oFsm.omciSectionInterleaveDelay * time.Millisecond)
mpagenkoaa3afe92021-05-21 16:20:58 +0000831 oFsm.mutexUpgradeParams.Lock()
mpagenko80622a52021-02-09 16:53:23 +0000832 }
833 }
mpagenkoa2b288f2021-10-21 11:25:27 +0000834} //runSwDlSectionWindow
mpagenko80622a52021-02-09 16:53:23 +0000835
836func (oFsm *OnuUpgradeFsm) enterVerifyWindow(ctx context.Context, e *fsm.Event) {
837 logger.Debugw(ctx, "OnuUpgradeFsm verify DL window ack", log.Fields{
838 "for window": oFsm.nextDownloadWindow, "device-id": oFsm.deviceID})
839}
840
841func (oFsm *OnuUpgradeFsm) enterFinalizeDL(ctx context.Context, e *fsm.Event) {
mpagenko80622a52021-02-09 16:53:23 +0000842 logger.Infow(ctx, "OnuUpgradeFsm finalize DL", log.Fields{
mpagenko59498c12021-03-18 14:15:15 +0000843 "device-id": oFsm.deviceID, "crc": strconv.FormatInt(int64(oFsm.imageCRC), 16), "delay": oFsm.delayEndSwDl})
mpagenko609a69a2021-12-02 08:12:58 +0000844 //use a background routine to wait EndSwDlDelay and then send the EndSwDl request
845 // in order to avoid blocking on synchronous event calls for the complete wait time
846 // and to allow for state transition before sending the EndSwDl request
847 go oFsm.delayAndSendEndSwDl(ctx)
848}
mpagenko80622a52021-02-09 16:53:23 +0000849
mpagenko609a69a2021-12-02 08:12:58 +0000850//delayAndSendEndSwDl ensures a delay before sending the EndSwDl request
851// may also be aborted by parallel channel reception on chAbortEndSwDl
852func (oFsm *OnuUpgradeFsm) delayAndSendEndSwDl(ctx context.Context) {
mpagenkoaa3afe92021-05-21 16:20:58 +0000853 oFsm.mutexUpgradeParams.RLock()
mpagenko80622a52021-02-09 16:53:23 +0000854 if oFsm.delayEndSwDl {
mpagenkoaa3afe92021-05-21 16:20:58 +0000855 oFsm.mutexUpgradeParams.RUnlock()
mpagenko609a69a2021-12-02 08:12:58 +0000856 //give the ONU some time for image evaluation (hoping it does not only start this evaluation on first EndSwDl itself)
857 logger.Debugw(ctx, "OnuUpgradeFsm delay EndSwDl", log.Fields{"device-id": oFsm.deviceID,
858 "duration_s": cOmciEndSwDlDelaySeconds})
859 select {
860 case <-time.After(cOmciEndSwDlDelaySeconds * time.Second):
861 logger.Warnw(ctx, "OnuUpgradeFsm start sending EndSwDl", log.Fields{
862 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier})
863 case <-oFsm.chAbortDelayEndSwDl:
864 logger.Debugw(ctx, "OnuUpgradeFsm abort request to send EndSwDl", log.Fields{"device-id": oFsm.deviceID})
865 //according further state transition is ensured by the entity that sent the abort
866 return
867 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000868 } else {
869 oFsm.mutexUpgradeParams.RUnlock()
mpagenko80622a52021-02-09 16:53:23 +0000870 }
871
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000872 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000873 if pBaseFsm == nil {
mpagenko38662d02021-08-11 09:45:19 +0000874 logger.Errorw(ctx, "EndSwDl abort: BaseFsm invalid", log.Fields{"device-id": oFsm.deviceID})
875 oFsm.mutexUpgradeParams.Lock()
mpagenko38662d02021-08-11 09:45:19 +0000876 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR
877 oFsm.mutexUpgradeParams.Unlock()
mpagenko59498c12021-03-18 14:15:15 +0000878 // Can't call FSM Event directly, decoupling it
mpagenko609a69a2021-12-02 08:12:58 +0000879 _ = pBaseFsm.PFsm.Event(UpgradeEvAbort)
mpagenko59498c12021-03-18 14:15:15 +0000880 return
881 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000882 err := oFsm.pOmciCC.SendEndSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
883 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, oFsm.origImageLength, oFsm.imageCRC)
Holger Hildebrandt288d9472021-09-06 10:47:53 +0000884
mpagenko80622a52021-02-09 16:53:23 +0000885 if err != nil {
mpagenkoa2b288f2021-10-21 11:25:27 +0000886 logger.Errorw(ctx, "EndSwDl abort: error sending EndSwDl", log.Fields{
mpagenko80622a52021-02-09 16:53:23 +0000887 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +0000888 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +0000889 return
890 }
mpagenko609a69a2021-12-02 08:12:58 +0000891 //from here on sending of EndSwDl(Abort) is not needed anymore (even though response is not yet received)
892 // this avoids sending of both EndSwDl(Success) and EndSwDl(Abort) when cancellation falls just into this window
893 // the ONU must theoretically be prepared to receive none of them (in case of OMCI transfer issues) e.g. by timeout
894 oFsm.isEndSwDlOpen = false
895 // wait for the EndSwDLResponse and check, if the ONU is ready for activation
896 _ = pBaseFsm.PFsm.Event(UpgradeEvWaitEndDownload)
897} //delayAndSendEndSwDl
mpagenko59498c12021-03-18 14:15:15 +0000898
899func (oFsm *OnuUpgradeFsm) enterWaitEndDL(ctx context.Context, e *fsm.Event) {
900 logger.Infow(ctx, "OnuUpgradeFsm WaitEndDl", log.Fields{
901 "device-id": oFsm.deviceID, "wait delay": oFsm.waitDelayEndSwDl * time.Second, "wait count": oFsm.waitCountEndSwDl})
902 if oFsm.waitCountEndSwDl == 0 {
903 logger.Errorw(ctx, "WaitEndDl abort: max limit of EndSwDL reached", log.Fields{
904 "device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000905 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000906 if pBaseFsm == nil {
907 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
908 "device-id": oFsm.deviceID})
909 return
910 }
mpagenko38662d02021-08-11 09:45:19 +0000911 oFsm.mutexUpgradeParams.Lock()
mpagenko39b703e2021-08-25 13:38:40 +0000912 oFsm.volthaDownloadReason = voltha.ImageState_IMAGE_REFUSED_BY_ONU //something like 'END_DOWNLOAD_TIMEOUT' would be better (proto)
mpagenko38662d02021-08-11 09:45:19 +0000913 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000914 go func(a_pAFsm *cmn.AdapterFsm) {
915 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko59498c12021-03-18 14:15:15 +0000916 }(pBaseFsm)
917 return
918 }
919
920 oFsm.waitCountEndSwDl--
921 select {
922 case <-time.After(oFsm.waitDelayEndSwDl * time.Second):
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000923 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000924 if pBaseFsm == nil {
925 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
926 "device-id": oFsm.deviceID})
927 //FSM may be reset already from somewhere else, nothing we can do here anymore
928 return
929 }
930 //retry End SW DL
mpagenkoaa3afe92021-05-21 16:20:58 +0000931 oFsm.mutexUpgradeParams.Lock()
mpagenko59498c12021-03-18 14:15:15 +0000932 oFsm.delayEndSwDl = false //no more extra delay for the request
mpagenkoaa3afe92021-05-21 16:20:58 +0000933 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000934 go func(a_pAFsm *cmn.AdapterFsm) {
935 _ = a_pAFsm.PFsm.Event(UpgradeEvContinueFinalize)
mpagenko59498c12021-03-18 14:15:15 +0000936 }(pBaseFsm)
937 return
938 case success := <-oFsm.chReceiveExpectedResponse:
939 logger.Debugw(ctx, "WaitEndDl stop wait timer", log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +0000940 oFsm.isEndSwDlOpen = false //no request to abort of download (already finished or immediate abort)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000941 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000942 if pBaseFsm == nil {
943 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
944 "device-id": oFsm.deviceID})
945 //FSM may be reset already from somewhere else, nothing we can do here anymore
946 return
947 }
948 if success {
949 //answer received with ready indication
mpagenko38662d02021-08-11 09:45:19 +0000950 //useAPIVersion43 may not conflict in concurrency in this state function
951 if oFsm.useAPIVersion43 { // newer API usage requires verification of downloaded image version
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000952 go func(a_pAFsm *cmn.AdapterFsm) {
953 _ = a_pAFsm.PFsm.Event(UpgradeEvCheckImageName)
mpagenkoc26d4c02021-05-06 14:27:57 +0000954 }(pBaseFsm)
mpagenko38662d02021-08-11 09:45:19 +0000955 } else { // elder API usage does not support image version check -immediately consider download as successful
956 if oFsm.activateImage {
957 //immediate activation requested
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000958 go func(a_pAFsm *cmn.AdapterFsm) {
959 _ = a_pAFsm.PFsm.Event(UpgradeEvRequestActivate)
mpagenko38662d02021-08-11 09:45:19 +0000960 }(pBaseFsm)
961 } else {
962 //have to wait on explicit activation request
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000963 go func(a_pAFsm *cmn.AdapterFsm) {
964 _ = a_pAFsm.PFsm.Event(UpgradeEvWaitForActivate)
mpagenko38662d02021-08-11 09:45:19 +0000965 }(pBaseFsm)
966 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000967 }
mpagenko59498c12021-03-18 14:15:15 +0000968 return
969 }
970 //timer was aborted
mpagenko45586762021-10-01 08:30:22 +0000971 oFsm.abortOnOmciError(ctx, true)
mpagenko59498c12021-03-18 14:15:15 +0000972 return
973 }
mpagenko80622a52021-02-09 16:53:23 +0000974}
975
mpagenko38662d02021-08-11 09:45:19 +0000976func (oFsm *OnuUpgradeFsm) enterCheckImageName(ctx context.Context, e *fsm.Event) {
977 logger.Debugw(ctx, "OnuUpgradeFsm checking downloaded image name", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000978 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
Holger Hildebrandt3ac49bd2022-02-07 17:46:43 +0000979 requestedAttributes := me.AttributeValueMap{me.SoftwareImage_IsCommitted: 0, me.SoftwareImage_IsActive: 0, me.SoftwareImage_Version: ""}
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000980 meInstance, err := oFsm.pOmciCC.SendGetMe(log.WithSpanFromContext(context.Background(), ctx),
981 me.SoftwareImageClassID, oFsm.InactiveImageMeID, requestedAttributes, oFsm.pDeviceHandler.GetOmciTimeout(),
982 false, oFsm.PAdaptFsm.CommChan)
mpagenko38662d02021-08-11 09:45:19 +0000983 if err != nil {
984 logger.Errorw(ctx, "OnuUpgradeFsm get Software Image ME result error",
985 log.Fields{"device-id": oFsm.deviceID, "Error": err})
mpagenko45586762021-10-01 08:30:22 +0000986 oFsm.abortOnOmciError(ctx, true)
mpagenko38662d02021-08-11 09:45:19 +0000987 return
988 }
989 oFsm.pLastTxMeInstance = meInstance
990}
991
mpagenko80622a52021-02-09 16:53:23 +0000992func (oFsm *OnuUpgradeFsm) enterActivateSw(ctx context.Context, e *fsm.Event) {
993 logger.Infow(ctx, "OnuUpgradeFsm activate SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000994 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
mpagenko80622a52021-02-09 16:53:23 +0000995
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000996 err := oFsm.pOmciCC.SendActivateSoftware(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
997 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID)
mpagenko80622a52021-02-09 16:53:23 +0000998 if err != nil {
999 logger.Errorw(ctx, "ActivateSw abort: can't send activate frame", log.Fields{
1000 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +00001001 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +00001002 return
1003 }
mpagenko38662d02021-08-11 09:45:19 +00001004 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +00001005 oFsm.upgradePhase = cUpgradeActivating //start of image activation for ONU
mpagenko38662d02021-08-11 09:45:19 +00001006 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATING
1007 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +00001008}
1009
1010func (oFsm *OnuUpgradeFsm) enterCommitSw(ctx context.Context, e *fsm.Event) {
mpagenko38662d02021-08-11 09:45:19 +00001011 logger.Debugw(ctx, "OnuUpgradeFsm start commit SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001012 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
mpagenko38662d02021-08-11 09:45:19 +00001013 //any abort request (also conditional) is still regarded as valid as the commit indication might not be possible to verify
1014 // (which is a bit problematic as the ONU might already be in committed state,
1015 // in this case (committing failed) always 'onuimage list' should be used to verify the real state (if ONU is reachable))
1016 if activeImageID, err := oFsm.pDevEntry.GetActiveImageMeID(ctx); err == nil {
mpagenkoaa3afe92021-05-21 16:20:58 +00001017 oFsm.mutexUpgradeParams.Lock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001018 if activeImageID == oFsm.InactiveImageMeID {
1019 inactiveImageID := oFsm.InactiveImageMeID
mpagenko15ff4a52021-03-02 10:09:20 +00001020 logger.Infow(ctx, "OnuUpgradeFsm commit SW", log.Fields{
mpagenkoaa3afe92021-05-21 16:20:58 +00001021 "device-id": oFsm.deviceID, "me-id": inactiveImageID}) //more efficient activeImageID with above check
1022 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTING
mpagenko45586762021-10-01 08:30:22 +00001023 oFsm.upgradePhase = cUpgradeCommitting //start of image commitment for ONU
mpagenkoaa3afe92021-05-21 16:20:58 +00001024 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001025 err := oFsm.pOmciCC.SendCommitSoftware(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
1026 oFsm.PAdaptFsm.CommChan, inactiveImageID) //more efficient activeImageID with above check
mpagenko15ff4a52021-03-02 10:09:20 +00001027 if err != nil {
1028 logger.Errorw(ctx, "CommitSw abort: can't send commit sw frame", log.Fields{
1029 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +00001030 oFsm.abortOnOmciError(ctx, true)
mpagenko15ff4a52021-03-02 10:09:20 +00001031 return
1032 }
1033 return
1034 }
mpagenko38662d02021-08-11 09:45:19 +00001035 oFsm.mutexUpgradeParams.Unlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001036 logger.Errorw(ctx, "OnuUpgradeFsm active ImageId <> IdToCommit", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001037 "device-id": oFsm.deviceID, "active ID": activeImageID, "to commit ID": oFsm.InactiveImageMeID})
mpagenko38662d02021-08-11 09:45:19 +00001038 } else {
1039 logger.Errorw(ctx, "OnuUpgradeFsm can't commit, no valid active image", log.Fields{
1040 "device-id": oFsm.deviceID})
mpagenko15ff4a52021-03-02 10:09:20 +00001041 }
mpagenko38662d02021-08-11 09:45:19 +00001042 oFsm.mutexUpgradeParams.Lock()
1043 oFsm.conditionalCancelRequested = false //any lingering conditional cancelRequest is superseded by this error
mpagenko38662d02021-08-11 09:45:19 +00001044 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
mpagenko38662d02021-08-11 09:45:19 +00001045 oFsm.mutexUpgradeParams.Unlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001046 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001047 pBaseFsm := oFsm.PAdaptFsm
mpagenko15ff4a52021-03-02 10:09:20 +00001048 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001049 go func(a_pAFsm *cmn.AdapterFsm) {
1050 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko15ff4a52021-03-02 10:09:20 +00001051 }(pBaseFsm)
1052}
1053
1054func (oFsm *OnuUpgradeFsm) enterCheckCommitted(ctx context.Context, e *fsm.Event) {
mpagenko38662d02021-08-11 09:45:19 +00001055 logger.Debugw(ctx, "OnuUpgradeFsm checking committed SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001056 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
Holger Hildebrandt3ac49bd2022-02-07 17:46:43 +00001057 requestedAttributes := me.AttributeValueMap{me.SoftwareImage_IsCommitted: 0, me.SoftwareImage_IsActive: 0, me.SoftwareImage_Version: ""}
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001058 meInstance, err := oFsm.pOmciCC.SendGetMe(log.WithSpanFromContext(context.Background(), ctx),
1059 me.SoftwareImageClassID, oFsm.InactiveImageMeID, requestedAttributes, oFsm.pDeviceHandler.GetOmciTimeout(), false, oFsm.PAdaptFsm.CommChan)
ozgecanetsiab36ed572021-04-01 10:38:48 +03001060 if err != nil {
1061 logger.Errorw(ctx, "OnuUpgradeFsm get Software Image ME result error",
1062 log.Fields{"device-id": oFsm.deviceID, "Error": err})
mpagenko45586762021-10-01 08:30:22 +00001063 oFsm.abortOnOmciError(ctx, true)
ozgecanetsiab36ed572021-04-01 10:38:48 +03001064 return
1065 }
mpagenko15ff4a52021-03-02 10:09:20 +00001066 oFsm.pLastTxMeInstance = meInstance
mpagenko80622a52021-02-09 16:53:23 +00001067}
1068
1069func (oFsm *OnuUpgradeFsm) enterResetting(ctx context.Context, e *fsm.Event) {
1070 logger.Debugw(ctx, "OnuUpgradeFsm resetting", log.Fields{"device-id": oFsm.deviceID})
1071
mpagenko45586762021-10-01 08:30:22 +00001072 oFsm.stateUpdateOnReset(ctx)
mpagenko38662d02021-08-11 09:45:19 +00001073
mpagenkoa2b288f2021-10-21 11:25:27 +00001074 oFsm.mutexAbortRequest.Lock()
1075 //to be sure to abort a possibly still running runSwDlSectionWindow()
1076 // in case the reset was not received from cancel() and download not finished correctly
1077 oFsm.abortRequested = oFsm.volthaDownloadReason
1078 oFsm.mutexAbortRequest.Unlock()
1079
mpagenkoc26d4c02021-05-06 14:27:57 +00001080 // in case the download-to-ONU timer is still running - cancel it
mpagenko39b703e2021-08-25 13:38:40 +00001081 //use non-blocking channel (to be independent from receiver state)
1082 select {
1083 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
1084 case oFsm.chOnuDlReady <- false:
1085 default:
mpagenkoc26d4c02021-05-06 14:27:57 +00001086 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001087 pConfigUpgradeStateAFsm := oFsm.PAdaptFsm
1088 if pConfigUpgradeStateAFsm != nil {
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001089 var nextEvent string
1090 if oFsm.isEndSwDlOpen {
mpagenko45586762021-10-01 08:30:22 +00001091 if oFsm.repeatAbort {
1092 oFsm.delayEndSwDl = true //run next abort with delay
1093 } else { //initial request
1094 oFsm.delayEndSwDl = false //run next abort with no delay
1095 oFsm.waitCountEndSwDl = cWaitCountEndSwDl //init for possible repetitions
1096 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001097 nextEvent = UpgradeEvAbortSwDownload
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001098 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001099 nextEvent = UpgradeEvRestart
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001100 }
1101 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001102 go func(a_pAFsm *cmn.AdapterFsm) {
1103 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1104 _ = a_pAFsm.PFsm.Event(nextEvent)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001105 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001106 }(pConfigUpgradeStateAFsm)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001107 }
1108}
mpagenkoc26d4c02021-05-06 14:27:57 +00001109
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001110func (oFsm *OnuUpgradeFsm) enterAbortingDL(ctx context.Context, e *fsm.Event) {
1111 logger.Debugw(ctx, "OnuUpgradeFsm aborting download to ONU", log.Fields{"device-id": oFsm.deviceID})
1112
mpagenko45586762021-10-01 08:30:22 +00001113 oFsm.mutexUpgradeParams.RLock()
1114 if oFsm.delayEndSwDl {
1115 oFsm.mutexUpgradeParams.RUnlock()
1116 //give the ONU some time for image discard activities
1117 time.Sleep(cOmciEndSwDlDelaySeconds * time.Second)
1118 } else {
1119 oFsm.mutexUpgradeParams.RUnlock()
1120 }
1121
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001122 pBaseFsm := oFsm.PAdaptFsm
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001123 if pBaseFsm == nil {
1124 logger.Errorw(ctx, "OnuUpgradeFsm aborting download: BaseFsm invalid", log.Fields{"device-id": oFsm.deviceID})
1125 return
1126 }
1127 // abort the download operation by sending an end software download message with invalid CRC and image size
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001128 err := oFsm.pOmciCC.SendEndSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
1129 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, 0, 0xFFFFFFFF)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001130
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001131 if err != nil {
1132 logger.Errorw(ctx, "OnuUpgradeFsm aborting download: can't send EndSwDl request", log.Fields{"device-id": oFsm.deviceID})
1133 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001134 go func(a_pAFsm *cmn.AdapterFsm) {
1135 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1136 _ = a_pAFsm.PFsm.Event(UpgradeEvRestart)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001137 }
1138 }(pBaseFsm)
1139 return
1140 }
mpagenkoa2b288f2021-10-21 11:25:27 +00001141
1142 //avoid waiting in the enterXXX function here,
1143 // otherwise synchronous event calls (like from RxMessage processing) may block and block complete Rx processing then
1144 go oFsm.waitOnAbortEndSwDlResponse(ctx)
mpagenko45586762021-10-01 08:30:22 +00001145}
1146
1147//abortingDlEvaluateResponse waits for a channel indication with decision to proceed the FSM processing
1148func (oFsm *OnuUpgradeFsm) abortingDlEvaluateResponse(ctx context.Context,
1149 pBaseFsm *cmn.AdapterFsm, aResponseResult tEndSwDlResponseResult) bool {
1150 switch aResponseResult {
1151 case cEndSwDlResponseBusy: // indication for device busy, needs repetition
1152 if oFsm.waitCountEndSwDl == 0 {
1153 logger.Errorw(ctx, "aborting download: max limit of EndSwDl reached", log.Fields{
1154 "device-id": oFsm.deviceID})
1155 go func(a_pAFsm *cmn.AdapterFsm) {
1156 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1157 _ = a_pAFsm.PFsm.Event(UpgradeEvRestart) //give up and let FSM terminate
1158 }
1159 }(pBaseFsm)
1160 } else {
1161 logger.Debugw(ctx, "aborting download: re-trigger sending abort SwDl", log.Fields{
1162 "device-id": oFsm.deviceID, "counter": oFsm.waitCountEndSwDl})
1163 oFsm.waitCountEndSwDl--
1164 oFsm.repeatAbort = true //repeated request in next round
1165 go func(a_pAFsm *cmn.AdapterFsm) {
1166 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1167 _ = a_pAFsm.PFsm.Event(UpgradeEvReset) //which then re-triggers sending AbortSwDL
1168 }
1169 }(pBaseFsm)
1170 }
1171 return true
1172 case cEndSwDlResponseSuccess: // indication for success response
1173 logger.Infow(ctx, "aborting download: success response, terminating FSM", log.Fields{
1174 "device-id": oFsm.deviceID})
1175 case cEndSwDlResponseAbort: // indication for request to abort waiting for response
1176 logger.Infow(ctx, "aborting download: request to abort waiting, terminating FSM", log.Fields{
1177 "device-id": oFsm.deviceID})
1178 default:
1179 logger.Errorw(ctx, "aborting download: unknown channel indication, terminating FSM", log.Fields{
1180 "device-id": oFsm.deviceID})
1181 } //switch
1182 return false
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001183}
1184
1185func (oFsm *OnuUpgradeFsm) enterRestarting(ctx context.Context, e *fsm.Event) {
1186 logger.Debugw(ctx, "OnuUpgradeFsm restarting", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001187 pConfigUpgradeStateAFsm := oFsm.PAdaptFsm
1188 if pConfigUpgradeStateAFsm != nil {
mpagenko80622a52021-02-09 16:53:23 +00001189 // abort running message processing
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001190 fsmAbortMsg := cmn.Message{
1191 Type: cmn.TestMsg,
1192 Data: cmn.TestMessage{
1193 TestMessageVal: cmn.AbortMessageProcessing,
mpagenko80622a52021-02-09 16:53:23 +00001194 },
1195 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001196 pConfigUpgradeStateAFsm.CommChan <- fsmAbortMsg
mpagenko80622a52021-02-09 16:53:23 +00001197
1198 //try to restart the FSM to 'disabled'
1199 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001200 go func(a_pAFsm *cmn.AdapterFsm) {
1201 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1202 _ = a_pAFsm.PFsm.Event(UpgradeEvDisable)
mpagenko80622a52021-02-09 16:53:23 +00001203 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001204 }(pConfigUpgradeStateAFsm)
mpagenko80622a52021-02-09 16:53:23 +00001205 }
1206}
1207
1208func (oFsm *OnuUpgradeFsm) enterDisabled(ctx context.Context, e *fsm.Event) {
1209 logger.Debugw(ctx, "OnuUpgradeFsm enters disabled state", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001210 // 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 +00001211 if oFsm.pDeviceHandler != nil {
1212 //request removal of 'reference' in the Handler (completely clear the FSM and its data)
mpagenko38662d02021-08-11 09:45:19 +00001213 pLastUpgradeImageState := &voltha.ImageState{
1214 Version: oFsm.imageVersion,
1215 DownloadState: oFsm.volthaDownloadState,
1216 Reason: oFsm.volthaDownloadReason,
1217 ImageState: oFsm.volthaImageState,
1218 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001219 go oFsm.pDeviceHandler.RemoveOnuUpgradeFsm(ctx, pLastUpgradeImageState)
mpagenko80622a52021-02-09 16:53:23 +00001220 }
1221}
1222
1223func (oFsm *OnuUpgradeFsm) processOmciUpgradeMessages(ctx context.Context) { //ctx context.Context?
1224 logger.Debugw(ctx, "Start OnuUpgradeFsm Msg processing", log.Fields{"for device-id": oFsm.deviceID})
1225loop:
1226 for {
1227 // case <-ctx.Done():
1228 // logger.Info(ctx,"MibSync Msg", log.Fields{"Message handling canceled via context for device-id": oFsm.deviceID})
1229 // break loop
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001230 message, ok := <-oFsm.PAdaptFsm.CommChan
mpagenko80622a52021-02-09 16:53:23 +00001231 if !ok {
1232 logger.Info(ctx, "OnuUpgradeFsm Rx Msg - could not read from channel", log.Fields{"device-id": oFsm.deviceID})
1233 // but then we have to ensure a restart of the FSM as well - as exceptional procedure
mpagenko45586762021-10-01 08:30:22 +00001234 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +00001235 break loop
1236 }
1237 logger.Debugw(ctx, "OnuUpgradeFsm Rx Msg", log.Fields{"device-id": oFsm.deviceID})
1238
1239 switch message.Type {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001240 case cmn.TestMsg:
1241 msg, _ := message.Data.(cmn.TestMessage)
1242 if msg.TestMessageVal == cmn.AbortMessageProcessing {
mpagenko80622a52021-02-09 16:53:23 +00001243 logger.Infow(ctx, "OnuUpgradeFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.deviceID})
1244 break loop
1245 }
1246 logger.Warnw(ctx, "OnuUpgradeFsm unknown TestMessage", log.Fields{"device-id": oFsm.deviceID, "MessageVal": msg.TestMessageVal})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001247 case cmn.OMCI:
1248 msg, _ := message.Data.(cmn.OmciMessage)
mpagenko80622a52021-02-09 16:53:23 +00001249 oFsm.handleOmciOnuUpgradeMessage(ctx, msg)
1250 default:
1251 logger.Warn(ctx, "OnuUpgradeFsm Rx unknown message", log.Fields{"device-id": oFsm.deviceID,
1252 "message.Type": message.Type})
1253 }
1254 }
1255 logger.Infow(ctx, "End OnuUpgradeFsm Msg processing", log.Fields{"device-id": oFsm.deviceID})
1256}
1257
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001258func (oFsm *OnuUpgradeFsm) handleOmciOnuUpgradeMessage(ctx context.Context, msg cmn.OmciMessage) {
mpagenko80622a52021-02-09 16:53:23 +00001259 logger.Debugw(ctx, "Rx OMCI OnuUpgradeFsm Msg", log.Fields{"device-id": oFsm.deviceID,
1260 "msgType": msg.OmciMsg.MessageType})
1261
1262 switch msg.OmciMsg.MessageType {
1263 case omci.StartSoftwareDownloadResponseType:
1264 {
mpagenko45586762021-10-01 08:30:22 +00001265 oFsm.handleRxStartSwDownloadResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001266 return
1267 } //StartSoftwareDownloadResponseType
1268 case omci.DownloadSectionResponseType:
1269 {
mpagenko45586762021-10-01 08:30:22 +00001270 oFsm.handleRxSwSectionResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001271 return
mpagenko80622a52021-02-09 16:53:23 +00001272 } //DownloadSectionResponseType
1273 case omci.EndSoftwareDownloadResponseType:
1274 {
mpagenko45586762021-10-01 08:30:22 +00001275 oFsm.handleRxEndSwDownloadResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001276 return
1277 } //EndSoftwareDownloadResponseType
1278 case omci.ActivateSoftwareResponseType:
1279 {
1280 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeActivateSoftwareResponse)
1281 if msgLayer == nil {
1282 logger.Errorw(ctx, "Omci Msg layer could not be detected for ActivateSw",
1283 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001284 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001285 return
1286 }
1287 msgObj, msgOk := msgLayer.(*omci.ActivateSoftwareResponse)
1288 if !msgOk {
1289 logger.Errorw(ctx, "Omci Msg layer could not be assigned for ActivateSw",
1290 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001291 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001292 return
1293 }
1294 logger.Debugw(ctx, "OnuUpgradeFsm ActivateSwResponse data", log.Fields{
1295 "device-id": oFsm.deviceID, "data-fields": msgObj})
1296 if msgObj.Result != me.Success {
1297 logger.Errorw(ctx, "OnuUpgradeFsm ActivateSwResponse result error - later: drive FSM to abort state ?",
1298 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
mpagenko45586762021-10-01 08:30:22 +00001299 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001300 return
1301 }
mpagenko183647c2021-06-08 15:25:04 +00001302 oFsm.mutexUpgradeParams.Lock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001303 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
mpagenko38662d02021-08-11 09:45:19 +00001304 // the image is regarded as active really only after ONU reboot and according indication (ONU down/up procedure)
mpagenko183647c2021-06-08 15:25:04 +00001305 oFsm.mutexUpgradeParams.Unlock()
1306 logger.Infow(ctx, "Expected ActivateSwResponse received",
1307 log.Fields{"device-id": oFsm.deviceID, "commit": oFsm.commitImage})
1308 if oFsm.commitImage {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001309 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvWaitForCommit)
mpagenko183647c2021-06-08 15:25:04 +00001310 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001311 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvActivationDone) // let the FSM wait for external commit request
mpagenko183647c2021-06-08 15:25:04 +00001312 }
mpagenko80622a52021-02-09 16:53:23 +00001313 return
1314 }
mpagenko183647c2021-06-08 15:25:04 +00001315 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +00001316 logger.Errorw(ctx, "OnuUpgradeFsm ActivateSwResponse wrong ME instance: abort",
1317 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
mpagenko45586762021-10-01 08:30:22 +00001318 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001319 return
1320 } //ActivateSoftwareResponseType
mpagenko15ff4a52021-03-02 10:09:20 +00001321 case omci.CommitSoftwareResponseType:
1322 {
1323 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeCommitSoftwareResponse)
1324 if msgLayer == nil {
1325 logger.Errorw(ctx, "Omci Msg layer could not be detected for CommitResponse",
1326 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001327 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001328 return
1329 }
1330 msgObj, msgOk := msgLayer.(*omci.CommitSoftwareResponse)
1331 if !msgOk {
1332 logger.Errorw(ctx, "Omci Msg layer could not be assigned for CommitResponse",
1333 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001334 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001335 return
1336 }
mpagenkobf67a092021-03-17 09:52:28 +00001337 if msgObj.Result != me.Success {
mpagenko15ff4a52021-03-02 10:09:20 +00001338 logger.Errorw(ctx, "OnuUpgradeFsm SwImage CommitResponse result error - later: drive FSM to abort state ?",
1339 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
mpagenko45586762021-10-01 08:30:22 +00001340 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001341 return
mpagenkobf67a092021-03-17 09:52:28 +00001342 }
mpagenkoaa3afe92021-05-21 16:20:58 +00001343 oFsm.mutexUpgradeParams.RLock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001344 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
mpagenkoaa3afe92021-05-21 16:20:58 +00001345 oFsm.mutexUpgradeParams.RUnlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001346 logger.Debugw(ctx, "OnuUpgradeFsm Expected SwImage CommitResponse received", log.Fields{"device-id": oFsm.deviceID})
1347 //verifying committed image
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001348 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvCheckCommitted)
mpagenko15ff4a52021-03-02 10:09:20 +00001349 return
1350 }
mpagenkoaa3afe92021-05-21 16:20:58 +00001351 oFsm.mutexUpgradeParams.RUnlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001352 logger.Errorw(ctx, "OnuUpgradeFsm SwImage CommitResponse wrong ME instance: abort",
1353 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
mpagenko45586762021-10-01 08:30:22 +00001354 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001355 return
1356 } //CommitSoftwareResponseType
1357 case omci.GetResponseType:
1358 {
mpagenko45586762021-10-01 08:30:22 +00001359 oFsm.handleRxSwGetResponse(ctx, msg)
mpagenko15ff4a52021-03-02 10:09:20 +00001360 return
1361 } //GetResponseType
mpagenko80622a52021-02-09 16:53:23 +00001362 default:
1363 {
1364 logger.Errorw(ctx, "Rx OMCI unhandled MsgType",
1365 log.Fields{"omciMsgType": msg.OmciMsg.MessageType, "device-id": oFsm.deviceID})
1366 return
1367 }
1368 }
1369}
1370
mpagenko45586762021-10-01 08:30:22 +00001371func (oFsm *OnuUpgradeFsm) handleRxStartSwDownloadResponse(ctx context.Context, msg cmn.OmciMessage) {
1372 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeStartSoftwareDownloadResponse)
1373 if msgLayer == nil {
1374 logger.Errorw(ctx, "Omci Msg layer could not be detected for StartSwDlResponse",
1375 log.Fields{"device-id": oFsm.deviceID})
1376 oFsm.abortOnOmciError(ctx, false)
1377 return
1378 }
1379 msgObj, msgOk := msgLayer.(*omci.StartSoftwareDownloadResponse)
1380 if !msgOk {
1381 logger.Errorw(ctx, "Omci Msg layer could not be assigned for StartSwDlResponse",
1382 log.Fields{"device-id": oFsm.deviceID})
1383 oFsm.abortOnOmciError(ctx, false)
1384 return
1385 }
1386 logger.Debugw(ctx, "OnuUpgradeFsm StartSwDlResponse data", log.Fields{
1387 "device-id": oFsm.deviceID, "data-fields": msgObj})
1388 if msgObj.Result != me.Success {
1389 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse result error - later: drive FSM to abort state ?",
1390 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1391 oFsm.abortOnOmciError(ctx, false)
1392 return
1393 }
1394
1395 oFsm.mutexUpgradeParams.Lock()
1396 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1397 logger.Debugw(ctx, "Expected StartSwDlResponse received", log.Fields{"device-id": oFsm.deviceID})
1398 if msgObj.WindowSize != oFsm.omciDownloadWindowSizeLimit {
1399 // also response WindowSize = 0 is a valid number for used Window size 1
1400 logger.Debugw(ctx, "different StartSwDlResponse window size requested by ONU", log.Fields{
1401 "acceptedOnuWindowSizeLimit": msgObj.WindowSize, "device-id": oFsm.deviceID})
1402 oFsm.omciDownloadWindowSizeLimit = msgObj.WindowSize
1403 }
1404 oFsm.noOfWindows = oFsm.noOfSections / uint32(oFsm.omciDownloadWindowSizeLimit+1)
1405 if oFsm.noOfSections%uint32(oFsm.omciDownloadWindowSizeLimit+1) > 0 {
1406 oFsm.noOfWindows++
1407 }
1408 logger.Debugw(ctx, "OnuUpgradeFsm will use", log.Fields{
1409 "windows": oFsm.noOfWindows, "sections": oFsm.noOfSections,
1410 "at WindowSizeLimit": oFsm.omciDownloadWindowSizeLimit})
1411 oFsm.nextDownloadSectionsAbsolute = 0
1412 oFsm.nextDownloadSectionsWindow = 0
1413 oFsm.nextDownloadWindow = 0
1414
1415 oFsm.mutexUpgradeParams.Unlock()
1416 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvRxStartSwDownload)
1417 return
1418 }
1419 oFsm.mutexUpgradeParams.Unlock()
1420 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse wrong ME instance: try again (later)?",
1421 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1422 oFsm.abortOnOmciError(ctx, false)
1423} //handleRxStartSwDownloadResponse
1424
1425func (oFsm *OnuUpgradeFsm) handleRxSwSectionResponse(ctx context.Context, msg cmn.OmciMessage) {
mpagenkoa2b288f2021-10-21 11:25:27 +00001426 if oFsm.PAdaptFsm == nil {
1427 logger.Infow(ctx, "DlSectionResponse received - but FSM not really valid anymore", log.Fields{
1428 "device-id": oFsm.deviceID})
1429 return
1430 }
1431 if !oFsm.PAdaptFsm.PFsm.Is(UpgradeStVerifyWindow) {
1432 //all the processing here is only relevant if the FSM is in state upgradeStVerifyWindow
1433 // otherwise this response can be ignored (may stem from a long-processing window send activity,
1434 // which is not anymore relevant based on intermediate (cancel) state transitions)
1435 logger.Infow(ctx, "DlSectionResponse received - but ignored", log.Fields{
1436 "device-id": oFsm.deviceID, "fsm-state": oFsm.PAdaptFsm.PFsm.Current()})
1437 return
1438 }
mpagenko45586762021-10-01 08:30:22 +00001439 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeDownloadSectionResponse)
1440 if msgLayer == nil {
1441 logger.Errorw(ctx, "Omci Msg layer could not be detected for DlSectionResponse",
1442 log.Fields{"device-id": oFsm.deviceID, "omci-message": msg.OmciMsg})
1443 oFsm.abortOnOmciError(ctx, false)
1444 return
1445 }
1446 msgObj, msgOk := msgLayer.(*omci.DownloadSectionResponse)
1447 if !msgOk {
1448 logger.Errorw(ctx, "Omci Msg layer could not be assigned for DlSectionResponse",
1449 log.Fields{"device-id": oFsm.deviceID})
1450 oFsm.abortOnOmciError(ctx, false)
1451 return
1452 }
1453 logger.Debugw(ctx, "OnuUpgradeFsm DlSectionResponse Data", log.Fields{
1454 "device-id": oFsm.deviceID, "data-fields": msgObj})
1455 if msgObj.Result != me.Success {
1456 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse result error - later: repeat window once?", //TODO!!!
1457 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1458 oFsm.abortOnOmciError(ctx, false)
1459 return
1460 }
1461 oFsm.mutexUpgradeParams.Lock()
1462 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1463 sectionNumber := msgObj.SectionNumber
1464 logger.Infow(ctx, "DlSectionResponse received", log.Fields{
1465 "window section-number": sectionNumber, "window": oFsm.nextDownloadWindow, "device-id": oFsm.deviceID})
1466
1467 oFsm.nextDownloadWindow++
1468 if oFsm.nextDownloadWindow >= oFsm.noOfWindows {
1469 if sectionNumber != oFsm.omciDownloadWindowSizeLast {
1470 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse section error last window - later: repeat window once?", //TODO!!!
1471 log.Fields{"device-id": oFsm.deviceID, "actual section": sectionNumber,
1472 "expected section": oFsm.omciDownloadWindowSizeLast})
1473 oFsm.mutexUpgradeParams.Unlock()
1474 oFsm.abortOnOmciError(ctx, false)
1475 return
1476 }
1477 oFsm.delayEndSwDl = true //ensure a delay for the EndSwDl message
1478 //CRC computation for all data bytes of the file
1479 imageCRC := crc32a.Checksum(oFsm.imageBuffer[:int(oFsm.origImageLength)]) //store internal for multiple usage
1480 //revert the retrieved CRC Byte Order (seems not to deliver NetworkByteOrder)
1481 var byteSlice []byte = make([]byte, 4)
1482 binary.LittleEndian.PutUint32(byteSlice, uint32(imageCRC))
1483 oFsm.imageCRC = binary.BigEndian.Uint32(byteSlice)
1484 oFsm.mutexUpgradeParams.Unlock()
1485 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvEndSwDownload)
1486 return
1487 }
1488 if sectionNumber != oFsm.omciDownloadWindowSizeLimit {
1489 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse section error - later: repeat window once?", //TODO!!!
1490 log.Fields{"device-id": oFsm.deviceID, "actual-section": sectionNumber,
1491 "expected section": oFsm.omciDownloadWindowSizeLimit})
1492 oFsm.mutexUpgradeParams.Unlock()
1493 oFsm.abortOnOmciError(ctx, false)
1494 return
1495 }
1496 oFsm.nextDownloadSectionsWindow = 0
1497 oFsm.mutexUpgradeParams.Unlock()
1498 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvContinueNextWindow)
1499 return
1500 }
1501 oFsm.mutexUpgradeParams.Unlock()
1502 logger.Errorw(ctx, "OnuUpgradeFsm Omci StartSwDlResponse wrong ME instance: try again (later)?",
1503 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1504 oFsm.abortOnOmciError(ctx, false)
1505} //handleRxSwSectionResponse
1506
1507func (oFsm *OnuUpgradeFsm) handleRxEndSwDownloadResponse(ctx context.Context, msg cmn.OmciMessage) {
1508 inAbortingState := oFsm.PAdaptFsm.PFsm.Is(UpgradeStAbortingDL)
1509
1510 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeEndSoftwareDownloadResponse)
1511 if msgLayer == nil {
1512 logger.Errorw(ctx, "Omci Msg layer could not be detected for EndSwDlResponse",
1513 log.Fields{"device-id": oFsm.deviceID})
1514 if !inAbortingState {
1515 oFsm.abortOnOmciError(ctx, false)
1516 } //else using error log and wait for another response or 'aborting' state timeout
1517 return
1518 }
1519 msgObj, msgOk := msgLayer.(*omci.EndSoftwareDownloadResponse)
1520 if !msgOk {
1521 logger.Errorw(ctx, "Omci Msg layer could not be assigned for EndSwDlResponse",
1522 log.Fields{"device-id": oFsm.deviceID})
1523 if !inAbortingState {
1524 oFsm.abortOnOmciError(ctx, false)
1525 } //else using error log and wait for another response or 'aborting' state timeout
1526 return
1527 }
1528 logger.Debugw(ctx, "OnuUpgradeFsm EndSwDlResponse data", log.Fields{
1529 "device-id": oFsm.deviceID, "data-fields": msgObj})
1530 if msgObj.Result != me.Success {
1531 if msgObj.Result == me.DeviceBusy {
1532 //ONU indicates it is still processing the image - let the FSM just wait and then repeat the request
1533 logger.Debugw(ctx, "OnuUpgradeFsm EndSwDlResponse busy: waiting before sending new request", log.Fields{
1534 "device-id": oFsm.deviceID})
1535 if inAbortingState {
1536 //if the EndSwDl was requested from state AbortingDL then use channel to indicate ONU busy/repeat indication
1537 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseBusy //repeat abort request
1538 }
mpagenko45586762021-10-01 08:30:22 +00001539 return
1540 }
mpagenko45586762021-10-01 08:30:22 +00001541 if inAbortingState {
1542 //if the EndSwDl was requested from state AbortingDL and response is error indication
mpagenkoa2b288f2021-10-21 11:25:27 +00001543 // that would be quite strange ONU behavior, no resolution from OnuAdapter, just let the FSM go on to disabled
1544 logger.Errorw(ctx, "OnuUpgradeFsm EndSwDlResponse result error - aborting anyway",
1545 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1546 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseAbort //error indication to stop waiting on EndDownloadResponse(abort)
1547 return
1548 }
1549 //else: ONU rejects the previous download, complete upgrade is immediately aborted with error
1550 logger.Errorw(ctx, "OnuUpgradeFsm EndSwDlResponse result error - abort upgrade",
1551 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1552 oFsm.mutexUpgradeParams.Lock()
1553 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
1554 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1555 oFsm.mutexUpgradeParams.Unlock()
1556 select {
1557 case oFsm.chOnuDlReady <- false:
1558 default:
1559 }
1560 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko45586762021-10-01 08:30:22 +00001561 return
1562 }
1563 oFsm.mutexUpgradeParams.Lock()
1564 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1565 logger.Debugw(ctx, "Expected EndSwDlResponse received", log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001566 if inAbortingState {
1567 oFsm.mutexUpgradeParams.Unlock()
1568 //if the EndSwDl was requested from state AbortingDL then use channel to indicate abort acceptance
1569 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseSuccess //success
1570 return
1571 }
1572 if !oFsm.useAPIVersion43 {
1573 //in the older API version the image version check was not possible
1574 // - assume new loaded image as valid-inactive immediately
1575 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_SUCCEEDED
1576 oFsm.volthaImageState = voltha.ImageState_IMAGE_INACTIVE
1577 oFsm.mutexUpgradeParams.Unlock()
mpagenkoa2b288f2021-10-21 11:25:27 +00001578 //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 +00001579 select {
mpagenko45586762021-10-01 08:30:22 +00001580 case oFsm.chOnuDlReady <- true:
1581 default:
1582 }
1583 } else {
1584 oFsm.mutexUpgradeParams.Unlock()
1585 }
1586 //use asynchronous channel sending to let the FSM proceed
1587 select {
1588 case oFsm.chReceiveExpectedResponse <- true:
1589 default:
1590 }
1591 return
1592 }
1593 oFsm.mutexUpgradeParams.Unlock()
1594 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse wrong ME instance: ignoring",
1595 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1596 // no state abort in case of unexpected ImageId, just keep waiting for the correct one
1597} //handleRxEndSwDownloadResponse
1598
1599func (oFsm *OnuUpgradeFsm) handleRxSwGetResponse(ctx context.Context, msg cmn.OmciMessage) {
1600 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeGetResponse)
1601 if msgLayer == nil {
1602 logger.Errorw(ctx, "Omci Msg layer could not be detected for SwImage GetResponse",
1603 log.Fields{"device-id": oFsm.deviceID})
1604 oFsm.abortOnOmciError(ctx, false)
1605 return
1606 }
1607 msgObj, msgOk := msgLayer.(*omci.GetResponse)
1608 if !msgOk {
1609 logger.Errorw(ctx, "Omci Msg layer could not be assigned for SwImage GetResponse",
1610 log.Fields{"device-id": oFsm.deviceID})
1611 oFsm.abortOnOmciError(ctx, false)
1612 return
1613 }
1614 logger.Debugw(ctx, "OnuUpgradeFsm SwImage GetResponse data", log.Fields{
1615 "device-id": oFsm.deviceID, "data-fields": msgObj})
1616 if msgObj.EntityClass == oFsm.pLastTxMeInstance.GetClassID() &&
1617 msgObj.EntityInstance == oFsm.pLastTxMeInstance.GetEntityID() {
1618 if msgObj.Result != me.Success {
1619 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse result error - later: drive FSM to abort state ?",
1620 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1621 oFsm.abortOnOmciError(ctx, false)
1622 return
1623 }
1624 } else {
1625 logger.Warnw(ctx, "OnuUpgradeFsm SwImage unexpected Entity GetResponse data - ignore",
1626 log.Fields{"device-id": oFsm.deviceID})
1627 return
1628 }
mpagenko45586762021-10-01 08:30:22 +00001629 meAttributes := msgObj.Attributes
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +00001630 var imageVersion string
1631 var imageIsCommitted, imageIsActive uint8
mpagenko45586762021-10-01 08:30:22 +00001632
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +00001633 if softwareImageIsCommitted, ok := meAttributes[me.SoftwareImage_IsCommitted]; ok {
1634 if softwareImageIsActiveimage, ok := meAttributes[me.SoftwareImage_IsActive]; ok {
1635 if softwareImageVersion, ok := meAttributes[me.SoftwareImage_Version]; ok {
1636 imageIsCommitted = softwareImageIsCommitted.(uint8)
1637 imageIsActive = softwareImageIsActiveimage.(uint8)
1638 imageVersion = cmn.TrimStringFromMeOctet(softwareImageVersion)
1639 logger.Debugw(ctx, "OnuUpgradeFsm - GetResponse Data for SoftwareImage",
1640 log.Fields{"device-id": oFsm.deviceID, "entityID": msgObj.EntityInstance,
1641 "version": imageVersion, "isActive": imageIsActive, "isCommitted": imageIsCommitted})
1642 } else {
1643 logger.Errorw(ctx,
1644 "OnuUpgradeFsm - Not all mandatory attributes present in in SoftwareImage instance - handling stopped!",
1645 log.Fields{"device-id": oFsm.deviceID})
1646 oFsm.abortOnOmciError(ctx, false)
1647 return
1648 }
1649 }
1650 }
mpagenko45586762021-10-01 08:30:22 +00001651 if oFsm.PAdaptFsm.PFsm.Current() == UpgradeStCheckImageName {
1652 //image name check after EndSwDownload, this state (and block) can only be taken if APIVersion43 is used
1653 oFsm.verifyOnuSwStatusAfterDownload(ctx, msgObj.EntityInstance, imageVersion, imageIsActive, imageIsCommitted)
1654 return
1655 }
1656
1657 //assumed only relevant state here is upgradeStCheckCommitted
1658 oFsm.mutexUpgradeParams.Lock()
1659 oFsm.conditionalCancelRequested = false //getting here any set (conditional) cancelRequest is not relevant anymore
1660 if msgObj.EntityInstance == oFsm.InactiveImageMeID && imageIsActive == cmn.SwIsActive {
1661 //a check on the delivered image version is not done, the ONU delivered version might be different from what might have been
1662 // indicated in the download image version string (version must be part of the image content itself)
1663 // so checking that might be quite unreliable
1664 //but with new API this was changed, assumption is that omci image version is known at download request and exactly that is used
1665 // in all the API references, so it can and should be checked here now
1666 if oFsm.useAPIVersion43 {
1667 if imageVersion != oFsm.imageVersion {
1668 //new active version indicated on OMCI from ONU is not the expected version
1669 logger.Errorw(ctx, "OnuUpgradeFsm image-version not matching the requested upgrade",
1670 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance,
1671 "onu-version": imageVersion, "expected-version": oFsm.imageVersion})
1672 // TODO!!!: error treatment?
1673 //TODO!!!: possibly send event information for aborted upgrade (aborted by wrong version)?
1674 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE //something like 'UNEXPECTED_VERSION' would be better - proto def
1675 oFsm.mutexUpgradeParams.Unlock()
1676 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1677 return
1678 }
1679 logger.Debugw(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU",
1680 log.Fields{"device-id": oFsm.deviceID})
1681 }
1682 if imageIsCommitted == cmn.SwIsCommitted {
1683 oFsm.upgradePhase = cUpgradeCommitted
1684 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTED
1685 //store the new commit flag to onuSwImageIndications (to keep them in sync)
1686 oFsm.pDevEntry.ModifySwImageActiveCommit(ctx, imageIsCommitted)
1687 logger.Infow(ctx, "requested SW image committed, releasing OnuUpgrade", log.Fields{"device-id": oFsm.deviceID})
1688 //deviceProcStatusUpdate not used anymore,
1689 // replaced by transferring the last (more) upgrade state information within removeOnuUpgradeFsm
1690 oFsm.mutexUpgradeParams.Unlock()
1691 //releasing the upgrade FSM on success
1692 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1693 return
1694 }
1695 //if not committed, abort upgrade as failed. There is no implementation here that would trigger this test again
1696 }
1697 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1698 oFsm.mutexUpgradeParams.Unlock()
1699 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse indications not matching requested upgrade",
1700 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1701 // TODO!!!: error treatment?
1702 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
1703 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1704} //handleRxSwGetResponse
1705
1706func (oFsm *OnuUpgradeFsm) verifyOnuSwStatusAfterDownload(ctx context.Context, aInstanceID uint16,
1707 aImageVersion string, aImageIsActive uint8, aImageIsCommitted uint8) {
1708 oFsm.mutexUpgradeParams.Lock()
1709 if aInstanceID == oFsm.InactiveImageMeID && aImageIsActive == cmn.SwIsInactive &&
1710 aImageIsCommitted == cmn.SwIsUncommitted {
1711 if aImageVersion != oFsm.imageVersion {
1712 //new stored inactive version indicated on OMCI from ONU is not the expected version
1713 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse version indication not matching requested upgrade",
1714 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": aInstanceID,
1715 "onu-version": aImageVersion, "expected-version": oFsm.imageVersion})
1716 //download state is set when entering the reset state
1717 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE //something like 'UNEXPECTED_VERSION' would be better - proto def
1718 oFsm.mutexUpgradeParams.Unlock()
1719 //stop the running ONU download timer
1720 //use non-blocking channel (to be independent from receiver state)
1721 select {
1722 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
1723 case oFsm.chOnuDlReady <- false:
1724 default:
1725 }
1726 // TODO!!!: error treatment?
1727 //TODO!!!: possibly send event information for aborted upgrade (aborted by wrong version)?
1728 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1729 return
1730 }
1731 //with APIVersion43 this is the point to consider the newly loaded image as valid (and inactive)
1732 oFsm.upgradePhase = cUpgradeDownloaded
1733 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_SUCCEEDED
1734 oFsm.volthaImageState = voltha.ImageState_IMAGE_INACTIVE
1735 //store the new inactive version to onuSwImageIndications (to keep them in sync)
1736 oFsm.pDevEntry.ModifySwImageInactiveVersion(ctx, oFsm.imageVersion)
1737 //proceed within upgrade FSM
1738 if oFsm.activateImage {
1739 //immediate activation requested
1740 oFsm.mutexUpgradeParams.Unlock()
1741 logger.Debugw(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU, continue with activation",
1742 log.Fields{"device-id": oFsm.deviceID})
1743 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvRequestActivate)
1744 } else {
1745 //have to wait on explicit activation request
1746 oFsm.mutexUpgradeParams.Unlock()
1747 logger.Infow(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU, wait for activate request",
1748 log.Fields{"device-id": oFsm.deviceID})
1749 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvWaitForActivate)
1750 }
1751 //use non-blocking channel (to be independent from receiver state)
1752 select {
1753 //use non-blocking channel to indicate that the download to ONU was successful
1754 case oFsm.chOnuDlReady <- true:
1755 default:
1756 }
1757 return
1758 }
1759 //not the expected image/image state
1760 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1761 oFsm.mutexUpgradeParams.Unlock()
1762 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse indications not matching requested upgrade",
1763 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": aInstanceID})
1764 // TODO!!!: error treatment?
1765 //TODO!!!: possibly send event information for aborted upgrade (aborted by ONU state indication)?
1766 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1767} //verifyOnuSwStatusAfterDownload
1768
1769//abortOnOmciError aborts the upgrade processing with evAbort
1770// asynchronous/synchronous based on parameter aAsync
1771func (oFsm *OnuUpgradeFsm) abortOnOmciError(ctx context.Context, aAsync bool) {
mpagenko38662d02021-08-11 09:45:19 +00001772 oFsm.mutexUpgradeParams.Lock()
1773 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
mpagenko38662d02021-08-11 09:45:19 +00001774 oFsm.volthaDownloadReason = voltha.ImageState_OMCI_TRANSFER_ERROR
mpagenko38662d02021-08-11 09:45:19 +00001775 oFsm.mutexUpgradeParams.Unlock()
1776 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001777 if oFsm.PAdaptFsm != nil {
mpagenko38662d02021-08-11 09:45:19 +00001778 var err error
1779 if aAsync { //asynchronous call requested to ensure state transition
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001780 go func(a_pAFsm *cmn.AdapterFsm) {
1781 if a_pAFsm.PFsm != nil {
1782 err = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko38662d02021-08-11 09:45:19 +00001783 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001784 }(oFsm.PAdaptFsm)
mpagenko38662d02021-08-11 09:45:19 +00001785 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001786 if oFsm.PAdaptFsm.PFsm != nil {
1787 err = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko38662d02021-08-11 09:45:19 +00001788 }
1789 }
1790 if err != nil {
1791 logger.Warnw(ctx, "onu upgrade fsm could not abort on omci error", log.Fields{
1792 "device-id": oFsm.deviceID, "error": err})
1793 }
1794 }
1795}
1796
mpagenkoc26d4c02021-05-06 14:27:57 +00001797//waitOnDownloadToAdapterReady state can only be reached with useAPIVersion43 (usage of pFileManager)
mpagenko38662d02021-08-11 09:45:19 +00001798// precondition: mutexIsAwaitingAdapterDlResponse is lockek on call
1799func (oFsm *OnuUpgradeFsm) waitOnDownloadToAdapterReady(ctx context.Context, aSyncChannel chan<- struct{},
1800 aWaitChannel chan bool) {
mpagenkoc26d4c02021-05-06 14:27:57 +00001801 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
mpagenko38662d02021-08-11 09:45:19 +00001802 downloadToAdapterTimeout := oFsm.pFileManager.GetDownloadTimeout(ctx)
mpagenkoc26d4c02021-05-06 14:27:57 +00001803 oFsm.isWaitingForAdapterDlResponse = true
1804 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenko38662d02021-08-11 09:45:19 +00001805 aSyncChannel <- struct{}{}
mpagenko80622a52021-02-09 16:53:23 +00001806 select {
1807 // maybe be also some outside cancel (but no context modeled for the moment ...)
1808 // case <-ctx.Done():
mpagenkoc26d4c02021-05-06 14:27:57 +00001809 // logger.Infow("OnuUpgradeFsm-waitOnDownloadToAdapterReady canceled", log.Fields{"for device-id": oFsm.deviceID})
1810 case <-time.After(downloadToAdapterTimeout): //10s should be enough for downloading some image to the adapter
1811 logger.Warnw(ctx, "OnuUpgradeFsm Waiting-adapter-download timeout", log.Fields{
1812 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier, "timeout": downloadToAdapterTimeout})
1813 oFsm.pFileManager.RemoveReadyRequest(ctx, oFsm.imageIdentifier, aWaitChannel)
mpagenko39b703e2021-08-25 13:38:40 +00001814 //running into timeout here may still have the download to adapter active -> abort
1815 oFsm.pFileManager.CancelDownload(ctx, oFsm.imageIdentifier)
mpagenkoc26d4c02021-05-06 14:27:57 +00001816 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1817 oFsm.isWaitingForAdapterDlResponse = false
1818 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenko39b703e2021-08-25 13:38:40 +00001819 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +00001820 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
mpagenko39b703e2021-08-25 13:38:40 +00001821 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'DOWNLOAD_TO_ADAPTER_TIMEOUT' would be better (proto)
mpagenko39b703e2021-08-25 13:38:40 +00001822 oFsm.mutexUpgradeParams.Unlock()
1823 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001824 if oFsm.PAdaptFsm != nil && oFsm.PAdaptFsm.PFsm != nil {
1825 err := oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko39b703e2021-08-25 13:38:40 +00001826 if err != nil {
1827 logger.Warnw(ctx, "onu upgrade fsm could not abort on omci error", log.Fields{
1828 "device-id": oFsm.deviceID, "error": err})
1829 }
1830 }
mpagenkoc26d4c02021-05-06 14:27:57 +00001831 return
1832
1833 case success := <-aWaitChannel:
1834 if success {
1835 logger.Debugw(ctx, "OnuUpgradeFsm image-downloaded received", log.Fields{"device-id": oFsm.deviceID})
1836 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1837 oFsm.isWaitingForAdapterDlResponse = false
1838 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
1839 //let the upgrade process proceed
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001840 pUpgradeFsm := oFsm.PAdaptFsm
mpagenkoc26d4c02021-05-06 14:27:57 +00001841 if pUpgradeFsm != nil {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001842 _ = pUpgradeFsm.PFsm.Event(UpgradeEvPrepareSwDownload)
mpagenkoc26d4c02021-05-06 14:27:57 +00001843 } else {
1844 logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
1845 }
1846 return
1847 }
mpagenko39b703e2021-08-25 13:38:40 +00001848 // waiting was aborted (assumed here to be caused by
1849 // error detection or cancel at download after upgrade FSM reset/abort with according image states set there)
mpagenkoc26d4c02021-05-06 14:27:57 +00001850 logger.Debugw(ctx, "OnuUpgradeFsm Waiting-adapter-download aborted", log.Fields{"device-id": oFsm.deviceID})
1851 oFsm.pFileManager.RemoveReadyRequest(ctx, oFsm.imageIdentifier, aWaitChannel)
1852 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1853 oFsm.isWaitingForAdapterDlResponse = false
1854 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenkoc26d4c02021-05-06 14:27:57 +00001855 return
mpagenko80622a52021-02-09 16:53:23 +00001856 }
1857}
mpagenkoc26d4c02021-05-06 14:27:57 +00001858
1859//waitOnDownloadToOnuReady state can only be reached with useAPIVersion43 (usage of pFileManager)
1860func (oFsm *OnuUpgradeFsm) waitOnDownloadToOnuReady(ctx context.Context, aWaitChannel chan bool) {
1861 downloadToOnuTimeout := time.Duration(1+(oFsm.imageLength/0x400000)) * oFsm.downloadToOnuTimeout4MB
1862 logger.Debugw(ctx, "OnuUpgradeFsm start download-to-ONU timer", log.Fields{"device-id": oFsm.deviceID,
1863 "duration": downloadToOnuTimeout})
mpagenkoc26d4c02021-05-06 14:27:57 +00001864 select {
1865 // maybe be also some outside cancel (but no context modeled for the moment ...)
1866 // case <-ctx.Done():
1867 // logger.Infow("OnuUpgradeFsm-waitOnDownloadToOnuReady canceled", log.Fields{"for device-id": oFsm.deviceID})
1868 case <-time.After(downloadToOnuTimeout): //using an image-size depending timout (in minutes)
1869 logger.Warnw(ctx, "OnuUpgradeFsm Waiting-ONU-download timeout", log.Fields{
1870 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier, "timeout": downloadToOnuTimeout})
mpagenkoc26d4c02021-05-06 14:27:57 +00001871 //the upgrade process has to be aborted
mpagenko45586762021-10-01 08:30:22 +00001872 oFsm.abortOnOmciError(ctx, false)
mpagenkoc26d4c02021-05-06 14:27:57 +00001873 return
1874
1875 case success := <-aWaitChannel:
1876 if success {
1877 logger.Debugw(ctx, "OnuUpgradeFsm image-downloaded on ONU received", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001878 //all fine, let the FSM proceed like defined from the sender of this event
1879 return
1880 }
1881 // waiting was aborted (assumed here to be caused by
mpagenko39b703e2021-08-25 13:38:40 +00001882 // error detection or cancel at download after upgrade FSM reset/abort with according image states set there)
mpagenkoc26d4c02021-05-06 14:27:57 +00001883 logger.Debugw(ctx, "OnuUpgradeFsm Waiting-ONU-download aborted", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001884 return
1885 }
1886}
mpagenko45586762021-10-01 08:30:22 +00001887
mpagenkoa2b288f2021-10-21 11:25:27 +00001888//waitOnAbortEndSwDlResponse waits for either abort/success or timeout of EndSwDownload (for abortion)
1889func (oFsm *OnuUpgradeFsm) waitOnAbortEndSwDlResponse(ctx context.Context) {
1890 logger.Debugw(ctx, "OnuUpgradeFsm start wait for EndSwDl response (abort)", log.Fields{"device-id": oFsm.deviceID})
1891 select {
1892 case <-time.After(oFsm.pOmciCC.GetMaxOmciTimeoutWithRetries() * time.Second):
1893 logger.Warnw(ctx, "OnuUpgradeFsm aborting download: timeout - no response received", log.Fields{"device-id": oFsm.deviceID})
1894 pUpgradeFsm := oFsm.PAdaptFsm
1895 if pUpgradeFsm != nil {
1896 _ = pUpgradeFsm.PFsm.Event(UpgradeEvRestart)
1897 } else {
1898 logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
1899 }
1900 return
1901 case response := <-oFsm.chReceiveAbortEndSwDlResponse:
1902 logger.Debugw(ctx, "OnuUpgradeFsm aborting download: response received",
1903 log.Fields{"device-id": oFsm.deviceID, "response": response})
1904 pUpgradeFsm := oFsm.PAdaptFsm
1905 if pUpgradeFsm != nil {
1906 if oFsm.abortingDlEvaluateResponse(ctx, pUpgradeFsm, response) {
1907 return //event sent from function already
1908 }
1909 _ = pUpgradeFsm.PFsm.Event(UpgradeEvRestart)
1910 } else {
1911 logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
1912 }
1913 return
1914 } //select
1915}
1916
mpagenko45586762021-10-01 08:30:22 +00001917//stateUpdateOnReset writes the download and/or image state on entering the reset state according to FSM internal indications
1918func (oFsm *OnuUpgradeFsm) stateUpdateOnReset(ctx context.Context) {
1919 oFsm.mutexUpgradeParams.Lock()
1920 defer oFsm.mutexUpgradeParams.Unlock()
1921 if !oFsm.conditionalCancelRequested {
1922 switch oFsm.upgradePhase {
1923 case cUpgradeUndefined, cUpgradeDownloading: //coming from downloading
1924 //make sure the download state is only changed in case the device has still been downloading
1925 if oFsm.volthaDownloadReason == voltha.ImageState_CANCELLED_ON_REQUEST {
1926 // indication for termination on request
1927 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_CANCELLED
1928 } else if oFsm.volthaDownloadReason != voltha.ImageState_NO_ERROR {
1929 // indication for termination on failure
1930 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_FAILED
1931 }
1932 //reset the image state from Downloading in this case
1933 oFsm.volthaImageState = voltha.ImageState_IMAGE_UNKNOWN //something like 'IMAGE_DOWNLOAD_ABORTED' would be better (proto)
1934 //in all other upgrade phases the last set download state remains valid
1935 case cUpgradeActivating:
1936 //reset the image state from Activating in this case
1937 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATION_ABORTED
1938 case cUpgradeCommitting: // indication for request to abort waiting for response
1939 //reset the image state from Activating in this case
1940 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMIT_ABORTED
1941 //default: in all other upgrade phases keep the last set imageState
1942 } //switch
1943 } else {
1944 //when reaching reset state with conditional cancel that can only result from ONU related problems
1945 // (mostly ONU down indication) - derived from resetFsms call
1946 // and it can only be related to the downloading-to-ONU phase (no need to check that additionally)
1947 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_FAILED
1948 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1949 //reset the image state from Downloading in this case
1950 oFsm.volthaImageState = voltha.ImageState_IMAGE_UNKNOWN //something like 'IMAGE_DOWNLOAD_ABORTED' would be better (proto)
1951 }
1952}
Holger Hildebrandte7cc6092022-02-01 11:37:03 +00001953
1954// PrepareForGarbageCollection - remove references to prepare for garbage collection
1955func (oFsm *OnuUpgradeFsm) PrepareForGarbageCollection(ctx context.Context, aDeviceID string) {
1956 logger.Debugw(ctx, "prepare for garbage collection", log.Fields{"device-id": aDeviceID})
1957 oFsm.pDeviceHandler = nil
1958 oFsm.pDownloadManager = nil
1959 oFsm.pFileManager = nil
1960 oFsm.pDevEntry = nil
1961 oFsm.pOmciCC = nil
1962}