Skip to content

Commit

Permalink
refactor: extract dsp negotiation transform lib
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Oct 15, 2024
1 parent 375b167 commit 5a0f430
Show file tree
Hide file tree
Showing 61 changed files with 647 additions and 525 deletions.
1 change: 1 addition & 0 deletions data-protocols/dsp/dsp-negotiation/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-negotiation:dsp-negotiation-http-dispatcher"))
api(project(":data-protocols:dsp:dsp-negotiation:dsp-negotiation-transform"))
api(project(":data-protocols:dsp:dsp-negotiation:lib:dsp-negotiation-validation-lib"))
api(project(":data-protocols:dsp:dsp-negotiation:lib:dsp-negotiation-transform-lib"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ plugins {
}

dependencies {
api(project(":data-protocols:dsp:dsp-spi"))
api(project(":data-protocols:dsp:dsp-http-spi"))
api(project(":extensions:common:json-ld"))
api(project(":spi:common:json-ld-spi"))
api(project(":spi:common:core-spi"))
api(project(":spi:common:transform-spi"))
api(project(":spi:control-plane:contract-spi"))

implementation(project(":data-protocols:dsp:dsp-negotiation:lib:dsp-negotiation-transform-lib"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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:contract-spi"))
implementation(project(":core:common:lib:transform-lib"))

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,37 @@
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.controlplane.contract.spi.types.agreement.ContractAgreementMessage;
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 java.time.Instant.ofEpochSecond;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_SCHEMA;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ASSIGNEE_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ASSIGNER_ATTRIBUTE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_PROPERTY_AGREEMENT;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_PROPERTY_TIMESTAMP;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_TYPE_CONTRACT_AGREEMENT_MESSAGE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CONSUMER_PID;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_PROVIDER_PID;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_PROPERTY_AGREEMENT_TERM;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_PROPERTY_TIMESTAMP_TERM;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_TYPE_CONTRACT_AGREEMENT_MESSAGE_TERM;
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;


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

private final JsonBuilderFactory jsonFactory;

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

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

Expand All @@ -67,15 +72,15 @@ public JsonObjectFromContractAgreementMessageTransformer(JsonBuilderFactory json
.add(ID, agreement.getId())
.add(ODRL_ASSIGNEE_ATTRIBUTE, agreement.getConsumerId())
.add(ODRL_ASSIGNER_ATTRIBUTE, agreement.getProviderId())
.add(DSPACE_PROPERTY_TIMESTAMP, signing)
.add(forNamespace(DSPACE_PROPERTY_TIMESTAMP_TERM), signing)
.build();

return jsonFactory.createObjectBuilder()
.add(ID, agreementMessage.getId())
.add(TYPE, DSPACE_TYPE_CONTRACT_AGREEMENT_MESSAGE_IRI)
.add(DSPACE_PROPERTY_PROVIDER_PID, agreementMessage.getProviderPid())
.add(DSPACE_PROPERTY_CONSUMER_PID, agreementMessage.getConsumerPid())
.add(DSPACE_PROPERTY_AGREEMENT, copiedPolicy)
.add(TYPE, forNamespace(DSPACE_TYPE_CONTRACT_AGREEMENT_MESSAGE_TERM))
.add(forNamespace(DSPACE_PROPERTY_PROVIDER_PID_TERM), agreementMessage.getProviderPid())
.add(forNamespace(DSPACE_PROPERTY_CONSUMER_PID_TERM), agreementMessage.getConsumerPid())
.add(forNamespace(DSPACE_PROPERTY_AGREEMENT_TERM), copiedPolicy)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,41 @@
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.controlplane.contract.spi.types.agreement.ContractAgreementVerificationMessage;
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.DspNegotiationPropertyAndTypeNames.DSPACE_TYPE_CONTRACT_AGREEMENT_VERIFICATION_MESSAGE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CONSUMER_PID;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_PROVIDER_PID;
import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_SCHEMA;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_TYPE_CONTRACT_AGREEMENT_VERIFICATION_MESSAGE_TERM;
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;

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

private final JsonBuilderFactory jsonFactory;

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

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

@Override
public @Nullable JsonObject transform(@NotNull ContractAgreementVerificationMessage verificationMessage, @NotNull TransformerContext context) {
return jsonFactory.createObjectBuilder()
.add(ID, verificationMessage.getId())
.add(TYPE, DSPACE_TYPE_CONTRACT_AGREEMENT_VERIFICATION_MESSAGE_IRI)
.add(DSPACE_PROPERTY_CONSUMER_PID, verificationMessage.getConsumerPid())
.add(DSPACE_PROPERTY_PROVIDER_PID, verificationMessage.getProviderPid())
.add(TYPE, forNamespace(DSPACE_TYPE_CONTRACT_AGREEMENT_VERIFICATION_MESSAGE_TERM))
.add(forNamespace(DSPACE_PROPERTY_CONSUMER_PID_TERM), verificationMessage.getConsumerPid())
.add(forNamespace(DSPACE_PROPERTY_PROVIDER_PID_TERM), verificationMessage.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.contract.spi.types.negotiation.ContractNegotiationError;
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.DspNegotiationPropertyAndTypeNames.DSPACE_TYPE_CONTRACT_NEGOTIATION_ERROR;
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.jsonld.spi.Namespaces.DSPACE_SCHEMA;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_TYPE_CONTRACT_NEGOTIATION_ERROR_TERM;
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;

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

private final JsonBuilderFactory jsonFactory;

public JsonObjectFromContractNegotiationErrorTransformer(JsonBuilderFactory jsonFactory) {
super(ContractNegotiationError.class, JsonObject.class);
this(jsonFactory, DSPACE_SCHEMA);
}

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

@Override
public @Nullable JsonObject transform(@NotNull ContractNegotiationError error, @NotNull TransformerContext context) {
return jsonFactory.createObjectBuilder()
.add(TYPE, DSPACE_TYPE_CONTRACT_NEGOTIATION_ERROR)
.add(DSPACE_PROPERTY_CODE_IRI, error.getCode())
.add(DSPACE_PROPERTY_REASON_IRI, jsonFactory.createArrayBuilder(error.getMessages()))
.add(TYPE, forNamespace(DSPACE_TYPE_CONTRACT_NEGOTIATION_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 @@ -17,43 +17,48 @@
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.controlplane.contract.spi.types.agreement.ContractNegotiationEventMessage;
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.DspNegotiationPropertyAndTypeNames.DSPACE_PROPERTY_EVENT_TYPE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_TYPE_CONTRACT_NEGOTIATION_EVENT_MESSAGE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_VALUE_NEGOTIATION_EVENT_TYPE_ACCEPTED;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_VALUE_NEGOTIATION_EVENT_TYPE_FINALIZED;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CONSUMER_PID;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_PROVIDER_PID;
import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_SCHEMA;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_PROPERTY_EVENT_TYPE_TERM;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_TYPE_CONTRACT_NEGOTIATION_EVENT_MESSAGE_TERM;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_VALUE_NEGOTIATION_EVENT_TYPE_ACCEPTED_TERM;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_VALUE_NEGOTIATION_EVENT_TYPE_FINALIZED_TERM;
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;


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

private final JsonBuilderFactory jsonFactory;

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

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

@Override
public @Nullable JsonObject transform(@NotNull ContractNegotiationEventMessage eventMessage, @NotNull TransformerContext context) {
return jsonFactory.createObjectBuilder()
.add(ID, eventMessage.getId())
.add(TYPE, DSPACE_TYPE_CONTRACT_NEGOTIATION_EVENT_MESSAGE_IRI)
.add(DSPACE_PROPERTY_CONSUMER_PID, eventMessage.getConsumerPid())
.add(DSPACE_PROPERTY_PROVIDER_PID, eventMessage.getProviderPid())
.add(DSPACE_PROPERTY_EVENT_TYPE, switch (eventMessage.getType()) {
case ACCEPTED -> DSPACE_VALUE_NEGOTIATION_EVENT_TYPE_ACCEPTED;
case FINALIZED -> DSPACE_VALUE_NEGOTIATION_EVENT_TYPE_FINALIZED;
.add(TYPE, forNamespace(DSPACE_TYPE_CONTRACT_NEGOTIATION_EVENT_MESSAGE_TERM))
.add(forNamespace(DSPACE_PROPERTY_CONSUMER_PID_TERM), eventMessage.getConsumerPid())
.add(forNamespace(DSPACE_PROPERTY_PROVIDER_PID_TERM), eventMessage.getProviderPid())
.add(forNamespace(DSPACE_PROPERTY_EVENT_TYPE_TERM), switch (eventMessage.getType()) {
case ACCEPTED -> forNamespace(DSPACE_VALUE_NEGOTIATION_EVENT_TYPE_ACCEPTED_TERM);
case FINALIZED -> forNamespace(DSPACE_VALUE_NEGOTIATION_EVENT_TYPE_FINALIZED_TERM);
})
.build();
}
Expand Down
Loading

0 comments on commit 5a0f430

Please sign in to comment.