Skip to content

Commit

Permalink
feat(dsp-negotiation): implement transform module
Browse files Browse the repository at this point in the history
  • Loading branch information
juliapampus committed Apr 12, 2023
1 parent a9005e6 commit 71e11db
Show file tree
Hide file tree
Showing 9 changed files with 483 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.JsonLdExtension;
import org.eclipse.edc.jsonld.transformer.JsonLdTransformerRegistry;
import org.eclipse.edc.protocol.dsp.negotiation.transform.from.JsonObjectFromContractAgreementRequestTransformer;
import org.eclipse.edc.protocol.dsp.negotiation.transform.from.JsonObjectFromContractNegotiationErrorTransformer;
import org.eclipse.edc.protocol.dsp.negotiation.transform.from.JsonObjectFromContractNegotiationTransformer;
import org.eclipse.edc.protocol.dsp.negotiation.transform.from.JsonObjectFromContractRequestTransformer;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
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 java.util.Map;

@Extension(value = JsonLdExtension.NAME)
@Provides({JsonLdTransformerRegistry.class})
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());

var mapper = typeManager.getMapper("json-ld");

registry.register(new JsonObjectFromContractAgreementRequestTransformer(builderFactory));
registry.register(new JsonObjectFromContractNegotiationTransformer(builderFactory));
registry.register(new JsonObjectFromContractRequestTransformer(builderFactory, mapper));
registry.register(new JsonObjectFromContractNegotiationErrorTransformer(builderFactory, mapper));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.ContractAgreement;
import org.eclipse.edc.connector.contract.spi.types.agreement.ContractAgreementRequest;
import org.eclipse.edc.jsonld.JsonLdKeywords;
import org.eclipse.edc.jsonld.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.transform.transformer.Namespaces.DSPACE_SCHEMA;

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

private final JsonBuilderFactory jsonFactory;

public JsonObjectFromContractAgreementRequestTransformer(JsonBuilderFactory jsonFactory) {
super(ContractAgreementRequest.class, JsonObject.class);
this.jsonFactory = jsonFactory;
}

@Override
public @Nullable JsonObject transform(@Nullable ContractAgreementRequest message, @NotNull TransformerContext context) {
if (message == null) {
return null;
}

var builder = jsonFactory.createObjectBuilder();
builder.add(JsonLdKeywords.ID, String.valueOf(UUID.randomUUID()));
builder.add(JsonLdKeywords.TYPE, DSPACE_SCHEMA + "ContractAgreementMessage");

builder.add(DSPACE_SCHEMA + "processId", message.getCorrelationId());
builder.add(DSPACE_SCHEMA + "agreement", transformContractAgreement(message.getContractAgreement(), context));

return builder.build();
}

