7927d0c4a460fbba5bbb34141b7ebbb69ee5fc63
[ric-plt/e2mgr.git] / E2Manager / managers / ran_reconnection_manager_test.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 //
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
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16
17 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20
21 package managers
22
23 import (
24         "e2mgr/clients"
25         "e2mgr/configuration"
26         "e2mgr/logger"
27         "e2mgr/mocks"
28         "e2mgr/rmrCgo"
29         "e2mgr/services"
30         "e2mgr/services/rmrsender"
31         "e2mgr/tests"
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         "github.com/pkg/errors"
35         "github.com/stretchr/testify/assert"
36         "github.com/stretchr/testify/mock"
37         "testing"
38 )
39
40 const ranName = "test"
41 const e2tAddress = "10.10.2.15:9800"
42
43 func initRanLostConnectionTest(t *testing.T) (*logger.Logger, *mocks.RmrMessengerMock, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *RanReconnectionManager, *mocks.HttpClientMock) {
44         logger, err := logger.InitLogger(logger.DebugLevel)
45         if err != nil {
46                 t.Errorf("#... - failed to initialize logger, error: %s", err)
47         }
48         config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3}
49
50         rmrMessengerMock := &mocks.RmrMessengerMock{}
51         rmrSender := initRmrSender(rmrMessengerMock, logger)
52
53         readerMock := &mocks.RnibReaderMock{}
54
55         writerMock := &mocks.RnibWriterMock{}
56
57         rnibDataService := services.NewRnibDataService(logger, config, readerMock, writerMock)
58         e2tInstancesManager := NewE2TInstancesManager(rnibDataService, logger)
59         ranSetupManager := NewRanSetupManager(logger, rmrSender, rnibDataService)
60         httpClient := &mocks.HttpClientMock{}
61         routingManagerClient := clients.NewRoutingManagerClient(logger, config, httpClient)
62         e2tAssociationManager := NewE2TAssociationManager(logger, rnibDataService, e2tInstancesManager, routingManagerClient)
63         ranReconnectionManager := NewRanReconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, ranSetupManager, e2tAssociationManager)
64         return logger, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager, httpClient
65 }
66
67 func TestRanReconnectionGetNodebFailure(t *testing.T) {
68         _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
69
70         var nodebInfo *entities.NodebInfo
71         readerMock.On("GetNodeb", ranName).Return(nodebInfo, common.NewInternalError(errors.New("Error")))
72         err := ranReconnectionManager.ReconnectRan(ranName)
73         assert.NotNil(t, err)
74         readerMock.AssertCalled(t, "GetNodeb", ranName)
75         writerMock.AssertNotCalled(t, "UpdateNodebInfo")
76 }
77
78 func TestShutdownRanReconnection(t *testing.T) {
79         _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
80
81         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}
82         var rnibErr error
83         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
84         err := ranReconnectionManager.ReconnectRan(ranName)
85         assert.Nil(t, err)
86         readerMock.AssertCalled(t, "GetNodeb", ranName)
87         writerMock.AssertNotCalled(t, "UpdateNodebInfo")
88 }
89
90 func TestShuttingdownRanReconnection(t *testing.T) {
91         _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
92
93         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
94         var rnibErr error
95         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
96         updatedNodebInfo := *origNodebInfo
97         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
98         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
99         err := ranReconnectionManager.ReconnectRan(ranName)
100         assert.Nil(t, err)
101         readerMock.AssertCalled(t, "GetNodeb", ranName)
102         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
103 }
104
105 func TestConnectingRanWithMaxAttemptsReconnectionDissociateSucceeds(t *testing.T) {
106         _, _, readerMock, writerMock, ranReconnectionManager, httpClient:= initRanLostConnectionTest(t)
107
108         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 20, AssociatedE2TInstanceAddress: E2TAddress}
109         var rnibErr error
110         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
111         updatedNodebInfo1 := *origNodebInfo
112         updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
113         writerMock.On("UpdateNodebInfo", &updatedNodebInfo1).Return(rnibErr)
114         updatedNodebInfo2 := *origNodebInfo
115         updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
116         updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
117         writerMock.On("UpdateNodebInfo", &updatedNodebInfo2).Return(rnibErr)
118         e2tInstance := &entities.E2TInstance{Address: E2TAddress, AssociatedRanList:[]string{ranName}}
119         readerMock.On("GetE2TInstance", E2TAddress).Return(e2tInstance, nil)
120         e2tInstanceToSave := * e2tInstance
121         e2tInstanceToSave .AssociatedRanList = []string{}
122         writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
123         mockHttpClient(httpClient, clients.DissociateRanE2TInstanceApiSuffix, true)
124         err := ranReconnectionManager.ReconnectRan(ranName)
125         assert.Nil(t, err)
126         readerMock.AssertCalled(t, "GetNodeb", ranName)
127         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
128 }
129
130 func TestConnectingRanWithMaxAttemptsReconnectionDissociateFails(t *testing.T) {
131         _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
132
133         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 20, AssociatedE2TInstanceAddress: e2tAddress}
134         var rnibErr error
135         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
136         updatedNodebInfo1 := *origNodebInfo
137         updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
138         writerMock.On("UpdateNodebInfo", &updatedNodebInfo1).Return(rnibErr)
139         updatedNodebInfo2 := *origNodebInfo
140         updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
141         updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
142         writerMock.On("UpdateNodebInfo", &updatedNodebInfo2).Return(rnibErr)
143         e2tInstance := &entities.E2TInstance{Address:e2tAddress, AssociatedRanList:[]string{ranName}}
144         readerMock.On("GetE2TInstance",e2tAddress).Return(e2tInstance, common.NewInternalError(errors.New("Error")))
145         err := ranReconnectionManager.ReconnectRan(ranName)
146         assert.NotNil(t, err)
147         readerMock.AssertCalled(t, "GetNodeb", ranName)
148         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
149         writerMock.AssertNotCalled(t, "SaveE2TInstance", )
150 }
151
152 func TestUnconnectableRanUpdateNodebInfoFailure(t *testing.T) {
153         _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
154
155         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
156         var rnibErr error
157         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
158         updatedNodebInfo := *origNodebInfo
159         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
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)
165 }
166
167 func TestConnectedRanExecuteSetupSuccess(t *testing.T) {
168         _, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
169
170         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST}
171         var rnibErr error
172         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
173         updatedNodebInfo := *origNodebInfo
174         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
175         updatedNodebInfo.ConnectionAttempts++
176         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(nil)
177         rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(&rmrCgo.MBuf{}, nil)
178         err := ranReconnectionManager.ReconnectRan(ranName)
179         assert.Nil(t, err)
180         readerMock.AssertCalled(t, "GetNodeb", ranName)
181         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
182         rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
183 }
184
185 func TestConnectedRanExecuteSetupFailure(t *testing.T) {
186         _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
187
188         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
189         var rnibErr error
190         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
191         updatedNodebInfo := *origNodebInfo
192         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
193         updatedNodebInfo.ConnectionAttempts++
194         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
195         err := ranReconnectionManager.ReconnectRan(ranName)
196         assert.NotNil(t, err)
197         readerMock.AssertCalled(t, "GetNodeb", ranName)
198         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
199 }
200
201 func TestNoSetConnectionStatus(t *testing.T) {
202         _, _, _, _, ranReconnectionManager, _ := initRanLostConnectionTest(t)
203         nodebInfo := &entities.NodebInfo{RanName: "ranName", GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
204         err := ranReconnectionManager.updateUnconnectableRan(nodebInfo)
205         assert.Nil(t, err)
206 }
207
208 func initRmrSender(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *rmrsender.RmrSender {
209         rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
210         rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
211         return rmrsender.NewRmrSender(log, rmrMessenger)
212 }