X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-interactions%2Fmodel-actors%2FactorServiceProvider%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fcontrolloop%2Factorserviceprovider%2Fimpl%2FActorImpl.java;h=336860ecb25ffbee40cf1893a91e5e7461f08300;hb=refs%2Fchanges%2F26%2F108826%2F3;hp=9b9aa914ec7901efffdbb19d6d07fa758b4e2253;hpb=89ce8bc6075096d015cdc8735634e0be14fe0357;p=policy%2Fmodels.git diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImpl.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImpl.java index 9b9aa914e..336860ecb 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImpl.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImpl.java @@ -20,18 +20,14 @@ package org.onap.policy.controlloop.actorserviceprovider.impl; -import com.google.common.collect.ImmutableMap; -import java.util.Arrays; import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; -import org.onap.policy.common.parameters.BeanValidationResult; import org.onap.policy.controlloop.actorserviceprovider.Operator; import org.onap.policy.controlloop.actorserviceprovider.Util; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ActorParams; import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException; import org.onap.policy.controlloop.actorserviceprovider.spi.Actor; import org.slf4j.Logger; @@ -46,44 +42,50 @@ public class ActorImpl extends StartConfigPartial> implement /** * Maps a name to an operator. */ - private Map name2operator; + private final Map name2operator = new ConcurrentHashMap<>(); /** * Constructs the object. * * @param name actor name - * @param operators the operations supported by this actor */ - public ActorImpl(String name, Operator... operators) { + public ActorImpl(String name) { super(name); - setOperators(Arrays.asList(operators)); } /** - * Sets the operators supported by this actor, overriding any previous list. + * This method simply returns {@code 0}. + */ + @Override + public int getSequenceNumber() { + return 0; + } + + /** + * Adds an operator supported by this actor. * - * @param operators the operations supported by this actor + * @param operator operation to be added */ - protected void setOperators(List operators) { + protected synchronized void addOperator(Operator operator) { + /* + * This method is "synchronized" to prevent the state from changing while the + * operator is added. The map, itself, does not need synchronization as it's a + * concurrent map. + */ + if (isConfigured()) { throw new IllegalStateException("attempt to set operators on a configured actor: " + getName()); } - Map map = new HashMap<>(); - for (Operator newOp : operators) { - map.compute(newOp.getName(), (opName, existingOp) -> { - if (existingOp == null) { - return newOp; - } - - // TODO: should this throw an exception? - logger.warn("duplicate names for actor operation {}.{}: {}, ignoring {}", getName(), opName, - existingOp.getClass().getSimpleName(), newOp.getClass().getSimpleName()); - return existingOp; - }); - } + name2operator.compute(operator.getName(), (opName, existingOp) -> { + if (existingOp == null) { + return operator; + } - this.name2operator = ImmutableMap.copyOf(map); + logger.warn("duplicate names for actor operation {}.{}: {}, ignoring {}", getName(), opName, + existingOp.getClass().getSimpleName(), operator.getClass().getSimpleName()); + return existingOp; + }); } @Override @@ -95,7 +97,7 @@ public class ActorImpl extends StartConfigPartial> implement public Operator getOperator(String name) { Operator operator = name2operator.get(name); if (operator == null) { - throw new IllegalArgumentException("unknown operation " + getName() + "." + name); + throw new IllegalArgumentException("unknown operator " + getName() + "." + name); } return operator; @@ -120,8 +122,6 @@ public class ActorImpl extends StartConfigPartial> implement final String actorName = getName(); logger.info("configuring operations for actor {}", actorName); - BeanValidationResult valres = new BeanValidationResult(actorName, parameters); - // function that creates operator-specific parameters, given the operation name Function> opParamsMaker = makeOperatorParameters(parameters); @@ -135,8 +135,8 @@ public class ActorImpl extends StartConfigPartial> implement operator.configure(subparams); } catch (ParameterValidationRuntimeException e) { - logger.warn("failed to configure operation {}.{}", actorName, operName, e); - valres.addResult(e.getResult()); + logger.warn("failed to configure operation {}.{} because:\n{}", actorName, operName, + e.getResult().getResult(), e); } catch (RuntimeException e) { logger.warn("failed to configure operation {}.{}", actorName, operName, e); @@ -155,7 +155,8 @@ public class ActorImpl extends StartConfigPartial> implement /** * Extracts the operator parameters from the actor parameters, for a given operator. - * This method assumes each operation has its own set of parameters. + * This method translates the parameters to an {@link ActorParams} and then creates a function + * that will extract operator-specific parameters. * * @param actorParameters actor parameters * @return a function to extract the operator parameters from the actor parameters. @@ -163,8 +164,13 @@ public class ActorImpl extends StartConfigPartial> implement * the given operation name */ protected Function> makeOperatorParameters(Map actorParameters) { + String actorName = getName(); - return operName -> Util.translateToMap(getName() + "." + operName, actorParameters.get(operName)); + // @formatter:off + return Util.translate(actorName, actorParameters, ActorParams.class) + .doValidation(actorName) + .makeOperationParameters(actorName); + // @formatter:on } /** @@ -177,7 +183,7 @@ public class ActorImpl extends StartConfigPartial> implement for (Operator oper : name2operator.values()) { if (oper.isConfigured()) { - Util.logException(oper::start, "failed to start operation {}.{}", actorName, oper.getName()); + Util.runFunction(oper::start, "failed to start operation {}.{}", actorName, oper.getName()); } else { logger.warn("not starting unconfigured operation {}.{}", actorName, oper.getName()); @@ -195,7 +201,7 @@ public class ActorImpl extends StartConfigPartial> implement // @formatter:off name2operator.values().forEach( - oper -> Util.logException(oper::stop, "failed to stop operation {}.{}", actorName, oper.getName())); + oper -> Util.runFunction(oper::stop, "failed to stop operation {}.{}", actorName, oper.getName())); // @formatter:on } @@ -208,32 +214,8 @@ public class ActorImpl extends StartConfigPartial> implement logger.info("shutting down operations for actor {}", actorName); // @formatter:off - name2operator.values().forEach(oper -> Util.logException(oper::shutdown, + name2operator.values().forEach(oper -> Util.runFunction(oper::shutdown, "failed to shutdown operation {}.{}", actorName, oper.getName())); // @formatter:on } - - // TODO old code: remove lines down to **HERE** - - @Override - public String actor() { - return null; - } - - @Override - public List recipes() { - return Collections.emptyList(); - } - - @Override - public List recipeTargets(String recipe) { - return Collections.emptyList(); - } - - @Override - public List recipePayloads(String recipe) { - return Collections.emptyList(); - } - - // **HERE** }