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.common.logging.flexlogger.FlexLogger;
25 import org.openecomp.policy.common.logging.flexlogger.Logger;
26 import org.openecomp.policy.controlloop.compiler.CompilerException;
27 import org.openecomp.policy.controlloop.compiler.ControlLoopCompilerCallback;
28 import org.openecomp.policy.controlloop.guard.compiler.ControlLoopGuardCompiler;
29 import org.openecomp.policy.controlloop.policy.builder.BuilderException;
30 import org.openecomp.policy.controlloop.policy.builder.MessageLevel;
31 import org.openecomp.policy.controlloop.policy.builder.Results;
32 import org.openecomp.policy.controlloop.policy.builder.impl.MessageImpl;
33 import org.openecomp.policy.controlloop.policy.builder.impl.ResultsImpl;
34 import org.openecomp.policy.controlloop.policy.guard.Constraint;
35 import org.openecomp.policy.controlloop.policy.guard.ControlLoopGuard;
36 import org.openecomp.policy.controlloop.policy.guard.Guard;
37 import org.openecomp.policy.controlloop.policy.guard.GuardPolicy;
38 import org.openecomp.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder;
39 import org.yaml.snakeyaml.DumperOptions;
40 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
41 import org.yaml.snakeyaml.Yaml;
43 public class ControlLoopGuardBuilderImpl implements ControlLoopGuardBuilder {
44 private static Logger logger = FlexLogger.getLogger(ControlLoopGuardBuilderImpl.class.getName());
45 private ControlLoopGuard cLGuard;
47 public ControlLoopGuardBuilderImpl(Guard guard) {
48 cLGuard = new ControlLoopGuard();
49 cLGuard.setGuard(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.getGuards() == null) {
62 cLGuard.setGuards(new LinkedList<>());
64 cLGuard.getGuards().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.getGuards() == 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.getGuards().remove(policy);
83 throw new BuilderException("Unknown guard policy: " + policy.getName());
90 public ControlLoopGuardBuilder removeAllGuardPolicies() throws BuilderException {
91 cLGuard.getGuards().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 if (!addLimitConstraints(id,constraints)) {
104 throw new BuilderException("No existing guard policy matching the id: " + id);
109 private boolean addLimitConstraints(String id, Constraint... constraints) throws BuilderException {
110 boolean exist = false;
111 for (GuardPolicy policy: cLGuard.getGuards()) {
113 // We could have only one guard policy matching the id
115 if (policy.getId().equals(id)) {
117 for (Constraint cons: constraints) {
118 if (!cons.isValid()) {
119 throw new BuilderException("Invalid guard constraint - some required fields are missing");
121 if (policy.getLimit_constraints() == null) {
122 policy.setLimit_constraints(new LinkedList<>());
124 policy.getLimit_constraints().add(cons);
133 public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException {
135 throw new BuilderException("The id of target guard policy must not be null");
137 if (constraints == null) {
138 throw new BuilderException("Constraint much not be null");
140 if (!removeConstraints(id, constraints)) {
141 throw new BuilderException("No existing guard policy matching the id: " + id);
146 private boolean removeConstraints(String id, Constraint... constraints) throws BuilderException {
147 boolean exist = false;
148 for (GuardPolicy policy: cLGuard.getGuards()) {
150 // We could have only one guard policy matching the id
152 if (policy.getId().equals(id)) {
154 for (Constraint cons: constraints) {
155 if (!cons.isValid()) {
156 throw new BuilderException("Invalid guard constraint - some required fields are missing");
158 boolean removed = policy.getLimit_constraints().remove(cons);
160 throw new BuilderException("Unknown guard constraint: " + cons);
170 public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException {
171 if (cLGuard.getGuards() == null || cLGuard.getGuards().isEmpty()) {
172 throw new BuilderException("No guard policies exist");
175 throw new BuilderException("The id of target guard policy must not be null");
177 boolean exist = false;
178 for (GuardPolicy policy: cLGuard.getGuards()) {
179 if (policy.getId().equals(id)) {
181 policy.getLimit_constraints().clear();
185 throw new BuilderException("No existing guard policy matching the id: " + id);
191 private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
193 private ResultsImpl results = new ResultsImpl();
196 public boolean onWarning(String message) {
197 results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
202 public boolean onError(String message) {
203 results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
209 public ControlLoopGuard getControlLoopGuard() {
210 return new ControlLoopGuard(this.cLGuard);
215 public Results buildSpecification() {
217 // Dump the specification
219 DumperOptions options = new DumperOptions();
220 options.setDefaultFlowStyle(FlowStyle.BLOCK);
221 options.setPrettyFlow(true);
222 Yaml yaml = new Yaml(options);
223 String dumpedYaml = yaml.dump(cLGuard);
225 // This is our callback class for our compiler
227 BuilderCompilerCallback callback = new BuilderCompilerCallback();
232 ControlLoopGuardCompiler.compile(cLGuard, callback);
233 } catch (CompilerException e) {
234 logger.error(e.getMessage() + e);
235 callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
240 callback.results.setSpecification(dumpedYaml);
241 return callback.results;