Skip to content

Commit

Permalink
feat: dataspace protocol negotiation transformer (eclipse-edc#2771)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Jan Peter Meyer <[email protected]>
Co-authored-by: Ronja Quensel <[email protected]>
  • Loading branch information
3 people authored and majadlymhmd committed May 10, 2023
1 parent f732930 commit fca7581
Show file tree
Hide file tree
Showing 30 changed files with 1,882 additions and 47 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering
*
* 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:
* Fraunhofer Institute for Software and Systems Engineering - initial API and implementation
*
*/

package org.eclipse.edc.protocol.dsp.negotiation.transform;

import jakarta.json.Json;
import org.eclipse.edc.jsonld.spi.transformer.JsonLdTransformerRegistry;
import org.eclipse.edc.protocol.dsp.negotiation.transform.from.JsonObjectFromContractAgreementMessageTransformer;
import org.eclipse.edc.protocol.dsp.negotiation.transform.from.JsonObjectFromContractAgreementVerificationMessageTransformer;
import org.eclipse.edc.protocol.dsp.negotiation.transform.from.JsonObjectFromContractNegotiationEventMessageTransformer;
import org.eclipse.edc.protocol.dsp.negotiation.transform.from.JsonObjectFromContractNegotiationTerminationMessageTransformer;
import org.eclipse.edc.protocol.dsp.negotiation.transform.from.JsonObjectFromContractNegotiationTransformer;
import org.eclipse.edc.protocol.dsp.negotiation.transform.from.JsonObjectFromContractRequestTransformer;
import org.eclipse.edc.protocol.dsp.negotiation.transform.to.JsonObjectToContractAgreementMessageTransformer;
import org.eclipse.edc.protocol.dsp.negotiation.transform.to.JsonObjectToContractAgreementVerificationMessageTransformer;
import org.eclipse.edc.protocol.dsp.negotiation.transform.to.JsonObjectToContractNegotiationEventMessageTransformer;
import org.eclipse.edc.protocol.dsp.negotiation.transform.to.JsonObjectToContractNegotiationTerminationMessageTransformer;
import org.eclipse.edc.protocol.dsp.negotiation.transform.to.JsonObjectToContractRequestMessageTransformer;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.types.TypeManager;

import java.util.Map;

/**
* Provides the transformers for negotiation message types via the {@link JsonLdTransformerRegistry}.
*/
@Extension(value = DspNegotiationTransformExtension.NAME)
public class DspNegotiationTransformExtension implements ServiceExtension {

public static final String NAME = "Dataspace Protocol Negotiation Transform Extension";

@Inject
private TypeManager typeManager;

@Inject
private JsonLdTransformerRegistry registry;

@Override
public String name() {
return NAME;
}

@Override
public void initialize(ServiceExtensionContext context) {
var builderFactory = Json.createBuilderFactory(Map.of());

registry.register(new JsonObjectFromContractAgreementMessageTransformer(builderFactory));
registry.register(new JsonObjectFromContractAgreementVerificationMessageTransformer(builderFactory));
registry.register(new JsonObjectFromContractNegotiationEventMessageTransformer(builderFactory));
registry.register(new JsonObjectFromContractNegotiationTerminationMessageTransformer(builderFactory));
registry.register(new JsonObjectFromContractNegotiationTransformer(builderFactory));
registry.register(new JsonObjectFromContractRequestTransformer(builderFactory));

registry.register(new JsonObjectToContractAgreementMessageTransformer());
registry.register(new JsonObjectToContractAgreementVerificationMessageTransformer());
registry.register(new JsonObjectToContractNegotiationEventMessageTransformer());
registry.register(new JsonObjectToContractRequestMessageTransformer());
registry.register(new JsonObjectToContractNegotiationTerminationMessageTransformer());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering
*
* 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:
* Fraunhofer Institute for Software and Systems Engineering - initial API and implementation
*
*/

package org.eclipse.edc.protocol.dsp.negotiation.transform.from;

import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.contract.spi.types.agreement.ContractAgreementMessage;
import org.eclipse.edc.jsonld.spi.JsonLdKeywords;
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.UUID;

import static org.eclipse.edc.protocol.dsp.negotiation.transform.DspNegotiationPropertyAndTypeNames.DSPACE_NEGOTIATION_AGREEMENT_MESSAGE;
import static org.eclipse.edc.protocol.dsp.negotiation.transform.DspNegotiationPropertyAndTypeNames.DSPACE_NEGOTIATION_PROPERTY_AGREEMENT;
import static org.eclipse.edc.protocol.dsp.negotiation.transform.DspNegotiationPropertyAndTypeNames.DSPACE_NEGOTIATION_PROPERTY_PROCESS_ID;


/**
* Creates a dspace:ContractAgreementMessage as {@link JsonObject} from {@link ContractAgreementMessage}.
*/
public class JsonObjectFromContractAgreementMessageTransformer extends AbstractJsonLdTransformer<ContractAgreementMessage, JsonObject> {

private final JsonBuilderFactory jsonFactory;

public JsonObjectFromContractAgreementMessageTransformer(JsonBuilderFactory jsonFactory) {
super(ContractAgreementMessage.class, JsonObject.class);
this.jsonFactory = jsonFactory;
}

@Override
public @Nullable JsonObject transform(@NotNull ContractAgreementMessage object, @NotNull TransformerContext context) {
var builder = jsonFactory.createObjectBuilder();
builder.add(JsonLdKeywords.ID, String.valueOf(UUID.randomUUID()));
builder.add(JsonLdKeywords.TYPE, DSPACE_NEGOTIATION_AGREEMENT_MESSAGE);

builder.add(DSPACE_NEGOTIATION_PROPERTY_PROCESS_ID, object.getProcessId());

var policy = context.transform(object.getContractAgreement().getPolicy(), JsonObject.class);
if (policy == null) {
context.reportProblem("Cannot transform from ContractAgreementMessage with null policy");
return null;
}

builder.add(DSPACE_NEGOTIATION_PROPERTY_AGREEMENT, policy);

return builder.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering
*
* 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:
* Fraunhofer Institute for Software and Systems Engineering - initial API and implementation
*
*/

package org.eclipse.edc.protocol.dsp.negotiation.transform.from;

import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.contract.spi.types.agreement.ContractAgreementVerificationMessage;
import org.eclipse.edc.jsonld.spi.JsonLdKeywords;
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.UUID;

import static org.eclipse.edc.protocol.dsp.negotiation.transform.DspNegotiationPropertyAndTypeNames.DSPACE_NEGOTIATION_AGREEMENT_VERIFICATION_MESSAGE;
import static org.eclipse.edc.protocol.dsp.negotiation.transform.DspNegotiationPropertyAndTypeNames.DSPACE_NEGOTIATION_PROPERTY_PROCESS_ID;

/**
* Creates a {@link JsonObject} from a {@link ContractAgreementVerificationMessage}.
*/
public class JsonObjectFromContractAgreementVerificationMessageTransformer extends AbstractJsonLdTransformer<ContractAgreementVerificationMessage, JsonObject> {

private final JsonBuilderFactory jsonFactory;

public JsonObjectFromContractAgreementVerificationMessageTransformer(JsonBuilderFactory jsonFactory) {
super(ContractAgreementVerificationMessage.class, JsonObject.class);
this.jsonFactory = jsonFactory;
}

@Override
public @Nullable JsonObject transform(@NotNull ContractAgreementVerificationMessage object, @NotNull TransformerContext context) {
var builder = jsonFactory.createObjectBuilder();
builder.add(JsonLdKeywords.ID, String.valueOf(UUID.randomUUID()));
builder.add(JsonLdKeywords.TYPE, DSPACE_NEGOTIATION_AGREEMENT_VERIFICATION_MESSAGE);

builder.add(DSPACE_NEGOTIATION_PROPERTY_PROCESS_ID, object.getProcessId());
// TODO add mapping of cred:credentialSubject for signature processes

return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering
*
* 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:
* Fraunhofer Institute for Software and Systems Engineering - initial API and implementation
*
*/

package org.eclipse.edc.protocol.dsp.negotiation.transform.from;

import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.contract.spi.types.agreement.ContractNegotiationEventMessage;
import org.eclipse.edc.jsonld.spi.JsonLdKeywords;
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.UUID;

import static org.eclipse.edc.protocol.dsp.negotiation.transform.DspNegotiationPropertyAndTypeNames.DSPACE_NEGOTIATION_EVENT_MESSAGE;
import static org.eclipse.edc.protocol.dsp.negotiation.transform.DspNegotiationPropertyAndTypeNames.DSPACE_NEGOTIATION_PROPERTY_CHECKSUM;
import static org.eclipse.edc.protocol.dsp.negotiation.transform.DspNegotiationPropertyAndTypeNames.DSPACE_NEGOTIATION_PROPERTY_EVENT_TYPE;
import static org.eclipse.edc.protocol.dsp.negotiation.transform.DspNegotiationPropertyAndTypeNames.DSPACE_NEGOTIATION_PROPERTY_EVENT_TYPE_ACCEPTED;
import static org.eclipse.edc.protocol.dsp.negotiation.transform.DspNegotiationPropertyAndTypeNames.DSPACE_NEGOTIATION_PROPERTY_EVENT_TYPE_FINALIZED;
import static org.eclipse.edc.protocol.dsp.negotiation.transform.DspNegotiationPropertyAndTypeNames.DSPACE_NEGOTIATION_PROPERTY_PROCESS_ID;


/**
* Creates a {@link JsonObject} from a {@link ContractNegotiationEventMessage}.
*/
public class JsonObjectFromContractNegotiationEventMessageTransformer extends AbstractJsonLdTransformer<ContractNegotiationEventMessage, JsonObject> {

private final JsonBuilderFactory jsonFactory;

public JsonObjectFromContractNegotiationEventMessageTransformer(JsonBuilderFactory jsonFactory) {
super(ContractNegotiationEventMessage.class, JsonObject.class);
this.jsonFactory = jsonFactory;
}

@Override
public @Nullable JsonObject transform(@NotNull ContractNegotiationEventMessage object, @NotNull TransformerContext context) {
var builder = jsonFactory.createObjectBuilder();
builder.add(JsonLdKeywords.ID, String.valueOf(UUID.randomUUID()));
builder.add(JsonLdKeywords.TYPE, DSPACE_NEGOTIATION_EVENT_MESSAGE);

builder.add(DSPACE_NEGOTIATION_PROPERTY_PROCESS_ID, object.getProcessId());
builder.add(DSPACE_NEGOTIATION_PROPERTY_CHECKSUM, object.getChecksum());

var eventType = eventType(object, context);
if (eventType == null) {
return null;
}

builder.add(DSPACE_NEGOTIATION_PROPERTY_EVENT_TYPE, eventType);


return builder.build();
}

private String eventType(ContractNegotiationEventMessage message, TransformerContext context) {
switch (message.getType()) {
case ACCEPTED:
return DSPACE_NEGOTIATION_PROPERTY_EVENT_TYPE_ACCEPTED;
case FINALIZED:
return DSPACE_NEGOTIATION_PROPERTY_EVENT_TYPE_FINALIZED;
default:
context.reportProblem(String.format("Could not map eventType %s to %s or %s in ContractNegotiationEventMessage", message.getType(),
DSPACE_NEGOTIATION_PROPERTY_EVENT_TYPE_ACCEPTED, DSPACE_NEGOTIATION_PROPERTY_EVENT_TYPE_FINALIZED));
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering
*
* 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:
* Fraunhofer Institute for Software and Systems Engineering - initial API and implementation
*
*/

package org.eclipse.edc.protocol.dsp.negotiation.transform.from;

import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationTerminationMessage;
import org.eclipse.edc.jsonld.spi.JsonLdKeywords;
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.UUID;

import static org.eclipse.edc.protocol.dsp.negotiation.transform.DspNegotiationPropertyAndTypeNames.DSPACE_NEGOTIATION_PROPERTY_CODE;
import static org.eclipse.edc.protocol.dsp.negotiation.transform.DspNegotiationPropertyAndTypeNames.DSPACE_NEGOTIATION_PROPERTY_PROCESS_ID;
import static org.eclipse.edc.protocol.dsp.negotiation.transform.DspNegotiationPropertyAndTypeNames.DSPACE_NEGOTIATION_PROPERTY_REASON;
import static org.eclipse.edc.protocol.dsp.negotiation.transform.DspNegotiationPropertyAndTypeNames.DSPACE_NEGOTIATION_TERMINATION_MESSAGE;

/**
* Creates a {@link JsonObject} from a {@link ContractNegotiationTerminationMessage}.
*/
public class JsonObjectFromContractNegotiationTerminationMessageTransformer extends AbstractJsonLdTransformer<ContractNegotiationTerminationMessage, JsonObject> {

private final JsonBuilderFactory jsonFactory;

public JsonObjectFromContractNegotiationTerminationMessageTransformer(JsonBuilderFactory jsonFactory) {
super(ContractNegotiationTerminationMessage.class, JsonObject.class);
this.jsonFactory = jsonFactory;
}

@Override
public @Nullable JsonObject transform(@NotNull ContractNegotiationTerminationMessage object, @NotNull TransformerContext context) {
var builder = jsonFactory.createObjectBuilder();
builder.add(JsonLdKeywords.ID, String.valueOf(UUID.randomUUID()));
builder.add(JsonLdKeywords.TYPE, DSPACE_NEGOTIATION_TERMINATION_MESSAGE);

builder.add(DSPACE_NEGOTIATION_PROPERTY_PROCESS_ID, object.getProcessId());
builder.add(DSPACE_NEGOTIATION_PROPERTY_REASON, object.getRejectionReason());
builder.add(DSPACE_NEGOTIATION_PROPERTY_CODE, object.getCode());

return builder.build();
}

}
Loading

0 comments on commit fca7581

Please sign in to comment.