blob: 6761ea7c1fa88c98509fb77cd7e006e8754d5851 [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"
22 "errors"
23 "time"
24
25 "github.com/looplab/fsm"
26
27 "github.com/opencord/omci-lib-go"
28 me "github.com/opencord/omci-lib-go/generated"
29 "github.com/opencord/voltha-lib-go/v3/pkg/log"
30 //ic "github.com/opencord/voltha-protos/v3/go/inter_container"
31 //"github.com/opencord/voltha-protos/v3/go/openflow_13"
32 //"github.com/opencord/voltha-protos/v3/go/voltha"
33)
34
35//LockStateFsm defines the structure for the state machine to lock/unlock the ONU UNI ports via OMCI
36type LockStateFsm struct {
37 pOmciCC *OmciCC
38 adminState bool
39 requestEvent OnuDeviceEvent
40 omciLockResponseReceived chan bool //seperate channel needed for checking UNI port OMCi message responses
41 pAdaptFsm *AdapterFsm
42}
43
mpagenko1cc3cb42020-07-27 15:24:38 +000044const (
45 // events of lock/unlock UNI port FSM
46 uniEvStart = "uniEvStart"
47 uniEvStartAdmin = "uniEvStartAdmin"
48 uniEvRxUnisResp = "uniEvRxUnisResp"
49 uniEvRxOnugResp = "uniEvRxOnugResp"
50 uniEvTimeoutSimple = "uniEvTimeoutSimple"
51 uniEvTimeoutUnis = "uniEvTimeoutUnis"
52 uniEvReset = "uniEvReset"
53 uniEvRestart = "uniEvRestart"
54)
55const (
56 // states of lock/unlock UNI port FSM
57 uniStDisabled = "uniStDisabled"
58 uniStStarting = "uniStStarting"
59 uniStSettingUnis = "uniStSettingUnis"
60 uniStSettingOnuG = "uniStSettingOnuG"
61 uniStAdminDone = "uniStAdminDone"
62 uniStResetting = "uniStResetting"
63)
64
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000065//NewLockStateFsm is the 'constructor' for the state machine to lock/unlock the ONU UNI ports via OMCI
66func NewLockStateFsm(apDevOmciCC *OmciCC, aAdminState bool, aRequestEvent OnuDeviceEvent,
67 aName string, aDeviceID string, aCommChannel chan Message) *LockStateFsm {
68 instFsm := &LockStateFsm{
69 pOmciCC: apDevOmciCC,
70 adminState: aAdminState,
71 requestEvent: aRequestEvent,
72 }
73 instFsm.pAdaptFsm = NewAdapterFsm(aName, aDeviceID, aCommChannel)
74 if instFsm.pAdaptFsm == nil {
75 logger.Errorw("LockStateFsm's AdapterFsm could not be instantiated!!", log.Fields{
76 "device-id": aDeviceID})
77 return nil
78 }
79 if aAdminState == true { //port locking requested
80 instFsm.pAdaptFsm.pFsm = fsm.NewFSM(
mpagenko1cc3cb42020-07-27 15:24:38 +000081 uniStDisabled,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000082 fsm.Events{
83
mpagenko1cc3cb42020-07-27 15:24:38 +000084 {Name: uniEvStart, Src: []string{uniStDisabled}, Dst: uniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000085
mpagenko1cc3cb42020-07-27 15:24:38 +000086 {Name: uniEvStartAdmin, Src: []string{uniStStarting}, Dst: uniStSettingUnis},
87 // the settingUnis state is used for multi ME config for all UNI related ports
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000088 // maybe such could be reflected in the state machine as well (port number parametrized)
89 // but that looks not straightforward here - so we keep it simple here for the beginning(?)
mpagenko1cc3cb42020-07-27 15:24:38 +000090 {Name: uniEvRxUnisResp, Src: []string{uniStSettingUnis}, Dst: uniStSettingOnuG},
91 {Name: uniEvRxOnugResp, Src: []string{uniStSettingOnuG}, Dst: uniStAdminDone},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000092
mpagenko1cc3cb42020-07-27 15:24:38 +000093 {Name: uniEvTimeoutSimple, Src: []string{uniStSettingOnuG}, Dst: uniStStarting},
94 {Name: uniEvTimeoutUnis, Src: []string{uniStSettingUnis}, Dst: uniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000095
mpagenko1cc3cb42020-07-27 15:24:38 +000096 {Name: uniEvReset, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis,
97 uniStAdminDone}, Dst: uniStResetting},
98 // exceptional treatment for all states except uniStResetting
99 {Name: uniEvRestart, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis,
100 uniStAdminDone, uniStResetting}, Dst: uniStDisabled},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000101 },
102
103 fsm.Callbacks{
mpagenko1cc3cb42020-07-27 15:24:38 +0000104 "enter_state": func(e *fsm.Event) { instFsm.pAdaptFsm.logFsmStateChange(e) },
105 ("enter_" + uniStStarting): func(e *fsm.Event) { instFsm.enterAdminStartingState(e) },
106 ("enter_" + uniStSettingOnuG): func(e *fsm.Event) { instFsm.enterSettingOnuGState(e) },
107 ("enter_" + uniStSettingUnis): func(e *fsm.Event) { instFsm.enterSettingUnisState(e) },
108 ("enter_" + uniStAdminDone): func(e *fsm.Event) { instFsm.enterAdminDoneState(e) },
109 ("enter_" + uniStResetting): func(e *fsm.Event) { instFsm.enterResettingState(e) },
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000110 },
111 )
112 } else { //port unlocking requested
113 instFsm.pAdaptFsm.pFsm = fsm.NewFSM(
mpagenko1cc3cb42020-07-27 15:24:38 +0000114 uniStDisabled,
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000115 fsm.Events{
116
mpagenko1cc3cb42020-07-27 15:24:38 +0000117 {Name: uniEvStart, Src: []string{uniStDisabled}, Dst: uniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000118
mpagenko1cc3cb42020-07-27 15:24:38 +0000119 {Name: uniEvStartAdmin, Src: []string{uniStStarting}, Dst: uniStSettingOnuG},
120 {Name: uniEvRxOnugResp, Src: []string{uniStSettingOnuG}, Dst: uniStSettingUnis},
121 // the settingUnis state is used for multi ME config for all UNI related ports
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000122 // maybe such could be reflected in the state machine as well (port number parametrized)
123 // but that looks not straightforward here - so we keep it simple here for the beginning(?)
mpagenko1cc3cb42020-07-27 15:24:38 +0000124 {Name: uniEvRxUnisResp, Src: []string{uniStSettingUnis}, Dst: uniStAdminDone},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000125
mpagenko1cc3cb42020-07-27 15:24:38 +0000126 {Name: uniEvTimeoutSimple, Src: []string{uniStSettingOnuG}, Dst: uniStStarting},
127 {Name: uniEvTimeoutUnis, Src: []string{uniStSettingUnis}, Dst: uniStStarting},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000128
mpagenko1cc3cb42020-07-27 15:24:38 +0000129 {Name: uniEvReset, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis,
130 uniStAdminDone}, Dst: uniStResetting},
131 // exceptional treatment for all states except uniStResetting
132 {Name: uniEvRestart, Src: []string{uniStStarting, uniStSettingOnuG, uniStSettingUnis,
133 uniStAdminDone, uniStResetting}, Dst: uniStDisabled},
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000134 },
135
136 fsm.Callbacks{
mpagenko1cc3cb42020-07-27 15:24:38 +0000137 "enter_state": func(e *fsm.Event) { instFsm.pAdaptFsm.logFsmStateChange(e) },
138 ("enter_" + uniStStarting): func(e *fsm.Event) { instFsm.enterAdminStartingState(e) },
139 ("enter_" + uniStSettingOnuG): func(e *fsm.Event) { instFsm.enterSettingOnuGState(e) },
140 ("enter_" + uniStSettingUnis): func(e *fsm.Event) { instFsm.enterSettingUnisState(e) },
141 ("enter_" + uniStAdminDone): func(e *fsm.Event) { instFsm.enterAdminDoneState(e) },
142 ("enter_" + uniStResetting): func(e *fsm.Event) { instFsm.enterResettingState(e) },
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000143 },
144 )
145 }
146 if instFsm.pAdaptFsm.pFsm == nil {
147 logger.Errorw("LockStateFsm's Base FSM could not be instantiated!!", log.Fields{
148 "device-id": aDeviceID})
149 return nil
150 }
151
152 logger.Infow("LockStateFsm created", log.Fields{"device-id": aDeviceID})
153 return instFsm
154}
155
156//SetSuccessEvent modifies the requested event notified on success
157//assumption is that this is only called in the disabled (idle) state of the FSM, hence no sem protection required
158func (oFsm *LockStateFsm) SetSuccessEvent(aEvent OnuDeviceEvent) {
159 oFsm.requestEvent = aEvent
160}
161
162func (oFsm *LockStateFsm) enterAdminStartingState(e *fsm.Event) {
163 logger.Debugw("LockStateFSM start", log.Fields{"in state": e.FSM.Current(),
164 "device-id": oFsm.pAdaptFsm.deviceID})
165 // in case the used channel is not yet defined (can be re-used after restarts)
166 if oFsm.omciLockResponseReceived == nil {
167 oFsm.omciLockResponseReceived = make(chan bool)
168 logger.Debug("LockStateFSM - OMCI UniLock RxChannel defined")
169 } else {
170 // as we may 're-use' this instance of FSM and the connected channel
171 // make sure there is no 'lingering' request in the already existing channel:
172 // (simple loop sufficient as we are the only receiver)
173 for len(oFsm.omciLockResponseReceived) > 0 {
174 <-oFsm.omciLockResponseReceived
175 }
176 }
177 // start go routine for processing of LockState messages
178 go oFsm.ProcessOmciLockMessages()
179
180 //let the state machine run forward from here directly
181 pLockStateAFsm := oFsm.pAdaptFsm
182 if pLockStateAFsm != nil {
183 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
184 go func(a_pAFsm *AdapterFsm) {
185 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
mpagenko1cc3cb42020-07-27 15:24:38 +0000186 a_pAFsm.pFsm.Event(uniEvStartAdmin)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000187 }
188 }(pLockStateAFsm)
189 }
190}
191
192func (oFsm *LockStateFsm) enterSettingOnuGState(e *fsm.Event) {
193 var omciAdminState uint8 = 1 //default locked
194 if oFsm.adminState == false {
195 omciAdminState = 0
196 }
197 logger.Debugw("LockStateFSM Tx Set::ONU-G:admin", log.Fields{
198 "omciAdmin": omciAdminState, "in state": e.FSM.Current(), "device-id": oFsm.pAdaptFsm.deviceID})
199 requestedAttributes := me.AttributeValueMap{"AdministrativeState": omciAdminState}
200 meInstance := oFsm.pOmciCC.sendSetOnuGLS(context.TODO(), ConstDefaultOmciTimeout, true,
201 requestedAttributes, oFsm.pAdaptFsm.commChan)
202 //accept also nil as (error) return value for writing to LastTx
203 // - this avoids misinterpretation of new received OMCI messages
204 // we might already abort the processing with nil here, but maybe some auto-recovery may be tried
205 // - may be improved later, for now we just handle it with the Rx timeout or missing next event (stick in state)
206 oFsm.pOmciCC.pLastTxMeInstance = meInstance
207}
208
209func (oFsm *LockStateFsm) enterSettingUnisState(e *fsm.Event) {
210 logger.Infow("LockStateFSM - starting PPTP config loop", log.Fields{
211 "in state": e.FSM.Current(), "device-id": oFsm.pAdaptFsm.deviceID, "LockState": oFsm.adminState})
212 go oFsm.performUniPortAdminSet()
213}
214
215func (oFsm *LockStateFsm) enterAdminDoneState(e *fsm.Event) {
216 logger.Debugw("LockStateFSM", log.Fields{"send notification to core in State": e.FSM.Current(), "device-id": oFsm.pAdaptFsm.deviceID})
217 //use DeviceHandler event notification directly, no need/support to update DeviceEntryState for lock/unlock
218 oFsm.pOmciCC.pBaseDeviceHandler.DeviceProcStatusUpdate(oFsm.requestEvent)
219 //let's reset the state machine in order to release all resources now
220 pLockStateAFsm := oFsm.pAdaptFsm
221 if pLockStateAFsm != nil {
222 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
223 go func(a_pAFsm *AdapterFsm) {
224 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
mpagenko1cc3cb42020-07-27 15:24:38 +0000225 a_pAFsm.pFsm.Event(uniEvReset)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000226 }
227 }(pLockStateAFsm)
228 }
229}
230
231func (oFsm *LockStateFsm) enterResettingState(e *fsm.Event) {
232 logger.Debugw("LockStateFSM resetting", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID})
233 pLockStateAFsm := oFsm.pAdaptFsm
234 if pLockStateAFsm != nil {
235 // abort running message processing
236 fsmAbortMsg := Message{
237 Type: TestMsg,
238 Data: TestMessage{
239 TestMessageVal: AbortMessageProcessing,
240 },
241 }
242 pLockStateAFsm.commChan <- fsmAbortMsg
243
244 //try to restart the FSM to 'disabled'
245 // see DownloadedState: decouple event transfer
246 go func(a_pAFsm *AdapterFsm) {
247 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
mpagenko1cc3cb42020-07-27 15:24:38 +0000248 a_pAFsm.pFsm.Event(uniEvRestart)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000249 }
250 }(pLockStateAFsm)
251 }
252}
253
254func (oFsm *LockStateFsm) ProcessOmciLockMessages( /*ctx context.Context*/ ) {
255 logger.Debugw("Start LockStateFsm Msg processing", log.Fields{"for device-id": oFsm.pAdaptFsm.deviceID})
256loop:
257 for {
258 select {
259 // case <-ctx.Done():
260 // logger.Info("MibSync Msg", log.Fields{"Message handling canceled via context for device-id": oFsm.pAdaptFsm.deviceID})
261 // break loop
262 case message, ok := <-oFsm.pAdaptFsm.commChan:
263 if !ok {
264 logger.Info("LockStateFsm Rx Msg - could not read from channel", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID})
265 // but then we have to ensure a restart of the FSM as well - as exceptional procedure
mpagenko1cc3cb42020-07-27 15:24:38 +0000266 oFsm.pAdaptFsm.pFsm.Event(uniEvRestart)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000267 break loop
268 }
269 logger.Debugw("LockStateFsm Rx Msg", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID})
270
271 switch message.Type {
272 case TestMsg:
273 msg, _ := message.Data.(TestMessage)
274 if msg.TestMessageVal == AbortMessageProcessing {
275 logger.Infow("LockStateFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.pAdaptFsm.deviceID})
276 break loop
277 }
278 logger.Warnw("LockStateFsm unknown TestMessage", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID, "MessageVal": msg.TestMessageVal})
279 case OMCI:
280 msg, _ := message.Data.(OmciMessage)
281 oFsm.handleOmciLockStateMessage(msg)
282 default:
283 logger.Warn("LockStateFsm Rx unknown message", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID,
284 "message.Type": message.Type})
285 }
286 }
287 }
288 logger.Infow("End LockStateFsm Msg processing", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID})
289}
290
291func (oFsm *LockStateFsm) handleOmciLockStateMessage(msg OmciMessage) {
292 logger.Debugw("Rx OMCI LockStateFsm Msg", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID,
293 "msgType": msg.OmciMsg.MessageType})
294
295 if msg.OmciMsg.MessageType == omci.SetResponseType {
296 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeSetResponse)
297 if msgLayer == nil {
298 logger.Error("LockStateFsm - Omci Msg layer could not be detected for SetResponse")
299 return
300 }
301 msgObj, msgOk := msgLayer.(*omci.SetResponse)
302 if !msgOk {
303 logger.Error("LockStateFsm - Omci Msg layer could not be assigned for SetResponse")
304 return
305 }
306 logger.Debugw("LockStateFsm SetResponse Data", log.Fields{"deviceId": oFsm.pAdaptFsm.deviceID, "data-fields": msgObj})
307 if msgObj.Result != me.Success {
308 logger.Errorw("LockStateFsm - Omci SetResponse Error - later: drive FSM to abort state ?", log.Fields{"Error": msgObj.Result})
309 // possibly force FSM into abort or ignore some errors for some messages? store error for mgmt display?
310 return
311 }
312 // compare comments above for CreateResponse (apply also here ...)
313 if msgObj.EntityClass == oFsm.pOmciCC.pLastTxMeInstance.GetClassID() &&
314 msgObj.EntityInstance == oFsm.pOmciCC.pLastTxMeInstance.GetEntityID() {
315 //store the created ME into DB //TODO??? obviously the Python code does not store the config ...
316 // if, then something like:
317 //oFsm.pOnuDB.StoreMe(msgObj)
318
319 switch oFsm.pOmciCC.pLastTxMeInstance.GetName() {
320 case "OnuG":
321 { // let the FSM proceed ...
mpagenko1cc3cb42020-07-27 15:24:38 +0000322 oFsm.pAdaptFsm.pFsm.Event(uniEvRxOnugResp)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000323 }
324 case "UniG", "VEIP":
325 { // let the PPTP init proceed by stopping the wait function
326 oFsm.omciLockResponseReceived <- true
327 }
328 }
329 }
330 } else {
331 logger.Errorw("LockStateFsm - Rx OMCI unhandled MsgType", log.Fields{"omciMsgType": msg.OmciMsg.MessageType})
332 return
333 }
334}
335
336func (oFsm *LockStateFsm) performUniPortAdminSet() {
337 var omciAdminState uint8 = 1 //default locked
338 if oFsm.adminState == false {
339 omciAdminState = 0
340 }
341 //set UNI-G or VEIP AdminState
342 requestedAttributes := me.AttributeValueMap{"AdministrativeState": omciAdminState}
343
344 for uniNo, uniPort := range oFsm.pOmciCC.pBaseDeviceHandler.uniEntityMap {
345 logger.Debugw("Setting PPTP admin state", log.Fields{
346 "deviceId": oFsm.pAdaptFsm.deviceID, "for PortNo": uniNo})
347
348 var meInstance *me.ManagedEntity
349 if uniPort.portType == UniPPTP {
350 meInstance = oFsm.pOmciCC.sendSetUniGLS(context.TODO(), uniPort.entityId, ConstDefaultOmciTimeout,
351 true, requestedAttributes, oFsm.pAdaptFsm.commChan)
352 oFsm.pOmciCC.pLastTxMeInstance = meInstance
353 } else if uniPort.portType == UniVEIP {
354 meInstance = oFsm.pOmciCC.sendSetVeipLS(context.TODO(), uniPort.entityId, ConstDefaultOmciTimeout,
355 true, requestedAttributes, oFsm.pAdaptFsm.commChan)
356 oFsm.pOmciCC.pLastTxMeInstance = meInstance
357 } else {
358 logger.Warnw("Unsupported PPTP type - skip",
359 log.Fields{"deviceId": oFsm.pAdaptFsm.deviceID, "Port": uniNo})
360 continue
361 }
362
363 //verify response
mpagenko3af1f032020-06-10 08:53:41 +0000364 err := oFsm.waitforOmciResponse(meInstance)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000365 if err != nil {
366 logger.Errorw("PPTP Admin State set failed, aborting LockState set!",
367 log.Fields{"deviceId": oFsm.pAdaptFsm.deviceID, "Port": uniNo})
mpagenko1cc3cb42020-07-27 15:24:38 +0000368 oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000369 return
370 }
371 } //for all UNI ports
372 // if Config has been done for all UNI related instances let the FSM proceed
373 // while we did not check here, if there is some port at all - !?
374 logger.Infow("PPTP config loop finished", log.Fields{"deviceId": oFsm.pAdaptFsm.deviceID})
mpagenko1cc3cb42020-07-27 15:24:38 +0000375 oFsm.pAdaptFsm.pFsm.Event(uniEvRxUnisResp)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000376 return
377}
378
mpagenko3af1f032020-06-10 08:53:41 +0000379func (oFsm *LockStateFsm) waitforOmciResponse(apMeInstance *me.ManagedEntity) error {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000380 select {
381 // maybe be also some outside cancel (but no context modelled for the moment ...)
382 // case <-ctx.Done():
383 // logger.Infow("LockState-bridge-init message reception canceled", log.Fields{"for device-id": oFsm.pAdaptFsm.deviceID})
mpagenko3af1f032020-06-10 08:53:41 +0000384 case <-time.After(30 * time.Second): //3s was detected to be to less in 8*8 bbsim test with debug Info/Debug
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000385 logger.Warnw("LockStateFSM uni-set timeout", log.Fields{"for device-id": oFsm.pAdaptFsm.deviceID})
386 return errors.New("LockStateFsm uni-set timeout")
387 case success := <-oFsm.omciLockResponseReceived:
388 if success == true {
389 logger.Debug("LockStateFSM uni-set response received")
390 return nil
391 }
392 // should not happen so far
393 logger.Warnw("LockStateFSM uni-set response error", log.Fields{"for device-id": oFsm.pAdaptFsm.deviceID})
394 return errors.New("LockStateFsm uni-set responseError")
395 }
396}