644ce7aa8689b5572008db13a475acc98c312c24
[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         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
32         "io/ioutil"
33         "net/http"
34         "testing"
35 )
36
37 const (
38         ranName    = "test"
39         e2tAddress = "10.10.2.15:9800"
40 )
41
42 func setupLostConnectionHandlerTest(isSuccessfulHttpPost bool) (*RanLostConnectionHandler, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *mocks.HttpClientMock) {
43         logger, _ := logger.InitLogger(logger.InfoLevel)
44         config := &configuration.Configuration{
45                 RnibRetryIntervalMs:       10,
46                 MaxRnibConnectionAttempts: 3,
47                 StateChangeMessageChannel: StateChangeMessageChannel,
48         }
49
50         readerMock := &mocks.RnibReaderMock{}
51         writerMock := &mocks.RnibWriterMock{}
52         rnibDataService := services.NewRnibDataService(logger, config, readerMock, writerMock)
53         e2tInstancesManager := managers.NewE2TInstancesManager(rnibDataService, logger)
54         httpClientMock := &mocks.HttpClientMock{}
55         routingManagerClient := clients.NewRoutingManagerClient(logger, config, httpClientMock)
56         ranListManager := managers.NewRanListManager(logger)
57         ranAlarmService := services.NewRanAlarmService(logger, config)
58         ranConnectStatusChangeManager := managers.NewRanConnectStatusChangeManager(logger, rnibDataService, ranListManager, ranAlarmService)
59
60         e2tAssociationManager := managers.NewE2TAssociationManager(logger, rnibDataService, e2tInstancesManager, routingManagerClient, ranConnectStatusChangeManager)
61         ranDisconnectionManager := managers.NewRanDisconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, e2tAssociationManager, ranConnectStatusChangeManager)
62         handler := NewRanLostConnectionHandler(logger, ranDisconnectionManager)
63
64         return handler, readerMock, writerMock, httpClientMock
65 }
66
67 func mockHttpClient(httpClientMock *mocks.HttpClientMock, isSuccessful bool) {
68         data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(e2tAddress, RanName)}
69         marshaled, _ := json.Marshal(data)
70         body := bytes.NewBuffer(marshaled)
71         respBody := ioutil.NopCloser(bytes.NewBufferString(""))
72         var respStatusCode int
73         if isSuccessful {
74                 respStatusCode = http.StatusCreated
75         } else {
76                 respStatusCode = http.StatusBadRequest
77         }
78         httpClientMock.On("Post", clients.DissociateRanE2TInstanceApiSuffix, "application/json", body).Return(&http.Response{StatusCode: respStatusCode, Body: respBody}, nil)
79 }
80
81 func TestLostConnectionHandlerConnectingRanSuccess(t *testing.T) {
82         handler, readerMock, writerMock, httpClientMock := setupLostConnectionHandlerTest(true)
83
84         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, AssociatedE2TInstanceAddress: e2tAddress}
85         var rnibErr error
86         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
87         updatedNodebInfo1 := *origNodebInfo
88         updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
89         writerMock.On("UpdateNodebInfo", &updatedNodebInfo1).Return(rnibErr)
90         updatedNodebInfo2 := *origNodebInfo
91         updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
92         updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
93         writerMock.On("UpdateNodebInfo", &updatedNodebInfo2).Return(rnibErr)
94         e2tInstance := &entities.E2TInstance{Address: e2tAddress, AssociatedRanList: []string{ranName}}
95         readerMock.On("GetE2TInstance", e2tAddress).Return(e2tInstance, nil)
96         e2tInstanceToSave := *e2tInstance
97         e2tInstanceToSave.AssociatedRanList = []string{}
98         writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
99         mockHttpClient(httpClientMock, true)
100         notificationRequest := models.NotificationRequest{RanName: ranName}
101         handler.Handle(&notificationRequest)
102         readerMock.AssertExpectations(t)
103         writerMock.AssertExpectations(t)
104         httpClientMock.AssertExpectations(t)
105         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
106 }
107
108 func TestLostConnectionHandlerConnectedRanSuccess(t *testing.T) {
109         handler, readerMock, writerMock, httpClientMock := setupLostConnectionHandlerTest(true)
110
111         origNodebInfo := &entities.NodebInfo{
112                 RanName:                      ranName,
113                 GlobalNbId:                   &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"},
114                 ConnectionStatus:             entities.ConnectionStatus_CONNECTED,
115                 AssociatedE2TInstanceAddress: e2tAddress,
116         }
117         var rnibErr error
118         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
119         updatedNodebInfo1 := *origNodebInfo
120         updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
121         writerMock.On("UpdateNodebInfoOnConnectionStatusInversion", &updatedNodebInfo1, StateChangeMessageChannel, ranName+"_DISCONNECTED").Return(rnibErr)
122         updatedNodebInfo2 := *origNodebInfo
123         updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
124         updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
125         writerMock.On("UpdateNodebInfo", &updatedNodebInfo2).Return(rnibErr)
126         e2tInstance := &entities.E2TInstance{Address: e2tAddress, AssociatedRanList: []string{ranName}}
127         readerMock.On("GetE2TInstance", e2tAddress).Return(e2tInstance, nil)
128         e2tInstanceToSave := *e2tInstance
129         e2tInstanceToSave.AssociatedRanList = []string{}
130         writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
131         mockHttpClient(httpClientMock, true)
132         notificationRequest := models.NotificationRequest{RanName: ranName}
133         handler.Handle(&notificationRequest)
134         readerMock.AssertExpectations(t)
135         writerMock.AssertExpectations(t)
136         httpClientMock.AssertExpectations(t)
137         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
138 }
139
140 func TestLostConnectionHandlerRmDissociateFailure(t *testing.T) {
141         handler, readerMock, writerMock, httpClientMock := setupLostConnectionHandlerTest(false)
142
143         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, AssociatedE2TInstanceAddress: e2tAddress}
144         var rnibErr error
145         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
146         updatedNodebInfo1 := *origNodebInfo
147         updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
148         writerMock.On("UpdateNodebInfo", &updatedNodebInfo1).Return(rnibErr)
149         updatedNodebInfo2 := *origNodebInfo
150         updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
151         updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
152         writerMock.On("UpdateNodebInfo", &updatedNodebInfo2).Return(rnibErr)
153         e2tInstance := &entities.E2TInstance{Address: e2tAddress, AssociatedRanList: []string{ranName}}
154         readerMock.On("GetE2TInstance", e2tAddress).Return(e2tInstance, nil)
155         e2tInstanceToSave := *e2tInstance
156         e2tInstanceToSave.AssociatedRanList = []string{}
157         writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
158         mockHttpClient(httpClientMock, false)
159         notificationRequest := models.NotificationRequest{RanName: ranName}
160         handler.Handle(&notificationRequest)
161         readerMock.AssertExpectations(t)
162         writerMock.AssertExpectations(t)
163         httpClientMock.AssertExpectations(t)
164         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
165 }