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)
71 GetRanFunctionDefinition(inventoryName string, Oid string) ([]string, error)
74 //GetNewRNibReader returns reference to RNibReader
75 func GetNewRNibReader(storage common.ISdlSyncStorage) RNibReader {
76 return &rNibReaderInstance{
79 ns: common.GetRNibNamespace(),
83 //GetRanFunctionDefinition from the OID
84 func (w *rNibReaderInstance) GetRanFunctionDefinition(inventoryName string, oid string) ([]string, error){
85 nb, err := w.GetNodeb (inventoryName)
86 if (nb.GetGnb() != nil) {
87 ranFunction := nb.GetGnb().RanFunctions
88 functionDefinitionList := make([]string, 0)
89 for _, ranFunction := range ranFunction {
90 if (oid == ranFunction.RanFunctionOid) {
91 functionDefinitionList = append(functionDefinitionList ,ranFunction.RanFunctionDefinition)
94 return functionDefinitionList, err
96 return nil, common.NewResourceNotFoundErrorf("#rNibReader.GetCellList - served cells not found. Responding node RAN name: % s.", inventoryName)
99 //GetRNibReader returns reference to RNibReader
100 //Deprecated: Will be removed in a future release, please use GetNewRNibReader instead.
101 func GetRNibReader(sdl common.ISdlInstance) RNibReader {
102 return &rNibReaderInstance{
109 func (w *rNibReaderInstance) GetNodeb(inventoryName string) (*entities.NodebInfo, error) {
110 key, rNibErr := common.ValidateAndBuildNodeBNameKey(inventoryName)
114 nbInfo := &entities.NodebInfo{}
115 err := w.getByKeyAndUnmarshal(key, nbInfo)
122 func (w *rNibReaderInstance) GetNodebByGlobalNbId(nodeType entities.Node_Type, globalNbId *entities.GlobalNbId) (*entities.NodebInfo, error) {
123 key, rNibErr := common.ValidateAndBuildNodeBIdKey(nodeType.String(), globalNbId.GetPlmnId(), globalNbId.GetNbId())
127 nbInfo := &entities.NodebInfo{}
128 err := w.getByKeyAndUnmarshal(key, nbInfo)
135 func (w *rNibReaderInstance) GetCellList(inventoryName string) (*entities.Cells, error) {
136 cells := &entities.Cells{}
137 nb, err := w.GetNodeb(inventoryName)
141 if nb.GetEnb() != nil && len(nb.GetEnb().GetServedCells()) > 0 {
142 cells.Type = entities.Cell_LTE_CELL
143 cells.List = &entities.Cells_ServedCellInfos{ServedCellInfos: &entities.ServedCellInfoList{ServedCells: nb.GetEnb().GetServedCells()}}
146 if nb.GetGnb() != nil && len(nb.GetGnb().GetServedNrCells()) > 0 {
147 cells.Type = entities.Cell_NR_CELL
148 cells.List = &entities.Cells_ServedNrCells{ServedNrCells: &entities.ServedNRCellList{ServedCells: nb.GetGnb().GetServedNrCells()}}
151 return nil, common.NewResourceNotFoundErrorf("#rNibReader.GetCellList - served cells not found. Responding node RAN name: %s.", inventoryName)
154 func (w *rNibReaderInstance) GetListGnbIds() ([]*entities.NbIdentity, error) {
155 return w.getListNodebIdsByType(entities.Node_GNB.String())
158 func (w *rNibReaderInstance) GetListEnbIds() ([]*entities.NbIdentity, error) {
159 return w.getListNodebIdsByType(entities.Node_ENB.String())
162 func (w *rNibReaderInstance) GetCountGnbList() (int, error) {
165 if w.sdlStorage != nil {
166 size, err = w.sdlStorage.GroupSize(w.ns, entities.Node_GNB.String())
168 size, err = w.sdl.GroupSize(entities.Node_GNB.String())
171 return 0, common.NewInternalError(err)
173 return int(size), nil
176 func (w *rNibReaderInstance) GetCell(inventoryName string, pci uint32) (*entities.Cell, error) {
177 key, rNibErr := common.ValidateAndBuildCellNamePciKey(inventoryName, pci)
181 cell := &entities.Cell{}
182 err := w.getByKeyAndUnmarshal(key, cell)
189 func (w *rNibReaderInstance) GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, error) {
192 if cellType == entities.Cell_LTE_CELL {
193 key, rNibErr = common.ValidateAndBuildCellIdKey(cellId)
194 } else if cellType == entities.Cell_NR_CELL {
195 key, rNibErr = common.ValidateAndBuildNrCellIdKey(cellId)
197 return nil, common.NewValidationErrorf("#rNibReader.GetCellById - invalid cell type: %v", cellType)
202 cell := &entities.Cell{}
203 err := w.getByKeyAndUnmarshal(key, cell)
210 func (w *rNibReaderInstance) GetListNodebIds() ([]*entities.NbIdentity, error) {
211 var dataEnb, dataGnb []string
213 if w.sdlStorage != nil {
214 dataEnb, err = w.sdlStorage.GetMembers(w.ns, entities.Node_ENB.String())
216 dataEnb, err = w.sdl.GetMembers(entities.Node_ENB.String())
219 return nil, common.NewInternalError(err)
221 if w.sdlStorage != nil {
222 dataGnb, err = w.sdlStorage.GetMembers(w.ns, entities.Node_GNB.String())
224 dataGnb, err = w.sdl.GetMembers(entities.Node_GNB.String())
227 return nil, common.NewInternalError(err)
229 allIds := append(dataEnb, dataGnb...)
230 data, rnibErr := w.unmarshalIdentityList(allIds)
234 func (w *rNibReaderInstance) GetRanLoadInformation(inventoryName string) (*entities.RanLoadInformation, error) {
235 key, rNibErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
239 loadInfo := &entities.RanLoadInformation{}
240 err := w.getByKeyAndUnmarshal(key, loadInfo)
247 func (w *rNibReaderInstance) GetE2TInstance(address string) (*entities.E2TInstance, error) {
248 key, rNibErr := common.ValidateAndBuildE2TInstanceKey(address)
252 e2tInstance := &entities.E2TInstance{}
253 err := w.getByKeyAndUnmarshalJson(key, e2tInstance)
257 return e2tInstance, err
260 func (w *rNibReaderInstance) GetE2TInstances(addresses []string) ([]*entities.E2TInstance, error) {
261 var data map[string]interface{}
264 keys := common.MapE2TAddressesToKeys(addresses)
266 e2tInstances := []*entities.E2TInstance{}
268 if w.sdlStorage != nil {
269 data, err = w.sdlStorage.Get(w.ns, keys)
271 data, err = w.sdl.Get(keys)
275 return []*entities.E2TInstance{}, common.NewInternalError(err)
279 return []*entities.E2TInstance{}, common.NewResourceNotFoundErrorf("#rNibReader.GetE2TInstances - e2t instances not found")
282 for _, v := range keys {
285 var e2tInstance entities.E2TInstance
286 err = json.Unmarshal([]byte(data[v].(string)), &e2tInstance)
291 e2tInstances = append(e2tInstances, &e2tInstance)
295 return e2tInstances, nil
298 func (w *rNibReaderInstance) GetE2TAddresses() ([]string, error) {
299 var e2tAddresses []string
300 err := w.getByKeyAndUnmarshalJson(E2TAddressesKey, &e2tAddresses)
304 return e2tAddresses, err
307 func (w *rNibReaderInstance) GetGeneralConfiguration() (*entities.GeneralConfiguration, error) {
308 config := &entities.GeneralConfiguration{}
309 key := common.BuildGeneralConfigurationKey()
311 err := w.getByKeyAndUnmarshalJson(key, config)
316 func (w *rNibReaderInstance) getByKeyAndUnmarshalJson(key string, entity interface{}) error {
317 var data map[string]interface{}
319 if w.sdlStorage != nil {
320 data, err = w.sdlStorage.Get(w.ns, []string{key})
322 data, err = w.sdl.Get([]string{key})
326 return common.NewInternalError(err)
329 if data != nil && data[key] != nil {
330 err = json.Unmarshal([]byte(data[key].(string)), entity)
332 return common.NewInternalError(err)
336 return common.NewResourceNotFoundErrorf("#rNibReader.getByKeyAndUnmarshalJson - entity of type %s not found. Key: %s", reflect.TypeOf(entity).String(), key)
339 func (w *rNibReaderInstance) getByKeyAndUnmarshal(key string, entity proto.Message) error {
340 var data map[string]interface{}
342 if w.sdlStorage != nil {
343 data, err = w.sdlStorage.Get(w.ns, []string{key})
345 data, err = w.sdl.Get([]string{key})
349 return common.NewInternalError(err)
351 if data != nil && data[key] != nil {
352 err = proto.Unmarshal([]byte(data[key].(string)), entity)
354 return common.NewInternalError(err)
358 return common.NewResourceNotFoundErrorf("#rNibReader.getByKeyAndUnmarshal - entity of type %s not found. Key: %s", reflect.TypeOf(entity).String(), key)
361 func (w *rNibReaderInstance) getListNodebIdsByType(nbType string) ([]*entities.NbIdentity, error) {
364 if w.sdlStorage != nil {
365 data, err = w.sdlStorage.GetMembers(w.ns, nbType)
367 data, err = w.sdl.GetMembers(nbType)
370 return nil, common.NewInternalError(err)
372 return w.unmarshalIdentityList(data)
375 func (w *rNibReaderInstance) unmarshalIdentityList(data []string) ([]*entities.NbIdentity, error) {
376 var members []*entities.NbIdentity
377 for _, d := range data {
378 member := entities.NbIdentity{}
379 err := proto.Unmarshal([]byte(d), &member)
381 return nil, common.NewInternalError(err)
383 members = append(members, &member)