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 SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) error
50 SaveE2TInstance(e2tInstance *entities.E2TInstance) error
51 SaveE2TAddresses(addresses []string) error
52 RemoveE2TInstance(e2tAddress string) error
53 UpdateGnbCells(nodebInfo *entities.NodebInfo, servedNrCells []*entities.ServedNRCell) error
54 RemoveServedNrCells(inventoryName string, servedNrCells []*entities.ServedNRCell) error
55 UpdateNodebInfoOnConnectionStatusInversion(nodebInfo *entities.NodebInfo, ent string) error
56 SaveGeneralConfiguration(config *entities.GeneralConfiguration) error
57 RemoveEnb(nodebInfo *entities.NodebInfo) error
58 RemoveServedCells(inventoryName string, servedCells []*entities.ServedCellInfo) error
59 UpdateEnb(nodebInfo *entities.NodebInfo, servedCells []*entities.ServedCellInfo) error
60 AddNbIdentity(nodeType entities.Node_Type, nbIdentity *entities.NbIdentity) error
61 RemoveNbIdentity(nodeType entities.Node_Type, nbIdentity *entities.NbIdentity) error
65 GetRNibWriter returns reference to RNibWriter
68 func GetRNibWriter(sdl common.ISdlInstance, rnibWriterConfig configuration.RnibWriterConfig) RNibWriter {
69 return &rNibWriterInstance{sdl: sdl, rnibWriterConfig: rnibWriterConfig}
72 func (w *rNibWriterInstance) AddNbIdentity(nodeType entities.Node_Type, nbIdentity *entities.NbIdentity) error {
73 nbIdData, err := proto.Marshal(nbIdentity)
76 return common.NewInternalError(err)
79 err = w.sdl.AddMember(nodeType.String(), nbIdData)
82 return common.NewInternalError(err)
87 func (w *rNibWriterInstance) RemoveServedNrCells(inventoryName string, servedNrCells []*entities.ServedNRCell) error {
88 cellKeysToRemove := buildServedNRCellKeysToRemove(inventoryName, servedNrCells)
90 err := w.sdl.Remove(cellKeysToRemove)
93 return common.NewInternalError(err)
99 func (w *rNibWriterInstance) RemoveServedCells(inventoryName string, servedCells []*entities.ServedCellInfo) error {
100 cellKeysToRemove := buildServedCellInfoKeysToRemove(inventoryName, servedCells)
102 err := w.sdl.Remove(cellKeysToRemove)
105 return common.NewInternalError(err)
111 func (w *rNibWriterInstance) SaveGeneralConfiguration(config *entities.GeneralConfiguration) error {
113 err := w.SaveWithKeyAndMarshal(common.BuildGeneralConfigurationKey(), config)
116 return common.NewInternalError(err)
123 SaveNodeb saves nodeB entity data in the redis DB according to the specified data model
125 func (w *rNibWriterInstance) SaveNodeb(nodebInfo *entities.NodebInfo) error {
127 data, err := proto.Marshal(nodebInfo)
130 return common.NewInternalError(err)
133 var pairs []interface{}
134 key, rNibErr := common.ValidateAndBuildNodeBNameKey(nodebInfo.RanName)
140 pairs = append(pairs, key, data)
142 if nodebInfo.GlobalNbId != nil {
144 key, rNibErr = common.ValidateAndBuildNodeBIdKey(nodebInfo.GetNodeType().String(), nodebInfo.GlobalNbId.GetPlmnId(), nodebInfo.GlobalNbId.GetNbId())
148 pairs = append(pairs, key, data)
151 if nodebInfo.GetEnb() != nil {
152 pairs, rNibErr = appendEnbCells(nodebInfo.RanName, nodebInfo.GetEnb().GetServedCells(), pairs)
158 if nodebInfo.GetGnb() != nil {
159 pairs, rNibErr = appendGnbCells(nodebInfo.RanName, nodebInfo.GetGnb().GetServedNrCells(), pairs)
165 if nodebInfo.GetNodeType() == entities.Node_ENB {
166 err = w.sdl.SetAndPublish([]string{w.rnibWriterConfig.RanManipulationMessageChannel, fmt.Sprintf("%s_%s", nodebInfo.RanName, RanAddedEvent)}, pairs)
168 err = w.sdl.Set(pairs)
172 return common.NewInternalError(err)
178 func (w *rNibWriterInstance) UpdateGnbCells(nodebInfo *entities.NodebInfo, servedNrCells []*entities.ServedNRCell) error {
180 pairs, err := buildUpdateNodebInfoPairs(nodebInfo)
186 pairs, err = appendGnbCells(nodebInfo.RanName, servedNrCells, pairs)
192 err = w.sdl.Set(pairs)
195 return common.NewInternalError(err)
201 func buildServedNRCellKeysToRemove(inventoryName string, servedNrCellsToRemove []*entities.ServedNRCell) []string {
203 var cellKeysToRemove []string
205 for _, cell := range servedNrCellsToRemove {
207 key, _ := common.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
210 cellKeysToRemove = append(cellKeysToRemove, key)
213 key, _ = common.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetServedNrCellInformation().GetNrPci())
216 cellKeysToRemove = append(cellKeysToRemove, key)
220 return cellKeysToRemove
223 func buildServedCellInfoKeysToRemove(inventoryName string, servedCellsToRemove []*entities.ServedCellInfo) []string {
225 var cellKeysToRemove []string
227 for _, cell := range servedCellsToRemove {
229 key, _ := common.ValidateAndBuildCellIdKey(cell.GetCellId())
232 cellKeysToRemove = append(cellKeysToRemove, key)
235 key, _ = common.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetPci())
238 cellKeysToRemove = append(cellKeysToRemove, key)
242 return cellKeysToRemove
245 func buildUpdateNodebInfoPairs(nodebInfo *entities.NodebInfo) ([]interface{}, error) {
246 nodebNameKey, rNibErr := common.ValidateAndBuildNodeBNameKey(nodebInfo.GetRanName())
249 return []interface{}{}, rNibErr
252 nodebIdKey, buildNodebIdKeyError := common.ValidateAndBuildNodeBIdKey(nodebInfo.GetNodeType().String(), nodebInfo.GlobalNbId.GetPlmnId(), nodebInfo.GlobalNbId.GetNbId())
254 data, err := proto.Marshal(nodebInfo)
257 return []interface{}{}, common.NewInternalError(err)
260 pairs := []interface{}{nodebNameKey, data}
262 if buildNodebIdKeyError == nil {
263 pairs = append(pairs, nodebIdKey, data)
269 func (w *rNibWriterInstance) buildRemoveEnbKeys(nodebInfo *entities.NodebInfo) ([]string, error) {
270 keys := buildServedCellInfoKeysToRemove(nodebInfo.GetRanName(), nodebInfo.GetEnb().GetServedCells())
272 nodebNameKey, rNibErr := common.ValidateAndBuildNodeBNameKey(nodebInfo.GetRanName())
275 return []string{}, rNibErr
278 keys = append(keys, nodebNameKey)
280 nodebIdKey, buildNodebIdKeyError := common.ValidateAndBuildNodeBIdKey(nodebInfo.GetNodeType().String(), nodebInfo.GlobalNbId.GetPlmnId(), nodebInfo.GlobalNbId.GetNbId())
282 if buildNodebIdKeyError == nil {
283 keys = append(keys, nodebIdKey)
289 func (w *rNibWriterInstance) RemoveNbIdentity(nodeType entities.Node_Type, nbIdentity *entities.NbIdentity) error {
290 nbIdData, err := proto.Marshal(nbIdentity)
292 return common.NewInternalError(err)
294 err = w.sdl.RemoveMember(nodeType.String(), nbIdData)
296 return common.NewInternalError(err)
304 func (w *rNibWriterInstance) UpdateNodebInfo(nodebInfo *entities.NodebInfo) error {
306 pairs, err := buildUpdateNodebInfoPairs(nodebInfo)
312 err = w.sdl.Set(pairs)
315 return common.NewInternalError(err)
322 SaveRanLoadInformation stores ran load information for the provided ran
324 func (w *rNibWriterInstance) SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) error {
326 key, rnibErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
332 data, err := proto.Marshal(ranLoadInformation)
335 return common.NewInternalError(err)
338 var pairs []interface{}
339 pairs = append(pairs, key, data)
341 err = w.sdl.Set(pairs)
344 return common.NewInternalError(err)
350 func (w *rNibWriterInstance) SaveE2TInstance(e2tInstance *entities.E2TInstance) error {
352 key, rnibErr := common.ValidateAndBuildE2TInstanceKey(e2tInstance.Address)
358 data, err := json.Marshal(e2tInstance)
361 return common.NewInternalError(err)
364 var pairs []interface{}
365 pairs = append(pairs, key, data)
367 err = w.sdl.Set(pairs)
370 return common.NewInternalError(err)
376 func (w *rNibWriterInstance) SaveE2TAddresses(addresses []string) error {
378 data, err := json.Marshal(addresses)
381 return common.NewInternalError(err)
384 var pairs []interface{}
385 pairs = append(pairs, E2TAddressesKey, data)
387 err = w.sdl.Set(pairs)
390 return common.NewInternalError(err)
396 func (w *rNibWriterInstance) RemoveE2TInstance(address string) error {
397 key, rNibErr := common.ValidateAndBuildE2TInstanceKey(address)
401 err := w.sdl.Remove([]string{key})
404 return common.NewInternalError(err)
409 func (w *rNibWriterInstance) SaveWithKeyAndMarshal(key string, entity interface{}) error {
411 data, err := json.Marshal(entity)
414 return common.NewInternalError(err)
417 var pairs []interface{}
418 pairs = append(pairs, key, data)
420 err = w.sdl.Set(pairs)
423 return common.NewInternalError(err)
430 UpdateNodebInfoOnConnectionStatusInversion...
432 func (w *rNibWriterInstance) UpdateNodebInfoOnConnectionStatusInversion(nodebInfo *entities.NodebInfo, event string) error {
434 pairs, err := buildUpdateNodebInfoPairs(nodebInfo)
440 err = w.sdl.SetAndPublish([]string{w.rnibWriterConfig.StateChangeMessageChannel, event}, pairs)
443 return common.NewInternalError(err)
449 func (w *rNibWriterInstance) RemoveEnb(nodebInfo *entities.NodebInfo) error {
450 keysToRemove, err := w.buildRemoveEnbKeys(nodebInfo)
455 err = w.sdl.RemoveAndPublish([]string{w.rnibWriterConfig.RanManipulationMessageChannel, fmt.Sprintf("%s_%s", nodebInfo.RanName, RanDeletedEvent)}, keysToRemove)
458 return common.NewInternalError(err)
464 func (w *rNibWriterInstance) UpdateEnb(nodebInfo *entities.NodebInfo, servedCells []*entities.ServedCellInfo) error {
466 pairs, err := buildUpdateNodebInfoPairs(nodebInfo)
472 pairs, err = appendEnbCells(nodebInfo.RanName, servedCells, pairs)
478 err = w.sdl.SetAndPublish([]string{w.rnibWriterConfig.RanManipulationMessageChannel, fmt.Sprintf("%s_%s", nodebInfo.RanName, RanUpdatedEvent)}, pairs)
481 return common.NewInternalError(err)
494 func appendEnbCells(inventoryName string, cells []*entities.ServedCellInfo, pairs []interface{}) ([]interface{}, error) {
495 for _, cell := range cells {
496 cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}}
497 cellData, err := proto.Marshal(&cellEntity)
499 return pairs, common.NewInternalError(err)
501 key, rNibErr := common.ValidateAndBuildCellIdKey(cell.GetCellId())
503 return pairs, rNibErr
505 pairs = append(pairs, key, cellData)
506 key, rNibErr = common.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetPci())
508 return pairs, rNibErr
510 pairs = append(pairs, key, cellData)
515 func appendGnbCells(inventoryName string, cells []*entities.ServedNRCell, pairs []interface{}) ([]interface{}, error) {
516 for _, cell := range cells {
517 cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: cell}}
518 cellData, err := proto.Marshal(&cellEntity)
520 return pairs, common.NewInternalError(err)
522 key, rNibErr := common.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
524 return pairs, rNibErr
526 pairs = append(pairs, key, cellData)
527 key, rNibErr = common.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetServedNrCellInformation().GetNrPci())
529 return pairs, rNibErr
531 pairs = append(pairs, key, cellData)