RIC916 new reconnect timer in E2 to reject new connect for x seconds
[ric-plt/e2mgr.git] / E2Manager / handlers / rmrmsghandlers / ran_lost_connection_handler_test.go
1 //// Copyright 2019 AT&T Intellectual Property
2 //// Copyright 2019 Nokia
3 ////
4 //// Licensed under the Apache License, Version 2.0 (the "License");
5 //// you may not use this file except in compliance with the License.
6 //// You may obtain a copy of the License at
7 ////
8 ////      http://www.apache.org/licenses/LICENSE-2.0
9 ////
10 //// Unless required by applicable law or agreed to in writing, software
11 //// distributed under the License is distributed on an "AS IS" BASIS,
12 //// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 //// See the License for the specific language governing permissions and
14 //// limitations under the License.
15
16 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 //  platform project (RICP).
18
19 package rmrmsghandlers
20
21 import (
22         "bytes"
23         "e2mgr/clients"
24         "e2mgr/configuration"
25         "e2mgr/logger"
26         "e2mgr/managers"
27         "e2mgr/mocks"
28         "e2mgr/models"
29         "e2mgr/services"
30         "encoding/json"
31         "io/ioutil"
32         "net/http"
33         "testing"
34
35         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
36         "github.com/stretchr/testify/mock"
37 )
38
39 const (
40         ranName    = "test"
41         e2tAddress = "10.10.2.15:9800"
42 )
43
44 func setupLostConnectionHandlerTest(isSuccessfulHttpPost bool) (*RanLostConnectionHandler, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *mocks.HttpClientMock) {
45         logger, _ := logger.InitLogger(logger.InfoLevel)
46         config := &configuration.Configuration{
47                 RnibRetryIntervalMs:       10,
48                 MaxRnibConnectionAttempts: 3,
49                 RnibWriter: configuration.RnibWriterConfig{
50                         StateChangeMessageChannel: StateChangeMessageChannel,
51                 },
52         }
53
54         readerMock := &mocks.RnibReaderMock{}
55         writerMock := &mocks.RnibWriterMock{}
56         rnibDataService := services.NewRnibDataService(logger, config, readerMock, writerMock)
57         e2tInstancesManager := managers.NewE2TInstancesManager(rnibDataService, logger)
58         httpClientMock := &mocks.HttpClientMock{}
59         routingManagerClient := clients.NewRoutingManagerClient(logger, config, httpClientMock)
60         ranListManager := managers.NewRanListManager(logger, rnibDataService)
61         ranAlarmService := services.NewRanAlarmService(logger, config)
62         ranConnectStatusChangeManager := managers.NewRanConnectStatusChangeManager(logger, rnibDataService, ranListManager, ranAlarmService)
63
64         e2tAssociationManager := managers.NewE2TAssociationManager(logger, rnibDataService, e2tInstancesManager, routingManagerClient, ranConnectStatusChangeManager)
65         ranDisconnectionManager := managers.NewRanDisconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, e2tAssociationManager, ranConnectStatusChangeManager)
66         handler := NewRanLostConnectionHandler(logger, ranDisconnectionManager)
67
68         return handler, readerMock, writerMock, httpClientMock
69 }
70
71 func mockHttpClient(httpClientMock *mocks.HttpClientMock, isSuccessful bool) {
72         data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(e2tAddress, RanName)}
73         marshaled, _ := json.Marshal(data)
74         body := bytes.NewBuffer(marshaled)
75         respBody := ioutil.NopCloser(bytes.NewBufferString(""))
76         var respStatusCode int
77         if isSuccessful {
78                 respStatusCode = http.StatusCreated
79         } else {
80                 respStatusCode = http.StatusBadRequest
81         }
82         httpClientMock.On("Post", clients.DissociateRanE2TInstanceApiSuffix, "application/json", body).Return(&http.Response{StatusCode: respStatusCode, Body: respBody}, nil)
83 }
84
85 func TestLostConnectionHandlerConnectingRanSuccess(t *testing.T) {
86         handler, readerMock, writerMock, httpClientMock := setupLostConnectionHandlerTest(true)
87
88         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, AssociatedE2TInstanceAddress: e2tAddress}
89         var rnibErr error
90         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
91         updatedNodebInfo1 := *origNodebInfo
92         updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
93         writerMock.On("UpdateNodebInfo", mock.Anything).Return(rnibErr)
94         updatedNodebInfo2 := *origNodebInfo
95         updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
96         updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
97         writerMock.On("UpdateNodebInfo", mock.Anything).Return(rnibErr)
98         e2tInstance := &entities.E2TInstance{Address: e2tAddress, AssociatedRanList: []string{ranName}}
99         readerMock.On("GetE2TInstance", e2tAddress).Return(e2tInstance, nil)
100         e2tInstanceToSave := *e2tInstance
101         e2tInstanceToSave.AssociatedRanList = []string{}
102         writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
103         mockHttpClient(httpClientMock, true)
104         notificationRequest := models.NotificationRequest{RanName: ranName}
105         handler.Handle(&notificationRequest)
106         readerMock.AssertExpectations(t)
107         writerMock.AssertExpectations(t)
108         httpClientMock.AssertExpectations(t)
109         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
110 }
111
112 func TestLostConnectionHandlerConnectedRanSuccess(t *testing.T) {
113         handler, readerMock, writerMock, httpClientMock := setupLostConnectionHandlerTest(true)
114
115         origNodebInfo := &entities.NodebInfo{
116                 RanName:                      ranName,
117                 GlobalNbId:                   &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"},
118                 ConnectionStatus:             entities.ConnectionStatus_CONNECTED,
119                 AssociatedE2TInstanceAddress: e2tAddress,
120         }
121         var rnibErr error
122         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
123         updatedNodebInfo1 := *origNodebInfo
124         updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
125         writerMock.On("UpdateNodebInfoOnConnectionStatusInversion", mock.Anything, ranName+"_DISCONNECTED").Return(rnibErr)
126         updatedNodebInfo2 := *origNodebInfo
127         updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
128         updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
129         writerMock.On("UpdateNodebInfo", mock.Anything).Return(rnibErr)
130         e2tInstance := &entities.E2TInstance{Address: e2tAddress, AssociatedRanList: []string{ranName}}
131         readerMock.On("GetE2TInstance", e2tAddress).Return(e2tInstance, nil)
132         e2tInstanceToSave := *e2tInstance
133         e2tInstanceToSave.AssociatedRanList = []string{}
134         writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
135         mockHttpClient(httpClientMock, true)
136         notificationRequest := models.NotificationRequest{RanName: ranName}
137         handler.Handle(&notificationRequest)
138         readerMock.AssertExpectations(t)
139         writerMock.AssertExpectations(t)
140         httpClientMock.AssertExpectations(t)
141         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
142 }
143
144 func TestLostConnectionHandlerRmDissociateFailure(t *testing.T) {
145         handler, readerMock, writerMock, httpClientMock := setupLostConnectionHandlerTest(false)
146
147         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, AssociatedE2TInstanceAddress: e2tAddress}
148         var rnibErr error
149         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
150         updatedNodebInfo1 := *origNodebInfo
151         updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
152         writerMock.On("UpdateNodebInfo", mock.Anything).Return(rnibErr)
153         updatedNodebInfo2 := *origNodebInfo
154         updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
155         updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
156         writerMock.On("UpdateNodebInfo", mock.Anything).Return(rnibErr)
157         e2tInstance := &entities.E2TInstance{Address: e2tAddress, AssociatedRanList: []string{ranName}}
158         readerMock.On("GetE2TInstance", e2tAddress).Return(e2tInstance, nil)
159         e2tInstanceToSave := *e2tInstance
160         e2tInstanceToSave.AssociatedRanList = []string{}
161         writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
162         mockHttpClient(httpClientMock, false)
163         notificationRequest := models.NotificationRequest{RanName: ranName}
164         handler.Handle(&notificationRequest)
165         readerMock.AssertExpectations(t)
166         writerMock.AssertExpectations(t)
167         httpClientMock.AssertExpectations(t)
168         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
169 }