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).
25 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
26 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
27 "github.com/golang/protobuf/proto"
30 const E2TAddressesKey = "E2TAddresses"
32 type rNibWriterInstance struct {
33 sdl common.ISdlInstance
37 RNibWriter interface allows saving data to the redis DB
39 type RNibWriter interface {
40 SaveNodeb(nbIdentity *entities.NbIdentity, nb *entities.NodebInfo) error
41 UpdateNodebInfo(nodebInfo *entities.NodebInfo) error
42 SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) error
43 SaveE2TInstance(e2tInstance *entities.E2TInstance) error
44 SaveE2TAddresses(addresses []string) error
45 RemoveE2TInstance(e2tAddress string) error
46 UpdateGnbCells(nodebInfo *entities.NodebInfo, servedNrCells []*entities.ServedNRCell) error
47 RemoveServedNrCells(inventoryName string, servedNrCells []*entities.ServedNRCell) error
48 UpdateNodebInfoOnConnectionStatusInversion(nodebInfo *entities.NodebInfo, stateChangeMessageChannel string, event string) error
49 SaveGeneralConfiguration(config *entities.GeneralConfiguration) error
53 GetRNibWriter returns reference to RNibWriter
56 func GetRNibWriter(sdl common.ISdlInstance) RNibWriter {
57 return &rNibWriterInstance{sdl: sdl}
61 func (w *rNibWriterInstance) RemoveServedNrCells(inventoryName string, servedNrCells []*entities.ServedNRCell) error {
62 cellKeysToRemove := buildCellKeysToRemove(inventoryName, servedNrCells)
63 err := w.sdl.Remove(cellKeysToRemove)
66 return common.NewInternalError(err)
72 func (w *rNibWriterInstance) SaveGeneralConfiguration(config *entities.GeneralConfiguration) error {
74 err := w.SaveWithKeyAndMarshal(common.BuildGeneralConfigurationKey(), config)
77 return common.NewInternalError(err)
84 SaveNodeb saves nodeB entity data in the redis DB according to the specified data model
86 func (w *rNibWriterInstance) SaveNodeb(nbIdentity *entities.NbIdentity, entity *entities.NodebInfo) error {
87 isNotEmptyIdentity := isNotEmpty(nbIdentity)
89 if isNotEmptyIdentity && entity.GetNodeType() == entities.Node_UNKNOWN {
90 return common.NewValidationError(fmt.Sprintf("#rNibWriter.saveNodeB - Unknown responding node type, entity: %v", entity))
92 data, err := proto.Marshal(entity)
94 return common.NewInternalError(err)
96 var pairs []interface{}
97 key, rNibErr := common.ValidateAndBuildNodeBNameKey(nbIdentity.InventoryName)
101 pairs = append(pairs, key, data)
103 if isNotEmptyIdentity {
104 key, rNibErr = common.ValidateAndBuildNodeBIdKey(entity.GetNodeType().String(), nbIdentity.GlobalNbId.GetPlmnId(), nbIdentity.GlobalNbId.GetNbId())
108 pairs = append(pairs, key, data)
111 if entity.GetEnb() != nil {
112 pairs, rNibErr = appendEnbCells(nbIdentity.InventoryName, entity.GetEnb().GetServedCells(), pairs)
117 if entity.GetGnb() != nil {
118 pairs, rNibErr = appendGnbCells(nbIdentity.InventoryName, entity.GetGnb().GetServedNrCells(), pairs)
123 err = w.sdl.Set(pairs)
125 return common.NewInternalError(err)
128 ranNameIdentity := &entities.NbIdentity{InventoryName: nbIdentity.InventoryName}
130 if isNotEmptyIdentity {
131 nbIdData, err := proto.Marshal(ranNameIdentity)
133 return common.NewInternalError(err)
135 err = w.sdl.RemoveMember(entities.Node_UNKNOWN.String(), nbIdData)
137 return common.NewInternalError(err)
140 nbIdentity = ranNameIdentity
143 nbIdData, err := proto.Marshal(nbIdentity)
145 return common.NewInternalError(err)
147 err = w.sdl.AddMember(entity.GetNodeType().String(), nbIdData)
149 return common.NewInternalError(err)
154 func (w *rNibWriterInstance) UpdateGnbCells(nodebInfo *entities.NodebInfo, servedNrCells []*entities.ServedNRCell) error {
156 pairs, err := buildUpdateNodebInfoPairs(nodebInfo)
162 pairs, err = appendGnbCells(nodebInfo.RanName, servedNrCells, pairs)
168 err = w.sdl.Set(pairs)
171 return common.NewInternalError(err)
177 func buildCellKeysToRemove(inventoryName string, servedNrCellsToRemove []*entities.ServedNRCell) []string {
179 cellKeysToRemove := []string{}
181 for _, cell := range servedNrCellsToRemove {
183 key, _ := common.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
186 cellKeysToRemove = append(cellKeysToRemove, key)
189 key, _ = common.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetServedNrCellInformation().GetNrPci())
192 cellKeysToRemove = append(cellKeysToRemove, key)
196 return cellKeysToRemove
199 func buildUpdateNodebInfoPairs(nodebInfo *entities.NodebInfo) ([]interface{}, error) {
200 nodebNameKey, rNibErr := common.ValidateAndBuildNodeBNameKey(nodebInfo.GetRanName())
203 return []interface{}{}, rNibErr
206 nodebIdKey, buildNodebIdKeyError := common.ValidateAndBuildNodeBIdKey(nodebInfo.GetNodeType().String(), nodebInfo.GlobalNbId.GetPlmnId(), nodebInfo.GlobalNbId.GetNbId())
208 data, err := proto.Marshal(nodebInfo)
211 return []interface{}{}, common.NewInternalError(err)
214 pairs := []interface{}{nodebNameKey, data}
216 if buildNodebIdKeyError == nil {
217 pairs = append(pairs, nodebIdKey, data)
226 func (w *rNibWriterInstance) UpdateNodebInfo(nodebInfo *entities.NodebInfo) error {
228 pairs, err := buildUpdateNodebInfoPairs(nodebInfo)
234 err = w.sdl.Set(pairs)
237 return common.NewInternalError(err)
244 SaveRanLoadInformation stores ran load information for the provided ran
246 func (w *rNibWriterInstance) SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) error {
248 key, rnibErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
254 data, err := proto.Marshal(ranLoadInformation)
257 return common.NewInternalError(err)
260 var pairs []interface{}
261 pairs = append(pairs, key, data)
263 err = w.sdl.Set(pairs)
266 return common.NewInternalError(err)
272 func (w *rNibWriterInstance) SaveE2TInstance(e2tInstance *entities.E2TInstance) error {
274 key, rnibErr := common.ValidateAndBuildE2TInstanceKey(e2tInstance.Address)
280 data, err := json.Marshal(e2tInstance)
283 return common.NewInternalError(err)
286 var pairs []interface{}
287 pairs = append(pairs, key, data)
289 err = w.sdl.Set(pairs)
292 return common.NewInternalError(err)
298 func (w *rNibWriterInstance) SaveE2TAddresses(addresses []string) error {
300 data, err := json.Marshal(addresses)
303 return common.NewInternalError(err)
306 var pairs []interface{}
307 pairs = append(pairs, E2TAddressesKey, data)
309 err = w.sdl.Set(pairs)
312 return common.NewInternalError(err)
318 func (w *rNibWriterInstance) RemoveE2TInstance(address string) error {
319 key, rNibErr := common.ValidateAndBuildE2TInstanceKey(address)
323 err := w.sdl.Remove([]string{key})
326 return common.NewInternalError(err)
331 func (w *rNibWriterInstance) SaveWithKeyAndMarshal(key string, entity interface{}) error {
333 data, err := json.Marshal(entity)
336 return common.NewInternalError(err)
339 var pairs []interface{}
340 pairs = append(pairs, key, data)
342 err = w.sdl.Set(pairs)
345 return common.NewInternalError(err)
352 UpdateNodebInfoOnConnectionStatusInversion...
354 func (w *rNibWriterInstance) UpdateNodebInfoOnConnectionStatusInversion(nodebInfo *entities.NodebInfo, stateChangeMessageChannel string, event string) error {
356 pairs, err := buildUpdateNodebInfoPairs(nodebInfo)
362 err = w.sdl.SetAndPublish([]string{stateChangeMessageChannel, event}, pairs)
365 return common.NewInternalError(err)
378 func appendEnbCells(inventoryName string, cells []*entities.ServedCellInfo, pairs []interface{}) ([]interface{}, error) {
379 for _, cell := range cells {
380 cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}}
381 cellData, err := proto.Marshal(&cellEntity)
383 return pairs, common.NewInternalError(err)
385 key, rNibErr := common.ValidateAndBuildCellIdKey(cell.GetCellId())
387 return pairs, rNibErr
389 pairs = append(pairs, key, cellData)
390 key, rNibErr = common.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetPci())
392 return pairs, rNibErr
394 pairs = append(pairs, key, cellData)
399 func appendGnbCells(inventoryName string, cells []*entities.ServedNRCell, pairs []interface{}) ([]interface{}, error) {
400 for _, cell := range cells {
401 cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: cell}}
402 cellData, err := proto.Marshal(&cellEntity)
404 return pairs, common.NewInternalError(err)
406 key, rNibErr := common.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
408 return pairs, rNibErr
410 pairs = append(pairs, key, cellData)
411 key, rNibErr = common.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetServedNrCellInformation().GetNrPci())
413 return pairs, rNibErr
415 pairs = append(pairs, key, cellData)
420 func isNotEmpty(nbIdentity *entities.NbIdentity) bool {
421 return nbIdentity.GlobalNbId != nil && nbIdentity.GlobalNbId.PlmnId != "" && nbIdentity.GlobalNbId.NbId != ""