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