You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by dr...@apache.org on 2017/02/28 15:02:10 UTC

[2/7] brooklyn-server git commit: Add DSL support for calling effectors in YAML

Add DSL support for calling effectors in YAML


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

Branch: refs/heads/master
Commit: 54d131a3b7f3cd01b7cea5db65f008fd734f5da3
Parents: 91a35e2
Author: Andrew Donald Kennedy <an...@cloudsoftcorp.com>
Authored: Mon May 23 23:31:13 2016 +0100
Committer: Andrew Donald Kennedy <an...@cloudsoftcorp.com>
Committed: Tue Feb 28 14:25:48 2017 +0000

----------------------------------------------------------------------
 .../spi/dsl/methods/BrooklynDslCommon.java      |  9 +++
 .../brooklyn/spi/dsl/methods/DslComponent.java  | 53 +++++++++++++++-
 .../camp/brooklyn/EffectorsYamlTest.java        | 63 ++++++++++++++++++++
 .../test/resources/test-app-with-effectors.yaml | 34 +++++++++++
 4 files changed, 158 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/54d131a3/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
----------------------------------------------------------------------
diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
index 2836895..82319d7 100644
--- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
+++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
@@ -70,6 +70,7 @@ import org.slf4j.LoggerFactory;
 import com.google.common.base.Function;
 import com.google.common.base.Objects;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 
@@ -233,6 +234,14 @@ public class BrooklynDslCommon {
         return new DslComponent(Scope.THIS, "").entityId();
     }
 
+    public static BrooklynDslDeferredSupplier<?> effector(String effectorName, Map<String, ?> args) {
+        return new DslComponent(Scope.THIS, "").effector(effectorName, args);
+    }
+
+    public static BrooklynDslDeferredSupplier<?> effector(String effectorName) {
+        return new DslComponent(Scope.THIS, "").effector(effectorName, ImmutableMap.<String, Object>of());
+    }
+
     /** Returns a {@link Sensor}, looking up the sensor on the context if available and using that,
      * or else defining an untyped (Object) sensor */
     @DslAccessible

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/54d131a3/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java
----------------------------------------------------------------------
diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java
index 9de3340..a67bfba 100644
--- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java
+++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java
@@ -20,10 +20,12 @@ package org.apache.brooklyn.camp.brooklyn.spi.dsl.methods;
 
 import static org.apache.brooklyn.camp.brooklyn.spi.dsl.DslUtils.resolved;
 
+import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutionException;
 
+import org.apache.brooklyn.api.effector.Effector;
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.mgmt.ExecutionContext;
 import org.apache.brooklyn.api.mgmt.Task;
@@ -61,6 +63,7 @@ import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
+import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Iterables;
 import com.google.common.util.concurrent.Callables;
 
@@ -510,6 +513,54 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity> implements
     }
 
     @DslAccessible
+    public BrooklynDslDeferredSupplier<?> effector(final String effectorName) {
+        return new ExecuteEffector(this, effectorName, ImmutableMap.<String, Object>of());
+    }
+    @DslAccessible
+    public BrooklynDslDeferredSupplier<?> effector(final String effectorName, final Map<String, ?> args) {
+        return new ExecuteEffector(this, effectorName, args);
+    }
+    protected static class ExecuteEffector extends BrooklynDslDeferredSupplier<Object> {
+        private static final long serialVersionUID = 1740899524088902383L;
+        private final DslComponent component;
+        private final String effectorName;
+        private final Map<String, ?> args;
+        public ExecuteEffector(DslComponent component, String effectorName, Map<String, ?> args) {
+            this.component = Preconditions.checkNotNull(component);
+            this.effectorName = effectorName;
+            this.args = args;
+        }
+        @SuppressWarnings("unchecked")
+        @Override
+        public Task<Object> newTask() {
+            Entity targetEntity = component.get();
+            Maybe<Effector<?>> targetEffector = targetEntity.getEntityType().getEffectorByName(effectorName);
+            if (targetEffector.isAbsentOrNull()) {
+                throw new IllegalArgumentException("Effector " + effectorName + " not found on entity: " + targetEntity);
+            }
+            return (Task<Object>) Entities.invokeEffector(targetEntity, targetEntity, targetEffector.get(), args);
+        }
+        @Override
+        public int hashCode() {
+            return Objects.hashCode(component, effectorName);
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj) return true;
+            if (obj == null || getClass() != obj.getClass()) return false;
+            ExecuteEffector that = ExecuteEffector.class.cast(obj);
+            return Objects.equal(this.component, that.component) &&
+                    Objects.equal(this.effectorName, that.effectorName);
+        }
+        @Override
+        public String toString() {
+            return (component.scope==Scope.THIS ? "" : component.toString()+".") +
+                "effector("+JavaStringEscapes.wrapJavaString(effectorName)+")";
+        }
+    }
+
+    @DslAccessible
     public BrooklynDslDeferredSupplier<?> config(final Object keyNameOrSupplier) {
         return new DslConfigSupplier(this, keyNameOrSupplier);
     }
