brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [2/5] brooklyn-server git commit: Removed OnPublicNetworkEnricher.sensor config
Date Fri, 10 Jun 2016 14:02:38 GMT
Removed OnPublicNetworkEnricher.sensor config

Users must supply “sensors” (e.g. with a single item in the list).
Simplifies the code a lot, and makes the configuration more consistent
for the user.

Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/4d201c31
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/4d201c31
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/4d201c31

Branch: refs/heads/master
Commit: 4d201c31bc15f3a2a67a22e920c225e8efa4d3b4
Parents: a5c16b2
Author: Aled Sage <aled.sage@gmail.com>
Authored: Fri Jun 10 13:12:15 2016 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Fri Jun 10 13:12:15 2016 +0100

----------------------------------------------------------------------
 .../core/network/AbstractOnNetworkEnricher.java | 27 ++----
 .../OnPublicNetworkEnricherRebindTest.java      |  2 +-
 .../network/OnPublicNetworkEnricherTest.java    | 86 +++++++++++++-------
 .../network/OnSubnetNetworkEnricherTest.java    | 22 +----
 4 files changed, 68 insertions(+), 69 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/4d201c31/core/src/main/java/org/apache/brooklyn/core/network/AbstractOnNetworkEnricher.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/network/AbstractOnNetworkEnricher.java
b/core/src/main/java/org/apache/brooklyn/core/network/AbstractOnNetworkEnricher.java
index b7ee12b..3195711 100644
--- a/core/src/main/java/org/apache/brooklyn/core/network/AbstractOnNetworkEnricher.java
+++ b/core/src/main/java/org/apache/brooklyn/core/network/AbstractOnNetworkEnricher.java
@@ -61,23 +61,16 @@ public abstract class AbstractOnNetworkEnricher extends AbstractEnricher
{
     private static final Logger LOG = LoggerFactory.getLogger(AbstractOnNetworkEnricher.class);
 
     @SuppressWarnings("serial")
-    public static final ConfigKey<AttributeSensor<?>> SENSOR = ConfigKeys.newConfigKey(
-            new TypeToken<AttributeSensor<?>>() {}, 
-            "sensor",
-            "The sensor whose mapped value is to be re-published (with suffix \"mapped.networkName\");
"
-                    + "either 'sensor' or 'sensors' should be specified");
-
-    @SuppressWarnings("serial")
     public static ConfigKey<Collection<? extends AttributeSensor<?>>> SENSORS
= ConfigKeys.newConfigKey(
             new TypeToken<Collection<? extends AttributeSensor<?>>>() {},

             "sensors",
             "The multiple sensors whose mapped values are to be re-published (with suffix
\"mapped.networkName\"); "
-                    + "if neither 'sensor' or 'sensors' is specified, defaults to 'mapAll'");
+                    + "if 'sensors' is not specified, defaults to 'mapMatching'");
 
     public static ConfigKey<String> MAP_MATCHING = ConfigKeys.newStringConfigKey(
             "mapMatching",
             "Whether to map all, based on a sensor naming convention (re-published with suffix
\"mapped.networkName\"); "
-                    + "if neither 'sensor' or 'sensors' is specified, defaults to matchin
case-insensitive suffix of "
+                    + "if 'sensors' is not specified, defaults to matching case-insensitive
suffix of "
                     + "'port', 'uri', 'url' or 'endpoint' ",
             "(?i).*(port|uri|url|endpoint)");
 
