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"
31 const E2TAddressesKey = "E2TAddresses"
33 type rNibWriterInstance struct {
34 sdl common.ISdlInstance
38 RNibWriter interface allows saving data to the redis DB
40 type RNibWriter interface {
41 SaveNodeb(nbIdentity *entities.NbIdentity, nb *entities.NodebInfo) error
42 UpdateNodebInfo(nodebInfo *entities.NodebInfo) error
43 SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) error
44 SaveE2TInstance(e2tInstance *entities.E2TInstance) error
45 SaveE2TAddresses(addresses []string) error
46 RemoveE2TInstance(e2tAddress string) error
50 GetRNibWriter returns reference to RNibWriter
53 func GetRNibWriter(sdl common.ISdlInstance) RNibWriter {
54 return &rNibWriterInstance{sdl: sdl}
58 SaveNodeb saves nodeB entity data in the redis DB according to the specified data model
60 func (w *rNibWriterInstance) SaveNodeb(nbIdentity *entities.NbIdentity, entity *entities.NodebInfo) error {
61 isNotEmptyIdentity := isNotEmpty(nbIdentity)
63 if isNotEmptyIdentity && entity.GetNodeType() == entities.Node_UNKNOWN {
64 return common.NewValidationError(fmt.Sprintf("#rNibWriter.saveNodeB - Unknown responding node type, entity: %v", entity))
66 data, err := proto.Marshal(entity)
68 return common.NewInternalError(err)
70 var pairs []interface{}
71 key, rNibErr := common.ValidateAndBuildNodeBNameKey(nbIdentity.InventoryName)
75 pairs = append(pairs, key, data)
77 if isNotEmptyIdentity {
78 key, rNibErr = common.ValidateAndBuildNodeBIdKey(entity.GetNodeType().String(), nbIdentity.GlobalNbId.GetPlmnId(), nbIdentity.GlobalNbId.GetNbId())
82 pairs = append(pairs, key, data)
85 if entity.GetEnb() != nil {
86 pairs, rNibErr = appendEnbCells(nbIdentity, entity.GetEnb().GetServedCells(), pairs)
91 if entity.GetGnb() != nil {
92 pairs, rNibErr = appendGnbCells(nbIdentity, entity.GetGnb().GetServedNrCells(), pairs)
97 err = w.sdl.Set(pairs)
99 return common.NewInternalError(err)
102 ranNameIdentity := &entities.NbIdentity{InventoryName: nbIdentity.InventoryName}
104 if isNotEmptyIdentity {
105 nbIdData, err := proto.Marshal(ranNameIdentity)
107 return common.NewInternalError(err)
109 err = w.sdl.RemoveMember(entities.Node_UNKNOWN.String(), nbIdData)
111 return common.NewInternalError(err)
114 nbIdentity = ranNameIdentity
117 nbIdData, err := proto.Marshal(nbIdentity)
119 return common.NewInternalError(err)
121 err = w.sdl.AddMember(entity.GetNodeType().String(), nbIdData)
123 return common.NewInternalError(err)
131 func (w *rNibWriterInstance) UpdateNodebInfo(nodebInfo *entities.NodebInfo) error {
133 nodebNameKey, rNibErr := common.ValidateAndBuildNodeBNameKey(nodebInfo.GetRanName())
139 nodebIdKey, buildNodebIdKeyError := common.ValidateAndBuildNodeBIdKey(nodebInfo.GetNodeType().String(), nodebInfo.GlobalNbId.GetPlmnId(), nodebInfo.GlobalNbId.GetNbId())
141 data, err := proto.Marshal(nodebInfo)
144 return common.NewInternalError(err)
147 var pairs []interface{}
148 pairs = append(pairs, nodebNameKey, data)
150 if buildNodebIdKeyError == nil {
151 pairs = append(pairs, nodebIdKey, data)
154 err = w.sdl.Set(pairs)
157 return common.NewInternalError(err)
164 SaveRanLoadInformation stores ran load information for the provided ran
166 func (w *rNibWriterInstance) SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) error {
168 key, rnibErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
174 data, err := proto.Marshal(ranLoadInformation)
177 return common.NewInternalError(err)
180 var pairs []interface{}
181 pairs = append(pairs, key, data)
183 err = w.sdl.Set(pairs)
186 return common.NewInternalError(err)
192 func (w *rNibWriterInstance) SaveE2TInstance(e2tInstance *entities.E2TInstance) error {
194 key, rnibErr := common.ValidateAndBuildE2TInstanceKey(e2tInstance.Address)
200 data, err := json.Marshal(e2tInstance)
203 return common.NewInternalError(err)
206 var pairs []interface{}
207 pairs = append(pairs, key, data)
209 err = w.sdl.Set(pairs)
212 return common.NewInternalError(err)
218 func (w *rNibWriterInstance) SaveE2TAddresses(addresses []string) error {
220 data, err := json.Marshal(addresses)
223 return common.NewInternalError(err)
226 var pairs []interface{}
227 pairs = append(pairs, E2TAddressesKey, data)
229 err = w.sdl.Set(pairs)
232 return common.NewInternalError(err)
239 func (w *rNibWriterInstance) RemoveE2TInstance(address string) error {
240 key, rNibErr := common.ValidateAndBuildE2TInstanceKey(address)
244 err := w.sdl.Remove([]string{key})
247 return common.NewInternalError(err)
259 func appendEnbCells(nbIdentity *entities.NbIdentity, cells []*entities.ServedCellInfo, pairs []interface{}) ([]interface{}, error) {
260 for _, cell := range cells {
261 cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}}
262 cellData, err := proto.Marshal(&cellEntity)
264 return pairs, common.NewInternalError(err)
266 key, rNibErr := common.ValidateAndBuildCellIdKey(cell.GetCellId())
268 return pairs, rNibErr
270 pairs = append(pairs, key, cellData)
271 key, rNibErr = common.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetPci())
273 return pairs, rNibErr
275 pairs = append(pairs, key, cellData)
280 func appendGnbCells(nbIdentity *entities.NbIdentity, cells []*entities.ServedNRCell, pairs []interface{}) ([]interface{}, error) {
281 for _, cell := range cells {
282 cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: cell}}
283 cellData, err := proto.Marshal(&cellEntity)
285 return pairs, common.NewInternalError(err)
287 key, rNibErr := common.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
289 return pairs, rNibErr
291 pairs = append(pairs, key, cellData)
292 key, rNibErr = common.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetServedNrCellInformation().GetNrPci())
294 return pairs, rNibErr
296 pairs = append(pairs, key, cellData)
301 func isNotEmpty(nbIdentity *entities.NbIdentity) bool {
302 return nbIdentity.GlobalNbId != nil && nbIdentity.GlobalNbId.PlmnId != "" && nbIdentity.GlobalNbId.NbId != ""