diff options
author | Ravindra Bakkamanthala <rb7147@att.com> | 2017-05-23 14:56:12 -0400 |
---|---|---|
committer | Ravindra Bakkamanthala <rb7147@att.com> | 2017-05-23 16:49:56 -0400 |
commit | 87c95be02a8a4d77e165dede90777e811b59dcae (patch) | |
tree | 4712199fc3520b530dda0c4d3b074c327df547f2 /ECOMP-ControlloopPolicy/src/test/java | |
parent | 7e547eaa55920dfbc9691eab33bb728395b50cf2 (diff) |
Commit includes ControlLoopPolicy API and bugfixes
Change-Id: I3e18bb8b4c31a0d908bb0cff4c85e2a3fb450a63
Signed-off-by: Ravindra Bakkamanthala <rb7147@att.com>
Diffstat (limited to 'ECOMP-ControlloopPolicy/src/test/java')
6 files changed, 1082 insertions, 0 deletions
diff --git a/ECOMP-ControlloopPolicy/src/test/java/org/openecomp/policy/controlloop/compiler/ControlLoopCompilerTest.java b/ECOMP-ControlloopPolicy/src/test/java/org/openecomp/policy/controlloop/compiler/ControlLoopCompilerTest.java new file mode 100644 index 000000000..d26d094c6 --- /dev/null +++ b/ECOMP-ControlloopPolicy/src/test/java/org/openecomp/policy/controlloop/compiler/ControlLoopCompilerTest.java @@ -0,0 +1,80 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP Policy Engine + * ================================================================================ + * 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.openecomp.policy.controlloop.compiler; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; + +import org.junit.Test; + +public class ControlLoopCompilerTest { + + @Test + public void testTest() { + try { + this.test("src/test/resources/v1.0.0/test.yaml"); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + public void testBad1() { + try { + this.test("src/test/resources/v1.0.0/bad_trigger_1.yaml"); + } catch (Exception e) { + e.printStackTrace(); + } + } + @Test + public void testBad2() { + try { + this.test("src/test/resources/v1.0.0/bad_trigger_2.yaml"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testBad() { + try { + this.test("src/test/resources/v1.0.0/bad_policies_1.yaml"); + } catch (Exception e) { + } + } + + public void test(String testFile) throws Exception { + try (InputStream is = new FileInputStream(new File(testFile))) { + ControlLoopCompiler.compile(is, null); + } catch (FileNotFoundException e) { + fail(e.getMessage()); + } catch (IOException e) { + fail(e.getMessage()); + } catch (Exception e) { + throw e; + } + } + +} diff --git a/ECOMP-ControlloopPolicy/src/test/java/org/openecomp/policy/controlloop/compiler/ControlLoopGuardCompilerTest.java b/ECOMP-ControlloopPolicy/src/test/java/org/openecomp/policy/controlloop/compiler/ControlLoopGuardCompilerTest.java new file mode 100644 index 000000000..1e035edf4 --- /dev/null +++ b/ECOMP-ControlloopPolicy/src/test/java/org/openecomp/policy/controlloop/compiler/ControlLoopGuardCompilerTest.java @@ -0,0 +1,101 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP Policy Engine + * ================================================================================ + * 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.openecomp.policy.controlloop.compiler; + +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; + +import org.junit.Test; +import org.openecomp.policy.controlloop.guard.compiler.ControlLoopGuardCompiler; + +public class ControlLoopGuardCompilerTest { + + @Test + public void testTest1() { + try { + this.test("src/test/resources/v2.0.0-guard/policy_guard_OpenECOMP_demo_vDNS.yaml"); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + public void testTest2() { + try { + this.test("src/test/resources/v2.0.0-guard/policy_guard_1707_appc.yaml"); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + public void testBad1() { + try { + this.test("src/test/resources/v2.0.0-guard/no_guard_policy.yaml"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testBad2() { + try { + this.test("src/test/resources/v2.0.0-guard/duplicate_guard_policy.yaml"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testBad3() { + try { + this.test("src/test/resources/v2.0.0-guard/no_guard_constraint.yaml"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testBad4() { + try { + this.test("src/test/resources/v2.0.0-guard/duplicate_guard_constraint.yaml"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void test(String testFile) throws Exception { + try (InputStream is = new FileInputStream(new File(testFile))) { + ControlLoopGuardCompiler.compile(is, null); + } catch (FileNotFoundException e) { + fail(e.getMessage()); + } catch (IOException e) { + fail(e.getMessage()); + } catch (Exception e) { + throw e; + } + } + +} diff --git a/ECOMP-ControlloopPolicy/src/test/java/org/openecomp/policy/controlloop/policy/ControlLoopPolicyBuilderTest.java b/ECOMP-ControlloopPolicy/src/test/java/org/openecomp/policy/controlloop/policy/ControlLoopPolicyBuilderTest.java new file mode 100644 index 000000000..713f35787 --- /dev/null +++ b/ECOMP-ControlloopPolicy/src/test/java/org/openecomp/policy/controlloop/policy/ControlLoopPolicyBuilderTest.java @@ -0,0 +1,516 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP Policy Engine + * ================================================================================ + * 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.openecomp.policy.controlloop.policy; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.UUID; + +import org.junit.Test; +import org.openecomp.policy.asdc.Resource; +import org.openecomp.policy.asdc.ResourceType; +import org.openecomp.policy.asdc.Service; +import org.openecomp.policy.controlloop.policy.builder.BuilderException; +import org.openecomp.policy.controlloop.policy.builder.ControlLoopPolicyBuilder; +import org.openecomp.policy.controlloop.policy.builder.Message; +import org.openecomp.policy.controlloop.policy.builder.MessageLevel; +import org.openecomp.policy.controlloop.policy.builder.Results; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; +import org.yaml.snakeyaml.error.YAMLException; + + +public class ControlLoopPolicyBuilderTest { + + @Test + public void testControlLoop() { + try { + // + // Create a builder for our policy + // + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + // + // Test add services + // + Service vSCP = new Service("vSCP"); + Service vUSP = new Service("vUSP"); + Service vTrinity = new Service("Trinity"); + builder = builder.addService(vSCP, vUSP, vTrinity); + assertTrue(builder.getControlLoop().getServices().size() == 3); + // + // Test remove services + // + builder = builder.removeService(vSCP); + assertTrue(builder.getControlLoop().getServices().size() == 2); + builder = builder.removeAllServices(); + assertTrue(builder.getControlLoop().getServices().size() == 0); + // + // Test add resources + // + Resource vCTS = new Resource("vCTS", ResourceType.VF); + Resource vCOM = new Resource("vCTS", ResourceType.VF); + Resource vRAR = new Resource("vCTS", ResourceType.VF); + builder = builder.addResource(vCTS, vCOM, vRAR); + assertTrue(builder.getControlLoop().getResources().size() == 3); + // + // Test remove resources + // + builder = builder.removeResource(vCTS); + assertTrue(builder.getControlLoop().getResources().size() == 2); + builder = builder.removeAllResources(); + assertTrue(builder.getControlLoop().getResources().size() == 0); + // + // Test set abatement + // + assertFalse(builder.getControlLoop().getAbatement()); + builder = builder.setAbatement(true); + assertTrue(builder.getControlLoop().getAbatement()); + } catch (BuilderException e) { + fail(e.getMessage()); + } + } + + @Test + public void testTimeout() { + try { + // + // Create a builder for our policy + // + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + // + // Test setTimeout + // + assertTrue(builder.getControlLoop().getTimeout() == 2400); + builder = builder.setTimeout(800); + assertTrue(builder.getControlLoop().getTimeout() == 800); + // + // Test calculateTimeout + // + Policy trigger = builder.setTriggerPolicy( + "Restart the VM", + "Upon getting the trigger event, restart the VM", + "APPC", + new Target(TargetType.VM), + "Restart", + null, + 2, + 300); + @SuppressWarnings("unused") + Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult( + "Rebuild VM", + "If the restart fails, rebuild it", + "APPC", + new Target(TargetType.VM), + "Rebuild", + null, + 1, + 600, + trigger.getId(), + PolicyResult.FAILURE, + PolicyResult.FAILURE_RETRIES, + PolicyResult.FAILURE_TIMEOUT); + assertTrue(builder.calculateTimeout().equals(new Integer(300 + 600))); + // + } catch (BuilderException e) { + fail(e.getMessage()); + } + } + + @Test + public void testTriggerPolicyMethods() { + try { + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + // + // Test isOpenLoop + // + assertTrue(builder.isOpenLoop()); + // + // Test set initial trigger policy + // + Policy triggerPolicy1 = builder.setTriggerPolicy( + "Restart the VM", + "Upon getting the trigger event, restart the VM", + "APPC", + new Target(TargetType.VM), + "Restart", + null, + 2, + 300); + assertTrue(builder.isOpenLoop() == false); + assertTrue(builder.getControlLoop().getTrigger_policy().equals(triggerPolicy1.getId())); + // + // Set trigger policy to a new policy + // + @SuppressWarnings("unused") + Policy triggerPolicy2 = builder.setTriggerPolicy( + "Rebuild the VM", + "Upon getting the trigger event, rebuild the VM", + "APPC", + new Target(TargetType.VM), + "Rebuild", + null, + 2, + 300); + // + // Test set trigger policy to another existing policy + // + @SuppressWarnings("unused") + ControlLoop cl = builder.setTriggerPolicy(triggerPolicy1.getId()); + assertTrue(builder.getControlLoop().getTrigger_policy().equals(triggerPolicy1.getId())); + // + // Test get trigger policy + // + assertTrue(builder.getTriggerPolicy().equals(triggerPolicy1)); + // + } catch (BuilderException e) { + fail(e.getMessage()); + } + } + + @Test + public void testAddRemovePolicies() { + try { + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + Policy triggerPolicy = builder.setTriggerPolicy( + "Restart the VM", + "Upon getting the trigger event, restart the VM", + "APPC", + new Target(TargetType.VM), + "Restart", + null, + 2, + 300); + // + // Test create a policy and chain it to the results of trigger policy + // + Policy onRestartFailurePolicy1 = builder.setPolicyForPolicyResult( + "Rebuild VM", + "If the restart fails, rebuild it.", + "APPC", + new Target(TargetType.VM), + "Rebuild", + null, + 1, + 600, + triggerPolicy.getId(), + PolicyResult.FAILURE, + PolicyResult.FAILURE_RETRIES, + PolicyResult.FAILURE_TIMEOUT, + PolicyResult.FAILURE_GUARD); + // + assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy1.getId())); + assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy1.getId())); + assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy1.getId())); + assertTrue(builder.getTriggerPolicy().getFailure_guard().equals(onRestartFailurePolicy1.getId())); + // + // Test remove policy + // + boolean removed = builder.removePolicy(onRestartFailurePolicy1.getId()); + assertTrue(removed); + assertTrue(builder.getTriggerPolicy().getFailure().equals(FinalResult.FINAL_FAILURE.toString())); + assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(FinalResult.FINAL_FAILURE_RETRIES.toString())); + assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString())); + assertTrue(builder.getTriggerPolicy().getFailure_guard().equals(FinalResult.FINAL_FAILURE_GUARD.toString())); + // + // Create another policy and chain it to the results of trigger policy + // + Policy onRestartFailurePolicy2 = builder.setPolicyForPolicyResult( + "Rebuild VM", + "If the restart fails, rebuild it.", + "APPC", + new Target(TargetType.VM), + "Rebuild", + null, + 2, + 600, + triggerPolicy.getId(), + PolicyResult.FAILURE, + PolicyResult.FAILURE_RETRIES, + PolicyResult.FAILURE_TIMEOUT); + // + // Test reset policy results + // + triggerPolicy = builder.resetPolicyResults(triggerPolicy.getId()); + assertTrue(builder.getTriggerPolicy().getFailure().equals(FinalResult.FINAL_FAILURE.toString())); + assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(FinalResult.FINAL_FAILURE_RETRIES.toString())); + assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString())); + // + // Test set the policy results to an existing operational policy + // + onRestartFailurePolicy2 = builder.setPolicyForPolicyResult( + onRestartFailurePolicy2.getId(), + triggerPolicy.getId(), + PolicyResult.FAILURE, + PolicyResult.FAILURE_RETRIES, + PolicyResult.FAILURE_TIMEOUT); + assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy2.getId())); + assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy2.getId())); + assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy2.getId())); + + // + // Test remove all existing operational policies + // + builder = builder.removeAllPolicies(); + assertTrue(builder.getControlLoop().getTrigger_policy().equals(FinalResult.FINAL_OPENLOOP.toString())); + // + } catch (BuilderException e) { + fail(e.getMessage()); + } + } + + @Test + public void testAddOperationsAccumulateParams() { + try { + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + Policy triggerPolicy = builder.setTriggerPolicy( + "Restart the eNodeB", + "Upon getting the trigger event, restart the eNodeB", + "SDNR", + new Target(TargetType.PNF), + "Restart", + null, + 2, + 300); + // + // Add the operationsAccumulateParams + // + triggerPolicy = builder.addOperationsAccumulateParams(triggerPolicy.getId(), new OperationsAccumulateParams("15m", 5)); + assertNotNull(builder.getTriggerPolicy().getOperationsAccumulateParams()); + assertTrue(builder.getTriggerPolicy().getOperationsAccumulateParams().getPeriod().equals("15m")); + assertTrue(builder.getTriggerPolicy().getOperationsAccumulateParams().getLimit() == 5); + // + } catch (BuilderException e) { + fail(e.getMessage()); + } + } + + + @Test + public void testBuildSpecification() { + try { + // + // Create the builder + // + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 800); + // + // Set the first invalid trigger policy + // + Policy policy1 = builder.setTriggerPolicy( + "Restart the VM", + "Upon getting the trigger event, restart the VM", + null, + null, + "Instantiate", + null, + 2, + 300); + Results results = builder.buildSpecification(); + // + // Check that ERRORs are in results for invalid policy arguments + // + boolean invalid_actor = false; + boolean invalid_recipe = false; + boolean invalid_target = false; + for (Message m : results.getMessages()) { + if (m.getMessage().equals("Policy actor is null") && m.getLevel() == MessageLevel.ERROR) { + invalid_actor = true; + } + if (m.getMessage().equals("Policy recipe is invalid") && m.getLevel() == MessageLevel.ERROR) { + invalid_recipe = true; + } + if (m.getMessage().equals("Policy target is null") && m.getLevel() == MessageLevel.ERROR) { + invalid_target = true; + } + } + // + assertTrue(invalid_actor); + assertTrue(invalid_recipe); + assertTrue(invalid_target); + // + // Remove the invalid policy + // + //@SuppressWarnings("unused") + boolean removed = builder.removePolicy(policy1.getId()); + assertTrue(removed); + assertTrue(builder.getTriggerPolicy() == null); + // + // Set a valid trigger policy + // + policy1 = builder.setTriggerPolicy( + "Rebuild VM", + "If the restart fails, rebuild it.", + "APPC", + new Target(TargetType.VM), + "Rebuild", + null, + 1, + 600); + // + // Set a second valid trigger policy + // + Policy policy2 = builder.setTriggerPolicy( + "Restart the VM", + "Upon getting the trigger event, restart the VM", + "APPC", + new Target(TargetType.VM), + "Restart", + null, + 2, + 300); + // + // Now, we have policy1 unreachable + // + results = builder.buildSpecification(); + boolean unreachable = false; + for (Message m : results.getMessages()) { + if (m.getMessage().equals("Policy " + policy1.getId() + " is not reachable.") && m.getLevel() == MessageLevel.WARNING) { + unreachable = true; + break; + } + } + assertTrue(unreachable); + // + // Set policy1 for the failure results of policy2 + // + policy1 = builder.setPolicyForPolicyResult( + policy1.getId(), + policy2.getId(), + PolicyResult.FAILURE, + PolicyResult.FAILURE_RETRIES, + PolicyResult.FAILURE_TIMEOUT); + results = builder.buildSpecification(); + boolean invalid_timeout = false; + for (Message m : results.getMessages()) { + if (m.getMessage().equals("controlLoop overall timeout is less than the sum of operational policy timeouts.") && m.getLevel() == MessageLevel.ERROR) { + invalid_timeout = true; + break; + } + } + assertTrue(invalid_timeout); + // + // Remove policy2 (revert controlLoop back to open loop) + // + removed = builder.removePolicy(policy2.getId()); + // + // ControlLoop is open loop now, but it still has policies (policy1) + // + results = builder.buildSpecification(); + unreachable = false; + for (Message m : results.getMessages()) { + if (m.getMessage().equals("Open Loop policy contains policies. The policies will never be invoked.") && m.getLevel() == MessageLevel.WARNING) { + unreachable = true; + break; + } + } + assertTrue(unreachable); + // + } catch (BuilderException e) { + fail(e.getMessage()); + } + } + + + @Test + public void test() { + this.test("src/test/resources/v1.0.0/policy_Test.yaml"); + } + + @Test + public void testEvilYaml() { + try (InputStream is = new FileInputStream(new File("src/test/resources/v1.0.0/test_evil.yaml"))) { + // + // Read the yaml into our Java Object + // + Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class)); + yaml.load(is); + } catch (FileNotFoundException e) { + fail(e.getLocalizedMessage()); + } catch (IOException e) { + fail(e.getLocalizedMessage()); + } catch (YAMLException e) { + // + // Should have this + // + } + } + + public void test(String testFile) { + try (InputStream is = new FileInputStream(new File(testFile))) { + // + // Read the yaml into our Java Object + // + Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class)); + Object obj = yaml.load(is); + assertNotNull(obj); + assertTrue(obj instanceof ControlLoopPolicy); + ControlLoopPolicy policyTobuild = (ControlLoopPolicy) obj; + // + // Now we're going to try to use the builder to build this. + // + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop( + policyTobuild.getControlLoop().getControlLoopName(), + policyTobuild.getControlLoop().getTimeout()); + // + // Add services + // + if (policyTobuild.getControlLoop().getServices() != null) { + builder = builder.addService(policyTobuild.getControlLoop().getServices().toArray(new Service[policyTobuild.getControlLoop().getServices().size()])); + } + // + // Add resources + // + if (policyTobuild.getControlLoop().getResources() != null) { + builder = builder.addResource(policyTobuild.getControlLoop().getResources().toArray(new Resource[policyTobuild.getControlLoop().getResources().size()])); + } + // + // Add the policies and be sure to set the trigger policy + // + if (policyTobuild.getPolicies() != null) { + for (Policy policy : policyTobuild.getPolicies()) { + if (policy.getId() == policyTobuild.getControlLoop().getTrigger_policy()) { + builder.setTriggerPolicy(policy.getName(), policy.getDescription(), policy.getActor(), policy.getTarget(), policy.getRecipe(), null, policy.getRetry(), policy.getTimeout()); + } + } + } + + // Question : how to change policy ID and results by using builder ?? + + @SuppressWarnings("unused") + Results results = builder.buildSpecification(); + + } catch (FileNotFoundException e) { + fail(e.getLocalizedMessage()); + } catch (IOException e) { + fail(e.getLocalizedMessage()); + } catch (BuilderException e) { + fail(e.getLocalizedMessage()); + } + + } + +} diff --git a/ECOMP-ControlloopPolicy/src/test/java/org/openecomp/policy/controlloop/policy/ControlLoopPolicyTest.java b/ECOMP-ControlloopPolicy/src/test/java/org/openecomp/policy/controlloop/policy/ControlLoopPolicyTest.java new file mode 100644 index 000000000..15d327087 --- /dev/null +++ b/ECOMP-ControlloopPolicy/src/test/java/org/openecomp/policy/controlloop/policy/ControlLoopPolicyTest.java @@ -0,0 +1,101 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP Policy Engine + * ================================================================================ + * 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.openecomp.policy.controlloop.policy; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; + +import org.junit.Test; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.DumperOptions.FlowStyle; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; + + +public class ControlLoopPolicyTest { + + @Test + public void test() { + this.test("src/test/resources/v1.0.0/policy_Test.yaml"); + } + + @Test + public void testMultipleService() { + this.test("src/test/resources/v1.0.0/policy_Test_MultipleService.yaml"); + } + + @Test + public void testOpenLoop() { + this.test("src/test/resources/v1.0.0/policy_OpenLoop_1610.yaml"); + } + + @Test + public void testOpenECOMPvDNS() { + this.test("src/test/resources/v2.0.0/policy_OpenECOMP_demo_vDNS.yaml"); + } + + public void test(String testFile) { + try (InputStream is = new FileInputStream(new File(testFile))) { + // + // Read the yaml into our Java Object + // + Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class)); + Object obj = yaml.load(is); + assertNotNull(obj); + assertTrue(obj instanceof ControlLoopPolicy); + dump(obj); + // + // Now dump it to a yaml string + // + DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(FlowStyle.BLOCK); + options.setPrettyFlow(true); + yaml = new Yaml(options); + String dumpedYaml = yaml.dump(obj); + System.out.println(dumpedYaml); + // + // Read that string back into our java object + // + Object newObject = yaml.load(dumpedYaml); + dump(newObject); + assertNotNull(newObject); + assertTrue(newObject instanceof ControlLoopPolicy); + // + // Have to comment it out tentatively since it causes junit to fail. + // Seems we cannot use assertEquals here. Need advice. + // + //assertEquals(newObject, obj); + } catch (FileNotFoundException e) { + fail(e.getLocalizedMessage()); + } catch (IOException e) { + fail(e.getLocalizedMessage()); + } + } + + public void dump(Object obj) { + System.out.println("Dumping " + obj.getClass().getCanonicalName()); + System.out.println(obj.toString()); + } +} diff --git a/ECOMP-ControlloopPolicy/src/test/java/org/openecomp/policy/controlloop/policy/guard/ControlLoopGuardBuilderTest.java b/ECOMP-ControlloopPolicy/src/test/java/org/openecomp/policy/controlloop/policy/guard/ControlLoopGuardBuilderTest.java new file mode 100644 index 000000000..5df0266ec --- /dev/null +++ b/ECOMP-ControlloopPolicy/src/test/java/org/openecomp/policy/controlloop/policy/guard/ControlLoopGuardBuilderTest.java @@ -0,0 +1,193 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP Policy Engine + * ================================================================================ + * 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.openecomp.policy.controlloop.policy.guard; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import org.junit.Test; +import org.openecomp.policy.controlloop.policy.builder.BuilderException; +import org.openecomp.policy.controlloop.policy.builder.Message; +import org.openecomp.policy.controlloop.policy.builder.MessageLevel; +import org.openecomp.policy.controlloop.policy.builder.Results; +import org.openecomp.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; + +public class ControlLoopGuardBuilderTest { + + @Test + public void testControlLoopGuard() { + try { + // + // Create a builder + // + ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard()); + // + // Assert there is no guard policies yet + // + Results results = builder.buildSpecification(); + boolean no_guard_policies = false; + for (Message m : results.getMessages()) { + if (m.getMessage().equals("ControlLoop Guard should have at least one guard policies") && m.getLevel() == MessageLevel.ERROR) { + no_guard_policies = true; + break; + } + } + assertTrue(no_guard_policies); + // + // Add a guard policy without limit constraint + // + GuardPolicy policy1 = new GuardPolicy("1111", "guardpolicy1", "guardpolicy1", "APPC", "restart"); + builder = builder.addGuardPolicy(policy1); + // + // Assert there is no limit constraint associated with the only guard policy + // + results = builder.buildSpecification(); + boolean no_constraint = false; + for (Message m : results.getMessages()) { + if (m.getMessage().equals("Guard policy guardpolicy1 does not have any limit constraint") && m.getLevel() == MessageLevel.ERROR) { + no_constraint = true; + break; + } + } + assertTrue(no_constraint); + // + // Add a constraint to policy1 + // + Map<String, String> time_in_range = new HashMap<String, String>(); + time_in_range.put("arg2", "PT5H"); + time_in_range.put("arg3", "PT24H"); + List<String> blacklist = new LinkedList<String>(); + blacklist.add("eNodeB_common_id1"); + blacklist.add("eNodeB_common_id2"); + Constraint cons = new Constraint(5, "PT15M", time_in_range, blacklist); + builder = builder.addLimitConstraint(policy1.getId(), cons); + // + // Add a duplicate constraint to policy1 + // + builder = builder.addLimitConstraint(policy1.getId(), cons); + // + // Assert there are duplicate constraints associated with the only guard policy + // + results = builder.buildSpecification(); + boolean duplicate_constraint = false; + for (Message m : results.getMessages()) { + if (m.getMessage().equals("Guard policy guardpolicy1 has duplicate limit constraints") && m.getLevel() == MessageLevel.WARNING) { + duplicate_constraint = true; + break; + } + } + assertTrue(duplicate_constraint); + // + // Remove the duplicate constraint + // + builder = builder.removeLimitConstraint(policy1.getId(), cons); + // + // Add a duplicate guard policy + // + builder = builder.addGuardPolicy(policy1); + builder = builder.addLimitConstraint(policy1.getId(), cons); + // + // Assert there are duplicate guard policies + // + results = builder.buildSpecification(); + boolean duplicate_guard_policy = false; + for (Message m : results.getMessages()) { + if (m.getMessage().equals("There are duplicate guard policies") && m.getLevel() == MessageLevel.WARNING) { + duplicate_guard_policy = true; + break; + } + } + assertTrue(duplicate_guard_policy); + // + // Remove the duplicate guard policy + // + builder = builder.removeGuardPolicy(policy1); + // + // Assert there are no Error/Warning message + // + results = builder.buildSpecification(); + assertTrue(results.getMessages().size() == 1); + // + } catch (BuilderException e) { + fail(e.getMessage()); + } + } + + @Test + public void test1() { + this.test("src/test/resources/v2.0.0-guard/policy_guard_OpenECOMP_demo_vDNS.yaml"); + } + + @Test + public void test2() { + this.test("src/test/resources/v2.0.0-guard/policy_guard_1707_appc.yaml"); + } + + public void test(String testFile) { + try (InputStream is = new FileInputStream(new File(testFile))) { + // + // Read the yaml into our Java Object + // + Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class)); + Object obj = yaml.load(is); + assertNotNull(obj); + assertTrue(obj instanceof ControlLoopGuard); + ControlLoopGuard guardTobuild = (ControlLoopGuard) obj; + // + // Now we're going to try to use the builder to build this. + // + ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(guardTobuild.getGuard()); + // + // Add guard policy + // + if (guardTobuild.getGuards() != null) { + builder = builder.addGuardPolicy(guardTobuild.getGuards().toArray(new GuardPolicy[guardTobuild.getGuards().size()])); + } + // + // Build the specification + // + Results results = builder.buildSpecification(); + // + // Print out the specification + // + System.out.println(results.getSpecification()); + // + } catch (FileNotFoundException e) { + fail(e.getLocalizedMessage()); + } catch (IOException e) { + fail(e.getLocalizedMessage()); + } catch (BuilderException e) { + fail(e.getLocalizedMessage()); + } + } +} diff --git a/ECOMP-ControlloopPolicy/src/test/java/org/openecomp/policy/controlloop/policy/guard/ControlLoopGuardTest.java b/ECOMP-ControlloopPolicy/src/test/java/org/openecomp/policy/controlloop/policy/guard/ControlLoopGuardTest.java new file mode 100644 index 000000000..a09fd2112 --- /dev/null +++ b/ECOMP-ControlloopPolicy/src/test/java/org/openecomp/policy/controlloop/policy/guard/ControlLoopGuardTest.java @@ -0,0 +1,91 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP Policy Engine + * ================================================================================ + * 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.openecomp.policy.controlloop.policy.guard; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; + +import org.junit.Test; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.DumperOptions.FlowStyle; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; + + +public class ControlLoopGuardTest { + + @Test + public void testGuardvDNS() { + this.test("src/test/resources/v2.0.0-guard/policy_guard_OpenECOMP_demo_vDNS.yaml"); + } + + @Test + public void testGuardvUSP() { + this.test("src/test/resources/v2.0.0-guard/policy_guard_1707_appc.yaml"); + } + + public void test(String testFile) { + try (InputStream is = new FileInputStream(new File(testFile))) { + // + // Read the yaml into our Java Object + // + Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class)); + Object obj = yaml.load(is); + assertNotNull(obj); + assertTrue(obj instanceof ControlLoopGuard); + dump(obj); + // + // Now dump it to a yaml string + // + DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(FlowStyle.BLOCK); + options.setPrettyFlow(true); + yaml = new Yaml(options); + String dumpedYaml = yaml.dump(obj); + System.out.println(dumpedYaml); + // + // Read that string back into our java object + // + Object newObject = yaml.load(dumpedYaml); + dump(newObject); + assertNotNull(newObject); + assertTrue(newObject instanceof ControlLoopGuard); + // + // Have to comment it out tentatively since it causes junit to fail. + // Seems we cannot use assertEquals here. Need advice. + // + //assertEquals(newObject, obj); + } catch (FileNotFoundException e) { + fail(e.getLocalizedMessage()); + } catch (IOException e) { + fail(e.getLocalizedMessage()); + } + } + + public void dump(Object obj) { + System.out.println("Dumping " + obj.getClass().getCanonicalName()); + System.out.println(obj.toString()); + } +} |