blob: ca954d62ec5ea264b98ec9d1ccd0985557abd12a [file] [log] [blame]
Takahiro Suzuki241c10e2020-12-17 20:17:57 +09001/*
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"
22 "fmt"
23 "reflect"
24 "sort"
25
26 me "github.com/opencord/omci-lib-go/generated"
27 "github.com/opencord/voltha-lib-go/v3/pkg/log"
28)
29
30type meDbMap map[me.ClassID]map[uint16]me.AttributeValueMap
31
32//onuDeviceDB structure holds information about known ME's
33type onuDeviceDB struct {
34 ctx context.Context
35 pOnuDeviceEntry *OnuDeviceEntry
36 meDb meDbMap
37}
38
39//newOnuDeviceDB returns a new instance for a specific ONU_Device_Entry
40func newOnuDeviceDB(ctx context.Context, aPOnuDeviceEntry *OnuDeviceEntry) *onuDeviceDB {
41 logger.Debugw("Init OnuDeviceDB for:", log.Fields{"device-id": aPOnuDeviceEntry.deviceID})
42 var onuDeviceDB onuDeviceDB
43 onuDeviceDB.ctx = ctx
44 onuDeviceDB.pOnuDeviceEntry = aPOnuDeviceEntry
45 onuDeviceDB.meDb = make(meDbMap)
46
47 return &onuDeviceDB
48}
49
50func (onuDeviceDB *onuDeviceDB) PutMe(meClassID me.ClassID, meEntityID uint16, meAttributes me.AttributeValueMap) {
51
52 if me.OnuDataClassID == meClassID {
53 return
54 }
55
56 meInstMap, ok := onuDeviceDB.meDb[meClassID]
57 if !ok {
58 logger.Debugw("meClassID not found - add to db :", log.Fields{"device-id": onuDeviceDB.pOnuDeviceEntry.deviceID})
59 meInstMap = make(map[uint16]me.AttributeValueMap)
60 onuDeviceDB.meDb[meClassID] = meInstMap
61 onuDeviceDB.meDb[meClassID][meEntityID] = meAttributes
62 } else {
63 meAttribs, ok := meInstMap[meEntityID]
64 if !ok {
65 /* verbose logging, avoid in >= debug level
66 logger.Debugw("meEntityId not found - add to db :", log.Fields{"device-id": onuDeviceDB.pOnuDeviceEntry.deviceID})
67 */
68 meInstMap[meEntityID] = meAttributes
69 } else {
70 /* verbose logging, avoid in >= debug level
71 logger.Debugw("ME-Instance exists already: merge attribute data :", log.Fields{"device-id": onuDeviceDB.pOnuDeviceEntry.deviceID, "meAttribs": meAttribs})
72 */
73 for k, v := range meAttributes {
74 meAttribs[k] = v
75 }
76 meInstMap[meEntityID] = meAttribs
77 /* verbose logging, avoid in >= debug level
78 logger.Debugw("ME-Instance updated :", log.Fields{"device-id": onuDeviceDB.pOnuDeviceEntry.deviceID, "meAttribs": meAttribs})
79 */
80 }
81 }
82}
83
84func (onuDeviceDB *onuDeviceDB) GetMe(meClassID me.ClassID, meEntityID uint16) me.AttributeValueMap {
85
86 if meAttributes, present := onuDeviceDB.meDb[meClassID][meEntityID]; present {
87 /* verbose logging, avoid in >= debug level
88 logger.Debugw("ME found:", log.Fields{"meClassID": meClassID, "meEntityID": meEntityID, "meAttributes": meAttributes,
89 "device-id": onuDeviceDB.pOnuDeviceEntry.deviceID})
90 */
91 return meAttributes
92 }
93 return nil
94}
95
96func (onuDeviceDB *onuDeviceDB) getUint32Attrib(meAttribute interface{}) (uint32, error) {
97
98 switch reflect.TypeOf(meAttribute).Kind() {
99 case reflect.Float64:
100 //JSON numbers by default are unmarshaled into values of float64 type if type information is not present
101 return uint32(meAttribute.(float64)), nil
102 case reflect.Uint32:
103 return uint32(meAttribute.(uint32)), nil
104 default:
105 return uint32(0), fmt.Errorf(fmt.Sprintf("wrong interface-type received-%s", onuDeviceDB.pOnuDeviceEntry.deviceID))
106 }
107}
108
109func (onuDeviceDB *onuDeviceDB) getSortedInstKeys(meClassID me.ClassID) []uint16 {
110
111 var meInstKeys []uint16
112
113 meInstMap := onuDeviceDB.meDb[meClassID]
114
115 for k := range meInstMap {
116 meInstKeys = append(meInstKeys, k)
117 }
118 logger.Debugw("meInstKeys - input order :", log.Fields{"meInstKeys": meInstKeys}) //TODO: delete the line after test phase!
119 sort.Slice(meInstKeys, func(i, j int) bool { return meInstKeys[i] < meInstKeys[j] })
120 logger.Debugw("meInstKeys - output order :", log.Fields{"meInstKeys": meInstKeys}) //TODO: delete the line after test phase!
121 return meInstKeys
122}
123
124func (onuDeviceDB *onuDeviceDB) logMeDb() {
125 logger.Debugw("ME instances stored for :", log.Fields{"device-id": onuDeviceDB.pOnuDeviceEntry.deviceID})
126 for meClassID, meInstMap := range onuDeviceDB.meDb {
127 for meEntityID, meAttribs := range meInstMap {
128 logger.Debugw("ME instance: ", log.Fields{"meClassID": meClassID, "meEntityID": meEntityID, "meAttribs": meAttribs, "device-id": onuDeviceDB.pOnuDeviceEntry.deviceID})
129 }
130 }
131}