Make Actors event-agnostic
[policy/models.git] / models-interactions / model-actors / actor.cds / src / main / java / org / onap / policy / controlloop / actor / cds / GrpcOperation.java
index 2417812..44d5181 100644 (file)
@@ -40,22 +40,17 @@ 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;
-import org.onap.policy.aai.AaiConstants;
-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.common.utils.coder.StandardCoderObject;
-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.TargetType;
 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.
@@ -77,11 +72,6 @@ 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.
      */
@@ -91,13 +81,15 @@ public class GrpcOperation extends OperationPartial {
     // @formatter:off
     private static final List<String> PNF_PROPERTY_NAMES = List.of(
                             OperationProperties.AAI_PNF,
-                            OperationProperties.EVENT_ADDITIONAL_PARAMS);
+                            OperationProperties.EVENT_ADDITIONAL_PARAMS,
+                            OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES);
 
 
     private static final List<String> VNF_PROPERTY_NAMES = List.of(
-                            OperationProperties.AAI_MODEL_INVARIANT_GENERIC_VNF,
-                            OperationProperties.AAI_RESOURCE_SERVICE_INSTANCE,
-                            OperationProperties.EVENT_ADDITIONAL_PARAMS);
+                            OperationProperties.AAI_RESOURCE_VNF,
+                            OperationProperties.AAI_SERVICE,
+                            OperationProperties.EVENT_ADDITIONAL_PARAMS,
+                            OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES);
     // @formatter:on
 
     /**
@@ -110,18 +102,16 @@ public class GrpcOperation extends OperationPartial {
         super(params, config, Collections.emptyList());
         this.config = config;
 
-        if (TargetType.PNF.equals(params.getTarget().getType())) {
-            aaiRequestor = this::getPnf;
+        if (TargetType.PNF.equals(params.getTargetType())) {
             aaiConverter = this::convertPnfToAaiProperties;
         } else {
-            aaiRequestor = this::getCq;
-            aaiConverter = this::convertCqToAaiProperties;
+            aaiConverter = this::convertVnfToAaiProperties;
         }
     }
 
     @Override
     public List<String> getPropertyNames() {
-        return (TargetType.PNF.equals(params.getTarget().getType()) ? PNF_PROPERTY_NAMES : VNF_PROPERTY_NAMES);
+        return (TargetType.PNF.equals(params.getTargetType()) ? PNF_PROPERTY_NAMES : VNF_PROPERTY_NAMES);
     }
 
     /**
@@ -132,40 +122,6 @@ public class GrpcOperation extends OperationPartial {
         return (timeoutSec == null || timeoutSec == 0 ? config.getTimeoutMs() : super.getTimeoutMs(timeoutSec));
     }
 
-    /**
-     * Ensures that A&AI query has been performed, and runs the guard.
-     */
-    @Override
-    @SuppressWarnings("unchecked")
-    protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
-        // 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();
-
-        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.
@@ -173,11 +129,16 @@ public class GrpcOperation extends OperationPartial {
      * @return a map of the PNF data
      */
     private Map<String, String> convertPnfToAaiProperties() {
+        Map<String, String> result = getProperty(OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES);
+        if (result != null) {
+            return result;
+        }
+
         // convert PNF data to a Map
-        StandardCoderObject pnf = params.getContext().getProperty(AaiGetPnfOperation.getKey(params.getTargetEntity()));
-        Map<String, Object> source = Util.translateToMap(getFullName(), pnf);
+        Object pnfData = getRequiredProperty(OperationProperties.AAI_PNF, "PNF");
+        Map<String, Object> source = Util.translateToMap(getFullName(), pnfData);
 
-        Map<String, String> result = new LinkedHashMap<>();
+        result = new LinkedHashMap<>();
 
         for (Entry<String, Object> ent : source.entrySet()) {
             result.put(AAI_PNF_PREFIX + ent.getKey(), ent.getValue().toString());
@@ -187,32 +148,36 @@ public class GrpcOperation extends OperationPartial {
     }
 
     /**
-     * Converts the A&AI Custom Query data to a map suitable for passing via the
+     * Converts the A&AI VNF data to a map suitable for passing via the
      * "aaiProperties" field in the CDS request.
      *
-     * @return a map of the custom query data
+     * @return a map of the VNF data
      */
-    private Map<String, String> convertCqToAaiProperties() {
-        AaiCqResponse aaicq = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY);
-
-        Map<String, String> result = new LinkedHashMap<>();
-
-        ServiceInstance serviceInstance = aaicq.getServiceInstance();
-        if (serviceInstance == null) {
-            throw new IllegalArgumentException("Target service instance could not be found");
+    private Map<String, String> convertVnfToAaiProperties() {
+        Map<String, String> result = getProperty(OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES);
+        if (result != null) {
+            return result;
         }
 
-        GenericVnf genericVnf = aaicq.getGenericVnfByModelInvariantId(params.getTarget().getResourceID());
-        if (genericVnf == null) {
-            throw new IllegalArgumentException("Target generic vnf could not be found");
-        }
+        result = new LinkedHashMap<>();
 
-        result.put(AAI_SERVICE_INSTANCE_ID_KEY, serviceInstance.getServiceInstanceId());
-        result.put(AAI_VNF_ID_KEY, genericVnf.getVnfId());
+        result.put(AAI_SERVICE_INSTANCE_ID_KEY, getServiceInstanceId());
+        result.put(AAI_VNF_ID_KEY, getVnfId());
 
         return result;
     }
 
+    protected String getServiceInstanceId() {
+        ServiceInstance serviceInstance =
+                        getRequiredProperty(OperationProperties.AAI_SERVICE, "Target service instance");
+        return serviceInstance.getServiceInstanceId();
+    }
+
+    protected String getVnfId() {
+        GenericVnf genericVnf = getRequiredProperty(OperationProperties.AAI_RESOURCE_VNF, "Target generic vnf");
+        return genericVnf.getVnfId();
+    }
+
     @Override
     public void generateSubRequestId(int attempt) {
         setSubRequestId("0");
@@ -228,7 +193,7 @@ public class GrpcOperation extends OperationPartial {
          * construct the request first so that we don't have to clean up the "client" if
          * an exception is thrown
          */
-        ExecutionServiceInput request = constructRequest(params);
+        ExecutionServiceInput request = constructRequest();
 
         CompletableFuture<OperationOutcome> future = new CompletableFuture<>();
 
@@ -251,10 +216,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
@@ -291,8 +255,9 @@ public class GrpcOperation extends OperationPartial {
         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 = getProperty(OperationProperties.EVENT_ADDITIONAL_PARAMS);
+        if (additionalParams != null) {
+            request.setAdditionalEventParams(additionalParams);
         }
 
         Builder struct = Struct.newBuilder();