blob: 0c22500485a836e86c2a6158a09b43588280d0b7 [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 Hildebrandt0da7e6f2021-05-12 13:08:43 +0000217 oFsm.mutexPLastTxMeInstance.Unlock()
ozgecanetsiab36ed572021-04-01 10:38:48 +0300218 logger.Errorw(ctx, "OnuGLS set failed, aborting LockStateFSM", log.Fields{"device-id": oFsm.deviceID})
219 pLockStateAFsm := oFsm.pAdaptFsm
220 if pLockStateAFsm != nil {
221 go func(a_pAFsm *AdapterFsm) {
222 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
223 _ = a_pAFsm.pFsm.Event(uniEvReset)
224 }
225 }(pLockStateAFsm)
226 }
227 return
228 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000229 //accept also nil as (error) return value for writing to LastTx
230 // - this avoids misinterpretation of new received OMCI messages
mpagenko01e726e2020-10-23 09:45:29 +0000231 oFsm.pLastTxMeInstance = meInstance
mpagenkob06684f2021-03-23 14:39:04 +0000232 if oFsm.pLastTxMeInstance == nil {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000233 oFsm.mutexPLastTxMeInstance.Unlock()
mpagenkob06684f2021-03-23 14:39:04 +0000234 logger.Errorw(ctx, "could not send OMCI message from LockStateFsm", log.Fields{
235 "device-id": oFsm.deviceID})
236 //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
237 pLockStateAFsm := oFsm.pAdaptFsm
238 if pLockStateAFsm != nil {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000239
mpagenkob06684f2021-03-23 14:39:04 +0000240 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
241 go func(a_pAFsm *AdapterFsm) {
242 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
243 _ = a_pAFsm.pFsm.Event(uniEvReset)
244 }
245 }(pLockStateAFsm)
246 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000247 return
mpagenkob06684f2021-03-23 14:39:04 +0000248 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000249 oFsm.mutexPLastTxMeInstance.Unlock()
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000250}
251
dbainbri4d3a0dc2020-12-02 00:33:42 +0000252func (oFsm *lockStateFsm) enterSettingUnisState(ctx context.Context, e *fsm.Event) {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000253 oFsm.mutexAdminState.RLock()
mpagenko8b5fdd22020-12-17 17:58:32 +0000254 logger.Debugw(ctx, "LockStateFSM - starting UniTP adminState loop", log.Fields{
mpagenko01e726e2020-10-23 09:45:29 +0000255 "in state": e.FSM.Current(), "device-id": oFsm.deviceID, "LockState": oFsm.adminState})
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000256 oFsm.mutexAdminState.RUnlock()
dbainbri4d3a0dc2020-12-02 00:33:42 +0000257 go oFsm.performUniPortAdminSet(ctx)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000258}
259
dbainbri4d3a0dc2020-12-02 00:33:42 +0000260func (oFsm *lockStateFsm) enterAdminDoneState(ctx context.Context, e *fsm.Event) {
261 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 +0000262 //use DeviceHandler event notification directly, no need/support to update DeviceEntryState for lock/unlock
dbainbri4d3a0dc2020-12-02 00:33:42 +0000263 oFsm.pDeviceHandler.deviceProcStatusUpdate(ctx, oFsm.requestEvent)
Holger Hildebrandt8165eda2020-09-24 09:39:24 +0000264
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000265 //let's reset the state machine in order to release all resources now
266 pLockStateAFsm := oFsm.pAdaptFsm
267 if pLockStateAFsm != nil {
268 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
269 go func(a_pAFsm *AdapterFsm) {
270 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
Himani Chawla4d908332020-08-31 12:30:20 +0530271 _ = a_pAFsm.pFsm.Event(uniEvReset)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000272 }
273 }(pLockStateAFsm)
274 }
275}
276
dbainbri4d3a0dc2020-12-02 00:33:42 +0000277func (oFsm *lockStateFsm) enterResettingState(ctx context.Context, e *fsm.Event) {
278 logger.Debugw(ctx, "LockStateFSM resetting", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000279 pLockStateAFsm := oFsm.pAdaptFsm
280 if pLockStateAFsm != nil {
281 // abort running message processing
282 fsmAbortMsg := Message{
283 Type: TestMsg,
284 Data: TestMessage{
285 TestMessageVal: AbortMessageProcessing,
286 },
287 }
288 pLockStateAFsm.commChan <- fsmAbortMsg
289
290 //try to restart the FSM to 'disabled'
291 // see DownloadedState: decouple event transfer
292 go func(a_pAFsm *AdapterFsm) {
293 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
Himani Chawla4d908332020-08-31 12:30:20 +0530294 _ = a_pAFsm.pFsm.Event(uniEvRestart)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000295 }
296 }(pLockStateAFsm)
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000297 oFsm.mutexPLastTxMeInstance.Lock()
mpagenko01e726e2020-10-23 09:45:29 +0000298 oFsm.pLastTxMeInstance = nil
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000299 oFsm.mutexPLastTxMeInstance.Unlock()
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000300 }
301}
302
dbainbri4d3a0dc2020-12-02 00:33:42 +0000303func (oFsm *lockStateFsm) processOmciLockMessages(ctx context.Context) {
304 logger.Debugw(ctx, "Start LockStateFsm Msg processing", log.Fields{"for device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000305loop:
306 for {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000307 // case <-ctx.Done():
dbainbri4d3a0dc2020-12-02 00:33:42 +0000308 // logger.Info(ctx,"MibSync Msg", log.Fields{"Message handling canceled via context for device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000309 // break loop
Himani Chawla4d908332020-08-31 12:30:20 +0530310 message, ok := <-oFsm.pAdaptFsm.commChan
311 if !ok {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000312 logger.Info(ctx, "LockStateFsm Rx Msg - could not read from channel", log.Fields{"device-id": oFsm.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530313 // but then we have to ensure a restart of the FSM as well - as exceptional procedure
314 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvRestart)
315 break loop
316 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000317 logger.Debugw(ctx, "LockStateFsm Rx Msg", log.Fields{"device-id": oFsm.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530318
319 switch message.Type {
320 case TestMsg:
321 msg, _ := message.Data.(TestMessage)
322 if msg.TestMessageVal == AbortMessageProcessing {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000323 logger.Debugw(ctx, "LockStateFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000324 break loop
325 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000326 logger.Warnw(ctx, "LockStateFsm unknown TestMessage", log.Fields{"device-id": oFsm.deviceID, "MessageVal": msg.TestMessageVal})
Himani Chawla4d908332020-08-31 12:30:20 +0530327 case OMCI:
328 msg, _ := message.Data.(OmciMessage)
dbainbri4d3a0dc2020-12-02 00:33:42 +0000329 oFsm.handleOmciLockStateMessage(ctx, msg)
Himani Chawla4d908332020-08-31 12:30:20 +0530330 default:
dbainbri4d3a0dc2020-12-02 00:33:42 +0000331 logger.Warn(ctx, "LockStateFsm Rx unknown message", log.Fields{"device-id": oFsm.deviceID,
Himani Chawla4d908332020-08-31 12:30:20 +0530332 "message.Type": message.Type})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000333 }
334 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000335 logger.Debugw(ctx, "End LockStateFsm Msg processing", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000336}
337
dbainbri4d3a0dc2020-12-02 00:33:42 +0000338func (oFsm *lockStateFsm) handleOmciLockStateMessage(ctx context.Context, msg OmciMessage) {
339 logger.Debugw(ctx, "Rx OMCI LockStateFsm Msg", log.Fields{"device-id": oFsm.deviceID,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000340 "msgType": msg.OmciMsg.MessageType})
341
342 if msg.OmciMsg.MessageType == omci.SetResponseType {
343 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeSetResponse)
344 if msgLayer == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000345 logger.Errorw(ctx, "LockStateFsm - Omci Msg layer could not be detected for SetResponse",
mpagenko01e726e2020-10-23 09:45:29 +0000346 log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000347 return
348 }
349 msgObj, msgOk := msgLayer.(*omci.SetResponse)
350 if !msgOk {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000351 logger.Errorw(ctx, "LockStateFsm - Omci Msg layer could not be assigned for SetResponse",
mpagenko01e726e2020-10-23 09:45:29 +0000352 log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000353 return
354 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000355 logger.Debugw(ctx, "LockStateFsm SetResponse Data", log.Fields{"device-id": oFsm.deviceID, "data-fields": msgObj})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000356 if msgObj.Result != me.Success {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000357 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 +0000358 // possibly force FSM into abort or ignore some errors for some messages? store error for mgmt display?
359 return
360 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000361
mpagenkob06684f2021-03-23 14:39:04 +0000362 //should never appear, left here for robustness
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000363 oFsm.mutexPLastTxMeInstance.RLock()
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700364 if oFsm.pLastTxMeInstance != nil {
365 // compare comments above for CreateResponse (apply also here ...)
366 if msgObj.EntityClass == oFsm.pLastTxMeInstance.GetClassID() &&
367 msgObj.EntityInstance == oFsm.pLastTxMeInstance.GetEntityID() {
368 //store the created ME into DB //TODO??? obviously the Python code does not store the config ...
369 // if, then something like:
370 //oFsm.pOnuDB.StoreMe(msgObj)
371
372 switch oFsm.pLastTxMeInstance.GetName() {
373 case "OnuG":
374 { // let the FSM proceed ...
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000375 oFsm.mutexPLastTxMeInstance.RUnlock()
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700376 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvRxOnugResp)
377 }
378 case "PhysicalPathTerminationPointEthernetUni", "VirtualEthernetInterfacePoint":
379 { // let the PPTP init proceed by stopping the wait function
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000380 oFsm.mutexPLastTxMeInstance.RUnlock()
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700381 oFsm.omciLockResponseReceived <- true
382 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000383 default:
384 {
385 logger.Warnw(ctx, "Unsupported ME name received!",
386 log.Fields{"ME name": oFsm.pLastTxMeInstance.GetName(), "device-id": oFsm.deviceID})
387 oFsm.mutexPLastTxMeInstance.RUnlock()
388 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000389 }
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700390 } else {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000391 oFsm.mutexPLastTxMeInstance.RUnlock()
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700392 logger.Warnf(ctx, "LockStateFsm - Received SetResponse Data for %s with wrong classID or entityID ",
393 log.Fields{"device-id": oFsm.deviceID, "data-fields": msgObj}, msgObj.EntityClass)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000394 }
Matteo Scandolob250bec2021-01-27 17:35:45 -0800395 } else {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000396 oFsm.mutexPLastTxMeInstance.RUnlock()
mpagenkob06684f2021-03-23 14:39:04 +0000397 logger.Errorw(ctx, "pLastTxMeInstance is nil", log.Fields{"device-id": oFsm.deviceID})
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700398 return
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000399 }
400 } else {
mpagenkob06684f2021-03-23 14:39:04 +0000401 logger.Errorw(ctx, "LockStateFsm - Rx OMCI unhandled MsgType", log.Fields{"omciMsgType": msg.OmciMsg.MessageType})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000402 return
403 }
404}
405
dbainbri4d3a0dc2020-12-02 00:33:42 +0000406func (oFsm *lockStateFsm) performUniPortAdminSet(ctx context.Context) {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000407 var omciAdminState uint8 = 1 //default locked
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000408 oFsm.mutexAdminState.RLock()
Himani Chawla4d908332020-08-31 12:30:20 +0530409 if !oFsm.adminState {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000410 omciAdminState = 0
411 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000412 oFsm.mutexAdminState.RUnlock()
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000413 //set PPTPEthUni or VEIP AdminState
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000414 requestedAttributes := me.AttributeValueMap{"AdministrativeState": omciAdminState}
415
416 for uniNo, uniPort := range oFsm.pOmciCC.pBaseDeviceHandler.uniEntityMap {
mpagenko8b5fdd22020-12-17 17:58:32 +0000417 // only unlock the UniPort in case it is defined for usage (R2.6 limit only one port),
418 // compare also limitation for logical voltha port in dh.enableUniPortStateUpdate()
Matteo Scandolo20d180c2021-06-10 17:20:21 +0200419
420 if (omciAdminState == 1) || (1<<uniPort.uniID)&oFsm.pDeviceHandler.pOpenOnuAc.config.UniPortMask == (1<<uniPort.uniID) {
mpagenko8b5fdd22020-12-17 17:58:32 +0000421 var meInstance *me.ManagedEntity
422 if uniPort.portType == uniPPTP {
423 logger.Debugw(ctx, "Setting PPTP admin state", log.Fields{
424 "device-id": oFsm.deviceID, "for PortNo": uniNo, "state (0-unlock)": omciAdminState})
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000425 oFsm.mutexPLastTxMeInstance.Lock()
ozgecanetsiab36ed572021-04-01 10:38:48 +0300426 meInstance, err := oFsm.pOmciCC.sendSetPptpEthUniLS(log.WithSpanFromContext(context.TODO(), ctx),
427 uniPort.entityID, oFsm.pDeviceHandler.pOpenOnuAc.omciTimeout,
mpagenko8b5fdd22020-12-17 17:58:32 +0000428 true, requestedAttributes, oFsm.pAdaptFsm.commChan)
ozgecanetsiab36ed572021-04-01 10:38:48 +0300429 if err != nil {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000430 oFsm.mutexPLastTxMeInstance.Unlock()
ozgecanetsiab36ed572021-04-01 10:38:48 +0300431 logger.Errorw(ctx, "SetPptpEthUniLS set failed, aborting LockStateFsm!",
432 log.Fields{"device-id": oFsm.deviceID})
433 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
434 return
435 }
mpagenko8b5fdd22020-12-17 17:58:32 +0000436 oFsm.pLastTxMeInstance = meInstance
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000437 oFsm.mutexPLastTxMeInstance.Unlock()
mpagenko8b5fdd22020-12-17 17:58:32 +0000438 } else if uniPort.portType == uniVEIP {
439 logger.Debugw(ctx, "Setting VEIP admin state", log.Fields{
440 "device-id": oFsm.deviceID, "for PortNo": uniNo, "state (0-unlock)": omciAdminState})
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000441 oFsm.mutexPLastTxMeInstance.Lock()
ozgecanetsiab36ed572021-04-01 10:38:48 +0300442 meInstance, err := oFsm.pOmciCC.sendSetVeipLS(log.WithSpanFromContext(context.TODO(), ctx),
443 uniPort.entityID, oFsm.pDeviceHandler.pOpenOnuAc.omciTimeout,
mpagenko8b5fdd22020-12-17 17:58:32 +0000444 true, requestedAttributes, oFsm.pAdaptFsm.commChan)
ozgecanetsiab36ed572021-04-01 10:38:48 +0300445 if err != nil {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000446 oFsm.mutexPLastTxMeInstance.Unlock()
ozgecanetsiab36ed572021-04-01 10:38:48 +0300447 logger.Errorw(ctx, "SetVeipLS set failed, aborting LockStateFsm!",
448 log.Fields{"device-id": oFsm.deviceID})
449 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
450 return
451 }
mpagenko8b5fdd22020-12-17 17:58:32 +0000452 oFsm.pLastTxMeInstance = meInstance
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000453 oFsm.mutexPLastTxMeInstance.Unlock()
mpagenko8b5fdd22020-12-17 17:58:32 +0000454 } else {
455 logger.Warnw(ctx, "Unsupported UniTP type - skip",
456 log.Fields{"device-id": oFsm.deviceID, "Port": uniNo})
457 continue
458 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000459 oFsm.mutexPLastTxMeInstance.RLock()
mpagenkob06684f2021-03-23 14:39:04 +0000460 if oFsm.pLastTxMeInstance == nil {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000461 oFsm.mutexPLastTxMeInstance.RUnlock()
mpagenkob06684f2021-03-23 14:39:04 +0000462 logger.Errorw(ctx, "could not send PortAdmin OMCI message from LockStateFsm", log.Fields{
463 "device-id": oFsm.deviceID, "Port": uniNo})
464 //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
465 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
466 return
467 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000468 oFsm.mutexPLastTxMeInstance.RUnlock()
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000469
mpagenko8b5fdd22020-12-17 17:58:32 +0000470 //verify response
471 err := oFsm.waitforOmciResponse(ctx, meInstance)
472 if err != nil {
473 logger.Errorw(ctx, "UniTP Admin State set failed, aborting LockState set!",
474 log.Fields{"device-id": oFsm.deviceID, "Port": uniNo})
475 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
476 return
477 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000478 }
479 } //for all UNI ports
480 // if Config has been done for all UNI related instances let the FSM proceed
481 // while we did not check here, if there is some port at all - !?
mpagenko8b5fdd22020-12-17 17:58:32 +0000482 logger.Infow(ctx, "UniTP adminState loop finished", log.Fields{"device-id": oFsm.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530483 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvRxUnisResp)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000484}
485
dbainbri4d3a0dc2020-12-02 00:33:42 +0000486func (oFsm *lockStateFsm) waitforOmciResponse(ctx context.Context, apMeInstance *me.ManagedEntity) error {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000487 select {
Himani Chawla4d908332020-08-31 12:30:20 +0530488 // maybe be also some outside cancel (but no context modeled for the moment ...)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000489 // case <-ctx.Done():
dbainbri4d3a0dc2020-12-02 00:33:42 +0000490 // logger.Infow(ctx,"LockState-bridge-init message reception canceled", log.Fields{"for device-id": oFsm.deviceID})
Holger Hildebrandt366ef192021-05-05 11:07:44 +0000491 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 +0000492 logger.Warnw(ctx, "LockStateFSM uni-set timeout", log.Fields{"for device-id": oFsm.deviceID})
mpagenko01e726e2020-10-23 09:45:29 +0000493 return fmt.Errorf("lockStateFsm uni-set timeout for device-id %s", oFsm.deviceID)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000494 case success := <-oFsm.omciLockResponseReceived:
Himani Chawla4d908332020-08-31 12:30:20 +0530495 if success {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000496 logger.Debug(ctx, "LockStateFSM uni-set response received")
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000497 return nil
498 }
499 // should not happen so far
dbainbri4d3a0dc2020-12-02 00:33:42 +0000500 logger.Warnw(ctx, "LockStateFSM uni-set response error", log.Fields{"for device-id": oFsm.deviceID})
mpagenko01e726e2020-10-23 09:45:29 +0000501 return fmt.Errorf("lockStateFsm uni-set responseError for device-id %s", oFsm.deviceID)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000502 }
503}