[GitHub] brooklyn-server pull request #382: InvokeEffectorOnSetChange

2016-10-13 Thread sjcorbett
Github user sjcorbett commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/382#discussion_r83262396
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/policy/InvokeEffectorOnSetChange.java ---
@@ -0,0 +1,203 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.brooklyn.policy;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.brooklyn.api.effector.Effector;
+import org.apache.brooklyn.api.entity.EntityLocal;
+import org.apache.brooklyn.api.sensor.AttributeSensor;
+import org.apache.brooklyn.api.sensor.Sensor;
+import org.apache.brooklyn.api.sensor.SensorEvent;
+import org.apache.brooklyn.api.sensor.SensorEventListener;
+import org.apache.brooklyn.config.ConfigKey;
+import org.apache.brooklyn.core.config.ConfigKeys;
+import org.apache.brooklyn.core.policy.AbstractPolicy;
+import org.apache.brooklyn.core.sensor.BasicAttributeSensor;
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.flags.TypeCoercions;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.text.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Sets;
+import com.google.common.reflect.TypeToken;
+
+/**
+ * Subscribes to events on a collection {@link AttributeSensor} and 
invokes the named
+ * effectors for each element that was added and removed.
+ * 
+ * The policy only detects replacements of the collection; it 
does not act on
+ * modifications. If the sensor has value A and an element is 
added 
+ * value A'  the on-added effector is not invoked. If 
the sensor is
+ * later set to B the delta is made between A 
and B,
+ * not A' and B.
+ * 
+ * To simplify the detection of additions and removals the collection is 
converted to a
+ * {@link Set}. This means that only a single event will fire for 
duplicate elements in
+ * the collection.
+ * 
+ * The effectors are provided the elements that changed in their parameter 
map. If the
+ * sensor is a collection of maps the elements are provided with their 
keys coerced to
+ * strings and their values unchanged. Otherwise the elements are provided 
in a
+ * single-entry map keyed by the value for {@link #PARAMETER_NAME}.
+ */
+public class InvokeEffectorOnSetChange extends AbstractPolicy implements 
SensorEventListener {
+
+private static final Logger LOG = 
LoggerFactory.getLogger(InvokeEffectorOnSetChange.class);
+
+public static final ConfigKey> TRIGGER_SENSOR = ConfigKeys.newConfigKey(
+new TypeToken>() {},
+"sensor",
+"Sensor to be monitored.");
+
+public static final ConfigKey PREVIOUS_SENSOR_NAME = 
ConfigKeys.newStringConfigKey(
+"previousSensorName",
+"The name under which the previous value for the trigger 
sensor should be published. " +
+"If unset \".previous\" will be appended to the name 
of the trigger sensor.");
+
+public static final ConfigKey ON_ADDED_EFFECTOR_NAME = 
ConfigKeys.newStringConfigKey(
+"onAdded",
+"Name of the effector to invoke when entries are added to the 
collection.");
+
+public static final ConfigKey ON_REMOVED_EFFECTOR_NAME = 
ConfigKeys.newStringConfigKey(
+"onRemoved",
+"Name of the effector to invoke when entries are removed from 
the collection.");
+
+public static final ConfigKey PARAMETER_NAME = 
ConfigKeys.newStringConfigKey(
+"parameterName",
+"The name of the parameter 

[GitHub] brooklyn-server pull request #382: InvokeEffectorOnSetChange

2016-10-13 Thread neykov
Github user neykov commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/382#discussion_r83200686
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/policy/InvokeEffectorOnSetChange.java ---
@@ -0,0 +1,203 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.brooklyn.policy;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.brooklyn.api.effector.Effector;
+import org.apache.brooklyn.api.entity.EntityLocal;
+import org.apache.brooklyn.api.sensor.AttributeSensor;
+import org.apache.brooklyn.api.sensor.Sensor;
+import org.apache.brooklyn.api.sensor.SensorEvent;
+import org.apache.brooklyn.api.sensor.SensorEventListener;
+import org.apache.brooklyn.config.ConfigKey;
+import org.apache.brooklyn.core.config.ConfigKeys;
+import org.apache.brooklyn.core.policy.AbstractPolicy;
+import org.apache.brooklyn.core.sensor.BasicAttributeSensor;
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.flags.TypeCoercions;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.text.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Sets;
+import com.google.common.reflect.TypeToken;
+
+/**
+ * Subscribes to events on a collection {@link AttributeSensor} and 
invokes the named
+ * effectors for each element that was added and removed.
+ * 
+ * The policy only detects replacements of the collection; it 
does not act on
+ * modifications. If the sensor has value A and an element is 
added 
+ * value A'  the on-added effector is not invoked. If 
the sensor is
+ * later set to B the delta is made between A 
and B,
+ * not A' and B.
+ * 
+ * To simplify the detection of additions and removals the collection is 
converted to a
+ * {@link Set}. This means that only a single event will fire for 
duplicate elements in
+ * the collection.
+ * 
+ * The effectors are provided the elements that changed in their parameter 
map. If the
+ * sensor is a collection of maps the elements are provided with their 
keys coerced to
+ * strings and their values unchanged. Otherwise the elements are provided 
in a
+ * single-entry map keyed by the value for {@link #PARAMETER_NAME}.
+ */
+public class InvokeEffectorOnSetChange extends AbstractPolicy implements 
SensorEventListener {
+
+private static final Logger LOG = 
LoggerFactory.getLogger(InvokeEffectorOnSetChange.class);
+
+public static final ConfigKey> TRIGGER_SENSOR = ConfigKeys.newConfigKey(
+new TypeToken>() {},
+"sensor",
+"Sensor to be monitored.");
+
+public static final ConfigKey PREVIOUS_SENSOR_NAME = 
ConfigKeys.newStringConfigKey(
+"previousSensorName",
+"The name under which the previous value for the trigger 
sensor should be published. " +
+"If unset \".previous\" will be appended to the name 
of the trigger sensor.");
+
+public static final ConfigKey ON_ADDED_EFFECTOR_NAME = 
ConfigKeys.newStringConfigKey(
+"onAdded",
+"Name of the effector to invoke when entries are added to the 
collection.");
+
+public static final ConfigKey ON_REMOVED_EFFECTOR_NAME = 
ConfigKeys.newStringConfigKey(
+"onRemoved",
+"Name of the effector to invoke when entries are removed from 
the collection.");
+
+public static final ConfigKey PARAMETER_NAME = 
ConfigKeys.newStringConfigKey(
+"parameterName",
+"The name of the parameter to 

[GitHub] brooklyn-server pull request #382: InvokeEffectorOnSetChange

2016-10-13 Thread neykov
Github user neykov commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/382#discussion_r83197672
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/policy/InvokeEffectorOnSetChange.java ---
@@ -0,0 +1,203 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.brooklyn.policy;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.brooklyn.api.effector.Effector;
+import org.apache.brooklyn.api.entity.EntityLocal;
+import org.apache.brooklyn.api.sensor.AttributeSensor;
+import org.apache.brooklyn.api.sensor.Sensor;
+import org.apache.brooklyn.api.sensor.SensorEvent;
+import org.apache.brooklyn.api.sensor.SensorEventListener;
+import org.apache.brooklyn.config.ConfigKey;
+import org.apache.brooklyn.core.config.ConfigKeys;
+import org.apache.brooklyn.core.policy.AbstractPolicy;
+import org.apache.brooklyn.core.sensor.BasicAttributeSensor;
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.flags.TypeCoercions;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.text.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Sets;
+import com.google.common.reflect.TypeToken;
+
+/**
+ * Subscribes to events on a collection {@link AttributeSensor} and 
invokes the named
+ * effectors for each element that was added and removed.
+ * 
+ * The policy only detects replacements of the collection; it 
does not act on
+ * modifications. If the sensor has value A and an element is 
added 
+ * value A'  the on-added effector is not invoked. If 
the sensor is
+ * later set to B the delta is made between A 
and B,
+ * not A' and B.
+ * 
+ * To simplify the detection of additions and removals the collection is 
converted to a
+ * {@link Set}. This means that only a single event will fire for 
duplicate elements in
+ * the collection.
+ * 
+ * The effectors are provided the elements that changed in their parameter 
map. If the
+ * sensor is a collection of maps the elements are provided with their 
keys coerced to
+ * strings and their values unchanged. Otherwise the elements are provided 
in a
+ * single-entry map keyed by the value for {@link #PARAMETER_NAME}.
+ */
+public class InvokeEffectorOnSetChange extends AbstractPolicy implements 
SensorEventListener {
+
+private static final Logger LOG = 
LoggerFactory.getLogger(InvokeEffectorOnSetChange.class);
+
+public static final ConfigKey> TRIGGER_SENSOR = ConfigKeys.newConfigKey(
+new TypeToken>() {},
+"sensor",
+"Sensor to be monitored.");
+
+public static final ConfigKey PREVIOUS_SENSOR_NAME = 
ConfigKeys.newStringConfigKey(
+"previousSensorName",
+"The name under which the previous value for the trigger 
sensor should be published. " +
+"If unset \".previous\" will be appended to the name 
of the trigger sensor.");
+
+public static final ConfigKey ON_ADDED_EFFECTOR_NAME = 
ConfigKeys.newStringConfigKey(
+"onAdded",
+"Name of the effector to invoke when entries are added to the 
collection.");
+
+public static final ConfigKey ON_REMOVED_EFFECTOR_NAME = 
ConfigKeys.newStringConfigKey(
+"onRemoved",
+"Name of the effector to invoke when entries are removed from 
the collection.");
+
+public static final ConfigKey PARAMETER_NAME = 
ConfigKeys.newStringConfigKey(
+"parameterName",
+"The name of the parameter to 

[GitHub] brooklyn-server pull request #382: InvokeEffectorOnSetChange

2016-10-13 Thread neykov
Github user neykov commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/382#discussion_r83198850
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/policy/InvokeEffectorOnSetChange.java ---
@@ -0,0 +1,203 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.brooklyn.policy;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.brooklyn.api.effector.Effector;
+import org.apache.brooklyn.api.entity.EntityLocal;
+import org.apache.brooklyn.api.sensor.AttributeSensor;
+import org.apache.brooklyn.api.sensor.Sensor;
+import org.apache.brooklyn.api.sensor.SensorEvent;
+import org.apache.brooklyn.api.sensor.SensorEventListener;
+import org.apache.brooklyn.config.ConfigKey;
+import org.apache.brooklyn.core.config.ConfigKeys;
+import org.apache.brooklyn.core.policy.AbstractPolicy;
+import org.apache.brooklyn.core.sensor.BasicAttributeSensor;
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.flags.TypeCoercions;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.text.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Sets;
+import com.google.common.reflect.TypeToken;
+
+/**
+ * Subscribes to events on a collection {@link AttributeSensor} and 
invokes the named
+ * effectors for each element that was added and removed.
+ * 
+ * The policy only detects replacements of the collection; it 
does not act on
+ * modifications. If the sensor has value A and an element is 
added 
+ * value A'  the on-added effector is not invoked. If 
the sensor is
+ * later set to B the delta is made between A 
and B,
+ * not A' and B.
+ * 
+ * To simplify the detection of additions and removals the collection is 
converted to a
+ * {@link Set}. This means that only a single event will fire for 
duplicate elements in
+ * the collection.
+ * 
+ * The effectors are provided the elements that changed in their parameter 
map. If the
+ * sensor is a collection of maps the elements are provided with their 
keys coerced to
+ * strings and their values unchanged. Otherwise the elements are provided 
in a
+ * single-entry map keyed by the value for {@link #PARAMETER_NAME}.
+ */
+public class InvokeEffectorOnSetChange extends AbstractPolicy implements 
SensorEventListener {
+
+private static final Logger LOG = 
LoggerFactory.getLogger(InvokeEffectorOnSetChange.class);
+
+public static final ConfigKey> TRIGGER_SENSOR = ConfigKeys.newConfigKey(
+new TypeToken>() {},
+"sensor",
+"Sensor to be monitored.");
+
+public static final ConfigKey PREVIOUS_SENSOR_NAME = 
ConfigKeys.newStringConfigKey(
+"previousSensorName",
+"The name under which the previous value for the trigger 
sensor should be published. " +
+"If unset \".previous\" will be appended to the name 
of the trigger sensor.");
+
+public static final ConfigKey ON_ADDED_EFFECTOR_NAME = 
ConfigKeys.newStringConfigKey(
+"onAdded",
+"Name of the effector to invoke when entries are added to the 
collection.");
+
+public static final ConfigKey ON_REMOVED_EFFECTOR_NAME = 
ConfigKeys.newStringConfigKey(
+"onRemoved",
+"Name of the effector to invoke when entries are removed from 
the collection.");
+
+public static final ConfigKey PARAMETER_NAME = 
ConfigKeys.newStringConfigKey(
+"parameterName",
+"The name of the parameter to 

[GitHub] brooklyn-server pull request #382: InvokeEffectorOnSetChange

2016-10-13 Thread neykov
Github user neykov commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/382#discussion_r83199029
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/policy/InvokeEffectorOnSetChange.java ---
@@ -0,0 +1,203 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.brooklyn.policy;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.brooklyn.api.effector.Effector;
+import org.apache.brooklyn.api.entity.EntityLocal;
+import org.apache.brooklyn.api.sensor.AttributeSensor;
+import org.apache.brooklyn.api.sensor.Sensor;
+import org.apache.brooklyn.api.sensor.SensorEvent;
+import org.apache.brooklyn.api.sensor.SensorEventListener;
+import org.apache.brooklyn.config.ConfigKey;
+import org.apache.brooklyn.core.config.ConfigKeys;
+import org.apache.brooklyn.core.policy.AbstractPolicy;
+import org.apache.brooklyn.core.sensor.BasicAttributeSensor;
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.flags.TypeCoercions;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.text.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Sets;
+import com.google.common.reflect.TypeToken;
+
+/**
+ * Subscribes to events on a collection {@link AttributeSensor} and 
invokes the named
+ * effectors for each element that was added and removed.
+ * 
+ * The policy only detects replacements of the collection; it 
does not act on
+ * modifications. If the sensor has value A and an element is 
added 
+ * value A'  the on-added effector is not invoked. If 
the sensor is
+ * later set to B the delta is made between A 
and B,
+ * not A' and B.
+ * 
+ * To simplify the detection of additions and removals the collection is 
converted to a
+ * {@link Set}. This means that only a single event will fire for 
duplicate elements in
+ * the collection.
+ * 
+ * The effectors are provided the elements that changed in their parameter 
map. If the
+ * sensor is a collection of maps the elements are provided with their 
keys coerced to
+ * strings and their values unchanged. Otherwise the elements are provided 
in a
+ * single-entry map keyed by the value for {@link #PARAMETER_NAME}.
+ */
+public class InvokeEffectorOnSetChange extends AbstractPolicy implements 
SensorEventListener {
+
+private static final Logger LOG = 
LoggerFactory.getLogger(InvokeEffectorOnSetChange.class);
+
+public static final ConfigKey> TRIGGER_SENSOR = ConfigKeys.newConfigKey(
+new TypeToken>() {},
+"sensor",
+"Sensor to be monitored.");
+
+public static final ConfigKey PREVIOUS_SENSOR_NAME = 
ConfigKeys.newStringConfigKey(
+"previousSensorName",
+"The name under which the previous value for the trigger 
sensor should be published. " +
+"If unset \".previous\" will be appended to the name 
of the trigger sensor.");
+
+public static final ConfigKey ON_ADDED_EFFECTOR_NAME = 
ConfigKeys.newStringConfigKey(
+"onAdded",
+"Name of the effector to invoke when entries are added to the 
collection.");
+
+public static final ConfigKey ON_REMOVED_EFFECTOR_NAME = 
ConfigKeys.newStringConfigKey(
+"onRemoved",
+"Name of the effector to invoke when entries are removed from 
the collection.");
+
+public static final ConfigKey PARAMETER_NAME = 
ConfigKeys.newStringConfigKey(
+"parameterName",
+"The name of the parameter to 

[GitHub] brooklyn-server pull request #382: InvokeEffectorOnSetChange

2016-10-13 Thread neykov
Github user neykov commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/382#discussion_r83198053
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/policy/InvokeEffectorOnSetChange.java ---
@@ -0,0 +1,203 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.brooklyn.policy;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.brooklyn.api.effector.Effector;
+import org.apache.brooklyn.api.entity.EntityLocal;
+import org.apache.brooklyn.api.sensor.AttributeSensor;
+import org.apache.brooklyn.api.sensor.Sensor;
+import org.apache.brooklyn.api.sensor.SensorEvent;
+import org.apache.brooklyn.api.sensor.SensorEventListener;
+import org.apache.brooklyn.config.ConfigKey;
+import org.apache.brooklyn.core.config.ConfigKeys;
+import org.apache.brooklyn.core.policy.AbstractPolicy;
+import org.apache.brooklyn.core.sensor.BasicAttributeSensor;
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.flags.TypeCoercions;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.text.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Sets;
+import com.google.common.reflect.TypeToken;
+
+/**
+ * Subscribes to events on a collection {@link AttributeSensor} and 
invokes the named
+ * effectors for each element that was added and removed.
+ * 
+ * The policy only detects replacements of the collection; it 
does not act on
+ * modifications. If the sensor has value A and an element is 
added 
+ * value A'  the on-added effector is not invoked. If 
the sensor is
+ * later set to B the delta is made between A 
and B,
+ * not A' and B.
+ * 
+ * To simplify the detection of additions and removals the collection is 
converted to a
+ * {@link Set}. This means that only a single event will fire for 
duplicate elements in
+ * the collection.
+ * 
+ * The effectors are provided the elements that changed in their parameter 
map. If the
+ * sensor is a collection of maps the elements are provided with their 
keys coerced to
+ * strings and their values unchanged. Otherwise the elements are provided 
in a
+ * single-entry map keyed by the value for {@link #PARAMETER_NAME}.
+ */
+public class InvokeEffectorOnSetChange extends AbstractPolicy implements 
SensorEventListener {
+
+private static final Logger LOG = 
LoggerFactory.getLogger(InvokeEffectorOnSetChange.class);
+
+public static final ConfigKey> TRIGGER_SENSOR = ConfigKeys.newConfigKey(
+new TypeToken>() {},
+"sensor",
+"Sensor to be monitored.");
+
+public static final ConfigKey PREVIOUS_SENSOR_NAME = 
ConfigKeys.newStringConfigKey(
+"previousSensorName",
+"The name under which the previous value for the trigger 
sensor should be published. " +
+"If unset \".previous\" will be appended to the name 
of the trigger sensor.");
+
+public static final ConfigKey ON_ADDED_EFFECTOR_NAME = 
ConfigKeys.newStringConfigKey(
+"onAdded",
+"Name of the effector to invoke when entries are added to the 
collection.");
+
+public static final ConfigKey ON_REMOVED_EFFECTOR_NAME = 
ConfigKeys.newStringConfigKey(
+"onRemoved",
+"Name of the effector to invoke when entries are removed from 
the collection.");
+
+public static final ConfigKey PARAMETER_NAME = 
ConfigKeys.newStringConfigKey(
+"parameterName",
+"The name of the parameter to 

[GitHub] brooklyn-server pull request #382: InvokeEffectorOnSetChange

2016-10-13 Thread neykov
Github user neykov commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/382#discussion_r83199736
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/policy/InvokeEffectorOnSetChange.java ---
@@ -0,0 +1,203 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.brooklyn.policy;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.brooklyn.api.effector.Effector;
+import org.apache.brooklyn.api.entity.EntityLocal;
+import org.apache.brooklyn.api.sensor.AttributeSensor;
+import org.apache.brooklyn.api.sensor.Sensor;
+import org.apache.brooklyn.api.sensor.SensorEvent;
+import org.apache.brooklyn.api.sensor.SensorEventListener;
+import org.apache.brooklyn.config.ConfigKey;
+import org.apache.brooklyn.core.config.ConfigKeys;
+import org.apache.brooklyn.core.policy.AbstractPolicy;
+import org.apache.brooklyn.core.sensor.BasicAttributeSensor;
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.flags.TypeCoercions;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.text.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Sets;
+import com.google.common.reflect.TypeToken;
+
+/**
+ * Subscribes to events on a collection {@link AttributeSensor} and 
invokes the named
+ * effectors for each element that was added and removed.
+ * 
+ * The policy only detects replacements of the collection; it 
does not act on
+ * modifications. If the sensor has value A and an element is 
added 
+ * value A'  the on-added effector is not invoked. If 
the sensor is
+ * later set to B the delta is made between A 
and B,
+ * not A' and B.
+ * 
+ * To simplify the detection of additions and removals the collection is 
converted to a
+ * {@link Set}. This means that only a single event will fire for 
duplicate elements in
+ * the collection.
+ * 
+ * The effectors are provided the elements that changed in their parameter 
map. If the
+ * sensor is a collection of maps the elements are provided with their 
keys coerced to
+ * strings and their values unchanged. Otherwise the elements are provided 
in a
+ * single-entry map keyed by the value for {@link #PARAMETER_NAME}.
+ */
+public class InvokeEffectorOnSetChange extends AbstractPolicy implements 
SensorEventListener {
+
+private static final Logger LOG = 
LoggerFactory.getLogger(InvokeEffectorOnSetChange.class);
+
+public static final ConfigKey> TRIGGER_SENSOR = ConfigKeys.newConfigKey(
+new TypeToken>() {},
+"sensor",
+"Sensor to be monitored.");
+
+public static final ConfigKey PREVIOUS_SENSOR_NAME = 
ConfigKeys.newStringConfigKey(
+"previousSensorName",
+"The name under which the previous value for the trigger 
sensor should be published. " +
+"If unset \".previous\" will be appended to the name 
of the trigger sensor.");
+
+public static final ConfigKey ON_ADDED_EFFECTOR_NAME = 
ConfigKeys.newStringConfigKey(
+"onAdded",
+"Name of the effector to invoke when entries are added to the 
collection.");
+
+public static final ConfigKey ON_REMOVED_EFFECTOR_NAME = 
ConfigKeys.newStringConfigKey(
+"onRemoved",
+"Name of the effector to invoke when entries are removed from 
the collection.");
+
+public static final ConfigKey PARAMETER_NAME = 
ConfigKeys.newStringConfigKey(
+"parameterName",
+"The name of the parameter to 

[GitHub] brooklyn-server pull request #382: InvokeEffectorOnSetChange

2016-10-13 Thread sjcorbett
GitHub user sjcorbett opened a pull request:

https://github.com/apache/brooklyn-server/pull/382

InvokeEffectorOnSetChange

Tracks a collection and invokes effectors when elements are added and 
removed.

You can merge this pull request into a Git repository by running:

$ git pull https://github.com/sjcorbett/brooklyn-server invoke-set-change

Alternatively you can review and apply these changes as the patch at:

https://github.com/apache/brooklyn-server/pull/382.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

This closes #382


commit 8bbf68a07f86b06900fcd84cf9b6f71669f97bc5
Author: Sam Corbett 
Date:   2016-10-13T10:24:15Z

getEffectorByName returns absent rather than throwing NPE when arg null.

commit 2797e0c23d53ae4e0e3900f8b92e117d1da92783
Author: Sam Corbett 
Date:   2016-10-13T10:24:35Z

Add Sensors.newSensor(TypeToken, name)

commit f3017f6a3e129ebf026da89d98113657aa649c61
Author: Sam Corbett 
Date:   2016-10-13T10:32:43Z

InvokeEffectorOnSetChange

Tracks a collection and invokes effectors when elements are added and
removed.




---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---