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"
31 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
32 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
33 "github.com/pkg/errors"
34 "github.com/stretchr/testify/assert"
38 const ranName = "test"
39 const e2tAddress = "10.10.2.15:9800"
41 func initRanLostConnectionTest(t *testing.T) (*logger.Logger, *mocks.RmrMessengerMock, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *RanDisconnectionManager, *mocks.HttpClientMock) {
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 readerMock := &mocks.RnibReaderMock{}
50 writerMock := &mocks.RnibWriterMock{}
51 rnibDataService := services.NewRnibDataService(logger, config, readerMock, writerMock)
52 e2tInstancesManager := NewE2TInstancesManager(rnibDataService, logger)
53 httpClient := &mocks.HttpClientMock{}
54 routingManagerClient := clients.NewRoutingManagerClient(logger, config, httpClient)
55 ranListManager := NewRanListManager(logger)
56 ranAlarmService := services.NewRanAlarmService(logger, config)
57 ranConnectStatusChangeManager := NewRanConnectStatusChangeManager(logger, rnibDataService,ranListManager, ranAlarmService)
58 e2tAssociationManager := NewE2TAssociationManager(logger, rnibDataService, e2tInstancesManager, routingManagerClient, ranConnectStatusChangeManager)
59 ranDisconnectionManager := NewRanDisconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, e2tAssociationManager)
60 return logger, rmrMessengerMock, readerMock, writerMock, ranDisconnectionManager, httpClient
63 func TestRanDisconnectionGetNodebFailure(t *testing.T) {
64 _, _, readerMock, writerMock, ranDisconnectionManager, _ := initRanLostConnectionTest(t)
66 var nodebInfo *entities.NodebInfo
67 readerMock.On("GetNodeb", ranName).Return(nodebInfo, common.NewInternalError(errors.New("Error")))
68 err := ranDisconnectionManager.DisconnectRan(ranName)
70 readerMock.AssertCalled(t, "GetNodeb", ranName)
71 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
74 func TestShutdownRan(t *testing.T) {
75 _, _, readerMock, writerMock, ranDisconnectionManager, _ := initRanLostConnectionTest(t)
77 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}
79 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
80 err := ranDisconnectionManager.DisconnectRan(ranName)
82 readerMock.AssertCalled(t, "GetNodeb", ranName)
83 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
86 func TestShuttingdownRan(t *testing.T) {
87 _, _, readerMock, writerMock, ranDisconnectionManager, _ := initRanLostConnectionTest(t)
89 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
91 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
92 updatedNodebInfo := *origNodebInfo
93 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
94 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
95 err := ranDisconnectionManager.DisconnectRan(ranName)
97 readerMock.AssertCalled(t, "GetNodeb", ranName)
98 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
101 func TestShuttingDownRanUpdateNodebInfoFailure(t *testing.T) {
102 _, _, readerMock, writerMock, ranDisconnectionManager, _ := initRanLostConnectionTest(t)
104 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
106 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
107 updatedNodebInfo := *origNodebInfo
108 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
109 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
110 err := ranDisconnectionManager.DisconnectRan(ranName)
111 assert.NotNil(t, err)
112 readerMock.AssertCalled(t, "GetNodeb", ranName)
113 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
116 func TestConnectingRanUpdateNodebInfoFailure(t *testing.T) {
117 _, _, readerMock, writerMock, ranDisconnectionManager, _ := initRanLostConnectionTest(t)
119 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING}
121 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
122 updatedNodebInfo := *origNodebInfo
123 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
124 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
125 err := ranDisconnectionManager.DisconnectRan(ranName)
126 assert.NotNil(t, err)
127 readerMock.AssertCalled(t, "GetNodeb", ranName)
128 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
131 func TestConnectingRanDisconnectSucceeds(t *testing.T) {
132 _, _, readerMock, writerMock, ranDisconnectionManager, httpClient := initRanLostConnectionTest(t)
134 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, AssociatedE2TInstanceAddress: E2TAddress}
136 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
137 updatedNodebInfo1 := *origNodebInfo
138 updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
139 writerMock.On("UpdateNodebInfo", &updatedNodebInfo1).Return(rnibErr)
140 updatedNodebInfo2 := *origNodebInfo
141 updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
142 updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
143 writerMock.On("UpdateNodebInfo", &updatedNodebInfo2).Return(rnibErr)
144 e2tInstance := &entities.E2TInstance{Address: E2TAddress, AssociatedRanList: []string{ranName}}
145 readerMock.On("GetE2TInstance", E2TAddress).Return(e2tInstance, nil)
146 e2tInstanceToSave := * e2tInstance
147 e2tInstanceToSave.AssociatedRanList = []string{}
148 writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
149 mockHttpClient(httpClient, clients.DissociateRanE2TInstanceApiSuffix, true)
150 err := ranDisconnectionManager.DisconnectRan(ranName)
152 readerMock.AssertCalled(t, "GetNodeb", ranName)
153 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
156 func TestConnectingRanDissociateFailsRmError(t *testing.T) {
157 _, _, readerMock, writerMock, ranDisconnectionManager, httpClient := initRanLostConnectionTest(t)
159 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, AssociatedE2TInstanceAddress: E2TAddress}
161 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
162 updatedNodebInfo1 := *origNodebInfo
163 updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
164 writerMock.On("UpdateNodebInfo", &updatedNodebInfo1).Return(rnibErr)
165 updatedNodebInfo2 := *origNodebInfo
166 updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
167 updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
168 writerMock.On("UpdateNodebInfo", &updatedNodebInfo2).Return(rnibErr)
169 e2tInstance := &entities.E2TInstance{Address: E2TAddress, AssociatedRanList: []string{ranName}}
170 readerMock.On("GetE2TInstance", E2TAddress).Return(e2tInstance, nil)
171 e2tInstanceToSave := * e2tInstance
172 e2tInstanceToSave.AssociatedRanList = []string{}
173 writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
174 mockHttpClient(httpClient, clients.DissociateRanE2TInstanceApiSuffix, false)
175 err := ranDisconnectionManager.DisconnectRan(ranName)
177 readerMock.AssertCalled(t, "GetNodeb", ranName)
178 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
181 func TestConnectingRanDissociateFailsDbError(t *testing.T) {
182 _, _, readerMock, writerMock, ranDisconnectionManager, _ := initRanLostConnectionTest(t)
184 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, AssociatedE2TInstanceAddress: e2tAddress}
186 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
187 updatedNodebInfo1 := *origNodebInfo
188 updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
189 writerMock.On("UpdateNodebInfo", &updatedNodebInfo1).Return(rnibErr)
190 updatedNodebInfo2 := *origNodebInfo
191 updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
192 updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
193 writerMock.On("UpdateNodebInfo", &updatedNodebInfo2).Return(rnibErr)
194 e2tInstance := &entities.E2TInstance{Address: e2tAddress, AssociatedRanList: []string{ranName}}
195 readerMock.On("GetE2TInstance", e2tAddress).Return(e2tInstance, common.NewInternalError(errors.New("Error")))
196 err := ranDisconnectionManager.DisconnectRan(ranName)
197 assert.NotNil(t, err)
198 readerMock.AssertCalled(t, "GetNodeb", ranName)
199 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
200 writerMock.AssertNotCalled(t, "SaveE2TInstance", )
203 func initRmrSender(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *rmrsender.RmrSender {
204 rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
205 rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
206 return rmrsender.NewRmrSender(log, rmrMessenger)