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.
22 rnibcommon "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
23 rnibentities "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
24 "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
25 "github.com/golang/protobuf/proto"
28 var writerPool *rnibcommon.Pool
30 type rNibWriterInstance struct {
36 RNibWriter interface allows saving data to the redis BD
38 type RNibWriter interface {
39 SaveNodeb(nbIdentity *rnibentities.NbIdentity, nb *rnibentities.NodebInfo) error
43 Init initializes the infrastructure required for the RNibWriter instance
45 func InitWriter(namespace string, poolSize int) {
46 initWriterPool(poolSize,
48 var sdlI ISdlInstance = sdlgo.NewSdlInstance(namespace, sdlgo.NewDatabase())
49 return &rNibWriterInstance{sdl: &sdlI, namespace: namespace}
51 func(obj interface{}) {
52 (*obj.(*rNibWriterInstance).sdl).Close()
57 InitPool initializes the writer's instances pool
59 func initWriterPool(poolSize int, newObj func() interface{}, destroyObj func(interface{})) {
60 writerPool = rnibcommon.NewPool(poolSize, newObj, destroyObj)
64 GetRNibWriter returns RNibWriter instance from the pool
66 func GetRNibWriter() RNibWriter {
67 return writerPool.Get().(RNibWriter)
71 SaveNodeb saves nodeB entity data in the redis DB according to the specified data model
73 func (w *rNibWriterInstance) SaveNodeb(nbIdentity *rnibentities.NbIdentity, entity *rnibentities.NodebInfo) error {
75 isNotEmptyIdentity := isNotEmpty(nbIdentity)
77 if isNotEmptyIdentity && entity.GetNodeType() == rnibentities.Node_UNKNOWN {
78 return rnibcommon.NewValidationError(fmt.Sprintf("#rNibWriter.saveNodeB - Unknown responding node type, entity: %v", entity))
80 defer writerPool.Put(w)
81 data, err := proto.Marshal(entity)
83 return rnibcommon.NewInternalError(err)
85 var pairs []interface{}
86 key, rNibErr := rnibcommon.ValidateAndBuildNodeBNameKey(nbIdentity.InventoryName)
90 pairs = append(pairs, key, data)
92 if isNotEmptyIdentity {
93 key, rNibErr = rnibcommon.ValidateAndBuildNodeBIdKey(entity.GetNodeType().String(), nbIdentity.GlobalNbId.GetPlmnId(), nbIdentity.GlobalNbId.GetNbId())
97 pairs = append(pairs, key, data)
100 if entity.GetEnb() != nil {
101 pairs, rNibErr = appendEnbCells(nbIdentity, entity.GetEnb().GetServedCells(), pairs)
106 if entity.GetGnb() != nil {
107 pairs, rNibErr = appendGnbCells(nbIdentity, entity.GetGnb().GetServedNrCells(), pairs)
112 err = (*w.sdl).Set(pairs)
114 return rnibcommon.NewInternalError(err)
116 if isNotEmptyIdentity {
117 nbIdData, err := proto.Marshal(nbIdentity)
119 return rnibcommon.NewInternalError(err)
121 err = (*w.sdl).AddMember(entity.GetNodeType().String(), nbIdData)
123 return rnibcommon.NewInternalError(err)
130 Close closes writer's pool
136 func appendEnbCells(nbIdentity *rnibentities.NbIdentity, cells []*rnibentities.ServedCellInfo, pairs []interface{}) ([]interface{}, error) {
137 for _, cell := range cells {
138 cellEntity := rnibentities.Cell{Type: rnibentities.Cell_LTE_CELL, Cell: &rnibentities.Cell_ServedCellInfo{ServedCellInfo: cell}}
139 cellData, err := proto.Marshal(&cellEntity)
141 return pairs, rnibcommon.NewInternalError(err)
143 key, rNibErr := rnibcommon.ValidateAndBuildCellIdKey(cell.GetCellId())
145 return pairs, rNibErr
147 pairs = append(pairs, key, cellData)
148 key, rNibErr = rnibcommon.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetPci())
150 return pairs, rNibErr
152 pairs = append(pairs, key, cellData)
157 func appendGnbCells(nbIdentity *rnibentities.NbIdentity, cells []*rnibentities.ServedNRCell, pairs []interface{}) ([]interface{}, error) {
158 for _, cell := range cells {
159 cellEntity := rnibentities.Cell{Type: rnibentities.Cell_NR_CELL, Cell: &rnibentities.Cell_ServedNrCell{ServedNrCell: cell}}
160 cellData, err := proto.Marshal(&cellEntity)
162 return pairs, rnibcommon.NewInternalError(err)
164 key, rNibErr := rnibcommon.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
166 return pairs, rNibErr
168 pairs = append(pairs, key, cellData)
169 key, rNibErr = rnibcommon.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetServedNrCellInformation().GetNrPci())
171 return pairs, rNibErr
173 pairs = append(pairs, key, cellData)
178 func isNotEmpty(nbIdentity *rnibentities.NbIdentity) bool {
179 return nbIdentity.GlobalNbId != nil && nbIdentity.GlobalNbId.PlmnId != "" && nbIdentity.GlobalNbId.NbId != ""