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)
57 Init initializes the infrastructure required for the RNibReader instance
59 func Init(namespace string, poolSize int) {
62 var sdlI common.ISdlInstance = sdlgo.NewSdlInstance(namespace, sdlgo.NewDatabase())
63 return &rNibReaderInstance{sdl: &sdlI, namespace: namespace}
65 func(obj interface{}) {
66 (*obj.(*rNibReaderInstance).sdl).Close()
70 func initPool(poolSize int, newObj func() interface{}, destroyObj func(interface{})) {
71 readerPool = common.NewPool(poolSize, newObj, destroyObj)
75 GetRNibReader returns RNibReader instance from the pool
77 func GetRNibReader() RNibReader {
78 return readerPool.Get().(RNibReader)
81 func (w *rNibReaderInstance) GetNodeb(inventoryName string) (*entities.NodebInfo, common.IRNibError) {
82 name, rNibErr := common.ValidateAndBuildNodeBNameKey(inventoryName)
86 defer readerPool.Put(w)
87 data, err := (*w.sdl).Get([]string{name})
89 return nil, common.NewInternalError(err)
91 nb := entities.NodebInfo{}
92 if data != nil && data[name] != nil {
93 err = proto.Unmarshal([]byte(data[name].(string)), &nb)
95 return nil, common.NewInternalError(err)
99 return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.GetNodeb - responding node %s not found", inventoryName))
102 func (w *rNibReaderInstance) GetNodebByGlobalNbId(nodeType entities.Node_Type, globalNbId *entities.GlobalNbId) (*entities.NodebInfo, common.IRNibError) {
103 key, rNibErr := common.ValidateAndBuildNodeBIdKey(nodeType.String(), globalNbId.GetPlmnId(), globalNbId.GetNbId())
107 defer readerPool.Put(w)
108 data, err := (*w.sdl).Get([]string{key})
110 return nil, common.NewInternalError(err)
112 nb := entities.NodebInfo{}
113 if data != nil && data[key] != nil {
114 err = proto.Unmarshal([]byte(data[key].(string)), &nb)
116 return nil, common.NewInternalError(err)
120 return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.GetNodebByGlobalNbId - responding node not found, global nodeb Id: %s", key))
123 func (w *rNibReaderInstance) GetCellList(inventoryName string) (*entities.Cells, common.IRNibError) {
124 cells := &entities.Cells{}
125 nb, err := w.GetNodeb(inventoryName)
129 if nb.GetEnb() != nil && len(nb.GetEnb().GetServedCells()) > 0 {
130 cells.Type = entities.Cell_LTE_CELL
131 cells.List = &entities.Cells_ServedCellInfos{ServedCellInfos: &entities.ServedCellInfoList{ServedCells: nb.GetEnb().GetServedCells()}}
133 } else if nb.GetGnb() != nil && len(nb.GetGnb().GetServedNrCells()) > 0 {
134 cells.Type = entities.Cell_NR_CELL
135 cells.List = &entities.Cells_ServedNrCells{ServedNrCells: &entities.ServedNRCellList{ServedCells: nb.GetGnb().GetServedNrCells()}}
138 return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.GetCellList - served cells not found. Responding node RAN name: %s.", inventoryName))
141 func (w *rNibReaderInstance) GetListGnbIds() (*[]*entities.NbIdentity, common.IRNibError) {
142 defer readerPool.Put(w)
143 data, err := (*w.sdl).GetMembers("GNB")
145 return nil, common.NewInternalError(err)
147 return unmarshalIdentityList(data)
150 func (w *rNibReaderInstance) GetListEnbIds() (*[]*entities.NbIdentity, common.IRNibError) {
151 defer readerPool.Put(w)
152 data, err := (*w.sdl).GetMembers("ENB")
154 return nil, common.NewInternalError(err)
156 return unmarshalIdentityList(data)
159 func (w *rNibReaderInstance) GetCountGnbList() (int, common.IRNibError) {
160 defer readerPool.Put(w)
161 data, err := (*w.sdl).GetMembers("GNB")
163 return 0, common.NewInternalError(err)
165 return len(data), nil
168 func (w *rNibReaderInstance) GetCell(inventoryName string, pci uint32) (*entities.Cell, common.IRNibError) {
169 key, rNibErr := common.ValidateAndBuildCellNamePciKey(inventoryName, pci)
173 return (*w).getCellByKey(key)
176 func (w *rNibReaderInstance) GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, common.IRNibError) {
178 var rNibErr common.IRNibError
179 if cellType == entities.Cell_LTE_CELL {
180 key, rNibErr = common.ValidateAndBuildCellIdKey(cellId)
181 } else if cellType == entities.Cell_NR_CELL {
182 key, rNibErr = common.ValidateAndBuildNrCellIdKey(cellId)
184 return nil, common.NewValidationError(errors.Errorf("#rNibReader.GetCellById - invalid cell type: %v", cellType))
189 return (*w).getCellByKey(key)
192 func (w *rNibReaderInstance) getCellByKey(key string) (*entities.Cell, common.IRNibError) {
193 defer readerPool.Put(w)
194 data, err := (*w.sdl).Get([]string{key})
196 return nil, common.NewInternalError(err)
198 cell := entities.Cell{}
199 if data != nil && data[key] != nil {
200 err = proto.Unmarshal([]byte(data[key].(string)), &cell)
202 return nil, common.NewInternalError(err)
206 return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.getCellByKey - cell not found, key: %s", key))
209 func unmarshalIdentityList(data []string) (*[]*entities.NbIdentity, common.IRNibError) {
210 var members []*entities.NbIdentity
211 for _, d := range data {
212 member := entities.NbIdentity{}
213 err := proto.Unmarshal([]byte(d), &member)
215 return nil, common.NewInternalError(err)
217 members = append(members, &member)