Remove Target and TargetType
[policy/models.git] / models-interactions / model-actors / actor.appc / src / main / java / org / onap / policy / controlloop / actor / appc / AppcOperation.java
index 8bc1a7f..70c7082 100644 (file)
@@ -23,19 +23,21 @@ package org.onap.policy.controlloop.actor.appc;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
 import org.onap.policy.appc.CommonHeader;
 import org.onap.policy.appc.Request;
 import org.onap.policy.appc.Response;
 import org.onap.policy.appc.ResponseCode;
+import org.onap.policy.common.utils.coder.Coder;
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.coder.StandardCoderInstantAsMillis;
 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation;
-import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperator;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
 import org.onap.policy.controlloop.actorserviceprovider.topic.SelectorKey;
-import org.onap.policy.controlloop.policy.PolicyResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -44,7 +46,7 @@ import org.slf4j.LoggerFactory;
  */
 public abstract class AppcOperation extends BidirectionalTopicOperation<Request, Response> {
     private static final Logger logger = LoggerFactory.getLogger(AppcOperation.class);
-    private static final StandardCoder coder = new StandardCoder();
+    private static final StandardCoder coder = new StandardCoderInstantAsMillis();
     public static final String VNF_ID_KEY = "generic-vnf.vnf-id";
 
     /**
@@ -60,10 +62,20 @@ public abstract class AppcOperation extends BidirectionalTopicOperation<Request,
      * Constructs the object.
      *
      * @param params operation parameters
-     * @param operator operator that created this operation
+     * @param config configuration for this operation
+     * @param propertyNames names of properties required by this operation
      */
-    public AppcOperation(ControlLoopOperationParams params, BidirectionalTopicOperator operator) {
-        super(params, operator, Response.class);
+    public AppcOperation(ControlLoopOperationParams params, BidirectionalTopicConfig config,
+                    List<String> propertyNames) {
+        super(params, config, Response.class, propertyNames);
+    }
+
+    /**
+     * Starts the GUARD.
+     */
+    @Override
+    protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
+        return startGuardAsync();
     }
 
     /**
@@ -78,9 +90,7 @@ public abstract class AppcOperation extends BidirectionalTopicOperation<Request,
         Request request = new Request();
         request.setCommonHeader(new CommonHeader());
         request.getCommonHeader().setRequestId(params.getRequestId());
-
-        // TODO ok to use UUID, or does it have to be the "attempt"?
-        request.getCommonHeader().setSubRequestId(UUID.randomUUID().toString());
+        request.getCommonHeader().setSubRequestId(getSubRequestId());
 
         request.setAction(getName());
 
@@ -104,10 +114,16 @@ public abstract class AppcOperation extends BidirectionalTopicOperation<Request,
      * @param source source from which to get the values
      * @param target where to place the decoded values
      */
-    private static void convertPayload(Map<String, String> source, Map<String, Object> target) {
-        for (Entry<String, String> ent : source.entrySet()) {
+    private static void convertPayload(Map<String, Object> source, Map<String, Object> target) {
+        for (Entry<String, Object> ent : source.entrySet()) {
+            Object value = ent.getValue();
+            if (value == null) {
+                target.put(ent.getKey(), null);
+                continue;
+            }
+
             try {
-                target.put(ent.getKey(), coder.decode(ent.getValue(), Object.class));
+                target.put(ent.getKey(), coder.decode(value.toString(), Object.class));
 
             } catch (CoderException e) {
                 logger.warn("cannot decode JSON value {}: {}", ent.getKey(), ent.getValue(), e);
@@ -120,13 +136,15 @@ public abstract class AppcOperation extends BidirectionalTopicOperation<Request,
      */
     @Override
     protected List<String> getExpectedKeyValues(int attempt, Request request) {
-        return List.of(request.getCommonHeader().getSubRequestId());
+        return List.of(getSubRequestId());
     }
 
     @Override
     protected Status detmStatus(String rawResponse, Response response) {
         if (response.getStatus() == null) {
-            throw new IllegalArgumentException("APP-C response is missing the response status");
+            // no status - this must be a request, not a response - just ignore it
+            logger.info("{}: ignoring request message for {}", getFullName(), params.getRequestId());
+            return Status.STILL_WAITING;
         }
 
         ResponseCode code = ResponseCode.toResponseCode(response.getStatus().getCode());
@@ -154,7 +172,9 @@ public abstract class AppcOperation extends BidirectionalTopicOperation<Request,
      * Sets the message to the status description, if available.
      */
     @Override
-    public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result, Response response) {
+    public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, Response response) {
+        outcome.setResponse(response);
+
         if (response.getStatus() == null || response.getStatus().getDescription() == null) {
             return setOutcome(outcome, result);
         }
@@ -163,4 +183,9 @@ public abstract class AppcOperation extends BidirectionalTopicOperation<Request,
         outcome.setMessage(response.getStatus().getDescription());
         return outcome;
     }
+
+    @Override
+    protected Coder getCoder() {
+        return coder;
+    }
 }