From 712274e8d332181729c06c31dcf58d49f2a4d8f7 Mon Sep 17 00:00:00 2001 From: jhh Date: Tue, 9 Apr 2019 14:24:34 -0500 Subject: deploy and undeploy as audits. Change-Id: Iea1766d5d8075394e1dccc109f4477bd2c270c36 Issue-ID: POLICY-1624 Signed-off-by: jhh --- .../policy/drools/controller/DroolsController.java | 17 +++++++++- .../controller/internal/MavenDroolsController.java | 39 ++++++++++++++++++++++ .../controller/internal/NullDroolsController.java | 16 +++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) (limited to 'policy-management/src/main') diff --git a/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsController.java b/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsController.java index 344725ff..78a2f4e8 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsController.java +++ b/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsController.java @@ -22,7 +22,7 @@ package org.onap.policy.drools.controller; import java.util.List; import java.util.Map; - +import org.checkerframework.checker.nullness.qual.NonNull; import org.onap.policy.common.capabilities.Lockable; import org.onap.policy.common.capabilities.Startable; import org.onap.policy.common.endpoints.event.comm.TopicSink; @@ -214,6 +214,11 @@ public interface DroolsController extends Startable, Lockable { */ List facts(String sessionName, String className, boolean delete); + /** + * Gets facts. + */ + List facts(@NonNull String sessionName, @NonNull Class clazz); + /** * gets the facts associated with a query for a give session for a given queried entity. * @@ -227,6 +232,16 @@ public interface DroolsController extends Startable, Lockable { List factQuery(String sessionName, String queryName, String queriedEntity, boolean delete, Object... queryParams); + /** + * Deletes a fact from a session. + */ + boolean delete(@NonNull String sessionName, @NonNull T fact); + + /** + * Delete a fact object of type T. + */ + boolean delete(@NonNull T fact); + /** * halts and permanently releases all resources. * diff --git a/policy-management/src/main/java/org/onap/policy/drools/controller/internal/MavenDroolsController.java b/policy-management/src/main/java/org/onap/policy/drools/controller/internal/MavenDroolsController.java index 95b053fb..a9ab212b 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/controller/internal/MavenDroolsController.java +++ b/policy-management/src/main/java/org/onap/policy/drools/controller/internal/MavenDroolsController.java @@ -27,7 +27,10 @@ import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; import org.apache.commons.collections4.queue.CircularFifoQueue; +import org.checkerframework.checker.nullness.qual.NonNull; import org.drools.core.ClassObjectFilter; import org.kie.api.definition.KiePackage; import org.kie.api.definition.rule.Query; @@ -832,6 +835,15 @@ public class MavenDroolsController implements DroolsController { return factObjects; } + @Override + public List facts(@NonNull String sessionName, @NonNull Class clazz) { + return facts(sessionName, clazz.getCanonicalName(), false) + .stream() + .filter(clazz::isInstance) + .map(clazz::cast) + .collect(Collectors.toList()); + } + @Override public List factQuery(String sessionName, String queryName, String queriedEntity, boolean delete, Object... queryParams) { @@ -880,6 +892,33 @@ public class MavenDroolsController implements DroolsController { return factObjects; } + @Override + public boolean delete(@NonNull String sessionName, @NonNull T fact) { + String factClassName = fact.getClass().getName(); + + PolicySession session = getSession(sessionName); + KieSession kieSession = session.getKieSession(); + + Collection factHandles = kieSession.getFactHandles(new ClassObjectFilter(fact.getClass())); + for (FactHandle factHandle : factHandles) { + try { + if (Objects.equals(fact, kieSession.getObject(factHandle))) { + logger.info("Deleting {} from {}", factClassName, sessionName); + kieSession.delete(factHandle); + return true; + } + } catch (Exception e) { + logger.warn("Object cannot be retrieved from fact {}", factHandle, e); + } + } + return false; + } + + @Override + public boolean delete(@NonNull T fact) { + return this.getSessionNames().stream().map((ss) -> delete(ss, fact)).reduce(false, Boolean::logicalOr); + } + @Override public Class fetchModelClass(String className) { return ReflectionUtil.fetchClass(this.policyContainer.getClassLoader(), className); diff --git a/policy-management/src/main/java/org/onap/policy/drools/controller/internal/NullDroolsController.java b/policy-management/src/main/java/org/onap/policy/drools/controller/internal/NullDroolsController.java index 815aaabb..5bf51efa 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/controller/internal/NullDroolsController.java +++ b/policy-management/src/main/java/org/onap/policy/drools/controller/internal/NullDroolsController.java @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.checkerframework.checker.nullness.qual.NonNull; import org.onap.policy.common.endpoints.event.comm.TopicSink; import org.onap.policy.drools.controller.DroolsController; import org.onap.policy.drools.core.PolicyContainer; @@ -180,6 +181,11 @@ public class NullDroolsController implements DroolsController { return new ArrayList<>(); } + @Override + public List facts(@NonNull String sessionName, @NonNull Class clazz) { + return new ArrayList<>(); + } + @Override public List factQuery(String sessionName, String queryName, String queriedEntity, @@ -187,6 +193,16 @@ public class NullDroolsController implements DroolsController { return new ArrayList<>(); } + @Override + public boolean delete(@NonNull String sessionName, @NonNull T fact) { + return false; + } + + @Override + public boolean delete(@NonNull T fact) { + return false; + } + private String makeInvokeMsg() { return this.getClass().getCanonicalName() + " invoked"; } -- cgit 1.2.3-korg