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;
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 =
47 "The id of target guard policy must not be null";
48 private static Logger logger = LoggerFactory.getLogger(ControlLoopGuardBuilderImpl.class.getName());
49 private ControlLoopGuard clGuard;
51 public ControlLoopGuardBuilderImpl(Guard guard) {
52 clGuard = new ControlLoopGuard();
53 clGuard.setGuard(guard);
57 public ControlLoopGuardBuilder addGuardPolicy(GuardPolicy... policies) throws BuilderException {
58 if (policies == null) {
59 throw new BuilderException("GuardPolicy must not be null");
61 for (GuardPolicy policy : policies) {
62 if (!policy.isValid()) {
63 throw new BuilderException("Invalid guard policy - some required fields are missing");
65 if (clGuard.getGuards() == null) {
66 clGuard.setGuards(new LinkedList<>());
68 clGuard.getGuards().add(policy);
74 public ControlLoopGuardBuilder removeGuardPolicy(GuardPolicy... policies) throws BuilderException {
75 if (policies == null) {
76 throw new BuilderException("GuardPolicy must not be null");
78 if (clGuard.getGuards() == null) {
79 throw new BuilderException("No existing guard policies to remove");
81 for (GuardPolicy policy : policies) {
82 if (!policy.isValid()) {
83 throw new BuilderException("Invalid guard policy - some required fields are missing");
85 boolean removed = clGuard.getGuards().remove(policy);
87 throw new BuilderException("Unknown guard policy: " + policy.getName());
94 public ControlLoopGuardBuilder removeAllGuardPolicies() throws BuilderException {
95 clGuard.getGuards().clear();
100 public ControlLoopGuardBuilder addLimitConstraint(String id, Constraint... constraints) throws BuilderException {
102 throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
104 if (constraints == null) {
105 throw new BuilderException("Constraint much not be null");
107 if (!addLimitConstraints(id, constraints)) {
108 throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
113 private boolean addLimitConstraints(String id, Constraint... constraints) throws BuilderException {
114 for (GuardPolicy policy: clGuard.getGuards()) {
116 // We could have only one guard policy matching the id
118 if (policy.getId().equals(id)) {
119 addConstraints(policy, constraints);
126 private void addConstraints(GuardPolicy policy, Constraint... constraints) throws BuilderException {
127 for (Constraint cons: constraints) {
128 if (!cons.isValid()) {
129 throw new BuilderException("Invalid guard constraint - some required fields are missing");
131 if (policy.getLimit_constraints() == null) {
132 policy.setLimit_constraints(new LinkedList<>());
134 policy.getLimit_constraints().add(cons);
139 public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException {
141 throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
143 if (constraints == null) {
144 throw new BuilderException("Constraint much not be null");
146 if (!removeConstraints(id, constraints)) {
147 throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
152 private boolean removeConstraints(String id, Constraint... constraints) throws BuilderException {
153 for (GuardPolicy policy: clGuard.getGuards()) {
155 // We could have only one guard policy matching the id
157 if (policy.getId().equals(id)) {
158 removeConstraints(policy, constraints);
165 private void removeConstraints(GuardPolicy policy, Constraint... constraints) throws BuilderException {
166 for (Constraint cons: constraints) {
167 if (!cons.isValid()) {
168 throw new BuilderException("Invalid guard constraint - some required fields are missing");
170 boolean removed = policy.getLimit_constraints().remove(cons);
172 throw new BuilderException("Unknown guard constraint: " + cons);
178 public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException {
179 if (clGuard.getGuards() == null || clGuard.getGuards().isEmpty()) {
180 throw new BuilderException("No guard policies exist");
183 throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
185 boolean exist = false;
186 for (GuardPolicy policy: clGuard.getGuards()) {
187 if (policy.getId().equals(id)) {
189 policy.getLimit_constraints().clear();
193 throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
199 private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
201 private ResultsImpl results = new ResultsImpl();
204 public boolean onWarning(String message) {
205 results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
210 public boolean onError(String message) {
211 results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
217 public ControlLoopGuard getControlLoopGuard() {
218 return new ControlLoopGuard(this.clGuard);
223 public Results buildSpecification() {
225 // Dump the specification
227 DumperOptions options = new DumperOptions();
228 options.setDefaultFlowStyle(FlowStyle.BLOCK);
229 options.setPrettyFlow(true);
230 Yaml yaml = new Yaml(options);
231 String dumpedYaml = yaml.dump(clGuard);
233 // This is our callback class for our compiler
235 BuilderCompilerCallback callback = new BuilderCompilerCallback();
240 ControlLoopGuardCompiler.compile(clGuard, callback);
241 } catch (CompilerException e) {
242 logger.error("Build specification threw ", e);
243 callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
248 callback.results.setSpecification(dumpedYaml);
249 return callback.results;