blob: ed30b41819e0a2faabcbf137cc3f4873802e03ea [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
44//NewLockStateFsm is the 'constructor' for the state machine to lock/unlock the ONU UNI ports via OMCI
45func NewLockStateFsm(apDevOmciCC *OmciCC, aAdminState bool, aRequestEvent OnuDeviceEvent,
46 aName string, aDeviceID string, aCommChannel chan Message) *LockStateFsm {
47 instFsm := &LockStateFsm{
48 pOmciCC: apDevOmciCC,
49 adminState: aAdminState,
50 requestEvent: aRequestEvent,
51 }
52 instFsm.pAdaptFsm = NewAdapterFsm(aName, aDeviceID, aCommChannel)
53 if instFsm.pAdaptFsm == nil {
54 logger.Errorw("LockStateFsm's AdapterFsm could not be instantiated!!", log.Fields{
55 "device-id": aDeviceID})
56 return nil
57 }
58 if aAdminState == true { //port locking requested
59 instFsm.pAdaptFsm.pFsm = fsm.NewFSM(
60 "disabled",
61 fsm.Events{
62
63 {Name: "start", Src: []string{"disabled"}, Dst: "starting"},
64
65 {Name: "start_admin", Src: []string{"starting"}, Dst: "settingUnis"},
66 // the settingUnis state is used for multi ME config for alle UNI related ports
67 // maybe such could be reflected in the state machine as well (port number parametrized)
68 // but that looks not straightforward here - so we keep it simple here for the beginning(?)
69 {Name: "rx_unis_resp", Src: []string{"settingUnis"}, Dst: "settingOnuG"},
70 {Name: "rx_onug_resp", Src: []string{"settingOnuG"}, Dst: "adminDone"},
71
72 {Name: "timeout_simple", Src: []string{"settingOnuG"}, Dst: "starting"},
73 {Name: "timeout_unis", Src: []string{"settingUnis"}, Dst: "starting"},
74
75 {Name: "reset", Src: []string{"starting", "settingOnuG", "settingUnis",
76 "adminDone"}, Dst: "resetting"},
77 // exceptional treatment for all states except "resetting"
78 {Name: "restart", Src: []string{"starting", "settingOnuG", "settingUnis",
79 "adminDone", "resetting"}, Dst: "disabled"},
80 },
81
82 fsm.Callbacks{
83 "enter_state": func(e *fsm.Event) { instFsm.pAdaptFsm.logFsmStateChange(e) },
84 "enter_starting": func(e *fsm.Event) { instFsm.enterAdminStartingState(e) },
85 "enter_settingOnuG": func(e *fsm.Event) { instFsm.enterSettingOnuGState(e) },
86 "enter_settingUnis": func(e *fsm.Event) { instFsm.enterSettingUnisState(e) },
87 "enter_adminDone": func(e *fsm.Event) { instFsm.enterAdminDoneState(e) },
88 "enter_resetting": func(e *fsm.Event) { instFsm.enterResettingState(e) },
89 },
90 )
91 } else { //port unlocking requested
92 instFsm.pAdaptFsm.pFsm = fsm.NewFSM(
93 "disabled",
94 fsm.Events{
95
96 {Name: "start", Src: []string{"disabled"}, Dst: "starting"},
97
98 {Name: "start_admin", Src: []string{"starting"}, Dst: "settingOnuG"},
99 {Name: "rx_onug_resp", Src: []string{"settingOnuG"}, Dst: "settingUnis"},
100 // the settingUnis state is used for multi ME config for alle UNI related ports
101 // maybe such could be reflected in the state machine as well (port number parametrized)
102 // but that looks not straightforward here - so we keep it simple here for the beginning(?)
103 {Name: "rx_unis_resp", Src: []string{"settingUnis"}, Dst: "adminDone"},
104
105 {Name: "timeout_simple", Src: []string{"settingOnuG"}, Dst: "starting"},
106 {Name: "timeout_unis", Src: []string{"settingUnis"}, Dst: "starting"},
107
108 {Name: "reset", Src: []string{"starting", "settingOnuG", "settingUnis",
109 "adminDone"}, Dst: "resetting"},
110 // exceptional treatment for all states except "resetting"
111 {Name: "restart", Src: []string{"starting", "settingOnuG", "settingUnis",
112 "adminDone", "resetting"}, Dst: "disabled"},
113 },
114
115 fsm.Callbacks{
116 "enter_state": func(e *fsm.Event) { instFsm.pAdaptFsm.logFsmStateChange(e) },
117 "enter_starting": func(e *fsm.Event) { instFsm.enterAdminStartingState(e) },
118 "enter_settingOnuG": func(e *fsm.Event) { instFsm.enterSettingOnuGState(e) },
119 "enter_settingUnis": func(e *fsm.Event) { instFsm.enterSettingUnisState(e) },
120 "enter_adminDone": func(e *fsm.Event) { instFsm.enterAdminDoneState(e) },
121 "enter_resetting": func(e *fsm.Event) { instFsm.enterResettingState(e) },
122 },
123 )
124 }
125 if instFsm.pAdaptFsm.pFsm == nil {
126 logger.Errorw("LockStateFsm's Base FSM could not be instantiated!!", log.Fields{
127 "device-id": aDeviceID})
128 return nil
129 }
130
131 logger.Infow("LockStateFsm created", log.Fields{"device-id": aDeviceID})
132 return instFsm
133}
134
135//SetSuccessEvent modifies the requested event notified on success
136//assumption is that this is only called in the disabled (idle) state of the FSM, hence no sem protection required
137func (oFsm *LockStateFsm) SetSuccessEvent(aEvent OnuDeviceEvent) {
138 oFsm.requestEvent = aEvent
139}
140
141func (oFsm *LockStateFsm) enterAdminStartingState(e *fsm.Event) {
142 logger.Debugw("LockStateFSM start", log.Fields{"in state": e.FSM.Current(),
143 "device-id": oFsm.pAdaptFsm.deviceID})
144 // in case the used channel is not yet defined (can be re-used after restarts)
145 if oFsm.omciLockResponseReceived == nil {
146 oFsm.omciLockResponseReceived = make(chan bool)
147 logger.Debug("LockStateFSM - OMCI UniLock RxChannel defined")
148 } else {
149 // as we may 're-use' this instance of FSM and the connected channel
150 // make sure there is no 'lingering' request in the already existing channel:
151 // (simple loop sufficient as we are the only receiver)
152 for len(oFsm.omciLockResponseReceived) > 0 {
153 <-oFsm.omciLockResponseReceived
154 }
155 }
156 // start go routine for processing of LockState messages
157 go oFsm.ProcessOmciLockMessages()
158
159 //let the state machine run forward from here directly
160 pLockStateAFsm := oFsm.pAdaptFsm
161 if pLockStateAFsm != nil {
162 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
163 go func(a_pAFsm *AdapterFsm) {
164 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
165 a_pAFsm.pFsm.Event("start_admin")
166 }
167 }(pLockStateAFsm)
168 }
169}
170
171func (oFsm *LockStateFsm) enterSettingOnuGState(e *fsm.Event) {
172 var omciAdminState uint8 = 1 //default locked
173 if oFsm.adminState == false {
174 omciAdminState = 0
175 }
176 logger.Debugw("LockStateFSM Tx Set::ONU-G:admin", log.Fields{
177 "omciAdmin": omciAdminState, "in state": e.FSM.Current(), "device-id": oFsm.pAdaptFsm.deviceID})
178 requestedAttributes := me.AttributeValueMap{"AdministrativeState": omciAdminState}
179 meInstance := oFsm.pOmciCC.sendSetOnuGLS(context.TODO(), ConstDefaultOmciTimeout, true,
180 requestedAttributes, oFsm.pAdaptFsm.commChan)
181 //accept also nil as (error) return value for writing to LastTx
182 // - this avoids misinterpretation of new received OMCI messages
183 // we might already abort the processing with nil here, but maybe some auto-recovery may be tried
184 // - may be improved later, for now we just handle it with the Rx timeout or missing next event (stick in state)
185 oFsm.pOmciCC.pLastTxMeInstance = meInstance
186}
187
188func (oFsm *LockStateFsm) enterSettingUnisState(e *fsm.Event) {
189 logger.Infow("LockStateFSM - starting PPTP config loop", log.Fields{
190 "in state": e.FSM.Current(), "device-id": oFsm.pAdaptFsm.deviceID, "LockState": oFsm.adminState})
191 go oFsm.performUniPortAdminSet()
192}
193
194func (oFsm *LockStateFsm) enterAdminDoneState(e *fsm.Event) {
195 logger.Debugw("LockStateFSM", log.Fields{"send notification to core in State": e.FSM.Current(), "device-id": oFsm.pAdaptFsm.deviceID})
196 //use DeviceHandler event notification directly, no need/support to update DeviceEntryState for lock/unlock
197 oFsm.pOmciCC.pBaseDeviceHandler.DeviceProcStatusUpdate(oFsm.requestEvent)
198 //let's reset the state machine in order to release all resources now
199 pLockStateAFsm := oFsm.pAdaptFsm
200 if pLockStateAFsm != nil {
201 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
202 go func(a_pAFsm *AdapterFsm) {
203 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
204 a_pAFsm.pFsm.Event("reset")
205 }
206 }(pLockStateAFsm)
207 }
208}
209
210func (oFsm *LockStateFsm) enterResettingState(e *fsm.Event) {
211 logger.Debugw("LockStateFSM resetting", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID})
212 pLockStateAFsm := oFsm.pAdaptFsm
213 if pLockStateAFsm != nil {
214 // abort running message processing
215 fsmAbortMsg := Message{
216 Type: TestMsg,
217 Data: TestMessage{
218 TestMessageVal: AbortMessageProcessing,
219 },
220 }
221 pLockStateAFsm.commChan <- fsmAbortMsg
222
223 //try to restart the FSM to 'disabled'
224 // see DownloadedState: decouple event transfer
225 go func(a_pAFsm *AdapterFsm) {
226 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
227 a_pAFsm.pFsm.Event("restart")
228 }
229 }(pLockStateAFsm)
230 }
231}
232
233func (oFsm *LockStateFsm) ProcessOmciLockMessages( /*ctx context.Context*/ ) {
234 logger.Debugw("Start LockStateFsm Msg processing", log.Fields{"for device-id": oFsm.pAdaptFsm.deviceID})
235loop:
236 for {
237 select {
238 // case <-ctx.Done():
239 // logger.Info("MibSync Msg", log.Fields{"Message handling canceled via context for device-id": oFsm.pAdaptFsm.deviceID})
240 // break loop
241 case message, ok := <-oFsm.pAdaptFsm.commChan:
242 if !ok {
243 logger.Info("LockStateFsm Rx Msg - could not read from channel", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID})
244 // but then we have to ensure a restart of the FSM as well - as exceptional procedure
245 oFsm.pAdaptFsm.pFsm.Event("restart")
246 break loop
247 }
248 logger.Debugw("LockStateFsm Rx Msg", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID})
249
250 switch message.Type {
251 case TestMsg:
252 msg, _ := message.Data.(TestMessage)
253 if msg.TestMessageVal == AbortMessageProcessing {
254 logger.Infow("LockStateFsm abort ProcessMsg", log.Fields{"for device-id": oFsm.pAdaptFsm.deviceID})
255 break loop
256 }
257 logger.Warnw("LockStateFsm unknown TestMessage", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID, "MessageVal": msg.TestMessageVal})
258 case OMCI:
259 msg, _ := message.Data.(OmciMessage)
260 oFsm.handleOmciLockStateMessage(msg)
261 default:
262 logger.Warn("LockStateFsm Rx unknown message", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID,
263 "message.Type": message.Type})
264 }
265 }
266 }
267 logger.Infow("End LockStateFsm Msg processing", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID})
268}
269
270func (oFsm *LockStateFsm) handleOmciLockStateMessage(msg OmciMessage) {
271 logger.Debugw("Rx OMCI LockStateFsm Msg", log.Fields{"device-id": oFsm.pAdaptFsm.deviceID,
272 "msgType": msg.OmciMsg.MessageType})
273
274 if msg.OmciMsg.MessageType == omci.SetResponseType {
275 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeSetResponse)
276 if msgLayer == nil {
277 logger.Error("LockStateFsm - Omci Msg layer could not be detected for SetResponse")
278 return
279 }
280 msgObj, msgOk := msgLayer.(*omci.SetResponse)
281 if !msgOk {
282 logger.Error("LockStateFsm - Omci Msg layer could not be assigned for SetResponse")
283 return
284 }
285 logger.Debugw("LockStateFsm SetResponse Data", log.Fields{"deviceId": oFsm.pAdaptFsm.deviceID, "data-fields": msgObj})
286 if msgObj.Result != me.Success {
287 logger.Errorw("LockStateFsm - Omci SetResponse Error - later: drive FSM to abort state ?", log.Fields{"Error": msgObj.Result})
288 // possibly force FSM into abort or ignore some errors for some messages? store error for mgmt display?
289 return
290 }
291 // compare comments above for CreateResponse (apply also here ...)
292 if msgObj.EntityClass == oFsm.pOmciCC.pLastTxMeInstance.GetClassID() &&
293 msgObj.EntityInstance == oFsm.pOmciCC.pLastTxMeInstance.GetEntityID() {
294 //store the created ME into DB //TODO??? obviously the Python code does not store the config ...
295 // if, then something like:
296 //oFsm.pOnuDB.StoreMe(msgObj)
297
298 switch oFsm.pOmciCC.pLastTxMeInstance.GetName() {
299 case "OnuG":
300 { // let the FSM proceed ...
301 oFsm.pAdaptFsm.pFsm.Event("rx_onug_resp")
302 }
303 case "UniG", "VEIP":
304 { // let the PPTP init proceed by stopping the wait function
305 oFsm.omciLockResponseReceived <- true
306 }
307 }
308 }
309 } else {
310 logger.Errorw("LockStateFsm - Rx OMCI unhandled MsgType", log.Fields{"omciMsgType": msg.OmciMsg.MessageType})
311 return
312 }
313}
314
315func (oFsm *LockStateFsm) performUniPortAdminSet() {
316 var omciAdminState uint8 = 1 //default locked
317 if oFsm.adminState == false {
318 omciAdminState = 0
319 }
320 //set UNI-G or VEIP AdminState
321 requestedAttributes := me.AttributeValueMap{"AdministrativeState": omciAdminState}
322
323 for uniNo, uniPort := range oFsm.pOmciCC.pBaseDeviceHandler.uniEntityMap {
324 logger.Debugw("Setting PPTP admin state", log.Fields{
325 "deviceId": oFsm.pAdaptFsm.deviceID, "for PortNo": uniNo})
326
327 var meInstance *me.ManagedEntity
328 if uniPort.portType == UniPPTP {
329 meInstance = oFsm.pOmciCC.sendSetUniGLS(context.TODO(), uniPort.entityId, ConstDefaultOmciTimeout,
330 true, requestedAttributes, oFsm.pAdaptFsm.commChan)
331 oFsm.pOmciCC.pLastTxMeInstance = meInstance
332 } else if uniPort.portType == UniVEIP {
333 meInstance = oFsm.pOmciCC.sendSetVeipLS(context.TODO(), uniPort.entityId, ConstDefaultOmciTimeout,
334 true, requestedAttributes, oFsm.pAdaptFsm.commChan)
335 oFsm.pOmciCC.pLastTxMeInstance = meInstance
336 } else {
337 logger.Warnw("Unsupported PPTP type - skip",
338 log.Fields{"deviceId": oFsm.pAdaptFsm.deviceID, "Port": uniNo})
339 continue
340 }
341
342 //verify response
mpagenko3af1f032020-06-10 08:53:41 +0000343 err := oFsm.waitforOmciResponse(meInstance)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000344 if err != nil {
345 logger.Errorw("PPTP Admin State set failed, aborting LockState set!",
346 log.Fields{"deviceId": oFsm.pAdaptFsm.deviceID, "Port": uniNo})
347 oFsm.pAdaptFsm.pFsm.Event("reset")
348 return
349 }
350 } //for all UNI ports
351 // if Config has been done for all UNI related instances let the FSM proceed
352 // while we did not check here, if there is some port at all - !?
353 logger.Infow("PPTP config loop finished", log.Fields{"deviceId": oFsm.pAdaptFsm.deviceID})
354 oFsm.pAdaptFsm.pFsm.Event("rx_unis_resp")
355 return
356}
357
mpagenko3af1f032020-06-10 08:53:41 +0000358func (oFsm *LockStateFsm) waitforOmciResponse(apMeInstance *me.ManagedEntity) error {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000359 select {
360 // maybe be also some outside cancel (but no context modelled for the moment ...)
361 // case <-ctx.Done():
362 // logger.Infow("LockState-bridge-init message reception canceled", log.Fields{"for device-id": oFsm.pAdaptFsm.deviceID})
mpagenko3af1f032020-06-10 08:53:41 +0000363 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 +0000364 logger.Warnw("LockStateFSM uni-set timeout", log.Fields{"for device-id": oFsm.pAdaptFsm.deviceID})
365 return errors.New("LockStateFsm uni-set timeout")
366 case success := <-oFsm.omciLockResponseReceived:
367 if success == true {
368 logger.Debug("LockStateFSM uni-set response received")
369 return nil
370 }
371 // should not happen so far
372 logger.Warnw("LockStateFSM uni-set response error", log.Fields{"for device-id": oFsm.pAdaptFsm.deviceID})
373 return errors.New("LockStateFsm uni-set responseError")
374 }
375}