From 7f69c5ca0a6f6018166f8fee3e811edf4dee1eb8 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 20 Feb 2020 19:08:55 -0500 Subject: Change payload to Map so it's more versatile This was supposed to be two separate commits, but I goofed something. Added guard query to Operation superclass. Modified VfModuleCreate to store the VF count, pass it to the guard, and bump it once the create completes successfully. Added code to check Actors for proper plug-in to ActorService. Renamed "operation" property to "operations", to be more consistent with other parameters (e.g., TopicParameterGroup). The META-INF/services files for the actors had mixed case, which did not match the package name of the Actor class, preventing the ServiceLoader from recognizing them. Also modified the ActorService to skip any that cannot actually be loaded, for whatever reason (e.g., not in the classpath). Issue-ID: POLICY-1625 Signed-off-by: Jim Hahn Change-Id: Ifa97744543f2866cc553138ec5ec644b033de780 --- .../actorserviceprovider/ActorService.java | 17 ++++++-- .../controlloop/ControlLoopEventContext.java | 8 ++++ .../impl/OperationPartial.java | 48 +++++++++++++++++++--- .../parameters/CommonActorParams.java | 12 ++++-- .../parameters/ControlLoopOperationParams.java | 2 +- 5 files changed, 74 insertions(+), 13 deletions(-) (limited to 'models-interactions/model-actors/actorServiceProvider/src/main') diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/ActorService.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/ActorService.java index 24c2cfc23..22c7d3365 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/ActorService.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/ActorService.java @@ -24,7 +24,9 @@ package org.onap.policy.controlloop.actorserviceprovider; import com.google.common.collect.ImmutableMap; import java.util.Collection; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; +import java.util.ServiceConfigurationError; import java.util.ServiceLoader; import java.util.Set; import org.onap.policy.common.parameters.BeanValidationResult; @@ -57,7 +59,17 @@ public class ActorService extends StartConfigPartial map = new HashMap<>(); - for (Actor newActor : loadActors()) { + Iterator iter = loadActors().iterator(); + while (iter.hasNext()) { + + Actor newActor; + try { + newActor = iter.next(); + } catch (ServiceConfigurationError e) { + logger.warn("unable to load actor", e); + continue; + } + map.compute(newActor.getName(), (name, existingActor) -> { if (existingActor == null) { return newActor; @@ -168,8 +180,7 @@ public class ActorService extends StartConfigPartial Util.runFunction(actor::stop, "failed to stop actor {}", actor.getName())); + name2actor.values().forEach(actor -> Util.runFunction(actor::stop, "failed to stop actor {}", actor.getName())); } @Override diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/controlloop/ControlLoopEventContext.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/controlloop/ControlLoopEventContext.java index 8099ea7c2..f7b58c11e 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/controlloop/ControlLoopEventContext.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/controlloop/ControlLoopEventContext.java @@ -116,6 +116,14 @@ public class ControlLoopEventContext implements Serializable { properties.put(name, value); } + /** + * Removes a property. + * @param name property name + */ + public void removeProperty(String name) { + properties.remove(name); + } + /** * Obtains the given property. * diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartial.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartial.java index e636228f6..c998209bc 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartial.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartial.java @@ -23,8 +23,10 @@ package org.onap.policy.controlloop.actorserviceprovider.impl; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; +import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Queue; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; @@ -71,6 +73,8 @@ public abstract class OperationPartial implements Operation { private static final Logger logger = LoggerFactory.getLogger(OperationPartial.class); private static final Coder coder = new StandardCoder(); + public static final String GUARD_ACTOR_NAME = "GUARD"; + public static final String GUARD_OPERATION_NAME = "Decision"; public static final long DEFAULT_RETRY_WAIT_MS = 1000L; private final OperatorConfig config; @@ -187,7 +191,7 @@ public abstract class OperationPartial implements Operation { /** * Invokes the operation's preprocessor step(s) as a "future". This method simply - * invokes {@link #startGuardAsync()}. + * returns {@code null}. *

* This method assumes the following: *

    @@ -199,12 +203,11 @@ public abstract class OperationPartial implements Operation { * {@code null} if this operation needs no preprocessor */ protected CompletableFuture startPreprocessorAsync() { - return startGuardAsync(); + return null; } /** - * Invokes the operation's guard step(s) as a "future". This method simply returns - * {@code null}. + * Invokes the operation's guard step(s) as a "future". *

    * This method assumes the following: *

      @@ -216,7 +219,42 @@ public abstract class OperationPartial implements Operation { * {@code null} if this operation has no guard */ protected CompletableFuture startGuardAsync() { - return null; + // get the guard payload + Map guardPayload = makeGuardPayload(); + + // wrap it in a "resource" + Map resource = new LinkedHashMap<>(); + resource.put("guard", guardPayload); + + Map payload = new LinkedHashMap<>(); + payload.put("resource", resource); + + /* + * Note: can't use constants from actor.guard, because that would create a + * circular dependency. + */ + return params.toBuilder().actor(GUARD_ACTOR_NAME).operation(GUARD_OPERATION_NAME).retry(null).timeoutSec(null) + .payload(payload).build().start(); + } + + /** + * Creates a payload to execute a guard operation. + * + * @return a new guard payload + */ + protected Map makeGuardPayload() { + Map guard = new LinkedHashMap<>(); + guard.put("actor", params.getActor()); + guard.put("recipe", params.getOperation()); + guard.put("target", params.getTargetEntity()); + guard.put("requestId", params.getRequestId()); + + String clname = params.getContext().getEvent().getClosedLoopControlName(); + if (clname != null) { + guard.put("clname", clname); + } + + return guard; } /** diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/CommonActorParams.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/CommonActorParams.java index dc6f2b657..b8218e53b 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/CommonActorParams.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/CommonActorParams.java @@ -33,18 +33,22 @@ import org.onap.policy.controlloop.actorserviceprovider.Util; /** * Superclass for Actor parameters that have default values in "this" object, and - * operation-specific values in {@link #operation}. + * operation-specific values in {@link #operations}. */ @Getter @Setter @EqualsAndHashCode public class CommonActorParams { + /** + * Name of the "operations" field contained within actor parameters. + */ + public static final String OPERATIONS_FIELD = "operations"; /** * Maps the operation name to its parameters. */ @NotNull - protected Map> operation; + protected Map> operations; /** @@ -57,10 +61,10 @@ public class CommonActorParams { public Function> makeOperationParameters(String name) { Map defaultParams = Util.translateToMap(name, this); - defaultParams.remove("operation"); + defaultParams.remove(OPERATIONS_FIELD); return operationName -> { - Map specificParams = operation.get(operationName); + Map specificParams = operations.get(operationName); if (specificParams == null) { return null; } diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/ControlLoopOperationParams.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/ControlLoopOperationParams.java index 925916097..7fc15c97b 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/ControlLoopOperationParams.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/ControlLoopOperationParams.java @@ -86,7 +86,7 @@ public class ControlLoopOperationParams { /** * Payload data for the request. */ - private Map payload; + private Map payload; /** * Number of retries allowed, or {@code null} if no retries. -- cgit 1.2.3-korg