blob: def0dc383e1b9664d55fca44d6bcf34991ad5a46 [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 Hildebrandt2ff21f12020-08-13 14:38:02 +000022 "fmt"
23 "reflect"
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000024 "sort"
Holger Hildebrandt24d51952020-05-04 14:03:42 +000025
Holger Hildebrandt24d51952020-05-04 14:03:42 +000026 me "github.com/opencord/omci-lib-go/generated"
27 "github.com/opencord/voltha-lib-go/v3/pkg/log"
28)
29
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000030type MeDbMap map[me.ClassID]map[uint16]me.AttributeValueMap
31
Holger Hildebrandt24d51952020-05-04 14:03:42 +000032//OnuDeviceDB structure holds information about known ME's
33type OnuDeviceDB struct {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000034 ctx context.Context
35 pOnuDeviceEntry *OnuDeviceEntry
36 meDb MeDbMap
Holger Hildebrandt24d51952020-05-04 14:03:42 +000037}
38
39//OnuDeviceDB returns a new instance for a specific ONU_Device_Entry
Himani Chawla4d908332020-08-31 12:30:20 +053040func NewOnuDeviceDB(ctx context.Context, aPOnuDeviceEntry *OnuDeviceEntry) *OnuDeviceDB {
41 logger.Debugw("Init OnuDeviceDB for:", log.Fields{"device-id": aPOnuDeviceEntry.deviceID})
Holger Hildebrandt24d51952020-05-04 14:03:42 +000042 var onuDeviceDB OnuDeviceDB
43 onuDeviceDB.ctx = ctx
Himani Chawla4d908332020-08-31 12:30:20 +053044 onuDeviceDB.pOnuDeviceEntry = aPOnuDeviceEntry
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000045 onuDeviceDB.meDb = make(MeDbMap)
46
Holger Hildebrandt24d51952020-05-04 14:03:42 +000047 return &onuDeviceDB
48}
49
Himani Chawla4d908332020-08-31 12:30:20 +053050func (onuDeviceDB *OnuDeviceDB) PutMe(meClassID me.ClassID, meEntityID uint16, meAttributes me.AttributeValueMap) {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000051
52 //filter out the OnuData
Himani Chawla4d908332020-08-31 12:30:20 +053053 if me.OnuDataClassID == meClassID {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000054 return
Holger Hildebrandt24d51952020-05-04 14:03:42 +000055 }
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000056
Himani Chawla4d908332020-08-31 12:30:20 +053057 //logger.Debugw("Search for key data :", log.Fields{"deviceId": onuDeviceDB.pOnuDeviceEntry.deviceID, "meClassID": meClassID, "meEntityID": meEntityID})
58 meInstMap, ok := onuDeviceDB.meDb[meClassID]
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000059 if !ok {
Himani Chawla4d908332020-08-31 12:30:20 +053060 logger.Debugw("meClassID not found - add to db :", log.Fields{"device-id": onuDeviceDB.pOnuDeviceEntry.deviceID})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000061 meInstMap = make(map[uint16]me.AttributeValueMap)
Himani Chawla4d908332020-08-31 12:30:20 +053062 onuDeviceDB.meDb[meClassID] = meInstMap
63 onuDeviceDB.meDb[meClassID][meEntityID] = meAttributes
Holger Hildebrandt24d51952020-05-04 14:03:42 +000064 } else {
Himani Chawla4d908332020-08-31 12:30:20 +053065 meAttribs, ok := meInstMap[meEntityID]
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000066 if !ok {
mpagenkodff5dda2020-08-28 11:52:01 +000067 /* verbose logging, avoid in >= debug level
divyadesai4d299552020-08-18 07:13:49 +000068 logger.Debugw("meEntityId not found - add to db :", log.Fields{"device-id": onuDeviceDB.pOnuDeviceEntry.deviceID})
mpagenkodff5dda2020-08-28 11:52:01 +000069 */
Himani Chawla4d908332020-08-31 12:30:20 +053070 meInstMap[meEntityID] = meAttributes
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000071 } else {
mpagenkodff5dda2020-08-28 11:52:01 +000072 /* verbose logging, avoid in >= debug level
divyadesai4d299552020-08-18 07:13:49 +000073 logger.Debugw("ME-Instance exists already: merge attribute data :", log.Fields{"device-id": onuDeviceDB.pOnuDeviceEntry.deviceID, "meAttribs": meAttribs})
mpagenkodff5dda2020-08-28 11:52:01 +000074 */
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000075 for k, v := range meAttributes {
76 meAttribs[k] = v
77 }
Himani Chawla4d908332020-08-31 12:30:20 +053078 meInstMap[meEntityID] = meAttribs
mpagenkodff5dda2020-08-28 11:52:01 +000079 /* verbose logging, avoid in >= debug level
divyadesai4d299552020-08-18 07:13:49 +000080 logger.Debugw("ME-Instance updated :", log.Fields{"device-id": onuDeviceDB.pOnuDeviceEntry.deviceID, "meAttribs": meAttribs})
mpagenkodff5dda2020-08-28 11:52:01 +000081 */
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000082 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +000083 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +000084}
85
Himani Chawla4d908332020-08-31 12:30:20 +053086func (onuDeviceDB *OnuDeviceDB) GetMe(meClassID me.ClassID, meEntityID uint16) me.AttributeValueMap {
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +000087
Himani Chawla4d908332020-08-31 12:30:20 +053088 if meAttributes, present := onuDeviceDB.meDb[meClassID][meEntityID]; present {
mpagenkodff5dda2020-08-28 11:52:01 +000089 /* verbose logging, avoid in >= debug level
Himani Chawla4d908332020-08-31 12:30:20 +053090 logger.Debugw("ME found:", log.Fields{"meClassID": meClassID, "meEntityID": meEntityID, "meAttributes": meAttributes,
divyadesai4d299552020-08-18 07:13:49 +000091 "device-id": onuDeviceDB.pOnuDeviceEntry.deviceID})
mpagenkodff5dda2020-08-28 11:52:01 +000092 */
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +000093 return meAttributes
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +000094 }
Himani Chawla4d908332020-08-31 12:30:20 +053095 return nil
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +000096}
97
Holger Hildebrandt2ff21f12020-08-13 14:38:02 +000098func (onuDeviceDB *OnuDeviceDB) GetUint32Attrib(meAttribute interface{}) (uint32, error) {
99
100 switch reflect.TypeOf(meAttribute).Kind() {
101 case reflect.Float64:
102 //JSON numbers by default are unmarshaled into values of float64 type if type information is not present
103 return uint32(meAttribute.(float64)), nil
104 case reflect.Uint32:
105 return uint32(meAttribute.(uint32)), nil
106 default:
107 return uint32(0), fmt.Errorf(fmt.Sprintf("wrong interface-type received-%s", onuDeviceDB.pOnuDeviceEntry.deviceID))
108 }
109}
110
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000111func (onuDeviceDB *OnuDeviceDB) GetSortedInstKeys(meClassID me.ClassID) []uint16 {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000112
113 var meInstKeys []uint16
114
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000115 meInstMap := onuDeviceDB.meDb[meClassID]
116
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000117 for k := range meInstMap {
118 meInstKeys = append(meInstKeys, k)
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000119 }
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000120 logger.Debugw("meInstKeys - input order :", log.Fields{"meInstKeys": meInstKeys}) //TODO: delete the line after test phase!
121 sort.Slice(meInstKeys, func(i, j int) bool { return meInstKeys[i] < meInstKeys[j] })
122 logger.Debugw("meInstKeys - output order :", log.Fields{"meInstKeys": meInstKeys}) //TODO: delete the line after test phase!
123 return meInstKeys
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000124}
125
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000126func (onuDeviceDB *OnuDeviceDB) LogMeDb() {
divyadesai4d299552020-08-18 07:13:49 +0000127 logger.Debugw("ME instances stored for :", log.Fields{"device-id": onuDeviceDB.pOnuDeviceEntry.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530128 for meClassID, meInstMap := range onuDeviceDB.meDb {
129 for meEntityID, meAttribs := range meInstMap {
130 logger.Debugw("ME instance: ", log.Fields{"meClassID": meClassID, "meEntityID": meEntityID, "meAttribs": meAttribs, "device-id": onuDeviceDB.pOnuDeviceEntry.deviceID})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000131 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000132 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000133}