[RICPLT-1820] Add ran_load_information proto
[ric-plt/nodeb-rnib.git] / reader / rNibReader.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 //
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
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16
17 package reader
18
19 import (
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"
25 )
26
27 var readerPool *common.Pool
28
29 type rNibReaderInstance struct {
30         sdl       *common.ISdlInstance
31         namespace string
32 }
33
34 /*
35 RNibReader interface allows retrieving data from redis BD by various keys
36  */
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 }
55 /*
56  Init initializes the infrastructure required for the RNibReader instance
57 */
58 func Init(namespace string, poolSize int) {
59         initPool(poolSize,
60         func() interface{} {
61                 var sdlI common.ISdlInstance = sdlgo.NewSdlInstance(namespace, sdlgo.NewDatabase())
62                 return &rNibReaderInstance{sdl: &sdlI, namespace: namespace}
63         },
64                 func(obj interface{}) {
65                 (*obj.(*rNibReaderInstance).sdl).Close()
66         })
67 }
68
69 func initPool(poolSize int, newObj func() interface{}, destroyObj func(interface{})) {
70         readerPool = common.NewPool(poolSize, newObj, destroyObj)
71 }
72
73 /*
74 GetRNibReader returns RNibReader instance from the pool
75  */
76 func GetRNibReader() RNibReader {
77         return readerPool.Get().(RNibReader)
78 }
79
80 func (w *rNibReaderInstance) GetNodeb(inventoryName string) (*entities.NodebInfo, common.IRNibError) {
81         name, rNibErr := common.ValidateAndBuildNodeBNameKey(inventoryName)
82         if rNibErr != nil{
83                 return nil, rNibErr
84         }
85         defer readerPool.Put(w)
86         data, err := (*w.sdl).Get([]string{name})
87         if err != nil{
88                 return nil, common.NewInternalError(err)
89         }
90         nb := entities.NodebInfo{}
91         if data != nil && data[name] != nil{
92                 err = proto.Unmarshal( []byte(data[name].(string)), &nb)
93                 if err != nil{
94                         return nil, common.NewInternalError(err)
95                 }
96                 return &nb, nil
97         }
98         return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.GetNodeb - responding node %s not found", inventoryName))
99 }
100
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())
103         if rNibErr != nil{
104                 return nil, rNibErr
105         }
106         defer readerPool.Put(w)
107         data, err := (*w.sdl).Get([]string{key})
108         if err != nil{
109                 return nil, common.NewInternalError(err)
110         }
111         nb := entities.NodebInfo{}
112         if data != nil && data[key] != nil{
113                 err = proto.Unmarshal( []byte(data[key].(string)), &nb)
114                 if err != nil{
115                         return nil, common.NewInternalError(err)
116                 }
117                 return &nb, nil
118         }
119         return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.GetNodebByGlobalNbId - responding node not found, global nodeb Id: %s", key))
120 }
121
122 func (w *rNibReaderInstance) GetCellList(inventoryName string) (*entities.Cells, common.IRNibError){
123         cells := &entities.Cells{}
124         nb, err := w.GetNodeb(inventoryName)
125         if err != nil{
126                 return nil, err
127         }
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()}}
131                 return cells, nil
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()}}
135                 return cells, nil
136         }
137         return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.GetCellList - served cells not found. Responding node RAN name: %s.", inventoryName))
138 }
139
140 func (w *rNibReaderInstance) GetListGnbIds()(*[]*entities.NbIdentity, common.IRNibError){
141         defer readerPool.Put(w)
142         data, err := (*w.sdl).GetMembers("GNB")
143         if err != nil{
144                 return nil, common.NewInternalError(err)
145         }
146         return unmarshalIdentityList(data)
147 }
148
149 func (w *rNibReaderInstance) GetListEnbIds()(*[]*entities.NbIdentity, common.IRNibError){
150         defer readerPool.Put(w)
151         data, err := (*w.sdl).GetMembers("ENB")
152         if err != nil{
153                 return nil, common.NewInternalError(err)
154         }
155         return unmarshalIdentityList(data)
156 }
157
158 func (w *rNibReaderInstance) GetCountGnbList()(int, common.IRNibError){
159         defer readerPool.Put(w)
160         data, err := (*w.sdl).GetMembers("GNB")
161         if err != nil{
162                 return 0, common.NewInternalError(err)
163         }
164         return len(data), nil
165 }
166
167 func (w *rNibReaderInstance) GetCell(inventoryName string, pci uint32) (*entities.Cell, common.IRNibError){
168         key, rNibErr := common.ValidateAndBuildCellNamePciKey(inventoryName, pci)
169         if rNibErr != nil{
170                 return nil, rNibErr
171         }
172         return (*w).getCellByKey(key)
173 }
174
175 func (w *rNibReaderInstance) GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, common.IRNibError){
176         var key string
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)
182         } else {
183                 return nil, common.NewValidationError(errors.Errorf("#rNibReader.GetCellById - invalid cell type: %v", cellType))
184         }
185         if rNibErr != nil{
186                 return nil, rNibErr
187         }
188         return (*w).getCellByKey(key)
189 }
190
191 func (w *rNibReaderInstance) getCellByKey(key string) (*entities.Cell, common.IRNibError){
192         defer readerPool.Put(w)
193         data, err := (*w.sdl).Get([]string{key})
194         if err != nil{
195                 return nil, common.NewInternalError(err)
196         }
197         cell := entities.Cell{}
198         if data != nil && data[key] != nil{
199                 err = proto.Unmarshal( []byte(data[key].(string)), &cell)
200                 if err != nil{
201                         return nil, common.NewInternalError(err)
202                 }
203                 return &cell, nil
204         }
205         return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.getCellByKey - cell not found, key: %s", key))
206 }
207
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)
213                 if err != nil{
214                         return nil, common.NewInternalError(err)
215                 }
216                 members = append(members, &member)
217         }
218         return &members, nil
219 }
220
221 func Close(){
222         readerPool.Close()
223 }