blob: 7cc1cd4eb0c066f520890653e00e520b90eddaa8 [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"
dbainbri4d3a0dc2020-12-02 00:33:42 +000027 "github.com/opencord/voltha-lib-go/v4/pkg/log"
Holger Hildebrandt24d51952020-05-04 14:03:42 +000028)
29
Himani Chawla6d2ae152020-09-02 13:11:20 +053030type meDbMap map[me.ClassID]map[uint16]me.AttributeValueMap
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000031
Himani Chawla6d2ae152020-09-02 13:11:20 +053032//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
Himani Chawla6d2ae152020-09-02 13:11:20 +053036 meDb meDbMap
Holger Hildebrandt24d51952020-05-04 14:03:42 +000037}
38
Himani Chawla6d2ae152020-09-02 13:11:20 +053039//newOnuDeviceDB returns a new instance for a specific ONU_Device_Entry
40func newOnuDeviceDB(ctx context.Context, aPOnuDeviceEntry *OnuDeviceEntry) *onuDeviceDB {
dbainbri4d3a0dc2020-12-02 00:33:42 +000041 logger.Debugw(ctx, "Init OnuDeviceDB for:", log.Fields{"device-id": aPOnuDeviceEntry.deviceID})
Himani Chawla6d2ae152020-09-02 13:11:20 +053042 var onuDeviceDB onuDeviceDB
Holger Hildebrandt24d51952020-05-04 14:03:42 +000043 onuDeviceDB.ctx = ctx
Himani Chawla4d908332020-08-31 12:30:20 +053044 onuDeviceDB.pOnuDeviceEntry = aPOnuDeviceEntry
Himani Chawla6d2ae152020-09-02 13:11:20 +053045 onuDeviceDB.meDb = make(meDbMap)
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000046
Holger Hildebrandt24d51952020-05-04 14:03:42 +000047 return &onuDeviceDB
48}
49
dbainbri4d3a0dc2020-12-02 00:33:42 +000050func (onuDeviceDB *onuDeviceDB) PutMe(ctx context.Context, 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
dbainbri4d3a0dc2020-12-02 00:33:42 +000057 //logger.Debugw(ctx,"Search for key data :", log.Fields{"deviceId": onuDeviceDB.pOnuDeviceEntry.deviceID, "meClassID": meClassID, "meEntityID": meEntityID})
Himani Chawla4d908332020-08-31 12:30:20 +053058 meInstMap, ok := onuDeviceDB.meDb[meClassID]
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000059 if !ok {
dbainbri4d3a0dc2020-12-02 00:33:42 +000060 logger.Debugw(ctx, "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
dbainbri4d3a0dc2020-12-02 00:33:42 +000068 logger.Debugw(ctx,"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
dbainbri4d3a0dc2020-12-02 00:33:42 +000073 logger.Debugw(ctx,"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
dbainbri4d3a0dc2020-12-02 00:33:42 +000080 logger.Debugw(ctx,"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 Chawla6d2ae152020-09-02 13:11: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
dbainbri4d3a0dc2020-12-02 00:33:42 +000090 logger.Debugw(ctx,"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
Himani Chawla6d2ae152020-09-02 13:11:20 +053098func (onuDeviceDB *onuDeviceDB) getUint32Attrib(meAttribute interface{}) (uint32, error) {
Holger Hildebrandt2ff21f12020-08-13 14:38:02 +000099
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
Girish Gowdra041dcb32020-11-16 16:54:30 -0800111/*
112func (onuDeviceDB *onuDeviceDB) getUint16Attrib(meAttribute interface{}) (uint16, error) {
113
114 switch reflect.TypeOf(meAttribute).Kind() {
115 case reflect.Uint16:
116 return meAttribute.(uint16), nil
117 default:
118 return uint16(0), fmt.Errorf(fmt.Sprintf("wrong interface-type received-%s", onuDeviceDB.pOnuDeviceEntry.deviceID))
119 }
120}
121*/
122
dbainbri4d3a0dc2020-12-02 00:33:42 +0000123func (onuDeviceDB *onuDeviceDB) getSortedInstKeys(ctx context.Context, meClassID me.ClassID) []uint16 {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000124
125 var meInstKeys []uint16
126
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000127 meInstMap := onuDeviceDB.meDb[meClassID]
128
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000129 for k := range meInstMap {
130 meInstKeys = append(meInstKeys, k)
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000131 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000132 logger.Debugw(ctx, "meInstKeys - input order :", log.Fields{"meInstKeys": meInstKeys}) //TODO: delete the line after test phase!
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000133 sort.Slice(meInstKeys, func(i, j int) bool { return meInstKeys[i] < meInstKeys[j] })
dbainbri4d3a0dc2020-12-02 00:33:42 +0000134 logger.Debugw(ctx, "meInstKeys - output order :", log.Fields{"meInstKeys": meInstKeys}) //TODO: delete the line after test phase!
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000135 return meInstKeys
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000136}
137
dbainbri4d3a0dc2020-12-02 00:33:42 +0000138func (onuDeviceDB *onuDeviceDB) logMeDb(ctx context.Context) {
139 logger.Debugw(ctx, "ME instances stored for :", log.Fields{"device-id": onuDeviceDB.pOnuDeviceEntry.deviceID})
Himani Chawla4d908332020-08-31 12:30:20 +0530140 for meClassID, meInstMap := range onuDeviceDB.meDb {
141 for meEntityID, meAttribs := range meInstMap {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000142 logger.Debugw(ctx, "ME instance: ", log.Fields{"meClassID": meClassID, "meEntityID": meEntityID, "meAttribs": meAttribs, "device-id": onuDeviceDB.pOnuDeviceEntry.deviceID})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000143 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000144 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000145}