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