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