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.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;
 
  34 import java.util.HashMap;
 
  35 import java.util.List;
 
  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;
 
  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();
 
  87     protected static final Executor blockingExecutor = command -> {
 
  88         Thread thread = new Thread(command);
 
  89         thread.setDaemon(true);
 
  93     private static CdsSimulator sim;
 
  96     private CdsProcessorGrpcClient cdsClient;
 
  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;
 
 109     public static void setUpBeforeClass() throws Exception {
 
 110         sim = Util.buildCdsSim();
 
 114     public static void tearDownAfterClass() {
 
 119      * Sets up the fields.
 
 122     public void setUp() throws Exception {
 
 123         MockitoAnnotations.initMocks(this);
 
 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);
 
 134         when(cdsClient.sendRequest(any(ExecutionServiceInput.class))).thenReturn(mock(CountDownLatch.class));
 
 137         onset = new VirtualControlLoopEvent();
 
 138         onset.setRequestId(REQUEST_ID);
 
 141         executor = new PseudoExecutor();
 
 143         target = new Target();
 
 144         target.setType(TargetType.VM);
 
 145         target.setResourceID(RESOURCE_ID);
 
 147         cqFuture = new CompletableFuture<>();
 
 148         when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(cqFuture);
 
 149         when(context.getEvent()).thenReturn(onset);
 
 151         params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
 
 152                         .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
 
 153                         .targetEntity(TARGET_ENTITY).target(target).build();
 
 157      * Tests "success" case with simulator.
 
 160     public void testSuccess() throws Exception {
 
 161         ControlLoopEventContext context = new ControlLoopEventContext(onset);
 
 164         Map<String, Object> payload = Map.of("artifact_name", "my_artifact", "artifact_version", "1.0");
 
 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();
 
 171         cdsProps.setHost("localhost");
 
 172         cdsProps.setPort(sim.getPort());
 
 173         cdsProps.setTimeout(3);
 
 175         GrpcConfig config = new GrpcConfig(blockingExecutor, cdsProps);
 
 177         operation = new GrpcOperation(params, config) {
 
 179             protected CompletableFuture<OperationOutcome> startGuardAsync() {
 
 180                 // indicate that guard completed successfully
 
 181                 return CompletableFuture.completedFuture(params.makeOutcome());
 
 185         OperationOutcome outcome = operation.start().get();
 
 186         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
 
 187         assertTrue(outcome.getResponse() instanceof ExecutionServiceOutput);
 
 191     public void testGetPropertyNames() {
 
 196         operation = new GrpcOperation(params, config);
 
 199         assertThat(operation.getPropertyNames()).isEqualTo(
 
 201                             OperationProperties.AAI_MODEL_INVARIANT_GENERIC_VNF,
 
 202                             OperationProperties.AAI_RESOURCE_SERVICE_INSTANCE,
 
 203                             OperationProperties.EVENT_ADDITIONAL_PARAMS));
 
 209         target.setType(TargetType.PNF);
 
 210         operation = new GrpcOperation(params, config);
 
 213         assertThat(operation.getPropertyNames()).isEqualTo(
 
 215                             OperationProperties.AAI_PNF,
 
 216                             OperationProperties.EVENT_ADDITIONAL_PARAMS));
 
 221     public void testStartPreprocessorAsync() throws InterruptedException, ExecutionException, TimeoutException {
 
 222         AtomicBoolean guardStarted = new AtomicBoolean();
 
 224         operation = new GrpcOperation(params, config) {
 
 226             protected CompletableFuture<OperationOutcome> startGuardAsync() {
 
 227                 guardStarted.set(true);
 
 232         CompletableFuture<OperationOutcome> future3 = operation.startPreprocessorAsync();
 
 233         assertNotNull(future3);
 
 234         assertTrue(guardStarted.get());
 
 235         verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
 
 237         cqFuture.complete(params.makeOutcome());
 
 238         assertTrue(executor.runAll(100));
 
 239         assertEquals(PolicyResult.SUCCESS, future3.get(2, TimeUnit.SECONDS).getResult());
 
 240         assertTrue(future3.isDone());
 
 244      * Tests startPreprocessorAsync() when the target type is PNF.
 
 247     public void testStartPreprocessorAsyncPnf() throws InterruptedException, ExecutionException, TimeoutException {
 
 248         AtomicBoolean guardStarted = new AtomicBoolean();
 
 250         target.setType(TargetType.PNF);
 
 252         operation = new GrpcOperation(params, config) {
 
 254             protected CompletableFuture<OperationOutcome> startGuardAsync() {
 
 255                 guardStarted.set(true);
 
 260         CompletableFuture<OperationOutcome> future3 = operation.startPreprocessorAsync();
 
 261         assertNotNull(future3);
 
 262         assertTrue(guardStarted.get());
 
 263         verify(context).obtain(eq(AaiGetPnfOperation.getKey(TARGET_ENTITY)), any());
 
 265         cqFuture.complete(params.makeOutcome());
 
 266         assertTrue(executor.runAll(100));
 
 267         assertEquals(PolicyResult.SUCCESS, future3.get(2, TimeUnit.SECONDS).getResult());
 
 268         assertTrue(future3.isDone());
 
 272      * Tests startPreprocessorAsync(), when preprocessing is disabled.
 
 275     public void testStartPreprocessorAsyncDisabled() {
 
 276         params = params.toBuilder().preprocessed(true).build();
 
 277         assertNull(new GrpcOperation(params, config).startPreprocessorAsync());
 
 281     public void testStartOperationAsync() throws Exception {
 
 283         ControlLoopEventContext context = new ControlLoopEventContext(onset);
 
 286         verifyOperation(context);
 
 290      * Tests startOperationAsync() when the target type is PNF.
 
 293     public void testStartOperationAsyncPnf() throws Exception {
 
 295         target.setType(TargetType.PNF);
 
 297         ControlLoopEventContext context = new ControlLoopEventContext(onset);
 
 298         loadPnfData(context);
 
 300         verifyOperation(context);
 
 304     public void testStartOperationAsyncWithAdditionalParams() throws Exception {
 
 306         Map<String, String> additionalParams = new HashMap<>();
 
 307         additionalParams.put("test", "additionalParams");
 
 308         onset.setAdditionalEventParams(additionalParams);
 
 309         ControlLoopEventContext context = new ControlLoopEventContext(onset);
 
 311         verifyOperation(context);
 
 315     public void testStartOperationAsyncError() throws Exception {
 
 316         operation = new GrpcOperation(params, config);
 
 317         assertThatIllegalArgumentException().isThrownBy(() -> operation.startOperationAsync(1, params.makeOutcome()));
 
 320     private void verifyOperation(ControlLoopEventContext context) {
 
 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\"}");
 
 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();
 
 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);
 
 340     private void loadPnfData(ControlLoopEventContext context) throws CoderException {
 
 341         String json = "{'dataA': 'valueA', 'dataB': 'valueB'}".replace('\'', '"');
 
 342         StandardCoderObject sco = coder.decode(json, StandardCoderObject.class);
 
 344         context.setProperty(AaiGetPnfOperation.getKey(TARGET_ENTITY), sco);
 
 347     private void loadCqData(ControlLoopEventContext context) {
 
 348         GenericVnf genvnf = new GenericVnf();
 
 349         genvnf.setVnfId(MY_VNF);
 
 351         ServiceInstance serviceInstance = new ServiceInstance();
 
 352         serviceInstance.setServiceInstanceId(MY_SVC_ID);
 
 354         AaiCqResponse cq = mock(AaiCqResponse.class);
 
 355         when(cq.getGenericVnfByModelInvariantId(any())).thenReturn(genvnf);
 
 356         when(cq.getServiceInstance()).thenReturn(serviceInstance);
 
 358         context.setProperty(AaiCqResponse.CONTEXT_KEY, cq);