Modify Actors to use properties when provided
[policy/models.git] / models-interactions / model-actors / actor.cds / src / main / java / org / onap / policy / controlloop / actor / cds / GrpcOperation.java
index e57bc04..d384244 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  * Copyright (C) 2020 Bell Canada. All rights reserved.
+ * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -24,12 +25,18 @@ import com.google.protobuf.InvalidProtocolBufferException;
 import com.google.protobuf.Struct;
 import com.google.protobuf.Struct.Builder;
 import com.google.protobuf.util.JsonFormat;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.UUID;
 import java.util.concurrent.CompletableFuture;
+import java.util.function.Supplier;
 import lombok.Getter;
+import org.onap.aai.domain.yang.GenericVnf;
+import org.onap.aai.domain.yang.ServiceInstance;
 import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers;
 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader;
 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
@@ -38,11 +45,16 @@ import org.onap.policy.aai.AaiCqResponse;
 import org.onap.policy.cds.client.CdsProcessorGrpcClient;
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.controlloop.actor.aai.AaiCustomQueryOperation;
+import org.onap.policy.controlloop.actor.aai.AaiGetPnfOperation;
 import org.onap.policy.controlloop.actor.cds.constants.CdsActorConstants;
 import org.onap.policy.controlloop.actor.cds.request.CdsActionRequest;
 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
+import org.onap.policy.controlloop.actorserviceprovider.Util;
 import org.onap.policy.controlloop.actorserviceprovider.impl.OperationPartial;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
+import org.onap.policy.controlloop.actorserviceprovider.pipeline.PipelineControllerFuture;
+import org.onap.policy.controlloop.policy.TargetType;
 
 /**
  * Operation that uses gRPC to send request to CDS.
@@ -51,7 +63,11 @@ import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOp
 @Getter
 public class GrpcOperation extends OperationPartial {
 
-    public static final String NAME = "gRPC";
+    public static final String NAME = "any";
+
+    private static final String AAI_PNF_PREFIX = "pnf.";
+    private static final String AAI_VNF_ID_KEY = "generic-vnf.vnf-id";
+    private static final String AAI_SERVICE_INSTANCE_ID_KEY = "service-instance.service-instance-id";
 
     private CdsProcessorGrpcClient client;
 
@@ -60,6 +76,31 @@ public class GrpcOperation extends OperationPartial {
      */
     private final GrpcConfig config;
 
+    /**
+     * Function to request the A&AI data appropriate to the target type.
+     */
+    private final Supplier<CompletableFuture<OperationOutcome>> aaiRequestor;
+
+    /**
+     * Function to convert the A&AI data associated with the target type.
+     */
+    private final Supplier<Map<String, String>> aaiConverter;
+
+
+    // @formatter:off
+    private static final List<String> PNF_PROPERTY_NAMES = List.of(
+                            OperationProperties.AAI_PNF,
+                            OperationProperties.EVENT_ADDITIONAL_PARAMS,
+                            OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES);
+
+
+    private static final List<String> VNF_PROPERTY_NAMES = List.of(
+                            OperationProperties.AAI_RESOURCE_VNF,
+                            OperationProperties.AAI_SERVICE,
+                            OperationProperties.EVENT_ADDITIONAL_PARAMS,
+                            OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES);
+    // @formatter:on
+
     /**
      * Constructs the object.
      *
@@ -67,8 +108,21 @@ public class GrpcOperation extends OperationPartial {
      * @param config configuration for this operation
      */
     public GrpcOperation(ControlLoopOperationParams params, GrpcConfig config) {
-        super(params, config);
+        super(params, config, Collections.emptyList());
         this.config = config;
+
+        if (TargetType.PNF.equals(params.getTarget().getType())) {
+            aaiRequestor = this::getPnf;
+            aaiConverter = this::convertPnfToAaiProperties;
+        } else {
+            aaiRequestor = this::getCq;
+            aaiConverter = this::convertCqToAaiProperties;
+        }
+    }
+
+    @Override
+    public List<String> getPropertyNames() {
+        return (TargetType.PNF.equals(params.getTarget().getType()) ? PNF_PROPERTY_NAMES : VNF_PROPERTY_NAMES);
     }
 
     /**
@@ -80,16 +134,137 @@ public class GrpcOperation extends OperationPartial {
     }
 
     /**
-     * Ensures that A&AI customer query has been performed.
+     * Ensures that A&AI query has been performed, and runs the guard.
      */
     @Override
     @SuppressWarnings("unchecked")
     protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
