68c31cead75738142857df5b54b4f5f5350b5925
[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         "testing"
36 )
37
38 func initRanLostConnectionTest(t *testing.T) (*logger.Logger, *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
44         rmrService := getRmrService(&mocks.RmrMessengerMock{}, logger)
45
46         readerMock := &mocks.RnibReaderMock{}
47         rnibReaderProvider := func() reader.RNibReader {
48                 return readerMock
49         }
50         writerMock := &mocks.RnibWriterMock{}
51         rnibWriterProvider := func() rNibWriter.RNibWriter {
52                 return writerMock
53         }
54
55         ranReconnectionManager := NewRanReconnectionManager(logger, configuration.ParseConfiguration(), rnibReaderProvider, rnibWriterProvider, rmrService)
56         return logger, readerMock, writerMock, ranReconnectionManager
57 }
58
59 func TestLostConnectionFetchingNodebFailure(t *testing.T) {
60         _, readerMock, _, ranReconnectionManager := initRanLostConnectionTest(t)
61         ranName := "test"
62         var nodebInfo *entities.NodebInfo
63         readerMock.On("GetNodeb", ranName).Return(nodebInfo, common.NewInternalError(errors.New("Error")))
64         err := ranReconnectionManager.ReconnectRan(ranName)
65         assert.NotNil(t, err)
66 }
67
68 func TestLostConnectionUpdatingNodebForUnconnectableRanFailure(t *testing.T) {
69         _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
70         ranName := "test"
71         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
72         var rnibErr common.IRNibError
73         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
74         updatedNodebInfo := *origNodebInfo
75         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
76         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
77         err := ranReconnectionManager.ReconnectRan(ranName)
78         assert.NotNil(t, err)
79 }
80
81 func TestLostConnectionOfConnectedRanWithMaxAttempts(t *testing.T) {
82         _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
83         ranName := "test"
84         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED, ConnectionAttempts: 20}
85         var rnibErr common.IRNibError
86         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
87         updatedNodebInfo := *origNodebInfo
88         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
89         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
90         err := ranReconnectionManager.ReconnectRan(ranName)
91         assert.Nil(t, err)
92 }
93
94 func TestLostConnectionOfShutdownRan(t *testing.T) {
95         _, readerMock, _, ranReconnectionManager := initRanLostConnectionTest(t)
96         ranName := "test"
97         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}
98         var rnibErr common.IRNibError
99         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
100         err := ranReconnectionManager.ReconnectRan(ranName)
101         assert.Nil(t, err)
102 }
103
104 func TestLostConnectionOfShuttingdownRan(t *testing.T) {
105         _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
106         ranName := "test"
107         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
108         var rnibErr common.IRNibError
109         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
110         updatedNodebInfo := *origNodebInfo
111         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
112         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
113         err := ranReconnectionManager.ReconnectRan(ranName)
114         assert.Nil(t, err)
115 }
116
117 // TODO: should extract to test_utils
118 func getRmrService(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *services.RmrService {
119         rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
120         messageChannel := make(chan *models.NotificationResponse)
121         rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
122         return services.NewRmrService(services.NewRmrConfig(tests.Port, tests.MaxMsgSize, tests.Flags, log), rmrMessenger, make(sessions.E2Sessions), messageChannel)
123 }