Modify Actors to use properties when provided
[policy/models.git] / models-interactions / model-actors / actor.so / src / main / java / org / onap / policy / controlloop / actor / so / VfModuleDelete.java
index 5134d58..0ff833c 100644 (file)
@@ -28,6 +28,7 @@ import java.net.http.HttpResponse;
 import java.net.http.HttpResponse.BodyHandlers;
 import java.nio.charset.StandardCharsets;
 import java.util.Base64;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.concurrent.CompletableFuture;
@@ -45,15 +46,16 @@ import org.onap.policy.aai.AaiCqResponse;
 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
 import org.onap.policy.common.endpoints.http.client.HttpClient;
 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
-import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
-import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
 import org.onap.policy.controlloop.actorserviceprovider.pipeline.PipelineControllerFuture;
 import org.onap.policy.so.SoModelInfo;
 import org.onap.policy.so.SoOperationType;
 import org.onap.policy.so.SoRequest;
 import org.onap.policy.so.SoRequestDetails;
+import org.onap.policy.so.SoResponse;
 
 /**
  * Operation to delete a VF Module. This gets the VF count from the A&AI Custom Query
@@ -66,14 +68,23 @@ public class VfModuleDelete extends SoOperation {
 
     private static final String PATH_PREFIX = "/";
 
+    // @formatter:off
+    private static final List<String> PROPERTY_NAMES = List.of(
+                            OperationProperties.AAI_SERVICE,
+                            OperationProperties.AAI_VNF,
+                            OperationProperties.AAI_DEFAULT_CLOUD_REGION,
+                            OperationProperties.AAI_DEFAULT_TENANT,
+                            OperationProperties.DATA_VF_COUNT);
+    // @formatter:on
+
     /**
      * Constructs the object.
      *
      * @param params operation parameters
      * @param config configuration for this operation
      */
