Changesets · Branches · Tags · Compare
| Changeset | b66ccbc58002a2d2b7dcebe084c9ce91b2e807b7ee445211a227c1de3c9b2044 |
|---|---|
| Author | Enrico Risa <enrico.risa@gmail.com> · 2024-08-30 09:53 +02:00 |
| Committer | GitHub <noreply@github.com> · 2024-08-30 09:53 +02:00 |
| Parents | 3bbf4329e9fc |
18 files changed
modified core/common/lib/policy-engine-lib/src/main/java/org/eclipse/edc/policy/engine/PolicyEngineImpl.java +30 −5
| @@ -14,11 +14,13 @@ | ||
| 14 | 14 | |
| 15 | 15 | package org.eclipse.edc.policy.engine; |
| 16 | 16 | |
| 17 | +import org.eclipse.edc.policy.engine.plan.PolicyEvaluationPlanner; | |
| 17 | 18 | import org.eclipse.edc.policy.engine.spi.AtomicConstraintFunction; |
| 18 | 19 | import org.eclipse.edc.policy.engine.spi.DynamicAtomicConstraintFunction; |
| 19 | 20 | import org.eclipse.edc.policy.engine.spi.PolicyContext; |
| 20 | 21 | import org.eclipse.edc.policy.engine.spi.PolicyEngine; |
| 21 | 22 | import org.eclipse.edc.policy.engine.spi.RuleFunction; |
| 23 | +import org.eclipse.edc.policy.engine.spi.plan.PolicyEvaluationPlan; | |
| 22 | 24 | import org.eclipse.edc.policy.engine.validation.PolicyValidator; |
| 23 | 25 | import org.eclipse.edc.policy.engine.validation.RuleValidator; |
| 24 | 26 | import org.eclipse.edc.policy.evaluator.PolicyEvaluator; |
| @@ -48,7 +50,7 @@ | ||
| 48 | 50 | */ |
| 49 | 51 | public class PolicyEngineImpl implements PolicyEngine { |
| 50 | 52 | |
| 51 | - private static final String ALL_SCOPES_DELIMITED = ALL_SCOPES + DELIMITER; | |
| 53 | + public static final String ALL_SCOPES_DELIMITED = ALL_SCOPES + DELIMITER; | |
| 52 | 54 | |
| 53 | 55 | private final Map<String, List<ConstraintFunctionEntry<Rule>>> constraintFunctions = new TreeMap<>(); |
| 54 | 56 | |
| @@ -65,6 +67,10 @@ | ||
| 65 | 67 | this.ruleValidator = ruleValidator; |
| 66 | 68 | } |
| 67 | 69 | |
| 70 | + public static boolean scopeFilter(String entry, String scope) { | |
| 71 | + return ALL_SCOPES_DELIMITED.equals(entry) || scope.startsWith(entry); | |
| 72 | + } | |
| 73 | + | |
| 68 | 74 | @Override |
| 69 | 75 | public Policy filter(Policy policy, String scope) { |
| 70 | 76 | return scopeFilter.applyScope(policy, scope); |
| @@ -148,6 +154,29 @@ | ||
| 148 | 154 | return validatorBuilder.build().validate(policy); |
| 149 | 155 | } |
| 150 | 156 | |
| 157 | + @Override | |
| 158 | + public PolicyEvaluationPlan createEvaluationPlan(String scope, Policy policy) { | |
| 159 | + var delimitedScope = scope + DELIMITER; | |
| 160 | + var planner = PolicyEvaluationPlanner.Builder.newInstance(delimitedScope).ruleValidator(ruleValidator); | |
| 161 | + | |
| 162 | + preValidators.forEach(planner::preValidators); | |
| 163 | + postValidators.forEach(planner::postValidators); | |
| 164 | + | |
| 165 | + constraintFunctions.forEach((functionScope, entry) -> entry.forEach(constraintEntry -> { | |
| 166 | + planner.evaluationFunction(functionScope, constraintEntry.key, constraintEntry.type, constraintEntry.function); | |
| 167 | + })); | |
| 168 | + | |
| 169 | + dynamicConstraintFunctions.forEach(dynFunctions -> | |
| 170 | + planner.evaluationFunction(dynFunctions.scope, dynFunctions.type, dynFunctions.function) | |
| 171 | + ); | |
| 172 | + | |
| 173 | + ruleFunctions.forEach((functionScope, entry) -> entry.forEach(functionEntry -> { | |
| 174 | + planner.evaluationFunction(functionScope, functionEntry.type, functionEntry.function); | |
| 175 | + })); | |
| 176 | + | |
| 177 | + return policy.accept(planner.build()); | |
| 178 | + } | |
| 179 | + | |
| 151 | 180 | @Override |
| 152 | 181 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 153 | 182 | public <R extends Rule> void registerFunction(String scope, Class<R> type, String key, AtomicConstraintFunction<R> function) { |
| @@ -176,10 +205,6 @@ | ||
| 176 | 205 | postValidators.computeIfAbsent(scope + DELIMITER, k -> new ArrayList<>()).add(validator); |
| 177 | 206 | } |
| 178 | 207 | |
| 179 | - private boolean scopeFilter(String entry, String scope) { | |
| 180 | - return ALL_SCOPES_DELIMITED.equals(entry) || scope.startsWith(entry); | |
| 181 | - } | |
| 182 | - | |
| 183 | 208 | @NotNull |
| 184 | 209 | private Result<Void> failValidator(String type, BiFunction<Policy, PolicyContext, Boolean> validator, PolicyContext context) { |
| 185 | 210 | return failure(context.hasProblems() ? context.getProblems() : List.of(type + " failed: " + validator.getClass().getName())); |
added core/common/lib/policy-engine-lib/src/main/java/org/eclipse/edc/policy/engine/plan/PolicyEvaluationPlanner.java +314 −0
| @@ -0,0 +1,314 @@ | ||
| 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.policy.engine.plan; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.policy.engine.spi.AtomicConstraintFunction; | |
| 18 | +import org.eclipse.edc.policy.engine.spi.DynamicAtomicConstraintFunction; | |
| 19 | +import org.eclipse.edc.policy.engine.spi.PolicyContext; | |
| 20 | +import org.eclipse.edc.policy.engine.spi.RuleFunction; | |
| 21 | +import org.eclipse.edc.policy.engine.spi.plan.PolicyEvaluationPlan; | |
| 22 | +import org.eclipse.edc.policy.engine.spi.plan.step.AndConstraintStep; | |
| 23 | +import org.eclipse.edc.policy.engine.spi.plan.step.AtomicConstraintStep; | |
| 24 | +import org.eclipse.edc.policy.engine.spi.plan.step.ConstraintStep; | |
| 25 | +import org.eclipse.edc.policy.engine.spi.plan.step.DutyStep; | |
| 26 | +import org.eclipse.edc.policy.engine.spi.plan.step.OrConstraintStep; | |
| 27 | +import org.eclipse.edc.policy.engine.spi.plan.step.PermissionStep; | |
| 28 | +import org.eclipse.edc.policy.engine.spi.plan.step.ProhibitionStep; | |
| 29 | +import org.eclipse.edc.policy.engine.spi.plan.step.RuleFunctionStep; | |
| 30 | +import org.eclipse.edc.policy.engine.spi.plan.step.RuleStep; | |
| 31 | +import org.eclipse.edc.policy.engine.spi.plan.step.ValidatorStep; | |
| 32 | +import org.eclipse.edc.policy.engine.spi.plan.step.XoneConstraintStep; | |
| 33 | +import org.eclipse.edc.policy.engine.validation.RuleValidator; | |
| 34 | +import org.eclipse.edc.policy.model.AndConstraint; | |
| 35 | +import org.eclipse.edc.policy.model.AtomicConstraint; | |
| 36 | +import org.eclipse.edc.policy.model.Constraint; | |
| 37 | +import org.eclipse.edc.policy.model.Duty; | |
| 38 | +import org.eclipse.edc.policy.model.MultiplicityConstraint; | |
| 39 | +import org.eclipse.edc.policy.model.Operator; | |
| 40 | +import org.eclipse.edc.policy.model.OrConstraint; | |
| 41 | +import org.eclipse.edc.policy.model.Permission; | |
| 42 | +import org.eclipse.edc.policy.model.Policy; | |
| 43 | +import org.eclipse.edc.policy.model.Prohibition; | |
| 44 | +import org.eclipse.edc.policy.model.Rule; | |
| 45 | +import org.eclipse.edc.policy.model.XoneConstraint; | |
| 46 | +import org.eclipse.edc.spi.result.Result; | |
| 47 | + | |
| 48 | +import java.util.ArrayList; | |
| 49 | +import java.util.List; | |
| 50 | +import java.util.Map; | |
| 51 | +import java.util.Objects; | |
| 52 | +import java.util.Stack; | |
| 53 | +import java.util.TreeMap; | |
| 54 | +import java.util.function.BiFunction; | |
| 55 | +import java.util.stream.Collectors; | |
| 56 | + | |
| 57 | +import static org.eclipse.edc.policy.engine.PolicyEngineImpl.scopeFilter; | |
| 58 | + | |
| 59 | +public class PolicyEvaluationPlanner implements Policy.Visitor<PolicyEvaluationPlan>, Rule.Visitor<RuleStep<? extends Rule>>, Constraint.Visitor<ConstraintStep> { | |
| 60 | + | |
| 61 | + private final Stack<Rule> ruleContext = new Stack<>(); | |
| 62 | + private final List<BiFunction<Policy, PolicyContext, Boolean>> preValidators = new ArrayList<>(); | |
| 63 | + private final List<BiFunction<Policy, PolicyContext, Boolean>> postValidators = new ArrayList<>(); | |
| 64 | + private final Map<String, List<ConstraintFunctionEntry<Rule>>> constraintFunctions = new TreeMap<>(); | |
| 65 | + private final List<DynamicAtomicConstraintFunctionEntry<Rule>> dynamicConstraintFunctions = new ArrayList<>(); | |
| 66 | + private final List<RuleFunctionFunctionEntry<Rule>> ruleFunctions = new ArrayList<>(); | |
| 67 | + private final String delimitedScope; | |
| 68 | + | |
| 69 | + private RuleValidator ruleValidator; | |
| 70 | + | |
| 71 | + private PolicyEvaluationPlanner(String delimitedScope) { | |
| 72 | + this.delimitedScope = delimitedScope; | |
| 73 | + } | |
| 74 | + | |
| 75 | + @Override | |
| 76 | + public AndConstraintStep visitAndConstraint(AndConstraint constraint) { | |
| 77 | + var steps = validateMultiplicityConstraint(constraint); | |
| 78 | + return new AndConstraintStep(steps, constraint); | |
| 79 | + } | |
| 80 | + | |
| 81 | + @Override | |
| 82 | + public OrConstraintStep visitOrConstraint(OrConstraint constraint) { | |
| 83 | + var steps = validateMultiplicityConstraint(constraint); | |
| 84 | + return new OrConstraintStep(steps, constraint); | |
| 85 | + | |
| 86 | + } | |
| 87 | + | |
| 88 | + @Override | |
| 89 | + public XoneConstraintStep visitXoneConstraint(XoneConstraint constraint) { | |
| 90 | + var steps = validateMultiplicityConstraint(constraint); | |
| 91 | + return new XoneConstraintStep(steps, constraint); | |
| 92 | + } | |
| 93 | + | |
| 94 | + @Override | |
| 95 | + public AtomicConstraintStep visitAtomicConstraint(AtomicConstraint constraint) { | |
| 96 | + var currentRule = currentRule(); | |
| 97 | + var leftValue = constraint.getLeftExpression().accept(s -> s.getValue().toString()); | |
| 98 | + var function = getFunctions(leftValue, currentRule.getClass()); | |
| 99 | + var isFiltered = !ruleValidator.isInScope(leftValue, delimitedScope) || function == null; | |
| 100 | + | |
| 101 | + return new AtomicConstraintStep(constraint, isFiltered, currentRule, function); | |
| 102 | + } | |
| 103 | + | |
| 104 | + @Override | |
| 105 | + public PolicyEvaluationPlan visitPolicy(Policy policy) { | |
| 106 | + | |
| 107 | + var builder = PolicyEvaluationPlan.Builder.newInstance(); | |
| 108 | + | |
| 109 | + preValidators.stream().map(ValidatorStep::new).forEach(builder::preValidator); | |
| 110 | + postValidators.stream().map(ValidatorStep::new).forEach(builder::postValidator); | |
| 111 | + | |
| 112 | + policy.getPermissions().stream().map(permission -> permission.accept(this)) | |
| 113 | + .map(PermissionStep.class::cast) | |
| 114 | + .forEach(builder::permission); | |
| 115 | + | |
| 116 | + policy.getObligations().stream().map(obligation -> obligation.accept(this)) | |
| 117 | + .map(DutyStep.class::cast) | |
| 118 | + .forEach(builder::obligation); | |
| 119 | + | |
| 120 | + policy.getProhibitions().stream().map(permission -> permission.accept(this)) | |
| 121 | + .map(ProhibitionStep.class::cast) | |
| 122 | + .forEach(builder::prohibition); | |
| 123 | + | |
| 124 | + return builder.build(); | |
| 125 | + } | |
| 126 | + | |
| 127 | + @Override | |
| 128 | + public PermissionStep visitPermission(Permission permission) { | |
| 129 | + var permissionStepBuilder = PermissionStep.Builder.newInstance(); | |
| 130 | + visitRule(permission, permissionStepBuilder); | |
| 131 | + | |
| 132 | + permission.getDuties().stream().map(this::visitDuty) | |
| 133 | + .forEach(permissionStepBuilder::dutyStep); | |
| 134 | + | |
| 135 | + return permissionStepBuilder.build(); | |
| 136 | + } | |
| 137 | + | |
| 138 | + @Override | |
| 139 | + public ProhibitionStep visitProhibition(Prohibition prohibition) { | |
| 140 | + var prohibitionStepBuilder = ProhibitionStep.Builder.newInstance(); | |
| 141 | + visitRule(prohibition, prohibitionStepBuilder); | |
| 142 | + return prohibitionStepBuilder.build(); | |
| 143 | + } | |
| 144 | + | |
| 145 | + @Override | |
| 146 | + public DutyStep visitDuty(Duty duty) { | |
| 147 | + var prohibitionStepBuilder = DutyStep.Builder.newInstance(); | |
| 148 | + visitRule(duty, prohibitionStepBuilder); | |
| 149 | + return prohibitionStepBuilder.build(); | |
| 150 | + } | |
| 151 | + | |
| 152 | + private AtomicConstraintFunction<Rule> getFunctions(String key, Class<? extends Rule> ruleKind) { | |
| 153 | + return constraintFunctions.getOrDefault(key, new ArrayList<>()) | |
| 154 | + .stream() | |
| 155 | + .filter(entry -> ruleKind.isAssignableFrom(entry.type())) | |
| 156 | + .map(entry -> entry.function) | |
| 157 | + .findFirst() | |
| 158 | + .or(() -> dynamicConstraintFunctions | |
| 159 | + .stream() | |
| 160 | + .filter(f -> ruleKind.isAssignableFrom(f.type)) | |
| 161 | + .filter(f -> f.function.canHandle(key)) | |
| 162 | + .map(entry -> wrapDynamicFunction(key, entry.function)) | |
| 163 | + .findFirst()) | |
| 164 | + .orElse(null); | |
| 165 | + } | |
| 166 | + | |
| 167 | + @SuppressWarnings({ "rawtypes", "unchecked" }) | |
| 168 | + private <R extends Rule> void visitRule(R rule, RuleStep.Builder builder) { | |
| 169 | + | |
| 170 | + try { | |
| 171 | + ruleContext.push(rule); | |
| 172 | + builder.filtered(shouldIgnoreRule(rule)); | |
| 173 | + builder.rule(rule); | |
| 174 | + | |
| 175 | + for (var functionEntry : ruleFunctions) { | |
| 176 | + if (rule.getClass().isAssignableFrom(functionEntry.type)) { | |
| 177 | + builder.ruleFunction(new RuleFunctionStep(functionEntry.function, rule)); | |
| 178 | + } | |
| 179 | + } | |
| 180 | + | |
| 181 | + rule.getConstraints().stream() | |
| 182 | + .map(constraint -> constraint.accept(this)) | |
| 183 | + .forEach(builder::constraint); | |
| 184 | + | |
| 185 | + } finally { | |
| 186 | + ruleContext.pop(); | |
| 187 | + } | |
| 188 | + | |
| 189 | + } | |
| 190 | + | |
| 191 | + private Rule currentRule() { | |
| 192 | + return ruleContext.peek(); | |
| 193 | + } | |
| 194 | + | |
| 195 | + private boolean shouldIgnoreRule(Rule rule) { | |
| 196 | + return rule.getAction() != null && !ruleValidator.isBounded(rule.getAction().getType()); | |
| 197 | + } | |
| 198 | + | |
| 199 | + private List<ConstraintStep> validateMultiplicityConstraint(MultiplicityConstraint multiplicityConstraint) { | |
| 200 | + return multiplicityConstraint.getConstraints() | |
| 201 | + .stream() | |
| 202 | + .map(c -> c.accept(this)) | |
| 203 | + .collect(Collectors.toList()); | |
| 204 | + } | |
| 205 | + | |
| 206 | + private <R extends Rule> AtomicConstraintFunction<R> wrapDynamicFunction(String key, DynamicAtomicConstraintFunction<R> function) { | |
| 207 | + return new AtomicConstraintFunctionWrapper<>(key, function); | |
| 208 | + } | |
| 209 | + | |
| 210 | + private record ConstraintFunctionEntry<R extends Rule>( | |
| 211 | + Class<R> type, | |
| 212 | + AtomicConstraintFunction<R> function) { | |
| 213 | + } | |
| 214 | + | |
| 215 | + private record DynamicAtomicConstraintFunctionEntry<R extends Rule>( | |
| 216 | + Class<R> type, | |
| 217 | + DynamicAtomicConstraintFunction<R> function) { | |
| 218 | + } | |
| 219 | + | |
| 220 | + private record RuleFunctionFunctionEntry<R extends Rule>( | |
| 221 | + Class<R> type, | |
| 222 | + RuleFunction<R> function) { | |
| 223 | + } | |
| 224 | + | |
| 225 | + private record AtomicConstraintFunctionWrapper<R extends Rule>( | |
| 226 | + String leftOperand, | |
| 227 | + DynamicAtomicConstraintFunction<R> inner) implements AtomicConstraintFunction<R> { | |
| 228 | + | |
| 229 | + @Override | |
| 230 | + public boolean evaluate(Operator operator, Object rightValue, R rule, PolicyContext context) { | |
| 231 | + return inner.evaluate(leftOperand, operator, rightValue, rule, context); | |
| 232 | + } | |
| 233 | + | |
| 234 | + @Override | |
| 235 | + public Result<Void> validate(Operator operator, Object rightValue, R rule) { | |
| 236 | + return inner.validate(leftOperand, operator, rightValue, rule); | |
| 237 | + } | |
| 238 | + } | |
| 239 | + | |
| 240 | + public static class Builder { | |
| 241 | + private final PolicyEvaluationPlanner planner; | |
| 242 | + | |
| 243 | + private Builder(String scope) { | |
| 244 | + planner = new PolicyEvaluationPlanner(scope); | |
| 245 | + } | |
| 246 | + | |
| 247 | + public static PolicyEvaluationPlanner.Builder newInstance(String scope) { | |
| 248 | + return new PolicyEvaluationPlanner.Builder(scope); | |
| 249 | + } | |
| 250 | + | |
| 251 | + public Builder ruleValidator(RuleValidator ruleValidator) { | |
| 252 | + planner.ruleValidator = ruleValidator; | |
| 253 | + return this; | |
| 254 | + } | |
| 255 | + | |
| 256 | + public Builder preValidator(String scope, BiFunction<Policy, PolicyContext, Boolean> validator) { | |
| 257 | + | |
| 258 | + if (scopeFilter(scope, planner.delimitedScope)) { | |
| 259 | + planner.preValidators.add(validator); | |
| 260 | + } | |
| 261 | + | |
| 262 | + return this; | |
| 263 | + } | |
| 264 | + | |
| 265 | + public Builder preValidators(String scope, List<BiFunction<Policy, PolicyContext, Boolean>> validators) { | |
| 266 | + validators.forEach(validator -> preValidator(scope, validator)); | |
| 267 | + return this; | |
| 268 | + } | |
| 269 | + | |
| 270 | + public Builder postValidator(String scope, BiFunction<Policy, PolicyContext, Boolean> validator) { | |
| 271 | + if (scopeFilter(scope, planner.delimitedScope)) { | |
| 272 | + planner.postValidators.add(validator); | |
| 273 | + } | |
| 274 | + return this; | |
| 275 | + } | |
| 276 | + | |
| 277 | + public Builder postValidators(String scope, List<BiFunction<Policy, PolicyContext, Boolean>> validators) { | |
| 278 | + validators.forEach(validator -> postValidator(scope, validator)); | |
| 279 | + return this; | |
| 280 | + } | |
| 281 | + | |
| 282 | + @SuppressWarnings({ "unchecked", "rawtypes" }) | |
| 283 | + public <R extends Rule> Builder evaluationFunction(String scope, String key, Class<R> ruleKind, AtomicConstraintFunction<R> function) { | |
| 284 | + | |
| 285 | + if (scopeFilter(scope, planner.delimitedScope)) { | |
| 286 | + planner.constraintFunctions.computeIfAbsent(key, k -> new ArrayList<>()) | |
| 287 | + .add(new ConstraintFunctionEntry(ruleKind, function)); | |
| 288 | + } | |
| 289 | + return this; | |
| 290 | + } | |
| 291 | + | |
| 292 | + @SuppressWarnings({ "unchecked", "rawtypes" }) | |
| 293 | + public <R extends Rule> Builder evaluationFunction(String scope, Class<R> ruleKind, DynamicAtomicConstraintFunction<R> function) { | |
| 294 | + if (scopeFilter(scope, planner.delimitedScope)) { | |
| 295 | + planner.dynamicConstraintFunctions.add(new DynamicAtomicConstraintFunctionEntry(ruleKind, function)); | |
| 296 | + } | |
| 297 | + return this; | |
| 298 | + } | |
| 299 | + | |
| 300 | + @SuppressWarnings({ "unchecked", "rawtypes" }) | |
| 301 | + public <R extends Rule> Builder evaluationFunction(String scope, Class<R> ruleKind, RuleFunction<R> function) { | |
| 302 | + if (scopeFilter(scope, planner.delimitedScope)) { | |
| 303 | + planner.ruleFunctions.add(new RuleFunctionFunctionEntry(ruleKind, function)); | |
| 304 | + } | |
| 305 | + return this; | |
| 306 | + } | |
| 307 | + | |
| 308 | + public PolicyEvaluationPlanner build() { | |
| 309 | + Objects.requireNonNull(planner.ruleValidator, "Rule validator should not be null"); | |
| 310 | + return planner; | |
| 311 | + } | |
| 312 | + | |
| 313 | + } | |
| 314 | +} |
modified core/common/lib/policy-engine-lib/src/main/java/org/eclipse/edc/policy/engine/validation/RuleValidator.java +8 −1
| @@ -30,7 +30,14 @@ | ||
| 30 | 30 | /** |
| 31 | 31 | * Checks if the input ruleType is bound to any scope |
| 32 | 32 | */ |
| 33 | - boolean isBounded(String ruleType) { | |
| 33 | + public boolean isBounded(String ruleType) { | |
| 34 | 34 | return !registry.bindings(ruleType).isEmpty(); |
| 35 | 35 | } |
| 36 | + | |
| 37 | + /** | |
| 38 | + * Checks if the input ruleType is bound within the input scope | |
| 39 | + */ | |
| 40 | + public boolean isInScope(String ruleType, String scope) { | |
| 41 | + return registry.isInScope(ruleType, scope); | |
| 42 | + } | |
| 36 | 43 | } |
added core/common/lib/policy-engine-lib/src/test/java/org/eclipse/edc/policy/engine/PolicyEngineImplPlannerTest.java +457 −0
| @@ -0,0 +1,457 @@ | ||
| 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.policy.engine; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.policy.engine.spi.DynamicAtomicConstraintFunction; | |
| 18 | +import org.eclipse.edc.policy.engine.spi.PolicyEngine; | |
| 19 | +import org.eclipse.edc.policy.engine.spi.RuleBindingRegistry; | |
| 20 | +import org.eclipse.edc.policy.engine.spi.RuleFunction; | |
| 21 | +import org.eclipse.edc.policy.engine.spi.plan.PolicyEvaluationPlan; | |
| 22 | +import org.eclipse.edc.policy.engine.spi.plan.step.AtomicConstraintStep; | |
| 23 | +import org.eclipse.edc.policy.engine.spi.plan.step.MultiplicityConstraintStep; | |
| 24 | +import org.eclipse.edc.policy.engine.spi.plan.step.RuleStep; | |
| 25 | +import org.eclipse.edc.policy.engine.validation.RuleValidator; | |
| 26 | +import org.eclipse.edc.policy.model.Action; | |
| 27 | +import org.eclipse.edc.policy.model.AndConstraint; | |
| 28 | +import org.eclipse.edc.policy.model.AtomicConstraint; | |
| 29 | +import org.eclipse.edc.policy.model.Duty; | |
| 30 | +import org.eclipse.edc.policy.model.LiteralExpression; | |
| 31 | +import org.eclipse.edc.policy.model.OrConstraint; | |
| 32 | +import org.eclipse.edc.policy.model.Permission; | |
| 33 | +import org.eclipse.edc.policy.model.Policy; | |
| 34 | +import org.eclipse.edc.policy.model.Prohibition; | |
| 35 | +import org.eclipse.edc.policy.model.Rule; | |
| 36 | +import org.eclipse.edc.policy.model.XoneConstraint; | |
| 37 | +import org.junit.jupiter.api.BeforeEach; | |
| 38 | +import org.junit.jupiter.api.Nested; | |
| 39 | +import org.junit.jupiter.api.Test; | |
| 40 | +import org.junit.jupiter.api.extension.ExtensionContext; | |
| 41 | +import org.junit.jupiter.params.ParameterizedTest; | |
| 42 | +import org.junit.jupiter.params.provider.Arguments; | |
| 43 | +import org.junit.jupiter.params.provider.ArgumentsProvider; | |
| 44 | +import org.junit.jupiter.params.provider.ArgumentsSource; | |
| 45 | + | |
| 46 | +import java.util.List; | |
| 47 | +import java.util.function.Function; | |
| 48 | +import java.util.stream.Stream; | |
| 49 | + | |
| 50 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 51 | +import static org.eclipse.edc.policy.engine.spi.PolicyEngine.ALL_SCOPES; | |
| 52 | +import static org.eclipse.edc.policy.model.Operator.EQ; | |
| 53 | +import static org.junit.jupiter.params.provider.Arguments.of; | |
| 54 | +import static org.mockito.ArgumentMatchers.any; | |
| 55 | +import static org.mockito.Mockito.mock; | |
| 56 | +import static org.mockito.Mockito.when; | |
| 57 | + | |
| 58 | + | |
| 59 | +class PolicyEngineImplPlannerTest { | |
| 60 | + | |
| 61 | + private static final String TEST_SCOPE = "test"; | |
| 62 | + | |
| 63 | + private final RuleBindingRegistry bindingRegistry = new RuleBindingRegistryImpl(); | |
| 64 | + private PolicyEngine policyEngine; | |
| 65 | + | |
| 66 | + private static AtomicConstraint atomicConstraint(String key, String value) { | |
| 67 | + var left = new LiteralExpression(key); | |
| 68 | + var right = new LiteralExpression(value); | |
| 69 | + return AtomicConstraint.Builder.newInstance() | |
| 70 | + .leftExpression(left) | |
| 71 | + .operator(EQ) | |
| 72 | + .rightExpression(right) | |
| 73 | + .build(); | |
| 74 | + } | |
| 75 | + | |
| 76 | + @BeforeEach | |
| 77 | + void setUp() { | |
| 78 | + policyEngine = new PolicyEngineImpl(new ScopeFilter(bindingRegistry), new RuleValidator(bindingRegistry)); | |
| 79 | + } | |
| 80 | + | |
| 81 | + @Nested | |
| 82 | + class EvaluationPlan { | |
| 83 | + | |
| 84 | + @ParameterizedTest | |
| 85 | + @ArgumentsSource(SimplePolicyProvider.class) | |
| 86 | + void withRule(Policy policy, Class<Rule> ruleClass, String action, String key, Function<PolicyEvaluationPlan, List<RuleStep<? extends Rule>>> stepsProvider) { | |
| 87 | + | |
| 88 | + bindingRegistry.bind(action, TEST_SCOPE); | |
| 89 | + bindingRegistry.bind(key, TEST_SCOPE); | |
| 90 | + | |
| 91 | + policyEngine.registerFunction(TEST_SCOPE, ruleClass, key, (op, rv, r, ctx) -> true); | |
| 92 | + | |
| 93 | + var plan = policyEngine.createEvaluationPlan(TEST_SCOPE, policy); | |
| 94 | + | |
| 95 | + assertThat(stepsProvider.apply(plan)).hasSize(1) | |
| 96 | + .first() | |
| 97 | + .satisfies(ruleStep -> { | |
| 98 | + assertThat(ruleStep.isFiltered()).isFalse(); | |
| 99 | + assertThat(ruleStep.getRuleFunctions()).hasSize(0); | |
| 100 | + assertThat(ruleStep.getConstraintSteps()).hasSize(1) | |
| 101 | + .first() | |
| 102 | + .isInstanceOfSatisfying(AtomicConstraintStep.class, (constraintStep) -> { | |
| 103 | + assertThat(constraintStep.isFiltered()).isFalse(); | |
| 104 | + assertThat(constraintStep.function()).isNotNull(); | |
| 105 | + assertThat(constraintStep.constraint()).isNotNull(); | |
| 106 | + assertThat(constraintStep.rule()).isInstanceOf(ruleClass); | |
| 107 | + }); | |
| 108 | + }); | |
| 109 | + } | |
| 110 | + | |
| 111 | + @ParameterizedTest | |
| 112 | + @ArgumentsSource(SimplePolicyProvider.class) | |
| 113 | + void withRuleAndDynFunction(Policy policy, Class<Rule> ruleClass, String action, String key, Function<PolicyEvaluationPlan, List<RuleStep<? extends Rule>>> stepsProvider) { | |
| 114 | + | |
| 115 | + DynamicAtomicConstraintFunction<Rule> function = mock(); | |
| 116 | + | |
| 117 | + when(function.canHandle(key)).thenReturn(true); | |
| 118 | + | |
| 119 | + bindingRegistry.bind(action, TEST_SCOPE); | |
| 120 | + bindingRegistry.bind(key, TEST_SCOPE); | |
| 121 | + | |
| 122 | + policyEngine.registerFunction(TEST_SCOPE, ruleClass, function); | |
| 123 | + | |
| 124 | + var plan = policyEngine.createEvaluationPlan(TEST_SCOPE, policy); | |
| 125 | + | |
| 126 | + assertThat(stepsProvider.apply(plan)).hasSize(1) | |
| 127 | + .first() | |
| 128 | + .satisfies(ruleStep -> { | |
| 129 | + assertThat(ruleStep.isFiltered()).isFalse(); | |
| 130 | + assertThat(ruleStep.getRuleFunctions()).hasSize(0); | |
| 131 | + assertThat(ruleStep.getConstraintSteps()).hasSize(1) | |
| 132 | + .first() | |
| 133 | + .isInstanceOfSatisfying(AtomicConstraintStep.class, (constraintStep) -> { | |
| 134 | + assertThat(constraintStep.isFiltered()).isFalse(); | |
| 135 | + assertThat(constraintStep.function()).isNotNull(); | |
| 136 | + assertThat(constraintStep.constraint()).isNotNull(); | |
| 137 | + assertThat(constraintStep.rule()).isInstanceOf(ruleClass); | |
| 138 | + }); | |
| 139 | + }); | |
| 140 | + } | |
| 141 | + | |
| 142 | + | |
| 143 | + @ParameterizedTest | |
| 144 | + @ArgumentsSource(SimplePolicyProvider.class) | |
| 145 | + void withRuleAndRuleFunction(Policy policy, Class<Rule> ruleClass, String action, String key, Function<PolicyEvaluationPlan, List<RuleStep<? extends Rule>>> stepsProvider) { | |
| 146 | + | |
| 147 | + RuleFunction<Rule> function = mock(); | |
| 148 | + | |
| 149 | + bindingRegistry.bind(action, TEST_SCOPE); | |
| 150 | + bindingRegistry.bind(key, TEST_SCOPE); | |
| 151 | + | |
| 152 | + policyEngine.registerFunction(ALL_SCOPES, ruleClass, function); | |
| 153 | + policyEngine.registerFunction(TEST_SCOPE, ruleClass, function); | |
| 154 | + | |
| 155 | + var plan = policyEngine.createEvaluationPlan(TEST_SCOPE, policy); | |
| 156 | + | |
| 157 | + assertThat(stepsProvider.apply(plan)).hasSize(1) | |
| 158 | + .first() | |
| 159 | + .satisfies(ruleStep -> { | |
| 160 | + assertThat(ruleStep.isFiltered()).isFalse(); | |
| 161 | + assertThat(ruleStep.getRuleFunctions()).hasSize(2); | |
| 162 | + assertThat(ruleStep.getConstraintSteps()).hasSize(1) | |
| 163 | + .first() | |
| 164 | + .isInstanceOfSatisfying(AtomicConstraintStep.class, (constraintStep) -> { | |
| 165 | + assertThat(constraintStep.isFiltered()).isTrue(); | |
| 166 | + assertThat(constraintStep.function()).isNull(); | |
| 167 | + assertThat(constraintStep.constraint()).isNotNull(); | |
| 168 | + assertThat(constraintStep.rule()).isInstanceOf(ruleClass); | |
| 169 | + }); | |
| 170 | + }); | |
| 171 | + } | |
| 172 | + | |
| 173 | + @ParameterizedTest | |
| 174 | + @ArgumentsSource(SimplePolicyProvider.class) | |
| 175 | + void withRuleAndRuleFunctionNotBound(Policy policy, Class<Rule> ruleClass, String action, String key, Function<PolicyEvaluationPlan, List<RuleStep<? extends Rule>>> stepsProvider) { | |
| 176 | + | |
| 177 | + RuleFunction<Rule> function = mock(); | |
| 178 | + | |
| 179 | + bindingRegistry.bind(action, TEST_SCOPE); | |
| 180 | + bindingRegistry.bind(key, TEST_SCOPE); | |
| 181 | + | |
| 182 | + policyEngine.registerFunction("invalidScope", ruleClass, function); | |
| 183 | + | |
| 184 | + var plan = policyEngine.createEvaluationPlan(TEST_SCOPE, policy); | |
| 185 | + | |
| 186 | + assertThat(stepsProvider.apply(plan)).hasSize(1) | |
| 187 | + .first() | |
| 188 | + .satisfies(ruleStep -> { | |
| 189 | + assertThat(ruleStep.isFiltered()).isFalse(); | |
| 190 | + assertThat(ruleStep.getRuleFunctions()).hasSize(0); | |
| 191 | + }); | |
| 192 | + } | |
| 193 | + | |
| 194 | + @Test | |
| 195 | + void withPermissionContainingDuty() { | |
| 196 | + | |
| 197 | + var key = "foo"; | |
| 198 | + var actionType = "action"; | |
| 199 | + var constraint = atomicConstraint(key, "bar"); | |
| 200 | + var action = Action.Builder.newInstance().type(actionType).build(); | |
| 201 | + var duty = Duty.Builder.newInstance().constraint(constraint).action(action).build(); | |
| 202 | + var permission = Permission.Builder.newInstance().constraint(constraint).duty(duty).action(action).build(); | |
| 203 | + var policy = Policy.Builder.newInstance().permission(permission).build(); | |
| 204 | + | |
| 205 | + bindingRegistry.bind(actionType, TEST_SCOPE); | |
| 206 | + bindingRegistry.bind(key, TEST_SCOPE); | |
| 207 | + | |
| 208 | + policyEngine.registerFunction(ALL_SCOPES, Duty.class, key, (op, rv, r, ctx) -> true); | |
| 209 | + | |
| 210 | + var plan = policyEngine.createEvaluationPlan(TEST_SCOPE, policy); | |
| 211 | + | |
| 212 | + assertThat(plan.getPermissionSteps()).hasSize(1) | |
| 213 | + .first() | |
| 214 | + .satisfies(ruleStep -> { | |
| 215 | + assertThat(ruleStep.isFiltered()).isFalse(); | |
| 216 | + assertThat(ruleStep.getDutySteps()).hasSize(1); | |
| 217 | + assertThat(ruleStep.getRuleFunctions()).hasSize(0); | |
| 218 | + assertThat(ruleStep.getConstraintSteps()).hasSize(1) | |
| 219 | + .first() | |
| 220 | + .isInstanceOfSatisfying(AtomicConstraintStep.class, (constraintStep) -> { | |
| 221 | + assertThat(constraintStep.isFiltered()).isTrue(); | |
| 222 | + assertThat(constraintStep.function()).isNull(); | |
| 223 | + assertThat(constraintStep.constraint()).isNotNull(); | |
| 224 | + assertThat(constraintStep.rule()).isInstanceOf(Permission.class); | |
| 225 | + }); | |
| 226 | + }); | |
| 227 | + } | |
| 228 | + | |
| 229 | + | |
| 230 | + private static class SimplePolicyProvider implements ArgumentsProvider { | |
| 231 | + @Override | |
| 232 | + public Stream<? extends Arguments> provideArguments(ExtensionContext context) { | |
| 233 | + | |
| 234 | + var leftOperand = "foo"; | |
| 235 | + var actionType = "action"; | |
| 236 | + | |
| 237 | + var action = Action.Builder.newInstance().type(actionType).build(); | |
| 238 | + var constraint = atomicConstraint(leftOperand, "bar"); | |
| 239 | + | |
| 240 | + var prohibition = Prohibition.Builder.newInstance().constraint(constraint).action(action).build(); | |
| 241 | + | |
| 242 | + Function<PolicyEvaluationPlan, List<? extends RuleStep<? extends Rule>>> permissionSteps = PolicyEvaluationPlan::getPermissionSteps; | |
| 243 | + Function<PolicyEvaluationPlan, List<? extends RuleStep<? extends Rule>>> dutySteps = PolicyEvaluationPlan::getDutySteps; | |
| 244 | + Function<PolicyEvaluationPlan, List<? extends RuleStep<? extends Rule>>> prohibitionSteps = PolicyEvaluationPlan::getProhibitionSteps; | |
| 245 | + | |
| 246 | + var permission = Permission.Builder.newInstance().constraint(constraint).action(action).build(); | |
| 247 | + var duty = Duty.Builder.newInstance().constraint(constraint).action(action).build(); | |
| 248 | + | |
| 249 | + return Stream.of( | |
| 250 | + of(Policy.Builder.newInstance().permission(permission).build(), Permission.class, actionType, leftOperand, permissionSteps), | |
| 251 | + of(Policy.Builder.newInstance().duty(duty).build(), Duty.class, actionType, leftOperand, dutySteps), | |
| 252 | + of(Policy.Builder.newInstance().prohibition(prohibition).build(), Prohibition.class, actionType, leftOperand, prohibitionSteps) | |
| 253 | + ); | |
| 254 | + } | |
| 255 | + } | |
| 256 | + } | |
| 257 | + | |
| 258 | + @Nested | |
| 259 | + class IgnoredStep { | |
| 260 | + | |
| 261 | + @Test | |
| 262 | + void shouldIgnorePermissionStep_whenActionNotBound() { | |
| 263 | + | |
| 264 | + bindingRegistry.bind("foo", TEST_SCOPE); | |
| 265 | + | |
| 266 | + var constraint = atomicConstraint("foo", "bar"); | |
| 267 | + | |
| 268 | + var permission = Permission.Builder.newInstance().action(Action.Builder.newInstance().type("action").build()).constraint(constraint).build(); | |
| 269 | + var policy = Policy.Builder.newInstance().permission(permission).build(); | |
| 270 | + policyEngine.registerFunction(ALL_SCOPES, Permission.class, "foo", (op, rv, r, ctx) -> true); | |
| 271 | + | |
| 272 | + var plan = policyEngine.createEvaluationPlan(TEST_SCOPE, policy); | |
| 273 | + | |
| 274 | + assertThat(plan.getPermissionSteps()).hasSize(1) | |
| 275 | + .first() | |
| 276 | + .satisfies(permissionStep -> { | |
| 277 | + assertThat(permissionStep.isFiltered()).isTrue(); | |
| 278 | + assertThat(permissionStep.getConstraintSteps()).hasSize(1) | |
| 279 | + .first() | |
| 280 | + .isInstanceOfSatisfying(AtomicConstraintStep.class, constraintStep -> { | |
| 281 | + assertThat(constraintStep.isFiltered()).isFalse(); | |
| 282 | + }); | |
| 283 | + }); | |
| 284 | + } | |
| 285 | + | |
| 286 | + @Test | |
| 287 | + void shouldIgnoreAtomicConstraintStep_whenLeftExpressionNotScopeBound() { | |
| 288 | + | |
| 289 | + bindingRegistry.bind("action", TEST_SCOPE); | |
| 290 | + | |
| 291 | + var constraint = atomicConstraint("foo", "bar"); | |
| 292 | + var permission = Permission.Builder.newInstance().action(Action.Builder.newInstance().type("action").build()).constraint(constraint).build(); | |
| 293 | + var policy = Policy.Builder.newInstance().permission(permission).build(); | |
| 294 | + | |
| 295 | + var plan = policyEngine.createEvaluationPlan(TEST_SCOPE, policy); | |
| 296 | + | |
| 297 | + assertThat(plan.getPermissionSteps()).hasSize(1) | |
| 298 | + .first() | |
| 299 | + .satisfies(permissionStep -> { | |
| 300 | + assertThat(permissionStep.isFiltered()).isFalse(); | |
| 301 | + assertThat(permissionStep.getConstraintSteps()).hasSize(1) | |
| 302 | + .first() | |
| 303 | + .isInstanceOfSatisfying(AtomicConstraintStep.class, constraintStep -> { | |
| 304 | + assertThat(constraintStep.isFiltered()).isTrue(); | |
| 305 | + }); | |
| 306 | + }); | |
| 307 | + } | |
| 308 | + | |
| 309 | + @Test | |
| 310 | + void shouldIgnoreAtomicConstraintStep_whenLeftExpressionNotFunctionBound() { | |
| 311 | + | |
| 312 | + bindingRegistry.bind("action", TEST_SCOPE); | |
| 313 | + bindingRegistry.bind("foo", TEST_SCOPE); | |
| 314 | + | |
| 315 | + var constraint = atomicConstraint("foo", "bar"); | |
| 316 | + var permission = Permission.Builder.newInstance().action(Action.Builder.newInstance().type("action").build()).constraint(constraint).build(); | |
| 317 | + var policy = Policy.Builder.newInstance().permission(permission).build(); | |
| 318 | + | |
| 319 | + var plan = policyEngine.createEvaluationPlan(TEST_SCOPE, policy); | |
| 320 | + | |
| 321 | + assertThat(plan.getPermissionSteps()).hasSize(1) | |
| 322 | + .first() | |
| 323 | + .satisfies(permissionStep -> { | |
| 324 | + assertThat(permissionStep.isFiltered()).isFalse(); | |
| 325 | + assertThat(permissionStep.getConstraintSteps()).hasSize(1) | |
| 326 | + .first() | |
| 327 | + .isInstanceOfSatisfying(AtomicConstraintStep.class, constraintStep -> { | |
| 328 | + assertThat(constraintStep.isFiltered()).isTrue(); | |
| 329 | + }); | |
| 330 | + }); | |
| 331 | + } | |
| 332 | + | |
| 333 | + @Test | |
| 334 | + void shouldIgnoreAtomicConstraintStep_whenLeftExpressionNotDynFunctionBound() { | |
| 335 | + | |
| 336 | + | |
| 337 | + DynamicAtomicConstraintFunction<Duty> function = mock(); | |
| 338 | + | |
| 339 | + when(function.canHandle(any())).thenReturn(true); | |
| 340 | + | |
| 341 | + bindingRegistry.bind("action", TEST_SCOPE); | |
| 342 | + bindingRegistry.bind("foo", TEST_SCOPE); | |
| 343 | + | |
| 344 | + var constraint = atomicConstraint("foo", "bar"); | |
| 345 | + var permission = Permission.Builder.newInstance().action(Action.Builder.newInstance().type("action").build()).constraint(constraint).build(); | |
| 346 | + var policy = Policy.Builder.newInstance().permission(permission).build(); | |
| 347 | + policyEngine.registerFunction(ALL_SCOPES, Duty.class, function); | |
| 348 | + | |
| 349 | + var plan = policyEngine.createEvaluationPlan(TEST_SCOPE, policy); | |
| 350 | + | |
| 351 | + assertThat(plan.getPermissionSteps()).hasSize(1) | |
| 352 | + .first() | |
| 353 | + .satisfies(permissionStep -> { | |
| 354 | + assertThat(permissionStep.isFiltered()).isFalse(); | |
| 355 | + assertThat(permissionStep.getConstraintSteps()).hasSize(1) | |
| 356 | + .first() | |
| 357 | + .isInstanceOfSatisfying(AtomicConstraintStep.class, constraintStep -> { | |
| 358 | + assertThat(constraintStep.isFiltered()).isTrue(); | |
| 359 | + assertThat(constraintStep.function()).isNull(); | |
| 360 | + }); | |
| 361 | + }); | |
| 362 | + } | |
| 363 | + | |
| 364 | + } | |
| 365 | + | |
| 366 | + @Nested | |
| 367 | + class MultiplicityConstraints { | |
| 368 | + | |
| 369 | + @ParameterizedTest | |
| 370 | + @ArgumentsSource(MultiplicityPolicyProvider.class) | |
| 371 | + void shouldEvaluate_withMultiplicityConstraint(Policy policy, Class<Rule> ruleClass, String action, String key, Function<PolicyEvaluationPlan, List<RuleStep<? extends Rule>>> stepsProvider) { | |
| 372 | + | |
| 373 | + bindingRegistry.bind(key, TEST_SCOPE); | |
| 374 | + bindingRegistry.bind(action, TEST_SCOPE); | |
| 375 | + | |
| 376 | + policyEngine.registerFunction(ALL_SCOPES, ruleClass, key, (op, rv, r, ctx) -> true); | |
| 377 | + | |
| 378 | + var plan = policyEngine.createEvaluationPlan(TEST_SCOPE, policy); | |
| 379 | + | |
| 380 | + assertThat(plan.getPreValidators()).isEmpty(); | |
| 381 | + assertThat(plan.getPostValidators()).isEmpty(); | |
| 382 | + | |
| 383 | + | |
| 384 | + assertThat(stepsProvider.apply(plan)).hasSize(1) | |
| 385 | + .first() | |
| 386 | + .satisfies((ruleStep -> { | |
| 387 | + assertThat(ruleStep.isFiltered()).isFalse(); | |
| 388 | + assertThat(ruleStep.getConstraintSteps()).hasSize(1) | |
| 389 | + .first() | |
| 390 | + .isInstanceOfSatisfying(MultiplicityConstraintStep.class, constraintStep -> { | |
| 391 | + assertThat(constraintStep.getSteps()).hasSize(2); | |
| 392 | + assertThat(constraintStep.getConstraint()).isNotNull(); | |
| 393 | + }); | |
| 394 | + })); | |
| 395 | + | |
| 396 | + } | |
| 397 | + | |
| 398 | + private static class MultiplicityPolicyProvider implements ArgumentsProvider { | |
| 399 | + @Override | |
| 400 | + public Stream<? extends Arguments> provideArguments(ExtensionContext context) { | |
| 401 | + | |
| 402 | + var leftOperand = "foo"; | |
| 403 | + var actionType = "action"; | |
| 404 | + | |
| 405 | + var firstConstraint = atomicConstraint("foo", "bar"); | |
| 406 | + var secondConstraint = atomicConstraint("baz", "bar"); | |
| 407 | + | |
| 408 | + var orConstraints = OrConstraint.Builder.newInstance().constraint(firstConstraint).constraint(secondConstraint).build(); | |
| 409 | + var andConstraints = AndConstraint.Builder.newInstance().constraint(firstConstraint).constraint(secondConstraint).build(); | |
| 410 | + var xoneConstraint = XoneConstraint.Builder.newInstance().constraint(firstConstraint).constraint(secondConstraint).build(); | |
| 411 | + | |
| 412 | + var permission = Permission.Builder.newInstance().constraint(andConstraints).build(); | |
| 413 | + var prohibition = Prohibition.Builder.newInstance().constraint(orConstraints).build(); | |
| 414 | + var duty = Duty.Builder.newInstance().constraint(xoneConstraint).build(); | |
| 415 | + | |
| 416 | + Function<PolicyEvaluationPlan, List<? extends RuleStep<? extends Rule>>> permissionSteps = PolicyEvaluationPlan::getPermissionSteps; | |
| 417 | + Function<PolicyEvaluationPlan, List<? extends RuleStep<? extends Rule>>> dutySteps = PolicyEvaluationPlan::getDutySteps; | |
| 418 | + Function<PolicyEvaluationPlan, List<? extends RuleStep<? extends Rule>>> prohibitionSteps = PolicyEvaluationPlan::getProhibitionSteps; | |
| 419 | + | |
| 420 | + return Stream.of( | |
| 421 | + of(Policy.Builder.newInstance().permission(permission).build(), Permission.class, actionType, leftOperand, permissionSteps), | |
| 422 | + of(Policy.Builder.newInstance().duty(duty).build(), Duty.class, actionType, leftOperand, dutySteps), | |
| 423 | + of(Policy.Builder.newInstance().prohibition(prohibition).build(), Prohibition.class, actionType, leftOperand, prohibitionSteps) | |
| 424 | + ); | |
| 425 | + } | |
| 426 | + } | |
| 427 | + } | |
| 428 | + | |
| 429 | + @Nested | |
| 430 | + class Validator { | |
| 431 | + | |
| 432 | + @Test | |
| 433 | + void shouldEvaluate_withNoValidators() { | |
| 434 | + var emptyPolicy = Policy.Builder.newInstance().build(); | |
| 435 | + policyEngine.registerPreValidator("foo", (policy, policyContext) -> true); | |
| 436 | + | |
| 437 | + var plan = policyEngine.createEvaluationPlan(TEST_SCOPE, emptyPolicy); | |
| 438 | + | |
| 439 | + assertThat(plan.getPostValidators()).isEmpty(); | |
| 440 | + assertThat(plan.getPreValidators()).isEmpty(); | |
| 441 | + } | |
| 442 | + | |
| 443 | + @Test | |
| 444 | + void shouldEvaluate_withValidators() { | |
| 445 | + var emptyPolicy = Policy.Builder.newInstance().build(); | |
| 446 | + policyEngine.registerPreValidator(TEST_SCOPE, (policy, policyContext) -> true); | |
| 447 | + policyEngine.registerPostValidator(TEST_SCOPE, (policy, policyContext) -> true); | |
| 448 | + | |
| 449 | + var plan = policyEngine.createEvaluationPlan(TEST_SCOPE, emptyPolicy); | |
| 450 | + | |
| 451 | + assertThat(plan.getPreValidators()).hasSize(1); | |
| 452 | + assertThat(plan.getPostValidators()).hasSize(1); | |
| 453 | + | |
| 454 | + } | |
| 455 | + } | |
| 456 | + | |
| 457 | +} |
modified spi/common/policy-engine-spi/src/main/java/org/eclipse/edc/policy/engine/spi/PolicyEngine.java +6 −0
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | |
| 15 | 15 | package org.eclipse.edc.policy.engine.spi; |
| 16 | 16 | |
| 17 | +import org.eclipse.edc.policy.engine.spi.plan.PolicyEvaluationPlan; | |
| 17 | 18 | import org.eclipse.edc.policy.model.Action; |
| 18 | 19 | import org.eclipse.edc.policy.model.AtomicConstraint; |
| 19 | 20 | import org.eclipse.edc.policy.model.Policy; |
| @@ -63,6 +64,11 @@ | ||
| 63 | 64 | */ |
| 64 | 65 | Result<Void> validate(Policy policy); |
| 65 | 66 | |
| 67 | + /** | |
| 68 | + * Returns the {@link PolicyEvaluationPlan} of the given policy within the given scope. | |
| 69 | + */ | |
| 70 | + PolicyEvaluationPlan createEvaluationPlan(String scope, Policy policy); | |
| 71 | + | |
| 66 | 72 | /** |
| 67 | 73 | * Registers a function that is invoked when a policy contains an atomic constraint whose left operator expression evaluates to the given key for the specified scope. |
| 68 | 74 | * |
added spi/common/policy-engine-spi/src/main/java/org/eclipse/edc/policy/engine/spi/plan/PolicyEvaluationPlan.java +100 −0
| @@ -0,0 +1,100 @@ | ||
| 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.policy.engine.spi.plan; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.policy.engine.spi.plan.step.DutyStep; | |
| 18 | +import org.eclipse.edc.policy.engine.spi.plan.step.PermissionStep; | |
| 19 | +import org.eclipse.edc.policy.engine.spi.plan.step.ProhibitionStep; | |
| 20 | +import org.eclipse.edc.policy.engine.spi.plan.step.ValidatorStep; | |
| 21 | +import org.eclipse.edc.policy.model.Policy; | |
| 22 | + | |
| 23 | +import java.util.ArrayList; | |
| 24 | +import java.util.List; | |
| 25 | + | |
| 26 | +/** | |
| 27 | + * The {@link PolicyEvaluationPlan} contains information about the evaluation process of a {@link Policy} | |
| 28 | + * withing a scope without executing it. | |
| 29 | + */ | |
| 30 | +public class PolicyEvaluationPlan { | |
| 31 | + | |
| 32 | + private final List<ValidatorStep> preValidators = new ArrayList<>(); | |
| 33 | + private final List<ValidatorStep> postValidators = new ArrayList<>(); | |
| 34 | + private final List<PermissionStep> permissionSteps = new ArrayList<>(); | |
| 35 | + private final List<ProhibitionStep> prohibitionSteps = new ArrayList<>(); | |
| 36 | + private final List<DutyStep> dutySteps = new ArrayList<>(); | |
| 37 | + | |
| 38 | + public List<ValidatorStep> getPostValidators() { | |
| 39 | + return postValidators; | |
| 40 | + } | |
| 41 | + | |
| 42 | + public List<ValidatorStep> getPreValidators() { | |
| 43 | + return preValidators; | |
| 44 | + } | |
| 45 | + | |
| 46 | + public List<PermissionStep> getPermissionSteps() { | |
| 47 | + return permissionSteps; | |
| 48 | + } | |
| 49 | + | |
| 50 | + public List<DutyStep> getDutySteps() { | |
| 51 | + return dutySteps; | |
| 52 | + } | |
| 53 | + | |
| 54 | + public List<ProhibitionStep> getProhibitionSteps() { | |
| 55 | + return prohibitionSteps; | |
| 56 | + } | |
| 57 | + | |
| 58 | + public static class Builder { | |
| 59 | + | |
| 60 | + private final PolicyEvaluationPlan plan = new PolicyEvaluationPlan(); | |
| 61 | + | |
| 62 | + private Builder() { | |
| 63 | + | |
| 64 | + } | |
| 65 | + | |
| 66 | + public static Builder newInstance() { | |
| 67 | + return new Builder(); | |
| 68 | + } | |
| 69 | + | |
| 70 | + public Builder preValidator(ValidatorStep validatorStep) { | |
| 71 | + plan.preValidators.add(validatorStep); | |
| 72 | + return this; | |
| 73 | + } | |
| 74 | + | |
| 75 | + | |
| 76 | + public Builder permission(PermissionStep permissionStep) { | |
| 77 | + plan.permissionSteps.add(permissionStep); | |
| 78 | + return this; | |
| 79 | + } | |
| 80 | + | |
| 81 | + public Builder prohibition(ProhibitionStep prohibitionStep) { | |
| 82 | + plan.prohibitionSteps.add(prohibitionStep); | |
| 83 | + return this; | |
| 84 | + } | |
| 85 | + | |
| 86 | + public Builder obligation(DutyStep dutyStep) { | |
| 87 | + plan.dutySteps.add(dutyStep); | |
| 88 | + return this; | |
| 89 | + } | |
| 90 | + | |
| 91 | + public Builder postValidator(ValidatorStep validatorStep) { | |
| 92 | + plan.postValidators.add(validatorStep); | |
| 93 | + return this; | |
| 94 | + } | |
| 95 | + | |
| 96 | + public PolicyEvaluationPlan build() { | |
| 97 | + return plan; | |
| 98 | + } | |
| 99 | + } | |
| 100 | +} |
added spi/common/policy-engine-spi/src/main/java/org/eclipse/edc/policy/engine/spi/plan/step/AndConstraintStep.java +29 −0
| @@ -0,0 +1,29 @@ | ||
| 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.policy.engine.spi.plan.step; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.policy.model.AndConstraint; | |
| 18 | + | |
| 19 | +import java.util.List; | |
| 20 | + | |
| 21 | +/** | |
| 22 | + * An evaluation step for {@link AndConstraint} | |
| 23 | + */ | |
| 24 | +public final class AndConstraintStep extends MultiplicityConstraintStep<AndConstraint> implements ConstraintStep { | |
| 25 | + | |
| 26 | + public AndConstraintStep(List<ConstraintStep> steps, AndConstraint constraint) { | |
| 27 | + super(steps, constraint); | |
| 28 | + } | |
| 29 | +} |
added spi/common/policy-engine-spi/src/main/java/org/eclipse/edc/policy/engine/spi/plan/step/AtomicConstraintStep.java +30 −0
| @@ -0,0 +1,30 @@ | ||
| 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.policy.engine.spi.plan.step; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.policy.engine.spi.AtomicConstraintFunction; | |
| 18 | +import org.eclipse.edc.policy.model.AtomicConstraint; | |
| 19 | +import org.eclipse.edc.policy.model.Rule; | |
| 20 | + | |
| 21 | +/** | |
| 22 | + * An evaluation step for {@link AtomicConstraint}. | |
| 23 | + * <p> | |
| 24 | + * The {@link AtomicConstraintStep} should be considered filtered when the left expression is not bound to a | |
| 25 | + * scope or an evaluation function {@link AtomicConstraintFunction} | |
| 26 | + */ | |
| 27 | +public record AtomicConstraintStep(AtomicConstraint constraint, boolean isFiltered, Rule rule, | |
| 28 | + AtomicConstraintFunction<? extends Rule> function) implements ConstraintStep { | |
| 29 | + | |
| 30 | +} |
added spi/common/policy-engine-spi/src/main/java/org/eclipse/edc/policy/engine/spi/plan/step/ConstraintStep.java +24 −0
| @@ -0,0 +1,24 @@ | ||
| 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.policy.engine.spi.plan.step; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.policy.model.Constraint; | |
| 18 | + | |
| 19 | +/** | |
| 20 | + * Base interface for {@link Constraint} step | |
| 21 | + */ | |
| 22 | +public sealed interface ConstraintStep permits AndConstraintStep, OrConstraintStep, XoneConstraintStep, AtomicConstraintStep { | |
| 23 | + | |
| 24 | +} |
added spi/common/policy-engine-spi/src/main/java/org/eclipse/edc/policy/engine/spi/plan/step/DutyStep.java +35 −0
| @@ -0,0 +1,35 @@ | ||
| 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.policy.engine.spi.plan.step; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.policy.model.Duty; | |
| 18 | + | |
| 19 | +/** | |
| 20 | + * An evaluation step for {@link Duty} rule; | |
| 21 | + */ | |
| 22 | +public class DutyStep extends RuleStep<Duty> { | |
| 23 | + | |
| 24 | + public static class Builder extends RuleStep.Builder<Duty, DutyStep, Builder> { | |
| 25 | + | |
| 26 | + private Builder() { | |
| 27 | + ruleStep = new DutyStep(); | |
| 28 | + } | |
| 29 | + | |
| 30 | + public static Builder newInstance() { | |
| 31 | + return new Builder(); | |
| 32 | + } | |
| 33 | + | |
| 34 | + } | |
| 35 | +} |
added spi/common/policy-engine-spi/src/main/java/org/eclipse/edc/policy/engine/spi/plan/step/MultiplicityConstraintStep.java +42 −0
| @@ -0,0 +1,42 @@ | ||
| 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.policy.engine.spi.plan.step; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.policy.model.MultiplicityConstraint; | |
| 18 | + | |
| 19 | +import java.util.List; | |
| 20 | + | |
| 21 | +/** | |
| 22 | + * Base evaluation step for {@link MultiplicityConstraint}. It carries the {@link MultiplicityConstraint} | |
| 23 | + * and the collection of child {@link ConstraintStep}. | |
| 24 | + */ | |
| 25 | +public abstract class MultiplicityConstraintStep<T extends MultiplicityConstraint> { | |
| 26 | + | |
| 27 | + private final List<ConstraintStep> steps; | |
| 28 | + private final T constraint; | |
| 29 | + | |
| 30 | + public MultiplicityConstraintStep(List<ConstraintStep> steps, T constraint) { | |
| 31 | + this.constraint = constraint; | |
| 32 | + this.steps = steps; | |
| 33 | + } | |
| 34 | + | |
| 35 | + public List<ConstraintStep> getSteps() { | |
| 36 | + return steps; | |
| 37 | + } | |
| 38 | + | |
| 39 | + public T getConstraint() { | |
| 40 | + return constraint; | |
| 41 | + } | |
| 42 | +} |
added spi/common/policy-engine-spi/src/main/java/org/eclipse/edc/policy/engine/spi/plan/step/OrConstraintStep.java +29 −0
| @@ -0,0 +1,29 @@ | ||
| 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.policy.engine.spi.plan.step; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.policy.model.OrConstraint; | |
| 18 | + | |
| 19 | +import java.util.List; | |
| 20 | + | |
| 21 | +/** | |
| 22 | + * An evaluation step for {@link OrConstraint} | |
| 23 | + */ | |
| 24 | +public final class OrConstraintStep extends MultiplicityConstraintStep<OrConstraint> implements ConstraintStep { | |
| 25 | + | |
| 26 | + public OrConstraintStep(List<ConstraintStep> steps, OrConstraint constraint) { | |
| 27 | + super(steps, constraint); | |
| 28 | + } | |
| 29 | +} |
added spi/common/policy-engine-spi/src/main/java/org/eclipse/edc/policy/engine/spi/plan/step/PermissionStep.java +50 −0
| @@ -0,0 +1,50 @@ | ||
| 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.policy.engine.spi.plan.step; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.policy.model.Permission; | |
| 18 | + | |
| 19 | +import java.util.ArrayList; | |
| 20 | +import java.util.List; | |
| 21 | + | |
| 22 | +/** | |
| 23 | + * An evaluation step for {@link Permission} rule; | |
| 24 | + */ | |
| 25 | +public class PermissionStep extends RuleStep<Permission> { | |
| 26 | + | |
| 27 | + private final List<DutyStep> dutySteps = new ArrayList<>(); | |
| 28 | + | |
| 29 | + public List<DutyStep> getDutySteps() { | |
| 30 | + return dutySteps; | |
| 31 | + } | |
| 32 | + | |
| 33 | + public static class Builder extends RuleStep.Builder<Permission, PermissionStep, Builder> { | |
| 34 | + | |
| 35 | + private Builder() { | |
| 36 | + ruleStep = new PermissionStep(); | |
| 37 | + } | |
| 38 | + | |
| 39 | + public static Builder newInstance() { | |
| 40 | + return new Builder(); | |
| 41 | + } | |
| 42 | + | |
| 43 | + public Builder dutyStep(DutyStep dutyStep) { | |
| 44 | + ruleStep.dutySteps.add(dutyStep); | |
| 45 | + return this; | |
| 46 | + } | |
| 47 | + } | |
| 48 | + | |
| 49 | + | |
| 50 | +} |
added spi/common/policy-engine-spi/src/main/java/org/eclipse/edc/policy/engine/spi/plan/step/ProhibitionStep.java +37 −0
| @@ -0,0 +1,37 @@ | ||
| 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.policy.engine.spi.plan.step; | |
| 16 | + | |
| 17 | +import com.fasterxml.jackson.annotation.JsonCreator; | |
| 18 | +import org.eclipse.edc.policy.model.Prohibition; | |
| 19 | + | |
| 20 | +/** | |
| 21 | + * An evaluation step for {@link Prohibition} rule; | |
| 22 | + */ | |
| 23 | +public class ProhibitionStep extends RuleStep<Prohibition> { | |
| 24 | + | |
| 25 | + public static class Builder extends RuleStep.Builder<Prohibition, ProhibitionStep, Builder> { | |
| 26 | + | |
| 27 | + private Builder() { | |
| 28 | + ruleStep = new ProhibitionStep(); | |
| 29 | + } | |
| 30 | + | |
| 31 | + @JsonCreator | |
| 32 | + public static Builder newInstance() { | |
| 33 | + return new Builder(); | |
| 34 | + } | |
| 35 | + | |
| 36 | + } | |
| 37 | +} |
added spi/common/policy-engine-spi/src/main/java/org/eclipse/edc/policy/engine/spi/plan/step/RuleFunctionStep.java +25 −0
| @@ -0,0 +1,25 @@ | ||
| 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.policy.engine.spi.plan.step; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.policy.engine.spi.RuleFunction; | |
| 18 | +import org.eclipse.edc.policy.model.Rule; | |
| 19 | + | |
| 20 | +/** | |
| 21 | + * An evaluation step for {@link RuleFunction} associated to a {@link Rule} | |
| 22 | + */ | |
| 23 | +public record RuleFunctionStep<R extends Rule>(RuleFunction<R> function, R rule) { | |
| 24 | + | |
| 25 | +} | |
| No newline at the end of the file |
added spi/common/policy-engine-spi/src/main/java/org/eclipse/edc/policy/engine/spi/plan/step/RuleStep.java +80 −0
| @@ -0,0 +1,80 @@ | ||
| 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.policy.engine.spi.plan.step; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.policy.model.Rule; | |
| 18 | + | |
| 19 | +import java.util.ArrayList; | |
| 20 | +import java.util.List; | |
| 21 | +import java.util.Objects; | |
| 22 | + | |
| 23 | +/** | |
| 24 | + * Base step class for {@link Rule}s evaluation. A rule can have multiple {@link ConstraintStep} | |
| 25 | + * and {@link RuleFunctionStep} associated during the evaluation process. | |
| 26 | + * A rule step is considered filtered (not evaluated) when the rule is not bound to a scope. | |
| 27 | + */ | |
| 28 | +public abstract class RuleStep<R extends Rule> { | |
| 29 | + | |
| 30 | + protected R rule; | |
| 31 | + protected boolean isFiltered = false; | |
| 32 | + | |
| 33 | + protected List<ConstraintStep> constraintSteps = new ArrayList<>(); | |
| 34 | + | |
| 35 | + protected List<RuleFunctionStep<R>> ruleFunctions = new ArrayList<>(); | |
| 36 | + | |
| 37 | + public List<ConstraintStep> getConstraintSteps() { | |
| 38 | + return constraintSteps; | |
| 39 | + } | |
| 40 | + | |
| 41 | + public List<RuleFunctionStep<R>> getRuleFunctions() { | |
| 42 | + return ruleFunctions; | |
| 43 | + } | |
| 44 | + | |
| 45 | + public boolean isFiltered() { | |
| 46 | + return isFiltered; | |
| 47 | + } | |
| 48 | + | |
| 49 | + @SuppressWarnings("unchecked") | |
| 50 | + public abstract static class Builder<R extends Rule, T extends RuleStep<R>, B extends Builder<R, T, B>> { | |
| 51 | + protected T ruleStep; | |
| 52 | + | |
| 53 | + | |
| 54 | + public B rule(R rule) { | |
| 55 | + ruleStep.rule = rule; | |
| 56 | + return (B) this; | |
| 57 | + } | |
| 58 | + | |
| 59 | + public B constraint(ConstraintStep constraint) { | |
| 60 | + ruleStep.constraintSteps.add(constraint); | |
| 61 | + return (B) this; | |
| 62 | + } | |
| 63 | + | |
| 64 | + public B ruleFunction(RuleFunctionStep<R> function) { | |
| 65 | + ruleStep.ruleFunctions.add(function); | |
| 66 | + return (B) this; | |
| 67 | + } | |
| 68 | + | |
| 69 | + public B filtered(boolean isFiltered) { | |
| 70 | + ruleStep.isFiltered = isFiltered; | |
| 71 | + return (B) this; | |
| 72 | + } | |
| 73 | + | |
| 74 | + public T build() { | |
| 75 | + Objects.requireNonNull(ruleStep.rule, "Rule should not be null"); | |
| 76 | + return ruleStep; | |
| 77 | + } | |
| 78 | + | |
| 79 | + } | |
| 80 | +} | |
| No newline at the end of the file |
added spi/common/policy-engine-spi/src/main/java/org/eclipse/edc/policy/engine/spi/plan/step/ValidatorStep.java +27 −0
| @@ -0,0 +1,27 @@ | ||
| 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.policy.engine.spi.plan.step; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.policy.engine.spi.PolicyContext; | |
| 18 | +import org.eclipse.edc.policy.model.Policy; | |
| 19 | + | |
| 20 | +import java.util.function.BiFunction; | |
| 21 | + | |
| 22 | +/** | |
| 23 | + * An evaluation step for pre- and post-validators invoked during the evaluation process. | |
| 24 | + */ | |
| 25 | +public record ValidatorStep(BiFunction<Policy, PolicyContext, Boolean> validator) { | |
| 26 | + | |
| 27 | +} |
added spi/common/policy-engine-spi/src/main/java/org/eclipse/edc/policy/engine/spi/plan/step/XoneConstraintStep.java +29 −0
| @@ -0,0 +1,29 @@ | ||
| 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.policy.engine.spi.plan.step; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.policy.model.XoneConstraint; | |
| 18 | + | |
| 19 | +import java.util.List; | |
| 20 | + | |
| 21 | +/** | |
| 22 | + * An evaluation step for {@link XoneConstraint} | |
| 23 | + */ | |
| 24 | +public final class XoneConstraintStep extends MultiplicityConstraintStep<XoneConstraint> implements ConstraintStep { | |
| 25 | + | |
| 26 | + public XoneConstraintStep(List<ConstraintStep> steps, XoneConstraint constraint) { | |
| 27 | + super(steps, constraint); | |
| 28 | + } | |
| 29 | +} |