Merge "Convert models to JUnit 5"
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / impl / HttpOperator.java
index add74aa..4918dd7 100644 (file)
 package org.onap.policy.controlloop.actorserviceprovider.impl;
 
 import java.util.Map;
-import java.util.concurrent.TimeUnit;
-import java.util.function.BiFunction;
-import lombok.Getter;
-import org.onap.policy.common.endpoints.http.client.HttpClient;
 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
 import org.onap.policy.common.parameters.ValidationResult;
-import org.onap.policy.controlloop.actorserviceprovider.Operation;
 import org.onap.policy.controlloop.actorserviceprovider.Util;
-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.ParameterValidationRuntimeException;
 
 /**
  * Operator that uses HTTP. The operator's parameters must be an {@link HttpParams}.
  */
-@Getter
-public abstract class HttpOperator extends OperatorPartial {
-
-    private HttpClient client;
-
-    /**
-     * Default timeout, in milliseconds, if none specified in the request.
-     */
-    private long timeoutMs;
-
-    /**
-     * URI path for this particular operation. Includes a leading "/".
-     */
-    private String path;
-
+public class HttpOperator extends TypedOperator<HttpConfig, HttpOperation<?>> {
 
     /**
      * Constructs the object.
@@ -59,45 +40,36 @@ public abstract class HttpOperator extends OperatorPartial {
      * @param actorName name of the actor with which this operator is associated
      * @param name operation name
      */
-    public HttpOperator(String actorName, String name) {
-        super(actorName, name);
+    protected HttpOperator(String actorName, String name) {
+        this(actorName, name, null);
     }
 
     /**
-     * Makes an operator that will construct operations.
+     * Constructs the object.
      *
-     * @param <T> response type
-     * @param actorName actor name
-     * @param operation operation name
+     * @param actorName name of the actor with which this operator is associated
+     * @param name operation name
      * @param operationMaker function to make an operation
-     * @return a new operator
      */
-    public static <T> HttpOperator makeOperator(String actorName, String operation,
-                    BiFunction<ControlLoopOperationParams, HttpOperator, HttpOperation<T>> operationMaker) {
-
-        return new HttpOperator(actorName, operation) {
-            @Override
-            public Operation buildOperation(ControlLoopOperationParams params) {
-                return operationMaker.apply(params, this);
-            }
-        };
+    public HttpOperator(String actorName, String name,
+                    OperationMaker<HttpConfig, HttpOperation<?>> operationMaker) {
+        super(actorName, name, operationMaker);
     }
 
     /**
-     * Translates the parameters to an {@link HttpParams} and then extracts the relevant
-     * values.
+     * Makes a new configuration using the specified parameters.
+     *
+     * @param parameters operator parameters
+     * @return a new configuration
      */
-    @Override
-    protected void doConfigure(Map<String, Object> parameters) {
+    protected HttpConfig makeConfiguration(Map<String, Object> parameters) {
         HttpParams params = Util.translate(getFullName(), parameters, HttpParams.class);
         ValidationResult result = params.validate(getFullName());
         if (!result.isValid()) {
             throw new ParameterValidationRuntimeException("invalid parameters", result);
         }
 
-        client = getClientFactory().get(params.getClientName());
-        path = params.getPath();
-        timeoutMs = TimeUnit.MILLISECONDS.convert(params.getTimeoutSec(), TimeUnit.SECONDS);
+        return new HttpConfig(getBlockingExecutor(), params, getClientFactory());
     }
 
     // these may be overridden by junit tests