Remove Target and TargetType
[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.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;
78
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();
88
89     protected static final Executor blockingExecutor = command -> {
90         Thread thread = new Thread(command);
91         thread.setDaemon(true);
92         thread.start();
93     };
94
95     private static CdsSimulator sim;
96
97     @Mock
98     private CdsProcessorGrpcClient cdsClient;
99     @Mock
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;
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         targetType = TargetType.VM;
147         targetEntityIds = new HashMap<>();
148         targetEntityIds.put(ControlLoopOperationParams.PARAMS_ENTITY_RESOURCEID, 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).targetType(targetType).targetEntityIds(targetEntityIds)
157                 .build();
158     }
159
160     /**
161      * Tests "success" case with simulator.
162      */
163     @Test
164     public void testSuccess() throws Exception {
165         ControlLoopEventContext context = new ControlLoopEventContext(onset);
166         loadCqData(context);
167
168         Map<String, Object> payload = Map.of("artifact_name", "my_artifact", "artifact_version", "1.0");
169
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();
174
175         cdsProps.setHost("localhost");
176         cdsProps.setPort(sim.getPort());
177         cdsProps.setTimeout(3);
178
179         GrpcConfig config = new GrpcConfig(blockingExecutor, cdsProps);
180
181         operation = new GrpcOperation(params, config) {
182             @Override
183             protected CompletableFuture<OperationOutcome> startGuardAsync() {
184                 // indicate that guard completed successfully
185                 return CompletableFuture.completedFuture(params.makeOutcome(null));
186             }
187         };
188
189         OperationOutcome outcome = operation.start().get();
190         assertEquals(OperationResult.SUCCESS, outcome.getResult());
191         assertTrue(outcome.getResponse() instanceof ExecutionServiceOutput);
192     }
193
194     /**
195      * Tests "success" case with simulator using properties.
196      */
197     @Test
198     public void testSuccessViaProperties() throws Exception {
199         ControlLoopEventContext context = new ControlLoopEventContext(onset);
200         loadCqData(context);
201
202         Map<String, Object> payload = Map.of("artifact_name", "my_artifact", "artifact_version", "1.0");
203
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();
208
209         cdsProps.setHost("localhost");
210         cdsProps.setPort(sim.getPort());
211         cdsProps.setTimeout(3);
212
213         GrpcConfig config = new GrpcConfig(blockingExecutor, cdsProps);
214
215         operation = new GrpcOperation(params, config);
216
217         // set the properties
218         operation.setProperty(OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES, Collections.emptyMap());
219
220         OperationOutcome outcome = operation.start().get();
221         assertEquals(OperationResult.SUCCESS, outcome.getResult());
222         assertTrue(outcome.getResponse() instanceof ExecutionServiceOutput);
223     }
224
225     @Test
226     public void testGetPropertyNames() {
227
228         /*
229          * check VNF case
230          */
231         operation = new GrpcOperation(params, config);
232
233         // @formatter:off
234         assertThat(operation.getPropertyNames()).isEqualTo(
235                         List.of(
236                             OperationProperties.AAI_RESOURCE_VNF,
237                             OperationProperties.AAI_SERVICE,
238                             OperationProperties.EVENT_ADDITIONAL_PARAMS,
239                             OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES));
240         // @formatter:on
241
242         /*
243          * check PNF case
244          */
245         params = params.toBuilder().targetType(TargetType.PNF).build();
246         operation = new GrpcOperation(params, config);
247
248         // @formatter:off
249         assertThat(operation.getPropertyNames()).isEqualTo(
250                         List.of(
251                             OperationProperties.AAI_PNF,
252                             OperationProperties.EVENT_ADDITIONAL_PARAMS,
253                             OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES));
254         // @formatter:on
255     }
256
257     @Test
258     public void testGetPnf() {
259         ControlLoopEventContext context = new ControlLoopEventContext(onset);
260         params = params.toBuilder().context(context).build();
261         operation = new GrpcOperation(params, config);
262
263         // in neither property nor context
264         assertThatIllegalArgumentException().isThrownBy(() -> operation.getPnfData()).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(null));
339         assertTrue(executor.runAll(100));
340         assertEquals(OperationResult.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         params = params.toBuilder().targetType(TargetType.PNF).build();
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(null));
367         assertTrue(executor.runAll(100));
368         assertEquals(OperationResult.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         targetType = 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()
419                         .isThrownBy(() -> operation.startOperationAsync(1, params.makeOutcome(null)));
420     }
421
422     @Test
423     public void testGetAdditionalEventParams() {
424         operation = new GrpcOperation(params, config);
425
426         // in neither property nor context
427         assertNull(operation.getAdditionalEventParams());
428
429         final Map<String, String> eventParams = Collections.emptyMap();
430
431         // only in context
432         onset.setAdditionalEventParams(eventParams);
433         assertSame(eventParams, operation.getAdditionalEventParams());
434
435         // both - should choose the property, even if it's null
436         operation.setProperty(OperationProperties.EVENT_ADDITIONAL_PARAMS, null);
437         assertNull(operation.getAdditionalEventParams());
438
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());
443     }
444
445     private void verifyOperation(ControlLoopEventContext context) {
446
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\"}");
450
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();
455
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);
464     }
465
466     private void loadPnfData(ControlLoopEventContext context) throws CoderException {
467         String json = "{'dataA': 'valueA', 'dataB': 'valueB'}".replace('\'', '"');
468         StandardCoderObject sco = coder.decode(json, StandardCoderObject.class);
469
470         context.setProperty(AaiGetPnfOperation.getKey(TARGET_ENTITY), sco);
471     }
472
473     private void loadCqData(ControlLoopEventContext context) {
474         GenericVnf genvnf = new GenericVnf();
475         genvnf.setVnfId(MY_VNF);
476
477         ServiceInstance serviceInstance = new ServiceInstance();
478         serviceInstance.setServiceInstanceId(MY_SVC_ID);
479
480         AaiCqResponse cq = mock(AaiCqResponse.class);
481         when(cq.getGenericVnfByModelInvariantId(any())).thenReturn(genvnf);
482         when(cq.getServiceInstance()).thenReturn(serviceInstance);
483
484         context.setProperty(AaiCqResponse.CONTEXT_KEY, cq);
485     }
486 }