blob: 3a0fe7cf785d986911cadce34aa7c35d1b31aac6 [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"
30 "github.com/opencord/omci-lib-go"
31 me "github.com/opencord/omci-lib-go/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
176 isWaitingForAdapterDlResponse bool
mpagenkoc26d4c02021-05-06 14:27:57 +0000177 chOnuDlReady chan bool
mpagenkoc26d4c02021-05-06 14:27:57 +0000178 activateImage bool
179 commitImage bool
mpagenko38662d02021-08-11 09:45:19 +0000180 mutexAbortRequest sync.RWMutex
181 abortRequested voltha.ImageState_ImageFailureReason
182 conditionalCancelRequested bool
mpagenko45586762021-10-01 08:30:22 +0000183 upgradePhase tUpgradePhase
mpagenkoaa3afe92021-05-21 16:20:58 +0000184 volthaDownloadState voltha.ImageState_ImageDownloadState
185 volthaDownloadReason voltha.ImageState_ImageFailureReason
186 volthaImageState voltha.ImageState_ImageActivationState
mpagenko45586762021-10-01 08:30:22 +0000187 downloadReasonCached voltha.ImageState_ImageFailureReason
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)
217 instFsm.chOnuDlReady = make(chan bool)
mpagenko45586762021-10-01 08:30:22 +0000218 instFsm.chReceiveAbortEndSwDlResponse = make(chan tEndSwDlResponseResult)
mpagenko80622a52021-02-09 16:53:23 +0000219
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000220 instFsm.PAdaptFsm = cmn.NewAdapterFsm(aName, instFsm.deviceID, aCommChannel)
221 if instFsm.PAdaptFsm == nil {
mpagenko80622a52021-02-09 16:53:23 +0000222 logger.Errorw(ctx, "OnuUpgradeFsm's AdapterFsm could not be instantiated!!", log.Fields{
223 "device-id": instFsm.deviceID})
224 return nil
225 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000226 instFsm.PAdaptFsm.PFsm = fsm.NewFSM(
227 UpgradeStDisabled,
mpagenko80622a52021-02-09 16:53:23 +0000228 fsm.Events{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000229 {Name: UpgradeEvStart, Src: []string{UpgradeStDisabled}, Dst: UpgradeStStarting},
230 {Name: UpgradeEvAdapterDownload, Src: []string{UpgradeStStarting}, Dst: UpgradeStWaitingAdapterDL},
231 {Name: UpgradeEvPrepareSwDownload, Src: []string{UpgradeStStarting, UpgradeStWaitingAdapterDL}, Dst: UpgradeStPreparingDL},
232 {Name: UpgradeEvRxStartSwDownload, Src: []string{UpgradeStPreparingDL}, Dst: UpgradeStDLSection},
233 {Name: UpgradeEvWaitWindowAck, Src: []string{UpgradeStDLSection}, Dst: UpgradeStVerifyWindow},
234 {Name: UpgradeEvContinueNextWindow, Src: []string{UpgradeStVerifyWindow}, Dst: UpgradeStDLSection},
235 {Name: UpgradeEvEndSwDownload, Src: []string{UpgradeStVerifyWindow}, Dst: UpgradeStFinalizeDL},
236 {Name: UpgradeEvWaitEndDownload, Src: []string{UpgradeStFinalizeDL}, Dst: UpgradeStWaitEndDL},
237 {Name: UpgradeEvContinueFinalize, Src: []string{UpgradeStWaitEndDL}, Dst: UpgradeStFinalizeDL},
238 //UpgradeStCheckImageName only used with useAPIVersion43
239 {Name: UpgradeEvCheckImageName, Src: []string{UpgradeStWaitEndDL}, Dst: UpgradeStCheckImageName},
240 //UpgradeEvWaitForActivate state transitions depend on useAPIVersion43
241 {Name: UpgradeEvWaitForActivate, Src: []string{UpgradeStWaitEndDL, UpgradeStCheckImageName}, Dst: UpgradeStWaitForActivate},
242 //UpgradeEvRequestActivate state transitions depend on useAPIVersion43
243 {Name: UpgradeEvRequestActivate, Src: []string{UpgradeStStarting, UpgradeStWaitEndDL, UpgradeStCheckImageName,
244 UpgradeStWaitForActivate}, Dst: UpgradeStRequestingActivate}, //allows also for direct activation (without download) [TODO!!!]
245 {Name: UpgradeEvActivationDone, Src: []string{UpgradeStRequestingActivate}, Dst: UpgradeStActivated},
246 {Name: UpgradeEvWaitForCommit, Src: []string{UpgradeStRequestingActivate}, Dst: UpgradeStWaitForCommit},
247 {Name: UpgradeEvCommitSw, Src: []string{UpgradeStStarting, UpgradeStRequestingActivate, UpgradeStWaitForCommit,
248 UpgradeStActivated}, Dst: UpgradeStCommitSw}, //allows also for direct commitment (without download) [TODO!!!]
249 {Name: UpgradeEvCheckCommitted, Src: []string{UpgradeStCommitSw}, Dst: UpgradeStCheckCommitted},
mpagenko80622a52021-02-09 16:53:23 +0000250
251 /*
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000252 {Name: UpgradeEvTimeoutSimple, Src: []string{
253 UpgradeStCreatingDot1PMapper, UpgradeStCreatingMBPCD, UpgradeStSettingTconts, UpgradeStSettingDot1PMapper}, Dst: UpgradeStStarting},
254 {Name: UpgradeEvTimeoutMids, Src: []string{
255 UpgradeStCreatingGemNCTPs, UpgradeStCreatingGemIWs, UpgradeStSettingPQs}, Dst: UpgradeStStarting},
mpagenko80622a52021-02-09 16:53:23 +0000256 */
257 // exceptional treatments
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000258 //on UpgradeEvReset: UpgradeStRequestingActivate, UpgradeStWaitForCommit and UpgradeStActivated are not reset
mpagenko1f8e8822021-06-25 14:10:21 +0000259 // (to let the FSM survive the expected OnuDown indication)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000260 {Name: UpgradeEvReset, Src: []string{UpgradeStStarting, UpgradeStWaitingAdapterDL, UpgradeStPreparingDL, UpgradeStDLSection,
261 UpgradeStVerifyWindow, UpgradeStDLSection, UpgradeStFinalizeDL, UpgradeStWaitEndDL, UpgradeStCheckImageName,
262 UpgradeStWaitForActivate,
mpagenko45586762021-10-01 08:30:22 +0000263 UpgradeStCommitSw, UpgradeStCheckCommitted, UpgradeStAbortingDL},
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000264 Dst: UpgradeStResetting},
265 {Name: UpgradeEvAbort, Src: []string{UpgradeStStarting, UpgradeStWaitingAdapterDL, UpgradeStPreparingDL, UpgradeStDLSection,
266 UpgradeStVerifyWindow, UpgradeStDLSection, UpgradeStFinalizeDL, UpgradeStWaitEndDL, UpgradeStCheckImageName,
267 UpgradeStWaitForActivate,
268 UpgradeStRequestingActivate, UpgradeStActivated, UpgradeStWaitForCommit,
269 UpgradeStCommitSw, UpgradeStCheckCommitted},
270 Dst: UpgradeStResetting},
271 {Name: UpgradeEvAbortSwDownload, Src: []string{UpgradeStResetting}, Dst: UpgradeStAbortingDL},
272 {Name: UpgradeEvRestart, Src: []string{UpgradeStResetting, UpgradeStAbortingDL}, Dst: UpgradeStRestarting},
273 {Name: UpgradeEvDisable, Src: []string{UpgradeStRestarting}, Dst: UpgradeStDisabled},
mpagenko80622a52021-02-09 16:53:23 +0000274 },
275 fsm.Callbacks{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000276 "enter_state": func(e *fsm.Event) { instFsm.PAdaptFsm.LogFsmStateChange(ctx, e) },
277 "enter_" + UpgradeStStarting: func(e *fsm.Event) { instFsm.enterStarting(ctx, e) },
278 "enter_" + UpgradeStWaitingAdapterDL: func(e *fsm.Event) { instFsm.enterWaitingAdapterDL(ctx, e) },
279 "enter_" + UpgradeStPreparingDL: func(e *fsm.Event) { instFsm.enterPreparingDL(ctx, e) },
280 "enter_" + UpgradeStDLSection: func(e *fsm.Event) { instFsm.enterDownloadSection(ctx, e) },
281 "enter_" + UpgradeStVerifyWindow: func(e *fsm.Event) { instFsm.enterVerifyWindow(ctx, e) },
282 "enter_" + UpgradeStFinalizeDL: func(e *fsm.Event) { instFsm.enterFinalizeDL(ctx, e) },
283 "enter_" + UpgradeStWaitEndDL: func(e *fsm.Event) { instFsm.enterWaitEndDL(ctx, e) },
284 "enter_" + UpgradeStCheckImageName: func(e *fsm.Event) { instFsm.enterCheckImageName(ctx, e) },
285 "enter_" + UpgradeStRequestingActivate: func(e *fsm.Event) { instFsm.enterActivateSw(ctx, e) },
286 "enter_" + UpgradeStCommitSw: func(e *fsm.Event) { instFsm.enterCommitSw(ctx, e) },
287 "enter_" + UpgradeStCheckCommitted: func(e *fsm.Event) { instFsm.enterCheckCommitted(ctx, e) },
288 "enter_" + UpgradeStResetting: func(e *fsm.Event) { instFsm.enterResetting(ctx, e) },
289 "enter_" + UpgradeStAbortingDL: func(e *fsm.Event) { instFsm.enterAbortingDL(ctx, e) },
290 "enter_" + UpgradeStRestarting: func(e *fsm.Event) { instFsm.enterRestarting(ctx, e) },
291 "enter_" + UpgradeStDisabled: func(e *fsm.Event) { instFsm.enterDisabled(ctx, e) },
mpagenko80622a52021-02-09 16:53:23 +0000292 },
293 )
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000294 if instFsm.PAdaptFsm.PFsm == nil {
mpagenko80622a52021-02-09 16:53:23 +0000295 logger.Errorw(ctx, "OnuUpgradeFsm's Base FSM could not be instantiated!!", log.Fields{
296 "device-id": instFsm.deviceID})
297 return nil
298 }
299
300 logger.Debugw(ctx, "OnuUpgradeFsm created", log.Fields{"device-id": instFsm.deviceID})
301 return instFsm
302}
303
304//SetDownloadParams configures the needed parameters for a specific download to the ONU
mpagenkoc26d4c02021-05-06 14:27:57 +0000305// called from 'old' API Activate_image_update()
mpagenko15ff4a52021-03-02 10:09:20 +0000306func (oFsm *OnuUpgradeFsm) SetDownloadParams(ctx context.Context, aInactiveImageID uint16,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000307 apImageDsc *voltha.ImageDownload, apDownloadManager *AdapterDownloadManager) error {
308 pBaseFsm := oFsm.PAdaptFsm.PFsm
309 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoaa3afe92021-05-21 16:20:58 +0000310 oFsm.mutexUpgradeParams.Lock()
mpagenko80622a52021-02-09 16:53:23 +0000311 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting", log.Fields{
312 "device-id": oFsm.deviceID, "image-description": apImageDsc})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000313 oFsm.InactiveImageMeID = aInactiveImageID //upgrade state machines run on configured inactive ImageId
mpagenko80622a52021-02-09 16:53:23 +0000314 oFsm.pImageDsc = apImageDsc
315 oFsm.pDownloadManager = apDownloadManager
Holger Hildebrandtac1e0592021-06-03 15:16:49 +0000316 oFsm.activateImage = true
317 oFsm.commitImage = true
mpagenkoaa3afe92021-05-21 16:20:58 +0000318 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000319
320 go func(aPBaseFsm *fsm.FSM) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000321 // let the upgrade FSM proceed to PreparingDL
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000322 _ = aPBaseFsm.Event(UpgradeEvPrepareSwDownload)
mpagenko80622a52021-02-09 16:53:23 +0000323 }(pBaseFsm)
324 return nil
325 }
326 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
327 "device-id": oFsm.deviceID})
328 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
329}
330
mpagenkoc26d4c02021-05-06 14:27:57 +0000331//SetDownloadParamsAfterDownload configures the needed parameters for a specific download to the ONU according to
332// updated API interface with R2.8: start download to ONU if the image is downloaded to the adapter
333// called from 'new' API Download_onu_image
334func (oFsm *OnuUpgradeFsm) SetDownloadParamsAfterDownload(ctx context.Context, aInactiveImageID uint16,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000335 apImageRequest *voltha.DeviceImageDownloadRequest, apDownloadManager *FileDownloadManager,
Holger Hildebrandtac010732021-06-02 13:35:39 +0000336 aImageIdentifier string) error {
mpagenkoc26d4c02021-05-06 14:27:57 +0000337 oFsm.mutexUpgradeParams.Lock()
338 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000339 if oFsm.PAdaptFsm != nil {
340 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000341 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000342 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000343 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting", log.Fields{
344 "device-id": oFsm.deviceID, "image-description": apImageRequest})
345 oFsm.useAPIVersion43 = true
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000346 oFsm.InactiveImageMeID = aInactiveImageID //upgrade state machines run on configured inactive ImageId
mpagenkoc26d4c02021-05-06 14:27:57 +0000347 oFsm.pFileManager = apDownloadManager
348 oFsm.imageIdentifier = aImageIdentifier
349 oFsm.imageVersion = apImageRequest.Image.Version
350 oFsm.activateImage = apImageRequest.ActivateOnSuccess
351 oFsm.commitImage = apImageRequest.CommitOnSuccess
mpagenko45586762021-10-01 08:30:22 +0000352 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_STARTED //state change indication for download request
mpagenkoc26d4c02021-05-06 14:27:57 +0000353 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000354 _ = pBaseFsm.Event(UpgradeEvAdapterDownload) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000355 return nil
356 }
357 oFsm.mutexUpgradeParams.Unlock()
358 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
359 "device-id": oFsm.deviceID})
360 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
361}
362
363//SetActivationParamsRunning sets the activate and commit flags for a running download to the ONU according to adapters rpc call
364// called from 'new' API Activate_onu_image
365func (oFsm *OnuUpgradeFsm) SetActivationParamsRunning(ctx context.Context,
366 aImageIdentifier string, aCommit bool) error {
mpagenko45586762021-10-01 08:30:22 +0000367 logger.Debugw(ctx, "OnuUpgradeFsm activate/commit parameter setting", log.Fields{
368 "device-id": oFsm.deviceID, "image-id": aImageIdentifier, "commit": aCommit})
mpagenkoc26d4c02021-05-06 14:27:57 +0000369 oFsm.mutexUpgradeParams.Lock()
370 //set activate/commit independent from state, if FSM is already beyond concerned states, then it does not matter anyway
371 // (as long as the Imageidentifier is correct)
mpagenkoc26d4c02021-05-06 14:27:57 +0000372 if aImageIdentifier != oFsm.imageIdentifier {
373 logger.Errorw(ctx, "OnuUpgradeFsm abort: mismatching upgrade image", log.Fields{
374 "device-id": oFsm.deviceID, "request-image": aImageIdentifier, "fsm-image": oFsm.imageIdentifier})
375 oFsm.mutexUpgradeParams.Unlock()
376 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm params ignored: requested image-name not used in current upgrade for device-id: %s",
377 oFsm.deviceID))
378 }
379 oFsm.activateImage = true
380 oFsm.commitImage = aCommit
mpagenko45586762021-10-01 08:30:22 +0000381 oFsm.mutexUpgradeParams.Unlock()
mpagenkoc26d4c02021-05-06 14:27:57 +0000382 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000383 if oFsm.PAdaptFsm != nil {
384 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000385 }
386 if pBaseFsm != nil {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000387 if pBaseFsm.Is(UpgradeStWaitForActivate) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000388 logger.Debugw(ctx, "OnuUpgradeFsm finish waiting for activate", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000389 _ = pBaseFsm.Event(UpgradeEvRequestActivate) //no need to call the FSM event in background here
mpagenko38662d02021-08-11 09:45:19 +0000390 } else {
mpagenko38662d02021-08-11 09:45:19 +0000391 logger.Debugw(ctx, "OnuUpgradeFsm not (yet?) waiting for activate", log.Fields{
392 "device-id": oFsm.deviceID, "current FsmState": pBaseFsm.Current()})
mpagenkoc26d4c02021-05-06 14:27:57 +0000393 }
394 return nil
395 }
396 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer", log.Fields{
397 "device-id": oFsm.deviceID})
398 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer for device-id: %s", oFsm.deviceID))
399}
400
401//SetActivationParamsStart starts upgrade processing with immediate activation
402// called from 'new' API Activate_onu_image
403func (oFsm *OnuUpgradeFsm) SetActivationParamsStart(ctx context.Context, aImageVersion string, aInactiveImageID uint16, aCommit bool) error {
404 oFsm.mutexUpgradeParams.Lock()
405 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000406 if oFsm.PAdaptFsm != nil {
407 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000408 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000409 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000410 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting to start with activation", log.Fields{
411 "device-id": oFsm.deviceID, "image-version": aImageVersion})
412 oFsm.useAPIVersion43 = true
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000413 oFsm.InactiveImageMeID = aInactiveImageID //upgrade state machines run on configured inactive ImageId
mpagenkoc26d4c02021-05-06 14:27:57 +0000414 oFsm.imageVersion = aImageVersion
415 oFsm.activateImage = true
416 oFsm.commitImage = aCommit
mpagenko38662d02021-08-11 09:45:19 +0000417 // indicate start of the upgrade activity
mpagenko45586762021-10-01 08:30:22 +0000418 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATING //state change indication for activate request
mpagenkoc26d4c02021-05-06 14:27:57 +0000419 oFsm.mutexUpgradeParams.Unlock()
420 //directly request the FSM to activate the image
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000421 _ = pBaseFsm.Event(UpgradeEvRequestActivate) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000422 return nil
423 }
424 oFsm.mutexUpgradeParams.Unlock()
425 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
426 "device-id": oFsm.deviceID})
427 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
428}
429
430//SetCommitmentParamsRunning sets the commit flag for a running download to the ONU according to adapters rpc call
431// called from 'new' API Commit_onu_image
mpagenko38662d02021-08-11 09:45:19 +0000432func (oFsm *OnuUpgradeFsm) SetCommitmentParamsRunning(ctx context.Context,
433 aImageIdentifier string, aImageVersion string) error {
mpagenkoc26d4c02021-05-06 14:27:57 +0000434 oFsm.mutexUpgradeParams.Lock()
435 //set commit independent from state, if FSM is already beyond commit state (just ready), then it does not matter anyway
436 // (as long as the Imageidentifier is correct)
437 logger.Debugw(ctx, "OnuUpgradeFsm commit parameter setting", log.Fields{
438 "device-id": oFsm.deviceID, "image-id": aImageIdentifier})
mpagenko38662d02021-08-11 09:45:19 +0000439 if (aImageIdentifier != oFsm.imageIdentifier) && (aImageVersion != oFsm.imageVersion) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000440 logger.Errorw(ctx, "OnuUpgradeFsm abort: mismatching upgrade image", log.Fields{
mpagenko38662d02021-08-11 09:45:19 +0000441 "device-id": oFsm.deviceID, "request-identifier": aImageIdentifier, "fsm-identifier": oFsm.imageIdentifier,
442 "request-version": aImageVersion, "fsm-version": oFsm.imageVersion})
mpagenkoc26d4c02021-05-06 14:27:57 +0000443 oFsm.mutexUpgradeParams.Unlock()
444 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm params ignored: requested image-name not used in current upgrade for device-id: %s",
445 oFsm.deviceID))
446 }
447 oFsm.commitImage = true
448 oFsm.mutexUpgradeParams.Unlock()
449 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000450 if oFsm.PAdaptFsm != nil {
451 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000452 }
453 if pBaseFsm != nil {
mpagenko183647c2021-06-08 15:25:04 +0000454 //let the FSM decide if it is ready to process the event
455 logger.Debugw(ctx, "OnuUpgradeFsm requesting commit",
456 log.Fields{"device-id": oFsm.deviceID, "current FsmState": pBaseFsm.Current()})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000457 _ = pBaseFsm.Event(UpgradeEvCommitSw) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000458 return nil
459 }
mpagenko38662d02021-08-11 09:45:19 +0000460 //should never occur
mpagenkoc26d4c02021-05-06 14:27:57 +0000461 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer", log.Fields{
462 "device-id": oFsm.deviceID})
463 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer for device-id: %s", oFsm.deviceID))
464}
465
466//SetCommitmentParamsStart starts upgrade processing with immediate commitment
467// called from 'new' API Commit_onu_image
468func (oFsm *OnuUpgradeFsm) SetCommitmentParamsStart(ctx context.Context, aImageVersion string, aActiveImageID uint16) error {
469 oFsm.mutexUpgradeParams.Lock()
470 var pBaseFsm *fsm.FSM = nil
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000471 if oFsm.PAdaptFsm != nil {
472 pBaseFsm = oFsm.PAdaptFsm.PFsm
mpagenkoc26d4c02021-05-06 14:27:57 +0000473 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000474 if pBaseFsm != nil && pBaseFsm.Is(UpgradeStStarting) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000475 logger.Debugw(ctx, "OnuUpgradeFsm Parameter setting to start with commitment", log.Fields{
476 "device-id": oFsm.deviceID, "image-version": aImageVersion})
477 oFsm.useAPIVersion43 = true
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000478 oFsm.InactiveImageMeID = aActiveImageID //upgrade state machines inactive ImageId is the new active ImageId
mpagenkoc26d4c02021-05-06 14:27:57 +0000479 oFsm.imageVersion = aImageVersion
480 oFsm.commitImage = true
mpagenko45586762021-10-01 08:30:22 +0000481 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTING //state change indication for activate request
mpagenkoc26d4c02021-05-06 14:27:57 +0000482 oFsm.mutexUpgradeParams.Unlock()
mpagenko38662d02021-08-11 09:45:19 +0000483 //directly request the FSM to commit the image
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000484 _ = pBaseFsm.Event(UpgradeEvCommitSw) //no need to call the FSM event in background here
mpagenkoc26d4c02021-05-06 14:27:57 +0000485 return nil
486 }
487 oFsm.mutexUpgradeParams.Unlock()
488 logger.Errorw(ctx, "OnuUpgradeFsm abort: invalid FSM base pointer or state", log.Fields{
489 "device-id": oFsm.deviceID})
490 return fmt.Errorf(fmt.Sprintf("OnuUpgradeFsm abort: invalid FSM base pointer or state for device-id: %s", oFsm.deviceID))
491}
492
mpagenko1f8e8822021-06-25 14:10:21 +0000493//GetCommitFlag delivers the commit flag that was configured here
494func (oFsm *OnuUpgradeFsm) GetCommitFlag(ctx context.Context) bool {
495 oFsm.mutexUpgradeParams.RLock()
496 defer oFsm.mutexUpgradeParams.RUnlock()
497 return oFsm.commitImage
498}
499
mpagenko38662d02021-08-11 09:45:19 +0000500//GetImageStates delivers the download/image states as per device proto buf definition
mpagenkoaa3afe92021-05-21 16:20:58 +0000501func (oFsm *OnuUpgradeFsm) GetImageStates(ctx context.Context,
mpagenko38662d02021-08-11 09:45:19 +0000502 aImageIdentifier string, aVersion string) *voltha.ImageState {
mpagenkoaa3afe92021-05-21 16:20:58 +0000503 pImageState := &voltha.ImageState{}
mpagenko38662d02021-08-11 09:45:19 +0000504 pImageState.Version = aVersion //version as requested
mpagenkoaa3afe92021-05-21 16:20:58 +0000505 // check if the request refers to some active image/version of the processing
506 oFsm.mutexUpgradeParams.RLock()
507 if (aImageIdentifier == oFsm.imageIdentifier) || (aVersion == oFsm.imageVersion) {
508 pImageState.DownloadState = oFsm.volthaDownloadState
509 pImageState.Reason = oFsm.volthaDownloadReason
510 pImageState.ImageState = oFsm.volthaImageState
511 } else {
512 pImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
513 pImageState.Reason = voltha.ImageState_NO_ERROR
514 pImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
515 }
516 oFsm.mutexUpgradeParams.RUnlock()
mpagenko38662d02021-08-11 09:45:19 +0000517 return pImageState
mpagenkoaa3afe92021-05-21 16:20:58 +0000518}
519
mpagenko38662d02021-08-11 09:45:19 +0000520//GetSpecificImageState delivers ImageState of the download/image states as per device proto buf definition
521func (oFsm *OnuUpgradeFsm) GetSpecificImageState(ctx context.Context) voltha.ImageState_ImageActivationState {
522 oFsm.mutexUpgradeParams.RLock()
523 imageState := oFsm.volthaImageState
524 oFsm.mutexUpgradeParams.RUnlock()
525 return imageState
526}
527
528//SetImageStateActive sets the FSM internal volthaImageState to ImageState_IMAGE_ACTIVE
529func (oFsm *OnuUpgradeFsm) SetImageStateActive(ctx context.Context) {
mpagenko183647c2021-06-08 15:25:04 +0000530 oFsm.mutexUpgradeParams.Lock()
531 defer oFsm.mutexUpgradeParams.Unlock()
mpagenko45586762021-10-01 08:30:22 +0000532 oFsm.upgradePhase = cUpgradeActivated
mpagenko38662d02021-08-11 09:45:19 +0000533 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVE
mpagenko38662d02021-08-11 09:45:19 +0000534}
535
536//GetImageVersion delivers image-version of the running upgrade
537func (oFsm *OnuUpgradeFsm) GetImageVersion(ctx context.Context) string {
538 oFsm.mutexUpgradeParams.RLock()
539 imageVersion := oFsm.imageVersion
540 oFsm.mutexUpgradeParams.RUnlock()
541 return imageVersion
mpagenko183647c2021-06-08 15:25:04 +0000542}
543
mpagenkoc26d4c02021-05-06 14:27:57 +0000544//CancelProcessing ensures that suspended processing at waiting on some response is aborted and reset of FSM
mpagenko38662d02021-08-11 09:45:19 +0000545func (oFsm *OnuUpgradeFsm) CancelProcessing(ctx context.Context, abCompleteAbort bool,
546 aReason voltha.ImageState_ImageFailureReason) {
547 oFsm.mutexAbortRequest.Lock()
548 oFsm.abortRequested = aReason //possibly abort the sectionDownload loop
549 oFsm.mutexAbortRequest.Unlock()
mpagenkoc26d4c02021-05-06 14:27:57 +0000550 //mutex protection is required for possible concurrent access to FSM members
551 //attention: for an unbuffered channel the sender is blocked until the value is received (processed)!
552 // accordingly the mutex must be released before sending to channel here (mutex acquired in receiver)
553 oFsm.mutexIsAwaitingAdapterDlResponse.RLock()
554 if oFsm.isWaitingForAdapterDlResponse {
555 oFsm.mutexIsAwaitingAdapterDlResponse.RUnlock()
556 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
557 oFsm.chAdapterDlReady <- false
558 } else {
559 oFsm.mutexIsAwaitingAdapterDlResponse.RUnlock()
560 }
561 //chOnuDlReady is cleared as part of the FSM reset processing (from enterResetting())
562
563 // 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 +0000564 // specific here: See definition of state changes: some states are excluded from reset for possible later commit
mpagenko45586762021-10-01 08:30:22 +0000565 pAdaptFsm := oFsm.PAdaptFsm
mpagenko59862f02021-10-11 08:53:18 +0000566 if pAdaptFsm != nil && pAdaptFsm.PFsm != nil {
mpagenkoc26d4c02021-05-06 14:27:57 +0000567 // calling FSM events in background to avoid blocking of the caller
mpagenko59862f02021-10-11 08:53:18 +0000568 go func(apFsm *fsm.FSM) {
569 if apFsm.Is(UpgradeStWaitEndDL) {
570 oFsm.chReceiveExpectedResponse <- false //which aborts the FSM in WaitEndDL state
571 } else if apFsm.Is(UpgradeStAbortingDL) {
572 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseAbort //abort waiting on EndDownloadResponse
573 }
mpagenko38662d02021-08-11 09:45:19 +0000574
mpagenko59862f02021-10-11 08:53:18 +0000575 var err error
576 if abCompleteAbort {
577 // in case of unconditional abort request the ImageState is set immediately
578 oFsm.mutexUpgradeParams.Lock()
579 //any previous lingering conditional cancelRequest is superseded by this abortion
580 oFsm.conditionalCancelRequested = false
581 oFsm.volthaDownloadReason = aReason
582 oFsm.mutexUpgradeParams.Unlock()
583 err = apFsm.Event(UpgradeEvAbort) //as unconditional default FSM cancellation
584 } else {
585 //at conditional abort request the image states are set when reaching the reset state
586 oFsm.conditionalCancelRequested = true
587 err = apFsm.Event(UpgradeEvReset) //as state-conditional default FSM cleanup
588 }
589 if err != nil {
590 //error return is expected in case of conditional request and no state transition
591 logger.Debugw(ctx, "onu upgrade fsm could not cancel with abort/reset event", log.Fields{
592 "device-id": oFsm.deviceID, "error": err})
593 }
594 }(pAdaptFsm.PFsm)
595 } //else the FSM seems already to be in some released state
mpagenkoc26d4c02021-05-06 14:27:57 +0000596}
597
mpagenko80622a52021-02-09 16:53:23 +0000598func (oFsm *OnuUpgradeFsm) enterStarting(ctx context.Context, e *fsm.Event) {
599 logger.Debugw(ctx, "OnuUpgradeFsm start", log.Fields{"in state": e.FSM.Current(),
600 "device-id": oFsm.deviceID})
601
602 // start go routine for processing of LockState messages
603 go oFsm.processOmciUpgradeMessages(ctx)
604}
605
mpagenkoc26d4c02021-05-06 14:27:57 +0000606//enterWaitingAdapterDL state can only be reached with useAPIVersion43
607func (oFsm *OnuUpgradeFsm) enterWaitingAdapterDL(ctx context.Context, e *fsm.Event) {
608 logger.Debugw(ctx, "OnuUpgradeFsm waiting for adapter download", log.Fields{"in state": e.FSM.Current(),
609 "device-id": oFsm.deviceID})
mpagenko38662d02021-08-11 09:45:19 +0000610 syncChannel := make(chan struct{})
611 go oFsm.waitOnDownloadToAdapterReady(ctx, syncChannel, oFsm.chAdapterDlReady)
612 //block until the wait routine is really blocked on chAdapterDlReady
613 <-syncChannel
mpagenkoc26d4c02021-05-06 14:27:57 +0000614 go oFsm.pFileManager.RequestDownloadReady(ctx, oFsm.imageIdentifier, oFsm.chAdapterDlReady)
615}
616
mpagenko80622a52021-02-09 16:53:23 +0000617func (oFsm *OnuUpgradeFsm) enterPreparingDL(ctx context.Context, e *fsm.Event) {
618 logger.Debugw(ctx, "OnuUpgradeFsm prepare Download to Onu", log.Fields{"in state": e.FSM.Current(),
619 "device-id": oFsm.deviceID})
620
mpagenkoc26d4c02021-05-06 14:27:57 +0000621 var fileLen int64
622 var err error
mpagenkoaa3afe92021-05-21 16:20:58 +0000623 oFsm.mutexUpgradeParams.Lock()
mpagenkoc26d4c02021-05-06 14:27:57 +0000624 if oFsm.useAPIVersion43 {
625 //with the new API structure download to adapter is implicit and we have to wait until the image is available
626 fileLen, err = oFsm.pFileManager.GetImageBufferLen(ctx, oFsm.imageIdentifier)
627 } else {
628 fileLen, err = oFsm.pDownloadManager.getImageBufferLen(ctx, oFsm.pImageDsc.Name, oFsm.pImageDsc.LocalDir)
629 }
mpagenko80622a52021-02-09 16:53:23 +0000630 if err != nil || fileLen > int64(cMaxUint32) {
mpagenko39b703e2021-08-25 13:38:40 +0000631 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto)
mpagenkoaa3afe92021-05-21 16:20:58 +0000632 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000633 logger.Errorw(ctx, "OnuUpgradeFsm abort: problems getting image buffer length", log.Fields{
634 "device-id": oFsm.deviceID, "error": err, "length": fileLen})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000635 pBaseFsm := oFsm.PAdaptFsm
mpagenko80622a52021-02-09 16:53:23 +0000636 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000637 go func(a_pAFsm *cmn.AdapterFsm) {
638 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko80622a52021-02-09 16:53:23 +0000639 }(pBaseFsm)
640 return
641 }
642
mpagenkoc26d4c02021-05-06 14:27:57 +0000643 //copy file content to buffer
mpagenko80622a52021-02-09 16:53:23 +0000644 oFsm.imageBuffer = make([]byte, fileLen)
mpagenkoc26d4c02021-05-06 14:27:57 +0000645 if oFsm.useAPIVersion43 {
646 oFsm.imageBuffer, err = oFsm.pFileManager.GetDownloadImageBuffer(ctx, oFsm.imageIdentifier)
647 } else {
648 oFsm.imageBuffer, err = oFsm.pDownloadManager.getDownloadImageBuffer(ctx, oFsm.pImageDsc.Name, oFsm.pImageDsc.LocalDir)
649 }
mpagenko80622a52021-02-09 16:53:23 +0000650 if err != nil {
mpagenko39b703e2021-08-25 13:38:40 +0000651 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto)
mpagenkoaa3afe92021-05-21 16:20:58 +0000652 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000653 logger.Errorw(ctx, "OnuUpgradeFsm abort: can't get image buffer", log.Fields{
654 "device-id": oFsm.deviceID, "error": err})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000655 pBaseFsm := oFsm.PAdaptFsm
mpagenko80622a52021-02-09 16:53:23 +0000656 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000657 go func(a_pAFsm *cmn.AdapterFsm) {
658 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko80622a52021-02-09 16:53:23 +0000659 }(pBaseFsm)
660 return
661 }
662
663 oFsm.noOfSections = uint32(fileLen / cOmciDownloadSectionSize)
664 if fileLen%cOmciDownloadSectionSize > 0 {
mpagenkoc26d4c02021-05-06 14:27:57 +0000665 bufferPadding := make([]byte, cOmciDownloadSectionSize-uint32((fileLen)%cOmciDownloadSectionSize))
mpagenko80622a52021-02-09 16:53:23 +0000666 //expand the imageBuffer to exactly fit multiples of cOmciDownloadSectionSize with padding
mpagenkoc26d4c02021-05-06 14:27:57 +0000667 oFsm.imageBuffer = append(oFsm.imageBuffer[:(fileLen)], bufferPadding...)
mpagenko80622a52021-02-09 16:53:23 +0000668 oFsm.noOfSections++
669 }
670 oFsm.origImageLength = uint32(fileLen)
671 oFsm.imageLength = uint32(len(oFsm.imageBuffer))
mpagenko80622a52021-02-09 16:53:23 +0000672 logger.Infow(ctx, "OnuUpgradeFsm starts with StartSwDl values", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000673 "MeId": oFsm.InactiveImageMeID, "windowSizeLimit": oFsm.omciDownloadWindowSizeLimit,
mpagenko80622a52021-02-09 16:53:23 +0000674 "ImageSize": oFsm.imageLength, "original file size": fileLen})
675 //"NumberOfCircuitPacks": oFsm.numberCircuitPacks, "CircuitPacks MeId": 0}) //parallel circuit packs download not supported
mpagenkoaa3afe92021-05-21 16:20:58 +0000676
677 oFsm.mutexUpgradeParams.Unlock()
mpagenko39b703e2021-08-25 13:38:40 +0000678
679 // flush commMetricsChan
680 select {
681 case <-oFsm.chOnuDlReady:
682 logger.Debug(ctx, "flushed OnuDlReady channel")
683 default:
684 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000685 go oFsm.waitOnDownloadToOnuReady(ctx, oFsm.chOnuDlReady) // start supervision of the complete download-to-ONU procedure
686
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000687 err = oFsm.pOmciCC.SendStartSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
688 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, oFsm.omciDownloadWindowSizeLimit, oFsm.origImageLength)
mpagenko80622a52021-02-09 16:53:23 +0000689 if err != nil {
690 logger.Errorw(ctx, "StartSwDl abort: can't send section", log.Fields{
691 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +0000692 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +0000693 return
694 }
Holger Hildebrandt288d9472021-09-06 10:47:53 +0000695 oFsm.isEndSwDlOpen = true
mpagenko80622a52021-02-09 16:53:23 +0000696}
697
698func (oFsm *OnuUpgradeFsm) enterDownloadSection(ctx context.Context, e *fsm.Event) {
699 logger.Debugw(ctx, "OnuUpgradeFsm start downloading sections", log.Fields{
700 "device-id": oFsm.deviceID, "absolute window": oFsm.nextDownloadWindow})
701
702 var windowAckRequest uint8 = 0
703 var bufferStartOffset uint32
704 var bufferEndOffset uint32
705 var downloadSection []byte
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000706 FramePrint := false //default no printing of downloadSection frames
mpagenkoaa3afe92021-05-21 16:20:58 +0000707 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +0000708 oFsm.upgradePhase = cUpgradeDownloading //start of downloading image to ONU
mpagenko80622a52021-02-09 16:53:23 +0000709 if oFsm.nextDownloadSectionsAbsolute == 0 {
710 //debug print of first section frame
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000711 FramePrint = true
mpagenkoaa3afe92021-05-21 16:20:58 +0000712 oFsm.volthaImageState = voltha.ImageState_IMAGE_DOWNLOADING
mpagenko80622a52021-02-09 16:53:23 +0000713 }
714
715 for {
mpagenko38662d02021-08-11 09:45:19 +0000716 oFsm.mutexAbortRequest.RLock()
717 // this way out of the section download loop on abort request
718 if oFsm.abortRequested != voltha.ImageState_NO_ERROR {
mpagenko45586762021-10-01 08:30:22 +0000719 //states are updated when entering the reset state ...
mpagenko38662d02021-08-11 09:45:19 +0000720 oFsm.volthaDownloadReason = oFsm.abortRequested
721 oFsm.mutexAbortRequest.RUnlock()
722 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000723 pBaseFsm := oFsm.PAdaptFsm
mpagenko38662d02021-08-11 09:45:19 +0000724 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000725 go func(a_pAFsm *cmn.AdapterFsm) {
726 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko38662d02021-08-11 09:45:19 +0000727 }(pBaseFsm)
728 return
729 }
730 oFsm.mutexAbortRequest.RUnlock()
731
mpagenko80622a52021-02-09 16:53:23 +0000732 bufferStartOffset = oFsm.nextDownloadSectionsAbsolute * cOmciDownloadSectionSize
733 bufferEndOffset = bufferStartOffset + cOmciDownloadSectionSize - 1 //for representing cOmciDownloadSectionSizeLimit values
734 logger.Debugw(ctx, "DlSection values are", log.Fields{
735 "DlSectionNoAbsolute": oFsm.nextDownloadSectionsAbsolute,
736 "DlSectionWindow": oFsm.nextDownloadSectionsWindow,
737 "startOffset": bufferStartOffset, "endOffset": bufferEndOffset})
738 if bufferStartOffset+1 > oFsm.imageLength || bufferEndOffset+1 > oFsm.imageLength { //should never occur in this state
739 logger.Errorw(ctx, "OnuUpgradeFsm buffer error: exceeded length", log.Fields{
740 "device-id": oFsm.deviceID, "bufferStartOffset": bufferStartOffset,
741 "bufferEndOffset": bufferEndOffset, "imageLength": oFsm.imageLength})
mpagenko39b703e2021-08-25 13:38:40 +0000742 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'LOCAL_FILE_ERROR' would be better (proto)
mpagenkoaa3afe92021-05-21 16:20:58 +0000743 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000744 //logical error -- reset the FSM
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000745 pBaseFsm := oFsm.PAdaptFsm
mpagenko80622a52021-02-09 16:53:23 +0000746 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000747 go func(a_pAFsm *cmn.AdapterFsm) {
748 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko80622a52021-02-09 16:53:23 +0000749 }(pBaseFsm)
750 return
751 }
752 downloadSection = oFsm.imageBuffer[bufferStartOffset : bufferEndOffset+1]
753 if oFsm.nextDownloadSectionsWindow == oFsm.omciDownloadWindowSizeLimit {
754 windowAckRequest = 1
755 logger.Debugw(ctx, "DlSection expect Response for complete window", log.Fields{
756 "device-id": oFsm.deviceID, "in window": oFsm.nextDownloadWindow})
757 }
758 if oFsm.nextDownloadSectionsAbsolute+1 >= oFsm.noOfSections {
759 windowAckRequest = 1
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000760 FramePrint = true //debug print of last frame
mpagenko15ff4a52021-03-02 10:09:20 +0000761 oFsm.omciDownloadWindowSizeLast = oFsm.nextDownloadSectionsWindow
762 logger.Infow(ctx, "DlSection expect Response for last window (section)", log.Fields{
mpagenko80622a52021-02-09 16:53:23 +0000763 "device-id": oFsm.deviceID, "DlSectionNoAbsolute": oFsm.nextDownloadSectionsAbsolute})
764 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000765 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 +0000766 err := oFsm.pOmciCC.SendDownloadSection(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
767 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, windowAckRequest, oFsm.nextDownloadSectionsWindow, downloadSection, FramePrint)
mpagenko80622a52021-02-09 16:53:23 +0000768 if err != nil {
769 logger.Errorw(ctx, "DlSection abort: can't send section", log.Fields{
mpagenko15ff4a52021-03-02 10:09:20 +0000770 "device-id": oFsm.deviceID, "section absolute": oFsm.nextDownloadSectionsAbsolute, "error": err})
mpagenko45586762021-10-01 08:30:22 +0000771 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +0000772 return
773 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000774 oFsm.mutexUpgradeParams.Lock()
mpagenko80622a52021-02-09 16:53:23 +0000775 oFsm.nextDownloadSectionsAbsolute++ //always increase the absolute section counter after having sent one
776 if windowAckRequest == 1 {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000777 pBaseFsm := oFsm.PAdaptFsm
mpagenko80622a52021-02-09 16:53:23 +0000778 // Can't call FSM Event directly, decoupling it
mpagenkoaa3afe92021-05-21 16:20:58 +0000779 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000780 go func(a_pAFsm *cmn.AdapterFsm) {
781 _ = a_pAFsm.PFsm.Event(UpgradeEvWaitWindowAck) //state transition to UpgradeStVerifyWindow
mpagenko80622a52021-02-09 16:53:23 +0000782 }(pBaseFsm)
783 return
784 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000785 FramePrint = false //for the next Section frame (if wanted, can be enabled in logic before sendXXX())
mpagenko80622a52021-02-09 16:53:23 +0000786 oFsm.nextDownloadSectionsWindow++ //increase the window related section counter only if not in the last section
mpagenko59498c12021-03-18 14:15:15 +0000787 if oFsm.omciSectionInterleaveDelay > 0 {
mpagenko80622a52021-02-09 16:53:23 +0000788 //ensure a defined intersection-time-gap to leave space for further processing, other ONU's ...
mpagenkoaa3afe92021-05-21 16:20:58 +0000789 oFsm.mutexUpgradeParams.Unlock() //unlock here to give other functions some chance to process during/after the send request
mpagenko59498c12021-03-18 14:15:15 +0000790 time.Sleep(oFsm.omciSectionInterleaveDelay * time.Millisecond)
mpagenkoaa3afe92021-05-21 16:20:58 +0000791 oFsm.mutexUpgradeParams.Lock()
mpagenko80622a52021-02-09 16:53:23 +0000792 }
793 }
794}
795
796func (oFsm *OnuUpgradeFsm) enterVerifyWindow(ctx context.Context, e *fsm.Event) {
797 logger.Debugw(ctx, "OnuUpgradeFsm verify DL window ack", log.Fields{
798 "for window": oFsm.nextDownloadWindow, "device-id": oFsm.deviceID})
799}
800
801func (oFsm *OnuUpgradeFsm) enterFinalizeDL(ctx context.Context, e *fsm.Event) {
mpagenko80622a52021-02-09 16:53:23 +0000802 logger.Infow(ctx, "OnuUpgradeFsm finalize DL", log.Fields{
mpagenko59498c12021-03-18 14:15:15 +0000803 "device-id": oFsm.deviceID, "crc": strconv.FormatInt(int64(oFsm.imageCRC), 16), "delay": oFsm.delayEndSwDl})
mpagenko80622a52021-02-09 16:53:23 +0000804
mpagenkoaa3afe92021-05-21 16:20:58 +0000805 oFsm.mutexUpgradeParams.RLock()
mpagenko80622a52021-02-09 16:53:23 +0000806 if oFsm.delayEndSwDl {
mpagenkoaa3afe92021-05-21 16:20:58 +0000807 oFsm.mutexUpgradeParams.RUnlock()
mpagenko80622a52021-02-09 16:53:23 +0000808 //give the ONU some time for image evaluation (hoping it does not base that on first EndSwDl itself)
809 // should not be set in case this state is used for real download abort (not yet implemented)
810 time.Sleep(cOmciEndSwDlDelaySeconds * time.Second)
mpagenkoaa3afe92021-05-21 16:20:58 +0000811 } else {
812 oFsm.mutexUpgradeParams.RUnlock()
mpagenko80622a52021-02-09 16:53:23 +0000813 }
814
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000815 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000816 if pBaseFsm == nil {
mpagenko38662d02021-08-11 09:45:19 +0000817 logger.Errorw(ctx, "EndSwDl abort: BaseFsm invalid", log.Fields{"device-id": oFsm.deviceID})
818 oFsm.mutexUpgradeParams.Lock()
mpagenko38662d02021-08-11 09:45:19 +0000819 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR
820 oFsm.mutexUpgradeParams.Unlock()
mpagenko59498c12021-03-18 14:15:15 +0000821 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000822 go func(a_pAFsm *cmn.AdapterFsm) {
823 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko59498c12021-03-18 14:15:15 +0000824 }(pBaseFsm)
825 return
826 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000827 err := oFsm.pOmciCC.SendEndSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
828 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, oFsm.origImageLength, oFsm.imageCRC)
Holger Hildebrandt288d9472021-09-06 10:47:53 +0000829
mpagenko80622a52021-02-09 16:53:23 +0000830 if err != nil {
831 logger.Errorw(ctx, "EndSwDl abort: can't send section", log.Fields{
832 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +0000833 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +0000834 return
835 }
mpagenko59498c12021-03-18 14:15:15 +0000836 // go waiting for the EndSwDLResponse and check, if the ONU is ready for activation
837 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000838 go func(a_pAFsm *cmn.AdapterFsm) {
839 _ = a_pAFsm.PFsm.Event(UpgradeEvWaitEndDownload)
mpagenko59498c12021-03-18 14:15:15 +0000840 }(pBaseFsm)
841}
842
843func (oFsm *OnuUpgradeFsm) enterWaitEndDL(ctx context.Context, e *fsm.Event) {
844 logger.Infow(ctx, "OnuUpgradeFsm WaitEndDl", log.Fields{
845 "device-id": oFsm.deviceID, "wait delay": oFsm.waitDelayEndSwDl * time.Second, "wait count": oFsm.waitCountEndSwDl})
846 if oFsm.waitCountEndSwDl == 0 {
847 logger.Errorw(ctx, "WaitEndDl abort: max limit of EndSwDL reached", log.Fields{
848 "device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000849 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000850 if pBaseFsm == nil {
851 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
852 "device-id": oFsm.deviceID})
853 return
854 }
mpagenko38662d02021-08-11 09:45:19 +0000855 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +0000856 oFsm.isEndSwDlOpen = false //no need to additionally request abort of download (already finished)
mpagenko39b703e2021-08-25 13:38:40 +0000857 oFsm.volthaDownloadReason = voltha.ImageState_IMAGE_REFUSED_BY_ONU //something like 'END_DOWNLOAD_TIMEOUT' would be better (proto)
mpagenko38662d02021-08-11 09:45:19 +0000858 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000859 go func(a_pAFsm *cmn.AdapterFsm) {
860 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko59498c12021-03-18 14:15:15 +0000861 }(pBaseFsm)
862 return
863 }
864
865 oFsm.waitCountEndSwDl--
866 select {
867 case <-time.After(oFsm.waitDelayEndSwDl * time.Second):
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000868 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000869 if pBaseFsm == nil {
870 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
871 "device-id": oFsm.deviceID})
872 //FSM may be reset already from somewhere else, nothing we can do here anymore
873 return
874 }
875 //retry End SW DL
mpagenkoaa3afe92021-05-21 16:20:58 +0000876 oFsm.mutexUpgradeParams.Lock()
mpagenko59498c12021-03-18 14:15:15 +0000877 oFsm.delayEndSwDl = false //no more extra delay for the request
mpagenkoaa3afe92021-05-21 16:20:58 +0000878 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000879 go func(a_pAFsm *cmn.AdapterFsm) {
880 _ = a_pAFsm.PFsm.Event(UpgradeEvContinueFinalize)
mpagenko59498c12021-03-18 14:15:15 +0000881 }(pBaseFsm)
882 return
883 case success := <-oFsm.chReceiveExpectedResponse:
884 logger.Debugw(ctx, "WaitEndDl stop wait timer", log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +0000885 oFsm.isEndSwDlOpen = false //no request to abort of download (already finished or immediate abort)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000886 pBaseFsm := oFsm.PAdaptFsm
mpagenko59498c12021-03-18 14:15:15 +0000887 if pBaseFsm == nil {
888 logger.Errorw(ctx, "WaitEndDl abort: BaseFsm invalid", log.Fields{
889 "device-id": oFsm.deviceID})
890 //FSM may be reset already from somewhere else, nothing we can do here anymore
891 return
892 }
893 if success {
894 //answer received with ready indication
mpagenko38662d02021-08-11 09:45:19 +0000895 //useAPIVersion43 may not conflict in concurrency in this state function
896 if oFsm.useAPIVersion43 { // newer API usage requires verification of downloaded image version
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000897 go func(a_pAFsm *cmn.AdapterFsm) {
898 _ = a_pAFsm.PFsm.Event(UpgradeEvCheckImageName)
mpagenkoc26d4c02021-05-06 14:27:57 +0000899 }(pBaseFsm)
mpagenko38662d02021-08-11 09:45:19 +0000900 } else { // elder API usage does not support image version check -immediately consider download as successful
901 if oFsm.activateImage {
902 //immediate activation requested
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000903 go func(a_pAFsm *cmn.AdapterFsm) {
904 _ = a_pAFsm.PFsm.Event(UpgradeEvRequestActivate)
mpagenko38662d02021-08-11 09:45:19 +0000905 }(pBaseFsm)
906 } else {
907 //have to wait on explicit activation request
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000908 go func(a_pAFsm *cmn.AdapterFsm) {
909 _ = a_pAFsm.PFsm.Event(UpgradeEvWaitForActivate)
mpagenko38662d02021-08-11 09:45:19 +0000910 }(pBaseFsm)
911 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000912 }
mpagenko59498c12021-03-18 14:15:15 +0000913 return
914 }
915 //timer was aborted
mpagenko45586762021-10-01 08:30:22 +0000916 oFsm.abortOnOmciError(ctx, true)
mpagenko59498c12021-03-18 14:15:15 +0000917 return
918 }
mpagenko80622a52021-02-09 16:53:23 +0000919}
920
mpagenko38662d02021-08-11 09:45:19 +0000921func (oFsm *OnuUpgradeFsm) enterCheckImageName(ctx context.Context, e *fsm.Event) {
922 logger.Debugw(ctx, "OnuUpgradeFsm checking downloaded image name", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000923 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
mpagenko38662d02021-08-11 09:45:19 +0000924 requestedAttributes := me.AttributeValueMap{"IsCommitted": 0, "IsActive": 0, "Version": ""}
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000925 meInstance, err := oFsm.pOmciCC.SendGetMe(log.WithSpanFromContext(context.Background(), ctx),
926 me.SoftwareImageClassID, oFsm.InactiveImageMeID, requestedAttributes, oFsm.pDeviceHandler.GetOmciTimeout(),
927 false, oFsm.PAdaptFsm.CommChan)
mpagenko38662d02021-08-11 09:45:19 +0000928 if err != nil {
929 logger.Errorw(ctx, "OnuUpgradeFsm get Software Image ME result error",
930 log.Fields{"device-id": oFsm.deviceID, "Error": err})
mpagenko45586762021-10-01 08:30:22 +0000931 oFsm.abortOnOmciError(ctx, true)
mpagenko38662d02021-08-11 09:45:19 +0000932 return
933 }
934 oFsm.pLastTxMeInstance = meInstance
935}
936
mpagenko80622a52021-02-09 16:53:23 +0000937func (oFsm *OnuUpgradeFsm) enterActivateSw(ctx context.Context, e *fsm.Event) {
938 logger.Infow(ctx, "OnuUpgradeFsm activate SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000939 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
mpagenko80622a52021-02-09 16:53:23 +0000940
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000941 err := oFsm.pOmciCC.SendActivateSoftware(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
942 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID)
mpagenko80622a52021-02-09 16:53:23 +0000943 if err != nil {
944 logger.Errorw(ctx, "ActivateSw abort: can't send activate frame", log.Fields{
945 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +0000946 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +0000947 return
948 }
mpagenko38662d02021-08-11 09:45:19 +0000949 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +0000950 oFsm.upgradePhase = cUpgradeActivating //start of image activation for ONU
mpagenko38662d02021-08-11 09:45:19 +0000951 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATING
952 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +0000953}
954
955func (oFsm *OnuUpgradeFsm) enterCommitSw(ctx context.Context, e *fsm.Event) {
mpagenko38662d02021-08-11 09:45:19 +0000956 logger.Debugw(ctx, "OnuUpgradeFsm start commit SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000957 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
mpagenko38662d02021-08-11 09:45:19 +0000958 //any abort request (also conditional) is still regarded as valid as the commit indication might not be possible to verify
959 // (which is a bit problematic as the ONU might already be in committed state,
960 // in this case (committing failed) always 'onuimage list' should be used to verify the real state (if ONU is reachable))
961 if activeImageID, err := oFsm.pDevEntry.GetActiveImageMeID(ctx); err == nil {
mpagenkoaa3afe92021-05-21 16:20:58 +0000962 oFsm.mutexUpgradeParams.Lock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000963 if activeImageID == oFsm.InactiveImageMeID {
964 inactiveImageID := oFsm.InactiveImageMeID
mpagenko15ff4a52021-03-02 10:09:20 +0000965 logger.Infow(ctx, "OnuUpgradeFsm commit SW", log.Fields{
mpagenkoaa3afe92021-05-21 16:20:58 +0000966 "device-id": oFsm.deviceID, "me-id": inactiveImageID}) //more efficient activeImageID with above check
967 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTING
mpagenko45586762021-10-01 08:30:22 +0000968 oFsm.upgradePhase = cUpgradeCommitting //start of image commitment for ONU
mpagenkoaa3afe92021-05-21 16:20:58 +0000969 oFsm.mutexUpgradeParams.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000970 err := oFsm.pOmciCC.SendCommitSoftware(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
971 oFsm.PAdaptFsm.CommChan, inactiveImageID) //more efficient activeImageID with above check
mpagenko15ff4a52021-03-02 10:09:20 +0000972 if err != nil {
973 logger.Errorw(ctx, "CommitSw abort: can't send commit sw frame", log.Fields{
974 "device-id": oFsm.deviceID, "error": err})
mpagenko45586762021-10-01 08:30:22 +0000975 oFsm.abortOnOmciError(ctx, true)
mpagenko15ff4a52021-03-02 10:09:20 +0000976 return
977 }
978 return
979 }
mpagenko38662d02021-08-11 09:45:19 +0000980 oFsm.mutexUpgradeParams.Unlock()
mpagenko15ff4a52021-03-02 10:09:20 +0000981 logger.Errorw(ctx, "OnuUpgradeFsm active ImageId <> IdToCommit", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000982 "device-id": oFsm.deviceID, "active ID": activeImageID, "to commit ID": oFsm.InactiveImageMeID})
mpagenko38662d02021-08-11 09:45:19 +0000983 } else {
984 logger.Errorw(ctx, "OnuUpgradeFsm can't commit, no valid active image", log.Fields{
985 "device-id": oFsm.deviceID})
mpagenko15ff4a52021-03-02 10:09:20 +0000986 }
mpagenko38662d02021-08-11 09:45:19 +0000987 oFsm.mutexUpgradeParams.Lock()
988 oFsm.conditionalCancelRequested = false //any lingering conditional cancelRequest is superseded by this error
mpagenko38662d02021-08-11 09:45:19 +0000989 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
mpagenko38662d02021-08-11 09:45:19 +0000990 oFsm.mutexUpgradeParams.Unlock()
mpagenko15ff4a52021-03-02 10:09:20 +0000991 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000992 pBaseFsm := oFsm.PAdaptFsm
mpagenko15ff4a52021-03-02 10:09:20 +0000993 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000994 go func(a_pAFsm *cmn.AdapterFsm) {
995 _ = a_pAFsm.PFsm.Event(UpgradeEvAbort)
mpagenko15ff4a52021-03-02 10:09:20 +0000996 }(pBaseFsm)
997}
998
999func (oFsm *OnuUpgradeFsm) enterCheckCommitted(ctx context.Context, e *fsm.Event) {
mpagenko38662d02021-08-11 09:45:19 +00001000 logger.Debugw(ctx, "OnuUpgradeFsm checking committed SW", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001001 "device-id": oFsm.deviceID, "me-id": oFsm.InactiveImageMeID})
mpagenko15ff4a52021-03-02 10:09:20 +00001002 requestedAttributes := me.AttributeValueMap{"IsCommitted": 0, "IsActive": 0, "Version": ""}
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001003 meInstance, err := oFsm.pOmciCC.SendGetMe(log.WithSpanFromContext(context.Background(), ctx),
1004 me.SoftwareImageClassID, oFsm.InactiveImageMeID, requestedAttributes, oFsm.pDeviceHandler.GetOmciTimeout(), false, oFsm.PAdaptFsm.CommChan)
ozgecanetsiab36ed572021-04-01 10:38:48 +03001005 if err != nil {
1006 logger.Errorw(ctx, "OnuUpgradeFsm get Software Image ME result error",
1007 log.Fields{"device-id": oFsm.deviceID, "Error": err})
mpagenko45586762021-10-01 08:30:22 +00001008 oFsm.abortOnOmciError(ctx, true)
ozgecanetsiab36ed572021-04-01 10:38:48 +03001009 return
1010 }
mpagenko15ff4a52021-03-02 10:09:20 +00001011 oFsm.pLastTxMeInstance = meInstance
mpagenko80622a52021-02-09 16:53:23 +00001012}
1013
1014func (oFsm *OnuUpgradeFsm) enterResetting(ctx context.Context, e *fsm.Event) {
1015 logger.Debugw(ctx, "OnuUpgradeFsm resetting", log.Fields{"device-id": oFsm.deviceID})
1016
mpagenko45586762021-10-01 08:30:22 +00001017 oFsm.stateUpdateOnReset(ctx)
mpagenko38662d02021-08-11 09:45:19 +00001018
mpagenkoc26d4c02021-05-06 14:27:57 +00001019 // in case the download-to-ONU timer is still running - cancel it
mpagenko39b703e2021-08-25 13:38:40 +00001020 //use non-blocking channel (to be independent from receiver state)
1021 select {
1022 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
1023 case oFsm.chOnuDlReady <- false:
1024 default:
mpagenkoc26d4c02021-05-06 14:27:57 +00001025 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001026 pConfigUpgradeStateAFsm := oFsm.PAdaptFsm
1027 if pConfigUpgradeStateAFsm != nil {
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001028 var nextEvent string
1029 if oFsm.isEndSwDlOpen {
mpagenko45586762021-10-01 08:30:22 +00001030 if oFsm.repeatAbort {
1031 oFsm.delayEndSwDl = true //run next abort with delay
1032 } else { //initial request
1033 oFsm.delayEndSwDl = false //run next abort with no delay
1034 oFsm.waitCountEndSwDl = cWaitCountEndSwDl //init for possible repetitions
1035 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001036 nextEvent = UpgradeEvAbortSwDownload
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001037 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001038 nextEvent = UpgradeEvRestart
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001039 }
1040 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001041 go func(a_pAFsm *cmn.AdapterFsm) {
1042 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1043 _ = a_pAFsm.PFsm.Event(nextEvent)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001044 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001045 }(pConfigUpgradeStateAFsm)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001046 }
1047}
mpagenkoc26d4c02021-05-06 14:27:57 +00001048
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001049func (oFsm *OnuUpgradeFsm) enterAbortingDL(ctx context.Context, e *fsm.Event) {
1050 logger.Debugw(ctx, "OnuUpgradeFsm aborting download to ONU", log.Fields{"device-id": oFsm.deviceID})
1051
mpagenko45586762021-10-01 08:30:22 +00001052 oFsm.mutexUpgradeParams.RLock()
1053 if oFsm.delayEndSwDl {
1054 oFsm.mutexUpgradeParams.RUnlock()
1055 //give the ONU some time for image discard activities
1056 time.Sleep(cOmciEndSwDlDelaySeconds * time.Second)
1057 } else {
1058 oFsm.mutexUpgradeParams.RUnlock()
1059 }
1060
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001061 pBaseFsm := oFsm.PAdaptFsm
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001062 if pBaseFsm == nil {
1063 logger.Errorw(ctx, "OnuUpgradeFsm aborting download: BaseFsm invalid", log.Fields{"device-id": oFsm.deviceID})
1064 return
1065 }
1066 // abort the download operation by sending an end software download message with invalid CRC and image size
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001067 err := oFsm.pOmciCC.SendEndSoftwareDownload(log.WithSpanFromContext(context.Background(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), false,
1068 oFsm.PAdaptFsm.CommChan, oFsm.InactiveImageMeID, 0, 0xFFFFFFFF)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001069
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001070 if err != nil {
1071 logger.Errorw(ctx, "OnuUpgradeFsm aborting download: can't send EndSwDl request", log.Fields{"device-id": oFsm.deviceID})
1072 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001073 go func(a_pAFsm *cmn.AdapterFsm) {
1074 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1075 _ = a_pAFsm.PFsm.Event(UpgradeEvRestart)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001076 }
1077 }(pBaseFsm)
1078 return
1079 }
1080 select {
1081 case <-time.After(oFsm.pOmciCC.GetMaxOmciTimeoutWithRetries() * time.Second):
1082 logger.Warnw(ctx, "OnuUpgradeFsm aborting download: timeout - no response received", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001083 go func(a_pAFsm *cmn.AdapterFsm) {
1084 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1085 _ = a_pAFsm.PFsm.Event(UpgradeEvRestart)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001086 }
1087 }(pBaseFsm)
1088 return
mpagenko45586762021-10-01 08:30:22 +00001089 case response := <-oFsm.chReceiveAbortEndSwDlResponse:
1090 logger.Debugw(ctx, "OnuUpgradeFsm aborting download: response received",
1091 log.Fields{"device-id": oFsm.deviceID, "response": response})
1092 //had to shift processing to separate function due to SCA complexity
1093 if oFsm.abortingDlEvaluateResponse(ctx, pBaseFsm, response) {
1094 return //event sent from function already
1095 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001096 go func(a_pAFsm *cmn.AdapterFsm) {
1097 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1098 _ = a_pAFsm.PFsm.Event(UpgradeEvRestart)
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001099 }
1100 }(pBaseFsm)
1101 return
mpagenko45586762021-10-01 08:30:22 +00001102 } //select
1103}
1104
1105//abortingDlEvaluateResponse waits for a channel indication with decision to proceed the FSM processing
1106func (oFsm *OnuUpgradeFsm) abortingDlEvaluateResponse(ctx context.Context,
1107 pBaseFsm *cmn.AdapterFsm, aResponseResult tEndSwDlResponseResult) bool {
1108 switch aResponseResult {
1109 case cEndSwDlResponseBusy: // indication for device busy, needs repetition
1110 if oFsm.waitCountEndSwDl == 0 {
1111 logger.Errorw(ctx, "aborting download: max limit of EndSwDl reached", log.Fields{
1112 "device-id": oFsm.deviceID})
1113 go func(a_pAFsm *cmn.AdapterFsm) {
1114 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1115 _ = a_pAFsm.PFsm.Event(UpgradeEvRestart) //give up and let FSM terminate
1116 }
1117 }(pBaseFsm)
1118 } else {
1119 logger.Debugw(ctx, "aborting download: re-trigger sending abort SwDl", log.Fields{
1120 "device-id": oFsm.deviceID, "counter": oFsm.waitCountEndSwDl})
1121 oFsm.waitCountEndSwDl--
1122 oFsm.repeatAbort = true //repeated request in next round
1123 go func(a_pAFsm *cmn.AdapterFsm) {
1124 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1125 _ = a_pAFsm.PFsm.Event(UpgradeEvReset) //which then re-triggers sending AbortSwDL
1126 }
1127 }(pBaseFsm)
1128 }
1129 return true
1130 case cEndSwDlResponseSuccess: // indication for success response
1131 logger.Infow(ctx, "aborting download: success response, terminating FSM", log.Fields{
1132 "device-id": oFsm.deviceID})
1133 case cEndSwDlResponseAbort: // indication for request to abort waiting for response
1134 logger.Infow(ctx, "aborting download: request to abort waiting, terminating FSM", log.Fields{
1135 "device-id": oFsm.deviceID})
1136 default:
1137 logger.Errorw(ctx, "aborting download: unknown channel indication, terminating FSM", log.Fields{
1138 "device-id": oFsm.deviceID})
1139 } //switch
1140 return false
Holger Hildebrandt288d9472021-09-06 10:47:53 +00001141}
1142
1143func (oFsm *OnuUpgradeFsm) enterRestarting(ctx context.Context, e *fsm.Event) {
1144 logger.Debugw(ctx, "OnuUpgradeFsm restarting", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001145 pConfigUpgradeStateAFsm := oFsm.PAdaptFsm
1146 if pConfigUpgradeStateAFsm != nil {
mpagenko80622a52021-02-09 16:53:23 +00001147 // abort running message processing
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001148 fsmAbortMsg := cmn.Message{
1149 Type: cmn.TestMsg,
1150 Data: cmn.TestMessage{
1151 TestMessageVal: cmn.AbortMessageProcessing,
mpagenko80622a52021-02-09 16:53:23 +00001152 },
1153 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001154 pConfigUpgradeStateAFsm.CommChan <- fsmAbortMsg
mpagenko80622a52021-02-09 16:53:23 +00001155
1156 //try to restart the FSM to 'disabled'
1157 // Can't call FSM Event directly, decoupling it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001158 go func(a_pAFsm *cmn.AdapterFsm) {
1159 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
1160 _ = a_pAFsm.PFsm.Event(UpgradeEvDisable)
mpagenko80622a52021-02-09 16:53:23 +00001161 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001162 }(pConfigUpgradeStateAFsm)
mpagenko80622a52021-02-09 16:53:23 +00001163 }
1164}
1165
1166func (oFsm *OnuUpgradeFsm) enterDisabled(ctx context.Context, e *fsm.Event) {
1167 logger.Debugw(ctx, "OnuUpgradeFsm enters disabled state", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001168 // 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 +00001169 if oFsm.pDeviceHandler != nil {
1170 //request removal of 'reference' in the Handler (completely clear the FSM and its data)
mpagenko38662d02021-08-11 09:45:19 +00001171 pLastUpgradeImageState := &voltha.ImageState{
1172 Version: oFsm.imageVersion,
1173 DownloadState: oFsm.volthaDownloadState,
1174 Reason: oFsm.volthaDownloadReason,
1175 ImageState: oFsm.volthaImageState,
1176 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001177 go oFsm.pDeviceHandler.RemoveOnuUpgradeFsm(ctx, pLastUpgradeImageState)
mpagenko80622a52021-02-09 16:53:23 +00001178 }
1179}
1180
1181func (oFsm *OnuUpgradeFsm) processOmciUpgradeMessages(ctx context.Context) { //ctx context.Context?
1182 logger.Debugw(ctx, "Start OnuUpgradeFsm Msg processing", log.Fields{"for device-id": oFsm.deviceID})
1183loop:
1184 for {
1185 // case <-ctx.Done():
1186 // logger.Info(ctx,"MibSync Msg", log.Fields{"Message handling canceled via context for device-id": oFsm.deviceID})
1187 // break loop
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001188 message, ok := <-oFsm.PAdaptFsm.CommChan
mpagenko80622a52021-02-09 16:53:23 +00001189 if !ok {
1190 logger.Info(ctx, "OnuUpgradeFsm Rx Msg - could not read from channel", log.Fields{"device-id": oFsm.deviceID})
1191 // but then we have to ensure a restart of the FSM as well - as exceptional procedure
mpagenko45586762021-10-01 08:30:22 +00001192 oFsm.abortOnOmciError(ctx, true)
mpagenko80622a52021-02-09 16:53:23 +00001193 break loop
1194 }
1195 logger.Debugw(ctx, "OnuUpgradeFsm Rx Msg", log.Fields{"device-id": oFsm.deviceID})
1196
1197 switch message.Type {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001198 case cmn.TestMsg:
1199 msg, _ := message.Data.(cmn.TestMessage)
1200 if msg.TestMessageVal == cmn.AbortMessageProcessing {
mpagenko80622a52021-02-09 16:53:23 +00001201 logger.Infow(ctx, "OnuUpgradeFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.deviceID})
1202 break loop
1203 }
1204 logger.Warnw(ctx, "OnuUpgradeFsm unknown TestMessage", log.Fields{"device-id": oFsm.deviceID, "MessageVal": msg.TestMessageVal})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001205 case cmn.OMCI:
1206 msg, _ := message.Data.(cmn.OmciMessage)
mpagenko80622a52021-02-09 16:53:23 +00001207 oFsm.handleOmciOnuUpgradeMessage(ctx, msg)
1208 default:
1209 logger.Warn(ctx, "OnuUpgradeFsm Rx unknown message", log.Fields{"device-id": oFsm.deviceID,
1210 "message.Type": message.Type})
1211 }
1212 }
1213 logger.Infow(ctx, "End OnuUpgradeFsm Msg processing", log.Fields{"device-id": oFsm.deviceID})
1214}
1215
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001216func (oFsm *OnuUpgradeFsm) handleOmciOnuUpgradeMessage(ctx context.Context, msg cmn.OmciMessage) {
mpagenko80622a52021-02-09 16:53:23 +00001217 logger.Debugw(ctx, "Rx OMCI OnuUpgradeFsm Msg", log.Fields{"device-id": oFsm.deviceID,
1218 "msgType": msg.OmciMsg.MessageType})
1219
1220 switch msg.OmciMsg.MessageType {
1221 case omci.StartSoftwareDownloadResponseType:
1222 {
mpagenko45586762021-10-01 08:30:22 +00001223 oFsm.handleRxStartSwDownloadResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001224 return
1225 } //StartSoftwareDownloadResponseType
1226 case omci.DownloadSectionResponseType:
1227 {
mpagenko45586762021-10-01 08:30:22 +00001228 oFsm.handleRxSwSectionResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001229 return
mpagenko80622a52021-02-09 16:53:23 +00001230 } //DownloadSectionResponseType
1231 case omci.EndSoftwareDownloadResponseType:
1232 {
mpagenko45586762021-10-01 08:30:22 +00001233 oFsm.handleRxEndSwDownloadResponse(ctx, msg)
mpagenko80622a52021-02-09 16:53:23 +00001234 return
1235 } //EndSoftwareDownloadResponseType
1236 case omci.ActivateSoftwareResponseType:
1237 {
1238 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeActivateSoftwareResponse)
1239 if msgLayer == nil {
1240 logger.Errorw(ctx, "Omci Msg layer could not be detected for ActivateSw",
1241 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001242 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001243 return
1244 }
1245 msgObj, msgOk := msgLayer.(*omci.ActivateSoftwareResponse)
1246 if !msgOk {
1247 logger.Errorw(ctx, "Omci Msg layer could not be assigned for ActivateSw",
1248 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001249 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001250 return
1251 }
1252 logger.Debugw(ctx, "OnuUpgradeFsm ActivateSwResponse data", log.Fields{
1253 "device-id": oFsm.deviceID, "data-fields": msgObj})
1254 if msgObj.Result != me.Success {
1255 logger.Errorw(ctx, "OnuUpgradeFsm ActivateSwResponse result error - later: drive FSM to abort state ?",
1256 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
mpagenko45586762021-10-01 08:30:22 +00001257 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001258 return
1259 }
mpagenko183647c2021-06-08 15:25:04 +00001260 oFsm.mutexUpgradeParams.Lock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001261 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
mpagenko38662d02021-08-11 09:45:19 +00001262 // the image is regarded as active really only after ONU reboot and according indication (ONU down/up procedure)
mpagenko183647c2021-06-08 15:25:04 +00001263 oFsm.mutexUpgradeParams.Unlock()
1264 logger.Infow(ctx, "Expected ActivateSwResponse received",
1265 log.Fields{"device-id": oFsm.deviceID, "commit": oFsm.commitImage})
1266 if oFsm.commitImage {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001267 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvWaitForCommit)
mpagenko183647c2021-06-08 15:25:04 +00001268 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001269 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvActivationDone) // let the FSM wait for external commit request
mpagenko183647c2021-06-08 15:25:04 +00001270 }
mpagenko80622a52021-02-09 16:53:23 +00001271 return
1272 }
mpagenko183647c2021-06-08 15:25:04 +00001273 oFsm.mutexUpgradeParams.Unlock()
mpagenko80622a52021-02-09 16:53:23 +00001274 logger.Errorw(ctx, "OnuUpgradeFsm ActivateSwResponse wrong ME instance: abort",
1275 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
mpagenko45586762021-10-01 08:30:22 +00001276 oFsm.abortOnOmciError(ctx, false)
mpagenko80622a52021-02-09 16:53:23 +00001277 return
1278 } //ActivateSoftwareResponseType
mpagenko15ff4a52021-03-02 10:09:20 +00001279 case omci.CommitSoftwareResponseType:
1280 {
1281 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeCommitSoftwareResponse)
1282 if msgLayer == nil {
1283 logger.Errorw(ctx, "Omci Msg layer could not be detected for CommitResponse",
1284 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001285 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001286 return
1287 }
1288 msgObj, msgOk := msgLayer.(*omci.CommitSoftwareResponse)
1289 if !msgOk {
1290 logger.Errorw(ctx, "Omci Msg layer could not be assigned for CommitResponse",
1291 log.Fields{"device-id": oFsm.deviceID})
mpagenko45586762021-10-01 08:30:22 +00001292 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001293 return
1294 }
mpagenkobf67a092021-03-17 09:52:28 +00001295 if msgObj.Result != me.Success {
mpagenko15ff4a52021-03-02 10:09:20 +00001296 logger.Errorw(ctx, "OnuUpgradeFsm SwImage CommitResponse result error - later: drive FSM to abort state ?",
1297 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
mpagenko45586762021-10-01 08:30:22 +00001298 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001299 return
mpagenkobf67a092021-03-17 09:52:28 +00001300 }
mpagenkoaa3afe92021-05-21 16:20:58 +00001301 oFsm.mutexUpgradeParams.RLock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001302 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
mpagenkoaa3afe92021-05-21 16:20:58 +00001303 oFsm.mutexUpgradeParams.RUnlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001304 logger.Debugw(ctx, "OnuUpgradeFsm Expected SwImage CommitResponse received", log.Fields{"device-id": oFsm.deviceID})
1305 //verifying committed image
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001306 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvCheckCommitted)
mpagenko15ff4a52021-03-02 10:09:20 +00001307 return
1308 }
mpagenkoaa3afe92021-05-21 16:20:58 +00001309 oFsm.mutexUpgradeParams.RUnlock()
mpagenko15ff4a52021-03-02 10:09:20 +00001310 logger.Errorw(ctx, "OnuUpgradeFsm SwImage CommitResponse wrong ME instance: abort",
1311 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
mpagenko45586762021-10-01 08:30:22 +00001312 oFsm.abortOnOmciError(ctx, false)
mpagenko15ff4a52021-03-02 10:09:20 +00001313 return
1314 } //CommitSoftwareResponseType
1315 case omci.GetResponseType:
1316 {
mpagenko45586762021-10-01 08:30:22 +00001317 oFsm.handleRxSwGetResponse(ctx, msg)
mpagenko15ff4a52021-03-02 10:09:20 +00001318 return
1319 } //GetResponseType
mpagenko80622a52021-02-09 16:53:23 +00001320 default:
1321 {
1322 logger.Errorw(ctx, "Rx OMCI unhandled MsgType",
1323 log.Fields{"omciMsgType": msg.OmciMsg.MessageType, "device-id": oFsm.deviceID})
1324 return
1325 }
1326 }
1327}
1328
mpagenko45586762021-10-01 08:30:22 +00001329func (oFsm *OnuUpgradeFsm) handleRxStartSwDownloadResponse(ctx context.Context, msg cmn.OmciMessage) {
1330 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeStartSoftwareDownloadResponse)
1331 if msgLayer == nil {
1332 logger.Errorw(ctx, "Omci Msg layer could not be detected for StartSwDlResponse",
1333 log.Fields{"device-id": oFsm.deviceID})
1334 oFsm.abortOnOmciError(ctx, false)
1335 return
1336 }
1337 msgObj, msgOk := msgLayer.(*omci.StartSoftwareDownloadResponse)
1338 if !msgOk {
1339 logger.Errorw(ctx, "Omci Msg layer could not be assigned for StartSwDlResponse",
1340 log.Fields{"device-id": oFsm.deviceID})
1341 oFsm.abortOnOmciError(ctx, false)
1342 return
1343 }
1344 logger.Debugw(ctx, "OnuUpgradeFsm StartSwDlResponse data", log.Fields{
1345 "device-id": oFsm.deviceID, "data-fields": msgObj})
1346 if msgObj.Result != me.Success {
1347 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse result error - later: drive FSM to abort state ?",
1348 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1349 oFsm.abortOnOmciError(ctx, false)
1350 return
1351 }
1352
1353 oFsm.mutexUpgradeParams.Lock()
1354 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1355 logger.Debugw(ctx, "Expected StartSwDlResponse received", log.Fields{"device-id": oFsm.deviceID})
1356 if msgObj.WindowSize != oFsm.omciDownloadWindowSizeLimit {
1357 // also response WindowSize = 0 is a valid number for used Window size 1
1358 logger.Debugw(ctx, "different StartSwDlResponse window size requested by ONU", log.Fields{
1359 "acceptedOnuWindowSizeLimit": msgObj.WindowSize, "device-id": oFsm.deviceID})
1360 oFsm.omciDownloadWindowSizeLimit = msgObj.WindowSize
1361 }
1362 oFsm.noOfWindows = oFsm.noOfSections / uint32(oFsm.omciDownloadWindowSizeLimit+1)
1363 if oFsm.noOfSections%uint32(oFsm.omciDownloadWindowSizeLimit+1) > 0 {
1364 oFsm.noOfWindows++
1365 }
1366 logger.Debugw(ctx, "OnuUpgradeFsm will use", log.Fields{
1367 "windows": oFsm.noOfWindows, "sections": oFsm.noOfSections,
1368 "at WindowSizeLimit": oFsm.omciDownloadWindowSizeLimit})
1369 oFsm.nextDownloadSectionsAbsolute = 0
1370 oFsm.nextDownloadSectionsWindow = 0
1371 oFsm.nextDownloadWindow = 0
1372
1373 oFsm.mutexUpgradeParams.Unlock()
1374 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvRxStartSwDownload)
1375 return
1376 }
1377 oFsm.mutexUpgradeParams.Unlock()
1378 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse wrong ME instance: try again (later)?",
1379 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1380 oFsm.abortOnOmciError(ctx, false)
1381} //handleRxStartSwDownloadResponse
1382
1383func (oFsm *OnuUpgradeFsm) handleRxSwSectionResponse(ctx context.Context, msg cmn.OmciMessage) {
1384 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeDownloadSectionResponse)
1385 if msgLayer == nil {
1386 logger.Errorw(ctx, "Omci Msg layer could not be detected for DlSectionResponse",
1387 log.Fields{"device-id": oFsm.deviceID, "omci-message": msg.OmciMsg})
1388 oFsm.abortOnOmciError(ctx, false)
1389 return
1390 }
1391 msgObj, msgOk := msgLayer.(*omci.DownloadSectionResponse)
1392 if !msgOk {
1393 logger.Errorw(ctx, "Omci Msg layer could not be assigned for DlSectionResponse",
1394 log.Fields{"device-id": oFsm.deviceID})
1395 oFsm.abortOnOmciError(ctx, false)
1396 return
1397 }
1398 logger.Debugw(ctx, "OnuUpgradeFsm DlSectionResponse Data", log.Fields{
1399 "device-id": oFsm.deviceID, "data-fields": msgObj})
1400 if msgObj.Result != me.Success {
1401 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse result error - later: repeat window once?", //TODO!!!
1402 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1403 oFsm.abortOnOmciError(ctx, false)
1404 return
1405 }
1406 oFsm.mutexUpgradeParams.Lock()
1407 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1408 sectionNumber := msgObj.SectionNumber
1409 logger.Infow(ctx, "DlSectionResponse received", log.Fields{
1410 "window section-number": sectionNumber, "window": oFsm.nextDownloadWindow, "device-id": oFsm.deviceID})
1411
1412 oFsm.nextDownloadWindow++
1413 if oFsm.nextDownloadWindow >= oFsm.noOfWindows {
1414 if sectionNumber != oFsm.omciDownloadWindowSizeLast {
1415 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse section error last window - later: repeat window once?", //TODO!!!
1416 log.Fields{"device-id": oFsm.deviceID, "actual section": sectionNumber,
1417 "expected section": oFsm.omciDownloadWindowSizeLast})
1418 oFsm.mutexUpgradeParams.Unlock()
1419 oFsm.abortOnOmciError(ctx, false)
1420 return
1421 }
1422 oFsm.delayEndSwDl = true //ensure a delay for the EndSwDl message
1423 //CRC computation for all data bytes of the file
1424 imageCRC := crc32a.Checksum(oFsm.imageBuffer[:int(oFsm.origImageLength)]) //store internal for multiple usage
1425 //revert the retrieved CRC Byte Order (seems not to deliver NetworkByteOrder)
1426 var byteSlice []byte = make([]byte, 4)
1427 binary.LittleEndian.PutUint32(byteSlice, uint32(imageCRC))
1428 oFsm.imageCRC = binary.BigEndian.Uint32(byteSlice)
1429 oFsm.mutexUpgradeParams.Unlock()
1430 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvEndSwDownload)
1431 return
1432 }
1433 if sectionNumber != oFsm.omciDownloadWindowSizeLimit {
1434 logger.Errorw(ctx, "OnuUpgradeFsm DlSectionResponse section error - later: repeat window once?", //TODO!!!
1435 log.Fields{"device-id": oFsm.deviceID, "actual-section": sectionNumber,
1436 "expected section": oFsm.omciDownloadWindowSizeLimit})
1437 oFsm.mutexUpgradeParams.Unlock()
1438 oFsm.abortOnOmciError(ctx, false)
1439 return
1440 }
1441 oFsm.nextDownloadSectionsWindow = 0
1442 oFsm.mutexUpgradeParams.Unlock()
1443 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvContinueNextWindow)
1444 return
1445 }
1446 oFsm.mutexUpgradeParams.Unlock()
1447 logger.Errorw(ctx, "OnuUpgradeFsm Omci StartSwDlResponse wrong ME instance: try again (later)?",
1448 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1449 oFsm.abortOnOmciError(ctx, false)
1450} //handleRxSwSectionResponse
1451
1452func (oFsm *OnuUpgradeFsm) handleRxEndSwDownloadResponse(ctx context.Context, msg cmn.OmciMessage) {
1453 inAbortingState := oFsm.PAdaptFsm.PFsm.Is(UpgradeStAbortingDL)
1454
1455 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeEndSoftwareDownloadResponse)
1456 if msgLayer == nil {
1457 logger.Errorw(ctx, "Omci Msg layer could not be detected for EndSwDlResponse",
1458 log.Fields{"device-id": oFsm.deviceID})
1459 if !inAbortingState {
1460 oFsm.abortOnOmciError(ctx, false)
1461 } //else using error log and wait for another response or 'aborting' state timeout
1462 return
1463 }
1464 msgObj, msgOk := msgLayer.(*omci.EndSoftwareDownloadResponse)
1465 if !msgOk {
1466 logger.Errorw(ctx, "Omci Msg layer could not be assigned for EndSwDlResponse",
1467 log.Fields{"device-id": oFsm.deviceID})
1468 if !inAbortingState {
1469 oFsm.abortOnOmciError(ctx, false)
1470 } //else using error log and wait for another response or 'aborting' state timeout
1471 return
1472 }
1473 logger.Debugw(ctx, "OnuUpgradeFsm EndSwDlResponse data", log.Fields{
1474 "device-id": oFsm.deviceID, "data-fields": msgObj})
1475 if msgObj.Result != me.Success {
1476 if msgObj.Result == me.DeviceBusy {
1477 //ONU indicates it is still processing the image - let the FSM just wait and then repeat the request
1478 logger.Debugw(ctx, "OnuUpgradeFsm EndSwDlResponse busy: waiting before sending new request", log.Fields{
1479 "device-id": oFsm.deviceID})
1480 if inAbortingState {
1481 //if the EndSwDl was requested from state AbortingDL then use channel to indicate ONU busy/repeat indication
1482 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseBusy //repeat abort request
1483 }
1484 oFsm.mutexUpgradeParams.Lock()
1485 oFsm.downloadReasonCached = oFsm.volthaDownloadReason //copy for later reconstruction
1486 oFsm.volthaDownloadReason = voltha.ImageState_DEVICE_BUSY
1487 oFsm.mutexUpgradeParams.Unlock()
1488 return
1489 }
1490 logger.Errorw(ctx, "OnuUpgradeFsm EndSwDlResponse result error - later: drive FSM to abort state ?",
1491 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1492 if inAbortingState {
1493 //if the EndSwDl was requested from state AbortingDL and response is error indication
1494 // that would be quite strange ONU behavior, no resolution from OnuAdapter, just let the FSM go on to disabled
1495 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseAbort //error indication to abort waiting on EndDownloadResponse
1496 } //else using error log and wait for another response or 'aborting' state timeout
1497 return
1498 }
1499 oFsm.mutexUpgradeParams.Lock()
1500 if msgObj.EntityInstance == oFsm.InactiveImageMeID {
1501 logger.Debugw(ctx, "Expected EndSwDlResponse received", log.Fields{"device-id": oFsm.deviceID})
1502 if oFsm.volthaDownloadReason == voltha.ImageState_DEVICE_BUSY { //was temporary on busy
1503 oFsm.volthaDownloadReason = oFsm.downloadReasonCached //recapture from mirror
1504 }
1505 if inAbortingState {
1506 oFsm.mutexUpgradeParams.Unlock()
1507 //if the EndSwDl was requested from state AbortingDL then use channel to indicate abort acceptance
1508 oFsm.chReceiveAbortEndSwDlResponse <- cEndSwDlResponseSuccess //success
1509 return
1510 }
1511 if !oFsm.useAPIVersion43 {
1512 //in the older API version the image version check was not possible
1513 // - assume new loaded image as valid-inactive immediately
1514 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_SUCCEEDED
1515 oFsm.volthaImageState = voltha.ImageState_IMAGE_INACTIVE
1516 oFsm.mutexUpgradeParams.Unlock()
1517 //use non-blocking channel (to be independent from receiver state)
1518 select {
1519 //use non-blocking channel to indicate that the download to ONU was successful
1520 case oFsm.chOnuDlReady <- true:
1521 default:
1522 }
1523 } else {
1524 oFsm.mutexUpgradeParams.Unlock()
1525 }
1526 //use asynchronous channel sending to let the FSM proceed
1527 select {
1528 case oFsm.chReceiveExpectedResponse <- true:
1529 default:
1530 }
1531 return
1532 }
1533 oFsm.mutexUpgradeParams.Unlock()
1534 logger.Errorw(ctx, "OnuUpgradeFsm StartSwDlResponse wrong ME instance: ignoring",
1535 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1536 // no state abort in case of unexpected ImageId, just keep waiting for the correct one
1537} //handleRxEndSwDownloadResponse
1538
1539func (oFsm *OnuUpgradeFsm) handleRxSwGetResponse(ctx context.Context, msg cmn.OmciMessage) {
1540 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeGetResponse)
1541 if msgLayer == nil {
1542 logger.Errorw(ctx, "Omci Msg layer could not be detected for SwImage GetResponse",
1543 log.Fields{"device-id": oFsm.deviceID})
1544 oFsm.abortOnOmciError(ctx, false)
1545 return
1546 }
1547 msgObj, msgOk := msgLayer.(*omci.GetResponse)
1548 if !msgOk {
1549 logger.Errorw(ctx, "Omci Msg layer could not be assigned for SwImage GetResponse",
1550 log.Fields{"device-id": oFsm.deviceID})
1551 oFsm.abortOnOmciError(ctx, false)
1552 return
1553 }
1554 logger.Debugw(ctx, "OnuUpgradeFsm SwImage GetResponse data", log.Fields{
1555 "device-id": oFsm.deviceID, "data-fields": msgObj})
1556 if msgObj.EntityClass == oFsm.pLastTxMeInstance.GetClassID() &&
1557 msgObj.EntityInstance == oFsm.pLastTxMeInstance.GetEntityID() {
1558 if msgObj.Result != me.Success {
1559 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse result error - later: drive FSM to abort state ?",
1560 log.Fields{"device-id": oFsm.deviceID, "Error": msgObj.Result})
1561 oFsm.abortOnOmciError(ctx, false)
1562 return
1563 }
1564 } else {
1565 logger.Warnw(ctx, "OnuUpgradeFsm SwImage unexpected Entity GetResponse data - ignore",
1566 log.Fields{"device-id": oFsm.deviceID})
1567 return
1568 }
1569
1570 meAttributes := msgObj.Attributes
1571 imageIsCommitted := meAttributes["IsCommitted"].(uint8)
1572 imageIsActive := meAttributes["IsActive"].(uint8)
1573 imageVersion := cmn.TrimStringFromMeOctet(meAttributes["Version"])
1574 logger.Debugw(ctx, "OnuUpgradeFsm - GetResponse Data for SoftwareImage",
1575 log.Fields{"device-id": oFsm.deviceID, "entityID": msgObj.EntityInstance,
1576 "version": imageVersion, "isActive": imageIsActive, "isCommitted": imageIsCommitted})
1577
1578 if oFsm.PAdaptFsm.PFsm.Current() == UpgradeStCheckImageName {
1579 //image name check after EndSwDownload, this state (and block) can only be taken if APIVersion43 is used
1580 oFsm.verifyOnuSwStatusAfterDownload(ctx, msgObj.EntityInstance, imageVersion, imageIsActive, imageIsCommitted)
1581 return
1582 }
1583
1584 //assumed only relevant state here is upgradeStCheckCommitted
1585 oFsm.mutexUpgradeParams.Lock()
1586 oFsm.conditionalCancelRequested = false //getting here any set (conditional) cancelRequest is not relevant anymore
1587 if msgObj.EntityInstance == oFsm.InactiveImageMeID && imageIsActive == cmn.SwIsActive {
1588 //a check on the delivered image version is not done, the ONU delivered version might be different from what might have been
1589 // indicated in the download image version string (version must be part of the image content itself)
1590 // so checking that might be quite unreliable
1591 //but with new API this was changed, assumption is that omci image version is known at download request and exactly that is used
1592 // in all the API references, so it can and should be checked here now
1593 if oFsm.useAPIVersion43 {
1594 if imageVersion != oFsm.imageVersion {
1595 //new active version indicated on OMCI from ONU is not the expected version
1596 logger.Errorw(ctx, "OnuUpgradeFsm image-version not matching the requested upgrade",
1597 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance,
1598 "onu-version": imageVersion, "expected-version": oFsm.imageVersion})
1599 // TODO!!!: error treatment?
1600 //TODO!!!: possibly send event information for aborted upgrade (aborted by wrong version)?
1601 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE //something like 'UNEXPECTED_VERSION' would be better - proto def
1602 oFsm.mutexUpgradeParams.Unlock()
1603 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1604 return
1605 }
1606 logger.Debugw(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU",
1607 log.Fields{"device-id": oFsm.deviceID})
1608 }
1609 if imageIsCommitted == cmn.SwIsCommitted {
1610 oFsm.upgradePhase = cUpgradeCommitted
1611 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMITTED
1612 //store the new commit flag to onuSwImageIndications (to keep them in sync)
1613 oFsm.pDevEntry.ModifySwImageActiveCommit(ctx, imageIsCommitted)
1614 logger.Infow(ctx, "requested SW image committed, releasing OnuUpgrade", log.Fields{"device-id": oFsm.deviceID})
1615 //deviceProcStatusUpdate not used anymore,
1616 // replaced by transferring the last (more) upgrade state information within removeOnuUpgradeFsm
1617 oFsm.mutexUpgradeParams.Unlock()
1618 //releasing the upgrade FSM on success
1619 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1620 return
1621 }
1622 //if not committed, abort upgrade as failed. There is no implementation here that would trigger this test again
1623 }
1624 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1625 oFsm.mutexUpgradeParams.Unlock()
1626 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse indications not matching requested upgrade",
1627 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": msgObj.EntityInstance})
1628 // TODO!!!: error treatment?
1629 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
1630 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1631} //handleRxSwGetResponse
1632
1633func (oFsm *OnuUpgradeFsm) verifyOnuSwStatusAfterDownload(ctx context.Context, aInstanceID uint16,
1634 aImageVersion string, aImageIsActive uint8, aImageIsCommitted uint8) {
1635 oFsm.mutexUpgradeParams.Lock()
1636 if aInstanceID == oFsm.InactiveImageMeID && aImageIsActive == cmn.SwIsInactive &&
1637 aImageIsCommitted == cmn.SwIsUncommitted {
1638 if aImageVersion != oFsm.imageVersion {
1639 //new stored inactive version indicated on OMCI from ONU is not the expected version
1640 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse version indication not matching requested upgrade",
1641 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": aInstanceID,
1642 "onu-version": aImageVersion, "expected-version": oFsm.imageVersion})
1643 //download state is set when entering the reset state
1644 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE //something like 'UNEXPECTED_VERSION' would be better - proto def
1645 oFsm.mutexUpgradeParams.Unlock()
1646 //stop the running ONU download timer
1647 //use non-blocking channel (to be independent from receiver state)
1648 select {
1649 //use channel to indicate that the download response waiting shall be aborted for this device (channel)
1650 case oFsm.chOnuDlReady <- false:
1651 default:
1652 }
1653 // TODO!!!: error treatment?
1654 //TODO!!!: possibly send event information for aborted upgrade (aborted by wrong version)?
1655 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1656 return
1657 }
1658 //with APIVersion43 this is the point to consider the newly loaded image as valid (and inactive)
1659 oFsm.upgradePhase = cUpgradeDownloaded
1660 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_SUCCEEDED
1661 oFsm.volthaImageState = voltha.ImageState_IMAGE_INACTIVE
1662 //store the new inactive version to onuSwImageIndications (to keep them in sync)
1663 oFsm.pDevEntry.ModifySwImageInactiveVersion(ctx, oFsm.imageVersion)
1664 //proceed within upgrade FSM
1665 if oFsm.activateImage {
1666 //immediate activation requested
1667 oFsm.mutexUpgradeParams.Unlock()
1668 logger.Debugw(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU, continue with activation",
1669 log.Fields{"device-id": oFsm.deviceID})
1670 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvRequestActivate)
1671 } else {
1672 //have to wait on explicit activation request
1673 oFsm.mutexUpgradeParams.Unlock()
1674 logger.Infow(ctx, "OnuUpgradeFsm - expected ONU image version indicated by the ONU, wait for activate request",
1675 log.Fields{"device-id": oFsm.deviceID})
1676 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvWaitForActivate)
1677 }
1678 //use non-blocking channel (to be independent from receiver state)
1679 select {
1680 //use non-blocking channel to indicate that the download to ONU was successful
1681 case oFsm.chOnuDlReady <- true:
1682 default:
1683 }
1684 return
1685 }
1686 //not the expected image/image state
1687 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1688 oFsm.mutexUpgradeParams.Unlock()
1689 logger.Errorw(ctx, "OnuUpgradeFsm SwImage GetResponse indications not matching requested upgrade",
1690 log.Fields{"device-id": oFsm.deviceID, "ResponseMeId": aInstanceID})
1691 // TODO!!!: error treatment?
1692 //TODO!!!: possibly send event information for aborted upgrade (aborted by ONU state indication)?
1693 _ = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
1694} //verifyOnuSwStatusAfterDownload
1695
1696//abortOnOmciError aborts the upgrade processing with evAbort
1697// asynchronous/synchronous based on parameter aAsync
1698func (oFsm *OnuUpgradeFsm) abortOnOmciError(ctx context.Context, aAsync bool) {
mpagenko38662d02021-08-11 09:45:19 +00001699 oFsm.mutexUpgradeParams.Lock()
1700 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
mpagenko38662d02021-08-11 09:45:19 +00001701 oFsm.volthaDownloadReason = voltha.ImageState_OMCI_TRANSFER_ERROR
mpagenko38662d02021-08-11 09:45:19 +00001702 oFsm.mutexUpgradeParams.Unlock()
1703 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001704 if oFsm.PAdaptFsm != nil {
mpagenko38662d02021-08-11 09:45:19 +00001705 var err error
1706 if aAsync { //asynchronous call requested to ensure state transition
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001707 go func(a_pAFsm *cmn.AdapterFsm) {
1708 if a_pAFsm.PFsm != nil {
1709 err = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko38662d02021-08-11 09:45:19 +00001710 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001711 }(oFsm.PAdaptFsm)
mpagenko38662d02021-08-11 09:45:19 +00001712 } else {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001713 if oFsm.PAdaptFsm.PFsm != nil {
1714 err = oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko38662d02021-08-11 09:45:19 +00001715 }
1716 }
1717 if err != nil {
1718 logger.Warnw(ctx, "onu upgrade fsm could not abort on omci error", log.Fields{
1719 "device-id": oFsm.deviceID, "error": err})
1720 }
1721 }
1722}
1723
mpagenkoc26d4c02021-05-06 14:27:57 +00001724//waitOnDownloadToAdapterReady state can only be reached with useAPIVersion43 (usage of pFileManager)
mpagenko38662d02021-08-11 09:45:19 +00001725// precondition: mutexIsAwaitingAdapterDlResponse is lockek on call
1726func (oFsm *OnuUpgradeFsm) waitOnDownloadToAdapterReady(ctx context.Context, aSyncChannel chan<- struct{},
1727 aWaitChannel chan bool) {
mpagenkoc26d4c02021-05-06 14:27:57 +00001728 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
mpagenko38662d02021-08-11 09:45:19 +00001729 downloadToAdapterTimeout := oFsm.pFileManager.GetDownloadTimeout(ctx)
mpagenkoc26d4c02021-05-06 14:27:57 +00001730 oFsm.isWaitingForAdapterDlResponse = true
1731 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenko38662d02021-08-11 09:45:19 +00001732 aSyncChannel <- struct{}{}
mpagenko80622a52021-02-09 16:53:23 +00001733 select {
1734 // maybe be also some outside cancel (but no context modeled for the moment ...)
1735 // case <-ctx.Done():
mpagenkoc26d4c02021-05-06 14:27:57 +00001736 // logger.Infow("OnuUpgradeFsm-waitOnDownloadToAdapterReady canceled", log.Fields{"for device-id": oFsm.deviceID})
1737 case <-time.After(downloadToAdapterTimeout): //10s should be enough for downloading some image to the adapter
1738 logger.Warnw(ctx, "OnuUpgradeFsm Waiting-adapter-download timeout", log.Fields{
1739 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier, "timeout": downloadToAdapterTimeout})
1740 oFsm.pFileManager.RemoveReadyRequest(ctx, oFsm.imageIdentifier, aWaitChannel)
mpagenko39b703e2021-08-25 13:38:40 +00001741 //running into timeout here may still have the download to adapter active -> abort
1742 oFsm.pFileManager.CancelDownload(ctx, oFsm.imageIdentifier)
mpagenkoc26d4c02021-05-06 14:27:57 +00001743 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1744 oFsm.isWaitingForAdapterDlResponse = false
1745 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenko39b703e2021-08-25 13:38:40 +00001746 oFsm.mutexUpgradeParams.Lock()
mpagenko45586762021-10-01 08:30:22 +00001747 oFsm.conditionalCancelRequested = false //any conditional cancelRequest is superseded by this abortion
mpagenko39b703e2021-08-25 13:38:40 +00001748 oFsm.volthaDownloadReason = voltha.ImageState_UNKNOWN_ERROR //something like 'DOWNLOAD_TO_ADAPTER_TIMEOUT' would be better (proto)
mpagenko39b703e2021-08-25 13:38:40 +00001749 oFsm.mutexUpgradeParams.Unlock()
1750 //TODO!!!: possibly send event information for aborted upgrade (aborted by omci processing)??
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001751 if oFsm.PAdaptFsm != nil && oFsm.PAdaptFsm.PFsm != nil {
1752 err := oFsm.PAdaptFsm.PFsm.Event(UpgradeEvAbort)
mpagenko39b703e2021-08-25 13:38:40 +00001753 if err != nil {
1754 logger.Warnw(ctx, "onu upgrade fsm could not abort on omci error", log.Fields{
1755 "device-id": oFsm.deviceID, "error": err})
1756 }
1757 }
mpagenkoc26d4c02021-05-06 14:27:57 +00001758 return
1759
1760 case success := <-aWaitChannel:
1761 if success {
1762 logger.Debugw(ctx, "OnuUpgradeFsm image-downloaded received", log.Fields{"device-id": oFsm.deviceID})
1763 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1764 oFsm.isWaitingForAdapterDlResponse = false
1765 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
1766 //let the upgrade process proceed
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001767 pUpgradeFsm := oFsm.PAdaptFsm
mpagenkoc26d4c02021-05-06 14:27:57 +00001768 if pUpgradeFsm != nil {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001769 _ = pUpgradeFsm.PFsm.Event(UpgradeEvPrepareSwDownload)
mpagenkoc26d4c02021-05-06 14:27:57 +00001770 } else {
1771 logger.Errorw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
1772 }
1773 return
1774 }
mpagenko39b703e2021-08-25 13:38:40 +00001775 // waiting was aborted (assumed here to be caused by
1776 // error detection or cancel at download after upgrade FSM reset/abort with according image states set there)
mpagenkoc26d4c02021-05-06 14:27:57 +00001777 logger.Debugw(ctx, "OnuUpgradeFsm Waiting-adapter-download aborted", log.Fields{"device-id": oFsm.deviceID})
1778 oFsm.pFileManager.RemoveReadyRequest(ctx, oFsm.imageIdentifier, aWaitChannel)
1779 oFsm.mutexIsAwaitingAdapterDlResponse.Lock()
1780 oFsm.isWaitingForAdapterDlResponse = false
1781 oFsm.mutexIsAwaitingAdapterDlResponse.Unlock()
mpagenkoc26d4c02021-05-06 14:27:57 +00001782 return
mpagenko80622a52021-02-09 16:53:23 +00001783 }
1784}
mpagenkoc26d4c02021-05-06 14:27:57 +00001785
1786//waitOnDownloadToOnuReady state can only be reached with useAPIVersion43 (usage of pFileManager)
1787func (oFsm *OnuUpgradeFsm) waitOnDownloadToOnuReady(ctx context.Context, aWaitChannel chan bool) {
1788 downloadToOnuTimeout := time.Duration(1+(oFsm.imageLength/0x400000)) * oFsm.downloadToOnuTimeout4MB
1789 logger.Debugw(ctx, "OnuUpgradeFsm start download-to-ONU timer", log.Fields{"device-id": oFsm.deviceID,
1790 "duration": downloadToOnuTimeout})
mpagenkoc26d4c02021-05-06 14:27:57 +00001791 select {
1792 // maybe be also some outside cancel (but no context modeled for the moment ...)
1793 // case <-ctx.Done():
1794 // logger.Infow("OnuUpgradeFsm-waitOnDownloadToOnuReady canceled", log.Fields{"for device-id": oFsm.deviceID})
1795 case <-time.After(downloadToOnuTimeout): //using an image-size depending timout (in minutes)
1796 logger.Warnw(ctx, "OnuUpgradeFsm Waiting-ONU-download timeout", log.Fields{
1797 "for device-id": oFsm.deviceID, "image-id": oFsm.imageIdentifier, "timeout": downloadToOnuTimeout})
mpagenkoc26d4c02021-05-06 14:27:57 +00001798 //the upgrade process has to be aborted
mpagenko45586762021-10-01 08:30:22 +00001799 oFsm.abortOnOmciError(ctx, false)
mpagenkoc26d4c02021-05-06 14:27:57 +00001800 return
1801
1802 case success := <-aWaitChannel:
1803 if success {
1804 logger.Debugw(ctx, "OnuUpgradeFsm image-downloaded on ONU received", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001805 //all fine, let the FSM proceed like defined from the sender of this event
1806 return
1807 }
1808 // waiting was aborted (assumed here to be caused by
mpagenko39b703e2021-08-25 13:38:40 +00001809 // error detection or cancel at download after upgrade FSM reset/abort with according image states set there)
mpagenkoc26d4c02021-05-06 14:27:57 +00001810 logger.Debugw(ctx, "OnuUpgradeFsm Waiting-ONU-download aborted", log.Fields{"device-id": oFsm.deviceID})
mpagenkoc26d4c02021-05-06 14:27:57 +00001811 return
1812 }
1813}
mpagenko45586762021-10-01 08:30:22 +00001814
1815//stateUpdateOnReset writes the download and/or image state on entering the reset state according to FSM internal indications
1816func (oFsm *OnuUpgradeFsm) stateUpdateOnReset(ctx context.Context) {
1817 oFsm.mutexUpgradeParams.Lock()
1818 defer oFsm.mutexUpgradeParams.Unlock()
1819 if !oFsm.conditionalCancelRequested {
1820 switch oFsm.upgradePhase {
1821 case cUpgradeUndefined, cUpgradeDownloading: //coming from downloading
1822 //make sure the download state is only changed in case the device has still been downloading
1823 if oFsm.volthaDownloadReason == voltha.ImageState_CANCELLED_ON_REQUEST {
1824 // indication for termination on request
1825 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_CANCELLED
1826 } else if oFsm.volthaDownloadReason != voltha.ImageState_NO_ERROR {
1827 // indication for termination on failure
1828 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_FAILED
1829 }
1830 //reset the image state from Downloading in this case
1831 oFsm.volthaImageState = voltha.ImageState_IMAGE_UNKNOWN //something like 'IMAGE_DOWNLOAD_ABORTED' would be better (proto)
1832 //in all other upgrade phases the last set download state remains valid
1833 case cUpgradeActivating:
1834 //reset the image state from Activating in this case
1835 oFsm.volthaImageState = voltha.ImageState_IMAGE_ACTIVATION_ABORTED
1836 case cUpgradeCommitting: // indication for request to abort waiting for response
1837 //reset the image state from Activating in this case
1838 oFsm.volthaImageState = voltha.ImageState_IMAGE_COMMIT_ABORTED
1839 //default: in all other upgrade phases keep the last set imageState
1840 } //switch
1841 } else {
1842 //when reaching reset state with conditional cancel that can only result from ONU related problems
1843 // (mostly ONU down indication) - derived from resetFsms call
1844 // and it can only be related to the downloading-to-ONU phase (no need to check that additionally)
1845 oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_FAILED
1846 oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
1847 //reset the image state from Downloading in this case
1848 oFsm.volthaImageState = voltha.ImageState_IMAGE_UNKNOWN //something like 'IMAGE_DOWNLOAD_ABORTED' would be better (proto)
1849 }
1850}