Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dsp): support implicit Offer policy type #4143

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DEPENDENCIES
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
maven/mavencentral/com.apicatalog/carbon-did/0.3.0, Apache-2.0, approved, clearlydefined

Check warning on line 1 in DEPENDENCIES

View workflow job for this annotation

GitHub Actions / check / Dash-Verify-Licenses

Restricted Dependencies found

Some dependencies are marked 'restricted' - please review them
maven/mavencentral/com.apicatalog/copper-multibase/0.5.0, Apache-2.0, approved, #14501
maven/mavencentral/com.apicatalog/copper-multicodec/0.1.1, Apache-2.0, approved, #14500
maven/mavencentral/com.apicatalog/iron-ed25519-cryptosuite-2020/0.14.0, Apache-2.0, approved, #14503
Expand Down Expand Up @@ -122,7 +122,7 @@
maven/mavencentral/io.netty/netty-transport-native-unix-common/4.1.86.Final, Apache-2.0 AND BSD-3-Clause AND MIT, approved, CQ20926
maven/mavencentral/io.netty/netty-transport/4.1.86.Final, Apache-2.0 AND BSD-3-Clause AND MIT, approved, CQ20926
maven/mavencentral/io.opentelemetry.instrumentation/opentelemetry-instrumentation-annotations/1.32.0, Apache-2.0, approved, #11684
maven/mavencentral/io.opentelemetry.proto/opentelemetry-proto/1.2.0-alpha, , restricted, clearlydefined
maven/mavencentral/io.opentelemetry.proto/opentelemetry-proto/1.2.0-alpha, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.opentelemetry/opentelemetry-api/1.32.0, Apache-2.0, approved, #11682
maven/mavencentral/io.opentelemetry/opentelemetry-context/1.32.0, Apache-2.0, approved, #11683
maven/mavencentral/io.prometheus/simpleclient/0.16.0, Apache-2.0, approved, clearlydefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

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;
Expand Down Expand Up @@ -54,23 +56,28 @@ public JsonObjectToPolicyTransformer(ParticipantIdMapper participantIdMapper) {
public @Nullable Policy transform(@NotNull JsonObject object, @NotNull TransformerContext context) {
var builder = Policy.Builder.newInstance();

var type = object.getJsonArray(TYPE).stream().findFirst()
.map(JsonString.class::cast)
.map(JsonString::getString)
.orElse(ODRL_POLICY_TYPE_SET);
var policyType = Optional.ofNullable(context.consumeData(Policy.class, TYPE))
.map(PolicyType.class::cast)
.orElseGet(() -> {
var tp = object.getJsonArray(TYPE).stream().findFirst()
.map(JsonString.class::cast)
.map(JsonString::getString)
.orElse(ODRL_POLICY_TYPE_SET);

var policyType = switch (type) {
case ODRL_POLICY_TYPE_SET -> PolicyType.SET;
case ODRL_POLICY_TYPE_OFFER -> PolicyType.OFFER;
case ODRL_POLICY_TYPE_AGREEMENT -> PolicyType.CONTRACT;
default -> null;
};
return switch (tp) {
case ODRL_POLICY_TYPE_SET -> PolicyType.SET;
case ODRL_POLICY_TYPE_OFFER -> PolicyType.OFFER;
case ODRL_POLICY_TYPE_AGREEMENT -> PolicyType.CONTRACT;
default -> null;
};
});

if (policyType == null) {
context.problem()
.invalidProperty()
.property(TYPE)
.error("Invalid type %s for ODRL policy, should be one of [%s, %s, %s]".formatted(type, ODRL_POLICY_TYPE_SET, ODRL_POLICY_TYPE_OFFER, ODRL_POLICY_TYPE_AGREEMENT))
.value(null)
.error("Invalid type for ODRL policy, should be one of [%s, %s, %s]".formatted(ODRL_POLICY_TYPE_SET, ODRL_POLICY_TYPE_OFFER, ODRL_POLICY_TYPE_AGREEMENT))
.report();
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.edc.connector.controlplane.transform.TestInput;
import org.eclipse.edc.policy.model.Duty;
import org.eclipse.edc.policy.model.Permission;
import org.eclipse.edc.policy.model.Policy;
import org.eclipse.edc.policy.model.PolicyType;
import org.eclipse.edc.policy.model.Prohibition;
import org.eclipse.edc.spi.agent.ParticipantIdMapper;
Expand Down Expand Up @@ -85,7 +86,6 @@ void setUp() {

@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();
Expand Down Expand Up @@ -171,6 +171,20 @@ void transform_invalidType_reportProblem() {
verify(context).reportProblem(anyString());
}

@Test
void shouldGetTypeFromContext_whenSet() {
when(context.consumeData(Policy.class, TYPE)).thenReturn(CONTRACT);

var policy = jsonFactory.createObjectBuilder()
.add(ODRL_TARGET_ATTRIBUTE, TARGET)
.build();

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

assertThat(result).isNotNull();
assertThat(result.getType()).isEqualTo(CONTRACT);
}

private static class PolicyTypeArguments implements ArgumentsProvider {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import org.eclipse.edc.connector.controlplane.contract.spi.types.offer.ContractOffer;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.policy.model.Policy;
import org.eclipse.edc.policy.model.PolicyType;
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_OFFER;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_TYPE_CONTRACT_OFFER_MESSAGE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CALLBACK_ADDRESS;
Expand Down Expand Up @@ -66,6 +68,7 @@ public JsonObjectToContractOfferMessageTransformer() {

var contractOffer = returnJsonObject(jsonObject.get(DSPACE_PROPERTY_OFFER), context, DSPACE_PROPERTY_OFFER, false);
if (contractOffer != null) {
context.setData(Policy.class, TYPE, PolicyType.OFFER);
var policy = transformObject(contractOffer, Policy.class, context);
if (policy == null) {
reportMissingProperty(DSPACE_TYPE_CONTRACT_OFFER_MESSAGE, DSPACE_PROPERTY_OFFER, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import jakarta.json.JsonObject;
import org.eclipse.edc.jsonld.spi.JsonLdKeywords;
import org.eclipse.edc.policy.model.Policy;
import org.eclipse.edc.policy.model.PolicyType;
import org.eclipse.edc.transform.spi.ProblemBuilder;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -28,6 +29,7 @@

import static org.assertj.core.api.Assertions.assertThat;
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_OFFER;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_TYPE_CONTRACT_OFFER_MESSAGE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CALLBACK_ADDRESS;
Expand All @@ -53,11 +55,10 @@ class JsonObjectToContractOfferMessageTransformerTest {
private final JsonBuilderFactory jsonFactory = Json.createBuilderFactory(Map.of());
private final TransformerContext context = mock();

private JsonObjectToContractOfferMessageTransformer transformer;
private final JsonObjectToContractOfferMessageTransformer transformer = new JsonObjectToContractOfferMessageTransformer();

@BeforeEach
void setUp() {
transformer = new JsonObjectToContractOfferMessageTransformer();
when(context.problem()).thenReturn(new ProblemBuilder(context));
}

Expand Down Expand Up @@ -92,6 +93,7 @@ void transform_shouldReturnMessage_whenValidJsonObject() {
assertThat(contractOffer.getAssetId()).isEqualTo(ASSET_ID);

verify(context, never()).reportProblem(anyString());
verify(context).setData(Policy.class, TYPE, PolicyType.OFFER);
}

@Deprecated(since = "0.4.1")
Expand Down
Loading