-    public VfModuleDelete(ControlLoopOperationParams params, HttpConfig config) {
-        super(params, config);
+    public VfModuleDelete(ControlLoopOperationParams params, HttpPollingConfig config) {
+        super(params, config, PROPERTY_NAMES);
 
         // ensure we have the necessary parameters
         validateTarget();
@@ -85,6 +96,9 @@ public class VfModuleDelete extends SoOperation {
     @Override
     @SuppressWarnings("unchecked")
     protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
+        if (params.isPreprocessed()) {
+            return null;
+        }
 
         // need the VF count
         ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
@@ -112,19 +126,20 @@ public class VfModuleDelete extends SoOperation {
     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
 
         // starting a whole new attempt - reset the count
-        resetGetCount();
+        resetPollCount();
 
         Pair<String, SoRequest> pair = makeRequest();
         SoRequest request = pair.getRight();
         String url = getPath() + pair.getLeft();
 
-        logMessage(EventType.OUT, CommInfrastructure.REST, url, request);
+        String strRequest = prettyPrint(request);
+        logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
 
         Map<String, Object> headers = createSimpleHeaders();
 
         // @formatter:off
         return handleResponse(outcome, url,
-            callback -> delete(url, headers, MediaType.APPLICATION_JSON, request, callback));
+            callback -> delete(url, headers, MediaType.APPLICATION_JSON, strRequest, callback));
         // @formatter:on
     }
 
@@ -133,7 +148,6 @@ public class VfModuleDelete extends SoOperation {
      * HttpClient, as the JerseyClient does not support it. This will add the content-type
      * and authorization headers, so they should not be included within "headers".
      *
-     * @param <Q> request type
      * @param uri URI suffix, to be appended to the URI prefix
      * @param headers headers to be included
      * @param contentType content type of the request
@@ -142,13 +156,10 @@ public class VfModuleDelete extends SoOperation {
      * @return a future to await the response. Note: it's untested whether canceling this
      *         future will actually cancel the underlying HTTP request
      */
-    protected <Q> CompletableFuture<Response> delete(String uri, Map<String, Object> headers, String contentType,
-                    Q request, InvocationCallback<Response> callback) {
+    protected CompletableFuture<Response> delete(String uri, Map<String, Object> headers, String contentType,
+                    String request, InvocationCallback<Response> callback) {
         // TODO move to HttpOperation
 
-        // make sure we can encode it before going any further
-        final String body = encodeRequest(request);
-
         final String url = getClient().getBaseUrl() + uri;
 
         Builder builder = HttpRequest.newBuilder(URI.create(url));
@@ -161,14 +172,14 @@ public class VfModuleDelete extends SoOperation {
 
         PipelineControllerFuture<Response> controller = new PipelineControllerFuture<>();
 
-        HttpRequest req = builder.method("DELETE", BodyPublishers.ofString(body)).build();
+        HttpRequest req = builder.method("DELETE", BodyPublishers.ofString(request)).build();
 
         CompletableFuture<HttpResponse<String>> future = makeHttpClient().sendAsync(req, BodyHandlers.ofString());
 
         // propagate "cancel" to the future
         controller.add(future);
 
-        future.thenApply(response -> new RestManagerResponse(response.statusCode(), response.body(), makeCoder()))
+        future.thenApply(response -> new RestManagerResponse(response.statusCode(), response.body(), getCoder()))
                         .whenComplete((resp, thrown) -> {
                             if (thrown != null) {
                                 callback.failed(thrown);
@@ -182,26 +193,6 @@ public class VfModuleDelete extends SoOperation {
         return controller;
     }
 
-    /**
-     * Encodes a request.
-     *
-     * @param <Q> request type
-     * @param request request to be encoded
-     * @return the encoded request
-     */
-    protected <Q> String encodeRequest(Q request) {
-        // TODO move to HttpOperation
-        try {
-            if (request instanceof String) {
-                return request.toString();
-            } else {
-                return makeCoder().encode(request);
-            }
-        } catch (CoderException e) {
-            throw new IllegalArgumentException("cannot encode request", e);
-        }
-    }
-
     /**
      * Adds the authorization header to the HTTP request, if configured.
      *
@@ -226,12 +217,20 @@ public class VfModuleDelete extends SoOperation {
         return builder.header("Authorization", "Basic " + encoded);
     }
 
+
     /**
-     * Decrements the VF count that's stored in the context.
+     * Decrements the VF count that's stored in the context, if the request was
+     * successful.
      */
     @Override
-    protected void successfulCompletion() {
-        setVfCount(getVfCount() - 1);
+    protected Status detmStatus(Response rawResponse, SoResponse response) {
+        Status status = super.detmStatus(rawResponse, response);
+
+        if (status == Status.SUCCESS) {
+            setVfCount(getVfCount() - 1);
+        }
+
+        return status;
     }
 
     /**
@@ -240,12 +239,11 @@ public class VfModuleDelete extends SoOperation {
      * @return a pair containing the request URL and the new request
      */
     protected Pair<String, SoRequest> makeRequest() {
-        final AaiCqResponse aaiCqResponse = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY);
         final SoModelInfo soModelInfo = prepareSoModelInfo();
-        final GenericVnf vnfItem = getVnfItem(aaiCqResponse, soModelInfo);
-        final ServiceInstance vnfServiceItem = getServiceInstance(aaiCqResponse);
-        final Tenant tenantItem = getDefaultTenant(aaiCqResponse);
-        final CloudRegion cloudRegionItem = getDefaultCloudRegion(aaiCqResponse);
+        final GenericVnf vnfItem = getVnfItem(soModelInfo);
+        final ServiceInstance vnfServiceItem = getServiceInstance();
+        final Tenant tenantItem = getDefaultTenant();
+        final CloudRegion cloudRegionItem = getDefaultCloudRegion();
 
         SoRequest request = new SoRequest();
         request.setOperationType(SoOperationType.DELETE_VF_MODULE);