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.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
26 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
27 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
28 "github.com/golang/protobuf/proto"
32 E2TAddressesKey = "E2TAddresses"
33 RanAddedEvent = "ADDED"
34 RanUpdatedEvent = "UPDATED"
35 RanDeletedEvent = "DELETED"
38 type rNibWriterInstance struct {
39 sdl common.ISdlInstance
40 rnibWriterConfig configuration.RnibWriterConfig
44 RNibWriter interface allows saving data to the redis DB
46 type RNibWriter interface {
47 SaveNodeb(nodebInfo *entities.NodebInfo) error
48 UpdateNodebInfo(nodebInfo *entities.NodebInfo) error
49 UpdateNodebInfoAndPublish(nodebInfo *entities.NodebInfo) error
50 SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) error
51 SaveE2TInstance(e2tInstance *entities.E2TInstance) error
52 SaveE2TAddresses(addresses []string) error
53 RemoveE2TInstance(e2tAddress string) error
54 UpdateGnbCells(nodebInfo *entities.NodebInfo, servedNrCells []*entities.ServedNRCell) error
55 RemoveServedNrCells(inventoryName string, servedNrCells []*entities.ServedNRCell) error
56 UpdateNodebInfoOnConnectionStatusInversion(nodebInfo *entities.NodebInfo, ent string) error
57 SaveGeneralConfiguration(config *entities.GeneralConfiguration) error
58 RemoveEnb(nodebInfo *entities.NodebInfo) error
59 RemoveServedCells(inventoryName string, servedCells []*entities.ServedCellInfo) error
60 UpdateEnb(nodebInfo *entities.NodebInfo, servedCells []*entities.ServedCellInfo) error
61 AddNbIdentity(nodeType entities.Node_Type, nbIdentity *entities.NbIdentity) error
62 RemoveNbIdentity(nodeType entities.Node_Type, nbIdentity *entities.NbIdentity) error
63 AddEnb(nodebInfo *entities.NodebInfo) error
64 UpdateNbIdentities(nodeType entities.Node_Type, oldNbIdentities []*entities.NbIdentity, newNbIdentities []*entities.NbIdentity) error
67 GetRNibWriter returns reference to RNibWriter
70 func GetRNibWriter(sdl common.ISdlInstance, rnibWriterConfig configuration.RnibWriterConfig) RNibWriter {
71 return &rNibWriterInstance{sdl: sdl, rnibWriterConfig: rnibWriterConfig}
74 func getChannelsAndEventsPair(channel string, ranName string, event string) []string {
75 return []string{channel, fmt.Sprintf("%s_%s", ranName, event)}
78 func (w *rNibWriterInstance) AddNbIdentity(nodeType entities.Node_Type, nbIdentity *entities.NbIdentity) error {
79 nbIdData, err := proto.Marshal(nbIdentity)
82 return common.NewInternalError(err)
85 err = w.sdl.AddMember(nodeType.String(), nbIdData)
88 return common.NewInternalError(err)
93 func (w *rNibWriterInstance) RemoveServedNrCells(inventoryName string, servedNrCells []*entities.ServedNRCell) error {
94 cellKeysToRemove := buildServedNRCellKeysToRemove(inventoryName, servedNrCells)
96 err := w.sdl.Remove(cellKeysToRemove)
99 return common.NewInternalError(err)
105 func (w *rNibWriterInstance) RemoveServedCells(inventoryName string, servedCells []*entities.ServedCellInfo) error {
106 cellKeysToRemove := buildServedCellInfoKeysToRemove(inventoryName, servedCells)
108 err := w.sdl.Remove(cellKeysToRemove)
111 return common.NewInternalError(err)
117 func (w *rNibWriterInstance) SaveGeneralConfiguration(config *entities.GeneralConfiguration) error {
119 err := w.SaveWithKeyAndMarshal(common.BuildGeneralConfigurationKey(), config)
122 return common.NewInternalError(err)
129 SaveNodeb saves nodeB entity data in the redis DB according to the specified data model
131 func (w *rNibWriterInstance) SaveNodeb(nodebInfo *entities.NodebInfo) error {
133 data, err := proto.Marshal(nodebInfo)
136 return common.NewInternalError(err)
139 var pairs []interface{}
140 key, rNibErr := common.ValidateAndBuildNodeBNameKey(nodebInfo.RanName)
146 pairs = append(pairs, key, data)
148 if nodebInfo.GlobalNbId != nil {
150 key, rNibErr = common.ValidateAndBuildNodeBIdKey(nodebInfo.GetNodeType().String(), nodebInfo.GlobalNbId.GetPlmnId(), nodebInfo.GlobalNbId.GetNbId())
154 pairs = append(pairs, key, data)
157 if nodebInfo.GetEnb() != nil {
158 pairs, rNibErr = appendEnbCells(nodebInfo.RanName, nodebInfo.GetEnb().GetServedCells(), pairs)
164 if nodebInfo.GetGnb() != nil {
165 pairs, rNibErr = appendGnbCells(nodebInfo.RanName, nodebInfo.GetGnb().GetServedNrCells(), pairs)
171 err = w.sdl.Set(pairs)
174 return common.NewInternalError(err)
180 func (w *rNibWriterInstance) AddEnb(nodebInfo *entities.NodebInfo) error {
182 data, err := proto.Marshal(nodebInfo)
185 return common.NewInternalError(err)
188 var pairs []interface{}
189 key, rNibErr := common.ValidateAndBuildNodeBNameKey(nodebInfo.RanName)
195 pairs = append(pairs, key, data)
197 if nodebInfo.GlobalNbId != nil {
199 key, rNibErr = common.ValidateAndBuildNodeBIdKey(nodebInfo.GetNodeType().String(), nodebInfo.GlobalNbId.GetPlmnId(), nodebInfo.GlobalNbId.GetNbId())
203 pairs = append(pairs, key, data)
206 pairs, rNibErr = appendEnbCells(nodebInfo.RanName, nodebInfo.GetEnb().GetServedCells(), pairs)
211 channelsAndEvents := getChannelsAndEventsPair(w.rnibWriterConfig.RanManipulationMessageChannel, nodebInfo.RanName, RanAddedEvent)
212 err = w.sdl.SetAndPublish(channelsAndEvents, pairs)
214 return common.NewInternalError(err)
220 func (w *rNibWriterInstance) UpdateNbIdentities(nodeType entities.Node_Type, oldNbIdentities []*entities.NbIdentity, newNbIdentities []*entities.NbIdentity) error {
222 nbIdIdentitiesToRemove, err := w.buildNbIdentitiesMembers(oldNbIdentities)
227 err = w.sdl.RemoveMember(nodeType.String(), nbIdIdentitiesToRemove[:]...)
232 nbIdIdentitiesToAdd, err := w.buildNbIdentitiesMembers(newNbIdentities)
237 err = w.sdl.AddMember(nodeType.String(), nbIdIdentitiesToAdd[:]...)
245 func (w *rNibWriterInstance) UpdateGnbCells(nodebInfo *entities.NodebInfo, servedNrCells []*entities.ServedNRCell) error {
247 pairs, err := buildUpdateNodebInfoPairs(nodebInfo)
253 pairs, err = appendGnbCells(nodebInfo.RanName, servedNrCells, pairs)
259 channelsAndEvents := getChannelsAndEventsPair(w.rnibWriterConfig.RanManipulationMessageChannel, nodebInfo.RanName, RanUpdatedEvent)
260 err = w.sdl.SetAndPublish(channelsAndEvents, pairs)
263 return common.NewInternalError(err)
269 func buildServedNRCellKeysToRemove(inventoryName string, servedNrCellsToRemove []*entities.ServedNRCell) []string {
271 var cellKeysToRemove []string
273 for _, cell := range servedNrCellsToRemove {
275 key, _ := common.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
278 cellKeysToRemove = append(cellKeysToRemove, key)
281 key, _ = common.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetServedNrCellInformation().GetNrPci())
284 cellKeysToRemove = append(cellKeysToRemove, key)
288 return cellKeysToRemove
291 func buildServedCellInfoKeysToRemove(inventoryName string, servedCellsToRemove []*entities.ServedCellInfo) []string {
293 var cellKeysToRemove []string
295 for _, cell := range servedCellsToRemove {
297 key, _ := common.ValidateAndBuildCellIdKey(cell.GetCellId())
300 cellKeysToRemove = append(cellKeysToRemove, key)
303 key, _ = common.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetPci())
306 cellKeysToRemove = append(cellKeysToRemove, key)
310 return cellKeysToRemove
313 func buildUpdateNodebInfoPairs(nodebInfo *entities.NodebInfo) ([]interface{}, error) {
314 nodebNameKey, rNibErr := common.ValidateAndBuildNodeBNameKey(nodebInfo.GetRanName())
317 return []interface{}{}, rNibErr
320 nodebIdKey, buildNodebIdKeyError := common.ValidateAndBuildNodeBIdKey(nodebInfo.GetNodeType().String(), nodebInfo.GlobalNbId.GetPlmnId(), nodebInfo.GlobalNbId.GetNbId())
322 data, err := proto.Marshal(nodebInfo)
325 return []interface{}{}, common.NewInternalError(err)
328 pairs := []interface{}{nodebNameKey, data}
330 if buildNodebIdKeyError == nil {
331 pairs = append(pairs, nodebIdKey, data)
337 func (w *rNibWriterInstance) buildRemoveEnbKeys(nodebInfo *entities.NodebInfo) ([]string, error) {
338 keys := buildServedCellInfoKeysToRemove(nodebInfo.GetRanName(), nodebInfo.GetEnb().GetServedCells())
340 nodebNameKey, rNibErr := common.ValidateAndBuildNodeBNameKey(nodebInfo.GetRanName())
343 return []string{}, rNibErr
346 keys = append(keys, nodebNameKey)
348 nodebIdKey, buildNodebIdKeyError := common.ValidateAndBuildNodeBIdKey(nodebInfo.GetNodeType().String(), nodebInfo.GlobalNbId.GetPlmnId(), nodebInfo.GlobalNbId.GetNbId())
350 if buildNodebIdKeyError == nil {
351 keys = append(keys, nodebIdKey)
357 func (w *rNibWriterInstance) RemoveNbIdentity(nodeType entities.Node_Type, nbIdentity *entities.NbIdentity) error {
358 nbIdData, err := proto.Marshal(nbIdentity)
360 return common.NewInternalError(err)
362 err = w.sdl.RemoveMember(nodeType.String(), nbIdData)
364 return common.NewInternalError(err)
369 func (w *rNibWriterInstance) updateNodebInfo(nodebInfo *entities.NodebInfo, publish bool) error {
371 pairs, err := buildUpdateNodebInfoPairs(nodebInfo)
378 channelsAndEvents := getChannelsAndEventsPair(w.rnibWriterConfig.RanManipulationMessageChannel, nodebInfo.RanName, RanUpdatedEvent)
379 err = w.sdl.SetAndPublish(channelsAndEvents, pairs)
381 err = w.sdl.Set(pairs)
385 return common.NewInternalError(err)
394 func (w *rNibWriterInstance) UpdateNodebInfo(nodebInfo *entities.NodebInfo) error {
395 return w.updateNodebInfo(nodebInfo, false)
399 UpdateNodebInfoAndPublish...
401 func (w *rNibWriterInstance) UpdateNodebInfoAndPublish(nodebInfo *entities.NodebInfo) error {
402 return w.updateNodebInfo(nodebInfo, true)
407 SaveRanLoadInformation stores ran load information for the provided ran
409 func (w *rNibWriterInstance) SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) error {
411 key, rnibErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
417 data, err := proto.Marshal(ranLoadInformation)
420 return common.NewInternalError(err)
423 var pairs []interface{}
424 pairs = append(pairs, key, data)
426 err = w.sdl.Set(pairs)
429 return common.NewInternalError(err)
435 func (w *rNibWriterInstance) SaveE2TInstance(e2tInstance *entities.E2TInstance) error {
437 key, rnibErr := common.ValidateAndBuildE2TInstanceKey(e2tInstance.Address)
443 data, err := json.Marshal(e2tInstance)
446 return common.NewInternalError(err)
449 var pairs []interface{}
450 pairs = append(pairs, key, data)
452 err = w.sdl.Set(pairs)
455 return common.NewInternalError(err)
461 func (w *rNibWriterInstance) SaveE2TAddresses(addresses []string) error {
463 data, err := json.Marshal(addresses)
466 return common.NewInternalError(err)
469 var pairs []interface{}
470 pairs = append(pairs, E2TAddressesKey, data)
472 err = w.sdl.Set(pairs)
475 return common.NewInternalError(err)
481 func (w *rNibWriterInstance) RemoveE2TInstance(address string) error {
482 key, rNibErr := common.ValidateAndBuildE2TInstanceKey(address)
486 err := w.sdl.Remove([]string{key})
489 return common.NewInternalError(err)
494 func (w *rNibWriterInstance) SaveWithKeyAndMarshal(key string, entity interface{}) error {
496 data, err := json.Marshal(entity)
499 return common.NewInternalError(err)
502 var pairs []interface{}
503 pairs = append(pairs, key, data)
505 err = w.sdl.Set(pairs)
508 return common.NewInternalError(err)
515 UpdateNodebInfoOnConnectionStatusInversion...
517 func (w *rNibWriterInstance) UpdateNodebInfoOnConnectionStatusInversion(nodebInfo *entities.NodebInfo, event string) error {
519 pairs, err := buildUpdateNodebInfoPairs(nodebInfo)
525 err = w.sdl.SetAndPublish([]string{w.rnibWriterConfig.StateChangeMessageChannel, event}, pairs)
528 return common.NewInternalError(err)
534 func (w *rNibWriterInstance) RemoveEnb(nodebInfo *entities.NodebInfo) error {
535 keysToRemove, err := w.buildRemoveEnbKeys(nodebInfo)
540 channelsAndEvents := getChannelsAndEventsPair(w.rnibWriterConfig.RanManipulationMessageChannel, nodebInfo.RanName, RanDeletedEvent)
541 err = w.sdl.RemoveAndPublish(channelsAndEvents, keysToRemove)
544 return common.NewInternalError(err)
550 func (w *rNibWriterInstance) UpdateEnb(nodebInfo *entities.NodebInfo, servedCells []*entities.ServedCellInfo) error {
552 pairs, err := buildUpdateNodebInfoPairs(nodebInfo)
558 pairs, err = appendEnbCells(nodebInfo.RanName, servedCells, pairs)
564 channelsAndEvents := getChannelsAndEventsPair(w.rnibWriterConfig.RanManipulationMessageChannel, nodebInfo.RanName, RanUpdatedEvent)
565 err = w.sdl.SetAndPublish(channelsAndEvents, pairs)
568 return common.NewInternalError(err)
574 func (w *rNibWriterInstance) buildNbIdentitiesMembers(nbIdentities []*entities.NbIdentity) ([]interface{}, error) {
576 var nbIdIdentitiesMembers []interface{}
577 for _, nbIdentity := range nbIdentities {
579 nbIdData, err := proto.Marshal(nbIdentity)
581 return nil, common.NewInternalError(err)
583 nbIdIdentitiesMembers = append(nbIdIdentitiesMembers, nbIdData)
586 return nbIdIdentitiesMembers, nil
595 func appendEnbCells(inventoryName string, cells []*entities.ServedCellInfo, pairs []interface{}) ([]interface{}, error) {
596 for _, cell := range cells {
597 cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}}
598 cellData, err := proto.Marshal(&cellEntity)
600 return pairs, common.NewInternalError(err)
602 key, rNibErr := common.ValidateAndBuildCellIdKey(cell.GetCellId())
604 return pairs, rNibErr
606 pairs = append(pairs, key, cellData)
607 key, rNibErr = common.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetPci())
609 return pairs, rNibErr
611 pairs = append(pairs, key, cellData)
616 func appendGnbCells(inventoryName string, cells []*entities.ServedNRCell, pairs []interface{}) ([]interface{}, error) {
617 for _, cell := range cells {
618 cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: cell}}
619 cellData, err := proto.Marshal(&cellEntity)
621 return pairs, common.NewInternalError(err)
623 key, rNibErr := common.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
625 return pairs, rNibErr
627 pairs = append(pairs, key, cellData)
628 key, rNibErr = common.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetServedNrCellInformation().GetNrPci())
630 return pairs, rNibErr
632 pairs = append(pairs, key, cellData)