@@ -360,36 +353,28 @@ public abstract class AbstractOnNetworkEnricher extends AbstractEnricher
{
     }
     
     protected void checkConfig() {
-        AttributeSensor<?> sensor = getConfig(SENSOR);
         Collection<? extends AttributeSensor<?>> sensors = getConfig(SENSORS);
         Maybe<Object> rawMapMatching = config().getRaw(MAP_MATCHING);
         String mapMatching = config().get(MAP_MATCHING);
         
-        if (sensor != null && sensors != null && !sensors.isEmpty()) {
-            throw new IllegalStateException(this+" must not have both 'sensor' and 'sensors'
config");
-        } else if (sensor == null && (sensors == null || sensors.isEmpty())) {
+        if (sensors == null || sensors.isEmpty()) {
             if (Strings.isBlank(mapMatching)) {
-                throw new IllegalStateException(this+" requires one of 'sensor' or 'sensors'
config (when 'mapMatching' is explicitly blank)");
+                throw new IllegalStateException(this+" requires 'sensors' config (when 'mapMatching'
is explicitly blank)");
             }
         } else if (rawMapMatching.isPresent()) {
-            throw new IllegalStateException(this+" must not have explicit 'mapMatching',
and either of 'sensor' or 'sensors' config");
+            throw new IllegalStateException(this+" must not have explicit 'mapMatching' and
'sensors' config");
         }
     }
     
     protected Collection<AttributeSensor<?>> resolveSensorsConfig() {
-        AttributeSensor<?> sensor = getConfig(SENSOR);
         Collection<? extends AttributeSensor<?>> sensors = getConfig(SENSORS);
 
         Collection<AttributeSensor<?>> result = Lists.newArrayList();
-        if (sensor != null) {
-            AttributeSensor<?> typedSensor = (AttributeSensor<?>) entity.getEntityType().getSensor(sensor.getName());
-            result.add(typedSensor != null ? typedSensor : sensor);
-        }
         if (sensors != null) {
             for (Object s : sensors) {
                 AttributeSensor<?> coercedSensor = TypeCoercions.coerce(s, AttributeSensor.class);
                 AttributeSensor<?> typedSensor = (AttributeSensor<?>) entity.getEntityType().getSensor(coercedSensor.getName());
-                result.add(typedSensor != null ? typedSensor : sensor);
+                result.add(typedSensor != null ? typedSensor : coercedSensor);
             }
         }
         return result;

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/4d201c31/core/src/test/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricherRebindTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricherRebindTest.java
b/core/src/test/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricherRebindTest.java
index 1a7c584..b09c091 100644
--- a/core/src/test/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricherRebindTest.java
+++ b/core/src/test/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricherRebindTest.java
@@ -56,7 +56,7 @@ public class OnPublicNetworkEnricherRebindTest extends RebindTestFixtureWithApp
         origEntity.sensors().set(Attributes.SUBNET_ADDRESS, "127.0.0.1");
         
         origEntity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class)
-                .configure(OnPublicNetworkEnricher.SENSOR, Attributes.MAIN_URI));
+                .configure(OnPublicNetworkEnricher.SENSORS, ImmutableList.of(Attributes.MAIN_URI)));
 
         rebind();
         TestEntity newEntity = (TestEntity) Iterables.getOnlyElement(newApp.getChildren());

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/4d201c31/core/src/test/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricherTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricherTest.java
b/core/src/test/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricherTest.java
index fd19c11..fcdf06d 100644
--- a/core/src/test/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricherTest.java
+++ b/core/src/test/java/org/apache/brooklyn/core/network/OnPublicNetworkEnricherTest.java
@@ -34,7 +34,6 @@ import org.apache.brooklyn.core.entity.Attributes;
 import org.apache.brooklyn.core.entity.EntityAsserts;
 import org.apache.brooklyn.core.location.access.PortForwardManager;
 import org.apache.brooklyn.core.location.access.PortForwardManagerLocationResolver;
-import org.apache.brooklyn.core.network.OnPublicNetworkEnricher;
 import org.apache.brooklyn.core.sensor.Sensors;
 import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
 import org.apache.brooklyn.core.test.entity.TestEntity;
@@ -133,7 +132,7 @@ public class OnPublicNetworkEnricherTest extends BrooklynAppUnitTestSupport
{
         }
         
         entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class)
-                .configure(OnPublicNetworkEnricher.SENSOR, sensor));
+                .configure(OnPublicNetworkEnricher.SENSORS, ImmutableList.of(sensor)));
 
         if (setUri == Timing.AFTER) {
             entity.sensors().set(sensor, sensorVal);
@@ -158,7 +157,7 @@ public class OnPublicNetworkEnricherTest extends BrooklynAppUnitTestSupport
{
         entity.addLocations(ImmutableList.of(machine));
         
         entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class)
-                .configure(OnPublicNetworkEnricher.SENSOR, sensor));
+                .configure(OnPublicNetworkEnricher.SENSORS, ImmutableList.of(sensor)));
 
         EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", VERY_SHORT_WAIT),
entity, Sensors.newStringSensor(sensor.getName()+".mapped.public"), null);
     }
@@ -171,7 +170,7 @@ public class OnPublicNetworkEnricherTest extends BrooklynAppUnitTestSupport
{
         entity.addLocations(ImmutableList.of(machine));
         
         entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class)
