852346b7da4223b1da9ac3c0b3c69e8e262a1604
[ric-plt/resource-status-manager.git] / RSM / rsmdb / rsm_reader.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 package rsmdb
18
19 import (
20         "encoding/json"
21         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
22         "reflect"
23         "rsm/models"
24 )
25
26 type rsmReaderInstance struct {
27         sdl common.ISdlInstance
28 }
29
30 // RsmReader interface allows retrieving data from redis BD by various keys
31 type RsmReader interface {
32         GetRsmGeneralConfiguration() (*models.RsmGeneralConfiguration, error)
33         GetRsmRanInfo(ranName string) (*models.RsmRanInfo, error)
34 }
35
36 // GetRsmReader returns reference to RsmReader
37 func GetRsmReader(sdl common.ISdlInstance) RsmReader {
38         return &rsmReaderInstance{sdl: sdl}
39 }
40
41 // GetRsmRanInfo returns the rsm data associated with ran 'ranName'
42 func (r *rsmReaderInstance) GetRsmRanInfo(ranName string) (*models.RsmRanInfo, error) {
43
44         key, err := common.ValidateAndBuildNodeBNameKey(ranName)
45         if err != nil {
46                 return nil, err
47         }
48
49         rsmRanInfo := &models.RsmRanInfo{}
50
51         err = r.getByKeyAndUnmarshal(key, rsmRanInfo)
52
53         if err != nil {
54                 return nil, err
55         }
56
57         return rsmRanInfo, nil
58 }
59
60 // GetRsmGeneralConfiguration returns resource status request related configuration
61 func (r *rsmReaderInstance) GetRsmGeneralConfiguration() (*models.RsmGeneralConfiguration, error) {
62         cfg := &models.RsmGeneralConfiguration{}
63         err := r.getByKeyAndUnmarshal(buildRsmGeneralConfigurationKey(), cfg)
64         return cfg, err
65 }
66
67 // getByKeyAndUnmarshal returns the value that is associated with key 'key' as a Go structure
68 func (r *rsmReaderInstance) getByKeyAndUnmarshal(key string, entity interface{}) error {
69         data, err := r.sdl.Get([]string{key})
70         if err != nil {
71                 return common.NewInternalError(err)
72         }
73         if data != nil && data[key] != nil {
74                 err = json.Unmarshal([]byte(data[key].(string)), entity)
75                 if err != nil {
76                         return common.NewInternalError(err)
77                 }
78                 return nil
79         }
80         return common.NewResourceNotFoundErrorf("#rsmReader.getByKeyAndUnmarshal - entity of type %s not found. Key: %s", reflect.TypeOf(entity).String(), key)
81 }
82
83 func buildRsmGeneralConfigurationKey() string {
84         return "CFG:GENERAL:v1.0.0"
85 }