60716383dd7f1cfec8b91fb69ba44047a07eece8
[policy/drools-applications.git] /
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * policy-yaml\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.onap.policy.controlloop.policy.guard.builder.impl;\r
22 \r
23 import java.util.LinkedList;\r
24 \r
25 import org.onap.policy.controlloop.compiler.CompilerException;\r
26 import org.onap.policy.controlloop.compiler.ControlLoopCompilerCallback;\r
27 import org.onap.policy.controlloop.guard.compiler.ControlLoopGuardCompiler;\r
28 import org.onap.policy.controlloop.policy.builder.BuilderException;\r
29 import org.onap.policy.controlloop.policy.builder.MessageLevel;\r
30 import org.onap.policy.controlloop.policy.builder.Results;\r
31 import org.onap.policy.controlloop.policy.builder.impl.MessageImpl;\r
32 import org.onap.policy.controlloop.policy.builder.impl.ResultsImpl;\r
33 import org.onap.policy.controlloop.policy.guard.Constraint;\r
34 import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;\r
35 import org.onap.policy.controlloop.policy.guard.Guard;\r
36 import org.onap.policy.controlloop.policy.guard.GuardPolicy;\r
37 import org.onap.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder;\r
38 import org.slf4j.Logger;\r
39 import org.slf4j.LoggerFactory;\r
40 import org.yaml.snakeyaml.DumperOptions;\r
41 import org.yaml.snakeyaml.DumperOptions.FlowStyle;\r
42 import org.yaml.snakeyaml.Yaml;\r
43 \r
44 public class ControlLoopGuardBuilderImpl implements ControlLoopGuardBuilder {\r
45     private static final String NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID = "No existing guard policy matching the id: ";\r
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";\r
47         private static Logger logger = LoggerFactory.getLogger(ControlLoopGuardBuilderImpl.class.getName());\r
48     private ControlLoopGuard cLGuard;\r
49     \r
50     public ControlLoopGuardBuilderImpl(Guard guard) {\r
51         cLGuard = new ControlLoopGuard();\r
52         cLGuard.setGuard(guard);\r
53     }\r
54     \r
55     @Override\r
56     public ControlLoopGuardBuilder addGuardPolicy(GuardPolicy... policies) throws BuilderException {\r
57         if (policies == null) {\r
58             throw new BuilderException("GuardPolicy must not be null");\r
59         }\r
60         for (GuardPolicy policy : policies) {\r
61             if (!policy.isValid()) {\r
62                 throw new BuilderException("Invalid guard policy - some required fields are missing");\r
63             }\r
64             if (cLGuard.getGuards() == null) {\r
65                 cLGuard.setGuards(new LinkedList<>());\r
66             }\r
67             cLGuard.getGuards().add(policy);\r
68         }\r
69         return this;\r
70     }\r
71 \r
72     @Override\r
73     public ControlLoopGuardBuilder removeGuardPolicy(GuardPolicy... policies) throws BuilderException {\r
74         if (policies == null) {\r
75             throw new BuilderException("GuardPolicy must not be null");\r
76         }\r
77         if (cLGuard.getGuards() == null) {\r
78             throw new BuilderException("No existing guard policies to remove");\r
79         }\r
80         for (GuardPolicy policy : policies) {\r
81             if (!policy.isValid()) {\r
82                 throw new BuilderException("Invalid guard policy - some required fields are missing");\r
83             }\r
84             boolean removed = cLGuard.getGuards().remove(policy);\r
85             if (!removed) {\r
86                 throw new BuilderException("Unknown guard policy: " + policy.getName());\r
87             }\r
88         }\r
89         return this;\r
90     }\r
91 \r
92     @Override\r
93     public ControlLoopGuardBuilder removeAllGuardPolicies() throws BuilderException {\r
94         cLGuard.getGuards().clear();\r
95         return this;\r
96     }\r
97 \r
98     @Override\r
99     public ControlLoopGuardBuilder addLimitConstraint(String id, Constraint... constraints) throws BuilderException {\r
100         if (id == null) {\r
101             throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);\r
102         }\r
103         if (constraints == null) {\r
104             throw new BuilderException("Constraint much not be null");\r
105         }\r
106         if (!addLimitConstraints(id,constraints)) {\r
107             throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);\r
108         }\r
109         return this;\r
110     }\r
111 \r
112     private boolean addLimitConstraints(String id, Constraint... constraints) throws BuilderException {\r
113         boolean exist = false;\r
114         for (GuardPolicy policy: cLGuard.getGuards()) {\r
115             //\r
116             // We could have only one guard policy matching the id\r
117             //\r
118             if (policy.getId().equals(id)) {\r
119                 exist = true;\r
120                 for (Constraint cons: constraints) {\r
121                     if (!cons.isValid()) {\r
122                         throw new BuilderException("Invalid guard constraint - some required fields are missing");\r
123                     }\r
124                     if (policy.getLimit_constraints() == null) {\r
125                         policy.setLimit_constraints(new LinkedList<>());\r
126                     }\r
127                     policy.getLimit_constraints().add(cons);\r
128                 }\r
129                 break;\r
130             }\r
131         }\r
132         return exist;\r
133     }\r
134 \r
135     @Override\r
136     public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException {\r
137         if (id == null) {\r
138             throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);\r
139         }\r
140         if (constraints == null) {\r
141             throw new BuilderException("Constraint much not be null");\r
142         }\r
143         if (!removeConstraints(id, constraints)) {\r
144             throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);\r
145         }\r
146         return this;\r
147     }\r
148 \r
149     private boolean removeConstraints(String id, Constraint... constraints) throws BuilderException {\r
150         boolean exist = false;\r
151         for (GuardPolicy policy: cLGuard.getGuards()) {\r
152             //\r
153             // We could have only one guard policy matching the id\r
154             //\r
155             if (policy.getId().equals(id)) {\r
156                 exist = true;\r
157                 for (Constraint cons: constraints) {\r
158                     if (!cons.isValid()) {\r
159                         throw new BuilderException("Invalid guard constraint - some required fields are missing");\r
160                     }\r
161                     boolean removed = policy.getLimit_constraints().remove(cons);\r
162                     if (!removed) {\r
163                         throw new BuilderException("Unknown guard constraint: " + cons);\r
164                     }\r
165                 }\r
166                 break;\r
167             }\r
168         }\r
169         return exist;\r
170     }\r
171 \r
172     @Override\r
173     public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException {\r
174         if (cLGuard.getGuards() == null || cLGuard.getGuards().isEmpty()) {\r
175             throw new BuilderException("No guard policies exist");\r
176         } \r
177         if (id == null) {\r
178             throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);\r
179         }\r
180         boolean exist = false;\r
181         for (GuardPolicy policy: cLGuard.getGuards()) {\r
182             if (policy.getId().equals(id)) {\r
183                 exist = true;\r
184                 policy.getLimit_constraints().clear();\r
185             }\r
186         }\r
187         if (!exist) {\r
188             throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);\r
189         }\r
190         return this;\r
191     }\r
192 \r
193     \r
194     private class BuilderCompilerCallback implements ControlLoopCompilerCallback {\r
195 \r
196         private ResultsImpl results = new ResultsImpl();\r
197         \r
198         @Override\r
199         public boolean onWarning(String message) {\r
200             results.addMessage(new MessageImpl(message, MessageLevel.WARNING));\r
201             return false;\r
202         }\r
203 \r
204         @Override\r
205         public boolean onError(String message) {\r
206             results.addMessage(new MessageImpl(message, MessageLevel.ERROR));\r
207             return false;\r
208         }\r
209     }\r
210     \r
211     @Override\r
212     public ControlLoopGuard getControlLoopGuard() {\r
213         return new ControlLoopGuard(this.cLGuard);\r
214     }   \r
215     \r
216     \r
217     @Override\r
218     public Results buildSpecification() {\r
219         //\r
220         // Dump the specification\r
221         //\r
222         DumperOptions options = new DumperOptions();\r
223         options.setDefaultFlowStyle(FlowStyle.BLOCK);\r
224         options.setPrettyFlow(true);\r
225         Yaml yaml = new Yaml(options);\r
226         String dumpedYaml = yaml.dump(cLGuard);\r
227         //\r
228         // This is our callback class for our compiler\r
229         //\r
230         BuilderCompilerCallback callback = new BuilderCompilerCallback();\r
231         //\r
232         // Compile it\r
233         //\r
234         try {\r
235             ControlLoopGuardCompiler.compile(cLGuard, callback);\r
236         } catch (CompilerException e) {\r
237             logger.error(e.getMessage() + e);\r
238             callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));\r
239         }\r
240         //\r
241         // Save the spec\r
242         //\r
243         callback.results.setSpecification(dumpedYaml);\r
244         return callback.results;\r
245     }\r
246 \r
247 }\r