2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.policy.guard.builder.impl;
24 import java.util.LinkedList;
26 import org.onap.policy.controlloop.compiler.CompilerException;
27 import org.onap.policy.controlloop.compiler.ControlLoopCompilerCallback;
28 import org.onap.policy.controlloop.guard.compiler.ControlLoopGuardCompiler;
29 import org.onap.policy.controlloop.policy.builder.BuilderException;
30 import org.onap.policy.controlloop.policy.builder.MessageLevel;
31 import org.onap.policy.controlloop.policy.builder.Results;
32 import org.onap.policy.controlloop.policy.builder.impl.MessageImpl;
33 import org.onap.policy.controlloop.policy.builder.impl.ResultsImpl;
34 import org.onap.policy.controlloop.policy.guard.Constraint;
35 import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
36 import org.onap.policy.controlloop.policy.guard.Guard;
37 import org.onap.policy.controlloop.policy.guard.GuardPolicy;
38 import org.onap.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.yaml.snakeyaml.DumperOptions;
42 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
43 import org.yaml.snakeyaml.Yaml;
45 public class ControlLoopGuardBuilderImpl implements ControlLoopGuardBuilder {
46 private static final String NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID = "No existing guard policy matching the id: ";
47 private static final String THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL =
48 "The id of target guard policy must not be null";
49 private static Logger logger = LoggerFactory.getLogger(ControlLoopGuardBuilderImpl.class.getName());
50 private ControlLoopGuard clGuard;
52 public ControlLoopGuardBuilderImpl(Guard guard) {
53 clGuard = new ControlLoopGuard();
54 clGuard.setGuard(guard);
58 public ControlLoopGuardBuilder addGuardPolicy(GuardPolicy... policies) throws BuilderException {
59 if (policies == null) {
60 throw new BuilderException("GuardPolicy must not be null");
62 for (GuardPolicy policy : policies) {
63 if (!policy.isValid()) {
64 throw new BuilderException("Invalid guard policy - some required fields are missing");
66 if (clGuard.getGuards() == null) {
67 clGuard.setGuards(new LinkedList<>());
69 clGuard.getGuards().add(policy);
75 public ControlLoopGuardBuilder removeGuardPolicy(GuardPolicy... policies) throws BuilderException {
76 if (policies == null) {
77 throw new BuilderException("GuardPolicy must not be null");
79 if (clGuard.getGuards() == null) {
80 throw new BuilderException("No existing guard policies to remove");
82 for (GuardPolicy policy : policies) {
83 if (!policy.isValid()) {
84 throw new BuilderException("Invalid guard policy - some required fields are missing");
86 boolean removed = clGuard.getGuards().remove(policy);
88 throw new BuilderException("Unknown guard policy: " + policy.getName());
95 public ControlLoopGuardBuilder removeAllGuardPolicies() throws BuilderException {
96 clGuard.getGuards().clear();
101 public ControlLoopGuardBuilder addLimitConstraint(String id, Constraint... constraints) throws BuilderException {
103 throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
105 if (constraints == null) {
106 throw new BuilderException("Constraint much not be null");
108 if (!addLimitConstraints(id,constraints)) {
109 throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
114 private boolean addLimitConstraints(String id, Constraint... constraints) throws BuilderException {
115 for (GuardPolicy policy: clGuard.getGuards()) {
117 // We could have only one guard policy matching the id
119 if (policy.getId().equals(id)) {
120 addConstraints(policy, constraints);
127 private void addConstraints(GuardPolicy policy, Constraint... constraints) throws BuilderException {
128 for (Constraint cons: constraints) {
129 if (!cons.isValid()) {
130 throw new BuilderException("Invalid guard constraint - some required fields are missing");
132 if (policy.getLimit_constraints() == null) {
133 policy.setLimit_constraints(new LinkedList<>());
135 policy.getLimit_constraints().add(cons);
140 public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException {
142 throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
144 if (constraints == null) {
145 throw new BuilderException("Constraint much not be null");
147 if (!removeConstraints(id, constraints)) {
148 throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
153 private boolean removeConstraints(String id, Constraint... constraints) throws BuilderException {
154 for (GuardPolicy policy: clGuard.getGuards()) {
156 // We could have only one guard policy matching the id
158 if (policy.getId().equals(id)) {
159 removeConstraints(policy, constraints);
166 private void removeConstraints(GuardPolicy policy, Constraint... constraints) throws BuilderException {
167 for (Constraint cons: constraints) {
168 if (!cons.isValid()) {
169 throw new BuilderException("Invalid guard constraint - some required fields are missing");
171 boolean removed = policy.getLimit_constraints().remove(cons);
173 throw new BuilderException("Unknown guard constraint: " + cons);
179 public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException {
180 if (clGuard.getGuards() == null || clGuard.getGuards().isEmpty()) {
181 throw new BuilderException("No guard policies exist");
184 throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
186 boolean exist = false;
187 for (GuardPolicy policy: clGuard.getGuards()) {
188 if (policy.getId().equals(id)) {
190 policy.getLimit_constraints().clear();
194 throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
200 private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
202 private ResultsImpl results = new ResultsImpl();
205 public boolean onWarning(String message) {
206 results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
211 public boolean onError(String message) {
212 results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
218 public ControlLoopGuard getControlLoopGuard() {
219 return new ControlLoopGuard(this.clGuard);
224 public Results buildSpecification() {
226 // Dump the specification
228 DumperOptions options = new DumperOptions();
229 options.setDefaultFlowStyle(FlowStyle.BLOCK);
230 options.setPrettyFlow(true);
231 Yaml yaml = new Yaml(options);
232 String dumpedYaml = yaml.dump(clGuard);
234 // This is our callback class for our compiler
236 BuilderCompilerCallback callback = new BuilderCompilerCallback();
241 ControlLoopGuardCompiler.compile(clGuard, callback);
242 } catch (CompilerException e) {
243 logger.error("Build specification threw ", e);
244 callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
249 callback.results.setSpecification(dumpedYaml);
250 return callback.results;