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
31 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
39 e2tAddress = "10.10.2.15:9800"
42 func setupLostConnectionHandlerTest(isSuccessfulHttpPost bool) (*RanLostConnectionHandler, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *mocks.HttpClientMock) {
43 logger, _ := logger.InitLogger(logger.InfoLevel)
44 config := &configuration.Configuration{
45 RnibRetryIntervalMs: 10,
46 MaxRnibConnectionAttempts: 3,
47 RnibWriter: configuration.RnibWriterConfig {
48 StateChangeMessageChannel: StateChangeMessageChannel,
52 readerMock := &mocks.RnibReaderMock{}
53 writerMock := &mocks.RnibWriterMock{}
54 rnibDataService := services.NewRnibDataService(logger, config, readerMock, writerMock)
55 e2tInstancesManager := managers.NewE2TInstancesManager(rnibDataService, logger)
56 httpClientMock := &mocks.HttpClientMock{}
57 routingManagerClient := clients.NewRoutingManagerClient(logger, config, httpClientMock)
58 ranListManager := managers.NewRanListManager(logger, rnibDataService)
59 ranAlarmService := services.NewRanAlarmService(logger, config)
60 ranConnectStatusChangeManager := managers.NewRanConnectStatusChangeManager(logger, rnibDataService, ranListManager, ranAlarmService)
62 e2tAssociationManager := managers.NewE2TAssociationManager(logger, rnibDataService, e2tInstancesManager, routingManagerClient, ranConnectStatusChangeManager)
63 ranDisconnectionManager := managers.NewRanDisconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, e2tAssociationManager, ranConnectStatusChangeManager)
64 handler := NewRanLostConnectionHandler(logger, ranDisconnectionManager)
66 return handler, readerMock, writerMock, httpClientMock
69 func mockHttpClient(httpClientMock *mocks.HttpClientMock, isSuccessful bool) {
70 data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(e2tAddress, RanName)}
71 marshaled, _ := json.Marshal(data)
72 body := bytes.NewBuffer(marshaled)
73 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
74 var respStatusCode int
76 respStatusCode = http.StatusCreated
78 respStatusCode = http.StatusBadRequest
80 httpClientMock.On("Post", clients.DissociateRanE2TInstanceApiSuffix, "application/json", body).Return(&http.Response{StatusCode: respStatusCode, Body: respBody}, nil)
83 func TestLostConnectionHandlerConnectingRanSuccess(t *testing.T) {
84 handler, readerMock, writerMock, httpClientMock := setupLostConnectionHandlerTest(true)
86 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, AssociatedE2TInstanceAddress: e2tAddress}
88 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
89 updatedNodebInfo1 := *origNodebInfo
90 updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
91 writerMock.On("UpdateNodebInfo", &updatedNodebInfo1).Return(rnibErr)
92 updatedNodebInfo2 := *origNodebInfo
93 updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
94 updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
95 writerMock.On("UpdateNodebInfo", &updatedNodebInfo2).Return(rnibErr)
96 e2tInstance := &entities.E2TInstance{Address: e2tAddress, AssociatedRanList: []string{ranName}}
97 readerMock.On("GetE2TInstance", e2tAddress).Return(e2tInstance, nil)
98 e2tInstanceToSave := *e2tInstance
99 e2tInstanceToSave.AssociatedRanList = []string{}
100 writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
101 mockHttpClient(httpClientMock, true)
102 notificationRequest := models.NotificationRequest{RanName: ranName}
103 handler.Handle(¬ificationRequest)
104 readerMock.AssertExpectations(t)
105 writerMock.AssertExpectations(t)
106 httpClientMock.AssertExpectations(t)
107 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
110 func TestLostConnectionHandlerConnectedRanSuccess(t *testing.T) {
111 handler, readerMock, writerMock, httpClientMock := setupLostConnectionHandlerTest(true)
113 origNodebInfo := &entities.NodebInfo{
115 GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"},
116 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
117 AssociatedE2TInstanceAddress: e2tAddress,
120 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
121 updatedNodebInfo1 := *origNodebInfo
122 updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
123 writerMock.On("UpdateNodebInfoOnConnectionStatusInversion", &updatedNodebInfo1, ranName+"_DISCONNECTED").Return(rnibErr)
124 updatedNodebInfo2 := *origNodebInfo
125 updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
126 updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
127 writerMock.On("UpdateNodebInfo", &updatedNodebInfo2).Return(rnibErr)
128 e2tInstance := &entities.E2TInstance{Address: e2tAddress, AssociatedRanList: []string{ranName}}
129 readerMock.On("GetE2TInstance", e2tAddress).Return(e2tInstance, nil)
130 e2tInstanceToSave := *e2tInstance
131 e2tInstanceToSave.AssociatedRanList = []string{}
132 writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
133 mockHttpClient(httpClientMock, true)
134 notificationRequest := models.NotificationRequest{RanName: ranName}
135 handler.Handle(¬ificationRequest)
136 readerMock.AssertExpectations(t)
137 writerMock.AssertExpectations(t)
138 httpClientMock.AssertExpectations(t)
139 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
142 func TestLostConnectionHandlerRmDissociateFailure(t *testing.T) {
143 handler, readerMock, writerMock, httpClientMock := setupLostConnectionHandlerTest(false)
145 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, AssociatedE2TInstanceAddress: e2tAddress}
147 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
148 updatedNodebInfo1 := *origNodebInfo
149 updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
150 writerMock.On("UpdateNodebInfo", &updatedNodebInfo1).Return(rnibErr)
151 updatedNodebInfo2 := *origNodebInfo
152 updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
153 updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
154 writerMock.On("UpdateNodebInfo", &updatedNodebInfo2).Return(rnibErr)
155 e2tInstance := &entities.E2TInstance{Address: e2tAddress, AssociatedRanList: []string{ranName}}
156 readerMock.On("GetE2TInstance", e2tAddress).Return(e2tInstance, nil)
157 e2tInstanceToSave := *e2tInstance
158 e2tInstanceToSave.AssociatedRanList = []string{}
159 writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
160 mockHttpClient(httpClientMock, false)
161 notificationRequest := models.NotificationRequest{RanName: ranName}
162 handler.Handle(¬ificationRequest)
163 readerMock.AssertExpectations(t)
164 writerMock.AssertExpectations(t)
165 httpClientMock.AssertExpectations(t)
166 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)