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 rnibcommon "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
24 rnibentities "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 *rnibcommon.Pool
31 type rNibWriterInstance struct {
37 RNibWriter interface allows saving data to the redis BD
39 type RNibWriter interface {
40 SaveNodeb(nbIdentity *rnibentities.NbIdentity, nb *rnibentities.NodebInfo) rnibcommon.IRNibError
44 Init initializes the infrastructure required for the RNibWriter instance
46 func InitWriter(namespace string, poolSize int) {
47 initWriterPool(poolSize,
49 var sdlI ISdlInstance = sdlgo.NewSdlInstance(namespace, sdlgo.NewDatabase())
50 return &rNibWriterInstance{sdl: &sdlI, namespace: namespace}
52 func(obj interface{}) {
53 (*obj.(*rNibWriterInstance).sdl).Close()
58 InitPool initializes the writer's instances pool
60 func initWriterPool(poolSize int, newObj func() interface{}, destroyObj func(interface{})) {
61 writerPool = rnibcommon.NewPool(poolSize, newObj, destroyObj)
65 GetRNibWriter returns RNibWriter instance from the pool
67 func GetRNibWriter() RNibWriter {
68 return writerPool.Get().(RNibWriter)
72 SaveNodeb saves nodeB entity data in the redis DB according to the specified data model
74 func (w *rNibWriterInstance) SaveNodeb(nbIdentity *rnibentities.NbIdentity, entity *rnibentities.NodebInfo) rnibcommon.IRNibError {
76 isNotEmptyIdentity := isNotEmpty(nbIdentity)
78 if isNotEmptyIdentity && entity.GetNodeType() == rnibentities.Node_UNKNOWN {
79 return rnibcommon.NewValidationError(errors.New(fmt.Sprintf("#rNibWriter.saveNodeB - Unknown responding node type, entity: %v", entity)))
81 defer writerPool.Put(w)
82 data, err := proto.Marshal(entity)
84 return rnibcommon.NewInternalError(err)
86 var pairs []interface{}
87 key, rNibErr := rnibcommon.ValidateAndBuildNodeBNameKey(nbIdentity.InventoryName)
91 pairs = append(pairs, key, data)
93 if isNotEmptyIdentity {
94 key, rNibErr = rnibcommon.ValidateAndBuildNodeBIdKey(entity.GetNodeType().String(), nbIdentity.GlobalNbId.GetPlmnId(), nbIdentity.GlobalNbId.GetNbId())
98 pairs = append(pairs, key, data)
101 if entity.GetEnb() != nil {
102 pairs, rNibErr = appendEnbCells(nbIdentity, entity.GetEnb().GetServedCells(), pairs)
107 if entity.GetGnb() != nil {
108 pairs, rNibErr = appendGnbCells(nbIdentity, entity.GetGnb().GetServedNrCells(), pairs)
113 err = (*w.sdl).Set(pairs)
115 return rnibcommon.NewInternalError(err)
117 if isNotEmptyIdentity {
118 nbIdData, err := proto.Marshal(nbIdentity)
120 return rnibcommon.NewInternalError(err)
122 err = (*w.sdl).AddMember(entity.GetNodeType().String(), nbIdData)
124 return rnibcommon.NewInternalError(err)
131 Close closes writer's pool
137 func appendEnbCells(nbIdentity *rnibentities.NbIdentity, cells []*rnibentities.ServedCellInfo, pairs []interface{}) ([]interface{}, rnibcommon.IRNibError) {
138 for _, cell := range cells {
139 cellEntity := rnibentities.Cell{Type: rnibentities.Cell_LTE_CELL, Cell: &rnibentities.Cell_ServedCellInfo{ServedCellInfo: cell}}
140 cellData, err := proto.Marshal(&cellEntity)
142 return pairs, rnibcommon.NewInternalError(err)
144 key, rNibErr := rnibcommon.ValidateAndBuildCellIdKey(cell.GetCellId())
146 return pairs, rNibErr
148 pairs = append(pairs, key, cellData)
149 key, rNibErr = rnibcommon.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetPci())
151 return pairs, rNibErr
153 pairs = append(pairs, key, cellData)
158 func appendGnbCells(nbIdentity *rnibentities.NbIdentity, cells []*rnibentities.ServedNRCell, pairs []interface{}) ([]interface{}, rnibcommon.IRNibError) {
159 for _, cell := range cells {
160 cellEntity := rnibentities.Cell{Type: rnibentities.Cell_NR_CELL, Cell: &rnibentities.Cell_ServedNrCell{ServedNrCell: cell}}
161 cellData, err := proto.Marshal(&cellEntity)
163 return pairs, rnibcommon.NewInternalError(err)
165 key, rNibErr := rnibcommon.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
167 return pairs, rNibErr
169 pairs = append(pairs, key, cellData)
170 key, rNibErr = rnibcommon.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetServedNrCellInformation().GetNrPci())
172 return pairs, rNibErr
174 pairs = append(pairs, key, cellData)
179 func isNotEmpty(nbIdentity *rnibentities.NbIdentity) bool {
180 return nbIdentity.GlobalNbId != nil && nbIdentity.GlobalNbId.PlmnId != "" && nbIdentity.GlobalNbId.NbId != ""