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