Skip to content

Commit

Permalink
feat: define policy.monitor policy scope
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt committed Oct 6, 2023
1 parent 9d39f5c commit c5e8029
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
*/

package org.eclipse.edc.connector.contract.validation;
package org.eclipse.edc.connector.core.policy;

import org.eclipse.edc.connector.contract.spi.types.agreement.ContractAgreement;
import org.eclipse.edc.policy.engine.spi.AtomicConstraintFunction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
*/

package org.eclipse.edc.connector.contract.validation;
package org.eclipse.edc.connector.core.policy;

import org.eclipse.edc.connector.contract.spi.types.agreement.ContractAgreement;
import org.eclipse.edc.junit.annotations.ComponentTest;
Expand Down Expand Up @@ -45,7 +45,7 @@
import static java.time.Duration.ofSeconds;
import static java.time.Instant.now;
import static org.eclipse.edc.connector.contract.spi.validation.ContractValidationService.TRANSFER_SCOPE;
import static org.eclipse.edc.connector.contract.validation.ContractExpiryCheckFunction.CONTRACT_EXPIRY_EVALUATION_KEY;
import static org.eclipse.edc.connector.core.policy.ContractExpiryCheckFunction.CONTRACT_EXPIRY_EVALUATION_KEY;
import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat;
import static org.eclipse.edc.policy.model.Operator.EQ;
import static org.eclipse.edc.policy.model.Operator.GEQ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import org.eclipse.edc.connector.contract.spi.offer.ContractDefinitionResolver;
import org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiation;
import org.eclipse.edc.connector.contract.spi.validation.ContractValidationService;
import org.eclipse.edc.connector.contract.validation.ContractExpiryCheckFunction;
import org.eclipse.edc.connector.contract.validation.ContractValidationServiceImpl;
import org.eclipse.edc.connector.core.policy.ContractExpiryCheckFunction;
import org.eclipse.edc.connector.policy.spi.store.PolicyDefinitionStore;
import org.eclipse.edc.policy.engine.spi.PolicyEngine;
import org.eclipse.edc.policy.engine.spi.RuleBindingRegistry;
Expand Down Expand Up @@ -59,11 +59,11 @@
import java.time.Clock;

import static org.eclipse.edc.connector.contract.spi.validation.ContractValidationService.TRANSFER_SCOPE;
import static org.eclipse.edc.connector.contract.validation.ContractExpiryCheckFunction.CONTRACT_EXPIRY_EVALUATION_KEY;
import static org.eclipse.edc.connector.core.entity.AbstractStateEntityManager.DEFAULT_BATCH_SIZE;
import static org.eclipse.edc.connector.core.entity.AbstractStateEntityManager.DEFAULT_ITERATION_WAIT;
import static org.eclipse.edc.connector.core.entity.AbstractStateEntityManager.DEFAULT_SEND_RETRY_BASE_DELAY;
import static org.eclipse.edc.connector.core.entity.AbstractStateEntityManager.DEFAULT_SEND_RETRY_LIMIT;
import static org.eclipse.edc.connector.core.policy.ContractExpiryCheckFunction.CONTRACT_EXPIRY_EVALUATION_KEY;
import static org.eclipse.edc.policy.model.OdrlNamespace.ODRL_SCHEMA;

