[RICPLT-2527] Implementation of DissociateRan + UTs
[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 func initRanLostConnectionTest(t *testing.T) (*logger.Logger, *mocks.RmrMessengerMock, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *RanReconnectionManager, *mocks.HttpClientMock) {
41         logger, err := logger.InitLogger(logger.DebugLevel)
42         if err != nil {
43                 t.Errorf("#... - failed to initialize logger, error: %s", err)
44         }
45         config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3}
46
47         rmrMessengerMock := &mocks.RmrMessengerMock{}
48         rmrSender := initRmrSender(rmrMessengerMock, logger)
49
50         readerMock := &mocks.RnibReaderMock{}
51
52         writerMock := &mocks.RnibWriterMock{}
53
54         rnibDataService := services.NewRnibDataService(logger, config, readerMock, writerMock)
55         e2tInstancesManager := NewE2TInstancesManager(rnibDataService, logger)
56         ranSetupManager := NewRanSetupManager(logger, rmrSender, rnibDataService)
57         httpClient := &mocks.HttpClientMock{}
58         routingManagerClient := clients.NewRoutingManagerClient(logger, config, httpClient)
59         e2tAssociationManager := NewE2TAssociationManager(logger, rnibDataService, e2tInstancesManager, routingManagerClient)
60         ranReconnectionManager := NewRanReconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, ranSetupManager, e2tAssociationManager)
61         return logger, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager, httpClient
62 }
63
64 func TestRanReconnectionGetNodebFailure(t *testing.T) {
65         _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
66         ranName := "test"
67         var nodebInfo *entities.NodebInfo
68         readerMock.On("GetNodeb", ranName).Return(nodebInfo, common.NewInternalError(errors.New("Error")))
69         err := ranReconnectionManager.ReconnectRan(ranName)
70         assert.NotNil(t, err)
71         readerMock.AssertCalled(t, "GetNodeb", ranName)
72         writerMock.AssertNotCalled(t, "UpdateNodebInfo")
73 }
74
75 func TestShutdownRanReconnection(t *testing.T) {
76         _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
77         ranName := "test"
78         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}
79         var rnibErr error
80         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
81         err := ranReconnectionManager.ReconnectRan(ranName)
82         assert.Nil(t, err)
83         readerMock.AssertCalled(t, "GetNodeb", ranName)
84         writerMock.AssertNotCalled(t, "UpdateNodebInfo")
85 }
86
87 func TestShuttingdownRanReconnection(t *testing.T) {
88         _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
89         ranName := "test"
90         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
91         var rnibErr error
92         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
93         updatedNodebInfo := *origNodebInfo
94         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
95         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
96         err := ranReconnectionManager.ReconnectRan(ranName)
97         assert.Nil(t, err)
98         readerMock.AssertCalled(t, "GetNodeb", ranName)
99         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
100 }
101
102 func TestConnectingRanWithMaxAttemptsReconnectionDissociateSucceeds(t *testing.T) {
103         _, _, readerMock, writerMock, ranReconnectionManager, httpClient:= initRanLostConnectionTest(t)
104         ranName := "test"
105         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 20, AssociatedE2TInstanceAddress: E2TAddress}
106         var rnibErr error
107         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
108         updatedNodebInfo := *origNodebInfo
109         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
110         updatedNodebInfo.AssociatedE2TInstanceAddress = ""
111         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
112         e2tInstance := &entities.E2TInstance{Address: E2TAddress, AssociatedRanList:[]string{ranName}}
113         readerMock.On("GetE2TInstance", E2TAddress).Return(e2tInstance, nil)
114         e2tInstanceToSave := * e2tInstance
115         e2tInstanceToSave .AssociatedRanList = []string{}
116         writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
117         mockHttpClient(httpClient, clients.DissociateRanE2TInstanceApiSuffix, true)
118         err := ranReconnectionManager.ReconnectRan(ranName)
119         assert.Nil(t, err)
120         readerMock.AssertCalled(t, "GetNodeb", ranName)
121         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
122 }
123
124 func TestConnectingRanWithMaxAttemptsReconnectionDissociateFails(t *testing.T) {
125         _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
126         ranName := "test"
127         e2tAddress := "10.0.2.15"
128         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 20, AssociatedE2TInstanceAddress: e2tAddress}
129         var rnibErr error
130         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
131         updatedNodebInfo := *origNodebInfo
132         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
133         updatedNodebInfo.AssociatedE2TInstanceAddress = ""
134         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
135         e2tInstance := &entities.E2TInstance{Address:e2tAddress, AssociatedRanList:[]string{ranName}}
136         readerMock.On("GetE2TInstance",e2tAddress).Return(e2tInstance, common.NewInternalError(errors.New("Error")))
137         err := ranReconnectionManager.ReconnectRan(ranName)
138         assert.NotNil(t, err)
139         readerMock.AssertCalled(t, "GetNodeb", ranName)
140         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
141         writerMock.AssertNotCalled(t, "SaveE2TInstance", )
142 }
143
144 func TestUnconnectableRanUpdateNodebInfoFailure(t *testing.T) {
145         _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
146         ranName := "test"
147         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
148         var rnibErr error
149         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
150         updatedNodebInfo := *origNodebInfo
151         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
152         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
153         err := ranReconnectionManager.ReconnectRan(ranName)
154         assert.NotNil(t, err)
155         readerMock.AssertCalled(t, "GetNodeb", ranName)
156         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
157 }
158
159 func TestConnectedRanExecuteSetupSuccess(t *testing.T) {
160         _, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
161         ranName := "test"
162         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST}
163         var rnibErr error
164         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
165         updatedNodebInfo := *origNodebInfo
166         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
167         updatedNodebInfo.ConnectionAttempts++
168         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(nil)
169         rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(&rmrCgo.MBuf{}, nil)
170         err := ranReconnectionManager.ReconnectRan(ranName)
171         assert.Nil(t, err)
172         readerMock.AssertCalled(t, "GetNodeb", ranName)
173         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
174         rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
175 }
176
177 func TestConnectedRanExecuteSetupFailure(t *testing.T) {
178         _, _, readerMock, writerMock, ranReconnectionManager, _ := initRanLostConnectionTest(t)
179         ranName := "test"
180         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
181         var rnibErr error
182         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
183         updatedNodebInfo := *origNodebInfo
184         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
185         updatedNodebInfo.ConnectionAttempts++
186         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
187         err := ranReconnectionManager.ReconnectRan(ranName)
188         assert.NotNil(t, err)
189         readerMock.AssertCalled(t, "GetNodeb", ranName)
190         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
191 }
192
193 func TestNoSetConnectionStatus(t *testing.T) {
194         _, _, _, _, ranReconnectionManager, _ := initRanLostConnectionTest(t)
195         nodebInfo := &entities.NodebInfo{RanName: "ranName", GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
196         err := ranReconnectionManager.updateUnconnectableRan(nodebInfo)
197         assert.Nil(t, err)
198 }
199
200 func initRmrSender(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *rmrsender.RmrSender {
201         rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
202         rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
203         return rmrsender.NewRmrSender(log, rmrMessenger)
204 }