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.
20 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
21 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
22 "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
23 "github.com/golang/protobuf/proto"
24 "github.com/pkg/errors"
28 var readerPool *common.Pool
30 type rNibReaderInstance struct {
31 sdl *common.ISdlInstance
36 RNibReader interface allows retrieving data from redis BD by various keys
38 type RNibReader interface {
39 // GetNodeb retrieves responding nodeb entity from redis DB by nodeb inventory name
40 GetNodeb(inventoryName string) (*entities.NodebInfo, common.IRNibError)
41 // GetNodebByGlobalNbId retrieves responding nodeb entity from redis DB by nodeb global Id
42 GetNodebByGlobalNbId(nodeType entities.Node_Type, globalNbId *entities.GlobalNbId) (*entities.NodebInfo, common.IRNibError)
43 // GetCellList retrieves the list of cell entities belonging to responding nodeb entity from redis DB by nodeb inventory name
44 GetCellList(inventoryName string) (*entities.Cells, common.IRNibError)
45 // GetListGnbIds retrieves the list of gNodeb identity entities
46 GetListGnbIds() (*[]*entities.NbIdentity, common.IRNibError)
47 // GetListEnbIds retrieves the list of eNodeb identity entities
48 GetListEnbIds() (*[]*entities.NbIdentity, common.IRNibError)
49 // Close closes reader's pool
50 GetCountGnbList() (int, common.IRNibError)
51 // GetCell retrieves the cell entity belonging to responding nodeb from redis DB by nodeb inventory name and cell pci
52 GetCell(inventoryName string, pci uint32) (*entities.Cell, common.IRNibError)
53 // GetCellById retrieves the cell entity from redis DB by cell type and cell Id
54 GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, common.IRNibError)
55 // GetListNodebIds returns the full list of Nodeb identity entities
56 GetListNodebIds()([]*entities.NbIdentity, common.IRNibError)
57 // GetRanLoadInformation retrieves nodeb load information entity from redis DB by nodeb inventory name
58 GetRanLoadInformation(inventoryName string) (*entities.RanLoadInformation, common.IRNibError)
62 Init initializes the infrastructure required for the RNibReader instance
64 func Init(namespace string, poolSize int) {
67 var sdlI common.ISdlInstance = sdlgo.NewSdlInstance(namespace, sdlgo.NewDatabase())
68 return &rNibReaderInstance{sdl: &sdlI, namespace: namespace}
70 func(obj interface{}) {
71 (*obj.(*rNibReaderInstance).sdl).Close()
75 func initPool(poolSize int, newObj func() interface{}, destroyObj func(interface{})) {
76 readerPool = common.NewPool(poolSize, newObj, destroyObj)
80 GetRNibReader returns RNibReader instance from the pool
82 func GetRNibReader() RNibReader {
83 return readerPool.Get().(RNibReader)
86 func (w *rNibReaderInstance) GetNodeb(inventoryName string) (*entities.NodebInfo, common.IRNibError) {
87 defer readerPool.Put(w)
88 key, rNibErr := common.ValidateAndBuildNodeBNameKey(inventoryName)
92 return w.getNodeb(key)
95 func (w *rNibReaderInstance) GetNodebByGlobalNbId(nodeType entities.Node_Type, globalNbId *entities.GlobalNbId) (*entities.NodebInfo, common.IRNibError) {
96 defer readerPool.Put(w)
97 key, rNibErr := common.ValidateAndBuildNodeBIdKey(nodeType.String(), globalNbId.GetPlmnId(), globalNbId.GetNbId())
101 return w.getNodeb(key)
104 func (w *rNibReaderInstance) GetCellList(inventoryName string) (*entities.Cells, common.IRNibError) {
105 cells := &entities.Cells{}
106 nb, err := w.GetNodeb(inventoryName)
110 if nb.GetEnb() != nil && len(nb.GetEnb().GetServedCells()) > 0 {
111 cells.Type = entities.Cell_LTE_CELL
112 cells.List = &entities.Cells_ServedCellInfos{ServedCellInfos: &entities.ServedCellInfoList{ServedCells: nb.GetEnb().GetServedCells()}}
114 } else if nb.GetGnb() != nil && len(nb.GetGnb().GetServedNrCells()) > 0 {
115 cells.Type = entities.Cell_NR_CELL
116 cells.List = &entities.Cells_ServedNrCells{ServedNrCells: &entities.ServedNRCellList{ServedCells: nb.GetGnb().GetServedNrCells()}}
119 return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.GetCellList - served cells not found. Responding node RAN name: %s.", inventoryName))
122 func (w *rNibReaderInstance) GetListGnbIds() (*[]*entities.NbIdentity, common.IRNibError) {
123 defer readerPool.Put(w)
124 return w.getListNodebIdsByType(entities.Node_GNB.String())
127 func (w *rNibReaderInstance) GetListEnbIds() (*[]*entities.NbIdentity, common.IRNibError) {
128 defer readerPool.Put(w)
129 return w.getListNodebIdsByType(entities.Node_ENB.String())
132 func (w *rNibReaderInstance) GetCountGnbList() (int, common.IRNibError) {
133 defer readerPool.Put(w)
134 size, err := (*w.sdl).GroupSize(entities.Node_GNB.String())
136 return 0, common.NewInternalError(err)
138 return int(size), nil
141 func (w *rNibReaderInstance) GetCell(inventoryName string, pci uint32) (*entities.Cell, common.IRNibError) {
142 defer readerPool.Put(w)
143 key, rNibErr := common.ValidateAndBuildCellNamePciKey(inventoryName, pci)
147 return w.getCellByKey(key)
150 func (w *rNibReaderInstance) GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, common.IRNibError) {
151 defer readerPool.Put(w)
153 var rNibErr common.IRNibError
154 if cellType == entities.Cell_LTE_CELL {
155 key, rNibErr = common.ValidateAndBuildCellIdKey(cellId)
156 } else if cellType == entities.Cell_NR_CELL {
157 key, rNibErr = common.ValidateAndBuildNrCellIdKey(cellId)
159 return nil, common.NewValidationError(errors.Errorf("#rNibReader.GetCellById - invalid cell type: %v", cellType))
164 return w.getCellByKey(key)
167 func (w *rNibReaderInstance) GetListNodebIds()([]*entities.NbIdentity, common.IRNibError){
168 defer readerPool.Put(w)
169 dataEnb, err := (*w.sdl).GetMembers(entities.Node_ENB.String())
171 return nil, common.NewInternalError(err)
173 dataGnb, err := (*w.sdl).GetMembers(entities.Node_GNB.String())
175 return nil, common.NewInternalError(err)
177 dataUnknown, err := (*w.sdl).GetMembers(entities.Node_UNKNOWN.String())
179 return nil, common.NewInternalError(err)
181 allIds := append(dataEnb, dataGnb...)
182 allIds = append(allIds, dataUnknown...)
183 data, rnibErr := unmarshalIdentityList(allIds)
184 return *data, rnibErr
187 func (w *rNibReaderInstance) GetRanLoadInformation(inventoryName string) (*entities.RanLoadInformation, common.IRNibError){
188 key, rNibErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
192 loadInfo := &entities.RanLoadInformation{}
193 err := w.getByKeyAndUnmarshal(key, loadInfo)
200 func (w *rNibReaderInstance) getByKeyAndUnmarshal(key string, entity proto.Message)common.IRNibError{
201 data, err := (*w.sdl).Get([]string{key})
203 return common.NewInternalError(err)
205 if data != nil && data[key] != nil {
206 err = proto.Unmarshal([]byte(data[key].(string)), entity)
208 return common.NewInternalError(err)
212 return common.NewResourceNotFoundError(errors.Errorf("#rNibReader.getByKeyAndUnmarshal - entity of type %s not found. Key: %s", reflect.TypeOf(entity).String(), key))
215 func (w *rNibReaderInstance) getNodeb(key string) (*entities.NodebInfo, common.IRNibError) {
216 data, err := (*w.sdl).Get([]string{key})
218 return nil, common.NewInternalError(err)
220 nb := entities.NodebInfo{}
221 if data != nil && data[key] != nil {
222 err = proto.Unmarshal([]byte(data[key].(string)), &nb)
224 return nil, common.NewInternalError(err)
228 return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.getNodeb - responding node not found. Key: %s", key))
231 func (w *rNibReaderInstance) getCellByKey(key string) (*entities.Cell, common.IRNibError) {
232 data, err := (*w.sdl).Get([]string{key})
234 return nil, common.NewInternalError(err)
236 cell := entities.Cell{}
237 if data != nil && data[key] != nil {
238 err = proto.Unmarshal([]byte(data[key].(string)), &cell)
240 return nil, common.NewInternalError(err)
244 return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.getCellByKey - cell not found, key: %s", key))
247 func (w *rNibReaderInstance) getListNodebIdsByType(nbType string) (*[]*entities.NbIdentity, common.IRNibError) {
248 data, err := (*w.sdl).GetMembers(nbType)
250 return nil, common.NewInternalError(err)
252 return unmarshalIdentityList(data)
255 func unmarshalIdentityList(data []string) (*[]*entities.NbIdentity, common.IRNibError) {
256 var members []*entities.NbIdentity
257 for _, d := range data {
258 member := entities.NbIdentity{}
259 err := proto.Unmarshal([]byte(d), &member)
261 return nil, common.NewInternalError(err)
263 members = append(members, &member)