blob: 9f78044a211d73f6af2b314503e500fcb95a0060 [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"
khenaidoo7d3c5582021-08-11 18:09:44 -040035 "github.com/opencord/voltha-protos/v5/go/voltha"
mpagenko80622a52021-02-09 16:53:23 +000036)
37
38const cMaxUint32 = ^uint32(0)
39
40const (
41 // internal predefined values - some off them should later be configurable (perhaps with theses as defaults)
42 cOmciDownloadSectionSize = 31 //in bytes
43 cOmciDownloadWindowSizeLimit = 31 //in sections for window offset (windowSize(32)-1)
44 //cOmciDownloadWindowRetryMax = 2 // max attempts for a specific window
mpagenkoc26d4c02021-05-06 14:27:57 +000045 cOmciSectionInterleaveMilliseconds = 0 //DownloadSection interleave time in milliseconds (0 for no delay)
46 cOmciEndSwDlDelaySeconds = 1 //End Software Download delay after last section (may be also configurable?)
47 cWaitCountEndSwDl = 6 //maximum number of EndSwDl requests
48 cWaitDelayEndSwDlSeconds = 10 //duration, how long is waited before next request on EndSwDl
mpagenko80622a52021-02-09 16:53:23 +000049 //cOmciDownloadCompleteTimeout = 5400 //in s for the complete timeout (may be better scale to image size/ noOfWindows)
50)
51
mpagenko45586762021-10-01 08:30:22 +000052// tEndSwDlResponseResult - Response result from EndSwDownload as used in channel indication
53type tUpgradePhase uint8
54
55const (
56 // undefined phase
57 cUpgradeUndefined tUpgradePhase = iota
58 // downloading image
59 cUpgradeDownloading
60 // image downloaded
61 cUpgradeDownloaded
62 // activating image
63 cUpgradeActivating
64 // image activated
65 cUpgradeActivated
66 // committing image
67 cUpgradeCommitting
68 // image committed
69 cUpgradeCommitted
70)
71
72// tEndSwDlResponseResult - Response result from EndSwDownload as used in channel indication
73type tEndSwDlResponseResult uint8
74
75const (
76 // response success
77 cEndSwDlResponseSuccess tEndSwDlResponseResult = iota
78 // response busy (repeat)
79 cEndSwDlResponseBusy
80 // response error or abort waiting for response
81 cEndSwDlResponseAbort
82)
83
84// upgrade FSM related events
mpagenko80622a52021-02-09 16:53:23 +000085const (
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000086 UpgradeEvStart = "UpgradeEvStart"
87 UpgradeEvDisable = "UpgradeEvDisable"
88 UpgradeEvAdapterDownload = "UpgradeEvAdapterDownload"
89 UpgradeEvPrepareSwDownload = "UpgradeEvPrepareSwDownload"
90 UpgradeEvRxStartSwDownload = "UpgradeEvRxStartSwDownload"
91 UpgradeEvWaitWindowAck = "UpgradeEvWaitWindowAck"
92 UpgradeEvContinueNextWindow = "UpgradeEvContinueNextWindow"
93 UpgradeEvEndSwDownload = "UpgradeEvEndSwDownload"
94 UpgradeEvWaitEndDownload = "UpgradeEvWaitEndDownload"
95 UpgradeEvContinueFinalize = "UpgradeEvContinueFinalize"
96 UpgradeEvCheckImageName = "UpgradeEvCheckImageName"
97 UpgradeEvWaitForActivate = "UpgradeEvWaitForActivate"
98 UpgradeEvRequestActivate = "UpgradeEvRequestActivate"
99 UpgradeEvActivationDone = "UpgradeEvActivationDone"
100 UpgradeEvWaitForCommit = "UpgradeEvWaitForCommit"
101 UpgradeEvCommitSw = "UpgradeEvCommitSw"
102 UpgradeEvCheckCommitted = "UpgradeEvCheckCommitted"
mpagenko80622a52021-02-09 16:53:23 +0000103
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000104 //UpgradeEvTimeoutSimple = "UpgradeEvTimeoutSimple"
105 //UpgradeEvTimeoutMids = "UpgradeEvTimeoutMids"
106 UpgradeEvReset = "UpgradeEvReset"
107 UpgradeEvAbort = "UpgradeEvAbort"
108 UpgradeEvRestart = "UpgradeEvRestart"
109 UpgradeEvAbortSwDownload = "UpgradeEvAbortSwDownload"
mpagenko80622a52021-02-09 16:53:23 +0000110)
111
mpagenko45586762021-10-01 08:30:22 +0000112// upgrade FSM related states
mpagenko80622a52021-02-09 16:53:23 +0000113const (
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000114 UpgradeStDisabled = "UpgradeStDisabled"
115 UpgradeStStarting = "UpgradeStStarting"
116 UpgradeStWaitingAdapterDL = "UpgradeStWaitingAdapterDL"
117 UpgradeStPreparingDL = "UpgradeStPreparingDL"
118 UpgradeStDLSection = "UpgradeStDLSection"
119 UpgradeStVerifyWindow = "UpgradeStVerifyWindow"
120 UpgradeStFinalizeDL = "UpgradeStFinalizeDL"
121 UpgradeStWaitEndDL = "UpgradeStWaitEndDL"
122 UpgradeStCheckImageName = "UpgradeStCheckImageName"
123 UpgradeStWaitForActivate = "UpgradeStWaitForActivate"
124 UpgradeStRequestingActivate = "UpgradeStRequestingActivate"
125 UpgradeStActivated = "UpgradeStActivated"
126 UpgradeStWaitForCommit = "UpgradeStWaitForCommit"
127 UpgradeStCommitSw = "UpgradeStCommitSw"
128 UpgradeStCheckCommitted = "UpgradeStCheckCommitted"
129 UpgradeStResetting = "UpgradeStResetting"
130 UpgradeStRestarting = "UpgradeStRestarting"
131 UpgradeStAbortingDL = "UpgradeStAbortingDL"
mpagenko80622a52021-02-09 16:53:23 +0000132)
133
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000134//COnuUpgradeFsmIdleState - required definition for IdleState detection for activities on OMCI
135const COnuUpgradeFsmIdleState = UpgradeStWaitForCommit
mpagenko80622a52021-02-09 16:53:23 +0000136
137//OnuUpgradeFsm defines the structure for the state machine to config the PON ANI ports of ONU UNI ports via OMCI
138type OnuUpgradeFsm struct {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000139 pDeviceHandler cmn.IdeviceHandler
140 pDownloadManager *AdapterDownloadManager
141 pFileManager *FileDownloadManager //used from R2.8 with new API version
mpagenko80622a52021-02-09 16:53:23 +0000142 deviceID string
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000143 pDevEntry cmn.IonuDeviceEntry
144 pOmciCC *cmn.OmciCC
145 pOnuDB *devdb.OnuDeviceDB
146 requestEvent cmn.OnuDeviceEvent
mpagenko80622a52021-02-09 16:53:23 +0000147 //omciMIdsResponseReceived chan bool //seperate channel needed for checking multiInstance OMCI message responses
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000148 PAdaptFsm *cmn.AdapterFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000149 pImageDsc *voltha.ImageDownload
150 imageBuffer []byte
151 origImageLength uint32 //as also limited by OMCI
152 imageCRC uint32 //as per OMCI - ITU I.363.5 crc
153 imageLength uint32 //including last bytes padding
154 omciDownloadWindowSizeLimit uint8 //windowSize-1 in sections
155 omciDownloadWindowSizeLast uint8 //number of sections in last window
156 noOfSections uint32 //uint32 range for sections should be sufficient for very long images
157 nextDownloadSectionsAbsolute uint32 //number of next section to download in overall image
158 nextDownloadSectionsWindow uint8 //number of next section to download within current window
159 noOfWindows uint32 //uint32 range for windows should be sufficient for very long images
160 nextDownloadWindow uint32 //number of next window to download
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000161 InactiveImageMeID uint16 //ME-ID of the inactive image
mpagenkoc26d4c02021-05-06 14:27:57 +0000162 downloadToOnuTimeout4MB time.Duration //timeout for downloading the image to the ONU for a 4MB image slice
163 omciSectionInterleaveDelay time.Duration //DownloadSectionInterleave delay in milliseconds
164 delayEndSwDl bool //flag to provide a delay between last section and EndSwDl
mpagenko45586762021-10-01 08:30:22 +0000165 repeatAbort bool //flag to indicate if OMCI EndSwDownload (abort) is to be repeated
mpagenkoc26d4c02021-05-06 14:27:57 +0000166 pLastTxMeInstance *me.ManagedEntity
167 waitCountEndSwDl uint8 //number, how often is waited for EndSwDl at maximum
168 waitDelayEndSwDl time.Duration //duration, how long is waited before next request on EndSwDl
169 chReceiveExpectedResponse chan bool
mpagenkoaa3afe92021-05-21 16:20:58 +0000170 useAPIVersion43 bool //flag for indication on which API version is used (and accordingly which specific methods)
171 mutexUpgradeParams sync.RWMutex //mutex to protect members for parallel function requests and omci response processing
172 imageVersion string //name of the image as used within OMCI (and on extrenal API interface)
173 imageIdentifier string //name of the image as used in the adapter
mpagenkoc26d4c02021-05-06 14:27:57 +0000174 mutexIsAwaitingAdapterDlResponse sync.RWMutex
175 chAdapterDlReady chan bool
mpagenko609a69a2021-12-02 08:12:58 +0000176 chAbortDelayEndSwDl chan struct{}
mpagenkoc26d4c02021-05-06 14:27:57 +0000177 isWaitingForAdapterDlResponse bool
mpagenkoc26d4c02021-05-06 14:27:57 +0000178 chOnuDlReady chan bool
mpagenkoc26d4c02021-05-06 14:27:57 +0000179 activateImage bool
180 commitImage bool
mpagenko38662d02021-08-11 09:45:19 +0000181 mutexAbortRequest sync.RWMutex
182 abortRequested voltha.ImageState_ImageFailureReason
183 conditionalCancelRequested bool
mpagenko45586762021-10-01 08:30:22 +0000184 upgradePhase tUpgradePhase
mpagenkoaa3afe92021-05-21 16:20:58 +0000185 volthaDownloadState voltha.ImageState_ImageDownloadState
186 volthaDownloadReason voltha.ImageState_ImageFailureReason
187 volthaImageState voltha.ImageState_ImageActivationState
Holger Hildebrandt288d9472021-09-06 10:47:53 +0000188 isEndSwDlOpen bool
mpagenko45586762021-10-01 08:30:22 +0000189 chReceiveAbortEndSwDlResponse chan tEndSwDlResponseResult
mpagenko80622a52021-02-09 16:53:23 +0000190}
191
192//NewOnuUpgradeFsm is the 'constructor' for the state machine to config the PON ANI ports
193// of ONU UNI ports via OMCI
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000194func NewOnuUpgradeFsm(ctx context.Context, apDeviceHandler cmn.IdeviceHandler,
195 apDevEntry cmn.IonuDeviceEntry, apOnuDB *devdb.OnuDeviceDB,
196 aRequestEvent cmn.OnuDeviceEvent, aName string, aCommChannel chan cmn.Message) *OnuUpgradeFsm {
mpagenko80622a52021-02-09 16:53:23 +0000197 instFsm := &OnuUpgradeFsm{
mpagenko59498c12021-03-18 14:15:15 +0000198 pDeviceHandler: apDeviceHandler,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000199 deviceID: apDeviceHandler.GetDeviceID(),
mpagenko38662d02021-08-11 09:45:19 +0000200 pDevEntry: apDevEntry,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000201 pOmciCC: apDevEntry.GetDevOmciCC(),
mpagenko59498c12021-03-18 14:15:15 +0000202 pOnuDB: apOnuDB,
203 requestEvent: aRequestEvent,
204 omciDownloadWindowSizeLimit: cOmciDownloadWindowSizeLimit,
205 omciSectionInterleaveDelay: cOmciSectionInterleaveMilliseconds,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000206 downloadToOnuTimeout4MB: apDeviceHandler.GetDlToOnuTimeout4M(),
mpagenko59498c12021-03-18 14:15:15 +0000207 waitCountEndSwDl: cWaitCountEndSwDl,
208 waitDelayEndSwDl: cWaitDelayEndSwDlSeconds,
mpagenko45586762021-10-01 08:30:22 +0000209 upgradePhase: cUpgradeUndefined,
210 volthaDownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN,
mpagenkoaa3afe92021-05-21 16:20:58 +0000211 volthaDownloadReason: voltha.ImageState_NO_ERROR,
212 volthaImageState: voltha.ImageState_IMAGE_UNKNOWN,
mpagenko38662d02021-08-11 09:45:19 +0000213 abortRequested: voltha.ImageState_NO_ERROR,
mpagenko80622a52021-02-09 16:53:23 +0000214 }
mpagenko59498c12021-03-18 14:15:15 +0000215 instFsm.chReceiveExpectedResponse = make(chan bool)
mpagenkoc26d4c02021-05-06 14:27:57 +0000216 instFsm.chAdapterDlReady = make(chan bool)
mpagenko609a69a2021-12-02 08:12:58 +0000217 instFsm.chAbortDelayEndSwDl = make(chan struct{})
mpagenkoc26d4c02021-05-06 14:27:57 +0000218 instFsm.chOnuDlReady = make(chan bool)
mpagenko45586762021-10-01 08:30:22 +0000219 instFsm.chReceiveAbortEndSwDlResponse = make(chan tEndSwDlResponseResult)
mpagenko80622a52021-02-09 16:53:23 +0000220
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000221 instFsm.PAdaptFsm = cmn.NewAdapterFsm(aName, instFsm.deviceID, aCommChannel)
222 if instFsm.PAdaptFsm == nil {
mpagenko80622a52021-02-09 16:53:23 +0000223 logger.Errorw(ctx, "OnuUpgradeFsm's AdapterFsm could not be instantiated!!", log.Fields{
224 "device-id": instFsm.deviceID})
225 return nil
226 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000227 instFsm.PAdaptFsm.PFsm = fsm.NewFSM(
228 UpgradeStDisabled,
mpagenko80622a52021-02-09 16:53:23 +0000229 fsm.Events{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000230 {Name: UpgradeEvStart, Src: []string{UpgradeStDisabled}, Dst: UpgradeStStarting},
231 {Name: UpgradeEvAdapterDownload, Src: []string{UpgradeStStarting}, Dst: UpgradeStWaitingAdapterDL},
232 {Name: UpgradeEvPrepareSwDownload, Src: []string{UpgradeStStarting, UpgradeStWaitingAdapterDL}, Dst: UpgradeStPreparingDL},
233 {Name: UpgradeEvRxStartSwDownload, Src: []string{UpgradeStPreparingDL}, Dst: UpgradeStDLSection},
234 {Name: UpgradeEvWaitWindowAck, Src: []string{UpgradeStDLSection}, Dst: UpgradeStVerifyWindow},
235 {Name: UpgradeEvContinueNextWindow, Src: []string{UpgradeStVerifyWindow}, Dst: UpgradeStDLSection},
236 {Name: UpgradeEvEndSwDownload, Src: []string{UpgradeStVerifyWindow}, Dst: UpgradeStFinalizeDL},
237 {Name: UpgradeEvWaitEndDownload, Src: []string{UpgradeStFinalizeDL}, Dst: UpgradeStWaitEndDL},
238 {Name: UpgradeEvContinueFinalize, Src: []string{UpgradeStWaitEndDL}, Dst: UpgradeStFinalizeDL},
239 //UpgradeStCheckImageName only used with useAPIVersion43
240 {Name: UpgradeEvCheckImageName, Src: []string{UpgradeStWaitEndDL}, Dst: UpgradeStCheckImageName},
241 //UpgradeEvWaitForActivate state transitions depend on useAPIVersion43
242 {Name: UpgradeEvWaitForActivate, Src: []string{UpgradeStWaitEndDL, UpgradeStCheckImageName}, Dst: UpgradeStWaitForActivate},
243 //UpgradeEvRequestActivate state transitions depend on useAPIVersion43
244 {Name: UpgradeEvRequestActivate, Src: []string{UpgradeStStarting, UpgradeStWaitEndDL, UpgradeStCheckImageName,
245 UpgradeStWaitForActivate}, Dst: UpgradeStRequestingActivate}, //allows also for direct activation (without download) [TODO!!!]
246 {Name: UpgradeEvActivationDone, Src: []string{UpgradeStRequestingActivate}, Dst: UpgradeStActivated},
247 {Name: UpgradeEvWaitForCommit, Src: []string{UpgradeStRequestingActivate}, Dst: UpgradeStWaitForCommit},
248 {Name: UpgradeEvCommitSw, Src: []string{UpgradeStStarting, UpgradeStRequestingActivate, UpgradeStWaitForCommit,
249 UpgradeStActivated}, Dst: UpgradeStCommitSw}, //allows also for direct commitment (without download) [TODO!!!]
250 {Name: UpgradeEvCheckCommitted, Src: []string{UpgradeStCommitSw}, Dst: UpgradeStCheckCommitted},
mpagenko80622a52021-02-09 16:53:23 +0000251
252 /*
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000253 {Name: UpgradeEvTimeoutSimple, Src: []string{
254 UpgradeStCreatingDot1PMapper, UpgradeStCreatingMBPCD, UpgradeStSettingTconts, UpgradeStSettingDot1PMapper}, Dst: UpgradeStStarting},
255 {Name: UpgradeEvTimeoutMids, Src: []string{
256 UpgradeStCreatingGemNCTPs, UpgradeStCreatingGemIWs, UpgradeStSettingPQs}, Dst: UpgradeStStarting},
mpagenko80622a52021-02-09 16:53:23 +0000257 */
258 // exceptional treatments
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000259 //on UpgradeEvReset: UpgradeStRequestingActivate, UpgradeStWaitForCommit and UpgradeStActivated are not reset
mpagenko1f8e8822021-06-25 14:10:21 +0000260 // (to let the FSM survive the expected OnuDown indication)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000261 {Name: UpgradeEvReset, Src: []string{UpgradeStStarting, UpgradeStWaitingAdapterDL, UpgradeStPreparingDL, UpgradeStDLSection,
262 UpgradeStVerifyWindow, UpgradeStDLSection, UpgradeStFinalizeDL, UpgradeStWaitEndDL, UpgradeStCheckImageName,
263 UpgradeStWaitForActivate,
mpagenko45586762021-10-01 08:30:22 +0000264 UpgradeStCommitSw, UpgradeStCheckCommitted, UpgradeStAbortingDL},
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000265 Dst: UpgradeStResetting},
266 {Name: UpgradeEvAbort, Src: []string{UpgradeStStarting, UpgradeStWaitingAdapterDL, UpgradeStPreparingDL, UpgradeStDLSection,
267 UpgradeStVerifyWindow, UpgradeStDLSection, UpgradeStFinalizeDL, UpgradeStWaitEndDL, UpgradeStCheckImageName,
268 UpgradeStWaitForActivate,
269 UpgradeStRequestingActivate, UpgradeStActivated, UpgradeStWaitForCommit,
270 UpgradeStCommitSw, UpgradeStCheckCommitted},
271 Dst: UpgradeStResetting},
272 {Name: UpgradeEvAbortSwDownload, Src: []string{UpgradeStResetting}, Dst: UpgradeStAbortingDL},
273 {Name: UpgradeEvRestart, Src: []string{UpgradeStResetting, UpgradeStAbortingDL}, Dst: UpgradeStRestarting},
274 {Name: UpgradeEvDisable, Src: []string{UpgradeStRestarting}, Dst: UpgradeStDisabled},
mpagenko80622a52021-02-09 16:53:23 +0000275 },
276 fsm.Callbacks{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000277 "enter_state": func(e *fsm.Event) { instFsm.PAdaptFsm.LogFsmStateChange(ctx, e) },
278 "enter_" + UpgradeStStarting: func(e *fsm.Event) { instFsm.enterStarting(ctx, e) },
279 "enter_" + UpgradeStWaitingAdapterDL: func(e *fsm.Event) { instFsm.enterWaitingAdapterDL(ctx, e) },
280 "enter_" + UpgradeStPreparingDL: func(e *fsm.Event) { instFsm.enterPreparingDL(ctx, e) },
281 "enter_" + UpgradeStDLSection: func(e *fsm.Event) { instFsm.enterDownloadSection(ctx, e) },
282 "enter_" + UpgradeStVerifyWindow: func(e *fsm.Event) { instFsm.enterVerifyWindow(ctx, e) },
283 "enter_" + UpgradeStFinalizeDL: func(e *fsm.Event) { instFsm.enterFinalizeDL(ctx, e) },
284 "enter_" + UpgradeStWaitEndDL: func(e *fsm.Event) { instFsm.enterWaitEndDL(ctx, e) },
285 "enter_" + UpgradeStCheckImageName: func(e *fsm.Event) { instFsm.enterCheckImageName(ctx, e) },
286 "enter_" + UpgradeStRequestingActivate: func(e *fsm.Event) { instFsm.enterActivateSw(ctx, e) },
287 "enter_" + UpgradeStCommitSw: func(e *fsm.Event) { instFsm.enterCommitSw(ctx, e) },
288 "enter_" + UpgradeStCheckCommitted: func(e *fsm.Event) { instFsm.enterCheckCommitted(ctx, e) },
289 "enter_" + UpgradeStResetting: func(e *fsm.Event) { instFsm.enterResetting(ctx, e) },
290 "enter_" + UpgradeStAbortingDL: func(e *fsm.Event) { instFsm.enterAbortingDL(ctx, e) },
291 "enter_" + UpgradeStRestarting: func(e *fsm.Event) { instFsm.enterRestarting(ctx, e) },
292 "enter_" + UpgradeStDisabled: func(e *fsm.Event) { instFsm.enterDisabled(ctx, e) },
mpagenko80622a52021-02-09 16:53:23 +0000293 },
294 )
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000295 if instFsm.PAdaptFsm.PFsm == nil {
mpagenko80622a52021-02-09 16:53:23 +0000296 logger.Errorw(ctx, "OnuUpgradeFsm's Base FSM could not be instantiated!!", log.Fields{
297 "device-id": instFsm.deviceID})
298 return nil
299 }
300
301 logger.Debugw(ctx, "OnuUpgradeFsm created", log.Fields{"device-id": instFsm.deviceID})
302 return instFsm
303}
304
305//SetDownloadParams configures the needed parameters for a specific download to the ONU
mpagenkoc26d4c02021-05-06 14:27:57 +0000306// called from 'old' API Activate_image_update()
mpagenko15ff4a52021-03-02 10:09:20 +0000307func (oFsm *OnuUpgradeFsm) SetDownloadParams(ctx context.Context, aInactiveImageID uint16,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000308 apImageDsc *voltha.ImageDownload, apDownloadManager *AdapterDownloadManager) error {
309 pBaseFsm := oFsm.PAdaptFsm.PFsm
310 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoaa3afe92021-05-21 16:20:58 +0000311 oFsm.mutexUpgradeParams.Lock()
mpagenko80622a52021-02-09 16:53:23 +0000312 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting", log.Fields{
313 "device-id": oFsm.deviceID, "image-description": apImageDsc})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000314 oFsm.InactiveImageMeID = aInactiveImageID //upgrade state machines run on configured inactive ImageId
mpagenko80622a52021-02-09 16:53:23 +0000315 oFsm.pImageDsc = apImageDsc
316 oFsm.pDownloadManager = apDownloadManager
Holger Hildebrandtac1e0592021-06-03 15:16:49 +0000317 oFsm.activateImage = true
318 oFsm.commitImage = true
mpagenkoaa3afe92021-05-21 16:20:58 +0000319 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000320
321 go func(aPBaseFsm *fsm.FSM) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000322 // let the upgrade FSM proceed to PreparingDL
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000323 _ = aPBaseFsm.Event(UpgradeEvPrepareSwDownload)
mpagenko80622a52021-02-09 16:53:23 +0000324 }(pBaseFsm)
325 return nil
326 }
327 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
328 "device-id": oFsm.deviceID})
329 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
330}
331
mpagenkoc26d4c02021-05-06 14:27:57 +0000332//SetDownloadParamsAfterDownload configures the needed parameters for a specific download to the ONU according to
333// updated API interface with R2.8: start download to ONU if the image is downloaded to the adapter
334// called from 'new' API Download_onu_image
335func (oFsm *OnuUpgradeFsm) SetDownloadParamsAfterDownload(ctx context.Context, aInactiveImageID uint16,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000336 apImageRequest *voltha.DeviceImageDownloadRequest, apDownloadManager *FileDownloadManager,
Holger Hildebrandtac010732021-06-02 13:35:39 +0000337 aImageIdentifier string) error {
mpagenkoc26d4c02021-05-06 14:27:57 +0000338 oFsm.mutexUpgradeParams.Lock()
339 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000340 if oFsm.PAdaptFsm != nil {
341 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000342 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000343 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000344 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting", log.Fields{
345 "device-id": oFsm.deviceID, "image-description": apImageRequest})
346 oFsm.useAPIVersion43 = true
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000347 oFsm.InactiveImageMeID = aInactiveImageID //upgrade state machines run on configured inactive ImageId
mpagenkoc26d4c02021-05-06 14:27:57 +0000348 oFsm.pFileManager = apDownloadManager
349 oFsm.imageIdentifier = aImageIdentifier
350 oFsm.imageVersion = apImageRequest.Image.Version
351 oFsm.activateImage = apImageRequest.ActivateOnSuccess
352 oFsm.commitImage = apImageRequest.CommitOnSuccess
mpagenko45586762021-10-01 08:30:22 +0000353 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_STARTED //state change indication for download request
mpagenkoc26d4c02021-05-06 14:27:57 +0000354 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000355 _ = pBaseFsm.Event(UpgradeEvAdapterDownload) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000356 return nil
357 }
358 oFsm.mutexUpgradeParams.Unlock()
359 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
360 "device-id": oFsm.deviceID})
361 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
362}
363
364//SetActivationParamsRunning sets the activate and commit flags for a running download to the ONU according to adapters rpc call
365// called from 'new' API Activate_onu_image
366func (oFsm *OnuUpgradeFsm) SetActivationParamsRunning(ctx context.Context,
367 aImageIdentifier string, aCommit bool) error {
mpagenko45586762021-10-01 08:30:22 +0000368 logger.Debugw(ctx, "OnuUpgradeFsm activate/commit parameter setting", log.Fields{
369 "device-id": oFsm.deviceID, "image-id": aImageIdentifier, "commit": aCommit})
mpagenkoc26d4c02021-05-06 14:27:57 +0000370 oFsm.mutexUpgradeParams.Lock()
371 //set activate/commit independent from state, if FSM is already beyond concerned states, then it does not matter anyway
372 // (as long as the Imageidentifier is correct)
mpagenkoc26d4c02021-05-06 14:27:57 +0000373 if aImageIdentifier != oFsm.imageIdentifier {
374 logger.Errorw(ctx, "OnuUpgradeFsm abort: mismatching upgrade image", log.Fields{
375 "device-id": oFsm.deviceID, "request-image": aImageIdentifier, "fsm-image": oFsm.imageIdentifier})
376 oFsm.mutexUpgradeParams.Unlock()
377 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm params ignored: requested image-name not used in current upgrade for device-id: %s",
378 oFsm.deviceID))
379 }
380 oFsm.activateImage = true
381 oFsm.commitImage = aCommit
mpagenko45586762021-10-01 08:30:22 +0000382 oFsm.mutexUpgradeParams.Unlock()
mpagenkoc26d4c02021-05-06 14:27:57 +0000383 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000384 if oFsm.PAdaptFsm != nil {
385 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000386 }
387 if pBaseFsm != nil {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000388 if pBaseFsm.Is(UpgradeStWaitForActivate) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000389 logger.Debugw(ctx, "OnuUpgradeFsm finish waiting for activate", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000390 _ = pBaseFsm.Event(UpgradeEvRequestActivate) //no need to call the FSM event in background here
mpagenko38662d02021-08-11 09:45:19 +0000391 } else {
mpagenko38662d02021-08-11 09:45:19 +0000392 logger.Debugw(ctx, "OnuUpgradeFsm not (yet?) waiting for activate", log.Fields{
393 "device-id": oFsm.deviceID, "current FsmState": pBaseFsm.Current()})
mpagenkoc26d4c02021-05-06 14:27:57 +0000394 }
395 return nil
396 }
397 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer", log.Fields{
398 "device-id": oFsm.deviceID})
399 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer for device-id: %s", oFsm.deviceID))
400}
401
402//SetActivationParamsStart starts upgrade processing with immediate activation
403// called from 'new' API Activate_onu_image
404func (oFsm *OnuUpgradeFsm) SetActivationParamsStart(ctx context.Context, aImageVersion string, aInactiveImageID uint16, aCommit bool) error {
405 oFsm.mutexUpgradeParams.Lock()
406 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000407 if oFsm.PAdaptFsm != nil {
408 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000409 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000410 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000411 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting to start with activation", log.Fields{
412 "device-id": oFsm.deviceID, "image-version": aImageVersion})
413 oFsm.useAPIVersion43 = true
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000414 oFsm.InactiveImageMeID = aInactiveImageID //upgrade state machines run on configured inactive ImageId
mpagenkoc26d4c02021-05-06 14:27:57 +0000415 oFsm.imageVersion = aImageVersion
416 oFsm.activateImage = true
417 oFsm.commitImage = aCommit
mpagenko38662d02021-08-11 09:45:19 +0000418 // indicate start of the upgrade activity
mpagenko45586762021-10-01 08:30:22 +0000419 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATING //state change indication for activate request
mpagenkoc26d4c02021-05-06 14:27:57 +0000420 oFsm.mutexUpgradeParams.Unlock()
421 //directly request the FSM to activate the image
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000422 _ = pBaseFsm.Event(UpgradeEvRequestActivate) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000423 return nil
424 }
425 oFsm.mutexUpgradeParams.Unlock()
426 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
427 "device-id": oFsm.deviceID})
428 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
429}
430
431//SetCommitmentParamsRunning sets the commit flag for a running download to the ONU according to adapters rpc call
432// called from 'new' API Commit_onu_image
mpagenko38662d02021-08-11 09:45:19 +0000433func (oFsm *OnuUpgradeFsm) SetCommitmentParamsRunning(ctx context.Context,
434 aImageIdentifier string, aImageVersion string) error {
mpagenkoc26d4c02021-05-06 14:27:57 +0000435 oFsm.mutexUpgradeParams.Lock()
436 //set commit independent from state, if FSM is already beyond commit state (just ready), then it does not matter anyway
437 // (as long as the Imageidentifier is correct)
438 logger.Debugw(ctx, "OnuUpgradeFsm commit parameter setting", log.Fields{
mpagenkoa2b288f2021-10-21 11:25:27 +0000439 "device-id": oFsm.deviceID, "image-id": aImageIdentifier, "image-version": aImageVersion})
mpagenko38662d02021-08-11 09:45:19 +0000440 if (aImageIdentifier != oFsm.imageIdentifier) && (aImageVersion != oFsm.imageVersion) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000441 logger.Errorw(ctx, "OnuUpgradeFsm abort: mismatching upgrade image", log.Fields{
mpagenko38662d02021-08-11 09:45:19 +0000442 "device-id": oFsm.deviceID, "request-identifier": aImageIdentifier, "fsm-identifier": oFsm.imageIdentifier,
443 "request-version": aImageVersion, "fsm-version": oFsm.imageVersion})
mpagenkoc26d4c02021-05-06 14:27:57 +0000444 oFsm.mutexUpgradeParams.Unlock()
445 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm params ignored: requested image-name not used in current upgrade for device-id: %s",
446 oFsm.deviceID))
447 }
448 oFsm.commitImage = true
449 oFsm.mutexUpgradeParams.Unlock()
450 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000451 if oFsm.PAdaptFsm != nil {
452 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000453 }
454 if pBaseFsm != nil {
mpagenko183647c2021-06-08 15:25:04 +0000455 //let the FSM decide if it is ready to process the event
456 logger.Debugw(ctx, "OnuUpgradeFsm requesting commit",
457 log.Fields{"device-id": oFsm.deviceID, "current FsmState": pBaseFsm.Current()})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000458 _ = pBaseFsm.Event(UpgradeEvCommitSw) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000459 return nil
460 }
mpagenko38662d02021-08-11 09:45:19 +0000461 //should never occur
mpagenkoc26d4c02021-05-06 14:27:57 +0000462 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer", log.Fields{
463 "device-id": oFsm.deviceID})
464 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer for device-id: %s", oFsm.deviceID))
465}
466
467//SetCommitmentParamsStart starts upgrade processing with immediate commitment
468// called from 'new' API Commit_onu_image
469func (oFsm *OnuUpgradeFsm) SetCommitmentParamsStart(ctx context.Context, aImageVersion string, aActiveImageID uint16) error {
470 oFsm.mutexUpgradeParams.Lock()
471 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000472 if oFsm.PAdaptFsm != nil {
473 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000474 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000475 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000476 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting to start with commitment", log.Fields{
477 "device-id": oFsm.deviceID, "image-version": aImageVersion})
478 oFsm.useAPIVersion43 = true
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000479 oFsm.InactiveImageMeID = aActiveImageID //upgrade state machines inactive ImageId is the new active ImageId
mpagenkoc26d4c02021-05-06 14:27:57 +0000480 oFsm.imageVersion = aImageVersion
481 oFsm.commitImage = true
mpagenko45586762021-10-01 08:30:22 +0000482 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTING //state change indication for activate request
mpagenkoc26d4c02021-05-06 14:27:57 +0000483 oFsm.mutexUpgradeParams.Unlock()
mpagenko38662d02021-08-11 09:45:19 +0000484 //directly request the FSM to commit the image
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000485 _ = pBaseFsm.Event(UpgradeEvCommitSw) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000486 return nil
487 }
488 oFsm.mutexUpgradeParams.Unlock()
489 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
490 "device-id": oFsm.deviceID})
491 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
492}
493
mpagenko1f8e8822021-06-25 14:10:21 +0000494//GetCommitFlag delivers the commit flag that was configured here
495func (oFsm *OnuUpgradeFsm) GetCommitFlag(ctx context.Context) bool {
496 oFsm.mutexUpgradeParams.RLock()
497 defer oFsm.mutexUpgradeParams.RUnlock()
498 return oFsm.commitImage
499}
500
mpagenko38662d02021-08-11 09:45:19 +0000501//GetImageStates delivers the download/image states as per device proto buf definition
mpagenkoaa3afe92021-05-21 16:20:58 +0000502func (oFsm *OnuUpgradeFsm) GetImageStates(ctx context.Context,
mpagenko38662d02021-08-11 09:45:19 +0000503 aImageIdentifier string, aVersion string) *voltha.ImageState {
mpagenkoaa3afe92021-05-21 16:20:58 +0000504 pImageState := &voltha.ImageState{}
mpagenko38662d02021-08-11 09:45:19 +0000505 pImageState.Version = aVersion //version as requested
mpagenkoaa3afe92021-05-21 16:20:58 +0000506 // check if the request refers to some active image/version of the processing
507 oFsm.mutexUpgradeParams.RLock()
508 if (aImageIdentifier == oFsm.imageIdentifier) || (aVersion == oFsm.imageVersion) {
509 pImageState.DownloadState = oFsm.volthaDownloadState
510 pImageState.Reason = oFsm.volthaDownloadReason
511 pImageState.ImageState = oFsm.volthaImageState
512 } else {
513 pImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
514 pImageState.Reason = voltha.ImageState_NO_ERROR
515 pImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
516 }
517 oFsm.mutexUpgradeParams.RUnlock()
mpagenko38662d02021-08-11 09:45:19 +0000518 return pImageState
mpagenkoaa3afe92021-05-21 16:20:58 +0000519}
520
mpagenko38662d02021-08-11 09:45:19 +0000521//SetImageStateActive sets the FSM internal volthaImageState to ImageState_IMAGE_ACTIVE
522func (oFsm *OnuUpgradeFsm) SetImageStateActive(ctx context.Context) {
mpagenko183647c2021-06-08 15:25:04 +0000523 oFsm.mutexUpgradeParams.Lock()
524 defer oFsm.mutexUpgradeParams.Unlock()
mpagenko45586762021-10-01 08:30:22 +0000525 oFsm.upgradePhase = cUpgradeActivated
mpagenko38662d02021-08-11 09:45:19 +0000526 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVE
mpagenko38662d02021-08-11 09:45:19 +0000527}
528
mpagenkoc26d4c02021-05-06 14:27:57 +0000529//CancelProcessing ensures that suspended processing at waiting on some response is aborted and reset of FSM
mpagenko38662d02021-08-11 09:45:19 +0000530func (oFsm *OnuUpgradeFsm) CancelProcessing(ctx context.Context, abCompleteAbort bool,
531 aReason voltha.ImageState_ImageFailureReason) {
mpagenkoa2b288f2021-10-21 11:25:27 +0000532 pAdaptFsm := oFsm.PAdaptFsm
533 if pAdaptFsm == nil || pAdaptFsm.PFsm == nil {
534 logger.Warnw(ctx, "OnuUpgradeFsm cancel, but FSM invalid", log.Fields{
535 "device-id": oFsm.deviceID})
536 return
537 }
538 logger.Debugw(ctx, "OnuUpgradeFsm start canceling", log.Fields{
539 "device-id": oFsm.deviceID, "in fsm-state": pAdaptFsm.PFsm.Current()})
mpagenko38662d02021-08-11 09:45:19 +0000540 oFsm.mutexAbortRequest.Lock()
541 oFsm.abortRequested = aReason //possibly abort the sectionDownload loop
542 oFsm.mutexAbortRequest.Unlock()
mpagenkoc26d4c02021-05-06 14:27:57 +0000543 //mutex protection is required for possible concurrent access to FSM members
544 //attention: for an unbuffered channel the sender is blocked until the value is received (processed)!
545 // accordingly the mutex must be released before sending to channel here (mutex acquired in receiver)
546 oFsm.mutexIsAwaitingAdapterDlResponse.RLock()
547 if oFsm.isWaitingForAdapterDlResponse {
548 oFsm.mutexIsAwaitingAdapterDlResponse.RUnlock()
549 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
550 oFsm.chAdapterDlReady <- false
551 } else {
552 oFsm.mutexIsAwaitingAdapterDlResponse.RUnlock()
553 }
mpagenko609a69a2021-12-02 08:12:58 +0000554
555 //abort a possible delaying of sending EndSwDl
556 //use asynchronous channel sending to avoid blocking here in case no receiver is waiting
557 select {
558 case oFsm.chAbortDelayEndSwDl <- struct{}{}:
559 default:
560 }
561
mpagenkoc26d4c02021-05-06 14:27:57 +0000562 //chOnuDlReady is cleared as part of the FSM reset processing (from enterResetting())
563
564 // 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 +0000565 // specific here: See definition of state changes: some states are excluded from reset for possible later commit
mpagenkoa2b288f2021-10-21 11:25:27 +0000566 pAdaptFsm = oFsm.PAdaptFsm
mpagenko59862f02021-10-11 08:53:18 +0000567 if pAdaptFsm != nil && pAdaptFsm.PFsm != nil {
mpagenkoc26d4c02021-05-06 14:27:57 +0000568 // calling FSM events in background to avoid blocking of the caller
mpagenko59862f02021-10-11 08:53:18 +0000569 go func(apFsm *fsm.FSM) {
570 if apFsm.Is(UpgradeStWaitEndDL) {
571 oFsm.chReceiveExpectedResponse <- false //which aborts the FSM in WaitEndDL state
572 } else if apFsm.Is(UpgradeStAbortingDL) {
573 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseAbort //abort waiting on EndDownloadResponse
574 }
mpagenko38662d02021-08-11 09:45:19 +0000575
mpagenko59862f02021-10-11 08:53:18 +0000576 var err error
577 if abCompleteAbort {
578 // in case of unconditional abort request the ImageState is set immediately
579 oFsm.mutexUpgradeParams.Lock()
580 //any previous lingering conditional cancelRequest is superseded by this abortion
581 oFsm.conditionalCancelRequested = false
582 oFsm.volthaDownloadReason = aReason
583 oFsm.mutexUpgradeParams.Unlock()
584 err = apFsm.Event(UpgradeEvAbort) //as unconditional default FSM cancellation
585 } else {
586 //at conditional abort request the image states are set when reaching the reset state
mpagenkoa2b288f2021-10-21 11:25:27 +0000587 oFsm.mutexUpgradeParams.Lock()
mpagenko59862f02021-10-11 08:53:18 +0000588 oFsm.conditionalCancelRequested = true
mpagenkoa2b288f2021-10-21 11:25:27 +0000589 oFsm.mutexUpgradeParams.Unlock()
mpagenko59862f02021-10-11 08:53:18 +0000590 err = apFsm.Event(UpgradeEvReset) //as state-conditional default FSM cleanup
591 }
592 if err != nil {
593 //error return is expected in case of conditional request and no state transition
594 logger.Debugw(ctx, "onu upgrade fsm could not cancel with abort/reset event", log.Fields{
595 "device-id": oFsm.deviceID, "error": err})
596 }
mpagenkoa2b288f2021-10-21 11:25:27 +0000597 logger.Debugw(ctx, "OnuUpgradeFsm canceling done", log.Fields{
598 "device-id": oFsm.deviceID})
mpagenko59862f02021-10-11 08:53:18 +0000599 }(pAdaptFsm.PFsm)
mpagenkoa2b288f2021-10-21 11:25:27 +0000600 } else { //the FSM seems already to be in some released state
601 logger.Warnw(ctx, "OnuUpgradeFsm canceling without FSM event", log.Fields{
602 "device-id": oFsm.deviceID})
603 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000604}
605
mpagenko80622a52021-02-09 16:53:23 +0000606func (oFsm *OnuUpgradeFsm) enterStarting(ctx context.Context, e *fsm.Event) {
607 logger.Debugw(ctx, "OnuUpgradeFsm start", log.Fields{"in state": e.FSM.Current(),
608 "device-id": oFsm.deviceID})
609
610 // start go routine for processing of LockState messages
611 go oFsm.processOmciUpgradeMessages(ctx)
612}
613
mpagenkoc26d4c02021-05-06 14:27:57 +0000614//enterWaitingAdapterDL state can only be reached with useAPIVersion43
615func (oFsm *OnuUpgradeFsm) enterWaitingAdapterDL(ctx context.Context, e *fsm.Event) {
616 logger.Debugw(ctx, "OnuUpgradeFsm waiting for adapter download", log.Fields{"in state": e.FSM.Current(),
617 "device-id": oFsm.deviceID})
mpagenko38662d02021-08-11 09:45:19 +0000618 syncChannel := make(chan struct{})
619 go oFsm.waitOnDownloadToAdapterReady(ctx, syncChannel, oFsm.chAdapterDlReady)
620 //block until the wait routine is really blocked on chAdapterDlReady
621 <-syncChannel
mpagenkoc26d4c02021-05-06 14:27:57 +0000622 go oFsm.pFileManager.RequestDownloadReady(ctx, oFsm.imageIdentifier, oFsm.chAdapterDlReady)
623}
624
mpagenko80622a52021-02-09 16:53:23 +0000625func (oFsm *OnuUpgradeFsm) enterPreparingDL(ctx context.Context, e *fsm.Event) {
626 logger.Debugw(ctx, "OnuUpgradeFsm prepare Download to Onu", log.Fields{"in state": e.FSM.Current(),
627 "device-id": oFsm.deviceID})
628
mpagenkoc26d4c02021-05-06 14:27:57 +0000629 var fileLen int64
630 var err error
mpagenkoaa3afe92021-05-21 16:20:58 +0000631 oFsm.mutexUpgradeParams.Lock()
mpagenkoc26d4c02021-05-06 14:27:57 +0000632 if oFsm.useAPIVersion43 {
633 //with the new API structure download to adapter is implicit and we have to wait until the image is available
634 fileLen, err = oFsm.pFileManager.GetImageBufferLen(ctx, oFsm.imageIdentifier)
635 } else {
636 fileLen, err = oFsm.pDownloadManager.getImageBufferLen(ctx, oFsm.pImageDsc.Name, oFsm.pImageDsc.LocalDir)
637 }
mpagenkoc497ee32021-11-10 17:30:20 +0000638 if err != nil || fileLen == 0 || fileLen > int64(cMaxUint32) {
mpagenko39b703e2021-08-25 13:38:40 +0000639 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto)
mpagenkoaa3afe92021-05-21 16:20:58 +0000640 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000641 logger.Errorw(ctx, "OnuUpgradeFsm abort: problems getting image buffer length", log.Fields{
642 "device-id": oFsm.deviceID, "error": err, "length": fileLen})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000643 pBaseFsm := oFsm.PAdaptFsm
mpagenko80622a52021-02-09 16:53:23 +0000644 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000645 go func(a_pAFsm *cmn.AdapterFsm) {
646 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko80622a52021-02-09 16:53:23 +0000647 }(pBaseFsm)
648 return
649 }
650
mpagenkoc26d4c02021-05-06 14:27:57 +0000651 //copy file content to buffer
mpagenkoc497ee32021-11-10 17:30:20 +0000652 var imageBuffer []byte
mpagenkoc26d4c02021-05-06 14:27:57 +0000653 if oFsm.useAPIVersion43 {
mpagenkoc497ee32021-11-10 17:30:20 +0000654 imageBuffer, err = oFsm.pFileManager.GetDownloadImageBuffer(ctx, oFsm.imageIdentifier)
mpagenkoc26d4c02021-05-06 14:27:57 +0000655 } else {
mpagenkoc497ee32021-11-10 17:30:20 +0000656 imageBuffer, err = oFsm.pDownloadManager.getDownloadImageBuffer(ctx, oFsm.pImageDsc.Name, oFsm.pImageDsc.LocalDir)
mpagenkoc26d4c02021-05-06 14:27:57 +0000657 }
mpagenko80622a52021-02-09 16:53:23 +0000658 if err != nil {
mpagenko39b703e2021-08-25 13:38:40 +0000659 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto)
mpagenkoaa3afe92021-05-21 16:20:58 +0000660 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000661 logger.Errorw(ctx, "OnuUpgradeFsm abort: can't get image buffer", log.Fields{
662 "device-id": oFsm.deviceID, "error": err})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000663 pBaseFsm := oFsm.PAdaptFsm
mpagenko80622a52021-02-09 16:53:23 +0000664 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000665 go func(a_pAFsm *cmn.AdapterFsm) {
666 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko80622a52021-02-09 16:53:23 +0000667 }(pBaseFsm)
668 return
669 }
mpagenkoc497ee32021-11-10 17:30:20 +0000670 //provide slice capacity already with the reserve of one section to avoid inflation of the slice to double size at append
671 oFsm.imageBuffer = make([]byte, fileLen, fileLen+cOmciDownloadSectionSize)
672 //better use a copy of the read image buffer in case the buffer/file is modified from outside,
673 // this also limits the slice len to the expected maximum fileLen
674 copy(oFsm.imageBuffer, imageBuffer)
mpagenko80622a52021-02-09 16:53:23 +0000675
676 oFsm.noOfSections = uint32(fileLen / cOmciDownloadSectionSize)
677 if fileLen%cOmciDownloadSectionSize > 0 {
mpagenkoc26d4c02021-05-06 14:27:57 +0000678 bufferPadding := make([]byte, cOmciDownloadSectionSize-uint32((fileLen)%cOmciDownloadSectionSize))
mpagenko80622a52021-02-09 16:53:23 +0000679 //expand the imageBuffer to exactly fit multiples of cOmciDownloadSectionSize with padding
mpagenkoc497ee32021-11-10 17:30:20 +0000680 oFsm.imageBuffer = append(oFsm.imageBuffer, bufferPadding...)
mpagenko80622a52021-02-09 16:53:23 +0000681 oFsm.noOfSections++
682 }
683 oFsm.origImageLength = uint32(fileLen)
684 oFsm.imageLength = uint32(len(oFsm.imageBuffer))
mpagenko80622a52021-02-09 16:53:23 +0000685 logger.Infow(ctx, "OnuUpgradeFsm starts with StartSwDl values", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000686 "MeId": oFsm.InactiveImageMeID, "windowSizeLimit": oFsm.omciDownloadWindowSizeLimit,
mpagenko80622a52021-02-09 16:53:23 +0000687 "ImageSize": oFsm.imageLength, "original file size": fileLen})
688 //"NumberOfCircuitPacks": oFsm.numberCircuitPacks, "CircuitPacks MeId": 0}) //parallel circuit packs download not supported
mpagenkoaa3afe92021-05-21 16:20:58 +0000689
690 oFsm.mutexUpgradeParams.Unlock()
mpagenko39b703e2021-08-25 13:38:40 +0000691
mpagenkoa2b288f2021-10-21 11:25:27 +0000692 // flush chOnuDlReady
mpagenko39b703e2021-08-25 13:38:40 +0000693 select {
694 case <-oFsm.chOnuDlReady:
695 logger.Debug(ctx, "flushed OnuDlReady channel")
696 default:
697 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000698 go oFsm.waitOnDownloadToOnuReady(ctx, oFsm.chOnuDlReady) // start supervision of the complete download-to-ONU procedure
699
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000700 err = oFsm.pOmciCC.SendStartSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
701 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, oFsm.omciDownloadWindowSizeLimit, oFsm.origImageLength)
mpagenko80622a52021-02-09 16:53:23 +0000702 if err != nil {
703 logger.Errorw(ctx, "StartSwDl abort: can't send section", log.Fields{
704 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +0000705 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +0000706 return
707 }
Holger Hildebrandt288d9472021-09-06 10:47:53 +0000708 oFsm.isEndSwDlOpen = true
mpagenko80622a52021-02-09 16:53:23 +0000709}
710
711func (oFsm *OnuUpgradeFsm) enterDownloadSection(ctx context.Context, e *fsm.Event) {
712 logger.Debugw(ctx, "OnuUpgradeFsm start downloading sections", log.Fields{
713 "device-id": oFsm.deviceID, "absolute window": oFsm.nextDownloadWindow})
mpagenkoa2b288f2021-10-21 11:25:27 +0000714 //use a background routine to send the multiple download sections frames in a loop
mpagenko609a69a2021-12-02 08:12:58 +0000715 // in order to avoid blocking on synchronous event calls for the entire (long) processing time
mpagenkoa2b288f2021-10-21 11:25:27 +0000716 go oFsm.runSwDlSectionWindow(ctx)
717}
mpagenko80622a52021-02-09 16:53:23 +0000718
mpagenkoa2b288f2021-10-21 11:25:27 +0000719//runSwDlSectionWindow runs a loop to send all DlSection frames of one window in background
720// may be aborted by parallel change of abortRequested
721func (oFsm *OnuUpgradeFsm) runSwDlSectionWindow(ctx context.Context) {
mpagenko80622a52021-02-09 16:53:23 +0000722 var windowAckRequest uint8 = 0
723 var bufferStartOffset uint32
724 var bufferEndOffset uint32
725 var downloadSection []byte
mpagenkoa2b288f2021-10-21 11:25:27 +0000726 framePrint := false //default no printing of downloadSection frames
mpagenkoaa3afe92021-05-21 16:20:58 +0000727 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +0000728 oFsm.upgradePhase = cUpgradeDownloading //start of downloading image to ONU
mpagenko80622a52021-02-09 16:53:23 +0000729 if oFsm.nextDownloadSectionsAbsolute == 0 {
730 //debug print of first section frame
mpagenkoa2b288f2021-10-21 11:25:27 +0000731 framePrint = true
mpagenkoaa3afe92021-05-21 16:20:58 +0000732 oFsm.volthaImageState = voltha.ImageState_IMAGE_DOWNLOADING
mpagenko80622a52021-02-09 16:53:23 +0000733 }
mpagenko80622a52021-02-09 16:53:23 +0000734 for {
mpagenko38662d02021-08-11 09:45:19 +0000735 oFsm.mutexAbortRequest.RLock()
736 // this way out of the section download loop on abort request
737 if oFsm.abortRequested != voltha.ImageState_NO_ERROR {
mpagenko45586762021-10-01 08:30:22 +0000738 //states are updated when entering the reset state ...
mpagenko38662d02021-08-11 09:45:19 +0000739 oFsm.volthaDownloadReason = oFsm.abortRequested
740 oFsm.mutexAbortRequest.RUnlock()
741 oFsm.mutexUpgradeParams.Unlock()
mpagenkoa2b288f2021-10-21 11:25:27 +0000742 pUpgradeFsm := oFsm.PAdaptFsm
743 if pUpgradeFsm != nil {
744 _ = pUpgradeFsm.PFsm.Event(UpgradeEvAbort)
745 logger.Debugw(ctx, "aborting runSwDlSectionWindow", log.Fields{
746 "device-id": oFsm.deviceID, "reason": oFsm.volthaDownloadReason})
747 return
748 }
749 logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
mpagenko38662d02021-08-11 09:45:19 +0000750 return
751 }
752 oFsm.mutexAbortRequest.RUnlock()
753
mpagenko80622a52021-02-09 16:53:23 +0000754 bufferStartOffset = oFsm.nextDownloadSectionsAbsolute * cOmciDownloadSectionSize
755 bufferEndOffset = bufferStartOffset + cOmciDownloadSectionSize - 1 //for representing cOmciDownloadSectionSizeLimit values
756 logger.Debugw(ctx, "DlSection values are", log.Fields{
757 "DlSectionNoAbsolute": oFsm.nextDownloadSectionsAbsolute,
758 "DlSectionWindow": oFsm.nextDownloadSectionsWindow,
759 "startOffset": bufferStartOffset, "endOffset": bufferEndOffset})
760 if bufferStartOffset+1 > oFsm.imageLength || bufferEndOffset+1 > oFsm.imageLength { //should never occur in this state
761 logger.Errorw(ctx, "OnuUpgradeFsm buffer error: exceeded length", log.Fields{
762 "device-id": oFsm.deviceID, "bufferStartOffset": bufferStartOffset,
763 "bufferEndOffset": bufferEndOffset, "imageLength": oFsm.imageLength})
mpagenko39b703e2021-08-25 13:38:40 +0000764 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto)
mpagenkoaa3afe92021-05-21 16:20:58 +0000765 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000766 //logical error -- reset the FSM
mpagenkoa2b288f2021-10-21 11:25:27 +0000767 pUpgradeFsm := oFsm.PAdaptFsm
768 if pUpgradeFsm != nil {
769 _ = pUpgradeFsm.PFsm.Event(UpgradeEvAbort)
770 return
771 }
772 logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
mpagenko80622a52021-02-09 16:53:23 +0000773 return
774 }
775 downloadSection = oFsm.imageBuffer[bufferStartOffset : bufferEndOffset+1]
776 if oFsm.nextDownloadSectionsWindow == oFsm.omciDownloadWindowSizeLimit {
777 windowAckRequest = 1
778 logger.Debugw(ctx, "DlSection expect Response for complete window", log.Fields{
779 "device-id": oFsm.deviceID, "in window": oFsm.nextDownloadWindow})
780 }
781 if oFsm.nextDownloadSectionsAbsolute+1 >= oFsm.noOfSections {
782 windowAckRequest = 1
mpagenkoa2b288f2021-10-21 11:25:27 +0000783 framePrint = true //debug print of last frame
mpagenko15ff4a52021-03-02 10:09:20 +0000784 oFsm.omciDownloadWindowSizeLast = oFsm.nextDownloadSectionsWindow
785 logger.Infow(ctx, "DlSection expect Response for last window (section)", log.Fields{
mpagenko80622a52021-02-09 16:53:23 +0000786 "device-id": oFsm.deviceID, "DlSectionNoAbsolute": oFsm.nextDownloadSectionsAbsolute})
787 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000788 oFsm.mutexUpgradeParams.Unlock() //unlock here to give other functions some chance to process during/after the send request
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000789 err := oFsm.pOmciCC.SendDownloadSection(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
mpagenkoa2b288f2021-10-21 11:25:27 +0000790 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, windowAckRequest, oFsm.nextDownloadSectionsWindow, downloadSection, framePrint)
mpagenko80622a52021-02-09 16:53:23 +0000791 if err != nil {
792 logger.Errorw(ctx, "DlSection abort: can't send section", log.Fields{
mpagenko15ff4a52021-03-02 10:09:20 +0000793 "device-id": oFsm.deviceID, "section absolute": oFsm.nextDownloadSectionsAbsolute, "error": err})
mpagenkoa2b288f2021-10-21 11:25:27 +0000794 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +0000795 return
796 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000797 oFsm.mutexUpgradeParams.Lock()
mpagenko80622a52021-02-09 16:53:23 +0000798 oFsm.nextDownloadSectionsAbsolute++ //always increase the absolute section counter after having sent one
799 if windowAckRequest == 1 {
mpagenkoaa3afe92021-05-21 16:20:58 +0000800 oFsm.mutexUpgradeParams.Unlock()
mpagenkoa2b288f2021-10-21 11:25:27 +0000801 pUpgradeFsm := oFsm.PAdaptFsm
802 if pUpgradeFsm != nil {
803 _ = pUpgradeFsm.PFsm.Event(UpgradeEvWaitWindowAck) //state transition to upgradeStVerifyWindow
804 return
805 }
806 logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
mpagenko80622a52021-02-09 16:53:23 +0000807 return
808 }
mpagenkoa2b288f2021-10-21 11:25:27 +0000809 framePrint = false //for the next Section frame (if wanted, can be enabled in logic before sendXXX())
mpagenko80622a52021-02-09 16:53:23 +0000810 oFsm.nextDownloadSectionsWindow++ //increase the window related section counter only if not in the last section
mpagenko59498c12021-03-18 14:15:15 +0000811 if oFsm.omciSectionInterleaveDelay > 0 {
mpagenko80622a52021-02-09 16:53:23 +0000812 //ensure a defined intersection-time-gap to leave space for further processing, other ONU's ...
mpagenkoaa3afe92021-05-21 16:20:58 +0000813 oFsm.mutexUpgradeParams.Unlock() //unlock here to give other functions some chance to process during/after the send request
mpagenko59498c12021-03-18 14:15:15 +0000814 time.Sleep(oFsm.omciSectionInterleaveDelay * time.Millisecond)
mpagenkoaa3afe92021-05-21 16:20:58 +0000815 oFsm.mutexUpgradeParams.Lock()
mpagenko80622a52021-02-09 16:53:23 +0000816 }
817 }
mpagenkoa2b288f2021-10-21 11:25:27 +0000818} //runSwDlSectionWindow
mpagenko80622a52021-02-09 16:53:23 +0000819
820func (oFsm *OnuUpgradeFsm) enterVerifyWindow(ctx context.Context, e *fsm.Event) {
821 logger.Debugw(ctx, "OnuUpgradeFsm verify DL window ack", log.Fields{
822 "for window": oFsm.nextDownloadWindow, "device-id": oFsm.deviceID})
823}
824
825func (oFsm *OnuUpgradeFsm) enterFinalizeDL(ctx context.Context, e *fsm.Event) {
mpagenko80622a52021-02-09 16:53:23 +0000826 logger.Infow(ctx, "OnuUpgradeFsm finalize DL", log.Fields{
mpagenko59498c12021-03-18 14:15:15 +0000827 "device-id": oFsm.deviceID, "crc": strconv.FormatInt(int64(oFsm.imageCRC), 16), "delay": oFsm.delayEndSwDl})
mpagenko609a69a2021-12-02 08:12:58 +0000828 //use a background routine to wait EndSwDlDelay and then send the EndSwDl request
829 // in order to avoid blocking on synchronous event calls for the complete wait time
830 // and to allow for state transition before sending the EndSwDl request
831 go oFsm.delayAndSendEndSwDl(ctx)
832}
mpagenko80622a52021-02-09 16:53:23 +0000833
mpagenko609a69a2021-12-02 08:12:58 +0000834//delayAndSendEndSwDl ensures a delay before sending the EndSwDl request
835// may also be aborted by parallel channel reception on chAbortEndSwDl
836func (oFsm *OnuUpgradeFsm) delayAndSendEndSwDl(ctx context.Context) {
mpagenkoaa3afe92021-05-21 16:20:58 +0000837 oFsm.mutexUpgradeParams.RLock()
mpagenko80622a52021-02-09 16:53:23 +0000838 if oFsm.delayEndSwDl {
mpagenkoaa3afe92021-05-21 16:20:58 +0000839 oFsm.mutexUpgradeParams.RUnlock()
mpagenko609a69a2021-12-02 08:12:58 +0000840 //give the ONU some time for image evaluation (hoping it does not only start this evaluation on first EndSwDl itself)
841 logger.Debugw(ctx, "OnuUpgradeFsm delay EndSwDl", log.Fields{"device-id": oFsm.deviceID,
842 "duration_s": cOmciEndSwDlDelaySeconds})
843 select {
844 case <-time.After(cOmciEndSwDlDelaySeconds * time.Second):
845 logger.Warnw(ctx, "OnuUpgradeFsm start sending EndSwDl", log.Fields{
846 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier})
847 case <-oFsm.chAbortDelayEndSwDl:
848 logger.Debugw(ctx, "OnuUpgradeFsm abort request to send EndSwDl", log.Fields{"device-id": oFsm.deviceID})
849 //according further state transition is ensured by the entity that sent the abort
850 return
851 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000852 } else {
853 oFsm.mutexUpgradeParams.RUnlock()
mpagenko80622a52021-02-09 16:53:23 +0000854 }
855
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000856 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000857 if pBaseFsm == nil {
mpagenko38662d02021-08-11 09:45:19 +0000858 logger.Errorw(ctx, "EndSwDl abort: BaseFsm invalid", log.Fields{"device-id": oFsm.deviceID})
859 oFsm.mutexUpgradeParams.Lock()
mpagenko38662d02021-08-11 09:45:19 +0000860 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR
861 oFsm.mutexUpgradeParams.Unlock()
mpagenko59498c12021-03-18 14:15:15 +0000862 // Can't call FSM Event directly, decoupling it
mpagenko609a69a2021-12-02 08:12:58 +0000863 _ = pBaseFsm.PFsm.Event(UpgradeEvAbort)
mpagenko59498c12021-03-18 14:15:15 +0000864 return
865 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000866 err := oFsm.pOmciCC.SendEndSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
867 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, oFsm.origImageLength, oFsm.imageCRC)
Holger Hildebrandt288d9472021-09-06 10:47:53 +0000868
mpagenko80622a52021-02-09 16:53:23 +0000869 if err != nil {
mpagenkoa2b288f2021-10-21 11:25:27 +0000870 logger.Errorw(ctx, "EndSwDl abort: error sending EndSwDl", log.Fields{
mpagenko80622a52021-02-09 16:53:23 +0000871 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +0000872 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +0000873 return
874 }
mpagenko609a69a2021-12-02 08:12:58 +0000875 //from here on sending of EndSwDl(Abort) is not needed anymore (even though response is not yet received)
876 // this avoids sending of both EndSwDl(Success) and EndSwDl(Abort) when cancellation falls just into this window
877 // the ONU must theoretically be prepared to receive none of them (in case of OMCI transfer issues) e.g. by timeout
878 oFsm.isEndSwDlOpen = false
879 // wait for the EndSwDLResponse and check, if the ONU is ready for activation
880 _ = pBaseFsm.PFsm.Event(UpgradeEvWaitEndDownload)
881} //delayAndSendEndSwDl
mpagenko59498c12021-03-18 14:15:15 +0000882
883func (oFsm *OnuUpgradeFsm) enterWaitEndDL(ctx context.Context, e *fsm.Event) {
884 logger.Infow(ctx, "OnuUpgradeFsm WaitEndDl", log.Fields{
885 "device-id": oFsm.deviceID, "wait delay": oFsm.waitDelayEndSwDl * time.Second, "wait count": oFsm.waitCountEndSwDl})
886 if oFsm.waitCountEndSwDl == 0 {
887 logger.Errorw(ctx, "WaitEndDl abort: max limit of EndSwDL reached", log.Fields{
888 "device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000889 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000890 if pBaseFsm == nil {
891 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
892 "device-id": oFsm.deviceID})
893 return
894 }
mpagenko38662d02021-08-11 09:45:19 +0000895 oFsm.mutexUpgradeParams.Lock()
mpagenko39b703e2021-08-25 13:38:40 +0000896 oFsm.volthaDownloadReason = voltha.ImageState_IMAGE_REFUSED_BY_ONU //something like 'END_DOWNLOAD_TIMEOUT' would be better (proto)
mpagenko38662d02021-08-11 09:45:19 +0000897 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000898 go func(a_pAFsm *cmn.AdapterFsm) {
899 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko59498c12021-03-18 14:15:15 +0000900 }(pBaseFsm)
901 return
902 }
903
904 oFsm.waitCountEndSwDl--
905 select {
906 case <-time.After(oFsm.waitDelayEndSwDl * time.Second):
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000907 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000908 if pBaseFsm == nil {
909 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
910 "device-id": oFsm.deviceID})
911 //FSM may be reset already from somewhere else, nothing we can do here anymore
912 return
913 }
914 //retry End SW DL
mpagenkoaa3afe92021-05-21 16:20:58 +0000915 oFsm.mutexUpgradeParams.Lock()
mpagenko59498c12021-03-18 14:15:15 +0000916 oFsm.delayEndSwDl = false //no more extra delay for the request
mpagenkoaa3afe92021-05-21 16:20:58 +0000917 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000918 go func(a_pAFsm *cmn.AdapterFsm) {
919 _ = a_pAFsm.PFsm.Event(UpgradeEvContinueFinalize)
mpagenko59498c12021-03-18 14:15:15 +0000920 }(pBaseFsm)
921 return
922 case success := <-oFsm.chReceiveExpectedResponse:
923 logger.Debugw(ctx, "WaitEndDl stop wait timer", log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +0000924 oFsm.isEndSwDlOpen = false //no request to abort of download (already finished or immediate abort)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000925 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000926 if pBaseFsm == nil {
927 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
928 "device-id": oFsm.deviceID})
929 //FSM may be reset already from somewhere else, nothing we can do here anymore
930 return
931 }
932 if success {
933 //answer received with ready indication
mpagenko38662d02021-08-11 09:45:19 +0000934 //useAPIVersion43 may not conflict in concurrency in this state function
935 if oFsm.useAPIVersion43 { // newer API usage requires verification of downloaded image version
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000936 go func(a_pAFsm *cmn.AdapterFsm) {
937 _ = a_pAFsm.PFsm.Event(UpgradeEvCheckImageName)
mpagenkoc26d4c02021-05-06 14:27:57 +0000938 }(pBaseFsm)
mpagenko38662d02021-08-11 09:45:19 +0000939 } else { // elder API usage does not support image version check -immediately consider download as successful
940 if oFsm.activateImage {
941 //immediate activation requested
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000942 go func(a_pAFsm *cmn.AdapterFsm) {
943 _ = a_pAFsm.PFsm.Event(UpgradeEvRequestActivate)
mpagenko38662d02021-08-11 09:45:19 +0000944 }(pBaseFsm)
945 } else {
946 //have to wait on explicit activation request
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000947 go func(a_pAFsm *cmn.AdapterFsm) {
948 _ = a_pAFsm.PFsm.Event(UpgradeEvWaitForActivate)
mpagenko38662d02021-08-11 09:45:19 +0000949 }(pBaseFsm)
950 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000951 }
mpagenko59498c12021-03-18 14:15:15 +0000952 return
953 }
954 //timer was aborted
mpagenko45586762021-10-01 08:30:22 +0000955 oFsm.abortOnOmciError(ctx, true)
mpagenko59498c12021-03-18 14:15:15 +0000956 return
957 }
mpagenko80622a52021-02-09 16:53:23 +0000958}
959
mpagenko38662d02021-08-11 09:45:19 +0000960func (oFsm *OnuUpgradeFsm) enterCheckImageName(ctx context.Context, e *fsm.Event) {
961 logger.Debugw(ctx, "OnuUpgradeFsm checking downloaded image name", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000962 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
mpagenko38662d02021-08-11 09:45:19 +0000963 requestedAttributes := me.AttributeValueMap{"IsCommitted": 0, "IsActive": 0, "Version": ""}
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000964 meInstance, err := oFsm.pOmciCC.SendGetMe(log.WithSpanFromContext(context.Background(), ctx),
965 me.SoftwareImageClassID, oFsm.InactiveImageMeID, requestedAttributes, oFsm.pDeviceHandler.GetOmciTimeout(),
966 false, oFsm.PAdaptFsm.CommChan)
mpagenko38662d02021-08-11 09:45:19 +0000967 if err != nil {
968 logger.Errorw(ctx, "OnuUpgradeFsm get Software Image ME result error",
969 log.Fields{"device-id": oFsm.deviceID, "Error": err})
mpagenko45586762021-10-01 08:30:22 +0000970 oFsm.abortOnOmciError(ctx, true)
mpagenko38662d02021-08-11 09:45:19 +0000971 return
972 }
973 oFsm.pLastTxMeInstance = meInstance
974}
975
mpagenko80622a52021-02-09 16:53:23 +0000976func (oFsm *OnuUpgradeFsm) enterActivateSw(ctx context.Context, e *fsm.Event) {
977 logger.Infow(ctx, "OnuUpgradeFsm activate SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000978 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
mpagenko80622a52021-02-09 16:53:23 +0000979
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000980 err := oFsm.pOmciCC.SendActivateSoftware(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
981 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID)
mpagenko80622a52021-02-09 16:53:23 +0000982 if err != nil {
983 logger.Errorw(ctx, "ActivateSw abort: can't send activate frame", log.Fields{
984 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +0000985 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +0000986 return
987 }
mpagenko38662d02021-08-11 09:45:19 +0000988 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +0000989 oFsm.upgradePhase = cUpgradeActivating //start of image activation for ONU
mpagenko38662d02021-08-11 09:45:19 +0000990 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATING
991 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000992}
993
994func (oFsm *OnuUpgradeFsm) enterCommitSw(ctx context.Context, e *fsm.Event) {
mpagenko38662d02021-08-11 09:45:19 +0000995 logger.Debugw(ctx, "OnuUpgradeFsm start commit SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000996 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
mpagenko38662d02021-08-11 09:45:19 +0000997 //any abort request (also conditional) is still regarded as valid as the commit indication might not be possible to verify
998 // (which is a bit problematic as the ONU might already be in committed state,
999 // in this case (committing failed) always 'onuimage list' should be used to verify the real state (if ONU is reachable))
1000 if activeImageID, err := oFsm.pDevEntry.GetActiveImageMeID(ctx); err == nil {
mpagenkoaa3afe92021-05-21 16:20:58 +00001001 oFsm.mutexUpgradeParams.Lock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001002 if activeImageID == oFsm.InactiveImageMeID {
1003 inactiveImageID := oFsm.InactiveImageMeID
mpagenko15ff4a52021-03-02 10:09:20 +00001004 logger.Infow(ctx, "OnuUpgradeFsm commit SW", log.Fields{
mpagenkoaa3afe92021-05-21 16:20:58 +00001005 "device-id": oFsm.deviceID, "me-id": inactiveImageID}) //more efficient activeImageID with above check
1006 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTING
mpagenko45586762021-10-01 08:30:22 +00001007 oFsm.upgradePhase = cUpgradeCommitting //start of image commitment for ONU
mpagenkoaa3afe92021-05-21 16:20:58 +00001008 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001009 err := oFsm.pOmciCC.SendCommitSoftware(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
1010 oFsm.PAdaptFsm.CommChan, inactiveImageID) //more efficient activeImageID with above check
mpagenko15ff4a52021-03-02 10:09:20 +00001011 if err != nil {
1012 logger.Errorw(ctx, "CommitSw abort: can't send commit sw frame", log.Fields{
1013 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +00001014 oFsm.abortOnOmciError(ctx, true)
mpagenko15ff4a52021-03-02 10:09:20 +00001015 return
1016 }
1017 return
1018 }
mpagenko38662d02021-08-11 09:45:19 +00001019 oFsm.mutexUpgradeParams.Unlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001020 logger.Errorw(ctx, "OnuUpgradeFsm active ImageId <> IdToCommit", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001021 "device-id": oFsm.deviceID, "active ID": activeImageID, "to commit ID": oFsm.InactiveImageMeID})
mpagenko38662d02021-08-11 09:45:19 +00001022 } else {
1023 logger.Errorw(ctx, "OnuUpgradeFsm can't commit, no valid active image", log.Fields{
1024 "device-id": oFsm.deviceID})
mpagenko15ff4a52021-03-02 10:09:20 +00001025 }
mpagenko38662d02021-08-11 09:45:19 +00001026 oFsm.mutexUpgradeParams.Lock()
1027 oFsm.conditionalCancelRequested = false //any lingering conditional cancelRequest is superseded by this error
mpagenko38662d02021-08-11 09:45:19 +00001028 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
mpagenko38662d02021-08-11 09:45:19 +00001029 oFsm.mutexUpgradeParams.Unlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001030 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001031 pBaseFsm := oFsm.PAdaptFsm
mpagenko15ff4a52021-03-02 10:09:20 +00001032 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001033 go func(a_pAFsm *cmn.AdapterFsm) {
1034 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko15ff4a52021-03-02 10:09:20 +00001035 }(pBaseFsm)
1036}
1037
1038func (oFsm *OnuUpgradeFsm) enterCheckCommitted(ctx context.Context, e *fsm.Event) {
mpagenko38662d02021-08-11 09:45:19 +00001039 logger.Debugw(ctx, "OnuUpgradeFsm checking committed SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001040 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
mpagenko15ff4a52021-03-02 10:09:20 +00001041 requestedAttributes := me.AttributeValueMap{"IsCommitted": 0, "IsActive": 0, "Version": ""}
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001042 meInstance, err := oFsm.pOmciCC.SendGetMe(log.WithSpanFromContext(context.Background(), ctx),
1043 me.SoftwareImageClassID, oFsm.InactiveImageMeID, requestedAttributes, oFsm.pDeviceHandler.GetOmciTimeout(), false, oFsm.PAdaptFsm.CommChan)
ozgecanetsiab36ed572021-04-01 10:38:48 +03001044 if err != nil {
1045 logger.Errorw(ctx, "OnuUpgradeFsm get Software Image ME result error",
1046 log.Fields{"device-id": oFsm.deviceID, "Error": err})
mpagenko45586762021-10-01 08:30:22 +00001047 oFsm.abortOnOmciError(ctx, true)
ozgecanetsiab36ed572021-04-01 10:38:48 +03001048 return
1049 }
mpagenko15ff4a52021-03-02 10:09:20 +00001050 oFsm.pLastTxMeInstance = meInstance
mpagenko80622a52021-02-09 16:53:23 +00001051}
1052
1053func (oFsm *OnuUpgradeFsm) enterResetting(ctx context.Context, e *fsm.Event) {
1054 logger.Debugw(ctx, "OnuUpgradeFsm resetting", log.Fields{"device-id": oFsm.deviceID})
1055
mpagenko45586762021-10-01 08:30:22 +00001056 oFsm.stateUpdateOnReset(ctx)
mpagenko38662d02021-08-11 09:45:19 +00001057
mpagenkoa2b288f2021-10-21 11:25:27 +00001058 oFsm.mutexAbortRequest.Lock()
1059 //to be sure to abort a possibly still running runSwDlSectionWindow()
1060 // in case the reset was not received from cancel() and download not finished correctly
1061 oFsm.abortRequested = oFsm.volthaDownloadReason
1062 oFsm.mutexAbortRequest.Unlock()
1063
mpagenkoc26d4c02021-05-06 14:27:57 +00001064 // in case the download-to-ONU timer is still running - cancel it
mpagenko39b703e2021-08-25 13:38:40 +00001065 //use non-blocking channel (to be independent from receiver state)
1066 select {
1067 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
1068 case oFsm.chOnuDlReady <- false:
1069 default:
mpagenkoc26d4c02021-05-06 14:27:57 +00001070 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001071 pConfigUpgradeStateAFsm := oFsm.PAdaptFsm
1072 if pConfigUpgradeStateAFsm != nil {
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001073 var nextEvent string
1074 if oFsm.isEndSwDlOpen {
mpagenko45586762021-10-01 08:30:22 +00001075 if oFsm.repeatAbort {
1076 oFsm.delayEndSwDl = true //run next abort with delay
1077 } else { //initial request
1078 oFsm.delayEndSwDl = false //run next abort with no delay
1079 oFsm.waitCountEndSwDl = cWaitCountEndSwDl //init for possible repetitions
1080 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001081 nextEvent = UpgradeEvAbortSwDownload
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001082 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001083 nextEvent = UpgradeEvRestart
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001084 }
1085 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001086 go func(a_pAFsm *cmn.AdapterFsm) {
1087 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1088 _ = a_pAFsm.PFsm.Event(nextEvent)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001089 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001090 }(pConfigUpgradeStateAFsm)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001091 }
1092}
mpagenkoc26d4c02021-05-06 14:27:57 +00001093
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001094func (oFsm *OnuUpgradeFsm) enterAbortingDL(ctx context.Context, e *fsm.Event) {
1095 logger.Debugw(ctx, "OnuUpgradeFsm aborting download to ONU", log.Fields{"device-id": oFsm.deviceID})
1096
mpagenko45586762021-10-01 08:30:22 +00001097 oFsm.mutexUpgradeParams.RLock()
1098 if oFsm.delayEndSwDl {
1099 oFsm.mutexUpgradeParams.RUnlock()
1100 //give the ONU some time for image discard activities
1101 time.Sleep(cOmciEndSwDlDelaySeconds * time.Second)
1102 } else {
1103 oFsm.mutexUpgradeParams.RUnlock()
1104 }
1105
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001106 pBaseFsm := oFsm.PAdaptFsm
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001107 if pBaseFsm == nil {
1108 logger.Errorw(ctx, "OnuUpgradeFsm aborting download: BaseFsm invalid", log.Fields{"device-id": oFsm.deviceID})
1109 return
1110 }
1111 // abort the download operation by sending an end software download message with invalid CRC and image size
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001112 err := oFsm.pOmciCC.SendEndSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
1113 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, 0, 0xFFFFFFFF)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001114
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001115 if err != nil {
1116 logger.Errorw(ctx, "OnuUpgradeFsm aborting download: can't send EndSwDl request", log.Fields{"device-id": oFsm.deviceID})
1117 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001118 go func(a_pAFsm *cmn.AdapterFsm) {
1119 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1120 _ = a_pAFsm.PFsm.Event(UpgradeEvRestart)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001121 }
1122 }(pBaseFsm)
1123 return
1124 }
mpagenkoa2b288f2021-10-21 11:25:27 +00001125
1126 //avoid waiting in the enterXXX function here,
1127 // otherwise synchronous event calls (like from RxMessage processing) may block and block complete Rx processing then
1128 go oFsm.waitOnAbortEndSwDlResponse(ctx)
mpagenko45586762021-10-01 08:30:22 +00001129}
1130
1131//abortingDlEvaluateResponse waits for a channel indication with decision to proceed the FSM processing
1132func (oFsm *OnuUpgradeFsm) abortingDlEvaluateResponse(ctx context.Context,
1133 pBaseFsm *cmn.AdapterFsm, aResponseResult tEndSwDlResponseResult) bool {
1134 switch aResponseResult {
1135 case cEndSwDlResponseBusy: // indication for device busy, needs repetition
1136 if oFsm.waitCountEndSwDl == 0 {
1137 logger.Errorw(ctx, "aborting download: max limit of EndSwDl reached", log.Fields{
1138 "device-id": oFsm.deviceID})
1139 go func(a_pAFsm *cmn.AdapterFsm) {
1140 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1141 _ = a_pAFsm.PFsm.Event(UpgradeEvRestart) //give up and let FSM terminate
1142 }
1143 }(pBaseFsm)
1144 } else {
1145 logger.Debugw(ctx, "aborting download: re-trigger sending abort SwDl", log.Fields{
1146 "device-id": oFsm.deviceID, "counter": oFsm.waitCountEndSwDl})
1147 oFsm.waitCountEndSwDl--
1148 oFsm.repeatAbort = true //repeated request in next round
1149 go func(a_pAFsm *cmn.AdapterFsm) {
1150 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1151 _ = a_pAFsm.PFsm.Event(UpgradeEvReset) //which then re-triggers sending AbortSwDL
1152 }
1153 }(pBaseFsm)
1154 }
1155 return true
1156 case cEndSwDlResponseSuccess: // indication for success response
1157 logger.Infow(ctx, "aborting download: success response, terminating FSM", log.Fields{
1158 "device-id": oFsm.deviceID})
1159 case cEndSwDlResponseAbort: // indication for request to abort waiting for response
1160 logger.Infow(ctx, "aborting download: request to abort waiting, terminating FSM", log.Fields{
1161 "device-id": oFsm.deviceID})
1162 default:
1163 logger.Errorw(ctx, "aborting download: unknown channel indication, terminating FSM", log.Fields{
1164 "device-id": oFsm.deviceID})
1165 } //switch
1166 return false
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001167}
1168
1169func (oFsm *OnuUpgradeFsm) enterRestarting(ctx context.Context, e *fsm.Event) {
1170 logger.Debugw(ctx, "OnuUpgradeFsm restarting", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001171 pConfigUpgradeStateAFsm := oFsm.PAdaptFsm
1172 if pConfigUpgradeStateAFsm != nil {
mpagenko80622a52021-02-09 16:53:23 +00001173 // abort running message processing
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001174 fsmAbortMsg := cmn.Message{
1175 Type: cmn.TestMsg,
1176 Data: cmn.TestMessage{
1177 TestMessageVal: cmn.AbortMessageProcessing,
mpagenko80622a52021-02-09 16:53:23 +00001178 },
1179 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001180 pConfigUpgradeStateAFsm.CommChan <- fsmAbortMsg
mpagenko80622a52021-02-09 16:53:23 +00001181
1182 //try to restart the FSM to 'disabled'
1183 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001184 go func(a_pAFsm *cmn.AdapterFsm) {
1185 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1186 _ = a_pAFsm.PFsm.Event(UpgradeEvDisable)
mpagenko80622a52021-02-09 16:53:23 +00001187 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001188 }(pConfigUpgradeStateAFsm)
mpagenko80622a52021-02-09 16:53:23 +00001189 }
1190}
1191
1192func (oFsm *OnuUpgradeFsm) enterDisabled(ctx context.Context, e *fsm.Event) {
1193 logger.Debugw(ctx, "OnuUpgradeFsm enters disabled state", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001194 // 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 +00001195 if oFsm.pDeviceHandler != nil {
1196 //request removal of 'reference' in the Handler (completely clear the FSM and its data)
mpagenko38662d02021-08-11 09:45:19 +00001197 pLastUpgradeImageState := &voltha.ImageState{
1198 Version: oFsm.imageVersion,
1199 DownloadState: oFsm.volthaDownloadState,
1200 Reason: oFsm.volthaDownloadReason,
1201 ImageState: oFsm.volthaImageState,
1202 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001203 go oFsm.pDeviceHandler.RemoveOnuUpgradeFsm(ctx, pLastUpgradeImageState)
mpagenko80622a52021-02-09 16:53:23 +00001204 }
1205}
1206
1207func (oFsm *OnuUpgradeFsm) processOmciUpgradeMessages(ctx context.Context) { //ctx context.Context?
1208 logger.Debugw(ctx, "Start OnuUpgradeFsm Msg processing", log.Fields{"for device-id": oFsm.deviceID})
1209loop:
1210 for {
1211 // case <-ctx.Done():
1212 // logger.Info(ctx,"MibSync Msg", log.Fields{"Message handling canceled via context for device-id": oFsm.deviceID})
1213 // break loop
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001214 message, ok := <-oFsm.PAdaptFsm.CommChan
mpagenko80622a52021-02-09 16:53:23 +00001215 if !ok {
1216 logger.Info(ctx, "OnuUpgradeFsm Rx Msg - could not read from channel", log.Fields{"device-id": oFsm.deviceID})
1217 // but then we have to ensure a restart of the FSM as well - as exceptional procedure
mpagenko45586762021-10-01 08:30:22 +00001218 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +00001219 break loop
1220 }
1221 logger.Debugw(ctx, "OnuUpgradeFsm Rx Msg", log.Fields{"device-id": oFsm.deviceID})
1222
1223 switch message.Type {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001224 case cmn.TestMsg:
1225 msg, _ := message.Data.(cmn.TestMessage)
1226 if msg.TestMessageVal == cmn.AbortMessageProcessing {
mpagenko80622a52021-02-09 16:53:23 +00001227 logger.Infow(ctx, "OnuUpgradeFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.deviceID})
1228 break loop
1229 }
1230 logger.Warnw(ctx, "OnuUpgradeFsm unknown TestMessage", log.Fields{"device-id": oFsm.deviceID, "MessageVal": msg.TestMessageVal})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001231 case cmn.OMCI:
1232 msg, _ := message.Data.(cmn.OmciMessage)
mpagenko80622a52021-02-09 16:53:23 +00001233 oFsm.handleOmciOnuUpgradeMessage(ctx, msg)
1234 default:
1235 logger.Warn(ctx, "OnuUpgradeFsm Rx unknown message", log.Fields{"device-id": oFsm.deviceID,
1236 "message.Type": message.Type})
1237 }
1238 }
1239 logger.Infow(ctx, "End OnuUpgradeFsm Msg processing", log.Fields{"device-id": oFsm.deviceID})
1240}
1241
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001242func (oFsm *OnuUpgradeFsm) handleOmciOnuUpgradeMessage(ctx context.Context, msg cmn.OmciMessage) {
mpagenko80622a52021-02-09 16:53:23 +00001243 logger.Debugw(ctx, "Rx OMCI OnuUpgradeFsm Msg", log.Fields{"device-id": oFsm.deviceID,
1244 "msgType": msg.OmciMsg.MessageType})
1245
1246 switch msg.OmciMsg.MessageType {
1247 case omci.StartSoftwareDownloadResponseType:
1248 {
mpagenko45586762021-10-01 08:30:22 +00001249 oFsm.handleRxStartSwDownloadResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001250 return
1251 } //StartSoftwareDownloadResponseType
1252 case omci.DownloadSectionResponseType:
1253 {
mpagenko45586762021-10-01 08:30:22 +00001254 oFsm.handleRxSwSectionResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001255 return
mpagenko80622a52021-02-09 16:53:23 +00001256 } //DownloadSectionResponseType
1257 case omci.EndSoftwareDownloadResponseType:
1258 {
mpagenko45586762021-10-01 08:30:22 +00001259 oFsm.handleRxEndSwDownloadResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001260 return
1261 } //EndSoftwareDownloadResponseType
1262 case omci.ActivateSoftwareResponseType:
1263 {
1264 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeActivateSoftwareResponse)
1265 if msgLayer == nil {
1266 logger.Errorw(ctx, "Omci Msg layer could not be detected for ActivateSw",
1267 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001268 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001269 return
1270 }
1271 msgObj, msgOk := msgLayer.(*omci.ActivateSoftwareResponse)
1272 if !msgOk {
1273 logger.Errorw(ctx, "Omci Msg layer could not be assigned for ActivateSw",
1274 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001275 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001276 return
1277 }
1278 logger.Debugw(ctx, "OnuUpgradeFsm ActivateSwResponse data", log.Fields{
1279 "device-id": oFsm.deviceID, "data-fields": msgObj})
1280 if msgObj.Result != me.Success {
1281 logger.Errorw(ctx, "OnuUpgradeFsm ActivateSwResponse result error - later: drive FSM to abort state ?",
1282 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
mpagenko45586762021-10-01 08:30:22 +00001283 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001284 return
1285 }
mpagenko183647c2021-06-08 15:25:04 +00001286 oFsm.mutexUpgradeParams.Lock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001287 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
mpagenko38662d02021-08-11 09:45:19 +00001288 // the image is regarded as active really only after ONU reboot and according indication (ONU down/up procedure)
mpagenko183647c2021-06-08 15:25:04 +00001289 oFsm.mutexUpgradeParams.Unlock()
1290 logger.Infow(ctx, "Expected ActivateSwResponse received",
1291 log.Fields{"device-id": oFsm.deviceID, "commit": oFsm.commitImage})
1292 if oFsm.commitImage {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001293 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvWaitForCommit)
mpagenko183647c2021-06-08 15:25:04 +00001294 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001295 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvActivationDone) // let the FSM wait for external commit request
mpagenko183647c2021-06-08 15:25:04 +00001296 }
mpagenko80622a52021-02-09 16:53:23 +00001297 return
1298 }
mpagenko183647c2021-06-08 15:25:04 +00001299 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +00001300 logger.Errorw(ctx, "OnuUpgradeFsm ActivateSwResponse wrong ME instance: abort",
1301 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
mpagenko45586762021-10-01 08:30:22 +00001302 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001303 return
1304 } //ActivateSoftwareResponseType
mpagenko15ff4a52021-03-02 10:09:20 +00001305 case omci.CommitSoftwareResponseType:
1306 {
1307 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeCommitSoftwareResponse)
1308 if msgLayer == nil {
1309 logger.Errorw(ctx, "Omci Msg layer could not be detected for CommitResponse",
1310 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001311 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001312 return
1313 }
1314 msgObj, msgOk := msgLayer.(*omci.CommitSoftwareResponse)
1315 if !msgOk {
1316 logger.Errorw(ctx, "Omci Msg layer could not be assigned for CommitResponse",
1317 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001318 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001319 return
1320 }
mpagenkobf67a092021-03-17 09:52:28 +00001321 if msgObj.Result != me.Success {
mpagenko15ff4a52021-03-02 10:09:20 +00001322 logger.Errorw(ctx, "OnuUpgradeFsm SwImage CommitResponse result error - later: drive FSM to abort state ?",
1323 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
mpagenko45586762021-10-01 08:30:22 +00001324 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001325 return
mpagenkobf67a092021-03-17 09:52:28 +00001326 }
mpagenkoaa3afe92021-05-21 16:20:58 +00001327 oFsm.mutexUpgradeParams.RLock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001328 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
mpagenkoaa3afe92021-05-21 16:20:58 +00001329 oFsm.mutexUpgradeParams.RUnlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001330 logger.Debugw(ctx, "OnuUpgradeFsm Expected SwImage CommitResponse received", log.Fields{"device-id": oFsm.deviceID})
1331 //verifying committed image
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001332 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvCheckCommitted)
mpagenko15ff4a52021-03-02 10:09:20 +00001333 return
1334 }
mpagenkoaa3afe92021-05-21 16:20:58 +00001335 oFsm.mutexUpgradeParams.RUnlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001336 logger.Errorw(ctx, "OnuUpgradeFsm SwImage CommitResponse wrong ME instance: abort",
1337 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
mpagenko45586762021-10-01 08:30:22 +00001338 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001339 return
1340 } //CommitSoftwareResponseType
1341 case omci.GetResponseType:
1342 {
mpagenko45586762021-10-01 08:30:22 +00001343 oFsm.handleRxSwGetResponse(ctx, msg)
mpagenko15ff4a52021-03-02 10:09:20 +00001344 return
1345 } //GetResponseType
mpagenko80622a52021-02-09 16:53:23 +00001346 default:
1347 {
1348 logger.Errorw(ctx, "Rx OMCI unhandled MsgType",
1349 log.Fields{"omciMsgType": msg.OmciMsg.MessageType, "device-id": oFsm.deviceID})
1350 return
1351 }
1352 }
1353}
1354
mpagenko45586762021-10-01 08:30:22 +00001355func (oFsm *OnuUpgradeFsm) handleRxStartSwDownloadResponse(ctx context.Context, msg cmn.OmciMessage) {
1356 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeStartSoftwareDownloadResponse)
1357 if msgLayer == nil {
1358 logger.Errorw(ctx, "Omci Msg layer could not be detected for StartSwDlResponse",
1359 log.Fields{"device-id": oFsm.deviceID})
1360 oFsm.abortOnOmciError(ctx, false)
1361 return
1362 }
1363 msgObj, msgOk := msgLayer.(*omci.StartSoftwareDownloadResponse)
1364 if !msgOk {
1365 logger.Errorw(ctx, "Omci Msg layer could not be assigned for StartSwDlResponse",
1366 log.Fields{"device-id": oFsm.deviceID})
1367 oFsm.abortOnOmciError(ctx, false)
1368 return
1369 }
1370 logger.Debugw(ctx, "OnuUpgradeFsm StartSwDlResponse data", log.Fields{
1371 "device-id": oFsm.deviceID, "data-fields": msgObj})
1372 if msgObj.Result != me.Success {
1373 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse result error - later: drive FSM to abort state ?",
1374 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1375 oFsm.abortOnOmciError(ctx, false)
1376 return
1377 }
1378
1379 oFsm.mutexUpgradeParams.Lock()
1380 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1381 logger.Debugw(ctx, "Expected StartSwDlResponse received", log.Fields{"device-id": oFsm.deviceID})
1382 if msgObj.WindowSize != oFsm.omciDownloadWindowSizeLimit {
1383 // also response WindowSize = 0 is a valid number for used Window size 1
1384 logger.Debugw(ctx, "different StartSwDlResponse window size requested by ONU", log.Fields{
1385 "acceptedOnuWindowSizeLimit": msgObj.WindowSize, "device-id": oFsm.deviceID})
1386 oFsm.omciDownloadWindowSizeLimit = msgObj.WindowSize
1387 }
1388 oFsm.noOfWindows = oFsm.noOfSections / uint32(oFsm.omciDownloadWindowSizeLimit+1)
1389 if oFsm.noOfSections%uint32(oFsm.omciDownloadWindowSizeLimit+1) > 0 {
1390 oFsm.noOfWindows++
1391 }
1392 logger.Debugw(ctx, "OnuUpgradeFsm will use", log.Fields{
1393 "windows": oFsm.noOfWindows, "sections": oFsm.noOfSections,
1394 "at WindowSizeLimit": oFsm.omciDownloadWindowSizeLimit})
1395 oFsm.nextDownloadSectionsAbsolute = 0
1396 oFsm.nextDownloadSectionsWindow = 0
1397 oFsm.nextDownloadWindow = 0
1398
1399 oFsm.mutexUpgradeParams.Unlock()
1400 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvRxStartSwDownload)
1401 return
1402 }
1403 oFsm.mutexUpgradeParams.Unlock()
1404 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse wrong ME instance: try again (later)?",
1405 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1406 oFsm.abortOnOmciError(ctx, false)
1407} //handleRxStartSwDownloadResponse
1408
1409func (oFsm *OnuUpgradeFsm) handleRxSwSectionResponse(ctx context.Context, msg cmn.OmciMessage) {
mpagenkoa2b288f2021-10-21 11:25:27 +00001410 if oFsm.PAdaptFsm == nil {
1411 logger.Infow(ctx, "DlSectionResponse received - but FSM not really valid anymore", log.Fields{
1412 "device-id": oFsm.deviceID})
1413 return
1414 }
1415 if !oFsm.PAdaptFsm.PFsm.Is(UpgradeStVerifyWindow) {
1416 //all the processing here is only relevant if the FSM is in state upgradeStVerifyWindow
1417 // otherwise this response can be ignored (may stem from a long-processing window send activity,
1418 // which is not anymore relevant based on intermediate (cancel) state transitions)
1419 logger.Infow(ctx, "DlSectionResponse received - but ignored", log.Fields{
1420 "device-id": oFsm.deviceID, "fsm-state": oFsm.PAdaptFsm.PFsm.Current()})
1421 return
1422 }
mpagenko45586762021-10-01 08:30:22 +00001423 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeDownloadSectionResponse)
1424 if msgLayer == nil {
1425 logger.Errorw(ctx, "Omci Msg layer could not be detected for DlSectionResponse",
1426 log.Fields{"device-id": oFsm.deviceID, "omci-message": msg.OmciMsg})
1427 oFsm.abortOnOmciError(ctx, false)
1428 return
1429 }
1430 msgObj, msgOk := msgLayer.(*omci.DownloadSectionResponse)
1431 if !msgOk {
1432 logger.Errorw(ctx, "Omci Msg layer could not be assigned for DlSectionResponse",
1433 log.Fields{"device-id": oFsm.deviceID})
1434 oFsm.abortOnOmciError(ctx, false)
1435 return
1436 }
1437 logger.Debugw(ctx, "OnuUpgradeFsm DlSectionResponse Data", log.Fields{
1438 "device-id": oFsm.deviceID, "data-fields": msgObj})
1439 if msgObj.Result != me.Success {
1440 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse result error - later: repeat window once?", //TODO!!!
1441 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1442 oFsm.abortOnOmciError(ctx, false)
1443 return
1444 }
1445 oFsm.mutexUpgradeParams.Lock()
1446 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1447 sectionNumber := msgObj.SectionNumber
1448 logger.Infow(ctx, "DlSectionResponse received", log.Fields{
1449 "window section-number": sectionNumber, "window": oFsm.nextDownloadWindow, "device-id": oFsm.deviceID})
1450
1451 oFsm.nextDownloadWindow++
1452 if oFsm.nextDownloadWindow >= oFsm.noOfWindows {
1453 if sectionNumber != oFsm.omciDownloadWindowSizeLast {
1454 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse section error last window - later: repeat window once?", //TODO!!!
1455 log.Fields{"device-id": oFsm.deviceID, "actual section": sectionNumber,
1456 "expected section": oFsm.omciDownloadWindowSizeLast})
1457 oFsm.mutexUpgradeParams.Unlock()
1458 oFsm.abortOnOmciError(ctx, false)
1459 return
1460 }
1461 oFsm.delayEndSwDl = true //ensure a delay for the EndSwDl message
1462 //CRC computation for all data bytes of the file
1463 imageCRC := crc32a.Checksum(oFsm.imageBuffer[:int(oFsm.origImageLength)]) //store internal for multiple usage
1464 //revert the retrieved CRC Byte Order (seems not to deliver NetworkByteOrder)
1465 var byteSlice []byte = make([]byte, 4)
1466 binary.LittleEndian.PutUint32(byteSlice, uint32(imageCRC))
1467 oFsm.imageCRC = binary.BigEndian.Uint32(byteSlice)
1468 oFsm.mutexUpgradeParams.Unlock()
1469 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvEndSwDownload)
1470 return
1471 }
1472 if sectionNumber != oFsm.omciDownloadWindowSizeLimit {
1473 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse section error - later: repeat window once?", //TODO!!!
1474 log.Fields{"device-id": oFsm.deviceID, "actual-section": sectionNumber,
1475 "expected section": oFsm.omciDownloadWindowSizeLimit})
1476 oFsm.mutexUpgradeParams.Unlock()
1477 oFsm.abortOnOmciError(ctx, false)
1478 return
1479 }
1480 oFsm.nextDownloadSectionsWindow = 0
1481 oFsm.mutexUpgradeParams.Unlock()
1482 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvContinueNextWindow)
1483 return
1484 }
1485 oFsm.mutexUpgradeParams.Unlock()
1486 logger.Errorw(ctx, "OnuUpgradeFsm Omci StartSwDlResponse wrong ME instance: try again (later)?",
1487 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1488 oFsm.abortOnOmciError(ctx, false)
1489} //handleRxSwSectionResponse
1490
1491func (oFsm *OnuUpgradeFsm) handleRxEndSwDownloadResponse(ctx context.Context, msg cmn.OmciMessage) {
1492 inAbortingState := oFsm.PAdaptFsm.PFsm.Is(UpgradeStAbortingDL)
1493
1494 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeEndSoftwareDownloadResponse)
1495 if msgLayer == nil {
1496 logger.Errorw(ctx, "Omci Msg layer could not be detected for EndSwDlResponse",
1497 log.Fields{"device-id": oFsm.deviceID})
1498 if !inAbortingState {
1499 oFsm.abortOnOmciError(ctx, false)
1500 } //else using error log and wait for another response or 'aborting' state timeout
1501 return
1502 }
1503 msgObj, msgOk := msgLayer.(*omci.EndSoftwareDownloadResponse)
1504 if !msgOk {
1505 logger.Errorw(ctx, "Omci Msg layer could not be assigned for EndSwDlResponse",
1506 log.Fields{"device-id": oFsm.deviceID})
1507 if !inAbortingState {
1508 oFsm.abortOnOmciError(ctx, false)
1509 } //else using error log and wait for another response or 'aborting' state timeout
1510 return
1511 }
1512 logger.Debugw(ctx, "OnuUpgradeFsm EndSwDlResponse data", log.Fields{
1513 "device-id": oFsm.deviceID, "data-fields": msgObj})
1514 if msgObj.Result != me.Success {
1515 if msgObj.Result == me.DeviceBusy {
1516 //ONU indicates it is still processing the image - let the FSM just wait and then repeat the request
1517 logger.Debugw(ctx, "OnuUpgradeFsm EndSwDlResponse busy: waiting before sending new request", log.Fields{
1518 "device-id": oFsm.deviceID})
1519 if inAbortingState {
1520 //if the EndSwDl was requested from state AbortingDL then use channel to indicate ONU busy/repeat indication
1521 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseBusy //repeat abort request
1522 }
mpagenko45586762021-10-01 08:30:22 +00001523 return
1524 }
mpagenko45586762021-10-01 08:30:22 +00001525 if inAbortingState {
1526 //if the EndSwDl was requested from state AbortingDL and response is error indication
mpagenkoa2b288f2021-10-21 11:25:27 +00001527 // that would be quite strange ONU behavior, no resolution from OnuAdapter, just let the FSM go on to disabled
1528 logger.Errorw(ctx, "OnuUpgradeFsm EndSwDlResponse result error - aborting anyway",
1529 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1530 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseAbort //error indication to stop waiting on EndDownloadResponse(abort)
1531 return
1532 }
1533 //else: ONU rejects the previous download, complete upgrade is immediately aborted with error
1534 logger.Errorw(ctx, "OnuUpgradeFsm EndSwDlResponse result error - abort upgrade",
1535 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1536 oFsm.mutexUpgradeParams.Lock()
1537 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
1538 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1539 oFsm.mutexUpgradeParams.Unlock()
1540 select {
1541 case oFsm.chOnuDlReady <- false:
1542 default:
1543 }
1544 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko45586762021-10-01 08:30:22 +00001545 return
1546 }
1547 oFsm.mutexUpgradeParams.Lock()
1548 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1549 logger.Debugw(ctx, "Expected EndSwDlResponse received", log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001550 if inAbortingState {
1551 oFsm.mutexUpgradeParams.Unlock()
1552 //if the EndSwDl was requested from state AbortingDL then use channel to indicate abort acceptance
1553 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseSuccess //success
1554 return
1555 }
1556 if !oFsm.useAPIVersion43 {
1557 //in the older API version the image version check was not possible
1558 // - assume new loaded image as valid-inactive immediately
1559 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_SUCCEEDED
1560 oFsm.volthaImageState = voltha.ImageState_IMAGE_INACTIVE
1561 oFsm.mutexUpgradeParams.Unlock()
mpagenkoa2b288f2021-10-21 11:25:27 +00001562 //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 +00001563 select {
mpagenko45586762021-10-01 08:30:22 +00001564 case oFsm.chOnuDlReady <- true:
1565 default:
1566 }
1567 } else {
1568 oFsm.mutexUpgradeParams.Unlock()
1569 }
1570 //use asynchronous channel sending to let the FSM proceed
1571 select {
1572 case oFsm.chReceiveExpectedResponse <- true:
1573 default:
1574 }
1575 return
1576 }
1577 oFsm.mutexUpgradeParams.Unlock()
1578 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse wrong ME instance: ignoring",
1579 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1580 // no state abort in case of unexpected ImageId, just keep waiting for the correct one
1581} //handleRxEndSwDownloadResponse
1582
1583func (oFsm *OnuUpgradeFsm) handleRxSwGetResponse(ctx context.Context, msg cmn.OmciMessage) {
1584 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeGetResponse)
1585 if msgLayer == nil {
1586 logger.Errorw(ctx, "Omci Msg layer could not be detected for SwImage GetResponse",
1587 log.Fields{"device-id": oFsm.deviceID})
1588 oFsm.abortOnOmciError(ctx, false)
1589 return
1590 }
1591 msgObj, msgOk := msgLayer.(*omci.GetResponse)
1592 if !msgOk {
1593 logger.Errorw(ctx, "Omci Msg layer could not be assigned for SwImage GetResponse",
1594 log.Fields{"device-id": oFsm.deviceID})
1595 oFsm.abortOnOmciError(ctx, false)
1596 return
1597 }
1598 logger.Debugw(ctx, "OnuUpgradeFsm SwImage GetResponse data", log.Fields{
1599 "device-id": oFsm.deviceID, "data-fields": msgObj})
1600 if msgObj.EntityClass == oFsm.pLastTxMeInstance.GetClassID() &&
1601 msgObj.EntityInstance == oFsm.pLastTxMeInstance.GetEntityID() {
1602 if msgObj.Result != me.Success {
1603 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse result error - later: drive FSM to abort state ?",
1604 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1605 oFsm.abortOnOmciError(ctx, false)
1606 return
1607 }
1608 } else {
1609 logger.Warnw(ctx, "OnuUpgradeFsm SwImage unexpected Entity GetResponse data - ignore",
1610 log.Fields{"device-id": oFsm.deviceID})
1611 return
1612 }
1613
1614 meAttributes := msgObj.Attributes
1615 imageIsCommitted := meAttributes["IsCommitted"].(uint8)
1616 imageIsActive := meAttributes["IsActive"].(uint8)
1617 imageVersion := cmn.TrimStringFromMeOctet(meAttributes["Version"])
1618 logger.Debugw(ctx, "OnuUpgradeFsm - GetResponse Data for SoftwareImage",
1619 log.Fields{"device-id": oFsm.deviceID, "entityID": msgObj.EntityInstance,
1620 "version": imageVersion, "isActive": imageIsActive, "isCommitted": imageIsCommitted})
1621
1622 if oFsm.PAdaptFsm.PFsm.Current() == UpgradeStCheckImageName {
1623 //image name check after EndSwDownload, this state (and block) can only be taken if APIVersion43 is used
1624 oFsm.verifyOnuSwStatusAfterDownload(ctx, msgObj.EntityInstance, imageVersion, imageIsActive, imageIsCommitted)
1625 return
1626 }
1627
1628 //assumed only relevant state here is upgradeStCheckCommitted
1629 oFsm.mutexUpgradeParams.Lock()
1630 oFsm.conditionalCancelRequested = false //getting here any set (conditional) cancelRequest is not relevant anymore
1631 if msgObj.EntityInstance == oFsm.InactiveImageMeID && imageIsActive == cmn.SwIsActive {
1632 //a check on the delivered image version is not done, the ONU delivered version might be different from what might have been
1633 // indicated in the download image version string (version must be part of the image content itself)
1634 // so checking that might be quite unreliable
1635 //but with new API this was changed, assumption is that omci image version is known at download request and exactly that is used
1636 // in all the API references, so it can and should be checked here now
1637 if oFsm.useAPIVersion43 {
1638 if imageVersion != oFsm.imageVersion {
1639 //new active version indicated on OMCI from ONU is not the expected version
1640 logger.Errorw(ctx, "OnuUpgradeFsm image-version not matching the requested upgrade",
1641 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance,
1642 "onu-version": imageVersion, "expected-version": oFsm.imageVersion})
1643 // TODO!!!: error treatment?
1644 //TODO!!!: possibly send event information for aborted upgrade (aborted by wrong version)?
1645 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE //something like 'UNEXPECTED_VERSION' would be better - proto def
1646 oFsm.mutexUpgradeParams.Unlock()
1647 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1648 return
1649 }
1650 logger.Debugw(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU",
1651 log.Fields{"device-id": oFsm.deviceID})
1652 }
1653 if imageIsCommitted == cmn.SwIsCommitted {
1654 oFsm.upgradePhase = cUpgradeCommitted
1655 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTED
1656 //store the new commit flag to onuSwImageIndications (to keep them in sync)
1657 oFsm.pDevEntry.ModifySwImageActiveCommit(ctx, imageIsCommitted)
1658 logger.Infow(ctx, "requested SW image committed, releasing OnuUpgrade", log.Fields{"device-id": oFsm.deviceID})
1659 //deviceProcStatusUpdate not used anymore,
1660 // replaced by transferring the last (more) upgrade state information within removeOnuUpgradeFsm
1661 oFsm.mutexUpgradeParams.Unlock()
1662 //releasing the upgrade FSM on success
1663 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1664 return
1665 }
1666 //if not committed, abort upgrade as failed. There is no implementation here that would trigger this test again
1667 }
1668 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1669 oFsm.mutexUpgradeParams.Unlock()
1670 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse indications not matching requested upgrade",
1671 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1672 // TODO!!!: error treatment?
1673 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
1674 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1675} //handleRxSwGetResponse
1676
1677func (oFsm *OnuUpgradeFsm) verifyOnuSwStatusAfterDownload(ctx context.Context, aInstanceID uint16,
1678 aImageVersion string, aImageIsActive uint8, aImageIsCommitted uint8) {
1679 oFsm.mutexUpgradeParams.Lock()
1680 if aInstanceID == oFsm.InactiveImageMeID && aImageIsActive == cmn.SwIsInactive &&
1681 aImageIsCommitted == cmn.SwIsUncommitted {
1682 if aImageVersion != oFsm.imageVersion {
1683 //new stored inactive version indicated on OMCI from ONU is not the expected version
1684 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse version indication not matching requested upgrade",
1685 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": aInstanceID,
1686 "onu-version": aImageVersion, "expected-version": oFsm.imageVersion})
1687 //download state is set when entering the reset state
1688 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE //something like 'UNEXPECTED_VERSION' would be better - proto def
1689 oFsm.mutexUpgradeParams.Unlock()
1690 //stop the running ONU download timer
1691 //use non-blocking channel (to be independent from receiver state)
1692 select {
1693 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
1694 case oFsm.chOnuDlReady <- false:
1695 default:
1696 }
1697 // TODO!!!: error treatment?
1698 //TODO!!!: possibly send event information for aborted upgrade (aborted by wrong version)?
1699 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1700 return
1701 }
1702 //with APIVersion43 this is the point to consider the newly loaded image as valid (and inactive)
1703 oFsm.upgradePhase = cUpgradeDownloaded
1704 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_SUCCEEDED
1705 oFsm.volthaImageState = voltha.ImageState_IMAGE_INACTIVE
1706 //store the new inactive version to onuSwImageIndications (to keep them in sync)
1707 oFsm.pDevEntry.ModifySwImageInactiveVersion(ctx, oFsm.imageVersion)
1708 //proceed within upgrade FSM
1709 if oFsm.activateImage {
1710 //immediate activation requested
1711 oFsm.mutexUpgradeParams.Unlock()
1712 logger.Debugw(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU, continue with activation",
1713 log.Fields{"device-id": oFsm.deviceID})
1714 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvRequestActivate)
1715 } else {
1716 //have to wait on explicit activation request
1717 oFsm.mutexUpgradeParams.Unlock()
1718 logger.Infow(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU, wait for activate request",
1719 log.Fields{"device-id": oFsm.deviceID})
1720 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvWaitForActivate)
1721 }
1722 //use non-blocking channel (to be independent from receiver state)
1723 select {
1724 //use non-blocking channel to indicate that the download to ONU was successful
1725 case oFsm.chOnuDlReady <- true:
1726 default:
1727 }
1728 return
1729 }
1730 //not the expected image/image state
1731 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1732 oFsm.mutexUpgradeParams.Unlock()
1733 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse indications not matching requested upgrade",
1734 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": aInstanceID})
1735 // TODO!!!: error treatment?
1736 //TODO!!!: possibly send event information for aborted upgrade (aborted by ONU state indication)?
1737 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1738} //verifyOnuSwStatusAfterDownload
1739
1740//abortOnOmciError aborts the upgrade processing with evAbort
1741// asynchronous/synchronous based on parameter aAsync
1742func (oFsm *OnuUpgradeFsm) abortOnOmciError(ctx context.Context, aAsync bool) {
mpagenko38662d02021-08-11 09:45:19 +00001743 oFsm.mutexUpgradeParams.Lock()
1744 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
mpagenko38662d02021-08-11 09:45:19 +00001745 oFsm.volthaDownloadReason = voltha.ImageState_OMCI_TRANSFER_ERROR
mpagenko38662d02021-08-11 09:45:19 +00001746 oFsm.mutexUpgradeParams.Unlock()
1747 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001748 if oFsm.PAdaptFsm != nil {
mpagenko38662d02021-08-11 09:45:19 +00001749 var err error
1750 if aAsync { //asynchronous call requested to ensure state transition
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001751 go func(a_pAFsm *cmn.AdapterFsm) {
1752 if a_pAFsm.PFsm != nil {
1753 err = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko38662d02021-08-11 09:45:19 +00001754 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001755 }(oFsm.PAdaptFsm)
mpagenko38662d02021-08-11 09:45:19 +00001756 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001757 if oFsm.PAdaptFsm.PFsm != nil {
1758 err = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko38662d02021-08-11 09:45:19 +00001759 }
1760 }
1761 if err != nil {
1762 logger.Warnw(ctx, "onu upgrade fsm could not abort on omci error", log.Fields{
1763 "device-id": oFsm.deviceID, "error": err})
1764 }
1765 }
1766}
1767
mpagenkoc26d4c02021-05-06 14:27:57 +00001768//waitOnDownloadToAdapterReady state can only be reached with useAPIVersion43 (usage of pFileManager)
mpagenko38662d02021-08-11 09:45:19 +00001769// precondition: mutexIsAwaitingAdapterDlResponse is lockek on call
1770func (oFsm *OnuUpgradeFsm) waitOnDownloadToAdapterReady(ctx context.Context, aSyncChannel chan<- struct{},
1771 aWaitChannel chan bool) {
mpagenkoc26d4c02021-05-06 14:27:57 +00001772 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
mpagenko38662d02021-08-11 09:45:19 +00001773 downloadToAdapterTimeout := oFsm.pFileManager.GetDownloadTimeout(ctx)
mpagenkoc26d4c02021-05-06 14:27:57 +00001774 oFsm.isWaitingForAdapterDlResponse = true
1775 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenko38662d02021-08-11 09:45:19 +00001776 aSyncChannel <- struct{}{}
mpagenko80622a52021-02-09 16:53:23 +00001777 select {
1778 // maybe be also some outside cancel (but no context modeled for the moment ...)
1779 // case <-ctx.Done():
mpagenkoc26d4c02021-05-06 14:27:57 +00001780 // logger.Infow("OnuUpgradeFsm-waitOnDownloadToAdapterReady canceled", log.Fields{"for device-id": oFsm.deviceID})
1781 case <-time.After(downloadToAdapterTimeout): //10s should be enough for downloading some image to the adapter
1782 logger.Warnw(ctx, "OnuUpgradeFsm Waiting-adapter-download timeout", log.Fields{
1783 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier, "timeout": downloadToAdapterTimeout})
1784 oFsm.pFileManager.RemoveReadyRequest(ctx, oFsm.imageIdentifier, aWaitChannel)
mpagenko39b703e2021-08-25 13:38:40 +00001785 //running into timeout here may still have the download to adapter active -> abort
1786 oFsm.pFileManager.CancelDownload(ctx, oFsm.imageIdentifier)
mpagenkoc26d4c02021-05-06 14:27:57 +00001787 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1788 oFsm.isWaitingForAdapterDlResponse = false
1789 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenko39b703e2021-08-25 13:38:40 +00001790 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +00001791 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
mpagenko39b703e2021-08-25 13:38:40 +00001792 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'DOWNLOAD_TO_ADAPTER_TIMEOUT' would be better (proto)
mpagenko39b703e2021-08-25 13:38:40 +00001793 oFsm.mutexUpgradeParams.Unlock()
1794 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001795 if oFsm.PAdaptFsm != nil && oFsm.PAdaptFsm.PFsm != nil {
1796 err := oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko39b703e2021-08-25 13:38:40 +00001797 if err != nil {
1798 logger.Warnw(ctx, "onu upgrade fsm could not abort on omci error", log.Fields{
1799 "device-id": oFsm.deviceID, "error": err})
1800 }
1801 }
mpagenkoc26d4c02021-05-06 14:27:57 +00001802 return
1803
1804 case success := <-aWaitChannel:
1805 if success {
1806 logger.Debugw(ctx, "OnuUpgradeFsm image-downloaded received", log.Fields{"device-id": oFsm.deviceID})
1807 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1808 oFsm.isWaitingForAdapterDlResponse = false
1809 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
1810 //let the upgrade process proceed
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001811 pUpgradeFsm := oFsm.PAdaptFsm
mpagenkoc26d4c02021-05-06 14:27:57 +00001812 if pUpgradeFsm != nil {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001813 _ = pUpgradeFsm.PFsm.Event(UpgradeEvPrepareSwDownload)
mpagenkoc26d4c02021-05-06 14:27:57 +00001814 } else {
1815 logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
1816 }
1817 return
1818 }
mpagenko39b703e2021-08-25 13:38:40 +00001819 // waiting was aborted (assumed here to be caused by
1820 // error detection or cancel at download after upgrade FSM reset/abort with according image states set there)
mpagenkoc26d4c02021-05-06 14:27:57 +00001821 logger.Debugw(ctx, "OnuUpgradeFsm Waiting-adapter-download aborted", log.Fields{"device-id": oFsm.deviceID})
1822 oFsm.pFileManager.RemoveReadyRequest(ctx, oFsm.imageIdentifier, aWaitChannel)
1823 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1824 oFsm.isWaitingForAdapterDlResponse = false
1825 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenkoc26d4c02021-05-06 14:27:57 +00001826 return
mpagenko80622a52021-02-09 16:53:23 +00001827 }
1828}
mpagenkoc26d4c02021-05-06 14:27:57 +00001829
1830//waitOnDownloadToOnuReady state can only be reached with useAPIVersion43 (usage of pFileManager)
1831func (oFsm *OnuUpgradeFsm) waitOnDownloadToOnuReady(ctx context.Context, aWaitChannel chan bool) {
1832 downloadToOnuTimeout := time.Duration(1+(oFsm.imageLength/0x400000)) * oFsm.downloadToOnuTimeout4MB
1833 logger.Debugw(ctx, "OnuUpgradeFsm start download-to-ONU timer", log.Fields{"device-id": oFsm.deviceID,
1834 "duration": downloadToOnuTimeout})
mpagenkoc26d4c02021-05-06 14:27:57 +00001835 select {
1836 // maybe be also some outside cancel (but no context modeled for the moment ...)
1837 // case <-ctx.Done():
1838 // logger.Infow("OnuUpgradeFsm-waitOnDownloadToOnuReady canceled", log.Fields{"for device-id": oFsm.deviceID})
1839 case <-time.After(downloadToOnuTimeout): //using an image-size depending timout (in minutes)
1840 logger.Warnw(ctx, "OnuUpgradeFsm Waiting-ONU-download timeout", log.Fields{
1841 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier, "timeout": downloadToOnuTimeout})
mpagenkoc26d4c02021-05-06 14:27:57 +00001842 //the upgrade process has to be aborted
mpagenko45586762021-10-01 08:30:22 +00001843 oFsm.abortOnOmciError(ctx, false)
mpagenkoc26d4c02021-05-06 14:27:57 +00001844 return
1845
1846 case success := <-aWaitChannel:
1847 if success {
1848 logger.Debugw(ctx, "OnuUpgradeFsm image-downloaded on ONU received", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001849 //all fine, let the FSM proceed like defined from the sender of this event
1850 return
1851 }
1852 // waiting was aborted (assumed here to be caused by
mpagenko39b703e2021-08-25 13:38:40 +00001853 // error detection or cancel at download after upgrade FSM reset/abort with according image states set there)
mpagenkoc26d4c02021-05-06 14:27:57 +00001854 logger.Debugw(ctx, "OnuUpgradeFsm Waiting-ONU-download aborted", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001855 return
1856 }
1857}
mpagenko45586762021-10-01 08:30:22 +00001858
mpagenkoa2b288f2021-10-21 11:25:27 +00001859//waitOnAbortEndSwDlResponse waits for either abort/success or timeout of EndSwDownload (for abortion)
1860func (oFsm *OnuUpgradeFsm) waitOnAbortEndSwDlResponse(ctx context.Context) {
1861 logger.Debugw(ctx, "OnuUpgradeFsm start wait for EndSwDl response (abort)", log.Fields{"device-id": oFsm.deviceID})
1862 select {
1863 case <-time.After(oFsm.pOmciCC.GetMaxOmciTimeoutWithRetries() * time.Second):
1864 logger.Warnw(ctx, "OnuUpgradeFsm aborting download: timeout - no response received", log.Fields{"device-id": oFsm.deviceID})
1865 pUpgradeFsm := oFsm.PAdaptFsm
1866 if pUpgradeFsm != nil {
1867 _ = pUpgradeFsm.PFsm.Event(UpgradeEvRestart)
1868 } else {
1869 logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
1870 }
1871 return
1872 case response := <-oFsm.chReceiveAbortEndSwDlResponse:
1873 logger.Debugw(ctx, "OnuUpgradeFsm aborting download: response received",
1874 log.Fields{"device-id": oFsm.deviceID, "response": response})
1875 pUpgradeFsm := oFsm.PAdaptFsm
1876 if pUpgradeFsm != nil {
1877 if oFsm.abortingDlEvaluateResponse(ctx, pUpgradeFsm, response) {
1878 return //event sent from function already
1879 }
1880 _ = pUpgradeFsm.PFsm.Event(UpgradeEvRestart)
1881 } else {
1882 logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
1883 }
1884 return
1885 } //select
1886}
1887
mpagenko45586762021-10-01 08:30:22 +00001888//stateUpdateOnReset writes the download and/or image state on entering the reset state according to FSM internal indications
1889func (oFsm *OnuUpgradeFsm) stateUpdateOnReset(ctx context.Context) {
1890 oFsm.mutexUpgradeParams.Lock()
1891 defer oFsm.mutexUpgradeParams.Unlock()
1892 if !oFsm.conditionalCancelRequested {
1893 switch oFsm.upgradePhase {
1894 case cUpgradeUndefined, cUpgradeDownloading: //coming from downloading
1895 //make sure the download state is only changed in case the device has still been downloading
1896 if oFsm.volthaDownloadReason == voltha.ImageState_CANCELLED_ON_REQUEST {
1897 // indication for termination on request
1898 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_CANCELLED
1899 } else if oFsm.volthaDownloadReason != voltha.ImageState_NO_ERROR {
1900 // indication for termination on failure
1901 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_FAILED
1902 }
1903 //reset the image state from Downloading in this case
1904 oFsm.volthaImageState = voltha.ImageState_IMAGE_UNKNOWN //something like 'IMAGE_DOWNLOAD_ABORTED' would be better (proto)
1905 //in all other upgrade phases the last set download state remains valid
1906 case cUpgradeActivating:
1907 //reset the image state from Activating in this case
1908 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATION_ABORTED
1909 case cUpgradeCommitting: // indication for request to abort waiting for response
1910 //reset the image state from Activating in this case
1911 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMIT_ABORTED
1912 //default: in all other upgrade phases keep the last set imageState
1913 } //switch
1914 } else {
1915 //when reaching reset state with conditional cancel that can only result from ONU related problems
1916 // (mostly ONU down indication) - derived from resetFsms call
1917 // and it can only be related to the downloading-to-ONU phase (no need to check that additionally)
1918 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_FAILED
1919 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1920 //reset the image state from Downloading in this case
1921 oFsm.volthaImageState = voltha.ImageState_IMAGE_UNKNOWN //something like 'IMAGE_DOWNLOAD_ABORTED' would be better (proto)
1922 }
1923}