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 E2TInfoListKey = "E2TInfoList"
32 type rNibReaderInstance struct {
33 sdl common.ISdlInstance
37 RNibReader interface allows retrieving data from redis BD by various keys
39 type RNibReader interface {
40 // GetNodeb retrieves responding nodeb entity from redis DB by nodeb inventory name
41 GetNodeb(inventoryName string) (*entities.NodebInfo, error)
42 // GetNodebByGlobalNbId retrieves responding nodeb entity from redis DB by nodeb global Id
43 GetNodebByGlobalNbId(nodeType entities.Node_Type, globalNbId *entities.GlobalNbId) (*entities.NodebInfo, error)
44 // GetCellList retrieves the list of cell entities belonging to responding nodeb entity from redis DB by nodeb inventory name
45 GetCellList(inventoryName string) (*entities.Cells, error)
46 // GetListGnbIds retrieves the list of gNodeb identity entities
47 GetListGnbIds() ([]*entities.NbIdentity, error)
48 // GetListEnbIds retrieves the list of eNodeb identity entities
49 GetListEnbIds() ([]*entities.NbIdentity, error)
50 // Close closes reader's pool
51 GetCountGnbList() (int, error)
52 // GetCell retrieves the cell entity belonging to responding nodeb from redis DB by nodeb inventory name and cell pci
53 GetCell(inventoryName string, pci uint32) (*entities.Cell, error)
54 // GetCellById retrieves the cell entity from redis DB by cell type and cell Id
55 GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, error)
56 // GetListNodebIds returns the full list of Nodeb identity entities
57 GetListNodebIds() ([]*entities.NbIdentity, error)
58 // GetRanLoadInformation retrieves nodeb load information entity from redis DB by nodeb inventory name
59 GetRanLoadInformation(inventoryName string) (*entities.RanLoadInformation, error)
61 GetE2TInstance(address string) (*entities.E2TInstance, error)
63 GetE2TInfoList() ([]*entities.E2TInstanceInfo, error)
68 GetRNibReader returns reference to RNibReader
70 func GetRNibReader(sdl common.ISdlInstance) RNibReader {
71 return &rNibReaderInstance{sdl: sdl}
74 func (w *rNibReaderInstance) GetNodeb(inventoryName string) (*entities.NodebInfo, error) {
75 key, rNibErr := common.ValidateAndBuildNodeBNameKey(inventoryName)
79 nbInfo := &entities.NodebInfo{}
80 err := w.getByKeyAndUnmarshal(key, nbInfo)
87 func (w *rNibReaderInstance) GetNodebByGlobalNbId(nodeType entities.Node_Type, globalNbId *entities.GlobalNbId) (*entities.NodebInfo, error) {
88 key, rNibErr := common.ValidateAndBuildNodeBIdKey(nodeType.String(), globalNbId.GetPlmnId(), globalNbId.GetNbId())
92 nbInfo := &entities.NodebInfo{}
93 err := w.getByKeyAndUnmarshal(key, nbInfo)
100 func (w *rNibReaderInstance) GetCellList(inventoryName string) (*entities.Cells, error) {
101 cells := &entities.Cells{}
102 nb, err := w.GetNodeb(inventoryName)
106 if nb.GetEnb() != nil && len(nb.GetEnb().GetServedCells()) > 0 {
107 cells.Type = entities.Cell_LTE_CELL
108 cells.List = &entities.Cells_ServedCellInfos{ServedCellInfos: &entities.ServedCellInfoList{ServedCells: nb.GetEnb().GetServedCells()}}
111 if nb.GetGnb() != nil && len(nb.GetGnb().GetServedNrCells()) > 0 {
112 cells.Type = entities.Cell_NR_CELL
113 cells.List = &entities.Cells_ServedNrCells{ServedNrCells: &entities.ServedNRCellList{ServedCells: nb.GetGnb().GetServedNrCells()}}
116 return nil, common.NewResourceNotFoundErrorf("#rNibReader.GetCellList - served cells not found. Responding node RAN name: %s.", inventoryName)
119 func (w *rNibReaderInstance) GetListGnbIds() ([]*entities.NbIdentity, error) {
120 return w.getListNodebIdsByType(entities.Node_GNB.String())
123 func (w *rNibReaderInstance) GetListEnbIds() ([]*entities.NbIdentity, error) {
124 return w.getListNodebIdsByType(entities.Node_ENB.String())
127 func (w *rNibReaderInstance) GetCountGnbList() (int, error) {
128 size, err := w.sdl.GroupSize(entities.Node_GNB.String())
130 return 0, common.NewInternalError(err)
132 return int(size), nil
135 func (w *rNibReaderInstance) GetCell(inventoryName string, pci uint32) (*entities.Cell, error) {
136 key, rNibErr := common.ValidateAndBuildCellNamePciKey(inventoryName, pci)
140 cell := &entities.Cell{}
141 err := w.getByKeyAndUnmarshal(key, cell)
148 func (w *rNibReaderInstance) GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, error) {
151 if cellType == entities.Cell_LTE_CELL {
152 key, rNibErr = common.ValidateAndBuildCellIdKey(cellId)
153 } else if cellType == entities.Cell_NR_CELL {
154 key, rNibErr = common.ValidateAndBuildNrCellIdKey(cellId)
156 return nil, common.NewValidationErrorf("#rNibReader.GetCellById - invalid cell type: %v", cellType)
161 cell := &entities.Cell{}
162 err := w.getByKeyAndUnmarshal(key, cell)
169 func (w *rNibReaderInstance) GetListNodebIds() ([]*entities.NbIdentity, error) {
170 dataEnb, err := w.sdl.GetMembers(entities.Node_ENB.String())
172 return nil, common.NewInternalError(err)
174 dataGnb, err := w.sdl.GetMembers(entities.Node_GNB.String())
176 return nil, common.NewInternalError(err)
178 dataUnknown, err := w.sdl.GetMembers(entities.Node_UNKNOWN.String())
180 return nil, common.NewInternalError(err)
182 allIds := append(dataEnb, dataGnb...)
183 allIds = append(allIds, dataUnknown...)
184 data, rnibErr := w.unmarshalIdentityList(allIds)
188 func (w *rNibReaderInstance) GetRanLoadInformation(inventoryName string) (*entities.RanLoadInformation, error) {
189 key, rNibErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
193 loadInfo := &entities.RanLoadInformation{}
194 err := w.getByKeyAndUnmarshal(key, loadInfo)
201 func (w *rNibReaderInstance) GetE2TInstance(address string) (*entities.E2TInstance, error) {
202 key, rNibErr := common.ValidateAndBuildE2TInstanceKey(address)
206 e2tInstance := &entities.E2TInstance{}
207 err := w.getByKeyAndUnmarshalJson(key, e2tInstance)
211 return e2tInstance, err
214 func (w *rNibReaderInstance) GetE2TInfoList() ([]*entities.E2TInstanceInfo, error) {
215 e2tInfoList := []*entities.E2TInstanceInfo{}
216 err := w.getByKeyAndUnmarshalJson(E2TInfoListKey, &e2tInfoList)
220 return e2tInfoList, err
223 func (w *rNibReaderInstance) getByKeyAndUnmarshalJson(key string, entity interface{}) error {
224 data, err := w.sdl.Get([]string{key})
227 return common.NewInternalError(err)
230 if data != nil && data[key] != nil {
231 err = json.Unmarshal([]byte(data[key].(string)), entity)
233 return common.NewInternalError(err)
237 return common.NewResourceNotFoundErrorf("#rNibReader.getByKeyAndUnmarshalJson - entity of type %s not found. Key: %s", reflect.TypeOf(entity).String(), key)
240 func (w *rNibReaderInstance) getByKeyAndUnmarshal(key string, entity proto.Message) error {
241 data, err := w.sdl.Get([]string{key})
243 return common.NewInternalError(err)
245 if data != nil && data[key] != nil {
246 err = proto.Unmarshal([]byte(data[key].(string)), entity)
248 return common.NewInternalError(err)
252 return common.NewResourceNotFoundErrorf("#rNibReader.getByKeyAndUnmarshal - entity of type %s not found. Key: %s", reflect.TypeOf(entity).String(), key)
255 func (w *rNibReaderInstance) getListNodebIdsByType(nbType string) ([]*entities.NbIdentity, error) {
256 data, err := w.sdl.GetMembers(nbType)
258 return nil, common.NewInternalError(err)
260 return w.unmarshalIdentityList(data)
263 func (w *rNibReaderInstance) unmarshalIdentityList(data []string) ([]*entities.NbIdentity, error) {
264 var members []*entities.NbIdentity
265 for _, d := range data {
266 member := entities.NbIdentity{}
267 err := proto.Unmarshal([]byte(d), &member)
269 return nil, common.NewInternalError(err)
271 members = append(members, &member)