blob: 1fe0230240ba3af5f0727a92120a224d6a087ece [file] [log] [blame]
Holger Hildebrandtccd390c2020-05-29 13:49:04 +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
17//Package adaptercoreonu provides the utility for onu devices, flows and statistics
18package adaptercoreonu
19
20import (
21 "context"
Andrea Campanella6515c582020-10-05 11:25:00 +020022 "fmt"
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000023 "time"
24
25 "github.com/looplab/fsm"
26
27 "github.com/opencord/omci-lib-go"
28 me "github.com/opencord/omci-lib-go/generated"
dbainbri4d3a0dc2020-12-02 00:33:42 +000029 "github.com/opencord/voltha-lib-go/v4/pkg/log"
30 //ic "github.com/opencord/voltha-protos/v4/go/inter_container"
31 //"github.com/opencord/voltha-protos/v4/go/openflow_13"
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000032)
33
Himani Chawla6d2ae152020-09-02 13:11:20 +053034//lockStateFsm defines the structure for the state machine to lock/unlock the ONU UNI ports via OMCI
35type lockStateFsm struct {
Holger Hildebrandt8165eda2020-09-24 09:39:24 +000036 pDeviceHandler *deviceHandler
mpagenko01e726e2020-10-23 09:45:29 +000037 deviceID string
Himani Chawla6d2ae152020-09-02 13:11:20 +053038 pOmciCC *omciCC
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000039 adminState bool
40 requestEvent OnuDeviceEvent
41 omciLockResponseReceived chan bool //seperate channel needed for checking UNI port OMCi message responses
42 pAdaptFsm *AdapterFsm
mpagenko01e726e2020-10-23 09:45:29 +000043 pLastTxMeInstance *me.ManagedEntity
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000044}
45
mpagenko1cc3cb42020-07-27 15:24:38 +000046const (
47 // events of lock/unlock UNI port FSM
48 uniEvStart = "uniEvStart"
49 uniEvStartAdmin = "uniEvStartAdmin"
50 uniEvRxUnisResp = "uniEvRxUnisResp"
51 uniEvRxOnugResp = "uniEvRxOnugResp"
52 uniEvTimeoutSimple = "uniEvTimeoutSimple"
53 uniEvTimeoutUnis = "uniEvTimeoutUnis"
54 uniEvReset = "uniEvReset"
55 uniEvRestart = "uniEvRestart"
56)
57const (
58 // states of lock/unlock UNI port FSM
59 uniStDisabled = "uniStDisabled"
60 uniStStarting = "uniStStarting"
61 uniStSettingUnis = "uniStSettingUnis"
62 uniStSettingOnuG = "uniStSettingOnuG"
63 uniStAdminDone = "uniStAdminDone"
64 uniStResetting = "uniStResetting"
65)
Holger Hildebrandt10d98192021-01-27 15:29:31 +000066const cUniFsmIdleState = uniStDisabled
mpagenko1cc3cb42020-07-27 15:24:38 +000067
Himani Chawla6d2ae152020-09-02 13:11:20 +053068//newLockStateFsm is the 'constructor' for the state machine to lock/unlock the ONU UNI ports via OMCI
dbainbri4d3a0dc2020-12-02 00:33:42 +000069func newLockStateFsm(ctx context.Context, apDevOmciCC *omciCC, aAdminState bool, aRequestEvent OnuDeviceEvent,
Holger Hildebrandt8165eda2020-09-24 09:39:24 +000070 aName string, apDeviceHandler *deviceHandler, aCommChannel chan Message) *lockStateFsm {
Himani Chawla6d2ae152020-09-02 13:11:20 +053071 instFsm := &lockStateFsm{
Holger Hildebrandt8165eda2020-09-24 09:39:24 +000072 pDeviceHandler: apDeviceHandler,
mpagenko01e726e2020-10-23 09:45:29 +000073 deviceID: apDeviceHandler.deviceID,
Holger Hildebrandt8165eda2020-09-24 09:39:24 +000074 pOmciCC: apDevOmciCC,
75 adminState: aAdminState,
76 requestEvent: aRequestEvent,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000077 }
mpagenko01e726e2020-10-23 09:45:29 +000078 instFsm.pAdaptFsm = NewAdapterFsm(aName, instFsm.deviceID, aCommChannel)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000079 if instFsm.pAdaptFsm == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +000080 logger.Errorw(ctx, "LockStateFsm's AdapterFsm could not be instantiated!!", log.Fields{
mpagenko01e726e2020-10-23 09:45:29 +000081 "device-id": instFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000082 return nil
83 }
Himani Chawla4d908332020-08-31 12:30:20 +053084 if aAdminState { //port locking requested
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000085 instFsm.pAdaptFsm.pFsm = fsm.NewFSM(
mpagenko1cc3cb42020-07-27 15:24:38 +000086 uniStDisabled,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000087 fsm.Events{
88
mpagenko1cc3cb42020-07-27 15:24:38 +000089 {Name: uniEvStart, Src: []string{uniStDisabled}, Dst: uniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000090
mpagenko1cc3cb42020-07-27 15:24:38 +000091 {Name: uniEvStartAdmin, Src: []string{uniStStarting}, Dst: uniStSettingUnis},
92 // the settingUnis state is used for multi ME config for all UNI related ports
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000093 // maybe such could be reflected in the state machine as well (port number parametrized)
94 // but that looks not straightforward here - so we keep it simple here for the beginning(?)
mpagenko1cc3cb42020-07-27 15:24:38 +000095 {Name: uniEvRxUnisResp, Src: []string{uniStSettingUnis}, Dst: uniStSettingOnuG},
96 {Name: uniEvRxOnugResp, Src: []string{uniStSettingOnuG}, Dst: uniStAdminDone},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000097
mpagenko1cc3cb42020-07-27 15:24:38 +000098 {Name: uniEvTimeoutSimple, Src: []string{uniStSettingOnuG}, Dst: uniStStarting},
99 {Name: uniEvTimeoutUnis, Src: []string{uniStSettingUnis}, Dst: uniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000100
mpagenko1cc3cb42020-07-27 15:24:38 +0000101 {Name: uniEvReset, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis,
102 uniStAdminDone}, Dst: uniStResetting},
103 // exceptional treatment for all states except uniStResetting
104 {Name: uniEvRestart, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis,
105 uniStAdminDone, uniStResetting}, Dst: uniStDisabled},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000106 },
107
108 fsm.Callbacks{
dbainbri4d3a0dc2020-12-02 00:33:42 +0000109 "enter_state": func(e *fsm.Event) { instFsm.pAdaptFsm.logFsmStateChange(ctx, e) },
110 ("enter_" + uniStStarting): func(e *fsm.Event) { instFsm.enterAdminStartingState(ctx, e) },
111 ("enter_" + uniStSettingOnuG): func(e *fsm.Event) { instFsm.enterSettingOnuGState(ctx, e) },
112 ("enter_" + uniStSettingUnis): func(e *fsm.Event) { instFsm.enterSettingUnisState(ctx, e) },
113 ("enter_" + uniStAdminDone): func(e *fsm.Event) { instFsm.enterAdminDoneState(ctx, e) },
114 ("enter_" + uniStResetting): func(e *fsm.Event) { instFsm.enterResettingState(ctx, e) },
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000115 },
116 )
117 } else { //port unlocking requested
118 instFsm.pAdaptFsm.pFsm = fsm.NewFSM(
mpagenko1cc3cb42020-07-27 15:24:38 +0000119 uniStDisabled,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000120 fsm.Events{
121
mpagenko1cc3cb42020-07-27 15:24:38 +0000122 {Name: uniEvStart, Src: []string{uniStDisabled}, Dst: uniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000123
mpagenko1cc3cb42020-07-27 15:24:38 +0000124 {Name: uniEvStartAdmin, Src: []string{uniStStarting}, Dst: uniStSettingOnuG},
125 {Name: uniEvRxOnugResp, Src: []string{uniStSettingOnuG}, Dst: uniStSettingUnis},
126 // the settingUnis state is used for multi ME config for all UNI related ports
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000127 // maybe such could be reflected in the state machine as well (port number parametrized)
128 // but that looks not straightforward here - so we keep it simple here for the beginning(?)
mpagenko1cc3cb42020-07-27 15:24:38 +0000129 {Name: uniEvRxUnisResp, Src: []string{uniStSettingUnis}, Dst: uniStAdminDone},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000130
mpagenko1cc3cb42020-07-27 15:24:38 +0000131 {Name: uniEvTimeoutSimple, Src: []string{uniStSettingOnuG}, Dst: uniStStarting},
132 {Name: uniEvTimeoutUnis, Src: []string{uniStSettingUnis}, Dst: uniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000133
mpagenko1cc3cb42020-07-27 15:24:38 +0000134 {Name: uniEvReset, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis,
135 uniStAdminDone}, Dst: uniStResetting},
136 // exceptional treatment for all states except uniStResetting
137 {Name: uniEvRestart, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis,
138 uniStAdminDone, uniStResetting}, Dst: uniStDisabled},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000139 },
140
141 fsm.Callbacks{
dbainbri4d3a0dc2020-12-02 00:33:42 +0000142 "enter_state": func(e *fsm.Event) { instFsm.pAdaptFsm.logFsmStateChange(ctx, e) },
143 ("enter_" + uniStStarting): func(e *fsm.Event) { instFsm.enterAdminStartingState(ctx, e) },
144 ("enter_" + uniStSettingOnuG): func(e *fsm.Event) { instFsm.enterSettingOnuGState(ctx, e) },
145 ("enter_" + uniStSettingUnis): func(e *fsm.Event) { instFsm.enterSettingUnisState(ctx, e) },
146 ("enter_" + uniStAdminDone): func(e *fsm.Event) { instFsm.enterAdminDoneState(ctx, e) },
147 ("enter_" + uniStResetting): func(e *fsm.Event) { instFsm.enterResettingState(ctx, e) },
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000148 },
149 )
150 }
151 if instFsm.pAdaptFsm.pFsm == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000152 logger.Errorw(ctx, "LockStateFsm's Base FSM could not be instantiated!!", log.Fields{
mpagenko01e726e2020-10-23 09:45:29 +0000153 "device-id": instFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000154 return nil
155 }
156
dbainbri4d3a0dc2020-12-02 00:33:42 +0000157 logger.Debugw(ctx, "LockStateFsm created", log.Fields{"device-id": instFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000158 return instFsm
159}
160
Himani Chawla6d2ae152020-09-02 13:11:20 +0530161//setSuccessEvent modifies the requested event notified on success
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000162//assumption is that this is only called in the disabled (idle) state of the FSM, hence no sem protection required
Himani Chawla6d2ae152020-09-02 13:11:20 +0530163func (oFsm *lockStateFsm) setSuccessEvent(aEvent OnuDeviceEvent) {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000164 oFsm.requestEvent = aEvent
165}
166
dbainbri4d3a0dc2020-12-02 00:33:42 +0000167func (oFsm *lockStateFsm) enterAdminStartingState(ctx context.Context, e *fsm.Event) {
168 logger.Debugw(ctx, "LockStateFSM start", log.Fields{"in state": e.FSM.Current(),
mpagenko01e726e2020-10-23 09:45:29 +0000169 "device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000170 // in case the used channel is not yet defined (can be re-used after restarts)
171 if oFsm.omciLockResponseReceived == nil {
172 oFsm.omciLockResponseReceived = make(chan bool)
dbainbri4d3a0dc2020-12-02 00:33:42 +0000173 logger.Debug(ctx, "LockStateFSM - OMCI UniLock RxChannel defined")
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000174 } else {
175 // as we may 're-use' this instance of FSM and the connected channel
mpagenkob06684f2021-03-23 14:39:04 +0000176 // make sure there is no 'lingering' request in the already existing channels:
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000177 // (simple loop sufficient as we are the only receiver)
178 for len(oFsm.omciLockResponseReceived) > 0 {
179 <-oFsm.omciLockResponseReceived
180 }
mpagenkob06684f2021-03-23 14:39:04 +0000181 for len(oFsm.pAdaptFsm.commChan) > 0 {
182 <-oFsm.pAdaptFsm.commChan
183 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000184 }
185 // start go routine for processing of LockState messages
dbainbri4d3a0dc2020-12-02 00:33:42 +0000186 go oFsm.processOmciLockMessages(ctx)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000187
188 //let the state machine run forward from here directly
189 pLockStateAFsm := oFsm.pAdaptFsm
190 if pLockStateAFsm != nil {
191 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
192 go func(a_pAFsm *AdapterFsm) {
193 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
Himani Chawla4d908332020-08-31 12:30:20 +0530194 _ = a_pAFsm.pFsm.Event(uniEvStartAdmin)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000195 }
196 }(pLockStateAFsm)
197 }
198}
199
dbainbri4d3a0dc2020-12-02 00:33:42 +0000200func (oFsm *lockStateFsm) enterSettingOnuGState(ctx context.Context, e *fsm.Event) {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000201 var omciAdminState uint8 = 1 //default locked
Himani Chawla4d908332020-08-31 12:30:20 +0530202 if !oFsm.adminState {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000203 omciAdminState = 0
204 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000205 logger.Debugw(ctx, "LockStateFSM Tx Set::ONU-G:admin", log.Fields{
mpagenko01e726e2020-10-23 09:45:29 +0000206 "omciAdmin": omciAdminState, "in state": e.FSM.Current(), "device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000207 requestedAttributes := me.AttributeValueMap{"AdministrativeState": omciAdminState}
ozgecanetsiab36ed572021-04-01 10:38:48 +0300208 meInstance, err := oFsm.pOmciCC.sendSetOnuGLS(log.WithSpanFromContext(context.TODO(), ctx), oFsm.pDeviceHandler.pOpenOnuAc.omciTimeout, true,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000209 requestedAttributes, oFsm.pAdaptFsm.commChan)
ozgecanetsiab36ed572021-04-01 10:38:48 +0300210 if err != nil {
211 logger.Errorw(ctx, "OnuGLS set failed, aborting LockStateFSM", log.Fields{"device-id": oFsm.deviceID})
212 pLockStateAFsm := oFsm.pAdaptFsm
213 if pLockStateAFsm != nil {
214 go func(a_pAFsm *AdapterFsm) {
215 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
216 _ = a_pAFsm.pFsm.Event(uniEvReset)
217 }
218 }(pLockStateAFsm)
219 }
220 return
221 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000222 //accept also nil as (error) return value for writing to LastTx
223 // - this avoids misinterpretation of new received OMCI messages
mpagenko01e726e2020-10-23 09:45:29 +0000224 oFsm.pLastTxMeInstance = meInstance
mpagenkob06684f2021-03-23 14:39:04 +0000225 if oFsm.pLastTxMeInstance == nil {
226 logger.Errorw(ctx, "could not send OMCI message from LockStateFsm", log.Fields{
227 "device-id": oFsm.deviceID})
228 //some more sophisticated approach is possible, e.g. repeating once, by now let's reset the state machine in order to release all resources now
229 pLockStateAFsm := oFsm.pAdaptFsm
230 if pLockStateAFsm != nil {
231 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
232 go func(a_pAFsm *AdapterFsm) {
233 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
234 _ = a_pAFsm.pFsm.Event(uniEvReset)
235 }
236 }(pLockStateAFsm)
237 }
238 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000239}
240
dbainbri4d3a0dc2020-12-02 00:33:42 +0000241func (oFsm *lockStateFsm) enterSettingUnisState(ctx context.Context, e *fsm.Event) {
mpagenko8b5fdd22020-12-17 17:58:32 +0000242 logger.Debugw(ctx, "LockStateFSM - starting UniTP adminState loop", log.Fields{
mpagenko01e726e2020-10-23 09:45:29 +0000243 "in state": e.FSM.Current(), "device-id": oFsm.deviceID, "LockState": oFsm.adminState})
dbainbri4d3a0dc2020-12-02 00:33:42 +0000244 go oFsm.performUniPortAdminSet(ctx)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000245}
246
dbainbri4d3a0dc2020-12-02 00:33:42 +0000247func (oFsm *lockStateFsm) enterAdminDoneState(ctx context.Context, e *fsm.Event) {
248 logger.Debugw(ctx, "LockStateFSM", log.Fields{"send notification to core in State": e.FSM.Current(), "device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000249 //use DeviceHandler event notification directly, no need/support to update DeviceEntryState for lock/unlock
dbainbri4d3a0dc2020-12-02 00:33:42 +0000250 oFsm.pDeviceHandler.deviceProcStatusUpdate(ctx, oFsm.requestEvent)
Holger Hildebrandt8165eda2020-09-24 09:39:24 +0000251
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000252 //let's reset the state machine in order to release all resources now
253 pLockStateAFsm := oFsm.pAdaptFsm
254 if pLockStateAFsm != nil {
255 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
256 go func(a_pAFsm *AdapterFsm) {
257 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
Himani Chawla4d908332020-08-31 12:30:20 +0530258 _ = a_pAFsm.pFsm.Event(uniEvReset)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000259 }
260 }(pLockStateAFsm)
261 }
262}
263
dbainbri4d3a0dc2020-12-02 00:33:42 +0000264func (oFsm *lockStateFsm) enterResettingState(ctx context.Context, e *fsm.Event) {
265 logger.Debugw(ctx, "LockStateFSM resetting", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000266 pLockStateAFsm := oFsm.pAdaptFsm
267 if pLockStateAFsm != nil {
268 // abort running message processing
269 fsmAbortMsg := Message{
270 Type: TestMsg,
271 Data: TestMessage{
272 TestMessageVal: AbortMessageProcessing,
273 },
274 }
275 pLockStateAFsm.commChan <- fsmAbortMsg
276
277 //try to restart the FSM to 'disabled'
278 // see DownloadedState: decouple event transfer
279 go func(a_pAFsm *AdapterFsm) {
280 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
Himani Chawla4d908332020-08-31 12:30:20 +0530281 _ = a_pAFsm.pFsm.Event(uniEvRestart)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000282 }
283 }(pLockStateAFsm)
mpagenko01e726e2020-10-23 09:45:29 +0000284 oFsm.pLastTxMeInstance = nil
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000285 }
286}
287
dbainbri4d3a0dc2020-12-02 00:33:42 +0000288func (oFsm *lockStateFsm) processOmciLockMessages(ctx context.Context) {
289 logger.Debugw(ctx, "Start LockStateFsm Msg processing", log.Fields{"for device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000290loop:
291 for {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000292 // case <-ctx.Done():
dbainbri4d3a0dc2020-12-02 00:33:42 +0000293 // logger.Info(ctx,"MibSync Msg", log.Fields{"Message handling canceled via context for device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000294 // break loop
Himani Chawla4d908332020-08-31 12:30:20 +0530295 message, ok := <-oFsm.pAdaptFsm.commChan
296 if !ok {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000297 logger.Info(ctx, "LockStateFsm Rx Msg - could not read from channel", log.Fields{"device-id": oFsm.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530298 // but then we have to ensure a restart of the FSM as well - as exceptional procedure
299 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvRestart)
300 break loop
301 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000302 logger.Debugw(ctx, "LockStateFsm Rx Msg", log.Fields{"device-id": oFsm.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530303
304 switch message.Type {
305 case TestMsg:
306 msg, _ := message.Data.(TestMessage)
307 if msg.TestMessageVal == AbortMessageProcessing {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000308 logger.Debugw(ctx, "LockStateFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000309 break loop
310 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000311 logger.Warnw(ctx, "LockStateFsm unknown TestMessage", log.Fields{"device-id": oFsm.deviceID, "MessageVal": msg.TestMessageVal})
Himani Chawla4d908332020-08-31 12:30:20 +0530312 case OMCI:
313 msg, _ := message.Data.(OmciMessage)
dbainbri4d3a0dc2020-12-02 00:33:42 +0000314 oFsm.handleOmciLockStateMessage(ctx, msg)
Himani Chawla4d908332020-08-31 12:30:20 +0530315 default:
dbainbri4d3a0dc2020-12-02 00:33:42 +0000316 logger.Warn(ctx, "LockStateFsm Rx unknown message", log.Fields{"device-id": oFsm.deviceID,
Himani Chawla4d908332020-08-31 12:30:20 +0530317 "message.Type": message.Type})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000318 }
319 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000320 logger.Debugw(ctx, "End LockStateFsm Msg processing", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000321}
322
dbainbri4d3a0dc2020-12-02 00:33:42 +0000323func (oFsm *lockStateFsm) handleOmciLockStateMessage(ctx context.Context, msg OmciMessage) {
324 logger.Debugw(ctx, "Rx OMCI LockStateFsm Msg", log.Fields{"device-id": oFsm.deviceID,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000325 "msgType": msg.OmciMsg.MessageType})
326
327 if msg.OmciMsg.MessageType == omci.SetResponseType {
328 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeSetResponse)
329 if msgLayer == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000330 logger.Errorw(ctx, "LockStateFsm - Omci Msg layer could not be detected for SetResponse",
mpagenko01e726e2020-10-23 09:45:29 +0000331 log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000332 return
333 }
334 msgObj, msgOk := msgLayer.(*omci.SetResponse)
335 if !msgOk {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000336 logger.Errorw(ctx, "LockStateFsm - Omci Msg layer could not be assigned for SetResponse",
mpagenko01e726e2020-10-23 09:45:29 +0000337 log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000338 return
339 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000340 logger.Debugw(ctx, "LockStateFsm SetResponse Data", log.Fields{"device-id": oFsm.deviceID, "data-fields": msgObj})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000341 if msgObj.Result != me.Success {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000342 logger.Errorw(ctx, "LockStateFsm - Omci SetResponse Error - later: drive FSM to abort state ?", log.Fields{"Error": msgObj.Result})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000343 // possibly force FSM into abort or ignore some errors for some messages? store error for mgmt display?
344 return
345 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000346
mpagenkob06684f2021-03-23 14:39:04 +0000347 //should never appear, left here for robustness
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700348 if oFsm.pLastTxMeInstance != nil {
349 // compare comments above for CreateResponse (apply also here ...)
350 if msgObj.EntityClass == oFsm.pLastTxMeInstance.GetClassID() &&
351 msgObj.EntityInstance == oFsm.pLastTxMeInstance.GetEntityID() {
352 //store the created ME into DB //TODO??? obviously the Python code does not store the config ...
353 // if, then something like:
354 //oFsm.pOnuDB.StoreMe(msgObj)
355
356 switch oFsm.pLastTxMeInstance.GetName() {
357 case "OnuG":
358 { // let the FSM proceed ...
359 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvRxOnugResp)
360 }
361 case "PhysicalPathTerminationPointEthernetUni", "VirtualEthernetInterfacePoint":
362 { // let the PPTP init proceed by stopping the wait function
363 oFsm.omciLockResponseReceived <- true
364 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000365 }
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700366 } else {
367 logger.Warnf(ctx, "LockStateFsm - Received SetResponse Data for %s with wrong classID or entityID ",
368 log.Fields{"device-id": oFsm.deviceID, "data-fields": msgObj}, msgObj.EntityClass)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000369 }
Matteo Scandolob250bec2021-01-27 17:35:45 -0800370 } else {
mpagenkob06684f2021-03-23 14:39:04 +0000371 logger.Errorw(ctx, "pLastTxMeInstance is nil", log.Fields{"device-id": oFsm.deviceID})
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700372 return
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000373 }
374 } else {
mpagenkob06684f2021-03-23 14:39:04 +0000375 logger.Errorw(ctx, "LockStateFsm - Rx OMCI unhandled MsgType", log.Fields{"omciMsgType": msg.OmciMsg.MessageType})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000376 return
377 }
378}
379
dbainbri4d3a0dc2020-12-02 00:33:42 +0000380func (oFsm *lockStateFsm) performUniPortAdminSet(ctx context.Context) {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000381 var omciAdminState uint8 = 1 //default locked
Himani Chawla4d908332020-08-31 12:30:20 +0530382 if !oFsm.adminState {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000383 omciAdminState = 0
384 }
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000385 //set PPTPEthUni or VEIP AdminState
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000386 requestedAttributes := me.AttributeValueMap{"AdministrativeState": omciAdminState}
387
388 for uniNo, uniPort := range oFsm.pOmciCC.pBaseDeviceHandler.uniEntityMap {
mpagenko8b5fdd22020-12-17 17:58:32 +0000389 // only unlock the UniPort in case it is defined for usage (R2.6 limit only one port),
390 // compare also limitation for logical voltha port in dh.enableUniPortStateUpdate()
391 if (omciAdminState == 1) || (1<<uniPort.uniID)&activeUniPortStateUpdateMask == (1<<uniPort.uniID) {
392 var meInstance *me.ManagedEntity
393 if uniPort.portType == uniPPTP {
394 logger.Debugw(ctx, "Setting PPTP admin state", log.Fields{
395 "device-id": oFsm.deviceID, "for PortNo": uniNo, "state (0-unlock)": omciAdminState})
ozgecanetsiab36ed572021-04-01 10:38:48 +0300396 meInstance, err := oFsm.pOmciCC.sendSetPptpEthUniLS(log.WithSpanFromContext(context.TODO(), ctx),
397 uniPort.entityID, oFsm.pDeviceHandler.pOpenOnuAc.omciTimeout,
mpagenko8b5fdd22020-12-17 17:58:32 +0000398 true, requestedAttributes, oFsm.pAdaptFsm.commChan)
ozgecanetsiab36ed572021-04-01 10:38:48 +0300399 if err != nil {
400 logger.Errorw(ctx, "SetPptpEthUniLS set failed, aborting LockStateFsm!",
401 log.Fields{"device-id": oFsm.deviceID})
402 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
403 return
404 }
mpagenko8b5fdd22020-12-17 17:58:32 +0000405 oFsm.pLastTxMeInstance = meInstance
406 } else if uniPort.portType == uniVEIP {
407 logger.Debugw(ctx, "Setting VEIP admin state", log.Fields{
408 "device-id": oFsm.deviceID, "for PortNo": uniNo, "state (0-unlock)": omciAdminState})
ozgecanetsiab36ed572021-04-01 10:38:48 +0300409 meInstance, err := oFsm.pOmciCC.sendSetVeipLS(log.WithSpanFromContext(context.TODO(), ctx),
410 uniPort.entityID, oFsm.pDeviceHandler.pOpenOnuAc.omciTimeout,
mpagenko8b5fdd22020-12-17 17:58:32 +0000411 true, requestedAttributes, oFsm.pAdaptFsm.commChan)
ozgecanetsiab36ed572021-04-01 10:38:48 +0300412 if err != nil {
413 logger.Errorw(ctx, "SetVeipLS set failed, aborting LockStateFsm!",
414 log.Fields{"device-id": oFsm.deviceID})
415 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
416 return
417 }
mpagenko8b5fdd22020-12-17 17:58:32 +0000418 oFsm.pLastTxMeInstance = meInstance
419 } else {
420 logger.Warnw(ctx, "Unsupported UniTP type - skip",
421 log.Fields{"device-id": oFsm.deviceID, "Port": uniNo})
422 continue
423 }
mpagenkob06684f2021-03-23 14:39:04 +0000424 if oFsm.pLastTxMeInstance == nil {
425 logger.Errorw(ctx, "could not send PortAdmin OMCI message from LockStateFsm", log.Fields{
426 "device-id": oFsm.deviceID, "Port": uniNo})
427 //some more sophisticated approach is possible, e.g. repeating once, by now let's reset the state machine in order to release all resources now
428 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
429 return
430 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000431
mpagenko8b5fdd22020-12-17 17:58:32 +0000432 //verify response
433 err := oFsm.waitforOmciResponse(ctx, meInstance)
434 if err != nil {
435 logger.Errorw(ctx, "UniTP Admin State set failed, aborting LockState set!",
436 log.Fields{"device-id": oFsm.deviceID, "Port": uniNo})
437 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
438 return
439 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000440 }
441 } //for all UNI ports
442 // if Config has been done for all UNI related instances let the FSM proceed
443 // while we did not check here, if there is some port at all - !?
mpagenko8b5fdd22020-12-17 17:58:32 +0000444 logger.Infow(ctx, "UniTP adminState loop finished", log.Fields{"device-id": oFsm.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530445 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvRxUnisResp)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000446}
447
dbainbri4d3a0dc2020-12-02 00:33:42 +0000448func (oFsm *lockStateFsm) waitforOmciResponse(ctx context.Context, apMeInstance *me.ManagedEntity) error {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000449 select {
Himani Chawla4d908332020-08-31 12:30:20 +0530450 // maybe be also some outside cancel (but no context modeled for the moment ...)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000451 // case <-ctx.Done():
dbainbri4d3a0dc2020-12-02 00:33:42 +0000452 // logger.Infow(ctx,"LockState-bridge-init message reception canceled", log.Fields{"for device-id": oFsm.deviceID})
mpagenko3af1f032020-06-10 08:53:41 +0000453 case <-time.After(30 * time.Second): //3s was detected to be to less in 8*8 bbsim test with debug Info/Debug
dbainbri4d3a0dc2020-12-02 00:33:42 +0000454 logger.Warnw(ctx, "LockStateFSM uni-set timeout", log.Fields{"for device-id": oFsm.deviceID})
mpagenko01e726e2020-10-23 09:45:29 +0000455 return fmt.Errorf("lockStateFsm uni-set timeout for device-id %s", oFsm.deviceID)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000456 case success := <-oFsm.omciLockResponseReceived:
Himani Chawla4d908332020-08-31 12:30:20 +0530457 if success {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000458 logger.Debug(ctx, "LockStateFSM uni-set response received")
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000459 return nil
460 }
461 // should not happen so far
dbainbri4d3a0dc2020-12-02 00:33:42 +0000462 logger.Warnw(ctx, "LockStateFSM uni-set response error", log.Fields{"for device-id": oFsm.deviceID})
mpagenko01e726e2020-10-23 09:45:29 +0000463 return fmt.Errorf("lockStateFsm uni-set responseError for device-id %s", oFsm.deviceID)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000464 }
465}