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

[12/21] incubator-brooklyn git commit: Add scopeRoot scope in CAMP DSL

Add scopeRoot scope in CAMP DSL


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

Branch: refs/heads/master
Commit: b1b82f9b756ae02467dab39f311bc3680f33229a
Parents: 1da725f
Author: Svetoslav Neykov <sv...@cloudsoftcorp.com>
Authored: Wed Nov 4 15:59:24 2015 +0200
Committer: Svetoslav Neykov <sv...@cloudsoftcorp.com>
Committed: Thu Nov 5 19:10:36 2015 +0200

----------------------------------------------------------------------
 .../apache/brooklyn/core/entity/Entities.java   | 11 ++++
 docs/guide/yaml/yaml-reference.md               |  4 ++
 .../spi/dsl/methods/BrooklynDslCommon.java      |  3 +
 .../brooklyn/spi/dsl/methods/DslComponent.java  |  8 ++-
 .../camp/brooklyn/EntitiesYamlTest.java         | 65 ++++++++++++++++++++
 .../brooklyn/ReferencingYamlTestEntity.java     |  4 ++
 .../resources/test-referencing-entities.yaml    |  7 +++
 7 files changed, 101 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/b1b82f9b/core/src/main/java/org/apache/brooklyn/core/entity/Entities.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/Entities.java b/core/src/main/java/org/apache/brooklyn/core/entity/Entities.java
index 8b10676..59e3ec1 100644
--- a/core/src/main/java/org/apache/brooklyn/core/entity/Entities.java
+++ b/core/src/main/java/org/apache/brooklyn/core/entity/Entities.java
@@ -100,6 +100,7 @@ import org.slf4j.LoggerFactory;
 import com.google.common.annotations.Beta;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Function;
+import com.google.common.base.Objects;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
@@ -1172,4 +1173,14 @@ public class Entities {
         return t;
     }
 
