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" |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 26 | "strconv" |
Mahir Gunyel | 7f4483a | 2021-05-06 12:53:43 -0700 | [diff] [blame] | 27 | "sync" |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 28 | |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 29 | me "github.com/opencord/omci-lib-go/v2/generated" |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 30 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 31 | ) |
| 32 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 33 | type meDbMap map[me.ClassID]map[uint16]me.AttributeValueMap |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 34 | |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 35 | // UnknownMeName type to be used for unknown ME names |
| 36 | type UnknownMeName string |
| 37 | |
| 38 | // allowed values for type UnknownMeName |
| 39 | const ( |
| 40 | CUnknownItuG988ManagedEntity = "UnknownItuG988ManagedEntity" |
| 41 | CUnknownVendorSpecificManagedEntity = "UnknownVendorSpecificManagedEntity" |
| 42 | ) |
| 43 | |
| 44 | type unknownMeAttribs struct { |
| 45 | AttribMask string `json:"AttributeMask"` |
| 46 | AttribBytes string `json:"AttributeBytes"` |
| 47 | } |
| 48 | type unknownMeDbMap map[UnknownMeName]map[me.ClassID]map[uint16]unknownMeAttribs |
| 49 | |
| 50 | //OnuDeviceDB structure holds information about ME's |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 51 | type OnuDeviceDB struct { |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 52 | ctx context.Context |
| 53 | deviceID string |
| 54 | MeDb meDbMap |
| 55 | meDbLock sync.RWMutex |
| 56 | UnknownMeDb unknownMeDbMap |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 59 | //NewOnuDeviceDB returns a new instance for a specific ONU_Device_Entry |
| 60 | func 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 Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 66 | OnuDeviceDB.UnknownMeDb = make(unknownMeDbMap) |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 67 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 68 | return &OnuDeviceDB |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 71 | //PutMe puts an ME instance into internal ONU DB |
| 72 | func (OnuDeviceDB *OnuDeviceDB) PutMe(ctx context.Context, meClassID me.ClassID, meEntityID uint16, meAttributes me.AttributeValueMap) { |
| 73 | OnuDeviceDB.meDbLock.Lock() |
| 74 | defer OnuDeviceDB.meDbLock.Unlock() |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 75 | //filter out the OnuData |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 76 | if me.OnuDataClassID == meClassID { |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 77 | return |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 78 | } |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 79 | _, ok := OnuDeviceDB.MeDb[meClassID] |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 80 | if !ok { |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 81 | 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] | 82 | OnuDeviceDB.MeDb[meClassID] = make(map[uint16]me.AttributeValueMap) |
| 83 | OnuDeviceDB.MeDb[meClassID][meEntityID] = make(me.AttributeValueMap) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 84 | OnuDeviceDB.MeDb[meClassID][meEntityID] = meAttributes |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 85 | } else { |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 86 | meAttribs, ok := OnuDeviceDB.MeDb[meClassID][meEntityID] |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 87 | if !ok { |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 88 | OnuDeviceDB.MeDb[meClassID][meEntityID] = make(me.AttributeValueMap) |
| 89 | OnuDeviceDB.MeDb[meClassID][meEntityID] = meAttributes |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 90 | } else { |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 91 | for k, v := range meAttributes { |
| 92 | meAttribs[k] = v |
| 93 | } |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 94 | OnuDeviceDB.MeDb[meClassID][meEntityID] = meAttribs |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 95 | } |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 96 | } |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 99 | //GetMe returns an ME instance from internal ONU DB |
| 100 | func (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 { |
mpagenko | dff5dda | 2020-08-28 11:52:01 +0000 | [diff] [blame] | 104 | /* verbose logging, avoid in >= debug level |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 105 | 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] | 106 | "device-id": OnuDeviceDB.deviceID}) |
mpagenko | dff5dda | 2020-08-28 11:52:01 +0000 | [diff] [blame] | 107 | */ |
Holger Hildebrandt | 9ca8b13 | 2020-08-07 14:45:03 +0000 | [diff] [blame] | 108 | return meAttributes |
Holger Hildebrandt | 9ca8b13 | 2020-08-07 14:45:03 +0000 | [diff] [blame] | 109 | } |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 110 | return nil |
Holger Hildebrandt | 9ca8b13 | 2020-08-07 14:45:03 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 113 | //GetUint32Attrib converts JSON numbers to uint32 |
| 114 | func (OnuDeviceDB *OnuDeviceDB) GetUint32Attrib(meAttribute interface{}) (uint32, error) { |
Holger Hildebrandt | 2ff21f1 | 2020-08-13 14:38:02 +0000 | [diff] [blame] | 115 | |
| 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 Gowdra | 09e5f21 | 2021-09-30 16:28:36 -0700 | [diff] [blame] | 121 | return meAttribute.(uint32), nil |
Holger Hildebrandt | 2ff21f1 | 2020-08-13 14:38:02 +0000 | [diff] [blame] | 122 | default: |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 123 | 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] | 124 | } |
| 125 | } |
| 126 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 127 | //GetUint16Attrib converts JSON numbers to uint16 |
| 128 | func (OnuDeviceDB *OnuDeviceDB) GetUint16Attrib(meAttribute interface{}) (uint16, error) { |
Girish Gowdra | 041dcb3 | 2020-11-16 16:54:30 -0800 | [diff] [blame] | 129 | |
| 130 | switch reflect.TypeOf(meAttribute).Kind() { |
Girish Gowdra | 09e5f21 | 2021-09-30 16:28:36 -0700 | [diff] [blame] | 131 | 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 Gowdra | 041dcb3 | 2020-11-16 16:54:30 -0800 | [diff] [blame] | 134 | case reflect.Uint16: |
| 135 | return meAttribute.(uint16), nil |
| 136 | default: |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 137 | 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] | 138 | } |
| 139 | } |
Girish Gowdra | 041dcb3 | 2020-11-16 16:54:30 -0800 | [diff] [blame] | 140 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 141 | //GetSortedInstKeys returns a sorted list of all instances of an ME |
| 142 | func (OnuDeviceDB *OnuDeviceDB) GetSortedInstKeys(ctx context.Context, meClassID me.ClassID) []uint16 { |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 143 | |
| 144 | var meInstKeys []uint16 |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 145 | OnuDeviceDB.meDbLock.RLock() |
| 146 | defer OnuDeviceDB.meDbLock.RUnlock() |
| 147 | meInstMap := OnuDeviceDB.MeDb[meClassID] |
Holger Hildebrandt | 9ca8b13 | 2020-08-07 14:45:03 +0000 | [diff] [blame] | 148 | |
Holger Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 149 | for k := range meInstMap { |
| 150 | meInstKeys = append(meInstKeys, k) |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 151 | } |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 152 | 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] | 153 | sort.Slice(meInstKeys, func(i, j int) bool { return meInstKeys[i] < meInstKeys[j] }) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 154 | 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] | 155 | return meInstKeys |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 158 | //LogMeDb logs content of internal ONU DB |
| 159 | func (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 Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 162 | for meEntityID, meAttribs := range meInstMap { |
Holger Hildebrandt | f3c7a18 | 2021-11-17 13:42:08 +0000 | [diff] [blame] | 163 | 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 |
| 170 | func (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 Hildebrandt | 9ac0d0f | 2020-05-13 11:22:02 +0000 | [diff] [blame] | 191 | } |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 192 | } |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 193 | } |