Skip to content

Commit 5676509

Browse files
committed
Refactoring, First basic transformer for assets into CBM datasets (WIP)
1 parent 5242ca1 commit 5676509

11 files changed

Lines changed: 367 additions & 123 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,4 @@ system-tests/src/test/resources/policy/provider-directory-local.json
6565
system-tests/src/test/resources/policy/consumer-directory-local.json
6666

6767
*.pem
68+
deployment/provider-ebird-init/scripts/samples

providers/provider/src/main/java/org/eclipse/edc/heleade/provider/extension/content/based/api/asset/AssetApiExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void initialize(ServiceExtensionContext context) {
7272
var managementTypeTransformerRegistry = transformerRegistry.forContext("management-api");
7373

7474
managementTypeTransformerRegistry.register(new CbmJsonObjectToAssetJsonObjectTransformer());
75-
managementTypeTransformerRegistry.register(new JsonObjectAssetToCbmJsonObjectTransformer());
75+
managementTypeTransformerRegistry.register(new AssetJsonObjectToCbmJsonObjectTransformer());
7676

7777
webService.registerResource(ApiContext.MANAGEMENT, new ContentBasedAssetApiController(assetService,
7878
managementTypeTransformerRegistry, monitor, validator));

providers/provider/src/main/java/org/eclipse/edc/heleade/provider/extension/content/based/api/asset/AssetJsonObject.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
public class AssetJsonObject {
2525

26-
private JsonObject jsonObject;
26+
private final JsonObject jsonObject;
2727

2828
/**
2929
* Constructor for wrapper class
@@ -35,8 +35,4 @@ public AssetJsonObject(JsonObject jsonObject) {
3535
public JsonObject getJsonObject() {
3636
return jsonObject;
3737
}
38-
39-
public void setJsonObject(JsonObject jsonObject) {
40-
this.jsonObject = jsonObject;
41-
}
4238
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* Copyright (c) 2025 Universidad de Alicante
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Apache License, Version 2.0 which is available at
6+
* https://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* SPDX-License-Identifier: Apache-2.0
9+
*
10+
* Contributors:
11+
* LdE - Universidad de Alicante - initial implementation
12+
*
13+
*/
14+
15+
package org.eclipse.edc.heleade.provider.extension.content.based.api.asset;
16+
17+
import jakarta.json.Json;
18+
import jakarta.json.JsonArrayBuilder;
19+
import jakarta.json.JsonObject;
20+
import jakarta.json.JsonObjectBuilder;
21+
import jakarta.json.JsonString;
22+
import jakarta.json.JsonValue;
23+
import org.eclipse.edc.connector.controlplane.asset.spi.domain.Asset;
24+
import org.eclipse.edc.connector.controlplane.catalog.spi.Dataset;
25+
import org.eclipse.edc.heleade.commons.content.based.catalog.CbmConstants;
26+
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
27+
import org.eclipse.edc.transform.spi.TransformerContext;
28+
import org.jetbrains.annotations.NotNull;
29+
import org.jetbrains.annotations.Nullable;
30+
31+
import static org.eclipse.edc.connector.controlplane.asset.spi.domain.Asset.EDC_ASSET_TYPE;
32+
import static org.eclipse.edc.connector.controlplane.asset.spi.domain.Asset.PROPERTY_ID;
33+
import static org.eclipse.edc.jsonld.spi.Namespaces.DCAT_SCHEMA;
34+
import static org.eclipse.edc.jsonld.spi.Namespaces.DCT_SCHEMA;
35+
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_DATASET_TYPE;
36+
import static org.eclipse.edc.jsonld.spi.TypeUtil.nodeType;
37+
import static org.eclipse.edc.spi.constants.CoreConstants.EDC_NAMESPACE;
38+
39+
/**
40+
* Converts from an {@link Asset} as a {@link JsonObject} in JSON-LD expanded form of a {@link Dataset}.
41+
*/
42+
public class AssetJsonObjectToCbmJsonObjectTransformer extends AbstractJsonLdTransformer<AssetJsonObject, JsonObject> {
43+
/**
44+
* Constructor for AssetJsonObjectToCbmJsonObjectTransformer.
45+
* Configures the transformer to handle transformations from a JsonObject
46+
* to another JsonObject, leveraging the base functionality provided
47+
* by the AbstractJsonLdTransformer.
48+
*/
49+
public AssetJsonObjectToCbmJsonObjectTransformer() {
50+
super(AssetJsonObject.class, JsonObject.class);
51+
}
52+
53+
@Override
54+
public @Nullable JsonObject transform(@NotNull AssetJsonObject assetJsonObject, @NotNull TransformerContext context) {
55+
JsonObject jsonObject = assetJsonObject.getJsonObject();
56+
57+
// Check if we have a valid Dataset
58+
if (!EDC_ASSET_TYPE.equals(nodeType(jsonObject))) {
59+
context.problem()
60+
.unexpectedType()
61+
.expected(EDC_ASSET_TYPE)
62+
.actual(nodeType(jsonObject))
63+
.report();
64+
return null;
65+
}
66+
67+
// Extract the ID
68+
var id = nodeId(jsonObject);
69+
if (id == null) {
70+
context.problem()
71+
.missingProperty()
72+
.property("@id")
73+
.report();
74+
return null;
75+
}
76+
77+
return transformAssetToCbmJsonObject(id, jsonObject);
78+
}
79+
80+
public static JsonObject transformAssetToCbmJsonObject(String id, JsonObject jsonObject) {
81+
// Check if we have a valid Dataset
82+
if (!EDC_ASSET_TYPE.equals(nodeType(jsonObject))) {
83+
return null;
84+
}
85+
86+
// Extract the ID
87+
if (id == null) {
88+
return null;
89+
}
90+
91+
// Start building the Asset object
92+
var datasetBuilder = Json.createObjectBuilder()
93+
.add("@id", id)
94+
.add("@type", DCAT_DATASET_TYPE);
95+
96+
// Add context from original object
97+
if (jsonObject.containsKey("@context")) {
98+
datasetBuilder.add("@context", jsonObject.get("@context"));
99+
}
100+
101+
var propertiesValue = jsonObject.get(EDC_NAMESPACE + "properties");
102+
JsonObject properties = getObjectOrFirstInArray(jsonObject.get(EDC_NAMESPACE + "properties"));
103+
104+
properties.forEach((key, value) -> {
105+
if (!isForbiddenProperty(key)) {
106+
datasetBuilder.add(key, value);
107+
}
108+
});
109+
110+
// build the data distribution
111+
JsonArrayBuilder distributionArrayBuilder = Json.createArrayBuilder();
112+
JsonObject dataAddress = getObjectOrFirstInArray(jsonObject.get(EDC_NAMESPACE + "dataAddress"));
113+
114+
JsonObjectBuilder distributionBuilder = Json.createObjectBuilder();
115+
JsonObjectBuilder format = Json.createObjectBuilder();
116+
117+
distributionBuilder.add("@type", DCAT_SCHEMA + "Distribution");
118+
119+
// get format and type
120+
String dataAddressType = getStringOrFirstInArray(dataAddress.get(EDC_NAMESPACE + "type"));
121+
format.add("@id", dataAddressType);
122+
format.add(DCT_SCHEMA + "type", format.build());
123+
distributionBuilder.add(DCT_SCHEMA + "format", format.build());
124+
125+
// get url
126+
var baseUrlValue = dataAddress.get(EDC_NAMESPACE + "baseUrl");
127+
if (baseUrlValue != null) {
128+
String baseUrl = getStringOrFirstInArray(baseUrlValue);
129+
distributionBuilder.add(DCAT_SCHEMA + "accessURL", baseUrl);
130+
}
131+
132+
distributionArrayBuilder.add(distributionBuilder.build());
133+
134+
135+
datasetBuilder.add(CbmConstants.DISTRIBUTION_TAG, distributionArrayBuilder.build());
136+
137+
return datasetBuilder.build();
138+
}
139+
140+
private static boolean isForbiddenProperty(String key) {
141+
// Properties that are forbidden
142+
return "id".equals(key) || PROPERTY_ID.equals(key) || key.startsWith("@");
143+
}
144+
145+
private static JsonObject getObjectOrFirstInArray(JsonValue value) {
146+
if (value.getValueType() == JsonValue.ValueType.ARRAY) {
147+
return value.asJsonArray().getJsonObject(0);
148+
} else {
149+
return value.asJsonObject();
150+
}
151+
}
152+
153+
private static String getStringOrFirstInArray(JsonValue value) {
154+
if (value.getValueType() == JsonValue.ValueType.ARRAY) {
155+
JsonObject jsonObject = value.asJsonArray().getJsonObject(0);
156+
return jsonObject.getString("@value");
157+
} else if (value.getValueType() == JsonValue.ValueType.OBJECT) {
158+
return value.asJsonObject().getString("@value");
159+
} else if (value.getValueType() == JsonValue.ValueType.STRING) {
160+
return ((JsonString) value).getString();
161+
}
162+
return null;
163+
}
164+
165+
}

providers/provider/src/main/java/org/eclipse/edc/heleade/provider/extension/content/based/api/asset/CbmJsonObject.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
*/
2323
public class CbmJsonObject {
2424

25-
private JsonObject jsonObject;
25+
private final JsonObject jsonObject;
2626

2727
/**
2828
* Constructor for wrapper class
29+
*
30+
* @param jsonObject object to wrap
2931
*/
3032
public CbmJsonObject(JsonObject jsonObject) {
3133
this.jsonObject = jsonObject;
@@ -34,8 +36,4 @@ public CbmJsonObject(JsonObject jsonObject) {
3436
public JsonObject getJsonObject() {
3537
return jsonObject;
3638
}
37-
38-
public void setJsonObject(JsonObject jsonObject) {
39-
this.jsonObject = jsonObject;
40-
}
4139
}

providers/provider/src/main/java/org/eclipse/edc/heleade/provider/extension/content/based/api/asset/CbmJsonObjectToAssetJsonObjectTransformer.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,33 @@ public CbmJsonObjectToAssetJsonObjectTransformer() {
7979
return null;
8080
}
8181

82+
AtomicBoolean propertiesValid = new AtomicBoolean(true);
83+
jsonObject.forEach((key, value) -> {
84+
if (!isIgnoredProperty(key)) {
85+
if (isForbiddenProperty(key)) {
86+
context.problem()
87+
.invalidProperty()
88+
.property(key)
89+
.report();
90+
}
91+
}
92+
});
93+
94+
return transformCbmToAssetJsonObject(id, jsonObject);
95+
}
96+
97+
public static JsonObject transformCbmToAssetJsonObject(String id, JsonObject jsonObject) {
98+
99+
// Check if we have a valid Dataset
100+
if (!DCAT_DATASET_TYPE.equals(nodeType(jsonObject)) && !CBM_SAMPLE_TYPE.equals(nodeType(jsonObject))) {
101+
return null;
102+
}
103+
104+
// Extract the ID
105+
if (id == null) {
106+
return null;
107+
}
108+
82109
// Start building the Asset object
83110
var assetBuilder = Json.createObjectBuilder()
84111
.add("@id", id)
@@ -94,10 +121,6 @@ public CbmJsonObjectToAssetJsonObjectTransformer() {
94121
jsonObject.forEach((key, value) -> {
95122
if (!isIgnoredProperty(key)) {
96123
if (isForbiddenProperty(key)) {
97-
context.problem()
98-
.invalidProperty()
99-
.property(key)
100-
.report();
101124
propertiesValid.set(false);
102125
}
103126
// Add property object to array
@@ -142,20 +165,20 @@ public CbmJsonObjectToAssetJsonObjectTransformer() {
142165
/**
143166
* Determines if a property should be ignored when building the properties object.
144167
*/
145-
private boolean isIgnoredProperty(String key) {
168+
private static boolean isIgnoredProperty(String key) {
146169
// Properties that should not go into the properties object
147170
return "@id".equals(key) || "@type".equals(key) || "@context".equals(key) || DCAT_DISTRIBUTION_ATTRIBUTE.equals(key) || ODRL_POLICY_ATTRIBUTE.equals(key);
148171
}
149172

150173
/**
151174
* Determines if a property is forbidden and method should fail.
152175
*/
153-
private boolean isForbiddenProperty(String key) {
176+
private static boolean isForbiddenProperty(String key) {
154177
// Properties that are forbidden
155178
return "id".equals(key) || PROPERTY_ID.equals(key);
156179
}
157180

158-
private JsonArrayBuilder getDataAddress(JsonObject dataDistribution) {
181+
private static JsonArrayBuilder getDataAddress(JsonObject dataDistribution) {
159182
var dataAddressArrayBuilder = Json.createArrayBuilder();
160183
var dataAddressBuilder = Json.createObjectBuilder();
161184
dataAddressBuilder.add("@type", EDC_ASSET_DATA_ADDRESS);
@@ -179,15 +202,15 @@ private JsonArrayBuilder getDataAddress(JsonObject dataDistribution) {
179202
return dataAddressArrayBuilder;
180203
}
181204

182-
private JsonArray getStringPropertyAsArray(String value) {
205+
private static JsonArray getStringPropertyAsArray(String value) {
183206
var propertyArray = Json.createArrayBuilder()
184207
.add(Json.createObjectBuilder()
185208
.add("@value", value)
186209
.build());
187210
return propertyArray.build();
188211
}
189212

190-
private String getDataAddressType(JsonObject dataDistribution) {
213+
private static String getDataAddressType(JsonObject dataDistribution) {
191214
if (dataDistribution.containsKey(DCT_SCHEMA + "format")) {
192215
String distributionType = dataDistribution.getJsonArray(DCT_SCHEMA + "format").getJsonObject(0).getString("@id");
193216
if (distributionType.equals("HttpData-PULL") || distributionType.equals("HttpData-PUSH")) {

providers/provider/src/main/java/org/eclipse/edc/heleade/provider/extension/content/based/api/asset/JsonObjectAssetToCbmJsonObjectTransformer.java

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)