brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [1/6] git commit: Added entity initializers for HttpRequestSensors. Polling over a URI and retrieving a single JSON value from the response.
Date Fri, 18 Jul 2014 16:37:40 GMT
Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master 4fa68908b -> 82f850665


Added entity initializers for HttpRequestSensors. Polling over a URI and retrieving a single
JSON value from the response.


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

Branch: refs/heads/master
Commit: 7a2881581e006f0f87b1eeedaea16dbf707e84fa
Parents: bcdf42c
Author: Miguel Barrientos <mbarrientos@lcc.uma.es>
Authored: Wed Jul 2 09:33:30 2014 +0200
Committer: Miguel Barrientos <mbarrientos@lcc.uma.es>
Committed: Mon Jul 14 18:25:51 2014 +0200

----------------------------------------------------------------------
 .../event/feed/http/HttpValueFunctions.java     |  8 +--
 .../entity/software/http/HttpRequestSensor.java | 63 ++++++++++++++++++++
 .../entity/software/HttpRequestSensorTest.java  | 63 ++++++++++++++++++++
 3 files changed, 130 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7a288158/core/src/main/java/brooklyn/event/feed/http/HttpValueFunctions.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/event/feed/http/HttpValueFunctions.java b/core/src/main/java/brooklyn/event/feed/http/HttpValueFunctions.java
