Prince Pereira | c1c21d6 | 2021-04-22 08:38:15 +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 hwcomponents stores methods and functions related to hardware |
| 18 | package hwcomponents |
| 19 | |
| 20 | import ( |
| 21 | "context" |
| 22 | "encoding/json" |
| 23 | "errors" |
| 24 | "fmt" |
| 25 | |
| 26 | copy "github.com/jinzhu/copier" |
| 27 | dmi "github.com/opencord/device-management-interface/go/dmi" |
| 28 | |
| 29 | "github.com/opencord/opendevice-manager/pkg/db" |
| 30 | log "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 31 | ) |
| 32 | |
| 33 | // DBAddByName inserts Device Info record to DB with Name as Key |
| 34 | func (rec *HwCompRecord) DBAddByUuid(ctx context.Context, deviceUuid string) error { |
| 35 | if rec.Uuid == "" || deviceUuid == "" { |
| 36 | logger.Errorw(ctx, "DBAddByUuid-failed-missing-uuid", log.Fields{"rec": rec, "dev-uuid": deviceUuid}) |
| 37 | return errors.New("missing uuid") |
| 38 | } |
| 39 | key := fmt.Sprintf(DbPathUuidToRecord, deviceUuid, rec.Uuid) |
| 40 | b, _ := json.Marshal(rec) |
| 41 | entry := string(b) |
| 42 | err := db.Put(ctx, key, entry) |
| 43 | cache.store(deviceUuid, rec) |
| 44 | logger.Infow(ctx, "Inserting-hw-comp-info-to-Db-in-DBAddByUuid-method", log.Fields{"rec": rec, "error": err}) |
| 45 | return err |
| 46 | } |
| 47 | |
| 48 | // DBAddByName inserts Device Info record to DB with Name as Key |
| 49 | func DBAddNameToUuidlookup(ctx context.Context, deviceUuid string, nameToUuidMap map[string]string) error { |
| 50 | if deviceUuid == "" || len(nameToUuidMap) == 0 { |
| 51 | logger.Errorw(ctx, "DBAddNameToUuidlookup-failed-missing-uuid-or-map", log.Fields{"map": nameToUuidMap, "dev-uuid": deviceUuid}) |
| 52 | return errors.New("missing info") |
| 53 | } |
| 54 | key := fmt.Sprintf(DbPathNameToUuid, deviceUuid) |
| 55 | b, _ := json.Marshal(nameToUuidMap) |
| 56 | entry := string(b) |
| 57 | err := db.Put(ctx, key, entry) |
| 58 | logger.Infow(ctx, "DBAddNameToUuidlookup-method-complete", log.Fields{"map": nameToUuidMap, "error": err}) |
| 59 | return err |
| 60 | } |
| 61 | |
| 62 | // DBSaveHwCompsFromPhysicalInventory iterates through each children and store hwcomponents in db |
| 63 | func DBSaveHwCompsFromPhysicalInventory(ctx context.Context, deviceUuid string, nameToUuidMap map[string]string, children []*dmi.Component) { |
| 64 | if len(children) == 0 { |
| 65 | return |
| 66 | } |
| 67 | for _, child := range children { |
| 68 | hwRec := new(HwCompRecord) |
| 69 | if err := copy.Copy(&hwRec, &child); hwRec.Name == "" { |
| 70 | logger.Errorw(ctx, "Failed-at-copying-hw-comp-from-inventory-list", log.Fields{"error": err, "child": child, "hw-comp": hwRec}) |
| 71 | continue |
| 72 | } |
| 73 | if child.Uri != nil { |
| 74 | hwRec.Uri = child.Uri.Uri |
| 75 | } |
| 76 | if child.Uuid != nil { |
| 77 | hwRec.Uuid = child.Uuid.Uuid |
| 78 | } |
| 79 | for _, grandChild := range child.Children { |
| 80 | hwRec.Children = append(hwRec.Children, grandChild.Uuid.Uuid) |
| 81 | } |
| 82 | hwRec.DBAddByUuid(ctx, deviceUuid) |
| 83 | nameToUuidMap[hwRec.Name] = hwRec.Uuid |
| 84 | DBSaveHwCompsFromPhysicalInventory(ctx, deviceUuid, nameToUuidMap, child.Children) |
| 85 | logger.Infow(ctx, "Successful-at-creating-object-for-new-hw-info", log.Fields{"new-object": child}) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // DBDelRecord deletes all entries for Device Info |
| 90 | func DBDelAllHwComponents(ctx context.Context, deviceUuid string) error { |
| 91 | |
| 92 | var err error |
| 93 | |
| 94 | if deviceUuid == "" { |
| 95 | logger.Errorw(ctx, "deleting-all-hw-components-failed", log.Fields{"reason": "missing-device-uuid"}) |
| 96 | return errors.New("missing device uuid") |
| 97 | } |
| 98 | |
| 99 | key := fmt.Sprintf(DbPrefix, deviceUuid) |
| 100 | err = db.DelAll(ctx, key) |
| 101 | cache.delDevice(deviceUuid) |
| 102 | logger.Infow(ctx, "deleting-all-hw-components-completed", log.Fields{"key": key}) |
| 103 | |
| 104 | return err |
| 105 | } |
| 106 | |
| 107 | // DBGetRecByUuid func reads hw comp record by uuid |
| 108 | func DBGetRecByUuid(ctx context.Context, deviceUuid, hwCompUuid string) (*HwCompRecord, error) { |
| 109 | if deviceUuid == "" || hwCompUuid == "" { |
| 110 | logger.Errorw(ctx, "DBGetHwCompRec-failed-missing-uuid", log.Fields{"device-uuid": deviceUuid, "hw-comp-uuid": hwCompUuid}) |
| 111 | return nil, errors.New("uuid field is empty") |
| 112 | } |
| 113 | |
| 114 | if rec := cache.get(deviceUuid, hwCompUuid); rec != nil { |
| 115 | return rec, nil |
| 116 | } |
| 117 | |
| 118 | key := fmt.Sprintf(DbPathUuidToRecord, deviceUuid, hwCompUuid) |
| 119 | entry, err := db.Read(ctx, key) |
| 120 | if err != nil { |
| 121 | logger.Errorw(ctx, "DBGetRecByUuid-failed-read-db", log.Fields{"error": err, "key": key}) |
| 122 | return nil, err |
| 123 | } |
| 124 | |
| 125 | rec := new(HwCompRecord) |
| 126 | if err = json.Unmarshal([]byte(entry), rec); err != nil { |
| 127 | logger.Errorw(ctx, "Failed-to-unmarshal-at-DBGetRecByUuid", log.Fields{"reason": err, "entry": entry}) |
| 128 | return nil, err |
| 129 | } |
| 130 | |
| 131 | cache.store(deviceUuid, rec) |
| 132 | |
| 133 | logger.Debugw(ctx, "DBGetHwCompRec-completed", log.Fields{"device-uuid": deviceUuid, "hw-comp-uuid": hwCompUuid, "rec": rec}) |
| 134 | return rec, nil |
| 135 | } |
| 136 | |
| 137 | // DBGetRecByName func reads hw comp record by name |
| 138 | func DBGetRecByName(ctx context.Context, deviceUuid, hwName string) (*HwCompRecord, error) { |
| 139 | if deviceUuid == "" || hwName == "" { |
| 140 | logger.Errorw(ctx, "DBGetRecByName-failed-missing-uuid", log.Fields{"device-uuid": deviceUuid, "hw-comp-name": hwName}) |
| 141 | return nil, errors.New("name field is empty") |
| 142 | } |
| 143 | key := fmt.Sprintf(DbPathNameToUuid, deviceUuid) |
| 144 | entry, err := db.Read(ctx, key) |
| 145 | if err != nil { |
| 146 | logger.Errorw(ctx, "DBGetRecByName-failed-read-db", log.Fields{"error": err, "key": key}) |
| 147 | return nil, err |
| 148 | } |
| 149 | |
| 150 | nameToUuidMap := make(map[string]string) |
| 151 | if err = json.Unmarshal([]byte(entry), &nameToUuidMap); nameToUuidMap == nil || err != nil { |
| 152 | logger.Errorw(ctx, "Failed-to-unmarshal-at-DBGetRecByName", log.Fields{"reason": err, "entry": entry}) |
| 153 | return nil, err |
| 154 | } |
| 155 | |
| 156 | if hwUuid, ok := nameToUuidMap[hwName]; ok { |
| 157 | rec, err2 := DBGetRecByUuid(ctx, deviceUuid, hwUuid) |
| 158 | logger.Debugw(ctx, "DBGetRecByName-completed", log.Fields{"device-uuid": deviceUuid, "hw-comp-name": hwName, "rec": rec, "error": err2}) |
| 159 | } |
| 160 | |
| 161 | return nil, errors.New("name not found") |
| 162 | } |