blob: a9de1ddfaa253450e2e76e7cd00311d5b58cfbdf [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
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000017//Package uniprt provides the utilities for uni port configuration
18package uniprt
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000019
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 Hildebrandt4b5e73f2021-08-19 06:51:21 +000031 cmn "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/common"
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000032)
33
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000034//LockStateFsm defines the structure for the state machine to lock/unlock the ONU UNI ports via OMCI
35type LockStateFsm struct {
mpagenko01e726e2020-10-23 09:45:29 +000036 deviceID string
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000037 pDeviceHandler cmn.IdeviceHandler
38 pOnuDeviceEntry cmn.IonuDeviceEntry
39 pOmciCC *cmn.OmciCC
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +000040 mutexAdminState sync.RWMutex
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000041 adminState bool
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000042 requestEvent cmn.OnuDeviceEvent
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000043 omciLockResponseReceived chan bool //seperate channel needed for checking UNI port OMCi message responses
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000044 PAdaptFsm *cmn.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
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000049// events of lock/unlock UNI port FSM
mpagenko1cc3cb42020-07-27 15:24:38 +000050const (
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000051 UniEvStart = "UniEvStart"
52 UniEvStartAdmin = "UniEvStartAdmin"
53 UniEvRxUnisResp = "UniEvRxUnisResp"
54 UniEvRxOnugResp = "UniEvRxOnugResp"
55 UniEvTimeoutSimple = "UniEvTimeoutSimple"
56 UniEvTimeoutUnis = "UniEvTimeoutUnis"
57 UniEvReset = "UniEvReset"
58 UniEvRestart = "UniEvRestart"
mpagenko1cc3cb42020-07-27 15:24:38 +000059)
mpagenko1cc3cb42020-07-27 15:24:38 +000060
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000061// states of lock/unlock UNI port FSM
62const (
63 UniStDisabled = "UniStDisabled"
64 UniStStarting = "UniStStarting"
65 UniStSettingUnis = "UniStSettingUnis"
66 UniStSettingOnuG = "UniStSettingOnuG"
67 UniStAdminDone = "UniStAdminDone"
68 UniStResetting = "UniStResetting"
69)
70
71// CUniFsmIdleState - TODO: add comment
72const CUniFsmIdleState = UniStDisabled
73
74//NewLockStateFsm is the 'constructor' for the state machine to lock/unlock the ONU UNI ports via OMCI
75func NewLockStateFsm(ctx context.Context, aAdminState bool, aRequestEvent cmn.OnuDeviceEvent,
76 aName string, apDeviceHandler cmn.IdeviceHandler, apOnuDeviceEntry cmn.IonuDeviceEntry, aCommChannel chan cmn.Message) *LockStateFsm {
77 instFsm := &LockStateFsm{
78 deviceID: apDeviceHandler.GetDeviceID(),
79 pDeviceHandler: apDeviceHandler,
80 pOnuDeviceEntry: apOnuDeviceEntry,
81 pOmciCC: apOnuDeviceEntry.GetDevOmciCC(),
82 adminState: aAdminState,
83 requestEvent: aRequestEvent,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000084 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000085 instFsm.PAdaptFsm = cmn.NewAdapterFsm(aName, instFsm.deviceID, aCommChannel)
86 if instFsm.PAdaptFsm == nil {
87 logger.Errorw(ctx, "LockStateFsm's cmn.AdapterFsm could not be instantiated!!", log.Fields{
mpagenko01e726e2020-10-23 09:45:29 +000088 "device-id": instFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000089 return nil
90 }
Himani Chawla4d908332020-08-31 12:30:20 +053091 if aAdminState { //port locking requested
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000092 instFsm.PAdaptFsm.PFsm = fsm.NewFSM(
93 UniStDisabled,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000094 fsm.Events{
95
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000096 {Name: UniEvStart, Src: []string{UniStDisabled}, Dst: UniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000097
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000098 {Name: UniEvStartAdmin, Src: []string{UniStStarting}, Dst: UniStSettingUnis},
mpagenko1cc3cb42020-07-27 15:24:38 +000099 // the settingUnis state is used for multi ME config for all UNI related ports
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000100 // maybe such could be reflected in the state machine as well (port number parametrized)
101 // but that looks not straightforward here - so we keep it simple here for the beginning(?)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000102 {Name: UniEvRxUnisResp, Src: []string{UniStSettingUnis}, Dst: UniStSettingOnuG},
103 {Name: UniEvRxOnugResp, Src: []string{UniStSettingOnuG}, Dst: UniStAdminDone},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000104
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000105 {Name: UniEvTimeoutSimple, Src: []string{UniStSettingOnuG}, Dst: UniStStarting},
106 {Name: UniEvTimeoutUnis, Src: []string{UniStSettingUnis}, Dst: UniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000107
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000108 {Name: UniEvReset, Src: []string{UniStStarting, UniStSettingOnuG, UniStSettingUnis,
109 UniStAdminDone}, Dst: UniStResetting},
110 // exceptional treatment for all states except UniStResetting
111 {Name: UniEvRestart, Src: []string{UniStStarting, UniStSettingOnuG, UniStSettingUnis,
112 UniStAdminDone, UniStResetting}, Dst: UniStDisabled},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000113 },
114
115 fsm.Callbacks{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000116 "enter_state": func(e *fsm.Event) { instFsm.PAdaptFsm.LogFsmStateChange(ctx, e) },
117 ("enter_" + UniStStarting): func(e *fsm.Event) { instFsm.enterAdminStartingState(ctx, e) },
118 ("enter_" + UniStSettingOnuG): func(e *fsm.Event) { instFsm.enterSettingOnuGState(ctx, e) },
119 ("enter_" + UniStSettingUnis): func(e *fsm.Event) { instFsm.enterSettingUnisState(ctx, e) },
120 ("enter_" + UniStAdminDone): func(e *fsm.Event) { instFsm.enterAdminDoneState(ctx, e) },
121 ("enter_" + UniStResetting): func(e *fsm.Event) { instFsm.enterResettingState(ctx, e) },
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000122 },
123 )
124 } else { //port unlocking requested
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000125 instFsm.PAdaptFsm.PFsm = fsm.NewFSM(
126 UniStDisabled,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000127 fsm.Events{
128
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000129 {Name: UniEvStart, Src: []string{UniStDisabled}, Dst: UniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000130
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000131 {Name: UniEvStartAdmin, Src: []string{UniStStarting}, Dst: UniStSettingOnuG},
132 {Name: UniEvRxOnugResp, Src: []string{UniStSettingOnuG}, Dst: UniStSettingUnis},
mpagenko1cc3cb42020-07-27 15:24:38 +0000133 // the settingUnis state is used for multi ME config for all UNI related ports
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000134 // maybe such could be reflected in the state machine as well (port number parametrized)
135 // but that looks not straightforward here - so we keep it simple here for the beginning(?)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000136 {Name: UniEvRxUnisResp, Src: []string{UniStSettingUnis}, Dst: UniStAdminDone},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000137
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000138 {Name: UniEvTimeoutSimple, Src: []string{UniStSettingOnuG}, Dst: UniStStarting},
139 {Name: UniEvTimeoutUnis, Src: []string{UniStSettingUnis}, Dst: UniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000140
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000141 {Name: UniEvReset, Src: []string{UniStStarting, UniStSettingOnuG, UniStSettingUnis,
142 UniStAdminDone}, Dst: UniStResetting},
143 // exceptional treatment for all states except UniStResetting
144 {Name: UniEvRestart, Src: []string{UniStStarting, UniStSettingOnuG, UniStSettingUnis,
145 UniStAdminDone, UniStResetting}, Dst: UniStDisabled},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000146 },
147
148 fsm.Callbacks{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000149 "enter_state": func(e *fsm.Event) { instFsm.PAdaptFsm.LogFsmStateChange(ctx, e) },
150 ("enter_" + UniStStarting): func(e *fsm.Event) { instFsm.enterAdminStartingState(ctx, e) },
151 ("enter_" + UniStSettingOnuG): func(e *fsm.Event) { instFsm.enterSettingOnuGState(ctx, e) },
152 ("enter_" + UniStSettingUnis): func(e *fsm.Event) { instFsm.enterSettingUnisState(ctx, e) },
153 ("enter_" + UniStAdminDone): func(e *fsm.Event) { instFsm.enterAdminDoneState(ctx, e) },
154 ("enter_" + UniStResetting): func(e *fsm.Event) { instFsm.enterResettingState(ctx, e) },
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000155 },
156 )
157 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000158 if instFsm.PAdaptFsm.PFsm == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000159 logger.Errorw(ctx, "LockStateFsm's Base FSM could not be instantiated!!", log.Fields{
mpagenko01e726e2020-10-23 09:45:29 +0000160 "device-id": instFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000161 return nil
162 }
163
dbainbri4d3a0dc2020-12-02 00:33:42 +0000164 logger.Debugw(ctx, "LockStateFsm created", log.Fields{"device-id": instFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000165 return instFsm
166}
167
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000168//SetSuccessEvent modifies the requested event notified on success
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000169//assumption is that this is only called in the disabled (idle) state of the FSM, hence no sem protection required
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000170func (oFsm *LockStateFsm) SetSuccessEvent(aEvent cmn.OnuDeviceEvent) {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000171 oFsm.requestEvent = aEvent
172}
173
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000174func (oFsm *LockStateFsm) enterAdminStartingState(ctx context.Context, e *fsm.Event) {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000175 logger.Debugw(ctx, "LockStateFSM start", log.Fields{"in state": e.FSM.Current(),
mpagenko01e726e2020-10-23 09:45:29 +0000176 "device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000177 // in case the used channel is not yet defined (can be re-used after restarts)
178 if oFsm.omciLockResponseReceived == nil {
179 oFsm.omciLockResponseReceived = make(chan bool)
dbainbri4d3a0dc2020-12-02 00:33:42 +0000180 logger.Debug(ctx, "LockStateFSM - OMCI UniLock RxChannel defined")
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000181 } else {
182 // as we may 're-use' this instance of FSM and the connected channel
mpagenkob06684f2021-03-23 14:39:04 +0000183 // make sure there is no 'lingering' request in the already existing channels:
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000184 // (simple loop sufficient as we are the only receiver)
185 for len(oFsm.omciLockResponseReceived) > 0 {
186 <-oFsm.omciLockResponseReceived
187 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000188 for len(oFsm.PAdaptFsm.CommChan) > 0 {
189 <-oFsm.PAdaptFsm.CommChan
mpagenkob06684f2021-03-23 14:39:04 +0000190 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000191 }
192 // start go routine for processing of LockState messages
dbainbri4d3a0dc2020-12-02 00:33:42 +0000193 go oFsm.processOmciLockMessages(ctx)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000194
195 //let the state machine run forward from here directly
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000196 pLockStateAFsm := oFsm.PAdaptFsm
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000197 if pLockStateAFsm != nil {
198 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000199 go func(a_pAFsm *cmn.AdapterFsm) {
200 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
201 _ = a_pAFsm.PFsm.Event(UniEvStartAdmin)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000202 }
203 }(pLockStateAFsm)
204 }
205}
206
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000207func (oFsm *LockStateFsm) enterSettingOnuGState(ctx context.Context, e *fsm.Event) {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000208 var omciAdminState uint8 = 1 //default locked
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000209 oFsm.mutexAdminState.RLock()
Himani Chawla4d908332020-08-31 12:30:20 +0530210 if !oFsm.adminState {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000211 omciAdminState = 0
212 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000213 oFsm.mutexAdminState.RUnlock()
dbainbri4d3a0dc2020-12-02 00:33:42 +0000214 logger.Debugw(ctx, "LockStateFSM Tx Set::ONU-G:admin", log.Fields{
mpagenko01e726e2020-10-23 09:45:29 +0000215 "omciAdmin": omciAdminState, "in state": e.FSM.Current(), "device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000216 requestedAttributes := me.AttributeValueMap{"AdministrativeState": omciAdminState}
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000217 oFsm.mutexPLastTxMeInstance.Lock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000218 meInstance, err := oFsm.pOmciCC.SendSetOnuGLS(log.WithSpanFromContext(context.TODO(), ctx), oFsm.pDeviceHandler.GetOmciTimeout(), true,
219 requestedAttributes, oFsm.PAdaptFsm.CommChan)
ozgecanetsiab36ed572021-04-01 10:38:48 +0300220 if err != nil {
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})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000223 pLockStateAFsm := oFsm.PAdaptFsm
ozgecanetsiab36ed572021-04-01 10:38:48 +0300224 if pLockStateAFsm != nil {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000225 go func(a_pAFsm *cmn.AdapterFsm) {
226 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
227 _ = a_pAFsm.PFsm.Event(UniEvReset)
ozgecanetsiab36ed572021-04-01 10:38:48 +0300228 }
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 Hildebrandt0da7e6f2021-05-12 13:08:43 +0000237 oFsm.mutexPLastTxMeInstance.Unlock()
mpagenkob06684f2021-03-23 14:39:04 +0000238 logger.Errorw(ctx, "could not send OMCI message from LockStateFsm", log.Fields{
239 "device-id": oFsm.deviceID})
240 //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
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000241 pLockStateAFsm := oFsm.PAdaptFsm
mpagenkob06684f2021-03-23 14:39:04 +0000242 if pLockStateAFsm != nil {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000243
mpagenkob06684f2021-03-23 14:39:04 +0000244 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000245 go func(a_pAFsm *cmn.AdapterFsm) {
246 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
247 _ = a_pAFsm.PFsm.Event(UniEvReset)
mpagenkob06684f2021-03-23 14:39:04 +0000248 }
249 }(pLockStateAFsm)
250 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000251 return
mpagenkob06684f2021-03-23 14:39:04 +0000252 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000253 oFsm.mutexPLastTxMeInstance.Unlock()
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000254}
255
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000256func (oFsm *LockStateFsm) enterSettingUnisState(ctx context.Context, e *fsm.Event) {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000257 oFsm.mutexAdminState.RLock()
mpagenko8b5fdd22020-12-17 17:58:32 +0000258 logger.Debugw(ctx, "LockStateFSM - starting UniTP adminState loop", log.Fields{
mpagenko01e726e2020-10-23 09:45:29 +0000259 "in state": e.FSM.Current(), "device-id": oFsm.deviceID, "LockState": oFsm.adminState})
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000260 oFsm.mutexAdminState.RUnlock()
dbainbri4d3a0dc2020-12-02 00:33:42 +0000261 go oFsm.performUniPortAdminSet(ctx)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000262}
263
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000264func (oFsm *LockStateFsm) enterAdminDoneState(ctx context.Context, e *fsm.Event) {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000265 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 +0000266 //use DeviceHandler event notification directly, no need/support to update DeviceEntryState for lock/unlock
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000267 oFsm.pDeviceHandler.DeviceProcStatusUpdate(ctx, oFsm.requestEvent)
Holger Hildebrandt8165eda2020-09-24 09:39:24 +0000268
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000269 //let's reset the state machine in order to release all resources now
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000270 pLockStateAFsm := oFsm.PAdaptFsm
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000271 if pLockStateAFsm != nil {
272 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000273 go func(a_pAFsm *cmn.AdapterFsm) {
274 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
275 _ = a_pAFsm.PFsm.Event(UniEvReset)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000276 }
277 }(pLockStateAFsm)
278 }
279}
280
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000281func (oFsm *LockStateFsm) enterResettingState(ctx context.Context, e *fsm.Event) {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000282 logger.Debugw(ctx, "LockStateFSM resetting", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000283 pLockStateAFsm := oFsm.PAdaptFsm
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000284 if pLockStateAFsm != nil {
285 // abort running message processing
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000286 fsmAbortMsg := cmn.Message{
287 Type: cmn.TestMsg,
288 Data: cmn.TestMessage{
289 TestMessageVal: cmn.AbortMessageProcessing,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000290 },
291 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000292 pLockStateAFsm.CommChan <- fsmAbortMsg
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000293
294 //try to restart the FSM to 'disabled'
295 // see DownloadedState: decouple event transfer
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000296 go func(a_pAFsm *cmn.AdapterFsm) {
297 if a_pAFsm != nil && a_pAFsm.PFsm != nil {
298 _ = a_pAFsm.PFsm.Event(UniEvRestart)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000299 }
300 }(pLockStateAFsm)
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000301 oFsm.mutexPLastTxMeInstance.Lock()
mpagenko01e726e2020-10-23 09:45:29 +0000302 oFsm.pLastTxMeInstance = nil
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000303 oFsm.mutexPLastTxMeInstance.Unlock()
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000304 }
305}
306
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000307func (oFsm *LockStateFsm) processOmciLockMessages(ctx context.Context) {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000308 logger.Debugw(ctx, "Start LockStateFsm Msg processing", log.Fields{"for device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000309loop:
310 for {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000311 // case <-ctx.Done():
dbainbri4d3a0dc2020-12-02 00:33:42 +0000312 // logger.Info(ctx,"MibSync Msg", log.Fields{"Message handling canceled via context for device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000313 // break loop
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000314 message, ok := <-oFsm.PAdaptFsm.CommChan
Himani Chawla4d908332020-08-31 12:30:20 +0530315 if !ok {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000316 logger.Info(ctx, "LockStateFsm Rx Msg - could not read from channel", log.Fields{"device-id": oFsm.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530317 // but then we have to ensure a restart of the FSM as well - as exceptional procedure
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000318 _ = oFsm.PAdaptFsm.PFsm.Event(UniEvRestart)
Himani Chawla4d908332020-08-31 12:30:20 +0530319 break loop
320 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000321 logger.Debugw(ctx, "LockStateFsm Rx Msg", log.Fields{"device-id": oFsm.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530322
323 switch message.Type {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000324 case cmn.TestMsg:
325 msg, _ := message.Data.(cmn.TestMessage)
326 if msg.TestMessageVal == cmn.AbortMessageProcessing {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000327 logger.Debugw(ctx, "LockStateFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000328 break loop
329 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000330 logger.Warnw(ctx, "LockStateFsm unknown TestMessage", log.Fields{"device-id": oFsm.deviceID, "MessageVal": msg.TestMessageVal})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000331 case cmn.OMCI:
332 msg, _ := message.Data.(cmn.OmciMessage)
dbainbri4d3a0dc2020-12-02 00:33:42 +0000333 oFsm.handleOmciLockStateMessage(ctx, msg)
Himani Chawla4d908332020-08-31 12:30:20 +0530334 default:
dbainbri4d3a0dc2020-12-02 00:33:42 +0000335 logger.Warn(ctx, "LockStateFsm Rx unknown message", log.Fields{"device-id": oFsm.deviceID,
Himani Chawla4d908332020-08-31 12:30:20 +0530336 "message.Type": message.Type})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000337 }
338 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000339 logger.Debugw(ctx, "End LockStateFsm Msg processing", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000340}
341
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000342func (oFsm *LockStateFsm) handleOmciLockStateMessage(ctx context.Context, msg cmn.OmciMessage) {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000343 logger.Debugw(ctx, "Rx OMCI LockStateFsm Msg", log.Fields{"device-id": oFsm.deviceID,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000344 "msgType": msg.OmciMsg.MessageType})
345
346 if msg.OmciMsg.MessageType == omci.SetResponseType {
347 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeSetResponse)
348 if msgLayer == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000349 logger.Errorw(ctx, "LockStateFsm - Omci Msg layer could not be detected 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 }
353 msgObj, msgOk := msgLayer.(*omci.SetResponse)
354 if !msgOk {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000355 logger.Errorw(ctx, "LockStateFsm - Omci Msg layer could not be assigned for SetResponse",
mpagenko01e726e2020-10-23 09:45:29 +0000356 log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000357 return
358 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000359 logger.Debugw(ctx, "LockStateFsm SetResponse Data", log.Fields{"device-id": oFsm.deviceID, "data-fields": msgObj})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000360 if msgObj.Result != me.Success {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000361 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 +0000362 // possibly force FSM into abort or ignore some errors for some messages? store error for mgmt display?
363 return
364 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000365
mpagenkob06684f2021-03-23 14:39:04 +0000366 //should never appear, left here for robustness
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000367 oFsm.mutexPLastTxMeInstance.RLock()
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700368 if oFsm.pLastTxMeInstance != nil {
369 // compare comments above for CreateResponse (apply also here ...)
370 if msgObj.EntityClass == oFsm.pLastTxMeInstance.GetClassID() &&
371 msgObj.EntityInstance == oFsm.pLastTxMeInstance.GetEntityID() {
372 //store the created ME into DB //TODO??? obviously the Python code does not store the config ...
373 // if, then something like:
374 //oFsm.pOnuDB.StoreMe(msgObj)
375
376 switch oFsm.pLastTxMeInstance.GetName() {
377 case "OnuG":
378 { // let the FSM proceed ...
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000379 oFsm.mutexPLastTxMeInstance.RUnlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000380 _ = oFsm.PAdaptFsm.PFsm.Event(UniEvRxOnugResp)
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700381 }
382 case "PhysicalPathTerminationPointEthernetUni", "VirtualEthernetInterfacePoint":
383 { // let the PPTP init proceed by stopping the wait function
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000384 oFsm.mutexPLastTxMeInstance.RUnlock()
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700385 oFsm.omciLockResponseReceived <- true
386 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000387 default:
388 {
389 logger.Warnw(ctx, "Unsupported ME name received!",
390 log.Fields{"ME name": oFsm.pLastTxMeInstance.GetName(), "device-id": oFsm.deviceID})
391 oFsm.mutexPLastTxMeInstance.RUnlock()
392 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000393 }
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700394 } else {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000395 oFsm.mutexPLastTxMeInstance.RUnlock()
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700396 logger.Warnf(ctx, "LockStateFsm - Received SetResponse Data for %s with wrong classID or entityID ",
397 log.Fields{"device-id": oFsm.deviceID, "data-fields": msgObj}, msgObj.EntityClass)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000398 }
Matteo Scandolob250bec2021-01-27 17:35:45 -0800399 } else {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000400 oFsm.mutexPLastTxMeInstance.RUnlock()
mpagenkob06684f2021-03-23 14:39:04 +0000401 logger.Errorw(ctx, "pLastTxMeInstance is nil", log.Fields{"device-id": oFsm.deviceID})
Girish Gowdra3d5d6752021-03-22 19:21:24 -0700402 return
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000403 }
404 } else {
mpagenkob06684f2021-03-23 14:39:04 +0000405 logger.Errorw(ctx, "LockStateFsm - Rx OMCI unhandled MsgType", log.Fields{"omciMsgType": msg.OmciMsg.MessageType})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000406 return
407 }
408}
409
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000410func (oFsm *LockStateFsm) performUniPortAdminSet(ctx context.Context) {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000411 var omciAdminState uint8 = 1 //default locked
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000412 oFsm.mutexAdminState.RLock()
Himani Chawla4d908332020-08-31 12:30:20 +0530413 if !oFsm.adminState {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000414 omciAdminState = 0
415 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000416 oFsm.mutexAdminState.RUnlock()
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000417 //set PPTPEthUni or VEIP AdminState
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000418 requestedAttributes := me.AttributeValueMap{"AdministrativeState": omciAdminState}
419
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000420 for uniNo, uniPort := range *oFsm.pDeviceHandler.GetUniEntityMap() {
mpagenko8b5fdd22020-12-17 17:58:32 +0000421 // only unlock the UniPort in case it is defined for usage (R2.6 limit only one port),
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000422 // compare also limitation for logical voltha port in dh.EnableUniPortStateUpdate()
Matteo Scandolo20d180c2021-06-10 17:20:21 +0200423
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000424 if (omciAdminState == 1) || (1<<uniPort.UniID)&oFsm.pDeviceHandler.GetUniPortMask() == (1<<uniPort.UniID) {
mpagenko8b5fdd22020-12-17 17:58:32 +0000425 var meInstance *me.ManagedEntity
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000426 if uniPort.PortType == cmn.UniPPTP {
mpagenko8b5fdd22020-12-17 17:58:32 +0000427 logger.Debugw(ctx, "Setting PPTP admin state", log.Fields{
428 "device-id": oFsm.deviceID, "for PortNo": uniNo, "state (0-unlock)": omciAdminState})
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000429 oFsm.mutexPLastTxMeInstance.Lock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000430 meInstance, err := oFsm.pOmciCC.SendSetPptpEthUniLS(log.WithSpanFromContext(context.TODO(), ctx),
431 uniPort.EntityID, oFsm.pDeviceHandler.GetOmciTimeout(),
432 true, requestedAttributes, oFsm.PAdaptFsm.CommChan)
ozgecanetsiab36ed572021-04-01 10:38:48 +0300433 if err != nil {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000434 oFsm.mutexPLastTxMeInstance.Unlock()
ozgecanetsiab36ed572021-04-01 10:38:48 +0300435 logger.Errorw(ctx, "SetPptpEthUniLS set failed, aborting LockStateFsm!",
436 log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000437 _ = oFsm.PAdaptFsm.PFsm.Event(UniEvReset)
ozgecanetsiab36ed572021-04-01 10:38:48 +0300438 return
439 }
mpagenko8b5fdd22020-12-17 17:58:32 +0000440 oFsm.pLastTxMeInstance = meInstance
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000441 oFsm.mutexPLastTxMeInstance.Unlock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000442 } else if uniPort.PortType == cmn.UniVEIP {
mpagenko8b5fdd22020-12-17 17:58:32 +0000443 logger.Debugw(ctx, "Setting VEIP admin state", log.Fields{
444 "device-id": oFsm.deviceID, "for PortNo": uniNo, "state (0-unlock)": omciAdminState})
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000445 oFsm.mutexPLastTxMeInstance.Lock()
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000446 meInstance, err := oFsm.pOmciCC.SendSetVeipLS(log.WithSpanFromContext(context.TODO(), ctx),
447 uniPort.EntityID, oFsm.pDeviceHandler.GetOmciTimeout(),
448 true, requestedAttributes, oFsm.PAdaptFsm.CommChan)
ozgecanetsiab36ed572021-04-01 10:38:48 +0300449 if err != nil {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000450 oFsm.mutexPLastTxMeInstance.Unlock()
ozgecanetsiab36ed572021-04-01 10:38:48 +0300451 logger.Errorw(ctx, "SetVeipLS set failed, aborting LockStateFsm!",
452 log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000453 _ = oFsm.PAdaptFsm.PFsm.Event(UniEvReset)
ozgecanetsiab36ed572021-04-01 10:38:48 +0300454 return
455 }
mpagenko8b5fdd22020-12-17 17:58:32 +0000456 oFsm.pLastTxMeInstance = meInstance
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000457 oFsm.mutexPLastTxMeInstance.Unlock()
mpagenko8b5fdd22020-12-17 17:58:32 +0000458 } else {
459 logger.Warnw(ctx, "Unsupported UniTP type - skip",
460 log.Fields{"device-id": oFsm.deviceID, "Port": uniNo})
461 continue
462 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000463 oFsm.mutexPLastTxMeInstance.RLock()
mpagenkob06684f2021-03-23 14:39:04 +0000464 if oFsm.pLastTxMeInstance == nil {
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000465 oFsm.mutexPLastTxMeInstance.RUnlock()
mpagenkob06684f2021-03-23 14:39:04 +0000466 logger.Errorw(ctx, "could not send PortAdmin OMCI message from LockStateFsm", log.Fields{
467 "device-id": oFsm.deviceID, "Port": uniNo})
468 //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
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000469 _ = oFsm.PAdaptFsm.PFsm.Event(UniEvReset)
mpagenkob06684f2021-03-23 14:39:04 +0000470 return
471 }
Holger Hildebrandt0da7e6f2021-05-12 13:08:43 +0000472 oFsm.mutexPLastTxMeInstance.RUnlock()
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000473
mpagenko8b5fdd22020-12-17 17:58:32 +0000474 //verify response
475 err := oFsm.waitforOmciResponse(ctx, meInstance)
476 if err != nil {
477 logger.Errorw(ctx, "UniTP Admin State set failed, aborting LockState set!",
478 log.Fields{"device-id": oFsm.deviceID, "Port": uniNo})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000479 _ = oFsm.PAdaptFsm.PFsm.Event(UniEvReset)
mpagenko8b5fdd22020-12-17 17:58:32 +0000480 return
481 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000482 }
483 } //for all UNI ports
484 // if Config has been done for all UNI related instances let the FSM proceed
485 // while we did not check here, if there is some port at all - !?
mpagenko8b5fdd22020-12-17 17:58:32 +0000486 logger.Infow(ctx, "UniTP adminState loop finished", log.Fields{"device-id": oFsm.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000487 _ = oFsm.PAdaptFsm.PFsm.Event(UniEvRxUnisResp)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000488}
489
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000490func (oFsm *LockStateFsm) waitforOmciResponse(ctx context.Context, apMeInstance *me.ManagedEntity) error {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000491 select {
Himani Chawla4d908332020-08-31 12:30:20 +0530492 // maybe be also some outside cancel (but no context modeled for the moment ...)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000493 // case <-ctx.Done():
dbainbri4d3a0dc2020-12-02 00:33:42 +0000494 // logger.Infow(ctx,"LockState-bridge-init message reception canceled", log.Fields{"for device-id": oFsm.deviceID})
Holger Hildebrandt366ef192021-05-05 11:07:44 +0000495 case <-time.After(oFsm.pOmciCC.GetMaxOmciTimeoutWithRetries() * time.Second): //3s was detected to be to less in 8*8 bbsim test with debug Info/Debug
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000496 logger.Warnw(ctx, "lockStateFSM uni-set timeout", log.Fields{"for device-id": oFsm.deviceID})
mpagenko01e726e2020-10-23 09:45:29 +0000497 return fmt.Errorf("lockStateFsm uni-set timeout for device-id %s", oFsm.deviceID)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000498 case success := <-oFsm.omciLockResponseReceived:
Himani Chawla4d908332020-08-31 12:30:20 +0530499 if success {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000500 logger.Debug(ctx, "LockStateFSM uni-set response received")
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000501 return nil
502 }
503 // should not happen so far
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000504 logger.Warnw(ctx, "lockStateFSM uni-set response error", log.Fields{"for device-id": oFsm.deviceID})
mpagenko01e726e2020-10-23 09:45:29 +0000505 return fmt.Errorf("lockStateFsm uni-set responseError for device-id %s", oFsm.deviceID)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000506 }
507}