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)
67 Init initializes the infrastructure required for the RNibReader instance
69 func Init(namespace string, poolSize int) {
72 var sdlI common.ISdlInstance = sdlgo.NewSdlInstance(namespace, sdlgo.NewDatabase())
73 return &rNibReaderInstance{sdl: &sdlI, namespace: namespace}
75 func(obj interface{}) {
76 (*obj.(*rNibReaderInstance).sdl).Close()
80 func initPool(poolSize int, newObj func() interface{}, destroyObj func(interface{})) {
81 readerPool = common.NewPool(poolSize, newObj, destroyObj)
85 GetRNibReader returns RNibReader instance from the pool
87 func GetRNibReader() RNibReader {
88 return readerPool.Get().(RNibReader)
91 func (w *rNibReaderInstance) GetNodeb(inventoryName string) (*entities.NodebInfo, common.IRNibError) {
92 defer readerPool.Put(w)
93 key, rNibErr := common.ValidateAndBuildNodeBNameKey(inventoryName)
97 return w.getNodeb(key)
100 func (w *rNibReaderInstance) GetNodebByGlobalNbId(nodeType entities.Node_Type, globalNbId *entities.GlobalNbId) (*entities.NodebInfo, common.IRNibError) {
101 defer readerPool.Put(w)
102 key, rNibErr := common.ValidateAndBuildNodeBIdKey(nodeType.String(), globalNbId.GetPlmnId(), globalNbId.GetNbId())
106 return w.getNodeb(key)
109 func (w *rNibReaderInstance) GetCellList(inventoryName string) (*entities.Cells, common.IRNibError) {
110 cells := &entities.Cells{}
111 nb, err := w.GetNodeb(inventoryName)
115 if nb.GetEnb() != nil && len(nb.GetEnb().GetServedCells()) > 0 {
116 cells.Type = entities.Cell_LTE_CELL
117 cells.List = &entities.Cells_ServedCellInfos{ServedCellInfos: &entities.ServedCellInfoList{ServedCells: nb.GetEnb().GetServedCells()}}
119 } else if nb.GetGnb() != nil && len(nb.GetGnb().GetServedNrCells()) > 0 {
120 cells.Type = entities.Cell_NR_CELL
121 cells.List = &entities.Cells_ServedNrCells{ServedNrCells: &entities.ServedNRCellList{ServedCells: nb.GetGnb().GetServedNrCells()}}
124 return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.GetCellList - served cells not found. Responding node RAN name: %s.", inventoryName))
127 func (w *rNibReaderInstance) GetListGnbIds() (*[]*entities.NbIdentity, common.IRNibError) {
128 defer readerPool.Put(w)
129 return w.getListNodebIdsByType(GnbType)
132 func (w *rNibReaderInstance) GetListEnbIds() (*[]*entities.NbIdentity, common.IRNibError) {
133 defer readerPool.Put(w)
134 return w.getListNodebIdsByType(EnbType)
137 func (w *rNibReaderInstance) GetCountGnbList() (int, common.IRNibError) {
138 defer readerPool.Put(w)
139 size, err := (*w.sdl).GroupSize(GnbType)
141 return 0, common.NewInternalError(err)
143 return int(size), nil
146 func (w *rNibReaderInstance) GetCell(inventoryName string, pci uint32) (*entities.Cell, common.IRNibError) {
147 defer readerPool.Put(w)
148 key, rNibErr := common.ValidateAndBuildCellNamePciKey(inventoryName, pci)
152 return w.getCellByKey(key)
155 func (w *rNibReaderInstance) GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, common.IRNibError) {
156 defer readerPool.Put(w)
158 var rNibErr common.IRNibError
159 if cellType == entities.Cell_LTE_CELL {
160 key, rNibErr = common.ValidateAndBuildCellIdKey(cellId)
161 } else if cellType == entities.Cell_NR_CELL {
162 key, rNibErr = common.ValidateAndBuildNrCellIdKey(cellId)
164 return nil, common.NewValidationError(errors.Errorf("#rNibReader.GetCellById - invalid cell type: %v", cellType))
169 return w.getCellByKey(key)
172 func (w *rNibReaderInstance) GetListNodebIds()([]*entities.NbIdentity, common.IRNibError){
173 defer readerPool.Put(w)
174 dataEnb, err := (*w.sdl).GetMembers(EnbType)
176 return nil, common.NewInternalError(err)
178 dataGnb, err := (*w.sdl).GetMembers(GnbType)
180 return nil, common.NewInternalError(err)
182 data, rnibErr := unmarshalIdentityList(append(dataEnb, dataGnb...))
183 return *data, rnibErr
186 func (w *rNibReaderInstance) GetRanLoadInformation(inventoryName string) (*entities.RanLoadInformation, common.IRNibError){
187 key, rNibErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
191 loadInfo := &entities.RanLoadInformation{}
192 err := w.getByKeyAndUnmarshal(key, loadInfo)
199 func (w *rNibReaderInstance) getByKeyAndUnmarshal(key string, entity proto.Message)common.IRNibError{
200 data, err := (*w.sdl).Get([]string{key})
202 return common.NewInternalError(err)
204 if data != nil && data[key] != nil {
205 err = proto.Unmarshal([]byte(data[key].(string)), entity)
207 return common.NewInternalError(err)
211 return common.NewResourceNotFoundError(errors.Errorf("#rNibReader.getByKeyAndUnmarshal - entity of type %s not found. Key: %s", reflect.TypeOf(entity).String(), key))
214 func (w *rNibReaderInstance) getNodeb(key string) (*entities.NodebInfo, common.IRNibError) {
215 data, err := (*w.sdl).Get([]string{key})
217 return nil, common.NewInternalError(err)
219 nb := entities.NodebInfo{}
220 if data != nil && data[key] != nil {
221 err = proto.Unmarshal([]byte(data[key].(string)), &nb)
223 return nil, common.NewInternalError(err)
227 return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.getNodeb - responding node not found. Key: %s", key))
230 func (w *rNibReaderInstance) getCellByKey(key string) (*entities.Cell, common.IRNibError) {
231 data, err := (*w.sdl).Get([]string{key})
233 return nil, common.NewInternalError(err)
235 cell := entities.Cell{}
236 if data != nil && data[key] != nil {
237 err = proto.Unmarshal([]byte(data[key].(string)), &cell)
239 return nil, common.NewInternalError(err)
243 return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.getCellByKey - cell not found, key: %s", key))
246 func (w *rNibReaderInstance) getListNodebIdsByType(nbType string) (*[]*entities.NbIdentity, common.IRNibError) {
247 data, err := (*w.sdl).GetMembers(nbType)
249 return nil, common.NewInternalError(err)
251 return unmarshalIdentityList(data)
254 func unmarshalIdentityList(data []string) (*[]*entities.NbIdentity, common.IRNibError) {
255 var members []*entities.NbIdentity
256 for _, d := range data {
257 member := entities.NbIdentity{}
258 err := proto.Unmarshal([]byte(d), &member)
260 return nil, common.NewInternalError(err)
262 members = append(members, &member)