Skip to content

Commit

Permalink
Use config key builder for composite effectors
Browse files Browse the repository at this point in the history
  • Loading branch information
grkvlt committed Apr 12, 2017
1 parent f5f6579 commit 42ff035
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;

/**
* Execute an effector, and depending on the result, execute either
Expand All @@ -47,24 +48,27 @@ public class ChoiceEffector extends AbstractCompositeEffector {

private static final Logger LOG = LoggerFactory.getLogger(ChoiceEffector.class);

public static final ConfigKey<String> INPUT = ConfigKeys.newStringConfigKey(
"input",
"Choice input parameter");
public static final ConfigKey<String> INPUT = ConfigKeys.builder(String.class)
.name("input")
.description("Choice input parameter")
.build();

public static final ConfigKey<Object> CHOICE = ConfigKeys.newConfigKey(
Object.class,
"choice",
"Effector details for the choice effector");
public static final ConfigKey<Object> CHOICE = ConfigKeys.builder(Object.class)
.name("choice")
.description("Effector details for the choice effector")
.constraint(Predicates.notNull())
.build();

public static final ConfigKey<Object> SUCCESS = ConfigKeys.newConfigKey(
Object.class,
"success",
"Effector details for the success effector");
public static final ConfigKey<Object> SUCCESS = ConfigKeys.builder(Object.class)
.name("success")
.description("Effector details for the success effector")
.constraint(Predicates.notNull())
.build();

public static final ConfigKey<Object> FAILURE = ConfigKeys.newConfigKey(
Object.class,
"failure",
"Effector details for the failure effector");
public static final ConfigKey<Object> FAILURE = ConfigKeys.builder(Object.class)
.name("failure")
.description("Effector details for the failure effector")
.build();

public ChoiceEffector(ConfigBag params) {
super(newEffectorBuilder(params).build());
Expand Down Expand Up @@ -109,6 +113,7 @@ public Object call(final ConfigBag params) {

Object output = invokeEffectorNamed(choiceTargetEntity, choiceEffectorName, params);
Boolean success = Boolean.parseBoolean(Strings.toString(output));
LOG.debug("{} result of {} was {}/{}", new Object[] { this, choiceEffectorName, Strings.toString(output), success });

Object effectorDetails = EntityInitializers.resolve(config, success ? SUCCESS : FAILURE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.reflect.TypeToken;

Expand All @@ -49,11 +50,12 @@ public class ComposeEffector extends AbstractCompositeEffector {

private static final Logger LOG = LoggerFactory.getLogger(ComposeEffector.class);

public static final ConfigKey<List<Object>> COMPOSE = ConfigKeys.newConfigKey(
new TypeToken<List<Object>>() { },
"compose",
"Effector details list for the compose effector",
ImmutableList.<Object>of());
public static final ConfigKey<List<Object>> COMPOSE = ConfigKeys.builder(new TypeToken<List<Object>>() { })
.name("compose")
.description("Effector details list for the compose effector")
.constraint(Predicates.notNull())
.defaultValue(ImmutableList.<Object>of())
.build();

public ComposeEffector(ConfigBag params) {
super(newEffectorBuilder(params).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

Expand All @@ -51,14 +52,16 @@ public class LoopEffector extends AbstractCompositeEffector {

private static final Logger LOG = LoggerFactory.getLogger(LoopEffector.class);

public static final ConfigKey<String> INPUT = ConfigKeys.newStringConfigKey(
"input",
"Loop input parameter");
public static final ConfigKey<String> INPUT = ConfigKeys.builder(String.class)
.name("input")
.description("Loop input parameter")
.build();

public static final ConfigKey<Object> LOOP = ConfigKeys.newConfigKey(
Object.class,
"loop",
"Effector details for the loop effector");
public static final ConfigKey<Object> LOOP = ConfigKeys.builder(Object.class)
.name("loop")
.description("Effector details for the loop effector")
.constraint(Predicates.notNull())
.build();

public LoopEffector(ConfigBag params) {
super(newEffectorBuilder(params).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.reflect.TypeToken;
Expand All @@ -50,11 +51,12 @@ public class ParallelEffector extends AbstractCompositeEffector {

private static final Logger LOG = LoggerFactory.getLogger(ParallelEffector.class);

public static final ConfigKey<List<Object>> PARALLEL = ConfigKeys.newConfigKey(
new TypeToken<List<Object>>() { },
"parallel",
"Effector details list for the parallel effector",
ImmutableList.<Object>of());
public static final ConfigKey<List<Object>> PARALLEL = ConfigKeys.builder(new TypeToken<List<Object>>() { })
.name("parallel")
.description("Effector details list for the parallel effector")
.constraint(Predicates.notNull())
.defaultValue(ImmutableList.<Object>of())
.build();

public ParallelEffector(ConfigBag params) {
super(newEffectorBuilder(params).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
import org.apache.brooklyn.core.effector.Effectors.EffectorBuilder;
import org.apache.brooklyn.core.entity.EntityInitializers;
import org.apache.brooklyn.util.core.config.ConfigBag;
import org.apache.brooklyn.util.math.MathPredicates;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

Expand All @@ -48,15 +50,18 @@ public class RepeatEffector extends AbstractCompositeEffector {

private static final Logger LOG = LoggerFactory.getLogger(RepeatEffector.class);

public static final ConfigKey<Object> REPEAT = ConfigKeys.newConfigKey(
Object.class,
"repeat",
"Effector details list for the repeat effector");
public static final ConfigKey<Object> REPEAT = ConfigKeys.builder(Object.class)
.name("repeat")
.description("Effector details list for the repeat effector")
.constraint(Predicates.notNull())
.build();

public static final ConfigKey<Integer> COUNT = ConfigKeys.newIntegerConfigKey(
"count",
"Number of times to rpeat the effector",
1);
public static final ConfigKey<Integer> COUNT = ConfigKeys.builder(Integer.class)
.name("count")
.description("Number of times to repeat the effector")
.constraint(MathPredicates.greaterThan(0d))
.defaultValue(1)
.build();

public RepeatEffector(ConfigBag params) {
super(newEffectorBuilder(params).build());
Expand Down Expand Up @@ -85,7 +90,7 @@ public List call(final ConfigBag params) {
synchronized (mutex) {
LOG.debug("{} called with config {}, params {}", new Object[] { this, config, params });
Object effectorDetails = EntityInitializers.resolve(config, REPEAT);
int count = EntityInitializers.resolve(config, COUNT);
Integer count = EntityInitializers.resolve(config, COUNT);

String effectorName = getEffectorName(effectorDetails);
String inputArgument = getInputArgument(effectorDetails);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;

/**
* Replace an effector with another, optionally executing the originsl
Expand All @@ -59,16 +60,17 @@ public enum ReplaceAction {

public static final String ORIGINAL = "original-";

public static final ConfigKey<ReplaceAction> ACTION = ConfigKeys.newConfigKey(
ReplaceAction.class,
"action",
"Action to take with the replaced effector",
ReplaceAction.OVERRIDE);

public static final ConfigKey<Object> REPLACE = ConfigKeys.newConfigKey(
Object.class,
"replace",
"Effector details for the replace effector");
public static final ConfigKey<ReplaceAction> ACTION = ConfigKeys.builder(ReplaceAction.class)
.name("action")
.description("Action to take with the replaced effector")
.defaultValue(ReplaceAction.OVERRIDE)
.build();

public static final ConfigKey<Object> REPLACE = ConfigKeys.builder(Object.class)
.name("replace")
.description("Effector details for the replace effector")
.constraint(Predicates.notNull())
.build();

public ReplaceEffector(ConfigBag params) {
super(newEffectorBuilder(params).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.reflect.TypeToken;

Expand All @@ -48,11 +49,12 @@ public class SequenceEffector extends AbstractCompositeEffector {

private static final Logger LOG = LoggerFactory.getLogger(SequenceEffector.class);

public static final ConfigKey<List<Object>> SEQUENCE = ConfigKeys.newConfigKey(
new TypeToken<List<Object>>() { },
"sequence",
"Effector details list for the sequence effector",
ImmutableList.<Object>of());
public static final ConfigKey<List<Object>> SEQUENCE = ConfigKeys.builder(new TypeToken<List<Object>>() { })
.name("sequence")
.description("Effector details list for the sequence effector")
.constraint(Predicates.notNull())
.defaultValue(ImmutableList.<Object>of())
.build();

public SequenceEffector(ConfigBag params) {
super(newEffectorBuilder(params).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;
import com.google.common.reflect.TypeToken;

/**
Expand All @@ -47,15 +48,17 @@ public class TransformEffector extends AbstractCompositeEffector {

private static final Logger LOG = LoggerFactory.getLogger(TransformEffector.class);

public static final ConfigKey<String> INPUT = ConfigKeys.newStringConfigKey(
"input",
"Transformer input parameter");
public static final ConfigKey<String> INPUT = ConfigKeys.builder(String.class)
.name("input")
.description("Transformer input parameter")
.build();

public static final ConfigKey<Function<Object,Object>> FUNCTION = ConfigKeys.newConfigKey(
new TypeToken<Function<Object,Object>>() { },
"function",
"Transformer function to apply",
Functions.identity());
public static final ConfigKey<Function<Object,Object>> FUNCTION = ConfigKeys.builder(new TypeToken<Function<Object,Object>>() { })
.name("function")
.description("Transformer function to apply")
.constraint(Predicates.notNull())
.defaultValue(Functions.identity())
.build();

public TransformEffector(ConfigBag params) {
super(newEffectorBuilder(params).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ public void onEvent(SensorEvent<Collection<?>> event) {
}

private void onAdded(Object newElement) {
onEvent(getOnAddedEffector(), newElement);
invokeEffector(getOnAddedEffector(), newElement);
}

private void onRemoved(Object newElement) {
onEvent(getOnRemovedEffector(), newElement);
invokeEffector(getOnRemovedEffector(), newElement);
}

private void onEvent(String effectorName, Object parameter) {
private void invokeEffector(String effectorName, Object parameter) {
Maybe<Effector<?>> effector = getEffector(effectorName);
if (effector.isPresentAndNonNull()) {
final Map<String, Object> parameters;
Expand Down

0 comments on commit 42ff035

Please sign in to comment.