Merge "Add actor for CDS"
authorLiam Fallon <liam.fallon@est.tech>
Wed, 26 Feb 2020 15:37:35 +0000 (15:37 +0000)
committerGerrit Code Review <gerrit@onap.org>
Wed, 26 Feb 2020 15:37:35 +0000 (15:37 +0000)
12 files changed:
models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardActorParams.java
models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardConfig.java
models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardOperation.java
models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardParams.java
models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardActorParamsTest.java
models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardConfigTest.java
models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardOperationTest.java
models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardParamsTest.java
models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/ActorService.java
models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartial.java
models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/ActorServiceProviderTest.java
models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/ActorServiceTest.java

index b8eb85c..aa6d727 100644 (file)
@@ -40,4 +40,9 @@ public class GuardActorParams extends HttpActorParams {
     private String onapComponent;
     private String onapInstance;
     private String action = DEFAULT_ACTION;
+
+    /**
+     * {@code True} if guard operations are disabled.
+     */
+    private boolean disabled = false;
 }
index c6bf326..0e711d1 100644 (file)
@@ -23,6 +23,7 @@ package org.onap.policy.controlloop.actor.guard;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.concurrent.Executor;
+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.controlloop.actorserviceprovider.parameters.HttpConfig;
@@ -33,6 +34,12 @@ import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
 public class GuardConfig extends HttpConfig {
     private final Map<String, Object> defaultRequest = new LinkedHashMap<>();
 
+    /**
+     * {@code True} if the associated guard operation is disabled.
+     */
+    @Getter
+    private boolean disabled;
+
     /**
      * Constructs the object.
      *
@@ -47,6 +54,8 @@ public class GuardConfig extends HttpConfig {
         addProperty("ONAPInstance", params.getOnapInstance());
         addProperty("ONAPName", params.getOnapName());
         addProperty("action", params.getAction());
+
+        this.disabled = params.isDisabled();
     }
 
     /**
index a5459f6..e35caa0 100644 (file)
@@ -84,6 +84,10 @@ public class GuardOperation extends HttpOperation<DecisionResponse> {
 
     @Override
     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
+        if (config.isDisabled()) {
+            // guard is disabled, thus it is always treated as a success
+            return CompletableFuture.completedFuture(params.makeOutcome());
+        }
 
         DecisionRequest request = Util.translate(getName(), makeRequest(), DecisionRequest.class);
 
index 2514825..5f83603 100644 (file)
@@ -41,4 +41,9 @@ public class GuardParams extends HttpParams {
     private String onapComponent;
     private String onapInstance;
     private String action;
+
+    /**
+     * {@code True} if the associated guard operation is disabled.
+     */
+    private boolean disabled;
 }
index 1c25609..0fef3bd 100644 (file)
@@ -64,6 +64,12 @@ public class GuardActorParamsTest {
         params = makeGuardActorParams();
     }
 
