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.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
24 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
25 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
26 "github.com/golang/protobuf/proto"
30 const E2TAddressesKey = "E2TAddresses"
32 type rNibReaderInstance struct {
33 sdl common.ISdlInstance //Deprecated: Will be removed in a future release and replaced by sdlStorage
34 sdlStorage common.ISdlSyncStorage
39 RNibReader interface allows retrieving data from redis BD by various keys
41 type RNibReader interface {
42 // GetNodeb retrieves responding nodeb entity from redis DB by nodeb inventory name
43 GetNodeb(inventoryName string) (*entities.NodebInfo, error)
44 // GetNodebByGlobalNbId retrieves responding nodeb entity from redis DB by nodeb global Id
45 GetNodebByGlobalNbId(nodeType entities.Node_Type, globalNbId *entities.GlobalNbId) (*entities.NodebInfo, error)
46 // GetCellList retrieves the list of cell entities belonging to responding nodeb entity from redis DB by nodeb inventory name
47 GetCellList(inventoryName string) (*entities.Cells, error)
48 // GetListGnbIds retrieves the list of gNodeb identity entities
49 GetListGnbIds() ([]*entities.NbIdentity, error)
50 // GetListEnbIds retrieves the list of eNodeb identity entities
51 GetListEnbIds() ([]*entities.NbIdentity, error)
52 // Close closes reader's pool
53 GetCountGnbList() (int, error)
54 // GetCell retrieves the cell entity belonging to responding nodeb from redis DB by nodeb inventory name and cell pci
55 GetCell(inventoryName string, pci uint32) (*entities.Cell, error)
56 // GetCellById retrieves the cell entity from redis DB by cell type and cell Id
57 GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, error)
58 // GetListNodebIds returns the full list of Nodeb identity entities
59 GetListNodebIds() ([]*entities.NbIdentity, error)
60 // GetRanLoadInformation retrieves nodeb load information entity from redis DB by nodeb inventory name
61 GetRanLoadInformation(inventoryName string) (*entities.RanLoadInformation, error)
63 GetE2TInstance(address string) (*entities.E2TInstance, error)
65 GetE2TInstances(addresses []string) ([]*entities.E2TInstance, error)
67 GetE2TAddresses() ([]string, error)
69 GetGeneralConfiguration() (*entities.GeneralConfiguration, error)
72 //GetNewRNibReader returns reference to RNibReader
73 func GetNewRNibReader(storage common.ISdlSyncStorage) RNibReader {
74 return &rNibReaderInstance{
77 ns: common.GetRNibNamespace(),
81 //GetRNibReader returns reference to RNibReader
82 //Deprecated: Will be removed in a future release, please use GetNewRNibReader instead.
83 func GetRNibReader(sdl common.ISdlInstance) RNibReader {
84 return &rNibReaderInstance{
91 func (w *rNibReaderInstance) GetNodeb(inventoryName string) (*entities.NodebInfo, error) {
92 key, rNibErr := common.ValidateAndBuildNodeBNameKey(inventoryName)
96 nbInfo := &entities.NodebInfo{}
97 err := w.getByKeyAndUnmarshal(key, nbInfo)
104 func (w *rNibReaderInstance) GetNodebByGlobalNbId(nodeType entities.Node_Type, globalNbId *entities.GlobalNbId) (*entities.NodebInfo, error) {
105 key, rNibErr := common.ValidateAndBuildNodeBIdKey(nodeType.String(), globalNbId.GetPlmnId(), globalNbId.GetNbId())
109 nbInfo := &entities.NodebInfo{}
110 err := w.getByKeyAndUnmarshal(key, nbInfo)
117 func (w *rNibReaderInstance) GetCellList(inventoryName string) (*entities.Cells, error) {
118 cells := &entities.Cells{}
119 nb, err := w.GetNodeb(inventoryName)
123 if nb.GetEnb() != nil && len(nb.GetEnb().GetServedCells()) > 0 {
124 cells.Type = entities.Cell_LTE_CELL
125 cells.List = &entities.Cells_ServedCellInfos{ServedCellInfos: &entities.ServedCellInfoList{ServedCells: nb.GetEnb().GetServedCells()}}
128 if nb.GetGnb() != nil && len(nb.GetGnb().GetServedNrCells()) > 0 {
129 cells.Type = entities.Cell_NR_CELL
130 cells.List = &entities.Cells_ServedNrCells{ServedNrCells: &entities.ServedNRCellList{ServedCells: nb.GetGnb().GetServedNrCells()}}
133 return nil, common.NewResourceNotFoundErrorf("#rNibReader.GetCellList - served cells not found. Responding node RAN name: %s.", inventoryName)
136 func (w *rNibReaderInstance) GetListGnbIds() ([]*entities.NbIdentity, error) {
137 return w.getListNodebIdsByType(entities.Node_GNB.String())
140 func (w *rNibReaderInstance) GetListEnbIds() ([]*entities.NbIdentity, error) {
141 return w.getListNodebIdsByType(entities.Node_ENB.String())
144 func (w *rNibReaderInstance) GetCountGnbList() (int, error) {
147 if w.sdlStorage != nil {
148 size, err = w.sdlStorage.GroupSize(w.ns, entities.Node_GNB.String())
150 size, err = w.sdl.GroupSize(entities.Node_GNB.String())
153 return 0, common.NewInternalError(err)
155 return int(size), nil
158 func (w *rNibReaderInstance) GetCell(inventoryName string, pci uint32) (*entities.Cell, error) {
159 key, rNibErr := common.ValidateAndBuildCellNamePciKey(inventoryName, pci)
163 cell := &entities.Cell{}
164 err := w.getByKeyAndUnmarshal(key, cell)
171 func (w *rNibReaderInstance) GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, error) {
174 if cellType == entities.Cell_LTE_CELL {
175 key, rNibErr = common.ValidateAndBuildCellIdKey(cellId)
176 } else if cellType == entities.Cell_NR_CELL {
177 key, rNibErr = common.ValidateAndBuildNrCellIdKey(cellId)
179 return nil, common.NewValidationErrorf("#rNibReader.GetCellById - invalid cell type: %v", cellType)
184 cell := &entities.Cell{}
185 err := w.getByKeyAndUnmarshal(key, cell)
192 func (w *rNibReaderInstance) GetListNodebIds() ([]*entities.NbIdentity, error) {
193 var dataEnb, dataGnb []string
195 if w.sdlStorage != nil {
196 dataEnb, err = w.sdlStorage.GetMembers(w.ns, entities.Node_ENB.String())
198 dataEnb, err = w.sdl.GetMembers(entities.Node_ENB.String())
201 return nil, common.NewInternalError(err)
203 if w.sdlStorage != nil {
204 dataGnb, err = w.sdlStorage.GetMembers(w.ns, entities.Node_GNB.String())
206 dataGnb, err = w.sdl.GetMembers(entities.Node_GNB.String())
209 return nil, common.NewInternalError(err)
211 allIds := append(dataEnb, dataGnb...)
212 data, rnibErr := w.unmarshalIdentityList(allIds)
216 func (w *rNibReaderInstance) GetRanLoadInformation(inventoryName string) (*entities.RanLoadInformation, error) {
217 key, rNibErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
221 loadInfo := &entities.RanLoadInformation{}
222 err := w.getByKeyAndUnmarshal(key, loadInfo)
229 func (w *rNibReaderInstance) GetE2TInstance(address string) (*entities.E2TInstance, error) {
230 key, rNibErr := common.ValidateAndBuildE2TInstanceKey(address)
234 e2tInstance := &entities.E2TInstance{}
235 err := w.getByKeyAndUnmarshalJson(key, e2tInstance)
239 return e2tInstance, err
242 func (w *rNibReaderInstance) GetE2TInstances(addresses []string) ([]*entities.E2TInstance, error) {
243 var data map[string]interface{}
246 keys := common.MapE2TAddressesToKeys(addresses)
248 e2tInstances := []*entities.E2TInstance{}
250 if w.sdlStorage != nil {
251 data, err = w.sdlStorage.Get(w.ns, keys)
253 data, err = w.sdl.Get(keys)
257 return []*entities.E2TInstance{}, common.NewInternalError(err)
261 return []*entities.E2TInstance{}, common.NewResourceNotFoundErrorf("#rNibReader.GetE2TInstances - e2t instances not found")
264 for _, v := range keys {
267 var e2tInstance entities.E2TInstance
268 err = json.Unmarshal([]byte(data[v].(string)), &e2tInstance)
273 e2tInstances = append(e2tInstances, &e2tInstance)
277 return e2tInstances, nil
280 func (w *rNibReaderInstance) GetE2TAddresses() ([]string, error) {
281 var e2tAddresses []string
282 err := w.getByKeyAndUnmarshalJson(E2TAddressesKey, &e2tAddresses)
286 return e2tAddresses, err
289 func (w *rNibReaderInstance) GetGeneralConfiguration() (*entities.GeneralConfiguration, error) {
290 config := &entities.GeneralConfiguration{}
291 key := common.BuildGeneralConfigurationKey()
293 err := w.getByKeyAndUnmarshalJson(key, config)
298 func (w *rNibReaderInstance) getByKeyAndUnmarshalJson(key string, entity interface{}) error {
299 var data map[string]interface{}
301 if w.sdlStorage != nil {
302 data, err = w.sdlStorage.Get(w.ns, []string{key})
304 data, err = w.sdl.Get([]string{key})
308 return common.NewInternalError(err)
311 if data != nil && data[key] != nil {
312 err = json.Unmarshal([]byte(data[key].(string)), entity)
314 return common.NewInternalError(err)
318 return common.NewResourceNotFoundErrorf("#rNibReader.getByKeyAndUnmarshalJson - entity of type %s not found. Key: %s", reflect.TypeOf(entity).String(), key)
321 func (w *rNibReaderInstance) getByKeyAndUnmarshal(key string, entity proto.Message) error {
322 var data map[string]interface{}
324 if w.sdlStorage != nil {
325 data, err = w.sdlStorage.Get(w.ns, []string{key})
327 data, err = w.sdl.Get([]string{key})
331 return common.NewInternalError(err)
333 if data != nil && data[key] != nil {
334 err = proto.Unmarshal([]byte(data[key].(string)), entity)
336 return common.NewInternalError(err)
340 return common.NewResourceNotFoundErrorf("#rNibReader.getByKeyAndUnmarshal - entity of type %s not found. Key: %s", reflect.TypeOf(entity).String(), key)
343 func (w *rNibReaderInstance) getListNodebIdsByType(nbType string) ([]*entities.NbIdentity, error) {
346 if w.sdlStorage != nil {
347 data, err = w.sdlStorage.GetMembers(w.ns, nbType)
349 data, err = w.sdl.GetMembers(nbType)
352 return nil, common.NewInternalError(err)
354 return w.unmarshalIdentityList(data)
357 func (w *rNibReaderInstance) unmarshalIdentityList(data []string) ([]*entities.NbIdentity, error) {
358 var members []*entities.NbIdentity
359 for _, d := range data {
360 member := entities.NbIdentity{}
361 err := proto.Unmarshal([]byte(d), &member)
363 return nil, common.NewInternalError(err)
365 members = append(members, &member)