X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?p=ric-plt%2Fresource-status-manager.git;a=blobdiff_plain;f=RSM%2Frsmdb%2Frsm_reader.go;fp=RSM%2Frsmdb%2Frsm_reader.go;h=852346b7da4223b1da9ac3c0b3c69e8e262a1604;hp=0000000000000000000000000000000000000000;hb=60652d98d51ee23c1eaca2e8bc2bf19c74c57658;hpb=b8d3ff3abf409da49ecab244cd6d2c2124dbce7c diff --git a/RSM/rsmdb/rsm_reader.go b/RSM/rsmdb/rsm_reader.go new file mode 100644 index 0000000..852346b --- /dev/null +++ b/RSM/rsmdb/rsm_reader.go @@ -0,0 +1,85 @@ +// +// Copyright 2019 AT&T Intellectual Property +// Copyright 2019 Nokia +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package rsmdb + +import ( + "encoding/json" + "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common" + "reflect" + "rsm/models" +) + +type rsmReaderInstance struct { + sdl common.ISdlInstance +} + +// RsmReader interface allows retrieving data from redis BD by various keys +type RsmReader interface { + GetRsmGeneralConfiguration() (*models.RsmGeneralConfiguration, error) + GetRsmRanInfo(ranName string) (*models.RsmRanInfo, error) +} + +// GetRsmReader returns reference to RsmReader +func GetRsmReader(sdl common.ISdlInstance) RsmReader { + return &rsmReaderInstance{sdl: sdl} +} + +// GetRsmRanInfo returns the rsm data associated with ran 'ranName' +func (r *rsmReaderInstance) GetRsmRanInfo(ranName string) (*models.RsmRanInfo, error) { + + key, err := common.ValidateAndBuildNodeBNameKey(ranName) + if err != nil { + return nil, err + } + + rsmRanInfo := &models.RsmRanInfo{} + + err = r.getByKeyAndUnmarshal(key, rsmRanInfo) + + if err != nil { + return nil, err + } + + return rsmRanInfo, nil +} + +// GetRsmGeneralConfiguration returns resource status request related configuration +func (r *rsmReaderInstance) GetRsmGeneralConfiguration() (*models.RsmGeneralConfiguration, error) { + cfg := &models.RsmGeneralConfiguration{} + err := r.getByKeyAndUnmarshal(buildRsmGeneralConfigurationKey(), cfg) + return cfg, err +} + +// getByKeyAndUnmarshal returns the value that is associated with key 'key' as a Go structure +func (r *rsmReaderInstance) getByKeyAndUnmarshal(key string, entity interface{}) error { + data, err := r.sdl.Get([]string{key}) + if err != nil { + return common.NewInternalError(err) + } + if data != nil && data[key] != nil { + err = json.Unmarshal([]byte(data[key].(string)), entity) + if err != nil { + return common.NewInternalError(err) + } + return nil + } + return common.NewResourceNotFoundErrorf("#rsmReader.getByKeyAndUnmarshal - entity of type %s not found. Key: %s", reflect.TypeOf(entity).String(), key) +} + +func buildRsmGeneralConfigurationKey() string { + return "CFG:GENERAL:v1.0.0" +}