Skip to content

Commit cc2f4f5

Browse files
committed
Updated to support Storage & Query enabled in the wldt-core version 0.4.0
1 parent 1bd50ef commit cc2f4f5

8 files changed

Lines changed: 314 additions & 29 deletions

File tree

README.md

Lines changed: 99 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# HTTP Digital Adapter Java
22

3-
The `HttpDigitalAdapter` is a powerful component designed to facilitate the integration of Digital Twins into HTTP-based systems.
4-
It serves as a bridge between a Digital Twin and HTTP-based applications, allowing developers to easily expose and interact with
3+
The `HttpDigitalAdapter` is a powerful component designed to facilitate the integration of Digital Twins into HTTP-based systems.
4+
It serves as a bridge between a Digital Twin and HTTP-based applications, allowing developers to easily expose and interact with
55
Digital Twin data and functionalities over HTTP.
66

77
Key Features:
@@ -10,20 +10,20 @@ Key Features:
1010
- **Dynamic Configuration:** Offers a flexible configuration mechanism through the HttpDigitalAdapterConfiguration, allowing developers to customize the adapter's behavior based on specific requirements.
1111
- **State Monitoring:** Monitors changes in the Digital Twin state and provides HTTP endpoints to query the state of the Digital Twin (properties, events, actions and relationships).
1212
- **Event Notifications:** Allows developers to retrieve event notifications triggered by changes in the Digital Twin state.
13+
- **Storage & Query:** Since version 0.2 the HTTP Digital Adapter is able to retrieve Storage Statistics and execute query on the target DT
1314

14-
<div align="center">
15-
<img class="center" src="images/adapter_schema.jpg" width="100%">
16-
</div>
15+
![HTTP Digital Adapter](images/http_digital_adapter_schema.jpg)
1716

1817
A complete example is provided in the `test` folder with a complete DT Creation in the `TestMain` class together with a demo DT with and emulated Physical Adapter and the HTTP Digital Adapter.
1918

2019
## WLDT-Core Version Compatibility
2120

2221
The correct mapping and compatibility between versions is reported in the following table
2322

24-
| **http-digital-adapter** | wldt-core 0.2.1 | wldt-core 0.3.0 |
25-
|:------------------------:|:------------------:|:------------------:|
26-
| 0.1.1 | :x: | :white_check_mark: |
23+
| **http-digital-adapter** | wldt-core 0.2.1 | wldt-core 0.3.0 | wldt-core 0.4.0 |
24+
|:------------------------:|:------------------:|:------------------:|:------------------:|
25+
| 0.1.1 | :x: | :white_check_mark: | :white_check_mark: |
26+
| 0.2 | :x: | :x: | :white_check_mark: |
2727

2828
## Installation
2929

@@ -35,21 +35,21 @@ To use HttpDigitalAdapter in your Java project, you can include it as a dependen
3535
<dependency>
3636
<groupId>io.github.wldt</groupId>
3737
<artifactId>http-digital-adapter</artifactId>
38-
<version>0.1.1</version>
38+
<version>0.2</version>
3939
</dependency>
4040
```
4141

4242
### Gradle
4343

4444
```groovy
45-
implementation 'io.github.wldt:http-digital-adapter:0.1.1'
45+
implementation 'io.github.wldt:http-digital-adapter:0.2'
4646
```
4747

4848
## Class Structure & Functionalities
4949

5050
### HttpDigitalAdapterConfiguration
5151

52-
The `HttpDigitalAdapterConfiguration` is a crucial part of the HttpDigitalAdapter, providing the necessary settings to
52+
The `HttpDigitalAdapterConfiguration` is a crucial part of the HttpDigitalAdapter, providing the necessary settings to
5353
tailor the adapter's behavior to meet specific needs.
5454

5555
Represents the configuration for an HTTP Digital Adapter, specifying the host, port,
@@ -61,7 +61,7 @@ Filters are meant to be white list filters, if they are empty, it means that ALL
6161

6262
This class provides methods to add filters for each type and getters to retrieve the configured values.
6363

64-
Key functionalities and exposed capabilities:
64+
Key functionalities and exposed capabilities:
6565

6666
- **Basic Configuration**
6767
- **Adapter ID:** A unique identifier for the HttpDigitalAdapter instance.
@@ -76,13 +76,13 @@ Key functionalities and exposed capabilities:
7676
- `addEventsFilter(Collection<String> eventsKey)`: Adds a collection of event keys to the event filter.
7777
- `addRelationshipFilter(String relationshipName)`: Adds a single relationship name to the relationship filter.
7878
- `addRelationshipsFilter(Collection<String> relationshipNames)`: Adds a collection of relationship names to the relationship filter.
79-
- Configured Filter can be accessed using:
79+
- Configured Filter can be accessed using:
8080
- `getPropertyFilter()`
8181
- `getActionFilter()`
8282
- `getEventFilter()`
8383
- `getRelationshipFilter()`
8484

85-
A basic example without any filter that accesses and uses the entire DT State is:
85+
A basic example without any filter that accesses and uses the entire DT State is:
8686

8787
```java
8888
HttpDigitalAdapterConfiguration config = new HttpDigitalAdapterConfiguration("my-http-adapter", "localhost", 8080);
@@ -123,8 +123,8 @@ System.out.println("Relationship Filter: " + relationshipFilter);
123123

