Skip to content

Commit

Permalink
feat: introduce allowedTransferTypes in DataPlaneInstance (#3952)
Browse files Browse the repository at this point in the history
* feat: introduce allowedTransferTypes in DataPlaneInstance

* chore: dependencies file
  • Loading branch information
wolf4ood authored Mar 4, 2024
1 parent 6f35b1b commit 77636db
Show file tree
Hide file tree
Showing 20 changed files with 173 additions and 16 deletions.
5 changes: 3 additions & 2 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,10 @@ maven/mavencentral/javax.ws.rs/javax.ws.rs-api/2.1, (CDDL-1.1 OR GPL-2.0 WITH Cl
maven/mavencentral/joda-time/joda-time/2.10.5, Apache-2.0, approved, clearlydefined
maven/mavencentral/junit/junit/4.13.2, EPL-2.0, approved, CQ23636
maven/mavencentral/net.bytebuddy/byte-buddy-agent/1.14.1, Apache-2.0, approved, #7164
maven/mavencentral/net.bytebuddy/byte-buddy-agent/1.14.11, Apache-2.0, approved, #7164
maven/mavencentral/net.bytebuddy/byte-buddy-agent/1.14.12, Apache-2.0, approved, #7164
maven/mavencentral/net.bytebuddy/byte-buddy/1.14.1, Apache-2.0 AND BSD-3-Clause, approved, #7163
maven/mavencentral/net.bytebuddy/byte-buddy/1.14.11, Apache-2.0 AND BSD-3-Clause, approved, #7163
maven/mavencentral/net.bytebuddy/byte-buddy/1.14.12, Apache-2.0 AND BSD-3-Clause, approved, #7163
maven/mavencentral/net.java.dev.jna/jna/5.13.0, Apache-2.0 AND LGPL-2.1-or-later, approved, #6709
maven/mavencentral/net.javacrumbs.json-unit/json-unit-core/2.36.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/net.minidev/accessors-smart/2.4.7, Apache-2.0, approved, #7515
Expand Down Expand Up @@ -321,8 +322,8 @@ maven/mavencentral/org.lz4/lz4-java/1.8.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.mock-server/mockserver-client-java/5.15.0, Apache-2.0 AND LGPL-3.0-only, approved, #9324
maven/mavencentral/org.mock-server/mockserver-core/5.15.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.mock-server/mockserver-netty/5.15.0, Apache-2.0, approved, #9276
maven/mavencentral/org.mockito/mockito-core/5.11.0, MIT AND (Apache-2.0 AND MIT) AND Apache-2.0, approved, #13505
maven/mavencentral/org.mockito/mockito-core/5.2.0, MIT AND (Apache-2.0 AND MIT) AND Apache-2.0, approved, #7401
maven/mavencentral/org.mockito/mockito-core/5.9.0, MIT AND (Apache-2.0 AND MIT) AND Apache-2.0, approved, #12774
maven/mavencentral/org.mockito/mockito-inline/5.2.0, MIT, approved, clearlydefined
maven/mavencentral/org.mozilla/rhino/1.7.7.2, MPL-2.0 AND BSD-3-Clause AND ISC, approved, CQ16320
maven/mavencentral/org.objenesis/objenesis/3.3, Apache-2.0, approved, clearlydefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ public List<DataPlaneInstance> getAll() {
}

@Override
public DataPlaneInstance select(DataAddress source, DataAddress destination, String selectionStrategy) {
public DataPlaneInstance select(DataAddress source, DataAddress destination, String selectionStrategy, String transferType) {
var strategy = selectionStrategyRegistry.find(selectionStrategy);
if (strategy == null) {
throw new IllegalArgumentException("Strategy " + selectionStrategy + " was not found");
}

return transactionContext.execute(() -> {
try (var stream = store.getAll()) {
var dataPlanes = stream.filter(dataPlane -> dataPlane.canHandle(source, destination)).toList();
var dataPlanes = stream.filter(dataPlane -> dataPlane.canHandle(source, destination, transferType)).toList();
return strategy.apply(dataPlanes);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public JsonObject find(JsonObject requestObject) {
.orElseThrow(InvalidRequestException::new);

var dpi = ofNullable(request.getStrategy())
.map(strategy -> catchException(() -> selectionService.select(request.getSource(), request.getDestination(), strategy)))
.map(strategy -> catchException(() -> selectionService.select(request.getSource(), request.getDestination(), strategy, request.getTransferType())))
.orElseGet(() -> catchException(() -> selectionService.select(request.getSource(), request.getDestination())));

if (dpi == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ public class SelectionRequest {
public static final String SELECTION_REQUEST_TYPE = EDC_NAMESPACE + "SelectionRequest";
public static final String SOURCE_ADDRESS = EDC_NAMESPACE + "source";
public static final String DEST_ADDRESS = EDC_NAMESPACE + "destination";
public static final String TRANSFER_TYPE = EDC_NAMESPACE + "transferType";
public static final String STRATEGY = EDC_NAMESPACE + "strategy";
private DataAddress source;
private DataAddress destination;
private String strategy;

private String transferType;

private SelectionRequest() {
}

Expand All @@ -47,6 +50,9 @@ public String getStrategy() {
return strategy;
}

public String getTransferType() {
return transferType;
}

public static final class Builder {
private final SelectionRequest instance;
Expand Down Expand Up @@ -74,6 +80,11 @@ public Builder strategy(String strategy) {
return this;
}

public Builder transferType(String transferType) {
this.instance.transferType = transferType;
return this;
}

public SelectionRequest build() {
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public record DataPlaneInstanceSchema(
"source-type1",
"source-type2"
],
"allowedDestTypes": ["your-dest-type"]
"allowedDestTypes": ["your-dest-type"],
"allowedTransferTypes": ["transfer-type"]
}
""";
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public record SelectionRequestSchema(
@Schema(name = TYPE, example = SELECTION_REQUEST_TYPE)
String type,
String strategy,
String transferType,
ManagementApiSchema.DataAddressSchema source,
ManagementApiSchema.DataAddressSchema destination
) {
Expand All @@ -41,7 +42,8 @@ public record SelectionRequestSchema(
"@type": "https://w3id.org/edc/v0.0.1/ns/DataAddress",
"type": "test-dst2"
},
"strategy": "you_custom_strategy"
"strategy": "you_custom_strategy",
"transferType": "you_custom_transfer_type"
}
""";
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_DEST_TYPES;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_SOURCE_TYPES;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_TRANSFER_TYPES;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.LAST_ACTIVE;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.PROPERTIES;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.TURN_COUNT;
Expand Down Expand Up @@ -64,6 +65,9 @@ public JsonObjectFromDataPlaneInstanceTransformer(JsonBuilderFactory jsonFactory
var dstBldr = jsonFactory.createArrayBuilder(dataPlaneInstance.getAllowedDestTypes());
builder.add(ALLOWED_DEST_TYPES, dstBldr);

var transferTypesBldr = jsonFactory.createArrayBuilder(dataPlaneInstance.getAllowedTransferTypes());
builder.add(ALLOWED_TRANSFER_TYPES, transferTypesBldr);


return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_DEST_TYPES;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_SOURCE_TYPES;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_TRANSFER_TYPES;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.Builder;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.LAST_ACTIVE;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.PROPERTIES;
Expand Down Expand Up @@ -71,6 +72,10 @@ private void transformProperties(String key, JsonValue jsonValue, DataPlaneInsta
var set = jsonValue.asJsonArray().stream().map(jv -> transformString(jv, context)).collect(Collectors.toSet());
builder.allowedSourceTypes(set);
}
case ALLOWED_TRANSFER_TYPES -> {
var set = jsonValue.asJsonArray().stream().map(jv -> transformString(jv, context)).collect(Collectors.toSet());
builder.allowedTransferType(set);
}
case PROPERTIES -> {
var props = jsonValue.asJsonArray().getJsonObject(0);
visitProperties(props, (k, val) -> transformProperties(k, val, builder, context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public JsonObjectToSelectionRequestTransformer() {
case SelectionRequest.SOURCE_ADDRESS ->
builder.source(transformObject(jsonValue, DataAddress.class, context));
case SelectionRequest.STRATEGY -> builder.strategy(transformString(jsonValue, context));
case SelectionRequest.TRANSFER_TYPE -> builder.transferType(transformString(jsonValue, context));
default -> throw new IllegalStateException("Unexpected value: " + key);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ void dataPlaneInstanceInputExample() throws JsonProcessingException {
assertThat(transformed.getUrl().toString()).isEqualTo("http://somewhere.com:1234/api/v1");
assertThat(transformed.getAllowedDestTypes()).containsExactlyInAnyOrder("your-dest-type");
assertThat(transformed.getAllowedSourceTypes()).containsExactlyInAnyOrder("source-type1", "source-type2");
assertThat(transformed.getAllowedTransferTypes()).containsExactlyInAnyOrder("transfer-type");
});
}

Expand All @@ -83,6 +84,7 @@ void selectionRequestInputExample() throws JsonProcessingException {
assertThat(transformed.getStrategy()).isNotBlank();
assertThat(transformed.getDestination()).isNotNull();
assertThat(transformed.getSource()).isNotNull();
assertThat(transformed.getTransferType()).isNotNull();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_DEST_TYPES;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_SOURCE_TYPES;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_TRANSFER_TYPES;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.LAST_ACTIVE;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.PROPERTIES;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.TURN_COUNT;
Expand All @@ -51,6 +52,7 @@ void transform() {
.url("http://foo.bar")
.allowedSourceType("test-source-type")
.allowedDestType("test-dest-type")
.allowedTransferType("test-transfer-type")
.lastActive(15)
.turnCount(42)
.property("foo", "bar")
Expand All @@ -63,6 +65,7 @@ void transform() {
assertThat(jsonObject.getString(URL)).isEqualTo("http://foo.bar");
assertThat(jsonObject.getJsonArray(ALLOWED_SOURCE_TYPES)).hasSize(1).allMatch(v -> ((JsonString) v).getString().equals("test-source-type"));
assertThat(jsonObject.getJsonArray(ALLOWED_DEST_TYPES)).hasSize(1).allMatch(v -> ((JsonString) v).getString().equals("test-dest-type"));
assertThat(jsonObject.getJsonArray(ALLOWED_TRANSFER_TYPES)).hasSize(1).allMatch(v -> ((JsonString) v).getString().equals("test-transfer-type"));
assertThat(jsonObject.getJsonNumber(LAST_ACTIVE).intValue()).isEqualTo(15);
assertThat(jsonObject.getJsonNumber(TURN_COUNT).intValue()).isEqualTo(42);
assertThat(jsonObject.getJsonObject(PROPERTIES).getJsonString("foo").getString()).isEqualTo("bar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_DEST_TYPES;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_SOURCE_TYPES;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_TRANSFER_TYPES;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.LAST_ACTIVE;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.TURN_COUNT;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.URL;
Expand Down Expand Up @@ -95,6 +96,28 @@ void transform_withProperties() {
assertThat(dpi.getProperties()).containsEntry(EDC_NAMESPACE + "publicApiUrl", "http://localhost/public");
}

@Test
void transform_withTransferTypes() {
var json = createObjectBuilder()
.add(ID, "test-id")
.add(URL, "http://somewhere.com:1234/api/v1")
.add(ALLOWED_SOURCE_TYPES, createArrayBuilder(Set.of("source1", "source2")))
.add(LAST_ACTIVE, 234L)
.add(TURN_COUNT, 42)
.add(ALLOWED_DEST_TYPES, createArrayBuilder(Set.of("dest1", "dest2")))
.add(ALLOWED_TRANSFER_TYPES, createArrayBuilder(Set.of("transfer1", "transfer2")))
.build();

var dpi = transformer.transform(expand(json), context);
assertThat(dpi).isNotNull();
assertThat(dpi.getUrl().toString()).isEqualTo("http://somewhere.com:1234/api/v1");
assertThat(dpi.getTurnCount()).isEqualTo(42);
assertThat(dpi.getLastActive()).isEqualTo(234L);
assertThat(dpi.getAllowedDestTypes()).hasSize(2).containsExactlyInAnyOrder("dest1", "dest2");
assertThat(dpi.getAllowedSourceTypes()).hasSize(2).containsExactlyInAnyOrder("source1", "source2");
assertThat(dpi.getAllowedTransferTypes()).hasSize(2).containsExactlyInAnyOrder("transfer1", "transfer2");
}

@Test
void transform_malformedUrl() {
var json = createObjectBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.eclipse.edc.connector.dataplane.selector.api.v2.model.SelectionRequest.DEST_ADDRESS;
import static org.eclipse.edc.connector.dataplane.selector.api.v2.model.SelectionRequest.SOURCE_ADDRESS;
import static org.eclipse.edc.connector.dataplane.selector.api.v2.model.SelectionRequest.STRATEGY;
import static org.eclipse.edc.connector.dataplane.selector.api.v2.model.SelectionRequest.TRANSFER_TYPE;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
Expand Down Expand Up @@ -59,7 +60,8 @@ void transform(String strategy) {
.add(TYPE, DataAddress.EDC_DATA_ADDRESS_TYPE)
.add(DataAddress.EDC_DATA_ADDRESS_TYPE_PROPERTY, "test-type")
.add(DataAddress.EDC_DATA_ADDRESS_KEY_NAME, "test-key")
);
)
.add(TRANSFER_TYPE, "transfer-type");
if (strategy != null) {
builder.add(STRATEGY, strategy);
}
Expand All @@ -70,6 +72,7 @@ void transform(String strategy) {
assertThat(rq.getStrategy()).isEqualTo(strategy);
assertThat(rq.getDestination()).isNotNull();
assertThat(rq.getSource()).isNotNull();
assertThat(rq.getTransferType()).isEqualTo("transfer-type");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public List<DataPlaneInstance> getAll() {
}

@Override
public DataPlaneInstance select(DataAddress source, DataAddress destination, String selectionStrategy) {
public DataPlaneInstance select(DataAddress source, DataAddress destination, String selectionStrategy, String transferType) {
var srcAddress = typeTransformerRegistry.transform(source, JsonObject.class).orElseThrow(f -> new EdcException(f.getFailureDetail()));
var dstAddress = typeTransformerRegistry.transform(destination, JsonObject.class).orElseThrow(f -> new EdcException(f.getFailureDetail()));
var jsonObject = Json.createObjectBuilder()
Expand All @@ -100,6 +100,10 @@ public DataPlaneInstance select(DataAddress source, DataAddress destination, Str
jsonObject.add(EDC_NAMESPACE + "strategy", this.selectionStrategy);
}

if (transferType != null) {
jsonObject.add(EDC_NAMESPACE + "transferType", transferType);
}

var body = RequestBody.create(jsonObject.build().toString(), TYPE_JSON);

var request = new Request.Builder().post(body).url(url + SELECT_PATH).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import static org.eclipse.edc.junit.testfixtures.TestUtils.testHttpClient;
import static org.eclipse.edc.spi.CoreConstants.JSON_LD;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -99,6 +100,17 @@ void find() {

}

@Test
void find_withTransferType() {
var expected = createInstance("some-instance");
when(SELECTOR_SERVICE_MOCK.select(any(), any(), eq("random"), eq("transferType"))).thenReturn(expected);

var result = service.select(DataAddress.Builder.newInstance().type("test1").build(), DataAddress.Builder.newInstance().type("test2").build(), "random", "transferType");

assertThat(result).usingRecursiveComparison().isEqualTo(expected);

}

@Override
protected Object controller() {
return new DataplaneSelectorApiController(SELECTOR_SERVICE_MOCK, typeTransformerRegistry, validator, Clock.systemUTC());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.eclipse.edc.runtime.metamodel.annotation.ExtensionPoint;
import org.eclipse.edc.spi.result.ServiceResult;
import org.eclipse.edc.spi.types.domain.DataAddress;
import org.jetbrains.annotations.Nullable;

import java.util.List;

Expand All @@ -44,7 +45,16 @@ default DataPlaneInstance select(DataAddress source, DataAddress destination) {
* Selects the {@link DataPlaneInstance} that can handle a source and destination {@link DataAddress} using the passed
* strategy.
*/
DataPlaneInstance select(DataAddress source, DataAddress destination, String selectionStrategy);
default DataPlaneInstance select(DataAddress source, DataAddress destination, String selectionStrategy) {
return select(source, destination, selectionStrategy, null);
}

/**
* Selects the {@link DataPlaneInstance} that can handle a source and destination {@link DataAddress} using the passed
* strategy and the optional transferType.
*/
DataPlaneInstance select(DataAddress source, DataAddress destination, String selectionStrategy, @Nullable String transferType);


/**
* Add a data plane instance
Expand Down
Loading

0 comments on commit 77636db

Please sign in to comment.