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"
27 var readerPool *common.Pool
29 type rNibReaderInstance struct {
30 sdl *common.ISdlInstance
35 RNibReader interface allows retrieving data from redis BD by various keys
37 type RNibReader interface {
38 // GetNodeb retrieves responding nodeb entity from redis DB by nodeb inventory name
39 GetNodeb(inventoryName string) (*entities.NodebInfo, common.IRNibError)
40 // GetNodebByGlobalNbId retrieves responding nodeb entity from redis DB by nodeb global Id
41 GetNodebByGlobalNbId(nodeType entities.Node_Type, globalNbId *entities.GlobalNbId) (*entities.NodebInfo, common.IRNibError)
42 // GetCellList retrieves the list of cell entities belonging to responding nodeb entity from redis DB by nodeb inventory name
43 GetCellList(inventoryName string) (*entities.Cells, common.IRNibError)
44 // GetListGnbIds retrieves the list of gNodeb identity entities
45 GetListGnbIds() (*[]*entities.NbIdentity, common.IRNibError)
46 // GetListEnbIds retrieves the list of eNodeb identity entities
47 GetListEnbIds() (*[]*entities.NbIdentity, common.IRNibError)
48 // Close closes reader's pool
49 GetCountGnbList() (int, common.IRNibError)
50 // GetCell retrieves the cell entity belonging to responding nodeb from redis DB by nodeb inventory name and cell pci
51 GetCell(inventoryName string, pci uint32) (*entities.Cell, common.IRNibError)
52 // GetCellById retrieves the cell entity from redis DB by cell type and cell Id
53 GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, common.IRNibError)
54 // GetListNodebIds returns the full list of Nodeb identity entities
55 GetListNodebIds()([]*entities.NbIdentity, common.IRNibError)
64 Init initializes the infrastructure required for the RNibReader instance
66 func Init(namespace string, poolSize int) {
69 var sdlI common.ISdlInstance = sdlgo.NewSdlInstance(namespace, sdlgo.NewDatabase())
70 return &rNibReaderInstance{sdl: &sdlI, namespace: namespace}
72 func(obj interface{}) {
73 (*obj.(*rNibReaderInstance).sdl).Close()
77 func initPool(poolSize int, newObj func() interface{}, destroyObj func(interface{})) {
78 readerPool = common.NewPool(poolSize, newObj, destroyObj)
82 GetRNibReader returns RNibReader instance from the pool
84 func GetRNibReader() RNibReader {
85 return readerPool.Get().(RNibReader)
88 func (w *rNibReaderInstance) GetNodeb(inventoryName string) (*entities.NodebInfo, common.IRNibError) {
89 defer readerPool.Put(w)
90 key, rNibErr := common.ValidateAndBuildNodeBNameKey(inventoryName)
94 return w.getNodeb(key)
97 func (w *rNibReaderInstance) GetNodebByGlobalNbId(nodeType entities.Node_Type, globalNbId *entities.GlobalNbId) (*entities.NodebInfo, common.IRNibError) {
98 defer readerPool.Put(w)
99 key, rNibErr := common.ValidateAndBuildNodeBIdKey(nodeType.String(), globalNbId.GetPlmnId(), globalNbId.GetNbId())
103 return w.getNodeb(key)
106 func (w *rNibReaderInstance) GetCellList(inventoryName string) (*entities.Cells, common.IRNibError) {
107 cells := &entities.Cells{}
108 nb, err := w.GetNodeb(inventoryName)
112 if nb.GetEnb() != nil && len(nb.GetEnb().GetServedCells()) > 0 {
113 cells.Type = entities.Cell_LTE_CELL
114 cells.List = &entities.Cells_ServedCellInfos{ServedCellInfos: &entities.ServedCellInfoList{ServedCells: nb.GetEnb().GetServedCells()}}
116 } else if nb.GetGnb() != nil && len(nb.GetGnb().GetServedNrCells()) > 0 {
117 cells.Type = entities.Cell_NR_CELL
118 cells.List = &entities.Cells_ServedNrCells{ServedNrCells: &entities.ServedNRCellList{ServedCells: nb.GetGnb().GetServedNrCells()}}
121 return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.GetCellList - served cells not found. Responding node RAN name: %s.", inventoryName))
124 func (w *rNibReaderInstance) GetListGnbIds() (*[]*entities.NbIdentity, common.IRNibError) {
125 defer readerPool.Put(w)
126 return w.getListNodebIdsByType(GnbType)
129 func (w *rNibReaderInstance) GetListEnbIds() (*[]*entities.NbIdentity, common.IRNibError) {
130 defer readerPool.Put(w)
131 return w.getListNodebIdsByType(EnbType)
134 func (w *rNibReaderInstance) GetCountGnbList() (int, common.IRNibError) {
135 defer readerPool.Put(w)
136 size, err := (*w.sdl).GroupSize(GnbType)
138 return 0, common.NewInternalError(err)
140 return int(size), nil
143 func (w *rNibReaderInstance) GetCell(inventoryName string, pci uint32) (*entities.Cell, common.IRNibError) {
144 defer readerPool.Put(w)
145 key, rNibErr := common.ValidateAndBuildCellNamePciKey(inventoryName, pci)
149 return w.getCellByKey(key)
152 func (w *rNibReaderInstance) GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, common.IRNibError) {
153 defer readerPool.Put(w)
155 var rNibErr common.IRNibError
156 if cellType == entities.Cell_LTE_CELL {
157 key, rNibErr = common.ValidateAndBuildCellIdKey(cellId)
158 } else if cellType == entities.Cell_NR_CELL {
159 key, rNibErr = common.ValidateAndBuildNrCellIdKey(cellId)
161 return nil, common.NewValidationError(errors.Errorf("#rNibReader.GetCellById - invalid cell type: %v", cellType))
166 return w.getCellByKey(key)
169 func (w *rNibReaderInstance) GetListNodebIds()([]*entities.NbIdentity, common.IRNibError){
170 defer readerPool.Put(w)
171 dataEnb, err := (*w.sdl).GetMembers(EnbType)
173 return nil, common.NewInternalError(err)
175 dataGnb, err := (*w.sdl).GetMembers(GnbType)
177 return nil, common.NewInternalError(err)
179 data, rnibErr := unmarshalIdentityList(append(dataEnb, dataGnb...))
180 return *data, rnibErr
183 func (w *rNibReaderInstance) getNodeb(key string) (*entities.NodebInfo, common.IRNibError) {
184 data, err := (*w.sdl).Get([]string{key})
186 return nil, common.NewInternalError(err)
188 nb := entities.NodebInfo{}
189 if data != nil && data[key] != nil {
190 err = proto.Unmarshal([]byte(data[key].(string)), &nb)
192 return nil, common.NewInternalError(err)
196 return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.getNodeb - responding node not found. Key: %s", key))
199 func (w *rNibReaderInstance) getCellByKey(key string) (*entities.Cell, common.IRNibError) {
200 data, err := (*w.sdl).Get([]string{key})
202 return nil, common.NewInternalError(err)
204 cell := entities.Cell{}
205 if data != nil && data[key] != nil {
206 err = proto.Unmarshal([]byte(data[key].(string)), &cell)
208 return nil, common.NewInternalError(err)
212 return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.getCellByKey - cell not found, key: %s", key))
215 func (w *rNibReaderInstance) getListNodebIdsByType(nbType string) (*[]*entities.NbIdentity, common.IRNibError) {
216 data, err := (*w.sdl).GetMembers(nbType)
218 return nil, common.NewInternalError(err)
220 return unmarshalIdentityList(data)
223 func unmarshalIdentityList(data []string) (*[]*entities.NbIdentity, common.IRNibError) {
224 var members []*entities.NbIdentity
225 for _, d := range data {
226 member := entities.NbIdentity{}
227 err := proto.Unmarshal([]byte(d), &member)
229 return nil, common.NewInternalError(err)
231 members = append(members, &member)