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.
22 rnibcommon "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
23 rnibentities "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
24 "github.com/golang/protobuf/proto"
27 type rNibWriterInstance struct {
28 sdl rnibcommon.ISdlInstance
32 RNibWriter interface allows saving data to the redis BD
34 type RNibWriter interface {
35 SaveNodeb(nbIdentity *rnibentities.NbIdentity, nb *rnibentities.NodebInfo) error
39 GetRNibWriter returns reference to RNibWriter
42 func GetRNibWriter(sdl rnibcommon.ISdlInstance) RNibWriter {
43 return &rNibWriterInstance{sdl: sdl}
47 SaveNodeb saves nodeB entity data in the redis DB according to the specified data model
49 func (w *rNibWriterInstance) SaveNodeb(nbIdentity *rnibentities.NbIdentity, entity *rnibentities.NodebInfo) error {
50 isNotEmptyIdentity := isNotEmpty(nbIdentity)
52 if isNotEmptyIdentity && entity.GetNodeType() == rnibentities.Node_UNKNOWN {
53 return rnibcommon.NewValidationError(fmt.Sprintf("#rNibWriter.saveNodeB - Unknown responding node type, entity: %v", entity))
55 data, err := proto.Marshal(entity)
57 return rnibcommon.NewInternalError(err)
59 var pairs []interface{}
60 key, rNibErr := rnibcommon.ValidateAndBuildNodeBNameKey(nbIdentity.InventoryName)
64 pairs = append(pairs, key, data)
66 if isNotEmptyIdentity {
67 key, rNibErr = rnibcommon.ValidateAndBuildNodeBIdKey(entity.GetNodeType().String(), nbIdentity.GlobalNbId.GetPlmnId(), nbIdentity.GlobalNbId.GetNbId())
71 pairs = append(pairs, key, data)
74 if entity.GetEnb() != nil {
75 pairs, rNibErr = appendEnbCells(nbIdentity.InventoryName, entity.GetEnb().GetServedCells(), pairs)
80 if entity.GetGnb() != nil {
81 pairs, rNibErr = appendGnbCells(nbIdentity.InventoryName, entity.GetGnb().GetServedNrCells(), pairs)
86 err = w.sdl.Set(pairs)
88 return rnibcommon.NewInternalError(err)
91 ranNameIdentity := &rnibentities.NbIdentity{InventoryName: nbIdentity.InventoryName}
93 if isNotEmptyIdentity {
94 nbIdData, err := proto.Marshal(ranNameIdentity)
96 return rnibcommon.NewInternalError(err)
98 err = w.sdl.RemoveMember(rnibentities.Node_UNKNOWN.String(), nbIdData)
100 return rnibcommon.NewInternalError(err)
103 nbIdentity = ranNameIdentity
106 nbIdData, err := proto.Marshal(nbIdentity)
108 return rnibcommon.NewInternalError(err)
110 err = w.sdl.AddMember(entity.GetNodeType().String(), nbIdData)
112 return rnibcommon.NewInternalError(err)
118 Close closes writer's pool
123 func appendEnbCells(inventoryName string, cells []*rnibentities.ServedCellInfo, pairs []interface{}) ([]interface{}, error) {
124 for _, cell := range cells {
125 cellEntity := rnibentities.Cell{Type: rnibentities.Cell_LTE_CELL, Cell: &rnibentities.Cell_ServedCellInfo{ServedCellInfo: cell}}
126 cellData, err := proto.Marshal(&cellEntity)
128 return pairs, rnibcommon.NewInternalError(err)
130 key, rNibErr := rnibcommon.ValidateAndBuildCellIdKey(cell.GetCellId())
132 return pairs, rNibErr
134 pairs = append(pairs, key, cellData)
135 key, rNibErr = rnibcommon.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetPci())
137 return pairs, rNibErr
139 pairs = append(pairs, key, cellData)
144 func appendGnbCells(inventoryName string, cells []*rnibentities.ServedNRCell, pairs []interface{}) ([]interface{}, error) {
145 for _, cell := range cells {
146 cellEntity := rnibentities.Cell{Type: rnibentities.Cell_NR_CELL, Cell: &rnibentities.Cell_ServedNrCell{ServedNrCell: cell}}
147 cellData, err := proto.Marshal(&cellEntity)
149 return pairs, rnibcommon.NewInternalError(err)
151 key, rNibErr := rnibcommon.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
153 return pairs, rNibErr
155 pairs = append(pairs, key, cellData)
156 key, rNibErr = rnibcommon.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetServedNrCellInformation().GetNrPci())
158 return pairs, rNibErr
160 pairs = append(pairs, key, cellData)
165 func isNotEmpty(nbIdentity *rnibentities.NbIdentity) bool {
166 return nbIdentity.GlobalNbId != nil && nbIdentity.GlobalNbId.PlmnId != "" && nbIdentity.GlobalNbId.NbId != ""