Modify Actors to use properties when provided
[policy/models.git] / models-interactions / model-actors / actor.cds / src / test / java / org / onap / policy / controlloop / actor / cds / GrpcOperationTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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=========================================================
18  */
19
20 package org.onap.policy.controlloop.actor.cds;
21
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;
34
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
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.controlloop.ControlLoopEventContext;
73 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
74 import org.onap.policy.controlloop.policy.PolicyResult;
75 import org.onap.policy.controlloop.policy.Target;
76 import org.onap.policy.controlloop.policy.TargetType;
77 import org.onap.policy.simulators.CdsSimulator;
78 import org.onap.policy.simulators.Util;
79
80 public class GrpcOperationTest {
81     private static final String TARGET_ENTITY = "entity";
82     private static final String MY_VNF = "my-vnf";
83     private static final String MY_SVC_ID = "my-service-instance-id";
84     private static final String RESOURCE_ID = "my-resource-id";
85     private static final String CDS_BLUEPRINT_NAME = "vfw-cds";
86     private static final String CDS_BLUEPRINT_VERSION = "1.0.0";
87     private static final UUID REQUEST_ID = UUID.randomUUID();
88     private static final Coder coder = new StandardCoder();
89
90     protected static final Executor blockingExecutor = command -> {
91         Thread thread = new Thread(command);
92         thread.setDaemon(true);
93         thread.start();
94     };
95
96     private static CdsSimulator sim;
97
98     @Mock
99     private CdsProcessorGrpcClient cdsClient;
100     @Mock
101     private ControlLoopEventContext context;
102     private CdsServerProperties cdsProps;
103     private VirtualControlLoopEvent onset;
104     private PseudoExecutor executor;
105     private Target target;
106     private ControlLoopOperationParams params;
107     private GrpcConfig config;
108     private CompletableFuture<OperationOutcome> cqFuture;
109     private GrpcOperation operation;
110
111     @BeforeClass
112     public static void setUpBeforeClass() throws Exception {
113         sim = Util.buildCdsSim();
114     }
115
116     @AfterClass
117     public static void tearDownAfterClass() {
118         sim.stop();
119     }
120
121     /**
122      * Sets up the fields.
123      */
124     @Before
125     public void setUp() throws Exception {
126         MockitoAnnotations.initMocks(this);
127
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);
135
136         // Setup cdsClient
137         when(cdsClient.sendRequest(any(ExecutionServiceInput.class))).thenReturn(mock(CountDownLatch.class));
138
139         // Setup onset event
140         onset = new VirtualControlLoopEvent();
141         onset.setRequestId(REQUEST_ID);
142
143         // Setup executor
144         executor = new PseudoExecutor();
145
146         target = new Target();
147         target.setType(TargetType.VM);
148         target.setResourceID(RESOURCE_ID);
149
150         cqFuture = new CompletableFuture<>();
151         when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(cqFuture);
152         when(context.getEvent()).thenReturn(onset);
153
154         params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
155                         .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
156                         .targetEntity(TARGET_ENTITY).target(target).build();
157     }
158
159     /**
160      * Tests "success" case with simulator.
161      */
162     @Test
163     public void testSuccess() throws Exception {
164         ControlLoopEventContext context = new ControlLoopEventContext(onset);
165         loadCqData(context);
166
167         Map<String, Object> payload = Map.of("artifact_name", "my_artifact", "artifact_version", "1.0");
168
169         params = ControlLoopOperationParams.builder()
170                         .actor(CdsActorConstants.CDS_ACTOR).operation("subscribe").context(context)
171                         .actorService(new ActorService()).targetEntity(TARGET_ENTITY).target(target).retry(0)
172                         .timeoutSec(5).executor(blockingExecutor).payload(payload).build();
173
174         cdsProps.setHost("localhost");
175         cdsProps.setPort(sim.getPort());
176         cdsProps.setTimeout(3);
177
178         GrpcConfig config = new GrpcConfig(blockingExecutor, cdsProps);
179
180         operation = new GrpcOperation(params, config) {
181             @Override
182             protected CompletableFuture<OperationOutcome> startGuardAsync() {
183                 // indicate that guard completed successfully
184                 return CompletableFuture.completedFuture(params.makeOutcome());
185             }
186         };
187
188         OperationOutcome outcome = operation.start().get();
189         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
190         assertTrue(outcome.getResponse() instanceof ExecutionServiceOutput);
191     }
192
193     /**
194      * Tests "success" case with simulator using properties.
195      */
196     @Test
197     public void testSuccessViaProperties() throws Exception {
198         ControlLoopEventContext context = new ControlLoopEventContext(onset);
199         loadCqData(context);
200
201         Map<String, Object> payload = Map.of("artifact_name", "my_artifact", "artifact_version", "1.0");
202
203         params = ControlLoopOperationParams.builder()
204                         .actor(CdsActorConstants.CDS_ACTOR).operation("subscribe").context(context)
205                         .actorService(new ActorService()).targetEntity(TARGET_ENTITY).target(target).retry(0)
206                         .timeoutSec(5).executor(blockingExecutor).payload(payload).preprocessed(true).build();
207
208         cdsProps.setHost("localhost");
209         cdsProps.setPort(sim.getPort());
210         cdsProps.setTimeout(3);
211
212         GrpcConfig config = new GrpcConfig(blockingExecutor, cdsProps);
213
214         operation = new GrpcOperation(params, config);
215
216         // set the properties
217         operation.setProperty(OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES, Collections.emptyMap());
218
219         OperationOutcome outcome = operation.start().get();
220         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
221         assertTrue(outcome.getResponse() instanceof ExecutionServiceOutput);
222     }
223
224     @Test
225     public void testGetPropertyNames() {
226
227         /*
228          * check VNF case
229          */
230         operation = new GrpcOperation(params, config);
231
232         // @formatter:off
233         assertThat(operation.getPropertyNames()).isEqualTo(
234                         List.of(
235                             OperationProperties.AAI_RESOURCE_VNF,
236                             OperationProperties.AAI_SERVICE,
237                             OperationProperties.EVENT_ADDITIONAL_PARAMS,
238                             OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES));
239         // @formatter:on
240
241         /*
242          * check PNF case
243          */
244         target.setType(TargetType.PNF);
245         operation = new GrpcOperation(params, config);
246
247         // @formatter:off
248         assertThat(operation.getPropertyNames()).isEqualTo(
249                         List.of(
250                             OperationProperties.AAI_PNF,
251                             OperationProperties.EVENT_ADDITIONAL_PARAMS,
252                             OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES));
253         // @formatter:on
254     }
255
256     @Test
257     public void testGetPnf() {
258         ControlLoopEventContext context = new ControlLoopEventContext(onset);
259         params = params.toBuilder().context(context).build();
260         operation = new GrpcOperation(params, config);
261
262         // in neither property nor context
263         assertThatIllegalArgumentException().isThrownBy(() -> operation.getPnfData())
264                         .withMessage("missing PNF data");
265
266         // only in context
267         Pnf pnf = new Pnf();
268         params.getContext().setProperty(AaiGetPnfOperation.getKey(params.getTargetEntity()), pnf);
269         assertSame(pnf, operation.getPnfData());
270
271         // both - should choose the property
272         Pnf pnf2 = new Pnf();
273         operation.setProperty(OperationProperties.AAI_PNF, pnf2);
274         assertSame(pnf2, operation.getPnfData());
275     }
276
277     @Test
278     public void testGetServiceInstanceId() {
279         ControlLoopEventContext context = new ControlLoopEventContext(onset);
280         params = params.toBuilder().context(context).build();
281         operation = new GrpcOperation(params, config);
282
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");
287
288         // only in custom query
289         loadCqData(params.getContext());
290         assertEquals(MY_SVC_ID, operation.getServiceInstanceId());
291
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());
297     }
298
299     @Test
300     public void testGetVnfId() {
301         ControlLoopEventContext context = new ControlLoopEventContext(onset);
302         params = params.toBuilder().context(context).build();
303         operation = new GrpcOperation(params, config);
304
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");
309
310         // only in custom query
311         loadCqData(params.getContext());
312         assertEquals(MY_VNF, operation.getVnfId());
313
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());
319     }
320
321     @Test
322     public void testStartPreprocessorAsync() throws InterruptedException, ExecutionException, TimeoutException {
323         AtomicBoolean guardStarted = new AtomicBoolean();
324
325         operation = new GrpcOperation(params, config) {
326             @Override
327             protected CompletableFuture<OperationOutcome> startGuardAsync() {
328                 guardStarted.set(true);
329                 return cqFuture;
330             }
331         };
332
333         CompletableFuture<OperationOutcome> future3 = operation.startPreprocessorAsync();
334         assertNotNull(future3);
335         assertTrue(guardStarted.get());
336         verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
337
338         cqFuture.complete(params.makeOutcome());
339         assertTrue(executor.runAll(100));
340         assertEquals(PolicyResult.SUCCESS, future3.get(2, TimeUnit.SECONDS).getResult());
341         assertTrue(future3.isDone());
342     }
343
344     /**
345      * Tests startPreprocessorAsync() when the target type is PNF.
346      */
347     @Test
348     public void testStartPreprocessorAsyncPnf() throws InterruptedException, ExecutionException, TimeoutException {
349         AtomicBoolean guardStarted = new AtomicBoolean();
350
351         target.setType(TargetType.PNF);
352
353         operation = new GrpcOperation(params, config) {
354             @Override
355             protected CompletableFuture<OperationOutcome> startGuardAsync() {
356                 guardStarted.set(true);
357                 return cqFuture;
358             }
359         };
360
361         CompletableFuture<OperationOutcome> future3 = operation.startPreprocessorAsync();
362         assertNotNull(future3);
363         assertTrue(guardStarted.get());
364         verify(context).obtain(eq(AaiGetPnfOperation.getKey(TARGET_ENTITY)), any());
365
366         cqFuture.complete(params.makeOutcome());
367         assertTrue(executor.runAll(100));
368         assertEquals(PolicyResult.SUCCESS, future3.get(2, TimeUnit.SECONDS).getResult());
369         assertTrue(future3.isDone());
370     }
371
372     /**
373      * Tests startPreprocessorAsync(), when preprocessing is disabled.
374      */
375     @Test
376     public void testStartPreprocessorAsyncDisabled() {
377         params = params.toBuilder().preprocessed(true).build();
378         assertNull(new GrpcOperation(params, config).startPreprocessorAsync());
379     }
380
381     @Test
382     public void testStartOperationAsync() throws Exception {
383
384         ControlLoopEventContext context = new ControlLoopEventContext(onset);
385         loadCqData(context);
386
387         verifyOperation(context);
388     }
389
390     /**
391      * Tests startOperationAsync() when the target type is PNF.
392      */
393     @Test
394     public void testStartOperationAsyncPnf() throws Exception {
395
396         target.setType(TargetType.PNF);
397
398         ControlLoopEventContext context = new ControlLoopEventContext(onset);
399         loadPnfData(context);
400
401         verifyOperation(context);
402     }
403
404     @Test
405     public void testStartOperationAsyncWithAdditionalParams() throws Exception {
406
407         Map<String, String> additionalParams = new HashMap<>();
408         additionalParams.put("test", "additionalParams");
409         onset.setAdditionalEventParams(additionalParams);
410         ControlLoopEventContext context = new ControlLoopEventContext(onset);
411         loadCqData(context);
412         verifyOperation(context);
413     }
414
415     @Test
416     public void testStartOperationAsyncError() throws Exception {
417         operation = new GrpcOperation(params, config);
418         assertThatIllegalArgumentException().isThrownBy(() -> operation.startOperationAsync(1, params.makeOutcome()));
419     }
420
421     @Test
422     public void testGetAdditionalEventParams() {
423         operation = new GrpcOperation(params, config);
424
425         // in neither property nor context
426         assertNull(operation.getAdditionalEventParams());
427
428         final Map<String, String> eventParams = Collections.emptyMap();
429
430         // only in context
431         onset.setAdditionalEventParams(eventParams);
432         assertSame(eventParams, operation.getAdditionalEventParams());
433
434         // both - should choose the property, even if it's null
435         operation.setProperty(OperationProperties.EVENT_ADDITIONAL_PARAMS, null);
436         assertNull(operation.getAdditionalEventParams());
437
438         // both - should choose the property
439         final Map<String, String> propParams = Collections.emptyMap();
440         operation.setProperty(OperationProperties.EVENT_ADDITIONAL_PARAMS, propParams);
441         assertSame(propParams, operation.getAdditionalEventParams());
442     }
443
444     private void verifyOperation(ControlLoopEventContext context) {
445
446         Map<String, Object> payloadMap = Map.of(CdsActorConstants.KEY_CBA_NAME, CDS_BLUEPRINT_NAME,
447                         CdsActorConstants.KEY_CBA_VERSION, CDS_BLUEPRINT_VERSION, "data",
448                         "{\"mapInfo\":{\"key\":\"val\"},\"arrayInfo\":[\"one\",\"two\"],\"paramInfo\":\"val\"}");
449
450         ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
451                         .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
452                         .targetEntity(TARGET_ENTITY).target(target).payload(payloadMap).build();
453
454         GrpcConfig config = new GrpcConfig(executor, cdsProps);
455         operation = new GrpcOperation(params, config);
456         assertEquals(1000, operation.getTimeoutMs(null));
457         assertEquals(1000, operation.getTimeoutMs(0));
458         assertEquals(2000, operation.getTimeoutMs(2));
459         operation.generateSubRequestId(1);
460         CompletableFuture<OperationOutcome> future3 = operation.startOperationAsync(1, params.makeOutcome());
461         assertNotNull(future3);
462     }
463
464     private void loadPnfData(ControlLoopEventContext context) throws CoderException {
465         String json = "{'dataA': 'valueA', 'dataB': 'valueB'}".replace('\'', '"');
466         StandardCoderObject sco = coder.decode(json, StandardCoderObject.class);
467
468         context.setProperty(AaiGetPnfOperation.getKey(TARGET_ENTITY), sco);
469     }
470
471     private void loadCqData(ControlLoopEventContext context) {
472         GenericVnf genvnf = new GenericVnf();
473         genvnf.setVnfId(MY_VNF);
474
475         ServiceInstance serviceInstance = new ServiceInstance();
476         serviceInstance.setServiceInstanceId(MY_SVC_ID);
477
478         AaiCqResponse cq = mock(AaiCqResponse.class);
479         when(cq.getGenericVnfByModelInvariantId(any())).thenReturn(genvnf);
480         when(cq.getServiceInstance()).thenReturn(serviceInstance);
481
482         context.setProperty(AaiCqResponse.CONTEXT_KEY, cq);
483     }
484 }