From 5ae90a9bf72d2d6dbb163c3dbea5474b657be7e7 Mon Sep 17 00:00:00 2001 From: Tarun Tej Velaga Date: Tue, 15 Aug 2017 13:20:02 +0000 Subject: Policy-yaml changes Changes to Policy-yaml and sdc projects based on changes from policy/engine. Issue-Id: POLICY-88 Change-Id: Ic1a58f00029b9a66db6980fc7732a2ac57390229 Signed-off-by: Tarun Tej Velaga --- .../policy/ControlLoopPolicyBuilderTest.java | 581 ++++++++++----------- .../policy/guard/ControlLoopGuardBuilderTest.java | 321 ++++++------ 2 files changed, 442 insertions(+), 460 deletions(-) (limited to 'controlloop/common/policy-yaml/src/test/java') diff --git a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyBuilderTest.java b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyBuilderTest.java index 90d498a8d..f1681247d 100644 --- a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyBuilderTest.java +++ b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyBuilderTest.java @@ -33,12 +33,6 @@ import java.io.InputStream; import java.util.UUID; import org.junit.Test; -import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.constructor.Constructor; -import org.yaml.snakeyaml.error.YAMLException; - -import org.onap.policy.aai.PNF; -import org.onap.policy.aai.PNFType; import org.onap.policy.controlloop.policy.builder.BuilderException; import org.onap.policy.controlloop.policy.builder.ControlLoopPolicyBuilder; import org.onap.policy.controlloop.policy.builder.Message; @@ -47,85 +41,74 @@ import org.onap.policy.controlloop.policy.builder.Results; import org.onap.policy.sdc.Resource; import org.onap.policy.sdc.ResourceType; import org.onap.policy.sdc.Service; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; +import org.yaml.snakeyaml.error.YAMLException; public class ControlLoopPolicyBuilderTest { - - @Test - public void testControlLoop() { + + @Test + public void testControlLoop() { try { - // - // Create a builder for our policy - // - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - // - // Test add services - // - Service Ser1 = new Service("Ser1"); - Service Ser2 = new Service("Ser2"); - Service Ser3 = new Service("Ser3"); - builder = builder.addService(Ser1, Ser2, Ser3); - assertTrue(builder.getControlLoop().services.size() == 3); - // - // Test remove services - // - builder = builder.removeService(Ser1); - assertTrue(builder.getControlLoop().services.size() == 2); - builder = builder.removeAllServices(); - assertTrue(builder.getControlLoop().services.size() == 0); - // - // Test add resources - // - Resource Resrc1 = new Resource("Resrc1", ResourceType.VF); - Resource Resrc2 = new Resource("Resrc1", ResourceType.VF); - Resource Resrc3 = new Resource("Resrc1", ResourceType.VF); - builder = builder.addResource(Resrc1, Resrc2, Resrc3); - assertTrue(builder.getControlLoop().resources.size() == 3); - // - // Test remove resources - // - builder = builder.removeResource(Resrc1); - assertTrue(builder.getControlLoop().resources.size() == 2); - builder = builder.removeAllResources(); - assertTrue(builder.getControlLoop().resources.size() == 0); - // - // Test set pnf - // - PNF pnf = new PNF(); - pnf.PNFName = UUID.randomUUID().toString(); - pnf.PNFType = PNFType.ENODEB; - assertTrue(builder.getControlLoop().pnf == null); - builder = builder.setPNF(pnf); - assertTrue(builder.getControlLoop().pnf != null); - // - // Test remove pnf - // - builder = builder.removePNF(); - assertTrue(builder.getControlLoop().pnf == null); + // + // 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().abatement); + assertFalse(builder.getControlLoop().getAbatement()); builder = builder.setAbatement(true); - assertTrue(builder.getControlLoop().abatement); - } catch (BuilderException e) { - fail(e.getMessage()); - } - } - - @Test - public void testTimeout() { + 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); + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); // // Test setTimeout // - assertTrue(builder.getControlLoop().timeout == 2400); + assertTrue(builder.getControlLoop().getTimeout() == 2400); builder = builder.setTimeout(800); - assertTrue(builder.getControlLoop().timeout == 800); + assertTrue(builder.getControlLoop().getTimeout() == 800); // // Test calculateTimeout // @@ -139,7 +122,7 @@ public class ControlLoopPolicyBuilderTest { 2, 300); @SuppressWarnings("unused") - Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult( + Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult( "Rebuild VM", "If the restart fails, rebuild it", "APPC", @@ -148,7 +131,7 @@ public class ControlLoopPolicyBuilderTest { null, 1, 600, - trigger.id, + trigger.getId(), PolicyResult.FAILURE, PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT); @@ -157,11 +140,11 @@ public class ControlLoopPolicyBuilderTest { } catch (BuilderException e) { fail(e.getMessage()); } - } - - @Test - public void testTriggerPolicyMethods() { - try { + } + + @Test + public void testTriggerPolicyMethods() { + try { ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); // // Test isOpenLoop @@ -180,12 +163,12 @@ public class ControlLoopPolicyBuilderTest { 2, 300); assertTrue(builder.isOpenLoop() == false); - assertTrue(builder.getControlLoop().trigger_policy.equals(triggerPolicy1.id)); + assertTrue(builder.getControlLoop().getTrigger_policy().equals(triggerPolicy1.getId())); // // Set trigger policy to a new policy // @SuppressWarnings("unused") - Policy triggerPolicy2 = builder.setTriggerPolicy( + Policy triggerPolicy2 = builder.setTriggerPolicy( "Rebuild the VM", "Upon getting the trigger event, rebuild the VM", "APPC", @@ -198,8 +181,8 @@ public class ControlLoopPolicyBuilderTest { // Test set trigger policy to another existing policy // @SuppressWarnings("unused") - ControlLoop cl = builder.setTriggerPolicy(triggerPolicy1.id); - assertTrue(builder.getControlLoop().trigger_policy.equals(triggerPolicy1.id)); + ControlLoop cl = builder.setTriggerPolicy(triggerPolicy1.getId()); + assertTrue(builder.getControlLoop().getTrigger_policy().equals(triggerPolicy1.getId())); // // Test get trigger policy // @@ -209,11 +192,11 @@ public class ControlLoopPolicyBuilderTest { fail(e.getMessage()); } } - - @Test - public void testAddRemovePolicies() { - try { - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + + @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", @@ -235,25 +218,25 @@ public class ControlLoopPolicyBuilderTest { null, 1, 600, - triggerPolicy.id, + triggerPolicy.getId(), PolicyResult.FAILURE, PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT, PolicyResult.FAILURE_GUARD); // - assertTrue(builder.getTriggerPolicy().failure.equals(onRestartFailurePolicy1.id)); - assertTrue(builder.getTriggerPolicy().failure_retries.equals(onRestartFailurePolicy1.id)); - assertTrue(builder.getTriggerPolicy().failure_timeout.equals(onRestartFailurePolicy1.id)); - assertTrue(builder.getTriggerPolicy().failure_guard.equals(onRestartFailurePolicy1.id)); + 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.id); + boolean removed = builder.removePolicy(onRestartFailurePolicy1.getId()); assertTrue(removed); - assertTrue(builder.getTriggerPolicy().failure.equals(FinalResult.FINAL_FAILURE.toString())); - assertTrue(builder.getTriggerPolicy().failure_retries.equals(FinalResult.FINAL_FAILURE_RETRIES.toString())); - assertTrue(builder.getTriggerPolicy().failure_timeout.equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString())); - assertTrue(builder.getTriggerPolicy().failure_guard.equals(FinalResult.FINAL_FAILURE_GUARD.toString())); + 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 // @@ -266,45 +249,45 @@ public class ControlLoopPolicyBuilderTest { null, 2, 600, - triggerPolicy.id, + triggerPolicy.getId(), PolicyResult.FAILURE, PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT); // // Test reset policy results // - triggerPolicy = builder.resetPolicyResults(triggerPolicy.id); - assertTrue(builder.getTriggerPolicy().failure.equals(FinalResult.FINAL_FAILURE.toString())); - assertTrue(builder.getTriggerPolicy().failure_retries.equals(FinalResult.FINAL_FAILURE_RETRIES.toString())); - assertTrue(builder.getTriggerPolicy().failure_timeout.equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString())); + 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.id, - triggerPolicy.id, + onRestartFailurePolicy2.getId(), + triggerPolicy.getId(), PolicyResult.FAILURE, PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT); - assertTrue(builder.getTriggerPolicy().failure.equals(onRestartFailurePolicy2.id)); - assertTrue(builder.getTriggerPolicy().failure_retries.equals(onRestartFailurePolicy2.id)); - assertTrue(builder.getTriggerPolicy().failure_timeout.equals(onRestartFailurePolicy2.id)); + 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().trigger_policy.equals(FinalResult.FINAL_OPENLOOP.toString())); + 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); + @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", @@ -317,28 +300,28 @@ public class ControlLoopPolicyBuilderTest { // // Add the operationsAccumulateParams // - triggerPolicy = builder.addOperationsAccumulateParams(triggerPolicy.id, new OperationsAccumulateParams("15m", 5)); - assertNotNull(builder.getTriggerPolicy().operationsAccumulateParams); - assertTrue(builder.getTriggerPolicy().operationsAccumulateParams.period.equals("15m")); - assertTrue(builder.getTriggerPolicy().operationsAccumulateParams.limit == 5); + 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) { + } 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( + } + + + @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, @@ -347,39 +330,39 @@ public class ControlLoopPolicyBuilderTest { 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.id); - assertTrue(removed); - assertTrue(builder.getTriggerPolicy() == null); - // - // Set a valid trigger policy - // - policy1 = builder.setTriggerPolicy( + 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", @@ -388,11 +371,11 @@ public class ControlLoopPolicyBuilderTest { null, 1, 600); - // - // Set a second valid trigger policy - // - Policy policy2 = builder.setTriggerPolicy( - "Restart the VM", + // + // 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), @@ -400,141 +383,141 @@ public class ControlLoopPolicyBuilderTest { 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.id + " is not reachable.") && m.getLevel() == MessageLevel.WARNING) { - unreachable = true; - break; - } - } - assertTrue(unreachable); - // - // Set policy1 for the failure results of policy2 - // - policy1 = builder.setPolicyForPolicyResult( - policy1.id, - policy2.id, - PolicyResult.FAILURE, + // + // 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.id); - // - // 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) { + 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.controlLoop.controlLoopName, - policyTobuild.controlLoop.timeout); - // - // Add services - // - if (policyTobuild.controlLoop.services != null) { - builder = builder.addService(policyTobuild.controlLoop.services.toArray(new Service[policyTobuild.controlLoop.services.size()])); - } - // - // Add resources - // - if (policyTobuild.controlLoop.resources != null) { - builder = builder.addResource(policyTobuild.controlLoop.resources.toArray(new Resource[policyTobuild.controlLoop.resources.size()])); - } - // - // Set pnf - // - if (policyTobuild.controlLoop.pnf != null) { - builder = builder.setPNF(policyTobuild.controlLoop.pnf); - } - // - // Add the policies and be sure to set the trigger policy - // - if (policyTobuild.policies != null) { - for (Policy policy : policyTobuild.policies) { - if (policy.id == policyTobuild.controlLoop.trigger_policy) { - builder.setTriggerPolicy(policy.name, policy.description, policy.actor, policy.target, policy.recipe, null, policy.retry, policy.timeout); - } - } - } - - // 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()); - } - - } + } + + + @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()])); + } + // + // Set pnf + // + if (policyTobuild.getControlLoop().getPnf() != null) { + builder = builder.setPNF(policyTobuild.getControlLoop().getPnf()); + } + // + // 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/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuardBuilderTest.java b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuardBuilderTest.java index 9cc1bbd6a..da81db394 100644 --- a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuardBuilderTest.java +++ b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuardBuilderTest.java @@ -20,10 +20,6 @@ package org.onap.policy.controlloop.policy.guard; -import org.junit.Test; -import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.constructor.Constructor; - import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -38,167 +34,170 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import org.junit.Test; import org.onap.policy.controlloop.policy.builder.BuilderException; import org.onap.policy.controlloop.policy.builder.Message; import org.onap.policy.controlloop.policy.builder.MessageLevel; import org.onap.policy.controlloop.policy.builder.Results; -import org.onap.policy.controlloop.poligy.guard.builder.ControlLoopGuardBuilder; +import org.onap.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 - // - String clname = "CL_vUSP123"; - LinkedList targets = new LinkedList(); - targets.add("s1"); - targets.add("s2"); - targets.add("s3"); - MatchParameters matchParameters = new MatchParameters(clname, "APPC", "Restart", targets); - GuardPolicy policy1 = new GuardPolicy("id123", "guardpolicy1", "description aaa", matchParameters); - 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 active_time_range = new HashMap(); - active_time_range.put("start", "00:00:00-05:00"); - active_time_range.put("end", "23:59:59-05:00"); - List blacklist = new LinkedList(); - blacklist.add("eNodeB_common_id1"); - blacklist.add("eNodeB_common_id2"); - Map time_window = new HashMap(); - time_window.put("value", "10"); - time_window.put("units", "minute"); - Constraint cons = new Constraint(5, time_window, active_time_range, blacklist); - builder = builder.addLimitConstraint(policy1.id, cons); - // - // Add a duplicate constraint to policy1 - // - builder = builder.addLimitConstraint(policy1.id, 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.id, cons); - // - // Add a duplicate guard policy - // - builder = builder.addGuardPolicy(policy1); - builder = builder.addLimitConstraint(policy1.id, 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_ONAP_demo_vDNS.yaml"); - } - - @Test - public void test2() { - this.test("src/test/resources/v2.0.0-guard/policy_guard_appc_restart.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.guard); - // - // Add guard policy - // - if (guardTobuild.guards != null) { - builder = builder.addGuardPolicy(guardTobuild.guards.toArray(new GuardPolicy[guardTobuild.guards.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()); - } - } + + @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 + // + String clname = "CL_vUSP123"; + LinkedList targets = new LinkedList(); + targets.add("s1"); + targets.add("s2"); + targets.add("s3"); + MatchParameters matchParameters = new MatchParameters(clname, "APPC", "Restart", targets); + GuardPolicy policy1 = new GuardPolicy("id123", "guardpolicy1", "description aaa", matchParameters); + 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 active_time_range = new HashMap(); + active_time_range.put("start", "00:00:00-05:00"); + active_time_range.put("end", "23:59:59-05:00"); + List blacklist = new LinkedList(); + blacklist.add("eNodeB_common_id1"); + blacklist.add("eNodeB_common_id2"); + Map time_window = new HashMap(); + time_window.put("value", "10"); + time_window.put("units", "minute"); + Constraint cons = new Constraint(5, time_window, active_time_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_ONAP_demo_vDNS.yaml"); + } + + @Test + public void test2() { + this.test("src/test/resources/v2.0.0-guard/policy_guard_appc_restart.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()); + } + } } -- cgit 1.2.3-korg