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.
26 "e2mgr/services/rmrsender"
28 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
29 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
30 "github.com/pkg/errors"
31 "github.com/stretchr/testify/assert"
32 "github.com/stretchr/testify/mock"
36 func initRanLostConnectionTest(t *testing.T) (*logger.Logger, *mocks.RmrMessengerMock, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *RanReconnectionManager) {
37 logger, err := logger.InitLogger(logger.DebugLevel)
39 t.Errorf("#... - failed to initialize logger, error: %s", err)
41 config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3}
43 rmrMessengerMock := &mocks.RmrMessengerMock{}
44 rmrSender := initRmrSender(rmrMessengerMock, logger)
46 readerMock := &mocks.RnibReaderMock{}
48 writerMock := &mocks.RnibWriterMock{}
50 rnibDataService := services.NewRnibDataService(logger, config, readerMock, writerMock)
51 ranSetupManager := NewRanSetupManager(logger, rmrSender, rnibDataService)
52 ranReconnectionManager := NewRanReconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, ranSetupManager)
53 return logger, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager
56 func TestRanReconnectionGetNodebFailure(t *testing.T) {
57 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
59 var nodebInfo *entities.NodebInfo
60 readerMock.On("GetNodeb", ranName).Return(nodebInfo, common.NewInternalError(errors.New("Error")))
61 err := ranReconnectionManager.ReconnectRan(ranName)
63 readerMock.AssertCalled(t, "GetNodeb", ranName)
64 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
67 func TestShutdownRanReconnection(t *testing.T) {
68 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
70 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}
72 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
73 err := ranReconnectionManager.ReconnectRan(ranName)
75 readerMock.AssertCalled(t, "GetNodeb", ranName)
76 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
79 func TestShuttingdownRanReconnection(t *testing.T) {
80 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
82 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
84 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
85 updatedNodebInfo := *origNodebInfo
86 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
87 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
88 err := ranReconnectionManager.ReconnectRan(ranName)
90 readerMock.AssertCalled(t, "GetNodeb", ranName)
91 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
94 func TestConnectingRanWithMaxAttemptsReconnection(t *testing.T) {
95 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
97 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 20}
99 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
100 updatedNodebInfo := *origNodebInfo
101 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
102 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
103 err := ranReconnectionManager.ReconnectRan(ranName)
105 readerMock.AssertCalled(t, "GetNodeb", ranName)
106 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
109 func TestUnconnectableRanUpdateNodebInfoFailure(t *testing.T) {
110 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
112 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
114 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
115 updatedNodebInfo := *origNodebInfo
116 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
117 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
118 err := ranReconnectionManager.ReconnectRan(ranName)
119 assert.NotNil(t, err)
120 readerMock.AssertCalled(t, "GetNodeb", ranName)
121 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
124 func TestConnectedRanExecuteSetupSuccess(t *testing.T) {
125 _, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
127 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST}
129 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
130 updatedNodebInfo := *origNodebInfo
131 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
132 updatedNodebInfo.ConnectionAttempts++
133 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(nil)
134 rmrMessengerMock.On("SendMsg", mock.Anything).Return(&rmrCgo.MBuf{}, nil)
135 err := ranReconnectionManager.ReconnectRan(ranName)
137 readerMock.AssertCalled(t, "GetNodeb", ranName)
138 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
139 rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
142 func TestConnectedRanExecuteSetupFailure(t *testing.T) {
143 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
145 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
147 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
148 updatedNodebInfo := *origNodebInfo
149 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
150 updatedNodebInfo.ConnectionAttempts++
151 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
152 err := ranReconnectionManager.ReconnectRan(ranName)
153 assert.NotNil(t, err)
154 readerMock.AssertCalled(t, "GetNodeb", ranName)
155 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
158 func TestUnnecessaryUpdateNodebInfoStatus(t *testing.T) {
159 _, _, _, _, ranReconnectionManager := initRanLostConnectionTest(t)
160 nodebInfo := &entities.NodebInfo{RanName: "ranName", GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
161 err := ranReconnectionManager.updateNodebInfoStatus(nodebInfo, entities.ConnectionStatus_CONNECTED)
165 func TestNoSetConnectionStatus(t *testing.T) {
166 _, _, _, _, ranReconnectionManager := initRanLostConnectionTest(t)
167 nodebInfo := &entities.NodebInfo{RanName: "ranName", GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
168 err := ranReconnectionManager.setConnectionStatusOfUnconnectableRan(nodebInfo)
172 func initRmrSender(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *rmrsender.RmrSender {
173 rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
174 rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
175 return rmrsender.NewRmrSender(log, rmrMessenger)