cbc88a30ea1d50fb52e1f9957e23db368a9e90bc
[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.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;
32
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
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;
75
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();
85
86     protected static final Executor blockingExecutor = command -> {
87         Thread thread = new Thread(command);
88         thread.setDaemon(true);
89         thread.start();
90     };
91
92     private static CdsSimulator sim;
93
94     @Mock
95     private CdsProcessorGrpcClient cdsClient;
96     private CdsServerProperties cdsProps;
97     private VirtualControlLoopEvent onset;
98     private PseudoExecutor executor;
99     private Target target;
100     private GrpcOperation operation;
101
102     @BeforeClass
103     public static void setUpBeforeClass() throws Exception {
104         sim = Util.buildCdsSim();
105     }
106
107     @AfterClass
108     public static void tearDownAfterClass() {
109         sim.stop();
110     }
111
112     /**
113      * Sets up the fields.
114      */
115     @Before
116     public void setUp() throws Exception {
117         MockitoAnnotations.initMocks(this);
118
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);
126
127         // Setup cdsClient
128         when(cdsClient.sendRequest(any(ExecutionServiceInput.class))).thenReturn(mock(CountDownLatch.class));
129
130         // Setup onset event
131         onset = new VirtualControlLoopEvent();
132         onset.setRequestId(REQUEST_ID);
133
134         // Setup executor
135         executor = new PseudoExecutor();
136
137         target = new Target();
138         target.setType(TargetType.VM);
139         target.setResourceID(RESOURCE_ID);
140     }
141
142     /**
143      * Tests "success" case with simulator.
144      */
145     @Test
146     public void testSuccess() throws Exception {
147         ControlLoopEventContext context = new ControlLoopEventContext(onset);
148         loadCqData(context);
149
150         Map<String, Object> payload = Map.of("artifact_name", "my_artifact", "artifact_version", "1.0");
151
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();
156
157         cdsProps.setHost("localhost");
158         cdsProps.setPort(sim.getPort());
159         cdsProps.setTimeout(3);
160
161         GrpcConfig config = new GrpcConfig(blockingExecutor, cdsProps);
162
163         operation = new GrpcOperation(params, config) {
164             @Override
165             protected CompletableFuture<OperationOutcome> startGuardAsync() {
166                 // indicate that guard completed successfully
167                 return CompletableFuture.completedFuture(params.makeOutcome());
168             }
169         };
170
171         OperationOutcome outcome = operation.start().get();
172         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
173         assertTrue(outcome.getResponse() instanceof ExecutionServiceOutput);
174     }
175
176     @Test
177     public void testGetPropertyNames() {
178         ControlLoopEventContext context = mock(ControlLoopEventContext.class);
179         when(context.getEvent()).thenReturn(onset);
180
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);
185
186         /*
187          * check VNF case
188          */
189         operation = new GrpcOperation(params, config);
190
191         // @formatter:off
192         assertThat(operation.getPropertyNames()).isEqualTo(
193                         List.of(
194                             OperationProperties.AAI_MODEL_INVARIANT_GENERIC_VNF,
195                             OperationProperties.AAI_RESOURCE_SERVICE_INSTANCE,
196                             OperationProperties.EVENT_ADDITIONAL_PARAMS));
197         // @formatter:on
198
199         /*
200          * check PNF case
201          */
202         target.setType(TargetType.PNF);
203         operation = new GrpcOperation(params, config);
204
205         // @formatter:off
206         assertThat(operation.getPropertyNames()).isEqualTo(
207                         List.of(
208                             OperationProperties.AAI_PNF,
209                             OperationProperties.EVENT_ADDITIONAL_PARAMS));
210         // @formatter:on
211     }
212
213     @Test
214     public void testStartPreprocessorAsync() throws InterruptedException, ExecutionException, TimeoutException {
215
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);
220
221         AtomicBoolean guardStarted = new AtomicBoolean();
222
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);
227
228         operation = new GrpcOperation(params, config) {
229             @Override
230             protected CompletableFuture<OperationOutcome> startGuardAsync() {
231                 guardStarted.set(true);
232                 return future2;
233             }
234         };
235
236         CompletableFuture<OperationOutcome> future3 = operation.startPreprocessorAsync();
237         assertNotNull(future3);
238         assertTrue(guardStarted.get());
239         verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
240
241         future2.complete(params.makeOutcome());
242         assertTrue(executor.runAll(100));
243         assertEquals(PolicyResult.SUCCESS, future3.get(2, TimeUnit.SECONDS).getResult());
244         assertTrue(future3.isDone());
245     }
246
247     /**
248      * Tests startPreprocessorAsync() when the target type is PNF.
249      */
250     @Test
251     public void testStartPreprocessorAsyncPnf() throws InterruptedException, ExecutionException, TimeoutException {
252
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);
257
258         AtomicBoolean guardStarted = new AtomicBoolean();
259
260         target.setType(TargetType.PNF);
261
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);
266
267         operation = new GrpcOperation(params, config) {
268             @Override
269             protected CompletableFuture<OperationOutcome> startGuardAsync() {
270                 guardStarted.set(true);
271                 return future2;
272             }
273         };
274
275         CompletableFuture<OperationOutcome> future3 = operation.startPreprocessorAsync();
276         assertNotNull(future3);
277         assertTrue(guardStarted.get());
278         verify(context).obtain(eq(AaiGetPnfOperation.getKey(TARGET_ENTITY)), any());
279
280         future2.complete(params.makeOutcome());
281         assertTrue(executor.runAll(100));
282         assertEquals(PolicyResult.SUCCESS, future3.get(2, TimeUnit.SECONDS).getResult());
283         assertTrue(future3.isDone());
284     }
285
286     @Test
287     public void testStartOperationAsync() throws Exception {
288
289         ControlLoopEventContext context = new ControlLoopEventContext(onset);
290         loadCqData(context);
291
292         verifyOperation(context);
293     }
294
295     /**
296      * Tests startOperationAsync() when the target type is PNF.
297      */
298     @Test
299     public void testStartOperationAsyncPnf() throws Exception {
300
301         target.setType(TargetType.PNF);
302
303         ControlLoopEventContext context = new ControlLoopEventContext(onset);
304         loadPnfData(context);
305
306         verifyOperation(context);
307     }
308
309     @Test
310     public void testStartOperationAsyncWithAdditionalParams() throws Exception {
311
312         Map<String, String> additionalParams = new HashMap<>();
313         additionalParams.put("test", "additionalParams");
314         onset.setAdditionalEventParams(additionalParams);
315         ControlLoopEventContext context = new ControlLoopEventContext(onset);
316         loadCqData(context);
317         verifyOperation(context);
318     }
319
320     @Test
321     public void testStartOperationAsyncError() throws Exception {
322
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();
327
328         GrpcConfig config = new GrpcConfig(executor, cdsProps);
329         operation = new GrpcOperation(params, config);
330         assertThatIllegalArgumentException().isThrownBy(() -> operation.startOperationAsync(1, params.makeOutcome()));
331     }
332
333     private void verifyOperation(ControlLoopEventContext context) {
334
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\"}");
338
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();
342
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);
351     }
352
353     private void loadPnfData(ControlLoopEventContext context) throws CoderException {
354         String json = "{'dataA': 'valueA', 'dataB': 'valueB'}".replace('\'', '"');
355         StandardCoderObject sco = coder.decode(json, StandardCoderObject.class);
356
357         context.setProperty(AaiGetPnfOperation.getKey(TARGET_ENTITY), sco);
358     }
359
360     private void loadCqData(ControlLoopEventContext context) {
361         GenericVnf genvnf = new GenericVnf();
362         genvnf.setVnfId(MY_VNF);
363
364         ServiceInstance serviceInstance = new ServiceInstance();
365         serviceInstance.setServiceInstanceId(MY_SVC_ID);
366
367         AaiCqResponse cq = mock(AaiCqResponse.class);
368         when(cq.getGenericVnfByModelInvariantId(any())).thenReturn(genvnf);
369         when(cq.getServiceInstance()).thenReturn(serviceInstance);
370
371         context.setProperty(AaiCqResponse.CONTEXT_KEY, cq);
372     }
373 }