blob: 03c328dfae18b3c70ec527449cd32ee4afa43984 [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()
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +000044 //possibly include a further MibReset here in order to enforce reset of incomplete set data ...(TODO?)
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000045}
46
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +000047func (onuDeviceEntry *OnuDeviceEntry) enterCreatingGalState(e *fsm.Event) {
48 logger.Debugw("MibDownload FSM", log.Fields{"Tx create::GAL Ethernet Profile in state": e.FSM.Current(), "device-id": onuDeviceEntry.deviceID})
49 meInstance := onuDeviceEntry.PDevOmciCC.sendCreateGalEthernetProfile(context.TODO(), ConstDefaultOmciTimeout, true)
50 //accept also nil as (error) return value for writing to LastTx
51 // - this avoids misinterpretation of new received OMCI messages
52 onuDeviceEntry.PDevOmciCC.pLastTxMeInstance = meInstance
53}
54
55func (onuDeviceEntry *OnuDeviceEntry) enterSettingOnu2gState(e *fsm.Event) {
56 logger.Debugw("MibDownload FSM", log.Fields{"Tx Set::ONU2-G in state": e.FSM.Current(), "device-id": onuDeviceEntry.deviceID})
57 meInstance := onuDeviceEntry.PDevOmciCC.sendSetOnu2g(context.TODO(), ConstDefaultOmciTimeout, true)
58 //accept also nil as (error) return value for writing to LastTx
59 // - this avoids misinterpretation of new received OMCI messages
60 onuDeviceEntry.PDevOmciCC.pLastTxMeInstance = meInstance
61}
62
63func (onuDeviceEntry *OnuDeviceEntry) enterBridgeInitState(e *fsm.Event) {
64 logger.Debugw("MibDownload FSM - starting bridge config loop", log.Fields{
65 "in state": e.FSM.Current(), "device-id": onuDeviceEntry.deviceID})
66 go onuDeviceEntry.performInitialBridgeSetup()
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000067}
68
69func (onuDeviceEntry *OnuDeviceEntry) enterDownloadedState(e *fsm.Event) {
70 logger.Debugw("MibDownload FSM", log.Fields{"send notification to core in State": e.FSM.Current(), "device-id": onuDeviceEntry.deviceID})
71 onuDeviceEntry.transferSystemEvent(MibDownloadDone)
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +000072 //let's reset the state machine in order to release all resources now
73 pMibDlFsm := onuDeviceEntry.pMibDownloadFsm
74 if pMibDlFsm != nil {
75 // obviously calling some FSM event here directly does not work - so trying to decouple it ...
76 go func(a_pAFsm *AdapterFsm) {
77 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
78 a_pAFsm.pFsm.Event("reset")
79 }
80 }(pMibDlFsm)
81 }
82}
83
84func (onuDeviceEntry *OnuDeviceEntry) enterResettingState(e *fsm.Event) {
85 logger.Debugw("MibDownload FSM resetting", log.Fields{"device-id": onuDeviceEntry.deviceID})
86 pMibDlFsm := onuDeviceEntry.pMibDownloadFsm
87 if pMibDlFsm != nil {
88 // abort running message processing
89 mibSyncMsg := Message{
90 Type: TestMsg,
91 Data: TestMessage{
92 TestMessageVal: AbortMessageProcessing,
93 },
94 }
95 pMibDlFsm.commChan <- mibSyncMsg
96
97 //try to restart the FSM to 'disabled'
98 // see DownloadedState: decouple event transfer
99 go func(a_pAFsm *AdapterFsm) {
100 if a_pAFsm != nil && a_pAFsm.pFsm != nil {
101 a_pAFsm.pFsm.Event("restart")
102 }
103 }(pMibDlFsm)
104 }
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000105}
106
107func (onuDeviceEntry *OnuDeviceEntry) ProcessMibDownloadMessages( /*ctx context.Context*/ ) {
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000108 logger.Debugw("Start MibDownload Msg processing", log.Fields{"for device-id": onuDeviceEntry.deviceID})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000109loop:
110 for {
111 select {
112 // case <-ctx.Done():
113 // logger.Info("MibSync Msg", log.Fields{"Message handling canceled via context for device-id": onuDeviceEntry.deviceID})
114 // break loop
115 case message, ok := <-onuDeviceEntry.pMibDownloadFsm.commChan:
116 if !ok {
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000117 logger.Info("MibDownload Rx Msg", log.Fields{"Message couldn't be read from channel for device-id": onuDeviceEntry.deviceID})
118 // but then we have to ensure a restart of the FSM as well - as exceptional procedure
119 onuDeviceEntry.pMibDownloadFsm.pFsm.Event("restart")
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000120 break loop
121 }
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000122 logger.Debugw("MibDownload Rx Msg", log.Fields{"Received message for device-id": onuDeviceEntry.deviceID})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000123
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000124 switch message.Type {
125 case TestMsg:
126 msg, _ := message.Data.(TestMessage)
127 if msg.TestMessageVal == AbortMessageProcessing {
128 logger.Infow("MibDownload abort ProcessMsg", log.Fields{"for device-id": onuDeviceEntry.deviceID})
129 break loop
130 }
131 logger.Warnw("MibDownload unknown TestMessage", log.Fields{"device-id": onuDeviceEntry.deviceID, "MessageVal": msg.TestMessageVal})
132 case OMCI:
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000133 msg, _ := message.Data.(OmciMessage)
134 onuDeviceEntry.handleOmciMibDownloadMessage(msg)
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000135 default:
136 logger.Warn("MibDownload Rx Msg", log.Fields{"Unknown message type received for device-id": onuDeviceEntry.deviceID,
137 "message.Type": message.Type})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000138 }
139 }
140 }
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000141 logger.Infow("End MibDownload Msg processing", log.Fields{"for device-id": onuDeviceEntry.deviceID})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000142}
143
144func (onuDeviceEntry *OnuDeviceEntry) handleOmciMibDownloadMessage(msg OmciMessage) {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000145 logger.Debugw("Rx OMCI MibDownload Msg", log.Fields{"device-id": onuDeviceEntry.deviceID,
146 "msgType": msg.OmciMsg.MessageType})
147
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000148 switch msg.OmciMsg.MessageType {
149 case omci.CreateResponseType:
150 {
151 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeCreateResponse)
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000152 if msgLayer == nil {
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000153 logger.Error("Omci Msg layer could not be detected for CreateResponse")
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000154 return
155 }
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000156 msgObj, msgOk := msgLayer.(*omci.CreateResponse)
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000157 if !msgOk {
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000158 logger.Error("Omci Msg layer could not be assigned for CreateResponse")
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000159 return
160 }
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000161 logger.Debugw("CreateResponse Data", log.Fields{"deviceId": onuDeviceEntry.deviceID, "data-fields": msgObj})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000162 if msgObj.Result != me.Success {
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000163 logger.Errorw("Omci CreateResponse Error - later: drive FSM to abort state ?", log.Fields{"Error": msgObj.Result})
164 // possibly force FSM into abort or ignore some errors for some messages? store error for mgmt display?
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000165 return
166 }
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000167 // maybe there is a way of pushing the specific create response type generally to the FSM
168 // and let the FSM verify, if the response was according to current state
169 // and possibly store the element to DB and progress - maybe some future option ...
170 // but as that is not straightforward to me I insert the type checkes manually here
171 // and feed the FSM with only 'pre-defined' events ...
172 if msgObj.EntityClass == onuDeviceEntry.PDevOmciCC.pLastTxMeInstance.GetClassID() &&
173 msgObj.EntityInstance == onuDeviceEntry.PDevOmciCC.pLastTxMeInstance.GetEntityID() {
174 //store the created ME into DB //TODO??? obviously the Python code does not store the config ...
175 // if, then something like:
176 //onuDeviceEntry.pOnuDB.StoreMe(msgObj)
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000177
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000178 // maybe we can use just the same eventName for different state transitions like "forward"
179 // - might be checked, but so far I go for sure and have to inspect the concrete state events ...
180 switch onuDeviceEntry.PDevOmciCC.pLastTxMeInstance.GetName() {
181 case "GalEthernetProfile":
182 { // let the FSM proceed ...
183 onuDeviceEntry.pMibDownloadFsm.pFsm.Event("rx_gal_resp")
184 }
185 case "MacBridgeServiceProfile",
186 "MacBridgePortConfigurationData",
187 "ExtendedVlanTaggingOperationConfigurationData":
188 { // let bridge init proceed by stopping the wait function
189 onuDeviceEntry.omciMessageReceived <- true
190 }
191 }
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000192 }
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000193 } //CreateResponseType
194 //TODO
195 // onuDeviceEntry.pMibDownloadFsm.pFsm.Event("rx_evtocd_resp")
196
197 case omci.SetResponseType:
198 {
199 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeSetResponse)
200 if msgLayer == nil {
201 logger.Error("Omci Msg layer could not be detected for SetResponse")
202 return
203 }
204 msgObj, msgOk := msgLayer.(*omci.SetResponse)
205 if !msgOk {
206 logger.Error("Omci Msg layer could not be assigned for SetResponse")
207 return
208 }
209 logger.Debugw("SetResponse Data for:", log.Fields{"deviceId": onuDeviceEntry.deviceID, "data-fields": msgObj})
210 if msgObj.Result != me.Success {
211 logger.Errorw("Omci SetResponse Error - later: drive FSM to abort state ?", log.Fields{"Error": msgObj.Result})
212 // possibly force FSM into abort or ignore some errors for some messages? store error for mgmt display?
213 return
214 }
215 logger.Debugw("SetResponse Data", log.Fields{"deviceId": onuDeviceEntry.deviceID, "data-fields": msgObj})
216 if msgObj.Result != me.Success {
217 logger.Errorw("Omci SetResponse Error - later: drive FSM to abort state ?", log.Fields{"Error": msgObj.Result})
218 // possibly force FSM into abort or ignore some errors for some messages? store error for mgmt display?
219 return
220 }
221 // compare comments above for CreateResponse (apply also here ...)
222 if msgObj.EntityClass == onuDeviceEntry.PDevOmciCC.pLastTxMeInstance.GetClassID() &&
223 msgObj.EntityInstance == onuDeviceEntry.PDevOmciCC.pLastTxMeInstance.GetEntityID() {
224 //store the created ME into DB //TODO??? obviously the Python code does not store the config ...
225 // if, then something like:
226 //onuDeviceEntry.pOnuDB.StoreMe(msgObj)
227
228 switch onuDeviceEntry.PDevOmciCC.pLastTxMeInstance.GetName() {
229 case "Onu2G":
230 { // let the FSM proceed ...
231 onuDeviceEntry.pMibDownloadFsm.pFsm.Event("rx_onu2g_resp")
232 }
233 //so far that was the only MibDownlad Set Element ...
234 }
235 }
236
237 // TODO!!!: further needed processing here ....
238
239 } //SetResponseType
240 default:
241 {
242 logger.Errorw("Rx OMCI MibDownload unhandled MsgType", log.Fields{"omciMsgType": msg.OmciMsg.MessageType})
243 return
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000244 }
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +0000245 } // switch msg.OmciMsg.MessageType
246}
247
248func (onuDeviceEntry *OnuDeviceEntry) performInitialBridgeSetup() {
249 for uniNo, uniPort := range onuDeviceEntry.baseDeviceHandler.uniEntityMap {
250 logger.Infow("Starting IntialBridgeSetup", log.Fields{
251 "deviceId": onuDeviceEntry.deviceID, "for PortNo": uniNo})
252
253 //create MBSP
254 meInstance := onuDeviceEntry.PDevOmciCC.sendCreateMBServiceProfile(
255 context.TODO(), uniPort, ConstDefaultOmciTimeout, true)
256 onuDeviceEntry.PDevOmciCC.pLastTxMeInstance = meInstance
257 //verify response
258 err := onuDeviceEntry.WaitforOmciResponse(meInstance)
259 if err != nil {
260 logger.Error("InitialBridgeSetup failed at MBSP, aborting MIB Download!")
261 onuDeviceEntry.pMibDownloadFsm.pFsm.Event("reset")
262 return
263 }
264
265 //create MBPCD
266 meInstance = onuDeviceEntry.PDevOmciCC.sendCreateMBPConfigData(
267 context.TODO(), uniPort, ConstDefaultOmciTimeout, true)
268 onuDeviceEntry.PDevOmciCC.pLastTxMeInstance = meInstance
269 //verify response
270 err = onuDeviceEntry.WaitforOmciResponse(meInstance)
271 if err != nil {
272 logger.Error("InitialBridgeSetup failed at MBPCD, aborting MIB Download!")
273 onuDeviceEntry.pMibDownloadFsm.pFsm.Event("reset")
274 return
275 }
276
277 //create EVTOCD
278 meInstance = onuDeviceEntry.PDevOmciCC.sendCreateEVTOConfigData(
279 context.TODO(), uniPort, ConstDefaultOmciTimeout, true)
280 onuDeviceEntry.PDevOmciCC.pLastTxMeInstance = meInstance
281 //verify response
282 err = onuDeviceEntry.WaitforOmciResponse(meInstance)
283 if err != nil {
284 logger.Error("InitialBridgeSetup failed at EVTOCD, aborting MIB Download!")
285 onuDeviceEntry.pMibDownloadFsm.pFsm.Event("reset")
286 return
287 }
288 }
289 // if Config has been done for all UNI related instances let the FSM proceed
290 // while we did not check here, if there is some port at all - !?
291 logger.Infow("IntialBridgeSetup finished", log.Fields{"deviceId": onuDeviceEntry.deviceID})
292 onuDeviceEntry.pMibDownloadFsm.pFsm.Event("rx_bridge_resp")
293 return
294}
295
296func (onuDeviceEntry *OnuDeviceEntry) WaitforOmciResponse(a_pMeInstance *me.ManagedEntity) error {
297 select {
298 // maybe be also some outside cancel (but no context modelled for the moment ...)
299 // case <-ctx.Done():
300 // logger.Info("MibDownload-bridge-init message reception canceled", log.Fields{"for device-id": onuDeviceEntry.deviceID})
301 case <-time.After(3 * time.Second):
302 logger.Warnw("MibDownload-bridge-init timeout", log.Fields{"for device-id": onuDeviceEntry.deviceID})
303 return errors.New("MibDownloadBridgeInit timeout")
304 case success := <-onuDeviceEntry.omciMessageReceived:
305 if success == true {
306 logger.Debug("MibDownload-bridge-init response received")
307 return nil
308 }
309 // should not happen so far
310 logger.Warnw("MibDownload-bridge-init response error", log.Fields{"for device-id": onuDeviceEntry.deviceID})
311 return errors.New("MibDownloadBridgeInit responseError")
312 }
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000313}