Skip to content

Commit

Permalink
refactor(api): expose dataplanes api v4alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt committed Jan 8, 2025
1 parent 8534c24 commit 09459be
Show file tree
Hide file tree
Showing 20 changed files with 684 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@

import java.util.Optional;

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.DATAPLANE_INSTANCE_STATE;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.DATAPLANE_INSTANCE_STATE_TIMESTAMP;
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;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.URL;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
Expand All @@ -54,10 +52,8 @@ public JsonObjectFromDataPlaneInstanceTransformer(JsonBuilderFactory jsonFactory
.add(ID, dataPlaneInstance.getId())
.add(TYPE, DataPlaneInstance.DATAPLANE_INSTANCE_TYPE)
.add(URL, dataPlaneInstance.getUrl().toString())
.add(LAST_ACTIVE, dataPlaneInstance.getLastActive())
.add(TURN_COUNT, dataPlaneInstance.getTurnCount());
.add(LAST_ACTIVE, dataPlaneInstance.getLastActive());

//properties
if (dataPlaneInstance.getProperties() != null && !dataPlaneInstance.getProperties().isEmpty()) {
var propBuilder = jsonFactory.createObjectBuilder();
transformProperties(dataPlaneInstance.getProperties(), propBuilder, mapper, context);
Expand All @@ -67,9 +63,6 @@ public JsonObjectFromDataPlaneInstanceTransformer(JsonBuilderFactory jsonFactory
var srcBldr = jsonFactory.createArrayBuilder(dataPlaneInstance.getAllowedSourceTypes());
builder.add(ALLOWED_SOURCE_TYPES, srcBldr);

var dstBldr = jsonFactory.createArrayBuilder(dataPlaneInstance.getAllowedDestTypes());
builder.add(ALLOWED_DEST_TYPES, dstBldr);

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2025 Cofinity-X
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Cofinity-X - initial API and implementation
*
*/

package org.eclipse.edc.transform.transformer.edc.from;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance;
import org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstanceStates;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

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.DATAPLANE_INSTANCE_STATE;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.DATAPLANE_INSTANCE_STATE_TIMESTAMP;
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;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.URL;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;

public class JsonObjectFromDataPlaneInstanceV3Transformer extends AbstractJsonLdTransformer<DataPlaneInstance, JsonObject> {
private final JsonBuilderFactory jsonFactory;
private final ObjectMapper mapper;

public JsonObjectFromDataPlaneInstanceV3Transformer(JsonBuilderFactory jsonFactory, ObjectMapper mapper) {
super(DataPlaneInstance.class, JsonObject.class);
this.jsonFactory = jsonFactory;
this.mapper = mapper;
}

@Override
public @Nullable JsonObject transform(@NotNull DataPlaneInstance dataPlaneInstance, @NotNull TransformerContext context) {
var builder = jsonFactory.createObjectBuilder()
.add(ID, dataPlaneInstance.getId())
.add(TYPE, DataPlaneInstance.DATAPLANE_INSTANCE_TYPE)
.add(URL, dataPlaneInstance.getUrl().toString())
.add(LAST_ACTIVE, dataPlaneInstance.getLastActive())
.add(TURN_COUNT, dataPlaneInstance.getTurnCount());

if (dataPlaneInstance.getProperties() != null && !dataPlaneInstance.getProperties().isEmpty()) {
var propBuilder = jsonFactory.createObjectBuilder();
transformProperties(dataPlaneInstance.getProperties(), propBuilder, mapper, context);
builder.add(PROPERTIES, propBuilder);
}

var srcBldr = jsonFactory.createArrayBuilder(dataPlaneInstance.getAllowedSourceTypes());
builder.add(ALLOWED_SOURCE_TYPES, srcBldr);

var dstBldr = jsonFactory.createArrayBuilder(dataPlaneInstance.getAllowedDestTypes());
builder.add(ALLOWED_DEST_TYPES, dstBldr);

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

var state = Optional.ofNullable(DataPlaneInstanceStates.from(dataPlaneInstance.getState()))
.map(Enum::name)
.orElse(null);

addIfNotNull(state, DATAPLANE_INSTANCE_STATE, builder);

builder.add(DATAPLANE_INSTANCE_STATE_TIMESTAMP, dataPlaneInstance.getStateTimestamp());

return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@
import java.util.Map;

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.DATAPLANE_INSTANCE_STATE;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.DATAPLANE_INSTANCE_STATE_TIMESTAMP;
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;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.URL;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstanceStates.AVAILABLE;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID;
Expand All @@ -54,10 +52,8 @@ void transform() {
.id("test-id")
.url("http://foo.bar")
.allowedSourceType("test-source-type")
.allowedDestType("test-dest-type")
.allowedTransferType("test-transfer-type")
.lastActive(15)
.turnCount(42)
.property("foo", "bar")
.build();

Expand All @@ -67,10 +63,8 @@ void transform() {
assertThat(jsonObject.getString(ID)).isEqualTo("test-id");
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 All @@ -81,10 +75,8 @@ void transform_withState() {
.id("test-id")
.url("http://foo.bar")
.allowedSourceType("test-source-type")
.allowedDestType("test-dest-type")
.allowedTransferType("test-transfer-type")
.lastActive(15)
.turnCount(42)
.state(AVAILABLE.code())
.property("foo", "bar")
.build();
Expand All @@ -95,10 +87,8 @@ void transform_withState() {
assertThat(jsonObject.getString(ID)).isEqualTo("test-id");
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.getString(DATAPLANE_INSTANCE_STATE)).isEqualTo(AVAILABLE.name());
assertThat(jsonObject.getJsonNumber(DATAPLANE_INSTANCE_STATE_TIMESTAMP).longValue()).isEqualTo(dpi.getStateTimestamp());
assertThat(jsonObject.getJsonObject(PROPERTIES).getJsonString("foo").getString()).isEqualTo("bar");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2025 Cofinity-X
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Cofinity-X - initial API and implementation
*
*/

package org.eclipse.edc.transform.transformer.edc.from;

import jakarta.json.Json;
import jakarta.json.JsonString;
import org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Map;

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.DATAPLANE_INSTANCE_STATE;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.DATAPLANE_INSTANCE_STATE_TIMESTAMP;
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;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.URL;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstanceStates.AVAILABLE;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID;
import static org.eclipse.edc.jsonld.util.JacksonJsonLd.createObjectMapper;
import static org.mockito.Mockito.mock;

class JsonObjectFromDataPlaneInstanceV3TransformerTest {

private final TransformerContext context = mock();
private JsonObjectFromDataPlaneInstanceV3Transformer transformer;

@BeforeEach
void setUp() {
transformer = new JsonObjectFromDataPlaneInstanceV3Transformer(Json.createBuilderFactory(Map.of()), createObjectMapper());
}

@Test
void transform() {
var dpi = DataPlaneInstance.Builder.newInstance()
.id("test-id")
.url("http://foo.bar")
.allowedSourceType("test-source-type")
.allowedDestType("test-dest-type")
.allowedTransferType("test-transfer-type")
.lastActive(15)
.turnCount(42)
.property("foo", "bar")
.build();

var jsonObject = transformer.transform(dpi, context);

assertThat(jsonObject).isNotNull();
assertThat(jsonObject.getString(ID)).isEqualTo("test-id");
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");

}

@Test
void transform_withState() {
var dpi = DataPlaneInstance.Builder.newInstance()
.id("test-id")
.url("http://foo.bar")
.allowedSourceType("test-source-type")
.allowedDestType("test-dest-type")
.allowedTransferType("test-transfer-type")
.lastActive(15)
.turnCount(42)
.state(AVAILABLE.code())
.property("foo", "bar")
.build();

var jsonObject = transformer.transform(dpi, context);

assertThat(jsonObject).isNotNull();
assertThat(jsonObject.getString(ID)).isEqualTo("test-id");
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.getString(DATAPLANE_INSTANCE_STATE)).isEqualTo(AVAILABLE.name());
assertThat(jsonObject.getJsonNumber(DATAPLANE_INSTANCE_STATE_TIMESTAMP).longValue()).isEqualTo(dpi.getStateTimestamp());
assertThat(jsonObject.getJsonObject(PROPERTIES).getJsonString("foo").getString()).isEqualTo("bar");

}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[
{
"version": "3.0.4",
"version": "3.0.5",
"urlPath": "/v3",
"lastUpdated": "2024-12-19T10:14:00Z",
"lastUpdated": "2025-01-08T14:26:00Z",
"maturity": "stable"
},
{
Expand All @@ -14,7 +14,7 @@
{
"version": "4.0.0-alpha",
"urlPath": "/v4alpha",
"lastUpdated": "2024-12-04T14:24:00Z",
"lastUpdated": "2025-01-08T14:26:00Z",
"maturity": "alpha"
}
]
Loading

0 comments on commit 09459be

Please sign in to comment.