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.
23 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
24 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
25 "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
26 "github.com/golang/protobuf/proto"
29 var writerPool *common.Pool
31 type rNibWriterInstance struct {
32 sdl *common.ISdlInstance
37 RNibWriter interface allows saving data to the redis DB
39 type RNibWriter interface {
40 SaveNodeb(nbIdentity *entities.NbIdentity, nb *entities.NodebInfo) common.IRNibError
41 UpdateNodebInfo(nodebInfo *entities.NodebInfo) common.IRNibError
42 SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) common.IRNibError
46 Init initializes the infrastructure required for the RNibWriter instance
48 func Init(namespace string, poolSize int) {
51 var sdlI common.ISdlInstance = sdlgo.NewSdlInstance(namespace, sdlgo.NewDatabase())
52 return &rNibWriterInstance{sdl: &sdlI, namespace: namespace}
54 func(obj interface{}) {
55 (*obj.(*rNibWriterInstance).sdl).Close()
60 InitPool initializes the writer's instances pool
62 func initPool(poolSize int, newObj func() interface{}, destroyObj func(interface{})) {
63 writerPool = common.NewPool(poolSize, newObj, destroyObj)
67 GetRNibWriter returns RNibWriter instance from the pool
69 func GetRNibWriter() RNibWriter {
70 return writerPool.Get().(RNibWriter)
74 SaveNodeb saves nodeB entity data in the redis DB according to the specified data model
76 func (w *rNibWriterInstance) SaveNodeb(nbIdentity *entities.NbIdentity, entity *entities.NodebInfo) common.IRNibError {
78 isNotEmptyIdentity := isNotEmpty(nbIdentity)
80 if isNotEmptyIdentity && entity.GetNodeType() == entities.Node_UNKNOWN {
81 return common.NewValidationError(errors.New(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 (w *rNibWriterInstance) UpdateNodebInfo(nodebInfo *entities.NodebInfo) common.IRNibError {
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 (w *rNibWriterInstance) SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) common.IRNibError {
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{}, common.IRNibError) {
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{}, common.IRNibError) {
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 != ""