a431a0bfe55b925d98b9835fabf0cd388f795f9e
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-yaml
4  * ================================================================================
5  * Copyright (C) 2017 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 = "The id of target guard policy must not be null";
47         private static Logger logger = LoggerFactory.getLogger(ControlLoopGuardBuilderImpl.class.getName());
48     private ControlLoopGuard cLGuard;
49     
50     public ControlLoopGuardBuilderImpl(Guard guard) {
51         cLGuard = new ControlLoopGuard();
52         cLGuard.setGuard(guard);
53     }
54     
55     @Override
56     public ControlLoopGuardBuilder addGuardPolicy(GuardPolicy... policies) throws BuilderException {
57         if (policies == null) {
58             throw new BuilderException("GuardPolicy must not be null");
59         }
60         for (GuardPolicy policy : policies) {
61             if (!policy.isValid()) {
62                 throw new BuilderException("Invalid guard policy - some required fields are missing");
63             }
64             if (cLGuard.getGuards() == null) {
65                 cLGuard.setGuards(new LinkedList<>());
66             }
67             cLGuard.getGuards().add(policy);
68         }
69         return this;
70     }
71
72     @Override
73     public ControlLoopGuardBuilder removeGuardPolicy(GuardPolicy... policies) throws BuilderException {
74         if (policies == null) {
75             throw new BuilderException("GuardPolicy must not be null");
76         }
77         if (cLGuard.getGuards() == null) {
78             throw new BuilderException("No existing guard policies to remove");
79         }
80         for (GuardPolicy policy : policies) {
81             if (!policy.isValid()) {
82                 throw new BuilderException("Invalid guard policy - some required fields are missing");
83             }
84             boolean removed = cLGuard.getGuards().remove(policy);
85             if (!removed) {
86                 throw new BuilderException("Unknown guard policy: " + policy.getName());
87             }
88         }
89         return this;
90     }
91
92     @Override
93     public ControlLoopGuardBuilder removeAllGuardPolicies() throws BuilderException {
94         cLGuard.getGuards().clear();
95         return this;
96     }
97
98     @Override
99     public ControlLoopGuardBuilder addLimitConstraint(String id, Constraint... constraints) throws BuilderException {
100         if (id == null) {
101             throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
102         }
103         if (constraints == null) {
104             throw new BuilderException("Constraint much not be null");
105         }
106         if (!addLimitConstraints(id,constraints)) {
107             throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
108         }
109         return this;
110     }
111
112     private boolean addLimitConstraints(String id, Constraint... constraints) throws BuilderException {
113         boolean exist = false;
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                 exist = true;
120                 for (Constraint cons: constraints) {
121                     if (!cons.isValid()) {
122                         throw new BuilderException("Invalid guard constraint - some required fields are missing");
123                     }
124                     if (policy.getLimit_constraints() == null) {
125                         policy.setLimit_constraints(new LinkedList<>());
126                     }
127                     policy.getLimit_constraints().add(cons);
128                 }
129                 break;
130             }
131         }
132         return exist;
133     }
134
135     @Override
136     public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException {
137         if (id == null) {
138             throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
139         }
140         if (constraints == null) {
141             throw new BuilderException("Constraint much not be null");
142         }
143         if (!removeConstraints(id, constraints)) {
144             throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
145         }
146         return this;
147     }
148
149     private boolean removeConstraints(String id, Constraint... constraints) throws BuilderException {
150         boolean exist = false;
151         for (GuardPolicy policy: cLGuard.getGuards()) {
152             //
153             // We could have only one guard policy matching the id
154             //
155             if (policy.getId().equals(id)) {
156                 exist = true;
157                 for (Constraint cons: constraints) {
158                     if (!cons.isValid()) {
159                         throw new BuilderException("Invalid guard constraint - some required fields are missing");
160                     }
161                     boolean removed = policy.getLimit_constraints().remove(cons);
162                     if (!removed) {
163                         throw new BuilderException("Unknown guard constraint: " + cons);
164                     }
165                 }
166                 break;
167             }
168         }
169         return exist;
170     }
171
172     @Override
173     public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException {
174         if (cLGuard.getGuards() == null || cLGuard.getGuards().isEmpty()) {
175             throw new BuilderException("No guard policies exist");
176         } 
177         if (id == null) {
178             throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
179         }
180         boolean exist = false;
181         for (GuardPolicy policy: cLGuard.getGuards()) {
182             if (policy.getId().equals(id)) {
183                 exist = true;
184                 policy.getLimit_constraints().clear();
185             }
186         }
187         if (!exist) {
188             throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
189         }
190         return this;
191     }
192
193     
194     private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
195
196         private ResultsImpl results = new ResultsImpl();
197         
198         @Override
199         public boolean onWarning(String message) {
200             results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
201             return false;
202         }
203
204         @Override
205         public boolean onError(String message) {
206             results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
207             return false;
208         }
209     }
210     
211     @Override
212     public ControlLoopGuard getControlLoopGuard() {
213         return new ControlLoopGuard(this.cLGuard);
214     }   
215     
216     
217     @Override
218     public Results buildSpecification() {
219         //
220         // Dump the specification
221         //
222         DumperOptions options = new DumperOptions();
223         options.setDefaultFlowStyle(FlowStyle.BLOCK);
224         options.setPrettyFlow(true);
225         Yaml yaml = new Yaml(options);
226         String dumpedYaml = yaml.dump(cLGuard);
227         //
228         // This is our callback class for our compiler
229         //
230         BuilderCompilerCallback callback = new BuilderCompilerCallback();
231         //
232         // Compile it
233         //
234         try {
235             ControlLoopGuardCompiler.compile(cLGuard, callback);
236         } catch (CompilerException e) {
237             logger.error(e.getMessage() + e);
238             callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
239         }
240         //
241         // Save the spec
242         //
243         callback.results.setSpecification(dumpedYaml);
244         return callback.results;
245     }
246
247 }