blob: ab9611dc730e4f43b62d54298f5a70c5db738b89 [file] [log] [blame]
Prince Pereirac1c21d62021-04-22 08:38:15 +00001/*
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 device stores methods and functions related to device
18package device
19
20import (
21 "context"
22 "sync"
23
24 "github.com/opencord/device-management-interface/go/dmi"
25
26 "github.com/jinzhu/copier"
27 "github.com/opencord/opendevice-manager/pkg/config"
28
29 v1 "github.com/opencord/opendevice-manager/pkg/models/device/v1"
30 "github.com/opencord/voltha-lib-go/v4/pkg/log"
31)
32
33// Constants defined are the DB Path meant for storing device info records
34const (
35 DbPathUuidToName = config.DBPrefix + config.CurDBVer + "/DevRec/DevUuid/%s"
36 DbPathNameToRecord = config.DBPrefix + config.CurDBVer + "/DevRec/DevName/%s"
37)
38
39// deviceCache stores device informations in buffer
40type deviceCache struct {
41 nameToRec *sync.Map // nameToRecord maintains cache for mapping from name to main record
42 uuidToName *sync.Map // uuidToName maintains cache for mapping from uuid to name
43}
44
45var cache *deviceCache
46
47// logger represents the log object
48var logger log.CLogger
49
50// initCache initialises device cache
51func initCache() {
52 cache = new(deviceCache)
53 cache.nameToRec = new(sync.Map)
54 cache.uuidToName = new(sync.Map)
55}
56
57// init function for the package
58func init() {
59 logger = config.Initlog()
60 initCache()
61}
62
63// ClearCacheEntry clearsentry from device cache
64func ClearCacheEntry(ctx context.Context, name, uuid string) {
65 if name != "" {
66 logger.Debugw(ctx, "Clearing-name-key-from-device-cache", log.Fields{"name": name})
67 cache.nameToRec.Delete(name)
68 }
69 if uuid != "" {
70 logger.Debugw(ctx, "Clearing-uuid-key-from-device-cache", log.Fields{"uuid": uuid})
71 cache.uuidToName.Delete(name)
72 }
73}
74
75// DeviceRecord refers to the structure defined for storing OLT info
76type DeviceRecord v1.DeviceRecordV1_0
77
78// NewDeviceRecord return record for aliased ModifiableComponent
79func NewDeviceRecord(ctx context.Context, req *dmi.ModifiableComponent) (*DeviceRecord, error) {
80 rec := new(DeviceRecord)
81 err := copier.Copy(&rec, &req)
82 if err != nil {
83 logger.Errorw(ctx, "Failed-at-creating-object-for-new-device-info", log.Fields{"error": err, "req": req})
84 return nil, err
85 }
86 rec.Uri = req.Uri.Uri
87 rec.State = new(dmi.ComponentState)
88 rec.State.AdminState = req.AdminState
89 logger.Infow(ctx, "Successful-at-creating-object-for-new-device-info", log.Fields{"new-object": rec})
90 return rec, nil
91}