brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [22/45] git commit: Conditional wrapping of YAML app.
Date Fri, 04 Jul 2014 09:51:04 GMT
Conditional wrapping of YAML app.

When loading YAML with a single application in the services list don't
create a wrapper application, use the one from the services list.
To explicitly force wrapping use custom attribute "wrapApp: true".

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

Branch: refs/heads/master
Commit: c43eb16fdc209a09f4cc7ee895816298bf48a785
Parents: 9bb861b
Author: Svetoslav Neykov <svetoslav.neykov@cloudsoftcorp.com>
Authored: Wed Jul 2 12:52:17 2014 +0300
Committer: Svetoslav Neykov <svetoslav.neykov@cloudsoftcorp.com>
Committed: Wed Jul 2 16:36:13 2014 +0300

----------------------------------------------------------------------
 .../BrooklynAssemblyTemplateInstantiator.java   | 80 +++++++++++++++-----
 .../io/brooklyn/camp/brooklyn/WrapAppTest.java  | 75 ++++++++++++++++++
 2 files changed, 134 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c43eb16f/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java
b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java
index 4ec821f..9492104 100644
--- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java
+++ b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java
@@ -13,6 +13,7 @@ import java.lang.reflect.Constructor;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -211,39 +212,76 @@ public class BrooklynAssemblyTemplateInstantiator implements AssemblyTemplateIns
         // AssemblyTemplates created via PDP, _specifying_ then entities to put in
         final ManagementContext mgmt = getBrooklynManagementContext(platform);
 
-        Map<Entity, EntitySpec<?>> entitySpecs = Maps.newLinkedHashMap();
-
-        BrooklynComponentTemplateResolver appResolver = BrooklynComponentTemplateResolver.Factory.newInstance(mgmt,
template);
-        EntitySpec<StartableApplication> appSpec = appResolver.resolveSpec(StartableApplication.class,
BasicApplicationImpl.class);
-        Application app = appResolver.newEntity(appSpec);
-        entitySpecs.put(app, appSpec);
+        Map<Entity, EntitySpec<?>> rootEntities = Maps.newLinkedHashMap();
+        Map<Entity, EntitySpec<?>> allEntities = Maps.newLinkedHashMap();
+        buildEntities(template, rootEntities, allEntities, mgmt);
         
-        for (ResolvableLink<PlatformComponentTemplate> ctl: template.getPlatformComponentTemplates().links())
{
-            PlatformComponentTemplate appChildComponentTemplate = ctl.resolve();
-            BrooklynComponentTemplateResolver entityResolver = BrooklynComponentTemplateResolver.Factory.newInstance(mgmt,
appChildComponentTemplate);
-            EntitySpec<? extends Entity> spec = entityResolver.resolveSpec();
+        EntitySpec<StartableApplication> appSpec;
+        StartableApplication app;
+        if(shouldWrapInApp(template, rootEntities)) {
+            BrooklynComponentTemplateResolver appResolver = BrooklynComponentTemplateResolver.Factory.newInstance(mgmt,
template);
+            appSpec = appResolver.resolveSpec(StartableApplication.class, BasicApplicationImpl.class);
+            app = appResolver.newEntity(appSpec);
+            setEntitiesParent(rootEntities, app);
+            allEntities.put(app, appSpec);
+        } else {
+            Entry<Entity, EntitySpec<?>> entry = rootEntities.entrySet().iterator().next();
+            app = (StartableApplication)entry.getKey();
+            appSpec = (EntitySpec<StartableApplication>)entry.getValue();
+        }
+        
+        initEntities(mgmt, allEntities);
+        
+        log.info("CAMP placing '{}' under management", appSpec);
+        Entities.startManagement(app, mgmt);
 
-            spec.parent(app);
-            Entity entity = entityResolver.newEntity(spec);
-            entitySpecs.put(entity, spec);
-            buildEntityHierarchy(mgmt, entitySpecs, entity, entityResolver.getChildren(appChildComponentTemplate.getCustomAttributes()));
+        return app;
+    }
+
+    private void setEntitiesParent(Map<Entity, EntitySpec<?>> entities, Application
parentApp) {
+        for(Entry<Entity, EntitySpec<?>> entry : entities.entrySet()) {
+            entry.getValue().parent(parentApp);
+            entry.getKey().setParent(parentApp);
         }
+    }
 
