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 updatedNodebInfo := *origNodebInfo
112 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
113 updatedNodebInfo.AssociatedE2TInstanceAddress = ""
114 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
115 e2tInstance := &entities.E2TInstance{Address: E2TAddress, AssociatedRanList:[]string{ranName}}
116 readerMock.On("GetE2TInstance", E2TAddress).Return(e2tInstance, nil)
117 e2tInstanceToSave := * e2tInstance
118 e2tInstanceToSave .AssociatedRanList = []string{}
119 writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
120 mockHttpClient(httpClient, clients.DissociateRanE2TInstanceApiSuffix, true)
121 err := ranReconnectionManager.ReconnectRan(ranName)
123 readerMock.AssertCalled(t, "GetNodeb", ranName)
124 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
127 func TestConnectingRanWithMaxAttemptsReconnectionDissociateFails(t *testing.T) {
128 _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
130 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 20, AssociatedE2TInstanceAddress: e2tAddress}
132 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
133 updatedNodebInfo := *origNodebInfo
134 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
135 updatedNodebInfo.AssociatedE2TInstanceAddress = ""
136 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
137 e2tInstance := &entities.E2TInstance{Address:e2tAddress, AssociatedRanList:[]string{ranName}}
138 readerMock.On("GetE2TInstance",e2tAddress).Return(e2tInstance, common.NewInternalError(errors.New("Error")))
139 err := ranReconnectionManager.ReconnectRan(ranName)
140 assert.NotNil(t, err)
141 readerMock.AssertCalled(t, "GetNodeb", ranName)
142 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
143 writerMock.AssertNotCalled(t, "SaveE2TInstance", )
146 func TestUnconnectableRanUpdateNodebInfoFailure(t *testing.T) {
147 _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
149 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
151 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
152 updatedNodebInfo := *origNodebInfo
153 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
154 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
155 err := ranReconnectionManager.ReconnectRan(ranName)
156 assert.NotNil(t, err)
157 readerMock.AssertCalled(t, "GetNodeb", ranName)
158 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
161 func TestConnectedRanExecuteSetupSuccess(t *testing.T) {
162 _, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
164 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST}
166 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
167 updatedNodebInfo := *origNodebInfo
168 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
169 updatedNodebInfo.ConnectionAttempts++
170 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(nil)
171 rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(&rmrCgo.MBuf{}, nil)
172 err := ranReconnectionManager.ReconnectRan(ranName)
174 readerMock.AssertCalled(t, "GetNodeb", ranName)
175 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
176 rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
179 func TestConnectedRanExecuteSetupFailure(t *testing.T) {
180 _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
182 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
184 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
185 updatedNodebInfo := *origNodebInfo
186 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
187 updatedNodebInfo.ConnectionAttempts++
188 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
189 err := ranReconnectionManager.ReconnectRan(ranName)
190 assert.NotNil(t, err)
191 readerMock.AssertCalled(t, "GetNodeb", ranName)
192 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
195 func TestNoSetConnectionStatus(t *testing.T) {
196 _, _, _, _, ranReconnectionManager, _ := initRanLostConnectionTest(t)
197 nodebInfo := &entities.NodebInfo{RanName: "ranName", GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
198 err := ranReconnectionManager.updateUnconnectableRan(nodebInfo)
202 func initRmrSender(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *rmrsender.RmrSender {
203 rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
204 rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
205 return rmrsender.NewRmrSender(log, rmrMessenger)