Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 1 | /* |
| 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 |
| 18 | package adaptercoreonu |
| 19 | |
| 20 | import ( |
| 21 | "context" |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 22 | "sort" |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 23 | |
| 24 | "github.com/opencord/omci-lib-go" |
| 25 | me "github.com/opencord/omci-lib-go/generated" |
| 26 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 27 | ) |
| 28 | |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 29 | type MeDbMap map[me.ClassID]map[uint16]me.AttributeValueMap |
| 30 | |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 31 | //OnuDeviceDB structure holds information about known ME's |
| 32 | type OnuDeviceDB struct { |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 33 | ctx context.Context |
| 34 | pOnuDeviceEntry *OnuDeviceEntry |
| 35 | meDb MeDbMap |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | //OnuDeviceDB returns a new instance for a specific ONU_Device_Entry |
| 39 | func NewOnuDeviceDB(ctx context.Context, a_pOnuDeviceEntry *OnuDeviceEntry) *OnuDeviceDB { |
| 40 | logger.Debugw("Init OnuDeviceDB for:", log.Fields{"deviceId": a_pOnuDeviceEntry.deviceID}) |
| 41 | var onuDeviceDB OnuDeviceDB |
| 42 | onuDeviceDB.ctx = ctx |
| 43 | onuDeviceDB.pOnuDeviceEntry = a_pOnuDeviceEntry |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 44 | onuDeviceDB.meDb = make(MeDbMap) |
| 45 | |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 46 | return &onuDeviceDB |
| 47 | } |
| 48 | |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 49 | func (onuDeviceDB *OnuDeviceDB) StoreMe(a_pMibUpResp *omci.MibUploadNextResponse) { |
| 50 | |
| 51 | meClassId := a_pMibUpResp.ReportedME.GetClassID() |
| 52 | meEntityId := a_pMibUpResp.ReportedME.GetEntityID() |
| 53 | meAttributes := a_pMibUpResp.ReportedME.GetAttributeValueMap() |
| 54 | |
| 55 | //filter out the OnuData |
| 56 | if me.OnuDataClassID == meClassId { |
| 57 | return |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 58 | } |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 59 | |
| 60 | logger.Debugw("Search for key data :", log.Fields{"deviceId": onuDeviceDB.pOnuDeviceEntry.deviceID, "meClassId": meClassId, "meEntityId": meEntityId}) |
| 61 | meInstMap, ok := onuDeviceDB.meDb[meClassId] |
| 62 | if !ok { |
| 63 | logger.Debugw("meClassId not found - add to db :", log.Fields{"deviceId": onuDeviceDB.pOnuDeviceEntry.deviceID}) |
| 64 | meInstMap = make(map[uint16]me.AttributeValueMap) |
| 65 | onuDeviceDB.meDb[meClassId] = meInstMap |
| 66 | onuDeviceDB.meDb[meClassId][meEntityId] = meAttributes |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 67 | } else { |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 68 | meAttribs, ok := onuDeviceDB.meDb[meClassId][meEntityId] |
| 69 | if !ok { |
| 70 | logger.Debugw("meEntityId not found - add to db :", log.Fields{"deviceId": onuDeviceDB.pOnuDeviceEntry.deviceID}) |
| 71 | onuDeviceDB.meDb[meClassId][meEntityId] = meAttributes |
| 72 | } else { |
| 73 | logger.Debugw("ME-Instance exists already: merge attribute data :", log.Fields{"deviceId": onuDeviceDB.pOnuDeviceEntry.deviceID, "meAttribs": meAttribs}) |
| 74 | |
| 75 | for k, v := range meAttributes { |
| 76 | meAttribs[k] = v |
| 77 | } |
| 78 | onuDeviceDB.meDb[meClassId][meEntityId] = meAttribs |
| 79 | logger.Debugw("ME-Instance updated :", log.Fields{"deviceId": onuDeviceDB.pOnuDeviceEntry.deviceID, "meAttribs": meAttribs}) |
| 80 | } |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 81 | } |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 84 | func (onuDeviceDB *OnuDeviceDB) GetSortedInstKeys(meInstMap map[uint16]me.AttributeValueMap) []uint16 { |
| 85 | |
| 86 | var meInstKeys []uint16 |
| 87 | |
| 88 | for k := range meInstMap { |
| 89 | meInstKeys = append(meInstKeys, k) |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 90 | } |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 91 | logger.Debugw("meInstKeys - input order :", log.Fields{"meInstKeys": meInstKeys}) //TODO: delete the line after test phase! |
| 92 | sort.Slice(meInstKeys, func(i, j int) bool { return meInstKeys[i] < meInstKeys[j] }) |
| 93 | logger.Debugw("meInstKeys - output order :", log.Fields{"meInstKeys": meInstKeys}) //TODO: delete the line after test phase! |
| 94 | return meInstKeys |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 97 | func (onuDeviceDB *OnuDeviceDB) LogMeDb() { |
| 98 | logger.Debugw("ME instances stored for :", log.Fields{"deviceId": onuDeviceDB.pOnuDeviceEntry.deviceID}) |
| 99 | for meClassId, meInstMap := range onuDeviceDB.meDb { |
| 100 | for meEntityId, meAttribs := range meInstMap { |
| 101 | logger.Debugw("ME instance: ", log.Fields{"meClassId": meClassId, "meEntityId": meEntityId, "meAttribs": meAttribs, "deviceId": onuDeviceDB.pOnuDeviceEntry.deviceID}) |
| 102 | } |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 103 | } |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 104 | } |