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.
21 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
22 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
23 "github.com/golang/protobuf/proto"
27 const E2TInfoListKey = "E2TInfoList"
29 type rNibReaderInstance struct {
30 sdl common.ISdlInstance
34 RNibReader interface allows retrieving data from redis BD by various keys
36 type RNibReader interface {
37 // GetNodeb retrieves responding nodeb entity from redis DB by nodeb inventory name
38 GetNodeb(inventoryName string) (*entities.NodebInfo, error)
39 // GetNodebByGlobalNbId retrieves responding nodeb entity from redis DB by nodeb global Id
40 GetNodebByGlobalNbId(nodeType entities.Node_Type, globalNbId *entities.GlobalNbId) (*entities.NodebInfo, error)
41 // GetCellList retrieves the list of cell entities belonging to responding nodeb entity from redis DB by nodeb inventory name
42 GetCellList(inventoryName string) (*entities.Cells, error)
43 // GetListGnbIds retrieves the list of gNodeb identity entities
44 GetListGnbIds() ([]*entities.NbIdentity, error)
45 // GetListEnbIds retrieves the list of eNodeb identity entities
46 GetListEnbIds() ([]*entities.NbIdentity, error)
47 // Close closes reader's pool
48 GetCountGnbList() (int, error)
49 // GetCell retrieves the cell entity belonging to responding nodeb from redis DB by nodeb inventory name and cell pci
50 GetCell(inventoryName string, pci uint32) (*entities.Cell, error)
51 // GetCellById retrieves the cell entity from redis DB by cell type and cell Id
52 GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, error)
53 // GetListNodebIds returns the full list of Nodeb identity entities
54 GetListNodebIds() ([]*entities.NbIdentity, error)
55 // GetRanLoadInformation retrieves nodeb load information entity from redis DB by nodeb inventory name
56 GetRanLoadInformation(inventoryName string) (*entities.RanLoadInformation, error)
58 GetE2TInstance(address string) (*entities.E2TInstance, error)
60 GetE2TInfoList() ([]*entities.E2TInstanceInfo, error)
65 GetRNibReader returns reference to RNibReader
67 func GetRNibReader(sdl common.ISdlInstance) RNibReader {
68 return &rNibReaderInstance{sdl: sdl}
71 func (w *rNibReaderInstance) GetNodeb(inventoryName string) (*entities.NodebInfo, error) {
72 key, rNibErr := common.ValidateAndBuildNodeBNameKey(inventoryName)
76 nbInfo := &entities.NodebInfo{}
77 err := w.getByKeyAndUnmarshal(key, nbInfo)
84 func (w *rNibReaderInstance) GetNodebByGlobalNbId(nodeType entities.Node_Type, globalNbId *entities.GlobalNbId) (*entities.NodebInfo, error) {
85 key, rNibErr := common.ValidateAndBuildNodeBIdKey(nodeType.String(), globalNbId.GetPlmnId(), globalNbId.GetNbId())
89 nbInfo := &entities.NodebInfo{}
90 err := w.getByKeyAndUnmarshal(key, nbInfo)
97 func (w *rNibReaderInstance) GetCellList(inventoryName string) (*entities.Cells, error) {
98 cells := &entities.Cells{}
99 nb, err := w.GetNodeb(inventoryName)
103 if nb.GetEnb() != nil && len(nb.GetEnb().GetServedCells()) > 0 {
104 cells.Type = entities.Cell_LTE_CELL
105 cells.List = &entities.Cells_ServedCellInfos{ServedCellInfos: &entities.ServedCellInfoList{ServedCells: nb.GetEnb().GetServedCells()}}
108 if nb.GetGnb() != nil && len(nb.GetGnb().GetServedNrCells()) > 0 {
109 cells.Type = entities.Cell_NR_CELL
110 cells.List = &entities.Cells_ServedNrCells{ServedNrCells: &entities.ServedNRCellList{ServedCells: nb.GetGnb().GetServedNrCells()}}
113 return nil, common.NewResourceNotFoundErrorf("#rNibReader.GetCellList - served cells not found. Responding node RAN name: %s.", inventoryName)
116 func (w *rNibReaderInstance) GetListGnbIds() ([]*entities.NbIdentity, error) {
117 return w.getListNodebIdsByType(entities.Node_GNB.String())
120 func (w *rNibReaderInstance) GetListEnbIds() ([]*entities.NbIdentity, error) {
121 return w.getListNodebIdsByType(entities.Node_ENB.String())
124 func (w *rNibReaderInstance) GetCountGnbList() (int, error) {
125 size, err := w.sdl.GroupSize(entities.Node_GNB.String())
127 return 0, common.NewInternalError(err)
129 return int(size), nil
132 func (w *rNibReaderInstance) GetCell(inventoryName string, pci uint32) (*entities.Cell, error) {
133 key, rNibErr := common.ValidateAndBuildCellNamePciKey(inventoryName, pci)
137 cell := &entities.Cell{}
138 err := w.getByKeyAndUnmarshal(key, cell)
145 func (w *rNibReaderInstance) GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, error) {
148 if cellType == entities.Cell_LTE_CELL {
149 key, rNibErr = common.ValidateAndBuildCellIdKey(cellId)
150 } else if cellType == entities.Cell_NR_CELL {
151 key, rNibErr = common.ValidateAndBuildNrCellIdKey(cellId)
153 return nil, common.NewValidationErrorf("#rNibReader.GetCellById - invalid cell type: %v", cellType)
158 cell := &entities.Cell{}
159 err := w.getByKeyAndUnmarshal(key, cell)
166 func (w *rNibReaderInstance) GetListNodebIds() ([]*entities.NbIdentity, error) {
167 dataEnb, err := w.sdl.GetMembers(entities.Node_ENB.String())
169 return nil, common.NewInternalError(err)
171 dataGnb, err := w.sdl.GetMembers(entities.Node_GNB.String())
173 return nil, common.NewInternalError(err)
175 dataUnknown, err := w.sdl.GetMembers(entities.Node_UNKNOWN.String())
177 return nil, common.NewInternalError(err)
179 allIds := append(dataEnb, dataGnb...)
180 allIds = append(allIds, dataUnknown...)
181 data, rnibErr := w.unmarshalIdentityList(allIds)
185 func (w *rNibReaderInstance) GetRanLoadInformation(inventoryName string) (*entities.RanLoadInformation, error) {
186 key, rNibErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
190 loadInfo := &entities.RanLoadInformation{}
191 err := w.getByKeyAndUnmarshal(key, loadInfo)
198 func (w *rNibReaderInstance) GetE2TInstance(address string) (*entities.E2TInstance, error) {
199 key, rNibErr := common.ValidateAndBuildE2TInstanceKey(address)
203 e2tInstance := &entities.E2TInstance{}
204 err := w.getByKeyAndUnmarshalJson(key, e2tInstance)
208 return e2tInstance, err
211 func (w *rNibReaderInstance) GetE2TInfoList() ([]*entities.E2TInstanceInfo, error) {
212 e2tInfoList := []*entities.E2TInstanceInfo{}
213 err := w.getByKeyAndUnmarshalJson(E2TInfoListKey, &e2tInfoList)
217 return e2tInfoList, err
220 func (w *rNibReaderInstance) getByKeyAndUnmarshalJson(key string, entity interface{}) error {
221 data, err := w.sdl.Get([]string{key})
224 return common.NewInternalError(err)
227 if data != nil && data[key] != nil {
228 err = json.Unmarshal([]byte(data[key].(string)), entity)
230 return common.NewInternalError(err)
234 return common.NewResourceNotFoundErrorf("#rNibReader.getByKeyAndUnmarshalJson - entity of type %s not found. Key: %s", reflect.TypeOf(entity).String(), key)
237 func (w *rNibReaderInstance) getByKeyAndUnmarshal(key string, entity proto.Message) error {
238 data, err := w.sdl.Get([]string{key})
240 return common.NewInternalError(err)
242 if data != nil && data[key] != nil {
243 err = proto.Unmarshal([]byte(data[key].(string)), entity)
245 return common.NewInternalError(err)
249 return common.NewResourceNotFoundErrorf("#rNibReader.getByKeyAndUnmarshal - entity of type %s not found. Key: %s", reflect.TypeOf(entity).String(), key)
252 func (w *rNibReaderInstance) getListNodebIdsByType(nbType string) ([]*entities.NbIdentity, error) {
253 data, err := w.sdl.GetMembers(nbType)
255 return nil, common.NewInternalError(err)
257 return w.unmarshalIdentityList(data)
260 func (w *rNibReaderInstance) unmarshalIdentityList(data []string) ([]*entities.NbIdentity, error) {
261 var members []*entities.NbIdentity
262 for _, d := range data {
263 member := entities.NbIdentity{}
264 err := proto.Unmarshal([]byte(d), &member)
266 return nil, common.NewInternalError(err)
268 members = append(members, &member)