Remove Target and TargetType
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / impl / OperationPartial.java
index b280216..c81575f 100644 (file)
@@ -23,6 +23,7 @@ package org.onap.policy.controlloop.actorserviceprovider.impl;
 import java.util.ArrayDeque;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
@@ -52,10 +53,11 @@ import org.onap.policy.controlloop.ControlLoopOperation;
 import org.onap.policy.controlloop.actorserviceprovider.CallbackManager;
 import org.onap.policy.controlloop.actorserviceprovider.Operation;
 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
+import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.OperatorConfig;
 import org.onap.policy.controlloop.actorserviceprovider.pipeline.PipelineControllerFuture;
-import org.onap.policy.controlloop.policy.PolicyResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -103,31 +105,60 @@ public abstract class OperationPartial implements Operation {
     @Setter(AccessLevel.PROTECTED)
     private String subRequestId;
 
+    @Getter
+    private final List<String> propertyNames;
+
+    /**
+     * Values for the properties identified by {@link #getPropertyNames()}.
+     */
+    private final Map<String, Object> properties = new HashMap<>();
+
 
     /**
      * Constructs the object.
      *
      * @param params operation parameters
      * @param config configuration for this operation
+     * @param propertyNames names of properties required by this operation
      */
-    public OperationPartial(ControlLoopOperationParams params, OperatorConfig config) {
+    public OperationPartial(ControlLoopOperationParams params, OperatorConfig config, List<String> propertyNames) {
         this.params = params;
         this.config = config;
         this.fullName = params.getActor() + "." + params.getOperation();
+        this.propertyNames = propertyNames;
     }
 
     public Executor getBlockingExecutor() {
         return config.getBlockingExecutor();
     }
 
+    @Override
     public String getActorName() {
         return params.getActor();
     }
 
+    @Override
     public String getName() {
         return params.getOperation();
     }
 
+    @Override
+    public boolean containsProperty(String name) {
+        return properties.containsKey(name);
+    }
+
+    @Override
+    public void setProperty(String name, Object value) {
+        logger.info("{}: set property {}={}", getFullName(), name, value);
+        properties.put(name, value);
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public <T> T getProperty(String name) {
+        return (T) properties.get(name);
+    }
+
     @Override
     public CompletableFuture<OperationOutcome> start() {
         // allocate a controller for the entire operation
@@ -187,12 +218,12 @@ public abstract class OperationPartial implements Operation {
             // propagate "stop" to the callbacks
             controller.add(callbacks);
 
-            final OperationOutcome outcome2 = params.makeOutcome();
+            final OperationOutcome outcome2 = params.makeOutcome(getTargetEntity());
 
             // TODO need a FAILURE_MISSING_DATA (e.g., A&AI)
 
             outcome2.setFinalOutcome(true);
-            outcome2.setResult(PolicyResult.FAILURE_GUARD);
+            outcome2.setResult(OperationResult.FAILURE_GUARD);
             outcome2.setMessage(outcome != null ? outcome.getMessage() : null);
 
             // @formatter:off
@@ -236,6 +267,10 @@ public abstract class OperationPartial implements Operation {
      *         {@code null} if this operation has no guard
      */
     protected CompletableFuture<OperationOutcome> startGuardAsync() {
+        if (params.isPreprocessed()) {
+            return null;
+        }
+
         // get the guard payload
         Map<String, Object> payload = makeGuardPayload();
 
@@ -253,10 +288,11 @@ public abstract class OperationPartial implements Operation {
      * @return a new guard payload
      */
     protected Map<String, Object> makeGuardPayload() {
+        // TODO delete this once preprocessing is done by the application
         Map<String, Object> guard = new LinkedHashMap<>();
         guard.put("actor", params.getActor());
         guard.put("operation", params.getOperation());
-        guard.put("target", params.getTargetEntity());
+        guard.put("target", getTargetEntity());
         guard.put("requestId", params.getRequestId());
 
         String clname = params.getContext().getEvent().getClosedLoopControlName();
@@ -289,6 +325,7 @@ public abstract class OperationPartial implements Operation {
 
     /**
      * Generates and sets {@link #subRequestId} to a new subrequest ID.
+     *
      * @param attempt attempt number, typically starting with 1
      */
     public void generateSubRequestId(int attempt) {
@@ -309,7 +346,7 @@ public abstract class OperationPartial implements Operation {
         logger.info("{}: start operation attempt {} for {}", getFullName(), attempt, params.getRequestId());
 
         final Executor executor = params.getExecutor();
-        final OperationOutcome outcome = params.makeOutcome();
+        final OperationOutcome outcome = params.makeOutcome(getTargetEntity());
         final CallbackManager callbacks = new CallbackManager();
 
         // this operation attempt gets its own controller
@@ -357,7 +394,7 @@ public abstract class OperationPartial implements Operation {
      * @return {@code true} if the outcome was successful
      */
     protected boolean isSuccess(OperationOutcome outcome) {
-        return (outcome != null && outcome.getResult() == PolicyResult.SUCCESS);
+        return (outcome != null && outcome.getResult() == OperationResult.SUCCESS);
     }
 
     /**
@@ -368,7 +405,7 @@ public abstract class OperationPartial implements Operation {
      *         <i>and</i> was associated with this operator, {@code false} otherwise
      */
     protected boolean isActorFailed(OperationOutcome outcome) {
-        return (isSameOperation(outcome) && outcome.getResult() == PolicyResult.FAILURE);
+        return (isSameOperation(outcome) && outcome.getResult() == OperationResult.FAILURE);
     }
 
     /**
@@ -440,7 +477,7 @@ public abstract class OperationPartial implements Operation {
                 outcome = origOutcome;
             } else {
                 logger.warn("{}: null outcome; treating as a failure for {}", getFullName(), params.getRequestId());
-                outcome = this.setOutcome(params.makeOutcome(), PolicyResult.FAILURE);
+                outcome = this.setOutcome(params.makeOutcome(getTargetEntity()), OperationResult.FAILURE);
             }
 
             // ensure correct actor/operation
@@ -448,7 +485,7 @@ public abstract class OperationPartial implements Operation {
             outcome.setOperation(getName());
 
             // determine if we should retry, based on the result
-            if (outcome.getResult() != PolicyResult.FAILURE) {
+            if (outcome.getResult() != OperationResult.FAILURE) {
                 // do not retry success or other failure types (e.g., exception)
                 outcome.setFinalOutcome(true);
                 return outcome;
@@ -469,7 +506,7 @@ public abstract class OperationPartial implements Operation {
                  * FAILURE_RETRIES
                  */
                 logger.info("operation {} retries exhausted for {}", getFullName(), params.getRequestId());
-                outcome.setResult(PolicyResult.FAILURE_RETRIES);
+                outcome.setResult(OperationResult.FAILURE_RETRIES);
                 outcome.setFinalOutcome(true);
             }
 
@@ -539,7 +576,7 @@ public abstract class OperationPartial implements Operation {
     private Function<Throwable, OperationOutcome> fromException(String type) {
 
         return thrown -> {
-            OperationOutcome outcome = params.makeOutcome();
+            OperationOutcome outcome = params.makeOutcome(getTargetEntity());
 
             if (thrown instanceof CancellationException || thrown.getCause() instanceof CancellationException) {
                 // do not include exception in the message, as it just clutters the log
@@ -954,7 +991,8 @@ public abstract class OperationPartial implements Operation {
      * @return the updated operation
      */
     public OperationOutcome setOutcome(OperationOutcome operation, Throwable thrown) {
-        PolicyResult result = (isTimeout(thrown) ? PolicyResult.FAILURE_TIMEOUT : PolicyResult.FAILURE_EXCEPTION);
+        OperationResult result = (isTimeout(thrown) ? OperationResult.FAILURE_TIMEOUT
+                : OperationResult.FAILURE_EXCEPTION);
         return setOutcome(operation, result);
     }
 
@@ -965,10 +1003,10 @@ public abstract class OperationPartial implements Operation {
      * @param result result of the operation
      * @return the updated operation
      */
-    public OperationOutcome setOutcome(OperationOutcome operation, PolicyResult result) {
+    public OperationOutcome setOutcome(OperationOutcome operation, OperationResult result) {
         logger.trace("{}: set outcome {} for {}", getFullName(), result, params.getRequestId());
         operation.setResult(result);
-        operation.setMessage(result == PolicyResult.SUCCESS ? ControlLoopOperation.SUCCESS_MSG
+        operation.setMessage(result == OperationResult.SUCCESS ? ControlLoopOperation.SUCCESS_MSG
                         : ControlLoopOperation.FAILED_MSG);
 
         return operation;
@@ -1057,6 +1095,16 @@ public abstract class OperationPartial implements Operation {
         return DEFAULT_RETRY_WAIT_MS;
     }
 
+    /**
+     * Gets the target entity, first trying the properties and then the parameters.
+     *
+     * @return the target entity
+     */
+    protected String getTargetEntity() {
+        String targetEntity = getProperty(OperationProperties.AAI_TARGET_ENTITY);
+        return (targetEntity != null ? targetEntity : params.getTargetEntity());
+    }
+
     /**
      * Gets the operation timeout.
      *