[RICPLT-2048] X2 ENDC Setup request refactoring
[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
18 package managers
19
20 import (
21         "e2mgr/configuration"
22         "e2mgr/logger"
23         "e2mgr/mocks"
24         "e2mgr/models"
25         "e2mgr/rNibWriter"
26         "e2mgr/rmrCgo"
27         "e2mgr/services"
28         "e2mgr/sessions"
29         "e2mgr/tests"
30         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
31         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
32         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
33         "github.com/pkg/errors"
34         "github.com/stretchr/testify/assert"
35         "github.com/stretchr/testify/mock"
36         "testing"
37 )
38
39 func initRanLostConnectionTest(t *testing.T) (*logger.Logger,*mocks.RmrMessengerMock, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *RanReconnectionManager) {
40         logger, err := logger.InitLogger(logger.DebugLevel)
41         if err != nil {
42                 t.Errorf("#... - failed to initialize logger, error: %s", err)
43         }
44
45         rmrMessengerMock := &mocks.RmrMessengerMock{}
46         rmrService := getRmrService(rmrMessengerMock, logger)
47
48         readerMock := &mocks.RnibReaderMock{}
49         rnibReaderProvider := func() reader.RNibReader {
50                 return readerMock
51         }
52         writerMock := &mocks.RnibWriterMock{}
53         rnibWriterProvider := func() rNibWriter.RNibWriter {
54                 return writerMock
55         }
56         ranSetupManager := NewRanSetupManager(logger,rmrService, rnibWriterProvider)
57         ranReconnectionManager := NewRanReconnectionManager(logger, configuration.ParseConfiguration(), rnibReaderProvider, rnibWriterProvider, ranSetupManager)
58         return logger,rmrMessengerMock, readerMock, writerMock, ranReconnectionManager
59 }
60
61 func TestRanReconnectionGetNodebFailure(t *testing.T) {
62         _,_, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
63         ranName := "test"
64         var nodebInfo *entities.NodebInfo
65         readerMock.On("GetNodeb", ranName).Return(nodebInfo, common.NewInternalError(errors.New("Error")))
66         err := ranReconnectionManager.ReconnectRan(ranName)
67         assert.NotNil(t, err)
68         readerMock.AssertCalled(t, "GetNodeb", ranName)
69         writerMock.AssertNotCalled(t, "UpdateNodebInfo")
70 }
71
72 func TestShutdownRanReconnection(t *testing.T) {
73         _,_, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
74         ranName := "test"
75         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}
76         var rnibErr error
77         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
78         err := ranReconnectionManager.ReconnectRan(ranName)
79         assert.Nil(t, err)
80         readerMock.AssertCalled(t, "GetNodeb", ranName)
81         writerMock.AssertNotCalled(t, "UpdateNodebInfo")
82 }
83
84 func TestShuttingdownRanReconnection(t *testing.T) {
85         _,_, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
86         ranName := "test"
87         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
88         var rnibErr error
89         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
90         updatedNodebInfo := *origNodebInfo
91         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
92         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
93         err := ranReconnectionManager.ReconnectRan(ranName)
94         assert.Nil(t, err)
95         readerMock.AssertCalled(t, "GetNodeb", ranName)
96         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
97 }
98
99 func TestConnectingRanWithMaxAttemptsReconnection(t *testing.T) {
100         _,_, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
101         ranName := "test"
102         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 20}
103         var rnibErr error
104         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
105         updatedNodebInfo := *origNodebInfo
106         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
107         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
108         err := ranReconnectionManager.ReconnectRan(ranName)
109         assert.Nil(t, err)
110         readerMock.AssertCalled(t, "GetNodeb", ranName)
111         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
112 }
113
114 func TestUnconnectableRanUpdateNodebInfoFailure(t *testing.T) {
115         _,_, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
116         ranName := "test"
117         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
118         var rnibErr error
119         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
120         updatedNodebInfo := *origNodebInfo
121         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
122         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
123         err := ranReconnectionManager.ReconnectRan(ranName)
124         assert.NotNil(t, err)
125         readerMock.AssertCalled(t, "GetNodeb", ranName)
126         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
127 }
128
129 func TestConnectedRanExecuteSetupSuccess(t *testing.T) {
130         _,rmrMessengerMock, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
131         ranName := "test"
132         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST}
133         var rnibErr error
134         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
135         updatedNodebInfo := *origNodebInfo
136         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
137         updatedNodebInfo.ConnectionAttempts++
138         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(nil)
139         rmrMessengerMock.On("SendMsg",mock.Anything, mock.AnythingOfType("int")).Return(&rmrCgo.MBuf{},nil)
140         err := ranReconnectionManager.ReconnectRan(ranName)
141         assert.Nil(t, err)
142         readerMock.AssertCalled(t, "GetNodeb", ranName)
143         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
144         rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
145 }
146
147 func TestConnectedRanExecuteSetupFailure(t *testing.T) {
148         _,_, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
149         ranName := "test"
150         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
151         var rnibErr error
152         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
153         updatedNodebInfo := *origNodebInfo
154         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
155         updatedNodebInfo.ConnectionAttempts++
156         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
157         err := ranReconnectionManager.ReconnectRan(ranName)
158         assert.NotNil(t, err)
159         readerMock.AssertCalled(t, "GetNodeb", ranName)
160         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
161 }
162
163 // TODO: should extract to test_utils
164 func getRmrService(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *services.RmrService {
165         rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
166         messageChannel := make(chan *models.NotificationResponse)
167         rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
168         return services.NewRmrService(services.NewRmrConfig(tests.Port, tests.MaxMsgSize, tests.Flags, log), rmrMessenger, make(sessions.E2Sessions), messageChannel)
169 }