diff options
Diffstat (limited to 'controlloop/common/policy-yaml/src')
5 files changed, 917 insertions, 995 deletions
diff --git a/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/policy/ControlLoop.java b/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/policy/ControlLoop.java index ad6a58b74..fc835dd51 100644 --- a/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/policy/ControlLoop.java +++ b/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/policy/ControlLoop.java @@ -23,31 +23,31 @@ package org.onap.policy.controlloop.policy; import java.util.LinkedList; import java.util.List; -import org.onap.policy.aai.PNF; +import org.onap.policy.aai.Pnf; import org.onap.policy.sdc.Resource; import org.onap.policy.sdc.Service; public class ControlLoop { - + private static final String COMPILER_VERSION = "2.0.0"; private String controlLoopName; private String version = COMPILER_VERSION; private List<Service> services; private List<Resource> resources; - private PNF pnf; + private Pnf pnf; private String triggerPolicy = FinalResult.FINAL_OPENLOOP.toString(); private Integer timeout; private Boolean abatement = false; - + public ControlLoop() { // Empty Constructor. } - - public static String getVERSION(){ + + public static String getVERSION() { return ControlLoop.COMPILER_VERSION; } - + public String getControlLoopName() { return controlLoopName; } @@ -99,16 +99,16 @@ public class ControlLoop { public String getVersion() { return version; } - - public void setVersion(String version){ + + public void setVersion(String version) { this.version = version; } - public PNF getPnf() { + public Pnf getPnf() { return pnf; } - public void setPnf(PNF pnf) { + public void setPnf(Pnf pnf) { this.pnf = pnf; } @@ -122,7 +122,7 @@ public class ControlLoop { } this.resources = new LinkedList<>(); if (controlLoop.resources != null) { - for (Resource resource: controlLoop.resources) { + for (Resource resource : controlLoop.resources) { this.resources.add(resource); } } @@ -130,12 +130,14 @@ public class ControlLoop { this.timeout = controlLoop.timeout; this.abatement = controlLoop.abatement; } + @Override public String toString() { return "ControlLoop [controlLoopName=" + controlLoopName + ", version=" + version + ", services=" + services - + ", resources=" + resources + ", trigger_policy=" + triggerPolicy + ", timeout=" - + timeout + ", abatement=" + abatement + "]"; + + ", resources=" + resources + ", trigger_policy=" + triggerPolicy + ", timeout=" + timeout + + ", abatement=" + abatement + "]"; } + @Override public int hashCode() { final int prime = 31; @@ -149,29 +151,30 @@ public class ControlLoop { result = prime * result + ((abatement == null) ? 0 : abatement.hashCode()); return result; } + @Override public boolean equals(Object obj) { - if (this == obj) + if (this == obj) { return true; - if (obj == null) + } + if (obj == null) { return false; - if (getClass() != obj.getClass()) + } + if (getClass() != obj.getClass()) { return false; + } ControlLoop other = (ControlLoop) obj; - return equalsMayBeNull(controlLoopName, other.controlLoopName) - && equalsMayBeNull(resources, other.resources) - && equalsMayBeNull(services, other.services) - && equalsMayBeNull(timeout, other.timeout) - && equalsMayBeNull(triggerPolicy, other.triggerPolicy) - && equalsMayBeNull(version, other.version) - && equalsMayBeNull(abatement, other.abatement); - } - - private boolean equalsMayBeNull(final Object obj1, final Object obj2){ - if ( obj1 == null ) { + return equalsMayBeNull(controlLoopName, other.controlLoopName) && equalsMayBeNull(resources, other.resources) + && equalsMayBeNull(services, other.services) && equalsMayBeNull(timeout, other.timeout) + && equalsMayBeNull(triggerPolicy, other.triggerPolicy) && equalsMayBeNull(version, other.version) + && equalsMayBeNull(abatement, other.abatement); + } + + private boolean equalsMayBeNull(final Object obj1, final Object obj2) { + if (obj1 == null) { return obj2 == null; } - return obj1.equals(obj2); + return obj1.equals(obj2); } - + } 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 d1212194d..4e034cb0e 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 @@ -22,7 +22,7 @@ package org.onap.policy.controlloop.policy.builder; import java.util.Map; -import org.onap.policy.aai.PNF; +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; @@ -33,7 +33,7 @@ import org.onap.policy.sdc.Resource; import org.onap.policy.sdc.Service; public interface ControlLoopPolicyBuilder { - + /** * Adds one or more services to the ControlLoop * @@ -43,20 +43,20 @@ public interface ControlLoopPolicyBuilder { * @throws BuilderException */ public ControlLoopPolicyBuilder addService(Service... services) throws BuilderException; - + /** * @param services * @return * @throws BuilderException */ public ControlLoopPolicyBuilder removeService(Service... services) throws BuilderException; - + /** * @return * @throws BuilderException */ public ControlLoopPolicyBuilder removeAllServices() throws BuilderException; - + /** * Adds one or more resources to the ControlLoop * @@ -66,51 +66,51 @@ public interface ControlLoopPolicyBuilder { * @throws BuilderException */ public ControlLoopPolicyBuilder addResource(Resource... resources) throws BuilderException; - + /** * @param resources * @return * @throws BuilderException */ public ControlLoopPolicyBuilder removeResource(Resource... resources) throws BuilderException; - + /** * @return * @throws BuilderException */ public ControlLoopPolicyBuilder removeAllResources() throws BuilderException; - + /** * @param pnf * @return * @throws BuilderException */ - public ControlLoopPolicyBuilder setPNF(PNF pnf) throws BuilderException; - + public ControlLoopPolicyBuilder setPNF(Pnf pnf) throws BuilderException; + /** * @return * @throws BuilderException */ public ControlLoopPolicyBuilder removePNF() throws BuilderException; - + /** - * @param abatement - * @return - * @throws BuilderException + * @param abatement + * @return + * @throws BuilderException */ public ControlLoopPolicyBuilder setAbatement(Boolean abatement) throws BuilderException; - - + + /** - * Sets the overall timeout value for the Control Loop. If any operational policies have retries and timeouts, - * then this overall timeout value should exceed all those values. + * Sets the overall timeout value for the Control Loop. If any operational policies have retries + * and timeouts, then this overall timeout value should exceed all those values. * * @param timeout * @return * @throws BuilderException */ public ControlLoopPolicyBuilder setTimeout(Integer timeout) throws BuilderException; - + /** * Scans the operational policies and calculate an minimum overall timeout for the Control Loop. * @@ -118,9 +118,10 @@ public interface ControlLoopPolicyBuilder { * @return Integer */ public Integer calculateTimeout(); - + /** - * Sets the initial trigger policy when a DCAE Closed Loop Event arrives in the ONAP Policy Platform. + * Sets the initial trigger policy when a DCAE Closed Loop Event arrives in the ONAP Policy + * Platform. * * * @param name @@ -133,8 +134,9 @@ public interface ControlLoopPolicyBuilder { * @return Policy * @throws BuilderException */ - 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(String name, String description, String actor, Target target, String recipe, + Map<String, String> payload, Integer retries, Integer timeout) throws BuilderException; + /** * * Changes the trigger policy to point to another existing Policy. @@ -144,27 +146,27 @@ public interface ControlLoopPolicyBuilder { * @return ControlLoop * @throws BuilderException */ - public ControlLoop setTriggerPolicy(String id) throws BuilderException; - + public ControlLoop setTriggerPolicy(String id) throws BuilderException; + /** * @return */ - public boolean isOpenLoop(); - + public boolean isOpenLoop(); + /** * @return * @throws BuilderException */ - public Policy getTriggerPolicy() throws BuilderException; - + public Policy getTriggerPolicy() throws BuilderException; + /** * Simply returns a copy of the ControlLoop information. * * * @return ControlLoop */ - public ControlLoop getControlLoop(); - + public ControlLoop getControlLoop(); + /** * Creates a policy that is chained to the result of another Policy. * @@ -181,10 +183,11 @@ public interface ControlLoopPolicyBuilder { * @return * @throws BuilderException */ - 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) throws BuilderException; - - + 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) + throws BuilderException; + + /** * Sets the policy result(s) to an existing Operational Policy. * @@ -195,11 +198,13 @@ public interface ControlLoopPolicyBuilder { * @return * @throws BuilderException */ - public Policy setPolicyForPolicyResult(String policyResultID, String policyID, PolicyResult... results) throws BuilderException; - + public Policy setPolicyForPolicyResult(String policyResultID, String policyID, PolicyResult... results) + throws BuilderException; + /** - * Removes an Operational Policy. Be mindful that if any other Operational Policies have results that point to this policy, any - * policies that have results pointing to this policy will have their result reset to the appropriate default FINAL_* result. + * Removes an Operational Policy. Be mindful that if any other Operational Policies have results + * that point to this policy, any policies that have results pointing to this policy will have + * their result reset to the appropriate default FINAL_* result. * * * @param policyID @@ -207,7 +212,7 @@ public interface ControlLoopPolicyBuilder { * @throws BuilderException */ public boolean removePolicy(String policyID) throws BuilderException; - + /** * Resets a policy's results to defualt FINAL_* codes. * @@ -215,32 +220,33 @@ public interface ControlLoopPolicyBuilder { * @return Policy * @throws BuilderException - Policy does not exist */ - public Policy resetPolicyResults(String policyID) throws BuilderException; - + public Policy resetPolicyResults(String policyID) throws BuilderException; + /** * Removes all existing Operational Policies and reverts back to an Open Loop. * * @return */ public ControlLoopPolicyBuilder removeAllPolicies(); - + /** * Adds an operationsAccumulateParams to an existing operational policy * * @return Policy * @throws BuilderException - Policy does not exist */ - public Policy addOperationsAccumulateParams(String policyID, OperationsAccumulateParams operationsAccumulateParams) throws BuilderException; - + public Policy addOperationsAccumulateParams(String policyID, OperationsAccumulateParams operationsAccumulateParams) + throws BuilderException; + /** - * This will compile and build the YAML specification for the Control Loop Policy. Please iterate the Results object for details. - * The Results object will contains warnings and errors. If the specification compiled successfully, you will be able to retrieve the - * YAML. + * This will compile and build the YAML specification for the Control Loop Policy. Please + * iterate the Results object for details. The Results object will contains warnings and errors. + * If the specification compiled successfully, you will be able to retrieve the YAML. * * @return Results */ - public Results buildSpecification(); - + public Results buildSpecification(); + /** * The Factory is used to build a ControlLoopPolicyBuilder implementation. * @@ -248,36 +254,39 @@ public interface ControlLoopPolicyBuilder { * */ public static class Factory { - private Factory(){ + private Factory() { // Private Constructor. } - + /** - * Builds a basic Control Loop with an overall timeout. Use this method if you wish to create an OpenLoop, or if you - * want to interactively build a Closed Loop. + * Builds a basic Control Loop with an overall timeout. Use this method if you wish to + * create an OpenLoop, or if you want to interactively build a Closed Loop. * * @param controlLoopName - Per Closed Loop AID v1.0, unique string for the closed loop. * @param timeout - Overall timeout for the Closed Loop to execute. * @return ControlLoopPolicyBuilder object */ - public static ControlLoopPolicyBuilder buildControlLoop (String controlLoopName, Integer timeout) { + public static ControlLoopPolicyBuilder buildControlLoop(String controlLoopName, Integer timeout) { return new ControlLoopPolicyBuilderImpl(controlLoopName, timeout); } - + /** * Build a Control Loop for a resource and services associated with the resource. * * @param controlLoopName - Per Closed Loop AID v1.0, unique string for the closed loop. * @param timeout - Overall timeout for the Closed Loop to execute. - * @param resource - Resource this closed loop is for. Should come from ASDC, but if not available use resourceName to distinguish. - * @param services - Zero or more services associated with this resource. Should come from ASDC, but if not available use serviceName to distinguish. + * @param resource - Resource this closed loop is for. Should come from ASDC, but if not + * available use resourceName to distinguish. + * @param services - Zero or more services associated with this resource. Should come from + * ASDC, but if not available use serviceName to distinguish. * @return ControlLoopPolicyBuilder object * @throws BuilderException */ - public static ControlLoopPolicyBuilder buildControlLoop (String controlLoopName, Integer timeout, Resource resource, Service... services) throws BuilderException { - return new ControlLoopPolicyBuilderImpl(controlLoopName, timeout, resource, services); + public static ControlLoopPolicyBuilder buildControlLoop(String controlLoopName, Integer timeout, + Resource resource, Service... services) throws BuilderException { + return new ControlLoopPolicyBuilderImpl(controlLoopName, timeout, resource, services); } - + /** * @param controlLoopName * @param timeout @@ -286,18 +295,21 @@ public interface ControlLoopPolicyBuilder { * @return * @throws BuilderException */ - public static ControlLoopPolicyBuilder buildControlLoop (String controlLoopName, Integer timeout, Service service, Resource... resources) throws BuilderException { - return new ControlLoopPolicyBuilderImpl(controlLoopName, timeout, service, resources); + public static ControlLoopPolicyBuilder buildControlLoop(String controlLoopName, Integer timeout, + Service service, Resource... resources) throws BuilderException { + return new ControlLoopPolicyBuilderImpl(controlLoopName, timeout, service, resources); } - + /** * @param controlLoopName - Per Closed Loop AID v1.0, unique string for the closed loop. * @param timeout - Overall timeout for the Closed Loop to execute. - * @param pnf - Physical Network Function. Should come from AIC, but if not available use well-known name to distinguish. Eg. eNodeB + * @param pnf - Physical Network Function. Should come from AIC, but if not available use + * well-known name to distinguish. Eg. eNodeB * @return ControlLoopPolicyBuilder object * @throws BuilderException */ - public static ControlLoopPolicyBuilder buildControlLoop (String controlLoopName, Integer timeout, PNF pnf) throws BuilderException { + public static ControlLoopPolicyBuilder buildControlLoop(String controlLoopName, Integer timeout, Pnf pnf) + throws BuilderException { return new ControlLoopPolicyBuilderImpl(controlLoopName, timeout, pnf); } } 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 45315b077..adbf12748 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 @@ -20,11 +20,13 @@ 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.aai.Pnf; import org.onap.policy.controlloop.compiler.CompilerException; import org.onap.policy.controlloop.compiler.ControlLoopCompiler; import org.onap.policy.controlloop.compiler.ControlLoopCompilerCallback; @@ -47,464 +49,466 @@ import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions.FlowStyle; import org.yaml.snakeyaml.Yaml; -import com.google.common.base.Strings; - public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder { - private static final String UNKNOWN_POLICY = "Unknown policy "; - private static Logger logger = LoggerFactory.getLogger(ControlLoopPolicyBuilderImpl.class.getName()); - private ControlLoopPolicy controlLoopPolicy; - - public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout) { - controlLoopPolicy = new ControlLoopPolicy(); - ControlLoop controlLoop = new ControlLoop(); - controlLoop.setControlLoopName(controlLoopName); - controlLoop.setTimeout(timeout); - controlLoopPolicy.setControlLoop(controlLoop); - } - - public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Resource resource, Service... services) throws BuilderException { - this(controlLoopName, timeout); - this.addResource(resource); - this.addService(services); - } - - public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, PNF pnf) throws BuilderException { - this(controlLoopName, timeout); - this.setPNF(pnf); - } - - public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Service service, Resource[] resources) throws BuilderException { - this(controlLoopName, timeout); - this.addService(service); - this.addResource(resources); - } - - @Override - public ControlLoopPolicyBuilder removePNF() throws BuilderException { - controlLoopPolicy.getControlLoop().setPnf(null); - return this; - } - - @Override - public ControlLoopPolicyBuilder addService(Service... services) throws BuilderException { - for (Service service : services) { - if (service == null) { - throw new BuilderException("Service must not be null"); - } - if (service.getServiceUUID() == null && Strings.isNullOrEmpty(service.getServiceName())) { - throw new BuilderException("Invalid service - need either a serviceUUID or serviceName"); - } - if(controlLoopPolicy.getControlLoop().getServices()==null){ - controlLoopPolicy.getControlLoop().setServices(new LinkedList<>()); - } - controlLoopPolicy.getControlLoop().getServices().add(service); - } - return this; - } - - @Override - public ControlLoopPolicyBuilder removeService(Service... services) throws BuilderException { - if (controlLoopPolicy.getControlLoop().getServices() == null) { - throw new BuilderException("No existing services to remove"); - } - for (Service service : services) { - if (service == null) { - throw new BuilderException("Service must not be null"); - } - if (service.getServiceUUID() == null && Strings.isNullOrEmpty(service.getServiceName())) { - throw new BuilderException("Invalid service - need either a serviceUUID or serviceName"); - } - boolean removed = controlLoopPolicy.getControlLoop().getServices().remove(service); - if (!removed) { - throw new BuilderException("Unknown service " + service.getServiceName()); - } - } - return this; - } - - @Override - public ControlLoopPolicyBuilder removeAllServices() throws BuilderException { - controlLoopPolicy.getControlLoop().getServices().clear(); - return this; - } - - - @Override - public ControlLoopPolicyBuilder addResource(Resource... resources) throws BuilderException { - for (Resource resource : resources) { - if (resource == null) { - throw new BuilderException("Resource must not be null"); - } - if (resource.getResourceUUID() == null && Strings.isNullOrEmpty(resource.getResourceName())) { - throw new BuilderException("Invalid resource - need either resourceUUID or resourceName"); - } - if(controlLoopPolicy.getControlLoop().getResources()==null){ - controlLoopPolicy.getControlLoop().setResources(new LinkedList<>()); - } - controlLoopPolicy.getControlLoop().getResources().add(resource); - } - return this; - } - - @Override - public ControlLoopPolicyBuilder setPNF(PNF pnf) throws BuilderException { - if (pnf == null) { - throw new BuilderException("PNF must not be null"); - } - if (pnf.getPNFName() == null && pnf.getPNFType() == null) { - throw new BuilderException("Invalid PNF - need either pnfName or pnfType"); - } - controlLoopPolicy.getControlLoop().setPnf(pnf); - return this; - } - - @Override - public ControlLoopPolicyBuilder setAbatement(Boolean abatement) throws BuilderException{ - if (abatement == null) { - throw new BuilderException("abatement must not be null"); - } - controlLoopPolicy.getControlLoop().setAbatement(abatement); - return this; - } - - @Override - public ControlLoopPolicyBuilder setTimeout(Integer timeout) { - controlLoopPolicy.getControlLoop().setTimeout(timeout); - return this; - } - - @Override - public Policy setTriggerPolicy(String name, String description, String actor, Target target, String recipe, - Map<String, String> payload, Integer retries, Integer timeout) throws BuilderException { - - Policy trigger = new Policy(UUID.randomUUID().toString(), name, description, actor, payload, target, recipe, retries, timeout); - - controlLoopPolicy.getControlLoop().setTrigger_policy(trigger.getId()); - - this.addNewPolicy(trigger); - // - // Return a copy of the policy - // - return new Policy(trigger); - } - - @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) throws BuilderException { - // - // Find the existing policy - // - Policy existingPolicy = this.findPolicy(policyID); - if (existingPolicy == null) { - throw new BuilderException(UNKNOWN_POLICY + policyID); - } - // - // Create the new Policy - // - Policy newPolicy = new Policy(UUID.randomUUID().toString(), name, description, actor, payload, target, recipe, retries, timeout); - // - // Connect the results - // - for (PolicyResult result : results) { - switch (result) { - case FAILURE: - existingPolicy.setFailure(newPolicy.getId()); - break; - case FAILURE_EXCEPTION: - existingPolicy.setFailure_exception(newPolicy.getId()); - break; - case FAILURE_RETRIES: - existingPolicy.setFailure_retries(newPolicy.getId()); - break; - case FAILURE_TIMEOUT: - existingPolicy.setFailure_timeout(newPolicy.getId()); - break; - case FAILURE_GUARD: - existingPolicy.setFailure_guard(newPolicy.getId()); - break; - case SUCCESS: - existingPolicy.setSuccess(newPolicy.getId()); - break; - default: - throw new BuilderException("Invalid PolicyResult " + result); - } - } - // - // Add it to our list - // - this.controlLoopPolicy.getPolicies().add(newPolicy); - // - // Return a policy to them - // - return new Policy(newPolicy); - } - - private class BuilderCompilerCallback implements ControlLoopCompilerCallback { - - private ResultsImpl results = new ResultsImpl(); - - @Override - public boolean onWarning(String message) { - results.addMessage(new MessageImpl(message, MessageLevel.WARNING)); - return false; - } - - @Override - public boolean onError(String message) { - results.addMessage(new MessageImpl(message, MessageLevel.ERROR)); - return false; - } - } - - @Override - public Results buildSpecification() { - // - // Dump the specification - // - DumperOptions options = new DumperOptions(); - options.setDefaultFlowStyle(FlowStyle.BLOCK); - options.setPrettyFlow(true); - Yaml yaml = new Yaml(options); - String dumpedYaml = yaml.dump(controlLoopPolicy); - // - // This is our callback class for our compiler - // - BuilderCompilerCallback callback = new BuilderCompilerCallback(); - // - // Compile it - // - try { - ControlLoopCompiler.compile(controlLoopPolicy, callback); - } catch (CompilerException e) { - logger.error(e.getMessage() + e); - callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION)); - } - // - // Save the spec - // - callback.results.setSpecification(dumpedYaml); - return callback.results; - } - - private void addNewPolicy(Policy policy) { - if (this.controlLoopPolicy.getPolicies() == null) { - this.controlLoopPolicy.setPolicies(new LinkedList<>()); - } - this.controlLoopPolicy.getPolicies().add(policy); - } - - private Policy findPolicy(String id) { - if (this.controlLoopPolicy.getPolicies() != null){ - for (Policy policy : this.controlLoopPolicy.getPolicies()) { - if (policy.getId().equals(id)) { - return policy; - } - } - } - return null; - } - - @Override - public ControlLoopPolicyBuilder removeResource(Resource... resources) throws BuilderException { - if (controlLoopPolicy.getControlLoop().getResources() == null) { - throw new BuilderException("No existing resources to remove"); - } - for (Resource resource : resources) { - if (resource == null) { - throw new BuilderException("Resource must not be null"); - } - if (resource.getResourceUUID() == null && Strings.isNullOrEmpty(resource.getResourceName())) { - throw new BuilderException("Invalid resource - need either a resourceUUID or resourceName"); - } - boolean removed = controlLoopPolicy.getControlLoop().getResources().remove(resource); - if (!removed) { - throw new BuilderException("Unknown resource " + resource.getResourceName()); - } - } - return this; - } - - @Override - public ControlLoopPolicyBuilder removeAllResources() throws BuilderException { - controlLoopPolicy.getControlLoop().getResources().clear(); - return this; - } - - @Override - public Integer calculateTimeout() { - int sum = 0; - for (Policy policy : this.controlLoopPolicy.getPolicies()) { - sum += policy.getTimeout().intValue(); - } - return Integer.valueOf(sum); - } - - @Override - public ControlLoop setTriggerPolicy(String id) throws BuilderException { - if (id == null) { - throw new BuilderException("Id must not be null"); - } - Policy trigger = this.findPolicy(id); - if (trigger == null) { - throw new BuilderException(UNKNOWN_POLICY + id); - } - else { - this.controlLoopPolicy.getControlLoop().setTrigger_policy(id); - } - return new ControlLoop(this.controlLoopPolicy.getControlLoop()); - } - - @Override - public boolean isOpenLoop() { - return this.controlLoopPolicy.getControlLoop().getTrigger_policy().equals(FinalResult.FINAL_OPENLOOP.toString()); - } - - @Override - public Policy getTriggerPolicy() throws BuilderException { - if (this.controlLoopPolicy.getControlLoop().getTrigger_policy().equals(FinalResult.FINAL_OPENLOOP.toString())) { - return null; - } - else { - return new Policy(this.findPolicy(this.controlLoopPolicy.getControlLoop().getTrigger_policy())); - } - } - - @Override - public ControlLoop getControlLoop() { - return new ControlLoop(this.controlLoopPolicy.getControlLoop()); - } - - @Override - public Policy setPolicyForPolicyResult(String policyResultID, String policyID, PolicyResult... results) - throws BuilderException { - // - // Find the existing policy - // - Policy existingPolicy = this.findPolicy(policyID); - if (existingPolicy == null) { - throw new BuilderException(policyID + " does not exist"); - } - if (this.findPolicy(policyResultID) == null) { - throw new BuilderException("Operational policy " + policyResultID + " does not exist"); - } - // - // Connect the results - // - for (PolicyResult result : results) { - switch (result) { - case FAILURE: - existingPolicy.setFailure(policyResultID); - break; - case FAILURE_EXCEPTION: - existingPolicy.setFailure_exception(policyResultID); - break; - case FAILURE_RETRIES: - existingPolicy.setFailure_retries(policyResultID); - break; - case FAILURE_TIMEOUT: - existingPolicy.setFailure_timeout(policyResultID); - break; - case FAILURE_GUARD: - existingPolicy.setFailure_guard(policyResultID); - break; - case SUCCESS: - existingPolicy.setSuccess(policyResultID); - break; - default: - throw new BuilderException("Invalid PolicyResult " + result); - } - } - return new Policy(this.findPolicy(policyResultID)); - } - - @Override - public boolean removePolicy(String policyID) throws BuilderException { - Policy existingPolicy = this.findPolicy(policyID); - if (existingPolicy == null) { - throw new BuilderException(UNKNOWN_POLICY + policyID); - } - // - // Check if the policy to remove is trigger_policy - // - if (this.controlLoopPolicy.getControlLoop().getTrigger_policy().equals(policyID)) { - this.controlLoopPolicy.getControlLoop().setTrigger_policy(FinalResult.FINAL_OPENLOOP.toString()); - } - else { - updateChainedPoliciesForPolicyRemoval(policyID); - } - // - // remove the policy - // - return this.controlLoopPolicy.getPolicies().remove(existingPolicy); - } - - private void updateChainedPoliciesForPolicyRemoval(String idOfPolicyBeingRemoved){ - for (Policy policy : this.controlLoopPolicy.getPolicies()) { - int index = this.controlLoopPolicy.getPolicies().indexOf(policy); - if (policy.getSuccess().equals(idOfPolicyBeingRemoved)) { - policy.setSuccess(FinalResult.FINAL_SUCCESS.toString()); - } - if (policy.getFailure().equals(idOfPolicyBeingRemoved)) { - policy.setFailure(FinalResult.FINAL_FAILURE.toString()); - } - if (policy.getFailure_retries().equals(idOfPolicyBeingRemoved)) { - policy.setFailure_retries(FinalResult.FINAL_FAILURE_RETRIES.toString()); - } - if (policy.getFailure_timeout().equals(idOfPolicyBeingRemoved)) { - policy.setFailure_timeout(FinalResult.FINAL_FAILURE_TIMEOUT.toString()); - } - if (policy.getFailure_exception().equals(idOfPolicyBeingRemoved)) { - policy.setFailure_exception(FinalResult.FINAL_FAILURE_EXCEPTION.toString()); - } - if (policy.getFailure_guard().equals(idOfPolicyBeingRemoved)) { - policy.setFailure_guard(FinalResult.FINAL_FAILURE_GUARD.toString()); - } - this.controlLoopPolicy.getPolicies().set(index, policy); - } - } - - @Override - public Policy resetPolicyResults(String policyID) throws BuilderException { - Policy existingPolicy = this.findPolicy(policyID); - if (existingPolicy == null) { - throw new BuilderException(UNKNOWN_POLICY + policyID); - } - // - // reset policy results - // - existingPolicy.setSuccess(FinalResult.FINAL_SUCCESS.toString()); - existingPolicy.setFailure(FinalResult.FINAL_FAILURE.toString()); - existingPolicy.setFailure_retries(FinalResult.FINAL_FAILURE_RETRIES.toString()); - existingPolicy.setFailure_timeout(FinalResult.FINAL_FAILURE_TIMEOUT.toString()); - existingPolicy.setFailure_exception(FinalResult.FINAL_FAILURE_EXCEPTION.toString()); - existingPolicy.setFailure_guard(FinalResult.FINAL_FAILURE_GUARD.toString()); - return new Policy(existingPolicy); - } - - @Override - public ControlLoopPolicyBuilder removeAllPolicies() { - // - // Remove all existing operational policies - // - this.controlLoopPolicy.getPolicies().clear(); - // - // Revert controlLoop back to an open loop - // - this.controlLoopPolicy.getControlLoop().setTrigger_policy(FinalResult.FINAL_OPENLOOP.toString()); - return this; - } - - @Override - public Policy addOperationsAccumulateParams(String policyID, OperationsAccumulateParams operationsAccumulateParams) throws BuilderException { - Policy existingPolicy = this.findPolicy(policyID); - if (existingPolicy == null) { - throw new BuilderException(UNKNOWN_POLICY + policyID); - } - // - // Add operationsAccumulateParams to existingPolicy - // - existingPolicy.setOperationsAccumulateParams(operationsAccumulateParams); - return new Policy(existingPolicy); - } + private static final String UNKNOWN_POLICY = "Unknown policy "; + private static Logger logger = LoggerFactory.getLogger(ControlLoopPolicyBuilderImpl.class.getName()); + private ControlLoopPolicy controlLoopPolicy; + + public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout) { + controlLoopPolicy = new ControlLoopPolicy(); + ControlLoop controlLoop = new ControlLoop(); + controlLoop.setControlLoopName(controlLoopName); + controlLoop.setTimeout(timeout); + controlLoopPolicy.setControlLoop(controlLoop); + } + + public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Resource resource, Service... services) + throws BuilderException { + this(controlLoopName, timeout); + this.addResource(resource); + this.addService(services); + } + + public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Pnf pnf) throws BuilderException { + this(controlLoopName, timeout); + this.setPNF(pnf); + } + + public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Service service, Resource[] resources) + throws BuilderException { + this(controlLoopName, timeout); + this.addService(service); + this.addResource(resources); + } + + @Override + public ControlLoopPolicyBuilder removePNF() throws BuilderException { + controlLoopPolicy.getControlLoop().setPnf(null); + return this; + } + + @Override + public ControlLoopPolicyBuilder addService(Service... services) throws BuilderException { + for (Service service : services) { + if (service == null) { + throw new BuilderException("Service must not be null"); + } + if (service.getServiceUUID() == null && Strings.isNullOrEmpty(service.getServiceName())) { + throw new BuilderException("Invalid service - need either a serviceUUID or serviceName"); + } + if (controlLoopPolicy.getControlLoop().getServices() == null) { + controlLoopPolicy.getControlLoop().setServices(new LinkedList<>()); + } + controlLoopPolicy.getControlLoop().getServices().add(service); + } + return this; + } + + @Override + public ControlLoopPolicyBuilder removeService(Service... services) throws BuilderException { + if (controlLoopPolicy.getControlLoop().getServices() == null) { + throw new BuilderException("No existing services to remove"); + } + for (Service service : services) { + if (service == null) { + throw new BuilderException("Service must not be null"); + } + if (service.getServiceUUID() == null && Strings.isNullOrEmpty(service.getServiceName())) { + throw new BuilderException("Invalid service - need either a serviceUUID or serviceName"); + } + boolean removed = controlLoopPolicy.getControlLoop().getServices().remove(service); + if (!removed) { + throw new BuilderException("Unknown service " + service.getServiceName()); + } + } + return this; + } + + @Override + public ControlLoopPolicyBuilder removeAllServices() throws BuilderException { + controlLoopPolicy.getControlLoop().getServices().clear(); + return this; + } + + + @Override + public ControlLoopPolicyBuilder addResource(Resource... resources) throws BuilderException { + for (Resource resource : resources) { + if (resource == null) { + throw new BuilderException("Resource must not be null"); + } + if (resource.getResourceUUID() == null && Strings.isNullOrEmpty(resource.getResourceName())) { + throw new BuilderException("Invalid resource - need either resourceUUID or resourceName"); + } + if (controlLoopPolicy.getControlLoop().getResources() == null) { + controlLoopPolicy.getControlLoop().setResources(new LinkedList<>()); + } + controlLoopPolicy.getControlLoop().getResources().add(resource); + } + return this; + } + + @Override + public ControlLoopPolicyBuilder setPNF(Pnf pnf) throws BuilderException { + if (pnf == null) { + throw new BuilderException("PNF must not be null"); + } + if (pnf.getPnfName() == null && pnf.getPnfType() == null) { + throw new BuilderException("Invalid PNF - need either pnfName or pnfType"); + } + controlLoopPolicy.getControlLoop().setPnf(pnf); + return this; + } + + @Override + public ControlLoopPolicyBuilder setAbatement(Boolean abatement) throws BuilderException { + if (abatement == null) { + throw new BuilderException("abatement must not be null"); + } + controlLoopPolicy.getControlLoop().setAbatement(abatement); + return this; + } + + @Override + public ControlLoopPolicyBuilder setTimeout(Integer timeout) { + controlLoopPolicy.getControlLoop().setTimeout(timeout); + return this; + } + + @Override + public Policy setTriggerPolicy(String name, String description, String actor, Target target, String recipe, + Map<String, String> payload, Integer retries, Integer timeout) throws BuilderException { + + Policy trigger = new Policy(UUID.randomUUID().toString(), name, description, actor, payload, target, recipe, + retries, timeout); + + controlLoopPolicy.getControlLoop().setTrigger_policy(trigger.getId()); + + this.addNewPolicy(trigger); + // + // Return a copy of the policy + // + return new Policy(trigger); + } + + @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) + throws BuilderException { + // + // Find the existing policy + // + Policy existingPolicy = this.findPolicy(policyID); + if (existingPolicy == null) { + throw new BuilderException(UNKNOWN_POLICY + policyID); + } + // + // Create the new Policy + // + Policy newPolicy = new Policy(UUID.randomUUID().toString(), name, description, actor, payload, target, recipe, + retries, timeout); + // + // Connect the results + // + for (PolicyResult result : results) { + switch (result) { + case FAILURE: + existingPolicy.setFailure(newPolicy.getId()); + break; + case FAILURE_EXCEPTION: + existingPolicy.setFailure_exception(newPolicy.getId()); + break; + case FAILURE_RETRIES: + existingPolicy.setFailure_retries(newPolicy.getId()); + break; + case FAILURE_TIMEOUT: + existingPolicy.setFailure_timeout(newPolicy.getId()); + break; + case FAILURE_GUARD: + existingPolicy.setFailure_guard(newPolicy.getId()); + break; + case SUCCESS: + existingPolicy.setSuccess(newPolicy.getId()); + break; + default: + throw new BuilderException("Invalid PolicyResult " + result); + } + } + // + // Add it to our list + // + this.controlLoopPolicy.getPolicies().add(newPolicy); + // + // Return a policy to them + // + return new Policy(newPolicy); + } + + private class BuilderCompilerCallback implements ControlLoopCompilerCallback { + + private ResultsImpl results = new ResultsImpl(); + + @Override + public boolean onWarning(String message) { + results.addMessage(new MessageImpl(message, MessageLevel.WARNING)); + return false; + } + + @Override + public boolean onError(String message) { + results.addMessage(new MessageImpl(message, MessageLevel.ERROR)); + return false; + } + } + + @Override + public Results buildSpecification() { + // + // Dump the specification + // + DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(FlowStyle.BLOCK); + options.setPrettyFlow(true); + Yaml yaml = new Yaml(options); + String dumpedYaml = yaml.dump(controlLoopPolicy); + // + // This is our callback class for our compiler + // + BuilderCompilerCallback callback = new BuilderCompilerCallback(); + // + // Compile it + // + try { + ControlLoopCompiler.compile(controlLoopPolicy, callback); + } catch (CompilerException e) { + logger.error(e.getMessage() + e); + callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION)); + } + // + // Save the spec + // + callback.results.setSpecification(dumpedYaml); + return callback.results; + } + + private void addNewPolicy(Policy policy) { + if (this.controlLoopPolicy.getPolicies() == null) { + this.controlLoopPolicy.setPolicies(new LinkedList<>()); + } + this.controlLoopPolicy.getPolicies().add(policy); + } + + private Policy findPolicy(String id) { + if (this.controlLoopPolicy.getPolicies() != null) { + for (Policy policy : this.controlLoopPolicy.getPolicies()) { + if (policy.getId().equals(id)) { + return policy; + } + } + } + return null; + } + + @Override + public ControlLoopPolicyBuilder removeResource(Resource... resources) throws BuilderException { + if (controlLoopPolicy.getControlLoop().getResources() == null) { + throw new BuilderException("No existing resources to remove"); + } + for (Resource resource : resources) { + if (resource == null) { + throw new BuilderException("Resource must not be null"); + } + if (resource.getResourceUUID() == null && Strings.isNullOrEmpty(resource.getResourceName())) { + throw new BuilderException("Invalid resource - need either a resourceUUID or resourceName"); + } + boolean removed = controlLoopPolicy.getControlLoop().getResources().remove(resource); + if (!removed) { + throw new BuilderException("Unknown resource " + resource.getResourceName()); + } + } + return this; + } + + @Override + public ControlLoopPolicyBuilder removeAllResources() throws BuilderException { + controlLoopPolicy.getControlLoop().getResources().clear(); + return this; + } + + @Override + public Integer calculateTimeout() { + int sum = 0; + for (Policy policy : this.controlLoopPolicy.getPolicies()) { + sum += policy.getTimeout().intValue(); + } + return Integer.valueOf(sum); + } + + @Override + public ControlLoop setTriggerPolicy(String id) throws BuilderException { + if (id == null) { + throw new BuilderException("Id must not be null"); + } + Policy trigger = this.findPolicy(id); + if (trigger == null) { + throw new BuilderException(UNKNOWN_POLICY + id); + } else { + this.controlLoopPolicy.getControlLoop().setTrigger_policy(id); + } + return new ControlLoop(this.controlLoopPolicy.getControlLoop()); + } + + @Override + public boolean isOpenLoop() { + return this.controlLoopPolicy.getControlLoop().getTrigger_policy() + .equals(FinalResult.FINAL_OPENLOOP.toString()); + } + + @Override + public Policy getTriggerPolicy() throws BuilderException { + if (this.controlLoopPolicy.getControlLoop().getTrigger_policy().equals(FinalResult.FINAL_OPENLOOP.toString())) { + return null; + } else { + return new Policy(this.findPolicy(this.controlLoopPolicy.getControlLoop().getTrigger_policy())); + } + } + + @Override + public ControlLoop getControlLoop() { + return new ControlLoop(this.controlLoopPolicy.getControlLoop()); + } + + @Override + public Policy setPolicyForPolicyResult(String policyResultID, String policyID, PolicyResult... results) + throws BuilderException { + // + // Find the existing policy + // + Policy existingPolicy = this.findPolicy(policyID); + if (existingPolicy == null) { + throw new BuilderException(policyID + " does not exist"); + } + if (this.findPolicy(policyResultID) == null) { + throw new BuilderException("Operational policy " + policyResultID + " does not exist"); + } + // + // Connect the results + // + for (PolicyResult result : results) { + switch (result) { + case FAILURE: + existingPolicy.setFailure(policyResultID); + break; + case FAILURE_EXCEPTION: + existingPolicy.setFailure_exception(policyResultID); + break; + case FAILURE_RETRIES: + existingPolicy.setFailure_retries(policyResultID); + break; + case FAILURE_TIMEOUT: + existingPolicy.setFailure_timeout(policyResultID); + break; + case FAILURE_GUARD: + existingPolicy.setFailure_guard(policyResultID); + break; + case SUCCESS: + existingPolicy.setSuccess(policyResultID); + break; + default: + throw new BuilderException("Invalid PolicyResult " + result); + } + } + return new Policy(this.findPolicy(policyResultID)); + } + + @Override + public boolean removePolicy(String policyID) throws BuilderException { + Policy existingPolicy = this.findPolicy(policyID); + if (existingPolicy == null) { + throw new BuilderException(UNKNOWN_POLICY + policyID); + } + // + // Check if the policy to remove is trigger_policy + // + if (this.controlLoopPolicy.getControlLoop().getTrigger_policy().equals(policyID)) { + this.controlLoopPolicy.getControlLoop().setTrigger_policy(FinalResult.FINAL_OPENLOOP.toString()); + } else { + updateChainedPoliciesForPolicyRemoval(policyID); + } + // + // remove the policy + // + return this.controlLoopPolicy.getPolicies().remove(existingPolicy); + } + + private void updateChainedPoliciesForPolicyRemoval(String idOfPolicyBeingRemoved) { + for (Policy policy : this.controlLoopPolicy.getPolicies()) { + int index = this.controlLoopPolicy.getPolicies().indexOf(policy); + if (policy.getSuccess().equals(idOfPolicyBeingRemoved)) { + policy.setSuccess(FinalResult.FINAL_SUCCESS.toString()); + } + if (policy.getFailure().equals(idOfPolicyBeingRemoved)) { + policy.setFailure(FinalResult.FINAL_FAILURE.toString()); + } + if (policy.getFailure_retries().equals(idOfPolicyBeingRemoved)) { + policy.setFailure_retries(FinalResult.FINAL_FAILURE_RETRIES.toString()); + } + if (policy.getFailure_timeout().equals(idOfPolicyBeingRemoved)) { + policy.setFailure_timeout(FinalResult.FINAL_FAILURE_TIMEOUT.toString()); + } + if (policy.getFailure_exception().equals(idOfPolicyBeingRemoved)) { + policy.setFailure_exception(FinalResult.FINAL_FAILURE_EXCEPTION.toString()); + } + if (policy.getFailure_guard().equals(idOfPolicyBeingRemoved)) { + policy.setFailure_guard(FinalResult.FINAL_FAILURE_GUARD.toString()); + } + this.controlLoopPolicy.getPolicies().set(index, policy); + } + } + + @Override + public Policy resetPolicyResults(String policyID) throws BuilderException { + Policy existingPolicy = this.findPolicy(policyID); + if (existingPolicy == null) { + throw new BuilderException(UNKNOWN_POLICY + policyID); + } + // + // reset policy results + // + existingPolicy.setSuccess(FinalResult.FINAL_SUCCESS.toString()); + existingPolicy.setFailure(FinalResult.FINAL_FAILURE.toString()); + existingPolicy.setFailure_retries(FinalResult.FINAL_FAILURE_RETRIES.toString()); + existingPolicy.setFailure_timeout(FinalResult.FINAL_FAILURE_TIMEOUT.toString()); + existingPolicy.setFailure_exception(FinalResult.FINAL_FAILURE_EXCEPTION.toString()); + existingPolicy.setFailure_guard(FinalResult.FINAL_FAILURE_GUARD.toString()); + return new Policy(existingPolicy); + } + + @Override + public ControlLoopPolicyBuilder removeAllPolicies() { + // + // Remove all existing operational policies + // + this.controlLoopPolicy.getPolicies().clear(); + // + // Revert controlLoop back to an open loop + // + this.controlLoopPolicy.getControlLoop().setTrigger_policy(FinalResult.FINAL_OPENLOOP.toString()); + return this; + } + + @Override + public Policy addOperationsAccumulateParams(String policyID, OperationsAccumulateParams operationsAccumulateParams) + throws BuilderException { + Policy existingPolicy = this.findPolicy(policyID); + if (existingPolicy == null) { + throw new BuilderException(UNKNOWN_POLICY + policyID); + } + // + // Add operationsAccumulateParams to existingPolicy + // + existingPolicy.setOperationsAccumulateParams(operationsAccumulateParams); + return new Policy(existingPolicy); + } } 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 05d4e469d..3133273f9 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,13 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.UUID; + import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.onap.policy.aai.PNF; -import org.onap.policy.aai.PNFType; +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; @@ -53,17 +54,18 @@ import org.yaml.snakeyaml.error.YAMLException; public class ControlLoopPolicyBuilderTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Test public void testControlLoop() { 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 add services // @@ -98,92 +100,102 @@ public class ControlLoopPolicyBuilderTest { fail(e.getMessage()); } } - + @Test public void testAddNullService() throws BuilderException { - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); expectedException.expect(BuilderException.class); expectedException.expectMessage("Service must not be null"); - builder.addService((Service)null); + builder.addService((Service) null); } - + @Test public void testAddInvalidService() throws BuilderException { - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); expectedException.expect(BuilderException.class); expectedException.expectMessage("Invalid service - need either a serviceUUID or serviceName"); builder.addService(new Service()); } - + @Test public void testAddServiceWithUUID() throws BuilderException { - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); UUID uuid = UUID.randomUUID(); Service serviceWithUUID = new Service(uuid); builder.addService(serviceWithUUID); assertTrue(builder.getControlLoop().getServices().size() == 1); } - + @Test public void testAddNullResource() throws BuilderException { - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); expectedException.expect(BuilderException.class); expectedException.expectMessage("Resource must not be null"); - builder.addResource((Resource)null); + builder.addResource((Resource) null); } - - + + @Test public void testAddInvalidResource() throws BuilderException { - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); expectedException.expect(BuilderException.class); expectedException.expectMessage("Invalid resource - need either resourceUUID or resourceName"); builder.addResource(new Resource()); } - + @Test public void testAddAndRemoveResourceWithUUID() throws BuilderException { - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); UUID uuid = UUID.randomUUID(); Resource resourceWithUUID = new Resource(uuid); builder.addResource(resourceWithUUID); assertTrue(builder.getControlLoop().getResources().size() == 1); - + builder.removeResource(resourceWithUUID); assertTrue(builder.getControlLoop().getResources().size() == 0); } - + @Test public void testRemoveNullResource() throws BuilderException { - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); Resource resource = new Resource("resource1", ResourceType.VF); builder.addResource(resource); expectedException.expect(BuilderException.class); expectedException.expectMessage("Resource must not be null"); - builder.removeResource((Resource)null); + builder.removeResource((Resource) null); } - + @Test public void testRemoveResourceNoExistingResources() throws BuilderException { - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); expectedException.expect(BuilderException.class); expectedException.expectMessage("No existing resources to remove"); builder.removeResource(new Resource("resource1", ResourceType.VF)); } - + @Test public void testRemoveInvalidResource() throws BuilderException { - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); Resource resource = new Resource("resource1", ResourceType.VF); builder.addResource(resource); expectedException.expect(BuilderException.class); expectedException.expectMessage("Invalid resource - need either a resourceUUID or resourceName"); builder.removeResource(new Resource()); } - + @Test public void testRemoveUnknownResource() throws BuilderException { - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); Resource resource = new Resource("resource1", ResourceType.VF); builder.addResource(resource); final String unknownResourceName = "reource2"; @@ -191,155 +203,150 @@ public class ControlLoopPolicyBuilderTest { expectedException.expectMessage("Unknown resource " + unknownResourceName); builder.removeResource(new Resource(unknownResourceName, ResourceType.VF)); } - + @Test public void testControlLoopWithInitialResourceAndServices() { try { Resource vCTS = new Resource("vCTS", ResourceType.VF); Service vSCP = new Service("vSCP"); Service vUSP = new Service("vUSP"); - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, vCTS, vSCP, vUSP); + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory + .buildControlLoop(UUID.randomUUID().toString(), 2400, vCTS, vSCP, vUSP); assertTrue(builder.getControlLoop().getResources().size() == 1); assertTrue(builder.getControlLoop().getServices().size() == 2); } catch (BuilderException e) { fail(e.getMessage()); } } - + @Test public void testControlLoopWithInitialResourcesAndService() { try { Resource vCTS = new Resource("vCTS", ResourceType.VF); Resource vCOM = new Resource("vCTS", ResourceType.VF); Service vSCP = new Service("vSCP"); - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, vSCP, vCTS, vCOM); + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory + .buildControlLoop(UUID.randomUUID().toString(), 2400, vSCP, vCTS, vCOM); assertTrue(builder.getControlLoop().getServices().size() == 1); assertTrue(builder.getControlLoop().getResources().size() == 2); } catch (BuilderException e) { fail(e.getMessage()); } } - + @Test @Ignore // I'VE MARKED THIS TEST CASE AS IGNORE BECAUSE THE TEST CASE FAILS - // This test case fails because builder.getControlLoop() returns an instance of ControlLoop copied using - // the ControlLoop(ControlLoop controlLoop) constructor. + // This test case fails because builder.getControlLoop() returns an instance of ControlLoop + // copied using + // the ControlLoop(ControlLoop controlLoop) constructor. // This constructor does not copy the value of pnf into the newly created object // On the face of it, this looks like a bug, but perhaps there is a reason for this // PLEASE ADVISE IF THE BEHAVIOUR IS INCORRECT OR THE TEST CASE IS INVALID public void testControlLoopForPnf() { try { - PNF pnf = new PNF(); - pnf.setPNFType(PNFType.ENODEB); - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, pnf); + Pnf pnf = new Pnf(); + pnf.setPnfType(PnfType.ENODEB); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, pnf); assertEquals(pnf, builder.getControlLoop().getPnf()); - + builder.removePNF(); assertNull(builder.getControlLoop().getPnf()); } catch (BuilderException e) { fail(e.getMessage()); } } - + @Test @Ignore // Fails for the same reason as the above test case public void testSetAndRemovePnf() throws BuilderException { - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); assertNull(builder.getControlLoop().getPnf()); - - PNF pnf = new PNF(); - pnf.setPNFType(PNFType.ENODEB); + + Pnf pnf = new Pnf(); + pnf.setPnfType(PnfType.ENODEB); builder.setPNF(pnf); assertEquals(pnf, builder.getControlLoop().getPnf()); - + builder.removePNF(); assertNull(builder.getControlLoop().getPnf()); } - + @Test public void testSetNullPnf() throws BuilderException { - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); expectedException.expect(BuilderException.class); expectedException.expectMessage("PNF must not be null"); builder.setPNF(null); } - + @Test public void testSetInvalidPnf() throws BuilderException { - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); expectedException.expect(BuilderException.class); expectedException.expectMessage("Invalid PNF - need either pnfName or pnfType"); - builder.setPNF(new PNF()); + builder.setPNF(new Pnf()); } - + @Test public void testSetAbatement() throws BuilderException { - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); assertFalse(builder.getControlLoop().getAbatement()); builder = builder.setAbatement(true); assertTrue(builder.getControlLoop().getAbatement()); } - + @Test public void testSetNullAbatement() throws BuilderException { - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); expectedException.expect(BuilderException.class); expectedException.expectMessage("abatement must not be null"); builder = builder.setAbatement(null); } - + @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().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); + 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); + 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); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); // // Test isOpenLoop // @@ -347,31 +354,19 @@ 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); + 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 + // 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); - // + 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") @@ -386,160 +381,120 @@ public class ControlLoopPolicyBuilderTest { fail(e.getMessage()); } } - + @Test public void testSetTriggerPolicyNullPolicyId() throws BuilderException { - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); expectedException.expect(BuilderException.class); expectedException.expectMessage("Id must not be null"); builder.setTriggerPolicy(null); } - + @Test public void testSetTriggerPolicyNoPoliciesExist() throws BuilderException { - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); final String unknownPolicyId = "100"; expectedException.expect(BuilderException.class); expectedException.expectMessage("Unknown policy " + unknownPolicyId); builder.setTriggerPolicy(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); + 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); final String unknownPolicyId = "100"; expectedException.expect(BuilderException.class); expectedException.expectMessage("Unknown policy " + unknownPolicyId); builder.setTriggerPolicy(unknownPolicyId); } - + @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); + 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_EXCEPTION, - PolicyResult.FAILURE_RETRIES, - PolicyResult.FAILURE_TIMEOUT, - PolicyResult.FAILURE_GUARD); + 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_EXCEPTION, + PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT, PolicyResult.FAILURE_GUARD); // assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy1.getId())); assertTrue(builder.getTriggerPolicy().getFailure_exception().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 create a policy and chain it to the results of trigger policy success // - Policy onSuccessPolicy1 = builder.setPolicyForPolicyResult( - "Do something", - "If the restart succeeds, do something else.", - "APPC", - new Target(TargetType.VM), - "SomethingElse", - null, - 1, - 600, - triggerPolicy.getId(), - PolicyResult.SUCCESS); + Policy onSuccessPolicy1 = builder.setPolicyForPolicyResult("Do something", + "If the restart succeeds, do something else.", "APPC", new Target(TargetType.VM), "SomethingElse", + null, 1, 600, triggerPolicy.getId(), PolicyResult.SUCCESS); // assertTrue(builder.getTriggerPolicy().getSuccess().equals(onSuccessPolicy1.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())); + 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); + 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())); - // + 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); + 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 set the policy result for success to an existing operational policy // - onRestartFailurePolicy2 = builder.setPolicyForPolicyResult( - onRestartFailurePolicy2.getId(), - triggerPolicy.getId(), - PolicyResult.FAILURE, - PolicyResult.FAILURE_EXCEPTION, - PolicyResult.FAILURE_GUARD, - PolicyResult.FAILURE_RETRIES, - PolicyResult.FAILURE_TIMEOUT, - PolicyResult.SUCCESS); + onRestartFailurePolicy2 = + builder.setPolicyForPolicyResult(onRestartFailurePolicy2.getId(), triggerPolicy.getId(), + PolicyResult.FAILURE, PolicyResult.FAILURE_EXCEPTION, PolicyResult.FAILURE_GUARD, + PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT, PolicyResult.SUCCESS); assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy2.getId())); assertTrue(builder.getTriggerPolicy().getFailure_exception().equals(onRestartFailurePolicy2.getId())); assertTrue(builder.getTriggerPolicy().getFailure_guard().equals(onRestartFailurePolicy2.getId())); assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy2.getId())); assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy2.getId())); assertTrue(builder.getTriggerPolicy().getSuccess().equals(onRestartFailurePolicy2.getId())); - + // // Test remove all existing operational policies // @@ -550,106 +505,68 @@ public class ControlLoopPolicyBuilderTest { fail(e.getMessage()); } } - + @Test public void testAddToUnknownPolicy() throws BuilderException { - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); final String policyId = "100"; expectedException.expect(BuilderException.class); expectedException.expectMessage("Unknown policy " + policyId); - - builder.setPolicyForPolicyResult( - "Rebuild VM", - "If the restart fails, rebuild it.", - "APPC", - new Target(TargetType.VM), - "Rebuild", - null, - 1, - 600, - policyId, - PolicyResult.FAILURE, - PolicyResult.FAILURE_RETRIES, - PolicyResult.FAILURE_TIMEOUT, - PolicyResult.FAILURE_GUARD); - } - + + builder.setPolicyForPolicyResult("Rebuild VM", "If the restart fails, rebuild it.", "APPC", + new Target(TargetType.VM), "Rebuild", null, 1, 600, policyId, PolicyResult.FAILURE, + PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT, PolicyResult.FAILURE_GUARD); + } + @Test public void testAddExistingPolicyToUnknownPolicy() throws BuilderException { - 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); - - - Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult( - "Rebuild VM", - "If the restart fails, rebuild it.", - "APPC", - new Target(TargetType.VM), - "Rebuild", - null, - 1, - 600, - triggerPolicy.getId(), - PolicyResult.FAILURE); - + 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); + + + Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult("Rebuild VM", + "If the restart fails, rebuild it.", "APPC", new Target(TargetType.VM), "Rebuild", null, 1, 600, + triggerPolicy.getId(), PolicyResult.FAILURE); + final String unknownPolicyId = "100"; expectedException.expect(BuilderException.class); expectedException.expectMessage(unknownPolicyId + " does not exist"); - - builder.setPolicyForPolicyResult( - onRestartFailurePolicy.getId(), - unknownPolicyId, - PolicyResult.FAILURE); + + builder.setPolicyForPolicyResult(onRestartFailurePolicy.getId(), unknownPolicyId, PolicyResult.FAILURE); } - + @Test public void testAddUnknownExistingPolicyToPolicy() throws BuilderException { - 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); - + 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); + final String unknownPolicyId = "100"; expectedException.expect(BuilderException.class); expectedException.expectMessage("Operational policy " + unknownPolicyId + " does not exist"); - - builder.setPolicyForPolicyResult( - unknownPolicyId, - triggerPolicy.getId(), - PolicyResult.FAILURE); + + builder.setPolicyForPolicyResult(unknownPolicyId, triggerPolicy.getId(), PolicyResult.FAILURE); } @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", - "RANController", - new Target(TargetType.PNF), - "Restart", - null, - 2, - 300); + 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); // // Add the operationsAccumulateParams // - triggerPolicy = builder.addOperationsAccumulateParams(triggerPolicy.getId(), new OperationsAccumulateParams("15m", 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); @@ -658,27 +575,21 @@ public class ControlLoopPolicyBuilderTest { fail(e.getMessage()); } } - - + + @Test public void testBuildSpecification() { try { // // Create the builder // - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 800); + 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); + 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 @@ -704,41 +615,29 @@ public class ControlLoopPolicyBuilderTest { // // Remove the invalid policy // - //@SuppressWarnings("unused") + // @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); + 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); + 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) { + if (m.getMessage().equals("Policy " + policy1.getId() + " is not reachable.") + && m.getLevel() == MessageLevel.WARNING) { unreachable = true; break; } @@ -747,23 +646,21 @@ public class ControlLoopPolicyBuilderTest { // // Set policy1 for the failure results of policy2 // - policy1 = builder.setPolicyForPolicyResult( - policy1.getId(), - policy2.getId(), - PolicyResult.FAILURE, - PolicyResult.FAILURE_RETRIES, - PolicyResult.FAILURE_TIMEOUT); + 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) { + 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) + // Remove policy2 (revert controlLoop back to open loop) // removed = builder.removePolicy(policy2.getId()); // @@ -772,7 +669,8 @@ public class ControlLoopPolicyBuilderTest { 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) { + if (m.getMessage().equals("Open Loop policy contains policies. The policies will never be invoked.") + && m.getLevel() == MessageLevel.WARNING) { unreachable = true; break; } @@ -783,13 +681,13 @@ public class ControlLoopPolicyBuilderTest { 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"))) { @@ -808,7 +706,7 @@ public class ControlLoopPolicyBuilderTest { // } } - + public void test(String testFile) { try (InputStream is = new FileInputStream(new File(testFile))) { // @@ -823,19 +721,20 @@ public class ControlLoopPolicyBuilderTest { // Now we're going to try to use the builder to build this. // ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop( - policyTobuild.getControlLoop().getControlLoopName(), - policyTobuild.getControlLoop().getTimeout()); + 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()])); + 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()])); + builder = builder.addResource(policyTobuild.getControlLoop().getResources() + .toArray(new Resource[policyTobuild.getControlLoop().getResources().size()])); } // // Set pnf @@ -849,16 +748,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(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) { @@ -866,7 +766,7 @@ public class ControlLoopPolicyBuilderTest { } catch (BuilderException e) { fail(e.getLocalizedMessage()); } - + } } diff --git a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopTest.java b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopTest.java index 0b2f62def..cc5a903ad 100644 --- a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopTest.java +++ b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopTest.java @@ -15,6 +15,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.policy.controlloop.policy; import static org.junit.Assert.assertEquals; @@ -23,122 +24,124 @@ import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.List; + import org.junit.Ignore; import org.junit.Test; -import org.onap.policy.aai.PNF; +import org.onap.policy.aai.Pnf; import org.onap.policy.sdc.Resource; import org.onap.policy.sdc.ResourceType; import org.onap.policy.sdc.Service; public class ControlLoopTest { - + private String controlLoopName = "control loop 1"; private String version = "1.0.1"; private String triggerPolicy = FinalResult.FINAL_OPENLOOP.toString(); private Integer timeout = 100; private Boolean abatement = false; - - @Test - public void testEqualsSameInstance() { - ControlLoop controlLoop1 = new ControlLoop(); - assertTrue(controlLoop1.equals(controlLoop1)); - } - - @Test - public void testEqualsNull() { - ControlLoop controlLoop1 = new ControlLoop(); - assertFalse(controlLoop1.equals(null)); - } - @Test - public void testEqualsInstanceOfDiffClass() { - ControlLoop controlLoop1 = new ControlLoop(); - assertFalse(controlLoop1.equals("")); - } - - @Test - public void testEqualsNoServicesAndResourcesOrTimeout() { - final PNF pnf = new PNF(); - pnf.setPNFName("pnf 1"); - - ControlLoop controlLoop1 = new ControlLoop(); - controlLoop1.setControlLoopName(controlLoopName); - controlLoop1.setVersion(version); - controlLoop1.setPnf(pnf); - controlLoop1.setTrigger_policy(triggerPolicy); - controlLoop1.setAbatement(abatement); - - ControlLoop controlLoop2 = new ControlLoop(); - controlLoop2.setControlLoopName(controlLoopName); - controlLoop2.setVersion(version); - controlLoop2.setPnf(pnf); - controlLoop2.setTrigger_policy(triggerPolicy); - controlLoop2.setAbatement(abatement); - - assertTrue(controlLoop1.equals(controlLoop2)); - } - - @Test - public void testEquals() { - final PNF pnf = new PNF(); - pnf.setPNFName("pnf 1"); - - ControlLoop controlLoop1 = new ControlLoop(); - controlLoop1.setControlLoopName(controlLoopName); - controlLoop1.setVersion(version); - Service service1 = new Service("service1"); - Service service2 = new Service("service2"); - List<Service> services = new ArrayList<>(); - services.add(service1); - services.add(service2); - controlLoop1.setServices(services); - Resource resource1 = new Resource("resource1", ResourceType.VF); - Resource resource2 = new Resource("resource2", ResourceType.VFC); - List<Resource> resources = new ArrayList<>(); - resources.add(resource1); - resources.add(resource2); - controlLoop1.setResources(resources); - controlLoop1.setPnf(pnf); - controlLoop1.setTrigger_policy(triggerPolicy); - controlLoop1.setTimeout(timeout); - controlLoop1.setAbatement(abatement); - - ControlLoop controlLoop2 = new ControlLoop(); - controlLoop2.setControlLoopName(controlLoopName); - controlLoop2.setVersion(version); - Service controlLoop2_service1 = new Service("service1"); - Service controlLoop2_service2 = new Service("service2"); - List<Service> controlLoop2_services = new ArrayList<>(); - controlLoop2_services.add(controlLoop2_service1); - controlLoop2_services.add(controlLoop2_service2); - controlLoop2.setServices(controlLoop2_services); - Resource controlLoop2_resource1 = new Resource("resource1", ResourceType.VF); - Resource controlLoop2_resource2 = new Resource("resource2", ResourceType.VFC); - List<Resource> controlLoop2_resources = new ArrayList<>(); - controlLoop2_resources.add(controlLoop2_resource1); - controlLoop2_resources.add(controlLoop2_resource2); - controlLoop2.setResources(controlLoop2_resources); - controlLoop2.setPnf(pnf); - controlLoop2.setTrigger_policy(triggerPolicy); - controlLoop2.setTimeout(timeout); - controlLoop1.setAbatement(abatement); - - assertTrue(controlLoop1.equals(controlLoop2)); - assertEquals(controlLoop1.hashCode(), controlLoop2.hashCode()); - } - - @Test + + @Test + public void testEqualsSameInstance() { + ControlLoop controlLoop1 = new ControlLoop(); + assertTrue(controlLoop1.equals(controlLoop1)); + } + + @Test + public void testEqualsNull() { + ControlLoop controlLoop1 = new ControlLoop(); + assertFalse(controlLoop1.equals(null)); + } + + @Test + public void testEqualsInstanceOfDiffClass() { + ControlLoop controlLoop1 = new ControlLoop(); + assertFalse(controlLoop1.equals("")); + } + + @Test + public void testEqualsNoServicesAndResourcesOrTimeout() { + final Pnf pnf = new Pnf(); + pnf.setPnfName("pnf 1"); + + ControlLoop controlLoop1 = new ControlLoop(); + controlLoop1.setControlLoopName(controlLoopName); + controlLoop1.setVersion(version); + controlLoop1.setPnf(pnf); + controlLoop1.setTrigger_policy(triggerPolicy); + controlLoop1.setAbatement(abatement); + + ControlLoop controlLoop2 = new ControlLoop(); + controlLoop2.setControlLoopName(controlLoopName); + controlLoop2.setVersion(version); + controlLoop2.setPnf(pnf); + controlLoop2.setTrigger_policy(triggerPolicy); + controlLoop2.setAbatement(abatement); + + assertTrue(controlLoop1.equals(controlLoop2)); + } + + @Test + public void testEquals() { + final Pnf pnf = new Pnf(); + pnf.setPnfName("pnf 1"); + + ControlLoop controlLoop1 = new ControlLoop(); + controlLoop1.setControlLoopName(controlLoopName); + controlLoop1.setVersion(version); + Service service1 = new Service("service1"); + Service service2 = new Service("service2"); + List<Service> services = new ArrayList<>(); + services.add(service1); + services.add(service2); + controlLoop1.setServices(services); + Resource resource1 = new Resource("resource1", ResourceType.VF); + Resource resource2 = new Resource("resource2", ResourceType.VFC); + List<Resource> resources = new ArrayList<>(); + resources.add(resource1); + resources.add(resource2); + controlLoop1.setResources(resources); + controlLoop1.setPnf(pnf); + controlLoop1.setTrigger_policy(triggerPolicy); + controlLoop1.setTimeout(timeout); + controlLoop1.setAbatement(abatement); + + ControlLoop controlLoop2 = new ControlLoop(); + controlLoop2.setControlLoopName(controlLoopName); + controlLoop2.setVersion(version); + Service controlLoop2_service1 = new Service("service1"); + Service controlLoop2_service2 = new Service("service2"); + List<Service> controlLoop2_services = new ArrayList<>(); + controlLoop2_services.add(controlLoop2_service1); + controlLoop2_services.add(controlLoop2_service2); + controlLoop2.setServices(controlLoop2_services); + Resource controlLoop2_resource1 = new Resource("resource1", ResourceType.VF); + Resource controlLoop2_resource2 = new Resource("resource2", ResourceType.VFC); + List<Resource> controlLoop2_resources = new ArrayList<>(); + controlLoop2_resources.add(controlLoop2_resource1); + controlLoop2_resources.add(controlLoop2_resource2); + controlLoop2.setResources(controlLoop2_resources); + controlLoop2.setPnf(pnf); + controlLoop2.setTrigger_policy(triggerPolicy); + controlLoop2.setTimeout(timeout); + controlLoop1.setAbatement(abatement); + + assertTrue(controlLoop1.equals(controlLoop2)); + assertEquals(controlLoop1.hashCode(), controlLoop2.hashCode()); + } + + @Test @Ignore // I'VE MARKED THIS TEST CASE AS IGNORE BECAUSE THE TEST CASE FAILS - // This test case fails because the ControlLoop(ControlLoop controlLoop) constructor. + // This test case fails because the ControlLoop(ControlLoop controlLoop) constructor. // does not copy the value of pnf and version into the newly created object // PLEASE ADVISE IF THE EXISTING BEHAVIOUR IS CORRECT - public void testControlLoop() { - final PNF pnf = new PNF(); - pnf.setPNFName("pnf 1"); - - ControlLoop controlLoop1 = new ControlLoop(); - controlLoop1.setControlLoopName(controlLoopName); - controlLoop1.setVersion(version); + public void testControlLoop() { + final Pnf pnf = new Pnf(); + pnf.setPnfName("pnf 1"); + + ControlLoop controlLoop1 = new ControlLoop(); + controlLoop1.setControlLoopName(controlLoopName); + controlLoop1.setVersion(version); Service service1 = new Service("service1"); Service service2 = new Service("service2"); List<Service> services = new ArrayList<>(); @@ -152,20 +155,20 @@ public class ControlLoopTest { resources.add(resource2); controlLoop1.setResources(resources); controlLoop1.setPnf(pnf); - controlLoop1.setTrigger_policy(triggerPolicy); - controlLoop1.setAbatement(abatement); - - ControlLoop controlLoop2 = new ControlLoop(controlLoop1); - - assertEquals(controlLoop1.getControlLoopName(), controlLoop2.getControlLoopName()); - assertEquals(controlLoop1.getVersion(), controlLoop2.getVersion()); - assertEquals(controlLoop1.getServices(), controlLoop2.getServices()); - assertEquals(controlLoop1.getResources(), controlLoop2.getResources()); - assertEquals(controlLoop1.getPnf(), controlLoop2.getPnf()); - assertEquals(controlLoop1.getTrigger_policy(), controlLoop2.getTrigger_policy()); - assertEquals(controlLoop1.getAbatement(), controlLoop2.getAbatement()); - - assertTrue(controlLoop1.equals(controlLoop2)); - } + controlLoop1.setTrigger_policy(triggerPolicy); + controlLoop1.setAbatement(abatement); + + ControlLoop controlLoop2 = new ControlLoop(controlLoop1); + + assertEquals(controlLoop1.getControlLoopName(), controlLoop2.getControlLoopName()); + assertEquals(controlLoop1.getVersion(), controlLoop2.getVersion()); + assertEquals(controlLoop1.getServices(), controlLoop2.getServices()); + assertEquals(controlLoop1.getResources(), controlLoop2.getResources()); + assertEquals(controlLoop1.getPnf(), controlLoop2.getPnf()); + assertEquals(controlLoop1.getTrigger_policy(), controlLoop2.getTrigger_policy()); + assertEquals(controlLoop1.getAbatement(), controlLoop2.getAbatement()); + + assertTrue(controlLoop1.equals(controlLoop2)); + } } |