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 "github.com/golang/protobuf/proto"
28 const E2TAddressesKey = "E2TAddresses"
30 type rNibWriterInstance struct {
31 sdl common.ISdlInstance
35 RNibWriter interface allows saving data to the redis DB
37 type RNibWriter interface {
38 SaveNodeb(nbIdentity *entities.NbIdentity, nb *entities.NodebInfo) error
39 UpdateNodebInfo(nodebInfo *entities.NodebInfo) error
40 SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) error
41 SaveE2TInstance(e2tInstance *entities.E2TInstance) error
42 SaveE2TAddresses(addresses []string) error
46 GetRNibWriter returns reference to RNibWriter
49 func GetRNibWriter(sdl common.ISdlInstance) RNibWriter {
50 return &rNibWriterInstance{sdl: sdl}
54 SaveNodeb saves nodeB entity data in the redis DB according to the specified data model
56 func (w *rNibWriterInstance) SaveNodeb(nbIdentity *entities.NbIdentity, entity *entities.NodebInfo) error {
57 isNotEmptyIdentity := isNotEmpty(nbIdentity)
59 if isNotEmptyIdentity && entity.GetNodeType() == entities.Node_UNKNOWN {
60 return common.NewValidationError(fmt.Sprintf("#rNibWriter.saveNodeB - Unknown responding node type, entity: %v", entity))
62 data, err := proto.Marshal(entity)
64 return common.NewInternalError(err)
66 var pairs []interface{}
67 key, rNibErr := common.ValidateAndBuildNodeBNameKey(nbIdentity.InventoryName)
71 pairs = append(pairs, key, data)
73 if isNotEmptyIdentity {
74 key, rNibErr = common.ValidateAndBuildNodeBIdKey(entity.GetNodeType().String(), nbIdentity.GlobalNbId.GetPlmnId(), nbIdentity.GlobalNbId.GetNbId())
78 pairs = append(pairs, key, data)
81 if entity.GetEnb() != nil {
82 pairs, rNibErr = appendEnbCells(nbIdentity, entity.GetEnb().GetServedCells(), pairs)
87 if entity.GetGnb() != nil {
88 pairs, rNibErr = appendGnbCells(nbIdentity, entity.GetGnb().GetServedNrCells(), pairs)
93 err = w.sdl.Set(pairs)
95 return common.NewInternalError(err)
98 ranNameIdentity := &entities.NbIdentity{InventoryName: nbIdentity.InventoryName}
100 if isNotEmptyIdentity {
101 nbIdData, err := proto.Marshal(ranNameIdentity)
103 return common.NewInternalError(err)
105 err = w.sdl.RemoveMember(entities.Node_UNKNOWN.String(), nbIdData)
107 return common.NewInternalError(err)
110 nbIdentity = ranNameIdentity
113 nbIdData, err := proto.Marshal(nbIdentity)
115 return common.NewInternalError(err)
117 err = w.sdl.AddMember(entity.GetNodeType().String(), nbIdData)
119 return common.NewInternalError(err)
127 func (w *rNibWriterInstance) UpdateNodebInfo(nodebInfo *entities.NodebInfo) error {
129 nodebNameKey, rNibErr := common.ValidateAndBuildNodeBNameKey(nodebInfo.GetRanName())
135 nodebIdKey, buildNodebIdKeyError := common.ValidateAndBuildNodeBIdKey(nodebInfo.GetNodeType().String(), nodebInfo.GlobalNbId.GetPlmnId(), nodebInfo.GlobalNbId.GetNbId())
137 data, err := proto.Marshal(nodebInfo)
140 return common.NewInternalError(err)
143 var pairs []interface{}
144 pairs = append(pairs, nodebNameKey, data)
146 if buildNodebIdKeyError == nil {
147 pairs = append(pairs, nodebIdKey, data)
150 err = w.sdl.Set(pairs)
153 return common.NewInternalError(err)
160 SaveRanLoadInformation stores ran load information for the provided ran
162 func (w *rNibWriterInstance) SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) error {
164 key, rnibErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
170 data, err := proto.Marshal(ranLoadInformation)
173 return common.NewInternalError(err)
176 var pairs []interface{}
177 pairs = append(pairs, key, data)
179 err = w.sdl.Set(pairs)
182 return common.NewInternalError(err)
188 func (w *rNibWriterInstance) SaveE2TInstance(e2tInstance *entities.E2TInstance) error {
190 key, rnibErr := common.ValidateAndBuildE2TInstanceKey(e2tInstance.Address)
196 data, err := json.Marshal(e2tInstance)
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)
214 func (w *rNibWriterInstance) SaveE2TAddresses(addresses []string) error {
216 data, err := json.Marshal(addresses)
219 return common.NewInternalError(err)
222 var pairs []interface{}
223 pairs = append(pairs, E2TAddressesKey, data)
225 err = w.sdl.Set(pairs)
228 return common.NewInternalError(err)
241 func appendEnbCells(nbIdentity *entities.NbIdentity, cells []*entities.ServedCellInfo, pairs []interface{}) ([]interface{}, error) {
242 for _, cell := range cells {
243 cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}}
244 cellData, err := proto.Marshal(&cellEntity)
246 return pairs, common.NewInternalError(err)
248 key, rNibErr := common.ValidateAndBuildCellIdKey(cell.GetCellId())
250 return pairs, rNibErr
252 pairs = append(pairs, key, cellData)
253 key, rNibErr = common.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetPci())
255 return pairs, rNibErr
257 pairs = append(pairs, key, cellData)
262 func appendGnbCells(nbIdentity *entities.NbIdentity, cells []*entities.ServedNRCell, pairs []interface{}) ([]interface{}, error) {
263 for _, cell := range cells {
264 cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: cell}}
265 cellData, err := proto.Marshal(&cellEntity)
267 return pairs, common.NewInternalError(err)
269 key, rNibErr := common.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
271 return pairs, rNibErr
273 pairs = append(pairs, key, cellData)
274 key, rNibErr = common.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetServedNrCellInformation().GetNrPci())
276 return pairs, rNibErr
278 pairs = append(pairs, key, cellData)
283 func isNotEmpty(nbIdentity *entities.NbIdentity) bool {
284 return nbIdentity.GlobalNbId != nil && nbIdentity.GlobalNbId.PlmnId != "" && nbIdentity.GlobalNbId.NbId != ""