-                .configure(OnPublicNetworkEnricher.SENSOR, Attributes.MAIN_URI));
+                .configure(OnPublicNetworkEnricher.SENSORS, ImmutableList.of(Attributes.MAIN_URI)));
 
         EntityAsserts.assertAttributeEqualsEventually(entity, Sensors.newStringSensor(Attributes.MAIN_URI.getName()+".mapped.public"),
"http://mypublichost:5678/my/path");
     }
@@ -184,7 +183,7 @@ public class OnPublicNetworkEnricherTest extends BrooklynAppUnitTestSupport
{
         entity.addLocations(ImmutableList.of(machine));
         
         entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class)
-                .configure(OnPublicNetworkEnricher.SENSOR, Attributes.MAIN_URI));
+                .configure(OnPublicNetworkEnricher.SENSORS, ImmutableList.of(Attributes.MAIN_URI)));
 
         EntityAsserts.assertAttributeEqualsEventually(entity, Sensors.newStringSensor(Attributes.MAIN_URI.getName()+".mapped.public"),
"https://mypublichost:5678/my/path");
     }
@@ -198,7 +197,7 @@ public class OnPublicNetworkEnricherTest extends BrooklynAppUnitTestSupport
{
         entity.addLocations(ImmutableList.of(machine));
         
         entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class)
-                .configure(OnPublicNetworkEnricher.SENSOR, Attributes.HTTP_PORT));
+                .configure(OnPublicNetworkEnricher.SENSORS, ImmutableList.of(Attributes.HTTP_PORT)));
 
         EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", VERY_SHORT_WAIT),
entity, Sensors.newStringSensor(Attributes.HTTP_PORT.getName()+".mapped.public"), null);
     }
@@ -211,7 +210,7 @@ public class OnPublicNetworkEnricherTest extends BrooklynAppUnitTestSupport
{
         portForwardManager.associate("myPublicIp", HostAndPort.fromParts("mypublichost",
5678), machine, 1234);
         
         entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class)
-                .configure(OnPublicNetworkEnricher.SENSOR, Attributes.HTTP_PORT));
+                .configure(OnPublicNetworkEnricher.SENSORS, ImmutableList.of(Attributes.HTTP_PORT)));
 
         EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", VERY_SHORT_WAIT),
entity, Sensors.newStringSensor(Attributes.HTTP_PORT.getName()+".mapped.public"), null);
     }
@@ -224,7 +223,7 @@ public class OnPublicNetworkEnricherTest extends BrooklynAppUnitTestSupport
{
         portForwardManager.associate("myPublicIp", HostAndPort.fromParts("mypublichost",
5678), machine, 1234);
         
         entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class)
-                .configure(OnPublicNetworkEnricher.SENSOR, TestEntity.NAME));
+                .configure(OnPublicNetworkEnricher.SENSORS, ImmutableList.of(TestEntity.NAME)));
 
         EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", VERY_SHORT_WAIT),
entity, Sensors.newStringSensor(TestEntity.NAME.getName()+".mapped.public"), null);
     }
@@ -293,16 +292,42 @@ public class OnPublicNetworkEnricherTest extends BrooklynAppUnitTestSupport
{
                 }
             }});
     }
-    
-    protected void assertAttributeEqualsEventually(String sensorName, String expectedVal)
throws Exception {
-        try {
-            EntityAsserts.assertAttributeEqualsEventually(entity, Sensors.newStringSensor(sensorName),
expectedVal);
-        } catch (Exception e) {
-            throw new Exception("Failed assertion for sensor '"+sensorName+"'; attributes
are "+entity.sensors().getAll(), e);
-        }
+
+    @Test
+    public <T> void testDoesNotDoRegexMatchingWhenSensorsSpecified() throws Exception
{
+        AttributeSensor<String> sensor = Sensors.newStringSensor("mysensor");
+        AttributeSensor<Integer> intPort = Sensors.newIntegerSensor("int.port");
+
+        entity.sensors().set(Attributes.SUBNET_ADDRESS, "127.0.0.1");
+        entity.sensors().set(intPort, 1234);
+        entity.sensors().set(sensor, "127.0.0.1:1234");
+        portForwardManager.associate("myPublicIp", HostAndPort.fromParts("mypublichost",
5678), machine, 1234);
+        entity.addLocations(ImmutableList.of(machine));
+        
+        entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class)
+                .configure(OnPublicNetworkEnricher.SENSORS, ImmutableList.of(sensor)));
+
+        assertAttributeEqualsEventually("mysensor.mapped.public", "mypublichost:5678");
+        assertAttributeEqualsContinually("int.endpoint.mapped.public", null, VERY_SHORT_WAIT);
     }
     
     @Test
