Adding new comments for Oran in all files with licenses
[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/configuration"
25         "e2mgr/logger"
26         "e2mgr/mocks"
27         "e2mgr/rNibWriter"
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         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
35         "github.com/pkg/errors"
36         "github.com/stretchr/testify/assert"
37         "github.com/stretchr/testify/mock"
38         "testing"
39 )
40
41 func initRanLostConnectionTest(t *testing.T) (*logger.Logger, *mocks.RmrMessengerMock, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *RanReconnectionManager) {
42         logger, err := logger.InitLogger(logger.DebugLevel)
43         if err != nil {
44                 t.Errorf("#... - failed to initialize logger, error: %s", err)
45         }
46         config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3}
47
48         rmrMessengerMock := &mocks.RmrMessengerMock{}
49         rmrSender := initRmrSender(rmrMessengerMock, logger)
50
51         readerMock := &mocks.RnibReaderMock{}
52         rnibReaderProvider := func() reader.RNibReader {
53                 return readerMock
54         }
55         writerMock := &mocks.RnibWriterMock{}
56         rnibWriterProvider := func() rNibWriter.RNibWriter {
57                 return writerMock
58         }
59         rnibDataService := services.NewRnibDataService(logger, config, rnibReaderProvider, rnibWriterProvider)
60         ranSetupManager := NewRanSetupManager(logger, rmrSender, rnibDataService)
61         ranReconnectionManager := NewRanReconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, ranSetupManager)
62         return logger, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager
63 }
64
65 func TestRanReconnectionGetNodebFailure(t *testing.T) {
66         _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
67         ranName := "test"
68         var nodebInfo *entities.NodebInfo
69         readerMock.On("GetNodeb", ranName).Return(nodebInfo, common.NewInternalError(errors.New("Error")))
70         err := ranReconnectionManager.ReconnectRan(ranName)
71         assert.NotNil(t, err)
72         readerMock.AssertCalled(t, "GetNodeb", ranName)
73         writerMock.AssertNotCalled(t, "UpdateNodebInfo")
74 }
75
76 func TestShutdownRanReconnection(t *testing.T) {
77         _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
78         ranName := "test"
79         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}
80         var rnibErr error
81         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
82         err := ranReconnectionManager.ReconnectRan(ranName)
83         assert.Nil(t, err)
84         readerMock.AssertCalled(t, "GetNodeb", ranName)
85         writerMock.AssertNotCalled(t, "UpdateNodebInfo")
86 }
87
88 func TestShuttingdownRanReconnection(t *testing.T) {
89         _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
90         ranName := "test"
91         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
92         var rnibErr error
93         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
94         updatedNodebInfo := *origNodebInfo
95         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
96         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
97         err := ranReconnectionManager.ReconnectRan(ranName)
98         assert.Nil(t, err)
99         readerMock.AssertCalled(t, "GetNodeb", ranName)
100         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
101 }
102
103 func TestConnectingRanWithMaxAttemptsReconnection(t *testing.T) {
104         _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
105         ranName := "test"
106         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 20}
107         var rnibErr error
108         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
109         updatedNodebInfo := *origNodebInfo
110         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
111         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
112         err := ranReconnectionManager.ReconnectRan(ranName)
113         assert.Nil(t, err)
114         readerMock.AssertCalled(t, "GetNodeb", ranName)
115         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
116 }
117
118 func TestUnconnectableRanUpdateNodebInfoFailure(t *testing.T) {
119         _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
120         ranName := "test"
121         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
122         var rnibErr error
123         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
124         updatedNodebInfo := *origNodebInfo
125         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
126         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
127         err := ranReconnectionManager.ReconnectRan(ranName)
128         assert.NotNil(t, err)
129         readerMock.AssertCalled(t, "GetNodeb", ranName)
130         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
131 }
132
133 func TestConnectedRanExecuteSetupSuccess(t *testing.T) {
134         _, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
135         ranName := "test"
136         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST}
137         var rnibErr error
138         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
139         updatedNodebInfo := *origNodebInfo
140         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
141         updatedNodebInfo.ConnectionAttempts++
142         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(nil)
143         rmrMessengerMock.On("SendMsg", mock.Anything).Return(&rmrCgo.MBuf{}, nil)
144         err := ranReconnectionManager.ReconnectRan(ranName)
145         assert.Nil(t, err)
146         readerMock.AssertCalled(t, "GetNodeb", ranName)
147         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
148         rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
149 }
150
151 func TestConnectedRanExecuteSetupFailure(t *testing.T) {
152         _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
153         ranName := "test"
154         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
155         var rnibErr error
156         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
157         updatedNodebInfo := *origNodebInfo
158         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
159         updatedNodebInfo.ConnectionAttempts++
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 initRmrSender(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *rmrsender.RmrSender {
168         rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
169         rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
170         return rmrsender.NewRmrSender(log, &rmrMessenger)
171 }