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)
116 ranNameIdentity := &entities.NbIdentity{InventoryName: nbIdentity.InventoryName}
118 if isNotEmptyIdentity{
119 nbIdData, err := proto.Marshal(ranNameIdentity)
121 return common.NewInternalError(err)
123 err = (*w.sdl).RemoveMember(entities.Node_UNKNOWN.String(), nbIdData)
125 return common.NewInternalError(err)
128 nbIdentity = ranNameIdentity
131 nbIdData, err := proto.Marshal(nbIdentity)
133 return common.NewInternalError(err)
135 err = (*w.sdl).AddMember(entity.GetNodeType().String(), nbIdData)
137 return common.NewInternalError(err)
143 SaveRanLoadInformation stores ran load information for the provided ran
145 func (w *rNibWriterInstance) SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) common.IRNibError {
147 defer writerPool.Put(w)
149 key, rnibErr:= common.ValidateAndBuildRanLoadInformationKey(inventoryName)
155 data, err := proto.Marshal(ranLoadInformation)
158 return common.NewInternalError(err)
161 var pairs []interface{}
162 pairs = append(pairs, key, data)
164 err = (*w.sdl).Set(pairs)
167 return common.NewInternalError(err)
174 Close closes writer's pool
180 func appendEnbCells(nbIdentity *entities.NbIdentity, cells []*entities.ServedCellInfo, pairs []interface{}) ([]interface{}, common.IRNibError) {
181 for _, cell := range cells {
182 cellEntity := entities.Cell{Type:entities.Cell_LTE_CELL, Cell:&entities.Cell_ServedCellInfo{ServedCellInfo:cell}}
183 cellData, err := proto.Marshal(&cellEntity)
185 return pairs, common.NewInternalError(err)
187 key, rNibErr := common.ValidateAndBuildCellIdKey(cell.GetCellId())
189 return pairs, rNibErr
191 pairs = append(pairs, key, cellData)
192 key, rNibErr = common.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetPci())
194 return pairs, rNibErr
196 pairs = append(pairs, key, cellData)
201 func appendGnbCells(nbIdentity *entities.NbIdentity, cells []*entities.ServedNRCell, pairs []interface{}) ([]interface{}, common.IRNibError) {
202 for _, cell := range cells {
203 cellEntity := entities.Cell{Type:entities.Cell_NR_CELL, Cell:&entities.Cell_ServedNrCell{ServedNrCell:cell}}
204 cellData, err := proto.Marshal(&cellEntity)
206 return pairs, common.NewInternalError(err)
208 key, rNibErr := common.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
210 return pairs, rNibErr
212 pairs = append(pairs, key, cellData)
213 key, rNibErr = common.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetServedNrCellInformation().GetNrPci())
215 return pairs, rNibErr
217 pairs = append(pairs, key, cellData)
222 func isNotEmpty(nbIdentity *entities.NbIdentity) bool {
223 return nbIdentity.GlobalNbId != nil && nbIdentity.GlobalNbId.PlmnId != "" && nbIdentity.GlobalNbId.NbId != ""