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.
27 "e2mgr/services/rmrsender"
29 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
30 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
31 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
32 "github.com/pkg/errors"
33 "github.com/stretchr/testify/assert"
34 "github.com/stretchr/testify/mock"
38 func initRanLostConnectionTest(t *testing.T) (*logger.Logger, *mocks.RmrMessengerMock, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *RanReconnectionManager) {
39 logger, err := logger.InitLogger(logger.DebugLevel)
41 t.Errorf("#... - failed to initialize logger, error: %s", err)
43 config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3}
45 rmrMessengerMock := &mocks.RmrMessengerMock{}
46 rmrSender := initRmrSender(rmrMessengerMock, logger)
48 readerMock := &mocks.RnibReaderMock{}
49 rnibReaderProvider := func() reader.RNibReader {
52 writerMock := &mocks.RnibWriterMock{}
53 rnibWriterProvider := func() rNibWriter.RNibWriter {
56 rnibDataService := services.NewRnibDataService(logger, config, rnibReaderProvider, rnibWriterProvider)
57 ranSetupManager := NewRanSetupManager(logger, rmrSender, rnibDataService)
58 ranReconnectionManager := NewRanReconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, ranSetupManager)
59 return logger, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager
62 func TestRanReconnectionGetNodebFailure(t *testing.T) {
63 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
65 var nodebInfo *entities.NodebInfo
66 readerMock.On("GetNodeb", ranName).Return(nodebInfo, common.NewInternalError(errors.New("Error")))
67 err := ranReconnectionManager.ReconnectRan(ranName)
69 readerMock.AssertCalled(t, "GetNodeb", ranName)
70 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
73 func TestShutdownRanReconnection(t *testing.T) {
74 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
76 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}
78 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
79 err := ranReconnectionManager.ReconnectRan(ranName)
81 readerMock.AssertCalled(t, "GetNodeb", ranName)
82 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
85 func TestShuttingdownRanReconnection(t *testing.T) {
86 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
88 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
90 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
91 updatedNodebInfo := *origNodebInfo
92 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
93 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
94 err := ranReconnectionManager.ReconnectRan(ranName)
96 readerMock.AssertCalled(t, "GetNodeb", ranName)
97 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
100 func TestConnectingRanWithMaxAttemptsReconnection(t *testing.T) {
101 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
103 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 20}
105 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
106 updatedNodebInfo := *origNodebInfo
107 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
108 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
109 err := ranReconnectionManager.ReconnectRan(ranName)
111 readerMock.AssertCalled(t, "GetNodeb", ranName)
112 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
115 func TestUnconnectableRanUpdateNodebInfoFailure(t *testing.T) {
116 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
118 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
120 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
121 updatedNodebInfo := *origNodebInfo
122 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
123 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
124 err := ranReconnectionManager.ReconnectRan(ranName)
125 assert.NotNil(t, err)
126 readerMock.AssertCalled(t, "GetNodeb", ranName)
127 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
130 func TestConnectedRanExecuteSetupSuccess(t *testing.T) {
131 _, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
133 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST}
135 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
136 updatedNodebInfo := *origNodebInfo
137 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
138 updatedNodebInfo.ConnectionAttempts++
139 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(nil)
140 rmrMessengerMock.On("SendMsg", mock.Anything).Return(&rmrCgo.MBuf{}, nil)
141 err := ranReconnectionManager.ReconnectRan(ranName)
143 readerMock.AssertCalled(t, "GetNodeb", ranName)
144 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
145 rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
148 func TestConnectedRanExecuteSetupFailure(t *testing.T) {
149 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
151 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
153 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
154 updatedNodebInfo := *origNodebInfo
155 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
156 updatedNodebInfo.ConnectionAttempts++
157 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
158 err := ranReconnectionManager.ReconnectRan(ranName)
159 assert.NotNil(t, err)
160 readerMock.AssertCalled(t, "GetNodeb", ranName)
161 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
164 func initRmrSender(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *rmrsender.RmrSender {
165 rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
166 rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
167 return rmrsender.NewRmrSender(log, &rmrMessenger)