From e10845684c65f2a1e99d31615df141cd318562bc Mon Sep 17 00:00:00 2001 From: Jorge Hernandez Date: Wed, 19 Jul 2017 18:54:02 -0500 Subject: [POLICY-88] validation of query pre-execution Change-Id: Ie02de9aca434c3d80b9e34126990e3b9995747bb Signed-off-by: Jorge Hernandez --- .../policy/drools/controller/DroolsController.java | 6 ++++++ .../controller/internal/MavenDroolsController.java | 19 ++++++++++++++++--- .../controller/internal/NullDroolsController.java | 14 +++++++++++--- 3 files changed, 33 insertions(+), 6 deletions(-) (limited to 'policy-management') diff --git a/policy-management/src/main/java/org/openecomp/policy/drools/controller/DroolsController.java b/policy-management/src/main/java/org/openecomp/policy/drools/controller/DroolsController.java index 5a5f3123..251cef11 100644 --- a/policy-management/src/main/java/org/openecomp/policy/drools/controller/DroolsController.java +++ b/policy-management/src/main/java/org/openecomp/policy/drools/controller/DroolsController.java @@ -23,6 +23,7 @@ package org.openecomp.policy.drools.controller; import java.util.List; import java.util.Map; +import org.openecomp.policy.drools.core.PolicyContainer; import org.openecomp.policy.drools.event.comm.TopicSink; import org.openecomp.policy.drools.properties.Lockable; import org.openecomp.policy.drools.properties.Startable; @@ -121,6 +122,11 @@ public interface DroolsController extends Startable, Lockable { */ public String[] getRecentSinkEvents(); + /** + * @return the underlying policy container + */ + public PolicyContainer getContainer(); + /** * Supports this encoder? * diff --git a/policy-management/src/main/java/org/openecomp/policy/drools/controller/internal/MavenDroolsController.java b/policy-management/src/main/java/org/openecomp/policy/drools/controller/internal/MavenDroolsController.java index bc577649..4eacbd1c 100644 --- a/policy-management/src/main/java/org/openecomp/policy/drools/controller/internal/MavenDroolsController.java +++ b/policy-management/src/main/java/org/openecomp/policy/drools/controller/internal/MavenDroolsController.java @@ -28,6 +28,8 @@ import java.util.Map; import org.apache.commons.collections4.queue.CircularFifoQueue; import org.drools.core.ClassObjectFilter; +import org.kie.api.definition.KiePackage; +import org.kie.api.definition.rule.Query; import org.kie.api.runtime.KieSession; import org.kie.api.runtime.rule.FactHandle; import org.kie.api.runtime.rule.QueryResults; @@ -649,11 +651,10 @@ public class MavenDroolsController implements DroolsController { } /** - * gets the policy container - * @return the underlying container + * {@inheritDoc} */ @JsonIgnore - protected PolicyContainer getContainer() { + public PolicyContainer getContainer() { return this.policyContainer; } @@ -824,6 +825,18 @@ public class MavenDroolsController implements DroolsController { PolicySession session = getSession(sessionName); KieSession kieSession = session.getKieSession(); + boolean found = false; + for (KiePackage kiePackage : kieSession.getKieBase().getKiePackages()) { + for (Query q : kiePackage.getQueries()) { + if (q.getName() != null && q.getName().equals(queryName)) { + found = true; + break; + } + } + } + if (!found) + throw new IllegalArgumentException("Invalid Query Name: " + queryName); + List factObjects = new ArrayList<>(); QueryResults queryResults = kieSession.getQueryResults(queryName, queryParams); diff --git a/policy-management/src/main/java/org/openecomp/policy/drools/controller/internal/NullDroolsController.java b/policy-management/src/main/java/org/openecomp/policy/drools/controller/internal/NullDroolsController.java index 4019beef..7f5d4665 100644 --- a/policy-management/src/main/java/org/openecomp/policy/drools/controller/internal/NullDroolsController.java +++ b/policy-management/src/main/java/org/openecomp/policy/drools/controller/internal/NullDroolsController.java @@ -26,6 +26,7 @@ import java.util.List; import java.util.Map; import org.openecomp.policy.drools.controller.DroolsController; +import org.openecomp.policy.drools.core.PolicyContainer; import org.openecomp.policy.drools.event.comm.TopicSink; import org.openecomp.policy.drools.protocol.coders.TopicCoderFilterConfiguration; @@ -152,7 +153,7 @@ public class NullDroolsController implements DroolsController { @Override public boolean deliver(TopicSink sink, Object event) throws IllegalArgumentException, IllegalStateException, UnsupportedOperationException { - throw new IllegalArgumentException(this.getClass().getCanonicalName() + " invoked"); + throw new IllegalStateException(this.getClass().getCanonicalName() + " invoked"); } /** @@ -162,6 +163,14 @@ public class NullDroolsController implements DroolsController { public Object[] getRecentSourceEvents() { return new String[0]; } + + /** + * {@inheritDoc} + */ + @Override + public PolicyContainer getContainer() { + return null; + } /** * {@inheritDoc} @@ -176,7 +185,7 @@ public class NullDroolsController implements DroolsController { */ @Override public boolean ownsCoder(Class coderClass, int modelHash) throws IllegalStateException { - throw new IllegalArgumentException(this.getClass().getCanonicalName() + " invoked"); + throw new IllegalStateException(this.getClass().getCanonicalName() + " invoked"); } /** @@ -250,5 +259,4 @@ public class NullDroolsController implements DroolsController { boolean delete, Object... queryParams) { return new ArrayList(); } - } -- cgit 1.2.3-korg