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.guard = 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.guards == null) {
60 CLGuard.guards = new LinkedList<GuardPolicy>();
62 CLGuard.guards.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.guards == 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.guards.remove(policy);
81 throw new BuilderException("Unknown guard policy: " + policy.name);
88 public ControlLoopGuardBuilder removeAllGuardPolicies() throws BuilderException {
89 CLGuard.guards.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 boolean exist = false;
102 for (GuardPolicy policy: CLGuard.guards) {
104 // We could have only one guard policy matching the id
106 if (policy.id.equals(id)) {
108 for (Constraint cons: constraints) {
109 if (!cons.isValid()) {
110 throw new BuilderException("Invalid guard constraint - some required fields are missing");
112 if (policy.limit_constraints == null) {
113 policy.limit_constraints = new LinkedList<Constraint>();
115 policy.limit_constraints.add(cons);
120 if (exist == false) {
121 throw new BuilderException("No existing guard policy matching the id: " + id);
127 public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException {
129 throw new BuilderException("The id of target guard policy must not be null");
131 if (constraints == null) {
132 throw new BuilderException("Constraint much not be null");
134 boolean exist = false;
135 for (GuardPolicy policy: CLGuard.guards) {
137 // We could have only one guard policy matching the id
139 if (policy.id.equals(id)) {
141 for (Constraint cons: constraints) {
142 if (!cons.isValid()) {
143 throw new BuilderException("Invalid guard constraint - some required fields are missing");
145 boolean removed = policy.limit_constraints.remove(cons);
147 throw new BuilderException("Unknown guard constraint: " + cons);
153 if (exist == false) {
154 throw new BuilderException("No existing guard policy matching the id: " + id);
160 public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException {
161 if (CLGuard.guards == null || CLGuard.guards.isEmpty()) {
162 throw new BuilderException("No guard policies exist");
165 throw new BuilderException("The id of target guard policy must not be null");
167 boolean exist = false;
168 for (GuardPolicy policy: CLGuard.guards) {
169 if (policy.id.equals(id)) {
171 policy.limit_constraints.clear();
174 if (exist == false) {
175 throw new BuilderException("No existing guard policy matching the id: " + id);
181 private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
183 public ResultsImpl results = new ResultsImpl();
186 public boolean onWarning(String message) {
187 results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
192 public boolean onError(String message) {
193 results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
199 public ControlLoopGuard getControlLoopGuard() {
200 ControlLoopGuard guard = new ControlLoopGuard(this.CLGuard);
206 public Results buildSpecification() {
208 // Dump the specification
210 DumperOptions options = new DumperOptions();
211 options.setDefaultFlowStyle(FlowStyle.BLOCK);
212 options.setPrettyFlow(true);
213 Yaml yaml = new Yaml(options);
214 String dumpedYaml = yaml.dump(CLGuard);
216 // This is our callback class for our compiler
218 BuilderCompilerCallback callback = new BuilderCompilerCallback();
223 ControlLoopGuardCompiler.compile(CLGuard, callback);
224 } catch (CompilerException e) {
225 callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
230 callback.results.setSpecification(dumpedYaml);
231 return callback.results;