brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [2/6] brooklyn-server git commit: Inferring required open ports depending on config key value type
Date Wed, 15 Jun 2016 22:52:20 GMT
Inferring required open ports depending on config key value type


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

Branch: refs/heads/master
Commit: 61d138e1a320ecf692686c8fae4b1585206217d3
Parents: b2ffcf2
Author: Kiuby88 <kiuby88.programming@gmail.com>
Authored: Wed May 11 18:21:49 2016 +0200
Committer: Kiuby88 <kiuby88.programming@gmail.com>
Committed: Wed May 11 18:25:46 2016 +0200

----------------------------------------------------------------------
 .../entity/software/base/InboundPortsUtils.java | 28 +++++++++++++++++--
 .../software/base/InboundPortsUtilsTest.java    | 29 +++++++++++++++-----
 2 files changed, 47 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/61d138e1/software/base/src/main/java/org/apache/brooklyn/entity/software/base/InboundPortsUtils.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/InboundPortsUtils.java
b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/InboundPortsUtils.java
index 73e7a86..2d11ed7 100644
--- a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/InboundPortsUtils.java
+++ b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/InboundPortsUtils.java
@@ -21,6 +21,7 @@ package org.apache.brooklyn.entity.software.base;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Sets;
 import com.google.common.reflect.TypeToken;
+
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.location.PortRange;
 import org.apache.brooklyn.config.ConfigKey;
@@ -77,12 +78,15 @@ public class InboundPortsUtils {
             Set<ConfigKey<?>> configKeys = Sets.newHashSet(extraConfigKeys);
             configKeys.addAll(entity.getEntityType().getConfigKeys());
             // Also add dynamically added config keys
-            configKeys.addAll(((EntityInternal)entity).config().getBag().getAllConfigAsConfigKeyMap().keySet());
+            configKeys.addAll(((EntityInternal) entity).config().getBag().getAllConfigAsConfigKeyMap().keySet());
+
+            if (portRegex == null) {
+                portRegex = ".*\\.port"; // defaults to legacy regex if not specified
+            }
 
-            if (portRegex == null) portRegex = ".*\\.port"; // defaults to legacy regex if
not specified
             Pattern portsPattern = Pattern.compile(portRegex);
             for (ConfigKey<?> k : configKeys) {
-                if (PortRange.class.isAssignableFrom(k.getType()) || portsPattern.matcher(k.getName()).matches())
{
+                if (isAssignableFromPortConfigKey(entity, k, portsPattern)) {
                     Object value = entity.config().get(k);
                     Maybe<PortRange> maybePortRange = TypeCoercions.tryCoerce(value,
new TypeToken<PortRange>() {
                     });
@@ -98,4 +102,22 @@ public class InboundPortsUtils {
         log.debug("getRequiredOpenPorts detected default {} for {}", ports, entity);
         return ports;
     }
+
+    /**
+     * Checks if a configkey can be managed as a config key {@link PortRange}. The method
checks if
+     * the config type or the entity config key value are assignable from the {@link PortRange}.
+     * Moreover the method retrieves true if the config key name matches with pattern.
+     *
+     * @param entity    the entity
+     * @param configKey the configkey that could be assigned as a {@link PortRange}
+     * @param portsPattern   pattern to recognized Config key {@link PortRange} of the name
+     * @return if the config key is recognized as a config key {@link PortRange}
+     */
+    private static boolean isAssignableFromPortConfigKey(Entity entity, ConfigKey<?>
configKey, Pattern portsPattern) {
+        log.info(configKey.getName());
+        return PortRange.class.isAssignableFrom(configKey.getType())
+                || (entity.config().get(configKey) instanceof PortRange)
+                || (portsPattern.matcher(configKey.getName()).matches());
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/61d138e1/software/base/src/test/java/org/apache/brooklyn/entity/software/base/InboundPortsUtilsTest.java
----------------------------------------------------------------------
diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/InboundPortsUtilsTest.java
b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/InboundPortsUtilsTest.java
index fe18191..0cd4043 100644
--- a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/InboundPortsUtilsTest.java
+++ b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/InboundPortsUtilsTest.java
@@ -21,18 +21,14 @@ package org.apache.brooklyn.entity.software.base;
 import java.util.Collection;
 
 import org.apache.brooklyn.api.entity.EntitySpec;
-import org.apache.brooklyn.api.mgmt.ManagementContext;
 import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.ConfigKeys;
-import org.apache.brooklyn.core.entity.Entities;
+import org.apache.brooklyn.core.location.PortRanges;
+import org.apache.brooklyn.core.sensor.PortAttributeSensorAndConfigKey;
 import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
-import org.apache.brooklyn.core.test.entity.TestApplication;
 import org.apache.brooklyn.core.test.entity.TestEntity;
-import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation;
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.Test;
 import org.testng.Assert;
+import org.testng.annotations.Test;
 
 import com.google.common.collect.ImmutableSet;
 
@@ -50,4 +46,23 @@ public class InboundPortsUtilsTest extends BrooklynAppUnitTestSupport {
         Collection<Integer> dynamicRequiredOpenPorts = InboundPortsUtils.getRequiredOpenPorts(entity,
ImmutableSet.<ConfigKey<?>>of(), true, null);
         Assert.assertEquals(dynamicRequiredOpenPorts, ImmutableSet.of(9999), "Expected new
port to be added");
     }
+
+    @Test
+    public void testGetRequiredOpenPortsGetsDynamicallyAddedPortBasedKeys() {
+        TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
+
+        PortAttributeSensorAndConfigKey newTestConfigKeyPort = ConfigKeys.newPortSensorAndConfigKey("new.test.config.port.string.first",
"port", "7777+");
+        PortAttributeSensorAndConfigKey newTestConfigKeyPort2 = ConfigKeys.newPortSensorAndConfigKey("new.test.config.port.string.second",
"port");
+
+        ConfigKey<String> newTestConfigKeyString = ConfigKeys.newStringConfigKey("new.test.config.key.string");
+        entity.config().set(newTestConfigKeyPort, PortRanges.fromString("8888+"));
+        entity.config().set(newTestConfigKeyPort2, PortRanges.fromInteger(9999));
+        entity.config().set(newTestConfigKeyString, "foo.bar");
+
+        Collection<Integer> dynamicRequiredOpenPorts = InboundPortsUtils.getRequiredOpenPorts(entity,
ImmutableSet.<ConfigKey<?>>of(), true, null);
+        Assert.assertTrue(dynamicRequiredOpenPorts.contains(8888));
+        Assert.assertTrue(dynamicRequiredOpenPorts.contains(9999));
+    }
+
+
 }


Mime
View raw message