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 final String NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID = "No existing guard policy matching the id: ";
\r
46 private static final String THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL = "The id of target guard policy must not be null";
\r
47 private static Logger logger = LoggerFactory.getLogger(ControlLoopGuardBuilderImpl.class.getName());
\r
48 private ControlLoopGuard cLGuard;
\r
50 public ControlLoopGuardBuilderImpl(Guard guard) {
\r
51 cLGuard = new ControlLoopGuard();
\r
52 cLGuard.setGuard(guard);
\r
56 public ControlLoopGuardBuilder addGuardPolicy(GuardPolicy... policies) throws BuilderException {
\r
57 if (policies == null) {
\r
58 throw new BuilderException("GuardPolicy must not be null");
\r
60 for (GuardPolicy policy : policies) {
\r
61 if (!policy.isValid()) {
\r
62 throw new BuilderException("Invalid guard policy - some required fields are missing");
\r
64 if (cLGuard.getGuards() == null) {
\r
65 cLGuard.setGuards(new LinkedList<>());
\r
67 cLGuard.getGuards().add(policy);
\r
73 public ControlLoopGuardBuilder removeGuardPolicy(GuardPolicy... policies) throws BuilderException {
\r
74 if (policies == null) {
\r
75 throw new BuilderException("GuardPolicy must not be null");
\r
77 if (cLGuard.getGuards() == null) {
\r
78 throw new BuilderException("No existing guard policies to remove");
\r
80 for (GuardPolicy policy : policies) {
\r
81 if (!policy.isValid()) {
\r
82 throw new BuilderException("Invalid guard policy - some required fields are missing");
\r
84 boolean removed = cLGuard.getGuards().remove(policy);
\r
86 throw new BuilderException("Unknown guard policy: " + policy.getName());
\r
93 public ControlLoopGuardBuilder removeAllGuardPolicies() throws BuilderException {
\r
94 cLGuard.getGuards().clear();
\r
99 public ControlLoopGuardBuilder addLimitConstraint(String id, Constraint... constraints) throws BuilderException {
\r
101 throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
\r
103 if (constraints == null) {
\r
104 throw new BuilderException("Constraint much not be null");
\r
106 if (!addLimitConstraints(id,constraints)) {
\r
107 throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
\r
112 private boolean addLimitConstraints(String id, Constraint... constraints) throws BuilderException {
\r
113 boolean exist = false;
\r
114 for (GuardPolicy policy: cLGuard.getGuards()) {
\r
116 // We could have only one guard policy matching the id
\r
118 if (policy.getId().equals(id)) {
\r
120 for (Constraint cons: constraints) {
\r
121 if (!cons.isValid()) {
\r
122 throw new BuilderException("Invalid guard constraint - some required fields are missing");
\r
124 if (policy.getLimit_constraints() == null) {
\r
125 policy.setLimit_constraints(new LinkedList<>());
\r
127 policy.getLimit_constraints().add(cons);
\r
136 public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException {
\r
138 throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
\r
140 if (constraints == null) {
\r
141 throw new BuilderException("Constraint much not be null");
\r
143 if (!removeConstraints(id, constraints)) {
\r
144 throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
\r
149 private boolean removeConstraints(String id, Constraint... constraints) throws BuilderException {
\r
150 boolean exist = false;
\r
151 for (GuardPolicy policy: cLGuard.getGuards()) {
\r
153 // We could have only one guard policy matching the id
\r
155 if (policy.getId().equals(id)) {
\r
157 for (Constraint cons: constraints) {
\r
158 if (!cons.isValid()) {
\r
159 throw new BuilderException("Invalid guard constraint - some required fields are missing");
\r
161 boolean removed = policy.getLimit_constraints().remove(cons);
\r
163 throw new BuilderException("Unknown guard constraint: " + cons);
\r
173 public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException {
\r
174 if (cLGuard.getGuards() == null || cLGuard.getGuards().isEmpty()) {
\r
175 throw new BuilderException("No guard policies exist");
\r
178 throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
\r
180 boolean exist = false;
\r
181 for (GuardPolicy policy: cLGuard.getGuards()) {
\r
182 if (policy.getId().equals(id)) {
\r
184 policy.getLimit_constraints().clear();
\r
188 throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
\r
194 private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
\r
196 private ResultsImpl results = new ResultsImpl();
\r
199 public boolean onWarning(String message) {
\r
200 results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
\r
205 public boolean onError(String message) {
\r
206 results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
\r
212 public ControlLoopGuard getControlLoopGuard() {
\r
213 return new ControlLoopGuard(this.cLGuard);
\r
218 public Results buildSpecification() {
\r
220 // Dump the specification
\r
222 DumperOptions options = new DumperOptions();
\r
223 options.setDefaultFlowStyle(FlowStyle.BLOCK);
\r
224 options.setPrettyFlow(true);
\r
225 Yaml yaml = new Yaml(options);
\r
226 String dumpedYaml = yaml.dump(cLGuard);
\r
228 // This is our callback class for our compiler
\r
230 BuilderCompilerCallback callback = new BuilderCompilerCallback();
\r
235 ControlLoopGuardCompiler.compile(cLGuard, callback);
\r
236 } catch (CompilerException e) {
\r
237 logger.error(e.getMessage() + e);
\r
238 callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
\r
243 callback.results.setSpecification(dumpedYaml);
\r
244 return callback.results;
\r