1 //// Copyright 2019 AT&T Intellectual Property
2 //// Copyright 2019 Nokia
4 //// Licensed under the Apache License, Version 2.0 (the "License");
5 //// you may not use this file except in compliance with the License.
6 //// You may obtain a copy of the License at
8 //// http://www.apache.org/licenses/LICENSE-2.0
10 //// Unless required by applicable law or agreed to in writing, software
11 //// distributed under the License is distributed on an "AS IS" BASIS,
12 //// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 //// See the License for the specific language governing permissions and
14 //// limitations under the License.
16 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 // platform project (RICP).
19 package rmrmsghandlers
35 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
36 "github.com/stretchr/testify/mock"
41 e2tAddress = "10.10.2.15:9800"
44 func setupLostConnectionHandlerTest(isSuccessfulHttpPost bool) (*RanLostConnectionHandler, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *mocks.HttpClientMock) {
46 logger, _ := logger.InitLogger(InfoLevel)
47 config := &configuration.Configuration{
48 RnibRetryIntervalMs: 10,
49 MaxRnibConnectionAttempts: 3,
50 RnibWriter: configuration.RnibWriterConfig{
51 StateChangeMessageChannel: StateChangeMessageChannel,
55 readerMock := &mocks.RnibReaderMock{}
56 writerMock := &mocks.RnibWriterMock{}
57 rnibDataService := services.NewRnibDataService(logger, config, readerMock, writerMock)
58 e2tInstancesManager := managers.NewE2TInstancesManager(rnibDataService, logger)
59 httpClientMock := &mocks.HttpClientMock{}
60 routingManagerClient := clients.NewRoutingManagerClient(logger, config, httpClientMock)
61 ranListManager := managers.NewRanListManager(logger, rnibDataService)
62 ranAlarmService := services.NewRanAlarmService(logger, config)
63 ranConnectStatusChangeManager := managers.NewRanConnectStatusChangeManager(logger, rnibDataService, ranListManager, ranAlarmService)
65 e2tAssociationManager := managers.NewE2TAssociationManager(logger, rnibDataService, e2tInstancesManager, routingManagerClient, ranConnectStatusChangeManager)
66 ranDisconnectionManager := managers.NewRanDisconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, e2tAssociationManager, ranConnectStatusChangeManager)
67 handler := NewRanLostConnectionHandler(logger, ranDisconnectionManager)
69 return handler, readerMock, writerMock, httpClientMock
72 func mockHttpClient(httpClientMock *mocks.HttpClientMock, isSuccessful bool) {
73 data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(e2tAddress, RanName)}
74 marshaled, _ := json.Marshal(data)
75 body := bytes.NewBuffer(marshaled)
76 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
77 var respStatusCode int
79 respStatusCode = http.StatusCreated
81 respStatusCode = http.StatusBadRequest
83 httpClientMock.On("Post", clients.DissociateRanE2TInstanceApiSuffix, "application/json", body).Return(&http.Response{StatusCode: respStatusCode, Body: respBody}, nil)
86 func TestLostConnectionHandlerConnectingRanSuccess(t *testing.T) {
87 handler, readerMock, writerMock, httpClientMock := setupLostConnectionHandlerTest(true)
89 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, AssociatedE2TInstanceAddress: e2tAddress}
91 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
92 updatedNodebInfo1 := *origNodebInfo
93 updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
94 writerMock.On("UpdateNodebInfo", mock.Anything).Return(rnibErr)
95 updatedNodebInfo2 := *origNodebInfo
96 updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
97 updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
98 writerMock.On("UpdateNodebInfo", mock.Anything).Return(rnibErr)
99 e2tInstance := &entities.E2TInstance{Address: e2tAddress, AssociatedRanList: []string{ranName}}
100 readerMock.On("GetE2TInstance", e2tAddress).Return(e2tInstance, nil)
101 e2tInstanceToSave := *e2tInstance
102 e2tInstanceToSave.AssociatedRanList = []string{}
103 writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
104 mockHttpClient(httpClientMock, true)
105 notificationRequest := models.NotificationRequest{RanName: ranName}
106 handler.Handle(¬ificationRequest)
107 readerMock.AssertExpectations(t)
108 writerMock.AssertExpectations(t)
109 httpClientMock.AssertExpectations(t)
110 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
113 func TestLostConnectionHandlerConnectedRanSuccess(t *testing.T) {
114 handler, readerMock, writerMock, httpClientMock := setupLostConnectionHandlerTest(true)
116 origNodebInfo := &entities.NodebInfo{
118 GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"},
119 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
120 AssociatedE2TInstanceAddress: e2tAddress,
123 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
124 updatedNodebInfo1 := *origNodebInfo
125 updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
126 writerMock.On("UpdateNodebInfoOnConnectionStatusInversion", mock.Anything, ranName+"_DISCONNECTED").Return(rnibErr)
127 updatedNodebInfo2 := *origNodebInfo
128 updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
129 updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
130 writerMock.On("UpdateNodebInfo", mock.Anything).Return(rnibErr)
131 e2tInstance := &entities.E2TInstance{Address: e2tAddress, AssociatedRanList: []string{ranName}}
132 readerMock.On("GetE2TInstance", e2tAddress).Return(e2tInstance, nil)
133 e2tInstanceToSave := *e2tInstance
134 e2tInstanceToSave.AssociatedRanList = []string{}
135 writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
136 mockHttpClient(httpClientMock, true)
137 notificationRequest := models.NotificationRequest{RanName: ranName}
138 handler.Handle(¬ificationRequest)
139 readerMock.AssertExpectations(t)
140 writerMock.AssertExpectations(t)
141 httpClientMock.AssertExpectations(t)
142 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
145 func TestLostConnectionHandlerRmDissociateFailure(t *testing.T) {
146 handler, readerMock, writerMock, httpClientMock := setupLostConnectionHandlerTest(false)
148 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, AssociatedE2TInstanceAddress: e2tAddress}
150 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
151 updatedNodebInfo1 := *origNodebInfo
152 updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
153 writerMock.On("UpdateNodebInfo", mock.Anything).Return(rnibErr)
154 updatedNodebInfo2 := *origNodebInfo
155 updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
156 updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
157 writerMock.On("UpdateNodebInfo", mock.Anything).Return(rnibErr)
158 e2tInstance := &entities.E2TInstance{Address: e2tAddress, AssociatedRanList: []string{ranName}}
159 readerMock.On("GetE2TInstance", e2tAddress).Return(e2tInstance, nil)
160 e2tInstanceToSave := *e2tInstance
161 e2tInstanceToSave.AssociatedRanList = []string{}
162 writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
163 mockHttpClient(httpClientMock, false)
164 notificationRequest := models.NotificationRequest{RanName: ranName}
165 handler.Handle(¬ificationRequest)
166 readerMock.AssertExpectations(t)
167 writerMock.AssertExpectations(t)
168 httpClientMock.AssertExpectations(t)
169 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)