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