124124
### HttpDigitalAdapter
125125

126-
The `HttpDigitalAdapter` itself is the core component responsible for handling HTTP requests and
127-
interacting with the underlying Digital Twin. It extends the capabilities of the base DigitalAdapter
126+
The `HttpDigitalAdapter` itself is the core component responsible for handling HTTP requests and
127+
interacting with the underlying Digital Twin. It extends the capabilities of the base DigitalAdapter
128128
to specifically cater to HTTP-based scenarios.
129129

130130
Key Functionalities:
@@ -166,10 +166,10 @@ digitalTwinEngine.addDigitalTwin(digitalTwin);
166166
digitalTwinEngine.startAll();
167167
```
168168

169-
## HTTP RESTful API
169+
## HTTP RESTful API
170170

171-
This section of the documentation provides detailed information about the RESTful API exposed by the WLDT - HTTP Digital Adapter.
172-
The API allows you to interact with the Digital Twin (DT) instance, retrieve its state, read properties, actions, event and relationships description,
171+
This section of the documentation provides detailed information about the RESTful API exposed by the WLDT - HTTP Digital Adapter.
172+
The API allows you to interact with the Digital Twin (DT) instance, retrieve its state, read properties, actions, event and relationships description,
173173
and trigger actions.
174174

175175
Available endpoints with the associated methods are:
@@ -185,12 +185,91 @@ Available endpoints with the associated methods are:
185185
- `POST` `/state/actions/{actionKey}`: Triggers the specified action (e.g., /state/actions/switch_on) in the Digital Twin state. The raw body contains the action request payload.
186186
- `GET` `/state/relationships`: Retrieves the list of relationships in the Digital Twin state.
187187
- `GET` `/state/relationships/{relationshipName}/instances`: Retrieves the instances of the specified relationship (e.g., /state/relationships/insideIn/instances) in the Digital Twin state.
188+
- `GET` `/storage`: Retrieves Storage Statistics from the target Digital Twin
189+
- `POST` `/storage/query`: Allows the execution of a query, where the query structure is specified through a JSON Message in the request Body. For additional information about the Query System see [Query System Page](/docs/guides/storage-layer/)
188190

189191
Note: Replace {propertyKey}, {actionKey}, and {relationshipName} with the actual values you want to retrieve or trigger.
190-
Make sure to use the appropriate HTTP method (GET, POST) and include any required parameters or payload as described in each endpoint's description. For more detailed information, refer to the Postman Collection for this API available in the folder `api`: [http_adapter_api_postman.json](api%2Fhttp_adapter_api_postman.json)`
192+
Make sure to use the appropriate HTTP method (GET, POST) and include any required parameters or payload as described in each endpoint's description. For more detailed information, refer to the Postman Collection for this API available in the folder `api`: [http_adapter_api_postman.json](https://github.com/wldt/http-digital-adapter-java/blob/master/api/http_adapter_api_postman.json)
191193

194+
Example of Storage Query Requests are the following:
192195

196+
Retrieve the first 4 Digital Twin State Variations
193197

