Remove Target and TargetType
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / impl / HttpOperation.java
index e83fe8c..c75ca1d 100644 (file)
 package org.onap.policy.controlloop.actorserviceprovider.impl;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.Executor;
 import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
 import java.util.function.Function;
 import javax.ws.rs.client.InvocationCallback;
 import javax.ws.rs.core.Response;
@@ -35,11 +37,12 @@ import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
 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.OperationResult;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
 import org.onap.policy.controlloop.actorserviceprovider.pipeline.PipelineControllerFuture;
-import org.onap.policy.controlloop.policy.PolicyResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -52,6 +55,13 @@ import org.slf4j.LoggerFactory;
 public abstract class HttpOperation<T> extends OperationPartial {
     private static final Logger logger = LoggerFactory.getLogger(HttpOperation.class);
 
+    /**
+     * Response status.
+     */
+    public enum Status {
+        SUCCESS, FAILURE, STILL_WAITING
+    }
+
     /**
      * Configuration for this operation.
      */
@@ -62,6 +72,18 @@ public abstract class HttpOperation<T> extends OperationPartial {
      */
     private final Class<T> responseClass;
 
+    /**
+     * {@code True} to use polling, {@code false} otherwise.
+     */
+    @Getter
+    private boolean usePolling;
+
+    /**
+     * Number of polls issued so far, on the current operation attempt.
+     */
+    @Getter
+    private int pollCount;
+
 
     /**
      * Constructs the object.
@@ -69,13 +91,26 @@ public abstract class HttpOperation<T> extends OperationPartial {
      * @param params operation parameters
      * @param config configuration for this operation
      * @param clazz response class
+     * @param propertyNames names of properties required by this operation
      */
-    public HttpOperation(ControlLoopOperationParams params, HttpConfig config, Class<T> clazz) {
-        super(params, config);
+    public HttpOperation(ControlLoopOperationParams params, HttpConfig config, Class<T> clazz,
+                    List<String> propertyNames) {
+        super(params, config, propertyNames);
         this.config = config;
         this.responseClass = clazz;
     }
 
+    /**
+     * Indicates that polling should be used.
+     */
+    protected void setUsePolling() {
+        if (!(config instanceof HttpPollingConfig)) {
+            throw new IllegalStateException("cannot poll without polling parameters");
+        }
+
+        usePolling = true;
+    }
+
     public HttpClient getClient() {
         return config.getClient();
     }
@@ -122,6 +157,17 @@ public abstract class HttpOperation<T> extends OperationPartial {
         return (getClient().getBaseUrl() + getPath());
     }
 
+    /**
+     * Resets the polling count
+     *
+     * <p/>
+     * Note: This should be invoked at the start of each operation (i.e., in
+     * {@link #startOperationAsync(int, OperationOutcome)}.
+     */
+    protected void resetPollCount() {
+        pollCount = 0;
+    }
+
     /**
      * Arranges to handle a response.
      *
@@ -177,7 +223,7 @@ public abstract class HttpOperation<T> extends OperationPartial {
 
         logger.info("{}.{}: response received for {}", params.getActor(), params.getOperation(), params.getRequestId());
 
-        String strResponse = HttpClient.getBody(rawResponse, String.class);
+        String strResponse = rawResponse.readEntity(String.class);
 
         logMessage(EventType.IN, CommInfrastructure.REST, url, strResponse);
 
@@ -186,7 +232,7 @@ public abstract class HttpOperation<T> extends OperationPartial {
             response = responseClass.cast(strResponse);
         } else {
             try {
-                response = makeCoder().decode(strResponse, responseClass);
+                response = getCoder().decode(strResponse, responseClass);
             } catch (CoderException e) {
                 logger.warn("{}.{} cannot decode response for {}", params.getActor(), params.getOperation(),
                                 params.getRequestId(), e);
@@ -197,11 +243,12 @@ public abstract class HttpOperation<T> extends OperationPartial {
         if (!isSuccess(rawResponse, response)) {
             logger.info("{}.{} request failed with http error code {} for {}", params.getActor(), params.getOperation(),
                             rawResponse.getStatus(), params.getRequestId());
-            return CompletableFuture.completedFuture(setOutcome(outcome, PolicyResult.FAILURE, rawResponse, response));
+            return CompletableFuture.completedFuture(
+                    setOutcome(outcome, OperationResult.FAILURE, rawResponse, response));
         }
 
         logger.info("{}.{} request succeeded for {}", params.getActor(), params.getOperation(), params.getRequestId());
-        setOutcome(outcome, PolicyResult.SUCCESS, rawResponse, response);
+        setOutcome(outcome, OperationResult.SUCCESS, rawResponse, response);
         return postProcessResponse(outcome, url, rawResponse, response);
     }
 
@@ -214,9 +261,10 @@ public abstract class HttpOperation<T> extends OperationPartial {
      * @param response decoded response
      * @return the updated operation
      */
-    public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result, Response rawResponse,
+    public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, Response rawResponse,
                     T response) {
 
+        outcome.setResponse(response);
         return setOutcome(outcome, result);
     }
 
@@ -233,7 +281,88 @@ public abstract class HttpOperation<T> extends OperationPartial {
     protected CompletableFuture<OperationOutcome> postProcessResponse(OperationOutcome outcome, String url,
                     Response rawResponse, T response) {
 
-        return CompletableFuture.completedFuture(outcome);
+        if (!usePolling) {
+            // doesn't use polling - just return the completed future
+            return CompletableFuture.completedFuture(outcome);
+        }
+
+        HttpPollingConfig cfg = (HttpPollingConfig) config;
+
+        switch (detmStatus(rawResponse, response)) {
+            case SUCCESS:
+                logger.info("{}.{} request succeeded for {}", params.getActor(), params.getOperation(),
+                                params.getRequestId());
+                return CompletableFuture
+                                .completedFuture(setOutcome(outcome, OperationResult.SUCCESS, rawResponse, response));
+
+            case FAILURE:
+                logger.info("{}.{} request failed for {}", params.getActor(), params.getOperation(),
+                                params.getRequestId());
+                return CompletableFuture
+                                .completedFuture(setOutcome(outcome, OperationResult.FAILURE, rawResponse, response));
+
+            case STILL_WAITING:
+            default:
+                logger.info("{}.{} request incomplete for {}", params.getActor(), params.getOperation(),
+                                params.getRequestId());
+                break;
+        }
+
+        // still incomplete
+
+        // see if the limit for the number of polls has been reached
+        if (pollCount++ >= cfg.getMaxPolls()) {
+            logger.warn("{}: execeeded 'poll' limit {} for {}", getFullName(), cfg.getMaxPolls(),
+                            params.getRequestId());
+            setOutcome(outcome, OperationResult.FAILURE_TIMEOUT);
+            return CompletableFuture.completedFuture(outcome);
+        }
+
+        // sleep and then poll
+        Function<Void, CompletableFuture<OperationOutcome>> doPoll = unused -> issuePoll(outcome);
+        return sleep(getPollWaitMs(), TimeUnit.MILLISECONDS).thenComposeAsync(doPoll);
+    }
+
+    /**
+     * Polls to see if the original request is complete. This method polls using an HTTP
+     * "get" request whose URL is constructed by appending the extracted "poll ID" to the
+     * poll path from the configuration data.
+     *
+     * @param outcome outcome to be populated with the response
+     * @return a future that can be used to cancel the poll or await its response
+     */
+    protected CompletableFuture<OperationOutcome> issuePoll(OperationOutcome outcome) {
+        String path = getPollingPath();
+        String url = getClient().getBaseUrl() + path;
+
+        logger.debug("{}: 'poll' count {} for {}", getFullName(), pollCount, params.getRequestId());
+
+        logMessage(EventType.OUT, CommInfrastructure.REST, url, null);
+
+        return handleResponse(outcome, url, callback -> getClient().get(callback, path, null));
+    }
+
+    /**
+     * Determines the status of the response. This particular method simply throws an
+     * exception.
+     *
+     * @param rawResponse raw response
+     * @param response decoded response
+     * @return the status of the response
+     */
+    protected Status detmStatus(Response rawResponse, T response) {
+        throw new UnsupportedOperationException("cannot determine response status");
+    }
+
+    /**
+     * Gets the URL to use when polling. Typically, this is some unique ID appended to the
+     * polling path found within the configuration data. This particular method simply
+     * returns the polling path from the configuration data.
+     *
+     * @return the URL to use when polling
+     */
+    protected String getPollingPath() {
+        return ((HttpPollingConfig) config).getPollPath();
     }
 
     /**
@@ -254,4 +383,12 @@ public abstract class HttpOperation<T> extends OperationPartial {
         NetLoggerUtil.log(direction, infra, sink, json);
         return json;
     }
+
+    // these may be overridden by junit tests
+
+    protected long getPollWaitMs() {
+        HttpPollingConfig cfg = (HttpPollingConfig) config;
+
+        return TimeUnit.MILLISECONDS.convert(cfg.getPollWaitSec(), TimeUnit.SECONDS);
+    }
 }