blob: d3338c8628e02f8e07ab5af1dcf80acf76cc969f [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 Hildebrandtf3c7a182021-11-17 13:42:08 +000022 "encoding/hex"
Holger Hildebrandt2ff21f12020-08-13 14:38:02 +000023 "fmt"
24 "reflect"
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000025 "sort"
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +000026 "strconv"
Mahir Gunyel7f4483a2021-05-06 12:53:43 -070027 "sync"
Holger Hildebrandt24d51952020-05-04 14:03:42 +000028
mpagenko836a1fd2021-11-01 16:12:42 +000029 me "github.com/opencord/omci-lib-go/v2/generated"
khenaidoo7d3c5582021-08-11 18:09:44 -040030 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Holger Hildebrandt24d51952020-05-04 14:03:42 +000031)
32
Himani Chawla6d2ae152020-09-02 13:11:20 +053033type meDbMap map[me.ClassID]map[uint16]me.AttributeValueMap
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000034
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +000035// UnknownMeName type to be used for unknown ME names
36type UnknownMeName string
37
38// allowed values for type UnknownMeName
39const (
40 CUnknownItuG988ManagedEntity = "UnknownItuG988ManagedEntity"
41 CUnknownVendorSpecificManagedEntity = "UnknownVendorSpecificManagedEntity"
42)
43
44type unknownMeAttribs struct {
45 AttribMask string `json:"AttributeMask"`
46 AttribBytes string `json:"AttributeBytes"`
47}
48type unknownMeDbMap map[UnknownMeName]map[me.ClassID]map[uint16]unknownMeAttribs
49
50//OnuDeviceDB structure holds information about ME's
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000051type OnuDeviceDB struct {
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +000052 ctx context.Context
53 deviceID string
54 MeDb meDbMap
55 meDbLock sync.RWMutex
56 UnknownMeDb unknownMeDbMap
Holger Hildebrandt24d51952020-05-04 14:03:42 +000057}
58
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000059//NewOnuDeviceDB returns a new instance for a specific ONU_Device_Entry
60func NewOnuDeviceDB(ctx context.Context, aDeviceID string) *OnuDeviceDB {
61 logger.Debugw(ctx, "Init OnuDeviceDB for:", log.Fields{"device-id": aDeviceID})
62 var OnuDeviceDB OnuDeviceDB
63 OnuDeviceDB.ctx = ctx
64 OnuDeviceDB.deviceID = aDeviceID
65 OnuDeviceDB.MeDb = make(meDbMap)
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +000066 OnuDeviceDB.UnknownMeDb = make(unknownMeDbMap)
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000067
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000068 return &OnuDeviceDB
Holger Hildebrandt24d51952020-05-04 14:03:42 +000069}
70
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000071//PutMe puts an ME instance into internal ONU DB
72func (OnuDeviceDB *OnuDeviceDB) PutMe(ctx context.Context, meClassID me.ClassID, meEntityID uint16, meAttributes me.AttributeValueMap) {
73 OnuDeviceDB.meDbLock.Lock()
74 defer OnuDeviceDB.meDbLock.Unlock()
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000075 //filter out the OnuData
Himani Chawla4d908332020-08-31 12:30:20 +053076 if me.OnuDataClassID == meClassID {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000077 return
Holger Hildebrandt24d51952020-05-04 14:03:42 +000078 }
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +000079 _, ok := OnuDeviceDB.MeDb[meClassID]
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000080 if !ok {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000081 logger.Debugw(ctx, "meClassID not found - add to db :", log.Fields{"device-id": OnuDeviceDB.deviceID})
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +000082 OnuDeviceDB.MeDb[meClassID] = make(map[uint16]me.AttributeValueMap)
83 OnuDeviceDB.MeDb[meClassID][meEntityID] = make(me.AttributeValueMap)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000084 OnuDeviceDB.MeDb[meClassID][meEntityID] = meAttributes
Holger Hildebrandt24d51952020-05-04 14:03:42 +000085 } else {
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +000086 meAttribs, ok := OnuDeviceDB.MeDb[meClassID][meEntityID]
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000087 if !ok {
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +000088 OnuDeviceDB.MeDb[meClassID][meEntityID] = make(me.AttributeValueMap)
89 OnuDeviceDB.MeDb[meClassID][meEntityID] = meAttributes
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000090 } else {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000091 for k, v := range meAttributes {
92 meAttribs[k] = v
93 }
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +000094 OnuDeviceDB.MeDb[meClassID][meEntityID] = meAttribs
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000095 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +000096 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +000097}
98
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000099//GetMe returns an ME instance from internal ONU DB
100func (OnuDeviceDB *OnuDeviceDB) GetMe(meClassID me.ClassID, meEntityID uint16) me.AttributeValueMap {
101 OnuDeviceDB.meDbLock.RLock()
102 defer OnuDeviceDB.meDbLock.RUnlock()
103 if meAttributes, present := OnuDeviceDB.MeDb[meClassID][meEntityID]; present {
mpagenkodff5dda2020-08-28 11:52:01 +0000104 /* verbose logging, avoid in >= debug level
dbainbri4d3a0dc2020-12-02 00:33:42 +0000105 logger.Debugw(ctx,"ME found:", log.Fields{"meClassID": meClassID, "meEntityID": meEntityID, "meAttributes": meAttributes,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000106 "device-id": OnuDeviceDB.deviceID})
mpagenkodff5dda2020-08-28 11:52:01 +0000107 */
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000108 return meAttributes
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000109 }
Himani Chawla4d908332020-08-31 12:30:20 +0530110 return nil
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000111}
112
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000113//GetUint32Attrib converts JSON numbers to uint32
114func (OnuDeviceDB *OnuDeviceDB) GetUint32Attrib(meAttribute interface{}) (uint32, error) {
Holger Hildebrandt2ff21f12020-08-13 14:38:02 +0000115
116 switch reflect.TypeOf(meAttribute).Kind() {
117 case reflect.Float64:
118 //JSON numbers by default are unmarshaled into values of float64 type if type information is not present
119 return uint32(meAttribute.(float64)), nil
120 case reflect.Uint32:
Girish Gowdra09e5f212021-09-30 16:28:36 -0700121 return meAttribute.(uint32), nil
Holger Hildebrandt2ff21f12020-08-13 14:38:02 +0000122 default:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000123 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 +0000124 }
125}
126
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000127//GetUint16Attrib converts JSON numbers to uint16
128func (OnuDeviceDB *OnuDeviceDB) GetUint16Attrib(meAttribute interface{}) (uint16, error) {
Girish Gowdra041dcb32020-11-16 16:54:30 -0800129
130 switch reflect.TypeOf(meAttribute).Kind() {
Girish Gowdra09e5f212021-09-30 16:28:36 -0700131 case reflect.Float64:
132 //JSON numbers by default are unmarshaled into values of float64 type if type information is not present
133 return uint16(meAttribute.(float64)), nil
Girish Gowdra041dcb32020-11-16 16:54:30 -0800134 case reflect.Uint16:
135 return meAttribute.(uint16), nil
136 default:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000137 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 -0800138 }
139}
Girish Gowdra041dcb32020-11-16 16:54:30 -0800140
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000141//GetSortedInstKeys returns a sorted list of all instances of an ME
142func (OnuDeviceDB *OnuDeviceDB) GetSortedInstKeys(ctx context.Context, meClassID me.ClassID) []uint16 {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000143
144 var meInstKeys []uint16
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000145 OnuDeviceDB.meDbLock.RLock()
146 defer OnuDeviceDB.meDbLock.RUnlock()
147 meInstMap := OnuDeviceDB.MeDb[meClassID]
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000148
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000149 for k := range meInstMap {
150 meInstKeys = append(meInstKeys, k)
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000151 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000152 logger.Debugw(ctx, "meInstKeys - input order :", log.Fields{"meInstKeys": meInstKeys}) //TODO: delete the line after test phase!
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000153 sort.Slice(meInstKeys, func(i, j int) bool { return meInstKeys[i] < meInstKeys[j] })
dbainbri4d3a0dc2020-12-02 00:33:42 +0000154 logger.Debugw(ctx, "meInstKeys - output order :", log.Fields{"meInstKeys": meInstKeys}) //TODO: delete the line after test phase!
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000155 return meInstKeys
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000156}
157
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000158//LogMeDb logs content of internal ONU DB
159func (OnuDeviceDB *OnuDeviceDB) LogMeDb(ctx context.Context) {
160 logger.Debugw(ctx, "ME instances stored for :", log.Fields{"device-id": OnuDeviceDB.deviceID})
161 for meClassID, meInstMap := range OnuDeviceDB.MeDb {
Himani Chawla4d908332020-08-31 12:30:20 +0530162 for meEntityID, meAttribs := range meInstMap {
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +0000163 logger.Debugw(ctx, "ME instance: ", log.Fields{"meClassID": meClassID, "meEntityID": meEntityID, "meAttribs": meAttribs,
164 "device-id": OnuDeviceDB.deviceID})
165 }
166 }
167}
168
169//PutUnknownMe puts an unknown ME instance into internal ONU DB
170func (OnuDeviceDB *OnuDeviceDB) PutUnknownMe(ctx context.Context, aMeName UnknownMeName, aMeClassID me.ClassID, aMeEntityID uint16,
171 aMeAttributeMask uint16, aMePayload []byte) {
172
173 meAttribMaskStr := strconv.FormatUint(uint64(aMeAttributeMask), 16)
174 attribs := unknownMeAttribs{meAttribMaskStr, hex.EncodeToString(aMePayload)}
175
176 _, ok := OnuDeviceDB.UnknownMeDb[aMeName]
177 if !ok {
178 OnuDeviceDB.UnknownMeDb[aMeName] = make(map[me.ClassID]map[uint16]unknownMeAttribs)
179 OnuDeviceDB.UnknownMeDb[aMeName][aMeClassID] = make(map[uint16]unknownMeAttribs)
180 OnuDeviceDB.UnknownMeDb[aMeName][aMeClassID][aMeEntityID] = attribs
181 } else {
182 _, ok := OnuDeviceDB.UnknownMeDb[aMeName][aMeClassID]
183 if !ok {
184 OnuDeviceDB.UnknownMeDb[aMeName][aMeClassID] = make(map[uint16]unknownMeAttribs)
185 OnuDeviceDB.UnknownMeDb[aMeName][aMeClassID][aMeEntityID] = attribs
186 } else {
187 _, ok := OnuDeviceDB.UnknownMeDb[aMeName][aMeClassID][aMeEntityID]
188 if !ok {
189 OnuDeviceDB.UnknownMeDb[aMeName][aMeClassID][aMeEntityID] = attribs
190 }
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000191 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000192 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000193}