Allow HTTP Device Factory instances to set observed values
diff --git a/platform/southbound/http/http-device-factory/pom.xml b/platform/southbound/http/http-device-factory/pom.xml
index e40adb2..ca25f52 100644
--- a/platform/southbound/http/http-device-factory/pom.xml
+++ b/platform/southbound/http/http-device-factory/pom.xml
@@ -74,6 +74,12 @@
<artifactId>jackson-databind</artifactId>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>org.eclipse.sensinact.gateway.nthbnd</groupId>
+ <artifactId>ldap-filter</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/platform/southbound/http/http-device-factory/src/main/java/org/eclipse/sensinact/gateway/sthbnd/http/factory/HttpMappingProtocolStackEndpointFactory.java b/platform/southbound/http/http-device-factory/src/main/java/org/eclipse/sensinact/gateway/sthbnd/http/factory/HttpMappingProtocolStackEndpointFactory.java
index 126cba5..7bc3cc6 100644
--- a/platform/southbound/http/http-device-factory/src/main/java/org/eclipse/sensinact/gateway/sthbnd/http/factory/HttpMappingProtocolStackEndpointFactory.java
+++ b/platform/southbound/http/http-device-factory/src/main/java/org/eclipse/sensinact/gateway/sthbnd/http/factory/HttpMappingProtocolStackEndpointFactory.java
@@ -10,6 +10,8 @@
*/
package org.eclipse.sensinact.gateway.sthbnd.http.factory;
+import static java.util.Arrays.asList;
+
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@@ -160,15 +162,13 @@
private final ExtModelConfiguration<TaskAwareHttpResponsePacket> buildConfiguration(HttpMediator mediator,
HttpMappingProtocolStackEndpointDescription endpointDescription, String resourceConfig) {
- byte serviceBuildPolicy = Arrays.stream(endpointDescription.getServiceBuildPolicy()).<Byte>collect(
- ()->{return Byte.valueOf((byte)0);},
- (b,s)->{b = Byte.valueOf((byte)(b.byteValue() | BuildPolicy.valueOf(s).getPolicy()));},
- (b1,b2)->{b1 = Byte.valueOf((byte)(b1.byteValue() | b2.byteValue()));}).byteValue();
+ byte serviceBuildPolicy = Arrays.stream(endpointDescription.getServiceBuildPolicy())
+ .map(s -> (int) BuildPolicy.valueOf(s).getPolicy())
+ .reduce(0, (b1,b2) -> b1.byteValue() | b2.byteValue()).byteValue();
- byte resourceBuildPolicy = Arrays.stream(endpointDescription.getResourceBuildPolicy()).<Byte>collect(
- ()->{return Byte.valueOf((byte)0);},
- (b,s)->{b = Byte.valueOf((byte)(b.byteValue() | BuildPolicy.valueOf(s).getPolicy()));},
- (b1,b2)->{b1 = Byte.valueOf((byte)(b1.byteValue() | b2.byteValue()));}).byteValue();
+ byte resourceBuildPolicy = Arrays.stream(endpointDescription.getResourceBuildPolicy())
+ .map(s -> (int) BuildPolicy.valueOf(s).getPolicy())
+ .reduce(0, (b1,b2) -> b1.byteValue() | b2.byteValue()).byteValue();
Modifiable modifiable = null;
try {
@@ -183,6 +183,7 @@
).withServiceBuildPolicy(resourceConfig==null?BuildPolicy.BUILD_NON_DESCRIBED.getPolicy():serviceBuildPolicy
).withResourceBuildPolicy(resourceConfig==null?BuildPolicy.BUILD_NON_DESCRIBED.getPolicy():resourceBuildPolicy
).withDefaultModifiable(modifiable
+ ).withObserved(asList(endpointDescription.getObserved())
).build(resourceConfig, endpointDescription.getDefaults());
HttpMappingProtocolStackConnectorCustomizer customizer = new HttpMappingProtocolStackConnectorCustomizer(
diff --git a/platform/southbound/http/http-device-factory/src/main/java/org/eclipse/sensinact/gateway/sthbnd/http/factory/config/HttpMappingProtocolStackEndpointDescription.java b/platform/southbound/http/http-device-factory/src/main/java/org/eclipse/sensinact/gateway/sthbnd/http/factory/config/HttpMappingProtocolStackEndpointDescription.java
index b30e461..82c7702 100644
--- a/platform/southbound/http/http-device-factory/src/main/java/org/eclipse/sensinact/gateway/sthbnd/http/factory/config/HttpMappingProtocolStackEndpointDescription.java
+++ b/platform/southbound/http/http-device-factory/src/main/java/org/eclipse/sensinact/gateway/sthbnd/http/factory/config/HttpMappingProtocolStackEndpointDescription.java
@@ -42,6 +42,9 @@
@JsonProperty(value="serviceBuildPolicy")
private String[] serviceBuildPolicy;
+ @JsonProperty(value="observed")
+ private String[] observed;
+
@JsonProperty(value="serviceProviderIdPattern")
private String serviceProviderIdPattern;
@@ -187,6 +190,22 @@
public void setServiceBuildPolicy(String[] serviceBuildPolicy) {
this.serviceBuildPolicy = serviceBuildPolicy;
}
+
+ /**
+ * @return the serviceBuildPolicy
+ */
+ public String[] getObserved() {
+ if(this.observed == null)
+ return new String[0];
+ return observed;
+ }
+
+ /**
+ * @param serviceBuildPolicy the serviceBuildPolicy to set
+ */
+ public void setObserved(String[] observed) {
+ this.observed = observed;
+ }
/**
* @return the serviceProviderIdPattern
diff --git a/platform/southbound/http/http-device-factory/src/test/java/org/eclipse/sensinact/gateway/sthbnd/http/factory/test/HttpDeviceFactoryTest.java b/platform/southbound/http/http-device-factory/src/test/java/org/eclipse/sensinact/gateway/sthbnd/http/factory/test/HttpDeviceFactoryTest.java
index 3423a44..1cb49d4 100644
--- a/platform/southbound/http/http-device-factory/src/test/java/org/eclipse/sensinact/gateway/sthbnd/http/factory/test/HttpDeviceFactoryTest.java
+++ b/platform/southbound/http/http-device-factory/src/test/java/org/eclipse/sensinact/gateway/sthbnd/http/factory/test/HttpDeviceFactoryTest.java
@@ -32,13 +32,18 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.eclipse.sensinact.gateway.common.bundle.Mediator;
import org.eclipse.sensinact.gateway.core.Core;
import org.eclipse.sensinact.gateway.core.DataResource;
import org.eclipse.sensinact.gateway.core.Resource;
import org.eclipse.sensinact.gateway.core.Service;
import org.eclipse.sensinact.gateway.core.ServiceProvider;
import org.eclipse.sensinact.gateway.core.Session;
+import org.eclipse.sensinact.gateway.core.filtering.FilteringCollection;
+import org.eclipse.sensinact.gateway.core.filtering.FilteringDefinition;
import org.eclipse.sensinact.gateway.core.message.SnaMessage;
+import org.eclipse.sensinact.gateway.core.method.DescribeResponse;
+import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
@@ -299,6 +304,36 @@
}
+ @Test
+ @Order(8)
+ @WithFactoryConfiguration(factoryPid = FACTORY_PID, name = "test",
+ location = "?",
+ properties = {
+ @Property(key = ENDPOINT_CONFIGURATION_PROP, value="src/test/resources/test8/config.json"),
+ @Property(key = ENDPOINT_TASKS_CONFIGURATION_PROP, value="src/test/resources/test8/tasks.json")
+ }
+ )
+ public void testObserved(@InjectBundleContext BundleContext ctx) throws Exception {
+ Session session = expectNServiceProviders(2);
+
+ testProvider(session, "test8_Foo", "data", "value", "94", "1.2:3.4",
+ LocalDateTime.of(2021, 10, 20, 18, 14).toEpochSecond(ZoneOffset.UTC));
+ testProvider(session, "test8_Bar", "data", "value", "28", "5.6:7.8",
+ LocalDateTime.of(2021, 10, 20, 18, 17).toEpochSecond(ZoneOffset.UTC));
+
+
+ DescribeResponse<String> response = session.getAll(
+ new FilteringCollection(new Mediator(ctx), false,
+ new FilteringDefinition("ldap", "(data.value.value<=40)")));
+
+ JSONObject jsonObject = new JSONObject(response.getJSON());
+ JSONArray array = jsonObject.getJSONArray("providers");
+
+ assertEquals(1, array.length());
+ assertEquals("test8_Bar", array.getJSONObject(0).get("name"));
+
+ }
+
private void testProvider(Session session, String providerName, String serviceName, String resourceName,
String value, String location, long timestamp) {
ServiceProvider provider = session.serviceProvider(providerName);
diff --git a/platform/southbound/http/http-device-factory/src/test/resources/test8/config.json b/platform/southbound/http/http-device-factory/src/test/resources/test8/config.json
new file mode 100644
index 0000000..3ece41b
--- /dev/null
+++ b/platform/southbound/http/http-device-factory/src/test/resources/test8/config.json
@@ -0,0 +1,6 @@
+{
+ "modifiable": "UPDATABLE",
+ "serviceProviderIdPattern": "test8_%s",
+ "timestampPattern": "dd.MM.yy-hh:mm",
+ "observed": ["data/value"]
+}
\ No newline at end of file
diff --git a/platform/southbound/http/http-device-factory/src/test/resources/test8/response.json b/platform/southbound/http/http-device-factory/src/test/resources/test8/response.json
new file mode 100644
index 0000000..ecb3662
--- /dev/null
+++ b/platform/southbound/http/http-device-factory/src/test/resources/test8/response.json
@@ -0,0 +1,4 @@
+[
+ {"Date":"20.10.2021","Time":"18:14","Name":"Foo","Latitude":"1.2","Longitude":"3.4","Value":"94"},
+ {"Date":"20.10.2021","Time":"18:17","Name":"Bar","Latitude":"5.6","Longitude":"7.8","Value":"28"}
+]
\ No newline at end of file
diff --git a/platform/southbound/http/http-device-factory/src/test/resources/test8/tasks.json b/platform/southbound/http/http-device-factory/src/test/resources/test8/tasks.json
new file mode 100644
index 0000000..1f0e407
--- /dev/null
+++ b/platform/southbound/http/http-device-factory/src/test/resources/test8/tasks.json
@@ -0,0 +1,32 @@
+{
+ "standalone": {
+ "recurrences": [
+ {
+ "period": 2000,
+ "delay": 500,
+ "configuration": {
+ "acceptType": "application/json",
+ "scheme": "http",
+ "host": "localhost",
+ "port": 8898,
+ "path": "/http-factory-test-resources/test8/response.json",
+ "nestedMapping": [
+ {
+ "path": "*",
+ "mapping": {
+ "Name": "::provider::",
+ "Value": "data/value",
+ "Latitude": "__lat",
+ "Longitude": "__lon",
+ "$concat(${lat},':',${lon})": "admin/location",
+ "Date": "__date",
+ "Time": "__time",
+ "$concat(${date},'-',${time})": "::timestamp::"
+ }
+ }
+ ]
+ }
+ }
+ ]
+ }
+}
\ No newline at end of file
diff --git a/platform/southbound/http/http-device-factory/tests.bndrun b/platform/southbound/http/http-device-factory/tests.bndrun
index 1158c1e..7073d3b 100644
--- a/platform/southbound/http/http-device-factory/tests.bndrun
+++ b/platform/southbound/http/http-device-factory/tests.bndrun
@@ -4,16 +4,15 @@
bnd.identity;id=junit-jupiter-engine,\
bnd.identity;id=junit-platform-launcher,\
bnd.identity;id='${project.groupId}.${project.artifactId}-tests',\
- bnd.identity;id=org.apache.felix.http.jetty,\
- bnd.identity;id=org.eclipse.sensinact.gateway.sensinact-security-none,\
- bnd.identity;id=org.eclipse.sensinact.gateway.sensinact-signature-validator-mock
+ bnd.identity;id='org.apache.felix.http.jetty',\
+ bnd.identity;id='org.eclipse.sensinact.gateway.sensinact-security-none',\
+ bnd.identity;id='org.eclipse.sensinact.gateway.sensinact-signature-validator-mock',\
+ bnd.identity;id='org.eclipse.sensinact.gateway.nthbnd.ldap-filter'
-runbundles: \
org.osgi.service.cm;version='[1.6.0,1.6.1)',\
- org.osgi.service.log;version='[1.4.0,1.4.1)',\
org.osgi.util.function;version='[1.1.0,1.1.1)',\
org.osgi.util.promise;version='[1.1.1,1.1.2)',\
- org.osgi.util.pushstream;version='[1.0.1,1.0.2)',\
junit-platform-commons;version='[1.7.2,1.7.3)',\
junit-jupiter-api;version='[5.7.2,5.7.3)',\
junit-jupiter-engine;version='[5.7.2,5.7.3)',\
@@ -48,7 +47,9 @@
org.osgi.test.junit5.cm;version='[1.0.1,1.0.2)',\
org.apache.felix.http.jetty;version='[4.1.10,4.1.11)',\
org.apache.felix.http.servlet-api;version='[1.1.4,1.1.5)',\
- slf4j.log4j12;version='[1.7.25,1.7.26)'
+ slf4j.log4j12;version='[1.7.25,1.7.26)',\
+ json;version='[20190722.0.0,20190722.0.1)',\
+ org.eclipse.sensinact.gateway.nthbnd.ldap-filter;version='[2.0.0,2.0.1)'
-runproperties: \
org.eclipse.sensinact.gateway.security.jks.filename=${.}/../../../sensinact-security/cert/keystore.jks,\