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
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 GetE2TInstances(addresses []string) ([]*entities.E2TInstance, error)
65 GetE2TAddresses() ([]string, error)
69 GetRNibReader returns reference to RNibReader
71 func GetRNibReader(sdl common.ISdlInstance) RNibReader {
72 return &rNibReaderInstance{sdl: sdl}
75 func (w *rNibReaderInstance) GetNodeb(inventoryName string) (*entities.NodebInfo, error) {
76 key, rNibErr := common.ValidateAndBuildNodeBNameKey(inventoryName)
80 nbInfo := &entities.NodebInfo{}
81 err := w.getByKeyAndUnmarshal(key, nbInfo)
88 func (w *rNibReaderInstance) GetNodebByGlobalNbId(nodeType entities.Node_Type, globalNbId *entities.GlobalNbId) (*entities.NodebInfo, error) {
89 key, rNibErr := common.ValidateAndBuildNodeBIdKey(nodeType.String(), globalNbId.GetPlmnId(), globalNbId.GetNbId())
93 nbInfo := &entities.NodebInfo{}
94 err := w.getByKeyAndUnmarshal(key, nbInfo)
101 func (w *rNibReaderInstance) GetCellList(inventoryName string) (*entities.Cells, error) {
102 cells := &entities.Cells{}
103 nb, err := w.GetNodeb(inventoryName)
107 if nb.GetEnb() != nil && len(nb.GetEnb().GetServedCells()) > 0 {
108 cells.Type = entities.Cell_LTE_CELL
109 cells.List = &entities.Cells_ServedCellInfos{ServedCellInfos: &entities.ServedCellInfoList{ServedCells: nb.GetEnb().GetServedCells()}}
112 if nb.GetGnb() != nil && len(nb.GetGnb().GetServedNrCells()) > 0 {
113 cells.Type = entities.Cell_NR_CELL
114 cells.List = &entities.Cells_ServedNrCells{ServedNrCells: &entities.ServedNRCellList{ServedCells: nb.GetGnb().GetServedNrCells()}}
117 return nil, common.NewResourceNotFoundErrorf("#rNibReader.GetCellList - served cells not found. Responding node RAN name: %s.", inventoryName)
120 func (w *rNibReaderInstance) GetListGnbIds() ([]*entities.NbIdentity, error) {
121 return w.getListNodebIdsByType(entities.Node_GNB.String())
124 func (w *rNibReaderInstance) GetListEnbIds() ([]*entities.NbIdentity, error) {
125 return w.getListNodebIdsByType(entities.Node_ENB.String())
128 func (w *rNibReaderInstance) GetCountGnbList() (int, error) {
129 size, err := w.sdl.GroupSize(entities.Node_GNB.String())
131 return 0, common.NewInternalError(err)
133 return int(size), nil
136 func (w *rNibReaderInstance) GetCell(inventoryName string, pci uint32) (*entities.Cell, error) {
137 key, rNibErr := common.ValidateAndBuildCellNamePciKey(inventoryName, pci)
141 cell := &entities.Cell{}
142 err := w.getByKeyAndUnmarshal(key, cell)
149 func (w *rNibReaderInstance) GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, error) {
152 if cellType == entities.Cell_LTE_CELL {
153 key, rNibErr = common.ValidateAndBuildCellIdKey(cellId)
154 } else if cellType == entities.Cell_NR_CELL {
155 key, rNibErr = common.ValidateAndBuildNrCellIdKey(cellId)
157 return nil, common.NewValidationErrorf("#rNibReader.GetCellById - invalid cell type: %v", cellType)
162 cell := &entities.Cell{}
163 err := w.getByKeyAndUnmarshal(key, cell)
170 func (w *rNibReaderInstance) GetListNodebIds() ([]*entities.NbIdentity, error) {
171 dataEnb, err := w.sdl.GetMembers(entities.Node_ENB.String())
173 return nil, common.NewInternalError(err)
175 dataGnb, err := w.sdl.GetMembers(entities.Node_GNB.String())
177 return nil, common.NewInternalError(err)
179 dataUnknown, err := w.sdl.GetMembers(entities.Node_UNKNOWN.String())
181 return nil, common.NewInternalError(err)
183 allIds := append(dataEnb, dataGnb...)
184 allIds = append(allIds, dataUnknown...)
185 data, rnibErr := w.unmarshalIdentityList(allIds)
189 func (w *rNibReaderInstance) GetRanLoadInformation(inventoryName string) (*entities.RanLoadInformation, error) {
190 key, rNibErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
194 loadInfo := &entities.RanLoadInformation{}
195 err := w.getByKeyAndUnmarshal(key, loadInfo)
202 func (w *rNibReaderInstance) GetE2TInstance(address string) (*entities.E2TInstance, error) {
203 key, rNibErr := common.ValidateAndBuildE2TInstanceKey(address)
207 e2tInstance := &entities.E2TInstance{}
208 err := w.getByKeyAndUnmarshalJson(key, e2tInstance)
212 return e2tInstance, err
215 func (w *rNibReaderInstance) GetE2TInstances(addresses []string) ([]*entities.E2TInstance, error) {
216 keys := common.MapE2TAddressesToKeys(addresses)
218 e2tInstances := []*entities.E2TInstance{}
220 data, err := w.sdl.Get(keys)
223 return []*entities.E2TInstance{}, common.NewInternalError(err)
227 return []*entities.E2TInstance{}, common.NewResourceNotFoundErrorf("#rNibReader.GetE2TInstances - e2t instances not found")
230 for _, v := range keys {
233 var e2tInstance entities.E2TInstance
234 err = json.Unmarshal([]byte(data[v].(string)), &e2tInstance)
239 e2tInstances = append(e2tInstances, &e2tInstance)
243 return e2tInstances, nil
246 func (w *rNibReaderInstance) GetE2TAddresses() ([]string, error) {
247 var e2tAddresses []string
248 err := w.getByKeyAndUnmarshalJson(E2TAddressesKey, &e2tAddresses)
252 return e2tAddresses, err
255 func (w *rNibReaderInstance) getByKeyAndUnmarshalJson(key string, entity interface{}) error {
256 data, err := w.sdl.Get([]string{key})
259 return common.NewInternalError(err)
262 if data != nil && data[key] != nil {
263 err = json.Unmarshal([]byte(data[key].(string)), entity)
265 return common.NewInternalError(err)
269 return common.NewResourceNotFoundErrorf("#rNibReader.getByKeyAndUnmarshalJson - entity of type %s not found. Key: %s", reflect.TypeOf(entity).String(), key)
272 func (w *rNibReaderInstance) getByKeyAndUnmarshal(key string, entity proto.Message) error {
273 data, err := w.sdl.Get([]string{key})
275 return common.NewInternalError(err)
277 if data != nil && data[key] != nil {
278 err = proto.Unmarshal([]byte(data[key].(string)), entity)
280 return common.NewInternalError(err)
284 return common.NewResourceNotFoundErrorf("#rNibReader.getByKeyAndUnmarshal - entity of type %s not found. Key: %s", reflect.TypeOf(entity).String(), key)
287 func (w *rNibReaderInstance) getListNodebIdsByType(nbType string) ([]*entities.NbIdentity, error) {
288 data, err := w.sdl.GetMembers(nbType)
290 return nil, common.NewInternalError(err)
292 return w.unmarshalIdentityList(data)
295 func (w *rNibReaderInstance) unmarshalIdentityList(data []string) ([]*entities.NbIdentity, error) {
296 var members []*entities.NbIdentity
297 for _, d := range data {
298 member := entities.NbIdentity{}
299 err := proto.Unmarshal([]byte(d), &member)
301 return nil, common.NewInternalError(err)
303 members = append(members, &member)