[RIC-431] Add Enb | Configuration changes | Some refactoring | K8S yamls
[ric-plt/e2mgr.git] / E2Manager / rNibWriter / rNibWriter_test.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 //
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
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16
17 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20 package rNibWriter
21
22 import (
23         "e2mgr/configuration"
24         "e2mgr/mocks"
25         "encoding/json"
26         "errors"
27         "fmt"
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"
32         "testing"
33         "time"
34 )
35
36 var namespace = "namespace"
37
38 const (
39         RanName = "test"
40 )
41
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"})
45         return
46 }
47
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},
52                 NodeType:         nodeType,
53                 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
54         }
55
56         if nodeType == entities.Node_ENB {
57                 nodebInfo.Configuration = &entities.NodebInfo_Enb{
58                         Enb: &entities.Enb{},
59                 }
60         } else if nodeType == entities.Node_GNB {
61                 nodebInfo.Configuration = &entities.NodebInfo_Gnb{
62                         Gnb: &entities.Gnb{},
63                 }
64         }
65
66         return nodebInfo
67 }
68
69 func generateServedNrCells(cellIds ...string) []*entities.ServedNRCell {
70
71         servedNrCells := []*entities.ServedNRCell{}
72
73         for i, v := range cellIds {
74                 servedNrCells = append(servedNrCells, &entities.ServedNRCell{ServedNrCellInformation: &entities.ServedNRCellInformation{
75                         CellId: v,
76                         ChoiceNrMode: &entities.ServedNRCellInformation_ChoiceNRMode{
77                                 Fdd: &entities.ServedNRCellInformation_ChoiceNRMode_FddInfo{
78
79                                 },
80                         },
81                         NrMode:      entities.Nr_FDD,
82                         NrPci:       uint32(i + 1),
83                         ServedPlmns: []string{"whatever"},
84                 }})
85         }
86
87         return servedNrCells
88 }
89
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)
95         assert.Nil(t, err)
96 }
97
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)
104 }
105
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)
113 }
114
115 func TestUpdateGnbCellsInvalidCellFailure(t *testing.T) {
116         inventoryName := "name"
117         plmnId := "02f829"
118         nbId := "4a952a0a"
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)
126 }
127
128 func getUpdateGnbCellsSetExpected(t *testing.T, nodebInfo *entities.NodebInfo, servedNrCells []*entities.ServedNRCell) []interface{} {
129
130         nodebInfoData, err := proto.Marshal(nodebInfo)
131         if err != nil {
132                 t.Fatalf("#rNibWriter_test.getUpdateGnbCellsSetExpected - Failed to marshal NodeB entity. Error: %s", err)
133         }
134
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}
138
139         for _, v := range servedNrCells {
140
141                 cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: v}}
142                 cellData, err := proto.Marshal(&cellEntity)
143
144                 if err != nil {
145                         t.Fatalf("#rNibWriter_test.getUpdateGnbCellsSetExpected - Failed to marshal cell entity. Error: %s", err)
146                 }
147
148                 nrCellIdKey, _ := common.ValidateAndBuildNrCellIdKey(v.GetServedNrCellInformation().GetCellId())
149                 cellNamePciKey, _ := common.ValidateAndBuildCellNamePciKey(nodebInfo.RanName, v.GetServedNrCellInformation().GetNrPci())
150                 setExpected = append(setExpected, nrCellIdKey, cellData, cellNamePciKey, cellData)
151         }
152
153         return setExpected
154 }
155
156 func TestUpdateGnbCellsSdlFailure(t *testing.T) {
157         inventoryName := "name"
158         plmnId := "02f829"
159         nbId := "4a952a0a"
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)
168 }
169
170 func TestUpdateGnbCellsSuccess(t *testing.T) {
171         inventoryName := "name"
172         plmnId := "02f829"
173         nbId := "4a952a0a"
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)
179         var e error
180         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
181         rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
182         assert.Nil(t, rNibErr)
183 }
184
185 func TestUpdateNodebInfoSuccess(t *testing.T) {
186         inventoryName := "name"
187         plmnId := "02f829"
188         nbId := "4a952a0a"
189         w, sdlInstanceMock := initSdlInstanceMock(namespace)
190         nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
191         data, err := proto.Marshal(nodebInfo)
192         if err != nil {
193                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
194         }
195         var e error
196         var setExpected []interface{}
197
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)
202
203         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
204
205         rNibErr := w.UpdateNodebInfo(nodebInfo)
206         assert.Nil(t, rNibErr)
207 }
208
209 func TestUpdateNodebInfoMissingInventoryNameFailure(t *testing.T) {
210         inventoryName := "name"
211         plmnId := "02f829"
212         nbId := "4a952a0a"
213         w, sdlInstanceMock := initSdlInstanceMock(namespace)
214         nodebInfo := &entities.NodebInfo{}
215         data, err := proto.Marshal(nodebInfo)
216         if err != nil {
217                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
218         }
219         var e error
220         var setExpected []interface{}
221
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)
226
227         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
228
229         rNibErr := w.UpdateNodebInfo(nodebInfo)
230
231         assert.NotNil(t, rNibErr)
232         assert.IsType(t, &common.ValidationError{}, rNibErr)
233 }
234
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)
241         if err != nil {
242                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
243         }
244         var e error
245         var setExpected []interface{}
246
247         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
248         setExpected = append(setExpected, nodebNameKey, data)
249         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
250
251         rNibErr := w.UpdateNodebInfo(nodebInfo)
252
253         assert.Nil(t, rNibErr)
254 }
255
256 func TestSaveEnb(t *testing.T) {
257         name := "name"
258         ranName := "RAN:" + name
259         w, sdlInstanceMock := initSdlInstanceMock(namespace)
260         nb := entities.NodebInfo{}
261         nb.RanName = name
262         nb.NodeType = entities.Node_ENB
263         nb.ConnectionStatus = 1
264         nb.Ip = "localhost"
265         nb.Port = 5656
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)
272         if err != nil {
273                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
274         }
275         var e error
276
277         cellData, err := proto.Marshal(&cellEntity)
278         if err != nil {
279                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal Cell entity. Error: %v", err)
280         }
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)
286
287         sdlInstanceMock.On("SetAndPublish", []string{"RAN_MANIPULATION", name + "_" + RanAddedEvent}, []interface{}{setExpected}).Return(e)
288
289         nbIdData, err := proto.Marshal(&entities.NbIdentity{InventoryName: name})
290         if err != nil {
291                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal nbIdentity entity. Error: %v", err)
292         }
293         sdlInstanceMock.On("RemoveMember", entities.Node_UNKNOWN.String(), []interface{}{nbIdData}).Return(e)
294
295         nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
296         nbIdData, err = proto.Marshal(nbIdentity)
297         if err != nil {
298                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB Identity entity. Error: %v", err)
299         }
300         sdlInstanceMock.On("AddMember", "ENB", []interface{}{nbIdData}).Return(e)
301
302         rNibErr := w.SaveNodeb(nbIdentity, &nb)
303         assert.Nil(t, rNibErr)
304 }
305
306 func TestSaveEnbCellIdValidationFailure(t *testing.T) {
307         name := "name"
308         w, _ := initSdlInstanceMock(namespace)
309         nb := entities.NodebInfo{}
310         nb.NodeType = entities.Node_ENB
311         nb.ConnectionStatus = 1
312         nb.Ip = "localhost"
313         nb.Port = 5656
314         enb := entities.Enb{}
315         cell := &entities.ServedCellInfo{Pci: 3}
316         enb.ServedCells = []*entities.ServedCellInfo{cell}
317         nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
318
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())
324 }
325
326 func TestSaveEnbInventoryNameValidationFailure(t *testing.T) {
327         w, _ := initSdlInstanceMock(namespace)
328         nb := entities.NodebInfo{}
329         nb.NodeType = entities.Node_ENB
330         nb.ConnectionStatus = 1
331         nb.Ip = "localhost"
332         nb.Port = 5656
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}
337
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())
343 }
344
345 func TestSaveGnbCellIdValidationFailure(t *testing.T) {
346         name := "name"
347         w, _ := initSdlInstanceMock(namespace)
348         nb := entities.NodebInfo{}
349         nb.NodeType = entities.Node_GNB
350         nb.ConnectionStatus = 1
351         nb.Ip = "localhost"
352         nb.Port = 5656
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}
358
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())
364 }
365
366 func TestSaveGnb(t *testing.T) {
367         name := "name"
368         ranName := "RAN:" + name
369         w, sdlInstanceMock := initSdlInstanceMock(namespace)
370         nb := entities.NodebInfo{}
371         nb.NodeType = entities.Node_GNB
372         nb.ConnectionStatus = 1
373         nb.Ip = "localhost"
374         nb.Port = 5656
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)
382         if err != nil {
383                 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal NodeB entity. Error: %v", err)
384         }
385         var e error
386
387         cellData, err := proto.Marshal(&cellEntity)
388         if err != nil {
389                 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal Cell entity. Error: %v", err)
390         }
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)
396
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)
400         if err != nil {
401                 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal NodeB Identity entity. Error: %v", err)
402         }
403         sdlInstanceMock.On("AddMember", "GNB", []interface{}{nbIdData}).Return(e)
404
405         nbIdData, err = proto.Marshal(&entities.NbIdentity{InventoryName: name})
406         if err != nil {
407                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal nbIdentity entity. Error: %v", err)
408         }
409         sdlInstanceMock.On("RemoveMember", entities.Node_UNKNOWN.String(), []interface{}{nbIdData}).Return(e)
410
411         rNibErr := w.SaveNodeb(nbIdentity, &nb)
412         assert.Nil(t, rNibErr)
413 }
414
415 func TestSaveRanLoadInformationSuccess(t *testing.T) {
416         inventoryName := "name"
417         loadKey, validationErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
418
419         if validationErr != nil {
420                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationSuccess - Failed to build ran load infromation key. Error: %v", validationErr)
421         }
422
423         w, sdlInstanceMock := initSdlInstanceMock(namespace)
424
425         ranLoadInformation := generateRanLoadInformation()
426         data, err := proto.Marshal(ranLoadInformation)
427
428         if err != nil {
429                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformation - Failed to marshal RanLoadInformation entity. Error: %v", err)
430         }
431
432         var e error
433         var setExpected []interface{}
434         setExpected = append(setExpected, loadKey, data)
435         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
436
437         rNibErr := w.SaveRanLoadInformation(inventoryName, ranLoadInformation)
438         assert.Nil(t, rNibErr)
439 }
440
441 func TestSaveRanLoadInformationMarshalNilFailure(t *testing.T) {
442         inventoryName := "name2"
443         w, _ := initSdlInstanceMock(namespace)
444
445         expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
446         err := w.SaveRanLoadInformation(inventoryName, nil)
447         assert.Equal(t, expectedErr, err)
448 }
449
450 func TestSaveRanLoadInformationEmptyInventoryNameFailure(t *testing.T) {
451         inventoryName := ""
452         w, _ := initSdlInstanceMock(namespace)
453
454         err := w.SaveRanLoadInformation(inventoryName, nil)
455         assert.NotNil(t, err)
456         assert.IsType(t, &common.ValidationError{}, err)
457 }
458
459 func TestSaveRanLoadInformationSdlFailure(t *testing.T) {
460         inventoryName := "name2"
461
462         loadKey, validationErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
463
464         if validationErr != nil {
465                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationSuccess - Failed to build ran load infromation key. Error: %v", validationErr)
466         }
467
468         w, sdlInstanceMock := initSdlInstanceMock(namespace)
469
470         ranLoadInformation := generateRanLoadInformation()
471         data, err := proto.Marshal(ranLoadInformation)
472
473         if err != nil {
474                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformation - Failed to marshal RanLoadInformation entity. Error: %v", err)
475         }
476
477         expectedErr := errors.New("expected error")
478         var setExpected []interface{}
479         setExpected = append(setExpected, loadKey, data)
480         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
481
482         rNibErr := w.SaveRanLoadInformation(inventoryName, ranLoadInformation)
483         assert.NotNil(t, rNibErr)
484         assert.IsType(t, &common.InternalError{}, rNibErr)
485 }
486
487 func generateCellLoadInformation() *entities.CellLoadInformation {
488         cellLoadInformation := entities.CellLoadInformation{}
489
490         cellLoadInformation.CellId = "123"
491
492         ulInterferenceOverloadIndication := entities.UlInterferenceOverloadIndication_HIGH_INTERFERENCE
493         cellLoadInformation.UlInterferenceOverloadIndications = []entities.UlInterferenceOverloadIndication{ulInterferenceOverloadIndication}
494
495         ulHighInterferenceInformation := entities.UlHighInterferenceInformation{
496                 TargetCellId:                 "456",
497                 UlHighInterferenceIndication: "xxx",
498         }
499
500         cellLoadInformation.UlHighInterferenceInfos = []*entities.UlHighInterferenceInformation{&ulHighInterferenceInformation}
501
502         cellLoadInformation.RelativeNarrowbandTxPower = &entities.RelativeNarrowbandTxPower{
503                 RntpPerPrb:                       "xxx",
504                 RntpThreshold:                    entities.RntpThreshold_NEG_4,
505                 NumberOfCellSpecificAntennaPorts: entities.NumberOfCellSpecificAntennaPorts_V1_ANT_PRT,
506                 PB:                               1,
507                 PdcchInterferenceImpact:          2,
508                 EnhancedRntp: &entities.EnhancedRntp{
509                         EnhancedRntpBitmap:     "xxx",
510                         RntpHighPowerThreshold: entities.RntpThreshold_NEG_2,
511                         EnhancedRntpStartTime:  &entities.StartTime{StartSfn: 500, StartSubframeNumber: 5},
512                 },
513         }
514
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",
520         }
521
522         cellLoadInformation.InvokeIndication = entities.InvokeIndication_ABS_INFORMATION
523
524         cellLoadInformation.ExtendedUlInterferenceOverloadInfo = &entities.ExtendedUlInterferenceOverloadInfo{
525                 AssociatedSubframes:                       "xxx",
526                 ExtendedUlInterferenceOverloadIndications: cellLoadInformation.UlInterferenceOverloadIndications,
527         }
528
529         compInformationItem := &entities.CompInformationItem{
530                 CompHypothesisSets: []*entities.CompHypothesisSet{{CellId: "789", CompHypothesis: "xxx"}},
531                 BenefitMetric:      50,
532         }
533
534         cellLoadInformation.CompInformation = &entities.CompInformation{
535                 CompInformationItems:     []*entities.CompInformationItem{compInformationItem},
536                 CompInformationStartTime: &entities.StartTime{StartSfn: 123, StartSubframeNumber: 456},
537         }
538
539         cellLoadInformation.DynamicDlTransmissionInformation = &entities.DynamicDlTransmissionInformation{
540                 State:             entities.NaicsState_NAICS_ACTIVE,
541                 TransmissionModes: "xxx",
542                 PB:                2,
543                 PAList:            []entities.PA{entities.PA_DB_NEG_3},
544         }
545
546         return &cellLoadInformation
547 }
548
549 func generateRanLoadInformation() *entities.RanLoadInformation {
550         ranLoadInformation := entities.RanLoadInformation{}
551
552         ranLoadInformation.LoadTimestamp = uint64(time.Now().UnixNano())
553
554         cellLoadInformation := generateCellLoadInformation()
555         ranLoadInformation.CellLoadInfos = []*entities.CellLoadInformation{cellLoadInformation}
556
557         return &ranLoadInformation
558 }
559
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)
566 }
567
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{}
572         nb.Port = 5656
573         nb.Ip = "localhost"
574         actualErr := w.SaveNodeb(nbIdentity, nb)
575         assert.IsType(t, &common.ValidationError{}, actualErr)
576 }
577
578 func TestSaveEntityFailure(t *testing.T) {
579         name := "name"
580         plmnId := "02f829"
581         nbId := "4a952a0a"
582
583         w, sdlInstanceMock := initSdlInstanceMock(namespace)
584         gnb := entities.NodebInfo{}
585         gnb.NodeType = entities.Node_GNB
586         data, err := proto.Marshal(&gnb)
587         if err != nil {
588                 t.Errorf("#rNibWriter_test.TestSaveEntityFailure - Failed to marshal NodeB entity. Error: %v", err)
589         }
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)
597 }
598
599 func TestGetRNibWriter(t *testing.T) {
600         received, _ := initSdlInstanceMock(namespace)
601         assert.NotEmpty(t, received)
602 }
603
604 func TestSaveE2TInstanceSuccess(t *testing.T) {
605         address := "10.10.2.15:9800"
606         loadKey, validationErr := common.ValidateAndBuildE2TInstanceKey(address)
607
608         if validationErr != nil {
609                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSuccess - Failed to build E2T Instance key. Error: %v", validationErr)
610         }
611
612         w, sdlInstanceMock := initSdlInstanceMock(namespace)
613
614         e2tInstance := generateE2tInstance(address)
615         data, err := json.Marshal(e2tInstance)
616
617         if err != nil {
618                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSuccess - Failed to marshal E2tInstance entity. Error: %v", err)
619         }
620
621         var e error
622         var setExpected []interface{}
623         setExpected = append(setExpected, loadKey, data)
624         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
625
626         rNibErr := w.SaveE2TInstance(e2tInstance)
627         assert.Nil(t, rNibErr)
628 }
629
630 func TestSaveE2TInstanceNullE2tInstanceFailure(t *testing.T) {
631         w, _ := initSdlInstanceMock(namespace)
632         var address string
633         e2tInstance := entities.NewE2TInstance(address, "test")
634         err := w.SaveE2TInstance(e2tInstance)
635         assert.NotNil(t, err)
636         assert.IsType(t, &common.ValidationError{}, err)
637 }
638
639 func TestSaveE2TInstanceSdlFailure(t *testing.T) {
640         address := "10.10.2.15:9800"
641         loadKey, validationErr := common.ValidateAndBuildE2TInstanceKey(address)
642
643         if validationErr != nil {
644                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSdlFailure - Failed to build E2T Instance key. Error: %v", validationErr)
645         }
646
647         w, sdlInstanceMock := initSdlInstanceMock(namespace)
648
649         e2tInstance := generateE2tInstance(address)
650         data, err := json.Marshal(e2tInstance)
651
652         if err != nil {
653                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSdlFailure - Failed to marshal E2tInstance entity. Error: %v", err)
654         }
655
656         expectedErr := errors.New("expected error")
657         var setExpected []interface{}
658         setExpected = append(setExpected, loadKey, data)
659         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
660
661         rNibErr := w.SaveE2TInstance(e2tInstance)
662         assert.NotNil(t, rNibErr)
663         assert.IsType(t, &common.InternalError{}, rNibErr)
664 }
665
666 func generateE2tInstance(address string) *entities.E2TInstance {
667         e2tInstance := entities.NewE2TInstance(address, "pod test")
668
669         e2tInstance.AssociatedRanList = []string{"test1", "test2"}
670
671         return e2tInstance
672 }
673
674 func TestSaveE2TAddressesSuccess(t *testing.T) {
675         address := "10.10.2.15:9800"
676         w, sdlInstanceMock := initSdlInstanceMock(namespace)
677
678         e2tAddresses := []string{address}
679         data, err := json.Marshal(e2tAddresses)
680
681         if err != nil {
682                 t.Errorf("#rNibWriter_test.TestSaveE2TInfoListSuccess - Failed to marshal E2TInfoList. Error: %v", err)
683         }
684
685         var e error
686         var setExpected []interface{}
687         setExpected = append(setExpected, E2TAddressesKey, data)
688         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
689
690         rNibErr := w.SaveE2TAddresses(e2tAddresses)
691         assert.Nil(t, rNibErr)
692 }
693
694 func TestSaveE2TAddressesSdlFailure(t *testing.T) {
695         address := "10.10.2.15:9800"
696         w, sdlInstanceMock := initSdlInstanceMock(namespace)
697
698         e2tAddresses := []string{address}
699         data, err := json.Marshal(e2tAddresses)
700
701         if err != nil {
702                 t.Errorf("#rNibWriter_test.TestSaveE2TInfoListSdlFailure - Failed to marshal E2TInfoList. Error: %v", err)
703         }
704
705         expectedErr := errors.New("expected error")
706         var setExpected []interface{}
707         setExpected = append(setExpected, E2TAddressesKey, data)
708         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
709
710         rNibErr := w.SaveE2TAddresses(e2tAddresses)
711         assert.NotNil(t, rNibErr)
712         assert.IsType(t, &common.InternalError{}, rNibErr)
713 }
714
715 func TestRemoveE2TInstanceSuccess(t *testing.T) {
716         address := "10.10.2.15:9800"
717         w, sdlInstanceMock := initSdlInstanceMock(namespace)
718
719         e2tAddresses := []string{fmt.Sprintf("E2TInstance:%s", address)}
720         var e error
721         sdlInstanceMock.On("Remove", e2tAddresses).Return(e)
722
723         rNibErr := w.RemoveE2TInstance(address)
724         assert.Nil(t, rNibErr)
725         sdlInstanceMock.AssertExpectations(t)
726 }
727
728 func TestRemoveE2TInstanceSdlFailure(t *testing.T) {
729         address := "10.10.2.15:9800"
730         w, sdlInstanceMock := initSdlInstanceMock(namespace)
731
732         e2tAddresses := []string{fmt.Sprintf("E2TInstance:%s", address)}
733         expectedErr := errors.New("expected error")
734         sdlInstanceMock.On("Remove", e2tAddresses).Return(expectedErr)
735
736         rNibErr := w.RemoveE2TInstance(address)
737         assert.IsType(t, &common.InternalError{}, rNibErr)
738 }
739
740 func TestRemoveE2TInstanceEmptyAddressFailure(t *testing.T) {
741         w, sdlInstanceMock := initSdlInstanceMock(namespace)
742
743         rNibErr := w.RemoveE2TInstance("")
744         assert.IsType(t, &common.ValidationError{}, rNibErr)
745         sdlInstanceMock.AssertExpectations(t)
746 }
747
748 func TestUpdateNodebInfoOnConnectionStatusInversionSuccess(t *testing.T) {
749         inventoryName := "name"
750         plmnId := "02f829"
751         nbId := "4a952a0a"
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)
757         if err != nil {
758                 t.Errorf("#rNibWriter_test.TestUpdateNodebInfoOnConnectionStatusInversionSuccess - Failed to marshal NodeB entity. Error: %v", err)
759         }
760         var e error
761         var setExpected []interface{}
762
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)
767
768         sdlInstanceMock.On("SetAndPublish", []string{channelName, eventName}, []interface{}{setExpected}).Return(e)
769
770         rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, channelName, eventName)
771         assert.Nil(t, rNibErr)
772 }
773
774 func TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure(t *testing.T) {
775         inventoryName := "name"
776         plmnId := "02f829"
777         nbId := "4a952a0a"
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)
783         if err != nil {
784                 t.Errorf("#rNibWriter_test.TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure - Failed to marshal NodeB entity. Error: %v", err)
785         }
786         var e error
787         var setExpected []interface{}
788
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)
793
794         sdlInstanceMock.On("SetAndPublish", []string{channelName, eventName}, []interface{}{setExpected}).Return(e)
795
796         rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, channelName, eventName)
797
798         assert.NotNil(t, rNibErr)
799         assert.IsType(t, &common.ValidationError{}, rNibErr)
800 }
801
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)
810         if err != nil {
811                 t.Errorf("#rNibWriter_test.TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure - Failed to marshal NodeB entity. Error: %v", err)
812         }
813         var e error
814         var setExpected []interface{}
815
816         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
817         setExpected = append(setExpected, nodebNameKey, data)
818         sdlInstanceMock.On("SetAndPublish", []string{channelName, eventName}, []interface{}{setExpected}).Return(e)
819
820         rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, channelName, eventName)
821
822         assert.Nil(t, rNibErr)
823 }
824
825 func TestSaveGeneralConfiguration(t *testing.T) {
826         w, sdlInstanceMock := initSdlInstanceMock(namespace)
827
828         key := common.BuildGeneralConfigurationKey()
829         configurationData := "{\"enableRic\":true}"
830         configuration := &entities.GeneralConfiguration{}
831         configuration.EnableRic = true
832
833         sdlInstanceMock.On("Set", []interface{}{[]interface{}{key, []byte(configurationData)}}).Return(nil)
834         rNibErr := w.SaveGeneralConfiguration(configuration)
835
836         assert.Nil(t, rNibErr)
837         sdlInstanceMock.AssertExpectations(t)
838 }
839
840 func TestSaveGeneralConfigurationDbError(t *testing.T) {
841         w, sdlInstanceMock := initSdlInstanceMock(namespace)
842
843         key := common.BuildGeneralConfigurationKey()
844         configurationData := "{\"enableRic\":true}"
845         configuration := &entities.GeneralConfiguration{}
846         configuration.EnableRic = true
847
848         expectedErr := errors.New("expected error")
849
850         sdlInstanceMock.On("Set", []interface{}{[]interface{}{key, []byte(configurationData)}}).Return(expectedErr)
851         rNibErr := w.SaveGeneralConfiguration(configuration)
852
853         assert.NotNil(t, rNibErr)
854 }
855
856 //Integration tests
857 //
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)
877 //              if err != nil{
878 //                      t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
879 //              }
880 //
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)
894 //              if err != nil{
895 //                      t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
896 //              }
897 //      }
898 //}
899 //
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)
910 //              if err != nil{
911 //                      t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
912 //              }
913 //      }
914 //}
915 //
916 //func TestSaveRanLoadInformationInteg(t *testing.T){
917 //              Init("e2Manager", 1)
918 //              w := GetRNibWriter()
919 //              ranLoadInformation := generateRanLoadInformation()
920 //              err := w.SaveRanLoadInformation("ran_integ", ranLoadInformation)
921 //              if err != nil{
922 //                      t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationInteg - Failed to save RanLoadInformation entity. Error: %v", err)
923 //              }
924 //}