Remove Target and TargetType
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / parameters / ControlLoopOperationParams.java
index 08aba81..0e4f09b 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.onap.policy.controlloop.actorserviceprovider.parameters;
 
+import java.util.Map;
 import java.util.UUID;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.Executor;
@@ -32,11 +33,13 @@ import lombok.Getter;
 import org.onap.policy.common.parameters.BeanValidationResult;
 import org.onap.policy.common.parameters.BeanValidator;
 import org.onap.policy.common.parameters.annotations.NotNull;
-import org.onap.policy.controlloop.ControlLoopOperation;
+import org.onap.policy.controlloop.VirtualControlLoopEvent;
 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
+import org.onap.policy.controlloop.actorserviceprovider.Operation;
+import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.TargetType;
 import org.onap.policy.controlloop.actorserviceprovider.Util;
 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
-import org.onap.policy.controlloop.policy.Policy;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -49,36 +52,91 @@ import org.slf4j.LoggerFactory;
 @AllArgsConstructor
 @EqualsAndHashCode
 public class ControlLoopOperationParams {
-
     private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationParams.class);
 
-    public static final String UNKNOWN = "-unknown-";
+    public static final String PARAMS_ENTITY_RESOURCEID = "resourceID";
+    public static final String PARAMS_ENTITY_MODEL_INVARIANT_ID = "modelInvariantId";
+    public static final String PARAMS_ENTITY_MODEL_VERSION_ID = "modelVersionId";
+    public static final String PARAMS_ENTITY_MODEL_NAME = "modelName";
+    public static final String PARAMS_ENTITY_MODEL_VERSION = "modelVersion";
+    public static final String PARAMS_ENTITY_MODEL_CUSTOMIZATION_ID = "modelCustomizationId";
 
+    /**
+     * Actor name.
+     */
+    @NotNull
+    private String actor;
 
     /**
-     * The actor service in which to find the actor/operation.
+     * Actor service in which to find the actor/operation.
      */
     @NotNull
     private ActorService actorService;
 
     /**
-     * The event for which the operation applies.
+     * Event for which the operation applies.
      */
-    @NotNull
+    // TODO to be removed
     private ControlLoopEventContext context;
 
     /**
-     * The executor to use to run the operation.
+     * If {@code null}, this value is extracted from the context.
+     */
+    private UUID requestId;
+
+    /**
+     * Executor to use to run the operation.
      */
     @NotNull
     @Builder.Default
     private Executor executor = ForkJoinPool.commonPool();
 
     /**
-     * The policy associated with the operation.
+     * Operation name.
      */
     @NotNull
-    private Policy policy;
+    private String operation;
+
+    /**
+     * Payload data for the request.
+     */
+    private Map<String, Object> payload;
+
+    /**
+     * {@code True} if the preprocessing steps have already been executed, {@code false}
+     * otherwise.
+     */
+    private boolean preprocessed;
+
+    /**
+     * Number of retries allowed, or {@code null} if no retries.
+     */
+    private Integer retry;
+
+    /**
+     * The Target Type information, extracted from the Policy. May be {@code null}, depending
+     * on the requirement of the operation to be invoked.
+     */
+    private TargetType targetType;
+
+    /**
+     * Target entitiy ids, extracted from the Policy. May be (@code null}, depending on
+     * the requirement of the operation to be invoked.
+     */
+    private Map<String, String> targetEntityIds;
+
+    /**
+     * Target entity.
+     */
+    // TODO to be removed
+    private String targetEntity;
+
+    /**
+     * Timeout, in seconds, or {@code null} if no timeout. Zero and negative values also
+     * imply no timeout.
+     */
+    @Builder.Default
+    private Integer timeoutSec = 300;
 
     /**
      * The function to invoke when the operation starts. This is optional.
@@ -87,7 +145,7 @@ public class ControlLoopOperationParams {
      * may happen if the current operation requires other operations to be performed first
      * (e.g., A&AI queries, guard checks).
      */
-    private Consumer<ControlLoopOperation> startCallback;
+    private Consumer<OperationOutcome> startCallback;
 
     /**
      * The function to invoke when the operation completes. This is optional.
@@ -96,21 +154,25 @@ public class ControlLoopOperationParams {
      * may happen if the current operation requires other operations to be performed first
      * (e.g., A&AI queries, guard checks).
      */
-    private Consumer<ControlLoopOperation> completeCallback;
+    private Consumer<OperationOutcome> completeCallback;
 
     /**
-     * Target entity.
+     * Starts the specified operation.
+     *
+     * @return a future that will return the result of the operation
+     * @throws IllegalArgumentException if the parameters are invalid
      */
-    @NotNull
-    private String target;
+    public CompletableFuture<OperationOutcome> start() {
+        return build().start();
+    }
 
     /**
-     * Starts the specified operation.
+     * Builds the specified operation.
      *
-     * @return a future that will return the result of the operation
+     * @return a new operation
      * @throws IllegalArgumentException if the parameters are invalid
      */
