blob: 2ceea252a3227f30ce49846db304b8ac95e5b9b4 [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"
khenaidoo7d3c5582021-08-11 18:09:44 -040030 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000031)
32
Himani Chawla6d2ae152020-09-02 13:11:20 +053033//lockStateFsm defines the structure for the state machine to lock/unlock the ONU UNI ports via OMCI
34type lockStateFsm struct {
Holger Hildebrandt8165eda2020-09-24 09:39:24 +000035 pDeviceHandler *deviceHandler
mpagenko01e726e2020-10-23 09:45:29 +000036 deviceID string
Himani Chawla6d2ae152020-09-02 13:11:20 +053037 pOmciCC *omciCC
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +000038 mutexAdminState sync.RWMutex
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
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +000043 mutexPLastTxMeInstance sync.RWMutex
mpagenko01e726e2020-10-23 09:45:29 +000044 pLastTxMeInstance *me.ManagedEntity
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000045}
46
mpagenko1cc3cb42020-07-27 15:24:38 +000047const (
48 // events of lock/unlock UNI port FSM
49 uniEvStart = "uniEvStart"
50 uniEvStartAdmin = "uniEvStartAdmin"
51 uniEvRxUnisResp = "uniEvRxUnisResp"
52 uniEvRxOnugResp = "uniEvRxOnugResp"
53 uniEvTimeoutSimple = "uniEvTimeoutSimple"
54 uniEvTimeoutUnis = "uniEvTimeoutUnis"
55 uniEvReset = "uniEvReset"
56 uniEvRestart = "uniEvRestart"
57)
58const (
59 // states of lock/unlock UNI port FSM
60 uniStDisabled = "uniStDisabled"
61 uniStStarting = "uniStStarting"
62 uniStSettingUnis = "uniStSettingUnis"
63 uniStSettingOnuG = "uniStSettingOnuG"
64 uniStAdminDone = "uniStAdminDone"
65 uniStResetting = "uniStResetting"
66)
Holger Hildebrandt10d98192021-01-27 15:29:31 +000067const cUniFsmIdleState = uniStDisabled
mpagenko1cc3cb42020-07-27 15:24:38 +000068
Himani Chawla6d2ae152020-09-02 13:11:20 +053069//newLockStateFsm is the 'constructor' for the state machine to lock/unlock the ONU UNI ports via OMCI
dbainbri4d3a0dc2020-12-02 00:33:42 +000070func newLockStateFsm(ctx context.Context, apDevOmciCC *omciCC, aAdminState bool, aRequestEvent OnuDeviceEvent,
Holger Hildebrandt8165eda2020-09-24 09:39:24 +000071 aName string, apDeviceHandler *deviceHandler, aCommChannel chan Message) *lockStateFsm {
Himani Chawla6d2ae152020-09-02 13:11:20 +053072 instFsm := &lockStateFsm{
Holger Hildebrandt8165eda2020-09-24 09:39:24 +000073 pDeviceHandler: apDeviceHandler,
mpagenko01e726e2020-10-23 09:45:29 +000074 deviceID: apDeviceHandler.deviceID,
Holger Hildebrandt8165eda2020-09-24 09:39:24 +000075 pOmciCC: apDevOmciCC,
76 adminState: aAdminState,
77 requestEvent: aRequestEvent,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000078 }
mpagenko01e726e2020-10-23 09:45:29 +000079 instFsm.pAdaptFsm = NewAdapterFsm(aName, instFsm.deviceID, aCommChannel)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000080 if instFsm.pAdaptFsm == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +000081 logger.Errorw(ctx, "LockStateFsm's AdapterFsm could not be instantiated!!", log.Fields{
mpagenko01e726e2020-10-23 09:45:29 +000082 "device-id": instFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000083 return nil
84 }
Himani Chawla4d908332020-08-31 12:30:20 +053085 if aAdminState { //port locking requested
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000086 instFsm.pAdaptFsm.pFsm = fsm.NewFSM(
mpagenko1cc3cb42020-07-27 15:24:38 +000087 uniStDisabled,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000088 fsm.Events{
89
mpagenko1cc3cb42020-07-27 15:24:38 +000090 {Name: uniEvStart, Src: []string{uniStDisabled}, Dst: uniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000091
mpagenko1cc3cb42020-07-27 15:24:38 +000092 {Name: uniEvStartAdmin, Src: []string{uniStStarting}, Dst: uniStSettingUnis},
93 // the settingUnis state is used for multi ME config for all UNI related ports
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000094 // maybe such could be reflected in the state machine as well (port number parametrized)
95 // but that looks not straightforward here - so we keep it simple here for the beginning(?)
mpagenko1cc3cb42020-07-27 15:24:38 +000096 {Name: uniEvRxUnisResp, Src: []string{uniStSettingUnis}, Dst: uniStSettingOnuG},
97 {Name: uniEvRxOnugResp, Src: []string{uniStSettingOnuG}, Dst: uniStAdminDone},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000098
mpagenko1cc3cb42020-07-27 15:24:38 +000099 {Name: uniEvTimeoutSimple, Src: []string{uniStSettingOnuG}, Dst: uniStStarting},
100 {Name: uniEvTimeoutUnis, Src: []string{uniStSettingUnis}, Dst: uniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000101
mpagenko1cc3cb42020-07-27 15:24:38 +0000102 {Name: uniEvReset, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis,
103 uniStAdminDone}, Dst: uniStResetting},
104 // exceptional treatment for all states except uniStResetting
105 {Name: uniEvRestart, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis,
106 uniStAdminDone, uniStResetting}, Dst: uniStDisabled},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000107 },
108
109 fsm.Callbacks{
dbainbri4d3a0dc2020-12-02 00:33:42 +0000110 "enter_state": func(e *fsm.Event) { instFsm.pAdaptFsm.logFsmStateChange(ctx, e) },
111 ("enter_" + uniStStarting): func(e *fsm.Event) { instFsm.enterAdminStartingState(ctx, e) },
112 ("enter_" + uniStSettingOnuG): func(e *fsm.Event) { instFsm.enterSettingOnuGState(ctx, e) },
113 ("enter_" + uniStSettingUnis): func(e *fsm.Event) { instFsm.enterSettingUnisState(ctx, e) },
114 ("enter_" + uniStAdminDone): func(e *fsm.Event) { instFsm.enterAdminDoneState(ctx, e) },
115 ("enter_" + uniStResetting): func(e *fsm.Event) { instFsm.enterResettingState(ctx, e) },
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000116 },
117 )
118 } else { //port unlocking requested
119 instFsm.pAdaptFsm.pFsm = fsm.NewFSM(
mpagenko1cc3cb42020-07-27 15:24:38 +0000120 uniStDisabled,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000121 fsm.Events{
122
mpagenko1cc3cb42020-07-27 15:24:38 +0000123 {Name: uniEvStart, Src: []string{uniStDisabled}, Dst: uniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000124
mpagenko1cc3cb42020-07-27 15:24:38 +0000125 {Name: uniEvStartAdmin, Src: []string{uniStStarting}, Dst: uniStSettingOnuG},
126 {Name: uniEvRxOnugResp, Src: []string{uniStSettingOnuG}, Dst: uniStSettingUnis},
127 // the settingUnis state is used for multi ME config for all UNI related ports
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000128 // maybe such could be reflected in the state machine as well (port number parametrized)
129 // but that looks not straightforward here - so we keep it simple here for the beginning(?)
mpagenko1cc3cb42020-07-27 15:24:38 +0000130 {Name: uniEvRxUnisResp, Src: []string{uniStSettingUnis}, Dst: uniStAdminDone},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000131
mpagenko1cc3cb42020-07-27 15:24:38 +0000132 {Name: uniEvTimeoutSimple, Src: []string{uniStSettingOnuG}, Dst: uniStStarting},
133 {Name: uniEvTimeoutUnis, Src: []string{uniStSettingUnis}, Dst: uniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000134
mpagenko1cc3cb42020-07-27 15:24:38 +0000135 {Name: uniEvReset, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis,
136 uniStAdminDone}, Dst: uniStResetting},
137 // exceptional treatment for all states except uniStResetting
138 {Name: uniEvRestart, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis,
139 uniStAdminDone, uniStResetting}, Dst: uniStDisabled},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000140 },
141
142 fsm.Callbacks{
dbainbri4d3a0dc2020-12-02 00:33:42 +0000143 "enter_state": func(e *fsm.Event) { instFsm.pAdaptFsm.logFsmStateChange(ctx, e) },
144 ("enter_" + uniStStarting): func(e *fsm.Event) { instFsm.enterAdminStartingState(ctx, e) },
145 ("enter_" + uniStSettingOnuG): func(e *fsm.Event) { instFsm.enterSettingOnuGState(ctx, e) },
146 ("enter_" + uniStSettingUnis): func(e *fsm.Event) { instFsm.enterSettingUnisState(ctx, e) },
147 ("enter_" + uniStAdminDone): func(e *fsm.Event) { instFsm.enterAdminDoneState(ctx, e) },
148 ("enter_" + uniStResetting): func(e *fsm.Event) { instFsm.enterResettingState(ctx, e) },
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000149 },
150 )
151 }
152 if instFsm.pAdaptFsm.pFsm == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000153 logger.Errorw(ctx, "LockStateFsm's Base FSM could not be instantiated!!", log.Fields{
mpagenko01e726e2020-10-23 09:45:29 +0000154 "device-id": instFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000155 return nil
156 }
157
dbainbri4d3a0dc2020-12-02 00:33:42 +0000158 logger.Debugw(ctx, "LockStateFsm created", log.Fields{"device-id": instFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000159 return instFsm
160}
161
Himani Chawla6d2ae152020-09-02 13:11:20 +0530162//setSuccessEvent modifies the requested event notified on success
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000163//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 +0530164func (oFsm *lockStateFsm) setSuccessEvent(aEvent OnuDeviceEvent) {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000165 oFsm.requestEvent = aEvent
166}
167
dbainbri4d3a0dc2020-12-02 00:33:42 +0000168func (oFsm *lockStateFsm) enterAdminStartingState(ctx context.Context, e *fsm.Event) {
169 logger.Debugw(ctx, "LockStateFSM start", log.Fields{"in state": e.FSM.Current(),
mpagenko01e726e2020-10-23 09:45:29 +0000170 "device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000171 // in case the used channel is not yet defined (can be re-used after restarts)
172 if oFsm.omciLockResponseReceived == nil {
173 oFsm.omciLockResponseReceived = make(chan bool)
dbainbri4d3a0dc2020-12-02 00:33:42 +0000174 logger.Debug(ctx, "LockStateFSM - OMCI UniLock RxChannel defined")
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000175 } else {
176 // as we may 're-use' this instance of FSM and the connected channel
mpagenkob06684f2021-03-23 14:39:04 +0000177 // make sure there is no 'lingering' request in the already existing channels:
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000178 // (simple loop sufficient as we are the only receiver)
179 for len(oFsm.omciLockResponseReceived) > 0 {
180 <-oFsm.omciLockResponseReceived
181 }
mpagenkob06684f2021-03-23 14:39:04 +0000182 for len(oFsm.pAdaptFsm.commChan) > 0 {
183 <-oFsm.pAdaptFsm.commChan
184 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000185 }
186 // start go routine for processing of LockState messages
dbainbri4d3a0dc2020-12-02 00:33:42 +0000187 go oFsm.processOmciLockMessages(ctx)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000188
189 //let the state machine run forward from here directly
190 pLockStateAFsm := oFsm.pAdaptFsm
191 if pLockStateAFsm != nil {
192 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
193 go func(a_pAFsm *AdapterFsm) {
194 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
Himani Chawla4d908332020-08-31 12:30:20 +0530195 _ = a_pAFsm.pFsm.Event(uniEvStartAdmin)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000196 }
197 }(pLockStateAFsm)
198 }
199}
200
dbainbri4d3a0dc2020-12-02 00:33:42 +0000201func (oFsm *lockStateFsm) enterSettingOnuGState(ctx context.Context, e *fsm.Event) {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000202 var omciAdminState uint8 = 1 //default locked
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000203 oFsm.mutexAdminState.RLock()
Himani Chawla4d908332020-08-31 12:30:20 +0530204 if !oFsm.adminState {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000205 omciAdminState = 0
206 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000207 oFsm.mutexAdminState.RUnlock()
dbainbri4d3a0dc2020-12-02 00:33:42 +0000208 logger.Debugw(ctx, "LockStateFSM Tx Set::ONU-G:admin", log.Fields{
mpagenko01e726e2020-10-23 09:45:29 +0000209 "omciAdmin": omciAdminState, "in state": e.FSM.Current(), "device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000210 requestedAttributes := me.AttributeValueMap{"AdministrativeState": omciAdminState}
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000211 oFsm.mutexPLastTxMeInstance.Lock()
ozgecanetsiab36ed572021-04-01 10:38:48 +0300212 meInstance, err := oFsm.pOmciCC.sendSetOnuGLS(log.WithSpanFromContext(context.TODO(), ctx), oFsm.pDeviceHandler.pOpenOnuAc.omciTimeout, true,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000213 requestedAttributes, oFsm.pAdaptFsm.commChan)
ozgecanetsiab36ed572021-04-01 10:38:48 +0300214 if err != nil {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000215 oFsm.mutexPLastTxMeInstance.Unlock()
ozgecanetsiab36ed572021-04-01 10:38:48 +0300216 logger.Errorw(ctx, "OnuGLS set failed, aborting LockStateFSM", log.Fields{"device-id": oFsm.deviceID})
217 pLockStateAFsm := oFsm.pAdaptFsm
218 if pLockStateAFsm != nil {
219 go func(a_pAFsm *AdapterFsm) {
220 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
221 _ = a_pAFsm.pFsm.Event(uniEvReset)
222 }
223 }(pLockStateAFsm)
224 }
225 return
226 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000227 //accept also nil as (error) return value for writing to LastTx
228 // - this avoids misinterpretation of new received OMCI messages
mpagenko01e726e2020-10-23 09:45:29 +0000229 oFsm.pLastTxMeInstance = meInstance
mpagenkob06684f2021-03-23 14:39:04 +0000230 if oFsm.pLastTxMeInstance == nil {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000231 oFsm.mutexPLastTxMeInstance.Unlock()
mpagenkob06684f2021-03-23 14:39:04 +0000232 logger.Errorw(ctx, "could not send OMCI message from LockStateFsm", log.Fields{
233 "device-id": oFsm.deviceID})
234 //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
235 pLockStateAFsm := oFsm.pAdaptFsm
236 if pLockStateAFsm != nil {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000237
mpagenkob06684f2021-03-23 14:39:04 +0000238 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
239 go func(a_pAFsm *AdapterFsm) {
240 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
241 _ = a_pAFsm.pFsm.Event(uniEvReset)
242 }
243 }(pLockStateAFsm)
244 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000245 return
mpagenkob06684f2021-03-23 14:39:04 +0000246 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000247 oFsm.mutexPLastTxMeInstance.Unlock()
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000248}
249
dbainbri4d3a0dc2020-12-02 00:33:42 +0000250func (oFsm *lockStateFsm) enterSettingUnisState(ctx context.Context, e *fsm.Event) {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000251 oFsm.mutexAdminState.RLock()
mpagenko8b5fdd22020-12-17 17:58:32 +0000252 logger.Debugw(ctx, "LockStateFSM - starting UniTP adminState loop", log.Fields{
mpagenko01e726e2020-10-23 09:45:29 +0000253 "in state": e.FSM.Current(), "device-id": oFsm.deviceID, "LockState": oFsm.adminState})
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000254 oFsm.mutexAdminState.RUnlock()
dbainbri4d3a0dc2020-12-02 00:33:42 +0000255 go oFsm.performUniPortAdminSet(ctx)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000256}
257
dbainbri4d3a0dc2020-12-02 00:33:42 +0000258func (oFsm *lockStateFsm) enterAdminDoneState(ctx context.Context, e *fsm.Event) {
259 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 +0000260 //use DeviceHandler event notification directly, no need/support to update DeviceEntryState for lock/unlock
dbainbri4d3a0dc2020-12-02 00:33:42 +0000261 oFsm.pDeviceHandler.deviceProcStatusUpdate(ctx, oFsm.requestEvent)
Holger Hildebrandt8165eda2020-09-24 09:39:24 +0000262
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000263 //let's reset the state machine in order to release all resources now
264 pLockStateAFsm := oFsm.pAdaptFsm
265 if pLockStateAFsm != nil {
266 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
267 go func(a_pAFsm *AdapterFsm) {
268 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
Himani Chawla4d908332020-08-31 12:30:20 +0530269 _ = a_pAFsm.pFsm.Event(uniEvReset)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000270 }
271 }(pLockStateAFsm)
272 }
273}
274
dbainbri4d3a0dc2020-12-02 00:33:42 +0000275func (oFsm *lockStateFsm) enterResettingState(ctx context.Context, e *fsm.Event) {
276 logger.Debugw(ctx, "LockStateFSM resetting", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000277 pLockStateAFsm := oFsm.pAdaptFsm
278 if pLockStateAFsm != nil {
279 // abort running message processing
280 fsmAbortMsg := Message{
281 Type: TestMsg,
282 Data: TestMessage{
283 TestMessageVal: AbortMessageProcessing,
284 },
285 }
286 pLockStateAFsm.commChan <- fsmAbortMsg
287
288 //try to restart the FSM to 'disabled'
289 // see DownloadedState: decouple event transfer
290 go func(a_pAFsm *AdapterFsm) {
291 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
Himani Chawla4d908332020-08-31 12:30:20 +0530292 _ = a_pAFsm.pFsm.Event(uniEvRestart)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000293 }
294 }(pLockStateAFsm)
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000295 oFsm.mutexPLastTxMeInstance.Lock()
mpagenko01e726e2020-10-23 09:45:29 +0000296 oFsm.pLastTxMeInstance = nil
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000297 oFsm.mutexPLastTxMeInstance.Unlock()
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000298 }
299}
300
dbainbri4d3a0dc2020-12-02 00:33:42 +0000301func (oFsm *lockStateFsm) processOmciLockMessages(ctx context.Context) {
302 logger.Debugw(ctx, "Start LockStateFsm Msg processing", log.Fields{"for device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000303loop:
304 for {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000305 // case <-ctx.Done():
dbainbri4d3a0dc2020-12-02 00:33:42 +0000306 // logger.Info(ctx,"MibSync Msg", log.Fields{"Message handling canceled via context for device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000307 // break loop
Himani Chawla4d908332020-08-31 12:30:20 +0530308 message, ok := <-oFsm.pAdaptFsm.commChan
309 if !ok {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000310 logger.Info(ctx, "LockStateFsm Rx Msg - could not read from channel", log.Fields{"device-id": oFsm.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530311 // but then we have to ensure a restart of the FSM as well - as exceptional procedure
312 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvRestart)
313 break loop
314 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000315 logger.Debugw(ctx, "LockStateFsm Rx Msg", log.Fields{"device-id": oFsm.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530316
317 switch message.Type {
318 case TestMsg:
319 msg, _ := message.Data.(TestMessage)
320 if msg.TestMessageVal == AbortMessageProcessing {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000321 logger.Debugw(ctx, "LockStateFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000322 break loop
323 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000324 logger.Warnw(ctx, "LockStateFsm unknown TestMessage", log.Fields{"device-id": oFsm.deviceID, "MessageVal": msg.TestMessageVal})
Himani Chawla4d908332020-08-31 12:30:20 +0530325 case OMCI:
326 msg, _ := message.Data.(OmciMessage)
dbainbri4d3a0dc2020-12-02 00:33:42 +0000327 oFsm.handleOmciLockStateMessage(ctx, msg)
Himani Chawla4d908332020-08-31 12:30:20 +0530328 default:
dbainbri4d3a0dc2020-12-02 00:33:42 +0000329 logger.Warn(ctx, "LockStateFsm Rx unknown message", log.Fields{"device-id": oFsm.deviceID,
Himani Chawla4d908332020-08-31 12:30:20 +0530330 "message.Type": message.Type})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000331 }
332 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000333 logger.Debugw(ctx, "End LockStateFsm Msg processing", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000334}
335
dbainbri4d3a0dc2020-12-02 00:33:42 +0000336func (oFsm *lockStateFsm) handleOmciLockStateMessage(ctx context.Context, msg OmciMessage) {
337 logger.Debugw(ctx, "Rx OMCI LockStateFsm Msg", log.Fields{"device-id": oFsm.deviceID,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000338 "msgType": msg.OmciMsg.MessageType})
339
340 if msg.OmciMsg.MessageType == omci.SetResponseType {
341 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeSetResponse)
342 if msgLayer == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000343 logger.Errorw(ctx, "LockStateFsm - Omci Msg layer could not be detected for SetResponse",
mpagenko01e726e2020-10-23 09:45:29 +0000344 log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000345 return
346 }
347 msgObj, msgOk := msgLayer.(*omci.SetResponse)
348 if !msgOk {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000349 logger.Errorw(ctx, "LockStateFsm - Omci Msg layer could not be assigned for SetResponse",
mpagenko01e726e2020-10-23 09:45:29 +0000350 log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000351 return
352 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000353 logger.Debugw(ctx, "LockStateFsm SetResponse Data", log.Fields{"device-id": oFsm.deviceID, "data-fields": msgObj})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000354 if msgObj.Result != me.Success {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000355 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 +0000356 // possibly force FSM into abort or ignore some errors for some messages? store error for mgmt display?
357 return
358 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000359
mpagenkob06684f2021-03-23 14:39:04 +0000360 //should never appear, left here for robustness
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000361 oFsm.mutexPLastTxMeInstance.RLock()
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700362 if oFsm.pLastTxMeInstance != nil {
363 // compare comments above for CreateResponse (apply also here ...)
364 if msgObj.EntityClass == oFsm.pLastTxMeInstance.GetClassID() &&
365 msgObj.EntityInstance == oFsm.pLastTxMeInstance.GetEntityID() {
366 //store the created ME into DB //TODO??? obviously the Python code does not store the config ...
367 // if, then something like:
368 //oFsm.pOnuDB.StoreMe(msgObj)
369
370 switch oFsm.pLastTxMeInstance.GetName() {
371 case "OnuG":
372 { // let the FSM proceed ...
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000373 oFsm.mutexPLastTxMeInstance.RUnlock()
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700374 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvRxOnugResp)
375 }
376 case "PhysicalPathTerminationPointEthernetUni", "VirtualEthernetInterfacePoint":
377 { // let the PPTP init proceed by stopping the wait function
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000378 oFsm.mutexPLastTxMeInstance.RUnlock()
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700379 oFsm.omciLockResponseReceived <- true
380 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000381 default:
382 {
383 logger.Warnw(ctx, "Unsupported ME name received!",
384 log.Fields{"ME name": oFsm.pLastTxMeInstance.GetName(), "device-id": oFsm.deviceID})
385 oFsm.mutexPLastTxMeInstance.RUnlock()
386 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000387 }
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700388 } else {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000389 oFsm.mutexPLastTxMeInstance.RUnlock()
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700390 logger.Warnf(ctx, "LockStateFsm - Received SetResponse Data for %s with wrong classID or entityID ",
391 log.Fields{"device-id": oFsm.deviceID, "data-fields": msgObj}, msgObj.EntityClass)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000392 }
Matteo Scandolob250bec2021-01-27 17:35:45 -0800393 } else {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000394 oFsm.mutexPLastTxMeInstance.RUnlock()
mpagenkob06684f2021-03-23 14:39:04 +0000395 logger.Errorw(ctx, "pLastTxMeInstance is nil", log.Fields{"device-id": oFsm.deviceID})
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700396 return
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000397 }
398 } else {
mpagenkob06684f2021-03-23 14:39:04 +0000399 logger.Errorw(ctx, "LockStateFsm - Rx OMCI unhandled MsgType", log.Fields{"omciMsgType": msg.OmciMsg.MessageType})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000400 return
401 }
402}
403
dbainbri4d3a0dc2020-12-02 00:33:42 +0000404func (oFsm *lockStateFsm) performUniPortAdminSet(ctx context.Context) {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000405 var omciAdminState uint8 = 1 //default locked
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000406 oFsm.mutexAdminState.RLock()
Himani Chawla4d908332020-08-31 12:30:20 +0530407 if !oFsm.adminState {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000408 omciAdminState = 0
409 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000410 oFsm.mutexAdminState.RUnlock()
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000411 //set PPTPEthUni or VEIP AdminState
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000412 requestedAttributes := me.AttributeValueMap{"AdministrativeState": omciAdminState}
413
414 for uniNo, uniPort := range oFsm.pOmciCC.pBaseDeviceHandler.uniEntityMap {
mpagenko8b5fdd22020-12-17 17:58:32 +0000415 // only unlock the UniPort in case it is defined for usage (R2.6 limit only one port),
416 // compare also limitation for logical voltha port in dh.enableUniPortStateUpdate()
Matteo Scandolo20d180c2021-06-10 17:20:21 +0200417
418 if (omciAdminState == 1) || (1<<uniPort.uniID)&oFsm.pDeviceHandler.pOpenOnuAc.config.UniPortMask == (1<<uniPort.uniID) {
mpagenko8b5fdd22020-12-17 17:58:32 +0000419 var meInstance *me.ManagedEntity
420 if uniPort.portType == uniPPTP {
421 logger.Debugw(ctx, "Setting PPTP admin state", log.Fields{
422 "device-id": oFsm.deviceID, "for PortNo": uniNo, "state (0-unlock)": omciAdminState})
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000423 oFsm.mutexPLastTxMeInstance.Lock()
ozgecanetsiab36ed572021-04-01 10:38:48 +0300424 meInstance, err := oFsm.pOmciCC.sendSetPptpEthUniLS(log.WithSpanFromContext(context.TODO(), ctx),
425 uniPort.entityID, oFsm.pDeviceHandler.pOpenOnuAc.omciTimeout,
mpagenko8b5fdd22020-12-17 17:58:32 +0000426 true, requestedAttributes, oFsm.pAdaptFsm.commChan)
ozgecanetsiab36ed572021-04-01 10:38:48 +0300427 if err != nil {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000428 oFsm.mutexPLastTxMeInstance.Unlock()
ozgecanetsiab36ed572021-04-01 10:38:48 +0300429 logger.Errorw(ctx, "SetPptpEthUniLS set failed, aborting LockStateFsm!",
430 log.Fields{"device-id": oFsm.deviceID})
431 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
432 return
433 }
mpagenko8b5fdd22020-12-17 17:58:32 +0000434 oFsm.pLastTxMeInstance = meInstance
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000435 oFsm.mutexPLastTxMeInstance.Unlock()
mpagenko8b5fdd22020-12-17 17:58:32 +0000436 } else if uniPort.portType == uniVEIP {
437 logger.Debugw(ctx, "Setting VEIP 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.sendSetVeipLS(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, "SetVeipLS set failed, aborting LockStateFsm!",
446 log.Fields{"device-id": oFsm.deviceID})
447 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
448 return
449 }
mpagenko8b5fdd22020-12-17 17:58:32 +0000450 oFsm.pLastTxMeInstance = meInstance
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000451 oFsm.mutexPLastTxMeInstance.Unlock()
mpagenko8b5fdd22020-12-17 17:58:32 +0000452 } else {
453 logger.Warnw(ctx, "Unsupported UniTP type - skip",
454 log.Fields{"device-id": oFsm.deviceID, "Port": uniNo})
455 continue
456 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000457 oFsm.mutexPLastTxMeInstance.RLock()
mpagenkob06684f2021-03-23 14:39:04 +0000458 if oFsm.pLastTxMeInstance == nil {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000459 oFsm.mutexPLastTxMeInstance.RUnlock()
mpagenkob06684f2021-03-23 14:39:04 +0000460 logger.Errorw(ctx, "could not send PortAdmin OMCI message from LockStateFsm", log.Fields{
461 "device-id": oFsm.deviceID, "Port": uniNo})
462 //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
463 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
464 return
465 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000466 oFsm.mutexPLastTxMeInstance.RUnlock()
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000467
mpagenko8b5fdd22020-12-17 17:58:32 +0000468 //verify response
469 err := oFsm.waitforOmciResponse(ctx, meInstance)
470 if err != nil {
471 logger.Errorw(ctx, "UniTP Admin State set failed, aborting LockState set!",
472 log.Fields{"device-id": oFsm.deviceID, "Port": uniNo})
473 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
474 return
475 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000476 }
477 } //for all UNI ports
478 // if Config has been done for all UNI related instances let the FSM proceed
479 // while we did not check here, if there is some port at all - !?
mpagenko8b5fdd22020-12-17 17:58:32 +0000480 logger.Infow(ctx, "UniTP adminState loop finished", log.Fields{"device-id": oFsm.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530481 _ = oFsm.pAdaptFsm.pFsm.Event(uniEvRxUnisResp)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000482}
483
dbainbri4d3a0dc2020-12-02 00:33:42 +0000484func (oFsm *lockStateFsm) waitforOmciResponse(ctx context.Context, apMeInstance *me.ManagedEntity) error {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000485 select {
Himani Chawla4d908332020-08-31 12:30:20 +0530486 // maybe be also some outside cancel (but no context modeled for the moment ...)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000487 // case <-ctx.Done():
dbainbri4d3a0dc2020-12-02 00:33:42 +0000488 // logger.Infow(ctx,"LockState-bridge-init message reception canceled", log.Fields{"for device-id": oFsm.deviceID})
Holger Hildebrandt366ef192021-05-05 11:07:44 +0000489 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 +0000490 logger.Warnw(ctx, "LockStateFSM uni-set timeout", log.Fields{"for device-id": oFsm.deviceID})
mpagenko01e726e2020-10-23 09:45:29 +0000491 return fmt.Errorf("lockStateFsm uni-set timeout for device-id %s", oFsm.deviceID)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000492 case success := <-oFsm.omciLockResponseReceived:
Himani Chawla4d908332020-08-31 12:30:20 +0530493 if success {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000494 logger.Debug(ctx, "LockStateFSM uni-set response received")
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000495 return nil
496 }
497 // should not happen so far
dbainbri4d3a0dc2020-12-02 00:33:42 +0000498 logger.Warnw(ctx, "LockStateFSM uni-set response error", log.Fields{"for device-id": oFsm.deviceID})
mpagenko01e726e2020-10-23 09:45:29 +0000499 return fmt.Errorf("lockStateFsm uni-set responseError for device-id %s", oFsm.deviceID)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000500 }
501}