From d697d828df129afa52555e9381d69c45d526c7d6 Mon Sep 17 00:00:00 2001 From: Parshad Patel Date: Mon, 26 Nov 2018 18:22:40 +0900 Subject: Rename test classes in drools-applications Make test classes name consitence by adding 'Support' or 'Dummy' at start of util or dummy type of test classes Issue-ID: POLICY-1258 Change-Id: Idea946375ee46f14512d4c4bc29e65adaa4d2b37 Signed-off-by: Parshad Patel --- .../demo/clc/ControlLoopCoordinationTest.java | 14 +- .../demo/clc/ControlLoopEventCleanupTest.java | 34 +- .../demo/clc/ControlLoopParamsCleanupTest.java | 20 +- .../onap/policy/template/demo/clc/SupportUtil.java | 367 ++++++++++++++++++ .../org/onap/policy/template/demo/clc/Util.java | 367 ------------------ .../policy/template/demo/CcvpnControlLoopTest.java | 14 +- .../template/demo/ControlLoopEventCleanupTest.java | 34 +- .../template/demo/ControlLoopFailureTest.java | 16 +- .../demo/ControlLoopParamsCleanupTest.java | 28 +- .../org/onap/policy/template/demo/SupportUtil.java | 412 +++++++++++++++++++++ .../java/org/onap/policy/template/demo/Util.java | 412 --------------------- .../policy/template/demo/VcpeControlLoopTest.java | 16 +- .../policy/template/demo/VdnsControlLoopTest.java | 20 +- .../policy/template/demo/VfcControlLoopTest.java | 20 +- .../policy/template/demo/VfwControlLoopTest.java | 16 +- .../policy/template/demo/VpciControlLoopTest.java | 16 +- 16 files changed, 903 insertions(+), 903 deletions(-) create mode 100644 controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/SupportUtil.java delete mode 100644 controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/Util.java create mode 100644 controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/SupportUtil.java delete mode 100644 controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/Util.java (limited to 'controlloop/templates') diff --git a/controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/ControlLoopCoordinationTest.java b/controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/ControlLoopCoordinationTest.java index 588d00563..d4af9ea4d 100644 --- a/controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/ControlLoopCoordinationTest.java +++ b/controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/ControlLoopCoordinationTest.java @@ -82,9 +82,9 @@ public class ControlLoopCoordinationTest implements TopicListener { static { /* Set environment properties */ - Util.setAaiProps(); - Util.setGuardPropsEmbedded(); - Util.setPuProp(); + SupportUtil.setAaiProps(); + SupportUtil.setGuardPropsEmbedded(); + SupportUtil.setPuProp(); } /** @@ -124,7 +124,7 @@ public class ControlLoopCoordinationTest implements TopicListener { .customJacksonCoder(null) .modelClassLoaderHash(1111)); try { - Util.buildAaiSim(); + SupportUtil.buildAaiSim(); } catch (Exception e) { fail(e.getMessage()); } @@ -244,7 +244,7 @@ public class ControlLoopCoordinationTest implements TopicListener { // // get dump of database entries and log // - List entries = Util.dumpDb(); + List entries = SupportUtil.dumpDb(); assertNotNull(entries); logger.debug("dumpDB, {} entries", entries.size()); for (Object entry : entries) { @@ -307,7 +307,7 @@ public class ControlLoopCoordinationTest implements TopicListener { /* * Load policies from yaml */ - Util.Pair pair = Util.loadYaml(yamlFile); + SupportUtil.Pair pair = SupportUtil.loadYaml(yamlFile); assertNotNull(pair); assertNotNull(pair.first); assertNotNull(pair.first.getControlLoop()); @@ -320,7 +320,7 @@ public class ControlLoopCoordinationTest implements TopicListener { /* * Construct a kie session */ - final KieSession kieSession = Util.buildContainer(droolsTemplate, + final KieSession kieSession = SupportUtil.buildContainer(droolsTemplate, controlLoopName.toString(), policyScope, policyName, diff --git a/controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/ControlLoopEventCleanupTest.java b/controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/ControlLoopEventCleanupTest.java index ef5fac693..e034990fe 100644 --- a/controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/ControlLoopEventCleanupTest.java +++ b/controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/ControlLoopEventCleanupTest.java @@ -52,7 +52,7 @@ import org.onap.policy.drools.protocol.coders.JsonProtocolFilter; import org.onap.policy.drools.system.PolicyController; import org.onap.policy.drools.system.PolicyEngine; import org.onap.policy.drools.utils.logging.LoggerUtil; -import org.onap.policy.template.demo.clc.Util.Pair; +import org.onap.policy.template.demo.clc.SupportUtil.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -97,7 +97,7 @@ public class ControlLoopEventCleanupTest { private static String saveGuardFlag; private static KieSession kieSession; - private static Util.RuleSpec[] specifications; + private static SupportUtil.RuleSpec[] specifications; /** * Setup the simulator. @@ -109,7 +109,7 @@ public class ControlLoopEventCleanupTest { saveGuardFlag = PolicyEngine.manager.getEnvironmentProperty(GUARD_DISABLED); PolicyEngine.manager.getEnvironment().setProperty(GUARD_DISABLED, "true"); - Util.setAaiProps(); + SupportUtil.setAaiProps(); PolicyEngine.manager.configure(new Properties()); assertTrue(PolicyEngine.manager.start()); @@ -144,7 +144,7 @@ public class ControlLoopEventCleanupTest { .modelClassLoaderHash(1111)); try { - Util.buildAaiSim(); + SupportUtil.buildAaiSim(); } catch (Exception e) { logger.error("Could not create simulator", e); @@ -156,15 +156,15 @@ public class ControlLoopEventCleanupTest { } try { - specifications = new Util.RuleSpec[2]; + specifications = new SupportUtil.RuleSpec[2]; - specifications[0] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME, + specifications[0] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME, POLICY_VERSION, loadYaml(YAML)); - specifications[1] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE, POLICY_NAME_B, - POLICY_VERSION, loadYaml(YAML_B)); + specifications[1] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE, + POLICY_NAME_B, POLICY_VERSION, loadYaml(YAML_B)); - kieSession = Util.buildContainer(POLICY_VERSION, specifications); + kieSession = SupportUtil.buildContainer(POLICY_VERSION, specifications); } catch (IOException e) { logger.error("Could not create kieSession", e); @@ -269,17 +269,17 @@ public class ControlLoopEventCleanupTest { * effect, so then we'll update the second rule set, which should trigger a * clean-up of both. */ - Util.RuleSpec[] specs = new Util.RuleSpec[1]; + SupportUtil.RuleSpec[] specs = new SupportUtil.RuleSpec[1]; specs[0] = specifications[1]; logger.info("UPDATING VERSION TO v5.0 - DELETED RULE SET"); - Util.updateContainer("v5.0", specs); + SupportUtil.updateContainer("v5.0", specs); - specs[0] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE, POLICY_NAME_B, POLICY_VERSION, - loadYaml(YAML)); + specs[0] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE, POLICY_NAME_B, + POLICY_VERSION, loadYaml(YAML)); logger.info("UPDATING VERSION TO v6.0 - UPDATED SECOND RULE SET"); - Util.updateContainer("v6.0", specs); + SupportUtil.updateContainer("v6.0", specs); kieSession.fireAllRules(); facts = getSessionObjects(); @@ -298,13 +298,13 @@ public class ControlLoopEventCleanupTest { */ private static void updatePolicy(String yamlFile, String policyVersion) throws IOException { - specifications[0] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME, + specifications[0] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME, policyVersion, loadYaml(yamlFile)); /* * Update the policy within the container. */ - Util.updateContainer(policyVersion, specifications); + SupportUtil.updateContainer(policyVersion, specifications); } /** @@ -315,7 +315,7 @@ public class ControlLoopEventCleanupTest { * @throws UnsupportedEncodingException if an error occurs */ private static String loadYaml(String yamlFile) throws UnsupportedEncodingException { - Pair pair = Util.loadYaml(yamlFile); + Pair pair = SupportUtil.loadYaml(yamlFile); assertNotNull(pair); assertNotNull(pair.first); assertNotNull(pair.first.getControlLoop()); diff --git a/controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/ControlLoopParamsCleanupTest.java b/controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/ControlLoopParamsCleanupTest.java index 257aeb350..e0696abff 100644 --- a/controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/ControlLoopParamsCleanupTest.java +++ b/controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/ControlLoopParamsCleanupTest.java @@ -37,7 +37,7 @@ import org.junit.Test; import org.kie.api.runtime.KieSession; import org.onap.policy.controlloop.policy.ControlLoopPolicy; import org.onap.policy.drools.utils.logging.LoggerUtil; -import org.onap.policy.template.demo.clc.Util.Pair; +import org.onap.policy.template.demo.clc.SupportUtil.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -73,7 +73,7 @@ public class ControlLoopParamsCleanupTest { private static final String CONTROL_LOOP_NAME_B = "ControlLoop-Params-Cleanup-Test-B"; private static KieSession kieSession; - private static Util.RuleSpec[] specifications; + private static SupportUtil.RuleSpec[] specifications; /** * Setup the simulator. @@ -83,15 +83,15 @@ public class ControlLoopParamsCleanupTest { LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO"); try { - specifications = new Util.RuleSpec[2]; + specifications = new SupportUtil.RuleSpec[2]; - specifications[0] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME, + specifications[0] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME, POLICY_VERSION, loadYaml(YAML)); - specifications[1] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE, POLICY_NAME_B, - POLICY_VERSION, loadYaml(YAML_B)); + specifications[1] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE, + POLICY_NAME_B, POLICY_VERSION, loadYaml(YAML_B)); - kieSession = Util.buildContainer(POLICY_VERSION, specifications); + kieSession = SupportUtil.buildContainer(POLICY_VERSION, specifications); } catch (IOException e) { logger.error("Could not create kieSession", e); @@ -189,13 +189,13 @@ public class ControlLoopParamsCleanupTest { */ private static void updatePolicy(String yamlFile, String policyVersion) throws IOException { - specifications[0] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME, + specifications[0] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME, policyVersion, loadYaml(yamlFile)); /* * Update the policy within the container. */ - Util.updateContainer(policyVersion, specifications); + SupportUtil.updateContainer(policyVersion, specifications); } /** @@ -206,7 +206,7 @@ public class ControlLoopParamsCleanupTest { * @throws UnsupportedEncodingException if an error occurs */ private static String loadYaml(String yamlFile) throws UnsupportedEncodingException { - Pair pair = Util.loadYaml(yamlFile); + Pair pair = SupportUtil.loadYaml(yamlFile); assertNotNull(pair); assertNotNull(pair.first); assertNotNull(pair.first.getControlLoop()); diff --git a/controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/SupportUtil.java b/controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/SupportUtil.java new file mode 100644 index 000000000..bf2038cc2 --- /dev/null +++ b/controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/SupportUtil.java @@ -0,0 +1,367 @@ +/*- + * ============LICENSE_START======================================================= + * demo + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.template.demo.clc; + +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import javax.persistence.Query; +import org.apache.commons.io.IOUtils; +import org.kie.api.KieServices; +import org.kie.api.builder.KieBuilder; +import org.kie.api.builder.KieFileSystem; +import org.kie.api.builder.Message; +import org.kie.api.builder.ReleaseId; +import org.kie.api.builder.Results; +import org.kie.api.runtime.KieContainer; +import org.kie.api.runtime.KieSession; +import org.onap.policy.common.endpoints.http.server.HttpServletServer; +import org.onap.policy.controlloop.policy.ControlLoopPolicy; +import org.onap.policy.controlloop.policy.guard.ControlLoopGuard; +import org.onap.policy.drools.system.PolicyEngine; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; + + +public final class SupportUtil { + + private static final String OPSHISTPUPROP = "OperationsHistoryPU"; + private static final Logger logger = LoggerFactory.getLogger(SupportUtil.class); + + // values from the last call to buildContainer() + + private static KieServices kieServices; + private static KieContainer keyContainer; + + public static class Pair { + public final A first; + public final B second; + + public Pair(A first, B second) { + this.first = first; + this.second = second; + } + } + + /** + * Load YAML. + * + * @param testFile test file to load + * @return the Pair of a policy and the yaml contents + */ + public static Pair loadYaml(String testFile) { + try (InputStream is = new FileInputStream(new File(testFile))) { + String contents = IOUtils.toString(is, StandardCharsets.UTF_8); + // + // Read the yaml into our Java Object + // + Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class)); + Object obj = yaml.load(contents); + + logger.debug(contents); + + return new Pair((ControlLoopPolicy) obj, contents); + } catch (IOException e) { + fail(e.getLocalizedMessage()); + } + return null; + } + + /** + * Load the YAML guard policy. + * + * @param testFile the test file to load + * @return return the guard object + */ + public static ControlLoopGuard loadYamlGuard(String testFile) { + try (InputStream is = new FileInputStream(new File(testFile))) { + String contents = IOUtils.toString(is, StandardCharsets.UTF_8); + // + // Read the yaml into our Java Object + // + Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class)); + Object obj = yaml.load(contents); + return (ControlLoopGuard) obj; + } catch (IOException e) { + fail(e.getLocalizedMessage()); + } + return null; + } + + public static HttpServletServer buildAaiSim() throws InterruptedException, IOException { + return org.onap.policy.simulators.Util.buildAaiSim(); + } + + /** + * Build a container containing a single set of rules. + * + * @param droolsTemplate template + * @param closedLoopControlName control loop id + * @param policyScope policy scope + * @param policyName policy name + * @param policyVersion policy version + * @param yamlSpecification incoming yaml specification + * @return the Kie session + * @throws IOException if the container cannot be built + */ + public static KieSession buildContainer(String droolsTemplate, String closedLoopControlName, String policyScope, + String policyName, String policyVersion, String yamlSpecification) throws IOException { + + RuleSpec spec = new RuleSpec(droolsTemplate, closedLoopControlName, policyScope, policyName, policyVersion, + yamlSpecification); + + return buildContainer(policyVersion, new RuleSpec[] {spec}); + } + + /** + * Build a container containing all of the specified rules. + * + * @param policyVersion policy version + * @param specifications rule specifications + * @return the Kie session + * @throws IOException if the container cannot be built + */ + public static KieSession buildContainer(String policyVersion, RuleSpec[] specifications) throws IOException { + // + // Get our Drools Kie factory + // + kieServices = KieServices.Factory.get(); + + ReleaseId releaseId = buildPolicy(policyVersion, specifications); + logger.debug(releaseId.toString()); + + // + // Create our kie Session and container + // + keyContainer = kieServices.newKieContainer(releaseId); + + return keyContainer.newKieSession(); + } + + /** + * Update the container with new rules. + * + * @param policyVersion new policy version + * @param specifications new rule specifications + * @throws IOException if the container cannot be built + */ + public static void updateContainer(String policyVersion, RuleSpec[] specifications) throws IOException { + ReleaseId releaseId = buildPolicy(policyVersion, specifications); + logger.debug(releaseId.toString()); + + keyContainer.updateToVersion(releaseId); + } + + /** + * Build the Policy so it can be loaded into a KIE container. + * + * @param policyVersion policy version + * @param specifications rule specifications + * @return the release + * @throws IOException if the container cannot be built + */ + private static ReleaseId buildPolicy(String policyVersion, RuleSpec[] specifications) throws IOException { + // + // Generate our drools rule from our template + // + KieFileSystem kfs = kieServices.newKieFileSystem(); + ReleaseId releaseId = kieServices.getRepository().getDefaultReleaseId(); + releaseId = kieServices.newReleaseId(releaseId.getGroupId(), releaseId.getArtifactId(), policyVersion); + + kfs.generateAndWritePomXML(releaseId); + + for (RuleSpec spec : specifications) { + String drlContents = spec.generateRules(); + kfs.write("src/main/resources/" + spec.policyName + ".drl", + kieServices.getResources().newByteArrayResource(drlContents.getBytes())); + } + + // + // Compile the rule + // + KieBuilder builder = kieServices.newKieBuilder(kfs).buildAll(); + Results results = builder.getResults(); + if (results.hasMessages(Message.Level.ERROR)) { + for (Message msg : results.getMessages()) { + logger.error(msg.toString()); + } + throw new RuntimeException("Drools Rule has Errors"); + } + for (Message msg : results.getMessages()) { + logger.debug(msg.toString()); + } + + return releaseId; + } + + /** + * Set the A&AI properties. + */ + public static void setAaiProps() { + PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666"); + PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI"); + PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI"); + } + + /** + * Set the Guard properties to use embedded XACML PDPEngine. + */ + public static void setGuardPropsEmbedded() { + /* + * Guard PDP-x connection Properties. No URL specified -> use embedded PDPEngine. + */ + PolicyEngine.manager.setEnvironmentProperty("prop.guard.propfile", + "src/test/resources/xacml/xacml_guard_clc.properties"); + PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_USER, "python"); + PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_PASS, "test"); + PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_CLIENT_USER, "python"); + PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_CLIENT_PASS, "test"); + PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_ENV, "TEST"); + PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_DISABLED, "false"); + } + + /** + * Set the operation history properties. + */ + public static void setPuProp() { + System.setProperty(OPSHISTPUPROP, "TestOperationsHistoryPU"); + } + + /** + * Dump the contents of the History database. + * + * @return a list of the database entries + */ + public static List dumpDb() { + // + // Connect to in-mem db + // + EntityManagerFactory emf = Persistence.createEntityManagerFactory("TestOperationsHistoryPU"); + EntityManager em = emf.createEntityManager(); + // + // Create query + // + String sql = "select * from operationshistory10"; + Query nq = em.createNativeQuery(sql); + List results = null; + // + // Execute query + // + try { + results = nq.getResultList(); + } catch (Exception ex) { + logger.error("getStatusFromDB threw: ", ex); + // + // Clean up and return null + // + em.close(); + emf.close(); + return null; + } + // + // Clean up and return results + // + em.close(); + emf.close(); + return results; + } + + /** + * Rule specification. + */ + public static class RuleSpec { + private String droolsTemplate; + private String closedLoopControlName; + private String policyScope; + private String policyName; + private String policyVersion; + private String yamlSpecification; + + /** + * Constructs the object. + * + * @param droolsTemplate template + * @param closedLoopControlName control loop id + * @param policyScope policy scope + * @param policyName policy name + * @param policyVersion policy version + * @param yamlSpecification incoming yaml specification + */ + public RuleSpec(String droolsTemplate, String closedLoopControlName, String policyScope, String policyName, + String policyVersion, String yamlSpecification) { + + this.droolsTemplate = droolsTemplate; + this.closedLoopControlName = closedLoopControlName; + this.policyScope = policyScope; + this.policyName = policyName; + this.policyVersion = policyVersion; + this.yamlSpecification = yamlSpecification; + } + + /** + * Generates the rules by reading the template and making variable substitutions. + * + * @return the rules + * @throws IOException if an error occurs + */ + private String generateRules() throws IOException { + Path rule = Paths.get(droolsTemplate); + String ruleTemplate = new String(Files.readAllBytes(rule)); + + Pattern pattern = Pattern.compile("\\$\\{closedLoopControlName\\}"); + Matcher matcher = pattern.matcher(ruleTemplate); + ruleTemplate = matcher.replaceAll(closedLoopControlName); + + pattern = Pattern.compile("\\$\\{policyScope\\}"); + matcher = pattern.matcher(ruleTemplate); + ruleTemplate = matcher.replaceAll(policyScope); + + pattern = Pattern.compile("\\$\\{policyName\\}"); + matcher = pattern.matcher(ruleTemplate); + ruleTemplate = matcher.replaceAll(policyName); + + pattern = Pattern.compile("\\$\\{policyVersion\\}"); + matcher = pattern.matcher(ruleTemplate); + ruleTemplate = matcher.replaceAll(policyVersion); + + pattern = Pattern.compile("\\$\\{controlLoopYaml\\}"); + matcher = pattern.matcher(ruleTemplate); + ruleTemplate = matcher.replaceAll(yamlSpecification); + + return ruleTemplate; + } + } +} diff --git a/controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/Util.java b/controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/Util.java deleted file mode 100644 index 1d105911c..000000000 --- a/controlloop/templates/template.demo.clc/src/test/java/org/onap/policy/template/demo/clc/Util.java +++ /dev/null @@ -1,367 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * demo - * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.template.demo.clc; - -import static org.junit.Assert.fail; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; -import javax.persistence.Query; -import org.apache.commons.io.IOUtils; -import org.kie.api.KieServices; -import org.kie.api.builder.KieBuilder; -import org.kie.api.builder.KieFileSystem; -import org.kie.api.builder.Message; -import org.kie.api.builder.ReleaseId; -import org.kie.api.builder.Results; -import org.kie.api.runtime.KieContainer; -import org.kie.api.runtime.KieSession; -import org.onap.policy.common.endpoints.http.server.HttpServletServer; -import org.onap.policy.controlloop.policy.ControlLoopPolicy; -import org.onap.policy.controlloop.policy.guard.ControlLoopGuard; -import org.onap.policy.drools.system.PolicyEngine; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.constructor.Constructor; - - -public final class Util { - - private static final String OPSHISTPUPROP = "OperationsHistoryPU"; - private static final Logger logger = LoggerFactory.getLogger(Util.class); - - // values from the last call to buildContainer() - - private static KieServices kieServices; - private static KieContainer keyContainer; - - public static class Pair { - public final A first; - public final B second; - - public Pair(A first, B second) { - this.first = first; - this.second = second; - } - } - - /** - * Load YAML. - * - * @param testFile test file to load - * @return the Pair of a policy and the yaml contents - */ - public static Pair loadYaml(String testFile) { - try (InputStream is = new FileInputStream(new File(testFile))) { - String contents = IOUtils.toString(is, StandardCharsets.UTF_8); - // - // Read the yaml into our Java Object - // - Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class)); - Object obj = yaml.load(contents); - - logger.debug(contents); - - return new Pair((ControlLoopPolicy) obj, contents); - } catch (IOException e) { - fail(e.getLocalizedMessage()); - } - return null; - } - - /** - * Load the YAML guard policy. - * - * @param testFile the test file to load - * @return return the guard object - */ - public static ControlLoopGuard loadYamlGuard(String testFile) { - try (InputStream is = new FileInputStream(new File(testFile))) { - String contents = IOUtils.toString(is, StandardCharsets.UTF_8); - // - // Read the yaml into our Java Object - // - Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class)); - Object obj = yaml.load(contents); - return (ControlLoopGuard) obj; - } catch (IOException e) { - fail(e.getLocalizedMessage()); - } - return null; - } - - public static HttpServletServer buildAaiSim() throws InterruptedException, IOException { - return org.onap.policy.simulators.Util.buildAaiSim(); - } - - /** - * Build a container containing a single set of rules. - * - * @param droolsTemplate template - * @param closedLoopControlName control loop id - * @param policyScope policy scope - * @param policyName policy name - * @param policyVersion policy version - * @param yamlSpecification incoming yaml specification - * @return the Kie session - * @throws IOException if the container cannot be built - */ - public static KieSession buildContainer(String droolsTemplate, String closedLoopControlName, String policyScope, - String policyName, String policyVersion, String yamlSpecification) throws IOException { - - RuleSpec spec = new RuleSpec(droolsTemplate, closedLoopControlName, policyScope, policyName, policyVersion, - yamlSpecification); - - return buildContainer(policyVersion, new RuleSpec[] {spec}); - } - - /** - * Build a container containing all of the specified rules. - * - * @param policyVersion policy version - * @param specifications rule specifications - * @return the Kie session - * @throws IOException if the container cannot be built - */ - public static KieSession buildContainer(String policyVersion, RuleSpec[] specifications) throws IOException { - // - // Get our Drools Kie factory - // - kieServices = KieServices.Factory.get(); - - ReleaseId releaseId = buildPolicy(policyVersion, specifications); - logger.debug(releaseId.toString()); - - // - // Create our kie Session and container - // - keyContainer = kieServices.newKieContainer(releaseId); - - return keyContainer.newKieSession(); - } - - /** - * Update the container with new rules. - * - * @param policyVersion new policy version - * @param specifications new rule specifications - * @throws IOException if the container cannot be built - */ - public static void updateContainer(String policyVersion, RuleSpec[] specifications) throws IOException { - ReleaseId releaseId = buildPolicy(policyVersion, specifications); - logger.debug(releaseId.toString()); - - keyContainer.updateToVersion(releaseId); - } - - /** - * Build the Policy so it can be loaded into a KIE container. - * - * @param policyVersion policy version - * @param specifications rule specifications - * @return the release - * @throws IOException if the container cannot be built - */ - private static ReleaseId buildPolicy(String policyVersion, RuleSpec[] specifications) throws IOException { - // - // Generate our drools rule from our template - // - KieFileSystem kfs = kieServices.newKieFileSystem(); - ReleaseId releaseId = kieServices.getRepository().getDefaultReleaseId(); - releaseId = kieServices.newReleaseId(releaseId.getGroupId(), releaseId.getArtifactId(), policyVersion); - - kfs.generateAndWritePomXML(releaseId); - - for (RuleSpec spec : specifications) { - String drlContents = spec.generateRules(); - kfs.write("src/main/resources/" + spec.policyName + ".drl", - kieServices.getResources().newByteArrayResource(drlContents.getBytes())); - } - - // - // Compile the rule - // - KieBuilder builder = kieServices.newKieBuilder(kfs).buildAll(); - Results results = builder.getResults(); - if (results.hasMessages(Message.Level.ERROR)) { - for (Message msg : results.getMessages()) { - logger.error(msg.toString()); - } - throw new RuntimeException("Drools Rule has Errors"); - } - for (Message msg : results.getMessages()) { - logger.debug(msg.toString()); - } - - return releaseId; - } - - /** - * Set the A&AI properties. - */ - public static void setAaiProps() { - PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666"); - PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI"); - PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI"); - } - - /** - * Set the Guard properties to use embedded XACML PDPEngine. - */ - public static void setGuardPropsEmbedded() { - /* - * Guard PDP-x connection Properties. No URL specified -> use embedded PDPEngine. - */ - PolicyEngine.manager.setEnvironmentProperty("prop.guard.propfile", - "src/test/resources/xacml/xacml_guard_clc.properties"); - PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_USER, "python"); - PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_PASS, "test"); - PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_CLIENT_USER, "python"); - PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_CLIENT_PASS, "test"); - PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_ENV, "TEST"); - PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_DISABLED, "false"); - } - - /** - * Set the operation history properties. - */ - public static void setPuProp() { - System.setProperty(OPSHISTPUPROP, "TestOperationsHistoryPU"); - } - - /** - * Dump the contents of the History database. - * - * @return a list of the database entries - */ - public static List dumpDb() { - // - // Connect to in-mem db - // - EntityManagerFactory emf = Persistence.createEntityManagerFactory("TestOperationsHistoryPU"); - EntityManager em = emf.createEntityManager(); - // - // Create query - // - String sql = "select * from operationshistory10"; - Query nq = em.createNativeQuery(sql); - List results = null; - // - // Execute query - // - try { - results = nq.getResultList(); - } catch (Exception ex) { - logger.error("getStatusFromDB threw: ", ex); - // - // Clean up and return null - // - em.close(); - emf.close(); - return null; - } - // - // Clean up and return results - // - em.close(); - emf.close(); - return results; - } - - /** - * Rule specification. - */ - public static class RuleSpec { - private String droolsTemplate; - private String closedLoopControlName; - private String policyScope; - private String policyName; - private String policyVersion; - private String yamlSpecification; - - /** - * Constructs the object. - * - * @param droolsTemplate template - * @param closedLoopControlName control loop id - * @param policyScope policy scope - * @param policyName policy name - * @param policyVersion policy version - * @param yamlSpecification incoming yaml specification - */ - public RuleSpec(String droolsTemplate, String closedLoopControlName, String policyScope, String policyName, - String policyVersion, String yamlSpecification) { - - this.droolsTemplate = droolsTemplate; - this.closedLoopControlName = closedLoopControlName; - this.policyScope = policyScope; - this.policyName = policyName; - this.policyVersion = policyVersion; - this.yamlSpecification = yamlSpecification; - } - - /** - * Generates the rules by reading the template and making variable substitutions. - * - * @return the rules - * @throws IOException if an error occurs - */ - private String generateRules() throws IOException { - Path rule = Paths.get(droolsTemplate); - String ruleTemplate = new String(Files.readAllBytes(rule)); - - Pattern pattern = Pattern.compile("\\$\\{closedLoopControlName\\}"); - Matcher matcher = pattern.matcher(ruleTemplate); - ruleTemplate = matcher.replaceAll(closedLoopControlName); - - pattern = Pattern.compile("\\$\\{policyScope\\}"); - matcher = pattern.matcher(ruleTemplate); - ruleTemplate = matcher.replaceAll(policyScope); - - pattern = Pattern.compile("\\$\\{policyName\\}"); - matcher = pattern.matcher(ruleTemplate); - ruleTemplate = matcher.replaceAll(policyName); - - pattern = Pattern.compile("\\$\\{policyVersion\\}"); - matcher = pattern.matcher(ruleTemplate); - ruleTemplate = matcher.replaceAll(policyVersion); - - pattern = Pattern.compile("\\$\\{controlLoopYaml\\}"); - matcher = pattern.matcher(ruleTemplate); - ruleTemplate = matcher.replaceAll(yamlSpecification); - - return ruleTemplate; - } - } -} diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/CcvpnControlLoopTest.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/CcvpnControlLoopTest.java index 7469fdc75..b64ec589a 100644 --- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/CcvpnControlLoopTest.java +++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/CcvpnControlLoopTest.java @@ -65,12 +65,12 @@ public class CcvpnControlLoopTest implements TopicListener { private static List noopTopics; private static KieSession kieSession; - private static Util.Pair pair; + private static SupportUtil.Pair pair; static { /* Set environment properties */ - Util.setAaiProps(); - Util.setSdncProps(); + SupportUtil.setAaiProps(); + SupportUtil.setSdncProps(); LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO"); } @@ -100,8 +100,8 @@ public class CcvpnControlLoopTest implements TopicListener { .modelClassLoaderHash(1111)); try { - Util.buildAaiSim(); - Util.buildSdncSim(); + SupportUtil.buildAaiSim(); + SupportUtil.buildSdncSim(); } catch (Exception e) { fail(e.getMessage()); } @@ -237,7 +237,7 @@ public class CcvpnControlLoopTest implements TopicListener { /* * Load policies from yaml */ - pair = Util.loadYaml(yamlFile); + pair = SupportUtil.loadYaml(yamlFile); assertNotNull(pair); assertNotNull(pair.first); assertNotNull(pair.first.getControlLoop()); @@ -247,7 +247,7 @@ public class CcvpnControlLoopTest implements TopicListener { /* * Construct a kie session */ - final KieSession kieSession = Util.buildContainer(droolsTemplate, + final KieSession kieSession = SupportUtil.buildContainer(droolsTemplate, pair.first.getControlLoop().getControlLoopName(), policyScope, policyName, policyVersion, URLEncoder.encode(pair.second, "UTF-8")); diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/ControlLoopEventCleanupTest.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/ControlLoopEventCleanupTest.java index 0713a2800..aea2d08d0 100644 --- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/ControlLoopEventCleanupTest.java +++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/ControlLoopEventCleanupTest.java @@ -52,7 +52,7 @@ import org.onap.policy.drools.protocol.coders.JsonProtocolFilter; import org.onap.policy.drools.system.PolicyController; import org.onap.policy.drools.system.PolicyEngine; import org.onap.policy.drools.utils.logging.LoggerUtil; -import org.onap.policy.template.demo.Util.Pair; +import org.onap.policy.template.demo.SupportUtil.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -98,7 +98,7 @@ public class ControlLoopEventCleanupTest { private static String saveGuardFlag; private static KieSession kieSession; - private static Util.RuleSpec[] specifications; + private static SupportUtil.RuleSpec[] specifications; /** * Setup the simulator. @@ -110,7 +110,7 @@ public class ControlLoopEventCleanupTest { saveGuardFlag = PolicyEngine.manager.getEnvironmentProperty(GUARD_DISABLED); PolicyEngine.manager.getEnvironment().setProperty(GUARD_DISABLED, "true"); - Util.setAaiProps(); + SupportUtil.setAaiProps(); PolicyEngine.manager.configure(new Properties()); assertTrue(PolicyEngine.manager.start()); @@ -145,7 +145,7 @@ public class ControlLoopEventCleanupTest { .modelClassLoaderHash(1111)); try { - Util.buildAaiSim(); + SupportUtil.buildAaiSim(); } catch (Exception e) { logger.error("Could not create simulator", e); @@ -157,15 +157,15 @@ public class ControlLoopEventCleanupTest { } try { - specifications = new Util.RuleSpec[2]; + specifications = new SupportUtil.RuleSpec[2]; - specifications[0] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME, + specifications[0] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME, POLICY_VERSION, loadYaml(YAML)); - specifications[1] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE, POLICY_NAME_B, - POLICY_VERSION, loadYaml(YAML_B)); + specifications[1] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE, + POLICY_NAME_B, POLICY_VERSION, loadYaml(YAML_B)); - kieSession = Util.buildContainer(POLICY_VERSION, specifications); + kieSession = SupportUtil.buildContainer(POLICY_VERSION, specifications); } catch (IOException e) { logger.error("Could not create kieSession", e); @@ -270,17 +270,17 @@ public class ControlLoopEventCleanupTest { * effect, so then we'll update the second rule set, which should trigger a * clean-up of both. */ - Util.RuleSpec[] specs = new Util.RuleSpec[1]; + SupportUtil.RuleSpec[] specs = new SupportUtil.RuleSpec[1]; specs[0] = specifications[1]; logger.info("UPDATING VERSION TO v5.0 - DELETED RULE SET"); - Util.updateContainer("v5.0", specs); + SupportUtil.updateContainer("v5.0", specs); - specs[0] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE, POLICY_NAME_B, POLICY_VERSION, - loadYaml(YAML)); + specs[0] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE, POLICY_NAME_B, + POLICY_VERSION, loadYaml(YAML)); logger.info("UPDATING VERSION TO v6.0 - UPDATED SECOND RULE SET"); - Util.updateContainer("v6.0", specs); + SupportUtil.updateContainer("v6.0", specs); kieSession.fireAllRules(); facts = getSessionObjects(); @@ -299,13 +299,13 @@ public class ControlLoopEventCleanupTest { */ private static void updatePolicy(String yamlFile, String policyVersion) throws IOException { - specifications[0] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME, + specifications[0] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME, policyVersion, loadYaml(yamlFile)); /* * Update the policy within the container. */ - Util.updateContainer(policyVersion, specifications); + SupportUtil.updateContainer(policyVersion, specifications); } /** @@ -316,7 +316,7 @@ public class ControlLoopEventCleanupTest { * @throws UnsupportedEncodingException if an error occurs */ private static String loadYaml(String yamlFile) throws UnsupportedEncodingException { - Pair pair = Util.loadYaml(yamlFile); + Pair pair = SupportUtil.loadYaml(yamlFile); assertNotNull(pair); assertNotNull(pair.first); assertNotNull(pair.first.getControlLoop()); diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/ControlLoopFailureTest.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/ControlLoopFailureTest.java index 112d4df93..86e7d6de8 100644 --- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/ControlLoopFailureTest.java +++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/ControlLoopFailureTest.java @@ -68,7 +68,7 @@ public class ControlLoopFailureTest implements TopicListener { private static List noopTopics; private static KieSession kieSession; - private static Util.Pair pair; + private static SupportUtil.Pair pair; private UUID requestId; private UUID requestId2; private UUID requestId3; @@ -76,9 +76,9 @@ public class ControlLoopFailureTest implements TopicListener { static { /* Set environment properties */ - Util.setAaiProps(); - Util.setGuardProps(); - Util.setPuProp(); + SupportUtil.setAaiProps(); + SupportUtil.setGuardProps(); + SupportUtil.setPuProp(); } /** @@ -118,8 +118,8 @@ public class ControlLoopFailureTest implements TopicListener { .customJacksonCoder(null) .modelClassLoaderHash(1111)); try { - Util.buildAaiSim(); - Util.buildGuardSim(); + SupportUtil.buildAaiSim(); + SupportUtil.buildGuardSim(); } catch (Exception e) { fail(e.getMessage()); } @@ -242,7 +242,7 @@ public class ControlLoopFailureTest implements TopicListener { /* * Load policies from yaml */ - pair = Util.loadYaml(yamlFile); + pair = SupportUtil.loadYaml(yamlFile); assertNotNull(pair); assertNotNull(pair.first); assertNotNull(pair.first.getControlLoop()); @@ -252,7 +252,7 @@ public class ControlLoopFailureTest implements TopicListener { /* * Construct a kie session */ - final KieSession kieSession = Util.buildContainer(droolsTemplate, + final KieSession kieSession = SupportUtil.buildContainer(droolsTemplate, pair.first.getControlLoop().getControlLoopName(), policyScope, policyName, policyVersion, URLEncoder.encode(pair.second, "UTF-8")); diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/ControlLoopParamsCleanupTest.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/ControlLoopParamsCleanupTest.java index f3c6e058d..6dc9c1c14 100644 --- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/ControlLoopParamsCleanupTest.java +++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/ControlLoopParamsCleanupTest.java @@ -37,7 +37,7 @@ import org.junit.Test; import org.kie.api.runtime.KieSession; import org.onap.policy.controlloop.policy.ControlLoopPolicy; import org.onap.policy.drools.utils.logging.LoggerUtil; -import org.onap.policy.template.demo.Util.Pair; +import org.onap.policy.template.demo.SupportUtil.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -74,7 +74,7 @@ public class ControlLoopParamsCleanupTest { private static final String CONTROL_LOOP_NAME_B = "ControlLoop-Params-Cleanup-Test-B"; private static KieSession kieSession; - private static Util.RuleSpec[] specifications; + private static SupportUtil.RuleSpec[] specifications; /** * Setup the simulator. @@ -84,15 +84,15 @@ public class ControlLoopParamsCleanupTest { LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO"); try { - specifications = new Util.RuleSpec[2]; + specifications = new SupportUtil.RuleSpec[2]; - specifications[0] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME, + specifications[0] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME, POLICY_VERSION, loadYaml(YAML)); - specifications[1] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE, POLICY_NAME_B, - POLICY_VERSION, loadYaml(YAML_B)); + specifications[1] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE, + POLICY_NAME_B, POLICY_VERSION, loadYaml(YAML_B)); - kieSession = Util.buildContainer(POLICY_VERSION, specifications); + kieSession = SupportUtil.buildContainer(POLICY_VERSION, specifications); } catch (IOException e) { logger.error("Could not create kieSession", e); @@ -185,17 +185,17 @@ public class ControlLoopParamsCleanupTest { * effect, so then we'll update the second rule set, which should trigger a * clean-up of both. */ - Util.RuleSpec[] specs = new Util.RuleSpec[1]; + SupportUtil.RuleSpec[] specs = new SupportUtil.RuleSpec[1]; specs[0] = specifications[1]; logger.info("UPDATING VERSION TO v5.0 - DELETED RULE SET"); - Util.updateContainer("v5.0", specs); + SupportUtil.updateContainer("v5.0", specs); - specs[0] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE, POLICY_NAME_B, + specs[0] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE, POLICY_NAME_B, POLICY_VERSION, loadYaml(YAML)); logger.info("UPDATING VERSION TO v6.0 - UPDATED SECOND RULE SET"); - Util.updateContainer("v6.0", specs); + SupportUtil.updateContainer("v6.0", specs); kieSession.fireAllRules(); facts = getSessionObjects(); @@ -213,13 +213,13 @@ public class ControlLoopParamsCleanupTest { */ private static void updatePolicy(String yamlFile, String policyVersion) throws IOException { - specifications[0] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME, + specifications[0] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME, policyVersion, loadYaml(yamlFile)); /* * Update the policy within the container. */ - Util.updateContainer(policyVersion, specifications); + SupportUtil.updateContainer(policyVersion, specifications); } /** @@ -230,7 +230,7 @@ public class ControlLoopParamsCleanupTest { * @throws UnsupportedEncodingException if an error occurs */ private static String loadYaml(String yamlFile) throws UnsupportedEncodingException { - Pair pair = Util.loadYaml(yamlFile); + Pair pair = SupportUtil.loadYaml(yamlFile); assertNotNull(pair); assertNotNull(pair.first); assertNotNull(pair.first.getControlLoop()); diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/SupportUtil.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/SupportUtil.java new file mode 100644 index 000000000..00949599e --- /dev/null +++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/SupportUtil.java @@ -0,0 +1,412 @@ +/*- + * ============LICENSE_START======================================================= + * demo + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.template.demo; + +import static org.junit.Assert.fail; + +import com.att.research.xacml.util.XACMLProperties; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.commons.io.IOUtils; +import org.kie.api.KieServices; +import org.kie.api.builder.KieBuilder; +import org.kie.api.builder.KieFileSystem; +import org.kie.api.builder.Message; +import org.kie.api.builder.ReleaseId; +import org.kie.api.builder.Results; +import org.kie.api.runtime.KieContainer; +import org.kie.api.runtime.KieSession; +import org.onap.policy.common.endpoints.http.server.HttpServletServer; +import org.onap.policy.controlloop.policy.ControlLoopPolicy; +import org.onap.policy.controlloop.policy.guard.ControlLoopGuard; +import org.onap.policy.drools.system.PolicyEngine; +import org.onap.policy.guard.PolicyGuardYamlToXacml; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; + + +public final class SupportUtil { + + private static final String OPSHISTPUPROP = "OperationsHistoryPU"; + private static final Logger logger = LoggerFactory.getLogger(SupportUtil.class); + + public static class Pair { + public final A first; + public final B second; + + public Pair(A first, B second) { + this.first = first; + this.second = second; + } + } + + // values from the last call to buildContainer() + + private static KieServices kieServices; + private static KieContainer keyContainer; + + /** + * Load YAML. + * + * @param testFile test file to load + * @return the Pair of a policy and the yaml contents + */ + public static Pair loadYaml(String testFile) { + try (InputStream is = new FileInputStream(new File(testFile))) { + String contents = IOUtils.toString(is, StandardCharsets.UTF_8); + // + // Read the yaml into our Java Object + // + Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class)); + Object obj = yaml.load(contents); + + logger.debug(contents); + + return new Pair((ControlLoopPolicy) obj, contents); + } catch (FileNotFoundException e) { + fail(e.getLocalizedMessage()); + } catch (IOException e) { + fail(e.getLocalizedMessage()); + } + return null; + } + + /** + * Load the YAML guard policy. + * + * @param testFile the test file to load + * @return return the guard object + */ + public static ControlLoopGuard loadYamlGuard(String testFile) { + try (InputStream is = new FileInputStream(new File(testFile))) { + String contents = IOUtils.toString(is, StandardCharsets.UTF_8); + // + // Read the yaml into our Java Object + // + Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class)); + Object obj = yaml.load(contents); + return (ControlLoopGuard) obj; + } catch (FileNotFoundException e) { + fail(e.getLocalizedMessage()); + } catch (IOException e) { + fail(e.getLocalizedMessage()); + } + return null; + } + + public static HttpServletServer buildAaiSim() throws InterruptedException, IOException { + return org.onap.policy.simulators.Util.buildAaiSim(); + } + + public static HttpServletServer buildSoSim() throws InterruptedException, IOException { + return org.onap.policy.simulators.Util.buildSoSim(); + } + + public static HttpServletServer buildVfcSim() throws InterruptedException, IOException { + return org.onap.policy.simulators.Util.buildVfcSim(); + } + + public static HttpServletServer buildGuardSim() throws InterruptedException, IOException { + return org.onap.policy.simulators.Util.buildGuardSim(); + } + + public static HttpServletServer buildSdncSim() throws InterruptedException, IOException { + return org.onap.policy.simulators.Util.buildSdncSim(); + } + + /** + * Build a container containing a single set of rules. + * + * @param droolsTemplate template + * @param closedLoopControlName control loop id + * @param policyScope policy scope + * @param policyName policy name + * @param policyVersion policy version + * @param yamlSpecification incoming yaml specification + * @return the Kie session + * @throws IOException if the container cannot be built + */ + public static KieSession buildContainer(String droolsTemplate, String closedLoopControlName, String policyScope, + String policyName, String policyVersion, String yamlSpecification) throws IOException { + + RuleSpec spec = new RuleSpec(droolsTemplate, closedLoopControlName, policyScope, policyName, policyVersion, + yamlSpecification); + + return buildContainer(policyVersion, new RuleSpec[] {spec}); + } + + /** + * Build a container containing all of the specified rules. + * + * @param policyVersion policy version + * @param specifications rule specifications + * @return the Kie session + * @throws IOException if the container cannot be built + */ + public static KieSession buildContainer(String policyVersion, RuleSpec[] specifications) throws IOException { + // + // Get our Drools Kie factory + // + kieServices = KieServices.Factory.get(); + + ReleaseId releaseId = buildPolicy(policyVersion, specifications); + logger.debug(releaseId.toString()); + + // + // Create our kie Session and container + // + keyContainer = kieServices.newKieContainer(releaseId); + + return setupSession(keyContainer.newKieSession()); + } + + /** + * Update the container with new rules. + * + * @param policyVersion new policy version + * @param specifications new rule specifications + * @throws IOException if the container cannot be built + */ + public static void updateContainer(String policyVersion, RuleSpec[] specifications) throws IOException { + ReleaseId releaseId = buildPolicy(policyVersion, specifications); + logger.debug(releaseId.toString()); + + keyContainer.updateToVersion(releaseId); + } + + /** + * Build the Policy so it can be loaded into a KIE container. + * + * @param policyVersion policy version + * @param specifications rule specifications + * @return the release + * @throws IOException if the container cannot be built + */ + private static ReleaseId buildPolicy(String policyVersion, RuleSpec[] specifications) throws IOException { + // + // Generate our drools rule from our template + // + KieFileSystem kfs = kieServices.newKieFileSystem(); + ReleaseId releaseId = kieServices.getRepository().getDefaultReleaseId(); + releaseId = kieServices.newReleaseId(releaseId.getGroupId(), releaseId.getArtifactId(), policyVersion); + + kfs.generateAndWritePomXML(releaseId); + + for (RuleSpec spec : specifications) { + String drlContents = spec.generateRules(); + kfs.write("src/main/resources/" + spec.policyName + ".drl", + kieServices.getResources().newByteArrayResource(drlContents.getBytes())); + } + + // + // Compile the rule + // + KieBuilder builder = kieServices.newKieBuilder(kfs).buildAll(); + Results results = builder.getResults(); + if (results.hasMessages(Message.Level.ERROR)) { + for (Message msg : results.getMessages()) { + logger.error(msg.toString()); + } + throw new RuntimeException("Drools Rule has Errors"); + } + for (Message msg : results.getMessages()) { + logger.debug(msg.toString()); + } + + return releaseId; + } + + private static KieSession setupSession(KieSession kieSession) { + + + // + // Create XACML Guard policy from YAML + // We prepare 4 Guards. Notice that Rebuilds recipe has two Guards (for checking policy combining algorithm) + // + PolicyGuardYamlToXacml.fromYamlToXacml("src/test/resources/yaml/policy_guard_appc_restart.yaml", + "src/main/resources/frequency_limiter_template.xml", + "src/test/resources/xacml/autogenerated_frequency_limiter_restart.xml"); + + PolicyGuardYamlToXacml.fromYamlToXacml("src/test/resources/yaml/policy_guard_appc_rebuild.yaml", + "src/main/resources/frequency_limiter_template.xml", + "src/test/resources/xacml/autogenerated_frequency_limiter_rebuild.xml"); + + PolicyGuardYamlToXacml.fromYamlToXacml("src/test/resources/yaml/policy_guard_appc_rebuild_1.yaml", + "src/main/resources/frequency_limiter_template.xml", + "src/test/resources/xacml/autogenerated_frequency_limiter_rebuild_1.xml"); + + PolicyGuardYamlToXacml.fromYamlToXacml("src/test/resources/yaml/policy_guard_appc_migrate.yaml", + "src/main/resources/frequency_limiter_template.xml", + "src/test/resources/xacml/autogenerated_frequency_limiter_migrate.xml"); + + PolicyGuardYamlToXacml.fromYamlToXacml("src/test/resources/yaml/policy_guard_appc_modifyconfig.yaml", + "src/main/resources/frequency_limiter_template.xml", + "src/test/resources/xacml/autogenerated_frequency_limiter_modifyconfig.xml"); + + PolicyGuardYamlToXacml.fromYamlToXacmlBlacklist( + "src/test/resources/yaml/policy_guard_appc_restart_blacklist.yaml", + "src/main/resources/blacklist_template.xml", + "src/test/resources/xacml/autogenerated_blacklist.xml"); + + // + // Creating an embedded XACML PDP + // + System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml/xacml_guard.properties"); + + return kieSession; + } + + /** + * Set the A&AI properties. + */ + public static void setAaiProps() { + PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666"); + PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI"); + PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI"); + } + + /** + * Set the SO properties. + */ + public static void setSoProps() { + PolicyEngine.manager.setEnvironmentProperty("so.url", "http://localhost:6667"); + PolicyEngine.manager.setEnvironmentProperty("so.username", "SO"); + PolicyEngine.manager.setEnvironmentProperty("so.password", "SO"); + } + + /** + * Set the SDNC properties. + */ + public static void setSdncProps() { + PolicyEngine.manager.setEnvironmentProperty("sdnc.url", "http://localhost:6670/restconf/operations"); + PolicyEngine.manager.setEnvironmentProperty("sdnc.username", "sdnc"); + PolicyEngine.manager.setEnvironmentProperty("sdnc.password", "sdnc"); + } + + /** + * Set the Guard properties. + */ + public static void setGuardProps() { + /* + * Guard PDP-x connection Properties + */ + PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_URL, "http://localhost:6669/pdp/api/getDecision"); + PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_USER, "python"); + PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_PASS, "test"); + PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_CLIENT_USER, "python"); + PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_CLIENT_PASS, "test"); + PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_ENV, "TEST"); + PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_DISABLED, "false"); + } + + /** + * Set the VFC properties. + */ + public static void setVfcProps() { + PolicyEngine.manager.setEnvironmentProperty("vfc.url", "http://localhost:6668"); + PolicyEngine.manager.setEnvironmentProperty("vfc.username", "VFC"); + PolicyEngine.manager.setEnvironmentProperty("vfc.password", "VFC"); + } + + /** + * Set the operation history properties. + */ + public static void setPuProp() { + System.setProperty(OPSHISTPUPROP, "TestOperationsHistoryPU"); + } + + /** + * Rule specification. + */ + public static class RuleSpec { + private String droolsTemplate; + private String closedLoopControlName; + private String policyScope; + private String policyName; + private String policyVersion; + private String yamlSpecification; + + /** + * Constructs the object. + * + * @param droolsTemplate template + * @param closedLoopControlName control loop id + * @param policyScope policy scope + * @param policyName policy name + * @param policyVersion policy version + * @param yamlSpecification incoming yaml specification + */ + public RuleSpec(String droolsTemplate, String closedLoopControlName, String policyScope, String policyName, + String policyVersion, String yamlSpecification) { + + this.droolsTemplate = droolsTemplate; + this.closedLoopControlName = closedLoopControlName; + this.policyScope = policyScope; + this.policyName = policyName; + this.policyVersion = policyVersion; + this.yamlSpecification = yamlSpecification; + } + + /** + * Generates the rules by reading the template and making variable substitutions. + * + * @return the rules + * @throws IOException if an error occurs + */ + private String generateRules() throws IOException { + Path rule = Paths.get(droolsTemplate); + String ruleTemplate = new String(Files.readAllBytes(rule)); + + Pattern pattern = Pattern.compile("\\$\\{closedLoopControlName\\}"); + Matcher matcher = pattern.matcher(ruleTemplate); + ruleTemplate = matcher.replaceAll(closedLoopControlName); + + pattern = Pattern.compile("\\$\\{policyScope\\}"); + matcher = pattern.matcher(ruleTemplate); + ruleTemplate = matcher.replaceAll(policyScope); + + pattern = Pattern.compile("\\$\\{policyName\\}"); + matcher = pattern.matcher(ruleTemplate); + ruleTemplate = matcher.replaceAll(policyName); + + pattern = Pattern.compile("\\$\\{policyVersion\\}"); + matcher = pattern.matcher(ruleTemplate); + ruleTemplate = matcher.replaceAll(policyVersion); + + pattern = Pattern.compile("\\$\\{controlLoopYaml\\}"); + matcher = pattern.matcher(ruleTemplate); + ruleTemplate = matcher.replaceAll(yamlSpecification); + + return ruleTemplate; + } + } +} diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/Util.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/Util.java deleted file mode 100644 index 758c65568..000000000 --- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/Util.java +++ /dev/null @@ -1,412 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * demo - * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.template.demo; - -import static org.junit.Assert.fail; - -import com.att.research.xacml.util.XACMLProperties; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import org.apache.commons.io.IOUtils; -import org.kie.api.KieServices; -import org.kie.api.builder.KieBuilder; -import org.kie.api.builder.KieFileSystem; -import org.kie.api.builder.Message; -import org.kie.api.builder.ReleaseId; -import org.kie.api.builder.Results; -import org.kie.api.runtime.KieContainer; -import org.kie.api.runtime.KieSession; -import org.onap.policy.common.endpoints.http.server.HttpServletServer; -import org.onap.policy.controlloop.policy.ControlLoopPolicy; -import org.onap.policy.controlloop.policy.guard.ControlLoopGuard; -import org.onap.policy.drools.system.PolicyEngine; -import org.onap.policy.guard.PolicyGuardYamlToXacml; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.constructor.Constructor; - - -public final class Util { - - private static final String OPSHISTPUPROP = "OperationsHistoryPU"; - private static final Logger logger = LoggerFactory.getLogger(Util.class); - - public static class Pair { - public final A first; - public final B second; - - public Pair(A first, B second) { - this.first = first; - this.second = second; - } - } - - // values from the last call to buildContainer() - - private static KieServices kieServices; - private static KieContainer keyContainer; - - /** - * Load YAML. - * - * @param testFile test file to load - * @return the Pair of a policy and the yaml contents - */ - public static Pair loadYaml(String testFile) { - try (InputStream is = new FileInputStream(new File(testFile))) { - String contents = IOUtils.toString(is, StandardCharsets.UTF_8); - // - // Read the yaml into our Java Object - // - Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class)); - Object obj = yaml.load(contents); - - logger.debug(contents); - - return new Pair((ControlLoopPolicy) obj, contents); - } catch (FileNotFoundException e) { - fail(e.getLocalizedMessage()); - } catch (IOException e) { - fail(e.getLocalizedMessage()); - } - return null; - } - - /** - * Load the YAML guard policy. - * - * @param testFile the test file to load - * @return return the guard object - */ - public static ControlLoopGuard loadYamlGuard(String testFile) { - try (InputStream is = new FileInputStream(new File(testFile))) { - String contents = IOUtils.toString(is, StandardCharsets.UTF_8); - // - // Read the yaml into our Java Object - // - Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class)); - Object obj = yaml.load(contents); - return (ControlLoopGuard) obj; - } catch (FileNotFoundException e) { - fail(e.getLocalizedMessage()); - } catch (IOException e) { - fail(e.getLocalizedMessage()); - } - return null; - } - - public static HttpServletServer buildAaiSim() throws InterruptedException, IOException { - return org.onap.policy.simulators.Util.buildAaiSim(); - } - - public static HttpServletServer buildSoSim() throws InterruptedException, IOException { - return org.onap.policy.simulators.Util.buildSoSim(); - } - - public static HttpServletServer buildVfcSim() throws InterruptedException, IOException { - return org.onap.policy.simulators.Util.buildVfcSim(); - } - - public static HttpServletServer buildGuardSim() throws InterruptedException, IOException { - return org.onap.policy.simulators.Util.buildGuardSim(); - } - - public static HttpServletServer buildSdncSim() throws InterruptedException, IOException { - return org.onap.policy.simulators.Util.buildSdncSim(); - } - - /** - * Build a container containing a single set of rules. - * - * @param droolsTemplate template - * @param closedLoopControlName control loop id - * @param policyScope policy scope - * @param policyName policy name - * @param policyVersion policy version - * @param yamlSpecification incoming yaml specification - * @return the Kie session - * @throws IOException if the container cannot be built - */ - public static KieSession buildContainer(String droolsTemplate, String closedLoopControlName, String policyScope, - String policyName, String policyVersion, String yamlSpecification) throws IOException { - - RuleSpec spec = new RuleSpec(droolsTemplate, closedLoopControlName, policyScope, policyName, policyVersion, - yamlSpecification); - - return buildContainer(policyVersion, new RuleSpec[] {spec}); - } - - /** - * Build a container containing all of the specified rules. - * - * @param policyVersion policy version - * @param specifications rule specifications - * @return the Kie session - * @throws IOException if the container cannot be built - */ - public static KieSession buildContainer(String policyVersion, RuleSpec[] specifications) throws IOException { - // - // Get our Drools Kie factory - // - kieServices = KieServices.Factory.get(); - - ReleaseId releaseId = buildPolicy(policyVersion, specifications); - logger.debug(releaseId.toString()); - - // - // Create our kie Session and container - // - keyContainer = kieServices.newKieContainer(releaseId); - - return setupSession(keyContainer.newKieSession()); - } - - /** - * Update the container with new rules. - * - * @param policyVersion new policy version - * @param specifications new rule specifications - * @throws IOException if the container cannot be built - */ - public static void updateContainer(String policyVersion, RuleSpec[] specifications) throws IOException { - ReleaseId releaseId = buildPolicy(policyVersion, specifications); - logger.debug(releaseId.toString()); - - keyContainer.updateToVersion(releaseId); - } - - /** - * Build the Policy so it can be loaded into a KIE container. - * - * @param policyVersion policy version - * @param specifications rule specifications - * @return the release - * @throws IOException if the container cannot be built - */ - private static ReleaseId buildPolicy(String policyVersion, RuleSpec[] specifications) throws IOException { - // - // Generate our drools rule from our template - // - KieFileSystem kfs = kieServices.newKieFileSystem(); - ReleaseId releaseId = kieServices.getRepository().getDefaultReleaseId(); - releaseId = kieServices.newReleaseId(releaseId.getGroupId(), releaseId.getArtifactId(), policyVersion); - - kfs.generateAndWritePomXML(releaseId); - - for (RuleSpec spec : specifications) { - String drlContents = spec.generateRules(); - kfs.write("src/main/resources/" + spec.policyName + ".drl", - kieServices.getResources().newByteArrayResource(drlContents.getBytes())); - } - - // - // Compile the rule - // - KieBuilder builder = kieServices.newKieBuilder(kfs).buildAll(); - Results results = builder.getResults(); - if (results.hasMessages(Message.Level.ERROR)) { - for (Message msg : results.getMessages()) { - logger.error(msg.toString()); - } - throw new RuntimeException("Drools Rule has Errors"); - } - for (Message msg : results.getMessages()) { - logger.debug(msg.toString()); - } - - return releaseId; - } - - private static KieSession setupSession(KieSession kieSession) { - - - // - // Create XACML Guard policy from YAML - // We prepare 4 Guards. Notice that Rebuilds recipe has two Guards (for checking policy combining algorithm) - // - PolicyGuardYamlToXacml.fromYamlToXacml("src/test/resources/yaml/policy_guard_appc_restart.yaml", - "src/main/resources/frequency_limiter_template.xml", - "src/test/resources/xacml/autogenerated_frequency_limiter_restart.xml"); - - PolicyGuardYamlToXacml.fromYamlToXacml("src/test/resources/yaml/policy_guard_appc_rebuild.yaml", - "src/main/resources/frequency_limiter_template.xml", - "src/test/resources/xacml/autogenerated_frequency_limiter_rebuild.xml"); - - PolicyGuardYamlToXacml.fromYamlToXacml("src/test/resources/yaml/policy_guard_appc_rebuild_1.yaml", - "src/main/resources/frequency_limiter_template.xml", - "src/test/resources/xacml/autogenerated_frequency_limiter_rebuild_1.xml"); - - PolicyGuardYamlToXacml.fromYamlToXacml("src/test/resources/yaml/policy_guard_appc_migrate.yaml", - "src/main/resources/frequency_limiter_template.xml", - "src/test/resources/xacml/autogenerated_frequency_limiter_migrate.xml"); - - PolicyGuardYamlToXacml.fromYamlToXacml("src/test/resources/yaml/policy_guard_appc_modifyconfig.yaml", - "src/main/resources/frequency_limiter_template.xml", - "src/test/resources/xacml/autogenerated_frequency_limiter_modifyconfig.xml"); - - PolicyGuardYamlToXacml.fromYamlToXacmlBlacklist( - "src/test/resources/yaml/policy_guard_appc_restart_blacklist.yaml", - "src/main/resources/blacklist_template.xml", - "src/test/resources/xacml/autogenerated_blacklist.xml"); - - // - // Creating an embedded XACML PDP - // - System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml/xacml_guard.properties"); - - return kieSession; - } - - /** - * Set the A&AI properties. - */ - public static void setAaiProps() { - PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666"); - PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI"); - PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI"); - } - - /** - * Set the SO properties. - */ - public static void setSoProps() { - PolicyEngine.manager.setEnvironmentProperty("so.url", "http://localhost:6667"); - PolicyEngine.manager.setEnvironmentProperty("so.username", "SO"); - PolicyEngine.manager.setEnvironmentProperty("so.password", "SO"); - } - - /** - * Set the SDNC properties. - */ - public static void setSdncProps() { - PolicyEngine.manager.setEnvironmentProperty("sdnc.url", "http://localhost:6670/restconf/operations"); - PolicyEngine.manager.setEnvironmentProperty("sdnc.username", "sdnc"); - PolicyEngine.manager.setEnvironmentProperty("sdnc.password", "sdnc"); - } - - /** - * Set the Guard properties. - */ - public static void setGuardProps() { - /* - * Guard PDP-x connection Properties - */ - PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_URL, "http://localhost:6669/pdp/api/getDecision"); - PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_USER, "python"); - PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_PASS, "test"); - PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_CLIENT_USER, "python"); - PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_CLIENT_PASS, "test"); - PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_ENV, "TEST"); - PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.PROP_GUARD_DISABLED, "false"); - } - - /** - * Set the VFC properties. - */ - public static void setVfcProps() { - PolicyEngine.manager.setEnvironmentProperty("vfc.url", "http://localhost:6668"); - PolicyEngine.manager.setEnvironmentProperty("vfc.username", "VFC"); - PolicyEngine.manager.setEnvironmentProperty("vfc.password", "VFC"); - } - - /** - * Set the operation history properties. - */ - public static void setPuProp() { - System.setProperty(OPSHISTPUPROP, "TestOperationsHistoryPU"); - } - - /** - * Rule specification. - */ - public static class RuleSpec { - private String droolsTemplate; - private String closedLoopControlName; - private String policyScope; - private String policyName; - private String policyVersion; - private String yamlSpecification; - - /** - * Constructs the object. - * - * @param droolsTemplate template - * @param closedLoopControlName control loop id - * @param policyScope policy scope - * @param policyName policy name - * @param policyVersion policy version - * @param yamlSpecification incoming yaml specification - */ - public RuleSpec(String droolsTemplate, String closedLoopControlName, String policyScope, String policyName, - String policyVersion, String yamlSpecification) { - - this.droolsTemplate = droolsTemplate; - this.closedLoopControlName = closedLoopControlName; - this.policyScope = policyScope; - this.policyName = policyName; - this.policyVersion = policyVersion; - this.yamlSpecification = yamlSpecification; - } - - /** - * Generates the rules by reading the template and making variable substitutions. - * - * @return the rules - * @throws IOException if an error occurs - */ - private String generateRules() throws IOException { - Path rule = Paths.get(droolsTemplate); - String ruleTemplate = new String(Files.readAllBytes(rule)); - - Pattern pattern = Pattern.compile("\\$\\{closedLoopControlName\\}"); - Matcher matcher = pattern.matcher(ruleTemplate); - ruleTemplate = matcher.replaceAll(closedLoopControlName); - - pattern = Pattern.compile("\\$\\{policyScope\\}"); - matcher = pattern.matcher(ruleTemplate); - ruleTemplate = matcher.replaceAll(policyScope); - - pattern = Pattern.compile("\\$\\{policyName\\}"); - matcher = pattern.matcher(ruleTemplate); - ruleTemplate = matcher.replaceAll(policyName); - - pattern = Pattern.compile("\\$\\{policyVersion\\}"); - matcher = pattern.matcher(ruleTemplate); - ruleTemplate = matcher.replaceAll(policyVersion); - - pattern = Pattern.compile("\\$\\{controlLoopYaml\\}"); - matcher = pattern.matcher(ruleTemplate); - ruleTemplate = matcher.replaceAll(yamlSpecification); - - return ruleTemplate; - } - } -} diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VcpeControlLoopTest.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VcpeControlLoopTest.java index c6d6dc082..02b35fadb 100644 --- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VcpeControlLoopTest.java +++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VcpeControlLoopTest.java @@ -70,14 +70,14 @@ public class VcpeControlLoopTest implements TopicListener { private static List noopTopics; private static KieSession kieSession; - private static Util.Pair pair; + private static SupportUtil.Pair pair; private UUID requestId; static { /* Set environment properties */ - Util.setAaiProps(); - Util.setGuardProps(); - Util.setPuProp(); + SupportUtil.setAaiProps(); + SupportUtil.setGuardProps(); + SupportUtil.setPuProp(); LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO"); } @@ -118,8 +118,8 @@ public class VcpeControlLoopTest implements TopicListener { .customJacksonCoder(null) .modelClassLoaderHash(1111)); try { - Util.buildAaiSim(); - Util.buildGuardSim(); + SupportUtil.buildAaiSim(); + SupportUtil.buildGuardSim(); } catch (Exception e) { fail(e.getMessage()); } @@ -253,7 +253,7 @@ public class VcpeControlLoopTest implements TopicListener { /* * Load policies from yaml */ - pair = Util.loadYaml(yamlFile); + pair = SupportUtil.loadYaml(yamlFile); assertNotNull(pair); assertNotNull(pair.first); assertNotNull(pair.first.getControlLoop()); @@ -263,7 +263,7 @@ public class VcpeControlLoopTest implements TopicListener { /* * Construct a kie session */ - final KieSession kieSession = Util.buildContainer(droolsTemplate, + final KieSession kieSession = SupportUtil.buildContainer(droolsTemplate, pair.first.getControlLoop().getControlLoopName(), policyScope, policyName, policyVersion, URLEncoder.encode(pair.second, "UTF-8")); diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VdnsControlLoopTest.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VdnsControlLoopTest.java index b5b7a4858..58f0840ac 100644 --- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VdnsControlLoopTest.java +++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VdnsControlLoopTest.java @@ -66,15 +66,15 @@ public class VdnsControlLoopTest implements TopicListener { private static List noopTopics; private static KieSession kieSession; - private static Util.Pair pair; + private static SupportUtil.Pair pair; private UUID requestId; static { /* Set environment properties */ - Util.setAaiProps(); - Util.setSoProps(); - Util.setGuardProps(); - Util.setPuProp(); + SupportUtil.setAaiProps(); + SupportUtil.setSoProps(); + SupportUtil.setGuardProps(); + SupportUtil.setPuProp(); LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO"); } @@ -104,9 +104,9 @@ public class VdnsControlLoopTest implements TopicListener { .modelClassLoaderHash(1111)); try { - Util.buildAaiSim(); - Util.buildSoSim(); - Util.buildGuardSim(); + SupportUtil.buildAaiSim(); + SupportUtil.buildSoSim(); + SupportUtil.buildGuardSim(); } catch (Exception e) { fail(e.getMessage()); } @@ -285,7 +285,7 @@ public class VdnsControlLoopTest implements TopicListener { /* * Load policies from yaml */ - pair = Util.loadYaml(yamlFile); + pair = SupportUtil.loadYaml(yamlFile); assertNotNull(pair); assertNotNull(pair.first); assertNotNull(pair.first.getControlLoop()); @@ -295,7 +295,7 @@ public class VdnsControlLoopTest implements TopicListener { /* * Construct a kie session */ - final KieSession kieSession = Util.buildContainer(droolsTemplate, + final KieSession kieSession = SupportUtil.buildContainer(droolsTemplate, pair.first.getControlLoop().getControlLoopName(), policyScope, policyName, policyVersion, URLEncoder.encode(pair.second, "UTF-8")); diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VfcControlLoopTest.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VfcControlLoopTest.java index 5a5dc8ac4..aa4c108c5 100644 --- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VfcControlLoopTest.java +++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VfcControlLoopTest.java @@ -69,15 +69,15 @@ public class VfcControlLoopTest implements TopicListener { private static List noopTopics; private static KieSession kieSession; - private static Util.Pair pair; + private static SupportUtil.Pair pair; private UUID requestId; static { /* Set environment properties */ - Util.setAaiProps(); - Util.setVfcProps(); - Util.setGuardProps(); - Util.setPuProp(); + SupportUtil.setAaiProps(); + SupportUtil.setVfcProps(); + SupportUtil.setGuardProps(); + SupportUtil.setPuProp(); LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO"); } @@ -107,9 +107,9 @@ public class VfcControlLoopTest implements TopicListener { .modelClassLoaderHash(1111)); try { - Util.buildAaiSim(); - Util.buildVfcSim(); - Util.buildGuardSim(); + SupportUtil.buildAaiSim(); + SupportUtil.buildVfcSim(); + SupportUtil.buildGuardSim(); } catch (Exception e) { fail(e.getMessage()); } @@ -253,7 +253,7 @@ public class VfcControlLoopTest implements TopicListener { /* * Load policies from yaml */ - pair = Util.loadYaml(yamlFile); + pair = SupportUtil.loadYaml(yamlFile); assertNotNull(pair); assertNotNull(pair.first); assertNotNull(pair.first.getControlLoop()); @@ -263,7 +263,7 @@ public class VfcControlLoopTest implements TopicListener { /* * Construct a kie session */ - final KieSession kieSession = Util.buildContainer(droolsTemplate, + final KieSession kieSession = SupportUtil.buildContainer(droolsTemplate, pair.first.getControlLoop().getControlLoopName(), policyScope, policyName, policyVersion, URLEncoder.encode(pair.second, "UTF-8")); diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VfwControlLoopTest.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VfwControlLoopTest.java index 2bcaa5b7e..123d1b398 100644 --- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VfwControlLoopTest.java +++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VfwControlLoopTest.java @@ -68,14 +68,14 @@ public class VfwControlLoopTest implements TopicListener { private static List noopTopics; private static KieSession kieSession; - private static Util.Pair pair; + private static SupportUtil.Pair pair; private UUID requestId; static { /* Set environment properties */ - Util.setAaiProps(); - Util.setGuardProps(); - Util.setPuProp(); + SupportUtil.setAaiProps(); + SupportUtil.setGuardProps(); + SupportUtil.setPuProp(); LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO"); } @@ -117,8 +117,8 @@ public class VfwControlLoopTest implements TopicListener { .modelClassLoaderHash(1111)); try { - Util.buildAaiSim(); - Util.buildGuardSim(); + SupportUtil.buildAaiSim(); + SupportUtil.buildGuardSim(); } catch (Exception e) { fail(e.getMessage()); } @@ -299,7 +299,7 @@ public class VfwControlLoopTest implements TopicListener { /* * Load policies from yaml */ - pair = Util.loadYaml(yamlFile); + pair = SupportUtil.loadYaml(yamlFile); assertNotNull(pair); assertNotNull(pair.first); assertNotNull(pair.first.getControlLoop()); @@ -309,7 +309,7 @@ public class VfwControlLoopTest implements TopicListener { /* * Construct a kie session */ - final KieSession kieSession = Util.buildContainer(droolsTemplate, + final KieSession kieSession = SupportUtil.buildContainer(droolsTemplate, pair.first.getControlLoop().getControlLoopName(), policyScope, policyName, policyVersion, URLEncoder.encode(pair.second, "UTF-8")); diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VpciControlLoopTest.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VpciControlLoopTest.java index 89e309f09..8cbb4d69d 100644 --- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VpciControlLoopTest.java +++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VpciControlLoopTest.java @@ -71,14 +71,14 @@ public class VpciControlLoopTest implements TopicListener { private static List noopTopics; private static KieSession kieSession; - private static Util.Pair pair; + private static SupportUtil.Pair pair; private UUID requestId; static { /* Set environment properties */ - Util.setAaiProps(); - Util.setGuardProps(); - Util.setPuProp(); + SupportUtil.setAaiProps(); + SupportUtil.setGuardProps(); + SupportUtil.setPuProp(); LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "DEBUG"); } @@ -119,8 +119,8 @@ public class VpciControlLoopTest implements TopicListener { .customJacksonCoder(null) .modelClassLoaderHash(1111)); try { - Util.buildAaiSim(); - Util.buildGuardSim(); + SupportUtil.buildAaiSim(); + SupportUtil.buildGuardSim(); } catch (Exception e) { fail(e.getMessage()); } @@ -258,7 +258,7 @@ public class VpciControlLoopTest implements TopicListener { /* * Load policies from yaml */ - pair = Util.loadYaml(yamlFile); + pair = SupportUtil.loadYaml(yamlFile); assertNotNull(pair); assertNotNull(pair.first); assertNotNull(pair.first.getControlLoop()); @@ -268,7 +268,7 @@ public class VpciControlLoopTest implements TopicListener { /* * Construct a kie session */ - final KieSession kieSession = Util.buildContainer(droolsTemplate, + final KieSession kieSession = SupportUtil.buildContainer(droolsTemplate, pair.first.getControlLoop().getControlLoopName(), policyScope, policyName, policyVersion, URLEncoder.encode(pair.second, "UTF-8")); -- cgit 1.2.3-korg