198+
```json
199+
{
200+
"resourceType": "DIGITAL_TWIN_STATE",
201+
"queryType": "SAMPLE_RANGE",
202+
"startIndex": 0,
203+
"endIndex": 3
204+
}
205+
```
206+
207+
Retrieve Digital Twin State Variations in a Time Range
208+
209+
```json
210+
{
211+
"resourceType": "DIGITAL_TWIN_STATE",
212+
"queryType": "TIME_RANGE",
213+
"startIndex": 161989898,
214+
"endIndex": 162989898
215+
}
216+
```
217+
218+
Retrieve the last Digital Twin State
219+
220+
```json
221+
{
222+
"resourceType": "DIGITAL_TWIN_STATE",
223+
"queryType": "LAST_VALUE"
224+
}
225+
```
226+
227+
Available keywords for Query Resource Type and Query Type are the following (as explained in the dedicated [Query System Page](/docs/guides/storage-layer/)):
228+
229+
- PHYSICAL_ASSET_PROPERTY_VARIATION
230+
- TIME_RANGE
231+
- SAMPLE_RANGE
232+
- COUNT
233+
- PHYSICAL_ASSET_EVENT_NOTIFICATION
234+
- TIME_RANGE
235+
- SAMPLE_RANGE
236+
- COUNT
237+
- PHYSICAL_ACTION_REQUEST
238+
- TIME_RANGE
239+
- SAMPLE_RANGE
240+
- COUNT
241+
- DIGITAL_ACTION_REQUEST
242+
- TIME_RANGE
243+
- SAMPLE_RANGE
244+
- COUNT
245+
- DIGITAL_TWIN_STATE
246+
- TIME_RANGE
247+
- SAMPLE_RANGE
248+
- COUNT
249+
- LAST_VALUE
250+
- NEW_PAD_NOTIFICATION
251+
- TIME_RANGE
252+
- SAMPLE_RANGE
253+
- COUNT
254+
- UPDATED_PAD_NOTIFICATION
255+
- TIME_RANGE
256+
- SAMPLE_RANGE
257+
- COUNT
258+
- PHYSICAL_RELATIONSHIP_INSTANCE_CREATED_NOTIFICATION
259+
- TIME_RANGE
260+
- SAMPLE_RANGE
261+
- COUNT
262+
- PHYSICAL_RELATIONSHIP_INSTANCE_DELETED_NOTIFICATION
263+
- TIME_RANGE
264+
- SAMPLE_RANGE
265+
- COUNT
266+
- LIFE_CYCLE_EVENT
267+
- TIME_RANGE
268+
- SAMPLE_RANGE
269+
- COUNT
270+
- LAST_VALUE
271+
- STORAGE_STATS
272+
- LAST_VALUE
194273

195274

196275

build.gradle.kts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ repositories {
1414
}
1515

1616
dependencies {
17-
api("io.undertow:undertow-core:2.2.27.Final")
17+
api("io.undertow:undertow-core:2.3.16.Final")
1818
api("com.github.spullara.mustache.java:compiler:0.9.10")
19-
api("ch.qos.logback:logback-classic:1.4.12")
19+
api("ch.qos.logback:logback-classic:1.4.14")
2020
api("com.google.code.gson:gson:2.10")
21-
api("io.github.wldt:wldt-core:0.3.0")
21+
api("io.github.wldt:wldt-core:0.4.0")
2222
testImplementation("junit:junit:4.13.2")
2323
}
2424

@@ -39,7 +39,7 @@ tasks.withType<Javadoc>() {
3939
}
4040

4141
group = "io.github.wldt"
42-
version = "0.1.1"
42+
version = "0.2"
4343
description = "Digital Adapter to expose the Digital Twin with the HTTP protocol within the WLDT Library"
4444
java.sourceCompatibility = JavaVersion.VERSION_1_8
4545

