Remove Target and TargetType
[policy/models.git] / models-interactions / model-actors / actor.aai / src / main / java / org / onap / policy / controlloop / actor / aai / AaiCustomQueryOperation.java
index 2cc2a69..612fba9 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.onap.policy.controlloop.actor.aai;
 
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.concurrent.CompletableFuture;
@@ -28,7 +29,6 @@ import javax.ws.rs.client.Invocation.Builder;
 import javax.ws.rs.client.WebTarget;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
-import lombok.Getter;
 import org.apache.commons.lang3.StringUtils;
 import org.onap.policy.aai.AaiConstants;
 import org.onap.policy.aai.AaiCqResponse;
@@ -36,6 +36,8 @@ import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
 import org.onap.policy.common.utils.coder.StandardCoderObject;
 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.impl.HttpOperation;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
@@ -57,12 +59,11 @@ public class AaiCustomQueryOperation extends HttpOperation<String> {
     public static final String RESOURCE_LINK = "resource-link";
     public static final String RESULT_DATA = "result-data";
 
+    private static final List<String> PROPERTY_NAMES = List.of(OperationProperties.AAI_VSERVER_LINK);
+
     // TODO make this configurable
     private static final String PREFIX = "/aai/v16";
 
-    @Getter
-    private final String vserver;
-
     /**
      * Constructs the object.
      *
@@ -70,12 +71,21 @@ public class AaiCustomQueryOperation extends HttpOperation<String> {
      * @param config configuration for this operation
      */
     public AaiCustomQueryOperation(ControlLoopOperationParams params, HttpConfig config) {
-        super(params, config, String.class);
+        super(params, config, String.class, PROPERTY_NAMES);
+    }
 
-        this.vserver = params.getContext().getEnrichment().get(VSERVER_VSERVER_NAME);
-        if (StringUtils.isBlank(this.vserver)) {
+    /**
+     * Gets the vserver name from the enrichment data.
+     *
+     * @return the vserver name
+     */
+    protected String getVserver() {
+        String vserver = this.params.getContext().getEnrichment().get(VSERVER_VSERVER_NAME);
+        if (StringUtils.isBlank(vserver)) {
             throw new IllegalArgumentException("missing " + VSERVER_VSERVER_NAME + " in enrichment data");
         }
+
+        return vserver;
     }
 
     /**
@@ -83,6 +93,11 @@ public class AaiCustomQueryOperation extends HttpOperation<String> {
      */
     @Override
     protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
+        if (params.isPreprocessed()) {
+            return null;
+        }
+
+        String vserver = getVserver();
         ControlLoopOperationParams tenantParams =
                         params.toBuilder().actor(AaiConstants.ACTOR_NAME).operation(AaiGetTenantOperation.NAME)
                                         .targetEntity(vserver).payload(null).retry(null).timeoutSec(null).build();
@@ -117,9 +132,10 @@ public class AaiCustomQueryOperation extends HttpOperation<String> {
 
         String url = str.toString();
 
-        logMessage(EventType.OUT, CommInfrastructure.REST, url, request);
+        String strRequest = prettyPrint(request);
+        logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
 
-        Entity<Map<String, String>> entity = Entity.entity(request, MediaType.APPLICATION_JSON);
+        Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
 
         return handleResponse(outcome, url, callback -> webldr.async().put(entity, callback));
     }
@@ -137,16 +153,33 @@ public class AaiCustomQueryOperation extends HttpOperation<String> {
      * Constructs the custom query using the previously retrieved tenant data.
      */
     private Map<String, String> makeRequest() {
+        return Map.of("start", getVserverLink(), "query", "query/closed-loop");
+    }
+
+    /**
+     * Gets the vserver link, first checking the properties, and then the tenant data.
+     *
+     * @return the vserver link
+     */
+    protected String getVserverLink() {
+        String resourceLink = getProperty(OperationProperties.AAI_VSERVER_LINK);
+        if (resourceLink != null) {
+            return resourceLink;
+        }
+
+        String vserver = getVserver();
         StandardCoderObject tenant = params.getContext().getProperty(AaiGetTenantOperation.getKey(vserver));
+        if (tenant == null) {
+            throw new IllegalStateException("cannot perform custom query - cannot determine resource-link");
+        }
 
-        String resourceLink = tenant.getString(RESULT_DATA, 0, RESOURCE_LINK);
+        resourceLink = tenant.getString(RESULT_DATA, 0, RESOURCE_LINK);
         if (resourceLink == null) {
             throw new IllegalArgumentException("cannot perform custom query - no resource-link");
         }
 
         resourceLink = resourceLink.replace(PREFIX, "");
-
-        return Map.of("start", resourceLink, "query", "query/closed-loop");
+        return resourceLink;
     }
 
     @Override
@@ -154,6 +187,19 @@ public class AaiCustomQueryOperation extends HttpOperation<String> {
         return AaiUtil.makeHeaders(params);
     }
 
+    @Override
+    public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, Response rawResponse,
+                    String response) {
+
+        super.setOutcome(outcome, result, rawResponse, response);
+
+        if (response != null) {
+            outcome.setResponse(new AaiCqResponse(response));
+        }
+
+        return outcome;
+    }
+
     /**
      * Injects the response into the context.
      */
@@ -161,8 +207,10 @@ public class AaiCustomQueryOperation extends HttpOperation<String> {
     protected CompletableFuture<OperationOutcome> postProcessResponse(OperationOutcome outcome, String url,
                     Response rawResponse, String response) {
 
-        logger.info("{}: caching response for {}", getFullName(), params.getRequestId());
-        params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, new AaiCqResponse(response));
+        if (params.getContext() != null) {
+            logger.info("{}: caching response for {}", getFullName(), params.getRequestId());
+            params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, new AaiCqResponse(response));
+        }
 
         return super.postProcessResponse(outcome, url, rawResponse, response);
     }