Skip to content

Commit

Permalink
fix compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt committed Oct 21, 2024
1 parent 2a506b4 commit 09eee23
Show file tree
Hide file tree
Showing 31 changed files with 809 additions and 685 deletions.
486 changes: 240 additions & 246 deletions DEPENDENCIES

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,25 @@

package org.eclipse.tractusx.edc.agreements.retirement;

import org.eclipse.edc.connector.controlplane.contract.spi.policy.TransferProcessPolicyContext;
import org.eclipse.edc.connector.controlplane.contract.spi.types.agreement.ContractAgreement;
import org.eclipse.edc.connector.policy.monitor.spi.PolicyMonitorContext;
import org.eclipse.edc.policy.engine.spi.PolicyContext;
import org.eclipse.edc.policy.engine.spi.PolicyValidatorFunction;
import org.eclipse.edc.policy.model.Policy;
import org.eclipse.edc.policy.engine.spi.PolicyValidatorRule;
import org.eclipse.tractusx.edc.agreements.retirement.spi.service.AgreementsRetirementService;


public record AgreementRetirementValidator(AgreementsRetirementService agreementsRetirementService) implements PolicyValidatorFunction {
public record AgreementRetirementValidator(AgreementsRetirementService agreementsRetirementService) {

@Override
public Boolean apply(Policy policy, PolicyContext policyContext) {
var agreement = policyContext.getContextData(ContractAgreement.class);
public PolicyValidatorRule<TransferProcessPolicyContext> transferProcess() {
return (policy, context) -> validate(context.contractAgreement(), context);
}

public PolicyValidatorRule<PolicyMonitorContext> policyMonitor() {
return (policy, context) -> validate(context.contractAgreement(), context);
}

public Boolean validate(ContractAgreement agreement, PolicyContext policyContext) {
if (agreement != null) {
if (agreementsRetirementService.isRetired(agreement.getId())) {
policyContext.reportProblem(String.format("Contract Agreement with ID=%s has been retired", agreement.getId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@

package org.eclipse.tractusx.edc.agreements.retirement;

import org.eclipse.edc.connector.controlplane.contract.spi.policy.TransferProcessPolicyContext;
import org.eclipse.edc.connector.policy.monitor.spi.PolicyMonitorContext;
import org.eclipse.edc.policy.engine.spi.PolicyEngine;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.tractusx.edc.agreements.retirement.spi.service.AgreementsRetirementService;

import static org.eclipse.edc.connector.controlplane.contract.spi.validation.ContractValidationService.TRANSFER_SCOPE;
import static org.eclipse.edc.connector.policy.monitor.PolicyMonitorExtension.POLICY_MONITOR_SCOPE;
import static org.eclipse.tractusx.edc.agreements.retirement.AgreementsRetirementPreValidatorRegisterExtension.NAME;


Expand All @@ -44,8 +44,8 @@ public class AgreementsRetirementPreValidatorRegisterExtension implements Servic

@Override
public void initialize(ServiceExtensionContext context) {
var agreementRetirementValidator = new AgreementRetirementValidator(service);
policyEngine.registerPreValidator(TRANSFER_SCOPE, agreementRetirementValidator);
policyEngine.registerPreValidator(POLICY_MONITOR_SCOPE, agreementRetirementValidator);
var validator = new AgreementRetirementValidator(service);
policyEngine.registerPreValidator(TransferProcessPolicyContext.class, validator.transferProcess());
policyEngine.registerPreValidator(PolicyMonitorContext.class, validator.policyMonitor());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class AgreementRetirementValidatorTest {

private AgreementRetirementValidator validator;
private final AgreementsRetirementService service = mock();
private final Policy policy = mock();
private final PolicyContext context = mock();

@BeforeEach
Expand All @@ -51,24 +50,18 @@ public void setup() {
@Test
@DisplayName("Verify validator returns true if no agreement is found in policyContext")
public void verify_agreementExistsInPolicyContext() {

when(context.getContextData(ContractAgreement.class))
.thenReturn(null);
assertThat(validator.apply(policy, context)).isTrue();

assertThat(validator.validate(null, context)).isTrue();
}

@Test
public void verify_returnFalseWhenRetired() {
var agreementId = "test-agreement";
var agreement = buildAgreement(agreementId);

when(context.getContextData(ContractAgreement.class))
.thenReturn(agreement);
when(service.isRetired(agreementId))
.thenReturn(true);

var result = validator.apply(policy, context);
var result = validator.validate(agreement, context);

assertThat(result).isFalse();
verify(context, times(1)).reportProblem(anyString());
Expand All @@ -79,12 +72,10 @@ public void verify_returnFalseWhenNotRetired() {
var agreementId = "test-agreement";
var agreement = buildAgreement(agreementId);

when(context.getContextData(ContractAgreement.class))
.thenReturn(agreement);
when(service.isRetired(agreementId))
.thenReturn(false);

var result = validator.apply(policy, context);
var result = validator.validate(agreement, context);

assertThat(result).isTrue();
verify(context, never()).reportProblem(anyString());
Expand All @@ -100,4 +91,4 @@ private ContractAgreement buildAgreement(String agreementId) {
.build();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
package org.eclipse.tractusx.edc.agreements.retirement;


import org.eclipse.edc.connector.controlplane.contract.spi.policy.TransferProcessPolicyContext;
import org.eclipse.edc.connector.policy.monitor.spi.PolicyMonitorContext;
import org.eclipse.edc.junit.extensions.DependencyInjectionExtension;
import org.eclipse.edc.policy.engine.spi.PolicyEngine;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.eclipse.edc.connector.controlplane.contract.spi.validation.ContractValidationService.TRANSFER_SCOPE;
import static org.eclipse.edc.connector.policy.monitor.PolicyMonitorExtension.POLICY_MONITOR_SCOPE;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
Expand All @@ -50,8 +50,8 @@ public void verify_functionIsRegisteredOnInitialization(ServiceExtensionContext
extension.initialize(context);

verify(policyEngine, times(1))
.registerPreValidator(eq(TRANSFER_SCOPE), any());
.registerPreValidator(eq(TransferProcessPolicyContext.class), any());
verify(policyEngine, times(1))
.registerPreValidator(eq(POLICY_MONITOR_SCOPE), any());
.registerPreValidator(eq(PolicyMonitorContext.class), any());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ dependencies {
api(project(":edc-extensions:bpn-validation:bpn-validation-spi"))
implementation(project(":spi:core-spi"))
api(libs.edc.spi.core)
implementation(libs.edc.spi.policy)
implementation(libs.edc.spi.catalog)
implementation(libs.edc.spi.contract)
implementation(libs.edc.spi.policy)
implementation(libs.edc.spi.policyengine)

testImplementation(libs.edc.junit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

package org.eclipse.tractusx.edc.validation.businesspartner;

import org.eclipse.edc.connector.controlplane.catalog.spi.policy.CatalogPolicyContext;
import org.eclipse.edc.connector.controlplane.contract.spi.policy.ContractNegotiationPolicyContext;
import org.eclipse.edc.connector.controlplane.contract.spi.policy.TransferProcessPolicyContext;
import org.eclipse.edc.policy.engine.spi.AtomicConstraintRuleFunction;
import org.eclipse.edc.policy.engine.spi.PolicyContext;
import org.eclipse.edc.policy.engine.spi.PolicyEngine;
import org.eclipse.edc.policy.engine.spi.RuleBindingRegistry;
import org.eclipse.edc.policy.model.Permission;
Expand All @@ -28,12 +33,12 @@
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.tractusx.edc.validation.businesspartner.functions.BusinessPartnerNumberPermissionFunction;

import static org.eclipse.edc.connector.controlplane.contract.spi.validation.ContractValidationService.NEGOTIATION_SCOPE;
import static org.eclipse.edc.connector.controlplane.contract.spi.validation.ContractValidationService.TRANSFER_SCOPE;
import static org.eclipse.edc.connector.controlplane.catalog.spi.policy.CatalogPolicyContext.CATALOG_SCOPE;
import static org.eclipse.edc.connector.controlplane.contract.spi.policy.ContractNegotiationPolicyContext.NEGOTIATION_SCOPE;
import static org.eclipse.edc.connector.controlplane.contract.spi.policy.TransferProcessPolicyContext.TRANSFER_SCOPE;
import static org.eclipse.edc.policy.model.OdrlNamespace.ODRL_SCHEMA;
import static org.eclipse.tractusx.edc.edr.spi.CoreConstants.TX_NAMESPACE;
import static org.eclipse.tractusx.edc.validation.businesspartner.BusinessPartnerNumberValidationExtension.NAME;
import static org.eclipse.tractusx.edc.validation.businesspartner.BusinessPartnerValidationExtension.CATALOGING_SCOPE;

/**
* Business partner number evaluation function.
Expand Down Expand Up @@ -70,23 +75,21 @@ public String name() {

@Override
public void initialize(ServiceExtensionContext context) {
var function = new BusinessPartnerNumberPermissionFunction();

var permissionFunction = new BusinessPartnerNumberPermissionFunction();

bindToScope(permissionFunction, TRANSFER_SCOPE);
bindToScope(permissionFunction, NEGOTIATION_SCOPE);
bindToScope(permissionFunction, CATALOGING_SCOPE);

bindToScope(TransferProcessPolicyContext.class, function.transferProcess(), TRANSFER_SCOPE);
bindToScope(ContractNegotiationPolicyContext.class, function.contractNegotiation(), NEGOTIATION_SCOPE);
bindToScope(CatalogPolicyContext.class, function.catalog(), CATALOG_SCOPE);
}

private void bindToScope(BusinessPartnerNumberPermissionFunction permissionFunction, String scope) {
private <C extends PolicyContext> void bindToScope(Class<C> contextType, AtomicConstraintRuleFunction<Permission, C> function, String scope) {
ruleBindingRegistry.bind("USE", scope);
ruleBindingRegistry.bind(ODRL_SCHEMA + "use", scope);
ruleBindingRegistry.bind(BUSINESS_PARTNER_CONSTRAINT_KEY, scope);
ruleBindingRegistry.bind(TX_BUSINESS_PARTNER_CONSTRAINT_KEY, scope);

policyEngine.registerFunction(scope, Permission.class, BUSINESS_PARTNER_CONSTRAINT_KEY, permissionFunction);
policyEngine.registerFunction(scope, Permission.class, TX_BUSINESS_PARTNER_CONSTRAINT_KEY, permissionFunction);
policyEngine.registerFunction(contextType, Permission.class, BUSINESS_PARTNER_CONSTRAINT_KEY, function);
policyEngine.registerFunction(contextType, Permission.class, TX_BUSINESS_PARTNER_CONSTRAINT_KEY, function);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

package org.eclipse.tractusx.edc.validation.businesspartner;

import org.eclipse.edc.connector.controlplane.catalog.spi.policy.CatalogPolicyContext;
import org.eclipse.edc.connector.controlplane.contract.spi.policy.ContractNegotiationPolicyContext;
import org.eclipse.edc.connector.controlplane.contract.spi.policy.TransferProcessPolicyContext;
import org.eclipse.edc.policy.engine.spi.AtomicConstraintRuleFunction;
import org.eclipse.edc.policy.engine.spi.PolicyContext;
import org.eclipse.edc.policy.engine.spi.PolicyEngine;
import org.eclipse.edc.policy.engine.spi.RuleBindingRegistry;
import org.eclipse.edc.policy.model.Permission;
Expand All @@ -29,9 +34,11 @@
import org.eclipse.tractusx.edc.validation.businesspartner.functions.BusinessPartnerGroupFunction;
import org.eclipse.tractusx.edc.validation.businesspartner.spi.BusinessPartnerStore;

import static org.eclipse.edc.connector.controlplane.contract.spi.validation.ContractValidationService.NEGOTIATION_SCOPE;
import static org.eclipse.edc.connector.controlplane.contract.spi.validation.ContractValidationService.TRANSFER_SCOPE;
import static org.eclipse.edc.connector.controlplane.catalog.spi.policy.CatalogPolicyContext.CATALOG_SCOPE;
import static org.eclipse.edc.connector.controlplane.contract.spi.policy.ContractNegotiationPolicyContext.NEGOTIATION_SCOPE;
import static org.eclipse.edc.connector.controlplane.contract.spi.policy.TransferProcessPolicyContext.TRANSFER_SCOPE;
import static org.eclipse.edc.policy.model.OdrlNamespace.ODRL_SCHEMA;
import static org.eclipse.tractusx.edc.validation.businesspartner.functions.BusinessPartnerGroupFunction.BUSINESS_PARTNER_CONSTRAINT_KEY;

/**
* Registers a {@link org.eclipse.tractusx.edc.validation.businesspartner.functions.BusinessPartnerGroupFunction} for the following scopes:
Expand All @@ -57,8 +64,6 @@
@Extension(value = "Registers a function to evaluate whether a BPN number is covered by a certain policy or not", categories = { "policy", "contract" })
public class BusinessPartnerValidationExtension implements ServiceExtension {

public static final String CATALOGING_SCOPE = "catalog";

private static final String USE = "USE";

@Inject
Expand All @@ -72,16 +77,17 @@ public class BusinessPartnerValidationExtension implements ServiceExtension {
public void initialize(ServiceExtensionContext context) {
var function = new BusinessPartnerGroupFunction(store);

bindToScope(function, TRANSFER_SCOPE);
bindToScope(function, NEGOTIATION_SCOPE);
bindToScope(function, CATALOGING_SCOPE);
bindToScope(TRANSFER_SCOPE, TransferProcessPolicyContext.class, function.transferProcess());
bindToScope(NEGOTIATION_SCOPE, ContractNegotiationPolicyContext.class, function.contractNegotiation());
bindToScope(CATALOG_SCOPE, CatalogPolicyContext.class, function.catalog());
}

private void bindToScope(BusinessPartnerGroupFunction function, String scope) {
private <C extends PolicyContext> void bindToScope(String scope, Class<C> contextType, AtomicConstraintRuleFunction<Permission, C> function) {
ruleBindingRegistry.bind(USE, scope);
ruleBindingRegistry.bind(ODRL_SCHEMA + "use", scope);
ruleBindingRegistry.bind(BusinessPartnerGroupFunction.BUSINESS_PARTNER_CONSTRAINT_KEY, scope);
ruleBindingRegistry.bind(BUSINESS_PARTNER_CONSTRAINT_KEY, scope);

policyEngine.registerFunction(scope, Permission.class, BusinessPartnerGroupFunction.BUSINESS_PARTNER_CONSTRAINT_KEY, function);
policyEngine.registerFunction(contextType, Permission.class, BUSINESS_PARTNER_CONSTRAINT_KEY, function);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

package org.eclipse.tractusx.edc.validation.businesspartner.functions;

import org.eclipse.edc.policy.engine.spi.AtomicConstraintFunction;
import org.eclipse.edc.connector.controlplane.catalog.spi.policy.CatalogPolicyContext;
import org.eclipse.edc.connector.controlplane.contract.spi.policy.ContractNegotiationPolicyContext;
import org.eclipse.edc.connector.controlplane.contract.spi.policy.TransferProcessPolicyContext;
import org.eclipse.edc.policy.engine.spi.AtomicConstraintRuleFunction;
import org.eclipse.edc.policy.engine.spi.PolicyContext;
import org.eclipse.edc.policy.model.Operator;
import org.eclipse.edc.policy.model.Permission;
Expand All @@ -31,6 +34,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
Expand Down Expand Up @@ -77,7 +81,7 @@
*
* @see BusinessPartnerStore
*/
public class BusinessPartnerGroupFunction implements AtomicConstraintFunction<Permission> {
public class BusinessPartnerGroupFunction {
public static final String BUSINESS_PARTNER_CONSTRAINT_KEY = TX_NAMESPACE + "BusinessPartnerGroup";
private static final List<Operator> ALLOWED_OPERATORS = List.of(EQ, NEQ, IN, IS_ALL_OF, IS_ANY_OF, IS_NONE_OF);
private static final Map<Operator, Function<BpnGroupHolder, Boolean>> OPERATOR_EVALUATOR_MAP = new HashMap<>();
Expand All @@ -93,6 +97,21 @@ public BusinessPartnerGroupFunction(BusinessPartnerStore store) {
OPERATOR_EVALUATOR_MAP.put(IS_NONE_OF, this::evaluateIsNoneOf);
}

public AtomicConstraintRuleFunction<Permission, TransferProcessPolicyContext> transferProcess() {
return (operator, rightValue, permission, context) ->
evaluate(operator, rightValue, context.agent(), context);
}

public AtomicConstraintRuleFunction<Permission, ContractNegotiationPolicyContext> contractNegotiation() {
return (operator, rightValue, permission, context) ->
evaluate(operator, rightValue, context.agent(), context);
}

public AtomicConstraintRuleFunction<Permission, CatalogPolicyContext> catalog() {
return (operator, rightValue, permission, context) ->
evaluate(operator, rightValue, context.agent(), context);
}

/**
* Policy evaluation function that checks whether a given BusinessPartnerNumber is covered by a given policy.
* The evaluation is prematurely aborted (returns {@code false}) if:
Expand All @@ -103,10 +122,7 @@ public BusinessPartnerGroupFunction(BusinessPartnerStore store) {
* <li>The right value is anything other than {@link String} or {@link Collection}</li>
* </ul>
*/
@Override
public boolean evaluate(Operator operator, Object rightValue, Permission rule, PolicyContext policyContext) {
var participantAgent = policyContext.getContextData(ParticipantAgent.class);

public boolean evaluate(Operator operator, Object rightValue, ParticipantAgent participantAgent, PolicyContext policyContext) {
// No participant agent found in context
if (participantAgent == null) {
policyContext.reportProblem("ParticipantAgent not found on PolicyContext");
Expand Down Expand Up @@ -153,11 +169,11 @@ private List<String> parseRightOperand(Object rightValue, PolicyContext context)
context.reportProblem(format("Right operand expected to be either String or a Collection, but was %s", rightValue.getClass()));
return null;
}

private Boolean evaluateIn(BpnGroupHolder bpnGroupHolder) {
var assigned = bpnGroupHolder.assignedGroups;
// checks whether both lists overlap
return bpnGroupHolder.allowedGroups.containsAll(assigned);
return new HashSet<>(bpnGroupHolder.allowedGroups).containsAll(assigned);
}

private Boolean evaluateNotEquals(BpnGroupHolder bpnGroupHolder) {
Expand Down Expand Up @@ -189,4 +205,5 @@ private boolean evaluateIsNoneOf(BpnGroupHolder bpnGroupHolder) {
*/
private record BpnGroupHolder(List<String> assignedGroups, List<String> allowedGroups) {
}

}
Loading

0 comments on commit 09eee23

Please sign in to comment.