Make targetEntity a property
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / parameters / ControlLoopOperationParams.java
index 57fce40..66573f3 100644 (file)
@@ -33,7 +33,9 @@ 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.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.Util;
 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
@@ -67,9 +69,14 @@ public class ControlLoopOperationParams {
     /**
      * Event for which the operation applies.
      */
-    @NotNull
+    // TODO to be removed
     private ControlLoopEventContext context;
 
+    /**
+     * If {@code null}, this value is extracted from the context.
+     */
+    private UUID requestId;
+
     /**
      * Executor to use to run the operation.
      */
@@ -86,7 +93,13 @@ public class ControlLoopOperationParams {
     /**
      * Payload data for the request.
      */
-    private Map<String, String> payload;
+    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.
@@ -94,15 +107,15 @@ public class ControlLoopOperationParams {
     private Integer retry;
 
     /**
-     * The entity's target information. May be {@code null}, depending on the requirement
-     * of the operation to be invoked.
+     * The Target information, extracted from the Policy. May be {@code null}, depending
+     * on the requirement of the operation to be invoked.
      */
     private Target target;
 
     /**
      * Target entity.
      */
-    @NotNull
+    // TODO to be removed
     private String targetEntity;
 
     /**
@@ -137,6 +150,16 @@ public class ControlLoopOperationParams {
      * @throws IllegalArgumentException if the parameters are invalid
      */
     public CompletableFuture<OperationOutcome> start() {
+        return build().start();
+    }
+
+    /**
+     * Builds the specified operation.
+     *
+     * @return a new operation
+     * @throws IllegalArgumentException if the parameters are invalid
+     */
+    public Operation build() {
         BeanValidationResult result = validate();
         if (!result.isValid()) {
             logger.warn("parameter error in operation {}.{} for {}:\n{}", getActor(), getOperation(), getRequestId(),
@@ -148,7 +171,7 @@ public class ControlLoopOperationParams {
         return actorService
                     .getActor(getActor())
                     .getOperator(getOperation())
-                    .startOperation(this);
+                    .buildOperation(this);
         // @formatter:on
     }
 
@@ -158,7 +181,12 @@ public class ControlLoopOperationParams {
      * @return the event's request ID, or {@code null} if no request ID is available
      */
     public UUID getRequestId() {
-        return (context == null || context.getEvent() == null ? null : context.getEvent().getRequestId());
+        if (requestId == null && context != null && context.getEvent() != null) {
+            // cache the request ID
+            requestId = context.getEvent().getRequestId();
+        }
+
+        return requestId;
     }
 
     /**
@@ -166,7 +194,19 @@ public class ControlLoopOperationParams {
      *
      * @return a new operation outcome
      */
+    // 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 OperationOutcome makeOutcome(String targetEntity) {
         OperationOutcome outcome = new OperationOutcome();
         outcome.setActor(getActor());
         outcome.setOperation(getOperation());
@@ -213,6 +253,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;
     }
 }