[RICPLT-1783] Add ValidateAndBuildRanLoadInformationKey | Fix dependencies | Reformat...
[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/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"
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 /*
57  Init initializes the infrastructure required for the RNibReader instance
58 */
59 func Init(namespace string, poolSize int) {
60         initPool(poolSize,
61                 func() interface{} {
62                         var sdlI common.ISdlInstance = sdlgo.NewSdlInstance(namespace, sdlgo.NewDatabase())
63                         return &rNibReaderInstance{sdl: &sdlI, namespace: namespace}
64                 },
65                 func(obj interface{}) {
66                         (*obj.(*rNibReaderInstance).sdl).Close()
67                 })
68 }
69
70 func initPool(poolSize int, newObj func() interface{}, destroyObj func(interface{})) {
71         readerPool = common.NewPool(poolSize, newObj, destroyObj)
72 }
73
74 /*
75 GetRNibReader returns RNibReader instance from the pool
76 */
77 func GetRNibReader() RNibReader {
78         return readerPool.Get().(RNibReader)
79 }
80
81 func (w *rNibReaderInstance) GetNodeb(inventoryName string) (*entities.NodebInfo, common.IRNibError) {
82         name, rNibErr := common.ValidateAndBuildNodeBNameKey(inventoryName)
83         if rNibErr != nil {
84                 return nil, rNibErr
85         }
86         defer readerPool.Put(w)
87         data, err := (*w.sdl).Get([]string{name})
88         if err != nil {
89                 return nil, common.NewInternalError(err)
90         }
91         nb := entities.NodebInfo{}
92         if data != nil && data[name] != nil {
93                 err = proto.Unmarshal([]byte(data[name].(string)), &nb)
94                 if err != nil {
95                         return nil, common.NewInternalError(err)
96                 }
97                 return &nb, nil
98         }
99         return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.GetNodeb - responding node %s not found", inventoryName))
100 }
101
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())
104         if rNibErr != nil {
105                 return nil, rNibErr
106         }
107         defer readerPool.Put(w)
108         data, err := (*w.sdl).Get([]string{key})
109         if err != nil {
110                 return nil, common.NewInternalError(err)
111         }
112         nb := entities.NodebInfo{}
113         if data != nil && data[key] != nil {
114                 err = proto.Unmarshal([]byte(data[key].(string)), &nb)
115                 if err != nil {
116                         return nil, common.NewInternalError(err)
117                 }
118                 return &nb, nil
119         }
120         return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.GetNodebByGlobalNbId - responding node not found, global nodeb Id: %s", key))
121 }
122
123 func (w *rNibReaderInstance) GetCellList(inventoryName string) (*entities.Cells, common.IRNibError) {
124         cells := &entities.Cells{}
125         nb, err := w.GetNodeb(inventoryName)
126         if err != nil {
127                 return nil, err
128         }
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()}}
132                 return cells, nil
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()}}
136                 return cells, nil
137         }
138         return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.GetCellList - served cells not found. Responding node RAN name: %s.", inventoryName))
139 }
140
141 func (w *rNibReaderInstance) GetListGnbIds() (*[]*entities.NbIdentity, common.IRNibError) {
142         defer readerPool.Put(w)
143         data, err := (*w.sdl).GetMembers("GNB")
144         if err != nil {
145                 return nil, common.NewInternalError(err)
146         }
147         return unmarshalIdentityList(data)
148 }
149
150 func (w *rNibReaderInstance) GetListEnbIds() (*[]*entities.NbIdentity, common.IRNibError) {
151         defer readerPool.Put(w)
152         data, err := (*w.sdl).GetMembers("ENB")
153         if err != nil {
154                 return nil, common.NewInternalError(err)
155         }
156         return unmarshalIdentityList(data)
157 }
158
159 func (w *rNibReaderInstance) GetCountGnbList() (int, common.IRNibError) {
160         defer readerPool.Put(w)
161         data, err := (*w.sdl).GetMembers("GNB")
162         if err != nil {
163                 return 0, common.NewInternalError(err)
164         }
165         return len(data), nil
166 }
167
168 func (w *rNibReaderInstance) GetCell(inventoryName string, pci uint32) (*entities.Cell, common.IRNibError) {
169         key, rNibErr := common.ValidateAndBuildCellNamePciKey(inventoryName, pci)
170         if rNibErr != nil {
171                 return nil, rNibErr
172         }
173         return (*w).getCellByKey(key)
174 }
175
176 func (w *rNibReaderInstance) GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, common.IRNibError) {
177         var key string
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)
183         } else {
184                 return nil, common.NewValidationError(errors.Errorf("#rNibReader.GetCellById - invalid cell type: %v", cellType))
185         }
186         if rNibErr != nil {
187                 return nil, rNibErr
188         }
189         return (*w).getCellByKey(key)
190 }
191
192 func (w *rNibReaderInstance) getCellByKey(key string) (*entities.Cell, common.IRNibError) {
193         defer readerPool.Put(w)
194         data, err := (*w.sdl).Get([]string{key})
195         if err != nil {
196                 return nil, common.NewInternalError(err)
197         }
198         cell := entities.Cell{}
199         if data != nil && data[key] != nil {
200                 err = proto.Unmarshal([]byte(data[key].(string)), &cell)
201                 if err != nil {
202                         return nil, common.NewInternalError(err)
203                 }
204                 return &cell, nil
205         }
206         return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.getCellByKey - cell not found, key: %s", key))
207 }
208
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)
214                 if err != nil {
215                         return nil, common.NewInternalError(err)
216                 }
217                 members = append(members, &member)
218         }
219         return &members, nil
220 }
221
222 func Close() {
223         readerPool.Close()
224 }