Drop endc/x2 connection setup support
[portal/ric-dashboard.git] / dashboard / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / config / E2ManagerMockConfiguration.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20 package org.oransc.ric.portal.dashboard.config;
21
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.doAnswer;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26
27 import java.lang.invoke.MethodHandles;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.oransc.ricplt.e2mgr.client.api.HealthCheckApi;
34 import org.oransc.ricplt.e2mgr.client.api.NodebApi;
35 import org.oransc.ricplt.e2mgr.client.invoker.ApiClient;
36 import org.oransc.ricplt.e2mgr.client.model.GetNodebResponse;
37 import org.oransc.ricplt.e2mgr.client.model.NodebIdentity;
38 import org.oransc.ricplt.e2mgr.client.model.NodebIdentityGlobalNbId;
39 import org.oransc.ricplt.e2mgr.client.model.ResetRequest;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.beans.factory.annotation.Value;
44 import org.springframework.context.annotation.Bean;
45 import org.springframework.context.annotation.Configuration;
46 import org.springframework.context.annotation.Profile;
47 import org.springframework.http.HttpStatus;
48
49 /**
50  * Creates a mock implementation of the E2 Manager client API.
51  */
52 @Configuration
53 @Profile("test")
54 public class E2ManagerMockConfiguration {
55
56         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
57
58         public static final String RAN_NAME_1 = "Connected-RAN";
59         public static final String RAN_NAME_2 = "Unknown-RAN";
60
61         // Simulate remote method delay for UI testing
62         private int delayMs;
63
64         @Autowired
65         public E2ManagerMockConfiguration(@Value("${mock.config.delay:0}") int delayMs) {
66                 logger.debug("ctor: configured with delay {}", delayMs);
67                 this.delayMs = delayMs;
68         }
69
70         private ApiClient apiClient() {
71                 ApiClient mockClient = mock(ApiClient.class);
72                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
73                 return mockClient;
74         }
75
76         private HealthCheckApi healthCheckApi() {
77                 ApiClient apiClient = apiClient();
78                 HealthCheckApi mockApi = mock(HealthCheckApi.class);
79                 when(mockApi.getApiClient()).thenReturn(apiClient);
80                 doAnswer(i -> null).when(mockApi).healthGet();
81                 return mockApi;
82         }
83
84         /**
85          * Builds a mock NodebApi object.
86          * 
87          * @param instanceKey
88          *                        RIC instance
89          * @return Object that returns instance-specific results
90          */
91         private NodebApi nodebApi(String instanceKey) {
92
93                 final NodebIdentityGlobalNbId globalNbId = new NodebIdentityGlobalNbId().nbId("mockNbId-" + instanceKey)
94                                 .plmnId("mockPlmId");
95                 final List<NodebIdentity> nodebIdList = new ArrayList<>();
96                 final Map<String, GetNodebResponse> nodebResponseMap = new HashMap<>();
97                 // Complete entry
98                 nodebIdList.add(new NodebIdentity().inventoryName(RAN_NAME_1).globalNbId(globalNbId));
99                 nodebResponseMap.put(RAN_NAME_1, new GetNodebResponse().connectionStatus("CONNECTED").failureType("")
100                                 .ip("127.0.0.1").nodeType("mockNodeType").port(123).ranName(RAN_NAME_1));
101                 // Partial entry
102                 // [{"nodebIdentity":{"globalNbId":null,"inventoryName":"AAAA123456"},
103                 // "nodebStatus":{"connectionStatus":"CONNECTING","enb":null,"failureType":null,
104                 // "globalNbId":null,"gnb":null,"ip":"10.2.0.6","nodeType":null,"port":36444,
105                 // "ranName":"AAAA123456","setupFailure":null}}]
106                 nodebIdList.add(new NodebIdentity().inventoryName(RAN_NAME_1).globalNbId(globalNbId));
107                 nodebResponseMap.put(RAN_NAME_1,
108                                 new GetNodebResponse().connectionStatus("CONNECTING").ip("127.0.0.1").port(456).ranName(RAN_NAME_2).nodeType("ENDC").port(100));
109                 nodebIdList.add(new NodebIdentity().inventoryName(RAN_NAME_2).globalNbId(globalNbId));
110                 nodebResponseMap.put(RAN_NAME_2,
111                                 new GetNodebResponse().connectionStatus("CONNECTED").ip("127.0.0.2").port(456).ranName(RAN_NAME_2).nodeType("X2").port(200));
112
113                 ApiClient apiClient = apiClient();
114                 NodebApi mockApi = mock(NodebApi.class);
115                 when(mockApi.getApiClient()).thenReturn(apiClient);
116                 doAnswer(inv -> {
117                         if (delayMs > 0) {
118                                 logger.debug("nodebShutdownPut sleeping {}", delayMs);
119                                 Thread.sleep(delayMs);
120                         }
121                         nodebIdList.clear();
122                         return null;
123                 }).when(mockApi).nodebShutdownPut();
124                 doAnswer(inv -> {
125                         if (delayMs > 0) {
126                                 logger.debug("reset sleeping {}", delayMs);
127                                 Thread.sleep(delayMs);
128                         }
129                         return null;
130                 }).when(mockApi).reset(any(String.class), any(ResetRequest.class));
131                 doAnswer(inv -> {
132                         if (delayMs > 0) {
133                                 logger.debug("getNb sleeping {}", delayMs);
134                                 Thread.sleep(delayMs);
135                         }
136                         String invName = inv.<String>getArgument(0);
137                         return nodebResponseMap.get(invName);
138                 }).when(mockApi).getNb(any(String.class));
139                 doAnswer(inv -> {
140                         if (delayMs > 0) {
141                                 logger.debug("getNodebIdList sleeping {}", delayMs);
142                                 Thread.sleep(delayMs);
143                         }
144                         return nodebIdList;
145                 }).when(mockApi).getNodebIdList();
146                 return mockApi;
147         }
148
149         @Bean
150         // Must use the same name as the non-mock configuration
151         public E2ManagerApiBuilder e2ManagerApiBuilder() {
152                 final E2ManagerApiBuilder mockBuilder = mock(E2ManagerApiBuilder.class);
153                 final HealthCheckApi mockHealthCheckApi = healthCheckApi();
154                 when(mockBuilder.getHealthCheckApi(any(String.class))).thenReturn(mockHealthCheckApi);
155                 for (final String key : RICInstanceMockConfiguration.INSTANCE_KEYS) {
156                         final NodebApi mockNodebApi = nodebApi(key);
157                         when(mockBuilder.getNodebApi(key)).thenReturn(mockNodebApi);
158                 }
159                 return mockBuilder;
160         }
161
162 }