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