Remove dmaap from models
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / impl / OperationPartial.java
index e9f6b02..c19ad6c 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2022 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -118,7 +118,7 @@ public abstract class OperationPartial implements Operation {
      * @param config configuration for this operation
      * @param propertyNames names of properties required by this operation
      */
-    public OperationPartial(ControlLoopOperationParams params, OperatorConfig config, List<String> propertyNames) {
+    protected OperationPartial(ControlLoopOperationParams params, OperatorConfig config, List<String> propertyNames) {
         this.params = params;
         this.config = config;
         this.fullName = params.getActor() + "." + params.getOperation();
@@ -165,7 +165,7 @@ public abstract class OperationPartial implements Operation {
      * @return the property value
      */
     @SuppressWarnings("unchecked")
-    protected <T> T getRequiredProperty(String name, String propertyType) {
+    public <T> T getRequiredProperty(String name, String propertyType) {
         T value = (T) properties.get(name);
         if (value == null) {
             throw new IllegalStateException("missing " + propertyType);
@@ -217,7 +217,6 @@ public abstract class OperationPartial implements Operation {
     /**
      * Starts the operation attempt, without doing any retries.
      *
-     * @param params operation parameters
      * @param attempt attempt number, typically starting with 1
      * @return a future that will return the result of a single operation attempt
      */
@@ -225,9 +224,9 @@ public abstract class OperationPartial implements Operation {
 
         logger.info("{}: start operation attempt {} for {}", getFullName(), attempt, params.getRequestId());
 
-        final Executor executor = params.getExecutor();
-        final OperationOutcome outcome = params.makeOutcome(getTargetEntity());
-        final CallbackManager callbacks = new CallbackManager();
+        final var executor = params.getExecutor();
+        final var outcome = makeOutcome();
+        final var callbacks = new CallbackManager();
 
         // this operation attempt gets its own controller
         final PipelineControllerFuture<OperationOutcome> controller = new PipelineControllerFuture<>();
@@ -357,7 +356,7 @@ public abstract class OperationPartial implements Operation {
                 outcome = origOutcome;
             } else {
                 logger.warn("{}: null outcome; treating as a failure for {}", getFullName(), params.getRequestId());
-                outcome = this.setOutcome(params.makeOutcome(getTargetEntity()), OperationResult.FAILURE);
+                outcome = this.setOutcome(makeOutcome(), OperationResult.FAILURE);
             }
 
             // ensure correct actor/operation
@@ -382,7 +381,7 @@ public abstract class OperationPartial implements Operation {
 
             } else {
                 /*
-                 * retries were specified and we've already tried them all - change to
+                 * retries were specified, and we've already tried them all - change to
                  * FAILURE_RETRIES
                  */
                 logger.info("operation {} retries exhausted for {}", getFullName(), params.getRequestId());
@@ -456,7 +455,7 @@ public abstract class OperationPartial implements Operation {
     private Function<Throwable, OperationOutcome> fromException(String type) {
 
         return thrown -> {
-            OperationOutcome outcome = params.makeOutcome(getTargetEntity());
+            OperationOutcome outcome = makeOutcome();
 
             if (thrown instanceof CancellationException || thrown.getCause() instanceof CancellationException) {
                 // do not include exception in the message, as it just clutters the log
@@ -518,7 +517,7 @@ public abstract class OperationPartial implements Operation {
             return futures[0];
         }
 
-        CompletableFuture.anyOf(futures).thenApply(outcome -> (OperationOutcome) outcome)
+        CompletableFuture.anyOf(futures).thenApply(OperationOutcome.class::cast)
                         .whenCompleteAsync(controller.delayedComplete(), params.getExecutor());
 
         return controller;
@@ -594,14 +593,13 @@ public abstract class OperationPartial implements Operation {
      * @return an array of futures, possibly zero-length. If the array is of size one,
      *         then that one item should be returned instead of the controller
      */
+    @SuppressWarnings("unchecked")
     private CompletableFuture<OperationOutcome>[] attachFutures(PipelineControllerFuture<OperationOutcome> controller,
                     List<Supplier<CompletableFuture<OperationOutcome>>> futureMakers,
                     UnaryOperator<CompletableFuture<OperationOutcome>> adorn) {
 
         if (futureMakers.isEmpty()) {
-            @SuppressWarnings("unchecked")
-            CompletableFuture<OperationOutcome>[] result = new CompletableFuture[0];
-            return result;
+            return new CompletableFuture[0];
         }
 
         // the last, unadorned future that is created
@@ -631,8 +629,7 @@ public abstract class OperationPartial implements Operation {
             }
         }
 
-        @SuppressWarnings("unchecked")
-        CompletableFuture<OperationOutcome>[] result = new CompletableFuture[futures.size()];
+        var result = new CompletableFuture[futures.size()];
 
         if (result.length == 1) {
             // special case - return the unadorned future
@@ -747,7 +744,7 @@ public abstract class OperationPartial implements Operation {
          * executing
          */
         final PipelineControllerFuture<OperationOutcome> controller = new PipelineControllerFuture<>();
-        final Executor executor = params.getExecutor();
+        final var executor = params.getExecutor();
 
         // @formatter:off
         controller.wrap(nextTask)
@@ -830,7 +827,7 @@ public abstract class OperationPartial implements Operation {
                 outcome.setEnd(null);
 
                 // pass a copy to the callback
-                OperationOutcome outcome2 = new OperationOutcome(outcome);
+                var outcome2 = new OperationOutcome(outcome);
                 outcome2.setFinalOutcome(false);
                 params.callbackStarted(outcome2);
             }
@@ -892,6 +889,18 @@ public abstract class OperationPartial implements Operation {
         return operation;
     }
 
+    /**
+     * Makes an outcome, populating the "target" field with the contents of the target
+     * entity property.
+     *
+     * @return a new operation outcome
+     */
+    protected OperationOutcome makeOutcome() {
+        OperationOutcome outcome = params.makeOutcome();
+        outcome.setTarget(getProperty(OperationProperties.AAI_TARGET_ENTITY));
+        return outcome;
+    }
+
     /**
      * Determines if a throwable is due to a timeout.
      *
@@ -975,16 +984,6 @@ public abstract class OperationPartial implements Operation {
         return DEFAULT_RETRY_WAIT_MS;
     }
 
-    /**
-     * Gets the target entity, first trying the properties and then the parameters.
-     *
-     * @return the target entity
-     */
-    protected String getTargetEntity() {
-        String targetEntity = getProperty(OperationProperties.AAI_TARGET_ENTITY);
-        return (targetEntity != null ? targetEntity : params.getTargetEntity());
-    }
-
     /**
      * Gets the operation timeout.
      *