2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 boolean exist = false;
116 for (GuardPolicy policy: clGuard.getGuards()) {
118 // We could have only one guard policy matching the id
120 if (policy.getId().equals(id)) {
122 for (Constraint cons: constraints) {
123 if (!cons.isValid()) {
124 throw new BuilderException("Invalid guard constraint - some required fields are missing");
126 if (policy.getLimit_constraints() == null) {
127 policy.setLimit_constraints(new LinkedList<>());
129 policy.getLimit_constraints().add(cons);
138 public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException {
140 throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
142 if (constraints == null) {
143 throw new BuilderException("Constraint much not be null");
145 if (!removeConstraints(id, constraints)) {
146 throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
151 private boolean removeConstraints(String id, Constraint... constraints) throws BuilderException {
152 boolean exist = false;
153 for (GuardPolicy policy: clGuard.getGuards()) {
155 // We could have only one guard policy matching the id
157 if (policy.getId().equals(id)) {
159 for (Constraint cons: constraints) {
160 if (!cons.isValid()) {
161 throw new BuilderException("Invalid guard constraint - some required fields are missing");
163 boolean removed = policy.getLimit_constraints().remove(cons);
165 throw new BuilderException("Unknown guard constraint: " + cons);
175 public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException {
176 if (clGuard.getGuards() == null || clGuard.getGuards().isEmpty()) {
177 throw new BuilderException("No guard policies exist");
180 throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
182 boolean exist = false;
183 for (GuardPolicy policy: clGuard.getGuards()) {
184 if (policy.getId().equals(id)) {
186 policy.getLimit_constraints().clear();
190 throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
196 private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
198 private ResultsImpl results = new ResultsImpl();
201 public boolean onWarning(String message) {
202 results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
207 public boolean onError(String message) {
208 results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
214 public ControlLoopGuard getControlLoopGuard() {
215 return new ControlLoopGuard(this.clGuard);
220 public Results buildSpecification() {
222 // Dump the specification
224 DumperOptions options = new DumperOptions();
225 options.setDefaultFlowStyle(FlowStyle.BLOCK);
226 options.setPrettyFlow(true);
227 Yaml yaml = new Yaml(options);
228 String dumpedYaml = yaml.dump(clGuard);
230 // This is our callback class for our compiler
232 BuilderCompilerCallback callback = new BuilderCompilerCallback();
237 ControlLoopGuardCompiler.compile(clGuard, callback);
238 } catch (CompilerException e) {
239 logger.error("Build specification threw ", e);
240 callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
245 callback.results.setSpecification(dumpedYaml);
246 return callback.results;