+    @Test
+    public void testIsDisabled() {
+        // disabled by default
+        assertFalse(params.isDisabled());
+    }
+
     @Test
     public void testValidate() {
         assertTrue(params.validate(CONTAINER).isValid());
index f94d122..49c1c91 100644 (file)
@@ -21,7 +21,9 @@
 package org.onap.policy.controlloop.actor.guard;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.when;
 
 import java.util.concurrent.Executor;
@@ -85,8 +87,14 @@ public class GuardConfigTest {
         // repeat, with minimal parameters
         params = GuardParams.builder().clientName(MY_CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
         config = new GuardConfig(executor, params, factory);
+        assertFalse(config.isDisabled());
 
         actual = Util.translate("", config.makeRequest(), DecisionRequest.class);
         assertEquals(new DecisionRequest(), actual);
+
+        // try with disabled=true
+        params = params.toBuilder().disabled(true).build();
+        config = new GuardConfig(executor, params, factory);
+        assertTrue(config.isDisabled());
     }
 }
index bd23a81..a43292a 100644 (file)
@@ -26,14 +26,17 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import java.util.Map;
 import java.util.TreeMap;
 import java.util.concurrent.CompletableFuture;
+import java.util.function.Consumer;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.Mock;
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.controlloop.actor.test.BasicHttpOperation;
 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
@@ -44,6 +47,12 @@ import org.onap.policy.models.decisions.concepts.DecisionResponse;
 
 public class GuardOperationTest extends BasicHttpOperation<DecisionRequest> {
 
+    @Mock
+    private Consumer<OperationOutcome> started;
+    @Mock
+    private Consumer<OperationOutcome> completed;
+
+    private GuardConfig guardConfig;
     private GuardOperation oper;
 
     /**
@@ -53,12 +62,14 @@ public class GuardOperationTest extends BasicHttpOperation<DecisionRequest> {
     public void setUp() throws Exception {
         super.setUpBasic();
 
-        GuardConfig cguard = mock(GuardConfig.class);
-        when(cguard.makeRequest()).thenAnswer(args -> new TreeMap<>(Map.of("action", "guard")));
+        guardConfig = mock(GuardConfig.class);
+        when(guardConfig.makeRequest()).thenAnswer(args -> new TreeMap<>(Map.of("action", "guard")));
 
-        config = cguard;
+        config = guardConfig;
         initConfig();
 
+        params = params.toBuilder().startCallback(started).completeCallback(completed).build();
+
         oper = new GuardOperation(params, config);
     }
 
@@ -87,6 +98,29 @@ public class GuardOperationTest extends BasicHttpOperation<DecisionRequest> {
         assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
     }
 
+    /**
+     * Tests startOperationAsync() when the guard is disabled.
+     */
+    @Test
+    public void testStartOperationAsyncDisabled() throws Exception {
+        // indicate that it's disabled
+        when(guardConfig.isDisabled()).thenReturn(true);
+
+        CompletableFuture<OperationOutcome> future2 = oper.start();
+        executor.runAll(100);
+
+        verify(client, never()).post(any(), any(), any(), any());
+
+        // should already be done
+        assertTrue(future2.isDone());
+
+        assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
+
+        // ensure callbacks were invoked
+        verify(started).accept(any());
+        verify(completed).accept(any());
+    }
+
     @Test
     public void testMakeRequest() throws CoderException {
         verifyPayload("makeReqStd.json", makePayload());
index b414885..1723683 100644 (file)
@@ -50,6 +50,12 @@ public class GuardParamsTest {
                         .action(MY_ACTION).clientName(CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
     }
 
+    @Test
+    public void testIsDisabled() {
+        // disabled by default
+        assertFalse(params.isDisabled());
+    }
+
     @Test
     public void testValidate() {
         assertTrue(params.validate(CONTAINER).isValid());
index 22c7d33..82f7444 100644 (file)
@@ -47,10 +47,6 @@ public class ActorService extends StartConfigPartial<Map<String, Map<String, Obj
 
     private final Map<String, Actor> name2actor;
 
-    private static class LazyHolder {
-        static final ActorService INSTANCE = new ActorService();
-    }
-
     /**
      * Constructs the object and loads the list of actors.
      */
@@ -84,15 +80,6 @@ public class ActorService extends StartConfigPartial<Map<String, Map<String, Obj
         name2actor = ImmutableMap.copyOf(map);
     }
 
-    /**
-     * Get the single instance.
-     *
-     * @return the instance
-     */
-    public static ActorService getInstance() {
-        return LazyHolder.INSTANCE;
-    }
-
     /**
      * Gets a particular actor.
      *
index ff1b462..24c7ec8 100644 (file)
@@ -68,6 +68,14 @@ import org.slf4j.LoggerFactory;
  * returned by overridden methods will do the same. Of course, if a class overrides
  * {@link #doOperation(int, OperationOutcome) doOperation()}, then there's little that can
  * be done to cancel that particular operation.
+ * <p/>
+ * In general tasks in a pipeline are executed by the same thread. However, the following
+ * should always be executed via the executor specified in "params":
+ * <ul>
+ * <li>start callback</li>
+ * <li>completion callback</li>
+ * <li>controller completion (i.e., delayedComplete())</li>
+ * </ul>
  */
 public abstract class OperationPartial implements Operation {
     private static final Logger logger = LoggerFactory.getLogger(OperationPartial.class);
@@ -558,8 +566,7 @@ public abstract class OperationPartial implements Operation {
      *         canceled. Similarly, when this future completes, any incomplete futures
      *         will be canceled
      */
-    public CompletableFuture<OperationOutcome> anyOf(
-                    List<Supplier<CompletableFuture<OperationOutcome>>> futureMakers) {
+    public CompletableFuture<OperationOutcome> anyOf(List<Supplier<CompletableFuture<OperationOutcome>>> futureMakers) {
 
         PipelineControllerFuture<OperationOutcome> controller = new PipelineControllerFuture<>();
 
@@ -610,8 +617,7 @@ public abstract class OperationPartial implements Operation {
      *         canceled. Similarly, when this future completes, any incomplete futures
      *         will be canceled
      */
-    public CompletableFuture<OperationOutcome> allOf(
-                    List<Supplier<CompletableFuture<OperationOutcome>>> futureMakers) {
+    public CompletableFuture<OperationOutcome> allOf(List<Supplier<CompletableFuture<OperationOutcome>>> futureMakers) {
         PipelineControllerFuture<OperationOutcome> controller = new PipelineControllerFuture<>();
 
         Queue<OperationOutcome> outcomes = new LinkedList<>();
@@ -809,7 +815,7 @@ public abstract class OperationPartial implements Operation {
 
         // @formatter:off
         controller.wrap(nextTask)
-                    .thenComposeAsync(nextTaskOnSuccess(controller, queue), executor)
+                    .thenCompose(nextTaskOnSuccess(controller, queue))
                     .whenCompleteAsync(controller.delayedComplete(), executor);
         // @formatter:on
 
@@ -843,7 +849,7 @@ public abstract class OperationPartial implements Operation {
             // @formatter:off
             return controller
                         .wrap(nextTask)
-                        .thenComposeAsync(nextTaskOnSuccess(controller, taskQueue), params.getExecutor());
+                        .thenCompose(nextTaskOnSuccess(controller, taskQueue));
             // @formatter:on
         };
     }
index 139c517..cca0694 100644 (file)
@@ -23,7 +23,7 @@
 package org.onap.policy.controlloop.actorserviceprovider;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
 import org.junit.Test;
 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
@@ -36,16 +36,11 @@ public class ActorServiceProviderTest {
 
     @Test
     public void testActorServiceProvider() {
-        ActorService actorService = ActorService.getInstance();
-        assertNotNull(actorService);
+        ActorService actorService = new ActorService();
 
-        assertEquals(1, actorService.getActors().size());
+        assertTrue(actorService.getActors().size() >= 1);
 
-        actorService = ActorService.getInstance();
-        assertNotNull(actorService);
-
-        Actor dummyActor = ActorService.getInstance().getActors().iterator().next();
-        assertNotNull(dummyActor);
+        Actor dummyActor = actorService.getActor(DummyActor.class.getSimpleName());
 
         assertEquals("DummyActor", dummyActor.actor());
 
index efc7bb8..989fc8d 100644 (file)
@@ -214,14 +214,6 @@ public class ActorServiceTest {
         iter.next();
     }
 
-    @Test
-    public void testGetInstance() {
-        service = ActorService.getInstance();
-        assertNotNull(service);
-
-        assertSame(service, ActorService.getInstance());
-    }
-
     @Test
     public void testGetActor() {
         assertSame(actor1, service.getActor(ACTOR1));
@@ -360,8 +352,9 @@ public class ActorServiceTest {
 
     @Test
     public void testLoadActors() {
-        assertFalse(ActorService.getInstance().getActors().isEmpty());
-        assertNotNull(ActorService.getInstance().getActor("DummyActor"));
+        ActorService service = new ActorService();
+        assertFalse(service.getActors().isEmpty());
+        assertNotNull(service.getActor(DummyActor.class.getSimpleName()));
     }
 
     /**