Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/59670 policy time function extension #75

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions edc/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ WORKDIR /app

# Copy the build files
COPY gradle/* .
COPY gradle/extensions extensions

# Setup wrapper
RUN gradle wrapper
Expand Down
2 changes: 2 additions & 0 deletions edc/gradle/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ dependencies {
implementation("$groupId:data-plane-core:$edcVersion")

implementation("$groupId:transfer-data-plane:$edcVersion")

implementation(project(":extensions:policy-contract-negotiation-policy-functions"))
}

application {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins {
`java-library`
id("application")
}

val groupId: String by project
val edcVersion: String by project

dependencies {
implementation("$groupId:runtime-metamodel:$edcVersion")

implementation("$groupId:data-plane-spi:$edcVersion")
implementation("$groupId:control-plane-core:$edcVersion")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering
*
* 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:
* Fraunhofer Institute for Software and Systems Engineering - initial API and implementation
*
*/

package org.eclipse.edc.sample.extension.policy;


import org.eclipse.edc.policy.engine.spi.PolicyEngine;
import org.eclipse.edc.policy.engine.spi.RuleBindingRegistry;
import org.eclipse.edc.policy.model.Permission;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;

import static org.eclipse.edc.policy.engine.spi.PolicyEngine.ALL_SCOPES;

public class PolicyFunctionsExtension implements ServiceExtension {
private final String policyTimeKey = "POLICY_EVALUATION_TIME";

@Inject
private RuleBindingRegistry ruleBindingRegistry;

@Inject
private PolicyEngine policyEngine;


@Override
public String name() {
return "Policy - contract-negotiation policies";
}

@Override
public void initialize(ServiceExtensionContext context) {
var monitor = context.getMonitor();

ruleBindingRegistry.bind("USE", ALL_SCOPES);
ruleBindingRegistry.bind(policyTimeKey, ALL_SCOPES);
policyEngine.registerFunction(ALL_SCOPES, Permission.class, policyTimeKey, new TimeIntervalFunction(monitor));

context.getMonitor().info("Policy Extension for date and time restrictions initialized!");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering
*
* 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:
* Fraunhofer Institute for Software and Systems Engineering - initial API and implementation
*
*/

package org.eclipse.edc.sample.extension.policy;

import org.eclipse.edc.policy.engine.spi.AtomicConstraintFunction;
import org.eclipse.edc.policy.engine.spi.PolicyContext;
import org.eclipse.edc.policy.model.Operator;
import org.eclipse.edc.policy.model.Permission;
import org.eclipse.edc.spi.monitor.Monitor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeIntervalFunction implements AtomicConstraintFunction<Permission> {

private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
private final Monitor monitor;

public TimeIntervalFunction(Monitor monitor) {
this.monitor = monitor;
}



@Override
public boolean evaluate(Operator operator, Object rightValue, Permission rule, PolicyContext context) {
try {
var policyDate = DATE_FORMAT.parse((String) rightValue);
var nowDate = new Date();
return switch (operator) {
case LT -> nowDate.before(policyDate);
case LEQ -> nowDate.before(policyDate) || nowDate.equals(policyDate);
case GT -> nowDate.after(policyDate);
case GEQ -> nowDate.after(policyDate) || nowDate.equals(policyDate);
case EQ -> nowDate.equals(policyDate);
case NEQ -> !nowDate.equals(policyDate);
default -> false;
};
} catch (ParseException e) {
monitor.severe("Failed to parse right value of constraint to date.");
return false;
}
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.eclipse.edc.sample.extension.policy.PolicyFunctionsExtension
2 changes: 1 addition & 1 deletion edc/gradle/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
groupId=org.eclipse.edc
edcVersion=0.1.0
edcVersion=0.1.2
defaultVersion=0.0.1-SNAPSHOT
javaVersion=11
2 changes: 2 additions & 0 deletions edc/gradle/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ dependencyResolutionManagement {
mavenLocal()
}
}

include(":extensions:policy-contract-negotiation-policy-functions")