Add header missing license header
[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 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20 package rsmdb
21
22 import (
23         "encoding/json"
24         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
25         "reflect"
26         "rsm/models"
27 )
28
29 type rsmReaderInstance struct {
30         sdl common.ISdlInstance
31 }
32
33 // RsmReader interface allows retrieving data from redis BD by various keys
34 type RsmReader interface {
35         GetRsmGeneralConfiguration() (*models.RsmGeneralConfiguration, error)
36         GetRsmRanInfo(ranName string) (*models.RsmRanInfo, error)
37 }
38
39 // GetRsmReader returns reference to RsmReader
40 func GetRsmReader(sdl common.ISdlInstance) RsmReader {
41         return &rsmReaderInstance{sdl: sdl}
42 }
43
44 // GetRsmRanInfo returns the rsm data associated with ran 'ranName'
45 func (r *rsmReaderInstance) GetRsmRanInfo(ranName string) (*models.RsmRanInfo, error) {
46
47         key, err := common.ValidateAndBuildNodeBNameKey(ranName)
48         if err != nil {
49                 return nil, err
50         }
51
52         rsmRanInfo := &models.RsmRanInfo{}
53
54         err = r.getByKeyAndUnmarshal(key, rsmRanInfo)
55
56         if err != nil {
57                 return nil, err
58         }
59
60         return rsmRanInfo, nil
61 }
62
63 // GetRsmGeneralConfiguration returns resource status request related configuration
64 func (r *rsmReaderInstance) GetRsmGeneralConfiguration() (*models.RsmGeneralConfiguration, error) {
65         cfg := &models.RsmGeneralConfiguration{}
66         err := r.getByKeyAndUnmarshal(buildRsmGeneralConfigurationKey(), cfg)
67         return cfg, err
68 }
69
70 // getByKeyAndUnmarshal returns the value that is associated with key 'key' as a Go structure
71 func (r *rsmReaderInstance) getByKeyAndUnmarshal(key string, entity interface{}) error {
72         data, err := r.sdl.Get([]string{key})
73         if err != nil {
74                 return common.NewInternalError(err)
75         }
76         if data != nil && data[key] != nil {
77                 err = json.Unmarshal([]byte(data[key].(string)), entity)
78                 if err != nil {
79                         return common.NewInternalError(err)
80                 }
81                 return nil
82         }
83         return common.NewResourceNotFoundErrorf("#rsmReader.getByKeyAndUnmarshal - entity of type %s not found. Key: %s", reflect.TypeOf(entity).String(), key)
84 }
85
86 func buildRsmGeneralConfigurationKey() string {
87         return "CFG:GENERAL:v1.0.0"
88 }