@Provides({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package org.eclipse.edc.connector.policy.monitor;

import org.eclipse.edc.connector.core.policy.ContractExpiryCheckFunction;
import org.eclipse.edc.connector.policy.monitor.manager.PolicyMonitorManagerImpl;
import org.eclipse.edc.connector.policy.monitor.spi.PolicyMonitorManager;
import org.eclipse.edc.connector.policy.monitor.spi.PolicyMonitorStore;
Expand All @@ -22,6 +23,8 @@
import org.eclipse.edc.connector.spi.transferprocess.TransferProcessService;
import org.eclipse.edc.connector.transfer.spi.event.TransferProcessStarted;
import org.eclipse.edc.policy.engine.spi.PolicyEngine;
import org.eclipse.edc.policy.engine.spi.PolicyScope;
import org.eclipse.edc.policy.model.Permission;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.runtime.metamodel.annotation.Provides;
Expand All @@ -37,6 +40,7 @@

import static org.eclipse.edc.connector.core.entity.AbstractStateEntityManager.DEFAULT_BATCH_SIZE;
import static org.eclipse.edc.connector.core.entity.AbstractStateEntityManager.DEFAULT_ITERATION_WAIT;
import static org.eclipse.edc.connector.core.policy.ContractExpiryCheckFunction.CONTRACT_EXPIRY_EVALUATION_KEY;
import static org.eclipse.edc.connector.policy.monitor.PolicyMonitorExtension.NAME;

@Extension(value = NAME)
Expand All @@ -51,6 +55,9 @@ public class PolicyMonitorExtension implements ServiceExtension {
@Setting(value = "the batch size in the policy monitor state machine. Default value " + DEFAULT_BATCH_SIZE, type = "int")
private static final String POLICY_MONITOR_BATCH_SIZE = "edc.policy.monitor.state-machine.batch-size";

@PolicyScope
public static final String POLICY_MONITOR_SCOPE = "policy.monitor";

@Inject
private ExecutorInstrumentation executorInstrumentation;

Expand Down Expand Up @@ -82,6 +89,8 @@ public void initialize(ServiceExtensionContext context) {
var iterationWaitMillis = context.getSetting(POLICY_MONITOR_ITERATION_WAIT_MILLIS, DEFAULT_ITERATION_WAIT);
var waitStrategy = new ExponentialWaitStrategy(iterationWaitMillis);

policyEngine.registerFunction(POLICY_MONITOR_SCOPE, Permission.class, CONTRACT_EXPIRY_EVALUATION_KEY, new ContractExpiryCheckFunction());

manager = PolicyMonitorManagerImpl.Builder.newInstance()
.clock(clock)
.batchSize(context.getSetting(POLICY_MONITOR_BATCH_SIZE, DEFAULT_BATCH_SIZE))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.time.Instant;
import java.util.function.Function;

import static org.eclipse.edc.connector.policy.monitor.PolicyMonitorExtension.POLICY_MONITOR_SCOPE;
import static org.eclipse.edc.connector.policy.monitor.spi.PolicyMonitorEntryStates.STARTED;
import static org.eclipse.edc.spi.persistence.StateEntityStore.hasState;

Expand Down Expand Up @@ -83,7 +84,7 @@ private boolean processMonitoring(PolicyMonitorEntry entry) {
.additional(ContractAgreement.class, contractAgreement)
.build();

var result = policyEngine.evaluate("transfer.process", policy, policyContext);
var result = policyEngine.evaluate(POLICY_MONITOR_SCOPE, policy, policyContext);
if (result.failed()) {
monitor.debug(() -> "[policy-monitor] Policy evaluation for TP %s failed: %s".formatted(entry.getId(), result.getFailureDetail()));
var command = new TerminateTransferCommand(entry.getId(), result.getFailureDetail());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.edc.connector.policy.monitor;

import org.eclipse.edc.connector.core.policy.ContractExpiryCheckFunction;
import org.eclipse.edc.junit.extensions.DependencyInjectionExtension;
import org.eclipse.edc.policy.engine.spi.PolicyEngine;
import org.eclipse.edc.policy.model.Permission;
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.core.policy.ContractExpiryCheckFunction.CONTRACT_EXPIRY_EVALUATION_KEY;
import static org.eclipse.edc.connector.policy.monitor.PolicyMonitorExtension.POLICY_MONITOR_SCOPE;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

@ExtendWith(DependencyInjectionExtension.class)
class PolicyMonitorExtensionTest {

private final PolicyEngine policyEngine = mock();

@BeforeEach
void setUp(ServiceExtensionContext context) {
context.registerService(PolicyEngine.class, policyEngine);
}

@Test
void shouldRegisterExpiryFunctionToPolicyEngine(PolicyMonitorExtension extension, ServiceExtensionContext context) {
extension.initialize(context);

verify(policyEngine).registerFunction(eq(POLICY_MONITOR_SCOPE), eq(Permission.class), eq(CONTRACT_EXPIRY_EVALUATION_KEY), isA(ContractExpiryCheckFunction.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static java.util.Collections.emptyList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.eclipse.edc.connector.policy.monitor.PolicyMonitorExtension.POLICY_MONITOR_SCOPE;
import static org.eclipse.edc.connector.policy.monitor.spi.PolicyMonitorEntryStates.COMPLETED;
import static org.eclipse.edc.connector.policy.monitor.spi.PolicyMonitorEntryStates.FAILED;
import static org.eclipse.edc.connector.policy.monitor.spi.PolicyMonitorEntryStates.STARTED;
Expand Down Expand Up @@ -105,7 +106,7 @@ void started_shouldTerminateTransferAndTransitionToComplete_whenPolicyIsNotValid
await().untilAsserted(() -> {
verify(contractAgreementService).findById("contractId");
var captor = ArgumentCaptor.forClass(PolicyContextImpl.class);
verify(policyEngine).evaluate(eq("transfer.process"), same(policy), captor.capture());
verify(policyEngine).evaluate(eq(POLICY_MONITOR_SCOPE), same(policy), captor.capture());
var policyContext = captor.getValue();
assertThat(policyContext.getContextData(ContractAgreement.class)).isSameAs(contractAgreement);
verify(transferProcessService).terminate(argThat(c -> c.getEntityId().equals("transferProcessId")));
Expand Down

0 comments on commit c5e8029

Please sign in to comment.