2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Bell Canada. All rights reserved.
4 * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
20 package org.onap.policy.controlloop.actor.cds;
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertSame;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.ArgumentMatchers.eq;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.List;
39 import java.util.UUID;
40 import java.util.concurrent.CompletableFuture;
41 import java.util.concurrent.CountDownLatch;
42 import java.util.concurrent.ExecutionException;
43 import java.util.concurrent.Executor;
44 import java.util.concurrent.TimeUnit;
45 import java.util.concurrent.TimeoutException;
46 import java.util.concurrent.atomic.AtomicBoolean;
47 import org.junit.AfterClass;
48 import org.junit.Before;
49 import org.junit.BeforeClass;
50 import org.junit.Test;
51 import org.mockito.Mock;
52 import org.mockito.MockitoAnnotations;
53 import org.onap.aai.domain.yang.GenericVnf;
54 import org.onap.aai.domain.yang.Pnf;
55 import org.onap.aai.domain.yang.ServiceInstance;
56 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
57 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
58 import org.onap.policy.aai.AaiCqResponse;
59 import org.onap.policy.cds.client.CdsProcessorGrpcClient;
60 import org.onap.policy.cds.properties.CdsServerProperties;
61 import org.onap.policy.common.utils.coder.Coder;
62 import org.onap.policy.common.utils.coder.CoderException;
63 import org.onap.policy.common.utils.coder.StandardCoder;
64 import org.onap.policy.common.utils.coder.StandardCoderObject;
65 import org.onap.policy.common.utils.time.PseudoExecutor;
66 import org.onap.policy.controlloop.VirtualControlLoopEvent;
67 import org.onap.policy.controlloop.actor.aai.AaiGetPnfOperation;
68 import org.onap.policy.controlloop.actor.cds.constants.CdsActorConstants;
69 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
70 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
71 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
72 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
73 import org.onap.policy.controlloop.actorserviceprovider.TargetType;
74 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
75 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
76 import org.onap.policy.simulators.CdsSimulator;
77 import org.onap.policy.simulators.Util;
79 public class GrpcOperationTest {
80 private static final String TARGET_ENTITY = "entity";
81 private static final String MY_VNF = "my-vnf";
82 private static final String MY_SVC_ID = "my-service-instance-id";
83 private static final String RESOURCE_ID = "my-resource-id";
84 private static final String CDS_BLUEPRINT_NAME = "vfw-cds";
85 private static final String CDS_BLUEPRINT_VERSION = "1.0.0";
86 private static final UUID REQUEST_ID = UUID.randomUUID();
87 private static final Coder coder = new StandardCoder();
89 protected static final Executor blockingExecutor = command -> {
90 Thread thread = new Thread(command);
91 thread.setDaemon(true);
95 private static CdsSimulator sim;
98 private CdsProcessorGrpcClient cdsClient;
100 private ControlLoopEventContext context;
101 private CdsServerProperties cdsProps;
102 private VirtualControlLoopEvent onset;
103 private PseudoExecutor executor;
104 private TargetType targetType;
105 private Map<String, String> targetEntityIds;
106 private ControlLoopOperationParams params;
107 private GrpcConfig config;
108 private CompletableFuture<OperationOutcome> cqFuture;
109 private GrpcOperation operation;
112 public static void setUpBeforeClass() throws Exception {
113 sim = Util.buildCdsSim();
117 public static void tearDownAfterClass() {
122 * Sets up the fields.
125 public void setUp() throws Exception {
126 MockitoAnnotations.initMocks(this);
128 // Setup the CDS properties
129 cdsProps = new CdsServerProperties();
130 cdsProps.setHost("10.10.10.10");
131 cdsProps.setPort(2000);
132 cdsProps.setUsername("testUser");
133 cdsProps.setPassword("testPassword");
134 cdsProps.setTimeout(1);
137 when(cdsClient.sendRequest(any(ExecutionServiceInput.class))).thenReturn(mock(CountDownLatch.class));
140 onset = new VirtualControlLoopEvent();
141 onset.setRequestId(REQUEST_ID);
144 executor = new PseudoExecutor();
146 targetType = TargetType.VM;
147 targetEntityIds = new HashMap<>();
148 targetEntityIds.put(ControlLoopOperationParams.PARAMS_ENTITY_RESOURCEID, RESOURCE_ID);
150 cqFuture = new CompletableFuture<>();
151 when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(cqFuture);
152 when(context.getEvent()).thenReturn(onset);
154 params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
155 .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
156 .targetEntity(TARGET_ENTITY).targetType(targetType).targetEntityIds(targetEntityIds)
161 * Tests "success" case with simulator.
164 public void testSuccess() throws Exception {
165 ControlLoopEventContext context = new ControlLoopEventContext(onset);
168 Map<String, Object> payload = Map.of("artifact_name", "my_artifact", "artifact_version", "1.0");
170 params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR).operation("subscribe")
171 .context(context).actorService(new ActorService()).targetEntity(TARGET_ENTITY)
172 .targetType(targetType).targetEntityIds(targetEntityIds)
173 .retry(0).timeoutSec(5).executor(blockingExecutor).payload(payload).build();
175 cdsProps.setHost("localhost");
176 cdsProps.setPort(sim.getPort());
177 cdsProps.setTimeout(3);
179 GrpcConfig config = new GrpcConfig(blockingExecutor, cdsProps);
181 operation = new GrpcOperation(params, config) {
183 protected CompletableFuture<OperationOutcome> startGuardAsync() {
184 // indicate that guard completed successfully
185 return CompletableFuture.completedFuture(params.makeOutcome(null));
189 OperationOutcome outcome = operation.start().get();
190 assertEquals(OperationResult.SUCCESS, outcome.getResult());
191 assertTrue(outcome.getResponse() instanceof ExecutionServiceOutput);
195 * Tests "success" case with simulator using properties.
198 public void testSuccessViaProperties() throws Exception {
199 ControlLoopEventContext context = new ControlLoopEventContext(onset);
202 Map<String, Object> payload = Map.of("artifact_name", "my_artifact", "artifact_version", "1.0");
204 params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR).operation("subscribe")
205 .context(context).actorService(new ActorService()).targetEntity(TARGET_ENTITY)
206 .targetType(targetType).targetEntityIds(targetEntityIds)
207 .retry(0).timeoutSec(5).executor(blockingExecutor).payload(payload).preprocessed(true).build();
209 cdsProps.setHost("localhost");
210 cdsProps.setPort(sim.getPort());
211 cdsProps.setTimeout(3);
213 GrpcConfig config = new GrpcConfig(blockingExecutor, cdsProps);
215 operation = new GrpcOperation(params, config);
217 // set the properties
218 operation.setProperty(OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES, Collections.emptyMap());
220 OperationOutcome outcome = operation.start().get();
221 assertEquals(OperationResult.SUCCESS, outcome.getResult());
222 assertTrue(outcome.getResponse() instanceof ExecutionServiceOutput);
226 public void testGetPropertyNames() {
231 operation = new GrpcOperation(params, config);
234 assertThat(operation.getPropertyNames()).isEqualTo(
236 OperationProperties.AAI_RESOURCE_VNF,
237 OperationProperties.AAI_SERVICE,
238 OperationProperties.EVENT_ADDITIONAL_PARAMS,
239 OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES));
245 params = params.toBuilder().targetType(TargetType.PNF).build();
246 operation = new GrpcOperation(params, config);
249 assertThat(operation.getPropertyNames()).isEqualTo(
251 OperationProperties.AAI_PNF,
252 OperationProperties.EVENT_ADDITIONAL_PARAMS,
253 OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES));
258 public void testGetPnf() {
259 ControlLoopEventContext context = new ControlLoopEventContext(onset);
260 params = params.toBuilder().context(context).build();
261 operation = new GrpcOperation(params, config);
263 // in neither property nor context
264 assertThatIllegalArgumentException().isThrownBy(() -> operation.getPnfData()).withMessage("missing PNF data");
268 params.getContext().setProperty(AaiGetPnfOperation.getKey(params.getTargetEntity()), pnf);
269 assertSame(pnf, operation.getPnfData());
271 // both - should choose the property
272 Pnf pnf2 = new Pnf();
273 operation.setProperty(OperationProperties.AAI_PNF, pnf2);
274 assertSame(pnf2, operation.getPnfData());
278 public void testGetServiceInstanceId() {
279 ControlLoopEventContext context = new ControlLoopEventContext(onset);
280 params = params.toBuilder().context(context).build();
281 operation = new GrpcOperation(params, config);
283 // in neither property nor custom query
284 context.setProperty(AaiCqResponse.CONTEXT_KEY, mock(AaiCqResponse.class));
285 assertThatIllegalArgumentException().isThrownBy(() -> operation.getServiceInstanceId())
286 .withMessage("Target service instance could not be found");
288 // only in custom query
289 loadCqData(params.getContext());
290 assertEquals(MY_SVC_ID, operation.getServiceInstanceId());
292 // both - should choose the property
293 ServiceInstance serviceInstance = new ServiceInstance();
294 serviceInstance.setServiceInstanceId("another-service-id");
295 operation.setProperty(OperationProperties.AAI_SERVICE, serviceInstance);
296 assertEquals("another-service-id", operation.getServiceInstanceId());
300 public void testGetVnfId() {
301 ControlLoopEventContext context = new ControlLoopEventContext(onset);
302 params = params.toBuilder().context(context).build();
303 operation = new GrpcOperation(params, config);
305 // in neither property nor custom query
306 context.setProperty(AaiCqResponse.CONTEXT_KEY, mock(AaiCqResponse.class));
307 assertThatIllegalArgumentException().isThrownBy(() -> operation.getVnfId())
308 .withMessage("Target generic vnf could not be found");
310 // only in custom query
311 loadCqData(params.getContext());
312 assertEquals(MY_VNF, operation.getVnfId());
314 // both - should choose the property
315 GenericVnf vnf = new GenericVnf();
316 vnf.setVnfId("another-vnf-id");
317 operation.setProperty(OperationProperties.AAI_RESOURCE_VNF, vnf);
318 assertEquals("another-vnf-id", operation.getVnfId());
322 public void testStartPreprocessorAsync() throws InterruptedException, ExecutionException, TimeoutException {
323 AtomicBoolean guardStarted = new AtomicBoolean();
325 operation = new GrpcOperation(params, config) {
327 protected CompletableFuture<OperationOutcome> startGuardAsync() {
328 guardStarted.set(true);
333 CompletableFuture<OperationOutcome> future3 = operation.startPreprocessorAsync();
334 assertNotNull(future3);
335 assertTrue(guardStarted.get());
336 verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
338 cqFuture.complete(params.makeOutcome(null));
339 assertTrue(executor.runAll(100));
340 assertEquals(OperationResult.SUCCESS, future3.get(2, TimeUnit.SECONDS).getResult());
341 assertTrue(future3.isDone());
345 * Tests startPreprocessorAsync() when the target type is PNF.
348 public void testStartPreprocessorAsyncPnf() throws InterruptedException, ExecutionException, TimeoutException {
349 AtomicBoolean guardStarted = new AtomicBoolean();
351 params = params.toBuilder().targetType(TargetType.PNF).build();
353 operation = new GrpcOperation(params, config) {
355 protected CompletableFuture<OperationOutcome> startGuardAsync() {
356 guardStarted.set(true);
361 CompletableFuture<OperationOutcome> future3 = operation.startPreprocessorAsync();
362 assertNotNull(future3);
363 assertTrue(guardStarted.get());
364 verify(context).obtain(eq(AaiGetPnfOperation.getKey(TARGET_ENTITY)), any());
366 cqFuture.complete(params.makeOutcome(null));
367 assertTrue(executor.runAll(100));
368 assertEquals(OperationResult.SUCCESS, future3.get(2, TimeUnit.SECONDS).getResult());
369 assertTrue(future3.isDone());
373 * Tests startPreprocessorAsync(), when preprocessing is disabled.
376 public void testStartPreprocessorAsyncDisabled() {
377 params = params.toBuilder().preprocessed(true).build();
378 assertNull(new GrpcOperation(params, config).startPreprocessorAsync());
382 public void testStartOperationAsync() throws Exception {
384 ControlLoopEventContext context = new ControlLoopEventContext(onset);
387 verifyOperation(context);
391 * Tests startOperationAsync() when the target type is PNF.
394 public void testStartOperationAsyncPnf() throws Exception {
396 targetType = TargetType.PNF;
398 ControlLoopEventContext context = new ControlLoopEventContext(onset);
399 loadPnfData(context);
401 verifyOperation(context);
405 public void testStartOperationAsyncWithAdditionalParams() throws Exception {
407 Map<String, String> additionalParams = new HashMap<>();
408 additionalParams.put("test", "additionalParams");
409 onset.setAdditionalEventParams(additionalParams);
410 ControlLoopEventContext context = new ControlLoopEventContext(onset);
412 verifyOperation(context);
416 public void testStartOperationAsyncError() throws Exception {
417 operation = new GrpcOperation(params, config);
418 assertThatIllegalArgumentException()
419 .isThrownBy(() -> operation.startOperationAsync(1, params.makeOutcome(null)));
423 public void testGetAdditionalEventParams() {
424 operation = new GrpcOperation(params, config);
426 // in neither property nor context
427 assertNull(operation.getAdditionalEventParams());
429 final Map<String, String> eventParams = Collections.emptyMap();
432 onset.setAdditionalEventParams(eventParams);
433 assertSame(eventParams, operation.getAdditionalEventParams());
435 // both - should choose the property, even if it's null
436 operation.setProperty(OperationProperties.EVENT_ADDITIONAL_PARAMS, null);
437 assertNull(operation.getAdditionalEventParams());
439 // both - should choose the property
440 final Map<String, String> propParams = Collections.emptyMap();
441 operation.setProperty(OperationProperties.EVENT_ADDITIONAL_PARAMS, propParams);
442 assertSame(propParams, operation.getAdditionalEventParams());
445 private void verifyOperation(ControlLoopEventContext context) {
447 Map<String, Object> payloadMap = Map.of(CdsActorConstants.KEY_CBA_NAME, CDS_BLUEPRINT_NAME,
448 CdsActorConstants.KEY_CBA_VERSION, CDS_BLUEPRINT_VERSION, "data",
449 "{\"mapInfo\":{\"key\":\"val\"},\"arrayInfo\":[\"one\",\"two\"],\"paramInfo\":\"val\"}");
451 ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
452 .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
453 .targetEntity(TARGET_ENTITY).targetType(targetType).targetEntityIds(targetEntityIds)
454 .payload(payloadMap).build();
456 GrpcConfig config = new GrpcConfig(executor, cdsProps);
457 operation = new GrpcOperation(params, config);
458 assertEquals(1000, operation.getTimeoutMs(null));
459 assertEquals(1000, operation.getTimeoutMs(0));
460 assertEquals(2000, operation.getTimeoutMs(2));
461 operation.generateSubRequestId(1);
462 CompletableFuture<OperationOutcome> future3 = operation.startOperationAsync(1, params.makeOutcome(null));
463 assertNotNull(future3);
466 private void loadPnfData(ControlLoopEventContext context) throws CoderException {
467 String json = "{'dataA': 'valueA', 'dataB': 'valueB'}".replace('\'', '"');
468 StandardCoderObject sco = coder.decode(json, StandardCoderObject.class);
470 context.setProperty(AaiGetPnfOperation.getKey(TARGET_ENTITY), sco);
473 private void loadCqData(ControlLoopEventContext context) {
474 GenericVnf genvnf = new GenericVnf();
475 genvnf.setVnfId(MY_VNF);
477 ServiceInstance serviceInstance = new ServiceInstance();
478 serviceInstance.setServiceInstanceId(MY_SVC_ID);
480 AaiCqResponse cq = mock(AaiCqResponse.class);
481 when(cq.getGenericVnfByModelInvariantId(any())).thenReturn(genvnf);
482 when(cq.getServiceInstance()).thenReturn(serviceInstance);
484 context.setProperty(AaiCqResponse.CONTEXT_KEY, cq);