diff options
author | krishnajinka <kris.jinka@samsung.com> | 2018-11-12 23:40:08 +0900 |
---|---|---|
committer | krishnajinka <kris.jinka@samsung.com> | 2018-11-12 23:40:29 +0900 |
commit | 9dffa5430faa920a5f02b54b93531cc2cb41b02e (patch) | |
tree | e2b4577e0b26fc56ad17978e1edff25a54a2c12b /controlloop/common/policy-yaml | |
parent | 69aba39cbd866a1b611613a7b14db50d86944b94 (diff) |
Use builder for trigger policy
Fix an issue reported by sonar for using more than
7 parameters in the method.
Issue-ID: POLICY-1251
Change-Id: I07d668b706a27dcc03e862d08262f2c0269ad4b7
Signed-off-by: Krishnajinka <kris.jinka@samsung.com>
Diffstat (limited to 'controlloop/common/policy-yaml')
3 files changed, 140 insertions, 52 deletions
diff --git a/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/policy/builder/ControlLoopPolicyBuilder.java b/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/policy/builder/ControlLoopPolicyBuilder.java index 8ea33d5f0..123fe2fa1 100644 --- a/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/policy/builder/ControlLoopPolicyBuilder.java +++ b/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/policy/builder/ControlLoopPolicyBuilder.java @@ -26,6 +26,7 @@ import org.onap.policy.aai.Pnf; import org.onap.policy.controlloop.policy.ControlLoop; import org.onap.policy.controlloop.policy.OperationsAccumulateParams; import org.onap.policy.controlloop.policy.Policy; +import org.onap.policy.controlloop.policy.PolicyParam; import org.onap.policy.controlloop.policy.PolicyResult; import org.onap.policy.controlloop.policy.Target; import org.onap.policy.controlloop.policy.builder.impl.ControlLoopPolicyBuilderImpl; @@ -135,18 +136,11 @@ public interface ControlLoopPolicyBuilder { * Platform. * * - * @param name name - * @param description description - * @param actor actor - * @param target target - * @param recipe recipe - * @param retries retries - * @param timeout timeout + * @param policy Policy parameters object * @return Policy object * @throws BuilderException builder exception */ - public Policy setTriggerPolicy(String name, String description, String actor, Target target, String recipe, - Map<String, String> payload, Integer retries, Integer timeout) throws BuilderException; + public Policy setTriggerPolicy(PolicyParam policy) throws BuilderException; /** * Changes the trigger policy to point to another existing Policy. @@ -155,7 +149,7 @@ public interface ControlLoopPolicyBuilder { * @return ControlLoop object * @throws BuilderException build exception */ - public ControlLoop setTriggerPolicy(String id) throws BuilderException; + public ControlLoop setExistingTriggerPolicy(String id) throws BuilderException; /** * Is an open loop. diff --git a/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/policy/builder/impl/ControlLoopPolicyBuilderImpl.java b/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/policy/builder/impl/ControlLoopPolicyBuilderImpl.java index 48f929149..6950523cb 100644 --- a/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/policy/builder/impl/ControlLoopPolicyBuilderImpl.java +++ b/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/policy/builder/impl/ControlLoopPolicyBuilderImpl.java @@ -199,21 +199,10 @@ public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder { } @Override - public Policy setTriggerPolicy(String name, String description, String actor, Target target, - String recipe, Map<String, String> payload, Integer retries, Integer timeout) + public Policy setTriggerPolicy(PolicyParam policyParam) throws BuilderException { - Policy trigger = new Policy(PolicyParam.builder() - .id(UUID.randomUUID().toString()) - .name(name) - .description(description) - .actor(actor) - .payload(payload) - .target(target) - .recipe(recipe) - .retries(retries) - .timeout(timeout) - .build()); + Policy trigger = new Policy(policyParam); controlLoopPolicy.getControlLoop().setTrigger_policy(trigger.getId()); @@ -225,7 +214,7 @@ public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder { } @Override - public ControlLoop setTriggerPolicy(String id) throws BuilderException { + public ControlLoop setExistingTriggerPolicy(String id) throws BuilderException { if (id == null) { throw new BuilderException("Id must not be null"); } 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 90ce96b62..ac36603a1 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 @@ -329,8 +329,15 @@ public class ControlLoopPolicyBuilderTest { // 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); + builder.setTriggerPolicy(PolicyParam.builder().id(UUID.randomUUID().toString()) + .name("Restart the VM") + .description("Upon getting the trigger event, restart the VM") + .actor("APPC") + .target(new Target(TargetType.VM)) + .recipe("Restart") + .payload(null) + .retries(2) + .timeout(300).build()); @SuppressWarnings("unused") Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult("Rebuild VM", "If the restart fails, rebuild it", "APPC", new Target(TargetType.VM), "Rebuild", null, 1, 600, @@ -355,8 +362,16 @@ public class ControlLoopPolicyBuilderTest { // 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); + builder.setTriggerPolicy( + PolicyParam.builder().id(UUID.randomUUID().toString()) + .name("Restart the VM") + .description("Upon getting the trigger event, restart the VM") + .actor("APPC") + .target(new Target(TargetType.VM)) + .recipe("Restart") + .payload(null) + .retries(2) + .timeout(300).build()); assertTrue(builder.isOpenLoop() == false); assertTrue(builder.getControlLoop().getTrigger_policy().equals(triggerPolicy1.getId())); // @@ -364,13 +379,22 @@ public class ControlLoopPolicyBuilderTest { // @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); + builder.setTriggerPolicy( + PolicyParam.builder() + .id(UUID.randomUUID().toString()) + .name("Rebuild the VM") + .description("Upon getting the trigger event, rebuild the VM") + .actor("APPC") + .target(new Target(TargetType.VM)) + .recipe("Rebuild") + .payload(null) + .retries(2) + .timeout(300).build()); // // Test set trigger policy to another existing policy // @SuppressWarnings("unused") - ControlLoop cl = builder.setTriggerPolicy(triggerPolicy1.getId()); + ControlLoop cl = builder.setExistingTriggerPolicy(triggerPolicy1.getId()); assertTrue(builder.getControlLoop().getTrigger_policy().equals(triggerPolicy1.getId())); // // Test get trigger policy @@ -388,7 +412,7 @@ public class ControlLoopPolicyBuilderTest { ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); expectedException.expect(BuilderException.class); expectedException.expectMessage("Id must not be null"); - builder.setTriggerPolicy(null); + builder.setExistingTriggerPolicy(null); } @Test @@ -398,19 +422,28 @@ public class ControlLoopPolicyBuilderTest { final String unknownPolicyId = "100"; expectedException.expect(BuilderException.class); expectedException.expectMessage("Unknown policy " + unknownPolicyId); - builder.setTriggerPolicy(unknownPolicyId); + builder.setExistingTriggerPolicy(unknownPolicyId); } @Test public void testSetTriggerPolicyUnknownPolicy() throws BuilderException { ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - builder.setTriggerPolicy("Restart the VM", "Upon getting the trigger event, restart the VM", "APPC", - new Target(TargetType.VM), "Restart", null, 2, 300); + builder.setTriggerPolicy( + PolicyParam.builder() + .id(UUID.randomUUID().toString()) + .name("Restart the VM") + .description("Upon getting the trigger event, restart the VM") + .actor("APPC") + .target(new Target(TargetType.VM)) + .recipe("Restart") + .payload(null) + .retries(2) + .timeout(300).build()); final String unknownPolicyId = "100"; expectedException.expect(BuilderException.class); expectedException.expectMessage("Unknown policy " + unknownPolicyId); - builder.setTriggerPolicy(unknownPolicyId); + builder.setExistingTriggerPolicy(unknownPolicyId); } @Test @@ -419,8 +452,17 @@ public class ControlLoopPolicyBuilderTest { 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); + builder.setTriggerPolicy( + PolicyParam.builder() + .id(UUID.randomUUID().toString()) + .name("Restart the VM") + .description("Upon getting the trigger event, restart the VM") + .actor("APPC") + .target(new Target(TargetType.VM)) + .recipe("Restart") + .payload(null) + .retries(2) + .timeout(300).build()); // // Test create a policy and chain it to the results of trigger policy // @@ -524,8 +566,17 @@ public class ControlLoopPolicyBuilderTest { 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); + builder.setTriggerPolicy( + PolicyParam.builder() + .id(UUID.randomUUID().toString()) + .name("Restart the VM") + .description("Upon getting the trigger event, restart the VM") + .actor("APPC") + .target(new Target(TargetType.VM)) + .recipe("Restart") + .payload(null) + .retries(2) + .timeout(300).build()); Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult("Rebuild VM", @@ -544,8 +595,17 @@ public class ControlLoopPolicyBuilderTest { 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); + builder.setTriggerPolicy( + PolicyParam.builder() + .id(UUID.randomUUID().toString()) + .name("Restart the VM") + .description("Upon getting the trigger event, restart the VM") + .actor("APPC") + .target(new Target(TargetType.VM)) + .recipe("Restart") + .payload(null) + .retries(2) + .timeout(300).build()); final String unknownPolicyId = "100"; expectedException.expect(BuilderException.class); @@ -560,8 +620,17 @@ public class ControlLoopPolicyBuilderTest { ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); Policy triggerPolicy = - builder.setTriggerPolicy("Restart the eNodeB", "Upon getting the trigger event, restart the eNodeB", - "RANController", new Target(TargetType.PNF), "Restart", null, 2, 300); + builder.setTriggerPolicy( + PolicyParam.builder() + .id(UUID.randomUUID().toString()) + .name("Restart the eNodeB") + .description("Upon getting the trigger event, restart the eNodeB") + .actor("RANController") + .target(new Target(TargetType.PNF)) + .recipe("Restart") + .payload(null) + .retries(2) + .timeout(300).build()); // // Add the operationsAccumulateParams // @@ -588,8 +657,17 @@ public class ControlLoopPolicyBuilderTest { // // Set the first invalid trigger policy // - final Policy policy1 = builder.setTriggerPolicy("Restart the VM", - "Upon getting the trigger event, restart the VM", null, null, "Instantiate", null, 2, 300); + final Policy policy1 = builder.setTriggerPolicy( + PolicyParam.builder() + .id(UUID.randomUUID().toString()) + .name("Restart the VM") + .description("Upon getting the trigger event, restart the VM") + .actor(null) + .target(null) + .recipe("Instantiate") + .payload(null) + .retries(2) + .timeout(300).build()); Results results = builder.buildSpecification(); // // Check that ERRORs are in results for invalid policy arguments @@ -622,14 +700,32 @@ public class ControlLoopPolicyBuilderTest { // // Set a valid trigger policy // - Policy policy1a = builder.setTriggerPolicy("Rebuild VM", "If the restart fails, rebuild it.", "APPC", - new Target(TargetType.VM), "Rebuild", null, 1, 600); + Policy policy1a = builder.setTriggerPolicy( + PolicyParam.builder() + .id(UUID.randomUUID().toString()) + .name("Rebuild VM") + .description("If the restart fails, rebuild it.") + .actor("APPC") + .target(new Target(TargetType.VM)) + .recipe("Rebuild") + .payload(null) + .retries(1) + .timeout(600).build()); // // Set a second valid trigger policy // final Policy policy2 = - builder.setTriggerPolicy("Restart the VM", "Upon getting the trigger event, restart the VM", "APPC", - new Target(TargetType.VM), "Restart", null, 2, 300); + builder.setTriggerPolicy( + PolicyParam.builder() + .id(UUID.randomUUID().toString()) + .name("Restart the VM") + .description("Upon getting the trigger event, restart the VM") + .actor("APPC") + .target(new Target(TargetType.VM)) + .recipe("Restart") + .payload(null) + .retries(2) + .timeout(300).build()); // // Now, we have policy1 unreachable // @@ -753,8 +849,17 @@ public class ControlLoopPolicyBuilderTest { 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()); + builder.setTriggerPolicy( + PolicyParam.builder() + .id(UUID.randomUUID().toString()) + .name(policy.getName()) + .description(policy.getDescription()) + .actor(policy.getActor()) + .target(policy.getTarget()) + .recipe(policy.getRecipe()) + .payload(null) + .retries(policy.getRetry()) + .timeout(policy.getTimeout()).build()); } } } |