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).
30 "e2mgr/services/rmrsender"
32 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
33 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
34 "github.com/pkg/errors"
35 "github.com/stretchr/testify/assert"
36 "github.com/stretchr/testify/mock"
40 const ranName = "test"
41 const e2tAddress = "10.10.2.15:9800"
43 func initRanLostConnectionTest(t *testing.T) (*logger.Logger, *mocks.RmrMessengerMock, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *RanReconnectionManager, *mocks.HttpClientMock) {
44 logger, err := logger.InitLogger(logger.DebugLevel)
46 t.Errorf("#... - failed to initialize logger, error: %s", err)
48 config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3}
50 rmrMessengerMock := &mocks.RmrMessengerMock{}
51 rmrSender := initRmrSender(rmrMessengerMock, logger)
53 readerMock := &mocks.RnibReaderMock{}
55 writerMock := &mocks.RnibWriterMock{}
57 rnibDataService := services.NewRnibDataService(logger, config, readerMock, writerMock)
58 e2tInstancesManager := NewE2TInstancesManager(rnibDataService, logger)
59 ranSetupManager := NewRanSetupManager(logger, rmrSender, rnibDataService)
60 httpClient := &mocks.HttpClientMock{}
61 routingManagerClient := clients.NewRoutingManagerClient(logger, config, httpClient)
62 e2tAssociationManager := NewE2TAssociationManager(logger, rnibDataService, e2tInstancesManager, routingManagerClient)
63 ranReconnectionManager := NewRanReconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, ranSetupManager, e2tAssociationManager)
64 return logger, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager, httpClient
67 func TestRanReconnectionGetNodebFailure(t *testing.T) {
68 _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
70 var nodebInfo *entities.NodebInfo
71 readerMock.On("GetNodeb", ranName).Return(nodebInfo, common.NewInternalError(errors.New("Error")))
72 err := ranReconnectionManager.ReconnectRan(ranName)
74 readerMock.AssertCalled(t, "GetNodeb", ranName)
75 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
78 func TestShutdownRanReconnection(t *testing.T) {
79 _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
81 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}
83 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
84 err := ranReconnectionManager.ReconnectRan(ranName)
86 readerMock.AssertCalled(t, "GetNodeb", ranName)
87 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
90 func TestShuttingdownRanReconnection(t *testing.T) {
91 _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
93 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
95 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
96 updatedNodebInfo := *origNodebInfo
97 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
98 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
99 err := ranReconnectionManager.ReconnectRan(ranName)
101 readerMock.AssertCalled(t, "GetNodeb", ranName)
102 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
105 func TestConnectingRanWithMaxAttemptsReconnectionDissociateSucceeds(t *testing.T) {
106 _, _, readerMock, writerMock, ranReconnectionManager, httpClient:= initRanLostConnectionTest(t)
108 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 20, AssociatedE2TInstanceAddress: E2TAddress}
110 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
111 updatedNodebInfo1 := *origNodebInfo
112 updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
113 writerMock.On("UpdateNodebInfo", &updatedNodebInfo1).Return(rnibErr)
114 updatedNodebInfo2 := *origNodebInfo
115 updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
116 updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
117 writerMock.On("UpdateNodebInfo", &updatedNodebInfo2).Return(rnibErr)
118 e2tInstance := &entities.E2TInstance{Address: E2TAddress, AssociatedRanList:[]string{ranName}}
119 readerMock.On("GetE2TInstance", E2TAddress).Return(e2tInstance, nil)
120 e2tInstanceToSave := * e2tInstance
121 e2tInstanceToSave .AssociatedRanList = []string{}
122 writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
123 mockHttpClient(httpClient, clients.DissociateRanE2TInstanceApiSuffix, true)
124 err := ranReconnectionManager.ReconnectRan(ranName)
126 readerMock.AssertCalled(t, "GetNodeb", ranName)
127 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
130 func TestConnectingRanWithMaxAttemptsReconnectionDissociateFails(t *testing.T) {
131 _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
133 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 20, AssociatedE2TInstanceAddress: e2tAddress}
135 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
136 updatedNodebInfo1 := *origNodebInfo
137 updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
138 writerMock.On("UpdateNodebInfo", &updatedNodebInfo1).Return(rnibErr)
139 updatedNodebInfo2 := *origNodebInfo
140 updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
141 updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
142 writerMock.On("UpdateNodebInfo", &updatedNodebInfo2).Return(rnibErr)
143 e2tInstance := &entities.E2TInstance{Address:e2tAddress, AssociatedRanList:[]string{ranName}}
144 readerMock.On("GetE2TInstance",e2tAddress).Return(e2tInstance, common.NewInternalError(errors.New("Error")))
145 err := ranReconnectionManager.ReconnectRan(ranName)
146 assert.NotNil(t, err)
147 readerMock.AssertCalled(t, "GetNodeb", ranName)
148 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
149 writerMock.AssertNotCalled(t, "SaveE2TInstance", )
152 func TestUnconnectableRanUpdateNodebInfoFailure(t *testing.T) {
153 _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
155 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
157 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
158 updatedNodebInfo := *origNodebInfo
159 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
160 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
161 err := ranReconnectionManager.ReconnectRan(ranName)
162 assert.NotNil(t, err)
163 readerMock.AssertCalled(t, "GetNodeb", ranName)
164 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
167 func TestConnectedRanExecuteSetupSuccess(t *testing.T) {
168 _, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
170 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST}
172 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
173 updatedNodebInfo := *origNodebInfo
174 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
175 updatedNodebInfo.ConnectionAttempts++
176 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(nil)
177 rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(&rmrCgo.MBuf{}, nil)
178 err := ranReconnectionManager.ReconnectRan(ranName)
180 readerMock.AssertCalled(t, "GetNodeb", ranName)
181 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
182 rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
185 func TestConnectedRanExecuteSetupFailure(t *testing.T) {
186 _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
188 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
190 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
191 updatedNodebInfo := *origNodebInfo
192 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
193 updatedNodebInfo.ConnectionAttempts++
194 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
195 err := ranReconnectionManager.ReconnectRan(ranName)
196 assert.NotNil(t, err)
197 readerMock.AssertCalled(t, "GetNodeb", ranName)
198 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
201 func TestNoSetConnectionStatus(t *testing.T) {
202 _, _, _, _, ranReconnectionManager, _ := initRanLostConnectionTest(t)
203 nodebInfo := &entities.NodebInfo{RanName: "ranName", GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
204 err := ranReconnectionManager.updateUnconnectableRan(nodebInfo)
208 func initRmrSender(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *rmrsender.RmrSender {
209 rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
210 rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
211 return rmrsender.NewRmrSender(log, rmrMessenger)