Skip to content

Commit

Permalink
PR remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt committed Oct 17, 2024
1 parent 246d235 commit 630cab3
Show file tree
Hide file tree
Showing 19 changed files with 178 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import org.eclipse.edc.policy.engine.spi.DynamicAtomicConstraintRuleFunction;
import org.eclipse.edc.policy.engine.spi.PolicyContext;
import org.eclipse.edc.policy.engine.spi.PolicyEngine;
import org.eclipse.edc.policy.engine.spi.PolicyRuleFunction;
import org.eclipse.edc.policy.engine.spi.PolicyValidatorFunction;
import org.eclipse.edc.policy.engine.spi.PolicyValidatorRule;
import org.eclipse.edc.policy.engine.spi.RulePolicyFunction;
import org.eclipse.edc.policy.engine.spi.plan.PolicyEvaluationPlan;
import org.eclipse.edc.policy.engine.validation.PolicyValidator;
import org.eclipse.edc.policy.engine.validation.RuleValidator;
Expand Down Expand Up @@ -101,13 +101,13 @@ public <C extends PolicyContext> Result<Void> evaluate(Policy policy, C context)
.forEach(entry -> {
if (Duty.class.isAssignableFrom(entry.type)) {
evalBuilder.dutyRuleFunction((rule) ->
((RulePolicyFunction<Rule, C>) entry.function).evaluate(rule, context));
((PolicyRuleFunction<Rule, C>) entry.function).evaluate(rule, context));
} else if (Permission.class.isAssignableFrom(entry.type)) {
evalBuilder.permissionRuleFunction((rule) ->
((RulePolicyFunction<Rule, C>) entry.function).evaluate(rule, context));
((PolicyRuleFunction<Rule, C>) entry.function).evaluate(rule, context));
} else if (Prohibition.class.isAssignableFrom(entry.type)) {
evalBuilder.prohibitionRuleFunction((rule) ->
((RulePolicyFunction<Rule, C>) entry.function).evaluate(rule, context));
((PolicyRuleFunction<Rule, C>) entry.function).evaluate(rule, context));
}
});

Expand Down Expand Up @@ -224,13 +224,13 @@ public <R extends Rule, C extends PolicyContext> void registerFunction(String sc
}

