2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.policy.guard.builder.impl;
23 import java.util.LinkedList;
25 import org.yaml.snakeyaml.DumperOptions;
26 import org.yaml.snakeyaml.Yaml;
27 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
29 import org.onap.policy.controlloop.compiler.CompilerException;
30 import org.onap.policy.controlloop.compiler.ControlLoopCompilerCallback;
31 import org.onap.policy.controlloop.guard.compiler.ControlLoopGuardCompiler;
32 import org.onap.policy.controlloop.policy.builder.BuilderException;
33 import org.onap.policy.controlloop.policy.builder.MessageLevel;
34 import org.onap.policy.controlloop.policy.builder.Results;
35 import org.onap.policy.controlloop.policy.builder.impl.MessageImpl;
36 import org.onap.policy.controlloop.policy.builder.impl.ResultsImpl;
37 import org.onap.policy.controlloop.policy.guard.Constraint;
38 import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
39 import org.onap.policy.controlloop.policy.guard.Guard;
40 import org.onap.policy.controlloop.policy.guard.GuardPolicy;
41 import org.onap.policy.controlloop.poligy.guard.builder.ControlLoopGuardBuilder;
43 public class ControlLoopGuardBuilderImpl implements ControlLoopGuardBuilder {
45 private ControlLoopGuard CLGuard;
47 public ControlLoopGuardBuilderImpl(Guard guard) {
48 CLGuard = new ControlLoopGuard();
49 CLGuard.guard = guard;
53 public ControlLoopGuardBuilder addGuardPolicy(GuardPolicy... policies) throws BuilderException {
54 if (policies == null) {
55 throw new BuilderException("GuardPolicy must not be null");
57 for (GuardPolicy policy : policies) {
58 if (!policy.isValid()) {
59 throw new BuilderException("Invalid guard policy - some required fields are missing");
61 if (CLGuard.guards == null) {
62 CLGuard.guards = new LinkedList<GuardPolicy>();
64 CLGuard.guards.add(policy);
70 public ControlLoopGuardBuilder removeGuardPolicy(GuardPolicy... policies) throws BuilderException {
71 if (policies == null) {
72 throw new BuilderException("GuardPolicy must not be null");
74 if (CLGuard.guards == null) {
75 throw new BuilderException("No existing guard policies to remove");
77 for (GuardPolicy policy : policies) {
78 if (!policy.isValid()) {
79 throw new BuilderException("Invalid guard policy - some required fields are missing");
81 boolean removed = CLGuard.guards.remove(policy);
83 throw new BuilderException("Unknown guard policy: " + policy.name);
90 public ControlLoopGuardBuilder removeAllGuardPolicies() throws BuilderException {
91 CLGuard.guards.clear();
96 public ControlLoopGuardBuilder addLimitConstraint(String id, Constraint... constraints) throws BuilderException {
98 throw new BuilderException("The id of target guard policy must not be null");
100 if (constraints == null) {
101 throw new BuilderException("Constraint much not be null");
103 boolean exist = false;
104 for (GuardPolicy policy: CLGuard.guards) {
106 // We could have only one guard policy matching the id
108 if (policy.id.equals(id)) {
110 for (Constraint cons: constraints) {
111 if (!cons.isValid()) {
112 throw new BuilderException("Invalid guard constraint - some required fields are missing");
114 if (policy.limit_constraints == null) {
115 policy.limit_constraints = new LinkedList<Constraint>();
117 policy.limit_constraints.add(cons);
122 if (exist == false) {
123 throw new BuilderException("No existing guard policy matching the id: " + id);
129 public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException {
131 throw new BuilderException("The id of target guard policy must not be null");
133 if (constraints == null) {
134 throw new BuilderException("Constraint much not be null");
136 boolean exist = false;
137 for (GuardPolicy policy: CLGuard.guards) {
139 // We could have only one guard policy matching the id
141 if (policy.id.equals(id)) {
143 for (Constraint cons: constraints) {
144 if (!cons.isValid()) {
145 throw new BuilderException("Invalid guard constraint - some required fields are missing");
147 boolean removed = policy.limit_constraints.remove(cons);
149 throw new BuilderException("Unknown guard constraint: " + cons);
155 if (exist == false) {
156 throw new BuilderException("No existing guard policy matching the id: " + id);
162 public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException {
163 if (CLGuard.guards == null || CLGuard.guards.isEmpty()) {
164 throw new BuilderException("No guard policies exist");
167 throw new BuilderException("The id of target guard policy must not be null");
169 boolean exist = false;
170 for (GuardPolicy policy: CLGuard.guards) {
171 if (policy.id.equals(id)) {
173 policy.limit_constraints.clear();
176 if (exist == false) {
177 throw new BuilderException("No existing guard policy matching the id: " + id);
183 private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
185 public ResultsImpl results = new ResultsImpl();
188 public boolean onWarning(String message) {
189 results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
194 public boolean onError(String message) {
195 results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
201 public ControlLoopGuard getControlLoopGuard() {
202 ControlLoopGuard guard = new ControlLoopGuard(this.CLGuard);
208 public Results buildSpecification() {
210 // Dump the specification
212 DumperOptions options = new DumperOptions();
213 options.setDefaultFlowStyle(FlowStyle.BLOCK);
214 options.setPrettyFlow(true);
215 Yaml yaml = new Yaml(options);
216 String dumpedYaml = yaml.dump(CLGuard);
218 // This is our callback class for our compiler
220 BuilderCompilerCallback callback = new BuilderCompilerCallback();
225 ControlLoopGuardCompiler.compile(CLGuard, callback);
226 } catch (CompilerException e) {
227 callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
232 callback.results.setSpecification(dumpedYaml);
233 return callback.results;