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 e2tAssociationManager := NewE2TAssociationManager(logger, rnibDataService, e2tInstancesManager, routingManagerClient)
56 ranReconnectionManager := NewRanDisconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, e2tAssociationManager)
57 return logger, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager, httpClient
60 func TestRanReconnectionGetNodebFailure(t *testing.T) {
61 _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
63 var nodebInfo *entities.NodebInfo
64 readerMock.On("GetNodeb", ranName).Return(nodebInfo, common.NewInternalError(errors.New("Error")))
65 err := ranReconnectionManager.DisconnectRan(ranName)
67 readerMock.AssertCalled(t, "GetNodeb", ranName)
68 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
71 func TestShutdownRanReconnection(t *testing.T) {
72 _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
74 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}
76 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
77 err := ranReconnectionManager.DisconnectRan(ranName)
79 readerMock.AssertCalled(t, "GetNodeb", ranName)
80 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
83 func TestShuttingdownRanReconnection(t *testing.T) {
84 _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
86 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
88 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
89 updatedNodebInfo := *origNodebInfo
90 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
91 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
92 err := ranReconnectionManager.DisconnectRan(ranName)
94 readerMock.AssertCalled(t, "GetNodeb", ranName)
95 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
98 func TestConnectingRanDissociateSucceeds(t *testing.T) {
99 _, _, readerMock, writerMock, ranReconnectionManager, httpClient := initRanLostConnectionTest(t)
101 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, AssociatedE2TInstanceAddress: E2TAddress}
103 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
104 updatedNodebInfo1 := *origNodebInfo
105 updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
106 writerMock.On("UpdateNodebInfo", &updatedNodebInfo1).Return(rnibErr)
107 updatedNodebInfo2 := *origNodebInfo
108 updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
109 updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
110 writerMock.On("UpdateNodebInfo", &updatedNodebInfo2).Return(rnibErr)
111 e2tInstance := &entities.E2TInstance{Address: E2TAddress, AssociatedRanList: []string{ranName}}
112 readerMock.On("GetE2TInstance", E2TAddress).Return(e2tInstance, nil)
113 e2tInstanceToSave := * e2tInstance
114 e2tInstanceToSave.AssociatedRanList = []string{}
115 writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
116 mockHttpClient(httpClient, clients.DissociateRanE2TInstanceApiSuffix, true)
117 err := ranReconnectionManager.DisconnectRan(ranName)
119 readerMock.AssertCalled(t, "GetNodeb", ranName)
120 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
123 func TestConnectingRanDissociateFails(t *testing.T) {
124 _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
126 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, AssociatedE2TInstanceAddress: e2tAddress}
128 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
129 updatedNodebInfo1 := *origNodebInfo
130 updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
131 writerMock.On("UpdateNodebInfo", &updatedNodebInfo1).Return(rnibErr)
132 updatedNodebInfo2 := *origNodebInfo
133 updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
134 updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
135 writerMock.On("UpdateNodebInfo", &updatedNodebInfo2).Return(rnibErr)
136 e2tInstance := &entities.E2TInstance{Address: e2tAddress, AssociatedRanList: []string{ranName}}
137 readerMock.On("GetE2TInstance", e2tAddress).Return(e2tInstance, common.NewInternalError(errors.New("Error")))
138 err := ranReconnectionManager.DisconnectRan(ranName)
139 assert.NotNil(t, err)
140 readerMock.AssertCalled(t, "GetNodeb", ranName)
141 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
142 writerMock.AssertNotCalled(t, "SaveE2TInstance", )
145 // TODO: change test function name
146 func TestUnconnectableRanUpdateNodebInfoFailure(t *testing.T) {
147 _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
149 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
151 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
152 updatedNodebInfo := *origNodebInfo
153 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
154 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
155 err := ranReconnectionManager.DisconnectRan(ranName)
156 assert.NotNil(t, err)
157 readerMock.AssertCalled(t, "GetNodeb", ranName)
158 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
161 func initRmrSender(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *rmrsender.RmrSender {
162 rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
163 rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
164 return rmrsender.NewRmrSender(log, rmrMessenger)