@@ -52,8 +52,8 @@ publishing {
5252
val snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots"
5353
url = uri(if (version.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl)
5454
credentials {
55-
username = (properties["ossrhUsername"] as String?)
56-
password = (properties["ossrhPassword"] as String?)
55+
username = (properties["ossrhToken"] as String?)
56+
password = (properties["ossrhTokenPassword"] as String?)
5757
}
5858
}
5959
maven {

src/main/java/it/wldt/adapter/http/digital/adapter/HttpDigitalAdapter.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import it.wldt.core.engine.DigitalTwin;
77
import it.wldt.core.state.*;
88
import it.wldt.exception.EventBusException;
9+
import it.wldt.storage.model.StorageStats;
10+
import it.wldt.storage.query.*;
911
import org.slf4j.Logger;
1012
import org.slf4j.LoggerFactory;
1113
import java.util.*;
@@ -53,6 +55,11 @@ public class HttpDigitalAdapter extends DigitalAdapter<HttpDigitalAdapterConfigu
5355
*/
5456
private Undertow server;
5557

58+
/**
59+
* The Query Executor used to interact with the Storage Manager
60+
*/
61+
private QueryExecutor queryExecutor;
62+
5663
/**
5764
* Constructs an HTTP Digital Adapter instance with the given configuration and digital twin instance.
5865
*
@@ -61,6 +68,8 @@ public class HttpDigitalAdapter extends DigitalAdapter<HttpDigitalAdapterConfigu
6168
*/
6269
public HttpDigitalAdapter(HttpDigitalAdapterConfiguration configuration, DigitalTwin digitalTwinInstance) {
6370
super(configuration.getId(), configuration);
71+
72+
// Set the Digital Twin Instance
6473
this.digitalTwinInstance = digitalTwinInstance;
6574
}
6675

@@ -104,12 +113,21 @@ protected void onEventNotificationReceived(DigitalTwinStateEventNotification<?>
104113
*/
105114
@Override
106115
public void onAdapterStart() {
116+
117+
// Create the query executor associated to the target DT Id and Adapter Id
118+
this.queryExecutor = new QueryExecutor(this.digitalTwinId, this.getId());
119+
120+
// Create the Query Executor
107121
this.server = Undertow.builder()
108122
.addHttpListener(getConfiguration().getPort(), getConfiguration().getHost())
109123
.setHandler(createDefaultRoutingHandler(this))
110124
.build();
125+
126+
// Start the Undertow Server
111127
this.server.start();
128+
112129
logger.info("HTTP Digital Adapter Started");
130+
113131
this.notifyDigitalAdapterBound();
114132
}
115133

@@ -542,4 +560,44 @@ public DigitalTwin onInstanceRequest() {
542560
return this.digitalTwinInstance;
543561
}
544562

563+
/**
564+
* Retrieves information about the Digital Twin Storage
565+
* @return An instance of StorageInfo with the details of the available Stored Information
566+
*/
567+
@Override
568+
public StorageStats onStorageInfoRequest() {
569+
try{
570+
571+
// Create Query Request to the Storage Manager for the Last Digital Twin State
572+
QueryRequest queryRequest = new QueryRequest();
573+
queryRequest.setResourceType(QueryResourceType.STORAGE_STATS);
574+
queryRequest.setRequestType(QueryRequestType.LAST_VALUE);
575+
576+
// Send the Query Request to the Storage Manager for the target DT
577+
QueryResult<?> queryResult = this.queryExecutor.syncQueryExecute(queryRequest);
578+
579+
if(queryResult != null && queryResult.isSuccessful() && queryResult.getTotalResults() == 1 && queryResult.getResults().get(0) instanceof StorageStats)
580+
return (StorageStats) queryResult.getResults().get(0);
581+
else
582+
return null;
583+
584+
}catch (Exception e){
585+
logger.error("Error retrieving Storage Info ! Msg: {}", e.getLocalizedMessage());
586+
return null;
587+
}
588+
}
589+
590+
@Override
591+
public QueryResult<?> onQueryRequest(QueryRequest queryRequest) {
592+
593+
try {
594+
// Send the Query Request to the Storage Manager for the target DT
595+
return this.queryExecutor.syncQueryExecute(queryRequest);
596+
}
597+
catch (Exception e){
598+
logger.error("Error executing Query Request ! Msg: {}", e.getLocalizedMessage());
599+
return new QueryResult<>(queryRequest, false, "Error executing Query Request ! Msg: " + e.getLocalizedMessage());
600+
}
601+
}
602+
545603
}

0 commit comments

Comments
 (0)