Changesets · Branches · Tags · Compare
| Changeset | f6f4c91a2d7de2e556382e2e0a9a9bb4033f097fd9af4e490a5846daa7034a42 |
|---|---|
| Author | Sascha Isele (ZF Friedrichshafen AG) <127207440+saschaisele-zf@users.noreply.github.com> · 2024-07-17 08:57 +02:00 |
| Committer | GitHub <noreply@github.com> · 2024-07-17 08:57 +02:00 |
| Parents | b488bd35123e |
20 files changed
added core/common/lib/validator-lib/src/main/java/org/eclipse/edc/validator/jsonobject/validators/OptionalIdArray.java +88 −0
| @@ -0,0 +1,88 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 ZF Friedrichshafen AG | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * ZF Friedrichshafen AG - initial implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.validator.jsonobject.validators; | |
| 16 | + | |
| 17 | +import jakarta.json.JsonArray; | |
| 18 | +import jakarta.json.JsonObject; | |
| 19 | +import org.eclipse.edc.validator.jsonobject.JsonLdPath; | |
| 20 | +import org.eclipse.edc.validator.spi.ValidationResult; | |
| 21 | +import org.eclipse.edc.validator.spi.Validator; | |
| 22 | + | |
| 23 | +import java.util.Optional; | |
| 24 | +import java.util.function.Function; | |
| 25 | + | |
| 26 | +import static java.lang.String.format; | |
| 27 | +import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID; | |
| 28 | +import static org.eclipse.edc.validator.spi.Violation.violation; | |
| 29 | + | |
| 30 | +/** | |
| 31 | + * Verifies that if an array is present the key:value pairs are of type @id:string with optional constraint on min size. | |
| 32 | + */ | |
| 33 | +public class OptionalIdArray implements Validator<JsonObject> { | |
| 34 | + private final JsonLdPath path; | |
| 35 | + private final Integer min; | |
| 36 | + | |
| 37 | + public OptionalIdArray(JsonLdPath path) { | |
| 38 | + this(path, null); | |
| 39 | + } | |
| 40 | + | |
| 41 | + public OptionalIdArray(JsonLdPath path, Integer min) { | |
| 42 | + this.path = path; | |
| 43 | + this.min = min; | |
| 44 | + } | |
| 45 | + | |
| 46 | + public static Function<JsonLdPath, Validator<JsonObject>> min(Integer min) { | |
| 47 | + return path -> new OptionalIdArray(path, min); | |
| 48 | + } | |
| 49 | + | |
| 50 | + @Override | |
| 51 | + public ValidationResult validate(JsonObject input) { | |
| 52 | + ValidationResult sizeResult; | |
| 53 | + | |
| 54 | + sizeResult = Optional.ofNullable(input.getJsonArray(path.last())) | |
| 55 | + .map(this::validateMin) | |
| 56 | + .orElse(ValidationResult.success()); | |
| 57 | + | |
| 58 | + if (sizeResult.failed()) { | |
| 59 | + return sizeResult; | |
| 60 | + } | |
| 61 | + | |
| 62 | + return Optional.ofNullable(input.getJsonArray(path.last())) | |
| 63 | + .map(this::validateType) | |
| 64 | + .orElse(ValidationResult.success()); | |
| 65 | + } | |
| 66 | + | |
| 67 | + private ValidationResult validateMin(JsonArray array) { | |
| 68 | + if (min == null || (array.size() >= min)) { | |
| 69 | + return ValidationResult.success(); | |
| 70 | + } | |
| 71 | + return ValidationResult.failure(violation(format("array '%s' should at least contains '%s' elements", path, min), path.toString())); | |
| 72 | + } | |
| 73 | + | |
| 74 | + private ValidationResult validateType(JsonArray array) { | |
| 75 | + for (var value : array) { | |
| 76 | + try { | |
| 77 | + var id = value.asJsonObject().getJsonString(ID); | |
| 78 | + if (id == null || id.getString().isBlank()) { | |
| 79 | + return ValidationResult.failure(violation(format("contents of array '%s' should not be blank or empty", path), path.toString())); | |
| 80 | + } | |
| 81 | + } catch (ClassCastException e) { | |
| 82 | + return ValidationResult.failure(violation(format("contents of array '%s' should be of type string", path), path.toString())); | |
| 83 | + } | |
| 84 | + } | |
| 85 | + return ValidationResult.success(); | |
| 86 | + } | |
| 87 | + | |
| 88 | +} |
modified core/common/lib/validator-lib/src/test/java/org/eclipse/edc/validator/jsonobject/JsonObjectValidatorTest.java +73 −0
| @@ -18,9 +18,11 @@ | ||
| 18 | 18 | import org.eclipse.edc.validator.jsonobject.validators.MandatoryArray; |
| 19 | 19 | import org.eclipse.edc.validator.jsonobject.validators.MandatoryObject; |
| 20 | 20 | import org.eclipse.edc.validator.jsonobject.validators.MandatoryValue; |
| 21 | +import org.eclipse.edc.validator.jsonobject.validators.OptionalIdArray; | |
| 21 | 22 | import org.eclipse.edc.validator.jsonobject.validators.OptionalIdNotBlank; |
| 22 | 23 | import org.eclipse.edc.validator.spi.ValidationFailure; |
| 23 | 24 | import org.eclipse.edc.validator.spi.Violation; |
| 25 | +import org.junit.jupiter.api.Nested; | |
| 24 | 26 | import org.junit.jupiter.api.Test; |
| 25 | 27 | |
| 26 | 28 | import static jakarta.json.Json.createArrayBuilder; |
| @@ -217,6 +219,77 @@ | ||
| 217 | 219 | }); |
| 218 | 220 | } |
| 219 | 221 | |
| 222 | + @Nested | |
| 223 | + class TestOptionalIdArray { | |
| 224 | + @Test | |
| 225 | + void shouldFail_ValidateOptionalIdArrayMinSize() { | |
| 226 | + var input = createObjectBuilder() | |
| 227 | + .add("arrayProperty", createArrayBuilder() | |
| 228 | + .add(createObjectBuilder().add(ID, "value1")) | |
| 229 | + .add(createObjectBuilder().add(ID, "value2")) | |
| 230 | + ); | |
| 231 | + | |
| 232 | + var result = JsonObjectValidator.newValidator() | |
| 233 | + .verify("arrayProperty", OptionalIdArray.min(3)) | |
| 234 | + .build() | |
| 235 | + .validate(input.build()); | |
| 236 | + | |
| 237 | + assertThat(result).isFailed().satisfies(failure -> { | |
| 238 | + assertThat(failure.getViolations()).anySatisfy(violation -> { | |
| 239 | + assertThat(violation.path()).contains("arrayProperty"); | |
| 240 | + }); | |
| 241 | + }); | |
| 242 | + } | |
| 243 | + | |
| 244 | + @Test | |
| 245 | + void shouldSucceed_ValidateOptionalIdArrayNoValue() { | |
| 246 | + var input = createObjectBuilder(); | |
| 247 | + | |
| 248 | + var result = JsonObjectValidator.newValidator() | |
| 249 | + .verify("arrayProperty", OptionalIdArray::new) | |
| 250 | + .build() | |
| 251 | + .validate(input.build()); | |
| 252 | + | |
| 253 | + assertThat(result).isSucceeded(); | |
| 254 | + } | |
| 255 | + | |
| 256 | + @Test | |
| 257 | + void shouldFail_ValidateOptionalIdArrayWrongType() { | |
| 258 | + var input = createObjectBuilder() | |
| 259 | + .add("arrayProperty", createArrayBuilder() | |
| 260 | + .add(createObjectBuilder().add("subProperty", createObjectBuilder())) | |
| 261 | + ); | |
| 262 | + | |
| 263 | + var result = JsonObjectValidator.newValidator() | |
| 264 | + .verify("arrayProperty", OptionalIdArray::new) | |
| 265 | + .build() | |
| 266 | + .validate(input.build()); | |
| 267 | + | |
| 268 | + assertThat(result).isFailed().satisfies(failure -> { | |
| 269 | + assertThat(failure.getViolations()).anySatisfy(violation -> { | |
| 270 | + assertThat(violation.path()).contains("arrayProperty"); | |
| 271 | + }); | |
| 272 | + }); | |
| 273 | + } | |
| 274 | + | |
| 275 | + @Test | |
| 276 | + void shouldSucceed_ValidateOptionalIdArray() { | |
| 277 | + var input = createObjectBuilder() | |
| 278 | + .add("arrayProperty", createArrayBuilder() | |
| 279 | + .add(createObjectBuilder().add(ID, "value1")) | |
| 280 | + .add(createObjectBuilder().add(ID, "value2")) | |
| 281 | + ); | |
| 282 | + | |
| 283 | + var result = JsonObjectValidator.newValidator() | |
| 284 | + .verify("arrayProperty", OptionalIdArray::new) | |
| 285 | + .build() | |
| 286 | + .validate(input.build()); | |
| 287 | + | |
| 288 | + assertThat(result).isSucceeded(); | |
| 289 | + } | |
| 290 | + } | |
| 291 | + | |
| 292 | + | |
| 220 | 293 | private JsonArrayBuilder value(String value) { |
| 221 | 294 | return createArrayBuilder().add(createObjectBuilder().add(VALUE, value)); |
| 222 | 295 | } |
modified core/control-plane/control-plane-transform/src/main/java/org/eclipse/edc/connector/controlplane/transform/odrl/to/JsonObjectToPolicyTransformer.java +12 −0
| @@ -16,6 +16,7 @@ | ||
| 16 | 16 | |
| 17 | 17 | import jakarta.json.JsonObject; |
| 18 | 18 | import jakarta.json.JsonString; |
| 19 | +import jakarta.json.JsonValue; | |
| 19 | 20 | import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer; |
| 20 | 21 | import org.eclipse.edc.policy.model.Duty; |
| 21 | 22 | import org.eclipse.edc.policy.model.Permission; |
| @@ -27,8 +28,11 @@ | ||
| 27 | 28 | import org.jetbrains.annotations.NotNull; |
| 28 | 29 | import org.jetbrains.annotations.Nullable; |
| 29 | 30 | |
| 31 | +import java.util.List; | |
| 32 | +import java.util.Objects; | |
| 30 | 33 | import java.util.Optional; |
| 31 | 34 | |
| 35 | +import static java.util.stream.Collectors.toList; | |
| 32 | 36 | import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE; |
| 33 | 37 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ASSIGNEE_ATTRIBUTE; |
| 34 | 38 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ASSIGNER_ATTRIBUTE; |
| @@ -37,6 +41,7 @@ | ||
| 37 | 41 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_AGREEMENT; |
| 38 | 42 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_OFFER; |
| 39 | 43 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_SET; |
| 44 | +import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PROFILE_ATTRIBUTE; | |
| 40 | 45 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PROHIBITION_ATTRIBUTE; |
| 41 | 46 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_TARGET_ATTRIBUTE; |
| 42 | 47 | |
| @@ -91,10 +96,17 @@ | ||
| 91 | 96 | case ODRL_TARGET_ATTRIBUTE -> v -> builder.target(transformString(v, context)); |
| 92 | 97 | case ODRL_ASSIGNER_ATTRIBUTE -> v -> builder.assigner(participantIdMapper.fromIri(transformString(v, context))); |
| 93 | 98 | case ODRL_ASSIGNEE_ATTRIBUTE -> v -> builder.assignee(participantIdMapper.fromIri(transformString(v, context))); |
| 99 | + case ODRL_PROFILE_ATTRIBUTE -> v -> builder.profiles(transformProfile(v)); | |
| 94 | 100 | default -> v -> builder.extensibleProperty(key, transformGenericProperty(v, context)); |
| 95 | 101 | }); |
| 96 | 102 | |
| 97 | 103 | return builderResult(builder::build, context); |
| 98 | 104 | } |
| 99 | 105 | |
| 106 | + private List<String> transformProfile(JsonValue value) { | |
| 107 | + return value.asJsonArray().stream() | |
| 108 | + .map(this::nodeId) | |
| 109 | + .filter(Objects::nonNull) | |
| 110 | + .collect(toList()); | |
| 111 | + } | |
| 100 | 112 | } |
modified core/control-plane/control-plane-transform/src/test/java/org/eclipse/edc/connector/controlplane/transform/odrl/to/JsonObjectToPolicyTransformerTest.java +17 −0
| @@ -40,6 +40,7 @@ | ||
| 40 | 40 | |
| 41 | 41 | import static org.assertj.core.api.Assertions.assertThat; |
| 42 | 42 | import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.CONTEXT; |
| 43 | +import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID; | |
| 43 | 44 | import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE; |
| 44 | 45 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ASSIGNEE_ATTRIBUTE; |
| 45 | 46 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ASSIGNER_ATTRIBUTE; |
| @@ -48,6 +49,7 @@ | ||
| 48 | 49 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_AGREEMENT; |
| 49 | 50 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_OFFER; |
| 50 | 51 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_SET; |
| 52 | +import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PROFILE_ATTRIBUTE; | |
| 51 | 53 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PROHIBITION_ATTRIBUTE; |
| 52 | 54 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_TARGET_ATTRIBUTE; |
| 53 | 55 | import static org.eclipse.edc.policy.model.PolicyType.CONTRACT; |
| @@ -185,6 +187,21 @@ | ||
| 185 | 187 | assertThat(result.getType()).isEqualTo(CONTRACT); |
| 186 | 188 | } |
| 187 | 189 | |
| 190 | + @Test | |
| 191 | + void transform_withProfile() { | |
| 192 | + | |
| 193 | + var policy = jsonFactory.createObjectBuilder() | |
| 194 | + .add(TYPE, ODRL_POLICY_TYPE_SET) | |
| 195 | + .add(ODRL_PROFILE_ATTRIBUTE, jsonFactory.createObjectBuilder().add(ID, "http://example.com/odrl:profile:01")) | |
| 196 | + .build(); | |
| 197 | + | |
| 198 | + var result = transformer.transform(TestInput.getExpanded(policy), context); | |
| 199 | + | |
| 200 | + assertThat(result).isNotNull(); | |
| 201 | + assertThat(result.getProfiles().get(0)).isEqualTo("http://example.com/odrl:profile:01"); | |
| 202 | + verify(context, never()).reportProblem(anyString()); | |
| 203 | + } | |
| 204 | + | |
| 188 | 205 | private static class PolicyTypeArguments implements ArgumentsProvider { |
| 189 | 206 | |
| 190 | 207 | @Override |
added extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/controlplane/api/management/policy/v31alpha/PolicyDefinitionApiV31Alpha.java +171 −0
| @@ -0,0 +1,171 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.connector.controlplane.api.management.policy.v31alpha; | |
| 16 | + | |
| 17 | +import io.swagger.v3.oas.annotations.OpenAPIDefinition; | |
| 18 | +import io.swagger.v3.oas.annotations.Operation; | |
| 19 | +import io.swagger.v3.oas.annotations.info.Info; | |
| 20 | +import io.swagger.v3.oas.annotations.media.ArraySchema; | |
| 21 | +import io.swagger.v3.oas.annotations.media.Content; | |
| 22 | +import io.swagger.v3.oas.annotations.media.Schema; | |
| 23 | +import io.swagger.v3.oas.annotations.parameters.RequestBody; | |
| 24 | +import io.swagger.v3.oas.annotations.responses.ApiResponse; | |
| 25 | +import io.swagger.v3.oas.annotations.tags.Tag; | |
| 26 | +import jakarta.json.JsonArray; | |
| 27 | +import jakarta.json.JsonObject; | |
| 28 | +import org.eclipse.edc.api.management.schema.ManagementApiSchema; | |
| 29 | +import org.eclipse.edc.api.model.ApiCoreSchema; | |
| 30 | + | |
| 31 | +import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; | |
| 32 | +import static org.eclipse.edc.connector.controlplane.policy.spi.PolicyDefinition.EDC_POLICY_DEFINITION_TYPE; | |
| 33 | +import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.CONTEXT; | |
| 34 | +import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID; | |
| 35 | +import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE; | |
| 36 | + | |
| 37 | +@OpenAPIDefinition(info = @Info(version = "v3.1-alpha")) | |
| 38 | +@Tag(name = "Policy Definition v3.1-alpha") | |
| 39 | +public interface PolicyDefinitionApiV31Alpha { | |
| 40 | + | |
| 41 | + @Operation(description = "Returns all policy definitions according to a query", | |
| 42 | + requestBody = @RequestBody(content = @Content(schema = @Schema(implementation = ApiCoreSchema.QuerySpecSchema.class))), | |
| 43 | + responses = { | |
| 44 | + @ApiResponse(responseCode = "200", description = "The policy definitions matching the query", | |
| 45 | + content = @Content(array = @ArraySchema(schema = @Schema(implementation = PolicyDefinitionOutputSchema.class)))), | |
| 46 | + @ApiResponse(responseCode = "400", description = "Request was malformed", | |
| 47 | + content = @Content(array = @ArraySchema(schema = @Schema(implementation = ApiCoreSchema.ApiErrorDetailSchema.class)))) } | |
| 48 | + ) | |
| 49 | + JsonArray queryPolicyDefinitionsV3(JsonObject querySpecJson); | |
| 50 | + | |
| 51 | + @Operation(description = "Gets a policy definition with the given ID", | |
| 52 | + responses = { | |
| 53 | + @ApiResponse(responseCode = "200", description = "The policy definition", | |
| 54 | + content = @Content(schema = @Schema(implementation = PolicyDefinitionOutputSchema.class))), | |
| 55 | + @ApiResponse(responseCode = "400", description = "Request was malformed, e.g. id was null", | |
| 56 | + content = @Content(array = @ArraySchema(schema = @Schema(implementation = ApiCoreSchema.ApiErrorDetailSchema.class)))), | |
| 57 | + @ApiResponse(responseCode = "404", description = "An policy definition with the given ID does not exist", | |
| 58 | + content = @Content(array = @ArraySchema(schema = @Schema(implementation = ApiCoreSchema.ApiErrorDetailSchema.class)))) | |
| 59 | + } | |
| 60 | + ) | |
| 61 | + JsonObject getPolicyDefinitionV3(String id); | |
| 62 | + | |
| 63 | + @Operation(description = "Creates a new policy definition", | |
| 64 | + requestBody = @RequestBody(content = @Content(schema = @Schema(implementation = PolicyDefinitionInputSchema.class))), | |
| 65 | + responses = { | |
| 66 | + @ApiResponse(responseCode = "200", description = "policy definition was created successfully. Returns the Policy Definition Id and created timestamp", | |
| 67 | + content = @Content(schema = @Schema(implementation = ApiCoreSchema.IdResponseSchema.class))), | |
| 68 | + @ApiResponse(responseCode = "400", description = "Request body was malformed", | |
| 69 | + content = @Content(array = @ArraySchema(schema = @Schema(implementation = ApiCoreSchema.ApiErrorDetailSchema.class)))), | |
| 70 | + @ApiResponse(responseCode = "409", description = "Could not create policy definition, because a contract definition with that ID already exists", | |
| 71 | + content = @Content(array = @ArraySchema(schema = @Schema(implementation = ApiCoreSchema.ApiErrorDetailSchema.class)))) } | |
| 72 | + ) | |
| 73 | + JsonObject createPolicyDefinitionV3(JsonObject policyDefinition); | |
| 74 | + | |
| 75 | + @Operation(description = "Removes a policy definition with the given ID if possible. Deleting a policy definition is " + | |
| 76 | + "only possible if that policy definition is not yet referenced by a contract definition, in which case an error is returned. " + | |
| 77 | + "DANGER ZONE: Note that deleting policy definitions can have unexpected results, do this at your own risk!", | |
| 78 | + responses = { | |
| 79 | + @ApiResponse(responseCode = "204", description = "Policy definition was deleted successfully"), | |
| 80 | + @ApiResponse(responseCode = "400", description = "Request was malformed, e.g. id was null", | |
| 81 | + content = @Content(array = @ArraySchema(schema = @Schema(implementation = ApiCoreSchema.ApiErrorDetailSchema.class)))), | |
| 82 | + @ApiResponse(responseCode = "404", description = "An policy definition with the given ID does not exist", | |
| 83 | + content = @Content(array = @ArraySchema(schema = @Schema(implementation = ApiCoreSchema.ApiErrorDetailSchema.class)))), | |
| 84 | + @ApiResponse(responseCode = "409", description = "The policy definition cannot be deleted, because it is referenced by a contract definition", | |
| 85 | + content = @Content(array = @ArraySchema(schema = @Schema(implementation = ApiCoreSchema.ApiErrorDetailSchema.class)))) | |
| 86 | + } | |
| 87 | + ) | |
| 88 | + void deletePolicyDefinitionV3(String id); | |
| 89 | + | |
| 90 | + @Operation(description = "Updates an existing Policy, If the Policy is not found, an error is reported", | |
| 91 | + requestBody = @RequestBody(content = @Content(schema = @Schema(implementation = PolicyDefinitionInputSchema.class))), | |
| 92 | + responses = { | |
| 93 | + @ApiResponse(responseCode = "204", description = "policy definition was updated successfully."), | |
| 94 | + @ApiResponse(responseCode = "400", description = "Request body was malformed", | |
| 95 | + content = @Content(array = @ArraySchema(schema = @Schema(implementation = ApiCoreSchema.ApiErrorDetailSchema.class)))), | |
| 96 | + @ApiResponse(responseCode = "404", description = "policy definition could not be updated, because it does not exists", | |
| 97 | + content = @Content(schema = @Schema(implementation = ApiCoreSchema.ApiErrorDetailSchema.class))) | |
| 98 | + } | |
| 99 | + ) | |
| 100 | + void updatePolicyDefinitionV3(String id, JsonObject policyDefinition); | |
| 101 | + | |
| 102 | + @Schema(name = "PolicyDefinitionInput", example = PolicyDefinitionInputSchema.POLICY_DEFINITION_INPUT_EXAMPLE) | |
| 103 | + record PolicyDefinitionInputSchema( | |
| 104 | + @Schema(name = CONTEXT, requiredMode = REQUIRED) | |
| 105 | + Object context, | |
| 106 | + @Schema(name = ID) | |
| 107 | + String id, | |
| 108 | + @Schema(name = TYPE, example = EDC_POLICY_DEFINITION_TYPE) | |
| 109 | + String type, | |
| 110 | + @Schema(requiredMode = REQUIRED) | |
| 111 | + ManagementApiSchema.PolicySchema policy) { | |
| 112 | + | |
| 113 | + // policy example took from https://w3c.github.io/odrl/bp/ | |
| 114 | + public static final String POLICY_DEFINITION_INPUT_EXAMPLE = """ | |
| 115 | + { | |
| 116 | + "@context": { "@vocab": "https://w3id.org/edc/v0.0.1/ns/" }, | |
| 117 | + "@id": "definition-id", | |
| 118 | + "policy": { | |
| 119 | + "@context": "http://www.w3.org/ns/odrl.jsonld", | |
| 120 | + "@type": "Set", | |
| 121 | + "uid": "http://example.com/policy:1010", | |
| 122 | + "profile": "http://example.com/odrl:profile:02", | |
| 123 | + "permission": [{ | |
| 124 | + "target": "http://example.com/asset:9898.movie", | |
| 125 | + "action": "display", | |
| 126 | + "constraint": [{ | |
| 127 | + "leftOperand": "spatial", | |
| 128 | + "operator": "eq", | |
| 129 | + "rightOperand": "https://www.wikidata.org/wiki/Q183", | |
| 130 | + "comment": "i.e Germany" | |
| 131 | + }] | |
| 132 | + }] | |
| 133 | + } | |
| 134 | + } | |
| 135 | + """; | |
| 136 | + } | |
| 137 | + | |
| 138 | + @Schema(name = "PolicyDefinitionOutput", example = PolicyDefinitionOutputSchema.POLICY_DEFINITION_OUTPUT_EXAMPLE) | |
| 139 | + record PolicyDefinitionOutputSchema( | |
| 140 | + @Schema(name = ID) | |
| 141 | + String id, | |
| 142 | + @Schema(name = TYPE, example = EDC_POLICY_DEFINITION_TYPE) | |
| 143 | + String type, | |
| 144 | + ManagementApiSchema.PolicySchema policy) { | |
| 145 | + | |
| 146 | + // policy example took from https://w3c.github.io/odrl/bp/ | |
| 147 | + public static final String POLICY_DEFINITION_OUTPUT_EXAMPLE = """ | |
| 148 | + { | |
| 149 | + "@context": { "@vocab": "https://w3id.org/edc/v0.0.1/ns/" }, | |
| 150 | + "@id": "definition-id", | |
| 151 | + "policy": { | |
| 152 | + "@context": "http://www.w3.org/ns/odrl.jsonld", | |
| 153 | + "@type": "Set", | |
| 154 | + "uid": "http://example.com/policy:1010", | |
| 155 | + "permission": [{ | |
| 156 | + "target": "http://example.com/asset:9898.movie", | |
| 157 | + "action": "display", | |
| 158 | + "constraint": [{ | |
| 159 | + "leftOperand": "spatial", | |
| 160 | + "operator": "eq", | |
| 161 | + "rightOperand": "https://www.wikidata.org/wiki/Q183", | |
| 162 | + "comment": "i.e Germany" | |
| 163 | + }] | |
| 164 | + }] | |
| 165 | + }, | |
| 166 | + "createdAt": 1688465655 | |
| 167 | + } | |
| 168 | + """; | |
| 169 | + } | |
| 170 | + | |
| 171 | +} |
added extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/controlplane/api/management/policy/v31alpha/PolicyDefinitionApiV31AlphaController.java +76 −0
| @@ -0,0 +1,76 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.connector.controlplane.api.management.policy.v31alpha; | |
| 16 | + | |
| 17 | +import jakarta.json.JsonArray; | |
| 18 | +import jakarta.json.JsonObject; | |
| 19 | +import jakarta.ws.rs.Consumes; | |
| 20 | +import jakarta.ws.rs.DELETE; | |
| 21 | +import jakarta.ws.rs.GET; | |
| 22 | +import jakarta.ws.rs.POST; | |
| 23 | +import jakarta.ws.rs.PUT; | |
| 24 | +import jakarta.ws.rs.Path; | |
| 25 | +import jakarta.ws.rs.PathParam; | |
| 26 | +import jakarta.ws.rs.Produces; | |
| 27 | +import org.eclipse.edc.connector.controlplane.api.management.policy.BasePolicyDefinitionApiController; | |
| 28 | +import org.eclipse.edc.connector.controlplane.services.spi.policydefinition.PolicyDefinitionService; | |
| 29 | +import org.eclipse.edc.spi.monitor.Monitor; | |
| 30 | +import org.eclipse.edc.transform.spi.TypeTransformerRegistry; | |
| 31 | +import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry; | |
| 32 | + | |
| 33 | +import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; | |
| 34 | + | |
| 35 | +@Consumes(APPLICATION_JSON) | |
| 36 | +@Produces(APPLICATION_JSON) | |
| 37 | +@Path("/v3.1alpha/policydefinitions") | |
| 38 | +public class PolicyDefinitionApiV31AlphaController extends BasePolicyDefinitionApiController implements PolicyDefinitionApiV31Alpha { | |
| 39 | + public PolicyDefinitionApiV31AlphaController(Monitor monitor, TypeTransformerRegistry transformerRegistry, PolicyDefinitionService service, JsonObjectValidatorRegistry validatorRegistry) { | |
| 40 | + super(monitor, transformerRegistry, service, validatorRegistry); | |
| 41 | + } | |
| 42 | + | |
| 43 | + @POST | |
| 44 | + @Path("request") | |
| 45 | + @Override | |
| 46 | + public JsonArray queryPolicyDefinitionsV3(JsonObject querySpecJson) { | |
| 47 | + return queryPolicyDefinitions(querySpecJson); | |
| 48 | + } | |
| 49 | + | |
| 50 | + @GET | |
| 51 | + @Path("{id}") | |
| 52 | + @Override | |
| 53 | + public JsonObject getPolicyDefinitionV3(@PathParam("id") String id) { | |
| 54 | + return getPolicyDefinition(id); | |
| 55 | + } | |
| 56 | + | |
| 57 | + @POST | |
| 58 | + @Override | |
| 59 | + public JsonObject createPolicyDefinitionV3(JsonObject request) { | |
| 60 | + return createPolicyDefinition(request); | |
| 61 | + } | |
| 62 | + | |
| 63 | + @DELETE | |
| 64 | + @Path("{id}") | |
| 65 | + @Override | |
| 66 | + public void deletePolicyDefinitionV3(@PathParam("id") String id) { | |
| 67 | + deletePolicyDefinition(id); | |
| 68 | + } | |
| 69 | + | |
| 70 | + @PUT | |
| 71 | + @Path("{id}") | |
| 72 | + @Override | |
| 73 | + public void updatePolicyDefinitionV3(@PathParam("id") String id, JsonObject input) { | |
| 74 | + updatePolicyDefinition(id, input); | |
| 75 | + } | |
| 76 | +} |
modified extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/controlplane/api/management/policy/validation/PolicyDefinitionValidator.java +14 −2
| @@ -19,6 +19,7 @@ | ||
| 19 | 19 | import org.eclipse.edc.validator.jsonobject.JsonObjectValidator; |
| 20 | 20 | import org.eclipse.edc.validator.jsonobject.validators.MandatoryObject; |
| 21 | 21 | import org.eclipse.edc.validator.jsonobject.validators.MandatoryValue; |
| 22 | +import org.eclipse.edc.validator.jsonobject.validators.OptionalIdArray; | |
| 22 | 23 | import org.eclipse.edc.validator.jsonobject.validators.OptionalIdNotBlank; |
| 23 | 24 | import org.eclipse.edc.validator.jsonobject.validators.TypeIs; |
| 24 | 25 | import org.eclipse.edc.validator.spi.ValidationResult; |
| @@ -41,6 +42,7 @@ | ||
| 41 | 42 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_OR_CONSTRAINT_ATTRIBUTE; |
| 42 | 43 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PERMISSION_ATTRIBUTE; |
| 43 | 44 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_SET; |
| 45 | +import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PROFILE_ATTRIBUTE; | |
| 44 | 46 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PROHIBITION_ATTRIBUTE; |
| 45 | 47 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_REMEDY_ATTRIBUTE; |
| 46 | 48 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_RIGHT_OPERAND_ATTRIBUTE; |
| @@ -62,7 +64,8 @@ | ||
| 62 | 64 | .verify(path -> new TypeIs(path, ODRL_POLICY_TYPE_SET)) |
| 63 | 65 | .verifyArrayItem(ODRL_PERMISSION_ATTRIBUTE, PermissionValidator::instance) |
| 64 | 66 | .verifyArrayItem(ODRL_OBLIGATION_ATTRIBUTE, DutyValidator::instance) |
| 65 | - .verifyArrayItem(ODRL_PROHIBITION_ATTRIBUTE, ProhibitionValidator::instance); | |
| 67 | + .verifyArrayItem(ODRL_PROHIBITION_ATTRIBUTE, ProhibitionValidator::instance) | |
| 68 | + .verify(ProfileValidator::new); | |
| 66 | 69 | } |
| 67 | 70 | } |
| 68 | 71 | |
| @@ -102,6 +105,16 @@ | ||
| 102 | 105 | |
| 103 | 106 | } |
| 104 | 107 | |
| 108 | + private record ProfileValidator(JsonLdPath path) implements Validator<JsonObject> { | |
| 109 | + @Override | |
| 110 | + public ValidationResult validate(JsonObject input) { | |
| 111 | + return JsonObjectValidator.newValidator() | |
| 112 | + .verify(ODRL_PROFILE_ATTRIBUTE, OptionalIdArray.min(1)) | |
| 113 | + .build() | |
| 114 | + .validate(input); | |
| 115 | + } | |
| 116 | + } | |
| 117 | + | |
| 105 | 118 | private static class ConsequenceValidator { |
| 106 | 119 | public static JsonObjectValidator.Builder instance(JsonObjectValidator.Builder builder) { |
| 107 | 120 | |
| @@ -120,7 +133,6 @@ | ||
| 120 | 133 | .map(it -> ValidationResult.success()) |
| 121 | 134 | .orElse(ValidationResult.failure(violation(format("%s is mandatory but missing or null", path.append(ODRL_ACTION_ATTRIBUTE)), ODRL_ACTION_ATTRIBUTE))); |
| 122 | 135 | } |
| 123 | - | |
| 124 | 136 | } |
| 125 | 137 | |
| 126 | 138 | private record ConstraintValidator(JsonLdPath path) implements Validator<JsonObject> { |
added extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/controlplane/api/management/policy/v31alpha/PolicyDefinitionApiV31AlphaControllerTest.java +36 −0
| @@ -0,0 +1,36 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.connector.controlplane.api.management.policy.v31alpha; | |
| 16 | + | |
| 17 | +import io.restassured.specification.RequestSpecification; | |
| 18 | +import org.eclipse.edc.connector.controlplane.api.management.policy.BasePolicyDefinitionApiControllerTest; | |
| 19 | + | |
| 20 | +import static io.restassured.RestAssured.given; | |
| 21 | + | |
| 22 | +public class PolicyDefinitionApiV31AlphaControllerTest extends BasePolicyDefinitionApiControllerTest { | |
| 23 | + | |
| 24 | + @Override | |
| 25 | + protected Object controller() { | |
| 26 | + return new PolicyDefinitionApiV31AlphaController(monitor, transformerRegistry, service, validatorRegistry); | |
| 27 | + } | |
| 28 | + | |
| 29 | + @Override | |
| 30 | + protected RequestSpecification baseRequest() { | |
| 31 | + return given() | |
| 32 | + .baseUri("http://localhost:%d/v3.1alpha/policydefinitions".formatted(port)) | |
| 33 | + .port(port); | |
| 34 | + } | |
| 35 | + | |
| 36 | +} |
modified extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/controlplane/api/management/policy/validation/PolicyDefinitionValidatorTest.java +85 −0
| @@ -36,6 +36,7 @@ | ||
| 36 | 36 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_OPERATOR_ATTRIBUTE; |
| 37 | 37 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PERMISSION_ATTRIBUTE; |
| 38 | 38 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_SET; |
| 39 | +import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PROFILE_ATTRIBUTE; | |
| 39 | 40 | import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_RIGHT_OPERAND_ATTRIBUTE; |
| 40 | 41 | import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat; |
| 41 | 42 | |
| @@ -201,6 +202,88 @@ | ||
| 201 | 202 | assertThat(result).isSucceeded(); |
| 202 | 203 | } |
| 203 | 204 | |
| 205 | + @Test | |
| 206 | + void shouldSucceed_whenProfileIsPresent() { | |
| 207 | + var policy = createObjectBuilder() | |
| 208 | + .add(TYPE, policySet()) | |
| 209 | + .add(ODRL_PROFILE_ATTRIBUTE, createArrayBuilder().add(createObjectBuilder().add(ID, "value1"))); | |
| 210 | + var policyDefinition = createObjectBuilder() | |
| 211 | + .add(EDC_POLICY_DEFINITION_POLICY, createArrayBuilder().add(policy)) | |
| 212 | + .build(); | |
| 213 | + | |
| 214 | + var result = validator.validate(policyDefinition); | |
| 215 | + | |
| 216 | + assertThat(result).isSucceeded(); | |
| 217 | + } | |
| 218 | + | |
| 219 | + @Test | |
| 220 | + void shouldFail_whenProfileIsWrongType() { | |
| 221 | + var policy = createObjectBuilder() | |
| 222 | + .add(TYPE, policySet()) | |
| 223 | + .add(ODRL_PROFILE_ATTRIBUTE, createArrayBuilder().add(createObjectBuilder().add(ID, createObjectBuilder()))); | |
| 224 | + var policyDefinition = createObjectBuilder() | |
| 225 | + .add(EDC_POLICY_DEFINITION_POLICY, createArrayBuilder().add(policy)) | |
| 226 | + .build(); | |
| 227 | + | |
| 228 | + var result = validator.validate(policyDefinition); | |
| 229 | + | |
| 230 | + assertThat(result).isFailed().extracting(ValidationFailure::getViolations).asInstanceOf(list(Violation.class)) | |
| 231 | + .isNotEmpty() | |
| 232 | + .filteredOn(it -> ODRL_PROFILE_ATTRIBUTE.equals(it.path())) | |
| 233 | + .anySatisfy(violation -> assertThat(violation.message()).contains("type")); | |
| 234 | + } | |
| 235 | + | |
| 236 | + @Test | |
| 237 | + void shouldFail_whenProfileIsEmptyArray() { | |
| 238 | + var policy = createObjectBuilder() | |
| 239 | + .add(TYPE, policySet()) | |
| 240 | + .add(ODRL_PROFILE_ATTRIBUTE, createArrayBuilder()); | |
| 241 | + var policyDefinition = createObjectBuilder() | |
| 242 | + .add(EDC_POLICY_DEFINITION_POLICY, createArrayBuilder().add(policy)) | |
| 243 | + .build(); | |
| 244 | + | |
| 245 | + var result = validator.validate(policyDefinition); | |
| 246 | + | |
| 247 | + assertThat(result).isFailed().extracting(ValidationFailure::getViolations).asInstanceOf(list(Violation.class)) | |
| 248 | + .isNotEmpty() | |
| 249 | + .filteredOn(it -> ODRL_PROFILE_ATTRIBUTE.equals(it.path())) | |
| 250 | + .anySatisfy(violation -> assertThat(violation.message()).contains("elements")); | |
| 251 | + } | |
| 252 | + | |
| 253 | + @Test | |
| 254 | + void shouldFail_whenProfileIsBlank() { | |
| 255 | + var policy = createObjectBuilder() | |
| 256 | + .add(TYPE, policySet()) | |
| 257 | + .add(ODRL_PROFILE_ATTRIBUTE, createArrayBuilder().add(createObjectBuilder().add(ID, " "))); | |
| 258 | + var policyDefinition = createObjectBuilder() | |
| 259 | + .add(EDC_POLICY_DEFINITION_POLICY, createArrayBuilder().add(policy)) | |
| 260 | + .build(); | |
| 261 | + | |
| 262 | + var result = validator.validate(policyDefinition); | |
| 263 | + | |
| 264 | + assertThat(result).isFailed().extracting(ValidationFailure::getViolations).asInstanceOf(list(Violation.class)) | |
| 265 | + .isNotEmpty() | |
| 266 | + .filteredOn(it -> ODRL_PROFILE_ATTRIBUTE.equals(it.path())) | |
| 267 | + .anySatisfy(violation -> assertThat(violation.message()).contains("blank")); | |
| 268 | + } | |
| 269 | + | |
| 270 | + @Test | |
| 271 | + void shouldFail_whenProfileIsEmpty() { | |
| 272 | + var policy = createObjectBuilder() | |
| 273 | + .add(TYPE, policySet()) | |
| 274 | + .add(ODRL_PROFILE_ATTRIBUTE, createArrayBuilder().add(createObjectBuilder().add(ID, ""))); | |
| 275 | + var policyDefinition = createObjectBuilder() | |
| 276 | + .add(EDC_POLICY_DEFINITION_POLICY, createArrayBuilder().add(policy)) | |
| 277 | + .build(); | |
| 278 | + | |
| 279 | + var result = validator.validate(policyDefinition); | |
| 280 | + | |
| 281 | + assertThat(result).isFailed().extracting(ValidationFailure::getViolations).asInstanceOf(list(Violation.class)) | |
| 282 | + .isNotEmpty() | |
| 283 | + .filteredOn(it -> ODRL_PROFILE_ATTRIBUTE.equals(it.path())) | |
| 284 | + .anySatisfy(violation -> assertThat(violation.message()).contains("empty")); | |
| 285 | + } | |
| 286 | + | |
| 204 | 287 | private JsonArrayBuilder policySet() { |
| 205 | 288 | return createArrayBuilder().add(ODRL_POLICY_TYPE_SET); |
| 206 | 289 | } |
| @@ -225,4 +308,6 @@ | ||
| 225 | 308 | .add(ODRL_RIGHT_OPERAND_ATTRIBUTE, rightOperand)); |
| 226 | 309 | } |
| 227 | 310 | |
| 311 | + | |
| 312 | + | |
| 228 | 313 | } |
modified extensions/control-plane/store/sql/policy-definition-store-sql/docs/er.puml +1 −0
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | permissions: string <<json>> |
| 18 | 18 | prohibitions: string <<json>> |
| 19 | 19 | duties: string <<json>> |
| 20 | + profiles: string <<json>> | |
| 20 | 21 | inherits_from: string |
| 21 | 22 | assigner: string |
| 22 | 23 | assignee: string |
modified extensions/control-plane/store/sql/policy-definition-store-sql/docs/schema.sql +2 −0
| @@ -21,6 +21,7 @@ | ||
| 21 | 21 | permissions JSON, |
| 22 | 22 | prohibitions JSON, |
| 23 | 23 | duties JSON, |
| 24 | + profiles JSON, | |
| 24 | 25 | extensible_properties JSON, |
| 25 | 26 | inherits_from VARCHAR, |
| 26 | 27 | assigner VARCHAR, |
| @@ -34,6 +35,7 @@ | ||
| 34 | 35 | COMMENT ON COLUMN edc_policydefinitions.permissions IS 'Java List<Permission> serialized as JSON'; |
| 35 | 36 | COMMENT ON COLUMN edc_policydefinitions.prohibitions IS 'Java List<Prohibition> serialized as JSON'; |
| 36 | 37 | COMMENT ON COLUMN edc_policydefinitions.duties IS 'Java List<Duty> serialized as JSON'; |
| 38 | +COMMENT ON COLUMN edc_policydefinitions.profiles IS 'Java List<String> serialized as JSON'; | |
| 37 | 39 | COMMENT ON COLUMN edc_policydefinitions.extensible_properties IS 'Java Map<String, Object> serialized as JSON'; |
| 38 | 40 | COMMENT ON COLUMN edc_policydefinitions.policy_type IS 'Java PolicyType serialized as JSON'; |
| 39 | 41 |
modified extensions/control-plane/store/sql/policy-definition-store-sql/src/main/java/org/eclipse/edc/connector/controlplane/store/sql/policydefinition/store/SqlPolicyDefinitionStore.java +5 −0
| @@ -53,6 +53,8 @@ | ||
| 53 | 53 | }; |
| 54 | 54 | private final TypeReference<List<Duty>> dutyListType = new TypeReference<>() { |
| 55 | 55 | }; |
| 56 | + private final TypeReference<List<String>> profileListType = new TypeReference<>() { | |
| 57 | + }; | |
| 56 | 58 | private final TypeReference<PolicyType> policyType = new TypeReference<>() { |
| 57 | 59 | }; |
| 58 | 60 | private final TypeReference<Map<String, Object>> extensiblePropertiesType = new TypeReference<>() { |
| @@ -147,6 +149,7 @@ | ||
| 147 | 149 | toJson(policy.getPermissions(), permissionListType), |
| 148 | 150 | toJson(policy.getProhibitions(), prohibitionListType), |
| 149 | 151 | toJson(policy.getObligations(), dutyListType), |
| 152 | + toJson(policy.getProfiles(), profileListType), | |
| 150 | 153 | toJson(policy.getExtensibleProperties()), |
| 151 | 154 | policy.getInheritsFrom(), |
| 152 | 155 | policy.getAssigner(), |
| @@ -170,6 +173,7 @@ | ||
| 170 | 173 | toJson(policy.getPermissions(), permissionListType), |
| 171 | 174 | toJson(policy.getProhibitions(), prohibitionListType), |
| 172 | 175 | toJson(policy.getObligations(), dutyListType), |
| 176 | + toJson(policy.getProfiles(), profileListType), | |
| 173 | 177 | toJson(policy.getExtensibleProperties()), |
| 174 | 178 | policy.getInheritsFrom(), |
| 175 | 179 | policy.getAssigner(), |
| @@ -189,6 +193,7 @@ | ||
| 189 | 193 | .permissions(fromJson(resultSet.getString(statements.getPermissionsColumn()), permissionListType)) |
| 190 | 194 | .prohibitions(fromJson(resultSet.getString(statements.getProhibitionsColumn()), prohibitionListType)) |
| 191 | 195 | .duties(fromJson(resultSet.getString(statements.getDutiesColumn()), dutyListType)) |
| 196 | + .profiles(fromJson(resultSet.getString(statements.getProfilesColumn()), profileListType)) | |
| 192 | 197 | .extensibleProperties(fromJson(resultSet.getString(statements.getExtensiblePropertiesColumn()), extensiblePropertiesType)) |
| 193 | 198 | .inheritsFrom(resultSet.getString(statements.getInheritsFromColumn())) |
| 194 | 199 | .assigner(resultSet.getString(statements.getAssignerColumn())) |
modified extensions/control-plane/store/sql/policy-definition-store-sql/src/main/java/org/eclipse/edc/connector/controlplane/store/sql/policydefinition/store/schema/BaseSqlDialectStatements.java +2 −0
| @@ -40,6 +40,7 @@ | ||
| 40 | 40 | .jsonColumn(getPermissionsColumn()) |
| 41 | 41 | .jsonColumn(getProhibitionsColumn()) |
| 42 | 42 | .jsonColumn(getDutiesColumn()) |
| 43 | + .jsonColumn(getProfilesColumn()) | |
| 43 | 44 | .jsonColumn(getExtensiblePropertiesColumn()) |
| 44 | 45 | .column(getInheritsFromColumn()) |
| 45 | 46 | .column(getAssignerColumn()) |
| @@ -57,6 +58,7 @@ | ||
| 57 | 58 | .jsonColumn(getPermissionsColumn()) |
| 58 | 59 | .jsonColumn(getProhibitionsColumn()) |
| 59 | 60 | .jsonColumn(getDutiesColumn()) |
| 61 | + .jsonColumn(getProfilesColumn()) | |
| 60 | 62 | .jsonColumn(getExtensiblePropertiesColumn()) |
| 61 | 63 | .column(getInheritsFromColumn()) |
| 62 | 64 | .column(getAssignerColumn()) |
modified extensions/control-plane/store/sql/policy-definition-store-sql/src/main/java/org/eclipse/edc/connector/controlplane/store/sql/policydefinition/store/schema/SqlPolicyStoreStatements.java +4 −0
| @@ -66,6 +66,10 @@ | ||
| 66 | 66 | return "duties"; |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | + default String getProfilesColumn() { | |
| 70 | + return "profiles"; | |
| 71 | + } | |
| 72 | + | |
| 69 | 73 | default String getExtensiblePropertiesColumn() { |
| 70 | 74 | return "extensible_properties"; |
| 71 | 75 | } |
modified extensions/control-plane/store/sql/policy-definition-store-sql/src/main/java/org/eclipse/edc/connector/controlplane/store/sql/policydefinition/store/schema/postgres/PolicyMapping.java +1 −0
| @@ -23,6 +23,7 @@ | ||
| 23 | 23 | add("permissions", new JsonFieldTranslator(PostgresDialectStatements.PERMISSIONS_ALIAS)); |
| 24 | 24 | add("prohibitions", new JsonFieldTranslator(PostgresDialectStatements.PROHIBITIONS_ALIAS)); |
| 25 | 25 | add("obligations", new JsonFieldTranslator(PostgresDialectStatements.OBLIGATIONS_ALIAS)); |
| 26 | + add("profiles", new JsonFieldTranslator(PostgresDialectStatements.PROFILES_ALIES)); | |
| 26 | 27 | add("extensibleProperties", new JsonFieldTranslator(PostgresDialectStatements.EXT_PROPERTIES_ALIAS)); |
| 27 | 28 | add("inheritsFrom", statements.getInheritsFromColumn()); |
| 28 | 29 | add("assigner", statements.getAssignerColumn()); |
modified extensions/control-plane/store/sql/policy-definition-store-sql/src/main/java/org/eclipse/edc/connector/controlplane/store/sql/policydefinition/store/schema/postgres/PostgresDialectStatements.java +4 −0
| @@ -32,6 +32,7 @@ | ||
| 32 | 32 | public static final String PERMISSIONS_ALIAS = "perm"; |
| 33 | 33 | public static final String OBLIGATIONS_ALIAS = "oblig"; |
| 34 | 34 | public static final String EXT_PROPERTIES_ALIAS = "extprop"; |
| 35 | + public static final String PROFILES_ALIES = "prof"; | |
| 35 | 36 | private static final String PRIVATE_PROPERTIES = "privateProperties."; |
| 36 | 37 | private static final String SELECT_QUERY = "SELECT * FROM %s"; |
| 37 | 38 | |
| @@ -55,6 +56,9 @@ | ||
| 55 | 56 | } else if (querySpec.containsAnyLeftOperand("policy.obligations")) { |
| 56 | 57 | var select = getSelectFromJsonArrayTemplate(getSelectTemplate(), getDutiesColumn(), OBLIGATIONS_ALIAS); |
| 57 | 58 | return new SqlQueryStatement(select, querySpec, new PolicyDefinitionMapping(this), operatorTranslator); |
| 59 | + } else if (querySpec.containsAnyLeftOperand("policy.profiles")) { | |
| 60 | + var select = getSelectFromJsonArrayTemplate(getSelectTemplate(), getProfilesColumn(), PROFILES_ALIES); | |
| 61 | + return new SqlQueryStatement(select, querySpec, new PolicyDefinitionMapping(this), operatorTranslator); | |
| 58 | 62 | } else if (querySpec.containsAnyLeftOperand("policy.extensibleProperties")) { |
| 59 | 63 | var select = getSelectFromJsonArrayTemplate(getSelectTemplate(), getExtensiblePropertiesColumn(), EXT_PROPERTIES_ALIAS); |
| 60 | 64 | return new SqlQueryStatement(select, querySpec, new PolicyDefinitionMapping(this), operatorTranslator); |
modified spi/common/json-ld-spi/src/main/java/org/eclipse/edc/jsonld/spi/PropertyAndTypeNames.java +1 −0
| @@ -78,6 +78,7 @@ | ||
| 78 | 78 | String ODRL_OR_CONSTRAINT_ATTRIBUTE = ODRL_SCHEMA + "or"; |
| 79 | 79 | String ODRL_XONE_CONSTRAINT_ATTRIBUTE = ODRL_SCHEMA + "xone"; |
| 80 | 80 | String ODRL_USE_ACTION_ATTRIBUTE = ODRL_SCHEMA + "use"; |
| 81 | + String ODRL_PROFILE_ATTRIBUTE = ODRL_SCHEMA + "profile"; | |
| 81 | 82 | |
| 82 | 83 | String DSPACE_PROPERTY_PARTICIPANT_ID = DSPACE_SCHEMA + "participantId"; |
| 83 | 84 | } |
modified spi/common/policy-model/src/main/java/org/eclipse/edc/policy/model/Policy.java +16 −2
| @@ -36,11 +36,13 @@ | ||
| 36 | 36 | private final List<Permission> permissions = new ArrayList<>(); |
| 37 | 37 | private final List<Prohibition> prohibitions = new ArrayList<>(); |
| 38 | 38 | private final List<Duty> obligations = new ArrayList<>(); |
| 39 | + private final List<String> profiles = new ArrayList<>(); | |
| 39 | 40 | private final Map<String, Object> extensibleProperties = new HashMap<>(); |
| 40 | 41 | private String inheritsFrom; |
| 41 | 42 | private String assigner; |
| 42 | 43 | private String assignee; |
| 43 | 44 | private String target; |
| 45 | + | |
| 44 | 46 | @JsonProperty("@type") |
| 45 | 47 | private PolicyType type = PolicyType.SET; |
| 46 | 48 | |
| @@ -59,6 +61,10 @@ | ||
| 59 | 61 | return obligations; |
| 60 | 62 | } |
| 61 | 63 | |
| 64 | + public List<String> getProfiles() { | |
| 65 | + return profiles; | |
| 66 | + } | |
| 67 | + | |
| 62 | 68 | @Nullable |
| 63 | 69 | public String getInheritsFrom() { |
| 64 | 70 | return inheritsFrom; |
| @@ -102,7 +108,7 @@ | ||
| 102 | 108 | return false; |
| 103 | 109 | } |
| 104 | 110 | Policy policy = (Policy) o; |
| 105 | - return permissions.equals(policy.permissions) && prohibitions.equals(policy.prohibitions) && obligations.equals(policy.obligations) && extensibleProperties.equals(policy.extensibleProperties) && | |
| 111 | + return permissions.equals(policy.permissions) && prohibitions.equals(policy.prohibitions) && obligations.equals(policy.obligations) && profiles.equals(policy.profiles) && extensibleProperties.equals(policy.extensibleProperties) && | |
| 106 | 112 | Objects.equals(inheritsFrom, policy.inheritsFrom) && Objects.equals(assigner, policy.assigner) && Objects.equals(assignee, policy.assignee) && Objects.equals(target, policy.target) && type == policy.type; |
| 107 | 113 | } |
| 108 | 114 | |
| @@ -123,6 +129,7 @@ | ||
| 123 | 129 | .type(type) |
| 124 | 130 | .extensibleProperties(extensibleProperties) |
| 125 | 131 | .target(target) |
| 132 | + .profiles(profiles) | |
| 126 | 133 | .build(); |
| 127 | 134 | } |
| 128 | 135 | |
| @@ -139,7 +146,8 @@ | ||
| 139 | 146 | .inheritsFrom(inheritsFrom) |
| 140 | 147 | .type(type) |
| 141 | 148 | .extensibleProperties(extensibleProperties) |
| 142 | - .target(target); | |
| 149 | + .target(target) | |
| 150 | + .profiles(profiles); | |
| 143 | 151 | } |
| 144 | 152 | |
| 145 | 153 | public interface Visitor<R> { |
| @@ -178,6 +186,12 @@ | ||
| 178 | 186 | return this; |
| 179 | 187 | } |
| 180 | 188 | |
| 189 | + | |
| 190 | + public Builder profiles(List<String> profiles) { | |
| 191 | + policy.profiles.addAll(profiles); | |
| 192 | + return this; | |
| 193 | + } | |
| 194 | + | |
| 181 | 195 | public Builder duty(Duty duty) { |
| 182 | 196 | policy.obligations.add(duty); |
| 183 | 197 | return this; |
modified spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/controlplane/policy/spi/testfixtures/TestFunctions.java +10 −0
| @@ -47,6 +47,16 @@ | ||
| 47 | 47 | .build(); |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | + public static PolicyDefinition createPolicy(String id, String target, List<String> profiles) { | |
| 51 | + var policy = createPolicyBuilder(id).profiles(profiles).target(target).build(); | |
| 52 | + | |
| 53 | + return PolicyDefinition.Builder.newInstance() | |
| 54 | + .createdAt(Clock.systemUTC().millis()) | |
| 55 | + .id(id) | |
| 56 | + .policy(policy) | |
| 57 | + .build(); | |
| 58 | + } | |
| 59 | + | |
| 50 | 60 | public static PolicyDefinition createPolicy(String id, String target, Map<String, Object> privateProperties) { |
| 51 | 61 | var policy = createPolicyBuilder(id).target(target).build(); |
| 52 | 62 |
modified spi/control-plane/policy-spi/src/testFixtures/java/org/eclipse/edc/connector/controlplane/policy/spi/testfixtures/store/PolicyDefinitionStoreTestBase.java +23 −7
| @@ -31,6 +31,7 @@ | ||
| 31 | 31 | import org.junit.jupiter.api.Nested; |
| 32 | 32 | import org.junit.jupiter.api.Test; |
| 33 | 33 | |
| 34 | +import java.util.List; | |
| 34 | 35 | import java.util.Map; |
| 35 | 36 | import java.util.UUID; |
| 36 | 37 | import java.util.stream.Collectors; |
| @@ -49,7 +50,7 @@ | ||
| 49 | 50 | class Create { |
| 50 | 51 | |
| 51 | 52 | @Test |
| 52 | - @DisplayName("Save a single policy that not exists ") | |
| 53 | + @DisplayName("Save a single policy that does not exist") | |
| 53 | 54 | void notExisting() { |
| 54 | 55 | var policy = TestFunctions.createPolicy(getRandomId()); |
| 55 | 56 | |
| @@ -92,7 +93,7 @@ | ||
| 92 | 93 | } |
| 93 | 94 | |
| 94 | 95 | @Test |
| 95 | - @DisplayName("Save a single policy that not exists ") | |
| 96 | + @DisplayName("Save a single policy that does not exist") | |
| 96 | 97 | void notExisting_with_privateProperties() { |
| 97 | 98 | var policy = TestFunctions.createPolicy(getRandomId(), null, Map.of("key1", "value1", "key2", "value2")); |
| 98 | 99 | |
| @@ -105,6 +106,21 @@ | ||
| 105 | 106 | assertThat(policyFromDb.getPrivateProperties().get("key1")).usingRecursiveComparison().isEqualTo("value1"); |
| 106 | 107 | assertThat(policyFromDb.getPrivateProperties().get("key2")).usingRecursiveComparison().isEqualTo("value2"); |
| 107 | 108 | } |
| 109 | + | |
| 110 | + @Test | |
| 111 | + @DisplayName("Save a single policy that does not exist") | |
| 112 | + void notExisting_with_Profile() { | |
| 113 | + var policy = TestFunctions.createPolicy(getRandomId(), null, List.of("value1")); | |
| 114 | + | |
| 115 | + getPolicyDefinitionStore().create(policy); | |
| 116 | + | |
| 117 | + var policyFromDb = getPolicyDefinitionStore().findById(policy.getId()); | |
| 118 | + assertThat(policy).usingRecursiveComparison().isEqualTo(policyFromDb); | |
| 119 | + | |
| 120 | + assertThat(policyFromDb.getPolicy().getProfiles()).hasSize(1); | |
| 121 | + assertThat(policyFromDb.getPolicy().getProfiles().get(0)).isEqualTo("value1"); | |
| 122 | + } | |
| 123 | + | |
| 108 | 124 | } |
| 109 | 125 | |
| 110 | 126 | @Nested |
| @@ -121,7 +137,7 @@ | ||
| 121 | 137 | } |
| 122 | 138 | |
| 123 | 139 | @Test |
| 124 | - @DisplayName("Update an Policy that exists, adding a property") | |
| 140 | + @DisplayName("Update a Policy that exists, adding a property") | |
| 125 | 141 | void policyExists() { |
| 126 | 142 | var id = getRandomId(); |
| 127 | 143 | var policy = createPolicyDef(id, "target"); |
| @@ -143,7 +159,7 @@ | ||
| 143 | 159 | } |
| 144 | 160 | |
| 145 | 161 | @Test |
| 146 | - @DisplayName("Update an Policy that exists, adding a property") | |
| 162 | + @DisplayName("Update a Policy that exists, adding a property") | |
| 147 | 163 | void policyExists_addProperty() { |
| 148 | 164 | var id = getRandomId(); |
| 149 | 165 | var policy = createPolicyDef(id, "target"); |
| @@ -167,7 +183,7 @@ | ||
| 167 | 183 | } |
| 168 | 184 | |
| 169 | 185 | @Test |
| 170 | - @DisplayName("Update an Policy that exists, remove a property") | |
| 186 | + @DisplayName("Update a Policy that exists, removing a property") | |
| 171 | 187 | void policyExists_removeProperty() { |
| 172 | 188 | var id = getRandomId(); |
| 173 | 189 | var policy = createPolicyDef(id, "target"); |
| @@ -190,7 +206,7 @@ | ||
| 190 | 206 | } |
| 191 | 207 | |
| 192 | 208 | @Test |
| 193 | - @DisplayName("Update an Policy that exists, replace a property") | |
| 209 | + @DisplayName("Update a Policy that exists, replacing a property") | |
| 194 | 210 | void policyExists_replaceProperty() { |
| 195 | 211 | var id = getRandomId(); |
| 196 | 212 | var policy = createPolicyDef(id, "target"); |
| @@ -582,7 +598,7 @@ | ||
| 582 | 598 | } |
| 583 | 599 | |
| 584 | 600 | @Test |
| 585 | - @DisplayName("find all a with property ") | |
| 601 | + @DisplayName("Find all with a property ") | |
| 586 | 602 | void findAll_with_privateProperties() { |
| 587 | 603 | var policy1 = TestFunctions.createPolicy(getRandomId(), null, Map.of("key1", "value1", "key2", "value2")); |
| 588 | 604 | var policy2 = TestFunctions.createPolicy(getRandomId(), null, Map.of("key3", "value3", "key4", "value4")); |