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(nbIdentity *entities.NbIdentity, nb *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, stateChangeMessageChannel string, event string) error
56 SaveGeneralConfiguration(config *entities.GeneralConfiguration) error
60 GetRNibWriter returns reference to RNibWriter
63 func GetRNibWriter(sdl common.ISdlInstance, rnibWriterConfig configuration.RnibWriterConfig) RNibWriter {
64 return &rNibWriterInstance{sdl: sdl, rnibWriterConfig: rnibWriterConfig}
67 func (w *rNibWriterInstance) RemoveServedNrCells(inventoryName string, servedNrCells []*entities.ServedNRCell) error {
68 cellKeysToRemove := buildCellKeysToRemove(inventoryName, servedNrCells)
69 err := w.sdl.Remove(cellKeysToRemove)
72 return common.NewInternalError(err)
78 func (w *rNibWriterInstance) SaveGeneralConfiguration(config *entities.GeneralConfiguration) error {
80 err := w.SaveWithKeyAndMarshal(common.BuildGeneralConfigurationKey(), config)
83 return common.NewInternalError(err)
90 SaveNodeb saves nodeB entity data in the redis DB according to the specified data model
92 func (w *rNibWriterInstance) SaveNodeb(nbIdentity *entities.NbIdentity, entity *entities.NodebInfo) error {
93 isNotEmptyIdentity := isNotEmpty(nbIdentity)
95 nodeType := entity.GetNodeType()
97 if isNotEmptyIdentity && nodeType == entities.Node_UNKNOWN {
98 return common.NewValidationError(fmt.Sprintf("#rNibWriter.saveNodeB - Unknown responding node type, entity: %v", entity))
101 data, err := proto.Marshal(entity)
104 return common.NewInternalError(err)
107 var pairs []interface{}
108 key, rNibErr := common.ValidateAndBuildNodeBNameKey(nbIdentity.InventoryName)
114 pairs = append(pairs, key, data)
116 if isNotEmptyIdentity {
118 key, rNibErr = common.ValidateAndBuildNodeBIdKey(nodeType.String(), nbIdentity.GlobalNbId.GetPlmnId(), nbIdentity.GlobalNbId.GetNbId())
122 pairs = append(pairs, key, data)
125 if entity.GetEnb() != nil {
126 pairs, rNibErr = appendEnbCells(nbIdentity.InventoryName, entity.GetEnb().GetServedCells(), pairs)
132 if entity.GetGnb() != nil {
133 pairs, rNibErr = appendGnbCells(nbIdentity.InventoryName, entity.GetGnb().GetServedNrCells(), pairs)
139 if nodeType == entities.Node_ENB {
140 err = w.sdl.SetAndPublish([]string{w.rnibWriterConfig.RanManipulationMessageChannel, fmt.Sprintf("%s_%s", entity.RanName, RanAddedEvent)}, pairs)
142 err = w.sdl.Set(pairs)
146 return common.NewInternalError(err)
149 ranNameIdentity := &entities.NbIdentity{InventoryName: nbIdentity.InventoryName}
151 if isNotEmptyIdentity {
152 nbIdData, err := proto.Marshal(ranNameIdentity)
154 return common.NewInternalError(err)
156 err = w.sdl.RemoveMember(entities.Node_UNKNOWN.String(), nbIdData)
158 return common.NewInternalError(err)
161 nbIdentity = ranNameIdentity
164 nbIdData, err := proto.Marshal(nbIdentity)
167 return common.NewInternalError(err)
170 err = w.sdl.AddMember(nodeType.String(), nbIdData)
173 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 buildCellKeysToRemove(inventoryName string, servedNrCellsToRemove []*entities.ServedNRCell) []string {
203 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 buildUpdateNodebInfoPairs(nodebInfo *entities.NodebInfo) ([]interface{}, error) {
224 nodebNameKey, rNibErr := common.ValidateAndBuildNodeBNameKey(nodebInfo.GetRanName())
227 return []interface{}{}, rNibErr
230 nodebIdKey, buildNodebIdKeyError := common.ValidateAndBuildNodeBIdKey(nodebInfo.GetNodeType().String(), nodebInfo.GlobalNbId.GetPlmnId(), nodebInfo.GlobalNbId.GetNbId())
232 data, err := proto.Marshal(nodebInfo)
235 return []interface{}{}, common.NewInternalError(err)
238 pairs := []interface{}{nodebNameKey, data}
240 if buildNodebIdKeyError == nil {
241 pairs = append(pairs, nodebIdKey, data)
250 func (w *rNibWriterInstance) UpdateNodebInfo(nodebInfo *entities.NodebInfo) error {
252 pairs, err := buildUpdateNodebInfoPairs(nodebInfo)
258 err = w.sdl.Set(pairs)
261 return common.NewInternalError(err)
268 SaveRanLoadInformation stores ran load information for the provided ran
270 func (w *rNibWriterInstance) SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) error {
272 key, rnibErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
278 data, err := proto.Marshal(ranLoadInformation)
281 return common.NewInternalError(err)
284 var pairs []interface{}
285 pairs = append(pairs, key, data)
287 err = w.sdl.Set(pairs)
290 return common.NewInternalError(err)
296 func (w *rNibWriterInstance) SaveE2TInstance(e2tInstance *entities.E2TInstance) error {
298 key, rnibErr := common.ValidateAndBuildE2TInstanceKey(e2tInstance.Address)
304 data, err := json.Marshal(e2tInstance)
307 return common.NewInternalError(err)
310 var pairs []interface{}
311 pairs = append(pairs, key, data)
313 err = w.sdl.Set(pairs)
316 return common.NewInternalError(err)
322 func (w *rNibWriterInstance) SaveE2TAddresses(addresses []string) error {
324 data, err := json.Marshal(addresses)
327 return common.NewInternalError(err)
330 var pairs []interface{}
331 pairs = append(pairs, E2TAddressesKey, data)
333 err = w.sdl.Set(pairs)
336 return common.NewInternalError(err)
342 func (w *rNibWriterInstance) RemoveE2TInstance(address string) error {
343 key, rNibErr := common.ValidateAndBuildE2TInstanceKey(address)
347 err := w.sdl.Remove([]string{key})
350 return common.NewInternalError(err)
355 func (w *rNibWriterInstance) SaveWithKeyAndMarshal(key string, entity interface{}) error {
357 data, err := json.Marshal(entity)
360 return common.NewInternalError(err)
363 var pairs []interface{}
364 pairs = append(pairs, key, data)
366 err = w.sdl.Set(pairs)
369 return common.NewInternalError(err)
376 UpdateNodebInfoOnConnectionStatusInversion...
378 func (w *rNibWriterInstance) UpdateNodebInfoOnConnectionStatusInversion(nodebInfo *entities.NodebInfo, stateChangeMessageChannel string, event string) error {
380 pairs, err := buildUpdateNodebInfoPairs(nodebInfo)
386 err = w.sdl.SetAndPublish([]string{stateChangeMessageChannel, event}, pairs)
389 return common.NewInternalError(err)
402 func appendEnbCells(inventoryName string, cells []*entities.ServedCellInfo, pairs []interface{}) ([]interface{}, error) {
403 for _, cell := range cells {
404 cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}}
405 cellData, err := proto.Marshal(&cellEntity)
407 return pairs, common.NewInternalError(err)
409 key, rNibErr := common.ValidateAndBuildCellIdKey(cell.GetCellId())
411 return pairs, rNibErr
413 pairs = append(pairs, key, cellData)
414 key, rNibErr = common.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetPci())
416 return pairs, rNibErr
418 pairs = append(pairs, key, cellData)
423 func appendGnbCells(inventoryName string, cells []*entities.ServedNRCell, pairs []interface{}) ([]interface{}, error) {
424 for _, cell := range cells {
425 cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: cell}}
426 cellData, err := proto.Marshal(&cellEntity)
428 return pairs, common.NewInternalError(err)
430 key, rNibErr := common.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
432 return pairs, rNibErr
434 pairs = append(pairs, key, cellData)
435 key, rNibErr = common.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetServedNrCellInformation().GetNrPci())
437 return pairs, rNibErr
439 pairs = append(pairs, key, cellData)
444 func isNotEmpty(nbIdentity *entities.NbIdentity) bool {
445 return nbIdentity.GlobalNbId != nil && nbIdentity.GlobalNbId.PlmnId != "" && nbIdentity.GlobalNbId.NbId != ""