Fix nexus and sonar vulnerabilities
[policy/models.git] / models-interactions / model-actors / actor.appc / src / main / java / org / onap / policy / controlloop / actor / appc / AppcOperation.java
index 8bc1a7f..ffdbde8 100644 (file)
@@ -2,7 +2,8 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2023 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -23,19 +24,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 org.onap.aai.domain.yang.GenericVnf;
 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 +47,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,27 +63,26 @@ 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);
+    protected AppcOperation(ControlLoopOperationParams params, BidirectionalTopicConfig config,
+                    List<String> propertyNames) {
+        super(params, config, Response.class, propertyNames);
     }
 
     /**
      * Makes a request, given the target VNF. This is a support function for
      * {@link #makeRequest(int)}.
      *
-     * @param attempt attempt number
      * @param targetVnf target VNF
      * @return a new request
      */
-    protected Request makeRequest(int attempt, String targetVnf) {
-        Request request = new Request();
+    protected Request makeRequest(GenericVnf targetVnf) {
+        var 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());
 
@@ -92,7 +94,7 @@ public abstract class AppcOperation extends BidirectionalTopicOperation<Request,
         }
 
         // add/replace specific values
-        request.getPayload().put(VNF_ID_KEY, targetVnf);
+        request.getPayload().put(VNF_ID_KEY, targetVnf.getVnfId());
 
         return request;
     }
@@ -104,10 +106,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,41 +128,39 @@ 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());
+        var code = ResponseCode.toResponseCode(response.getStatus().getCode());
         if (code == null) {
             throw new IllegalArgumentException(
                             "unknown APPC-C response status code: " + response.getStatus().getCode());
         }
 
-        switch (code) {
-            case SUCCESS:
-                return Status.SUCCESS;
-            case FAILURE:
-                return Status.FAILURE;
-            case ERROR:
-            case REJECT:
-                throw new IllegalArgumentException("APP-C request was not accepted, code=" + code);
-            case ACCEPT:
-            default:
-                // awaiting a "final" response
-                return Status.STILL_WAITING;
-        }
+        return switch (code) {
+            case SUCCESS -> Status.SUCCESS;
+            case FAILURE -> Status.FAILURE;
+            case ERROR, REJECT -> throw new IllegalArgumentException("APP-C request was not accepted, code=" + code);
+            // awaiting a "final" response
+            default -> Status.STILL_WAITING;
+        };
     }
 
     /**
      * 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 +169,9 @@ public abstract class AppcOperation extends BidirectionalTopicOperation<Request,
         outcome.setMessage(response.getStatus().getDescription());
         return outcome;
     }
+
+    @Override
+    protected Coder getCoder() {
+        return coder;
+    }
 }