blob: 97dfc47fd2db94b0a985526e0c0d81be4547f1cb [file] [log] [blame]
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +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 Hildebrandt2fb70892020-10-28 11:53:18 +000021 "bytes"
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000022 "context"
Holger Hildebrandtc54939a2020-06-17 08:14:27 +000023 "encoding/hex"
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000024 "encoding/json"
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000025 "errors"
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000026 "fmt"
27 "strconv"
mpagenko3af1f032020-06-10 08:53:41 +000028 "strings"
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000029
30 "github.com/looplab/fsm"
31
32 //"sync"
divyadesaibbed37c2020-08-28 13:35:20 +053033 "time"
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000034
35 //"github.com/opencord/voltha-lib-go/v3/pkg/kafka"
36 "github.com/opencord/omci-lib-go"
37 me "github.com/opencord/omci-lib-go/generated"
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000038 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000039 "github.com/opencord/voltha-lib-go/v3/pkg/log"
40 //ic "github.com/opencord/voltha-protos/v3/go/inter_container"
41 //"github.com/opencord/voltha-protos/v3/go/openflow_13"
42 //"github.com/opencord/voltha-protos/v3/go/voltha"
43)
44
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000045var supportedClassIds = []me.ClassID{
46 me.CardholderClassID, // 5
47 me.CircuitPackClassID, // 6
48 me.SoftwareImageClassID, // 7
49 me.PhysicalPathTerminationPointEthernetUniClassID, // 11
50 me.OltGClassID, // 131
51 me.OnuPowerSheddingClassID, // 133
52 me.IpHostConfigDataClassID, // 134
53 me.OnuGClassID, // 256
54 me.Onu2GClassID, // 257
55 me.TContClassID, // 262
56 me.AniGClassID, // 263
57 me.UniGClassID, // 264
58 me.PriorityQueueClassID, // 277
59 me.TrafficSchedulerClassID, // 278
60 me.VirtualEthernetInterfacePointClassID, // 329
61 me.EnhancedSecurityControlClassID, // 332
62 me.OnuDynamicPowerManagementControlClassID, // 336
63 // 347 // definitions for ME "IPv6 host config data" are currently missing in omci-lib-go!
64}
65
66var fsmMsg TestMessageType
67
Holger Hildebrandt61b24d02020-11-16 13:36:40 +000068func (oo *OnuDeviceEntry) enterStartingState(e *fsm.Event) {
69 logger.Debugw("MibSync FSM", log.Fields{"Start processing MibSync-msgs in State": e.FSM.Current(), "device-id": oo.deviceID})
70 oo.pOnuDB = newOnuDeviceDB(context.TODO(), oo)
71 go oo.processMibSyncMessages()
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000072}
73
Holger Hildebrandt61b24d02020-11-16 13:36:40 +000074func (oo *OnuDeviceEntry) enterResettingMibState(e *fsm.Event) {
75 logger.Debugw("MibSync FSM", log.Fields{"Start MibTemplate processing in State": e.FSM.Current(), "device-id": oo.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000076
Holger Hildebrandt61b24d02020-11-16 13:36:40 +000077 logger.Debugw("MibSync FSM", log.Fields{"send mibReset in State": e.FSM.Current(), "device-id": oo.deviceID})
78 _ = oo.PDevOmciCC.sendMibReset(context.TODO(), ConstDefaultOmciTimeout, true)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000079
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000080 //TODO: needs to handle timeouts
Holger Hildebrandtc54939a2020-06-17 08:14:27 +000081}
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000082
Holger Hildebrandt61b24d02020-11-16 13:36:40 +000083func (oo *OnuDeviceEntry) enterGettingVendorAndSerialState(e *fsm.Event) {
84 logger.Debugw("MibSync FSM", log.Fields{"Start getting VendorId and SerialNumber in State": e.FSM.Current(), "device-id": oo.deviceID})
Holger Hildebrandtc54939a2020-06-17 08:14:27 +000085 requestedAttributes := me.AttributeValueMap{"VendorId": "", "SerialNumber": 0}
Holger Hildebrandt61b24d02020-11-16 13:36:40 +000086 meInstance := oo.PDevOmciCC.sendGetMe(context.TODO(), me.OnuGClassID, onugMeID, requestedAttributes, ConstDefaultOmciTimeout, true)
Holger Hildebrandtc54939a2020-06-17 08:14:27 +000087 //accept also nil as (error) return value for writing to LastTx
88 // - this avoids misinterpretation of new received OMCI messages
Holger Hildebrandt61b24d02020-11-16 13:36:40 +000089 oo.PDevOmciCC.pLastTxMeInstance = meInstance
Holger Hildebrandtc54939a2020-06-17 08:14:27 +000090}
Holger Hildebrandtccd390c2020-05-29 13:49:04 +000091
Holger Hildebrandt61b24d02020-11-16 13:36:40 +000092func (oo *OnuDeviceEntry) enterGettingEquipmentIDState(e *fsm.Event) {
93 logger.Debugw("MibSync FSM", log.Fields{"Start getting EquipmentId in State": e.FSM.Current(), "device-id": oo.deviceID})
Holger Hildebrandtc54939a2020-06-17 08:14:27 +000094 requestedAttributes := me.AttributeValueMap{"EquipmentId": ""}
Holger Hildebrandt61b24d02020-11-16 13:36:40 +000095 meInstance := oo.PDevOmciCC.sendGetMe(context.TODO(), me.Onu2GClassID, onu2gMeID, requestedAttributes, ConstDefaultOmciTimeout, true)
Holger Hildebrandtc54939a2020-06-17 08:14:27 +000096 //accept also nil as (error) return value for writing to LastTx
97 // - this avoids misinterpretation of new received OMCI messages
Holger Hildebrandt61b24d02020-11-16 13:36:40 +000098 oo.PDevOmciCC.pLastTxMeInstance = meInstance
Holger Hildebrandtc54939a2020-06-17 08:14:27 +000099}
100
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000101func (oo *OnuDeviceEntry) enterGettingFirstSwVersionState(e *fsm.Event) {
102 logger.Debugw("MibSync FSM", log.Fields{"Start getting IsActive and Version of first SW-image in State": e.FSM.Current(), "device-id": oo.deviceID})
Holger Hildebrandtc54939a2020-06-17 08:14:27 +0000103 requestedAttributes := me.AttributeValueMap{"IsActive": 0, "Version": ""}
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000104 meInstance := oo.PDevOmciCC.sendGetMe(context.TODO(), me.SoftwareImageClassID, firstSwImageMeID, requestedAttributes, ConstDefaultOmciTimeout, true)
Holger Hildebrandtc54939a2020-06-17 08:14:27 +0000105 //accept also nil as (error) return value for writing to LastTx
106 // - this avoids misinterpretation of new received OMCI messages
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000107 oo.PDevOmciCC.pLastTxMeInstance = meInstance
Holger Hildebrandtc54939a2020-06-17 08:14:27 +0000108}
109
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000110func (oo *OnuDeviceEntry) enterGettingSecondSwVersionState(e *fsm.Event) {
111 logger.Debugw("MibSync FSM", log.Fields{"Start getting IsActive and Version of second SW-image in State": e.FSM.Current(), "device-id": oo.deviceID})
Holger Hildebrandtc54939a2020-06-17 08:14:27 +0000112 requestedAttributes := me.AttributeValueMap{"IsActive": 0, "Version": ""}
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000113 meInstance := oo.PDevOmciCC.sendGetMe(context.TODO(), me.SoftwareImageClassID, secondSwImageMeID, requestedAttributes, ConstDefaultOmciTimeout, true)
Holger Hildebrandtc54939a2020-06-17 08:14:27 +0000114 //accept also nil as (error) return value for writing to LastTx
115 // - this avoids misinterpretation of new received OMCI messages
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000116 oo.PDevOmciCC.pLastTxMeInstance = meInstance
Holger Hildebrandtc54939a2020-06-17 08:14:27 +0000117}
118
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000119func (oo *OnuDeviceEntry) enterGettingMacAddressState(e *fsm.Event) {
120 logger.Debugw("MibSync FSM", log.Fields{"Start getting MacAddress in State": e.FSM.Current(), "device-id": oo.deviceID})
Holger Hildebrandtc54939a2020-06-17 08:14:27 +0000121 requestedAttributes := me.AttributeValueMap{"MacAddress": ""}
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000122 meInstance := oo.PDevOmciCC.sendGetMe(context.TODO(), me.IpHostConfigDataClassID, ipHostConfigDataMeID, requestedAttributes, ConstDefaultOmciTimeout, true)
Holger Hildebrandtc54939a2020-06-17 08:14:27 +0000123 //accept also nil as (error) return value for writing to LastTx
124 // - this avoids misinterpretation of new received OMCI messages
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000125 oo.PDevOmciCC.pLastTxMeInstance = meInstance
Holger Hildebrandtc54939a2020-06-17 08:14:27 +0000126}
127
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000128func (oo *OnuDeviceEntry) enterGettingMibTemplate(e *fsm.Event) {
Holger Hildebrandtc54939a2020-06-17 08:14:27 +0000129
Himani Chawla6d2ae152020-09-02 13:11:20 +0530130 for i := firstSwImageMeID; i <= secondSwImageMeID; i++ {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000131 if oo.swImages[i].isActive > 0 {
132 oo.activeSwVersion = oo.swImages[i].version
Holger Hildebrandtc54939a2020-06-17 08:14:27 +0000133 }
134 }
135
136 meStoredFromTemplate := false
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000137 oo.mibTemplatePath = fmt.Sprintf(cSuffixMibTemplateKvStore, oo.vendorID, oo.equipmentID, oo.activeSwVersion)
Matteo Scandolof1f39a72020-11-24 12:08:11 -0800138 logger.Debugw("MibSync FSM - MibTemplate - etcd search string", log.Fields{"path": fmt.Sprintf("%s/%s", cBasePathMibTemplateKvStore, oo.mibTemplatePath)})
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000139 Value, err := oo.mibTemplateKVStore.Get(context.TODO(), oo.mibTemplatePath)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000140 if err == nil {
141 if Value != nil {
142 logger.Debugf("MibSync FSM - MibTemplate read: Key: %s, Value: %s %s", Value.Key, Value.Value)
mpagenko3af1f032020-06-10 08:53:41 +0000143
144 // swap out tokens with specific data
145 mibTmpString, _ := kvstore.ToString(Value.Value)
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000146 mibTmpString2 := strings.Replace(mibTmpString, "%SERIAL_NUMBER%", oo.serialNumber, -1)
147 mibTmpString = strings.Replace(mibTmpString2, "%MAC_ADDRESS%", oo.macAddress, -1)
mpagenko3af1f032020-06-10 08:53:41 +0000148 mibTmpBytes := []byte(mibTmpString)
149 logger.Debugf("MibSync FSM - MibTemplate tokens swapped out: %s", mibTmpBytes)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000150
151 var fistLevelMap map[string]interface{}
152 if err = json.Unmarshal(mibTmpBytes, &fistLevelMap); err != nil {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000153 logger.Errorw("MibSync FSM - Failed to unmarshal template", log.Fields{"error": err, "device-id": oo.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000154 } else {
155 for fistLevelKey, firstLevelValue := range fistLevelMap {
156 logger.Debugw("MibSync FSM - fistLevelKey", log.Fields{"fistLevelKey": fistLevelKey})
157 if uint16ValidNumber, err := strconv.ParseUint(fistLevelKey, 10, 16); err == nil {
Himani Chawla4d908332020-08-31 12:30:20 +0530158 meClassID := me.ClassID(uint16ValidNumber)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000159 logger.Debugw("MibSync FSM - fistLevelKey is a number in uint16-range", log.Fields{"uint16ValidNumber": uint16ValidNumber})
Himani Chawla6d2ae152020-09-02 13:11:20 +0530160 if isSupportedClassID(meClassID) {
Himani Chawla26e555c2020-08-31 12:30:20 +0530161 logger.Debugw("MibSync FSM - fistLevelKey is a supported classID", log.Fields{"meClassID": meClassID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000162 secondLevelMap := firstLevelValue.(map[string]interface{})
163 for secondLevelKey, secondLevelValue := range secondLevelMap {
164 logger.Debugw("MibSync FSM - secondLevelKey", log.Fields{"secondLevelKey": secondLevelKey})
165 if uint16ValidNumber, err := strconv.ParseUint(secondLevelKey, 10, 16); err == nil {
Himani Chawla4d908332020-08-31 12:30:20 +0530166 meEntityID := uint16(uint16ValidNumber)
167 logger.Debugw("MibSync FSM - secondLevelKey is a number and a valid EntityId", log.Fields{"meEntityID": meEntityID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000168 thirdLevelMap := secondLevelValue.(map[string]interface{})
169 for thirdLevelKey, thirdLevelValue := range thirdLevelMap {
Andrea Campanellad10d1db2020-09-08 17:00:29 +0200170 if thirdLevelKey == "Attributes" {
Himani Chawla4d908332020-08-31 12:30:20 +0530171 logger.Debugw("MibSync FSM - thirdLevelKey refers to attributes", log.Fields{"thirdLevelKey": thirdLevelKey})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000172 attributesMap := thirdLevelValue.(map[string]interface{})
173 logger.Debugw("MibSync FSM - attributesMap", log.Fields{"attributesMap": attributesMap})
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000174 oo.pOnuDB.PutMe(meClassID, meEntityID, attributesMap)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000175 meStoredFromTemplate = true
176 }
177 }
178 }
179 }
180 }
181 }
182 }
183 }
184 } else {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000185 logger.Debugw("No MIB template found", log.Fields{"path": oo.mibTemplatePath, "device-id": oo.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000186 }
187 } else {
Andrea Campanella6515c582020-10-05 11:25:00 +0200188 logger.Errorf("Get from kvstore operation failed for path",
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000189 log.Fields{"path": oo.mibTemplatePath, "device-id": oo.deviceID})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000190 }
191 if meStoredFromTemplate {
192 logger.Debug("MibSync FSM - valid MEs stored from template")
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000193 oo.pOnuDB.logMeDb()
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000194 fsmMsg = LoadMibTemplateOk
195 } else {
196 logger.Debug("MibSync FSM - no valid MEs stored from template - perform MIB-upload!")
197 fsmMsg = LoadMibTemplateFailed
198 }
199
200 mibSyncMsg := Message{
201 Type: TestMsg,
202 Data: TestMessage{
203 TestMessageVal: fsmMsg,
204 },
205 }
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000206 oo.pMibUploadFsm.commChan <- mibSyncMsg
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000207}
208
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000209func (oo *OnuDeviceEntry) enterUploadingState(e *fsm.Event) {
210 logger.Debugw("MibSync FSM", log.Fields{"send MibUpload in State": e.FSM.Current(), "device-id": oo.deviceID})
211 _ = oo.PDevOmciCC.sendMibUpload(context.TODO(), ConstDefaultOmciTimeout, true)
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000212}
213
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000214func (oo *OnuDeviceEntry) enterInSyncState(e *fsm.Event) {
215 logger.Debugw("MibSync FSM", log.Fields{"send notification to core in State": e.FSM.Current(), "device-id": oo.deviceID})
216 oo.transferSystemEvent(MibDatabaseSync)
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000217}
218
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000219func (oo *OnuDeviceEntry) enterExaminingMdsState(e *fsm.Event) {
220 logger.Debugw("MibSync FSM", log.Fields{"Start GetMds processing in State": e.FSM.Current(), "device-id": oo.deviceID})
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000221 logger.Debug("function not implemented yet")
222}
223
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000224func (oo *OnuDeviceEntry) enterResynchronizingState(e *fsm.Event) {
225 logger.Debugw("MibSync FSM", log.Fields{"Start MibResync processing in State": e.FSM.Current(), "device-id": oo.deviceID})
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000226 logger.Debug("function not implemented yet")
227}
228
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000229func (oo *OnuDeviceEntry) enterAuditingState(e *fsm.Event) {
230 logger.Debugw("MibSync FSM", log.Fields{"Start MibResync processing in State": e.FSM.Current(), "device-id": oo.deviceID})
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000231 logger.Debug("function not implemented yet")
232}
233
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000234func (oo *OnuDeviceEntry) enterOutOfSyncState(e *fsm.Event) {
235 logger.Debugw("MibSync FSM", log.Fields{"Start MibReconcile processing in State": e.FSM.Current(), "device-id": oo.deviceID})
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000236 logger.Debug("function not implemented yet")
237}
238
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000239func (oo *OnuDeviceEntry) processMibSyncMessages( /*ctx context.Context*/ ) {
240 logger.Debugw("MibSync Msg", log.Fields{"Start routine to process OMCI-messages for device-id": oo.deviceID})
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000241loop:
242 for {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000243 // case <-ctx.Done():
244 // logger.Info("MibSync Msg", log.Fields{"Message handling canceled via context for device-id": onuDeviceEntry.deviceID})
245 // break loop
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000246 message, ok := <-oo.pMibUploadFsm.commChan
Himani Chawla4d908332020-08-31 12:30:20 +0530247 if !ok {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000248 logger.Info("MibSync Msg", log.Fields{"Message couldn't be read from channel for device-id": oo.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530249 break loop
250 }
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000251 logger.Debugw("MibSync Msg", log.Fields{"Received message on ONU MibSyncChan for device-id": oo.deviceID})
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000252
Himani Chawla4d908332020-08-31 12:30:20 +0530253 switch message.Type {
254 case TestMsg:
255 msg, _ := message.Data.(TestMessage)
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000256 oo.handleTestMsg(msg)
Himani Chawla4d908332020-08-31 12:30:20 +0530257 case OMCI:
258 msg, _ := message.Data.(OmciMessage)
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000259 oo.handleOmciMessage(msg)
Himani Chawla4d908332020-08-31 12:30:20 +0530260 default:
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000261 logger.Warn("MibSync Msg", log.Fields{"Unknown message type received for device-id": oo.deviceID, "message.Type": message.Type})
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000262 }
263 }
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000264 logger.Info("MibSync Msg", log.Fields{"Stopped handling of MibSyncChan for device-id": oo.deviceID})
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000265 // TODO: only this action?
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000266 _ = oo.pMibUploadFsm.pFsm.Event(ulEvStop)
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000267}
268
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000269func (oo *OnuDeviceEntry) handleTestMsg(msg TestMessage) {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000270
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000271 logger.Debugw("MibSync Msg", log.Fields{"TestMessage received for device-id": oo.deviceID, "msg.TestMessageVal": msg.TestMessageVal})
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000272
273 switch msg.TestMessageVal {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000274 case LoadMibTemplateFailed:
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000275 _ = oo.pMibUploadFsm.pFsm.Event(ulEvUploadMib)
276 logger.Debugw("MibSync Msg", log.Fields{"state": string(oo.pMibUploadFsm.pFsm.Current())})
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000277 case LoadMibTemplateOk:
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000278 _ = oo.pMibUploadFsm.pFsm.Event(ulEvSuccess)
279 logger.Debugw("MibSync Msg", log.Fields{"state": string(oo.pMibUploadFsm.pFsm.Current())})
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000280 default:
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000281 logger.Warn("MibSync Msg", log.Fields{"Unknown message type received for device-id": oo.deviceID, "msg.TestMessageVal": msg.TestMessageVal})
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000282 }
283}
284
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000285func (oo *OnuDeviceEntry) handleOmciMibResetResponseMessage(msg OmciMessage) {
286 if oo.pMibUploadFsm.pFsm.Is(ulStResettingMib) {
Himani Chawla4d908332020-08-31 12:30:20 +0530287 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeMibResetResponse)
288 if msgLayer != nil {
289 msgObj, msgOk := msgLayer.(*omci.MibResetResponse)
290 if msgOk {
291 logger.Debugw("MibResetResponse Data", log.Fields{"data-fields": msgObj})
292 if msgObj.Result == me.Success {
293 // trigger retrieval of VendorId and SerialNumber
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000294 _ = oo.pMibUploadFsm.pFsm.Event(ulEvGetVendorAndSerial)
Himani Chawla4d908332020-08-31 12:30:20 +0530295 return
296 }
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000297 logger.Errorw("Omci MibResetResponse Error", log.Fields{"device-id": oo.deviceID, "Error": msgObj.Result})
Himani Chawla4d908332020-08-31 12:30:20 +0530298 } else {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000299 logger.Errorw("Omci Msg layer could not be assigned", log.Fields{"device-id": oo.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530300 }
301 } else {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000302 logger.Errorw("Omci Msg layer could not be detected", log.Fields{"device-id": oo.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530303 }
304 } else {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000305 logger.Errorw("Wrong Omci MibResetResponse received", log.Fields{"in state ": oo.pMibUploadFsm.pFsm.Current,
306 "device-id": oo.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530307 }
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000308 logger.Info("MibSync Msg", log.Fields{"Stopped handling of MibSyncChan for device-id": oo.deviceID})
309 _ = oo.pMibUploadFsm.pFsm.Event(ulEvStop)
Himani Chawla4d908332020-08-31 12:30:20 +0530310
311}
312
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000313func (oo *OnuDeviceEntry) handleOmciMibUploadResponseMessage(msg OmciMessage) {
Himani Chawla4d908332020-08-31 12:30:20 +0530314 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeMibUploadResponse)
315 if msgLayer == nil {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000316 logger.Errorw("Omci Msg layer could not be detected", log.Fields{"device-id": oo.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530317 return
318 }
319 msgObj, msgOk := msgLayer.(*omci.MibUploadResponse)
320 if !msgOk {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000321 logger.Errorw("Omci Msg layer could not be assigned", log.Fields{"device-id": oo.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530322 return
323 }
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000324 logger.Debugw("MibUploadResponse Data for:", log.Fields{"device-id": oo.deviceID, "data-fields": msgObj})
Himani Chawla4d908332020-08-31 12:30:20 +0530325 /* to be verified / reworked !!! */
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000326 oo.PDevOmciCC.uploadNoOfCmds = msgObj.NumberOfCommands
327 if oo.PDevOmciCC.uploadSequNo < oo.PDevOmciCC.uploadNoOfCmds {
328 _ = oo.PDevOmciCC.sendMibUploadNext(context.TODO(), ConstDefaultOmciTimeout, true)
Himani Chawla4d908332020-08-31 12:30:20 +0530329 } else {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000330 logger.Errorw("Invalid number of commands received for:", log.Fields{"device-id": oo.deviceID, "uploadNoOfCmds": oo.PDevOmciCC.uploadNoOfCmds})
Himani Chawla4d908332020-08-31 12:30:20 +0530331 //TODO right action?
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000332 _ = oo.pMibUploadFsm.pFsm.Event(ulEvTimeout)
Himani Chawla4d908332020-08-31 12:30:20 +0530333 }
334}
335
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000336func (oo *OnuDeviceEntry) handleOmciMibUploadNextResponseMessage(msg OmciMessage) {
Himani Chawla4d908332020-08-31 12:30:20 +0530337 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeMibUploadNextResponse)
Andrea Campanella6515c582020-10-05 11:25:00 +0200338
Holger Hildebrandte2439342020-12-03 16:06:54 +0000339 if msgLayer == nil {
340 logger.Errorw("Omci Msg layer could not be detected", log.Fields{"device-id": oo.deviceID})
341 return
342 }
343 msgObj, msgOk := msgLayer.(*omci.MibUploadNextResponse)
344 if !msgOk {
345 logger.Errorw("Omci Msg layer could not be assigned", log.Fields{"device-id": oo.deviceID})
346 return
347 }
348 meName := msgObj.ReportedME.GetName()
349 if meName == "UnknownItuG988ManagedEntity" || meName == "UnknownVendorSpecificManagedEntity" {
350 logger.Debugw("MibUploadNextResponse Data for unknown ME received - temporary workaround is to ignore it!",
351 log.Fields{"device-id": oo.deviceID, "data-fields": msgObj, "meName": meName})
352 } else {
353 logger.Debugw("MibUploadNextResponse Data for:",
354 log.Fields{"device-id": oo.deviceID, "meName": meName, "data-fields": msgObj})
Holger Hildebrandt8998b872020-10-05 13:48:39 +0000355 meClassID := msgObj.ReportedME.GetClassID()
356 meEntityID := msgObj.ReportedME.GetEntityID()
357 meAttributes := msgObj.ReportedME.GetAttributeValueMap()
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000358 oo.pOnuDB.PutMe(meClassID, meEntityID, meAttributes)
Himani Chawla4d908332020-08-31 12:30:20 +0530359 }
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000360 if oo.PDevOmciCC.uploadSequNo < oo.PDevOmciCC.uploadNoOfCmds {
361 _ = oo.PDevOmciCC.sendMibUploadNext(context.TODO(), ConstDefaultOmciTimeout, true)
Himani Chawla4d908332020-08-31 12:30:20 +0530362 } else {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000363 oo.pOnuDB.logMeDb()
364 err := oo.createAndPersistMibTemplate()
Himani Chawla4d908332020-08-31 12:30:20 +0530365 if err != nil {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000366 logger.Errorw("MibSync - MibTemplate - Failed to create and persist the mib template", log.Fields{"error": err, "device-id": oo.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530367 }
368
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000369 _ = oo.pMibUploadFsm.pFsm.Event(ulEvSuccess)
Himani Chawla4d908332020-08-31 12:30:20 +0530370 }
371}
372
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000373func (oo *OnuDeviceEntry) handleOmciGetResponseMessage(msg OmciMessage) error {
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000374 var err error = nil
Himani Chawla4d908332020-08-31 12:30:20 +0530375 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeGetResponse)
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000376 if msgLayer == nil {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000377 logger.Errorw("omci Msg layer could not be detected for GetResponse - handling of MibSyncChan stopped", log.Fields{"device-id": oo.deviceID})
378 _ = oo.pMibUploadFsm.pFsm.Event(ulEvStop)
379 return fmt.Errorf("omci Msg layer could not be detected for GetResponse - handling of MibSyncChan stopped: %s", oo.deviceID)
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000380 }
381 msgObj, msgOk := msgLayer.(*omci.GetResponse)
382 if !msgOk {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000383 logger.Errorw("omci Msg layer could not be assigned for GetResponse - handling of MibSyncChan stopped", log.Fields{"device-id": oo.deviceID})
384 _ = oo.pMibUploadFsm.pFsm.Event(ulEvStop)
385 return fmt.Errorf("omci Msg layer could not be assigned for GetResponse - handling of MibSyncChan stopped: %s", oo.deviceID)
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000386 }
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000387 logger.Debugw("MibSync FSM - GetResponse Data", log.Fields{"device-id": oo.deviceID, "data-fields": msgObj})
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000388 if msgObj.Result == me.Success {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000389 entityID := oo.PDevOmciCC.pLastTxMeInstance.GetEntityID()
390 if msgObj.EntityClass == oo.PDevOmciCC.pLastTxMeInstance.GetClassID() && msgObj.EntityInstance == entityID {
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000391 meAttributes := msgObj.Attributes
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000392 meInstance := oo.PDevOmciCC.pLastTxMeInstance.GetName()
393 logger.Debugf("MibSync FSM - GetResponse Data for %s", log.Fields{"device-id": oo.deviceID, "data-fields": msgObj}, meInstance)
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000394 switch meInstance {
395 case "OnuG":
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000396 oo.vendorID = trimStringFromInterface(meAttributes["VendorId"])
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000397 snBytes, _ := me.InterfaceToOctets(meAttributes["SerialNumber"])
398 if onugSerialNumberLen == len(snBytes) {
399 snVendorPart := fmt.Sprintf("%s", snBytes[:4])
400 snNumberPart := hex.EncodeToString(snBytes[4:])
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000401 oo.serialNumber = snVendorPart + snNumberPart
402 logger.Debugw("MibSync FSM - GetResponse Data for Onu-G - VendorId/SerialNumber", log.Fields{"device-id": oo.deviceID,
403 "onuDeviceEntry.vendorID": oo.vendorID, "onuDeviceEntry.serialNumber": oo.serialNumber})
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000404 } else {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000405 logger.Infow("MibSync FSM - SerialNumber has wrong length - fill serialNumber with zeros", log.Fields{"device-id": oo.deviceID, "length": len(snBytes)})
406 oo.serialNumber = cEmptySerialNumberString
Himani Chawla4d908332020-08-31 12:30:20 +0530407 }
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000408 // trigger retrieval of EquipmentId
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000409 _ = oo.pMibUploadFsm.pFsm.Event(ulEvGetEquipmentID)
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000410 return nil
411 case "Onu2G":
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000412 oo.equipmentID = trimStringFromInterface(meAttributes["EquipmentId"])
413 logger.Debugw("MibSync FSM - GetResponse Data for Onu2-G - EquipmentId", log.Fields{"device-id": oo.deviceID,
414 "onuDeviceEntry.equipmentID": oo.equipmentID})
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000415 // trigger retrieval of 1st SW-image info
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000416 _ = oo.pMibUploadFsm.pFsm.Event(ulEvGetFirstSwVersion)
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000417 return nil
418 case "SoftwareImage":
419 if entityID <= secondSwImageMeID {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000420 oo.swImages[entityID].version = trimStringFromInterface(meAttributes["Version"])
421 oo.swImages[entityID].isActive = meAttributes["IsActive"].(uint8)
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000422 logger.Debugw("MibSync FSM - GetResponse Data for SoftwareImage - Version/IsActive",
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000423 log.Fields{"device-id": oo.deviceID, "entityID": entityID,
424 "version": oo.swImages[entityID].version, "isActive": oo.swImages[entityID].isActive})
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000425 } else {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000426 err = fmt.Errorf("mibSync FSM - Failed to GetResponse Data for SoftwareImage: %s", oo.deviceID)
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000427 }
428 if firstSwImageMeID == entityID {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000429 _ = oo.pMibUploadFsm.pFsm.Event(ulEvGetSecondSwVersion)
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000430 return nil
431 } else if secondSwImageMeID == entityID {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000432 _ = oo.pMibUploadFsm.pFsm.Event(ulEvGetMacAddress)
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000433 return nil
434 }
435 case "IpHostConfigData":
436 macBytes, _ := me.InterfaceToOctets(meAttributes["MacAddress"])
437 if omciMacAddressLen == len(macBytes) {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000438 oo.macAddress = hex.EncodeToString(macBytes[:])
439 logger.Debugw("MibSync FSM - GetResponse Data for IpHostConfigData - MacAddress", log.Fields{"device-id": oo.deviceID,
440 "onuDeviceEntry.macAddress": oo.macAddress})
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000441 } else {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000442 logger.Infow("MibSync FSM - MacAddress wrong length - fill macAddress with zeros", log.Fields{"device-id": oo.deviceID, "length": len(macBytes)})
443 oo.macAddress = cEmptyMacAddrString
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000444 }
445 // trigger retrieval of mib template
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000446 _ = oo.pMibUploadFsm.pFsm.Event(ulEvGetMibTemplate)
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000447 return nil
Himani Chawla4d908332020-08-31 12:30:20 +0530448 }
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000449 }
Himani Chawla4d908332020-08-31 12:30:20 +0530450 } else {
Holger Hildebrandt80129db2020-11-23 10:49:32 +0000451 if err = oo.handleOmciGetResponseErrors(msgObj); err == nil {
452 return nil
453 }
Himani Chawla4d908332020-08-31 12:30:20 +0530454 }
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000455 logger.Info("MibSync Msg", log.Fields{"Stopped handling of MibSyncChan for device-id": oo.deviceID})
456 _ = oo.pMibUploadFsm.pFsm.Event(ulEvStop)
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000457 return err
Himani Chawla4d908332020-08-31 12:30:20 +0530458}
459
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000460func (oo *OnuDeviceEntry) handleOmciMessage(msg OmciMessage) {
461 logger.Debugw("MibSync Msg", log.Fields{"OmciMessage received for device-id": oo.deviceID,
Andrea Campanella6515c582020-10-05 11:25:00 +0200462 "msgType": msg.OmciMsg.MessageType, "msg": msg})
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000463 //further analysis could be done here based on msg.OmciMsg.Payload, e.g. verification of error code ...
464 switch msg.OmciMsg.MessageType {
465 case omci.MibResetResponseType:
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000466 oo.handleOmciMibResetResponseMessage(msg)
Holger Hildebrandtc54939a2020-06-17 08:14:27 +0000467
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000468 case omci.MibUploadResponseType:
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000469 oo.handleOmciMibUploadResponseMessage(msg)
Himani Chawla4d908332020-08-31 12:30:20 +0530470
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000471 case omci.MibUploadNextResponseType:
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000472 oo.handleOmciMibUploadNextResponseMessage(msg)
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000473
Holger Hildebrandtc54939a2020-06-17 08:14:27 +0000474 case omci.GetResponseType:
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000475 //TODO: error handling
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000476 _ = oo.handleOmciGetResponseMessage(msg)
Himani Chawla4d908332020-08-31 12:30:20 +0530477
Andrea Campanella6515c582020-10-05 11:25:00 +0200478 default:
479 log.Warnw("Unknown Message Type", log.Fields{"msgType": msg.OmciMsg.MessageType})
480
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000481 }
482}
483
Holger Hildebrandt80129db2020-11-23 10:49:32 +0000484func (oo *OnuDeviceEntry) handleOmciGetResponseErrors(msgObj *omci.GetResponse) error {
485 var err error = nil
486 logger.Debugf("MibSync FSM - erroneous result in GetResponse Data: %s", log.Fields{"device-id": oo.deviceID, "data-fields": msgObj}, msgObj.Result)
487 // Up to now the following erroneous results have been seen for different ONU-types to indicate an unsupported ME
488 if msgObj.Result == me.UnknownInstance || msgObj.Result == me.UnknownEntity || msgObj.Result == me.ProcessingError || msgObj.Result == me.NotSupported {
489 entityID := oo.PDevOmciCC.pLastTxMeInstance.GetEntityID()
490 if msgObj.EntityClass == oo.PDevOmciCC.pLastTxMeInstance.GetClassID() && msgObj.EntityInstance == entityID {
491 meInstance := oo.PDevOmciCC.pLastTxMeInstance.GetName()
492 switch meInstance {
493 case "IpHostConfigData":
494 logger.Debugw("MibSync FSM - erroneous result for IpHostConfigData received - ONU doesn't support ME - fill macAddress with zeros",
495 log.Fields{"device-id": oo.deviceID, "data-fields": msgObj})
496 oo.macAddress = cEmptyMacAddrString
497 // trigger retrieval of mib template
498 _ = oo.pMibUploadFsm.pFsm.Event(ulEvGetMibTemplate)
499 return nil
500 default:
501 logger.Warnf("MibSync FSM - erroneous result for %s received - no exceptional treatment defined", log.Fields{"device-id": oo.deviceID, "data-fields": msgObj}, meInstance)
502 err = fmt.Errorf("erroneous result for %s received - no exceptional treatment defined: %s", meInstance, oo.deviceID)
503 }
504 }
505 } else {
506 logger.Errorf("MibSync FSM - erroneous result in GetResponse Data: %s", log.Fields{"device-id": oo.deviceID, "data-fields": msgObj}, msgObj.Result)
507 err = fmt.Errorf("erroneous result in GetResponse Data: %s - %s", msgObj.Result, oo.deviceID)
508 }
509 return err
510}
511
Himani Chawla6d2ae152020-09-02 13:11:20 +0530512func isSupportedClassID(meClassID me.ClassID) bool {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000513 for _, v := range supportedClassIds {
Himani Chawla4d908332020-08-31 12:30:20 +0530514 if v == meClassID {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000515 return true
516 }
517 }
518 return false
519}
520
Holger Hildebrandt2fb70892020-10-28 11:53:18 +0000521func trimStringFromInterface(input interface{}) string {
522 ifBytes, _ := me.InterfaceToOctets(input)
523 return fmt.Sprintf("%s", bytes.Trim(ifBytes, "\x00"))
524}
525
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000526func (oo *OnuDeviceEntry) mibDbVolatileDict() error {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000527 logger.Debug("MibVolatileDict- running from default Entry code")
528 return errors.New("not_implemented")
529}
530
Himani Chawla6d2ae152020-09-02 13:11:20 +0530531// createAndPersistMibTemplate method creates a mib template for the device id when operator enables the ONU device for the first time.
divyadesaibbed37c2020-08-28 13:35:20 +0530532// We are creating a placeholder for "SerialNumber" for ME Class ID 6 and 256 and "MacAddress" for ME Class ID 134 in the template
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000533// and then storing the template into etcd "service/voltha/omci_mibs/go_templates/verdor_id/equipment_id/software_version" path.
534func (oo *OnuDeviceEntry) createAndPersistMibTemplate() error {
535 logger.Debugw("MibSync - MibTemplate - path name", log.Fields{"path": oo.mibTemplatePath,
536 "device-id": oo.deviceID})
divyadesaibbed37c2020-08-28 13:35:20 +0530537
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000538 oo.pOpenOnuAc.lockMibTemplateGenerated.Lock()
539 if mibTemplateIsGenerated, exist := oo.pOpenOnuAc.mibTemplatesGenerated[oo.mibTemplatePath]; exist {
540 if mibTemplateIsGenerated {
541 logger.Debugw("MibSync - MibTemplate - another thread has already started to generate it - skip",
542 log.Fields{"path": oo.mibTemplatePath, "device-id": oo.deviceID})
543 oo.pOpenOnuAc.lockMibTemplateGenerated.Unlock()
544 return nil
545 }
546 logger.Debugw("MibSync - MibTemplate - previous generation attempt seems to be failed - try again",
547 log.Fields{"path": oo.mibTemplatePath, "device-id": oo.deviceID})
548 } else {
549 logger.Debugw("MibSync - MibTemplate - first ONU-instance of this kind - start generation",
550 log.Fields{"path": oo.mibTemplatePath, "device-id": oo.deviceID})
551 }
552 oo.pOpenOnuAc.mibTemplatesGenerated[oo.mibTemplatePath] = true
553 oo.pOpenOnuAc.lockMibTemplateGenerated.Unlock()
554
555 currentTime := time.Now()
divyadesaibbed37c2020-08-28 13:35:20 +0530556 templateMap := make(map[string]interface{})
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000557 templateMap["TemplateName"] = oo.mibTemplatePath
divyadesaibbed37c2020-08-28 13:35:20 +0530558 templateMap["TemplateCreated"] = currentTime.Format("2006-01-02 15:04:05.000000")
559
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000560 firstLevelMap := oo.pOnuDB.meDb
divyadesaibbed37c2020-08-28 13:35:20 +0530561 for firstLevelKey, firstLevelValue := range firstLevelMap {
562 logger.Debugw("MibSync - MibTemplate - firstLevelKey", log.Fields{"firstLevelKey": firstLevelKey})
Himani Chawla26e555c2020-08-31 12:30:20 +0530563 classID := strconv.Itoa(int(firstLevelKey))
divyadesaibbed37c2020-08-28 13:35:20 +0530564
565 secondLevelMap := make(map[string]interface{})
566 for secondLevelKey, secondLevelValue := range firstLevelValue {
567 thirdLevelMap := make(map[string]interface{})
Himani Chawla26e555c2020-08-31 12:30:20 +0530568 entityID := strconv.Itoa(int(secondLevelKey))
divyadesaibbed37c2020-08-28 13:35:20 +0530569 thirdLevelMap["Attributes"] = secondLevelValue
Himani Chawla26e555c2020-08-31 12:30:20 +0530570 thirdLevelMap["InstanceId"] = entityID
571 secondLevelMap[entityID] = thirdLevelMap
572 if classID == "6" || classID == "256" {
divyadesaibbed37c2020-08-28 13:35:20 +0530573 forthLevelMap := map[string]interface{}(thirdLevelMap["Attributes"].(me.AttributeValueMap))
574 delete(forthLevelMap, "SerialNumber")
575 forthLevelMap["SerialNumber"] = "%SERIAL_NUMBER%"
576
577 }
Himani Chawla26e555c2020-08-31 12:30:20 +0530578 if classID == "134" {
divyadesaibbed37c2020-08-28 13:35:20 +0530579 forthLevelMap := map[string]interface{}(thirdLevelMap["Attributes"].(me.AttributeValueMap))
580 delete(forthLevelMap, "MacAddress")
581 forthLevelMap["MacAddress"] = "%MAC_ADDRESS%"
582 }
583 }
Himani Chawla26e555c2020-08-31 12:30:20 +0530584 secondLevelMap["ClassId"] = classID
585 templateMap[classID] = secondLevelMap
divyadesaibbed37c2020-08-28 13:35:20 +0530586 }
587 mibTemplate, err := json.Marshal(&templateMap)
588 if err != nil {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000589 logger.Errorw("MibSync - MibTemplate - Failed to marshal mibTemplate", log.Fields{"error": err, "device-id": oo.deviceID})
590 oo.pOpenOnuAc.lockMibTemplateGenerated.Lock()
591 oo.pOpenOnuAc.mibTemplatesGenerated[oo.mibTemplatePath] = false
592 oo.pOpenOnuAc.lockMibTemplateGenerated.Unlock()
divyadesaibbed37c2020-08-28 13:35:20 +0530593 return err
594 }
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000595 err = oo.mibTemplateKVStore.Put(context.TODO(), oo.mibTemplatePath, string(mibTemplate))
divyadesaibbed37c2020-08-28 13:35:20 +0530596 if err != nil {
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000597 logger.Errorw("MibSync - MibTemplate - Failed to store template in etcd", log.Fields{"error": err, "device-id": oo.deviceID})
598 oo.pOpenOnuAc.lockMibTemplateGenerated.Lock()
599 oo.pOpenOnuAc.mibTemplatesGenerated[oo.mibTemplatePath] = false
600 oo.pOpenOnuAc.lockMibTemplateGenerated.Unlock()
divyadesaibbed37c2020-08-28 13:35:20 +0530601 return err
602 }
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000603 logger.Debugw("MibSync - MibTemplate - Stored the template to etcd", log.Fields{"device-id": oo.deviceID})
divyadesaibbed37c2020-08-28 13:35:20 +0530604 return nil
605}
606
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000607// func (onuDeviceEntry *OnuDeviceEntry) MibTemplateTask() error {
608// return errors.New("not_implemented")
609// }
610// func (onuDeviceEntry *OnuDeviceEntry) MibUploadTask() error {
611// return errors.New("not_implemented")
612// }
613// func (onuDeviceEntry *OnuDeviceEntry) GetMdsTask() error {
614// return errors.New("not_implemented")
615// }
616// func (onuDeviceEntry *OnuDeviceEntry) MibResyncTask() error {
617// return errors.New("not_implemented")
618// }
619// func (onuDeviceEntry *OnuDeviceEntry) MibReconcileTask() error {
620// return errors.New("not_implemented")
621// }