2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 // Copyright 2023 Capgemini
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
18 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 // platform project (RICP).
27 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
28 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
29 "github.com/golang/protobuf/proto"
33 E2TAddressesKey = "E2TAddresses"
34 RanAddedEvent = "ADDED"
35 RanUpdatedEvent = "UPDATED"
36 RanDeletedEvent = "DELETED"
39 type rNibWriterInstance struct {
40 sdl common.ISdlSyncStorage
41 rnibWriterConfig configuration.RnibWriterConfig
46 RNibWriter interface allows saving data to the redis DB
48 type RNibWriter interface {
49 SaveNodeb(nodebInfo *entities.NodebInfo) error
50 UpdateNodebInfo(nodebInfo *entities.NodebInfo) error
51 UpdateNodebInfoAndPublish(nodebInfo *entities.NodebInfo) error
52 SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) error
53 SaveE2TInstance(e2tInstance *entities.E2TInstance) error
54 SaveE2TAddresses(addresses []string) error
55 RemoveE2TInstance(e2tAddress string) error
56 UpdateGnbCells(nodebInfo *entities.NodebInfo, servedNrCells []*entities.ServedNRCell) error
57 RemoveServedNrCells(inventoryName string, servedNrCells []*entities.ServedNRCell) error
58 UpdateNodebInfoOnConnectionStatusInversion(nodebInfo *entities.NodebInfo, ent string) error
59 SaveGeneralConfiguration(config *entities.GeneralConfiguration) error
60 RemoveEnb(nodebInfo *entities.NodebInfo) error
61 RemoveServedCells(inventoryName string, servedCells []*entities.ServedCellInfo) error
62 UpdateEnb(nodebInfo *entities.NodebInfo, servedCells []*entities.ServedCellInfo) error
63 AddNbIdentity(nodeType entities.Node_Type, nbIdentity *entities.NbIdentity) error
64 RemoveNbIdentity(nodeType entities.Node_Type, nbIdentity *entities.NbIdentity) error
65 AddEnb(nodebInfo *entities.NodebInfo) error
66 UpdateNbIdentities(nodeType entities.Node_Type, oldNbIdentities []*entities.NbIdentity, newNbIdentities []*entities.NbIdentity) error
70 GetRNibWriter returns reference to RNibWriter
73 func GetRNibWriter(sdl common.ISdlSyncStorage, rnibWriterConfig configuration.RnibWriterConfig) RNibWriter {
74 return &rNibWriterInstance{
76 rnibWriterConfig: rnibWriterConfig,
77 ns: common.GetRNibNamespace(),
81 func getChannelsAndEventsPair(channel string, ranName string, event string) []string {
82 return []string{channel, fmt.Sprintf("%s_%s", ranName, event)}
85 func (w *rNibWriterInstance) AddNbIdentity(nodeType entities.Node_Type, nbIdentity *entities.NbIdentity) error {
86 nbIdData, err := proto.Marshal(nbIdentity)
89 return common.NewInternalError(err)
92 err = w.sdl.AddMember(w.ns, nodeType.String(), nbIdData)
95 return common.NewInternalError(err)
100 func (w *rNibWriterInstance) RemoveServedNrCells(inventoryName string, servedNrCells []*entities.ServedNRCell) error {
101 cellKeysToRemove := buildServedNRCellKeysToRemove(inventoryName, servedNrCells)
103 err := w.sdl.Remove(w.ns, cellKeysToRemove)
106 return common.NewInternalError(err)
112 func (w *rNibWriterInstance) RemoveServedCells(inventoryName string, servedCells []*entities.ServedCellInfo) error {
113 cellKeysToRemove := buildServedCellInfoKeysToRemove(inventoryName, servedCells)
115 err := w.sdl.Remove(w.ns, cellKeysToRemove)
118 return common.NewInternalError(err)
124 func (w *rNibWriterInstance) SaveGeneralConfiguration(config *entities.GeneralConfiguration) error {
126 err := w.SaveWithKeyAndMarshal(common.BuildGeneralConfigurationKey(), config)
129 return common.NewInternalError(err)
136 SaveNodeb saves nodeB entity data in the redis DB according to the specified data model
138 func (w *rNibWriterInstance) SaveNodeb(nodebInfo *entities.NodebInfo) error {
140 data, err := proto.Marshal(nodebInfo)
143 return common.NewInternalError(err)
146 var pairs []interface{}
147 key, rNibErr := common.ValidateAndBuildNodeBNameKey(nodebInfo.RanName)
153 pairs = append(pairs, key, data)
155 if nodebInfo.GlobalNbId != nil {
157 key, rNibErr = common.ValidateAndBuildNodeBIdKey(nodebInfo.GetNodeType().String(), nodebInfo.GlobalNbId.GetPlmnId(), nodebInfo.GlobalNbId.GetNbId(),nodebInfo.GetCuUpId(),nodebInfo.GetDuId())
161 pairs = append(pairs, key, data)
164 if nodebInfo.GetEnb() != nil {
165 pairs, rNibErr = appendEnbCells(nodebInfo.RanName, nodebInfo.GetEnb().GetServedCells(), pairs)
171 if nodebInfo.GetGnb() != nil {
172 pairs, rNibErr = appendGnbCells(nodebInfo.RanName, nodebInfo.GetGnb().GetServedNrCells(), pairs)
178 err = w.sdl.Set(w.ns, pairs)
181 return common.NewInternalError(err)
187 func (w *rNibWriterInstance) AddEnb(nodebInfo *entities.NodebInfo) error {
189 data, err := proto.Marshal(nodebInfo)
192 return common.NewInternalError(err)
195 var pairs []interface{}
196 key, rNibErr := common.ValidateAndBuildNodeBNameKey(nodebInfo.RanName)
202 pairs = append(pairs, key, data)
204 if nodebInfo.GlobalNbId != nil {
206 key, rNibErr = common.ValidateAndBuildNodeBIdKey(nodebInfo.GetNodeType().String(), nodebInfo.GlobalNbId.GetPlmnId(), nodebInfo.GlobalNbId.GetNbId(),nodebInfo.GetCuUpId(),nodebInfo.GetDuId())
210 pairs = append(pairs, key, data)
213 pairs, rNibErr = appendEnbCells(nodebInfo.RanName, nodebInfo.GetEnb().GetServedCells(), pairs)
218 channelsAndEvents := getChannelsAndEventsPair(w.rnibWriterConfig.RanManipulationMessageChannel, nodebInfo.RanName, RanAddedEvent)
219 err = w.sdl.SetAndPublish(w.ns, channelsAndEvents, pairs)
221 return common.NewInternalError(err)
227 func (w *rNibWriterInstance) UpdateNbIdentities(nodeType entities.Node_Type, oldNbIdentities []*entities.NbIdentity, newNbIdentities []*entities.NbIdentity) error {
229 nbIdIdentitiesToRemove, err := w.buildNbIdentitiesMembers(oldNbIdentities)
234 err = w.sdl.RemoveMember(w.ns, nodeType.String(), nbIdIdentitiesToRemove[:]...)
239 nbIdIdentitiesToAdd, err := w.buildNbIdentitiesMembers(newNbIdentities)
244 err = w.sdl.AddMember(w.ns, nodeType.String(), nbIdIdentitiesToAdd[:]...)
252 func (w *rNibWriterInstance) UpdateGnbCells(nodebInfo *entities.NodebInfo, servedNrCells []*entities.ServedNRCell) error {
254 pairs, err := buildUpdateNodebInfoPairs(nodebInfo)
260 pairs, err = appendGnbCells(nodebInfo.RanName, servedNrCells, pairs)
266 channelsAndEvents := getChannelsAndEventsPair(w.rnibWriterConfig.RanManipulationMessageChannel, nodebInfo.RanName, RanUpdatedEvent)
267 err = w.sdl.SetAndPublish(w.ns, channelsAndEvents, pairs)
270 return common.NewInternalError(err)
276 func buildServedNRCellKeysToRemove(inventoryName string, servedNrCellsToRemove []*entities.ServedNRCell) []string {
278 var cellKeysToRemove []string
280 for _, cell := range servedNrCellsToRemove {
282 key, _ := common.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
285 cellKeysToRemove = append(cellKeysToRemove, key)
288 key, _ = common.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetServedNrCellInformation().GetNrPci())
291 cellKeysToRemove = append(cellKeysToRemove, key)
295 return cellKeysToRemove
298 func buildServedCellInfoKeysToRemove(inventoryName string, servedCellsToRemove []*entities.ServedCellInfo) []string {
300 var cellKeysToRemove []string
302 for _, cell := range servedCellsToRemove {
304 key, _ := common.ValidateAndBuildCellIdKey(cell.GetCellId())
307 cellKeysToRemove = append(cellKeysToRemove, key)
310 key, _ = common.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetPci())
313 cellKeysToRemove = append(cellKeysToRemove, key)
317 return cellKeysToRemove
320 func buildUpdateNodebInfoPairs(nodebInfo *entities.NodebInfo) ([]interface{}, error) {
321 nodebNameKey, rNibErr := common.ValidateAndBuildNodeBNameKey(nodebInfo.GetRanName())
324 return []interface{}{}, rNibErr
327 nodebIdKey, buildNodebIdKeyError := common.ValidateAndBuildNodeBIdKey(nodebInfo.GetNodeType().String(), nodebInfo.GlobalNbId.GetPlmnId(), nodebInfo.GlobalNbId.GetNbId(),nodebInfo.GetCuUpId(),nodebInfo.GetDuId())
329 data, err := proto.Marshal(nodebInfo)
332 return []interface{}{}, common.NewInternalError(err)
335 pairs := []interface{}{nodebNameKey, data}
337 if buildNodebIdKeyError == nil {
338 pairs = append(pairs, nodebIdKey, data)
344 func (w *rNibWriterInstance) buildRemoveEnbKeys(nodebInfo *entities.NodebInfo) ([]string, error) {
345 keys := buildServedCellInfoKeysToRemove(nodebInfo.GetRanName(), nodebInfo.GetEnb().GetServedCells())
347 nodebNameKey, rNibErr := common.ValidateAndBuildNodeBNameKey(nodebInfo.GetRanName())
350 return []string{}, rNibErr
353 keys = append(keys, nodebNameKey)
355 nodebIdKey, buildNodebIdKeyError := common.ValidateAndBuildNodeBIdKey(nodebInfo.GetNodeType().String(), nodebInfo.GlobalNbId.GetPlmnId(), nodebInfo.GlobalNbId.GetNbId(),nodebInfo.GetCuUpId(),nodebInfo.GetDuId())
357 if buildNodebIdKeyError == nil {
358 keys = append(keys, nodebIdKey)
364 func (w *rNibWriterInstance) RemoveNbIdentity(nodeType entities.Node_Type, nbIdentity *entities.NbIdentity) error {
365 nbIdData, err := proto.Marshal(nbIdentity)
367 return common.NewInternalError(err)
369 err = w.sdl.RemoveMember(w.ns, nodeType.String(), nbIdData)
371 return common.NewInternalError(err)
376 func (w *rNibWriterInstance) updateNodebInfo(nodebInfo *entities.NodebInfo, publish bool) error {
378 pairs, err := buildUpdateNodebInfoPairs(nodebInfo)
385 channelsAndEvents := getChannelsAndEventsPair(w.rnibWriterConfig.RanManipulationMessageChannel, nodebInfo.RanName, RanUpdatedEvent)
386 err = w.sdl.SetAndPublish(w.ns, channelsAndEvents, pairs)
388 err = w.sdl.Set(w.ns, pairs)
392 return common.NewInternalError(err)
401 func (w *rNibWriterInstance) UpdateNodebInfo(nodebInfo *entities.NodebInfo) error {
402 return w.updateNodebInfo(nodebInfo, false)
406 UpdateNodebInfoAndPublish...
408 func (w *rNibWriterInstance) UpdateNodebInfoAndPublish(nodebInfo *entities.NodebInfo) error {
409 return w.updateNodebInfo(nodebInfo, true)
413 SaveRanLoadInformation stores ran load information for the provided ran
415 func (w *rNibWriterInstance) SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) error {
417 key, rnibErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
423 data, err := proto.Marshal(ranLoadInformation)
426 return common.NewInternalError(err)
429 var pairs []interface{}
430 pairs = append(pairs, key, data)
432 err = w.sdl.Set(w.ns, pairs)
435 return common.NewInternalError(err)
441 func (w *rNibWriterInstance) SaveE2TInstance(e2tInstance *entities.E2TInstance) error {
443 key, rnibErr := common.ValidateAndBuildE2TInstanceKey(e2tInstance.Address)
449 data, err := json.Marshal(e2tInstance)
452 return common.NewInternalError(err)
455 var pairs []interface{}
456 pairs = append(pairs, key, data)
458 err = w.sdl.Set(w.ns, pairs)
461 return common.NewInternalError(err)
467 func (w *rNibWriterInstance) SaveE2TAddresses(addresses []string) error {
469 data, err := json.Marshal(addresses)
472 return common.NewInternalError(err)
475 var pairs []interface{}
476 pairs = append(pairs, E2TAddressesKey, data)
478 err = w.sdl.Set(w.ns, pairs)
481 return common.NewInternalError(err)
487 func (w *rNibWriterInstance) RemoveE2TInstance(address string) error {
488 key, rNibErr := common.ValidateAndBuildE2TInstanceKey(address)
492 err := w.sdl.Remove(w.ns, []string{key})
495 return common.NewInternalError(err)
500 func (w *rNibWriterInstance) SaveWithKeyAndMarshal(key string, entity interface{}) error {
502 data, err := json.Marshal(entity)
505 return common.NewInternalError(err)
508 var pairs []interface{}
509 pairs = append(pairs, key, data)
511 err = w.sdl.Set(w.ns, pairs)
514 return common.NewInternalError(err)
521 UpdateNodebInfoOnConnectionStatusInversion...
523 func (w *rNibWriterInstance) UpdateNodebInfoOnConnectionStatusInversion(nodebInfo *entities.NodebInfo, event string) error {
525 pairs, err := buildUpdateNodebInfoPairs(nodebInfo)
531 err = w.sdl.SetAndPublish(w.ns, []string{w.rnibWriterConfig.StateChangeMessageChannel, event}, pairs)
534 return common.NewInternalError(err)
540 func (w *rNibWriterInstance) RemoveEnb(nodebInfo *entities.NodebInfo) error {
541 keysToRemove, err := w.buildRemoveEnbKeys(nodebInfo)
546 channelsAndEvents := getChannelsAndEventsPair(w.rnibWriterConfig.RanManipulationMessageChannel, nodebInfo.RanName, RanDeletedEvent)
547 err = w.sdl.RemoveAndPublish(w.ns, channelsAndEvents, keysToRemove)
550 return common.NewInternalError(err)
556 func (w *rNibWriterInstance) UpdateEnb(nodebInfo *entities.NodebInfo, servedCells []*entities.ServedCellInfo) error {
558 pairs, err := buildUpdateNodebInfoPairs(nodebInfo)
564 pairs, err = appendEnbCells(nodebInfo.RanName, servedCells, pairs)
570 channelsAndEvents := getChannelsAndEventsPair(w.rnibWriterConfig.RanManipulationMessageChannel, nodebInfo.RanName, RanUpdatedEvent)
571 err = w.sdl.SetAndPublish(w.ns, channelsAndEvents, pairs)
574 return common.NewInternalError(err)
580 func (w *rNibWriterInstance) buildNbIdentitiesMembers(nbIdentities []*entities.NbIdentity) ([]interface{}, error) {
582 var nbIdIdentitiesMembers []interface{}
583 for _, nbIdentity := range nbIdentities {
585 nbIdData, err := proto.Marshal(nbIdentity)
587 return nil, common.NewInternalError(err)
589 nbIdIdentitiesMembers = append(nbIdIdentitiesMembers, nbIdData)
592 return nbIdIdentitiesMembers, nil
602 func appendEnbCells(inventoryName string, cells []*entities.ServedCellInfo, pairs []interface{}) ([]interface{}, error) {
603 for _, cell := range cells {
604 cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}}
605 cellData, err := proto.Marshal(&cellEntity)
607 return pairs, common.NewInternalError(err)
609 key, rNibErr := common.ValidateAndBuildCellIdKey(cell.GetCellId())
611 return pairs, rNibErr
613 pairs = append(pairs, key, cellData)
614 key, rNibErr = common.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetPci())
616 return pairs, rNibErr
618 pairs = append(pairs, key, cellData)
623 func appendGnbCells(inventoryName string, cells []*entities.ServedNRCell, pairs []interface{}) ([]interface{}, error) {
624 for _, cell := range cells {
625 cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: cell}}
626 cellData, err := proto.Marshal(&cellEntity)
628 return pairs, common.NewInternalError(err)
630 key, rNibErr := common.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
632 return pairs, rNibErr
634 pairs = append(pairs, key, cellData)
635 key, rNibErr = common.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetServedNrCellInformation().GetNrPci())
637 return pairs, rNibErr
639 pairs = append(pairs, key, cellData)