blob: 4b829d5e90ecd8b2d8f7b1972adaac8958ff7ab0 [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 Hildebrandt0da7e6f2021-05-12 13:08:43 +000023 "sync"
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000024 "time"
25
26 "github.com/looplab/fsm"
27
28 "github.com/opencord/omci-lib-go"
29 me "github.com/opencord/omci-lib-go/generated"
Girish Gowdra50e56422021-06-01 16:46:04 -070030 "github.com/opencord/voltha-lib-go/v5/pkg/log"
dbainbri4d3a0dc2020-12-02 00:33:42 +000031 //ic "github.com/opencord/voltha-protos/v4/go/inter_container"
32 //"github.com/opencord/voltha-protos/v4/go/openflow_13"
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000033)
34
Himani Chawla6d2ae152020-09-02 13:11:20 +053035//lockStateFsm defines the structure for the state machine to lock/unlock the ONU UNI ports via OMCI
36type lockStateFsm struct {
Holger Hildebrandt8165eda2020-09-24 09:39:24 +000037 pDeviceHandler *deviceHandler
mpagenko01e726e2020-10-23 09:45:29 +000038 deviceID string
Himani Chawla6d2ae152020-09-02 13:11:20 +053039 pOmciCC *omciCC
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +000040 mutexAdminState sync.RWMutex
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000041 adminState bool
42 requestEvent OnuDeviceEvent
43 omciLockResponseReceived chan bool //seperate channel needed for checking UNI port OMCi message responses
44 pAdaptFsm *AdapterFsm
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +000045 mutexPLastTxMeInstance sync.RWMutex
mpagenko01e726e2020-10-23 09:45:29 +000046 pLastTxMeInstance *me.ManagedEntity
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000047}
48
mpagenko1cc3cb42020-07-27 15:24:38 +000049const (
50 // events of lock/unlock UNI port FSM
51 uniEvStart = "uniEvStart"
52 uniEvStartAdmin = "uniEvStartAdmin"
53 uniEvRxUnisResp = "uniEvRxUnisResp"
54 uniEvRxOnugResp = "uniEvRxOnugResp"
55 uniEvTimeoutSimple = "uniEvTimeoutSimple"
56 uniEvTimeoutUnis = "uniEvTimeoutUnis"
57 uniEvReset = "uniEvReset"
58 uniEvRestart = "uniEvRestart"
59)
60const (
61 // states of lock/unlock UNI port FSM
62 uniStDisabled = "uniStDisabled"
63 uniStStarting = "uniStStarting"
64 uniStSettingUnis = "uniStSettingUnis"
65 uniStSettingOnuG = "uniStSettingOnuG"
66 uniStAdminDone = "uniStAdminDone"
67 uniStResetting = "uniStResetting"
68)
Holger Hildebrandt10d98192021-01-27 15:29:31 +000069const cUniFsmIdleState = uniStDisabled
mpagenko1cc3cb42020-07-27 15:24:38 +000070
Himani Chawla6d2ae152020-09-02 13:11:20 +053071//newLockStateFsm is the 'constructor' for the state machine to lock/unlock the ONU UNI ports via OMCI
dbainbri4d3a0dc2020-12-02 00:33:42 +000072func newLockStateFsm(ctx context.Context, apDevOmciCC *omciCC, aAdminState bool, aRequestEvent OnuDeviceEvent,
Holger Hildebrandt8165eda2020-09-24 09:39:24 +000073 aName string, apDeviceHandler *deviceHandler, aCommChannel chan Message) *lockStateFsm {
Himani Chawla6d2ae152020-09-02 13:11:20 +053074 instFsm := &lockStateFsm{
Holger Hildebrandt8165eda2020-09-24 09:39:24 +000075 pDeviceHandler: apDeviceHandler,
mpagenko01e726e2020-10-23 09:45:29 +000076 deviceID: apDeviceHandler.deviceID,
Holger Hildebrandt8165eda2020-09-24 09:39:24 +000077 pOmciCC: apDevOmciCC,
78 adminState: aAdminState,
79 requestEvent: aRequestEvent,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000080 }
mpagenko01e726e2020-10-23 09:45:29 +000081 instFsm.pAdaptFsm = NewAdapterFsm(aName, instFsm.deviceID, aCommChannel)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000082 if instFsm.pAdaptFsm == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +000083 logger.Errorw(ctx, "LockStateFsm's AdapterFsm could not be instantiated!!", log.Fields{
mpagenko01e726e2020-10-23 09:45:29 +000084 "device-id": instFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000085 return nil
86 }
Himani Chawla4d908332020-08-31 12:30:20 +053087 if aAdminState { //port locking requested
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000088 instFsm.pAdaptFsm.pFsm = fsm.NewFSM(
mpagenko1cc3cb42020-07-27 15:24:38 +000089 uniStDisabled,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000090 fsm.Events{
91
mpagenko1cc3cb42020-07-27 15:24:38 +000092 {Name: uniEvStart, Src: []string{uniStDisabled}, Dst: uniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000093
mpagenko1cc3cb42020-07-27 15:24:38 +000094 {Name: uniEvStartAdmin, Src: []string{uniStStarting}, Dst: uniStSettingUnis},
95 // the settingUnis state is used for multi ME config for all UNI related ports
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000096 // maybe such could be reflected in the state machine as well (port number parametrized)
97 // but that looks not straightforward here - so we keep it simple here for the beginning(?)
mpagenko1cc3cb42020-07-27 15:24:38 +000098 {Name: uniEvRxUnisResp, Src: []string{uniStSettingUnis}, Dst: uniStSettingOnuG},
99 {Name: uniEvRxOnugResp, Src: []string{uniStSettingOnuG}, Dst: uniStAdminDone},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000100
mpagenko1cc3cb42020-07-27 15:24:38 +0000101 {Name: uniEvTimeoutSimple, Src: []string{uniStSettingOnuG}, Dst: uniStStarting},
102 {Name: uniEvTimeoutUnis, Src: []string{uniStSettingUnis}, Dst: uniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000103
mpagenko1cc3cb42020-07-27 15:24:38 +0000104 {Name: uniEvReset, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis,
105 uniStAdminDone}, Dst: uniStResetting},
106 // exceptional treatment for all states except uniStResetting
107 {Name: uniEvRestart, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis,
108 uniStAdminDone, uniStResetting}, Dst: uniStDisabled},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000109 },
110
111 fsm.Callbacks{
dbainbri4d3a0dc2020-12-02 00:33:42 +0000112 "enter_state": func(e *fsm.Event) { instFsm.pAdaptFsm.logFsmStateChange(ctx, e) },
113 ("enter_" + uniStStarting): func(e *fsm.Event) { instFsm.enterAdminStartingState(ctx, e) },
114 ("enter_" + uniStSettingOnuG): func(e *fsm.Event) { instFsm.enterSettingOnuGState(ctx, e) },
115 ("enter_" + uniStSettingUnis): func(e *fsm.Event) { instFsm.enterSettingUnisState(ctx, e) },
116 ("enter_" + uniStAdminDone): func(e *fsm.Event) { instFsm.enterAdminDoneState(ctx, e) },
117 ("enter_" + uniStResetting): func(e *fsm.Event) { instFsm.enterResettingState(ctx, e) },
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000118 },
119 )
120 } else { //port unlocking requested
121 instFsm.pAdaptFsm.pFsm = fsm.NewFSM(
mpagenko1cc3cb42020-07-27 15:24:38 +0000122 uniStDisabled,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000123 fsm.Events{
124
mpagenko1cc3cb42020-07-27 15:24:38 +0000125 {Name: uniEvStart, Src: []string{uniStDisabled}, Dst: uniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000126
mpagenko1cc3cb42020-07-27 15:24:38 +0000127 {Name: uniEvStartAdmin, Src: []string{uniStStarting}, Dst: uniStSettingOnuG},
128 {Name: uniEvRxOnugResp, Src: []string{uniStSettingOnuG}, Dst: uniStSettingUnis},
129 // the settingUnis state is used for multi ME config for all UNI related ports
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000130 // maybe such could be reflected in the state machine as well (port number parametrized)
131 // but that looks not straightforward here - so we keep it simple here for the beginning(?)
mpagenko1cc3cb42020-07-27 15:24:38 +0000132 {Name: uniEvRxUnisResp, Src: []string{uniStSettingUnis}, Dst: uniStAdminDone},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000133
mpagenko1cc3cb42020-07-27 15:24:38 +0000134 {Name: uniEvTimeoutSimple, Src: []string{uniStSettingOnuG}, Dst: uniStStarting},
135 {Name: uniEvTimeoutUnis, Src: []string{uniStSettingUnis}, Dst: uniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000136
mpagenko1cc3cb42020-07-27 15:24:38 +0000137 {Name: uniEvReset, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis,
138 uniStAdminDone}, Dst: uniStResetting},
139 // exceptional treatment for all states except uniStResetting
140 {Name: uniEvRestart, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis,
141 uniStAdminDone, uniStResetting}, Dst: uniStDisabled},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000142 },
143
144 fsm.Callbacks{
dbainbri4d3a0dc2020-12-02 00:33:42 +0000145 "enter_state": func(e *fsm.Event) { instFsm.pAdaptFsm.logFsmStateChange(ctx, e) },
146 ("enter_" + uniStStarting): func(e *fsm.Event) { instFsm.enterAdminStartingState(ctx, e) },
147 ("enter_" + uniStSettingOnuG): func(e *fsm.Event) { instFsm.enterSettingOnuGState(ctx, e) },
148 ("enter_" + uniStSettingUnis): func(e *fsm.Event) { instFsm.enterSettingUnisState(ctx, e) },
149 ("enter_" + uniStAdminDone): func(e *fsm.Event) { instFsm.enterAdminDoneState(ctx, e) },
150 ("enter_" + uniStResetting): func(e *fsm.Event) { instFsm.enterResettingState(ctx, e) },
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000151 },
152 )
153 }
154 if instFsm.pAdaptFsm.pFsm == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000155 logger.Errorw(ctx, "LockStateFsm's Base FSM could not be instantiated!!", log.Fields{
mpagenko01e726e2020-10-23 09:45:29 +0000156 "device-id": instFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000157 return nil
158 }
159
dbainbri4d3a0dc2020-12-02 00:33:42 +0000160 logger.Debugw(ctx, "LockStateFsm created", log.Fields{"device-id": instFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000161 return instFsm
162}
163
Himani Chawla6d2ae152020-09-02 13:11:20 +0530164//setSuccessEvent modifies the requested event notified on success
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000165//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 +0530166func (oFsm *lockStateFsm) setSuccessEvent(aEvent OnuDeviceEvent) {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000167 oFsm.requestEvent = aEvent
168}
169
dbainbri4d3a0dc2020-12-02 00:33:42 +0000170func (oFsm *lockStateFsm) enterAdminStartingState(ctx context.Context, e *fsm.Event) {
171 logger.Debugw(ctx, "LockStateFSM start", log.Fields{"in state": e.FSM.Current(),
mpagenko01e726e2020-10-23 09:45:29 +0000172 "device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000173 // in case the used channel is not yet defined (can be re-used after restarts)
174 if oFsm.omciLockResponseReceived == nil {
175 oFsm.omciLockResponseReceived = make(chan bool)
dbainbri4d3a0dc2020-12-02 00:33:42 +0000176 logger.Debug(ctx, "LockStateFSM - OMCI UniLock RxChannel defined")
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000177 } else {
178 // as we may 're-use' this instance of FSM and the connected channel
mpagenkob06684f2021-03-23 14:39:04 +0000179 // make sure there is no 'lingering' request in the already existing channels:
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000180 // (simple loop sufficient as we are the only receiver)
181 for len(oFsm.omciLockResponseReceived) > 0 {
182 <-oFsm.omciLockResponseReceived
183 }
mpagenkob06684f2021-03-23 14:39:04 +0000184 for len(oFsm.pAdaptFsm.commChan) > 0 {
185 <-oFsm.pAdaptFsm.commChan
186 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000187 }
188 // start go routine for processing of LockState messages
dbainbri4d3a0dc2020-12-02 00:33:42 +0000189 go oFsm.processOmciLockMessages(ctx)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000190
191 //let the state machine run forward from here directly
192 pLockStateAFsm := oFsm.pAdaptFsm
193 if pLockStateAFsm != nil {
194 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
195 go func(a_pAFsm *AdapterFsm) {
196 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
Himani Chawla4d908332020-08-31 12:30:20 +0530197 _ = a_pAFsm.pFsm.Event(uniEvStartAdmin)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000198 }
199 }(pLockStateAFsm)
200 }
201}
202
dbainbri4d3a0dc2020-12-02 00:33:42 +0000203func (oFsm *lockStateFsm) enterSettingOnuGState(ctx context.Context, e *fsm.Event) {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000204 var omciAdminState uint8 = 1 //default locked
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000205 oFsm.mutexAdminState.RLock()
Himani Chawla4d908332020-08-31 12:30:20 +0530206 if !oFsm.adminState {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000207 omciAdminState = 0
208 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000209 oFsm.mutexAdminState.RUnlock()
dbainbri4d3a0dc2020-12-02 00:33:42 +0000210 logger.Debugw(ctx, "LockStateFSM Tx Set::ONU-G:admin", log.Fields{
mpagenko01e726e2020-10-23 09:45:29 +0000211 "omciAdmin": omciAdminState, "in state": e.FSM.Current(), "device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000212 requestedAttributes := me.AttributeValueMap{"AdministrativeState": omciAdminState}
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000213 oFsm.mutexPLastTxMeInstance.Lock()
ozgecanetsiab36ed572021-04-01 10:38:48 +0300214 meInstance, err := oFsm.pOmciCC.sendSetOnuGLS(log.WithSpanFromContext(context.TODO(), ctx), oFsm.pDeviceHandler.pOpenOnuAc.omciTimeout, true,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000215 requestedAttributes, oFsm.pAdaptFsm.commChan)
ozgecanetsiab36ed572021-04-01 10:38:48 +0300216 if err != nil {
Holger Hildebrandtda15a092022-01-07 15:30:49 +0000217 //Indicate the failure in UnLock case
218 if omciAdminState == 0 {
219 oFsm.setSuccessEvent(UniEnableStateFailed)
220 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000221 oFsm.mutexPLastTxMeInstance.Unlock()
ozgecanetsiab36ed572021-04-01 10:38:48 +0300222 logger.Errorw(ctx, "OnuGLS set failed, aborting LockStateFSM", log.Fields{"device-id": oFsm.deviceID})
223 pLockStateAFsm := oFsm.pAdaptFsm
224 if pLockStateAFsm != nil {
225 go func(a_pAFsm *AdapterFsm) {
226 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
227 _ = a_pAFsm.pFsm.Event(uniEvReset)
228 }
229 }(pLockStateAFsm)
230 }
231 return
232 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000233 //accept also nil as (error) return value for writing to LastTx
234 // - this avoids misinterpretation of new received OMCI messages
mpagenko01e726e2020-10-23 09:45:29 +0000235 oFsm.pLastTxMeInstance = meInstance
mpagenkob06684f2021-03-23 14:39:04 +0000236 if oFsm.pLastTxMeInstance == nil {
Holger Hildebrandtda15a092022-01-07 15:30:49 +0000237 //Indicate the failure in UnLock case
238 if omciAdminState == 0 {
239 oFsm.setSuccessEvent(UniEnableStateFailed)
240 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000241 oFsm.mutexPLastTxMeInstance.Unlock()
mpagenkob06684f2021-03-23 14:39:04 +0000242 logger.Errorw(ctx, "could not send OMCI message from LockStateFsm", log.Fields{
243 "device-id": oFsm.deviceID})
244 //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
245 pLockStateAFsm := oFsm.pAdaptFsm
246 if pLockStateAFsm != nil {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000247
mpagenkob06684f2021-03-23 14:39:04 +0000248 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
249 go func(a_pAFsm *AdapterFsm) {
250 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
251 _ = a_pAFsm.pFsm.Event(uniEvReset)
252 }
253 }(pLockStateAFsm)
254 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000255 return
mpagenkob06684f2021-03-23 14:39:04 +0000256 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000257 oFsm.mutexPLastTxMeInstance.Unlock()
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000258}
259
dbainbri4d3a0dc2020-12-02 00:33:42 +0000260func (oFsm *lockStateFsm) enterSettingUnisState(ctx context.Context, e *fsm.Event) {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000261 oFsm.mutexAdminState.RLock()
mpagenko8b5fdd22020-12-17 17:58:32 +0000262 logger.Debugw(ctx, "LockStateFSM - starting UniTP adminState loop", log.Fields{
mpagenko01e726e2020-10-23 09:45:29 +0000263 "in state": e.FSM.Current(), "device-id": oFsm.deviceID, "LockState": oFsm.adminState})
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000264 oFsm.mutexAdminState.RUnlock()
dbainbri4d3a0dc2020-12-02 00:33:42 +0000265 go oFsm.performUniPortAdminSet(ctx)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000266}
267
dbainbri4d3a0dc2020-12-02 00:33:42 +0000268func (oFsm *lockStateFsm) enterAdminDoneState(ctx context.Context, e *fsm.Event) {
269 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 +0000270 //use DeviceHandler event notification directly, no need/support to update DeviceEntryState for lock/unlock
dbainbri4d3a0dc2020-12-02 00:33:42 +0000271 oFsm.pDeviceHandler.deviceProcStatusUpdate(ctx, oFsm.requestEvent)
Holger Hildebrandt8165eda2020-09-24 09:39:24 +0000272
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000273 //let's reset the state machine in order to release all resources now
274 pLockStateAFsm := oFsm.pAdaptFsm
275 if pLockStateAFsm != nil {
276 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
277 go func(a_pAFsm *AdapterFsm) {
278 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
Himani Chawla4d908332020-08-31 12:30:20 +0530279 _ = a_pAFsm.pFsm.Event(uniEvReset)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000280 }
281 }(pLockStateAFsm)
282 }
283}
284
dbainbri4d3a0dc2020-12-02 00:33:42 +0000285func (oFsm *lockStateFsm) enterResettingState(ctx context.Context, e *fsm.Event) {
286 logger.Debugw(ctx, "LockStateFSM resetting", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtda15a092022-01-07 15:30:49 +0000287 //If the fsm is reseted because of a failure during reenable, then issue the fail event.
288 if oFsm.requestEvent == UniEnableStateFailed {
289 logger.Debugw(ctx, "LockStateFSM send notification to core", log.Fields{"state": e.FSM.Current(), "device-id": oFsm.deviceID})
290 //use DeviceHandler event notification directly, no need/support to update DeviceEntryState for lock/unlock
291 oFsm.pDeviceHandler.deviceProcStatusUpdate(ctx, oFsm.requestEvent)
292 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000293 pLockStateAFsm := oFsm.pAdaptFsm
294 if pLockStateAFsm != nil {
295 // abort running message processing
296 fsmAbortMsg := Message{
297 Type: TestMsg,
298 Data: TestMessage{
299 TestMessageVal: AbortMessageProcessing,
300 },
301 }
302 pLockStateAFsm.commChan <- fsmAbortMsg
303
304 //try to restart the FSM to 'disabled'
305 // see DownloadedState: decouple event transfer
306 go func(a_pAFsm *AdapterFsm) {
307 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
Himani Chawla4d908332020-08-31 12:30:20 +0530308 _ = a_pAFsm.pFsm.Event(uniEvRestart)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000309 }
310 }(pLockStateAFsm)
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000311 oFsm.mutexPLastTxMeInstance.Lock()
mpagenko01e726e2020-10-23 09:45:29 +0000312 oFsm.pLastTxMeInstance = nil
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000313 oFsm.mutexPLastTxMeInstance.Unlock()
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000314 }
315}
316
dbainbri4d3a0dc2020-12-02 00:33:42 +0000317func (oFsm *lockStateFsm) processOmciLockMessages(ctx context.Context) {
318 logger.Debugw(ctx, "Start LockStateFsm Msg processing", log.Fields{"for device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000319loop:
320 for {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000321 // case <-ctx.Done():
dbainbri4d3a0dc2020-12-02 00:33:42 +0000322 // logger.Info(ctx,"MibSync Msg", log.Fields{"Message handling canceled via context for device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000323 // break loop
Himani Chawla4d908332020-08-31 12:30:20 +0530324 message, ok := <-oFsm.pAdaptFsm.commChan
325 if !ok {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000326 logger.Info(ctx, "LockStateFsm Rx Msg - could not read from channel", log.Fields{"device-id": oFsm.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530327 // but then we have to ensure a restart of the FSM as well - as exceptional procedure
328 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvRestart)
329 break loop
330 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000331 logger.Debugw(ctx, "LockStateFsm Rx Msg", log.Fields{"device-id": oFsm.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530332
333 switch message.Type {
334 case TestMsg:
335 msg, _ := message.Data.(TestMessage)
336 if msg.TestMessageVal == AbortMessageProcessing {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000337 logger.Debugw(ctx, "LockStateFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000338 break loop
339 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000340 logger.Warnw(ctx, "LockStateFsm unknown TestMessage", log.Fields{"device-id": oFsm.deviceID, "MessageVal": msg.TestMessageVal})
Himani Chawla4d908332020-08-31 12:30:20 +0530341 case OMCI:
342 msg, _ := message.Data.(OmciMessage)
dbainbri4d3a0dc2020-12-02 00:33:42 +0000343 oFsm.handleOmciLockStateMessage(ctx, msg)
Himani Chawla4d908332020-08-31 12:30:20 +0530344 default:
dbainbri4d3a0dc2020-12-02 00:33:42 +0000345 logger.Warn(ctx, "LockStateFsm Rx unknown message", log.Fields{"device-id": oFsm.deviceID,
Himani Chawla4d908332020-08-31 12:30:20 +0530346 "message.Type": message.Type})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000347 }
348 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000349 logger.Debugw(ctx, "End LockStateFsm Msg processing", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000350}
351
dbainbri4d3a0dc2020-12-02 00:33:42 +0000352func (oFsm *lockStateFsm) handleOmciLockStateMessage(ctx context.Context, msg OmciMessage) {
353 logger.Debugw(ctx, "Rx OMCI LockStateFsm Msg", log.Fields{"device-id": oFsm.deviceID,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000354 "msgType": msg.OmciMsg.MessageType})
355
356 if msg.OmciMsg.MessageType == omci.SetResponseType {
357 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeSetResponse)
358 if msgLayer == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000359 logger.Errorw(ctx, "LockStateFsm - Omci Msg layer could not be detected for SetResponse",
mpagenko01e726e2020-10-23 09:45:29 +0000360 log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000361 return
362 }
363 msgObj, msgOk := msgLayer.(*omci.SetResponse)
364 if !msgOk {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000365 logger.Errorw(ctx, "LockStateFsm - Omci Msg layer could not be assigned for SetResponse",
mpagenko01e726e2020-10-23 09:45:29 +0000366 log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000367 return
368 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000369 logger.Debugw(ctx, "LockStateFsm SetResponse Data", log.Fields{"device-id": oFsm.deviceID, "data-fields": msgObj})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000370 if msgObj.Result != me.Success {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000371 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 +0000372 // possibly force FSM into abort or ignore some errors for some messages? store error for mgmt display?
373 return
374 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000375
mpagenkob06684f2021-03-23 14:39:04 +0000376 //should never appear, left here for robustness
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000377 oFsm.mutexPLastTxMeInstance.RLock()
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700378 if oFsm.pLastTxMeInstance != nil {
379 // compare comments above for CreateResponse (apply also here ...)
380 if msgObj.EntityClass == oFsm.pLastTxMeInstance.GetClassID() &&
381 msgObj.EntityInstance == oFsm.pLastTxMeInstance.GetEntityID() {
382 //store the created ME into DB //TODO??? obviously the Python code does not store the config ...
383 // if, then something like:
384 //oFsm.pOnuDB.StoreMe(msgObj)
385
386 switch oFsm.pLastTxMeInstance.GetName() {
387 case "OnuG":
388 { // let the FSM proceed ...
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000389 oFsm.mutexPLastTxMeInstance.RUnlock()
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700390 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvRxOnugResp)
391 }
392 case "PhysicalPathTerminationPointEthernetUni", "VirtualEthernetInterfacePoint":
393 { // let the PPTP init proceed by stopping the wait function
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000394 oFsm.mutexPLastTxMeInstance.RUnlock()
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700395 oFsm.omciLockResponseReceived <- true
396 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000397 default:
398 {
399 logger.Warnw(ctx, "Unsupported ME name received!",
400 log.Fields{"ME name": oFsm.pLastTxMeInstance.GetName(), "device-id": oFsm.deviceID})
401 oFsm.mutexPLastTxMeInstance.RUnlock()
402 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000403 }
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700404 } else {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000405 oFsm.mutexPLastTxMeInstance.RUnlock()
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700406 logger.Warnf(ctx, "LockStateFsm - Received SetResponse Data for %s with wrong classID or entityID ",
407 log.Fields{"device-id": oFsm.deviceID, "data-fields": msgObj}, msgObj.EntityClass)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000408 }
Matteo Scandolob250bec2021-01-27 17:35:45 -0800409 } else {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000410 oFsm.mutexPLastTxMeInstance.RUnlock()
mpagenkob06684f2021-03-23 14:39:04 +0000411 logger.Errorw(ctx, "pLastTxMeInstance is nil", log.Fields{"device-id": oFsm.deviceID})
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700412 return
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000413 }
414 } else {
mpagenkob06684f2021-03-23 14:39:04 +0000415 logger.Errorw(ctx, "LockStateFsm - Rx OMCI unhandled MsgType", log.Fields{"omciMsgType": msg.OmciMsg.MessageType})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000416 return
417 }
418}
419
dbainbri4d3a0dc2020-12-02 00:33:42 +0000420func (oFsm *lockStateFsm) performUniPortAdminSet(ctx context.Context) {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000421 var omciAdminState uint8 = 1 //default locked
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000422 oFsm.mutexAdminState.RLock()
Himani Chawla4d908332020-08-31 12:30:20 +0530423 if !oFsm.adminState {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000424 omciAdminState = 0
425 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000426 oFsm.mutexAdminState.RUnlock()
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000427 //set PPTPEthUni or VEIP AdminState
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000428 requestedAttributes := me.AttributeValueMap{"AdministrativeState": omciAdminState}
429
430 for uniNo, uniPort := range oFsm.pOmciCC.pBaseDeviceHandler.uniEntityMap {
mpagenko8b5fdd22020-12-17 17:58:32 +0000431 // only unlock the UniPort in case it is defined for usage (R2.6 limit only one port),
432 // compare also limitation for logical voltha port in dh.enableUniPortStateUpdate()
Matteo Scandolo20d180c2021-06-10 17:20:21 +0200433
434 if (omciAdminState == 1) || (1<<uniPort.uniID)&oFsm.pDeviceHandler.pOpenOnuAc.config.UniPortMask == (1<<uniPort.uniID) {
mpagenko8b5fdd22020-12-17 17:58:32 +0000435 var meInstance *me.ManagedEntity
436 if uniPort.portType == uniPPTP {
437 logger.Debugw(ctx, "Setting PPTP admin state", log.Fields{
438 "device-id": oFsm.deviceID, "for PortNo": uniNo, "state (0-unlock)": omciAdminState})
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000439 oFsm.mutexPLastTxMeInstance.Lock()
ozgecanetsiab36ed572021-04-01 10:38:48 +0300440 meInstance, err := oFsm.pOmciCC.sendSetPptpEthUniLS(log.WithSpanFromContext(context.TODO(), ctx),
441 uniPort.entityID, oFsm.pDeviceHandler.pOpenOnuAc.omciTimeout,
mpagenko8b5fdd22020-12-17 17:58:32 +0000442 true, requestedAttributes, oFsm.pAdaptFsm.commChan)
ozgecanetsiab36ed572021-04-01 10:38:48 +0300443 if err != nil {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000444 oFsm.mutexPLastTxMeInstance.Unlock()
ozgecanetsiab36ed572021-04-01 10:38:48 +0300445 logger.Errorw(ctx, "SetPptpEthUniLS set failed, aborting LockStateFsm!",
446 log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtda15a092022-01-07 15:30:49 +0000447 //Indicate the failure in UnLock case
448 if omciAdminState == 0 {
449 oFsm.setSuccessEvent(UniEnableStateFailed)
450 }
ozgecanetsiab36ed572021-04-01 10:38:48 +0300451 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
452 return
453 }
mpagenko8b5fdd22020-12-17 17:58:32 +0000454 oFsm.pLastTxMeInstance = meInstance
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000455 oFsm.mutexPLastTxMeInstance.Unlock()
mpagenko8b5fdd22020-12-17 17:58:32 +0000456 } else if uniPort.portType == uniVEIP {
457 logger.Debugw(ctx, "Setting VEIP admin state", log.Fields{
458 "device-id": oFsm.deviceID, "for PortNo": uniNo, "state (0-unlock)": omciAdminState})
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000459 oFsm.mutexPLastTxMeInstance.Lock()
ozgecanetsiab36ed572021-04-01 10:38:48 +0300460 meInstance, err := oFsm.pOmciCC.sendSetVeipLS(log.WithSpanFromContext(context.TODO(), ctx),
461 uniPort.entityID, oFsm.pDeviceHandler.pOpenOnuAc.omciTimeout,
mpagenko8b5fdd22020-12-17 17:58:32 +0000462 true, requestedAttributes, oFsm.pAdaptFsm.commChan)
ozgecanetsiab36ed572021-04-01 10:38:48 +0300463 if err != nil {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000464 oFsm.mutexPLastTxMeInstance.Unlock()
ozgecanetsiab36ed572021-04-01 10:38:48 +0300465 logger.Errorw(ctx, "SetVeipLS set failed, aborting LockStateFsm!",
466 log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtda15a092022-01-07 15:30:49 +0000467 //Indicate the failure in UnLock case
468 if omciAdminState == 0 {
469 oFsm.setSuccessEvent(UniEnableStateFailed)
470 }
ozgecanetsiab36ed572021-04-01 10:38:48 +0300471 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
472 return
473 }
mpagenko8b5fdd22020-12-17 17:58:32 +0000474 oFsm.pLastTxMeInstance = meInstance
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000475 oFsm.mutexPLastTxMeInstance.Unlock()
mpagenko8b5fdd22020-12-17 17:58:32 +0000476 } else {
477 logger.Warnw(ctx, "Unsupported UniTP type - skip",
478 log.Fields{"device-id": oFsm.deviceID, "Port": uniNo})
479 continue
480 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000481 oFsm.mutexPLastTxMeInstance.RLock()
mpagenkob06684f2021-03-23 14:39:04 +0000482 if oFsm.pLastTxMeInstance == nil {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000483 oFsm.mutexPLastTxMeInstance.RUnlock()
mpagenkob06684f2021-03-23 14:39:04 +0000484 logger.Errorw(ctx, "could not send PortAdmin OMCI message from LockStateFsm", log.Fields{
485 "device-id": oFsm.deviceID, "Port": uniNo})
Holger Hildebrandtda15a092022-01-07 15:30:49 +0000486 //Indicate the failure in UnLock case
487 if omciAdminState == 0 {
488 oFsm.setSuccessEvent(UniEnableStateFailed)
489 }
mpagenkob06684f2021-03-23 14:39:04 +0000490 //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
491 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
492 return
493 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000494 oFsm.mutexPLastTxMeInstance.RUnlock()
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000495
mpagenko8b5fdd22020-12-17 17:58:32 +0000496 //verify response
497 err := oFsm.waitforOmciResponse(ctx, meInstance)
498 if err != nil {
499 logger.Errorw(ctx, "UniTP Admin State set failed, aborting LockState set!",
500 log.Fields{"device-id": oFsm.deviceID, "Port": uniNo})
Holger Hildebrandtda15a092022-01-07 15:30:49 +0000501 //Indicate the failure in UnLock case
502 if omciAdminState == 0 {
503 oFsm.setSuccessEvent(UniEnableStateFailed)
504 }
mpagenko8b5fdd22020-12-17 17:58:32 +0000505 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
506 return
507 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000508 }
509 } //for all UNI ports
510 // if Config has been done for all UNI related instances let the FSM proceed
511 // while we did not check here, if there is some port at all - !?
mpagenko8b5fdd22020-12-17 17:58:32 +0000512 logger.Infow(ctx, "UniTP adminState loop finished", log.Fields{"device-id": oFsm.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530513 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvRxUnisResp)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000514}
515
dbainbri4d3a0dc2020-12-02 00:33:42 +0000516func (oFsm *lockStateFsm) waitforOmciResponse(ctx context.Context, apMeInstance *me.ManagedEntity) error {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000517 select {
Himani Chawla4d908332020-08-31 12:30:20 +0530518 // maybe be also some outside cancel (but no context modeled for the moment ...)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000519 // case <-ctx.Done():
dbainbri4d3a0dc2020-12-02 00:33:42 +0000520 // logger.Infow(ctx,"LockState-bridge-init message reception canceled", log.Fields{"for device-id": oFsm.deviceID})
Holger Hildebrandt366ef192021-05-05 11:07:44 +0000521 case <-time.After(oFsm.pOmciCC.GetMaxOmciTimeoutWithRetries() * time.Second): //3s was detected to be to less in 8*8 bbsim test with debug Info/Debug
dbainbri4d3a0dc2020-12-02 00:33:42 +0000522 logger.Warnw(ctx, "LockStateFSM uni-set timeout", log.Fields{"for device-id": oFsm.deviceID})
mpagenko01e726e2020-10-23 09:45:29 +0000523 return fmt.Errorf("lockStateFsm uni-set timeout for device-id %s", oFsm.deviceID)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000524 case success := <-oFsm.omciLockResponseReceived:
Himani Chawla4d908332020-08-31 12:30:20 +0530525 if success {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000526 logger.Debug(ctx, "LockStateFSM uni-set response received")
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000527 return nil
528 }
529 // should not happen so far
dbainbri4d3a0dc2020-12-02 00:33:42 +0000530 logger.Warnw(ctx, "LockStateFSM uni-set response error", log.Fields{"for device-id": oFsm.deviceID})
mpagenko01e726e2020-10-23 09:45:29 +0000531 return fmt.Errorf("lockStateFsm uni-set responseError for device-id %s", oFsm.deviceID)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000532 }
533}