private @Nullable JsonObject transformContractAgreement(ContractAgreement agreement, TransformerContext context) {
return context.transform(agreement.getPolicy(), JsonObject.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.jsonld.JsonLdKeywords;
import org.eclipse.edc.jsonld.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.protocol.dsp.negotiation.spi.types.NegotiationError;
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.transform.transformer.Namespaces.DSPACE_SCHEMA;

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

private final JsonBuilderFactory jsonFactory;
private final ObjectMapper mapper;

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

@Override
public @Nullable JsonObject transform(@Nullable NegotiationError error, @NotNull TransformerContext context) {
if (error == null) {
return null;
}

var builder = jsonFactory.createObjectBuilder();
builder.add(JsonLdKeywords.ID, String.valueOf(UUID.randomUUID()));
builder.add(JsonLdKeywords.TYPE, DSPACE_SCHEMA + "ContractNegotiationError");

var reasons = error.getReasons().stream()
.collect(jsonFactory::createArrayBuilder, JsonArrayBuilder::add, JsonArrayBuilder::add)
.build();

builder.add(DSPACE_SCHEMA + "processId", error.getProcessId());
builder.add(DSPACE_SCHEMA + "code", error.getCode());
builder.add(DSPACE_SCHEMA + "reason", reasons);

return builder.build();
}

}
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.ContractNegotiation;
import org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationStates;
import org.eclipse.edc.jsonld.JsonLdKeywords;
import org.eclipse.edc.jsonld.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static org.eclipse.edc.protocol.dsp.transform.transformer.Namespaces.DSPACE_SCHEMA;

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

private final JsonBuilderFactory jsonFactory;

public JsonObjectFromContractNegotiationTransformer(JsonBuilderFactory jsonFactory) {
super(ContractNegotiation.class, JsonObject.class);
this.jsonFactory = jsonFactory;
}

@Override
public @Nullable JsonObject transform(@Nullable ContractNegotiation negotiation, @NotNull TransformerContext context) {
if (negotiation == null) {
return null;
}

var builder = jsonFactory.createObjectBuilder();
builder.add(JsonLdKeywords.ID, negotiation.getId());
builder.add(JsonLdKeywords.TYPE, DSPACE_SCHEMA + "ContractNegotiation");

builder.add(DSPACE_SCHEMA + "correlationId", negotiation.getCorrelationId());
builder.add(DSPACE_SCHEMA + "state", ContractNegotiationStates.from(negotiation.getState()).name());
//builder.add(DSPACE_PREFIX + "checksum", negotiation.getChecksum()); TODO

return builder.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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 com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.contract.spi.types.negotiation.ContractOfferRequest;
import org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRejection;
import org.eclipse.edc.connector.contract.spi.types.offer.ContractOffer;
import org.eclipse.edc.jsonld.JsonLdKeywords;
import org.eclipse.edc.jsonld.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.transform.transformer.Namespaces.DSPACE_SCHEMA;

/**
* Creates a {@link JsonObject} from a {@link ContractOfferRequest}.
*/
public class JsonObjectFromContractRejectionTransformer extends AbstractJsonLdTransformer<ContractRejection, JsonObject> {

private final JsonBuilderFactory jsonFactory;
private final ObjectMapper mapper;

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

@Override
public @Nullable JsonObject transform(@Nullable ContractRejection message, @NotNull TransformerContext context) {
if (message == null) {
return null;
}

var builder = jsonFactory.createObjectBuilder();
builder.add(JsonLdKeywords.ID, String.valueOf(UUID.randomUUID()));
builder.add(JsonLdKeywords.TYPE, DSPACE_SCHEMA + "ContractRequestMessage");

//builder.add(DSPACE_SCHEMA + "dataSet", message.getContractOffer().getAsset().getId());
builder.add(DSPACE_SCHEMA + "processId", message.getCorrelationId());
//builder.add(DSPACE_SCHEMA + "offer", transformContractOffer(message.getContractOffer(), context));

return builder.build();
}

private @Nullable JsonObject transformContractOffer(ContractOffer offer, TransformerContext context) {
return context.transform(offer.getPolicy(), JsonObject.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.contract.spi.types.negotiation.ContractOfferRequest;
import org.eclipse.edc.connector.contract.spi.types.offer.ContractOffer;
import org.eclipse.edc.jsonld.JsonLdKeywords;
import org.eclipse.edc.jsonld.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.transform.transformer.Namespaces.DSPACE_SCHEMA;

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

private final JsonBuilderFactory jsonFactory;
private final ObjectMapper mapper;

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

@Override
public @Nullable JsonObject transform(@Nullable ContractOfferRequest message, @NotNull TransformerContext context) {
if (message == null) {
return null;
}

var builder = jsonFactory.createObjectBuilder();
builder.add(JsonLdKeywords.ID, String.valueOf(UUID.randomUUID()));
builder.add(JsonLdKeywords.TYPE, DSPACE_SCHEMA + "ContractRequestMessage");

builder.add(DSPACE_SCHEMA + "dataSet", message.getContractOffer().getAsset().getId());
builder.add(DSPACE_SCHEMA + "processId", message.getCorrelationId());
builder.add(DSPACE_SCHEMA + "offer", transformContractOffer(message.getContractOffer(), context));

return builder.build();
}

private @Nullable JsonObject transformContractOffer(ContractOffer offer, TransformerContext context) {
return context.transform(offer.getPolicy(), JsonObject.class);
}

}
Loading

0 comments on commit 71e11db

Please sign in to comment.