+    public static Entity catalogItemScopeRoot(Entity entity) {
+        Entity root = entity;
+        while (root.getParent() != null &&
+                root != root.getParent() &&
+                Objects.equal(root.getParent().getCatalogItemId(), root.getCatalogItemId())) {
+            root = root.getParent();
+        }
+        return root;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/b1b82f9b/docs/guide/yaml/yaml-reference.md
----------------------------------------------------------------------
diff --git a/docs/guide/yaml/yaml-reference.md b/docs/guide/yaml/yaml-reference.md
index 221e71e..fc37ba8 100644
--- a/docs/guide/yaml/yaml-reference.md
+++ b/docs/guide/yaml/yaml-reference.md
@@ -165,6 +165,10 @@ concise DSL defined here:
   * `sibling`: looks for the `ID` anywhere among children of the parent entity
   * `parent`: returns the parent entity (ignores the `ID`)
   * `this`: returns this entity (ignores the `ID`)
+* `$brooklyn:root()` will return the topmost entity (the application)
+* `$broopklyn:scopeRoot()` will return the root entity in the current plan scope.
+  For catalog items it's the topmost entity in the plan, for application plans it is the same as
+  `$brooklyn:root()`.
 * `$brooklyn:formatString("pattern e.g. %s %s", "field 1", "field 2")` returns a future which creates the formatted string
   with the given parameters, where parameters may be strings *or* other tasks such as `attributeWhenReady`
 * `$brooklyn:literal("string")` returns the given string as a literal (suppressing any `$brooklyn:` expansion)

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/b1b82f9b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
index 422dac3..88a1f1d 100644
--- a/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
+++ b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
@@ -87,6 +87,9 @@ public class BrooklynDslCommon {
     public static DslComponent root() {
         return new DslComponent(Scope.ROOT, null);
     }
+    public static DslComponent scopeRoot() {
+        return new DslComponent(Scope.SCOPE_ROOT, null);
+    }
     // prefer the syntax above to the below now, but not deprecating the below
     public static DslComponent component(String id) {
         return component("global", id);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/b1b82f9b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java
index d5f3078..40fd757 100644
--- a/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java
+++ b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java
@@ -110,6 +110,8 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity> {
                     break;
                 case ROOT:
                     return getEntity().getApplication();
+                case SCOPE_ROOT:
+                    return Entities.catalogItemScopeRoot(getEntity());
                 case DESCENDANT:
                     entitiesToSearch = Entities.descendants(getEntity());
                     break;
@@ -160,6 +162,9 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity> {
     public DslComponent root() {
         return new DslComponent(this, Scope.ROOT, "");
     }
+    public DslComponent scopeRoot() {
+        return new DslComponent(this, Scope.SCOPE_ROOT, "");
+    }
     
     @Deprecated /** @deprecated since 0.7.0 */
     public DslComponent component(String scopeOrId) {
@@ -283,9 +288,10 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity> {
         DESCENDANT ("descendant"),
         ANCESTOR("ancestor"),
         ROOT("root"),
+        SCOPE_ROOT("scopeRoot"),
         THIS ("this");
         
-        public static final Set<Scope> VALUES = ImmutableSet.of(GLOBAL, CHILD, PARENT, SIBLING, DESCENDANT, ANCESTOR, ROOT, THIS);
+        public static final Set<Scope> VALUES = ImmutableSet.of(GLOBAL, CHILD, PARENT, SIBLING, DESCENDANT, ANCESTOR, ROOT, SCOPE_ROOT, THIS);
         
         private final String name;
         

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/b1b82f9b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/EntitiesYamlTest.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/EntitiesYamlTest.java b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/EntitiesYamlTest.java
index 266772b..a997904 100644
--- a/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/EntitiesYamlTest.java
+++ b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/EntitiesYamlTest.java
@@ -467,6 +467,7 @@ public class EntitiesYamlTest extends AbstractYamlTest {
 
         Map<ConfigKey<Entity>, Entity> keyToEntity = new ImmutableMap.Builder<ConfigKey<Entity>, Entity>()
             .put(ReferencingYamlTestEntity.TEST_REFERENCE_ROOT, app)
+            .put(ReferencingYamlTestEntity.TEST_REFERENCE_SCOPE_ROOT, app)
             .put(ReferencingYamlTestEntity.TEST_REFERENCE_APP, app)
             .put(ReferencingYamlTestEntity.TEST_REFERENCE_ENTITY1, entity1)
             .put(ReferencingYamlTestEntity.TEST_REFERENCE_ENTITY1_ALT, entity1)
@@ -494,6 +495,70 @@ public class EntitiesYamlTest extends AbstractYamlTest {
             }
         }
     }
+    
+    @Test
+    public void testScopeReferences() throws Exception {
+        addCatalogItems(
+                "brooklyn.catalog:",
+                "  items:",
+                "  -  id: ref_child",
+                "     item:",
+                "      type: " + ReferencingYamlTestEntity.class.getName(),
+                "      test.reference.root: $brooklyn:root()",
+                "      test.reference.scope_root: $brooklyn:scopeRoot()",
+                "      brooklyn.children:",
+                "      - type: " + ReferencingYamlTestEntity.class.getName(),
+                "        test.reference.root: $brooklyn:root()",
+                "        test.reference.scope_root: $brooklyn:scopeRoot()",
+
+                "  -  id: ref_parent",
+                "     item:",
+                "      type: " + ReferencingYamlTestEntity.class.getName(),
+                "      test.reference.root: $brooklyn:root()",
+                "      test.reference.scope_root: $brooklyn:scopeRoot()",
+                "      brooklyn.children:",
+                "      - type: " + ReferencingYamlTestEntity.class.getName(),
+                "        test.reference.root: $brooklyn:root()",
+                "        test.reference.scope_root: $brooklyn:scopeRoot()",
+                "        brooklyn.children:",
+                "        - type: ref_child");
+        Entity app = createAndStartApplication(
+                "brooklyn.config:",
+                "  test.reference.root: $brooklyn:root()",
+                "  test.reference.scope_root: $brooklyn:scopeRoot()",
+                "services:",
+                "- type: " + ReferencingYamlTestEntity.class.getName(),
+                "  test.reference.root: $brooklyn:root()",
+                "  test.reference.scope_root: $brooklyn:scopeRoot()",
+                "  brooklyn.children:",
+                "  - type: " + ReferencingYamlTestEntity.class.getName(),
+                "    test.reference.root: $brooklyn:root()",
+                "    test.reference.scope_root: $brooklyn:scopeRoot()",
+                "    brooklyn.children:",
+                "    - type: ref_parent");
+        
+        assertScopes(app, app, app);
+        Entity e1 = nextChild(app);
+        assertScopes(e1, app, app);
+        Entity e2 = nextChild(e1);
+        assertScopes(e2, app, app);
+        Entity e3 = nextChild(e2);
+        assertScopes(e3, app, e3);
+        Entity e4 = nextChild(e3);
+        assertScopes(e4, app, e3);
+        Entity e5 = nextChild(e4);
+        assertScopes(e5, app, e5);
+        Entity e6 = nextChild(e5);
+        assertScopes(e6, app, e5);
+    }
+    
+    private static Entity nextChild(Entity entity) {
+        return Iterables.getOnlyElement(entity.getChildren());
+    }
+    private static void assertScopes(Entity entity, Entity root, Entity scopeRoot) {
+        assertEquals(entity.config().get(ReferencingYamlTestEntity.TEST_REFERENCE_ROOT), root);
+        assertEquals(entity.config().get(ReferencingYamlTestEntity.TEST_REFERENCE_SCOPE_ROOT), scopeRoot);
+    }
 
     private void checkReferences(final Entity entity, Map<ConfigKey<Entity>, Entity> keyToEntity) throws Exception {
         for (final ConfigKey<Entity> key : keyToEntity.keySet()) {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/b1b82f9b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ReferencingYamlTestEntity.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ReferencingYamlTestEntity.java b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ReferencingYamlTestEntity.java
index 089343c..7a8ac59 100644
--- a/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ReferencingYamlTestEntity.java
+++ b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ReferencingYamlTestEntity.java
@@ -32,6 +32,10 @@ public interface ReferencingYamlTestEntity extends Entity {
             .name("test.reference.root")
             .build();
     @SuppressWarnings("serial")
+    public static final ConfigKey<Entity> TEST_REFERENCE_SCOPE_ROOT = BasicConfigKey.builder(new TypeToken<Entity>(){})
+            .name("test.reference.scope_root")
+            .build();
+    @SuppressWarnings("serial")
     public static final ConfigKey<Entity> TEST_REFERENCE_APP = BasicConfigKey.builder(new TypeToken<Entity>(){})
             .name("test.reference.app")
             .build();

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/b1b82f9b/usage/camp/src/test/resources/test-referencing-entities.yaml
----------------------------------------------------------------------
diff --git a/usage/camp/src/test/resources/test-referencing-entities.yaml b/usage/camp/src/test/resources/test-referencing-entities.yaml
index ebe4662..c87a8de 100644
--- a/usage/camp/src/test/resources/test-referencing-entities.yaml
+++ b/usage/camp/src/test/resources/test-referencing-entities.yaml
@@ -38,6 +38,7 @@ origin: https://github.com/apache/incubator-brooklyn
 id: app1
 brooklyn.config:
   test.reference.root: $brooklyn:root()
+  test.reference.scope_root: $brooklyn:scopeRoot()
   test.reference.app: $brooklyn:component("app1")
   test.reference.entity1: $brooklyn:entity("e1")
   test.reference.entity1a: $brooklyn:config("test.reference.entity1")
@@ -53,6 +54,7 @@ services:
   name: entity 1
   brooklyn.config:
     test.reference.root: $brooklyn:root()
+    test.reference.scope_root: $brooklyn:scopeRoot()
     test.reference.app: $brooklyn:component("parent", "ignored")
     test.reference.entity1: $brooklyn:component("this", "ignored")
     test.reference.entity1a: $brooklyn:ancestor("app1").child("e1")
@@ -69,6 +71,7 @@ services:
       brooklyn.config:
         self: $brooklyn:entity("c1")
         test.reference.root: $brooklyn:root()
+        test.reference.scope_root: $brooklyn:scopeRoot()
         test.reference.app: $brooklyn:parent().parent()
         test.reference.entity1: $brooklyn:parent()
         test.reference.entity1a: $brooklyn:entity("e1").parent().child("e1")
@@ -84,6 +87,7 @@ services:
         name: grandchild 1
         brooklyn.config:
           test.reference.root: $brooklyn:root()
+          test.reference.scope_root: $brooklyn:scopeRoot()
           test.reference.app: $brooklyn:component("app1")
           test.reference.entity1: $brooklyn:component("e1")
           test.reference.entity2: $brooklyn:component("e2")
@@ -96,6 +100,7 @@ services:
         name: grandchild 2
         brooklyn.config:
           test.reference.root: $brooklyn:root()
+          test.reference.scope_root: $brooklyn:scopeRoot()
           test.reference.app: $brooklyn:component("app1")
           test.reference.entity1: $brooklyn:component("e1")
           test.reference.entity2: $brooklyn:component("e2")
@@ -108,6 +113,7 @@ services:
       name: child 2
       brooklyn.config:
         test.reference.root: $brooklyn:root()
+        test.reference.scope_root: $brooklyn:scopeRoot()
         test.reference.app: $brooklyn:parent().parent().descendant("app1")
         test.reference.entity1: $brooklyn:component("e1")
         test.reference.entity2: $brooklyn:component("e2")
@@ -120,6 +126,7 @@ services:
   name: entity 2
   brooklyn.config:
     test.reference.root: $brooklyn:root()
+    test.reference.scope_root: $brooklyn:scopeRoot()
     test.reference.app: $brooklyn:component("app1")
     test.reference.entity1: $brooklyn:component("e1")
     test.reference.entity2: $brooklyn:component("e2")