diff options
Diffstat (limited to 'controlloop')
6 files changed, 307 insertions, 67 deletions
diff --git a/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/policy/Policy.java b/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/policy/Policy.java index 06dc36e69..b7cb78711 100644 --- a/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/policy/Policy.java +++ b/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/policy/Policy.java @@ -84,8 +84,8 @@ public class Policy implements Serializable { * @param retries retries * @param timeout timeout */ - public Policy(String name, String actor, String recipe, Map<String, String> payload, Target target, - Integer retries, Integer timeout) { + public Policy(String name, String actor, String recipe, Map<String, String> payload, Target target, + Integer retries, Integer timeout) { this(name, actor, recipe, payload, target); this.retry = retries; this.timeout = timeout; @@ -93,22 +93,14 @@ public class Policy implements Serializable { /** * Constructor. - * - * @param id id - * @param name name - * @param description description - * @param actor actor - * @param payload payload - * @param target target - * @param recipe recipe - * @param retries retries - * @param timeout timeout + * + * @param policyParam provide parameter object */ - public Policy(String id, String name, String description, String actor, Map<String, String> payload, - Target target, String recipe, Integer retries, Integer timeout) { - this(name, actor, recipe, payload, target, retries, timeout); - this.id = id; - this.description = description; + public Policy(PolicyParam policyParam) { + this(policyParam.getName(), policyParam.getActor(), policyParam.getRecipe(), policyParam.getPayload(), + policyParam.getTarget(), policyParam.getRetries(), policyParam.getTimeout()); + this.id = policyParam.getId(); + this.description = policyParam.getDescription(); } /** @@ -341,5 +333,4 @@ public class Policy implements Serializable { } return obj1.equals(obj2); } - } diff --git a/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/policy/PolicyParam.java b/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/policy/PolicyParam.java new file mode 100644 index 000000000..8891a3f5b --- /dev/null +++ b/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/policy/PolicyParam.java @@ -0,0 +1,132 @@ +/* + * ============LICENSE_START======================================================= + * policy-endpoints + * ================================================================================ + * Copyright (C) 2018 Samsung Electronics Co., Ltd. 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.controlloop.policy; + +import java.util.Map; + +public class PolicyParam { + private String id; + private String name; + private String description; + private String actor; + private Map<String, String> payload; + private Target target; + private String recipe; + private Integer retries; + private Integer timeout; + + public static PolicyParamBuilder builder() { + return new PolicyParamBuilder(); + } + + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public String getDescription() { + return description; + } + + public String getActor() { + return actor; + } + + public Map<String, String> getPayload() { + return payload; + } + + public Target getTarget() { + return target; + } + + public String getRecipe() { + return recipe; + } + + public Integer getRetries() { + return retries; + } + + public Integer getTimeout() { + return timeout; + } + + public static class PolicyParamBuilder { + + PolicyParam policyParm = new PolicyParam(); + + private PolicyParamBuilder() { + } + + public PolicyParam build() { + return policyParm; + } + + public PolicyParamBuilder id(String id) { + policyParm.id = id; + return this; + } + + public PolicyParamBuilder name(String name) { + policyParm.name = name; + return this; + } + + public PolicyParamBuilder description(String description) { + policyParm.description = description; + return this; + } + + public PolicyParamBuilder actor(String actor) { + policyParm.actor = actor; + return this; + } + + public PolicyParamBuilder payload(Map<String, String> payload) { + policyParm.payload = payload; + return this; + } + + public PolicyParamBuilder target(Target target) { + policyParm.target = target; + return this; + } + + public PolicyParamBuilder recipe(String recipe) { + policyParm.recipe = recipe; + return this; + } + + public PolicyParamBuilder retries(Integer retries) { + policyParm.retries = retries; + return this; + } + + public PolicyParamBuilder timeout(Integer timeout) { + policyParm.timeout = timeout; + return this; + } + } +}
\ No newline at end of file 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 ba434356a..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 @@ -21,11 +21,9 @@ package org.onap.policy.controlloop.policy.builder.impl; import com.google.common.base.Strings; - import java.util.LinkedList; import java.util.Map; import java.util.UUID; - import org.onap.policy.aai.Pnf; import org.onap.policy.controlloop.compiler.CompilerException; import org.onap.policy.controlloop.compiler.ControlLoopCompiler; @@ -35,6 +33,7 @@ import org.onap.policy.controlloop.policy.ControlLoopPolicy; import org.onap.policy.controlloop.policy.FinalResult; 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.BuilderException; @@ -200,11 +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) throws BuilderException { + public Policy setTriggerPolicy(PolicyParam policyParam) + throws BuilderException { - Policy trigger = new Policy(UUID.randomUUID().toString(), name, description, actor, payload, target, recipe, - retries, timeout); + Policy trigger = new Policy(policyParam); controlLoopPolicy.getControlLoop().setTrigger_policy(trigger.getId()); @@ -216,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"); } @@ -231,7 +229,8 @@ public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder { @Override public Policy setPolicyForPolicyResult(String name, String description, String actor, Target target, String recipe, - Map<String, String> payload, Integer retries, Integer timeout, String policyId, PolicyResult... results) + Map<String, String> payload, Integer retries, Integer timeout, + String policyId, PolicyResult... results) throws BuilderException { // // Find the existing policy @@ -243,8 +242,17 @@ public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder { // // Create the new Policy // - Policy newPolicy = new Policy(UUID.randomUUID().toString(), name, description, actor, payload, target, recipe, - retries, timeout); + Policy newPolicy = 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()); // // Connect the results // 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()); } } } diff --git a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/PolicyTest.java b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/PolicyTest.java index 6e13b326b..d903b1534 100644 --- a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/PolicyTest.java +++ b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/PolicyTest.java @@ -161,7 +161,17 @@ public class PolicyTest { Target target = new Target(); target.setResourceID("myresourceD"); - policy = new Policy("idD", "nameD", "descD", "actorD", payload, target, "recipeD", 301, 302); + policy = new Policy( + PolicyParam.builder().id("idD") + .name("nameD") + .description("descD") + .actor("actorD") + .payload(payload) + .target(target) + .recipe("recipeD") + .retries(301) + .timeout(302) + .build()); assertEquals("idD", policy.getId()); assertEquals("nameD", policy.getName()); assertEquals("descD", policy.getDescription()); |