summaryrefslogtreecommitdiffstats
path: root/controlloop/templates/template.demo/src
diff options
context:
space:
mode:
Diffstat (limited to 'controlloop/templates/template.demo/src')
-rw-r--r--controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/AaiSimulator.java45
-rw-r--r--controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/AaiSimulatorTest.java47
-rw-r--r--controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/ControlLoopXacmlGuardTest.java239
-rw-r--r--controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/Util.java168
4 files changed, 261 insertions, 238 deletions
diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/AaiSimulator.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/AaiSimulator.java
new file mode 100644
index 000000000..f4ed267b4
--- /dev/null
+++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/AaiSimulator.java
@@ -0,0 +1,45 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * demo
+ * ================================================================================
+ * Copyright (C) 2017 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 javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+
+@Path("/aai")
+public class AaiSimulator {
+
+ @GET
+ @Path("/v8/network/generic-vnfs/generic-vnf/{vnfId}")
+ public String aaiGetQuery (@PathParam("vnfID") String vnfId)
+ {
+ return "{\"relationship-list\": {\"relationship\":[{\"related-to-property\": [{\"property-key\": \"service-instance.service-instance-name\"}]},{\"related-to-property\": [ {\"property-key\": \"vserver.vserver-name\",\"property-value\": \"USUCP0PCOIL0110UJZZ01-vsrx\" }]} ]}}";
+ }
+
+ @POST
+ @Path("/search/named-query")
+ public String aaiPostQuery()
+ {
+ return "{\"inventory-response-item\":[{\"vserver\":{\"vserver-id\": \"vserver-id-Manisha-01\",\"vserver-name\": \"vserver-name-16102016-aai3255-data-11-1\",\"vserver-name2\": \"example-vserver-name2-val-68608\",\"prov-status\": \"example-prov-status-val-59118\",\"vserver-selflink\": \"example-vserver-selflink-val-10902\",\"in-maint\": true,\"is-closed-loop-disabled\": false,\"resource-version\": \"1477946963\"},\"model-name\": \"service-instance\",\"generic-vnf\": {\"vnf-id\": \"de7cc3ab-0212-47df-9e64-da1c79234deb\",\"vnf-name\": \"ZRDM2MMEX39\",\"vnf-type\": \"vMME Svc Jul 14/vMME VF Jul 14 1\",\"service-id\": \"a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb\",\"orchestration-status\": \"active\",\"in-maint\": false,\"is-closed-loop-disabled\": false,\"resource-version\": \"1503082370097\",\"model-invariant-id\": \"82194af1-3c2c-485a-8f44-420e22a9eaa4\",\"model-version-id\": \"46b92144-923a-4d20-b85a-3cbd847668a9\"},\"extra-properties\": {},\"inventory-response-items\":{\"inventory-response-item\":[{\"generic-vnf\":{\"vnf-id\": \"generic-vnf-id-Manisha-01\",\"vnf-name\": \"bpsx0001v-16102016-aai3255-data-11\",\"vnf-name2\": \"example-vnf-name2-val-8204\",\"vnf-type\": \"my-vnf-type\",\"service-id\": \"c7611ebe-c324-48f1-8085-94aef0c6ef3d\",\"regional-resource-zone\": \"example-regional-resource-zone-val-8204\",\"prov-status\": \"ACTIVE\",\"operational-state\": \"example-operational-state-val-3289\",\"license-key\": \"example-license-key-val-3289\",\"equipment-role\": \"example-equipment-role-val-3289\",\"orchestration-status\": \"example-orchestration-status-val-3289\",\"heat-stack-id\": \"example-heat-stack-id-val-3289\",\"mso-catalog-key\": \"example-mso-catalog-key-val-3289\",\"management-option\": \"example-management-option-val-8204\",\"ipv4-oam-address\": \"example-ipv4-oam-address-val-8204\",\"ipv4-loopback0-address\": \"example-ipv4-loopback0-address-val-8204\",\"nm-lan-v6-address\": \"example-nm-lan-v6-address-val-8204\",\"management-v6-address\": \"example-management-v6-address-val-8204\",\"vcpu\": 7957,\"vcpu-units\": \"example-vcpu-units-val-8204\",\"vmemory\": 168,\"vmemory-units\": \"example-vmemory-units-val-8204\",\"vdisk\": 3227,\"vdisk-units\": \"example-vdisk-units-val-8204\",\"in-maint\": false,\"is-closed-loop-disabled\": false,\"resource-version\": \"1477946966\"},\"model-name\": \"service-instance\",\"service-instance\": {\"service-instance-id\": \"37b8cdb7-94eb-468f-a0c2-4e3c3546578e\",\"service-instance-name\": \"Changed Service Instance NAME\",\"model-invariant-id\": \"82194af1-3c2c-485a-8f44-420e22a9eaa4\",\"model-version-id\": \"46b92144-923a-4d20-b85a-3cbd847668a9\",\"resource-version\": \"1503082993532\",\"orchestration-status\": \"Active\"},\"extra-properties\": {},\"inventory-response-items\":{\"inventory-response-item\":[{\"service-instance\":{\"service-instance-id\": \"service-instance-id-Manisha-01\",\"service-instance-name\": \"example-service-instance-name-val-5008-1\",\"widget-model-id\": \"example-widget-model-id-val-52958\",\"widget-model-version\": \"example-widget-model-version-val-42840\",\"bandwidth-total\": \"example-bandwidth-total-val-99587\",\"bandwidth-up-wan1\": \"example-bandwidth-up-wan1-val-73709\",\"bandwidth-down-wan1\": \"example-bandwidth-down-wan1-val-20007\",\"bandwidth-up-wan2\": \"example-bandwidth-up-wan2-val-16857\",\"bandwidth-down-wan2\": \"example-bandwidth-down-wan2-val-95839\",\"vhn-portal-url\": \"example-vhn-portal-url-val-21541\",\"operational-status\": \"example-operational-status-val-48090\",\"service-instance-location-id\": \"example-service-instance-location-id-val-9684\",\"resource-version\": \"1477946961\"},\"extra-properties\": {}},{\"model-name\": \"pnf\",\"generic-vnf\": {\"vnf-id\": \"jimmy-test\",\"vnf-name\": \"jimmy-test-vnf\",\"vnf-type\": \"vMME Svc Jul 14/vMME VF Jul 14 1\",\"service-id\": \"a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb\",\"orchestration-status\": \"active\",\"in-maint\": false,\"is-closed-loop-disabled\": false,\"resource-version\": \"1504013830207\",\"model-invariant-id\": \"862b25a1-262a-4961-bdaa-cdc55d69785a\",\"model-version-id\": \"e9f1fa7d-c839-418a-9601-03dc0d2ad687\"},\"extra-properties\": {}},{\"model-name\": \"service-instance\",\"generic-vnf\": {\"vnf-id\": \"jimmy-test-vnf2\",\"vnf-name\": \"jimmy-test-vnf2-named\",\"vnf-type\": \"vMME Svc Jul 14/vMME VF Jul 14 1\",\"service-id\": \"a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb\",\"orchestration-status\": \"active\",\"in-maint\": false,\"is-closed-loop-disabled\": false,\"resource-version\": \"1504014833841\",\"model-invariant-id\": \"82194af1-3c2c-485a-8f44-420e22a9eaa4\",\"model-version-id\": \"46b92144-923a-4d20-b85a-3cbd847668a9\"},\"extra-properties\": {}}]}}]}}]}";
+ }
+
+}
diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/AaiSimulatorTest.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/AaiSimulatorTest.java
new file mode 100644
index 000000000..73d50a5db
--- /dev/null
+++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/AaiSimulatorTest.java
@@ -0,0 +1,47 @@
+package org.onap.policy.template.demo;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+import java.util.UUID;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.onap.policy.aai.AAIGETResponse;
+import org.onap.policy.aai.AAINQF199.AAINQF199Manager;
+import org.onap.policy.aai.AAINQF199.AAINQF199Request;
+import org.onap.policy.aai.AAINQF199.AAINQF199Response;
+import org.onap.policy.drools.http.server.HttpServletServer;
+
+public class AaiSimulatorTest {
+
+ @BeforeClass
+ public static void setUpSimulator() {
+ try {
+ Util.buildAaiSim();
+ } catch (InterruptedException e) {
+ fail(e.getMessage());
+ }
+ }
+
+ @AfterClass
+ public static void tearDownSimulator() {
+ HttpServletServer.factory.destroy();
+ }
+
+ @Test
+ public void testGet() {
+ AAIGETResponse response = AAINQF199Manager.getQuery("http://localhost:6666", "testUser", "testPass", UUID.randomUUID(), "5e49ca06-2972-4532-9ed4-6d071588d792");
+ assertNotNull(response);
+ assertNotNull(response.relationshipList);
+ }
+
+ @Test
+ public void testPost() {
+ AAINQF199Response response = AAINQF199Manager.postQuery("http://localhost:6666", "testUser", "testPass", new AAINQF199Request(), UUID.randomUUID());
+ assertNotNull(response);
+ assertNotNull(response.inventoryResponseItems);
+ }
+}
diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/ControlLoopXacmlGuardTest.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/ControlLoopXacmlGuardTest.java
index 35f3cc18d..23f03f7e8 100644
--- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/ControlLoopXacmlGuardTest.java
+++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/ControlLoopXacmlGuardTest.java
@@ -28,38 +28,12 @@ import static org.junit.Assert.fail;
import java.io.IOException;
import java.net.URLEncoder;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
import java.time.Instant;
import java.util.HashMap;
import java.util.UUID;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import org.junit.Ignore;
import org.junit.Test;
-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.builder.model.KieModuleModel;
-import org.kie.api.event.rule.AfterMatchFiredEvent;
-import org.kie.api.event.rule.AgendaEventListener;
-import org.kie.api.event.rule.AgendaGroupPoppedEvent;
-import org.kie.api.event.rule.AgendaGroupPushedEvent;
-import org.kie.api.event.rule.BeforeMatchFiredEvent;
-import org.kie.api.event.rule.MatchCancelledEvent;
-import org.kie.api.event.rule.MatchCreatedEvent;
-import org.kie.api.event.rule.ObjectDeletedEvent;
-import org.kie.api.event.rule.ObjectInsertedEvent;
-import org.kie.api.event.rule.ObjectUpdatedEvent;
-import org.kie.api.event.rule.RuleFlowGroupActivatedEvent;
-import org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent;
-import org.kie.api.event.rule.RuleRuntimeEventListener;
-import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.FactHandle;
import org.onap.policy.appc.Request;
@@ -71,20 +45,12 @@ import org.onap.policy.controlloop.ControlLoopNotificationType;
import org.onap.policy.controlloop.VirtualControlLoopEvent;
import org.onap.policy.controlloop.VirtualControlLoopNotification;
-import org.onap.policy.controlloop.ControlLoopLogger;
-import org.onap.policy.controlloop.impl.ControlLoopLoggerStdOutImpl;
import org.onap.policy.controlloop.policy.ControlLoopPolicy;
import org.onap.policy.controlloop.policy.TargetType;
import org.onap.policy.drools.impl.PolicyEngineJUnitImpl;
import org.onap.policy.guard.PolicyGuard;
-import org.onap.policy.guard.PolicyGuardYamlToXacml;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.att.research.xacml.api.pdp.PDPEngine;
-import com.att.research.xacml.api.pdp.PDPEngineFactory;
-import com.att.research.xacml.util.FactoryException;
-import com.att.research.xacml.util.XACMLProperties;
-
//import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
@@ -122,7 +88,7 @@ public class ControlLoopXacmlGuardTest {
//
// Build a container
//
- final KieSession kieSession = buildContainer(droolsTemplate,
+ final KieSession kieSession = Util.buildContainer(droolsTemplate,
pair.a.getControlLoop().getControlLoopName(),
policyScope,
policyName,
@@ -130,122 +96,11 @@ public class ControlLoopXacmlGuardTest {
URLEncoder.encode(pair.b, "UTF-8"));
-
logger.debug("============");
logger.debug(URLEncoder.encode(pair.b, "UTF-8"));
logger.debug("============");
-
- kieSession.addEventListener(new RuleRuntimeEventListener() {
-
- @Override
- public void objectInserted(ObjectInsertedEvent event) {
- }
-
- @Override
- public void objectUpdated(ObjectUpdatedEvent event) {
- }
-
- @Override
- public void objectDeleted(ObjectDeletedEvent event) {
- }
- });
- kieSession.addEventListener(new AgendaEventListener() {
-
- @Override
- public void matchCreated(MatchCreatedEvent event) {
- //logger.debug("matchCreated: " + event.getMatch().getRule());
- }
-
- @Override
- public void matchCancelled(MatchCancelledEvent event) {
- }
-
- @Override
- public void beforeMatchFired(BeforeMatchFiredEvent event) {
- //logger.debug("beforeMatchFired: " + event.getMatch().getRule() + event.getMatch().getObjects());
- }
-
- @Override
- public void afterMatchFired(AfterMatchFiredEvent event) {
- }
-
- @Override
- public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
- }
-
- @Override
- public void agendaGroupPushed(AgendaGroupPushedEvent event) {
- }
-
- @Override
- public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
- }
-
- @Override
- public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
- }
-
- @Override
- public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
- }
-
- @Override
- public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
- }
-
- });
-
- //
- // 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.fromYamlToXacmlBlacklist("src/test/resources/yaml/policy_guard_appc_restart_blacklist.yaml",
- "src/main/resources/blacklist_template.xml",
- "src/test/resources/xacml/autogenerated_blacklist.xml");
-
-
- //
- // Insert our globals
- //
- final ControlLoopLogger controlLoopLogger = new ControlLoopLoggerStdOutImpl();
- kieSession.setGlobal("Logger", controlLoopLogger);
- final PolicyEngineJUnitImpl engine = new PolicyEngineJUnitImpl();
- kieSession.setGlobal("Engine", engine);
-
-
- //
- // Creating an embedded XACML PDP
- //
- final PDPEngine xacmlPdpEngine;
- System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml/xacml_guard.properties");
-
- PDPEngineFactory factory;
- try {
- factory = PDPEngineFactory.newInstance();
- xacmlPdpEngine = factory.newEngine();
- kieSession.setGlobal("XacmlPdpEngine", xacmlPdpEngine);
- } catch (FactoryException e1) {
- e1.printStackTrace();
- }
-
-
+ final PolicyEngineJUnitImpl engine = (PolicyEngineJUnitImpl) kieSession.getGlobal("Engine");
//
// Initial fire of rules
@@ -531,94 +386,4 @@ public class ControlLoopXacmlGuardTest {
kieSession.insert(event);
Thread.sleep(250);
}
-
-
-
-
- public static String generatePolicy(String ruleContents,
- String closedLoopControlName,
- String policyScope,
- String policyName,
- String policyVersion,
- String controlLoopYaml) {
-
- Pattern p = Pattern.compile("\\$\\{closedLoopControlName\\}");
- Matcher m = p.matcher(ruleContents);
- ruleContents = m.replaceAll(closedLoopControlName);
-
- p = Pattern.compile("\\$\\{policyScope\\}");
- m = p.matcher(ruleContents);
- ruleContents = m.replaceAll(policyScope);
-
- p = Pattern.compile("\\$\\{policyName\\}");
- m = p.matcher(ruleContents);
- ruleContents = m.replaceAll(policyName);
-
- p = Pattern.compile("\\$\\{policyVersion\\}");
- m = p.matcher(ruleContents);
- ruleContents = m.replaceAll(policyVersion);
-
- p = Pattern.compile("\\$\\{controlLoopYaml\\}");
- m = p.matcher(ruleContents);
- ruleContents = m.replaceAll(controlLoopYaml);
- logger.debug(ruleContents);
-
- return ruleContents;
- }
-
- public static KieSession buildContainer(String droolsTemplate, String closedLoopControlName, String policyScope, String policyName, String policyVersion, String yamlSpecification) throws IOException {
- //
- // Get our Drools Kie factory
- //
- KieServices ks = KieServices.Factory.get();
-
- KieModuleModel kModule = ks.newKieModuleModel();
-
- logger.debug("KMODULE: {} {}", System.lineSeparator(), kModule.toXML());
-
- //
- // Generate our drools rule from our template
- //
- KieFileSystem kfs = ks.newKieFileSystem();
-
- kfs.writeKModuleXML(kModule.toXML());
- {
- Path rule = Paths.get(droolsTemplate);
- String ruleTemplate = new String(Files.readAllBytes(rule));
- String drlContents = generatePolicy(ruleTemplate,
- closedLoopControlName,
- policyScope,
- policyName,
- policyVersion,
- yamlSpecification);
-
- kfs.write("src/main/resources/" + policyName + ".drl", ks.getResources().newByteArrayResource(drlContents.getBytes()));
- }
- //
- // Compile the rule
- //
- KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
- Results results = builder.getResults();
- if (results.hasMessages(Message.Level.ERROR)) {
- for (Message msg : results.getMessages()) {
- logger.error("{}", msg);
- }
- throw new RuntimeException("Drools Rule has Errors");
- }
- for (Message msg : results.getMessages()) {
- logger.debug("{}", msg);
- }
- //
- // Create our kie Session and container
- //
- ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
- logger.debug("{}", releaseId);
- KieContainer kContainer = ks.newKieContainer(releaseId);
-
- return kContainer.newKieSession();
- }
-
-
-
-
}
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
index 62e7341a5..9bba63b82 100644
--- 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
@@ -28,13 +28,36 @@ 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.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
-
+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.builder.model.KieModuleModel;
+import org.kie.api.runtime.KieContainer;
+import org.kie.api.runtime.KieSession;
+import org.onap.policy.controlloop.ControlLoopLogger;
+import org.onap.policy.controlloop.impl.ControlLoopLoggerStdOutImpl;
import org.onap.policy.controlloop.policy.ControlLoopPolicy;
import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
+import org.onap.policy.drools.http.server.HttpServletServer;
+import org.onap.policy.drools.impl.PolicyEngineJUnitImpl;
+import org.onap.policy.guard.PolicyGuardYamlToXacml;
+
+import com.att.research.xacml.api.pdp.PDPEngine;
+import com.att.research.xacml.api.pdp.PDPEngineFactory;
+import com.att.research.xacml.util.FactoryException;
+import com.att.research.xacml.util.XACMLProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -89,5 +112,148 @@ public final class Util {
}
return null;
}
+
+ public static HttpServletServer buildAaiSim() throws InterruptedException {
+ HttpServletServer testServer = HttpServletServer.factory.build("testServer", "localhost", 6666, "/", false, true);
+ testServer.addServletClass("/*", AaiSimulator.class.getName());
+ testServer.waitedStart(5000);
+ return testServer;
+ }
+
+ private static String generatePolicy(String ruleContents,
+ String closedLoopControlName,
+ String policyScope,
+ String policyName,
+ String policyVersion,
+ String controlLoopYaml) {
+
+ Pattern p = Pattern.compile("\\$\\{closedLoopControlName\\}");
+ Matcher m = p.matcher(ruleContents);
+ ruleContents = m.replaceAll(closedLoopControlName);
+
+ p = Pattern.compile("\\$\\{policyScope\\}");
+ m = p.matcher(ruleContents);
+ ruleContents = m.replaceAll(policyScope);
+
+ p = Pattern.compile("\\$\\{policyName\\}");
+ m = p.matcher(ruleContents);
+ ruleContents = m.replaceAll(policyName);
+
+ p = Pattern.compile("\\$\\{policyVersion\\}");
+ m = p.matcher(ruleContents);
+ ruleContents = m.replaceAll(policyVersion);
+
+ p = Pattern.compile("\\$\\{controlLoopYaml\\}");
+ m = p.matcher(ruleContents);
+ ruleContents = m.replaceAll(controlLoopYaml);
+
+ return ruleContents;
+ }
+
+ public static KieSession buildContainer(String droolsTemplate, String closedLoopControlName, String policyScope, String policyName, String policyVersion, String yamlSpecification) throws IOException {
+ //
+ // Get our Drools Kie factory
+ //
+ KieServices ks = KieServices.Factory.get();
+
+ KieModuleModel kModule = ks.newKieModuleModel();
+
+ logger.debug("KMODULE:" + System.lineSeparator() + kModule.toXML());
+
+ //
+ // Generate our drools rule from our template
+ //
+ KieFileSystem kfs = ks.newKieFileSystem();
+
+ kfs.writeKModuleXML(kModule.toXML());
+ {
+ Path rule = Paths.get(droolsTemplate);
+ String ruleTemplate = new String(Files.readAllBytes(rule));
+ String drlContents = generatePolicy(ruleTemplate,
+ closedLoopControlName,
+ policyScope,
+ policyName,
+ policyVersion,
+ yamlSpecification);
+
+ kfs.write("src/main/resources/" + policyName + ".drl", ks.getResources().newByteArrayResource(drlContents.getBytes()));
+ }
+ //
+ // Compile the rule
+ //
+ KieBuilder builder = ks.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());
+ }
+ //
+ // Create our kie Session and container
+ //
+ ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
+ logger.debug(releaseId.toString());
+ KieContainer kContainer = ks.newKieContainer(releaseId);
+
+ return setupSession(kContainer.newKieSession());
+ }
+
+ 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.fromYamlToXacmlBlacklist("src/test/resources/yaml/policy_guard_appc_restart_blacklist.yaml",
+ "src/main/resources/blacklist_template.xml",
+ "src/test/resources/xacml/autogenerated_blacklist.xml");
+
+
+ //
+ // Insert our globals
+ //
+ final ControlLoopLogger controlLoopLogger = new ControlLoopLoggerStdOutImpl();
+ kieSession.setGlobal("Logger", controlLoopLogger);
+ final PolicyEngineJUnitImpl engine = new PolicyEngineJUnitImpl();
+ kieSession.setGlobal("Engine", engine);
+
+
+ //
+ // Creating an embedded XACML PDP
+ //
+ final PDPEngine xacmlPdpEngine;
+ System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml/xacml_guard.properties");
+
+ PDPEngineFactory factory;
+ try {
+ factory = PDPEngineFactory.newInstance();
+ xacmlPdpEngine = factory.newEngine();
+ kieSession.setGlobal("XacmlPdpEngine", xacmlPdpEngine);
+ } catch (FactoryException e1) {
+ e1.printStackTrace();
+ }
+ return kieSession;
+ }
}