Skip to content

Commit

Permalink
feat(dsp): replace consumerId and providerId with odrl assignee and a…
Browse files Browse the repository at this point in the history
…ssigner
  • Loading branch information
ndr-brt committed Feb 28, 2024
1 parent a97fa44 commit 486b433
Show file tree
Hide file tree
Showing 19 changed files with 242 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ACTION_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ACTION_TYPE_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_AND_CONSTRAINT_ATTRIBUTE;
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.jsonld.spi.PropertyAndTypeNames.ODRL_CONSEQUENCE_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_CONSTRAINT_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_DUTY_ATTRIBUTE;
Expand Down Expand Up @@ -157,6 +159,9 @@ public JsonObject visitPolicy(Policy policy) {
.add(ODRL_PROHIBITION_ATTRIBUTE, prohibitionsBuilder)
.add(ODRL_OBLIGATION_ATTRIBUTE, obligationsBuilder);

Optional.ofNullable(policy.getAssignee()).ifPresent(it -> builder.add(ODRL_ASSIGNEE_ATTRIBUTE, it));
Optional.ofNullable(policy.getAssigner()).ifPresent(it -> builder.add(ODRL_ASSIGNER_ATTRIBUTE, it));

Optional.ofNullable(policy.getTarget())
.ifPresent(target -> builder.add(
ODRL_TARGET_ATTRIBUTE,
Expand Down Expand Up @@ -242,6 +247,7 @@ private String getTypeAsString(PolicyType type) {
case CONTRACT -> ODRL_POLICY_TYPE_AGREEMENT;
};
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.jetbrains.annotations.Nullable;

import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
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.jsonld.spi.PropertyAndTypeNames.ODRL_OBLIGATION_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PERMISSION_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_AGREEMENT;
Expand Down Expand Up @@ -76,6 +78,8 @@ public JsonObjectToPolicyTransformer() {
case ODRL_PROHIBITION_ATTRIBUTE -> v -> builder.prohibitions(transformArray(v, Prohibition.class, context));
case ODRL_OBLIGATION_ATTRIBUTE -> v -> builder.duties(transformArray(v, Duty.class, context));
case ODRL_TARGET_ATTRIBUTE -> v -> builder.target(transformString(v, context));
case ODRL_ASSIGNER_ATTRIBUTE -> v -> builder.assigner(transformString(v, context));
case ODRL_ASSIGNEE_ATTRIBUTE -> v -> builder.assignee(transformString(v, context));
default -> v -> builder.extensibleProperty(key, transformGenericProperty(v, context));
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.InstanceOfAssertFactories.LIST;
import static org.assertj.core.api.InstanceOfAssertFactories.type;
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.JsonLdKeywords.VALUE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ACTION_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ACTION_TYPE_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_AND_CONSTRAINT_ATTRIBUTE;
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.jsonld.spi.PropertyAndTypeNames.ODRL_CONSEQUENCE_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_CONSTRAINT_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_DUTY_ATTRIBUTE;
Expand Down Expand Up @@ -97,6 +100,8 @@ void transform_policyWithAllRuleTypes_returnJsonObject() {
var duty = Duty.Builder.newInstance().action(action).build();
var policy = Policy.Builder.newInstance()
.target("target")
.assignee("assignee")
.assigner("assigner")
.permission(permission)
.prohibition(prohibition)
.duty(duty)
Expand All @@ -111,10 +116,13 @@ void transform_policyWithAllRuleTypes_returnJsonObject() {
.isInstanceOf(JsonArray.class)
.extracting(JsonValue::asJsonArray)
.matches(it -> !it.isEmpty())
.asList().first()
.asInstanceOf(LIST).first()
.asInstanceOf(type(JsonObject.class))
.matches(it -> it.getString(ID).equals("target"));


assertThat(result.getString(ODRL_ASSIGNEE_ATTRIBUTE)).isEqualTo("assignee");
assertThat(result.getString(ODRL_ASSIGNER_ATTRIBUTE)).isEqualTo("assigner");

assertThat(result.get(ODRL_PERMISSION_ATTRIBUTE))
.isNotNull()
.isInstanceOf(JsonArray.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.Map;
import java.util.stream.Stream;
Expand All @@ -40,6 +39,8 @@
import static org.eclipse.edc.core.transform.transformer.TestInput.getExpanded;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.CONTEXT;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
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.jsonld.spi.PropertyAndTypeNames.ODRL_OBLIGATION_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PERMISSION_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_AGREEMENT;
Expand Down Expand Up @@ -82,14 +83,29 @@ void setUp() {
}


@ParameterizedTest
@MethodSource("jsonSource")
void transform_withAllRuleTypesAsObjects_returnPolicy(JsonObject policy) {
@Test
void transform_withAllRuleTypesAsObjects_returnPolicy() {
var jsonFactory = Json.createBuilderFactory(Map.of());
var permissionJson = jsonFactory.createObjectBuilder().add(TYPE, "permission").build();
var prohibitionJson = jsonFactory.createObjectBuilder().add(TYPE, "prohibition").build();
var dutyJson = jsonFactory.createObjectBuilder().add(TYPE, "duty").build();

var policy = jsonFactory.createObjectBuilder()
.add(TYPE, ODRL_POLICY_TYPE_SET)
.add(ODRL_TARGET_ATTRIBUTE, TARGET)
.add(ODRL_ASSIGNER_ATTRIBUTE, "assigner")
.add(ODRL_ASSIGNEE_ATTRIBUTE, "assignee")
.add(ODRL_PERMISSION_ATTRIBUTE, permissionJson)
.add(ODRL_PROHIBITION_ATTRIBUTE, prohibitionJson)
.add(ODRL_OBLIGATION_ATTRIBUTE, dutyJson)
.build();

var result = transformer.transform(getExpanded(policy), context);

assertThat(result).isNotNull();
assertThat(result.getTarget()).isEqualTo(TARGET);
assertThat(result.getAssigner()).isEqualTo("assigner");
assertThat(result.getAssignee()).isEqualTo("assignee");
assertThat(result.getPermissions()).hasSize(1);
assertThat(result.getPermissions().get(0)).isEqualTo(permission);
assertThat(result.getProhibitions()).hasSize(1);
Expand All @@ -101,7 +117,6 @@ void transform_withAllRuleTypesAsObjects_returnPolicy(JsonObject policy) {
verify(context, times(1)).transform(isA(JsonObject.class), eq(Permission.class));
verify(context, times(1)).transform(isA(JsonObject.class), eq(Prohibition.class));
verify(context, times(1)).transform(isA(JsonObject.class), eq(Duty.class));

}

@Test
Expand Down Expand Up @@ -157,7 +172,7 @@ void transform_invalidType_reportProblem() {
private static class PolicyTypeArguments implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) throws Exception {
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) {
return Stream.of(
arguments(ODRL_POLICY_TYPE_SET, SET),
arguments(ODRL_POLICY_TYPE_OFFER, OFFER),
Expand All @@ -166,30 +181,4 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext extensionCo
}
}


static Stream<JsonObject> jsonSource() {
var jsonFactory = Json.createBuilderFactory(Map.of());
var permissionJson = jsonFactory.createObjectBuilder().add(TYPE, "permission").build();
var prohibitionJson = jsonFactory.createObjectBuilder().add(TYPE, "prohibition").build();
var dutyJson = jsonFactory.createObjectBuilder().add(TYPE, "duty").build();

return Stream.of(
jsonFactory.createObjectBuilder()
.add(TYPE, ODRL_POLICY_TYPE_SET)
.add(ODRL_TARGET_ATTRIBUTE, TARGET)
.add(ODRL_PERMISSION_ATTRIBUTE, permissionJson)
.add(ODRL_PROHIBITION_ATTRIBUTE, prohibitionJson)
.add(ODRL_OBLIGATION_ATTRIBUTE, dutyJson)
.build(),
jsonFactory.createObjectBuilder()
.add(TYPE, ODRL_POLICY_TYPE_SET)
.add(ODRL_TARGET_ATTRIBUTE, jsonFactory.createArrayBuilder().add(TARGET))
.add(ODRL_PERMISSION_ATTRIBUTE, jsonFactory.createArrayBuilder().add(permissionJson))
.add(ODRL_PROHIBITION_ATTRIBUTE, jsonFactory.createArrayBuilder().add(prohibitionJson))
.add(ODRL_OBLIGATION_ATTRIBUTE, jsonFactory.createArrayBuilder().add(dutyJson))
.build()

);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import org.eclipse.edc.policy.model.Policy;
import org.eclipse.edc.spi.types.TypeManager;

import java.util.List;
import java.util.function.BiPredicate;

public class PolicyEquality implements BiPredicate<Policy, Policy> {

private static final List<String> EXCLUDED_PROPERTIES = List.of("@type", "assignee", "target");
private final ObjectMapper mapper;

public PolicyEquality(TypeManager typeManager) {
Expand All @@ -33,10 +35,10 @@ public PolicyEquality(TypeManager typeManager) {
public boolean test(Policy one, Policy two) {
var oneTree = mapper.<ObjectNode>valueToTree(one);
var twoTree = mapper.<ObjectNode>valueToTree(two);
oneTree.remove("@type");
twoTree.remove("@type");
oneTree.remove("target");
twoTree.remove("target");
EXCLUDED_PROPERTIES.forEach(property -> {
oneTree.remove(property);
twoTree.remove(property);
});
return oneTree.equals(twoTree);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,14 @@ void policyTypeIsExcludedFromTheComparison() {

assertThat(result).isTrue();
}

@Test
void assigneeIsExcludedFromTheComparison() {
var one = Policy.Builder.newInstance().assignee("one").build();
var two = Policy.Builder.newInstance().assignee("other").build();

var result = comparator.test(one, two);

assertThat(result).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ 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:transform-spi"))
api(project(":spi:control-plane:contract-spi"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
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.PropertyAndTypeNames.ODRL_ASSIGNEE_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ASSIGNER_ATTRIBUTE;
import static org.eclipse.edc.protocol.dsp.type.DspNegotiationPropertyAndTypeNames.DSPACE_PROPERTY_AGREEMENT;
import static org.eclipse.edc.protocol.dsp.type.DspNegotiationPropertyAndTypeNames.DSPACE_PROPERTY_CONSUMER_ID;
import static org.eclipse.edc.protocol.dsp.type.DspNegotiationPropertyAndTypeNames.DSPACE_PROPERTY_PROVIDER_ID;
Expand All @@ -50,13 +52,6 @@ public JsonObjectFromContractAgreementMessageTransformer(JsonBuilderFactory json

@Override
public @Nullable JsonObject transform(@NotNull ContractAgreementMessage agreementMessage, @NotNull TransformerContext context) {
var builder = jsonFactory.createObjectBuilder()
.add(ID, agreementMessage.getId())
.add(TYPE, DSPACE_TYPE_CONTRACT_AGREEMENT_MESSAGE)
.add(DSPACE_PROPERTY_PROVIDER_PID, agreementMessage.getProviderPid())
.add(DSPACE_PROPERTY_CONSUMER_PID, agreementMessage.getConsumerPid())
.add(DSPACE_PROPERTY_PROCESS_ID, agreementMessage.getProcessId());

var agreement = agreementMessage.getContractAgreement();

var policy = context.transform(agreement.getPolicy(), JsonObject.class);
Expand All @@ -69,19 +64,25 @@ public JsonObjectFromContractAgreementMessageTransformer(JsonBuilderFactory json
return null;
}

// add the consumer id, provider id, and signing timestamp to the agreement
var signing = ofEpochSecond(agreement.getContractSigningDate()).toString();

var copiedPolicy = Json.createObjectBuilder(policy)
.add(ID, agreement.getId())
.add(ODRL_ASSIGNEE_ATTRIBUTE, agreement.getConsumerId())
.add(ODRL_ASSIGNER_ATTRIBUTE, agreement.getProviderId())
.add(DSPACE_PROPERTY_CONSUMER_ID, agreement.getConsumerId())
.add(DSPACE_PROPERTY_PROVIDER_ID, agreement.getProviderId())
.add(DSPACE_PROPERTY_TIMESTAMP, signing)
.build();

builder.add(DSPACE_PROPERTY_AGREEMENT, copiedPolicy);

return builder.build();
return jsonFactory.createObjectBuilder()
.add(ID, agreementMessage.getId())
.add(TYPE, DSPACE_TYPE_CONTRACT_AGREEMENT_MESSAGE)
.add(DSPACE_PROPERTY_PROVIDER_PID, agreementMessage.getProviderPid())
.add(DSPACE_PROPERTY_CONSUMER_PID, agreementMessage.getConsumerPid())
.add(DSPACE_PROPERTY_PROCESS_ID, agreementMessage.getProcessId())
.add(DSPACE_PROPERTY_AGREEMENT, copiedPolicy)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,17 @@ private ContractAgreement contractAgreement(JsonObject jsonAgreement, Policy pol
.report();
return null;
}
builder.id(agreementId);

if (!transformMandatoryString(jsonAgreement.get(DSPACE_PROPERTY_CONSUMER_ID), builder::consumerId, context)) {
var assignee = policy.getAssignee();
var assigner = policy.getAssigner();

builder.id(agreementId)
.consumerId(assignee)
.providerId(assigner)
.policy(policy)
.assetId(policy.getTarget());

if (assignee == null && !transformMandatoryString(jsonAgreement.get(DSPACE_PROPERTY_CONSUMER_ID), builder::consumerId, context)) {
context.problem()
.missingProperty()
.type(DSPACE_TYPE_CONTRACT_AGREEMENT_MESSAGE)
Expand All @@ -137,7 +145,7 @@ private ContractAgreement contractAgreement(JsonObject jsonAgreement, Policy pol
return null;
}

if (!transformMandatoryString(jsonAgreement.get(DSPACE_PROPERTY_PROVIDER_ID), builder::providerId, context)) {
if (assigner == null && !transformMandatoryString(jsonAgreement.get(DSPACE_PROPERTY_PROVIDER_ID), builder::providerId, context)) {
context.problem()
.missingProperty()
.type(DSPACE_TYPE_CONTRACT_AGREEMENT_MESSAGE)
Expand All @@ -146,9 +154,6 @@ private ContractAgreement contractAgreement(JsonObject jsonAgreement, Policy pol
return null;
}

builder.policy(policy);
builder.assetId(policy.getTarget());

var timestamp = transformString(jsonAgreement.get(DSPACE_PROPERTY_TIMESTAMP), context);
if (timestamp == null) {
context.problem()
Expand Down
Loading

0 comments on commit 486b433

Please sign in to comment.