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