Skip to content

Commit

Permalink
feat: introduce DPS-API JSON-LD transformer registry (#3939)
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger authored and wolf4ood committed Feb 29, 2024
1 parent 4ec736b commit c3176a6
Show file tree
Hide file tree
Showing 29 changed files with 614 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class DataPlaneAuthorizationServiceImpl implements DataPlaneAuthorization
public static final String CLAIM_AGREEMENT_ID = "agreement_id";
public static final String CLAIM_ASSET_ID = "asset_id";
public static final String CLAIM_PROCESS_ID = "process_id";
public static final String CLAIM_TRANSFER_TYPE = "transfer_type";
public static final String CLAIM_FLOW_TYPE = "flow_type";
private final DataPlaneAccessTokenService accessTokenService;
private final PublicEndpointGeneratorService endpointGenerator;
private final DataPlaneAccessControlService accessControlService;
Expand Down Expand Up @@ -94,7 +94,7 @@ private TokenParameters createTokenParams(DataFlowStartMessage message) {
.claims(CLAIM_AGREEMENT_ID, message.getAgreementId())
.claims(CLAIM_ASSET_ID, message.getAssetId())
.claims(CLAIM_PROCESS_ID, message.getProcessId())
.claims(CLAIM_TRANSFER_TYPE, message.getTransferType())
.claims(CLAIM_FLOW_TYPE, message.getFlowType().toString())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.edc.spi.result.Result;
import org.eclipse.edc.spi.types.domain.DataAddress;
import org.eclipse.edc.spi.types.domain.transfer.DataFlowStartMessage;
import org.eclipse.edc.spi.types.domain.transfer.FlowType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
Expand Down Expand Up @@ -156,7 +157,7 @@ void authorize_accessNotGranted() {
private DataFlowStartMessage.Builder createStartMessage() {
return DataFlowStartMessage.Builder.newInstance()
.processId("test-processid")
.transferType("test-transfer-type")
.flowType(FlowType.PULL)
.agreementId("test-agreementid")
.participantId("test-participantid")
.assetId("test-assetid")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@

import static java.lang.String.format;
import static org.eclipse.edc.connector.transfer.dataplane.spi.TransferDataPlaneConstants.HTTP_PROXY;
import static org.eclipse.edc.connector.transfer.spi.flow.FlowType.PULL;
import static org.eclipse.edc.spi.response.ResponseStatus.FATAL_ERROR;
import static org.eclipse.edc.spi.response.StatusResult.failure;
import static org.eclipse.edc.spi.types.domain.transfer.FlowType.PULL;

public class ConsumerPullTransferDataFlowController implements DataFlowController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@

import static java.util.stream.Collectors.toSet;
import static org.eclipse.edc.connector.transfer.dataplane.spi.TransferDataPlaneConstants.HTTP_PROXY;
import static org.eclipse.edc.connector.transfer.spi.flow.FlowType.PULL;
import static org.eclipse.edc.connector.transfer.spi.flow.FlowType.PUSH;
import static org.eclipse.edc.spi.types.domain.transfer.FlowType.PULL;
import static org.eclipse.edc.spi.types.domain.transfer.FlowType.PUSH;

public class ProviderPushTransferDataFlowController implements DataFlowController {

Expand Down Expand Up @@ -68,7 +68,7 @@ public boolean canHandle(TransferProcess transferProcess) {
.processId(transferProcess.getId())
.sourceDataAddress(transferProcess.getContentDataAddress())
.destinationDataAddress(transferProcess.getDataDestination())
.transferType(transferProcess.getTransferType())
.flowType(PUSH)
.callbackAddress(callbackUrl != null ? callbackUrl.get() : null)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ dependencies {
api(project(":spi:common:web-spi"))
api(project(":spi:data-plane:data-plane-spi"))

implementation(project(":core:common:transform-core")) // for the transformer registry impl
implementation(project(":core:common:jersey-providers"))
implementation(project(":extensions:data-plane:data-plane-signaling:data-plane-signaling-transform"))
implementation(libs.jakarta.rsApi)

testImplementation(project(":core:common:junit"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,31 @@

package org.eclipse.edc.connector.api.signaling.configuration;

import jakarta.json.Json;
import org.eclipse.edc.connector.api.signaling.transform.SignalingApiTransformerRegistry;
import org.eclipse.edc.connector.api.signaling.transform.SignalingApiTransformerRegistryImpl;
import org.eclipse.edc.connector.api.signaling.transform.from.JsonObjectFromDataFlowSuspendMessageTransformer;
import org.eclipse.edc.connector.api.signaling.transform.from.JsonObjectFromDataFlowTerminateMessageTransformer;
import org.eclipse.edc.connector.api.signaling.transform.to.JsonObjectToDataFlowSuspendMessageTransformer;
import org.eclipse.edc.connector.api.signaling.transform.to.JsonObjectToDataFlowTerminateMessageTransformer;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.runtime.metamodel.annotation.Provider;
import org.eclipse.edc.runtime.metamodel.annotation.Provides;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.transform.spi.TypeTransformerRegistry;
import org.eclipse.edc.web.jersey.jsonld.JerseyJsonLdInterceptor;
import org.eclipse.edc.web.jersey.jsonld.ObjectMapperProvider;
import org.eclipse.edc.web.spi.WebServer;
import org.eclipse.edc.web.spi.WebService;
import org.eclipse.edc.web.spi.configuration.WebServiceConfigurer;
import org.eclipse.edc.web.spi.configuration.WebServiceSettings;

import java.util.Map;

import static org.eclipse.edc.connector.api.signaling.configuration.SignalingApiConfigurationExtension.NAME;
import static org.eclipse.edc.policy.model.OdrlNamespace.ODRL_PREFIX;
import static org.eclipse.edc.policy.model.OdrlNamespace.ODRL_SCHEMA;
Expand Down Expand Up @@ -62,6 +73,8 @@ public class SignalingApiConfigurationExtension implements ServiceExtension {
private JsonLd jsonLd;
@Inject
private TypeManager typeManager;
@Inject
private TypeTransformerRegistry transformerRegistry;

@Override
public String name() {
Expand All @@ -78,4 +91,16 @@ public void initialize(ServiceExtensionContext context) {
webService.registerResource(webServiceConfiguration.getContextAlias(), new ObjectMapperProvider(jsonLdMapper));
webService.registerResource(webServiceConfiguration.getContextAlias(), new JerseyJsonLdInterceptor(jsonLd, jsonLdMapper, SIGNALING_SCOPE));
}

@Provider
public SignalingApiTransformerRegistry managementApiTypeTransformerRegistry() {
var factory = Json.createBuilderFactory(Map.of());

var registry = new SignalingApiTransformerRegistryImpl(this.transformerRegistry);
registry.register(new JsonObjectFromDataFlowSuspendMessageTransformer(factory));
registry.register(new JsonObjectToDataFlowSuspendMessageTransformer());
registry.register(new JsonObjectFromDataFlowTerminateMessageTransformer(factory));
registry.register(new JsonObjectToDataFlowTerminateMessageTransformer());
return registry;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* 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:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/


plugins {
`java-library`
}

dependencies {
api(project(":spi:common:json-ld-spi"))
api(project(":spi:common:web-spi"))
api(project(":spi:data-plane:data-plane-spi"))

implementation(project(":core:common:transform-core")) // for the transformer registry impl
implementation(project(":core:common:jersey-providers"))
implementation(libs.jakarta.rsApi)

testImplementation(project(":core:common:junit"))
testImplementation(project(":extensions:common:json-ld"))
}
edcBuild {
swagger {
apiGroup.set("control-api")
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* 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:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.edc.connector.api.signaling.transform;

import org.eclipse.edc.transform.spi.TypeTransformerRegistry;

/**
* Type transformer used in the data plane signaling api context
*/
public interface SignalingApiTransformerRegistry extends TypeTransformerRegistry {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* 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:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.edc.connector.api.signaling.transform;

import org.eclipse.edc.core.transform.TypeTransformerRegistryImpl;
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.transform.spi.TypeTransformer;
import org.eclipse.edc.transform.spi.TypeTransformerRegistry;
import org.jetbrains.annotations.NotNull;

public class SignalingApiTransformerRegistryImpl extends TypeTransformerRegistryImpl implements SignalingApiTransformerRegistry {
private final TypeTransformerRegistry fallback;

public SignalingApiTransformerRegistryImpl(TypeTransformerRegistry fallback) {
this.fallback = fallback;
}

@Override
public @NotNull <INPUT, OUTPUT> TypeTransformer<INPUT, OUTPUT> transformerFor(@NotNull INPUT input, @NotNull Class<OUTPUT> outputType) {
try {
return super.transformerFor(input, outputType);
} catch (EdcException exception) {
return fallback.transformerFor(input, outputType);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* 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:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.edc.connector.api.signaling.transform.from;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.spi.types.domain.transfer.DataFlowStartMessage;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.spi.types.domain.transfer.DataFlowStartMessage.EDC_DATA_FLOW_START_MESSAGE_AGREEMENT_ID;
import static org.eclipse.edc.spi.types.domain.transfer.DataFlowStartMessage.EDC_DATA_FLOW_START_MESSAGE_DATASET_ID;
import static org.eclipse.edc.spi.types.domain.transfer.DataFlowStartMessage.EDC_DATA_FLOW_START_MESSAGE_DESTINATION_CALLBACK_ADDRESS;
import static org.eclipse.edc.spi.types.domain.transfer.DataFlowStartMessage.EDC_DATA_FLOW_START_MESSAGE_DESTINATION_DATA_ADDRESS;
import static org.eclipse.edc.spi.types.domain.transfer.DataFlowStartMessage.EDC_DATA_FLOW_START_MESSAGE_FLOW_TYPE;
import static org.eclipse.edc.spi.types.domain.transfer.DataFlowStartMessage.EDC_DATA_FLOW_START_MESSAGE_PARTICIPANT_ID;
import static org.eclipse.edc.spi.types.domain.transfer.DataFlowStartMessage.EDC_DATA_FLOW_START_MESSAGE_PROPERTIES;
import static org.eclipse.edc.spi.types.domain.transfer.DataFlowStartMessage.EDC_DATA_FLOW_START_MESSAGE_SOURCE_DATA_ADDRESS;
import static org.eclipse.edc.spi.types.domain.transfer.DataFlowStartMessage.EDC_DATA_FLOW_START_MESSAGE_TYPE;

/**
* Converts from a {@link DataFlowStartMessage} to a {@link JsonObject} in JSON-LD expanded form .
*/
public class JsonObjectFromDataFlowStartMessageTransformer extends AbstractJsonLdTransformer<DataFlowStartMessage, JsonObject> {
private final JsonBuilderFactory jsonFactory;
private final ObjectMapper mapper;

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

@Override
public @Nullable JsonObject transform(@NotNull DataFlowStartMessage message, @NotNull TransformerContext context) {
var propertiesBuilder = jsonFactory.createObjectBuilder();
transformProperties(message.getProperties(), propertiesBuilder, mapper, context);
return jsonFactory.createObjectBuilder()
.add(TYPE, EDC_DATA_FLOW_START_MESSAGE_TYPE)
.add(EDC_DATA_FLOW_START_MESSAGE_FLOW_TYPE, message.getFlowType().toString())
.add(EDC_DATA_FLOW_START_MESSAGE_AGREEMENT_ID, message.getAgreementId())
.add(EDC_DATA_FLOW_START_MESSAGE_DATASET_ID, message.getAssetId())
.add(EDC_DATA_FLOW_START_MESSAGE_PROPERTIES, propertiesBuilder)
.add(EDC_DATA_FLOW_START_MESSAGE_DESTINATION_CALLBACK_ADDRESS, message.getCallbackAddress().toString())
.add(EDC_DATA_FLOW_START_MESSAGE_DESTINATION_DATA_ADDRESS, context.transform(message.getDestinationDataAddress(), JsonObject.class))
.add(EDC_DATA_FLOW_START_MESSAGE_SOURCE_DATA_ADDRESS, context.transform(message.getSourceDataAddress(), JsonObject.class))
.add(EDC_DATA_FLOW_START_MESSAGE_PARTICIPANT_ID, message.getParticipantId())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
*/

package org.eclipse.edc.core.transform.transformer.from;
package org.eclipse.edc.connector.api.signaling.transform.from;

import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
*/

package org.eclipse.edc.core.transform.transformer.from;
package org.eclipse.edc.connector.api.signaling.transform.from;

import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
Expand Down
Loading

0 comments on commit c3176a6

Please sign in to comment.