index cc7db00..0981b0e 100644
--- a/core/src/main/java/brooklyn/event/feed/http/HttpValueFunctions.java
+++ b/core/src/main/java/brooklyn/event/feed/http/HttpValueFunctions.java
@@ -42,7 +42,7 @@ public class HttpValueFunctions {
     }
 
     public static Function<HttpToolResponse, Boolean> responseCodeEquals(final int
expected) {
-        return chain(HttpValueFunctions.responseCode(), Functions.forPredicate(Predicates.equalTo(expected)));
+        return Functionals.chain(HttpValueFunctions.responseCode(), Functions.forPredicate(Predicates.equalTo(expected)));
     }
     
     public static Function<HttpToolResponse, Boolean> responseCodeEquals(final int...
expected) {
@@ -50,7 +50,7 @@ public class HttpValueFunctions {
         for (int e : expected) {
             expectedList.add((Integer)e);
         }
-        return chain(HttpValueFunctions.responseCode(), Functions.forPredicate(Predicates.in(expectedList)));
+        return Functionals.chain(HttpValueFunctions.responseCode(), Functions.forPredicate(Predicates.in(expectedList)));
     }
     
     public static Function<HttpToolResponse, String> stringContentsFunction() {
@@ -62,7 +62,7 @@ public class HttpValueFunctions {
     }
     
     public static Function<HttpToolResponse, JsonElement> jsonContents() {
-        return chain(stringContentsFunction(), JsonFunctions.asJson());
+        return Functionals.chain(stringContentsFunction(), JsonFunctions.asJson());
     }
     
     public static <T> Function<HttpToolResponse, T> jsonContents(String element,
Class<T> expected) {
@@ -70,7 +70,7 @@ public class HttpValueFunctions {
     }
     
     public static <T> Function<HttpToolResponse, T> jsonContents(String[] elements,
Class<T> expected) {
-        return chain(jsonContents(), JsonFunctions.walk(elements), JsonFunctions.cast(expected));
+        return Functionals.chain(jsonContents(), JsonFunctions.walk(elements), JsonFunctions.cast(expected));
     }
     
     public static Function<HttpToolResponse, Long> latency() {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7a288158/software/base/src/main/java/brooklyn/entity/software/http/HttpRequestSensor.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/brooklyn/entity/software/http/HttpRequestSensor.java
b/software/base/src/main/java/brooklyn/entity/software/http/HttpRequestSensor.java
new file mode 100644
index 0000000..8cc04f9
--- /dev/null
+++ b/software/base/src/main/java/brooklyn/entity/software/http/HttpRequestSensor.java
@@ -0,0 +1,63 @@
+package brooklyn.entity.software.http;
+
+import brooklyn.config.ConfigKey;
+import brooklyn.entity.basic.ConfigKeys;
+import brooklyn.entity.basic.EntityLocal;
+import brooklyn.entity.effector.AddSensor;
+import brooklyn.event.AttributeSensor;
+import brooklyn.event.feed.http.HttpFeed;
+import brooklyn.event.feed.http.HttpPollConfig;
+import brooklyn.event.feed.http.HttpValueFunctions;
+import brooklyn.util.config.ConfigBag;
+import brooklyn.util.time.Duration;
+import com.google.common.base.Preconditions;
+import com.jayway.jsonpath.JsonPath;
+import org.slf4j.LoggerFactory;
+
+import java.util.Map;
+
+/**
+ * Configurable {@link brooklyn.entity.proxying.EntityInitializer} which adds an HTTP sensor
feed to retrieve the
+ * <code>JSONObject</code> from a JSON response in order to populate the sensor
with the indicated <code>name</code>.
+ */
+// generics introduced here because we might support a configurable 'targetType` parameter
in future, 
+// with automatic casting (e.g. for ints); this way it remains compatible
+public final class HttpRequestSensor<T extends String> extends AddSensor<String,
AttributeSensor<String>> {
+
+    private static final org.slf4j.Logger log = LoggerFactory.getLogger(HttpRequestSensor.class);
+
+    public static final ConfigKey<String> JSON_PATH = ConfigKeys.newStringConfigKey("jsonPath");
+    public static final ConfigKey<String> SENSOR_URI = ConfigKeys.newStringConfigKey("uri");
+
+    String jsonPath;
+    String uri;
+
+    public HttpRequestSensor(ConfigBag params) {
+        super(newSensor(String.class, params));
+        jsonPath = Preconditions.checkNotNull(params.get(JSON_PATH), JSON_PATH);
+        uri = Preconditions.checkNotNull(params.get(SENSOR_URI), SENSOR_URI);
+    }
+
+    @Override
+    public void apply(final EntityLocal entity) {
+        super.apply(entity);
+
+        Duration period = Duration.ONE_SECOND;
+
+        HttpPollConfig<String> pollConfig = new HttpPollConfig<String>(sensor)
+                .onSuccess(HttpValueFunctions.jsonContents(jsonPath, String.class));
+
+        if (period != null) pollConfig.period(period);
+
+        HttpFeed.builder().entity(entity)
+                .baseUri(uri)
+                .poll(pollConfig)
+                .build();
+
+        log.info("HTTPRequestSensor: " + uri + " -> " + jsonPath);
+    }
+
+    public HttpRequestSensor(Map<String, String> params) {
+        this(ConfigBag.newInstance(params));
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7a288158/software/base/src/test/java/brooklyn/entity/software/HttpRequestSensorTest.java
----------------------------------------------------------------------
diff --git a/software/base/src/test/java/brooklyn/entity/software/HttpRequestSensorTest.java
b/software/base/src/test/java/brooklyn/entity/software/HttpRequestSensorTest.java
new file mode 100644
index 0000000..1ea5f48
--- /dev/null
+++ b/software/base/src/test/java/brooklyn/entity/software/HttpRequestSensorTest.java
@@ -0,0 +1,63 @@
+package brooklyn.entity.software;
+
+import brooklyn.entity.basic.ApplicationBuilder;
+import brooklyn.entity.basic.Attributes;
+import brooklyn.entity.basic.Entities;
+import brooklyn.entity.basic.EntityLocal;
+import brooklyn.entity.proxying.EntitySpec;
+import brooklyn.entity.software.http.HttpRequestSensor;
+import brooklyn.event.AttributeSensor;
+import brooklyn.event.basic.Sensors;
+import brooklyn.location.Location;
+import brooklyn.test.entity.TestApplication;
+import brooklyn.test.entity.TestEntity;
+import brooklyn.util.config.ConfigBag;
+import com.google.common.collect.ImmutableList;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.assertEquals;
+import static brooklyn.test.Asserts.succeedsEventually;
+
+public class HttpRequestSensorTest {
+    final static AttributeSensor<String> SENSOR_STRING = Sensors.newStringSensor("aString",
"");
+    final static AttributeSensor<String> SENSOR_JSON_OBJECT = Sensors.newStringSensor("aJSONObject","");
+    final static AttributeSensor<String> SENSOR_URI = Sensors.newStringSensor("uri","");
+
+    private TestApplication app;
+    private EntityLocal entity;
+
+    @BeforeMethod(alwaysRun=true)
+    public void setUp() throws Exception {
+        app = ApplicationBuilder.newManagedApp(TestApplication.class);
+        entity = app.createAndManageChild(EntitySpec.create(TestEntity.class).location(app.newLocalhostProvisioningLocation().obtain()));
+        app.start(ImmutableList.<Location>of());
+    }
+
+    @AfterMethod(alwaysRun=true)
+    public void tearDown() throws Exception {
+        if (app != null) Entities.destroyAll(app.getManagementContext());
+    }
+
+    @Test(groups="Integration")
+    public void testHttpSensor() throws Exception {
+        new HttpRequestSensor<String>(ConfigBag.newInstance()
+                .configure(HttpRequestSensor.SENSOR_NAME, SENSOR_STRING.getName())
+                .configure(HttpRequestSensor.JSON_PATH, "myKey")
+                .configure(HttpRequestSensor.SENSOR_URI, "http://echo.jsontest.com/myKey/myValue"))
+            .apply(entity);
+        entity.setAttribute(Attributes.SERVICE_UP, true);
+
+        succeedsEventually(new Runnable() {
+            public void run() {
+                String val = entity.getAttribute(SENSOR_STRING);
+                assertTrue(val != null);
+            }
+        });
+
+        String val = entity.getAttribute(SENSOR_STRING);
+        assertEquals(val, "myValue", "val=" + val);
+    }
+}


Mime
View raw message