Skip preprocessor step in Actors
[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.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.UUID;
38 import java.util.concurrent.CompletableFuture;
39 import java.util.concurrent.CountDownLatch;
40 import java.util.concurrent.ExecutionException;
41 import java.util.concurrent.Executor;
42 import java.util.concurrent.TimeUnit;
43 import java.util.concurrent.TimeoutException;
44 import java.util.concurrent.atomic.AtomicBoolean;
45 import org.junit.AfterClass;
46 import org.junit.Before;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 import org.mockito.Mock;
50 import org.mockito.MockitoAnnotations;
51 import org.onap.aai.domain.yang.GenericVnf;
52 import org.onap.aai.domain.yang.ServiceInstance;
53 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
54 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
55 import org.onap.policy.aai.AaiCqResponse;
56 import org.onap.policy.cds.client.CdsProcessorGrpcClient;
57 import org.onap.policy.cds.properties.CdsServerProperties;
58 import org.onap.policy.common.utils.coder.Coder;
59 import org.onap.policy.common.utils.coder.CoderException;
60 import org.onap.policy.common.utils.coder.StandardCoder;
61 import org.onap.policy.common.utils.coder.StandardCoderObject;
62 import org.onap.policy.common.utils.time.PseudoExecutor;
63 import org.onap.policy.controlloop.VirtualControlLoopEvent;
64 import org.onap.policy.controlloop.actor.aai.AaiGetPnfOperation;
65 import org.onap.policy.controlloop.actor.cds.constants.CdsActorConstants;
66 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
67 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
68 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
69 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
70 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
71 import org.onap.policy.controlloop.policy.PolicyResult;
72 import org.onap.policy.controlloop.policy.Target;
73 import org.onap.policy.controlloop.policy.TargetType;
74 import org.onap.policy.simulators.CdsSimulator;
75 import org.onap.policy.simulators.Util;
76
77 public class GrpcOperationTest {
78     private static final String TARGET_ENTITY = "entity";
79     private static final String MY_VNF = "my-vnf";
80     private static final String MY_SVC_ID = "my-service-instance-id";
81     private static final String RESOURCE_ID = "my-resource-id";
82     private static final String CDS_BLUEPRINT_NAME = "vfw-cds";
83     private static final String CDS_BLUEPRINT_VERSION = "1.0.0";
84     private static final UUID REQUEST_ID = UUID.randomUUID();
85     private static final Coder coder = new StandardCoder();
86
87     protected static final Executor blockingExecutor = command -> {
88         Thread thread = new Thread(command);
89         thread.setDaemon(true);
90         thread.start();
91     };
92
93     private static CdsSimulator sim;
94
95     @Mock
96     private CdsProcessorGrpcClient cdsClient;
97     @Mock
98     private ControlLoopEventContext context;
99     private CdsServerProperties cdsProps;
100     private VirtualControlLoopEvent onset;
101     private PseudoExecutor executor;
102     private Target target;
103     private ControlLoopOperationParams params;
104     private GrpcConfig config;
105     private CompletableFuture<OperationOutcome> cqFuture;
106     private GrpcOperation operation;
107
108     @BeforeClass
109     public static void setUpBeforeClass() throws Exception {
110         sim = Util.buildCdsSim();
111     }
112
113     @AfterClass
114     public static void tearDownAfterClass() {
115         sim.stop();
116     }
117
118     /**
119      * Sets up the fields.
120      */
121     @Before
122     public void setUp() throws Exception {
123         MockitoAnnotations.initMocks(this);
124
125         // Setup the CDS properties
126         cdsProps = new CdsServerProperties();
127         cdsProps.setHost("10.10.10.10");
128         cdsProps.setPort(2000);
129         cdsProps.setUsername("testUser");
130         cdsProps.setPassword("testPassword");
131         cdsProps.setTimeout(1);
132
133         // Setup cdsClient
134         when(cdsClient.sendRequest(any(ExecutionServiceInput.class))).thenReturn(mock(CountDownLatch.class));
135
136         // Setup onset event
137         onset = new VirtualControlLoopEvent();
138         onset.setRequestId(REQUEST_ID);
139
140         // Setup executor
141         executor = new PseudoExecutor();
142
143         target = new Target();
144         target.setType(TargetType.VM);
145         target.setResourceID(RESOURCE_ID);
146
147         cqFuture = new CompletableFuture<>();
148         when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(cqFuture);
149         when(context.getEvent()).thenReturn(onset);
150
151         params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
152                         .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
153                         .targetEntity(TARGET_ENTITY).target(target).build();
154     }
155
156     /**
157      * Tests "success" case with simulator.
158      */
159     @Test
160     public void testSuccess() throws Exception {
161         ControlLoopEventContext context = new ControlLoopEventContext(onset);
162         loadCqData(context);
163
164         Map<String, Object> payload = Map.of("artifact_name", "my_artifact", "artifact_version", "1.0");
165
166         params = ControlLoopOperationParams.builder()
167                         .actor(CdsActorConstants.CDS_ACTOR).operation("subscribe").context(context)
168                         .actorService(new ActorService()).targetEntity(TARGET_ENTITY).target(target).retry(0)
169                         .timeoutSec(5).executor(blockingExecutor).payload(payload).build();
170
171         cdsProps.setHost("localhost");
172         cdsProps.setPort(sim.getPort());
173         cdsProps.setTimeout(3);
174
175         GrpcConfig config = new GrpcConfig(blockingExecutor, cdsProps);
176
177         operation = new GrpcOperation(params, config) {
178             @Override
179             protected CompletableFuture<OperationOutcome> startGuardAsync() {
180                 // indicate that guard completed successfully
181                 return CompletableFuture.completedFuture(params.makeOutcome());
182             }
183         };
184
185         OperationOutcome outcome = operation.start().get();
186         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
187         assertTrue(outcome.getResponse() instanceof ExecutionServiceOutput);
188     }
189
190     @Test
191     public void testGetPropertyNames() {
192
193         /*
194          * check VNF case
195          */
196         operation = new GrpcOperation(params, config);
197
198         // @formatter:off
199         assertThat(operation.getPropertyNames()).isEqualTo(
200                         List.of(
201                             OperationProperties.AAI_MODEL_INVARIANT_GENERIC_VNF,
202                             OperationProperties.AAI_RESOURCE_SERVICE_INSTANCE,
203                             OperationProperties.EVENT_ADDITIONAL_PARAMS));
204         // @formatter:on
205
206         /*
207          * check PNF case
208          */
209         target.setType(TargetType.PNF);
210         operation = new GrpcOperation(params, config);
211
212         // @formatter:off
213         assertThat(operation.getPropertyNames()).isEqualTo(
214                         List.of(
215                             OperationProperties.AAI_PNF,
216                             OperationProperties.EVENT_ADDITIONAL_PARAMS));
217         // @formatter:on
218     }
219
220     @Test
221     public void testStartPreprocessorAsync() throws InterruptedException, ExecutionException, TimeoutException {
222         AtomicBoolean guardStarted = new AtomicBoolean();
223
224         operation = new GrpcOperation(params, config) {
225             @Override
226             protected CompletableFuture<OperationOutcome> startGuardAsync() {
227                 guardStarted.set(true);
228                 return cqFuture;
229             }
230         };
231
232         CompletableFuture<OperationOutcome> future3 = operation.startPreprocessorAsync();
233         assertNotNull(future3);
234         assertTrue(guardStarted.get());
235         verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
236
237         cqFuture.complete(params.makeOutcome());
238         assertTrue(executor.runAll(100));
239         assertEquals(PolicyResult.SUCCESS, future3.get(2, TimeUnit.SECONDS).getResult());
240         assertTrue(future3.isDone());
241     }
242
243     /**
244      * Tests startPreprocessorAsync() when the target type is PNF.
245      */
246     @Test
247     public void testStartPreprocessorAsyncPnf() throws InterruptedException, ExecutionException, TimeoutException {
248         AtomicBoolean guardStarted = new AtomicBoolean();
249
250         target.setType(TargetType.PNF);
251
252         operation = new GrpcOperation(params, config) {
253             @Override
254             protected CompletableFuture<OperationOutcome> startGuardAsync() {
255                 guardStarted.set(true);
256                 return cqFuture;
257             }
258         };
259
260         CompletableFuture<OperationOutcome> future3 = operation.startPreprocessorAsync();
261         assertNotNull(future3);
262         assertTrue(guardStarted.get());
263         verify(context).obtain(eq(AaiGetPnfOperation.getKey(TARGET_ENTITY)), any());
264
265         cqFuture.complete(params.makeOutcome());
266         assertTrue(executor.runAll(100));
267         assertEquals(PolicyResult.SUCCESS, future3.get(2, TimeUnit.SECONDS).getResult());
268         assertTrue(future3.isDone());
269     }
270
271     /**
272      * Tests startPreprocessorAsync(), when preprocessing is disabled.
273      */
274     @Test
275     public void testStartPreprocessorAsyncDisabled() {
276         params = params.toBuilder().preprocessed(true).build();
277         assertNull(new GrpcOperation(params, config).startPreprocessorAsync());
278     }
279
280     @Test
281     public void testStartOperationAsync() throws Exception {
282
283         ControlLoopEventContext context = new ControlLoopEventContext(onset);
284         loadCqData(context);
285
286         verifyOperation(context);
287     }
288
289     /**
290      * Tests startOperationAsync() when the target type is PNF.
291      */
292     @Test
293     public void testStartOperationAsyncPnf() throws Exception {
294
295         target.setType(TargetType.PNF);
296
297         ControlLoopEventContext context = new ControlLoopEventContext(onset);
298         loadPnfData(context);
299
300         verifyOperation(context);
301     }
302
303     @Test
304     public void testStartOperationAsyncWithAdditionalParams() throws Exception {
305
306         Map<String, String> additionalParams = new HashMap<>();
307         additionalParams.put("test", "additionalParams");
308         onset.setAdditionalEventParams(additionalParams);
309         ControlLoopEventContext context = new ControlLoopEventContext(onset);
310         loadCqData(context);
311         verifyOperation(context);
312     }
313
314     @Test
315     public void testStartOperationAsyncError() throws Exception {
316         operation = new GrpcOperation(params, config);
317         assertThatIllegalArgumentException().isThrownBy(() -> operation.startOperationAsync(1, params.makeOutcome()));
318     }
319
320     private void verifyOperation(ControlLoopEventContext context) {
321
322         Map<String, Object> payloadMap = Map.of(CdsActorConstants.KEY_CBA_NAME, CDS_BLUEPRINT_NAME,
323                         CdsActorConstants.KEY_CBA_VERSION, CDS_BLUEPRINT_VERSION, "data",
324                         "{\"mapInfo\":{\"key\":\"val\"},\"arrayInfo\":[\"one\",\"two\"],\"paramInfo\":\"val\"}");
325
326         ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
327                         .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
328                         .targetEntity(TARGET_ENTITY).target(target).payload(payloadMap).build();
329
330         GrpcConfig config = new GrpcConfig(executor, cdsProps);
331         operation = new GrpcOperation(params, config);
332         assertEquals(1000, operation.getTimeoutMs(null));
333         assertEquals(1000, operation.getTimeoutMs(0));
334         assertEquals(2000, operation.getTimeoutMs(2));
335         operation.generateSubRequestId(1);
336         CompletableFuture<OperationOutcome> future3 = operation.startOperationAsync(1, params.makeOutcome());
337         assertNotNull(future3);
338     }
339
340     private void loadPnfData(ControlLoopEventContext context) throws CoderException {
341         String json = "{'dataA': 'valueA', 'dataB': 'valueB'}".replace('\'', '"');
342         StandardCoderObject sco = coder.decode(json, StandardCoderObject.class);
343
344         context.setProperty(AaiGetPnfOperation.getKey(TARGET_ENTITY), sco);
345     }
346
347     private void loadCqData(ControlLoopEventContext context) {
348         GenericVnf genvnf = new GenericVnf();
349         genvnf.setVnfId(MY_VNF);
350
351         ServiceInstance serviceInstance = new ServiceInstance();
352         serviceInstance.setServiceInstanceId(MY_SVC_ID);
353
354         AaiCqResponse cq = mock(AaiCqResponse.class);
355         when(cq.getGenericVnfByModelInvariantId(any())).thenReturn(genvnf);
356         when(cq.getServiceInstance()).thenReturn(serviceInstance);
357
358         context.setProperty(AaiCqResponse.CONTEXT_KEY, cq);
359     }
360 }