@Override
public <R extends Rule, C extends PolicyContext> void registerFunction(Class<C> contextType, Class<R> type, RulePolicyFunction<R, C> function) {
public <R extends Rule, C extends PolicyContext> void registerFunction(Class<C> contextType, Class<R> type, PolicyRuleFunction<R, C> function) {
ruleFunctions.add(new RuleFunctionEntry(contextType, type, function));
}

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public <R extends Rule, C extends PolicyContext> void registerFunction(String scope, Class<R> type, RulePolicyFunction<R, C> function) {
public <R extends Rule, C extends PolicyContext> void registerFunction(String scope, Class<R> type, PolicyRuleFunction<R, C> function) {
ruleFunctions.add(new RuleFunctionEntry(contextType(scope), type, function));
}

Expand Down Expand Up @@ -285,7 +285,7 @@ private record DynamicConstraintFunctionEntry<R extends Rule, C extends PolicyCo
private record RuleFunctionEntry<R extends Rule, C extends PolicyContext>(
Class<C> contextType,
Class<R> type,
RulePolicyFunction<R, C> function
PolicyRuleFunction<R, C> function
) implements FunctionEntry<C> { }

private record ValidatorRuleEntry<C extends PolicyContext>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import org.eclipse.edc.policy.engine.spi.AtomicConstraintRuleFunction;
import org.eclipse.edc.policy.engine.spi.DynamicAtomicConstraintRuleFunction;
import org.eclipse.edc.policy.engine.spi.PolicyContext;
import org.eclipse.edc.policy.engine.spi.PolicyRuleFunction;
import org.eclipse.edc.policy.engine.spi.PolicyValidatorRule;
import org.eclipse.edc.policy.engine.spi.RulePolicyFunction;
import org.eclipse.edc.policy.engine.spi.plan.PolicyEvaluationPlan;
import org.eclipse.edc.policy.engine.spi.plan.step.AndConstraintStep;
import org.eclipse.edc.policy.engine.spi.plan.step.AtomicConstraintStep;
Expand Down Expand Up @@ -224,7 +224,7 @@ private record DynamicAtomicConstraintFunctionEntry<R extends Rule, C extends Po

private record RuleFunctionFunctionEntry<R extends Rule, C extends PolicyContext>(
Class<R> type,
RulePolicyFunction<R, C> function) {
PolicyRuleFunction<R, C> function) {
}

public static class Builder {
Expand Down Expand Up @@ -268,7 +268,7 @@ public <R extends Rule, C extends PolicyContext> Builder evaluationFunction(Clas
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public <R extends Rule, C extends PolicyContext> Builder evaluationFunction(Class<R> ruleKind, RulePolicyFunction<R, C> function) {
public <R extends Rule, C extends PolicyContext> Builder evaluationFunction(Class<R> ruleKind, PolicyRuleFunction<R, C> function) {
planner.ruleFunctions.add(new RuleFunctionFunctionEntry(ruleKind, function));
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import org.eclipse.edc.policy.engine.spi.PolicyContext;
import org.eclipse.edc.policy.engine.spi.PolicyContextImpl;
import org.eclipse.edc.policy.engine.spi.PolicyEngine;
import org.eclipse.edc.policy.engine.spi.PolicyRuleFunction;
import org.eclipse.edc.policy.engine.spi.PolicyValidatorRule;
import org.eclipse.edc.policy.engine.spi.RuleBindingRegistry;
import org.eclipse.edc.policy.engine.spi.RulePolicyFunction;
import org.eclipse.edc.policy.engine.spi.plan.PolicyEvaluationPlan;
import org.eclipse.edc.policy.engine.spi.plan.step.AtomicConstraintStep;
import org.eclipse.edc.policy.engine.spi.plan.step.MultiplicityConstraintStep;
Expand Down Expand Up @@ -159,7 +159,7 @@ public boolean canHandle(Object leftValue) {
@ArgumentsSource(SimplePolicyProvider.class)
void withRuleAndRuleFunction(Policy policy, Class<Rule> ruleClass, String action, String key, Function<PolicyEvaluationPlan, List<RuleStep<? extends Rule>>> stepsProvider) {

RulePolicyFunction<Rule, TestContext> function = mock();
PolicyRuleFunction<Rule, TestContext> function = mock();

bindingRegistry.bind(action, TEST_SCOPE);
bindingRegistry.bind(key, TEST_SCOPE);
Expand Down Expand Up @@ -192,7 +192,7 @@ void withRuleAndRuleFunctionNotBound(Policy policy, Class<Rule> ruleClass, Strin
bindingRegistry.bind(action, TEST_SCOPE);
bindingRegistry.bind(key, TEST_SCOPE);

RulePolicyFunction<Rule, UnboundedContext> function = mock();
PolicyRuleFunction<Rule, UnboundedContext> function = mock();
policyEngine.registerFunction(UnboundedContext.class, ruleClass, function);

var plan = policyEngine.createEvaluationPlan(TEST_SCOPE, policy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import org.eclipse.edc.policy.engine.spi.PolicyContext;
import org.eclipse.edc.policy.engine.spi.PolicyContextImpl;
import org.eclipse.edc.policy.engine.spi.PolicyEngine;
import org.eclipse.edc.policy.engine.spi.PolicyRuleFunction;
import org.eclipse.edc.policy.engine.spi.PolicyValidatorRule;
import org.eclipse.edc.policy.engine.spi.RuleBindingRegistry;
import org.eclipse.edc.policy.engine.spi.RulePolicyFunction;
import org.eclipse.edc.policy.engine.validation.RuleValidator;
import org.eclipse.edc.policy.model.Action;
import org.eclipse.edc.policy.model.AtomicConstraint;
Expand Down Expand Up @@ -467,7 +467,7 @@ void shouldUseTypedContextOnRuleFunction() {
var duty = Duty.Builder.newInstance().constraint(constraint).build();
var policy = Policy.Builder.newInstance().duty(duty).build();

RulePolicyFunction<Duty, TestContext> function = mock();
PolicyRuleFunction<Duty, TestContext> function = mock();

policyEngine.registerFunction(TestContext.class, Duty.class, function);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.eclipse.edc.connector.controlplane.contract.listener.ContractNegotiationEventListener;
import org.eclipse.edc.connector.controlplane.contract.negotiation.ConsumerContractNegotiationManagerImpl;
import org.eclipse.edc.connector.controlplane.contract.negotiation.ProviderContractNegotiationManagerImpl;
import org.eclipse.edc.connector.controlplane.contract.policy.ContractExpiryFunction;
import org.eclipse.edc.connector.controlplane.contract.policy.PolicyEquality;
import org.eclipse.edc.connector.controlplane.contract.spi.negotiation.ConsumerContractNegotiationManager;
import org.eclipse.edc.connector.controlplane.contract.spi.negotiation.ContractNegotiationPendingGuard;
Expand All @@ -35,6 +34,7 @@
import org.eclipse.edc.connector.controlplane.contract.spi.types.negotiation.ContractNegotiation;
import org.eclipse.edc.connector.controlplane.contract.spi.validation.ContractValidationService;
import org.eclipse.edc.connector.controlplane.contract.validation.ContractValidationServiceImpl;
import org.eclipse.edc.connector.controlplane.policy.contract.ContractExpiryCheckFunction;
import org.eclipse.edc.connector.controlplane.policy.spi.store.PolicyDefinitionStore;
import org.eclipse.edc.policy.engine.spi.PolicyEngine;
import org.eclipse.edc.policy.engine.spi.RuleBindingRegistry;
Expand Down Expand Up @@ -62,7 +62,7 @@
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.connector.controlplane.policy.contract.ContractExpiryCheck.CONTRACT_EXPIRY_EVALUATION_KEY;
import static org.eclipse.edc.connector.controlplane.policy.contract.ContractExpiryCheckFunction.CONTRACT_EXPIRY_EVALUATION_KEY;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_USE_ACTION_ATTRIBUTE;
import static org.eclipse.edc.statemachine.AbstractStateEntityManager.DEFAULT_BATCH_SIZE;
import static org.eclipse.edc.statemachine.AbstractStateEntityManager.DEFAULT_ITERATION_WAIT;
Expand Down Expand Up @@ -190,8 +190,9 @@ private void registerServices(ServiceExtensionContext context) {
// bind/register rule to evaluate contract expiry
ruleBindingRegistry.bind(ODRL_USE_ACTION_ATTRIBUTE, TRANSFER_SCOPE);
ruleBindingRegistry.bind(CONTRACT_EXPIRY_EVALUATION_KEY, TRANSFER_SCOPE);
var function = new ContractExpiryFunction();
policyEngine.registerFunction(TransferProcessPolicyContext.class, Permission.class, CONTRACT_EXPIRY_EVALUATION_KEY, function);

policyEngine.registerFunction(TransferProcessPolicyContext.class, Permission.class, CONTRACT_EXPIRY_EVALUATION_KEY,
new ContractExpiryCheckFunction<>());

var iterationWaitMillis = context.getSetting(NEGOTIATION_STATE_MACHINE_ITERATION_WAIT_MILLIS, DEFAULT_ITERATION_WAIT);
var waitStrategy = context.hasService(NegotiationWaitStrategy.class) ? context.getService(NegotiationWaitStrategy.class) : new ExponentialWaitStrategy(iterationWaitMillis);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* Copyright (c) 2024 Cofinity-X
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand All @@ -8,14 +8,16 @@
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
* Cofinity-X - initial API and implementation
*
*/

package org.eclipse.edc.connector.controlplane.policy.contract;

import org.eclipse.edc.connector.controlplane.contract.spi.types.agreement.ContractAgreement;
import org.eclipse.edc.connector.controlplane.contract.spi.policy.AgreementPolicyContext;
import org.eclipse.edc.policy.engine.spi.AtomicConstraintRuleFunction;
import org.eclipse.edc.policy.model.Operator;
import org.eclipse.edc.policy.model.Permission;
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.result.Result;

Expand Down Expand Up @@ -60,38 +62,42 @@
* </ul>
* Please note that all {@link Operator}s except {@link Operator#IN} are supported.
*/
public class ContractExpiryCheck {
public class ContractExpiryCheckFunction<C extends AgreementPolicyContext> implements AtomicConstraintRuleFunction<Permission, C> {

public static final String CONTRACT_EXPIRY_EVALUATION_KEY = EDC_NAMESPACE + "inForceDate";

private static final String EXPRESSION_REGEX = "(contract[A,a]greement)\\+(-?[0-9]+)(s|m|h|d)";
private static final int REGEX_GROUP_NUMERIC = 2;
private static final int REGEX_GROUP_UNIT = 3;

public Result<Void> evaluate(Operator operator, Object rightValue, Instant now, ContractAgreement agreement) {
@Override
public boolean evaluate(Operator operator, Object rightValue, Permission rule, C context) {
if (rightValue == null) {
return Result.failure("Right-value is null.");
context.reportProblem("Right-value is null.");
return false;
}

if (!(rightValue instanceof String rightValueStr)) {
return Result.failure("Right-value expected to be String but was " + rightValue.getClass());
context.reportProblem("Right-value expected to be String but was " + rightValue.getClass());
return false;
}

return Optional.ofNullable(asInstant(rightValueStr))
.or(() -> Optional.ofNullable(asDuration(rightValueStr))
.map(duration -> Instant.ofEpochSecond(agreement.getContractSigningDate())
.map(duration -> Instant.ofEpochSecond(context.contractAgreement().getContractSigningDate())
.plus(duration)
)
).map(bound -> checkFixedPeriod(now, operator, bound))
.map(it -> it ? Result.success() : Result.<Void>failure(""))
).map(bound -> checkFixedPeriod(context.now(), operator, bound))
.orElseGet(() -> {
var message = "Unsupported right-value, expected either an ISO-8061 String or a expression matching '%s', but got '%s'"
.formatted(CONTRACT_EXPIRY_EVALUATION_KEY, rightValueStr);
return Result.failure(message);
.formatted(ContractExpiryCheckFunction.CONTRACT_EXPIRY_EVALUATION_KEY, rightValueStr);
context.reportProblem(message);
return false;
});
}

/**
* Checks whether an input string fits the regex {@link ContractExpiryCheck#EXPRESSION_REGEX}, e.g. "contractAgreement+50m"
* Checks whether an input string fits the regex {@link #EXPRESSION_REGEX}, e.g. "contractAgreement+50m"
* and parses that string into a {@link Duration} if successful.
*
* @param rightValueStr A string potentially containing a duration expression.
Expand Down Expand Up @@ -145,5 +151,4 @@ private Instant asInstant(String isoString) {
return null;
}
}

}
Loading

0 comments on commit 630cab3

Please sign in to comment.