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).
28 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
29 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
30 "github.com/golang/protobuf/proto"
31 "github.com/stretchr/testify/assert"
36 var namespace = "namespace"
42 func initSdlInstanceMock(namespace string) (w RNibWriter, sdlInstanceMock *mocks.MockSdlInstance) {
43 sdlInstanceMock = new(mocks.MockSdlInstance)
44 w = GetRNibWriter(sdlInstanceMock, configuration.RnibWriterConfig{StateChangeMessageChannel: "RAN_CONNECTION_STATUS_CHANGE", RanManipulationMessageChannel: "RAN_MANIPULATION"})
48 func generateNodebInfo(inventoryName string, nodeType entities.Node_Type, plmnId string, nbId string) *entities.NodebInfo {
49 nodebInfo := &entities.NodebInfo{
50 RanName: inventoryName,
51 GlobalNbId: &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId},
53 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
56 if nodeType == entities.Node_ENB {
57 nodebInfo.Configuration = &entities.NodebInfo_Enb{
60 } else if nodeType == entities.Node_GNB {
61 nodebInfo.Configuration = &entities.NodebInfo_Gnb{
69 func generateServedNrCells(cellIds ...string) []*entities.ServedNRCell {
71 servedNrCells := []*entities.ServedNRCell{}
73 for i, v := range cellIds {
74 servedNrCells = append(servedNrCells, &entities.ServedNRCell{ServedNrCellInformation: &entities.ServedNRCellInformation{
76 ChoiceNrMode: &entities.ServedNRCellInformation_ChoiceNRMode{
77 Fdd: &entities.ServedNRCellInformation_ChoiceNRMode_FddInfo{
81 NrMode: entities.Nr_FDD,
83 ServedPlmns: []string{"whatever"},
90 func TestRemoveServedNrCellsSuccess(t *testing.T) {
91 w, sdlInstanceMock := initSdlInstanceMock(namespace)
92 servedNrCellsToRemove := generateServedNrCells("whatever1", "whatever2")
93 sdlInstanceMock.On("Remove", buildCellKeysToRemove(RanName, servedNrCellsToRemove)).Return(nil)
94 err := w.RemoveServedNrCells(RanName, servedNrCellsToRemove)
98 func TestRemoveServedNrCellsFailure(t *testing.T) {
99 w, sdlInstanceMock := initSdlInstanceMock(namespace)
100 servedNrCellsToRemove := generateServedNrCells("whatever1", "whatever2")
101 sdlInstanceMock.On("Remove", buildCellKeysToRemove(RanName, servedNrCellsToRemove)).Return(errors.New("expected error"))
102 err := w.RemoveServedNrCells(RanName, servedNrCellsToRemove)
103 assert.IsType(t, &common.InternalError{}, err)
106 func TestUpdateGnbCellsInvalidNodebInfoFailure(t *testing.T) {
107 w, sdlInstanceMock := initSdlInstanceMock(namespace)
108 servedNrCells := generateServedNrCells("test1", "test2")
109 nodebInfo := &entities.NodebInfo{}
110 sdlInstanceMock.AssertNotCalled(t, "Set")
111 rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
112 assert.IsType(t, &common.ValidationError{}, rNibErr)
115 func TestUpdateGnbCellsInvalidCellFailure(t *testing.T) {
116 inventoryName := "name"
119 w, sdlInstanceMock := initSdlInstanceMock(namespace)
120 servedNrCells := []*entities.ServedNRCell{{ServedNrCellInformation: &entities.ServedNRCellInformation{}}}
121 nodebInfo := generateNodebInfo(inventoryName, entities.Node_GNB, plmnId, nbId)
122 nodebInfo.GetGnb().ServedNrCells = servedNrCells
123 sdlInstanceMock.AssertNotCalled(t, "Set")
124 rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
125 assert.IsType(t, &common.ValidationError{}, rNibErr)
128 func getUpdateGnbCellsSetExpected(t *testing.T, nodebInfo *entities.NodebInfo, servedNrCells []*entities.ServedNRCell) []interface{} {
130 nodebInfoData, err := proto.Marshal(nodebInfo)
132 t.Fatalf("#rNibWriter_test.getUpdateGnbCellsSetExpected - Failed to marshal NodeB entity. Error: %s", err)
135 nodebNameKey, _ := common.ValidateAndBuildNodeBNameKey(nodebInfo.RanName)
136 nodebIdKey, _ := common.ValidateAndBuildNodeBIdKey(nodebInfo.NodeType.String(), nodebInfo.GlobalNbId.PlmnId, nodebInfo.GlobalNbId.NbId)
137 setExpected := []interface{}{nodebNameKey, nodebInfoData, nodebIdKey, nodebInfoData}
139 for _, v := range servedNrCells {
141 cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: v}}
142 cellData, err := proto.Marshal(&cellEntity)
145 t.Fatalf("#rNibWriter_test.getUpdateGnbCellsSetExpected - Failed to marshal cell entity. Error: %s", err)
148 nrCellIdKey, _ := common.ValidateAndBuildNrCellIdKey(v.GetServedNrCellInformation().GetCellId())
149 cellNamePciKey, _ := common.ValidateAndBuildCellNamePciKey(nodebInfo.RanName, v.GetServedNrCellInformation().GetNrPci())
150 setExpected = append(setExpected, nrCellIdKey, cellData, cellNamePciKey, cellData)
156 func TestUpdateGnbCellsSdlFailure(t *testing.T) {
157 inventoryName := "name"
160 w, sdlInstanceMock := initSdlInstanceMock(namespace)
161 servedNrCells := generateServedNrCells("test1", "test2")
162 nodebInfo := generateNodebInfo(inventoryName, entities.Node_GNB, plmnId, nbId)
163 nodebInfo.GetGnb().ServedNrCells = servedNrCells
164 setExpected := getUpdateGnbCellsSetExpected(t, nodebInfo, servedNrCells)
165 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(errors.New("expected error"))
166 rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
167 assert.IsType(t, &common.InternalError{}, rNibErr)
170 func TestUpdateGnbCellsSuccess(t *testing.T) {
171 inventoryName := "name"
174 w, sdlInstanceMock := initSdlInstanceMock(namespace)
175 servedNrCells := generateServedNrCells("test1", "test2")
176 nodebInfo := generateNodebInfo(inventoryName, entities.Node_GNB, plmnId, nbId)
177 nodebInfo.GetGnb().ServedNrCells = servedNrCells
178 setExpected := getUpdateGnbCellsSetExpected(t, nodebInfo, servedNrCells)
180 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
181 rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
182 assert.Nil(t, rNibErr)
185 func TestUpdateNodebInfoSuccess(t *testing.T) {
186 inventoryName := "name"
189 w, sdlInstanceMock := initSdlInstanceMock(namespace)
190 nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
191 data, err := proto.Marshal(nodebInfo)
193 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
196 var setExpected []interface{}
198 nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
199 nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
200 setExpected = append(setExpected, nodebNameKey, data)
201 setExpected = append(setExpected, nodebIdKey, data)
203 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
205 rNibErr := w.UpdateNodebInfo(nodebInfo)
206 assert.Nil(t, rNibErr)
209 func TestUpdateNodebInfoMissingInventoryNameFailure(t *testing.T) {
210 inventoryName := "name"
213 w, sdlInstanceMock := initSdlInstanceMock(namespace)
214 nodebInfo := &entities.NodebInfo{}
215 data, err := proto.Marshal(nodebInfo)
217 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
220 var setExpected []interface{}
222 nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
223 nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
224 setExpected = append(setExpected, nodebNameKey, data)
225 setExpected = append(setExpected, nodebIdKey, data)
227 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
229 rNibErr := w.UpdateNodebInfo(nodebInfo)
231 assert.NotNil(t, rNibErr)
232 assert.IsType(t, &common.ValidationError{}, rNibErr)
235 func TestUpdateNodebInfoMissingGlobalNbId(t *testing.T) {
236 inventoryName := "name"
237 w, sdlInstanceMock := initSdlInstanceMock(namespace)
238 nodebInfo := &entities.NodebInfo{}
239 nodebInfo.RanName = inventoryName
240 data, err := proto.Marshal(nodebInfo)
242 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
245 var setExpected []interface{}
247 nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
248 setExpected = append(setExpected, nodebNameKey, data)
249 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
251 rNibErr := w.UpdateNodebInfo(nodebInfo)
253 assert.Nil(t, rNibErr)
256 func TestSaveEnb(t *testing.T) {
258 ranName := "RAN:" + name
259 w, sdlInstanceMock := initSdlInstanceMock(namespace)
260 nb := entities.NodebInfo{}
262 nb.NodeType = entities.Node_ENB
263 nb.ConnectionStatus = 1
266 enb := entities.Enb{}
267 cell := &entities.ServedCellInfo{CellId: "aaff", Pci: 3}
268 cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}}
269 enb.ServedCells = []*entities.ServedCellInfo{cell}
270 nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
271 data, err := proto.Marshal(&nb)
273 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
277 cellData, err := proto.Marshal(&cellEntity)
279 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal Cell entity. Error: %v", err)
281 var setExpected []interface{}
282 setExpected = append(setExpected, ranName, data)
283 setExpected = append(setExpected, "ENB:02f829:4a952a0a", data)
284 setExpected = append(setExpected, fmt.Sprintf("CELL:%s", cell.GetCellId()), cellData)
285 setExpected = append(setExpected, fmt.Sprintf("PCI:%s:%02x", name, cell.GetPci()), cellData)
287 sdlInstanceMock.On("SetAndPublish", []string{"RAN_MANIPULATION", name + "_" + RanAddedEvent}, []interface{}{setExpected}).Return(e)
289 nbIdData, err := proto.Marshal(&entities.NbIdentity{InventoryName: name})
291 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal nbIdentity entity. Error: %v", err)
293 sdlInstanceMock.On("RemoveMember", entities.Node_UNKNOWN.String(), []interface{}{nbIdData}).Return(e)
295 nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
296 nbIdData, err = proto.Marshal(nbIdentity)
298 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB Identity entity. Error: %v", err)
300 sdlInstanceMock.On("AddMember", "ENB", []interface{}{nbIdData}).Return(e)
302 rNibErr := w.SaveNodeb(nbIdentity, &nb)
303 assert.Nil(t, rNibErr)
306 func TestSaveEnbCellIdValidationFailure(t *testing.T) {
308 w, _ := initSdlInstanceMock(namespace)
309 nb := entities.NodebInfo{}
310 nb.NodeType = entities.Node_ENB
311 nb.ConnectionStatus = 1
314 enb := entities.Enb{}
315 cell := &entities.ServedCellInfo{Pci: 3}
316 enb.ServedCells = []*entities.ServedCellInfo{cell}
317 nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
319 nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
320 rNibErr := w.SaveNodeb(nbIdentity, &nb)
321 assert.NotNil(t, rNibErr)
322 assert.IsType(t, &common.ValidationError{}, rNibErr)
323 assert.Equal(t, "#utils.ValidateAndBuildCellIdKey - an empty cell id received", rNibErr.Error())
326 func TestSaveEnbInventoryNameValidationFailure(t *testing.T) {
327 w, _ := initSdlInstanceMock(namespace)
328 nb := entities.NodebInfo{}
329 nb.NodeType = entities.Node_ENB
330 nb.ConnectionStatus = 1
333 enb := entities.Enb{}
334 cell := &entities.ServedCellInfo{CellId: "aaa", Pci: 3}
335 enb.ServedCells = []*entities.ServedCellInfo{cell}
336 nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
338 nbIdentity := &entities.NbIdentity{InventoryName: "", GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
339 rNibErr := w.SaveNodeb(nbIdentity, &nb)
340 assert.NotNil(t, rNibErr)
341 assert.IsType(t, &common.ValidationError{}, rNibErr)
342 assert.Equal(t, "#utils.ValidateAndBuildNodeBNameKey - an empty inventory name received", rNibErr.Error())
345 func TestSaveGnbCellIdValidationFailure(t *testing.T) {
347 w, _ := initSdlInstanceMock(namespace)
348 nb := entities.NodebInfo{}
349 nb.NodeType = entities.Node_GNB
350 nb.ConnectionStatus = 1
353 gnb := entities.Gnb{}
354 cellInfo := &entities.ServedNRCellInformation{NrPci: 2}
355 cell := &entities.ServedNRCell{ServedNrCellInformation: cellInfo}
356 gnb.ServedNrCells = []*entities.ServedNRCell{cell}
357 nb.Configuration = &entities.NodebInfo_Gnb{Gnb: &gnb}
359 nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
360 rNibErr := w.SaveNodeb(nbIdentity, &nb)
361 assert.NotNil(t, rNibErr)
362 assert.IsType(t, &common.ValidationError{}, rNibErr)
363 assert.Equal(t, "#utils.ValidateAndBuildNrCellIdKey - an empty cell id received", rNibErr.Error())
366 func TestSaveGnb(t *testing.T) {
368 ranName := "RAN:" + name
369 w, sdlInstanceMock := initSdlInstanceMock(namespace)
370 nb := entities.NodebInfo{}
371 nb.NodeType = entities.Node_GNB
372 nb.ConnectionStatus = 1
375 gnb := entities.Gnb{}
376 cellInfo := &entities.ServedNRCellInformation{NrPci: 2, CellId: "ccdd"}
377 cell := &entities.ServedNRCell{ServedNrCellInformation: cellInfo}
378 cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: cell}}
379 gnb.ServedNrCells = []*entities.ServedNRCell{cell}
380 nb.Configuration = &entities.NodebInfo_Gnb{Gnb: &gnb}
381 data, err := proto.Marshal(&nb)
383 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal NodeB entity. Error: %v", err)
387 cellData, err := proto.Marshal(&cellEntity)
389 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal Cell entity. Error: %v", err)
391 var setExpected []interface{}
392 setExpected = append(setExpected, ranName, data)
393 setExpected = append(setExpected, "GNB:02f829:4a952a0a", data)
394 setExpected = append(setExpected, fmt.Sprintf("NRCELL:%s", cell.GetServedNrCellInformation().GetCellId()), cellData)
395 setExpected = append(setExpected, fmt.Sprintf("PCI:%s:%02x", name, cell.GetServedNrCellInformation().GetNrPci()), cellData)
397 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
398 nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
399 nbIdData, err := proto.Marshal(nbIdentity)
401 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal NodeB Identity entity. Error: %v", err)
403 sdlInstanceMock.On("AddMember", "GNB", []interface{}{nbIdData}).Return(e)
405 nbIdData, err = proto.Marshal(&entities.NbIdentity{InventoryName: name})
407 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal nbIdentity entity. Error: %v", err)
409 sdlInstanceMock.On("RemoveMember", entities.Node_UNKNOWN.String(), []interface{}{nbIdData}).Return(e)
411 rNibErr := w.SaveNodeb(nbIdentity, &nb)
412 assert.Nil(t, rNibErr)
415 func TestSaveRanLoadInformationSuccess(t *testing.T) {
416 inventoryName := "name"
417 loadKey, validationErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
419 if validationErr != nil {
420 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationSuccess - Failed to build ran load infromation key. Error: %v", validationErr)
423 w, sdlInstanceMock := initSdlInstanceMock(namespace)
425 ranLoadInformation := generateRanLoadInformation()
426 data, err := proto.Marshal(ranLoadInformation)
429 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformation - Failed to marshal RanLoadInformation entity. Error: %v", err)
433 var setExpected []interface{}
434 setExpected = append(setExpected, loadKey, data)
435 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
437 rNibErr := w.SaveRanLoadInformation(inventoryName, ranLoadInformation)
438 assert.Nil(t, rNibErr)
441 func TestSaveRanLoadInformationMarshalNilFailure(t *testing.T) {
442 inventoryName := "name2"
443 w, _ := initSdlInstanceMock(namespace)
445 expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
446 err := w.SaveRanLoadInformation(inventoryName, nil)
447 assert.Equal(t, expectedErr, err)
450 func TestSaveRanLoadInformationEmptyInventoryNameFailure(t *testing.T) {
452 w, _ := initSdlInstanceMock(namespace)
454 err := w.SaveRanLoadInformation(inventoryName, nil)
455 assert.NotNil(t, err)
456 assert.IsType(t, &common.ValidationError{}, err)
459 func TestSaveRanLoadInformationSdlFailure(t *testing.T) {
460 inventoryName := "name2"
462 loadKey, validationErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
464 if validationErr != nil {
465 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationSuccess - Failed to build ran load infromation key. Error: %v", validationErr)
468 w, sdlInstanceMock := initSdlInstanceMock(namespace)
470 ranLoadInformation := generateRanLoadInformation()
471 data, err := proto.Marshal(ranLoadInformation)
474 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformation - Failed to marshal RanLoadInformation entity. Error: %v", err)
477 expectedErr := errors.New("expected error")
478 var setExpected []interface{}
479 setExpected = append(setExpected, loadKey, data)
480 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
482 rNibErr := w.SaveRanLoadInformation(inventoryName, ranLoadInformation)
483 assert.NotNil(t, rNibErr)
484 assert.IsType(t, &common.InternalError{}, rNibErr)
487 func generateCellLoadInformation() *entities.CellLoadInformation {
488 cellLoadInformation := entities.CellLoadInformation{}
490 cellLoadInformation.CellId = "123"
492 ulInterferenceOverloadIndication := entities.UlInterferenceOverloadIndication_HIGH_INTERFERENCE
493 cellLoadInformation.UlInterferenceOverloadIndications = []entities.UlInterferenceOverloadIndication{ulInterferenceOverloadIndication}
495 ulHighInterferenceInformation := entities.UlHighInterferenceInformation{
497 UlHighInterferenceIndication: "xxx",
500 cellLoadInformation.UlHighInterferenceInfos = []*entities.UlHighInterferenceInformation{&ulHighInterferenceInformation}
502 cellLoadInformation.RelativeNarrowbandTxPower = &entities.RelativeNarrowbandTxPower{
504 RntpThreshold: entities.RntpThreshold_NEG_4,
505 NumberOfCellSpecificAntennaPorts: entities.NumberOfCellSpecificAntennaPorts_V1_ANT_PRT,
507 PdcchInterferenceImpact: 2,
508 EnhancedRntp: &entities.EnhancedRntp{
509 EnhancedRntpBitmap: "xxx",
510 RntpHighPowerThreshold: entities.RntpThreshold_NEG_2,
511 EnhancedRntpStartTime: &entities.StartTime{StartSfn: 500, StartSubframeNumber: 5},
515 cellLoadInformation.AbsInformation = &entities.AbsInformation{
516 Mode: entities.AbsInformationMode_ABS_INFO_FDD,
517 AbsPatternInfo: "xxx",
518 NumberOfCellSpecificAntennaPorts: entities.NumberOfCellSpecificAntennaPorts_V2_ANT_PRT,
519 MeasurementSubset: "xxx",
522 cellLoadInformation.InvokeIndication = entities.InvokeIndication_ABS_INFORMATION
524 cellLoadInformation.ExtendedUlInterferenceOverloadInfo = &entities.ExtendedUlInterferenceOverloadInfo{
525 AssociatedSubframes: "xxx",
526 ExtendedUlInterferenceOverloadIndications: cellLoadInformation.UlInterferenceOverloadIndications,
529 compInformationItem := &entities.CompInformationItem{
530 CompHypothesisSets: []*entities.CompHypothesisSet{{CellId: "789", CompHypothesis: "xxx"}},
534 cellLoadInformation.CompInformation = &entities.CompInformation{
535 CompInformationItems: []*entities.CompInformationItem{compInformationItem},
536 CompInformationStartTime: &entities.StartTime{StartSfn: 123, StartSubframeNumber: 456},
539 cellLoadInformation.DynamicDlTransmissionInformation = &entities.DynamicDlTransmissionInformation{
540 State: entities.NaicsState_NAICS_ACTIVE,
541 TransmissionModes: "xxx",
543 PAList: []entities.PA{entities.PA_DB_NEG_3},
546 return &cellLoadInformation
549 func generateRanLoadInformation() *entities.RanLoadInformation {
550 ranLoadInformation := entities.RanLoadInformation{}
552 ranLoadInformation.LoadTimestamp = uint64(time.Now().UnixNano())
554 cellLoadInformation := generateCellLoadInformation()
555 ranLoadInformation.CellLoadInfos = []*entities.CellLoadInformation{cellLoadInformation}
557 return &ranLoadInformation
560 func TestSaveNilEntityFailure(t *testing.T) {
561 w, _ := initSdlInstanceMock(namespace)
562 expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
563 nbIdentity := &entities.NbIdentity{}
564 actualErr := w.SaveNodeb(nbIdentity, nil)
565 assert.Equal(t, expectedErr, actualErr)
568 func TestSaveUnknownTypeEntityFailure(t *testing.T) {
569 w, _ := initSdlInstanceMock(namespace)
570 nbIdentity := &entities.NbIdentity{InventoryName: "name", GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
571 nb := &entities.NodebInfo{}
574 actualErr := w.SaveNodeb(nbIdentity, nb)
575 assert.IsType(t, &common.ValidationError{}, actualErr)
578 func TestSaveEntityFailure(t *testing.T) {
583 w, sdlInstanceMock := initSdlInstanceMock(namespace)
584 gnb := entities.NodebInfo{}
585 gnb.NodeType = entities.Node_GNB
586 data, err := proto.Marshal(&gnb)
588 t.Errorf("#rNibWriter_test.TestSaveEntityFailure - Failed to marshal NodeB entity. Error: %v", err)
590 nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId}}
591 setExpected := []interface{}{"RAN:" + name, data}
592 setExpected = append(setExpected, "GNB:"+plmnId+":"+nbId, data)
593 expectedErr := errors.New("expected error")
594 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
595 rNibErr := w.SaveNodeb(nbIdentity, &gnb)
596 assert.NotEmpty(t, rNibErr)
599 func TestGetRNibWriter(t *testing.T) {
600 received, _ := initSdlInstanceMock(namespace)
601 assert.NotEmpty(t, received)
604 func TestSaveE2TInstanceSuccess(t *testing.T) {
605 address := "10.10.2.15:9800"
606 loadKey, validationErr := common.ValidateAndBuildE2TInstanceKey(address)
608 if validationErr != nil {
609 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSuccess - Failed to build E2T Instance key. Error: %v", validationErr)
612 w, sdlInstanceMock := initSdlInstanceMock(namespace)
614 e2tInstance := generateE2tInstance(address)
615 data, err := json.Marshal(e2tInstance)
618 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSuccess - Failed to marshal E2tInstance entity. Error: %v", err)
622 var setExpected []interface{}
623 setExpected = append(setExpected, loadKey, data)
624 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
626 rNibErr := w.SaveE2TInstance(e2tInstance)
627 assert.Nil(t, rNibErr)
630 func TestSaveE2TInstanceNullE2tInstanceFailure(t *testing.T) {
631 w, _ := initSdlInstanceMock(namespace)
633 e2tInstance := entities.NewE2TInstance(address, "test")
634 err := w.SaveE2TInstance(e2tInstance)
635 assert.NotNil(t, err)
636 assert.IsType(t, &common.ValidationError{}, err)
639 func TestSaveE2TInstanceSdlFailure(t *testing.T) {
640 address := "10.10.2.15:9800"
641 loadKey, validationErr := common.ValidateAndBuildE2TInstanceKey(address)
643 if validationErr != nil {
644 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSdlFailure - Failed to build E2T Instance key. Error: %v", validationErr)
647 w, sdlInstanceMock := initSdlInstanceMock(namespace)
649 e2tInstance := generateE2tInstance(address)
650 data, err := json.Marshal(e2tInstance)
653 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSdlFailure - Failed to marshal E2tInstance entity. Error: %v", err)
656 expectedErr := errors.New("expected error")
657 var setExpected []interface{}
658 setExpected = append(setExpected, loadKey, data)
659 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
661 rNibErr := w.SaveE2TInstance(e2tInstance)
662 assert.NotNil(t, rNibErr)
663 assert.IsType(t, &common.InternalError{}, rNibErr)
666 func generateE2tInstance(address string) *entities.E2TInstance {
667 e2tInstance := entities.NewE2TInstance(address, "pod test")
669 e2tInstance.AssociatedRanList = []string{"test1", "test2"}
674 func TestSaveE2TAddressesSuccess(t *testing.T) {
675 address := "10.10.2.15:9800"
676 w, sdlInstanceMock := initSdlInstanceMock(namespace)
678 e2tAddresses := []string{address}
679 data, err := json.Marshal(e2tAddresses)
682 t.Errorf("#rNibWriter_test.TestSaveE2TInfoListSuccess - Failed to marshal E2TInfoList. Error: %v", err)
686 var setExpected []interface{}
687 setExpected = append(setExpected, E2TAddressesKey, data)
688 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
690 rNibErr := w.SaveE2TAddresses(e2tAddresses)
691 assert.Nil(t, rNibErr)
694 func TestSaveE2TAddressesSdlFailure(t *testing.T) {
695 address := "10.10.2.15:9800"
696 w, sdlInstanceMock := initSdlInstanceMock(namespace)
698 e2tAddresses := []string{address}
699 data, err := json.Marshal(e2tAddresses)
702 t.Errorf("#rNibWriter_test.TestSaveE2TInfoListSdlFailure - Failed to marshal E2TInfoList. Error: %v", err)
705 expectedErr := errors.New("expected error")
706 var setExpected []interface{}
707 setExpected = append(setExpected, E2TAddressesKey, data)
708 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
710 rNibErr := w.SaveE2TAddresses(e2tAddresses)
711 assert.NotNil(t, rNibErr)
712 assert.IsType(t, &common.InternalError{}, rNibErr)
715 func TestRemoveE2TInstanceSuccess(t *testing.T) {
716 address := "10.10.2.15:9800"
717 w, sdlInstanceMock := initSdlInstanceMock(namespace)
719 e2tAddresses := []string{fmt.Sprintf("E2TInstance:%s", address)}
721 sdlInstanceMock.On("Remove", e2tAddresses).Return(e)
723 rNibErr := w.RemoveE2TInstance(address)
724 assert.Nil(t, rNibErr)
725 sdlInstanceMock.AssertExpectations(t)
728 func TestRemoveE2TInstanceSdlFailure(t *testing.T) {
729 address := "10.10.2.15:9800"
730 w, sdlInstanceMock := initSdlInstanceMock(namespace)
732 e2tAddresses := []string{fmt.Sprintf("E2TInstance:%s", address)}
733 expectedErr := errors.New("expected error")
734 sdlInstanceMock.On("Remove", e2tAddresses).Return(expectedErr)
736 rNibErr := w.RemoveE2TInstance(address)
737 assert.IsType(t, &common.InternalError{}, rNibErr)
740 func TestRemoveE2TInstanceEmptyAddressFailure(t *testing.T) {
741 w, sdlInstanceMock := initSdlInstanceMock(namespace)
743 rNibErr := w.RemoveE2TInstance("")
744 assert.IsType(t, &common.ValidationError{}, rNibErr)
745 sdlInstanceMock.AssertExpectations(t)
748 func TestUpdateNodebInfoOnConnectionStatusInversionSuccess(t *testing.T) {
749 inventoryName := "name"
752 channelName := "RAN_CONNECT_STATE_CHANGE"
753 eventName := inventoryName + "_" + "CONNECTED"
754 w, sdlInstanceMock := initSdlInstanceMock(namespace)
755 nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
756 data, err := proto.Marshal(nodebInfo)
758 t.Errorf("#rNibWriter_test.TestUpdateNodebInfoOnConnectionStatusInversionSuccess - Failed to marshal NodeB entity. Error: %v", err)
761 var setExpected []interface{}
763 nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
764 nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
765 setExpected = append(setExpected, nodebNameKey, data)
766 setExpected = append(setExpected, nodebIdKey, data)
768 sdlInstanceMock.On("SetAndPublish", []string{channelName, eventName}, []interface{}{setExpected}).Return(e)
770 rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, channelName, eventName)
771 assert.Nil(t, rNibErr)
774 func TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure(t *testing.T) {
775 inventoryName := "name"
778 channelName := "RAN_CONNECT_STATE_CHANGE"
779 eventName := inventoryName + "_" + "CONNECTED"
780 w, sdlInstanceMock := initSdlInstanceMock(namespace)
781 nodebInfo := &entities.NodebInfo{}
782 data, err := proto.Marshal(nodebInfo)
784 t.Errorf("#rNibWriter_test.TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure - Failed to marshal NodeB entity. Error: %v", err)
787 var setExpected []interface{}
789 nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
790 nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
791 setExpected = append(setExpected, nodebNameKey, data)
792 setExpected = append(setExpected, nodebIdKey, data)
794 sdlInstanceMock.On("SetAndPublish", []string{channelName, eventName}, []interface{}{setExpected}).Return(e)
796 rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, channelName, eventName)
798 assert.NotNil(t, rNibErr)
799 assert.IsType(t, &common.ValidationError{}, rNibErr)
802 func TestUpdateNodebInfoOnConnectionStatusInversionMissingGlobalNbId(t *testing.T) {
803 inventoryName := "name"
804 channelName := "RAN_CONNECT_STATE_CHANGE"
805 eventName := inventoryName + "_" + "CONNECTED"
806 w, sdlInstanceMock := initSdlInstanceMock(namespace)
807 nodebInfo := &entities.NodebInfo{}
808 nodebInfo.RanName = inventoryName
809 data, err := proto.Marshal(nodebInfo)
811 t.Errorf("#rNibWriter_test.TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure - Failed to marshal NodeB entity. Error: %v", err)
814 var setExpected []interface{}
816 nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
817 setExpected = append(setExpected, nodebNameKey, data)
818 sdlInstanceMock.On("SetAndPublish", []string{channelName, eventName}, []interface{}{setExpected}).Return(e)
820 rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, channelName, eventName)
822 assert.Nil(t, rNibErr)
825 func TestSaveGeneralConfiguration(t *testing.T) {
826 w, sdlInstanceMock := initSdlInstanceMock(namespace)
828 key := common.BuildGeneralConfigurationKey()
829 configurationData := "{\"enableRic\":true}"
830 configuration := &entities.GeneralConfiguration{}
831 configuration.EnableRic = true
833 sdlInstanceMock.On("Set", []interface{}{[]interface{}{key, []byte(configurationData)}}).Return(nil)
834 rNibErr := w.SaveGeneralConfiguration(configuration)
836 assert.Nil(t, rNibErr)
837 sdlInstanceMock.AssertExpectations(t)
840 func TestSaveGeneralConfigurationDbError(t *testing.T) {
841 w, sdlInstanceMock := initSdlInstanceMock(namespace)
843 key := common.BuildGeneralConfigurationKey()
844 configurationData := "{\"enableRic\":true}"
845 configuration := &entities.GeneralConfiguration{}
846 configuration.EnableRic = true
848 expectedErr := errors.New("expected error")
850 sdlInstanceMock.On("Set", []interface{}{[]interface{}{key, []byte(configurationData)}}).Return(expectedErr)
851 rNibErr := w.SaveGeneralConfiguration(configuration)
853 assert.NotNil(t, rNibErr)
858 //func TestSaveEnbGnbInteg(t *testing.T){
859 // for i := 0; i<10; i++{
860 // Init("e2Manager", 1)
861 // w := GetRNibWriter()
862 // nb := entities.NodebInfo{}
863 // nb.NodeType = entities.Node_ENB
864 // nb.ConnectionStatus = entities.ConnectionStatus_CONNECTED
865 // nb.Ip = "localhost"
866 // nb.Port = uint32(5656 + i)
867 // enb := entities.Enb{}
868 // cell1 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",111 + i), Pci:uint32(11 + i)}
869 // cell2 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",222 + i), Pci:uint32(22 + i)}
870 // cell3 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",333 + i), Pci:uint32(33 + i)}
871 // enb.ServedCells = []*entities.ServedCellInfo{cell1, cell2, cell3}
872 // nb.Configuration = &entities.NodebInfo_Enb{Enb:&enb}
873 // plmnId := 0x02f828
874 // nbId := 0x4a952a0a
875 // nbIdentity := &entities.NbIdentity{InventoryName: fmt.Sprintf("nameEnb%d" ,i), GlobalNbId:&entities.GlobalNbId{RicId:fmt.Sprintf("%02x", plmnId + i), NbId:fmt.Sprintf("%02x", nbId + i)}}
876 // err := w.SaveNodeb(nbIdentity, &nb)
878 // t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
881 // nb1 := entities.NodebInfo{}
882 // nb1.NodeType = entities.Node_GNB
883 // nb1.ConnectionStatus = entities.ConnectionStatus_CONNECTED
884 // nb1.Ip = "localhost"
885 // nb1.Port = uint32(6565 + i)
886 // gnb := entities.Gnb{}
887 // gCell1 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",1111 + i), NrPci:uint32(1 + i)}}
888 // gCell2 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",2222 + i), NrPci:uint32(2 + i)}}
889 // gCell3 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",3333 + i), NrPci:uint32(3 + i)}}
890 // gnb.ServedNrCells = []*entities.ServedNRCell{gCell1, gCell2, gCell3,}
891 // nb1.Configuration = &entities.NodebInfo_Gnb{Gnb:&gnb}
892 // nbIdentity = &entities.NbIdentity{InventoryName: fmt.Sprintf("nameGnb%d" ,i), GlobalNbId:&entities.GlobalNbId{RicId:fmt.Sprintf("%02x", plmnId - i), NbId:fmt.Sprintf("%02x", nbId - i)}}
893 // err = w.SaveNodeb(nbIdentity, &nb1)
895 // t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
900 //func TestSaveNbRanNamesInteg(t *testing.T){
901 // for i := 0; i<10; i++{
902 // Init("e2Manager", 1)
903 // w := GetRNibWriter()
904 // nb := entities.NodebInfo{}
905 // nb.ConnectionStatus = entities.ConnectionStatus_CONNECTING
906 // nb.Ip = "localhost"
907 // nb.Port = uint32(5656 + i)
908 // nbIdentity := &entities.NbIdentity{InventoryName: fmt.Sprintf("nameOnly%d" ,i)}
909 // err := w.SaveNodeb(nbIdentity, &nb)
911 // t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
916 //func TestSaveRanLoadInformationInteg(t *testing.T){
917 // Init("e2Manager", 1)
918 // w := GetRNibWriter()
919 // ranLoadInformation := generateRanLoadInformation()
920 // err := w.SaveRanLoadInformation("ran_integ", ranLoadInformation)
922 // t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationInteg - Failed to save RanLoadInformation entity. Error: %v", err)