-    public CompletableFuture<ControlLoopOperation> start() {
+    public Operation build() {
         BeanValidationResult result = validate();
         if (!result.isValid()) {
             logger.warn("parameter error in operation {}.{} for {}:\n{}", getActor(), getOperation(), getRequestId(),
@@ -120,51 +182,50 @@ public class ControlLoopOperationParams {
 
         // @formatter:off
         return actorService
-                    .getActor(policy.getActor())
-                    .getOperator(policy.getRecipe())
-                    .startOperation(this);
+                    .getActor(getActor())
+                    .getOperator(getOperation())
+                    .buildOperation(this);
         // @formatter:on
     }
 
     /**
-     * Gets the name of the actor from the policy.
+     * Gets the requested ID of the associated event.
      *
-     * @return the actor name, or {@link #UNKNOWN} if no name is available
+     * @return the event's request ID, or {@code null} if no request ID is available
      */
-    public String getActor() {
-        return (policy == null || policy.getActor() == null ? UNKNOWN : policy.getActor());
-    }
+    public UUID getRequestId() {
+        if (requestId == null && context != null && context.getEvent() != null) {
+            // cache the request ID
+            requestId = context.getEvent().getRequestId();
+        }
 
-    /**
-     * Gets the name of the operation from the policy.
-     *
-     * @return the operation name, or {@link #UNKNOWN} if no name is available
-     */
-    public String getOperation() {
-        return (policy == null || policy.getRecipe() == null ? UNKNOWN : policy.getRecipe());
+        return requestId;
     }
 
     /**
-     * Gets the requested ID of the associated event.
+     * Makes an operation outcome, populating it from the parameters.
      *
-     * @return the event's request ID, or {@code null} if no request ID is available
+     * @return a new operation outcome
      */
-    public UUID getRequestId() {
-        return (context == null || context.getEvent() == null ? null : context.getEvent().getRequestId());
+    // TODO to be removed
+    public OperationOutcome makeOutcome() {
+        return makeOutcome(getTargetEntity());
     }
 
     /**
      * Makes an operation outcome, populating it from the parameters.
      *
+     * @param targetEntity the target entity
+     *
      * @return a new operation outcome
      */
-    public ControlLoopOperation makeOutcome() {
-        ControlLoopOperation operation = new ControlLoopOperation();
-        operation.setActor(getActor());
-        operation.setOperation(getOperation());
-        operation.setTarget(target);
+    public OperationOutcome makeOutcome(String targetEntity) {
+        OperationOutcome outcome = new OperationOutcome();
+        outcome.setActor(getActor());
+        outcome.setOperation(getOperation());
+        outcome.setTarget(targetEntity);
 
-        return operation;
+        return outcome;
     }
 
     /**
@@ -173,11 +234,11 @@ public class ControlLoopOperationParams {
      *
      * @param operation the operation that is being started
      */
-    public void callbackStarted(ControlLoopOperation operation) {
+    public void callbackStarted(OperationOutcome operation) {
         logger.info("started operation {}.{} for {}", operation.getActor(), operation.getOperation(), getRequestId());
 
         if (startCallback != null) {
-            Util.logException(() -> startCallback.accept(operation), "{}.{}: start-callback threw an exception for {}",
+            Util.runFunction(() -> startCallback.accept(operation), "{}.{}: start-callback threw an exception for {}",
                             operation.getActor(), operation.getOperation(), getRequestId());
         }
     }
@@ -188,12 +249,12 @@ public class ControlLoopOperationParams {
      *
      * @param operation the operation that is being started
      */
-    public void callbackCompleted(ControlLoopOperation operation) {
+    public void callbackCompleted(OperationOutcome operation) {
         logger.info("completed operation {}.{} outcome={} for {}", operation.getActor(), operation.getOperation(),
-                        operation.getOutcome(), getRequestId());
+                        operation.getResult(), getRequestId());
 
         if (completeCallback != null) {
-            Util.logException(() -> completeCallback.accept(operation),
+            Util.runFunction(() -> completeCallback.accept(operation),
                             "{}.{}: complete-callback threw an exception for {}", operation.getActor(),
                             operation.getOperation(), getRequestId());
         }
@@ -205,6 +266,34 @@ public class ControlLoopOperationParams {
      * @return the validation result
      */
     public BeanValidationResult validate() {
-        return new BeanValidator().validateTop(ControlLoopOperationParams.class.getSimpleName(), this);
+        BeanValidationResult result =
+                        new BeanValidator().validateTop(ControlLoopOperationParams.class.getSimpleName(), this);
+
+        // validate that we have a request ID, or that we can get it from the context's
+        // event
+
+        if (context == null) {
+            // no context specified - invoker must provide a request ID then
+            result.validateNotNull("requestId", requestId);
+
+        } else if (requestId == null) {
+            // have a context, but no request ID - check the context's event for the
+            // request ID
+            BeanValidationResult contextResult = new BeanValidationResult("context", context);
+            VirtualControlLoopEvent event = context.getEvent();
+            contextResult.validateNotNull("event", event);
+
+            if (event != null) {
+                // cache the request id for later use
+                BeanValidationResult eventResult = new BeanValidationResult("event", event);
+                eventResult.validateNotNull("requestId", event.getRequestId());
+
+                contextResult.addResult(eventResult);
+            }
+
+            result.addResult(contextResult);
+        }
+
+        return result;
     }
 }