blob: 0b985d64a2580dd0324313729cd3c429418e0ea2 [file] [log] [blame]
Holger Hildebrandt24d51952020-05-04 14:03:42 +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 (
21 "context"
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000022 "sort"
Holger Hildebrandt24d51952020-05-04 14:03:42 +000023
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 Hildebrandt9ac0d0f2020-05-13 11:22:02 +000029type MeDbMap map[me.ClassID]map[uint16]me.AttributeValueMap
30
Holger Hildebrandt24d51952020-05-04 14:03:42 +000031//OnuDeviceDB structure holds information about known ME's
32type OnuDeviceDB struct {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000033 ctx context.Context
34 pOnuDeviceEntry *OnuDeviceEntry
35 meDb MeDbMap
Holger Hildebrandt24d51952020-05-04 14:03:42 +000036}
37
38//OnuDeviceDB returns a new instance for a specific ONU_Device_Entry
39func 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 Hildebrandt9ac0d0f2020-05-13 11:22:02 +000044 onuDeviceDB.meDb = make(MeDbMap)
45
Holger Hildebrandt24d51952020-05-04 14:03:42 +000046 return &onuDeviceDB
47}
48
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000049func (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 Hildebrandt24d51952020-05-04 14:03:42 +000058 }
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000059
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 Hildebrandt24d51952020-05-04 14:03:42 +000067 } else {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000068 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 Hildebrandt24d51952020-05-04 14:03:42 +000081 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +000082}
83
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000084func (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 Hildebrandt24d51952020-05-04 14:03:42 +000090 }
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000091 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 Hildebrandt24d51952020-05-04 14:03:42 +000095}
96
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000097func (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 Hildebrandt24d51952020-05-04 14:03:42 +0000103 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000104}