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.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
33 import java.util.HashMap;
34 import java.util.List;
36 import java.util.UUID;
37 import java.util.concurrent.CompletableFuture;
38 import java.util.concurrent.CountDownLatch;
39 import java.util.concurrent.ExecutionException;
40 import java.util.concurrent.Executor;
41 import java.util.concurrent.TimeUnit;
42 import java.util.concurrent.TimeoutException;
43 import java.util.concurrent.atomic.AtomicBoolean;
44 import org.junit.AfterClass;
45 import org.junit.Before;
46 import org.junit.BeforeClass;
47 import org.junit.Test;
48 import org.mockito.Mock;
49 import org.mockito.MockitoAnnotations;
50 import org.onap.aai.domain.yang.GenericVnf;
51 import org.onap.aai.domain.yang.ServiceInstance;
52 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
53 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
54 import org.onap.policy.aai.AaiCqResponse;
55 import org.onap.policy.cds.client.CdsProcessorGrpcClient;
56 import org.onap.policy.cds.properties.CdsServerProperties;
57 import org.onap.policy.common.utils.coder.Coder;
58 import org.onap.policy.common.utils.coder.CoderException;
59 import org.onap.policy.common.utils.coder.StandardCoder;
60 import org.onap.policy.common.utils.coder.StandardCoderObject;
61 import org.onap.policy.common.utils.time.PseudoExecutor;
62 import org.onap.policy.controlloop.VirtualControlLoopEvent;
63 import org.onap.policy.controlloop.actor.aai.AaiGetPnfOperation;
64 import org.onap.policy.controlloop.actor.cds.constants.CdsActorConstants;
65 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
66 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
67 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
68 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
69 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
70 import org.onap.policy.controlloop.policy.PolicyResult;
71 import org.onap.policy.controlloop.policy.Target;
72 import org.onap.policy.controlloop.policy.TargetType;
73 import org.onap.policy.simulators.CdsSimulator;
74 import org.onap.policy.simulators.Util;
76 public class GrpcOperationTest {
77 private static final String TARGET_ENTITY = "entity";
78 private static final String MY_VNF = "my-vnf";
79 private static final String MY_SVC_ID = "my-service-instance-id";
80 private static final String RESOURCE_ID = "my-resource-id";
81 private static final String CDS_BLUEPRINT_NAME = "vfw-cds";
82 private static final String CDS_BLUEPRINT_VERSION = "1.0.0";
83 private static final UUID REQUEST_ID = UUID.randomUUID();
84 private static final Coder coder = new StandardCoder();
86 protected static final Executor blockingExecutor = command -> {
87 Thread thread = new Thread(command);
88 thread.setDaemon(true);
92 private static CdsSimulator sim;
95 private CdsProcessorGrpcClient cdsClient;
96 private CdsServerProperties cdsProps;
97 private VirtualControlLoopEvent onset;
98 private PseudoExecutor executor;
99 private Target target;
100 private GrpcOperation operation;
103 public static void setUpBeforeClass() throws Exception {
104 sim = Util.buildCdsSim();
108 public static void tearDownAfterClass() {
113 * Sets up the fields.
116 public void setUp() throws Exception {
117 MockitoAnnotations.initMocks(this);
119 // Setup the CDS properties
120 cdsProps = new CdsServerProperties();
121 cdsProps.setHost("10.10.10.10");
122 cdsProps.setPort(2000);
123 cdsProps.setUsername("testUser");
124 cdsProps.setPassword("testPassword");
125 cdsProps.setTimeout(1);
128 when(cdsClient.sendRequest(any(ExecutionServiceInput.class))).thenReturn(mock(CountDownLatch.class));
131 onset = new VirtualControlLoopEvent();
132 onset.setRequestId(REQUEST_ID);
135 executor = new PseudoExecutor();
137 target = new Target();
138 target.setType(TargetType.VM);
139 target.setResourceID(RESOURCE_ID);
143 * Tests "success" case with simulator.
146 public void testSuccess() throws Exception {
147 ControlLoopEventContext context = new ControlLoopEventContext(onset);
150 Map<String, Object> payload = Map.of("artifact_name", "my_artifact", "artifact_version", "1.0");
152 final ControlLoopOperationParams params = ControlLoopOperationParams.builder()
153 .actor(CdsActorConstants.CDS_ACTOR).operation("subscribe").context(context)
154 .actorService(new ActorService()).targetEntity(TARGET_ENTITY).target(target).retry(0)
155 .timeoutSec(5).executor(blockingExecutor).payload(payload).build();
157 cdsProps.setHost("localhost");
158 cdsProps.setPort(sim.getPort());
159 cdsProps.setTimeout(3);
161 GrpcConfig config = new GrpcConfig(blockingExecutor, cdsProps);
163 operation = new GrpcOperation(params, config) {
165 protected CompletableFuture<OperationOutcome> startGuardAsync() {
166 // indicate that guard completed successfully
167 return CompletableFuture.completedFuture(params.makeOutcome());
171 OperationOutcome outcome = operation.start().get();
172 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
173 assertTrue(outcome.getResponse() instanceof ExecutionServiceOutput);
177 public void testGetPropertyNames() {
178 ControlLoopEventContext context = mock(ControlLoopEventContext.class);
179 when(context.getEvent()).thenReturn(onset);
181 ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
182 .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
183 .targetEntity(TARGET_ENTITY).target(target).build();
184 GrpcConfig config = new GrpcConfig(executor, cdsProps);
189 operation = new GrpcOperation(params, config);
192 assertThat(operation.getPropertyNames()).isEqualTo(
194 OperationProperties.AAI_MODEL_INVARIANT_GENERIC_VNF,
195 OperationProperties.AAI_RESOURCE_SERVICE_INSTANCE,
196 OperationProperties.EVENT_ADDITIONAL_PARAMS));
202 target.setType(TargetType.PNF);
203 operation = new GrpcOperation(params, config);
206 assertThat(operation.getPropertyNames()).isEqualTo(
208 OperationProperties.AAI_PNF,
209 OperationProperties.EVENT_ADDITIONAL_PARAMS));
214 public void testStartPreprocessorAsync() throws InterruptedException, ExecutionException, TimeoutException {
216 CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
217 ControlLoopEventContext context = mock(ControlLoopEventContext.class);
218 when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future2);
219 when(context.getEvent()).thenReturn(onset);
221 AtomicBoolean guardStarted = new AtomicBoolean();
223 ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
224 .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
225 .targetEntity(TARGET_ENTITY).target(target).build();
226 GrpcConfig config = new GrpcConfig(executor, cdsProps);
228 operation = new GrpcOperation(params, config) {
230 protected CompletableFuture<OperationOutcome> startGuardAsync() {
231 guardStarted.set(true);
236 CompletableFuture<OperationOutcome> future3 = operation.startPreprocessorAsync();
237 assertNotNull(future3);
238 assertTrue(guardStarted.get());
239 verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
241 future2.complete(params.makeOutcome());
242 assertTrue(executor.runAll(100));
243 assertEquals(PolicyResult.SUCCESS, future3.get(2, TimeUnit.SECONDS).getResult());
244 assertTrue(future3.isDone());
248 * Tests startPreprocessorAsync() when the target type is PNF.
251 public void testStartPreprocessorAsyncPnf() throws InterruptedException, ExecutionException, TimeoutException {
253 CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
254 ControlLoopEventContext context = mock(ControlLoopEventContext.class);
255 when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future2);
256 when(context.getEvent()).thenReturn(onset);
258 AtomicBoolean guardStarted = new AtomicBoolean();
260 target.setType(TargetType.PNF);
262 ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
263 .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
264 .targetEntity(TARGET_ENTITY).target(target).build();
265 GrpcConfig config = new GrpcConfig(executor, cdsProps);
267 operation = new GrpcOperation(params, config) {
269 protected CompletableFuture<OperationOutcome> startGuardAsync() {
270 guardStarted.set(true);
275 CompletableFuture<OperationOutcome> future3 = operation.startPreprocessorAsync();
276 assertNotNull(future3);
277 assertTrue(guardStarted.get());
278 verify(context).obtain(eq(AaiGetPnfOperation.getKey(TARGET_ENTITY)), any());
280 future2.complete(params.makeOutcome());
281 assertTrue(executor.runAll(100));
282 assertEquals(PolicyResult.SUCCESS, future3.get(2, TimeUnit.SECONDS).getResult());
283 assertTrue(future3.isDone());
287 public void testStartOperationAsync() throws Exception {
289 ControlLoopEventContext context = new ControlLoopEventContext(onset);
292 verifyOperation(context);
296 * Tests startOperationAsync() when the target type is PNF.
299 public void testStartOperationAsyncPnf() throws Exception {
301 target.setType(TargetType.PNF);
303 ControlLoopEventContext context = new ControlLoopEventContext(onset);
304 loadPnfData(context);
306 verifyOperation(context);
310 public void testStartOperationAsyncWithAdditionalParams() throws Exception {
312 Map<String, String> additionalParams = new HashMap<>();
313 additionalParams.put("test", "additionalParams");
314 onset.setAdditionalEventParams(additionalParams);
315 ControlLoopEventContext context = new ControlLoopEventContext(onset);
317 verifyOperation(context);
321 public void testStartOperationAsyncError() throws Exception {
323 ControlLoopEventContext context = new ControlLoopEventContext(onset);
324 ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
325 .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
326 .targetEntity(TARGET_ENTITY).target(target).build();
328 GrpcConfig config = new GrpcConfig(executor, cdsProps);
329 operation = new GrpcOperation(params, config);
330 assertThatIllegalArgumentException().isThrownBy(() -> operation.startOperationAsync(1, params.makeOutcome()));
333 private void verifyOperation(ControlLoopEventContext context) {
335 Map<String, Object> payloadMap = Map.of(CdsActorConstants.KEY_CBA_NAME, CDS_BLUEPRINT_NAME,
336 CdsActorConstants.KEY_CBA_VERSION, CDS_BLUEPRINT_VERSION, "data",
337 "{\"mapInfo\":{\"key\":\"val\"},\"arrayInfo\":[\"one\",\"two\"],\"paramInfo\":\"val\"}");
339 ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
340 .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
341 .targetEntity(TARGET_ENTITY).target(target).payload(payloadMap).build();
343 GrpcConfig config = new GrpcConfig(executor, cdsProps);
344 operation = new GrpcOperation(params, config);
345 assertEquals(1000, operation.getTimeoutMs(null));
346 assertEquals(1000, operation.getTimeoutMs(0));
347 assertEquals(2000, operation.getTimeoutMs(2));
348 operation.generateSubRequestId(1);
349 CompletableFuture<OperationOutcome> future3 = operation.startOperationAsync(1, params.makeOutcome());
350 assertNotNull(future3);
353 private void loadPnfData(ControlLoopEventContext context) throws CoderException {
354 String json = "{'dataA': 'valueA', 'dataB': 'valueB'}".replace('\'', '"');
355 StandardCoderObject sco = coder.decode(json, StandardCoderObject.class);
357 context.setProperty(AaiGetPnfOperation.getKey(TARGET_ENTITY), sco);
360 private void loadCqData(ControlLoopEventContext context) {
361 GenericVnf genvnf = new GenericVnf();
362 genvnf.setVnfId(MY_VNF);
364 ServiceInstance serviceInstance = new ServiceInstance();
365 serviceInstance.setServiceInstanceId(MY_SVC_ID);
367 AaiCqResponse cq = mock(AaiCqResponse.class);
368 when(cq.getGenericVnfByModelInvariantId(any())).thenReturn(genvnf);
369 when(cq.getServiceInstance()).thenReturn(serviceInstance);
371 context.setProperty(AaiCqResponse.CONTEXT_KEY, cq);