2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.ric.portal.dashboard.config;
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;
27 import java.lang.invoke.MethodHandles;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
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.oransc.ricplt.e2mgr.client.model.SetupRequest;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.beans.factory.annotation.Value;
45 import org.springframework.context.annotation.Bean;
46 import org.springframework.context.annotation.Configuration;
47 import org.springframework.context.annotation.Profile;
48 import org.springframework.http.HttpStatus;
51 * Creates a mock implementation of the E2 Manager client API.
55 public class E2ManagerMockConfiguration {
57 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59 public static final String RAN_NAME_1 = "Connected-RAN";
60 public static final String RAN_NAME_2 = "Unknown-RAN";
62 // Simulate remote method delay for UI testing
66 public E2ManagerMockConfiguration(@Value("${mock.config.delay:0}") int delayMs) {
67 logger.debug("ctor: configured with delay {}", delayMs);
68 this.delayMs = delayMs;
71 private ApiClient apiClient() {
72 ApiClient mockClient = mock(ApiClient.class);
73 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
77 private HealthCheckApi healthCheckApi() {
78 ApiClient apiClient = apiClient();
79 HealthCheckApi mockApi = mock(HealthCheckApi.class);
80 when(mockApi.getApiClient()).thenReturn(apiClient);
81 doAnswer(i -> null).when(mockApi).healthGet();
86 * Builds a mock NodebApi object.
90 * @return Object that returns instance-specific results
92 private NodebApi nodebApi(String instanceKey) {
94 final NodebIdentityGlobalNbId globalNbId = new NodebIdentityGlobalNbId().nbId("mockNbId-" + instanceKey)
96 final List<NodebIdentity> nodebIdList = new ArrayList<>();
97 final Map<String, GetNodebResponse> nodebResponseMap = new HashMap<>();
99 nodebIdList.add(new NodebIdentity().inventoryName(RAN_NAME_1).globalNbId(globalNbId));
100 nodebResponseMap.put(RAN_NAME_1, new GetNodebResponse().connectionStatus("CONNECTED").failureType("")
101 .ip("127.0.0.1").nodeType("mockNodeType").port(123).ranName(RAN_NAME_1));
103 // [{"nodebIdentity":{"globalNbId":null,"inventoryName":"AAAA123456"},
104 // "nodebStatus":{"connectionStatus":"CONNECTING","enb":null,"failureType":null,
105 // "globalNbId":null,"gnb":null,"ip":"10.2.0.6","nodeType":null,"port":36444,
106 // "ranName":"AAAA123456","setupFailure":null}}]
107 nodebIdList.add(new NodebIdentity().inventoryName(RAN_NAME_2));
108 nodebResponseMap.put(RAN_NAME_2,
109 new GetNodebResponse().connectionStatus("CONNECTING").ip("127.0.0.2").port(456).ranName(RAN_NAME_2));
111 ApiClient apiClient = apiClient();
112 NodebApi mockApi = mock(NodebApi.class);
113 when(mockApi.getApiClient()).thenReturn(apiClient);
116 logger.debug("nodebShutdownPut sleeping {}", delayMs);
117 Thread.sleep(delayMs);
121 }).when(mockApi).nodebShutdownPut();
124 logger.debug("reset sleeping {}", delayMs);
125 Thread.sleep(delayMs);
128 }).when(mockApi).reset(any(String.class), any(ResetRequest.class));
131 logger.debug("getNb sleeping {}", delayMs);
132 Thread.sleep(delayMs);
134 String invName = inv.<String>getArgument(0);
135 return nodebResponseMap.get(invName);
136 }).when(mockApi).getNb(any(String.class));
139 logger.debug("getNodebIdList sleeping {}", delayMs);
140 Thread.sleep(delayMs);
143 }).when(mockApi).getNodebIdList();
146 logger.debug("endcSetup sleeping {}", delayMs);
147 Thread.sleep(delayMs);
149 SetupRequest sr = inv.<SetupRequest>getArgument(0);
150 nodebIdList.add(new NodebIdentity().inventoryName(sr.getRanName()).globalNbId(globalNbId));
151 nodebResponseMap.put(sr.getRanName(),
152 new GetNodebResponse().connectionStatus("mockConnectionStatus").failureType("mockFailureType")
153 .ip(sr.getRanIp()).nodeType("ENDC").port(sr.getRanPort()).ranName(sr.getRanName()));
155 }).when(mockApi).endcSetup(any(SetupRequest.class));
158 logger.debug("x2Setup sleeping {}", delayMs);
159 Thread.sleep(delayMs);
161 SetupRequest sr = inv.<SetupRequest>getArgument(0);
162 nodebIdList.add(new NodebIdentity().inventoryName(sr.getRanName()).globalNbId(globalNbId));
163 nodebResponseMap.put(sr.getRanName(),
164 new GetNodebResponse().connectionStatus("mockConnectionStatus").failureType("mockFailureType")
165 .ip(sr.getRanIp()).nodeType("X2").port(sr.getRanPort()).ranName(sr.getRanName()));
167 }).when(mockApi).x2Setup(any(SetupRequest.class));
172 // Must use the same name as the non-mock configuration
173 public E2ManagerApiBuilder e2ManagerApiBuilder() {
174 final E2ManagerApiBuilder mockBuilder = mock(E2ManagerApiBuilder.class);
175 final HealthCheckApi mockHealthCheckApi = healthCheckApi();
176 when(mockBuilder.getHealthCheckApi(any(String.class))).thenReturn(mockHealthCheckApi);
177 for (final String key : RICInstanceMockConfiguration.INSTANCE_KEYS) {
178 final NodebApi mockNodebApi = nodebApi(key);
179 when(mockBuilder.getNodebApi(key)).thenReturn(mockNodebApi);