diff options
Diffstat (limited to 'controlloop/common/policy-yaml/src/main')
3 files changed, 574 insertions, 555 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); + } } |