blob: 9906b533e98d3785ce7497bc7eb069e0a738feb6 [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 hwcomponents stores methods and functions related to hardware
18package hwcomponents
19
20import (
21 "sync"
22
23 config "github.com/opencord/opendevice-manager/pkg/config"
24 v1 "github.com/opencord/opendevice-manager/pkg/models/hwcomponents/v1"
25 log "github.com/opencord/voltha-lib-go/v4/pkg/log"
26)
27
28// Constants defined are the DB Path meant for storing hw component info records
29const (
30 DbPrefix = config.DBPrefix + config.CurDBVer + "/HwCompRec/%s"
31 // Key : /OpenDevMgr/v1/HwCompRec/{Device-Uuid}/Components
32 // Val : Map => {"hw-comp-name-1":"hw-comp-uuid-1", "hw-comp-name-2":"hw-comp-uuid-2"}
33 DbPathNameToUuid = DbPrefix + "/Components"
34 // Key : /OpenDevMgr/v1/HwCompRec/{Device-Uuid}/Uuid/{Hw-Comp-Uuid}
35 // Val : HwCompRecord{}
36 DbPathUuidToRecord = DbPrefix + "/Uuid/%s"
37)
38
39// compCache stores component information in buffer
40type compCache struct {
41 uuidToRec map[string]map[string]*HwCompRecord // nameToRecord maintains cache for mapping from name to main record
42 mutex sync.Mutex
43}
44
45var cache *compCache
46
47// logger represents the log object
48var logger log.CLogger
49
50// initCache initialises device cache
51func initCache() {
52 cache = new(compCache)
53 cache.uuidToRec = make(map[string]map[string]*HwCompRecord)
54 cache.mutex = sync.Mutex{}
55}
56
57// init function for the package
58func init() {
59 logger = config.Initlog()
60 initCache()
61}
62
63type HwCompRecord v1.HwCompRecordV1_0
64
65func (*compCache) store(devUuid string, rec *HwCompRecord) {
66 cache.mutex.Lock()
67 defer cache.mutex.Unlock()
68
69 var uuidToRecMap map[string]*HwCompRecord
70
71 if val, ok := cache.uuidToRec[devUuid]; !ok {
72 uuidToRecMap = make(map[string]*HwCompRecord)
73 } else {
74 uuidToRecMap = val
75 }
76
77 uuidToRecMap[rec.Uuid] = rec
78 cache.uuidToRec[devUuid] = uuidToRecMap
79}
80
81func (*compCache) get(devUuid, compUuid string) *HwCompRecord {
82 cache.mutex.Lock()
83 defer cache.mutex.Unlock()
84
85 var uuidToRecMap map[string]*HwCompRecord
86
87 if val, ok := cache.uuidToRec[devUuid]; !ok {
88 return nil
89 } else {
90 uuidToRecMap = val
91 }
92
93 if rec, ok := uuidToRecMap[compUuid]; ok {
94 return rec
95 }
96
97 return nil
98}
99
100func (*compCache) delDevice(devUuid string) *HwCompRecord {
101 cache.mutex.Lock()
102 defer cache.mutex.Unlock()
103 delete(cache.uuidToRec, devUuid)
104 return nil
105}