Changes for Checkstyle 8.32
[policy/models.git] / models-interactions / model-yaml / src / main / java / org / onap / policy / controlloop / policy / guard / builder / impl / ControlLoopGuardBuilderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-yaml
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop.policy.guard.builder.impl;
23
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;
43
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;
50
51     public ControlLoopGuardBuilderImpl(Guard guard) {
52         clGuard = new ControlLoopGuard();
53         clGuard.setGuard(guard);
54     }
55
56     @Override
57     public ControlLoopGuardBuilder addGuardPolicy(GuardPolicy... policies) throws BuilderException {
58         if (policies == null) {
59             throw new BuilderException("GuardPolicy must not be null");
60         }
61         for (GuardPolicy policy : policies) {
62             if (!policy.isValid()) {
63                 throw new BuilderException("Invalid guard policy - some required fields are missing");
64             }
65             if (clGuard.getGuards() == null) {
66                 clGuard.setGuards(new LinkedList<>());
67             }
68             clGuard.getGuards().add(policy);
69         }
70         return this;
71     }
72
73     @Override
74     public ControlLoopGuardBuilder removeGuardPolicy(GuardPolicy... policies) throws BuilderException {
75         if (policies == null) {
76             throw new BuilderException("GuardPolicy must not be null");
77         }
78         if (clGuard.getGuards() == null) {
79             throw new BuilderException("No existing guard policies to remove");
80         }
81         for (GuardPolicy policy : policies) {
82             if (!policy.isValid()) {
83                 throw new BuilderException("Invalid guard policy - some required fields are missing");
84             }
85             boolean removed = clGuard.getGuards().remove(policy);
86             if (!removed) {
87                 throw new BuilderException("Unknown guard policy: " + policy.getName());
88             }
89         }
90         return this;
91     }
92
93     @Override
94     public ControlLoopGuardBuilder removeAllGuardPolicies() throws BuilderException {
95         clGuard.getGuards().clear();
96         return this;
97     }
98
99     @Override
100     public ControlLoopGuardBuilder addLimitConstraint(String id, Constraint... constraints) throws BuilderException {
101         if (id == null) {
102             throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
103         }
104         if (constraints == null) {
105             throw new BuilderException("Constraint much not be null");
106         }
107         if (!addLimitConstraints(id, constraints)) {
108             throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
109         }
110         return this;
111     }
112
113     private boolean addLimitConstraints(String id, Constraint... constraints) throws BuilderException {
114         for (GuardPolicy policy: clGuard.getGuards()) {
115             //
116             // We could have only one guard policy matching the id
117             //
118             if (policy.getId().equals(id)) {
119                 addConstraints(policy, constraints);
120                 return true;
121             }
122         }
123         return false;
124     }
125
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");
130             }
131             if (policy.getLimit_constraints() == null) {
132                 policy.setLimit_constraints(new LinkedList<>());
133             }
134             policy.getLimit_constraints().add(cons);
135         }
136     }
137
138     @Override
139     public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException {
140         if (id == null) {
141             throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
142         }
143         if (constraints == null) {
144             throw new BuilderException("Constraint much not be null");
145         }
146         if (!removeConstraints(id, constraints)) {
147             throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
148         }
149         return this;
150     }
151
152     private boolean removeConstraints(String id, Constraint... constraints) throws BuilderException {
153         for (GuardPolicy policy: clGuard.getGuards()) {
154             //
155             // We could have only one guard policy matching the id
156             //
157             if (policy.getId().equals(id)) {
158                 removeConstraints(policy, constraints);
159                 return true;
160             }
161         }
162         return false;
163     }
164
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");
169             }
170             boolean removed = policy.getLimit_constraints().remove(cons);
171             if (!removed) {
172                 throw new BuilderException("Unknown guard constraint: " + cons);
173             }
174         }
175     }
176
177     @Override
178     public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException {
179         if (clGuard.getGuards() == null || clGuard.getGuards().isEmpty()) {
180             throw new BuilderException("No guard policies exist");
181         }
182         if (id == null) {
183             throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
184         }
185         boolean exist = false;
186         for (GuardPolicy policy: clGuard.getGuards()) {
187             if (policy.getId().equals(id)) {
188                 exist = true;
189                 policy.getLimit_constraints().clear();
190             }
191         }
192         if (!exist) {
193             throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
194         }
195         return this;
196     }
197
198
199     private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
200
201         private ResultsImpl results = new ResultsImpl();
202
203         @Override
204         public boolean onWarning(String message) {
205             results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
206             return false;
207         }
208
209         @Override
210         public boolean onError(String message) {
211             results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
212             return false;
213         }
214     }
215
216     @Override
217     public ControlLoopGuard getControlLoopGuard() {
218         return new ControlLoopGuard(this.clGuard);
219     }
220
221
222     @Override
223     public Results buildSpecification() {
224         //
225         // Dump the specification
226         //
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);
232         //
233         // This is our callback class for our compiler
234         //
235         BuilderCompilerCallback callback = new BuilderCompilerCallback();
236         //
237         // Compile it
238         //
239         try {
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));
244         }
245         //
246         // Save the spec
247         //
248         callback.results.setSpecification(dumpedYaml);
249         return callback.results;
250     }
251
252 }