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=========================================================
20 package org.openecomp.policy.controlloop.policy.guard.builder.impl;
22 import java.util.LinkedList;
24 import org.openecomp.policy.controlloop.compiler.CompilerException;
25 import org.openecomp.policy.controlloop.compiler.ControlLoopCompilerCallback;
26 import org.openecomp.policy.controlloop.guard.compiler.ControlLoopGuardCompiler;
27 import org.openecomp.policy.controlloop.policy.builder.BuilderException;
28 import org.openecomp.policy.controlloop.policy.builder.MessageLevel;
29 import org.openecomp.policy.controlloop.policy.builder.Results;
30 import org.openecomp.policy.controlloop.policy.builder.impl.MessageImpl;
31 import org.openecomp.policy.controlloop.policy.builder.impl.ResultsImpl;
32 import org.openecomp.policy.controlloop.policy.guard.Constraint;
33 import org.openecomp.policy.controlloop.policy.guard.ControlLoopGuard;
34 import org.openecomp.policy.controlloop.policy.guard.Guard;
35 import org.openecomp.policy.controlloop.policy.guard.GuardPolicy;
36 import org.openecomp.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder;
37 import org.yaml.snakeyaml.DumperOptions;
38 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
39 import org.yaml.snakeyaml.Yaml;
41 public class ControlLoopGuardBuilderImpl implements ControlLoopGuardBuilder {
43 private ControlLoopGuard cLGuard;
45 public ControlLoopGuardBuilderImpl(Guard guard) {
46 cLGuard = new ControlLoopGuard();
47 cLGuard.setGuard(guard);
51 public ControlLoopGuardBuilder addGuardPolicy(GuardPolicy... policies) throws BuilderException {
52 if (policies == null) {
53 throw new BuilderException("GuardPolicy must not be null");
55 for (GuardPolicy policy : policies) {
56 if (!policy.isValid()) {
57 throw new BuilderException("Invalid guard policy - some required fields are missing");
59 if (cLGuard.getGuards() == null) {
60 cLGuard.setGuards(new LinkedList<>());
62 cLGuard.getGuards().add(policy);
68 public ControlLoopGuardBuilder removeGuardPolicy(GuardPolicy... policies) throws BuilderException {
69 if (policies == null) {
70 throw new BuilderException("GuardPolicy must not be null");
72 if (cLGuard.getGuards() == null) {
73 throw new BuilderException("No existing guard policies to remove");
75 for (GuardPolicy policy : policies) {
76 if (!policy.isValid()) {
77 throw new BuilderException("Invalid guard policy - some required fields are missing");
79 boolean removed = cLGuard.getGuards().remove(policy);
81 throw new BuilderException("Unknown guard policy: " + policy.getName());
88 public ControlLoopGuardBuilder removeAllGuardPolicies() throws BuilderException {
89 cLGuard.getGuards().clear();
94 public ControlLoopGuardBuilder addLimitConstraint(String id, Constraint... constraints) throws BuilderException {
96 throw new BuilderException("The id of target guard policy must not be null");
98 if (constraints == null) {
99 throw new BuilderException("Constraint much not be null");
101 if (!addLimitConstraints(id,constraints)) {
102 throw new BuilderException("No existing guard policy matching the id: " + id);
107 private boolean addLimitConstraints(String id, Constraint... constraints) throws BuilderException {
108 boolean exist = false;
109 for (GuardPolicy policy: cLGuard.getGuards()) {
111 // We could have only one guard policy matching the id
113 if (policy.getId().equals(id)) {
115 for (Constraint cons: constraints) {
116 if (!cons.isValid()) {
117 throw new BuilderException("Invalid guard constraint - some required fields are missing");
119 if (policy.getLimit_constraints() == null) {
120 policy.setLimit_constraints(new LinkedList<>());
122 policy.getLimit_constraints().add(cons);
131 public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException {
133 throw new BuilderException("The id of target guard policy must not be null");
135 if (constraints == null) {
136 throw new BuilderException("Constraint much not be null");
138 if (!removeConstraints(id, constraints)) {
139 throw new BuilderException("No existing guard policy matching the id: " + id);
144 private boolean removeConstraints(String id, Constraint... constraints) throws BuilderException {
145 boolean exist = false;
146 for (GuardPolicy policy: cLGuard.getGuards()) {
148 // We could have only one guard policy matching the id
150 if (policy.getId().equals(id)) {
152 for (Constraint cons: constraints) {
153 if (!cons.isValid()) {
154 throw new BuilderException("Invalid guard constraint - some required fields are missing");
156 boolean removed = policy.getLimit_constraints().remove(cons);
158 throw new BuilderException("Unknown guard constraint: " + cons);
168 public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException {
169 if (cLGuard.getGuards() == null || cLGuard.getGuards().isEmpty()) {
170 throw new BuilderException("No guard policies exist");
173 throw new BuilderException("The id of target guard policy must not be null");
175 boolean exist = false;
176 for (GuardPolicy policy: cLGuard.getGuards()) {
177 if (policy.getId().equals(id)) {
179 policy.getLimit_constraints().clear();
183 throw new BuilderException("No existing guard policy matching the id: " + id);
189 private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
191 private ResultsImpl results = new ResultsImpl();
194 public boolean onWarning(String message) {
195 results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
200 public boolean onError(String message) {
201 results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
207 public ControlLoopGuard getControlLoopGuard() {
208 return new ControlLoopGuard(this.cLGuard);
213 public Results buildSpecification() {
215 // Dump the specification
217 DumperOptions options = new DumperOptions();
218 options.setDefaultFlowStyle(FlowStyle.BLOCK);
219 options.setPrettyFlow(true);
220 Yaml yaml = new Yaml(options);
221 String dumpedYaml = yaml.dump(cLGuard);
223 // This is our callback class for our compiler
225 BuilderCompilerCallback callback = new BuilderCompilerCallback();
230 ControlLoopGuardCompiler.compile(cLGuard, callback);
231 } catch (CompilerException e) {
232 callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
237 callback.results.setSpecification(dumpedYaml);
238 return callback.results;