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/entities"
21 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
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)
56 Init initializes the infrastructure required for the RNibReader instance
58 func Init(namespace string, poolSize int) {
61 var sdlI common.ISdlInstance = sdlgo.NewSdlInstance(namespace, sdlgo.NewDatabase())
62 return &rNibReaderInstance{sdl: &sdlI, namespace: namespace}
64 func(obj interface{}) {
65 (*obj.(*rNibReaderInstance).sdl).Close()
69 func initPool(poolSize int, newObj func() interface{}, destroyObj func(interface{})) {
70 readerPool = common.NewPool(poolSize, newObj, destroyObj)
74 GetRNibReader returns RNibReader instance from the pool
76 func GetRNibReader() RNibReader {
77 return readerPool.Get().(RNibReader)
80 func (w *rNibReaderInstance) GetNodeb(inventoryName string) (*entities.NodebInfo, common.IRNibError) {
81 name, rNibErr := common.ValidateAndBuildNodeBNameKey(inventoryName)
85 defer readerPool.Put(w)
86 data, err := (*w.sdl).Get([]string{name})
88 return nil, common.NewInternalError(err)
90 nb := entities.NodebInfo{}
91 if data != nil && data[name] != nil{
92 err = proto.Unmarshal( []byte(data[name].(string)), &nb)
94 return nil, common.NewInternalError(err)
98 return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.GetNodeb - responding node %s not found", inventoryName))
101 func (w *rNibReaderInstance) GetNodebByGlobalNbId(nodeType entities.Node_Type, globalNbId *entities.GlobalNbId) (*entities.NodebInfo, common.IRNibError) {
102 key, rNibErr := common.ValidateAndBuildNodeBIdKey(nodeType.String(), globalNbId.GetPlmnId(), globalNbId.GetNbId())
106 defer readerPool.Put(w)
107 data, err := (*w.sdl).Get([]string{key})
109 return nil, common.NewInternalError(err)
111 nb := entities.NodebInfo{}
112 if data != nil && data[key] != nil{
113 err = proto.Unmarshal( []byte(data[key].(string)), &nb)
115 return nil, common.NewInternalError(err)
119 return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.GetNodebByGlobalNbId - responding node not found, global nodeb Id: %s", key))
122 func (w *rNibReaderInstance) GetCellList(inventoryName string) (*entities.Cells, common.IRNibError){
123 cells := &entities.Cells{}
124 nb, err := w.GetNodeb(inventoryName)
128 if nb.GetEnb() != nil && len(nb.GetEnb().GetServedCells()) > 0{
129 cells.Type = entities.Cell_LTE_CELL
130 cells.List = &entities.Cells_ServedCellInfos{ServedCellInfos:&entities.ServedCellInfoList{ServedCells:nb.GetEnb().GetServedCells()}}
132 } else if nb.GetGnb() != nil && len(nb.GetGnb().GetServedNrCells()) > 0{
133 cells.Type = entities.Cell_NR_CELL
134 cells.List = &entities.Cells_ServedNrCells{ServedNrCells:&entities.ServedNRCellList{ServedCells:nb.GetGnb().GetServedNrCells()}}
137 return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.GetCellList - served cells not found. Responding node RAN name: %s.", inventoryName))
140 func (w *rNibReaderInstance) GetListGnbIds()(*[]*entities.NbIdentity, common.IRNibError){
141 defer readerPool.Put(w)
142 data, err := (*w.sdl).GetMembers("GNB")
144 return nil, common.NewInternalError(err)
146 return unmarshalIdentityList(data)
149 func (w *rNibReaderInstance) GetListEnbIds()(*[]*entities.NbIdentity, common.IRNibError){
150 defer readerPool.Put(w)
151 data, err := (*w.sdl).GetMembers("ENB")
153 return nil, common.NewInternalError(err)
155 return unmarshalIdentityList(data)
158 func (w *rNibReaderInstance) GetCountGnbList()(int, common.IRNibError){
159 defer readerPool.Put(w)
160 data, err := (*w.sdl).GetMembers("GNB")
162 return 0, common.NewInternalError(err)
164 return len(data), nil
167 func (w *rNibReaderInstance) GetCell(inventoryName string, pci uint32) (*entities.Cell, common.IRNibError){
168 key, rNibErr := common.ValidateAndBuildCellNamePciKey(inventoryName, pci)
172 return (*w).getCellByKey(key)
175 func (w *rNibReaderInstance) GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, common.IRNibError){
177 var rNibErr common.IRNibError
178 if cellType == entities.Cell_LTE_CELL {
179 key, rNibErr = common.ValidateAndBuildCellIdKey(cellId)
180 } else if cellType == entities.Cell_NR_CELL {
181 key, rNibErr = common.ValidateAndBuildNrCellIdKey(cellId)
183 return nil, common.NewValidationError(errors.Errorf("#rNibReader.GetCellById - invalid cell type: %v", cellType))
188 return (*w).getCellByKey(key)
191 func (w *rNibReaderInstance) getCellByKey(key string) (*entities.Cell, common.IRNibError){
192 defer readerPool.Put(w)
193 data, err := (*w.sdl).Get([]string{key})
195 return nil, common.NewInternalError(err)
197 cell := entities.Cell{}
198 if data != nil && data[key] != nil{
199 err = proto.Unmarshal( []byte(data[key].(string)), &cell)
201 return nil, common.NewInternalError(err)
205 return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.getCellByKey - cell not found, key: %s", key))
208 func unmarshalIdentityList(data []string ) (*[]*entities.NbIdentity, common.IRNibError){
209 var members []*entities.NbIdentity
210 for _, d := range data{
211 member := entities.NbIdentity{}
212 err := proto.Unmarshal([]byte(d), &member)
214 return nil, common.NewInternalError(err)
216 members = append(members, &member)