+        if (params.isPreprocessed()) {
+            return null;
+        }
+
+        // run A&AI Query and Guard, in parallel
+        return allOf(aaiRequestor, this::startGuardAsync);
+    }
+
+    /**
+     * Requests the A&AI PNF data.
+     *
+     * @return a future to get the PNF data
+     */
+    private CompletableFuture<OperationOutcome> getPnf() {
+        ControlLoopOperationParams pnfParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
+                        .operation(AaiGetPnfOperation.NAME).payload(null).retry(null).timeoutSec(null).build();
+
+        return params.getContext().obtain(AaiGetPnfOperation.getKey(params.getTargetEntity()), pnfParams);
+    }
+
+    /**
+     * Requests the A&AI Custom Query data.
+     *
+     * @return a future to get the custom query data
+     */
+    private CompletableFuture<OperationOutcome> getCq() {
         ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
                         .operation(AaiCustomQueryOperation.NAME).payload(null).retry(null).timeoutSec(null).build();
 
-        // run Custom Query and Guard, in parallel
-        return allOf(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams), this::startGuardAsync);
+        return params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams);
+    }
+
+    /**
+     * Converts the A&AI PNF data to a map suitable for passing via the "aaiProperties"
+     * field in the CDS request.
+     *
+     * @return a map of the PNF data
+     */
+    private Map<String, String> convertPnfToAaiProperties() {
+        Map<String, String> result = this.getProperty(OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES);
+        if (result != null) {
+            return result;
+        }
+
+        // convert PNF data to a Map
+        Map<String, Object> source = Util.translateToMap(getFullName(), getPnfData());
+
+        result = new LinkedHashMap<>();
+
+        for (Entry<String, Object> ent : source.entrySet()) {
+            result.put(AAI_PNF_PREFIX + ent.getKey(), ent.getValue().toString());
+        }
+
+        return result;
+    }
+
+    /**
+     * Gets the PNF from the operation properties, if it exists, or from the context
+     * properties otherwise.
+     *
+     * @return the PNF item
+     */
+    protected Object getPnfData() {
+        Object pnf = getProperty(OperationProperties.AAI_PNF);
+        if (pnf != null) {
+            return pnf;
+        }
+
+        pnf = params.getContext().getProperty(AaiGetPnfOperation.getKey(params.getTargetEntity()));
+        if (pnf == null) {
+            throw new IllegalArgumentException("missing PNF data");
+        }
+
+        return pnf;
+    }
+
+    /**
+     * Converts the A&AI Custom Query data to a map suitable for passing via the
+     * "aaiProperties" field in the CDS request.
+     *
+     * @return a map of the custom query data
+     */
+    private Map<String, String> convertCqToAaiProperties() {
+        Map<String, String> result = this.getProperty(OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES);
+        if (result != null) {
+            return result;
+        }
+
+        result = new LinkedHashMap<>();
+
+        result.put(AAI_SERVICE_INSTANCE_ID_KEY, getServiceInstanceId());
+        result.put(AAI_VNF_ID_KEY, getVnfId());
+
+        return result;
+    }
+
+    protected String getServiceInstanceId() {
+        ServiceInstance serviceInstance = getProperty(OperationProperties.AAI_SERVICE);
+        if (serviceInstance != null) {
+            return serviceInstance.getServiceInstanceId();
+        }
+
+        AaiCqResponse aaicq = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY);
+
+        serviceInstance = aaicq.getServiceInstance();
+        if (serviceInstance == null) {
+            throw new IllegalArgumentException("Target service instance could not be found");
+        }
+
+        return serviceInstance.getServiceInstanceId();
+    }
+
+    protected String getVnfId() {
+        GenericVnf genericVnf = getProperty(OperationProperties.AAI_RESOURCE_VNF);
+        if (genericVnf != null) {
+            return genericVnf.getVnfId();
+        }
+
+        AaiCqResponse aaicq = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY);
+
+        genericVnf = aaicq.getGenericVnfByModelInvariantId(params.getTarget().getResourceID());
+        if (genericVnf == null) {
+            throw new IllegalArgumentException("Target generic vnf could not be found");
+        }
+
+        return genericVnf.getVnfId();
     }
 
     @Override
