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).
29 "e2mgr/services/rmrsender"
33 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
34 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
35 "github.com/pkg/errors"
36 "github.com/stretchr/testify/assert"
37 "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, *RanDisconnectionManager, *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 readerMock := &mocks.RnibReaderMock{}
52 writerMock := &mocks.RnibWriterMock{}
53 rnibDataService := services.NewRnibDataService(logger, config, readerMock, writerMock)
54 e2tInstancesManager := NewE2TInstancesManager(rnibDataService, logger)
55 httpClient := &mocks.HttpClientMock{}
56 routingManagerClient := clients.NewRoutingManagerClient(logger, config, httpClient)
57 ranListManager := NewRanListManager(logger, rnibDataService)
58 ranAlarmService := services.NewRanAlarmService(logger, config)
59 ranConnectStatusChangeManager := NewRanConnectStatusChangeManager(logger, rnibDataService, ranListManager, ranAlarmService)
60 e2tAssociationManager := NewE2TAssociationManager(logger, rnibDataService, e2tInstancesManager, routingManagerClient, ranConnectStatusChangeManager)
61 ranDisconnectionManager := NewRanDisconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, e2tAssociationManager, ranConnectStatusChangeManager)
62 return logger, rmrMessengerMock, readerMock, writerMock, ranDisconnectionManager, httpClient
65 func TestRanDisconnectionGetNodebFailure(t *testing.T) {
66 _, _, readerMock, writerMock, ranDisconnectionManager, _ := initRanLostConnectionTest(t)
68 var nodebInfo *entities.NodebInfo
69 readerMock.On("GetNodeb", ranName).Return(nodebInfo, common.NewInternalError(errors.New("Error")))
70 err := ranDisconnectionManager.DisconnectRan(ranName)
72 readerMock.AssertCalled(t, "GetNodeb", ranName)
73 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
76 func TestShutdownRan(t *testing.T) {
77 _, _, readerMock, writerMock, ranDisconnectionManager, _ := 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 := ranDisconnectionManager.DisconnectRan(ranName)
84 readerMock.AssertCalled(t, "GetNodeb", ranName)
85 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
88 func TestShuttingdownRan(t *testing.T) {
89 _, _, readerMock, writerMock, ranDisconnectionManager, _ := 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", mock.Anything).Return(rnibErr)
97 err := ranDisconnectionManager.DisconnectRan(ranName)
99 readerMock.AssertCalled(t, "GetNodeb", ranName)
100 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
103 func TestShuttingDownRanUpdateNodebInfoFailure(t *testing.T) {
104 _, _, readerMock, writerMock, ranDisconnectionManager, _ := initRanLostConnectionTest(t)
106 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
108 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
109 updatedNodebInfo := *origNodebInfo
110 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
111 writerMock.On("UpdateNodebInfo", mock.Anything).Return(common.NewInternalError(errors.New("Error")))
112 err := ranDisconnectionManager.DisconnectRan(ranName)
113 assert.NotNil(t, err)
114 readerMock.AssertCalled(t, "GetNodeb", ranName)
115 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
118 func TestConnectingRanUpdateNodebInfoFailure(t *testing.T) {
119 _, _, readerMock, writerMock, ranDisconnectionManager, _ := initRanLostConnectionTest(t)
121 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING}
123 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
124 updatedNodebInfo := *origNodebInfo
125 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
126 writerMock.On("UpdateNodebInfo", mock.Anything).Return(common.NewInternalError(errors.New("Error")))
127 err := ranDisconnectionManager.DisconnectRan(ranName)
128 assert.NotNil(t, err)
129 readerMock.AssertCalled(t, "GetNodeb", ranName)
130 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
133 func TestConnectingRanDisconnectSucceeds(t *testing.T) {
134 _, _, readerMock, writerMock, ranDisconnectionManager, httpClient := initRanLostConnectionTest(t)
136 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, AssociatedE2TInstanceAddress: E2TAddress}
138 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
139 updatedNodebInfo1 := *origNodebInfo
140 updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
141 writerMock.On("UpdateNodebInfo", mock.Anything).Return(rnibErr)
142 updatedNodebInfo2 := *origNodebInfo
143 updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
144 updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
145 writerMock.On("UpdateNodebInfo", mock.Anything).Return(rnibErr)
146 e2tInstance := &entities.E2TInstance{Address: E2TAddress, AssociatedRanList: []string{ranName}}
147 readerMock.On("GetE2TInstance", E2TAddress).Return(e2tInstance, nil)
148 e2tInstanceToSave := *e2tInstance
149 e2tInstanceToSave.AssociatedRanList = []string{}
150 writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
151 mockHttpClient(httpClient, clients.DissociateRanE2TInstanceApiSuffix, true)
152 err := ranDisconnectionManager.DisconnectRan(ranName)
154 readerMock.AssertCalled(t, "GetNodeb", ranName)
155 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
158 func TestConnectingRanDissociateFailsRmError(t *testing.T) {
159 _, _, readerMock, writerMock, ranDisconnectionManager, httpClient := initRanLostConnectionTest(t)
161 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, AssociatedE2TInstanceAddress: E2TAddress}
163 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
164 updatedNodebInfo1 := *origNodebInfo
165 updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
166 writerMock.On("UpdateNodebInfo", mock.Anything).Return(rnibErr)
167 updatedNodebInfo2 := *origNodebInfo
168 updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
169 updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
170 writerMock.On("UpdateNodebInfo", mock.Anything).Return(rnibErr)
171 e2tInstance := &entities.E2TInstance{Address: E2TAddress, AssociatedRanList: []string{ranName}}
172 readerMock.On("GetE2TInstance", E2TAddress).Return(e2tInstance, nil)
173 e2tInstanceToSave := *e2tInstance
174 e2tInstanceToSave.AssociatedRanList = []string{}
175 writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
176 mockHttpClient(httpClient, clients.DissociateRanE2TInstanceApiSuffix, false)
177 err := ranDisconnectionManager.DisconnectRan(ranName)
179 readerMock.AssertCalled(t, "GetNodeb", ranName)
180 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
183 func TestConnectingRanDissociateFailsDbError(t *testing.T) {
184 _, _, readerMock, writerMock, ranDisconnectionManager, _ := initRanLostConnectionTest(t)
186 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, AssociatedE2TInstanceAddress: e2tAddress}
188 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
189 updatedNodebInfo1 := *origNodebInfo
190 updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
191 writerMock.On("UpdateNodebInfo", mock.Anything).Return(rnibErr)
192 updatedNodebInfo2 := *origNodebInfo
193 updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
194 updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
195 writerMock.On("UpdateNodebInfo", mock.Anything).Return(rnibErr)
196 e2tInstance := &entities.E2TInstance{Address: e2tAddress, AssociatedRanList: []string{ranName}}
197 readerMock.On("GetE2TInstance", e2tAddress).Return(e2tInstance, common.NewInternalError(errors.New("Error")))
198 err := ranDisconnectionManager.DisconnectRan(ranName)
199 assert.NotNil(t, err)
200 readerMock.AssertCalled(t, "GetNodeb", ranName)
201 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
202 writerMock.AssertNotCalled(t, "SaveE2TInstance")
205 func initRmrSender(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *rmrsender.RmrSender {
206 rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
207 rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
208 return rmrsender.NewRmrSender(log, rmrMessenger)