Delete SensorPropagatingEnricher Was deprecated in 0.7.0; did not have a constructor usable when deserialising from persisted state, so that backwards compatibility is not a worry!
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/bbe1171d Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/bbe1171d Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/bbe1171d Branch: refs/heads/master Commit: bbe1171d8a00b17bd5a6465755653584185700f8 Parents: f3fe15f Author: Aled Sage <[email protected]> Authored: Sun Jun 11 14:01:56 2017 +0100 Committer: Aled Sage <[email protected]> Committed: Sun Jun 11 14:57:39 2017 +0100 ---------------------------------------------------------------------- .../stock/SensorPropagatingEnricher.java | 186 ------------------- ...SensorPropagatingEnricherDeprecatedTest.java | 107 ----------- 2 files changed, 293 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/bbe1171d/core/src/main/java/org/apache/brooklyn/enricher/stock/SensorPropagatingEnricher.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/enricher/stock/SensorPropagatingEnricher.java b/core/src/main/java/org/apache/brooklyn/enricher/stock/SensorPropagatingEnricher.java deleted file mode 100644 index bb7f2ab..0000000 --- a/core/src/main/java/org/apache/brooklyn/enricher/stock/SensorPropagatingEnricher.java +++ /dev/null @@ -1,186 +0,0 @@ -/* - * 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.enricher.stock; - -import java.util.Collection; -import java.util.Map; -import java.util.Set; - -import org.apache.brooklyn.api.entity.Entity; -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.core.enricher.AbstractEnricher; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Sets; - -/** - * an enricher policy which just listens for the target sensor(s) on a child entity and passes it up. - * now superseded by syntax such as: - * - * <pre>{@code Enrichers.builder().propagating(sensor).from(source).build()}</pre> - * - * @deprecated since 0.7.0; use {@link Enrichers#builder()} - * - * @see Propagator if need to sub-class - */ -@Deprecated -public class SensorPropagatingEnricher extends AbstractEnricher implements SensorEventListener<Object> { - - public static final Logger log = LoggerFactory.getLogger(SensorPropagatingEnricher.class); - - /** the entity to listen to */ - private final Entity source; - - /** the sensors to listen to */ - private final Set<Sensor<?>> sensors; - - /** the sensors to listen to */ - private final Map<Sensor<?>, Sensor<?>> sensorMappings; - - public static SensorPropagatingEnricher newInstanceListeningToAllSensors(Entity source) { - return newInstanceListeningToAllSensorsBut(source); - } - public static SensorPropagatingEnricher newInstanceListeningToAllSensorsBut(Entity source, Sensor<?>... excludes) { - Set<Sensor<?>> excluded = ImmutableSet.copyOf(excludes); - Set<Sensor<?>> includes = Sets.newLinkedHashSet(); - - for (Sensor<?> it : source.getEntityType().getSensors()) { - if (!excluded.contains(it)) includes.add(it); - } - return new SensorPropagatingEnricher(source, includes); - } - - public static SensorPropagatingEnricher newInstanceListeningTo(Entity source, Sensor<?>... includes) { - return new SensorPropagatingEnricher(source, includes); - } - - /** - * listens to sensors from source, propagates them here renamed according to the map - * - * Instead, consider calling: - * <pre> - * {@code - * addEnricher(Enrichers.builder() - * .propagating(mapOfOldSensorNamesToNewSensorNames) - * .from(source) - * .build()); - * } - * </pre> - * - * @deprecated since 0.7.0; use {@link Enrichers#builder()} - */ - @Deprecated - public static SensorPropagatingEnricher newInstanceRenaming(Entity source, Map<? extends Sensor<?>, ? extends Sensor<?>> sensors) { - return new SensorPropagatingEnricher(source, sensors); - } - - /** - * @deprecated since 0.7.0; use {@link Enrichers#builder()} - */ - @Deprecated - public SensorPropagatingEnricher(Entity source, Sensor<?>... sensors) { - this(source, ImmutableList.copyOf(sensors)); - } - - /** - * Instead, consider calling: - * <pre> - * {@code - * addEnricher(Enrichers.builder() - * .propagating(sensors) - * .from(source) - * .build()); - * } - * </pre> - * - * @deprecated since 0.7.0; use {@link Enrichers#builder()} - */ - @Deprecated - public SensorPropagatingEnricher(Entity source, Collection<Sensor<?>> sensors) { - this.source = source; - this.sensors = ImmutableSet.copyOf(sensors); - this.sensorMappings = ImmutableMap.of(); - } - - public SensorPropagatingEnricher(Entity source, Map<? extends Sensor<?>, ? extends Sensor<?>> sensors) { - this.source = source; - this.sensors = ImmutableSet.copyOf(sensors.keySet()); - this.sensorMappings = ImmutableMap.copyOf(sensors); - } - - @Override - public void setEntity(EntityLocal entity) { - super.setEntity(entity); - for (Sensor<?> s: sensors) { - subscriptions().subscribe(source, s, this); - } - } - - @Override - @SuppressWarnings({ "unchecked", "rawtypes" }) - public void onEvent(SensorEvent<Object> event) { - // propagate upwards - Sensor<?> sourceSensor = event.getSensor(); - Sensor<?> destinationSensor = getDestinationSensor(sourceSensor); - - if (log.isTraceEnabled()) log.trace("policy {} got {}, propagating via {}{}", - new Object[] {this, event, entity, (sourceSensor == destinationSensor ? "" : " (as "+destinationSensor+")")}); - - if (event.getSensor() instanceof AttributeSensor) { - entity.sensors().set((AttributeSensor)destinationSensor, event.getValue()); - } else { - entity.sensors().emit((Sensor)destinationSensor, event.getValue()); - } - } - - /** useful post-addition to emit current values */ - public void emitAllAttributes() { - emitAllAttributes(false); - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - public void emitAllAttributes(boolean includeNullValues) { - for (Sensor s: sensors) { - if (s instanceof AttributeSensor) { - AttributeSensor destinationSensor = (AttributeSensor<?>) getDestinationSensor(s); - Object v = source.getAttribute((AttributeSensor)s); - if (v != null || includeNullValues) entity.sensors().set(destinationSensor, v); - } - } - } - - /** convenience, to be called by the host */ - public SensorPropagatingEnricher addToEntityAndEmitAll(Entity host) { - host.enrichers().add(this); - emitAllAttributes(); - return this; - } - - private Sensor<?> getDestinationSensor(Sensor<?> sourceSensor) { - return sensorMappings.containsKey(sourceSensor) ? sensorMappings.get(sourceSensor): sourceSensor; - } -} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/bbe1171d/core/src/test/java/org/apache/brooklyn/enricher/stock/SensorPropagatingEnricherDeprecatedTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/enricher/stock/SensorPropagatingEnricherDeprecatedTest.java b/core/src/test/java/org/apache/brooklyn/enricher/stock/SensorPropagatingEnricherDeprecatedTest.java deleted file mode 100644 index 1377078..0000000 --- a/core/src/test/java/org/apache/brooklyn/enricher/stock/SensorPropagatingEnricherDeprecatedTest.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * 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.enricher.stock; - -import java.util.concurrent.atomic.AtomicReference; - -import org.apache.brooklyn.api.entity.EntitySpec; -import org.apache.brooklyn.api.sensor.AttributeSensor; -import org.apache.brooklyn.api.sensor.SensorEvent; -import org.apache.brooklyn.api.sensor.SensorEventListener; -import org.apache.brooklyn.core.entity.EntityAsserts; -import org.apache.brooklyn.core.sensor.Sensors; -import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; -import org.apache.brooklyn.core.test.entity.TestEntity; -import org.apache.brooklyn.test.Asserts; -import org.apache.brooklyn.util.collections.MutableMap; -import org.apache.brooklyn.util.javalang.AtomicReferences; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import com.google.common.base.Predicates; -import com.google.common.collect.ImmutableMap; - -public class SensorPropagatingEnricherDeprecatedTest extends BrooklynAppUnitTestSupport { - - private TestEntity entity; - - @BeforeMethod(alwaysRun=true) - @Override - public void setUp() throws Exception { - super.setUp(); - entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); - } - - @Test - public void testPropagatesSpecificSensor() { - app.enrichers().add(SensorPropagatingEnricher.newInstanceListeningTo(entity, TestEntity.NAME)); - - // name propagated - entity.sensors().set(TestEntity.NAME, "foo"); - EntityAsserts.assertAttributeEqualsEventually(app, TestEntity.NAME, "foo"); - - // sequence not propagated - entity.sensors().set(TestEntity.SEQUENCE, 2); - EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 100), app, TestEntity.SEQUENCE, null); - } - - @Test - public void testPropagatesAllSensors() { - app.enrichers().add(SensorPropagatingEnricher.newInstanceListeningToAllSensors(entity)); - - // all attributes propagated - entity.sensors().set(TestEntity.NAME, "foo"); - entity.sensors().set(TestEntity.SEQUENCE, 2); - - EntityAsserts.assertAttributeEqualsEventually(app, TestEntity.NAME, "foo"); - EntityAsserts.assertAttributeEqualsEventually(app, TestEntity.SEQUENCE, 2); - - // notification-sensor propagated - final AtomicReference<Integer> notif = new AtomicReference<Integer>(); - app.subscriptions().subscribe(app, TestEntity.MY_NOTIF, new SensorEventListener<Integer>() { - @Override public void onEvent(SensorEvent<Integer> event) { - notif.set(event.getValue()); - }}); - entity.sensors().emit(TestEntity.MY_NOTIF, 7); - Asserts.eventually(AtomicReferences.supplier(notif), Predicates.equalTo(7)); - } - - @Test - public void testPropagatesAllBut() { - app.enrichers().add(SensorPropagatingEnricher.newInstanceListeningToAllSensorsBut(entity, TestEntity.SEQUENCE)) ; - - // name propagated - entity.sensors().set(TestEntity.NAME, "foo"); - EntityAsserts.assertAttributeEqualsEventually(app, TestEntity.NAME, "foo"); - - // sequence not propagated - entity.sensors().set(TestEntity.SEQUENCE, 2); - EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 100), app, TestEntity.SEQUENCE, null); - } - - @Test - public void testPropagatingAsDifferentSensor() { - final AttributeSensor<String> ANOTHER_ATTRIBUTE = Sensors.newStringSensor("another.attribute", ""); - app.enrichers().add(SensorPropagatingEnricher.newInstanceRenaming(entity, ImmutableMap.of(TestEntity.NAME, ANOTHER_ATTRIBUTE))); - - // name propagated as different attribute - entity.sensors().set(TestEntity.NAME, "foo"); - EntityAsserts.assertAttributeEqualsEventually(app, ANOTHER_ATTRIBUTE, "foo"); - } -}