+    @SuppressWarnings({ "unchecked", "rawtypes" })
+    public <T> void testCoercesSensorName() throws Exception {
+        AttributeSensor<String> sensor = Sensors.newStringSensor("mysensor");
+
+        entity.sensors().set(sensor, "127.0.0.1:1234");
+        portForwardManager.associate("myPublicIp", HostAndPort.fromParts("mypublichost",
5678), machine, 1234);
+        entity.addLocations(ImmutableList.of(machine));
+        
+        // Ugly casting in java, but easy to get passed this when constructed from YAML
+        entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class)
+                .configure(OnPublicNetworkEnricher.SENSORS, ((List<AttributeSensor<?>>)(List)ImmutableList.of("mysensor"))));
+
+        assertAttributeEqualsEventually("mysensor.mapped.public", "mypublichost:5678");
+    }
+
+    @Test
     public void testSensorNameConverter() throws Exception {
         OnPublicNetworkEnricher enricher = entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class));
         Function<? super String, String> converter = enricher.getConfig(OnPublicNetworkEnricher.SENSOR_NAME_CONVERTER);
@@ -324,24 +349,27 @@ public class OnPublicNetworkEnricherTest extends BrooklynAppUnitTestSupport
{
         }
     }
     
-    @Test(expectedExceptions=IllegalStateException.class, expectedExceptionsMessageRegExp=".*must
not have both 'sensor' and 'sensors'.*")
-    public void testFailsIfSensorAndSensorsConfigured() throws Exception {
+    @Test(expectedExceptions=IllegalStateException.class, expectedExceptionsMessageRegExp=".*must
not have explicit 'mapMatching' and 'sensors'.*")
+    public void testFailsIfSensorsAndMapMatchingConfigured() throws Exception {
         entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class)
-                .configure(OnPublicNetworkEnricher.SENSOR, Attributes.HTTP_PORT)
-                .configure(OnPublicNetworkEnricher.SENSORS, ImmutableList.of(Attributes.HTTPS_PORT)));
+                .configure(OnPublicNetworkEnricher.SENSORS, ImmutableList.of(Attributes.HTTPS_PORT))
+                .configure(OnPublicNetworkEnricher.MAP_MATCHING, ".*uri"));
     }
     
-    @Test(expectedExceptions=IllegalStateException.class, expectedExceptionsMessageRegExp=".*must
not have explicit 'mapMatching', and either of 'sensor' or 'sensors'.*")
-    public void testFailsIfSensorAndMapMatchingConfigured() throws Exception {
-        entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class)
-                .configure(OnPublicNetworkEnricher.SENSOR, Attributes.HTTP_PORT)
-                .configure(OnPublicNetworkEnricher.MAP_MATCHING, ".*uri"));
+    protected void assertAttributeEqualsEventually(String sensorName, String expectedVal)
throws Exception {
+        try {
+            EntityAsserts.assertAttributeEqualsEventually(entity, Sensors.newStringSensor(sensorName),
expectedVal);
+        } catch (Exception e) {
+            throw new Exception("Failed assertion for sensor '"+sensorName+"'; attributes
are "+entity.sensors().getAll(), e);
+        }
     }
     
-    @Test(expectedExceptions=IllegalStateException.class, expectedExceptionsMessageRegExp=".*must
not have explicit 'mapMatching', and either of 'sensor' or 'sensors'.*")
-    public void testFailsIfSensorsAndMapMatchingConfigured() throws Exception {
-        entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class)
-                .configure(OnPublicNetworkEnricher.SENSORS, ImmutableList.of(Attributes.HTTPS_PORT))
-                .configure(OnPublicNetworkEnricher.MAP_MATCHING, ".*uri"));
+    protected void assertAttributeEqualsContinually(String sensorName, String expectedVal,
Duration duration) throws Exception {
+        try {
+            EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", duration),
entity, Sensors.newStringSensor(sensorName), expectedVal);
+        } catch (Exception e) {
+            throw new Exception("Failed assertion for sensor '"+sensorName+"'; attributes
are "+entity.sensors().getAll(), e);
+        }
     }
 }
