blob: 58c5c9ae3a70b82f962938660591ed06545a6133 [file] [log] [blame]
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +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 (
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +000021 "context"
22 "errors"
23 "time"
24
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000025 "github.com/looplab/fsm"
26
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +000027 "github.com/opencord/omci-lib-go"
28 me "github.com/opencord/omci-lib-go/generated"
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000029 "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
35func (onuDeviceEntry *OnuDeviceEntry) enterDLStartingState(e *fsm.Event) {
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +000036 logger.Debugw("MibDownload FSM", log.Fields{"Start downloading OMCI MIB in state": e.FSM.Current(), "device-id": onuDeviceEntry.deviceID})
37 // in case the used channel is not yet defined (can be re-used after restarts)
38 if onuDeviceEntry.omciMessageReceived == nil {
39 onuDeviceEntry.omciMessageReceived = make(chan bool)
40 logger.Debug("MibDownload FSM - defining the BridgeInit RxChannel")
41 }
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000042 // start go routine for processing of MibDownload messages
43 go onuDeviceEntry.ProcessMibDownloadMessages()
44}
45
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +000046func (onuDeviceEntry *OnuDeviceEntry) enterCreatingGalState(e *fsm.Event) {
47 logger.Debugw("MibDownload FSM", log.Fields{"Tx create::GAL Ethernet Profile in state": e.FSM.Current(), "device-id": onuDeviceEntry.deviceID})
48 meInstance := onuDeviceEntry.PDevOmciCC.sendCreateGalEthernetProfile(context.TODO(), ConstDefaultOmciTimeout, true)
49 //accept also nil as (error) return value for writing to LastTx
50 // - this avoids misinterpretation of new received OMCI messages
51 onuDeviceEntry.PDevOmciCC.pLastTxMeInstance = meInstance
52}
53
54func (onuDeviceEntry *OnuDeviceEntry) enterSettingOnu2gState(e *fsm.Event) {
55 logger.Debugw("MibDownload FSM", log.Fields{"Tx Set::ONU2-G in state": e.FSM.Current(), "device-id": onuDeviceEntry.deviceID})
56 meInstance := onuDeviceEntry.PDevOmciCC.sendSetOnu2g(context.TODO(), ConstDefaultOmciTimeout, true)
57 //accept also nil as (error) return value for writing to LastTx
58 // - this avoids misinterpretation of new received OMCI messages
59 onuDeviceEntry.PDevOmciCC.pLastTxMeInstance = meInstance
60}
61
62func (onuDeviceEntry *OnuDeviceEntry) enterBridgeInitState(e *fsm.Event) {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000063 logger.Infow("MibDownload FSM - starting bridge config port loop", log.Fields{
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +000064 "in state": e.FSM.Current(), "device-id": onuDeviceEntry.deviceID})
65 go onuDeviceEntry.performInitialBridgeSetup()
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000066}
67
68func (onuDeviceEntry *OnuDeviceEntry) enterDownloadedState(e *fsm.Event) {
69 logger.Debugw("MibDownload FSM", log.Fields{"send notification to core in State": e.FSM.Current(), "device-id": onuDeviceEntry.deviceID})
70 onuDeviceEntry.transferSystemEvent(MibDownloadDone)
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +000071 //let's reset the state machine in order to release all resources now
72 pMibDlFsm := onuDeviceEntry.pMibDownloadFsm
73 if pMibDlFsm != nil {
74 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
75 go func(a_pAFsm *AdapterFsm) {
76 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
mpagenko1cc3cb42020-07-27 15:24:38 +000077 a_pAFsm.pFsm.Event(dlEvReset)
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +000078 }
79 }(pMibDlFsm)
80 }
81}
82
83func (onuDeviceEntry *OnuDeviceEntry) enterResettingState(e *fsm.Event) {
84 logger.Debugw("MibDownload FSM resetting", log.Fields{"device-id": onuDeviceEntry.deviceID})
85 pMibDlFsm := onuDeviceEntry.pMibDownloadFsm
86 if pMibDlFsm != nil {
87 // abort running message processing
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000088 fsmAbortMsg := Message{
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +000089 Type: TestMsg,
90 Data: TestMessage{
91 TestMessageVal: AbortMessageProcessing,
92 },
93 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000094 pMibDlFsm.commChan <- fsmAbortMsg
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +000095
96 //try to restart the FSM to 'disabled'
97 // see DownloadedState: decouple event transfer
98 go func(a_pAFsm *AdapterFsm) {
99 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
mpagenko1cc3cb42020-07-27 15:24:38 +0000100 a_pAFsm.pFsm.Event(dlEvRestart)
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000101 }
102 }(pMibDlFsm)
103 }
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000104}
105
106func (onuDeviceEntry *OnuDeviceEntry) ProcessMibDownloadMessages( /*ctx context.Context*/ ) {
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000107 logger.Debugw("Start MibDownload Msg processing", log.Fields{"for device-id": onuDeviceEntry.deviceID})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000108loop:
109 for {
110 select {
111 // case <-ctx.Done():
112 // logger.Info("MibSync Msg", log.Fields{"Message handling canceled via context for device-id": onuDeviceEntry.deviceID})
113 // break loop
114 case message, ok := <-onuDeviceEntry.pMibDownloadFsm.commChan:
115 if !ok {
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000116 logger.Info("MibDownload Rx Msg", log.Fields{"Message couldn't be read from channel for device-id": onuDeviceEntry.deviceID})
117 // but then we have to ensure a restart of the FSM as well - as exceptional procedure
mpagenko1cc3cb42020-07-27 15:24:38 +0000118 onuDeviceEntry.pMibDownloadFsm.pFsm.Event(dlEvRestart)
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000119 break loop
120 }
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000121 logger.Debugw("MibDownload Rx Msg", log.Fields{"Received message for device-id": onuDeviceEntry.deviceID})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000122
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000123 switch message.Type {
124 case TestMsg:
125 msg, _ := message.Data.(TestMessage)
126 if msg.TestMessageVal == AbortMessageProcessing {
127 logger.Infow("MibDownload abort ProcessMsg", log.Fields{"for device-id": onuDeviceEntry.deviceID})
128 break loop
129 }
130 logger.Warnw("MibDownload unknown TestMessage", log.Fields{"device-id": onuDeviceEntry.deviceID, "MessageVal": msg.TestMessageVal})
131 case OMCI:
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000132 msg, _ := message.Data.(OmciMessage)
133 onuDeviceEntry.handleOmciMibDownloadMessage(msg)
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000134 default:
135 logger.Warn("MibDownload Rx Msg", log.Fields{"Unknown message type received for device-id": onuDeviceEntry.deviceID,
136 "message.Type": message.Type})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000137 }
138 }
139 }
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000140 logger.Infow("End MibDownload Msg processing", log.Fields{"for device-id": onuDeviceEntry.deviceID})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000141}
142
143func (onuDeviceEntry *OnuDeviceEntry) handleOmciMibDownloadMessage(msg OmciMessage) {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000144 logger.Debugw("Rx OMCI MibDownload Msg", log.Fields{"device-id": onuDeviceEntry.deviceID,
145 "msgType": msg.OmciMsg.MessageType})
146
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000147 switch msg.OmciMsg.MessageType {
148 case omci.CreateResponseType:
149 {
150 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeCreateResponse)
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000151 if msgLayer == nil {
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000152 logger.Error("Omci Msg layer could not be detected for CreateResponse")
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000153 return
154 }
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000155 msgObj, msgOk := msgLayer.(*omci.CreateResponse)
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000156 if !msgOk {
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000157 logger.Error("Omci Msg layer could not be assigned for CreateResponse")
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000158 return
159 }
divyadesai4d299552020-08-18 07:13:49 +0000160 logger.Debugw("CreateResponse Data", log.Fields{"device-id": onuDeviceEntry.deviceID, "data-fields": msgObj})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000161 if msgObj.Result != me.Success {
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000162 logger.Errorw("Omci CreateResponse Error - later: drive FSM to abort state ?", log.Fields{"Error": msgObj.Result})
163 // possibly force FSM into abort or ignore some errors for some messages? store error for mgmt display?
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000164 return
165 }
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000166 // maybe there is a way of pushing the specific create response type generally to the FSM
167 // and let the FSM verify, if the response was according to current state
168 // and possibly store the element to DB and progress - maybe some future option ...
169 // but as that is not straightforward to me I insert the type checkes manually here
170 // and feed the FSM with only 'pre-defined' events ...
171 if msgObj.EntityClass == onuDeviceEntry.PDevOmciCC.pLastTxMeInstance.GetClassID() &&
172 msgObj.EntityInstance == onuDeviceEntry.PDevOmciCC.pLastTxMeInstance.GetEntityID() {
173 //store the created ME into DB //TODO??? obviously the Python code does not store the config ...
174 // if, then something like:
175 //onuDeviceEntry.pOnuDB.StoreMe(msgObj)
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000176
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000177 // maybe we can use just the same eventName for different state transitions like "forward"
178 // - might be checked, but so far I go for sure and have to inspect the concrete state events ...
179 switch onuDeviceEntry.PDevOmciCC.pLastTxMeInstance.GetName() {
180 case "GalEthernetProfile":
181 { // let the FSM proceed ...
mpagenko1cc3cb42020-07-27 15:24:38 +0000182 onuDeviceEntry.pMibDownloadFsm.pFsm.Event(dlEvRxGalResp)
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000183 }
184 case "MacBridgeServiceProfile",
185 "MacBridgePortConfigurationData",
186 "ExtendedVlanTaggingOperationConfigurationData":
187 { // let bridge init proceed by stopping the wait function
188 onuDeviceEntry.omciMessageReceived <- true
189 }
190 }
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000191 }
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000192 } //CreateResponseType
193 //TODO
194 // onuDeviceEntry.pMibDownloadFsm.pFsm.Event("rx_evtocd_resp")
195
196 case omci.SetResponseType:
197 {
198 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeSetResponse)
199 if msgLayer == nil {
200 logger.Error("Omci Msg layer could not be detected for SetResponse")
201 return
202 }
203 msgObj, msgOk := msgLayer.(*omci.SetResponse)
204 if !msgOk {
205 logger.Error("Omci Msg layer could not be assigned for SetResponse")
206 return
207 }
divyadesai4d299552020-08-18 07:13:49 +0000208 logger.Debugw("SetResponse Data", log.Fields{"device-id": onuDeviceEntry.deviceID, "data-fields": msgObj})
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000209 if msgObj.Result != me.Success {
210 logger.Errorw("Omci SetResponse Error - later: drive FSM to abort state ?", log.Fields{"Error": msgObj.Result})
211 // possibly force FSM into abort or ignore some errors for some messages? store error for mgmt display?
212 return
213 }
214 // compare comments above for CreateResponse (apply also here ...)
215 if msgObj.EntityClass == onuDeviceEntry.PDevOmciCC.pLastTxMeInstance.GetClassID() &&
216 msgObj.EntityInstance == onuDeviceEntry.PDevOmciCC.pLastTxMeInstance.GetEntityID() {
217 //store the created ME into DB //TODO??? obviously the Python code does not store the config ...
218 // if, then something like:
219 //onuDeviceEntry.pOnuDB.StoreMe(msgObj)
220
221 switch onuDeviceEntry.PDevOmciCC.pLastTxMeInstance.GetName() {
222 case "Onu2G":
223 { // let the FSM proceed ...
mpagenko1cc3cb42020-07-27 15:24:38 +0000224 onuDeviceEntry.pMibDownloadFsm.pFsm.Event(dlEvRxOnu2gResp)
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000225 }
226 //so far that was the only MibDownlad Set Element ...
227 }
228 }
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000229 } //SetResponseType
230 default:
231 {
232 logger.Errorw("Rx OMCI MibDownload unhandled MsgType", log.Fields{"omciMsgType": msg.OmciMsg.MessageType})
233 return
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000234 }
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000235 } // switch msg.OmciMsg.MessageType
236}
237
238func (onuDeviceEntry *OnuDeviceEntry) performInitialBridgeSetup() {
239 for uniNo, uniPort := range onuDeviceEntry.baseDeviceHandler.uniEntityMap {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000240 logger.Debugw("Starting IntialBridgeSetup", log.Fields{
divyadesai4d299552020-08-18 07:13:49 +0000241 "device-id": onuDeviceEntry.deviceID, "for PortNo": uniNo})
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000242
243 //create MBSP
244 meInstance := onuDeviceEntry.PDevOmciCC.sendCreateMBServiceProfile(
245 context.TODO(), uniPort, ConstDefaultOmciTimeout, true)
246 onuDeviceEntry.PDevOmciCC.pLastTxMeInstance = meInstance
247 //verify response
mpagenko3af1f032020-06-10 08:53:41 +0000248 err := onuDeviceEntry.waitforOmciResponse(meInstance)
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000249 if err != nil {
250 logger.Error("InitialBridgeSetup failed at MBSP, aborting MIB Download!")
mpagenko1cc3cb42020-07-27 15:24:38 +0000251 onuDeviceEntry.pMibDownloadFsm.pFsm.Event(dlEvReset)
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000252 return
253 }
254
255 //create MBPCD
256 meInstance = onuDeviceEntry.PDevOmciCC.sendCreateMBPConfigData(
257 context.TODO(), uniPort, ConstDefaultOmciTimeout, true)
258 onuDeviceEntry.PDevOmciCC.pLastTxMeInstance = meInstance
259 //verify response
mpagenko3af1f032020-06-10 08:53:41 +0000260 err = onuDeviceEntry.waitforOmciResponse(meInstance)
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000261 if err != nil {
262 logger.Error("InitialBridgeSetup failed at MBPCD, aborting MIB Download!")
mpagenko1cc3cb42020-07-27 15:24:38 +0000263 onuDeviceEntry.pMibDownloadFsm.pFsm.Event(dlEvReset)
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000264 return
265 }
266
267 //create EVTOCD
268 meInstance = onuDeviceEntry.PDevOmciCC.sendCreateEVTOConfigData(
269 context.TODO(), uniPort, ConstDefaultOmciTimeout, true)
270 onuDeviceEntry.PDevOmciCC.pLastTxMeInstance = meInstance
271 //verify response
mpagenko3af1f032020-06-10 08:53:41 +0000272 err = onuDeviceEntry.waitforOmciResponse(meInstance)
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000273 if err != nil {
274 logger.Error("InitialBridgeSetup failed at EVTOCD, aborting MIB Download!")
mpagenko1cc3cb42020-07-27 15:24:38 +0000275 onuDeviceEntry.pMibDownloadFsm.pFsm.Event(dlEvReset)
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000276 return
277 }
278 }
279 // if Config has been done for all UNI related instances let the FSM proceed
280 // while we did not check here, if there is some port at all - !?
divyadesai4d299552020-08-18 07:13:49 +0000281 logger.Infow("IntialBridgeSetup finished", log.Fields{"device-id": onuDeviceEntry.deviceID})
mpagenko1cc3cb42020-07-27 15:24:38 +0000282 onuDeviceEntry.pMibDownloadFsm.pFsm.Event(dlEvRxBridgeResp)
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000283 return
284}
285
mpagenko3af1f032020-06-10 08:53:41 +0000286func (onuDeviceEntry *OnuDeviceEntry) waitforOmciResponse(apMeInstance *me.ManagedEntity) error {
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000287 select {
288 // maybe be also some outside cancel (but no context modelled for the moment ...)
289 // case <-ctx.Done():
290 // logger.Info("MibDownload-bridge-init message reception canceled", log.Fields{"for device-id": onuDeviceEntry.deviceID})
mpagenko3af1f032020-06-10 08:53:41 +0000291 case <-time.After(30 * time.Second): //3s was detected to be to less in 8*8 bbsim test with debug Info/Debug
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000292 logger.Warnw("MibDownload-bridge-init timeout", log.Fields{"for device-id": onuDeviceEntry.deviceID})
293 return errors.New("MibDownloadBridgeInit timeout")
294 case success := <-onuDeviceEntry.omciMessageReceived:
295 if success == true {
296 logger.Debug("MibDownload-bridge-init response received")
297 return nil
298 }
299 // should not happen so far
300 logger.Warnw("MibDownload-bridge-init response error", log.Fields{"for device-id": onuDeviceEntry.deviceID})
301 return errors.New("MibDownloadBridgeInit responseError")
302 }
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000303}