VOL-4019: Initial commit with grpc nbi, sbi, etcd, kafka and hw management rpcs.
Change-Id: I78feaf7da284028fc61f42c5e0c5f56e72fe9e78
diff --git a/pkg/models/device/db.go b/pkg/models/device/db.go
new file mode 100644
index 0000000..a98213e
--- /dev/null
+++ b/pkg/models/device/db.go
@@ -0,0 +1,191 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Package modifiablecomponent stores ModifiableComponent methods and functions
+package device
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+
+ "github.com/opencord/device-management-interface/go/dmi"
+
+ "github.com/opencord/voltha-lib-go/v4/pkg/log"
+
+ "github.com/opencord/opendevice-manager/pkg/db"
+
+ copy "github.com/jinzhu/copier"
+)
+
+// DBGetByName func reads device record by name
+func DBGetByName(ctx context.Context, name string) (*DeviceRecord, error) {
+ if name == "" {
+ logger.Errorw(ctx, "DBGetByName-failed-missing-device-name", log.Fields{})
+ return nil, errors.New("name field is empty")
+ }
+
+ logger.Debugw(ctx, "DBGetByName-invoked", log.Fields{"name": name})
+ defer logger.Debugw(ctx, "DBGetByName-completed", log.Fields{"name": name})
+
+ if val, ok := cache.nameToRec.Load(name); ok {
+ return val.(*DeviceRecord), nil
+ }
+
+ key := fmt.Sprintf(DbPathNameToRecord, name)
+ entry, err := db.Read(ctx, key)
+ if err != nil {
+ logger.Errorw(ctx, "DBGetByName-failed-read-db", log.Fields{"error": err, "key": key})
+ return nil, err
+ }
+
+ rec := new(DeviceRecord)
+ if err = json.Unmarshal([]byte(entry), rec); err != nil {
+ logger.Errorw(ctx, "Failed-to-unmarshal-at-DBGetByName", log.Fields{"reason": err, "entry": entry})
+ return nil, err
+ }
+
+ cache.nameToRec.Store(name, rec)
+
+ return rec, nil
+}
+
+// DBGetByUuid func reads device record by Uuid
+func DBGetByUuid(ctx context.Context, uuid string) (*DeviceRecord, error) {
+
+ if uuid == "" {
+ logger.Errorw(ctx, "DBGetByUuid-failed-missing-device-uuid", log.Fields{})
+ return nil, errors.New("uuid field is empty")
+ }
+
+ logger.Debugw(ctx, "DBGetByUuid-invoked", log.Fields{"uuid": uuid})
+ defer logger.Debugw(ctx, "DBGetByUuid-completed", log.Fields{"uuid": uuid})
+
+ var name string
+ var err error
+
+ if val, ok := cache.uuidToName.Load(uuid); ok {
+ name = val.(string)
+ } else {
+
+ key := fmt.Sprintf(DbPathUuidToName, uuid)
+ name, err = db.Read(ctx, key)
+ if err != nil {
+ logger.Errorw(ctx, "DBGetByUuid-failed-read-db", log.Fields{"error": err, "key": key})
+ return nil, err
+ }
+ }
+
+ cache.uuidToName.Store(uuid, name)
+
+ return DBGetByName(ctx, name)
+}
+
+// DBGetAll func returns all device records
+func DBGetAll(ctx context.Context) ([]*DeviceRecord, error) {
+ key := fmt.Sprintf(DbPathNameToRecord, "")
+ kvPairs, err := db.ReadAll(ctx, key)
+ if err != nil {
+ logger.Errorw(ctx, "DBGetAll-failed-read-db", log.Fields{"error": err, "key": key})
+ return nil, err
+ }
+
+ var listDevs []*DeviceRecord
+
+ for _, entry := range kvPairs {
+ rec := new(DeviceRecord)
+ if err = json.Unmarshal([]byte(entry), rec); err != nil {
+ logger.Errorw(ctx, "Failed-to-unmarshal-at-DBGetByName", log.Fields{"reason": err, "entry": entry})
+ } else {
+ listDevs = append(listDevs, rec)
+ }
+ }
+
+ logger.Infow(ctx, "DBGetAll-success", log.Fields{"entry": listDevs})
+
+ return listDevs, nil
+}
+
+// DBAddByName inserts Device Info record to DB with Name as Key
+func (rec *DeviceRecord) DBAddByName(ctx context.Context) error {
+ if rec.Name == "" {
+ logger.Errorw(ctx, "DBAddByName-failed-missing-device-name", log.Fields{"rec": rec})
+ return errors.New("missing name")
+ }
+ key := fmt.Sprintf(DbPathNameToRecord, rec.Name)
+ b, _ := json.Marshal(rec)
+ entry := string(b)
+ err := db.Put(ctx, key, entry)
+ cache.nameToRec.Store(rec.Name, rec)
+ logger.Infow(ctx, "Inserting-device-info-to-Db-in-DBAddByName-method", log.Fields{"rec": rec, "error": err})
+ return err
+}
+
+// DBAddUuidLookup creates a lookup of name from uuid
+func (rec *DeviceRecord) DBAddUuidLookup(ctx context.Context) error {
+ if rec.Uuid == "" || rec.Name == "" {
+ logger.Errorw(ctx, "DBAddUuidLookup-failed-missing-device-name-or-uuid", log.Fields{"rec": rec})
+ return errors.New("missing name")
+ }
+ key := fmt.Sprintf(DbPathUuidToName, rec.Uuid)
+ err := db.Put(ctx, key, rec.Name)
+ cache.uuidToName.Store(rec.Uuid, rec.Name)
+ logger.Infow(ctx, "DBAddUuidLookup-success", log.Fields{"rec": rec, "error": err})
+ return err
+}
+
+// DBDelRecord deletes all entries for Device Info
+func (rec *DeviceRecord) DBDelRecord(ctx context.Context) error {
+
+ var err error
+
+ if rec.Name != "" {
+ key := fmt.Sprintf(DbPathNameToRecord, rec.Name)
+ logger.Infow(ctx, "deleting-device-info-record-using-name", log.Fields{"name": rec.Name, "key": key})
+ err = db.Del(ctx, key)
+ cache.nameToRec.Delete(rec.Name)
+ }
+
+ if err == nil && rec.Uuid != "" {
+ key := fmt.Sprintf(DbPathUuidToName, rec.Uuid)
+ logger.Infow(ctx, "deleting-device-info-record-using-uuid", log.Fields{"uuid": rec.Uuid, "key": key})
+ err = db.Del(ctx, key)
+ cache.uuidToName.Delete(rec.Uuid)
+ }
+
+ return err
+}
+
+// DBSaveHwInfo stores hardware copies info from response and stores in db
+func (rec *DeviceRecord) DBSaveHwInfo(ctx context.Context, hw *dmi.Hardware) error {
+ defer logger.Infow(ctx, "saving-hw-info-to-device-record-completed", log.Fields{"rec": rec})
+ rec.LastBooted = hw.LastBooted
+ rec.LastChange = hw.LastChange
+ name := rec.Name
+ uuid := rec.Uuid
+ if err := copy.Copy(&rec, &hw.Root); err != nil {
+ logger.Errorw(ctx, "copy-failed-at-DBSaveHwInfo", log.Fields{"rec": rec, "error": err, "hw": hw})
+ return err
+ }
+ rec.Children = []string{}
+ for _, child := range hw.Root.Children {
+ rec.Children = append(rec.Children, child.Uuid.Uuid)
+ }
+ rec.Name = name
+ rec.Uuid = uuid
+ return rec.DBAddByName(ctx)
+}
diff --git a/pkg/models/device/models.go b/pkg/models/device/models.go
new file mode 100644
index 0000000..ab9611d
--- /dev/null
+++ b/pkg/models/device/models.go
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Package device stores methods and functions related to device
+package device
+
+import (
+ "context"
+ "sync"
+
+ "github.com/opencord/device-management-interface/go/dmi"
+
+ "github.com/jinzhu/copier"
+ "github.com/opencord/opendevice-manager/pkg/config"
+
+ v1 "github.com/opencord/opendevice-manager/pkg/models/device/v1"
+ "github.com/opencord/voltha-lib-go/v4/pkg/log"
+)
+
+// Constants defined are the DB Path meant for storing device info records
+const (
+ DbPathUuidToName = config.DBPrefix + config.CurDBVer + "/DevRec/DevUuid/%s"
+ DbPathNameToRecord = config.DBPrefix + config.CurDBVer + "/DevRec/DevName/%s"
+)
+
+// deviceCache stores device informations in buffer
+type deviceCache struct {
+ nameToRec *sync.Map // nameToRecord maintains cache for mapping from name to main record
+ uuidToName *sync.Map // uuidToName maintains cache for mapping from uuid to name
+}
+
+var cache *deviceCache
+
+// logger represents the log object
+var logger log.CLogger
+
+// initCache initialises device cache
+func initCache() {
+ cache = new(deviceCache)
+ cache.nameToRec = new(sync.Map)
+ cache.uuidToName = new(sync.Map)
+}
+
+// init function for the package
+func init() {
+ logger = config.Initlog()
+ initCache()
+}
+
+// ClearCacheEntry clearsentry from device cache
+func ClearCacheEntry(ctx context.Context, name, uuid string) {
+ if name != "" {
+ logger.Debugw(ctx, "Clearing-name-key-from-device-cache", log.Fields{"name": name})
+ cache.nameToRec.Delete(name)
+ }
+ if uuid != "" {
+ logger.Debugw(ctx, "Clearing-uuid-key-from-device-cache", log.Fields{"uuid": uuid})
+ cache.uuidToName.Delete(name)
+ }
+}
+
+// DeviceRecord refers to the structure defined for storing OLT info
+type DeviceRecord v1.DeviceRecordV1_0
+
+// NewDeviceRecord return record for aliased ModifiableComponent
+func NewDeviceRecord(ctx context.Context, req *dmi.ModifiableComponent) (*DeviceRecord, error) {
+ rec := new(DeviceRecord)
+ err := copier.Copy(&rec, &req)
+ if err != nil {
+ logger.Errorw(ctx, "Failed-at-creating-object-for-new-device-info", log.Fields{"error": err, "req": req})
+ return nil, err
+ }
+ rec.Uri = req.Uri.Uri
+ rec.State = new(dmi.ComponentState)
+ rec.State.AdminState = req.AdminState
+ logger.Infow(ctx, "Successful-at-creating-object-for-new-device-info", log.Fields{"new-object": rec})
+ return rec, nil
+}
diff --git a/pkg/models/device/util.go b/pkg/models/device/util.go
new file mode 100644
index 0000000..8930cab
--- /dev/null
+++ b/pkg/models/device/util.go
@@ -0,0 +1,114 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Package modifiablecomponent stores ModifiableComponent methods and functions
+package device
+
+import (
+ "context"
+
+ "github.com/opencord/device-management-interface/go/dmi"
+
+ "github.com/opencord/voltha-lib-go/v4/pkg/log"
+)
+
+// GetLoggableEntitiesFromDevRec represnets the fetch the log level with entity from device record
+func (rec *DeviceRecord) GetLoggableEntitiesFromDevRec(ctx context.Context, entities []string) ([]*dmi.EntitiesLogLevel, bool) {
+
+ var traceLevel, debugLevel, infoLevel, warnLevel, errorLevel []string
+
+ if len(entities) > 0 {
+ // getLogLevel request for given entities
+ for _, entity := range entities {
+ if logLevel, ok := rec.Logging.LoggableEntities[entity]; ok {
+ switch logLevel {
+ case dmi.LogLevel_TRACE:
+ traceLevel = append(traceLevel, entity)
+ case dmi.LogLevel_DEBUG:
+ debugLevel = append(debugLevel, entity)
+ case dmi.LogLevel_INFO:
+ infoLevel = append(infoLevel, entity)
+ case dmi.LogLevel_WARN:
+ warnLevel = append(warnLevel, entity)
+ case dmi.LogLevel_ERROR:
+ errorLevel = append(errorLevel, entity)
+ }
+ } else {
+ logger.Warnw(ctx, "entity-was-not-found-in-device-record", log.Fields{"device-name": rec.Name, "entity": entity})
+ return nil, false
+ }
+
+ }
+ } else if len(rec.Logging.LoggableEntities) == 0 {
+ // if LoggableEntities length is zero means loglevel is applicable for entire hardware
+ logger.Debug(ctx, "all-entities-have-common-loglevel", log.Fields{"device-name": rec.Name, "log-level": rec.Logging.LogLevel})
+ return []*dmi.EntitiesLogLevel{{LogLevel: rec.Logging.LogLevel}}, true
+ } else {
+ // get globle log level or get loggble entities will invoke here
+ logger.Debug(ctx, "all-entities-have-diffrent-loglevel", log.Fields{"device-name": rec.Name})
+ for entity, logLevel := range rec.Logging.LoggableEntities {
+ switch logLevel {
+ case dmi.LogLevel_TRACE:
+ traceLevel = append(traceLevel, entity)
+ case dmi.LogLevel_DEBUG:
+ debugLevel = append(debugLevel, entity)
+ case dmi.LogLevel_INFO:
+ infoLevel = append(infoLevel, entity)
+ case dmi.LogLevel_WARN:
+ warnLevel = append(warnLevel, entity)
+ case dmi.LogLevel_ERROR:
+ errorLevel = append(errorLevel, entity)
+ }
+ }
+ }
+
+ entitiesLogLevel := []*dmi.EntitiesLogLevel{
+ {LogLevel: dmi.LogLevel_TRACE, Entities: traceLevel},
+ {LogLevel: dmi.LogLevel_DEBUG, Entities: debugLevel},
+ {LogLevel: dmi.LogLevel_INFO, Entities: infoLevel},
+ {LogLevel: dmi.LogLevel_WARN, Entities: warnLevel},
+ {LogLevel: dmi.LogLevel_ERROR, Entities: errorLevel},
+ }
+ logger.Debug(ctx, "entities-with-log-level", log.Fields{"entities": entitiesLogLevel})
+
+ return entitiesLogLevel, true
+}
+
+// SaveLoggableEntities func is is used to save the log level with entity in device record
+func (rec *DeviceRecord) SaveLoggableEntities(ctx context.Context, listEntities []*dmi.EntitiesLogLevel) {
+
+ if rec.Logging.LoggableEntities == nil {
+
+ logger.Debug(ctx, "allocating-memory-for-loggable-entitie", log.Fields{"device-name": rec.Name})
+ rec.Logging.LoggableEntities = make(map[string]dmi.LogLevel)
+ }
+
+ if len(listEntities) == 1 && listEntities[0].Entities == nil {
+
+ logger.Debug(ctx, "set-global-log-level", log.Fields{"device-name": rec.Name})
+ rec.Logging.LogLevel = listEntities[0].LogLevel
+
+ } else {
+
+ logger.Debug(ctx, "setting-entity-log-level", log.Fields{"device-name": rec.Name, "list-of-entities": listEntities})
+ for _, entities := range listEntities {
+ logLevel := entities.LogLevel
+ for _, entity := range entities.Entities {
+ rec.Logging.LoggableEntities[entity] = logLevel
+ }
+ }
+ }
+}
diff --git a/pkg/models/device/v1/models.go b/pkg/models/device/v1/models.go
new file mode 100644
index 0000000..cf0a1ad
--- /dev/null
+++ b/pkg/models/device/v1/models.go
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Package contains version v1 of Device Info
+package v1
+
+import (
+ "github.com/golang/protobuf/ptypes/timestamp"
+ "github.com/opencord/device-management-interface/go/dmi"
+)
+
+type DeviceRecordV1_0 struct {
+ Uuid string `json:"uuid,omitempty"`
+ Name string `json:"name,omitempty"`
+ Make string `json:"make,omitempty"`
+ Class dmi.ComponentType `json:"class,omitempty"`
+ Parent *dmi.Component `json:"parent,omitempty"`
+ ParentRelPos int32 `json:"parent_rel_pos,omitempty"`
+ Alias string `json:"alias,omitempty"`
+ AssetId string `json:"asset_id,omitempty"`
+ Uri string `json:"uri,omitempty"`
+ HardwareRev string `json:"hardware_rev,omitempty"`
+ FirmwareRev string `json:"firmware_rev,omitempty"`
+ SoftwareRev string `json:"software_rev,omitempty"`
+ SerialNum string `json:"serial_num,omitempty"`
+ ModelName string `json:"model_name,omitempty"`
+ MfgName string `json:"mfg_name,omitempty"`
+ MfgDate *timestamp.Timestamp `json:"mfg_date,omitempty"`
+ State *dmi.ComponentState `json:"state,omitempty"`
+ Inventories map[string]string `json:"inventories,omitempty"`
+ Children []string `json:"children,omitempty"` // Children stores uuid of all direct child
+ Logging LoggingInfo `json:"logging,omitempty"`
+ LastChange *timestamp.Timestamp `json:"last_change,omitempty"`
+ LastBooted *timestamp.Timestamp `json:"last_booted,omitempty"` // Timestamp at which the hardware last booted
+}
+
+type LoggingInfo struct {
+ EndPoint string `json:"end_point,omitempty"`
+ Protocol string `json:"protocol,omitempty"`
+ LogLevel dmi.LogLevel `json:"log_level,omitempty"`
+ LoggableEntities map[string]dmi.LogLevel `json:"loggable_entities,omitempty"`
+}
diff --git a/pkg/models/hwcomponents/db.go b/pkg/models/hwcomponents/db.go
new file mode 100644
index 0000000..7d59d3f
--- /dev/null
+++ b/pkg/models/hwcomponents/db.go
@@ -0,0 +1,162 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Package hwcomponents stores methods and functions related to hardware
+package hwcomponents
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+
+ copy "github.com/jinzhu/copier"
+ dmi "github.com/opencord/device-management-interface/go/dmi"
+
+ "github.com/opencord/opendevice-manager/pkg/db"
+ log "github.com/opencord/voltha-lib-go/v4/pkg/log"
+)
+
+// DBAddByName inserts Device Info record to DB with Name as Key
+func (rec *HwCompRecord) DBAddByUuid(ctx context.Context, deviceUuid string) error {
+ if rec.Uuid == "" || deviceUuid == "" {
+ logger.Errorw(ctx, "DBAddByUuid-failed-missing-uuid", log.Fields{"rec": rec, "dev-uuid": deviceUuid})
+ return errors.New("missing uuid")
+ }
+ key := fmt.Sprintf(DbPathUuidToRecord, deviceUuid, rec.Uuid)
+ b, _ := json.Marshal(rec)
+ entry := string(b)
+ err := db.Put(ctx, key, entry)
+ cache.store(deviceUuid, rec)
+ logger.Infow(ctx, "Inserting-hw-comp-info-to-Db-in-DBAddByUuid-method", log.Fields{"rec": rec, "error": err})
+ return err
+}
+
+// DBAddByName inserts Device Info record to DB with Name as Key
+func DBAddNameToUuidlookup(ctx context.Context, deviceUuid string, nameToUuidMap map[string]string) error {
+ if deviceUuid == "" || len(nameToUuidMap) == 0 {
+ logger.Errorw(ctx, "DBAddNameToUuidlookup-failed-missing-uuid-or-map", log.Fields{"map": nameToUuidMap, "dev-uuid": deviceUuid})
+ return errors.New("missing info")
+ }
+ key := fmt.Sprintf(DbPathNameToUuid, deviceUuid)
+ b, _ := json.Marshal(nameToUuidMap)
+ entry := string(b)
+ err := db.Put(ctx, key, entry)
+ logger.Infow(ctx, "DBAddNameToUuidlookup-method-complete", log.Fields{"map": nameToUuidMap, "error": err})
+ return err
+}
+
+// DBSaveHwCompsFromPhysicalInventory iterates through each children and store hwcomponents in db
+func DBSaveHwCompsFromPhysicalInventory(ctx context.Context, deviceUuid string, nameToUuidMap map[string]string, children []*dmi.Component) {
+ if len(children) == 0 {
+ return
+ }
+ for _, child := range children {
+ hwRec := new(HwCompRecord)
+ if err := copy.Copy(&hwRec, &child); hwRec.Name == "" {
+ logger.Errorw(ctx, "Failed-at-copying-hw-comp-from-inventory-list", log.Fields{"error": err, "child": child, "hw-comp": hwRec})
+ continue
+ }
+ if child.Uri != nil {
+ hwRec.Uri = child.Uri.Uri
+ }
+ if child.Uuid != nil {
+ hwRec.Uuid = child.Uuid.Uuid
+ }
+ for _, grandChild := range child.Children {
+ hwRec.Children = append(hwRec.Children, grandChild.Uuid.Uuid)
+ }
+ hwRec.DBAddByUuid(ctx, deviceUuid)
+ nameToUuidMap[hwRec.Name] = hwRec.Uuid
+ DBSaveHwCompsFromPhysicalInventory(ctx, deviceUuid, nameToUuidMap, child.Children)
+ logger.Infow(ctx, "Successful-at-creating-object-for-new-hw-info", log.Fields{"new-object": child})
+ }
+}
+
+// DBDelRecord deletes all entries for Device Info
+func DBDelAllHwComponents(ctx context.Context, deviceUuid string) error {
+
+ var err error
+
+ if deviceUuid == "" {
+ logger.Errorw(ctx, "deleting-all-hw-components-failed", log.Fields{"reason": "missing-device-uuid"})
+ return errors.New("missing device uuid")
+ }
+
+ key := fmt.Sprintf(DbPrefix, deviceUuid)
+ err = db.DelAll(ctx, key)
+ cache.delDevice(deviceUuid)
+ logger.Infow(ctx, "deleting-all-hw-components-completed", log.Fields{"key": key})
+
+ return err
+}
+
+// DBGetRecByUuid func reads hw comp record by uuid
+func DBGetRecByUuid(ctx context.Context, deviceUuid, hwCompUuid string) (*HwCompRecord, error) {
+ if deviceUuid == "" || hwCompUuid == "" {
+ logger.Errorw(ctx, "DBGetHwCompRec-failed-missing-uuid", log.Fields{"device-uuid": deviceUuid, "hw-comp-uuid": hwCompUuid})
+ return nil, errors.New("uuid field is empty")
+ }
+
+ if rec := cache.get(deviceUuid, hwCompUuid); rec != nil {
+ return rec, nil
+ }
+
+ key := fmt.Sprintf(DbPathUuidToRecord, deviceUuid, hwCompUuid)
+ entry, err := db.Read(ctx, key)
+ if err != nil {
+ logger.Errorw(ctx, "DBGetRecByUuid-failed-read-db", log.Fields{"error": err, "key": key})
+ return nil, err
+ }
+
+ rec := new(HwCompRecord)
+ if err = json.Unmarshal([]byte(entry), rec); err != nil {
+ logger.Errorw(ctx, "Failed-to-unmarshal-at-DBGetRecByUuid", log.Fields{"reason": err, "entry": entry})
+ return nil, err
+ }
+
+ cache.store(deviceUuid, rec)
+
+ logger.Debugw(ctx, "DBGetHwCompRec-completed", log.Fields{"device-uuid": deviceUuid, "hw-comp-uuid": hwCompUuid, "rec": rec})
+ return rec, nil
+}
+
+// DBGetRecByName func reads hw comp record by name
+func DBGetRecByName(ctx context.Context, deviceUuid, hwName string) (*HwCompRecord, error) {
+ if deviceUuid == "" || hwName == "" {
+ logger.Errorw(ctx, "DBGetRecByName-failed-missing-uuid", log.Fields{"device-uuid": deviceUuid, "hw-comp-name": hwName})
+ return nil, errors.New("name field is empty")
+ }
+ key := fmt.Sprintf(DbPathNameToUuid, deviceUuid)
+ entry, err := db.Read(ctx, key)
+ if err != nil {
+ logger.Errorw(ctx, "DBGetRecByName-failed-read-db", log.Fields{"error": err, "key": key})
+ return nil, err
+ }
+
+ nameToUuidMap := make(map[string]string)
+ if err = json.Unmarshal([]byte(entry), &nameToUuidMap); nameToUuidMap == nil || err != nil {
+ logger.Errorw(ctx, "Failed-to-unmarshal-at-DBGetRecByName", log.Fields{"reason": err, "entry": entry})
+ return nil, err
+ }
+
+ if hwUuid, ok := nameToUuidMap[hwName]; ok {
+ rec, err2 := DBGetRecByUuid(ctx, deviceUuid, hwUuid)
+ logger.Debugw(ctx, "DBGetRecByName-completed", log.Fields{"device-uuid": deviceUuid, "hw-comp-name": hwName, "rec": rec, "error": err2})
+ }
+
+ return nil, errors.New("name not found")
+}
diff --git a/pkg/models/hwcomponents/models.go b/pkg/models/hwcomponents/models.go
new file mode 100644
index 0000000..9906b53
--- /dev/null
+++ b/pkg/models/hwcomponents/models.go
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Package hwcomponents stores methods and functions related to hardware
+package hwcomponents
+
+import (
+ "sync"
+
+ config "github.com/opencord/opendevice-manager/pkg/config"
+ v1 "github.com/opencord/opendevice-manager/pkg/models/hwcomponents/v1"
+ log "github.com/opencord/voltha-lib-go/v4/pkg/log"
+)
+
+// Constants defined are the DB Path meant for storing hw component info records
+const (
+ DbPrefix = config.DBPrefix + config.CurDBVer + "/HwCompRec/%s"
+ // Key : /OpenDevMgr/v1/HwCompRec/{Device-Uuid}/Components
+ // Val : Map => {"hw-comp-name-1":"hw-comp-uuid-1", "hw-comp-name-2":"hw-comp-uuid-2"}
+ DbPathNameToUuid = DbPrefix + "/Components"
+ // Key : /OpenDevMgr/v1/HwCompRec/{Device-Uuid}/Uuid/{Hw-Comp-Uuid}
+ // Val : HwCompRecord{}
+ DbPathUuidToRecord = DbPrefix + "/Uuid/%s"
+)
+
+// compCache stores component information in buffer
+type compCache struct {
+ uuidToRec map[string]map[string]*HwCompRecord // nameToRecord maintains cache for mapping from name to main record
+ mutex sync.Mutex
+}
+
+var cache *compCache
+
+// logger represents the log object
+var logger log.CLogger
+
+// initCache initialises device cache
+func initCache() {
+ cache = new(compCache)
+ cache.uuidToRec = make(map[string]map[string]*HwCompRecord)
+ cache.mutex = sync.Mutex{}
+}
+
+// init function for the package
+func init() {
+ logger = config.Initlog()
+ initCache()
+}
+
+type HwCompRecord v1.HwCompRecordV1_0
+
+func (*compCache) store(devUuid string, rec *HwCompRecord) {
+ cache.mutex.Lock()
+ defer cache.mutex.Unlock()
+
+ var uuidToRecMap map[string]*HwCompRecord
+
+ if val, ok := cache.uuidToRec[devUuid]; !ok {
+ uuidToRecMap = make(map[string]*HwCompRecord)
+ } else {
+ uuidToRecMap = val
+ }
+
+ uuidToRecMap[rec.Uuid] = rec
+ cache.uuidToRec[devUuid] = uuidToRecMap
+}
+
+func (*compCache) get(devUuid, compUuid string) *HwCompRecord {
+ cache.mutex.Lock()
+ defer cache.mutex.Unlock()
+
+ var uuidToRecMap map[string]*HwCompRecord
+
+ if val, ok := cache.uuidToRec[devUuid]; !ok {
+ return nil
+ } else {
+ uuidToRecMap = val
+ }
+
+ if rec, ok := uuidToRecMap[compUuid]; ok {
+ return rec
+ }
+
+ return nil
+}
+
+func (*compCache) delDevice(devUuid string) *HwCompRecord {
+ cache.mutex.Lock()
+ defer cache.mutex.Unlock()
+ delete(cache.uuidToRec, devUuid)
+ return nil
+}
diff --git a/pkg/models/hwcomponents/v1/models.go b/pkg/models/hwcomponents/v1/models.go
new file mode 100644
index 0000000..b7734a8
--- /dev/null
+++ b/pkg/models/hwcomponents/v1/models.go
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Package v1 stores models for hw components
+package v1
+
+import (
+ dmi "github.com/opencord/device-management-interface/go/dmi"
+
+ timestamp "github.com/golang/protobuf/ptypes/timestamp"
+)
+
+type HwCompRecordV1_0 struct {
+ Name string `json:"name,omitempty"`
+ Class dmi.ComponentType `json:"class,omitempty"`
+ Description string `json:"description,omitempty"`
+ Parent string `json:"parent,omitempty"`
+ ParentRelPos int32 `json:"parent_rel_pos,omitempty"`
+ Children []string `json:"children,omitempty"` // Children stores uuid of all direct child
+ SerialNum string `json:"serial_num,omitempty"`
+ MfgName string `json:"mfg_name,omitempty"`
+ ModelName string `json:"model_name,omitempty"`
+ Alias string `json:"alias,omitempty"`
+ AssetId string `json:"asset_id,omitempty"`
+ IsFru bool `json:"is_fru,omitempty"`
+ MfgDate *timestamp.Timestamp `json:"mfg_date,omitempty"`
+ Uri string `json:"uri,omitempty"`
+ Uuid string `json:"uuid,omitempty"`
+ State *dmi.ComponentState `json:"state,omitempty"`
+ SensorData []*dmi.ComponentSensorData `json:"sensor_data,omitempty"`
+ Specific string `json:"specific,omitempty"`
+}