diff options
Diffstat (limited to 'ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard')
8 files changed, 1116 insertions, 0 deletions
diff --git a/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/Constraint.java b/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/Constraint.java new file mode 100644 index 000000000..ac3dbb03f --- /dev/null +++ b/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/Constraint.java @@ -0,0 +1,171 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Engine + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.policy.controlloop.policy.guard; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +public class Constraint { + + private Integer freq_limit_per_target; + private Map<String,String> time_window; + private Map<String, String> active_time_range; + + private List<String> blacklist; + + public Constraint() { + // Do Nothing empty constructor. + } + + public Integer getFreq_limit_per_target() { + return freq_limit_per_target; + } + + + public void setFreq_limit_per_target(Integer freq_limit_per_target) { + this.freq_limit_per_target = freq_limit_per_target; + } + + + public Map<String, String> getTime_window() { + return time_window; + } + + + public void setTime_window(Map<String, String> time_window) { + this.time_window = time_window; + } + + + public Map<String, String> getActive_time_range() { + return active_time_range; + } + + + public void setActive_time_range(Map<String, String> active_time_range) { + this.active_time_range = active_time_range; + } + + + public List<String> getBlacklist() { + return blacklist; + } + + public void setBlacklist(List<String> blacklist) { + this.blacklist = blacklist; + } + + public Constraint(Integer freq_limit_per_target, Map<String, String> time_window) { + this.freq_limit_per_target = freq_limit_per_target; + if(time_window!=null){ + this.time_window = Collections.unmodifiableMap(time_window); + } + } + + public Constraint(List<String> blacklist) { + this.blacklist = new LinkedList<>(blacklist); + } + + public Constraint(Integer freq_limit_per_target, Map<String, String> time_window, List<String> blacklist) { + this.freq_limit_per_target = freq_limit_per_target; + this.time_window = Collections.unmodifiableMap(time_window); + this.blacklist = new LinkedList<>(blacklist); + } + + public Constraint(Integer freq_limit_per_target, Map<String, String> time_window, Map<String, String> active_time_range) { + this(freq_limit_per_target, time_window); + if (active_time_range != null) { + this.active_time_range = Collections.unmodifiableMap(active_time_range); + } + } + + public Constraint(Integer freq_limit_per_target, Map<String, String> time_window, Map<String, String> active_time_range, List<String> blacklist) { + this(freq_limit_per_target, time_window); + if (active_time_range != null) { + this.active_time_range = Collections.unmodifiableMap(active_time_range); + } + if(blacklist!=null){ + this.blacklist = new LinkedList<>(blacklist); + } + } + + public Constraint(Constraint constraint) { + this.freq_limit_per_target = constraint.freq_limit_per_target; + this.time_window = constraint.time_window; + if (constraint.active_time_range != null) { + this.active_time_range = Collections.unmodifiableMap(constraint.active_time_range); + } + this.blacklist = new LinkedList<>(constraint.blacklist); + } + + public boolean isValid() { + return ((freq_limit_per_target == null && time_window != null)|| (time_window == null && freq_limit_per_target != null))? false : true; + } + + @Override + public String toString() { + return "Constraint [freq_limit_per_target=" + freq_limit_per_target + ", time_window=" + time_window + ", active_time_range=" + active_time_range + ", blacklist=" + blacklist + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((freq_limit_per_target == null) ? 0 : freq_limit_per_target.hashCode()); + result = prime * result + ((time_window == null) ? 0 : time_window.hashCode()); + result = prime * result + ((active_time_range == null) ? 0 : active_time_range.hashCode()); + result = prime * result + ((blacklist == null) ? 0 : blacklist.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Constraint other = (Constraint) obj; + if (freq_limit_per_target == null) { + if (other.freq_limit_per_target != null) + return false; + } else if (!freq_limit_per_target.equals(other.freq_limit_per_target)) + return false; + if (time_window == null) { + if (other.time_window != null) + return false; + } else if (!time_window.equals(other.time_window)) + return false; + if (active_time_range == null) { + if (other.active_time_range != null) + return false; + } else if (!active_time_range.equals(other.active_time_range)) + return false; + if (blacklist == null) { + if (other.blacklist != null) + return false; + } else if (!blacklist.equals(other.blacklist)) + return false; + return true; + } +} diff --git a/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuard.java b/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuard.java new file mode 100644 index 000000000..5d832140c --- /dev/null +++ b/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuard.java @@ -0,0 +1,92 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Engine + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.policy.controlloop.policy.guard; + +import java.util.LinkedList; + +public class ControlLoopGuard { + + private Guard guard; + + private LinkedList<GuardPolicy> guards; + + public ControlLoopGuard() { + //DO Nothing Empty Constructor + } + + public Guard getGuard() { + return guard; + } + + public void setGuard(Guard guard) { + this.guard = guard; + } + + public LinkedList<GuardPolicy> getGuards() { + return guards; + } + + public void setGuards(LinkedList<GuardPolicy> guards) { + this.guards = guards; + } + + public ControlLoopGuard(ControlLoopGuard cLGuard) { + this.guard = new Guard(); + this.guards = new LinkedList<>(cLGuard.guards); + } + + @Override + public String toString() { + return "Guard [guard=" + guard + ", GuardPolicies=" + guards + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((guard == null) ? 0 : guard.hashCode()); + result = prime * result + ((guards == null) ? 0 : guards.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ControlLoopGuard other = (ControlLoopGuard) obj; + if (guard == null) { + if (other.guard != null) + return false; + } else if (!guard.equals(other.guard)) + return false; + if (guards == null) { + if (other.guards != null) + return false; + } else if (!guards.equals(other.guards)) + return false; + return true; + } + + +} diff --git a/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/ControlLoopParameter.java b/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/ControlLoopParameter.java new file mode 100644 index 000000000..dafa95094 --- /dev/null +++ b/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/ControlLoopParameter.java @@ -0,0 +1,89 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Engine + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.policy.controlloop.policy.guard; + +public class ControlLoopParameter { + private String controlLoopName; + private String version; + + public String getControlLoopName() { + return controlLoopName; + } + + public void setControlLoopName(String controlLoopName) { + this.controlLoopName = controlLoopName; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public ControlLoopParameter(String controlLoopName, String version) { + super(); + this.controlLoopName = controlLoopName; + this.version = version; + } + + public ControlLoopParameter(ControlLoopParameter cl) { + + this.controlLoopName = cl.controlLoopName; + this.version = cl.version; + } + + @Override + public String toString() { + return "ControlLoopParameter [controlLoopName=" + controlLoopName + ", version=" + version + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((controlLoopName == null) ? 0 : controlLoopName.hashCode()); + result = prime * result + ((version == null) ? 0 : version.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ControlLoopParameter other = (ControlLoopParameter) obj; + if (controlLoopName == null) { + if (other.controlLoopName != null) + return false; + } else if (!controlLoopName.equals(other.controlLoopName)) + return false; + if (version == null) { + if (other.version != null) + return false; + } else if (!version.equals(other.version)) + return false; + return true; + } +} diff --git a/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/Guard.java b/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/Guard.java new file mode 100644 index 000000000..453466989 --- /dev/null +++ b/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/Guard.java @@ -0,0 +1,67 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Engine + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.policy.controlloop.policy.guard; + +public class Guard { + + private static final String DEFAULTVERSION = "2.0.0"; + + private String version = DEFAULTVERSION; + + public Guard() { + //DO Nothing empty Constructor. + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + @Override + public String toString() { + return "Guard [version=" + version + "]"; + } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((version == null) ? 0 : version.hashCode()); + return result; + } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Guard other = (Guard) obj; + if (version == null) { + if (other.version != null) + return false; + } else if (!version.equals(other.version)) + return false; + return true; + } +} diff --git a/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/GuardPolicy.java b/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/GuardPolicy.java new file mode 100644 index 000000000..2068a46da --- /dev/null +++ b/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/GuardPolicy.java @@ -0,0 +1,181 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Engine + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.policy.controlloop.policy.guard; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.UUID; + +public class GuardPolicy { + + private String id = UUID.randomUUID().toString(); + private String name; + private String description; + private MatchParameters match_parameters; + private LinkedList<Constraint> limit_constraints; + + public GuardPolicy() { + //Do Nothing Empty Constructor. + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public MatchParameters getMatch_parameters() { + return match_parameters; + } + + public void setMatch_parameters(MatchParameters match_parameters) { + this.match_parameters = match_parameters; + } + + public LinkedList<Constraint> getLimit_constraints() { + return limit_constraints; + } + + public void setLimit_constraints(LinkedList<Constraint> limit_constraints) { + this.limit_constraints = limit_constraints; + } + + public GuardPolicy(String id) { + this.id = id; + } + + public GuardPolicy(String name, MatchParameters matchParameters) { + this.name = name; + this.match_parameters = matchParameters; + } + + public GuardPolicy(String id, String name, String description, MatchParameters matchParameters) { + this(name, matchParameters); + this.id = id; + this.description = description; + } + + public GuardPolicy(String name, MatchParameters matchParameters, List<Constraint> limitConstraints) { + this(name, matchParameters); + if (limit_constraints != null) { + this.limit_constraints = (LinkedList<Constraint>) Collections.unmodifiableList(limitConstraints); + } + } + + public GuardPolicy(String name, String description, MatchParameters matchParameters, List<Constraint> limitConstraints) { + this(name, matchParameters, limitConstraints); + this.description = description; + } + + public GuardPolicy(String id, String name, String description, MatchParameters matchParameters, List<Constraint> limitConstraints) { + this(name, description, matchParameters, limitConstraints); + this.id = id; + } + + public GuardPolicy(GuardPolicy policy) { + this.id = policy.id; + this.name = policy.name; + this.description = policy.description; + this.match_parameters = new MatchParameters(policy.match_parameters); + if (policy.limit_constraints != null) { + this.limit_constraints = (LinkedList<Constraint>) Collections.unmodifiableList(policy.limit_constraints); + } + } + + public boolean isValid() { + return (id==null || name ==null)? false : true; + } + + @Override + public String toString() { + return "Policy [id=" + id + ", name=" + name + ", description=" + description + ", match_parameters=" + +match_parameters + ", limitConstraints=" + limit_constraints + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((description == null) ? 0 : description.hashCode()); + result = prime * result + ((id == null) ? 0 : id.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((limit_constraints == null) ? 0 : limit_constraints.hashCode()); + result = prime * result + ((match_parameters == null) ? 0 : match_parameters.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + GuardPolicy other = (GuardPolicy) obj; + if (description == null) { + if (other.description != null) + return false; + } else if (!description.equals(other.description)) + return false; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (limit_constraints == null) { + if (other.limit_constraints != null) + return false; + } else if (!limit_constraints.equals(other.limit_constraints)) + return false; + if (match_parameters==null){ + if(other.match_parameters !=null) + return false; + } else if(!match_parameters.equals(other.match_parameters)) + return false; + return true; + } + + +} diff --git a/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/MatchParameters.java b/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/MatchParameters.java new file mode 100644 index 000000000..da64cfcaa --- /dev/null +++ b/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/MatchParameters.java @@ -0,0 +1,142 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Engine + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.policy.controlloop.policy.guard; + +import java.util.LinkedList; +import java.util.List; + +public class MatchParameters { + private String controlLoopName; + private String actor; + private String recipe; + private List<String> targets; + + public MatchParameters() { + // Do Nothing Empty Constructor. + } + + public String getControlLoopName() { + return controlLoopName; + } + + public void setControlLoopName(String controlLoopName) { + this.controlLoopName = controlLoopName; + } + + public String getActor() { + return actor; + } + + public void setActor(String actor) { + this.actor = actor; + } + + public String getRecipe() { + return recipe; + } + + public void setRecipe(String recipe) { + this.recipe = recipe; + } + + public List<String> getTargets() { + return targets; + } + + public void setTargets(List<String> targets) { + this.targets = targets; + } + + public MatchParameters(String actor, String recipe) { + this.actor = actor; + this.recipe = recipe; + } + + public MatchParameters(String actor, String recipe, List<String> targets) { + this(actor, recipe); + if (targets != null) { + this.targets = new LinkedList<>(targets); + } + } + + public MatchParameters(String controlLoopName, String actor, String recipe, List<String> targets) { + this(actor, recipe, targets); + this.controlLoopName = controlLoopName; + } + + public MatchParameters(MatchParameters matchParameters) { + + this.controlLoopName = matchParameters.controlLoopName; + this.actor = matchParameters.actor; + this.recipe = matchParameters.recipe; + if (matchParameters.targets != null) { + this.targets = new LinkedList<>(matchParameters.targets); + } + } + + @Override + public String toString() { + return "MatchParameters [controlLoopName=" + controlLoopName + ", actor=" + actor + ", recipe=" + recipe + + ", targets=" + targets + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((actor == null) ? 0 : actor.hashCode()); + result = prime * result + ((controlLoopName == null) ? 0 : controlLoopName.hashCode()); + result = prime * result + ((recipe == null) ? 0 : recipe.hashCode()); + result = prime * result + ((targets == null) ? 0 : targets.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + MatchParameters other = (MatchParameters) obj; + if (actor == null) { + if (other.actor != null) + return false; + } else if (!actor.equals(other.actor)) + return false; + if (controlLoopName == null) { + if (other.controlLoopName != null) + return false; + } else if (!controlLoopName.equals(other.controlLoopName)) + return false; + if (recipe == null) { + if (other.recipe != null) + return false; + } else if (!recipe.equals(other.recipe)) + return false; + if (targets == null) { + if (other.targets != null) + return false; + } else if (!targets.equals(other.targets)) + return false; + return true; + } +}
\ No newline at end of file diff --git a/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/builder/ControlLoopGuardBuilder.java b/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/builder/ControlLoopGuardBuilder.java new file mode 100644 index 000000000..e0e240db7 --- /dev/null +++ b/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/builder/ControlLoopGuardBuilder.java @@ -0,0 +1,130 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Engine + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.policy.controlloop.policy.guard.builder; + +import org.onap.policy.controlloop.policy.builder.BuilderException; +import org.onap.policy.controlloop.policy.builder.Results; +import org.onap.policy.controlloop.policy.guard.Constraint; +import org.onap.policy.controlloop.policy.guard.ControlLoopGuard; +import org.onap.policy.controlloop.policy.guard.Guard; +import org.onap.policy.controlloop.policy.guard.GuardPolicy; +import org.onap.policy.controlloop.policy.guard.builder.impl.ControlLoopGuardBuilderImpl; + +public interface ControlLoopGuardBuilder { + + /** + * Adds one or more guard policies to the Control Loop Guard + * + * + * @param policies + * @return + * @throws BuilderException + */ + public ControlLoopGuardBuilder addGuardPolicy(GuardPolicy... policies) throws BuilderException; + + /** + * Removes one or more guard policies from the Control Loop Guard + * + * + * @param policies + * @return + * @throws BuilderException + */ + public ControlLoopGuardBuilder removeGuardPolicy(GuardPolicy... policies) throws BuilderException; + + /** + * Removes all guard policies from the Control Loop Guard + * + * + * @param + * @return + * @throws BuilderException + */ + public ControlLoopGuardBuilder removeAllGuardPolicies() throws BuilderException; + + /** + * Adds one or more time limit constraints to the guard policy + * + * + * @param id (guard policy id) + * @param constraints + * @return + * @throws BuilderException + */ + public ControlLoopGuardBuilder addLimitConstraint(String id, Constraint... constraints) throws BuilderException; + + /** + * Removes one or more time limit constraints from the guard policy + * + * + * @param id (guard policy id) + * @param constraints + * @return + * @throws BuilderException + */ + public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException; + + /** + * Removes all time limit constraints from the guard policy + * + * + * @param id (guard policy id) + * @return + * @throws BuilderException + */ + public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException; + + /** + * Simply return a copy of control loop guard + * + * @return ControlLoopGuard + */ + public ControlLoopGuard getControlLoopGuard(); + + /** + * This will compile and build the YAML specification for the Control Loop Guard. 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(); + + /** + * The Factory is used to build a ControlLoopGuardBuilder implementation. + * + */ + public static class Factory { + + private Factory(){ + //Do Nothing Private Constructor. + } + /** + * @param guard + * @return ControlLoopGuardBuilder object + * @throws BuilderException + */ + public static ControlLoopGuardBuilder buildControlLoopGuard (Guard guard) throws BuilderException { + + return new ControlLoopGuardBuilderImpl(guard); + + } + } +} diff --git a/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/builder/impl/ControlLoopGuardBuilderImpl.java b/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/builder/impl/ControlLoopGuardBuilderImpl.java new file mode 100644 index 000000000..6152a4d08 --- /dev/null +++ b/ONAP-ControlloopPolicy/src/main/java/org/onap/policy/controlloop/policy/guard/builder/impl/ControlLoopGuardBuilderImpl.java @@ -0,0 +1,244 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Engine + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.policy.controlloop.policy.guard.builder.impl; + +import java.util.LinkedList; + +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.controlloop.compiler.CompilerException; +import org.onap.policy.controlloop.compiler.ControlLoopCompilerCallback; +import org.onap.policy.controlloop.guard.compiler.ControlLoopGuardCompiler; +import org.onap.policy.controlloop.policy.builder.BuilderException; +import org.onap.policy.controlloop.policy.builder.MessageLevel; +import org.onap.policy.controlloop.policy.builder.Results; +import org.onap.policy.controlloop.policy.builder.impl.MessageImpl; +import org.onap.policy.controlloop.policy.builder.impl.ResultsImpl; +import org.onap.policy.controlloop.policy.guard.Constraint; +import org.onap.policy.controlloop.policy.guard.ControlLoopGuard; +import org.onap.policy.controlloop.policy.guard.Guard; +import org.onap.policy.controlloop.policy.guard.GuardPolicy; +import org.onap.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.DumperOptions.FlowStyle; +import org.yaml.snakeyaml.Yaml; + +public class ControlLoopGuardBuilderImpl implements ControlLoopGuardBuilder { + private static Logger logger = FlexLogger.getLogger(ControlLoopGuardBuilderImpl.class.getName()); + private ControlLoopGuard cLGuard; + + public ControlLoopGuardBuilderImpl(Guard guard) { + cLGuard = new ControlLoopGuard(); + cLGuard.setGuard(guard); + } + + @Override + public ControlLoopGuardBuilder addGuardPolicy(GuardPolicy... policies) throws BuilderException { + if (policies == null) { + throw new BuilderException("GuardPolicy must not be null"); + } + for (GuardPolicy policy : policies) { + if (!policy.isValid()) { + throw new BuilderException("Invalid guard policy - some required fields are missing"); + } + if (cLGuard.getGuards() == null) { + cLGuard.setGuards(new LinkedList<>()); + } + cLGuard.getGuards().add(policy); + } + return this; + } + + @Override + public ControlLoopGuardBuilder removeGuardPolicy(GuardPolicy... policies) throws BuilderException { + if (policies == null) { + throw new BuilderException("GuardPolicy must not be null"); + } + if (cLGuard.getGuards() == null) { + throw new BuilderException("No existing guard policies to remove"); + } + for (GuardPolicy policy : policies) { + if (!policy.isValid()) { + throw new BuilderException("Invalid guard policy - some required fields are missing"); + } + boolean removed = cLGuard.getGuards().remove(policy); + if (!removed) { + throw new BuilderException("Unknown guard policy: " + policy.getName()); + } + } + return this; + } + + @Override + public ControlLoopGuardBuilder removeAllGuardPolicies() throws BuilderException { + cLGuard.getGuards().clear(); + return this; + } + + @Override + public ControlLoopGuardBuilder addLimitConstraint(String id, Constraint... constraints) throws BuilderException { + if (id == null) { + throw new BuilderException("The id of target guard policy must not be null"); + } + if (constraints == null) { + throw new BuilderException("Constraint much not be null"); + } + if (!addLimitConstraints(id,constraints)) { + throw new BuilderException("No existing guard policy matching the id: " + id); + } + return this; + } + + private boolean addLimitConstraints(String id, Constraint... constraints) throws BuilderException { + boolean exist = false; + for (GuardPolicy policy: cLGuard.getGuards()) { + // + // We could have only one guard policy matching the id + // + if (policy.getId().equals(id)) { + exist = true; + for (Constraint cons: constraints) { + if (!cons.isValid()) { + throw new BuilderException("Invalid guard constraint - some required fields are missing"); + } + if (policy.getLimit_constraints() == null) { + policy.setLimit_constraints(new LinkedList<>()); + } + policy.getLimit_constraints().add(cons); + } + break; + } + } + return exist; + } + + @Override + public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException { + if (id == null) { + throw new BuilderException("The id of target guard policy must not be null"); + } + if (constraints == null) { + throw new BuilderException("Constraint much not be null"); + } + if (!removeConstraints(id, constraints)) { + throw new BuilderException("No existing guard policy matching the id: " + id); + } + return this; + } + + private boolean removeConstraints(String id, Constraint... constraints) throws BuilderException { + boolean exist = false; + for (GuardPolicy policy: cLGuard.getGuards()) { + // + // We could have only one guard policy matching the id + // + if (policy.getId().equals(id)) { + exist = true; + for (Constraint cons: constraints) { + if (!cons.isValid()) { + throw new BuilderException("Invalid guard constraint - some required fields are missing"); + } + boolean removed = policy.getLimit_constraints().remove(cons); + if (!removed) { + throw new BuilderException("Unknown guard constraint: " + cons); + } + } + break; + } + } + return exist; + } + + @Override + public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException { + if (cLGuard.getGuards() == null || cLGuard.getGuards().isEmpty()) { + throw new BuilderException("No guard policies exist"); + } + if (id == null) { + throw new BuilderException("The id of target guard policy must not be null"); + } + boolean exist = false; + for (GuardPolicy policy: cLGuard.getGuards()) { + if (policy.getId().equals(id)) { + exist = true; + policy.getLimit_constraints().clear(); + } + } + if (!exist) { + throw new BuilderException("No existing guard policy matching the id: " + id); + } + return this; + } + + + 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 ControlLoopGuard getControlLoopGuard() { + return new ControlLoopGuard(this.cLGuard); + } + + + @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(cLGuard); + // + // This is our callback class for our compiler + // + BuilderCompilerCallback callback = new BuilderCompilerCallback(); + // + // Compile it + // + try { + ControlLoopGuardCompiler.compile(cLGuard, 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; + } + +} |