Skip to content

Commit

Permalink
refactor: extract dsp transfer process transform lib
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Oct 16, 2024
1 parent 10e603d commit c537cf2
Show file tree
Hide file tree
Showing 35 changed files with 319 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ public interface DspTransferProcessPropertyAndTypeNames {
String DSPACE_TYPE_TRANSFER_START_MESSAGE_IRI = DSPACE_SCHEMA + DSPACE_TYPE_TRANSFER_START_MESSAGE_TERM;
String DSPACE_TYPE_TRANSFER_COMPLETION_MESSAGE_TERM = "TransferCompletionMessage";
String DSPACE_TYPE_TRANSFER_COMPLETION_MESSAGE_IRI = DSPACE_SCHEMA + DSPACE_TYPE_TRANSFER_COMPLETION_MESSAGE_TERM;
String DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE = DSPACE_SCHEMA + "TransferSuspensionMessage";
String DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE_TERM = "TransferSuspensionMessage";
String DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE_IRI = DSPACE_SCHEMA + DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE_TERM;
String DSPACE_TYPE_TRANSFER_TERMINATION_MESSAGE_TERM = "TransferTerminationMessage";
String DSPACE_TYPE_TRANSFER_TERMINATION_MESSAGE_IRI = DSPACE_SCHEMA + DSPACE_TYPE_TRANSFER_TERMINATION_MESSAGE_TERM;
String DSPACE_TYPE_TRANSFER_PROCESS = DSPACE_SCHEMA + "TransferProcess";
String DSPACE_TYPE_TRANSFER_ERROR = DSPACE_SCHEMA + "TransferError";

String DSPACE_PROPERTY_CONTRACT_AGREEMENT_ID = DSPACE_SCHEMA + "agreementId";
String DSPACE_PROPERTY_DATA_ADDRESS = DSPACE_SCHEMA + "dataAddress";
String DSPACE_TYPE_TRANSFER_PROCESS_TERM = "TransferProcess";
String DSPACE_TYPE_TRANSFER_PROCESS_IRI = DSPACE_SCHEMA + DSPACE_TYPE_TRANSFER_PROCESS_TERM;
String DSPACE_TYPE_TRANSFER_ERROR_TERM = "TransferError";
String DSPACE_TYPE_TRANSFER_ERROR_IRI = DSPACE_SCHEMA + DSPACE_TYPE_TRANSFER_ERROR_TERM;
String DSPACE_PROPERTY_CONTRACT_AGREEMENT_ID_TERM = DSPACE_SCHEMA + "agreementId";
String DSPACE_PROPERTY_CONTRACT_AGREEMENT_ID_IRI = DSPACE_SCHEMA + DSPACE_PROPERTY_CONTRACT_AGREEMENT_ID_TERM;
String DSPACE_PROPERTY_DATA_ADDRESS_TERM = "dataAddress";
String DSPACE_PROPERTY_DATA_ADDRESS_IRI = DSPACE_SCHEMA + DSPACE_PROPERTY_DATA_ADDRESS_TERM;
}
1 change: 1 addition & 0 deletions data-protocols/dsp/dsp-transfer-process/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ dependencies {
api(project(":data-protocols:dsp:dsp-transfer-process:dsp-transfer-process-http-dispatcher"))
api(project(":data-protocols:dsp:dsp-transfer-process:dsp-transfer-process-transform"))
api(project(":data-protocols:dsp:dsp-transfer-process:lib:dsp-transfer-process-validation-lib"))
api(project(":data-protocols:dsp:dsp-transfer-process:lib:dsp-transfer-process-transform-lib"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_COMPLETION_MESSAGE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_REQUEST_MESSAGE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_START_MESSAGE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_TERMINATION_MESSAGE_IRI;
import static org.eclipse.edc.protocol.dsp.transferprocess.http.api.TransferProcessApiPaths.BASE_PATH;
import static org.eclipse.edc.protocol.dsp.transferprocess.http.api.TransferProcessApiPaths.TRANSFER_COMPLETION;
Expand Down Expand Up @@ -200,7 +200,7 @@ public Response transferProcessTermination(@PathParam("id") String id, JsonObjec
public Response transferProcessSuspension(@PathParam("id") String id, JsonObject jsonObject, @HeaderParam(AUTHORIZATION) String token) {
var request = PostDspRequest.Builder.newInstance(TransferSuspensionMessage.class, TransferProcess.class, TransferError.class)
.processId(id)
.expectedMessageType(DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE)
.expectedMessageType(DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE_IRI)
.message(jsonObject)
.token(token)
.serviceCall(protocolService::notifySuspended)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_COMPLETION_MESSAGE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_REQUEST_MESSAGE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_START_MESSAGE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_TERMINATION_MESSAGE_IRI;
import static org.eclipse.edc.protocol.dsp.transferprocess.http.api.TransferProcessApiPaths.BASE_PATH;
import static org.eclipse.edc.protocol.dsp.transferprocess.http.api.TransferProcessApiPaths.TRANSFER_COMPLETION;
Expand Down Expand Up @@ -171,7 +171,7 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
Arguments.of(
BASE_PATH + PROCESS_ID + TRANSFER_SUSPENSION,
TransferSuspensionMessage.class,
DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE),
DSPACE_TYPE_TRANSFER_SUSPENSION_MESSAGE_IRI),
Arguments.of(
BASE_PATH + PROCESS_ID + TRANSFER_TERMINATION,
TransferTerminationMessage.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies {
api(project(":data-protocols:dsp:dsp-spi"))
api(project(":data-protocols:dsp:dsp-http-spi"))
implementation(project(":core:common:lib:transform-lib"))
implementation(project(":data-protocols:dsp:dsp-transfer-process:lib:dsp-transfer-process-transform-lib"))

testImplementation(project(":core:common:lib:json-ld-lib"))
testImplementation(project(":core:common:junit"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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(":data-protocols:dsp:dsp-spi"))
api(project(":spi:control-plane:transfer-spi"))
implementation(project(":core:common:lib:transform-lib"))

testImplementation(project(":core:common:junit"))
testImplementation(project(":extensions:common:json-ld"))
testImplementation(project(":core:common:lib:json-ld-lib"))

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,39 @@
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.controlplane.transfer.spi.types.protocol.TransferCompletionMessage;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.jsonld.spi.transformer.AbstractNamespaceAwareJsonLdTransformer;
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.ID;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CONSUMER_PID_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_PROVIDER_PID_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_COMPLETION_MESSAGE_IRI;
import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_SCHEMA;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CONSUMER_PID_TERM;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_PROVIDER_PID_TERM;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_COMPLETION_MESSAGE_TERM;


public class JsonObjectFromTransferCompletionMessageTransformer extends AbstractJsonLdTransformer<TransferCompletionMessage, JsonObject> {
public class JsonObjectFromTransferCompletionMessageTransformer extends AbstractNamespaceAwareJsonLdTransformer<TransferCompletionMessage, JsonObject> {

private final JsonBuilderFactory jsonBuilderFactory;

public JsonObjectFromTransferCompletionMessageTransformer(JsonBuilderFactory jsonBuilderFactory) {
super(TransferCompletionMessage.class, JsonObject.class);
this(jsonBuilderFactory, DSPACE_SCHEMA);
}

public JsonObjectFromTransferCompletionMessageTransformer(JsonBuilderFactory jsonBuilderFactory, String namespace) {
super(TransferCompletionMessage.class, JsonObject.class, namespace);
this.jsonBuilderFactory = jsonBuilderFactory;
}

@Override
public @Nullable JsonObject transform(@NotNull TransferCompletionMessage transferCompletionMessage, @NotNull TransformerContext context) {
return jsonBuilderFactory.createObjectBuilder()
.add(ID, transferCompletionMessage.getId())
.add(TYPE, DSPACE_TYPE_TRANSFER_COMPLETION_MESSAGE_IRI)
.add(DSPACE_PROPERTY_CONSUMER_PID_IRI, transferCompletionMessage.getConsumerPid())
.add(DSPACE_PROPERTY_PROVIDER_PID_IRI, transferCompletionMessage.getProviderPid())
.add(TYPE, forNamespace(DSPACE_TYPE_TRANSFER_COMPLETION_MESSAGE_TERM))
.add(forNamespace(DSPACE_PROPERTY_CONSUMER_PID_TERM), transferCompletionMessage.getConsumerPid())
.add(forNamespace(DSPACE_PROPERTY_PROVIDER_PID_TERM), transferCompletionMessage.getProviderPid())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,39 @@
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.controlplane.transfer.spi.types.protocol.TransferError;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.jsonld.spi.transformer.AbstractNamespaceAwareJsonLdTransformer;
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.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CODE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_REASON_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_ERROR;
import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_SCHEMA;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CODE_TERM;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_REASON_TERM;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_ERROR_TERM;

/**
* Transforms a {@link TransferError} to a {@link JsonObject} in JSON-LD expanded form.
*/
public class JsonObjectFromTransferErrorTransformer extends AbstractJsonLdTransformer<TransferError, JsonObject> {
public class JsonObjectFromTransferErrorTransformer extends AbstractNamespaceAwareJsonLdTransformer<TransferError, JsonObject> {

private final JsonBuilderFactory jsonFactory;

public JsonObjectFromTransferErrorTransformer(JsonBuilderFactory jsonFactory) {
super(TransferError.class, JsonObject.class);
this(jsonFactory, DSPACE_SCHEMA);
}

public JsonObjectFromTransferErrorTransformer(JsonBuilderFactory jsonFactory, String namespace) {
super(TransferError.class, JsonObject.class, namespace);
this.jsonFactory = jsonFactory;
}

@Override
public @Nullable JsonObject transform(@NotNull TransferError error, @NotNull TransformerContext context) {
return jsonFactory.createObjectBuilder()
.add(TYPE, DSPACE_TYPE_TRANSFER_ERROR)
.add(DSPACE_PROPERTY_CODE_IRI, error.getCode())
.add(DSPACE_PROPERTY_REASON_IRI, jsonFactory.createArrayBuilder(error.getMessages()))
.add(TYPE, forNamespace(DSPACE_TYPE_TRANSFER_ERROR_TERM))
.add(forNamespace(DSPACE_PROPERTY_CODE_TERM), error.getCode())
.add(forNamespace(DSPACE_PROPERTY_REASON_TERM), jsonFactory.createArrayBuilder(error.getMessages()))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,45 @@
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.controlplane.transfer.spi.types.TransferProcess;
import org.eclipse.edc.connector.controlplane.transfer.spi.types.TransferProcessStates;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.jsonld.spi.transformer.AbstractNamespaceAwareJsonLdTransformer;
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.ID;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CONSUMER_PID_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_PROVIDER_PID_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_STATE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_PROCESS;
import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_SCHEMA;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CONSUMER_PID_TERM;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_PROVIDER_PID_TERM;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_STATE_TERM;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_PROCESS_TERM;

public class JsonObjectFromTransferProcessTransformer extends AbstractJsonLdTransformer<TransferProcess, JsonObject> {
public class JsonObjectFromTransferProcessTransformer extends AbstractNamespaceAwareJsonLdTransformer<TransferProcess, JsonObject> {

private final JsonBuilderFactory jsonBuilderFactory;

public JsonObjectFromTransferProcessTransformer(JsonBuilderFactory jsonBuilderFactory) {
super(TransferProcess.class, JsonObject.class);
this(jsonBuilderFactory, DSPACE_SCHEMA);
}

public JsonObjectFromTransferProcessTransformer(JsonBuilderFactory jsonBuilderFactory, String namespace) {
super(TransferProcess.class, JsonObject.class, namespace);
this.jsonBuilderFactory = jsonBuilderFactory;
}

@Override
public @Nullable JsonObject transform(@NotNull TransferProcess transferProcess, @NotNull TransformerContext context) {
var builder = jsonBuilderFactory.createObjectBuilder()
.add(ID, transferProcess.getId())
.add(TYPE, DSPACE_TYPE_TRANSFER_PROCESS)
.add(DSPACE_PROPERTY_STATE_IRI, TransferProcessStates.from(transferProcess.getState()).name());
.add(TYPE, forNamespace(DSPACE_TYPE_TRANSFER_PROCESS_TERM))
.add(forNamespace(DSPACE_PROPERTY_STATE_TERM), TransferProcessStates.from(transferProcess.getState()).name());

if (transferProcess.getType() == TransferProcess.Type.PROVIDER) {
builder.add(DSPACE_PROPERTY_PROVIDER_PID_IRI, transferProcess.getId());
addIfNotNull(transferProcess.getCorrelationId(), DSPACE_PROPERTY_CONSUMER_PID_IRI, builder);
builder.add(forNamespace(DSPACE_PROPERTY_PROVIDER_PID_TERM), transferProcess.getId());
addIfNotNull(transferProcess.getCorrelationId(), forNamespace(DSPACE_PROPERTY_CONSUMER_PID_TERM), builder);
} else {
builder.add(DSPACE_PROPERTY_CONSUMER_PID_IRI, transferProcess.getId());
addIfNotNull(transferProcess.getCorrelationId(), DSPACE_PROPERTY_PROVIDER_PID_IRI, builder);
builder.add(forNamespace(DSPACE_PROPERTY_CONSUMER_PID_TERM), transferProcess.getId());
addIfNotNull(transferProcess.getCorrelationId(), forNamespace(DSPACE_PROPERTY_PROVIDER_PID_TERM), builder);
}

return builder.build();
Expand Down
Loading

0 comments on commit c537cf2

Please sign in to comment.