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.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
25 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
26 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
27 "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
28 "github.com/golang/protobuf/proto"
31 var writerPool *common.Pool
33 type rNibWriterInstance struct {
34 sdl *common.ISdlInstance
39 RNibWriter interface allows saving data to the redis DB
41 type RNibWriter interface {
42 SaveNodeb(nbIdentity *entities.NbIdentity, nb *entities.NodebInfo) error
43 UpdateNodebInfo(nodebInfo *entities.NodebInfo) error
44 SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) error
48 Init initializes the infrastructure required for the RNibWriter instance
50 func Init(namespace string, poolSize int) {
53 var sdlI common.ISdlInstance = sdlgo.NewSdlInstance(namespace, sdlgo.NewDatabase())
54 return &rNibWriterInstance{sdl: &sdlI, namespace: namespace}
56 func(obj interface{}) {
57 (*obj.(*rNibWriterInstance).sdl).Close()
62 InitPool initializes the writer's instances pool
64 func initPool(poolSize int, newObj func() interface{}, destroyObj func(interface{})) {
65 writerPool = common.NewPool(poolSize, newObj, destroyObj)
68 GetRNibWriter returns reference to RNibWriter
70 func GetRNibWriter() RNibWriter {
71 return &rNibWriterInstance{}
74 SaveNodeb saves nodeB entity data in the redis DB according to the specified data model
76 func (*rNibWriterInstance) SaveNodeb(nbIdentity *entities.NbIdentity, entity *entities.NodebInfo) error {
77 w := writerPool.Get().(*rNibWriterInstance)
78 isNotEmptyIdentity := isNotEmpty(nbIdentity)
80 if isNotEmptyIdentity && entity.GetNodeType() == entities.Node_UNKNOWN {
81 return common.NewValidationError(fmt.Sprintf("#rNibWriter.saveNodeB - Unknown responding node type, entity: %v", entity))
83 defer writerPool.Put(w)
84 data, err := proto.Marshal(entity)
86 return common.NewInternalError(err)
88 var pairs []interface{}
89 key, rNibErr := common.ValidateAndBuildNodeBNameKey(nbIdentity.InventoryName)
93 pairs = append(pairs, key, data)
95 if isNotEmptyIdentity {
96 key, rNibErr = common.ValidateAndBuildNodeBIdKey(entity.GetNodeType().String(), nbIdentity.GlobalNbId.GetPlmnId(), nbIdentity.GlobalNbId.GetNbId())
100 pairs = append(pairs, key, data)
103 if entity.GetEnb() != nil {
104 pairs, rNibErr = appendEnbCells(nbIdentity, entity.GetEnb().GetServedCells(), pairs)
109 if entity.GetGnb() != nil {
110 pairs, rNibErr = appendGnbCells(nbIdentity, entity.GetGnb().GetServedNrCells(), pairs)
115 err = (*w.sdl).Set(pairs)
117 return common.NewInternalError(err)
120 ranNameIdentity := &entities.NbIdentity{InventoryName: nbIdentity.InventoryName}
122 if isNotEmptyIdentity {
123 nbIdData, err := proto.Marshal(ranNameIdentity)
125 return common.NewInternalError(err)
127 err = (*w.sdl).RemoveMember(entities.Node_UNKNOWN.String(), nbIdData)
129 return common.NewInternalError(err)
132 nbIdentity = ranNameIdentity
135 nbIdData, err := proto.Marshal(nbIdentity)
137 return common.NewInternalError(err)
139 err = (*w.sdl).AddMember(entity.GetNodeType().String(), nbIdData)
141 return common.NewInternalError(err)
149 func (*rNibWriterInstance) UpdateNodebInfo(nodebInfo *entities.NodebInfo) error {
150 w := writerPool.Get().(*rNibWriterInstance)
151 defer writerPool.Put(w)
153 nodebNameKey, rNibErr := common.ValidateAndBuildNodeBNameKey(nodebInfo.GetRanName())
159 nodebIdKey, buildNodebIdKeyError := common.ValidateAndBuildNodeBIdKey(nodebInfo.GetNodeType().String(), nodebInfo.GlobalNbId.GetPlmnId(), nodebInfo.GlobalNbId.GetNbId())
161 data, err := proto.Marshal(nodebInfo)
164 return common.NewInternalError(err)
167 var pairs []interface{}
168 pairs = append(pairs, nodebNameKey, data)
170 if buildNodebIdKeyError == nil {
171 pairs = append(pairs, nodebIdKey, data)
174 err = (*w.sdl).Set(pairs)
177 return common.NewInternalError(err)
184 SaveRanLoadInformation stores ran load information for the provided ran
186 func (*rNibWriterInstance) SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) error {
187 w := writerPool.Get().(*rNibWriterInstance)
188 defer writerPool.Put(w)
190 key, rnibErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
196 data, err := proto.Marshal(ranLoadInformation)
199 return common.NewInternalError(err)
202 var pairs []interface{}
203 pairs = append(pairs, key, data)
205 err = (*w.sdl).Set(pairs)
208 return common.NewInternalError(err)
215 Close closes writer's pool
221 func appendEnbCells(nbIdentity *entities.NbIdentity, cells []*entities.ServedCellInfo, pairs []interface{}) ([]interface{}, error) {
222 for _, cell := range cells {
223 cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}}
224 cellData, err := proto.Marshal(&cellEntity)
226 return pairs, common.NewInternalError(err)
228 key, rNibErr := common.ValidateAndBuildCellIdKey(cell.GetCellId())
230 return pairs, rNibErr
232 pairs = append(pairs, key, cellData)
233 key, rNibErr = common.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetPci())
235 return pairs, rNibErr
237 pairs = append(pairs, key, cellData)
242 func appendGnbCells(nbIdentity *entities.NbIdentity, cells []*entities.ServedNRCell, pairs []interface{}) ([]interface{}, error) {
243 for _, cell := range cells {
244 cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: cell}}
245 cellData, err := proto.Marshal(&cellEntity)
247 return pairs, common.NewInternalError(err)
249 key, rNibErr := common.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
251 return pairs, rNibErr
253 pairs = append(pairs, key, cellData)
254 key, rNibErr = common.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetServedNrCellInformation().GetNrPci())
256 return pairs, rNibErr
258 pairs = append(pairs, key, cellData)
263 func isNotEmpty(nbIdentity *entities.NbIdentity) bool {
264 return nbIdentity.GlobalNbId != nil && nbIdentity.GlobalNbId.PlmnId != "" && nbIdentity.GlobalNbId.NbId != ""