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.
21 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
22 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
25 "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
26 "github.com/golang/protobuf/proto"
31 var writerPool *common.Pool
33 type rNibWriterInstance struct {
34 sdl *common.ISdlInstance
38 RNibWriter interface allows saving data to the redis DB
40 type RNibWriter interface {
41 SaveNodeb(nbIdentity *entities.NbIdentity, nb *entities.NodebInfo) common.IRNibError
42 SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) common.IRNibError
45 Init initializes the infrastructure required for the RNibWriter instance
47 func Init(namespace string, poolSize int) {
50 var sdlI common.ISdlInstance = sdlgo.NewSdlInstance(namespace, sdlgo.NewDatabase())
51 return &rNibWriterInstance{sdl: &sdlI, namespace: namespace}
53 func(obj interface{}) {
54 (*obj.(*rNibWriterInstance).sdl).Close()
58 InitPool initializes the writer's instances pool
60 func initPool(poolSize int, newObj func() interface{}, destroyObj func(interface{})) {
61 writerPool = common.NewPool(poolSize, newObj, destroyObj)
64 GetRNibWriter returns RNibWriter instance from the pool
66 func GetRNibWriter() RNibWriter {
67 return writerPool.Get().(RNibWriter)
70 SaveNodeb saves nodeB entity data in the redis DB according to the specified data model
72 func (w *rNibWriterInstance) SaveNodeb(nbIdentity *entities.NbIdentity, entity *entities.NodebInfo) common.IRNibError {
74 isNotEmptyIdentity := isNotEmpty(nbIdentity)
76 if isNotEmptyIdentity && entity.GetNodeType() == entities.Node_UNKNOWN{
77 return common.NewValidationError(errors.New( fmt.Sprintf("#rNibWriter.saveNodeB - Unknown responding node type, entity: %v", entity)))
79 defer writerPool.Put(w)
80 data, err := proto.Marshal(entity)
82 return common.NewInternalError(err)
84 var pairs []interface{}
85 key, rNibErr := common.ValidateAndBuildNodeBNameKey(nbIdentity.InventoryName)
89 pairs = append(pairs, key, data)
91 if isNotEmptyIdentity {
92 key, rNibErr = common.ValidateAndBuildNodeBIdKey(entity.GetNodeType().String(), nbIdentity.GlobalNbId.GetPlmnId(), nbIdentity.GlobalNbId.GetNbId())
96 pairs = append(pairs, key, data)
99 if entity.GetEnb() != nil {
100 pairs, rNibErr = appendEnbCells(nbIdentity, entity.GetEnb().GetServedCells(), pairs)
105 if entity.GetGnb() != nil {
106 pairs, rNibErr = appendGnbCells(nbIdentity, entity.GetGnb().GetServedNrCells(), pairs)
111 err = (*w.sdl).Set(pairs)
113 return common.NewInternalError(err)
115 if isNotEmptyIdentity {
116 nbIdData, err := proto.Marshal(nbIdentity)
118 return common.NewInternalError(err)
120 err = (*w.sdl).AddMember(entity.GetNodeType().String(), nbIdData)
122 return common.NewInternalError(err)
129 SaveRanLoadInformation stores ran load information for the provided ran
131 func (w *rNibWriterInstance) SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) common.IRNibError {
133 defer writerPool.Put(w)
135 key, rnibErr:= common.ValidateAndBuildRanLoadInformationKey(inventoryName)
141 data, err := proto.Marshal(ranLoadInformation)
144 return common.NewInternalError(err)
147 var pairs []interface{}
148 pairs = append(pairs, key, data)
150 err = (*w.sdl).Set(pairs)
153 return common.NewInternalError(err)
160 Close closes writer's pool
166 func appendEnbCells(nbIdentity *entities.NbIdentity, cells []*entities.ServedCellInfo, pairs []interface{}) ([]interface{}, common.IRNibError) {
167 for _, cell := range cells {
168 cellEntity := entities.Cell{Type:entities.Cell_LTE_CELL, Cell:&entities.Cell_ServedCellInfo{ServedCellInfo:cell}}
169 cellData, err := proto.Marshal(&cellEntity)
171 return pairs, common.NewInternalError(err)
173 key, rNibErr := common.ValidateAndBuildCellIdKey(cell.GetCellId())
175 return pairs, rNibErr
177 pairs = append(pairs, key, cellData)
178 key, rNibErr = common.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetPci())
180 return pairs, rNibErr
182 pairs = append(pairs, key, cellData)
187 func appendGnbCells(nbIdentity *entities.NbIdentity, cells []*entities.ServedNRCell, pairs []interface{}) ([]interface{}, common.IRNibError) {
188 for _, cell := range cells {
189 cellEntity := entities.Cell{Type:entities.Cell_NR_CELL, Cell:&entities.Cell_ServedNrCell{ServedNrCell:cell}}
190 cellData, err := proto.Marshal(&cellEntity)
192 return pairs, common.NewInternalError(err)
194 key, rNibErr := common.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
196 return pairs, rNibErr
198 pairs = append(pairs, key, cellData)
199 key, rNibErr = common.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetServedNrCellInformation().GetNrPci())
201 return pairs, rNibErr
203 pairs = append(pairs, key, cellData)
208 func isNotEmpty(nbIdentity *entities.NbIdentity) bool {
209 return nbIdentity.GlobalNbId != nil && nbIdentity.GlobalNbId.PlmnId != "" && nbIdentity.GlobalNbId.NbId != ""