Merge "Add debugging of REST call"
[policy/drools-applications.git] / controlloop / common / policy-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-2018 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.policy.guard.builder.impl;
22
23 import java.util.LinkedList;
24
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         boolean exist = false;
115         for (GuardPolicy policy: clGuard.getGuards()) {
116             //
117             // We could have only one guard policy matching the id
118             //
119             if (policy.getId().equals(id)) {
120                 exist = true;
121                 for (Constraint cons: constraints) {
122                     if (!cons.isValid()) {
123                         throw new BuilderException("Invalid guard constraint - some required fields are missing");
124                     }
125                     if (policy.getLimit_constraints() == null) {
126                         policy.setLimit_constraints(new LinkedList<>());
127                     }
128                     policy.getLimit_constraints().add(cons);
129                 }
130                 break;
131             }
132         }
133         return exist;
134     }
135
136     @Override
137     public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException {
138         if (id == null) {
139             throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
140         }
141         if (constraints == null) {
142             throw new BuilderException("Constraint much not be null");
143         }
144         if (!removeConstraints(id, constraints)) {
145             throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
146         }
147         return this;
148     }
149
150     private boolean removeConstraints(String id, Constraint... constraints) throws BuilderException {
151         boolean exist = false;
152         for (GuardPolicy policy: clGuard.getGuards()) {
153             //
154             // We could have only one guard policy matching the id
155             //
156             if (policy.getId().equals(id)) {
157                 exist = true;
158                 for (Constraint cons: constraints) {
159                     if (!cons.isValid()) {
160                         throw new BuilderException("Invalid guard constraint - some required fields are missing");
161                     }
162                     boolean removed = policy.getLimit_constraints().remove(cons);
163                     if (!removed) {
164                         throw new BuilderException("Unknown guard constraint: " + cons);
165                     }
166                 }
167                 break;
168             }
169         }
170         return exist;
171     }
172
173     @Override
174     public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException {
175         if (clGuard.getGuards() == null || clGuard.getGuards().isEmpty()) {
176             throw new BuilderException("No guard policies exist");
177         } 
178         if (id == null) {
179             throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
180         }
181         boolean exist = false;
182         for (GuardPolicy policy: clGuard.getGuards()) {
183             if (policy.getId().equals(id)) {
184                 exist = true;
185                 policy.getLimit_constraints().clear();
186             }
187         }
188         if (!exist) {
189             throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
190         }
191         return this;
192     }
193
194     
195     private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
196
197         private ResultsImpl results = new ResultsImpl();
198         
199         @Override
200         public boolean onWarning(String message) {
201             results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
202             return false;
203         }
204
205         @Override
206         public boolean onError(String message) {
207             results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
208             return false;
209         }
210     }
211     
212     @Override
213     public ControlLoopGuard getControlLoopGuard() {
214         return new ControlLoopGuard(this.clGuard);
215     }   
216     
217     
218     @Override
219     public Results buildSpecification() {
220         //
221         // Dump the specification
222         //
223         DumperOptions options = new DumperOptions();
224         options.setDefaultFlowStyle(FlowStyle.BLOCK);
225         options.setPrettyFlow(true);
226         Yaml yaml = new Yaml(options);
227         String dumpedYaml = yaml.dump(clGuard);
228         //
229         // This is our callback class for our compiler
230         //
231         BuilderCompilerCallback callback = new BuilderCompilerCallback();
232         //
233         // Compile it
234         //
235         try {
236             ControlLoopGuardCompiler.compile(clGuard, callback);
237         } catch (CompilerException e) {
238             logger.error("Build specification threw ", e);
239             callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
240         }
241         //
242         // Save the spec
243         //
244         callback.results.setSpecification(dumpedYaml);
245         return callback.results;
246     }
247
248 }