blob: 008c37c36a014971ca347aa28a7e6df383928c26 [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
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000017//Package devdb provides access to internal device ME DB
18package devdb
Holger Hildebrandt24d51952020-05-04 14:03:42 +000019
20import (
21 "context"
Holger Hildebrandt2ff21f12020-08-13 14:38:02 +000022 "fmt"
23 "reflect"
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000024 "sort"
Mahir Gunyel7f4483a2021-05-06 12:53:43 -070025 "sync"
Holger Hildebrandt24d51952020-05-04 14:03:42 +000026
mpagenko836a1fd2021-11-01 16:12:42 +000027 me "github.com/opencord/omci-lib-go/v2/generated"
khenaidoo7d3c5582021-08-11 18:09:44 -040028 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Holger Hildebrandt24d51952020-05-04 14:03:42 +000029)
30
Himani Chawla6d2ae152020-09-02 13:11:20 +053031type meDbMap map[me.ClassID]map[uint16]me.AttributeValueMap
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000032
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000033//OnuDeviceDB structure holds information about known ME's
34type OnuDeviceDB struct {
35 ctx context.Context
36 deviceID string
37 MeDb meDbMap
38 meDbLock sync.RWMutex
Holger Hildebrandt24d51952020-05-04 14:03:42 +000039}
40
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000041//NewOnuDeviceDB returns a new instance for a specific ONU_Device_Entry
42func NewOnuDeviceDB(ctx context.Context, aDeviceID string) *OnuDeviceDB {
43 logger.Debugw(ctx, "Init OnuDeviceDB for:", log.Fields{"device-id": aDeviceID})
44 var OnuDeviceDB OnuDeviceDB
45 OnuDeviceDB.ctx = ctx
46 OnuDeviceDB.deviceID = aDeviceID
47 OnuDeviceDB.MeDb = make(meDbMap)
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000048
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000049 return &OnuDeviceDB
Holger Hildebrandt24d51952020-05-04 14:03:42 +000050}
51
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000052//PutMe puts an ME instance into internal ONU DB
53func (OnuDeviceDB *OnuDeviceDB) PutMe(ctx context.Context, meClassID me.ClassID, meEntityID uint16, meAttributes me.AttributeValueMap) {
54 OnuDeviceDB.meDbLock.Lock()
55 defer OnuDeviceDB.meDbLock.Unlock()
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000056 //filter out the OnuData
Himani Chawla4d908332020-08-31 12:30:20 +053057 if me.OnuDataClassID == meClassID {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000058 return
Holger Hildebrandt24d51952020-05-04 14:03:42 +000059 }
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000060
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000061 //logger.Debugw(ctx,"Search for key data :", log.Fields{"deviceId": OnuDeviceDB.deviceID, "meClassID": meClassID, "meEntityID": meEntityID})
62 meInstMap, ok := OnuDeviceDB.MeDb[meClassID]
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000063 if !ok {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000064 logger.Debugw(ctx, "meClassID not found - add to db :", log.Fields{"device-id": OnuDeviceDB.deviceID})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000065 meInstMap = make(map[uint16]me.AttributeValueMap)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000066 OnuDeviceDB.MeDb[meClassID] = meInstMap
67 OnuDeviceDB.MeDb[meClassID][meEntityID] = meAttributes
Holger Hildebrandt24d51952020-05-04 14:03:42 +000068 } else {
Himani Chawla4d908332020-08-31 12:30:20 +053069 meAttribs, ok := meInstMap[meEntityID]
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000070 if !ok {
mpagenkodff5dda2020-08-28 11:52:01 +000071 /* verbose logging, avoid in >= debug level
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000072 logger.Debugw(ctx,"meEntityId not found - add to db :", log.Fields{"device-id": OnuDeviceDB.deviceID})
mpagenkodff5dda2020-08-28 11:52:01 +000073 */
Himani Chawla4d908332020-08-31 12:30:20 +053074 meInstMap[meEntityID] = meAttributes
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000075 } else {
mpagenkodff5dda2020-08-28 11:52:01 +000076 /* verbose logging, avoid in >= debug level
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000077 logger.Debugw(ctx,"ME-Instance exists already: merge attribute data :", log.Fields{"device-id": OnuDeviceDB.deviceID, "meAttribs": meAttribs})
mpagenkodff5dda2020-08-28 11:52:01 +000078 */
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000079 for k, v := range meAttributes {
80 meAttribs[k] = v
81 }
Himani Chawla4d908332020-08-31 12:30:20 +053082 meInstMap[meEntityID] = meAttribs
mpagenkodff5dda2020-08-28 11:52:01 +000083 /* verbose logging, avoid in >= debug level
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000084 logger.Debugw(ctx,"ME-Instance updated :", log.Fields{"device-id": OnuDeviceDB.deviceID, "meAttribs": meAttribs})
mpagenkodff5dda2020-08-28 11:52:01 +000085 */
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000086 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +000087 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +000088}
89
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000090//GetMe returns an ME instance from internal ONU DB
91func (OnuDeviceDB *OnuDeviceDB) GetMe(meClassID me.ClassID, meEntityID uint16) me.AttributeValueMap {
92 OnuDeviceDB.meDbLock.RLock()
93 defer OnuDeviceDB.meDbLock.RUnlock()
94 if meAttributes, present := OnuDeviceDB.MeDb[meClassID][meEntityID]; present {
mpagenkodff5dda2020-08-28 11:52:01 +000095 /* verbose logging, avoid in >= debug level
dbainbri4d3a0dc2020-12-02 00:33:42 +000096 logger.Debugw(ctx,"ME found:", log.Fields{"meClassID": meClassID, "meEntityID": meEntityID, "meAttributes": meAttributes,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000097 "device-id": OnuDeviceDB.deviceID})
mpagenkodff5dda2020-08-28 11:52:01 +000098 */
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +000099 return meAttributes
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000100 }
Himani Chawla4d908332020-08-31 12:30:20 +0530101 return nil
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000102}
103
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000104//GetUint32Attrib converts JSON numbers to uint32
105func (OnuDeviceDB *OnuDeviceDB) GetUint32Attrib(meAttribute interface{}) (uint32, error) {
Holger Hildebrandt2ff21f12020-08-13 14:38:02 +0000106
107 switch reflect.TypeOf(meAttribute).Kind() {
108 case reflect.Float64:
109 //JSON numbers by default are unmarshaled into values of float64 type if type information is not present
110 return uint32(meAttribute.(float64)), nil
111 case reflect.Uint32:
Girish Gowdra09e5f212021-09-30 16:28:36 -0700112 return meAttribute.(uint32), nil
Holger Hildebrandt2ff21f12020-08-13 14:38:02 +0000113 default:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000114 return uint32(0), fmt.Errorf(fmt.Sprintf("wrong-interface-type-%v-received-for-device-%s", reflect.TypeOf(meAttribute).Kind(), OnuDeviceDB.deviceID))
Holger Hildebrandt2ff21f12020-08-13 14:38:02 +0000115 }
116}
117
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000118//GetUint16Attrib converts JSON numbers to uint16
119func (OnuDeviceDB *OnuDeviceDB) GetUint16Attrib(meAttribute interface{}) (uint16, error) {
Girish Gowdra041dcb32020-11-16 16:54:30 -0800120
121 switch reflect.TypeOf(meAttribute).Kind() {
Girish Gowdra09e5f212021-09-30 16:28:36 -0700122 case reflect.Float64:
123 //JSON numbers by default are unmarshaled into values of float64 type if type information is not present
124 return uint16(meAttribute.(float64)), nil
Girish Gowdra041dcb32020-11-16 16:54:30 -0800125 case reflect.Uint16:
126 return meAttribute.(uint16), nil
127 default:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000128 return uint16(0), fmt.Errorf(fmt.Sprintf("wrong-interface-type-%v-received-for-device-%s", reflect.TypeOf(meAttribute).Kind(), OnuDeviceDB.deviceID))
Girish Gowdra041dcb32020-11-16 16:54:30 -0800129 }
130}
Girish Gowdra041dcb32020-11-16 16:54:30 -0800131
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000132//GetSortedInstKeys returns a sorted list of all instances of an ME
133func (OnuDeviceDB *OnuDeviceDB) GetSortedInstKeys(ctx context.Context, meClassID me.ClassID) []uint16 {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000134
135 var meInstKeys []uint16
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000136 OnuDeviceDB.meDbLock.RLock()
137 defer OnuDeviceDB.meDbLock.RUnlock()
138 meInstMap := OnuDeviceDB.MeDb[meClassID]
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000139
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000140 for k := range meInstMap {
141 meInstKeys = append(meInstKeys, k)
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000142 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000143 logger.Debugw(ctx, "meInstKeys - input order :", log.Fields{"meInstKeys": meInstKeys}) //TODO: delete the line after test phase!
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000144 sort.Slice(meInstKeys, func(i, j int) bool { return meInstKeys[i] < meInstKeys[j] })
dbainbri4d3a0dc2020-12-02 00:33:42 +0000145 logger.Debugw(ctx, "meInstKeys - output order :", log.Fields{"meInstKeys": meInstKeys}) //TODO: delete the line after test phase!
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000146 return meInstKeys
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000147}
148
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000149//LogMeDb logs content of internal ONU DB
150func (OnuDeviceDB *OnuDeviceDB) LogMeDb(ctx context.Context) {
151 logger.Debugw(ctx, "ME instances stored for :", log.Fields{"device-id": OnuDeviceDB.deviceID})
152 for meClassID, meInstMap := range OnuDeviceDB.MeDb {
Himani Chawla4d908332020-08-31 12:30:20 +0530153 for meEntityID, meAttribs := range meInstMap {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000154 logger.Debugw(ctx, "ME instance: ", log.Fields{"meClassID": meClassID, "meEntityID": meEntityID, "meAttribs": meAttribs, "device-id": OnuDeviceDB.deviceID})
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000155 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000156 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000157}