Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 1 | /* |
| 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 Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 17 | //Package devdb provides access to internal device ME DB |
| 18 | package devdb |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 19 | |
| 20 | import ( |
| 21 | "context" |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 22 | "encoding/hex" |
Holger Hildebrandt | 2ff21f1 | 2020-08-13 14:38:02 +0000 | [diff] [blame] | 23 | "fmt" |
| 24 | "reflect" |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 25 | "sort" |
Mahir Gunyel | 7f4483a | 2021-05-06 12:53:43 -0700 | [diff] [blame] | 26 | "sync" |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 27 | |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 28 | me "github.com/opencord/omci-lib-go/v2/generated" |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 29 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 30 | ) |
| 31 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 32 | type meDbMap map[me.ClassID]map[uint16]me.AttributeValueMap |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 33 | |
Holger Hildebrandt | 3908002 | 2022-03-16 10:03:17 +0000 | [diff] [blame] | 34 | // UnknownMeOrAttribName type to be used for unknown ME and attribute names |
| 35 | type UnknownMeOrAttribName string |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 36 | |
Holger Hildebrandt | 3908002 | 2022-03-16 10:03:17 +0000 | [diff] [blame] | 37 | // names for unknown ME and attribute identifiers |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 38 | const ( |
| 39 | CUnknownItuG988ManagedEntity = "UnknownItuG988ManagedEntity" |
| 40 | CUnknownVendorSpecificManagedEntity = "UnknownVendorSpecificManagedEntity" |
Holger Hildebrandt | 3908002 | 2022-03-16 10:03:17 +0000 | [diff] [blame] | 41 | CUnknownAttributesManagedEntity = "UnknownAttributesManagedEntity" |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 42 | ) |
| 43 | |
Holger Hildebrandt | 3908002 | 2022-03-16 10:03:17 +0000 | [diff] [blame] | 44 | // CStartUnknownMeAttribsInBaseLayerPayload defines start of unknown ME attribs after ClassID, InstanceID and AttribMask |
| 45 | const CStartUnknownMeAttribsInBaseLayerPayload = 6 |
| 46 | |
| 47 | type unknownAttribs struct { |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 48 | AttribMask string `json:"AttributeMask"` |
| 49 | AttribBytes string `json:"AttributeBytes"` |
| 50 | } |
Holger Hildebrandt | 3908002 | 2022-03-16 10:03:17 +0000 | [diff] [blame] | 51 | type unknownMeAndAttribDbMap map[UnknownMeOrAttribName]map[me.ClassID]map[uint16]unknownAttribs |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 52 | |
| 53 | //OnuDeviceDB structure holds information about ME's |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 54 | type OnuDeviceDB struct { |
Holger Hildebrandt | 3908002 | 2022-03-16 10:03:17 +0000 | [diff] [blame] | 55 | ctx context.Context |
| 56 | deviceID string |
| 57 | MeDb meDbMap |
| 58 | meDbLock sync.RWMutex |
| 59 | UnknownMeAndAttribDb unknownMeAndAttribDbMap |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 62 | //NewOnuDeviceDB returns a new instance for a specific ONU_Device_Entry |
| 63 | func 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 Hildebrandt | 3908002 | 2022-03-16 10:03:17 +0000 | [diff] [blame] | 69 | OnuDeviceDB.UnknownMeAndAttribDb = make(unknownMeAndAttribDbMap) |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 70 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 71 | return &OnuDeviceDB |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 74 | //PutMe puts an ME instance into internal ONU DB |
| 75 | func (OnuDeviceDB *OnuDeviceDB) PutMe(ctx context.Context, meClassID me.ClassID, meEntityID uint16, meAttributes me.AttributeValueMap) { |
| 76 | OnuDeviceDB.meDbLock.Lock() |
| 77 | defer OnuDeviceDB.meDbLock.Unlock() |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 78 | //filter out the OnuData |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 79 | if me.OnuDataClassID == meClassID { |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 80 | return |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 81 | } |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 82 | _, ok := OnuDeviceDB.MeDb[meClassID] |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 83 | if !ok { |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 84 | logger.Debugw(ctx, "meClassID not found - add to db :", log.Fields{"device-id": OnuDeviceDB.deviceID}) |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 85 | OnuDeviceDB.MeDb[meClassID] = make(map[uint16]me.AttributeValueMap) |
| 86 | OnuDeviceDB.MeDb[meClassID][meEntityID] = make(me.AttributeValueMap) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 87 | OnuDeviceDB.MeDb[meClassID][meEntityID] = meAttributes |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 88 | } else { |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 89 | meAttribs, ok := OnuDeviceDB.MeDb[meClassID][meEntityID] |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 90 | if !ok { |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 91 | OnuDeviceDB.MeDb[meClassID][meEntityID] = make(me.AttributeValueMap) |
| 92 | OnuDeviceDB.MeDb[meClassID][meEntityID] = meAttributes |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 93 | } else { |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 94 | for k, v := range meAttributes { |
| 95 | meAttribs[k] = v |
| 96 | } |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 97 | OnuDeviceDB.MeDb[meClassID][meEntityID] = meAttribs |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 98 | } |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 99 | } |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 102 | //GetMe returns an ME instance from internal ONU DB |
| 103 | func (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 { |
mpagenko | dff5dda | 2020-08-28 11:52:01 +0000 | [diff] [blame] | 107 | /* verbose logging, avoid in >= debug level |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 108 | logger.Debugw(ctx,"ME found:", log.Fields{"meClassID": meClassID, "meEntityID": meEntityID, "meAttributes": meAttributes, |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 109 | "device-id": OnuDeviceDB.deviceID}) |
mpagenko | dff5dda | 2020-08-28 11:52:01 +0000 | [diff] [blame] | 110 | */ |
Holger Hildebrandt | 9ca8b13 | 2020-08-07 14:45:03 +0000 | [diff] [blame] | 111 | return meAttributes |
Holger Hildebrandt | 9ca8b13 | 2020-08-07 14:45:03 +0000 | [diff] [blame] | 112 | } |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 113 | return nil |
Holger Hildebrandt | 9ca8b13 | 2020-08-07 14:45:03 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 116 | //GetUint32Attrib converts JSON numbers to uint32 |
| 117 | func (OnuDeviceDB *OnuDeviceDB) GetUint32Attrib(meAttribute interface{}) (uint32, error) { |
Holger Hildebrandt | 2ff21f1 | 2020-08-13 14:38:02 +0000 | [diff] [blame] | 118 | |
| 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 Gowdra | 09e5f21 | 2021-09-30 16:28:36 -0700 | [diff] [blame] | 124 | return meAttribute.(uint32), nil |
Holger Hildebrandt | 2ff21f1 | 2020-08-13 14:38:02 +0000 | [diff] [blame] | 125 | default: |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 126 | return uint32(0), fmt.Errorf(fmt.Sprintf("wrong-interface-type-%v-received-for-device-%s", reflect.TypeOf(meAttribute).Kind(), OnuDeviceDB.deviceID)) |
Holger Hildebrandt | 2ff21f1 | 2020-08-13 14:38:02 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 130 | //GetUint16Attrib converts JSON numbers to uint16 |
| 131 | func (OnuDeviceDB *OnuDeviceDB) GetUint16Attrib(meAttribute interface{}) (uint16, error) { |
Girish Gowdra | 041dcb3 | 2020-11-16 16:54:30 -0800 | [diff] [blame] | 132 | |
| 133 | switch reflect.TypeOf(meAttribute).Kind() { |
Girish Gowdra | 09e5f21 | 2021-09-30 16:28:36 -0700 | [diff] [blame] | 134 | 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 Gowdra | 041dcb3 | 2020-11-16 16:54:30 -0800 | [diff] [blame] | 137 | case reflect.Uint16: |
| 138 | return meAttribute.(uint16), nil |
| 139 | default: |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 140 | return uint16(0), fmt.Errorf(fmt.Sprintf("wrong-interface-type-%v-received-for-device-%s", reflect.TypeOf(meAttribute).Kind(), OnuDeviceDB.deviceID)) |
Girish Gowdra | 041dcb3 | 2020-11-16 16:54:30 -0800 | [diff] [blame] | 141 | } |
| 142 | } |
Girish Gowdra | 041dcb3 | 2020-11-16 16:54:30 -0800 | [diff] [blame] | 143 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 144 | //GetSortedInstKeys returns a sorted list of all instances of an ME |
| 145 | func (OnuDeviceDB *OnuDeviceDB) GetSortedInstKeys(ctx context.Context, meClassID me.ClassID) []uint16 { |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 146 | |
| 147 | var meInstKeys []uint16 |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 148 | OnuDeviceDB.meDbLock.RLock() |
| 149 | defer OnuDeviceDB.meDbLock.RUnlock() |
| 150 | meInstMap := OnuDeviceDB.MeDb[meClassID] |
Holger Hildebrandt | 9ca8b13 | 2020-08-07 14:45:03 +0000 | [diff] [blame] | 151 | |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 152 | for k := range meInstMap { |
| 153 | meInstKeys = append(meInstKeys, k) |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 154 | } |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 155 | logger.Debugw(ctx, "meInstKeys - input order :", log.Fields{"meInstKeys": meInstKeys}) //TODO: delete the line after test phase! |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 156 | sort.Slice(meInstKeys, func(i, j int) bool { return meInstKeys[i] < meInstKeys[j] }) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 157 | logger.Debugw(ctx, "meInstKeys - output order :", log.Fields{"meInstKeys": meInstKeys}) //TODO: delete the line after test phase! |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 158 | return meInstKeys |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Holger Hildebrandt | 5ba6c13 | 2022-10-06 13:53:14 +0000 | [diff] [blame] | 161 | //GetNumberOfInst returns the number of instances of an ME |
| 162 | func (OnuDeviceDB *OnuDeviceDB) GetNumberOfInst(meClassID me.ClassID) int { |
| 163 | OnuDeviceDB.meDbLock.RLock() |
| 164 | defer OnuDeviceDB.meDbLock.RUnlock() |
| 165 | return len(OnuDeviceDB.MeDb[meClassID]) |
| 166 | } |
| 167 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 168 | //LogMeDb logs content of internal ONU DB |
| 169 | func (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 Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 172 | for meEntityID, meAttribs := range meInstMap { |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 173 | logger.Debugw(ctx, "ME instance: ", log.Fields{"meClassID": meClassID, "meEntityID": meEntityID, "meAttribs": meAttribs, |
| 174 | "device-id": OnuDeviceDB.deviceID}) |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
Holger Hildebrandt | 3908002 | 2022-03-16 10:03:17 +0000 | [diff] [blame] | 179 | //PutUnknownMeOrAttrib puts an instance with unknown ME or attributes into internal ONU DB |
| 180 | func (OnuDeviceDB *OnuDeviceDB) PutUnknownMeOrAttrib(ctx context.Context, aMeName UnknownMeOrAttribName, aMeClassID me.ClassID, aMeEntityID uint16, |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 181 | aMeAttributeMask uint16, aMePayload []byte) { |
| 182 | |
Holger Hildebrandt | 3908002 | 2022-03-16 10:03:17 +0000 | [diff] [blame] | 183 | meAttribMaskStr := fmt.Sprintf("0x%04x", aMeAttributeMask) |
| 184 | attribs := unknownAttribs{meAttribMaskStr, hex.EncodeToString(aMePayload)} |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 185 | |
Holger Hildebrandt | 3908002 | 2022-03-16 10:03:17 +0000 | [diff] [blame] | 186 | _, ok := OnuDeviceDB.UnknownMeAndAttribDb[aMeName] |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 187 | if !ok { |
Holger Hildebrandt | 3908002 | 2022-03-16 10:03:17 +0000 | [diff] [blame] | 188 | 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 Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 191 | } else { |
Holger Hildebrandt | 3908002 | 2022-03-16 10:03:17 +0000 | [diff] [blame] | 192 | _, ok := OnuDeviceDB.UnknownMeAndAttribDb[aMeName][aMeClassID] |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 193 | if !ok { |
Holger Hildebrandt | 3908002 | 2022-03-16 10:03:17 +0000 | [diff] [blame] | 194 | OnuDeviceDB.UnknownMeAndAttribDb[aMeName][aMeClassID] = make(map[uint16]unknownAttribs) |
| 195 | OnuDeviceDB.UnknownMeAndAttribDb[aMeName][aMeClassID][aMeEntityID] = attribs |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 196 | } else { |
Holger Hildebrandt | 3908002 | 2022-03-16 10:03:17 +0000 | [diff] [blame] | 197 | _, ok := OnuDeviceDB.UnknownMeAndAttribDb[aMeName][aMeClassID][aMeEntityID] |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 198 | if !ok { |
Holger Hildebrandt | 3908002 | 2022-03-16 10:03:17 +0000 | [diff] [blame] | 199 | OnuDeviceDB.UnknownMeAndAttribDb[aMeName][aMeClassID][aMeEntityID] = attribs |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 200 | } |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 201 | } |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 202 | } |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 203 | } |