blob: 63ad543ca2157d3b3e13f7e358776fa9f864062c [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
Holger Hildebrandt24d51952020-05-04 14:03:42 +000024 me "github.com/opencord/omci-lib-go/generated"
25 "github.com/opencord/voltha-lib-go/v3/pkg/log"
26)
27
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000028type MeDbMap map[me.ClassID]map[uint16]me.AttributeValueMap
29
Holger Hildebrandt24d51952020-05-04 14:03:42 +000030//OnuDeviceDB structure holds information about known ME's
31type OnuDeviceDB struct {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000032 ctx context.Context
33 pOnuDeviceEntry *OnuDeviceEntry
34 meDb MeDbMap
Holger Hildebrandt24d51952020-05-04 14:03:42 +000035}
36
37//OnuDeviceDB returns a new instance for a specific ONU_Device_Entry
38func NewOnuDeviceDB(ctx context.Context, a_pOnuDeviceEntry *OnuDeviceEntry) *OnuDeviceDB {
divyadesai4d299552020-08-18 07:13:49 +000039 logger.Debugw("Init OnuDeviceDB for:", log.Fields{"device-id": a_pOnuDeviceEntry.deviceID})
Holger Hildebrandt24d51952020-05-04 14:03:42 +000040 var onuDeviceDB OnuDeviceDB
41 onuDeviceDB.ctx = ctx
42 onuDeviceDB.pOnuDeviceEntry = a_pOnuDeviceEntry
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000043 onuDeviceDB.meDb = make(MeDbMap)
44
Holger Hildebrandt24d51952020-05-04 14:03:42 +000045 return &onuDeviceDB
46}
47
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +000048func (onuDeviceDB *OnuDeviceDB) PutMe(meClassId me.ClassID, meEntityId uint16, meAttributes me.AttributeValueMap) {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000049
50 //filter out the OnuData
51 if me.OnuDataClassID == meClassId {
52 return
Holger Hildebrandt24d51952020-05-04 14:03:42 +000053 }
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000054
mpagenko1cc3cb42020-07-27 15:24:38 +000055 //logger.Debugw("Search for key data :", log.Fields{"deviceId": onuDeviceDB.pOnuDeviceEntry.deviceID, "meClassId": meClassId, "meEntityId": meEntityId})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000056 meInstMap, ok := onuDeviceDB.meDb[meClassId]
57 if !ok {
divyadesai4d299552020-08-18 07:13:49 +000058 logger.Debugw("meClassId not found - add to db :", log.Fields{"device-id": onuDeviceDB.pOnuDeviceEntry.deviceID})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000059 meInstMap = make(map[uint16]me.AttributeValueMap)
60 onuDeviceDB.meDb[meClassId] = meInstMap
61 onuDeviceDB.meDb[meClassId][meEntityId] = meAttributes
Holger Hildebrandt24d51952020-05-04 14:03:42 +000062 } else {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000063 meAttribs, ok := onuDeviceDB.meDb[meClassId][meEntityId]
64 if !ok {
divyadesai4d299552020-08-18 07:13:49 +000065 logger.Debugw("meEntityId not found - add to db :", log.Fields{"device-id": onuDeviceDB.pOnuDeviceEntry.deviceID})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000066 onuDeviceDB.meDb[meClassId][meEntityId] = meAttributes
67 } else {
divyadesai4d299552020-08-18 07:13:49 +000068 logger.Debugw("ME-Instance exists already: merge attribute data :", log.Fields{"device-id": onuDeviceDB.pOnuDeviceEntry.deviceID, "meAttribs": meAttribs})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000069
70 for k, v := range meAttributes {
71 meAttribs[k] = v
72 }
73 onuDeviceDB.meDb[meClassId][meEntityId] = meAttribs
divyadesai4d299552020-08-18 07:13:49 +000074 logger.Debugw("ME-Instance updated :", log.Fields{"device-id": onuDeviceDB.pOnuDeviceEntry.deviceID, "meAttribs": meAttribs})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000075 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +000076 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +000077}
78
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +000079func (onuDeviceDB *OnuDeviceDB) GetMe(meClassId me.ClassID, meEntityId uint16) me.AttributeValueMap {
80
81 if meAttributes, present := onuDeviceDB.meDb[meClassId][meEntityId]; present {
82 logger.Debugw("ME found:", log.Fields{"meClassId": meClassId, "meEntityId": meEntityId, "meAttributes": meAttributes,
divyadesai4d299552020-08-18 07:13:49 +000083 "device-id": onuDeviceDB.pOnuDeviceEntry.deviceID})
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +000084 return meAttributes
85 } else {
86 return nil
87 }
88}
89
90func (onuDeviceDB *OnuDeviceDB) GetSortedInstKeys(meClassID me.ClassID) []uint16 {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000091
92 var meInstKeys []uint16
93
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +000094 meInstMap := onuDeviceDB.meDb[meClassID]
95
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000096 for k := range meInstMap {
97 meInstKeys = append(meInstKeys, k)
Holger Hildebrandt24d51952020-05-04 14:03:42 +000098 }
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000099 logger.Debugw("meInstKeys - input order :", log.Fields{"meInstKeys": meInstKeys}) //TODO: delete the line after test phase!
100 sort.Slice(meInstKeys, func(i, j int) bool { return meInstKeys[i] < meInstKeys[j] })
101 logger.Debugw("meInstKeys - output order :", log.Fields{"meInstKeys": meInstKeys}) //TODO: delete the line after test phase!
102 return meInstKeys
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000103}
104
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000105func (onuDeviceDB *OnuDeviceDB) LogMeDb() {
divyadesai4d299552020-08-18 07:13:49 +0000106 logger.Debugw("ME instances stored for :", log.Fields{"device-id": onuDeviceDB.pOnuDeviceEntry.deviceID})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000107 for meClassId, meInstMap := range onuDeviceDB.meDb {
108 for meEntityId, meAttribs := range meInstMap {
divyadesai4d299552020-08-18 07:13:49 +0000109 logger.Debugw("ME instance: ", log.Fields{"meClassId": meClassId, "meEntityId": meEntityId, "meAttribs": meAttribs, "device-id": onuDeviceDB.pOnuDeviceEntry.deviceID})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000110 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000111 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000112}