Skip to content

Commit

Permalink
Merge branch 'feature/441281_policies_visualization' into 'develop'
Browse files Browse the repository at this point in the history
feature/441281_policies_visualization

See merge request upm-inesdata/inesdata-connector!35
  • Loading branch information
ralconada-gmv committed Sep 12, 2024
2 parents b69f6e4 + 21dd1e0 commit 2712fd3
Show file tree
Hide file tree
Showing 12 changed files with 989 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import jakarta.json.JsonObject;
import org.eclipse.edc.api.model.ApiCoreSchema;
import org.upm.inesdata.complexpolicy.model.PolicyDefinitionCreateDto;
import org.upm.inesdata.complexpolicy.model.PolicyDefinitionDto;

import java.util.List;

@OpenAPIDefinition(
info = @Info(
Expand Down Expand Up @@ -63,4 +66,34 @@ public interface ComplexPolicyDefinitionApi {
)
JsonObject createPolicyDefinitionV3(PolicyDefinitionCreateDto var1);

@Operation(
description = "Creates a new policy definition",
requestBody = @RequestBody(
content = {@Content(
schema = @Schema(
)
)}
),
responses = {@ApiResponse(
responseCode = "200",
description = "Returns the Policy Definitions",
content = {@Content(
schema = @Schema(
implementation = ApiCoreSchema.IdResponseSchema.class
)
)}
), @ApiResponse(
responseCode = "400",
description = "Request body was malformed",
content = {@Content(
array = @ArraySchema(
schema = @Schema(
implementation = ApiCoreSchema.ApiErrorDetailSchema.class
)
)
)}
)}
)
List<PolicyDefinitionDto> getPolicyDefinitions(JsonObject querySpecJson);

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@
import org.eclipse.edc.connector.controlplane.services.spi.policydefinition.PolicyDefinitionService;
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.query.QuerySpec;
import org.eclipse.edc.transform.spi.TypeTransformerRegistry;
import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry;
import org.eclipse.edc.web.spi.exception.InvalidRequestException;
import org.eclipse.edc.web.spi.exception.ValidationFailureException;
import org.upm.inesdata.complexpolicy.mapper.PolicyMapper;
import org.upm.inesdata.complexpolicy.model.PolicyDefinitionCreateDto;
import org.upm.inesdata.complexpolicy.model.PolicyDefinitionDto;
import org.upm.inesdata.complexpolicy.model.UiPolicyExpression;

import java.util.Comparator;
import java.util.List;

import static java.lang.String.format;
import static org.eclipse.edc.spi.query.QuerySpec.EDC_QUERY_SPEC_TYPE;
import static org.eclipse.edc.web.spi.exception.ServiceResultHandler.exceptionMapper;

@Consumes({"application/json"})
Expand Down Expand Up @@ -59,11 +66,38 @@ public JsonObject createPolicyDefinitionV3(PolicyDefinitionCreateDto request) {
.orElseThrow(f -> new EdcException("Error creating response body: " + f.getFailureDetail()));
}


public PolicyDefinition buildPolicyDefinition(String id, UiPolicyExpression uiPolicyExpression) {
var policy = policyMapper.buildPolicy(uiPolicyExpression);
return PolicyDefinition.Builder.newInstance()
.id(id)
.policy(policy)
.build();
}



@POST
@Path("request")
public List<PolicyDefinitionDto> getPolicyDefinitions(JsonObject querySpecJson) {
QuerySpec querySpec;
if (querySpecJson == null) {
querySpec = QuerySpec.Builder.newInstance().build();
} else {
validator.validate(EDC_QUERY_SPEC_TYPE, querySpecJson).orElseThrow(ValidationFailureException::new);

querySpec = transformerRegistry.transform(querySpecJson, QuerySpec.class)
.orElseThrow(InvalidRequestException::new);
}

var policyDefinitions = getAllPolicyDefinitions(querySpec);
return policyDefinitions.stream()
.sorted(Comparator.comparing(PolicyDefinition::getCreatedAt).reversed())
.map(policyMapper::buildPolicyDefinitionDto)
.toList();
}

private List<PolicyDefinition> getAllPolicyDefinitions(QuerySpec querySpec) {
return service.search(querySpec).orElseThrow(f -> new EdcException("Error getting policy definitions: " + f.getFailureDetail()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

import jakarta.json.JsonObject;
import lombok.RequiredArgsConstructor;
import org.eclipse.edc.connector.controlplane.policy.spi.PolicyDefinition;
import org.eclipse.edc.policy.model.Action;
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.transform.spi.TypeTransformerRegistry;
import org.upm.inesdata.complexpolicy.exception.FailedMappingException;
import org.upm.inesdata.complexpolicy.model.PolicyDefinitionDto;
import org.upm.inesdata.complexpolicy.model.UiPolicy;
import org.upm.inesdata.complexpolicy.model.UiPolicyExpression;
import org.upm.inesdata.complexpolicy.utils.JsonUtils;
Expand Down Expand Up @@ -101,4 +103,20 @@ public JsonObject buildPolicyJsonLd(Policy policy) {
return typeTransformerRegistry.transform(policy, JsonObject.class)
.orElseThrow(FailedMappingException::ofFailure);
}

/**
* Builds a simplified policy definition DTO from a policy definition
* <p>
* This operation is lossy.
*
* @param policyDefinition policy definition
* @return ui policy
*/
public PolicyDefinitionDto buildPolicyDefinitionDto(PolicyDefinition policyDefinition) {
var policy = buildUiPolicy(policyDefinition.getPolicy());
return PolicyDefinitionDto.builder()
.policyDefinitionId(policyDefinition.getId())
.policy(policy)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.upm.inesdata.complexpolicy.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;

@Data
@AllArgsConstructor
@RequiredArgsConstructor
@Builder(toBuilder = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@Schema(description = "Policy Definition as required for the Policy Definition Page")
public class PolicyDefinitionDto {
@Schema(description = "Policy Definition ID", requiredMode = Schema.RequiredMode.REQUIRED)
private String policyDefinitionId;

@Schema(description = "Policy Contents", requiredMode = Schema.RequiredMode.REQUIRED)
private UiPolicy policy;
}
6 changes: 6 additions & 0 deletions extensions/federated-catalog-cache-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {

dependencies {
api(project(":spi:federated-catalog-cache-spi"))
api(project(":extensions:complex-policy-api"))
api(libs.edc.spi.core)
implementation(libs.edc.spi.transform)
implementation(libs.edc.web.spi)
Expand All @@ -26,5 +27,10 @@ dependencies {
implementation(libs.edc.federated.catalog.api)
implementation(libs.edc.federated.catalog.core)
implementation(libs.edc.federated.catalog.spi)
annotationProcessor(libs.lombok)
compileOnly(libs.lombok)
testAnnotationProcessor(libs.lombok)
testCompileOnly(libs.lombok)
api(libs.edc.policy.definition.api)

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.upm.inesdata.federated;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.Json;
import org.eclipse.edc.connector.api.management.configuration.ManagementApiConfigurationExtension;
import org.eclipse.edc.connector.controlplane.transform.edc.from.JsonObjectFromAssetTransformer;
import org.eclipse.edc.connector.controlplane.transform.edc.to.JsonObjectToAssetTransformer;
import org.eclipse.edc.connector.controlplane.transform.odrl.OdrlTransformersFactory;
import org.eclipse.edc.connector.controlplane.transform.odrl.to.JsonObjectToPolicyTransformer;
import org.eclipse.edc.connector.core.agent.NoOpParticipantIdMapper;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
Expand All @@ -17,8 +20,16 @@
import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry;
import org.eclipse.edc.web.spi.WebService;
import org.eclipse.edc.web.spi.configuration.ApiContext;
import org.upm.inesdata.complexpolicy.mapper.AtomicConstraintMapper;
import org.upm.inesdata.complexpolicy.mapper.ExpressionExtractor;
import org.upm.inesdata.complexpolicy.mapper.ExpressionMapper;
import org.upm.inesdata.complexpolicy.mapper.LiteralMapper;
import org.upm.inesdata.complexpolicy.mapper.OperatorMapper;
import org.upm.inesdata.complexpolicy.mapper.PolicyMapper;
import org.upm.inesdata.complexpolicy.mapper.PolicyValidator;
import org.upm.inesdata.federated.controller.FederatedCatalogCacheApiController;
import org.upm.inesdata.federated.service.FederatedCatalogCacheServiceImpl;
import org.upm.inesdata.federated.transformer.JsonObjectFromUiContractOfferTransformer;
import org.upm.inesdata.spi.federated.FederatedCatalogCacheService;
import org.upm.inesdata.spi.federated.index.PaginatedFederatedCacheStoreIndex;

Expand Down Expand Up @@ -81,8 +92,18 @@ public void initialize(ServiceExtensionContext context) {
managementApiTransformerRegistry.register(new JsonObjectFromAssetTransformer(factory, jsonLdMapper));
managementApiTransformerRegistry.register(new JsonObjectToAssetTransformer());

var participantIdMapper = new NoOpParticipantIdMapper();
managementApiTransformerRegistry.register(new JsonObjectToPolicyTransformer(participantIdMapper));
managementApiTransformerRegistry.register(new JsonObjectFromUiContractOfferTransformer(jsonLdMapper, factory));
OdrlTransformersFactory.jsonObjectToOdrlTransformers(participantIdMapper).forEach(managementApiTransformerRegistry::register);

ExpressionMapper expressionMapper = new ExpressionMapper(
new AtomicConstraintMapper(new LiteralMapper(new ObjectMapper()), new OperatorMapper()));
ExpressionExtractor expressionExtractor = new ExpressionExtractor(new PolicyValidator(), expressionMapper);
PolicyMapper policyMapper = new PolicyMapper(expressionExtractor, expressionMapper, managementApiTransformerRegistry);

var federatedCatalogCacheApiController = new FederatedCatalogCacheApiController(this.federatedCatalogCacheService(), managementApiTransformerRegistry,
validator,monitor);
validator, monitor, policyMapper);
webService.registerResource(ApiContext.MANAGEMENT, federatedCatalogCacheApiController);
}
}
Loading

0 comments on commit 2712fd3

Please sign in to comment.