2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
21 package org.onap.policy.controlloop.policy.guard.builder.impl;
\r
23 import java.util.LinkedList;
\r
25 import org.onap.policy.controlloop.compiler.CompilerException;
\r
26 import org.onap.policy.controlloop.compiler.ControlLoopCompilerCallback;
\r
27 import org.onap.policy.controlloop.guard.compiler.ControlLoopGuardCompiler;
\r
28 import org.onap.policy.controlloop.policy.builder.BuilderException;
\r
29 import org.onap.policy.controlloop.policy.builder.MessageLevel;
\r
30 import org.onap.policy.controlloop.policy.builder.Results;
\r
31 import org.onap.policy.controlloop.policy.builder.impl.MessageImpl;
\r
32 import org.onap.policy.controlloop.policy.builder.impl.ResultsImpl;
\r
33 import org.onap.policy.controlloop.policy.guard.Constraint;
\r
34 import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
\r
35 import org.onap.policy.controlloop.policy.guard.Guard;
\r
36 import org.onap.policy.controlloop.policy.guard.GuardPolicy;
\r
37 import org.onap.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder;
\r
38 import org.slf4j.Logger;
\r
39 import org.slf4j.LoggerFactory;
\r
40 import org.yaml.snakeyaml.DumperOptions;
\r
41 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
\r
42 import org.yaml.snakeyaml.Yaml;
\r
44 public class ControlLoopGuardBuilderImpl implements ControlLoopGuardBuilder {
\r
45 private static Logger logger = LoggerFactory.getLogger(ControlLoopGuardBuilderImpl.class.getName());
\r
46 private ControlLoopGuard cLGuard;
\r
48 public ControlLoopGuardBuilderImpl(Guard guard) {
\r
49 cLGuard = new ControlLoopGuard();
\r
50 cLGuard.setGuard(guard);
\r
54 public ControlLoopGuardBuilder addGuardPolicy(GuardPolicy... policies) throws BuilderException {
\r
55 if (policies == null) {
\r
56 throw new BuilderException("GuardPolicy must not be null");
\r
58 for (GuardPolicy policy : policies) {
\r
59 if (!policy.isValid()) {
\r
60 throw new BuilderException("Invalid guard policy - some required fields are missing");
\r
62 if (cLGuard.getGuards() == null) {
\r
63 cLGuard.setGuards(new LinkedList<>());
\r
65 cLGuard.getGuards().add(policy);
\r
71 public ControlLoopGuardBuilder removeGuardPolicy(GuardPolicy... policies) throws BuilderException {
\r
72 if (policies == null) {
\r
73 throw new BuilderException("GuardPolicy must not be null");
\r
75 if (cLGuard.getGuards() == null) {
\r
76 throw new BuilderException("No existing guard policies to remove");
\r
78 for (GuardPolicy policy : policies) {
\r
79 if (!policy.isValid()) {
\r
80 throw new BuilderException("Invalid guard policy - some required fields are missing");
\r
82 boolean removed = cLGuard.getGuards().remove(policy);
\r
84 throw new BuilderException("Unknown guard policy: " + policy.getName());
\r
91 public ControlLoopGuardBuilder removeAllGuardPolicies() throws BuilderException {
\r
92 cLGuard.getGuards().clear();
\r
97 public ControlLoopGuardBuilder addLimitConstraint(String id, Constraint... constraints) throws BuilderException {
\r
99 throw new BuilderException("The id of target guard policy must not be null");
\r
101 if (constraints == null) {
\r
102 throw new BuilderException("Constraint much not be null");
\r
104 if (!addLimitConstraints(id,constraints)) {
\r
105 throw new BuilderException("No existing guard policy matching the id: " + id);
\r
110 private boolean addLimitConstraints(String id, Constraint... constraints) throws BuilderException {
\r
111 boolean exist = false;
\r
112 for (GuardPolicy policy: cLGuard.getGuards()) {
\r
114 // We could have only one guard policy matching the id
\r
116 if (policy.getId().equals(id)) {
\r
118 for (Constraint cons: constraints) {
\r
119 if (!cons.isValid()) {
\r
120 throw new BuilderException("Invalid guard constraint - some required fields are missing");
\r
122 if (policy.getLimit_constraints() == null) {
\r
123 policy.setLimit_constraints(new LinkedList<>());
\r
125 policy.getLimit_constraints().add(cons);
\r
134 public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException {
\r
136 throw new BuilderException("The id of target guard policy must not be null");
\r
138 if (constraints == null) {
\r
139 throw new BuilderException("Constraint much not be null");
\r
141 if (!removeConstraints(id, constraints)) {
\r
142 throw new BuilderException("No existing guard policy matching the id: " + id);
\r
147 private boolean removeConstraints(String id, Constraint... constraints) throws BuilderException {
\r
148 boolean exist = false;
\r
149 for (GuardPolicy policy: cLGuard.getGuards()) {
\r
151 // We could have only one guard policy matching the id
\r
153 if (policy.getId().equals(id)) {
\r
155 for (Constraint cons: constraints) {
\r
156 if (!cons.isValid()) {
\r
157 throw new BuilderException("Invalid guard constraint - some required fields are missing");
\r
159 boolean removed = policy.getLimit_constraints().remove(cons);
\r
161 throw new BuilderException("Unknown guard constraint: " + cons);
\r
171 public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException {
\r
172 if (cLGuard.getGuards() == null || cLGuard.getGuards().isEmpty()) {
\r
173 throw new BuilderException("No guard policies exist");
\r
176 throw new BuilderException("The id of target guard policy must not be null");
\r
178 boolean exist = false;
\r
179 for (GuardPolicy policy: cLGuard.getGuards()) {
\r
180 if (policy.getId().equals(id)) {
\r
182 policy.getLimit_constraints().clear();
\r
186 throw new BuilderException("No existing guard policy matching the id: " + id);
\r
192 private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
\r
194 private ResultsImpl results = new ResultsImpl();
\r
197 public boolean onWarning(String message) {
\r
198 results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
\r
203 public boolean onError(String message) {
\r
204 results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
\r
210 public ControlLoopGuard getControlLoopGuard() {
\r
211 return new ControlLoopGuard(this.cLGuard);
\r
216 public Results buildSpecification() {
\r
218 // Dump the specification
\r
220 DumperOptions options = new DumperOptions();
\r
221 options.setDefaultFlowStyle(FlowStyle.BLOCK);
\r
222 options.setPrettyFlow(true);
\r
223 Yaml yaml = new Yaml(options);
\r
224 String dumpedYaml = yaml.dump(cLGuard);
\r
226 // This is our callback class for our compiler
\r
228 BuilderCompilerCallback callback = new BuilderCompilerCallback();
\r
233 ControlLoopGuardCompiler.compile(cLGuard, callback);
\r
234 } catch (CompilerException e) {
\r
235 logger.error(e.getMessage() + e);
\r
236 callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
\r
241 callback.results.setSpecification(dumpedYaml);
\r
242 return callback.results;
\r