2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
21 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
26 type rsmReaderInstance struct {
27 sdl common.ISdlInstance
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)
36 // GetRsmReader returns reference to RsmReader
37 func GetRsmReader(sdl common.ISdlInstance) RsmReader {
38 return &rsmReaderInstance{sdl: sdl}
41 // GetRsmRanInfo returns the rsm data associated with ran 'ranName'
42 func (r *rsmReaderInstance) GetRsmRanInfo(ranName string) (*models.RsmRanInfo, error) {
44 key, err := common.ValidateAndBuildNodeBNameKey(ranName)
49 rsmRanInfo := &models.RsmRanInfo{}
51 err = r.getByKeyAndUnmarshal(key, rsmRanInfo)
57 return rsmRanInfo, nil
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)
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})
71 return common.NewInternalError(err)
73 if data != nil && data[key] != nil {
74 err = json.Unmarshal([]byte(data[key].(string)), entity)
76 return common.NewInternalError(err)
80 return common.NewResourceNotFoundErrorf("#rsmReader.getByKeyAndUnmarshal - entity of type %s not found. Key: %s", reflect.TypeOf(entity).String(), key)
83 func buildRsmGeneralConfigurationKey() string {
84 return "CFG:GENERAL:v1.0.0"