blob: dc3e82d6fe9d07c10a8abc139afd4eb66ff352e4 [file] [log] [blame]
Holger Hildebrandt24d51952020-05-04 14:03:42 +00001/*
Joey Armstrong89c812c2024-01-12 19:00:20 -05002 * Copyright 2020-2024 Open Networking Foundation (ONF) and the ONF Contributors
Holger Hildebrandt24d51952020-05-04 14:03:42 +00003 *
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
praneeth kumar nalmas3947c582023-12-13 15:38:50 +053017// Package devdb provides access to internal device ME DB
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000018package 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"
Mahir Gunyel7f4483a2021-05-06 12:53:43 -070026 "sync"
Holger Hildebrandt24d51952020-05-04 14:03:42 +000027
mpagenko836a1fd2021-11-01 16:12:42 +000028 me "github.com/opencord/omci-lib-go/v2/generated"
khenaidoo7d3c5582021-08-11 18:09:44 -040029 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Holger Hildebrandt24d51952020-05-04 14:03:42 +000030)
31
Himani Chawla6d2ae152020-09-02 13:11:20 +053032type meDbMap map[me.ClassID]map[uint16]me.AttributeValueMap
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000033
Holger Hildebrandt39080022022-03-16 10:03:17 +000034// UnknownMeOrAttribName type to be used for unknown ME and attribute names
35type UnknownMeOrAttribName string
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +000036
Holger Hildebrandt39080022022-03-16 10:03:17 +000037// names for unknown ME and attribute identifiers
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +000038const (
39 CUnknownItuG988ManagedEntity = "UnknownItuG988ManagedEntity"
40 CUnknownVendorSpecificManagedEntity = "UnknownVendorSpecificManagedEntity"
Holger Hildebrandt39080022022-03-16 10:03:17 +000041 CUnknownAttributesManagedEntity = "UnknownAttributesManagedEntity"
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +000042)
43
Holger Hildebrandt39080022022-03-16 10:03:17 +000044// CStartUnknownMeAttribsInBaseLayerPayload defines start of unknown ME attribs after ClassID, InstanceID and AttribMask
45const CStartUnknownMeAttribsInBaseLayerPayload = 6
46
47type unknownAttribs struct {
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +000048 AttribMask string `json:"AttributeMask"`
49 AttribBytes string `json:"AttributeBytes"`
50}
Holger Hildebrandt39080022022-03-16 10:03:17 +000051type unknownMeAndAttribDbMap map[UnknownMeOrAttribName]map[me.ClassID]map[uint16]unknownAttribs
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +000052
praneeth kumar nalmas3947c582023-12-13 15:38:50 +053053// OnuDeviceDB structure holds information about ME's
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000054type OnuDeviceDB struct {
Holger Hildebrandt39080022022-03-16 10:03:17 +000055 ctx context.Context
56 deviceID string
57 MeDb meDbMap
58 meDbLock sync.RWMutex
59 UnknownMeAndAttribDb unknownMeAndAttribDbMap
Holger Hildebrandt24d51952020-05-04 14:03:42 +000060}
61
praneeth kumar nalmas3947c582023-12-13 15:38:50 +053062// NewOnuDeviceDB returns a new instance for a specific ONU_Device_Entry
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000063func NewOnuDeviceDB(ctx context.Context, aDeviceID string) *OnuDeviceDB {
64 logger.Debugw(ctx, "Init OnuDeviceDB for:", log.Fields{"device-id": aDeviceID})
65 var OnuDeviceDB OnuDeviceDB
66 OnuDeviceDB.ctx = ctx
67 OnuDeviceDB.deviceID = aDeviceID
68 OnuDeviceDB.MeDb = make(meDbMap)
Holger Hildebrandt39080022022-03-16 10:03:17 +000069 OnuDeviceDB.UnknownMeAndAttribDb = make(unknownMeAndAttribDbMap)
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000070
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000071 return &OnuDeviceDB
Holger Hildebrandt24d51952020-05-04 14:03:42 +000072}
73
praneeth kumar nalmas3947c582023-12-13 15:38:50 +053074// PutMe puts an ME instance into internal ONU DB
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000075func (OnuDeviceDB *OnuDeviceDB) PutMe(ctx context.Context, meClassID me.ClassID, meEntityID uint16, meAttributes me.AttributeValueMap) {
76 OnuDeviceDB.meDbLock.Lock()
77 defer OnuDeviceDB.meDbLock.Unlock()
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000078 //filter out the OnuData
Himani Chawla4d908332020-08-31 12:30:20 +053079 if me.OnuDataClassID == meClassID {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000080 return
Holger Hildebrandt24d51952020-05-04 14:03:42 +000081 }
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +000082 _, ok := OnuDeviceDB.MeDb[meClassID]
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000083 if !ok {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000084 logger.Debugw(ctx, "meClassID not found - add to db :", log.Fields{"device-id": OnuDeviceDB.deviceID})
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +000085 OnuDeviceDB.MeDb[meClassID] = make(map[uint16]me.AttributeValueMap)
86 OnuDeviceDB.MeDb[meClassID][meEntityID] = make(me.AttributeValueMap)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000087 OnuDeviceDB.MeDb[meClassID][meEntityID] = meAttributes
Holger Hildebrandt24d51952020-05-04 14:03:42 +000088 } else {
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +000089 meAttribs, ok := OnuDeviceDB.MeDb[meClassID][meEntityID]
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000090 if !ok {
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +000091 OnuDeviceDB.MeDb[meClassID][meEntityID] = make(me.AttributeValueMap)
92 OnuDeviceDB.MeDb[meClassID][meEntityID] = meAttributes
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000093 } else {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000094 for k, v := range meAttributes {
95 meAttribs[k] = v
96 }
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +000097 OnuDeviceDB.MeDb[meClassID][meEntityID] = meAttribs
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +000098 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +000099 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000100}
101
praneeth kumar nalmas3947c582023-12-13 15:38:50 +0530102// GetMe returns an ME instance from internal ONU DB
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000103func (OnuDeviceDB *OnuDeviceDB) GetMe(meClassID me.ClassID, meEntityID uint16) me.AttributeValueMap {
104 OnuDeviceDB.meDbLock.RLock()
105 defer OnuDeviceDB.meDbLock.RUnlock()
106 if meAttributes, present := OnuDeviceDB.MeDb[meClassID][meEntityID]; present {
mpagenkodff5dda2020-08-28 11:52:01 +0000107 /* verbose logging, avoid in >= debug level
dbainbri4d3a0dc2020-12-02 00:33:42 +0000108 logger.Debugw(ctx,"ME found:", log.Fields{"meClassID": meClassID, "meEntityID": meEntityID, "meAttributes": meAttributes,
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000109 "device-id": OnuDeviceDB.deviceID})
mpagenkodff5dda2020-08-28 11:52:01 +0000110 */
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000111 return meAttributes
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000112 }
Himani Chawla4d908332020-08-31 12:30:20 +0530113 return nil
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000114}
115
praneeth kumar nalmas3947c582023-12-13 15:38:50 +0530116// GetUint32Attrib converts JSON numbers to uint32
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000117func (OnuDeviceDB *OnuDeviceDB) GetUint32Attrib(meAttribute interface{}) (uint32, error) {
Holger Hildebrandt2ff21f12020-08-13 14:38:02 +0000118
119 switch reflect.TypeOf(meAttribute).Kind() {
120 case reflect.Float64:
121 //JSON numbers by default are unmarshaled into values of float64 type if type information is not present
122 return uint32(meAttribute.(float64)), nil
123 case reflect.Uint32:
Girish Gowdra09e5f212021-09-30 16:28:36 -0700124 return meAttribute.(uint32), nil
Holger Hildebrandt2ff21f12020-08-13 14:38:02 +0000125 default:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000126 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 +0000127 }
128}
129
praneeth kumar nalmas3947c582023-12-13 15:38:50 +0530130// GetUint16Attrib converts JSON numbers to uint16
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000131func (OnuDeviceDB *OnuDeviceDB) GetUint16Attrib(meAttribute interface{}) (uint16, error) {
Girish Gowdra041dcb32020-11-16 16:54:30 -0800132
133 switch reflect.TypeOf(meAttribute).Kind() {
Girish Gowdra09e5f212021-09-30 16:28:36 -0700134 case reflect.Float64:
135 //JSON numbers by default are unmarshaled into values of float64 type if type information is not present
136 return uint16(meAttribute.(float64)), nil
Girish Gowdra041dcb32020-11-16 16:54:30 -0800137 case reflect.Uint16:
138 return meAttribute.(uint16), nil
139 default:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000140 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 -0800141 }
142}
Girish Gowdra041dcb32020-11-16 16:54:30 -0800143
praneeth kumar nalmas3947c582023-12-13 15:38:50 +0530144// GetSortedInstKeys returns a sorted list of all instances of an ME
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000145func (OnuDeviceDB *OnuDeviceDB) GetSortedInstKeys(ctx context.Context, meClassID me.ClassID) []uint16 {
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000146
147 var meInstKeys []uint16
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000148 OnuDeviceDB.meDbLock.RLock()
149 defer OnuDeviceDB.meDbLock.RUnlock()
150 meInstMap := OnuDeviceDB.MeDb[meClassID]
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000151
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000152 for k := range meInstMap {
153 meInstKeys = append(meInstKeys, k)
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000154 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000155 logger.Debugw(ctx, "meInstKeys - input order :", log.Fields{"meInstKeys": meInstKeys}) //TODO: delete the line after test phase!
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000156 sort.Slice(meInstKeys, func(i, j int) bool { return meInstKeys[i] < meInstKeys[j] })
dbainbri4d3a0dc2020-12-02 00:33:42 +0000157 logger.Debugw(ctx, "meInstKeys - output order :", log.Fields{"meInstKeys": meInstKeys}) //TODO: delete the line after test phase!
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000158 return meInstKeys
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000159}
160
praneeth kumar nalmas3947c582023-12-13 15:38:50 +0530161// GetNumberOfInst returns the number of instances of an ME
Holger Hildebrandt5ba6c132022-10-06 13:53:14 +0000162func (OnuDeviceDB *OnuDeviceDB) GetNumberOfInst(meClassID me.ClassID) int {
163 OnuDeviceDB.meDbLock.RLock()
164 defer OnuDeviceDB.meDbLock.RUnlock()
165 return len(OnuDeviceDB.MeDb[meClassID])
166}
167
praneeth kumar nalmas3947c582023-12-13 15:38:50 +0530168// LogMeDb logs content of internal ONU DB
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000169func (OnuDeviceDB *OnuDeviceDB) LogMeDb(ctx context.Context) {
170 logger.Debugw(ctx, "ME instances stored for :", log.Fields{"device-id": OnuDeviceDB.deviceID})
171 for meClassID, meInstMap := range OnuDeviceDB.MeDb {
Himani Chawla4d908332020-08-31 12:30:20 +0530172 for meEntityID, meAttribs := range meInstMap {
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +0000173 logger.Debugw(ctx, "ME instance: ", log.Fields{"meClassID": meClassID, "meEntityID": meEntityID, "meAttribs": meAttribs,
174 "device-id": OnuDeviceDB.deviceID})
175 }
176 }
177}
178
praneeth kumar nalmas3947c582023-12-13 15:38:50 +0530179// PutUnknownMeOrAttrib puts an instance with unknown ME or attributes into internal ONU DB
Holger Hildebrandt39080022022-03-16 10:03:17 +0000180func (OnuDeviceDB *OnuDeviceDB) PutUnknownMeOrAttrib(ctx context.Context, aMeName UnknownMeOrAttribName, aMeClassID me.ClassID, aMeEntityID uint16,
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +0000181 aMeAttributeMask uint16, aMePayload []byte) {
182
Holger Hildebrandt39080022022-03-16 10:03:17 +0000183 meAttribMaskStr := fmt.Sprintf("0x%04x", aMeAttributeMask)
184 attribs := unknownAttribs{meAttribMaskStr, hex.EncodeToString(aMePayload)}
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +0000185
Holger Hildebrandt39080022022-03-16 10:03:17 +0000186 _, ok := OnuDeviceDB.UnknownMeAndAttribDb[aMeName]
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +0000187 if !ok {
Holger Hildebrandt39080022022-03-16 10:03:17 +0000188 OnuDeviceDB.UnknownMeAndAttribDb[aMeName] = make(map[me.ClassID]map[uint16]unknownAttribs)
189 OnuDeviceDB.UnknownMeAndAttribDb[aMeName][aMeClassID] = make(map[uint16]unknownAttribs)
190 OnuDeviceDB.UnknownMeAndAttribDb[aMeName][aMeClassID][aMeEntityID] = attribs
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +0000191 } else {
Holger Hildebrandt39080022022-03-16 10:03:17 +0000192 _, ok := OnuDeviceDB.UnknownMeAndAttribDb[aMeName][aMeClassID]
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +0000193 if !ok {
Holger Hildebrandt39080022022-03-16 10:03:17 +0000194 OnuDeviceDB.UnknownMeAndAttribDb[aMeName][aMeClassID] = make(map[uint16]unknownAttribs)
195 OnuDeviceDB.UnknownMeAndAttribDb[aMeName][aMeClassID][aMeEntityID] = attribs
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +0000196 } else {
Holger Hildebrandt39080022022-03-16 10:03:17 +0000197 _, ok := OnuDeviceDB.UnknownMeAndAttribDb[aMeName][aMeClassID][aMeEntityID]
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +0000198 if !ok {
Holger Hildebrandt39080022022-03-16 10:03:17 +0000199 OnuDeviceDB.UnknownMeAndAttribDb[aMeName][aMeClassID][aMeEntityID] = attribs
Holger Hildebrandtf3c7a182021-11-17 13:42:08 +0000200 }
Holger Hildebrandt9ac0d0f2020-05-13 11:22:02 +0000201 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000202 }
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000203}
praneeth kumar nalmas3947c582023-12-13 15:38:50 +0530204
205// DeleteMe deletes an ME instance from internal ONU DB
206func (OnuDeviceDB *OnuDeviceDB) DeleteMe(meClassID me.ClassID, meEntityID uint16) {
207 OnuDeviceDB.meDbLock.Lock()
208 defer OnuDeviceDB.meDbLock.Unlock()
209 delete(OnuDeviceDB.MeDb[meClassID], meEntityID)
210}