Modify Actors to use properties when provided
[policy/models.git] / models-interactions / model-actors / actor.aai / src / main / java / org / onap / policy / controlloop / actor / aai / AaiCustomQueryOperation.java
index b009288..c8e0870 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,7 @@ 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.impl.HttpOperation;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
@@ -57,12 +58,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 +70,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,11 +92,16 @@ 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(AaiGetOperation.TENANT)
+                        params.toBuilder().actor(AaiConstants.ACTOR_NAME).operation(AaiGetTenantOperation.NAME)
                                         .targetEntity(vserver).payload(null).retry(null).timeoutSec(null).build();
 
-        return params.getContext().obtain(AaiGetOperation.getTenantKey(vserver), tenantParams);
+        return params.getContext().obtain(AaiGetTenantOperation.getKey(vserver), tenantParams);
     }
 
     @Override
@@ -117,9 +131,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 +152,33 @@ public class AaiCustomQueryOperation extends HttpOperation<String> {
      * Constructs the custom query using the previously retrieved tenant data.
      */
     private Map<String, String> makeRequest() {
-        StandardCoderObject tenant = params.getContext().getProperty(AaiGetOperation.getTenantKey(vserver));
+        return Map.of("start", getVserverLink(), "query", "query/closed-loop");
+    }
 
-        String resourceLink = tenant.getString(RESULT_DATA, 0, RESOURCE_LINK);
+    /**
+     * 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");
+        }
+
+        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