@@ -763,5 +814,5 @@ public class DslComponent extends BrooklynDslDeferredSupplier<Entity> implements
                
         return DslToStringHelpers.component(scopeComponent, remainder);
     }
-    
+
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/54d131a3/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/EffectorsYamlTest.java
----------------------------------------------------------------------
diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/EffectorsYamlTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/EffectorsYamlTest.java
new file mode 100644
index 0000000..befeff1
--- /dev/null
+++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/EffectorsYamlTest.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.camp.brooklyn;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.core.entity.Entities;
+import org.apache.brooklyn.core.entity.EntityAsserts;
+import org.apache.brooklyn.core.entity.trait.Startable;
+import org.apache.brooklyn.util.time.Duration;
+
+@Test
+public class EffectorsYamlTest extends AbstractYamlTest {
+    private static final Logger log = LoggerFactory.getLogger(EffectorsYamlTest.class);
+
+    @Test
+    public void testWithAppEnricher() throws Exception {
+        Entity app = createAndStartApplication(loadYaml("test-app-with-effectors.yaml"));
+        waitForApplicationTasks(app);
+        Assert.assertEquals(app.getDisplayName(), "test-app-with-effectors");
+
+        Entities.dumpInfo(app);
+        Thread.sleep(Duration.THIRTY_SECONDS.toMilliseconds());
+
+        Entity start1 = null, start2 = null;
+        Assert.assertEquals(app.getChildren().size(), 2);
+        for (Entity child : app.getChildren()) {
+            if (child.getDisplayName().equals("start1"))
+                start1 = child;
+            if (child.getDisplayName().equals("start2"))
+                start2 = child;
+        }
+        Assert.assertNotNull(start1);
+        Assert.assertNotNull(start2);
+        EntityAsserts.assertAttributeEquals(start1, Startable.SERVICE_UP, false);
+        EntityAsserts.assertAttributeEquals(start2, Startable.SERVICE_UP, true);
+    }
+
+    @Override
+    protected Logger getLogger() {
+        return log;
+    }
+}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/54d131a3/camp/camp-brooklyn/src/test/resources/test-app-with-effectors.yaml
----------------------------------------------------------------------
diff --git a/camp/camp-brooklyn/src/test/resources/test-app-with-effectors.yaml b/camp/camp-brooklyn/src/test/resources/test-app-with-effectors.yaml
new file mode 100644
index 0000000..1db93c1
--- /dev/null
+++ b/camp/camp-brooklyn/src/test/resources/test-app-with-effectors.yaml
@@ -0,0 +1,34 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+name: test-app-with-effectors
+description: Application with effector that stops another entity
+origin: https://github.com/apache/brooklyn
+services:
+- type: org.apache.brooklyn.entity.stock.BasicStartable
+  id: start1
+  name: start1
+- type: org.apache.brooklyn.entity.stock.BasicStartable
+  id: start2
+  name: start2
+  brooklyn.initializers:
+  - type: org.apache.brooklyn.core.sensor.StaticSensor
+    brooklyn.config:
+      name: stop
+      targetType: java.lang.Object
+      static.value: $brooklyn:component("start1").effector("stop")