You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by al...@apache.org on 2015/07/24 07:07:21 UTC

[2/4] incubator-brooklyn git commit: Apply root flags to catalog items (a.k.a. brooklyn.flags)

Apply root flags to catalog items (a.k.a. brooklyn.flags)

Root flags weren't applied to catalog items because BrooklynEntityMatcher filtered known root entries based on the entity type and for catalog items it didn't have the type. Instead delegate filtering the flags to later stages, leaving to BrooklynEntityMatcher to put any unused attributes in a brooklyn.flags section.


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/68f5bd7d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/68f5bd7d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/68f5bd7d

Branch: refs/heads/master
Commit: 68f5bd7d3814e433589fa6adf010aef66f969021
Parents: ea76389
Author: Svetoslav Neykov <sv...@cloudsoftcorp.com>
Authored: Thu Jul 16 17:20:30 2015 +0300
Committer: Svetoslav Neykov <sv...@cloudsoftcorp.com>
Committed: Thu Jul 16 17:38:48 2015 +0300

----------------------------------------------------------------------
 .../BrooklynComponentTemplateResolver.java      | 12 ++-
 .../spi/creation/BrooklynEntityMatcher.java     | 88 +++++---------------
 .../camp/brooklyn/EntitiesYamlTest.java         | 38 +++++++++
 .../brooklyn/catalog/CatalogYamlEntityTest.java | 48 +++++++++++
 4 files changed, 115 insertions(+), 71 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/68f5bd7d/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java