@@ -103,13 +278,26 @@ public class GrpcOperation extends OperationPartial {
     @Override
     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
 
+        /*
+         * construct the request first so that we don't have to clean up the "client" if
+         * an exception is thrown
+         */
+        ExecutionServiceInput request = constructRequest();
+
         CompletableFuture<OperationOutcome> future = new CompletableFuture<>();
+
         client = new CdsProcessorGrpcClient(new CdsActorServiceManager(outcome, future),
                         config.getCdsServerProperties());
 
-        ExecutionServiceInput request = constructRequest(params);
         client.sendRequest(request);
-        return future;
+
+        // arrange to shutdown the client when the request completes
+        PipelineControllerFuture<OperationOutcome> controller = new PipelineControllerFuture<>();
+
+        controller.wrap(future).whenCompleteAsync(controller.delayedComplete(), params.getExecutor())
+                        .whenCompleteAsync((arg1, arg2) -> client.close(), getBlockingExecutor());
+
+        return controller;
     }
 
     /**
@@ -117,10 +305,9 @@ public class GrpcOperation extends OperationPartial {
      * enriched parameters. TO-DO: Avoid leaking Exceptions to the Kie Session thread. TBD
      * item for Frankfurt release.
      *
-     * @param params the control loop parameters specifying the onset, payload, etc.
      * @return an ExecutionServiceInput instance.
      */
-    public ExecutionServiceInput constructRequest(ControlLoopOperationParams params) {
+    public ExecutionServiceInput constructRequest() {
 
         // For the current operational TOSCA policy model (yaml) CBA name and version are
         // embedded in the payload
@@ -151,11 +338,15 @@ public class GrpcOperation extends OperationPartial {
         // implementation to inject whatever AAI parameters are of interest to them.
         // E.g. For vFW usecase El-Alto inject service-instance-id, generic-vnf-id as
         // needed by CDS.
-        request.setAaiProperties(params.getContext().getEnrichment());
+        //
+        // Note: that is a future enhancement. For now, the actor is hard-coded to
+        // use the A&AI query result specific to the target type
+        request.setAaiProperties(aaiConverter.get());
 
         // Inject any additional event parameters that may be present in the onset event
-        if (params.getContext().getEvent().getAdditionalEventParams() != null) {
-            request.setAdditionalEventParams(params.getContext().getEvent().getAdditionalEventParams());
+        Map<String, String> additionalParams = getAdditionalEventParams();
+        if (additionalParams != null) {
+            request.setAdditionalEventParams(additionalParams);
         }
 
         Builder struct = Struct.newBuilder();
@@ -171,8 +362,7 @@ public class GrpcOperation extends OperationPartial {
 
         // Build CDS gRPC request common-header
         CommonHeader commonHeader = CommonHeader.newBuilder().setOriginatorId(CdsActorConstants.ORIGINATOR_ID)
-                        .setRequestId(params.getContext().getEvent().getRequestId().toString())
-                        .setSubRequestId(getSubRequestId()).build();
+                        .setRequestId(params.getRequestId().toString()).setSubRequestId(getSubRequestId()).build();
 
         // Build CDS gRPC request action-identifier
         ActionIdentifiers actionIdentifiers =
@@ -184,6 +374,14 @@ public class GrpcOperation extends OperationPartial {
                         .setPayload(struct.build()).build();
     }
 
+    protected Map<String, String> getAdditionalEventParams() {
+        if (containsProperty(OperationProperties.EVENT_ADDITIONAL_PARAMS)) {
+            return getProperty(OperationProperties.EVENT_ADDITIONAL_PARAMS);
+        }
+
+        return params.getContext().getEvent().getAdditionalEventParams();
+    }
+
     private Map<String, String> convertPayloadMap(Map<String, Object> payload) {
         Map<String, String> convertedPayload = new HashMap<>();
         for (Entry<String, Object> entry : payload.entrySet()) {