-        for (final Entity entity : entitySpecs.keySet()) {
-            final EntitySpec<?> spec = entitySpecs.get(entity);
+    private void initEntities(final ManagementContext mgmt, Map<Entity, EntitySpec<?>>
entities) {
+        for (Entry<Entity, EntitySpec<?>> entry : entities.entrySet()) {
+            final Entity entity = entry.getKey();
+            
+            @SuppressWarnings("unchecked")
+            final EntitySpec<Entity> spec = (EntitySpec<Entity>)entry.getValue();
             
             ((EntityInternal) entity).getExecutionContext().submit(MutableMap.of(), new Runnable()
{
                 @Override
                 public void run() {
-                    initEntity(mgmt, entity, (EntitySpec<Entity>)spec);
+                    initEntity(mgmt, entity, spec);
                 }
             }).getUnchecked();
         }
-        
-        log.info("CAMP placing '{}' under management", appSpec);
-        Entities.startManagement(app, mgmt);
+    }
 
-        return app;
+    private boolean shouldWrapInApp(AssemblyTemplate template, Map<Entity, EntitySpec<?>>
rootEntities) {
+        return isWrapAppRequested(template) ||
+                rootEntities.size() != 1 ||
+                !(rootEntities.keySet().iterator().next() instanceof StartableApplication);
+    }
+
+    private boolean isWrapAppRequested(AssemblyTemplate template) {
+        return Boolean.TRUE.equals(template.getCustomAttributes().get("wrappedApp"));
+    }
+
+    private void buildEntities(AssemblyTemplate template, Map<Entity, EntitySpec<?>>
parentEntities, 
+            Map<Entity, EntitySpec<?>> allEntities, ManagementContext mgmt) {
+        for (ResolvableLink<PlatformComponentTemplate> ctl: template.getPlatformComponentTemplates().links())
{
+            PlatformComponentTemplate appChildComponentTemplate = ctl.resolve();
+            BrooklynComponentTemplateResolver entityResolver = BrooklynComponentTemplateResolver.Factory.newInstance(mgmt,
appChildComponentTemplate);
+            EntitySpec<? extends Entity> spec = entityResolver.resolveSpec();
+            Entity entity = entityResolver.newEntity(spec);
+            parentEntities.put(entity, spec);
+            allEntities.put(entity, spec);
+            buildEntityHierarchy(mgmt, allEntities, entity, entityResolver.getChildren(appChildComponentTemplate.getCustomAttributes()));
+        }
     }
 
     protected <T extends Entity> void initEntity(ManagementContext mgmt, T entity,
EntitySpec<T> spec) {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c43eb16f/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/WrapAppTest.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/WrapAppTest.java b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/WrapAppTest.java
new file mode 100644
index 0000000..31e9a66
--- /dev/null
+++ b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/WrapAppTest.java
@@ -0,0 +1,75 @@
+package io.brooklyn.camp.brooklyn;
+
+import java.io.StringReader;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import brooklyn.entity.basic.StartableApplication;
+
+public class WrapAppTest extends AbstractYamlTest {
+    private static final String NO_WRAP_APP_IMPLICIT =
+            "name: Empty App\n" +
+            "services:\n" +
+            "   - type: brooklyn.test.entity.TestApplication";
+        
+    private static final String NO_WRAP_APP_EXPLICIT =
+            "name: Empty App\n" +
+            "wrappedApp: false\n" +
+            "services:\n" +
+            "   - type: brooklyn.test.entity.TestApplication";
+        
+    private static final String WRAP_APP_IMPLICIT =
+            "name: Empty App\n" +
+            "services:\n" +
+            "   - type: brooklyn.test.entity.TestApplication\n" +
+            "   - type: brooklyn.test.entity.TestApplication";
+        
+    private static final String WRAP_APP_EXPLICIT =
+            "name: Empty App\n" +
+            "wrappedApp: true\n" +
+            "services:\n" +
+            "   - type: brooklyn.test.entity.TestApplication";
+    
+    private static final String WRAP_ENTITY =
+            "name: Empty App\n" +
+            "services:\n" +
+            "   - type: brooklyn.test.entity.TestEntity";
+    
+    @Test
+    public void testNoWrapAppImplicit() throws Exception {
+        StartableApplication app = createApp(NO_WRAP_APP_IMPLICIT);
+        Assert.assertTrue(app.getChildren().size() == 0);
+    }
+    
+    @Test
+    public void testNoWrapAppExplicit() throws Exception {
+        StartableApplication app = createApp(NO_WRAP_APP_EXPLICIT);
+        Assert.assertTrue(app.getChildren().size() == 0);
+    }
+    
+    @Test
+    public void testWrapAppImplicit() throws Exception {
+        StartableApplication app = createApp(WRAP_APP_IMPLICIT);
+        Assert.assertTrue(app.getChildren().size() == 2);
+    }
+    
+    @Test
+    public void testWrapAppExplicit() throws Exception {
+        StartableApplication app = createApp(WRAP_APP_EXPLICIT);
+        Assert.assertTrue(app.getChildren().size() == 1);
+    }
+    
+    @Test
+    public void testWrapEntity() throws Exception {
+        StartableApplication app = createApp(WRAP_ENTITY);
+        Assert.assertTrue(app.getChildren().size() == 1);
+    }
+    
+    private StartableApplication createApp(String yaml) throws Exception {
+        StringReader in = new StringReader(yaml);
+        StartableApplication app = (StartableApplication)createAndStartApplication(in);
+        in.close();
+        return app;
+    }
+}


Mime
View raw message