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 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
35 "github.com/pkg/errors"
36 "github.com/stretchr/testify/assert"
37 "github.com/stretchr/testify/mock"
41 func initRanLostConnectionTest(t *testing.T) (*logger.Logger, *mocks.RmrMessengerMock, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *RanReconnectionManager) {
42 logger, err := logger.InitLogger(logger.DebugLevel)
44 t.Errorf("#... - failed to initialize logger, error: %s", err)
46 config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3}
48 rmrMessengerMock := &mocks.RmrMessengerMock{}
49 rmrSender := initRmrSender(rmrMessengerMock, logger)
51 readerMock := &mocks.RnibReaderMock{}
52 rnibReaderProvider := func() reader.RNibReader {
55 writerMock := &mocks.RnibWriterMock{}
56 rnibWriterProvider := func() rNibWriter.RNibWriter {
59 rnibDataService := services.NewRnibDataService(logger, config, rnibReaderProvider, rnibWriterProvider)
60 ranSetupManager := NewRanSetupManager(logger, rmrSender, rnibDataService)
61 ranReconnectionManager := NewRanReconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, ranSetupManager)
62 return logger, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager
65 func TestRanReconnectionGetNodebFailure(t *testing.T) {
66 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
68 var nodebInfo *entities.NodebInfo
69 readerMock.On("GetNodeb", ranName).Return(nodebInfo, common.NewInternalError(errors.New("Error")))
70 err := ranReconnectionManager.ReconnectRan(ranName)
72 readerMock.AssertCalled(t, "GetNodeb", ranName)
73 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
76 func TestShutdownRanReconnection(t *testing.T) {
77 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
79 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}
81 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
82 err := ranReconnectionManager.ReconnectRan(ranName)
84 readerMock.AssertCalled(t, "GetNodeb", ranName)
85 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
88 func TestShuttingdownRanReconnection(t *testing.T) {
89 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
91 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
93 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
94 updatedNodebInfo := *origNodebInfo
95 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
96 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
97 err := ranReconnectionManager.ReconnectRan(ranName)
99 readerMock.AssertCalled(t, "GetNodeb", ranName)
100 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
103 func TestConnectingRanWithMaxAttemptsReconnection(t *testing.T) {
104 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
106 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 20}
108 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
109 updatedNodebInfo := *origNodebInfo
110 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
111 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
112 err := ranReconnectionManager.ReconnectRan(ranName)
114 readerMock.AssertCalled(t, "GetNodeb", ranName)
115 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
118 func TestUnconnectableRanUpdateNodebInfoFailure(t *testing.T) {
119 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
121 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
123 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
124 updatedNodebInfo := *origNodebInfo
125 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
126 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
127 err := ranReconnectionManager.ReconnectRan(ranName)
128 assert.NotNil(t, err)
129 readerMock.AssertCalled(t, "GetNodeb", ranName)
130 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
133 func TestConnectedRanExecuteSetupSuccess(t *testing.T) {
134 _, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
136 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST}
138 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
139 updatedNodebInfo := *origNodebInfo
140 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
141 updatedNodebInfo.ConnectionAttempts++
142 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(nil)
143 rmrMessengerMock.On("SendMsg", mock.Anything).Return(&rmrCgo.MBuf{}, nil)
144 err := ranReconnectionManager.ReconnectRan(ranName)
146 readerMock.AssertCalled(t, "GetNodeb", ranName)
147 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
148 rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
151 func TestConnectedRanExecuteSetupFailure(t *testing.T) {
152 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
154 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
156 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
157 updatedNodebInfo := *origNodebInfo
158 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
159 updatedNodebInfo.ConnectionAttempts++
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 initRmrSender(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *rmrsender.RmrSender {
168 rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
169 rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
170 return rmrsender.NewRmrSender(log, &rmrMessenger)