+

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/4d201c31/core/src/test/java/org/apache/brooklyn/core/network/OnSubnetNetworkEnricherTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/network/OnSubnetNetworkEnricherTest.java
b/core/src/test/java/org/apache/brooklyn/core/network/OnSubnetNetworkEnricherTest.java
index 3345dc8..4f7dac5 100644
--- a/core/src/test/java/org/apache/brooklyn/core/network/OnSubnetNetworkEnricherTest.java
+++ b/core/src/test/java/org/apache/brooklyn/core/network/OnSubnetNetworkEnricherTest.java
@@ -126,7 +126,7 @@ public class OnSubnetNetworkEnricherTest extends BrooklynAppUnitTestSupport
{
         }
         
         entity.enrichers().add(EnricherSpec.create(OnSubnetNetworkEnricher.class)
-                .configure(OnSubnetNetworkEnricher.SENSOR, sensor));
+                .configure(OnSubnetNetworkEnricher.SENSORS, ImmutableList.of(sensor)));
 
         if (setUri == Timing.AFTER) {
             entity.sensors().set(sensor, sensorVal);
@@ -147,7 +147,7 @@ public class OnSubnetNetworkEnricherTest extends BrooklynAppUnitTestSupport
{
         entity.addLocations(ImmutableList.of(machine));
         
         entity.enrichers().add(EnricherSpec.create(OnSubnetNetworkEnricher.class)
-                .configure(OnSubnetNetworkEnricher.SENSOR, sensor));
+                .configure(OnSubnetNetworkEnricher.SENSORS, ImmutableList.of(sensor)));
 
         EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", VERY_SHORT_WAIT),
entity, Sensors.newStringSensor(sensor.getName()+".mapped.subnet"), null);
     }
@@ -157,7 +157,7 @@ public class OnSubnetNetworkEnricherTest extends BrooklynAppUnitTestSupport
{
         entity.sensors().set(Attributes.HTTP_PORT, 1234);
         
         entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class)
-                .configure(OnPublicNetworkEnricher.SENSOR, Attributes.HTTP_PORT));
+                .configure(OnPublicNetworkEnricher.SENSORS, ImmutableList.of(Attributes.HTTP_PORT)));
 
         EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", VERY_SHORT_WAIT),
entity, Sensors.newStringSensor(Attributes.HTTP_PORT.getName()+".mapped.subnet"), null);
     }
@@ -255,21 +255,7 @@ public class OnSubnetNetworkEnricherTest extends BrooklynAppUnitTestSupport
{
         }
     }
     
-    @Test(expectedExceptions=IllegalStateException.class, expectedExceptionsMessageRegExp=".*must
not have both 'sensor' and 'sensors'.*")
-    public void testFailsIfSensorAndSensorsConfigured() throws Exception {
-        entity.enrichers().add(EnricherSpec.create(OnSubnetNetworkEnricher.class)
-                .configure(OnSubnetNetworkEnricher.SENSOR, Attributes.HTTP_PORT)
-                .configure(OnSubnetNetworkEnricher.SENSORS, ImmutableList.of(Attributes.HTTPS_PORT)));
-    }
-    
-    @Test(expectedExceptions=IllegalStateException.class, expectedExceptionsMessageRegExp=".*must
not have explicit 'mapMatching', and either of 'sensor' or 'sensors'.*")
-    public void testFailsIfSensorAndMapMatchingConfigured() throws Exception {
-        entity.enrichers().add(EnricherSpec.create(OnSubnetNetworkEnricher.class)
-                .configure(OnSubnetNetworkEnricher.SENSOR, Attributes.HTTP_PORT)
-                .configure(OnSubnetNetworkEnricher.MAP_MATCHING, ".*uri"));
-    }
-    
-    @Test(expectedExceptions=IllegalStateException.class, expectedExceptionsMessageRegExp=".*must
not have explicit 'mapMatching', and either of 'sensor' or 'sensors'.*")
+    @Test(expectedExceptions=IllegalStateException.class, expectedExceptionsMessageRegExp=".*must
not have explicit 'mapMatching' and 'sensors'.*")
     public void testFailsIfSensorsAndMapMatchingConfigured() throws Exception {
         entity.enrichers().add(EnricherSpec.create(OnSubnetNetworkEnricher.class)
                 .configure(OnSubnetNetworkEnricher.SENSORS, ImmutableList.of(Attributes.HTTPS_PORT))


Mime
View raw message