index e26240f..ca4748f 100644
--- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java
+++ b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java
@@ -348,11 +348,17 @@ public class BrooklynComponentTemplateResolver {
 
     @SuppressWarnings({ "unchecked", "rawtypes" })
     protected void configureEntityConfig(EntitySpec<?> spec) {
-        ConfigBag bag = ConfigBag.newInstance((Map<Object, Object>) attrs.getStringKey("brooklyn.config"));
-
         // first take *recognised* flags and config keys from the top-level, and put them in the bag (of brooklyn.config)
-        // (for component templates this will have been done already by BrooklynEntityMatcher, but for specs it is needed here)
+        // attrs will contain only brooklyn.xxx properties when coming from BrooklynEntityMatcher.
+        // Any top-level flags will go into "brooklyn.flags". When resolving a spec from $brooklyn:entitySpec
+        // top level flags remain in place. Have to support both cases.
+
+        ConfigBag bag = ConfigBag.newInstance((Map<Object, Object>) attrs.getStringKey(BrooklynCampReservedKeys.BROOKLYN_CONFIG));
         ConfigBag bagFlags = ConfigBag.newInstanceCopying(attrs);
+        if (attrs.containsKey(BrooklynCampReservedKeys.BROOKLYN_FLAGS)) {
+            bagFlags.putAll((Map<String, Object>) attrs.getStringKey(BrooklynCampReservedKeys.BROOKLYN_FLAGS));
+        }
+
         List<FlagConfigKeyAndValueRecord> topLevelApparentConfig = FlagUtils.findAllFlagsAndConfigKeys(null, spec.getType(), bagFlags);
         for (FlagConfigKeyAndValueRecord r: topLevelApparentConfig) {
             if (r.getConfigKeyMaybeValue().isPresent())

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/68f5bd7d/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityMatcher.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityMatcher.java b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityMatcher.java
index 50b57e9..699bc12 100644
--- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityMatcher.java
+++ b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityMatcher.java
@@ -19,6 +19,7 @@
 package io.brooklyn.camp.brooklyn.spi.creation;
 
 import io.brooklyn.camp.brooklyn.BrooklynCampConstants;
+import io.brooklyn.camp.brooklyn.BrooklynCampReservedKeys;
 import io.brooklyn.camp.spi.PlatformComponentTemplate;
 import io.brooklyn.camp.spi.PlatformComponentTemplate.Builder;
 import io.brooklyn.camp.spi.pdp.AssemblyTemplateConstructor;
@@ -32,15 +33,10 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import brooklyn.catalog.internal.BasicBrooklynCatalog;
-import brooklyn.entity.Entity;
 import brooklyn.management.ManagementContext;
 import brooklyn.management.classloading.BrooklynClassLoadingContext;
 import brooklyn.management.classloading.JavaBrooklynClassLoadingContext;
 import brooklyn.util.collections.MutableMap;
-import brooklyn.util.config.ConfigBag;
-import brooklyn.util.exceptions.Exceptions;
-import brooklyn.util.flags.FlagUtils;
-import brooklyn.util.flags.FlagUtils.FlagConfigKeyAndValueRecord;
 import brooklyn.util.net.Urls;
 import brooklyn.util.text.Strings;
 
@@ -131,37 +127,28 @@ public class BrooklynEntityMatcher implements PdpMatcher {
         if (locations!=null)
             builder.customAttribute("locations", locations);
 
-        MutableMap<Object, Object> brooklynConfig = MutableMap.of();
-        Object origBrooklynConfig = attrs.remove("brooklyn.config");
-        if (origBrooklynConfig!=null) {
-            if (!(origBrooklynConfig instanceof Map))
-                throw new IllegalArgumentException("brooklyn.config must be a map of brooklyn config keys");
-            brooklynConfig.putAll((Map<?,?>)origBrooklynConfig);
+        MutableMap<Object, Object> brooklynFlags = MutableMap.of();
+        Object origBrooklynFlags = attrs.remove(BrooklynCampReservedKeys.BROOKLYN_FLAGS);
+        if (origBrooklynFlags!=null) {
+            if (!(origBrooklynFlags instanceof Map))
+                throw new IllegalArgumentException("brooklyn.flags must be a map of brooklyn flags");
+            brooklynFlags.putAll((Map<?,?>)origBrooklynFlags);
         }
-        // also take *recognised* flags and config keys from the top-level, and put them under config
-        List<FlagConfigKeyAndValueRecord> topLevelApparentConfig = extractValidConfigFlagsOrKeys(type, attrs, true);
-        if (topLevelApparentConfig!=null) for (FlagConfigKeyAndValueRecord r: topLevelApparentConfig) {
-            if (r.getConfigKeyMaybeValue().isPresent())
-                brooklynConfig.put(r.getConfigKey(), r.getConfigKeyMaybeValue().get());
-            if (r.getFlagMaybeValue().isPresent())
-                brooklynConfig.put(r.getFlagName(), r.getFlagMaybeValue().get());
-        }
-        // (any other brooklyn config goes here)
-        if (!brooklynConfig.isEmpty())
-            builder.customAttribute("brooklyn.config", brooklynConfig);
-
-        addCustomListAttributeIfNonNull(builder, attrs, "brooklyn.policies");
-        addCustomListAttributeIfNonNull(builder, attrs, "brooklyn.enrichers");
-        addCustomListAttributeIfNonNull(builder, attrs, "brooklyn.initializers");
-        addCustomListAttributeIfNonNull(builder, attrs, "brooklyn.children");
-        addCustomMapAttributeIfNonNull(builder, attrs, "brooklyn.catalog");
-
-        if (!attrs.isEmpty()) {
-            log.warn("Ignoring PDP attributes on "+deploymentPlanItem+": "+attrs);
+
+        addCustomMapAttributeIfNonNull(builder, attrs, BrooklynCampReservedKeys.BROOKLYN_CONFIG);
+        addCustomListAttributeIfNonNull(builder, attrs, BrooklynCampReservedKeys.BROOKLYN_POLICIES);
+        addCustomListAttributeIfNonNull(builder, attrs, BrooklynCampReservedKeys.BROOKLYN_ENRICHERS);
+        addCustomListAttributeIfNonNull(builder, attrs, BrooklynCampReservedKeys.BROOKLYN_INITIALIZERS);
+        addCustomListAttributeIfNonNull(builder, attrs, BrooklynCampReservedKeys.BROOKLYN_CHILDREN);
+        addCustomMapAttributeIfNonNull(builder, attrs, BrooklynCampReservedKeys.BROOKLYN_CATALOG);
+
+        brooklynFlags.putAll(attrs);
+        if (!brooklynFlags.isEmpty()) {
+            builder.customAttribute(BrooklynCampReservedKeys.BROOKLYN_FLAGS, brooklynFlags);
         }
-        
+
         atc.add(builder.build());
-        
+
         return true;
     }
 
@@ -203,39 +190,4 @@ public class BrooklynEntityMatcher implements PdpMatcher {
         }
     }
 
-    /** finds flags and keys on the given typeName which are present in the given map;
-     * returns those (using the config key name), and removes them from attrs
-     */
-    protected List<FlagConfigKeyAndValueRecord> extractValidConfigFlagsOrKeys(String typeName, Map<String, Object> attrs, boolean removeIfFound) {
-        if (attrs==null || attrs.isEmpty())
-            return null;
-        try {
-            // TODO don't use the mgmt loader, but instead use something like catalog.createSpec
-            // currently we get warnings and are unable to retrieve flags for items which come from catalog 
-            Class<? extends Entity> type = BrooklynComponentTemplateResolver.Factory.newInstance(JavaBrooklynClassLoadingContext.create(mgmt), typeName).loadEntityClass();
-            ConfigBag bag = ConfigBag.newInstance(attrs);
-            List<FlagConfigKeyAndValueRecord> values = FlagUtils.findAllFlagsAndConfigKeys(null, type, bag);
-            
-            if (removeIfFound) {
-                // remove from attrs
-                MutableMap<String, Object> used = MutableMap.copyOf(bag.getAllConfig());
-                for (String unusedKey: bag.getUnusedConfig().keySet())
-                    used.remove(unusedKey);
-                for (String usedKey: used.keySet())
-                    attrs.remove(usedKey);
-            }
-            
-            return values;
-        } catch (Exception e) {
-            Exceptions.propagateIfFatal(e);
-            if (e.toString().contains("Could not find")) {
-                // normal for catalog items, there will be no java type
-                log.debug("Ignoring configuration attributes on "+typeName+", details: "+e);
-            } else {
-                log.warn("Ignoring configuration attributes on "+typeName+" due to "+e, e);
-            }
-            return null;
-        }
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/68f5bd7d/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EntitiesYamlTest.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EntitiesYamlTest.java b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EntitiesYamlTest.java
index 6deda0f..9885686 100644
--- a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EntitiesYamlTest.java
+++ b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EntitiesYamlTest.java
@@ -171,6 +171,23 @@ public class EntitiesYamlTest extends AbstractYamlTest {
         Assert.assertNull(dynamicConfNameValue);
     }
 
+    @Test
+    public void testExplicitFlags() throws Exception {
+        Entity testEntity = setupAndCheckTestEntityInBasicYamlWith( 
+            "  brooklyn.flags:",
+            "    confName: Foo Bar");
+        Assert.assertEquals(testEntity.getConfig(TestEntity.CONF_NAME), "Foo Bar");
+    }
+
+    @Test
+    public void testUndeclaredExplicitFlagsIgnored() throws Exception {
+        Entity testEntity = setupAndCheckTestEntityInBasicYamlWith( 
+            "  brooklyn.flags:",
+            "    test.dynamic.confName: Foo Bar");
+        String dynamicConfNameValue = testEntity.getConfig(ConfigKeys.newStringConfigKey("test.dynamic.confName"));
+        Assert.assertNull(dynamicConfNameValue);
+    }
+
     @SuppressWarnings("unchecked")
     @Test
     public void testEmptyConfig() throws Exception {
@@ -624,6 +641,27 @@ public class EntitiesYamlTest extends AbstractYamlTest {
     }
 
     @Test
+    public void testEntitySpecExplicitFlags() throws Exception {
+        String yaml =
+                "services:\n"+
+                "- serviceType: brooklyn.test.entity.TestEntity\n"+
+                "  brooklyn.flags:\n"+
+                "    confName: inParent\n"+
+                "  brooklyn.config:\n"+
+                "   test.childSpec:\n"+
+                "     $brooklyn:entitySpec:\n"+
+                "       type: brooklyn.test.entity.TestEntity\n"+
+                "       brooklyn.flags:\n"+
+                "         confName: inchildspec\n";
+        
+        Application app = (Application) createStartWaitAndLogApplication(new StringReader(yaml));
+        TestEntity entity = (TestEntity) Iterables.getOnlyElement(app.getChildren());
+        
+        TestEntity child = (TestEntity) entity.createAndManageChildFromConfig();
+        assertEquals(child.getConfig(TestEntity.CONF_NAME), "inchildspec");
+    }
+
+    @Test
     public void testEntitySpecWithChildren() throws Exception {
         String yaml =
                 "services:\n"+

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/68f5bd7d/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/catalog/CatalogYamlEntityTest.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/catalog/CatalogYamlEntityTest.java b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/catalog/CatalogYamlEntityTest.java
index 6e17bfb..fecbe43 100644
--- a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/catalog/CatalogYamlEntityTest.java
+++ b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/catalog/CatalogYamlEntityTest.java
@@ -38,6 +38,7 @@ import brooklyn.entity.basic.BasicEntity;
 import brooklyn.management.osgi.OsgiStandaloneTest;
 import brooklyn.management.osgi.OsgiTestResources;
 import brooklyn.test.TestResourceUnavailableException;
+import brooklyn.test.entity.TestEntity;
 import brooklyn.util.ResourceUtils;
 import brooklyn.util.collections.MutableList;
 import brooklyn.util.exceptions.Exceptions;
@@ -609,7 +610,54 @@ public class CatalogYamlEntityTest extends AbstractYamlTest {
             assertTrue(e.toString().contains("recursive"), "Unexpected error message: "+e);
         }
     }
+
+    @Test
+    public void testConfigAppliedToCatalogItem() throws Exception {
+        addTestEntityCatalogItem();
+        String testName = "test-applies-config-on-catalog-item";
+        Entity app = createAndStartApplication(
+                "services:",
+                "- type: " + ver("test"),
+                "  brooklyn.config:",
+                "    test.confName: " + testName);
+        Entity testEntity = Iterables.getOnlyElement(app.getChildren());
+        assertEquals(testEntity.config().get(TestEntity.CONF_NAME), testName);
+    }
+
+    @Test
+    public void testFlagsAppliesToCatalogItem() throws Exception {
+        addTestEntityCatalogItem();
+        String testName = "test-applies-config-on-catalog-item";
+        Entity app = createAndStartApplication(
+                "services:",
+                "- type: " + ver("test"),
+                "  confName: " + testName);
+        Entity testEntity = Iterables.getOnlyElement(app.getChildren());
+        assertEquals(testEntity.config().get(TestEntity.CONF_NAME), testName);
+    }
+
+    @Test
+    public void testExplicitFlagsAppliesToCatalogItem() throws Exception {
+        addTestEntityCatalogItem();
+        String testName = "test-applies-config-on-catalog-item";
+        Entity app = createAndStartApplication(
+                "services:",
+                "- type: " + ver("test"),
+                "  brooklyn.flags:",
+                "    confName: " + testName);
+        Entity testEntity = Iterables.getOnlyElement(app.getChildren());
+        assertEquals(testEntity.config().get(TestEntity.CONF_NAME), testName);
+    }
     
+    private void addTestEntityCatalogItem() {
+        addCatalogItems(
+                "brooklyn.catalog:",
+                "   id: test",
+                "   version: " + TEST_VERSION,
+                "services:",
+                "- type: " + TestEntity.class.getName());
+    }
+
     private void registerAndLaunchAndAssertSimpleEntity(String symbolicName, String serviceType) throws Exception {
         addCatalogOSGiEntity(symbolicName, serviceType);
         String yaml = "name: simple-app-yaml\n" +