be34a0a240344f36d229e6c012f643f46b28872d
[policy/engine.git] / ECOMP-ControlloopPolicy / src / main / java / org / openecomp / policy / controlloop / policy / builder / impl / ControlLoopPolicyBuilderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
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.openecomp.policy.controlloop.policy.builder.impl;
22
23 import java.util.LinkedList;
24 import java.util.Map;
25 import java.util.UUID;
26
27 import org.openecomp.policy.controlloop.compiler.CompilerException;
28 import org.openecomp.policy.controlloop.compiler.ControlLoopCompiler;
29 import org.openecomp.policy.controlloop.compiler.ControlLoopCompilerCallback;
30 import org.openecomp.policy.controlloop.policy.ControlLoop;
31 import org.openecomp.policy.controlloop.policy.ControlLoopPolicy;
32 import org.openecomp.policy.controlloop.policy.FinalResult;
33 import org.openecomp.policy.controlloop.policy.OperationsAccumulateParams;
34 import org.openecomp.policy.controlloop.policy.Policy;
35 import org.openecomp.policy.controlloop.policy.PolicyResult;
36 import org.openecomp.policy.controlloop.policy.Target;
37 import org.openecomp.policy.controlloop.policy.builder.BuilderException;
38 import org.openecomp.policy.controlloop.policy.builder.ControlLoopPolicyBuilder;
39 import org.openecomp.policy.controlloop.policy.builder.MessageLevel;
40 import org.openecomp.policy.controlloop.policy.builder.Results;
41 import org.yaml.snakeyaml.DumperOptions;
42 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
43 import org.yaml.snakeyaml.Yaml;
44
45 public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder {
46
47         private ControlLoopPolicy policy;
48         
49         public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout) throws BuilderException {
50                 policy = new ControlLoopPolicy();
51                 policy.controlLoop = new ControlLoop();
52                 policy.controlLoop.controlLoopName = controlLoopName;
53                 policy.controlLoop.timeout = timeout;
54         }
55         
56         @Override
57         public ControlLoopPolicyBuilder setAbatement(Boolean abatement) throws BuilderException{
58                 if (abatement == null) {
59                         throw new BuilderException("abatement must not be null");
60                 }
61                 policy.controlLoop.abatement = abatement;
62                 return this;
63         }
64         
65         @Override
66         public ControlLoopPolicyBuilder setTimeout(Integer timeout) {
67                 policy.controlLoop.timeout = timeout;
68                 return this;
69         }
70         
71         @Override
72         public Policy setTriggerPolicy(String name, String description, String actor, Target target, String recipe,
73                         Map<String, String> payload, Integer retries, Integer timeout) throws BuilderException {
74                 
75                 Policy trigger = new Policy(UUID.randomUUID().toString(), name, description, actor, payload, target, recipe, retries, timeout);
76                 
77                 policy.controlLoop.trigger_policy = trigger.id;
78                 
79                 this.addNewPolicy(trigger);
80                 //
81                 // Return a copy of the policy
82                 //
83                 return new Policy(trigger);
84         }
85
86         @Override
87         public Policy setPolicyForPolicyResult(String name, String description, String actor,
88                         Target target, String recipe, Map<String, String> payload, Integer retries, Integer timeout, String policyID, PolicyResult... results) throws BuilderException {
89                 //
90                 // Find the existing policy
91                 //
92                 Policy existingPolicy = this.findPolicy(policyID);
93                 if (existingPolicy == null) {
94                         throw new BuilderException("Unknown policy " + policyID);
95                 }
96                 //
97                 // Create the new Policy
98                 //
99                 Policy newPolicy = new Policy(UUID.randomUUID().toString(), name, description, actor, payload, target, recipe, retries, timeout);
100                 //
101                 // Connect the results
102                 //
103                 for (PolicyResult result : results) {
104                         switch (result) {
105                         case FAILURE:
106                                 existingPolicy.failure = newPolicy.id;
107                                 break;
108                         case FAILURE_EXCEPTION:
109                                 existingPolicy.failure_exception = newPolicy.id;
110                                 break;
111                         case FAILURE_RETRIES:
112                                 existingPolicy.failure_retries = newPolicy.id;
113                                 break;
114                         case FAILURE_TIMEOUT:
115                                 existingPolicy.failure_timeout = newPolicy.id;
116                                 break;
117                         case FAILURE_GUARD:
118                                 existingPolicy.failure_guard = newPolicy.id;
119                                 break;
120                         case SUCCESS:
121                                 existingPolicy.success = newPolicy.id;
122                                 break;
123                         default:
124                                 throw new BuilderException("Invalid PolicyResult " + result);
125                         }
126                 }
127                 //
128                 // Add it to our list
129                 //
130                 this.policy.policies.add(newPolicy);
131                 //
132                 // Return a policy to them
133                 //
134                 return new Policy(newPolicy);
135         }
136         
137         private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
138
139                 public ResultsImpl results = new ResultsImpl();
140                 
141                 @Override
142                 public boolean onWarning(String message) {
143                         results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
144                         return false;
145                 }
146
147                 @Override
148                 public boolean onError(String message) {
149                         results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
150                         return false;
151                 }
152         }
153
154         @Override
155         public Results  buildSpecification() {
156                 //
157                 // Dump the specification
158                 //
159                 DumperOptions options = new DumperOptions();
160                 options.setDefaultFlowStyle(FlowStyle.BLOCK);
161                 options.setPrettyFlow(true);
162                 Yaml yaml = new Yaml(options);
163                 String dumpedYaml = yaml.dump(policy);
164                 //
165                 // This is our callback class for our compiler
166                 //
167                 BuilderCompilerCallback callback = new BuilderCompilerCallback();
168                 //
169                 // Compile it
170                 //
171                 try {
172                         ControlLoopCompiler.compile(policy, callback);
173                 } catch (CompilerException e) {
174                         callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
175                 }
176                 //
177                 // Save the spec
178                 //
179                 callback.results.setSpecification(dumpedYaml);
180                 return callback.results;
181         }
182
183         private void addNewPolicy(Policy policy) {
184                 if (this.policy.policies == null) {
185                         this.policy.policies = new LinkedList<Policy>();
186                 }
187                 this.policy.policies.add(policy);
188         }
189         
190         private Policy findPolicy(String id) {
191                 for (Policy policy : this.policy.policies) {
192                         if (policy.id.equals(id)) {
193                                 return policy;
194                         }
195                 }
196                 return null;
197         }
198
199         @Override
200         public Integer calculateTimeout() {
201                 int sum = 0;
202         for (Policy policy : this.policy.policies) {
203             sum += policy.timeout.intValue();
204         }
205         return new Integer(sum);
206         }
207
208         @Override
209         public ControlLoop setTriggerPolicy(String id) throws BuilderException {
210                 if (id == null) {
211             throw new BuilderException("Id must not be null");
212         }
213             Policy trigger = this.findPolicy(id);
214         if (trigger == null) {
215             throw new BuilderException("Unknown policy " + id);
216         }
217         else {
218             this.policy.controlLoop.trigger_policy = id;
219         }
220         return new ControlLoop(this.policy.controlLoop);
221     }
222
223         @Override
224         public boolean isOpenLoop() {
225         if (this.policy.controlLoop.trigger_policy.equals(FinalResult.FINAL_OPENLOOP.toString())) {
226             return true;
227         }       
228         else {
229             return false;
230         }
231         }
232
233         @Override
234         public Policy getTriggerPolicy() throws BuilderException {
235             if (this.policy.controlLoop.trigger_policy.equals(FinalResult.FINAL_OPENLOOP.toString())) {
236             return null;
237         }
238         else {
239             Policy trigger = new Policy(this.findPolicy(this.policy.controlLoop.trigger_policy));
240             return trigger;
241         }
242     }
243
244         @Override
245         public ControlLoop getControlLoop() {
246                 ControlLoop loop = new ControlLoop(this.policy.controlLoop);
247                 return loop;
248         }
249
250         @Override
251         public Policy setPolicyForPolicyResult(String policyResultID, String policyID, PolicyResult... results)
252                         throws BuilderException {
253                 //
254         // Find the existing policy
255         //
256         Policy existingPolicy = this.findPolicy(policyID);
257         if (existingPolicy == null) {
258             throw new BuilderException(policyID + " does not exist");
259         }
260         if (this.findPolicy(policyResultID) == null) {
261             throw new BuilderException("Operational policy " + policyResultID + " does not exist");
262         }
263         //
264         // Connect the results
265         //
266         for (PolicyResult result : results) {
267             switch (result) {
268             case FAILURE:
269                 existingPolicy.failure = policyResultID;
270                 break;
271             case FAILURE_EXCEPTION:
272                 existingPolicy.failure_exception = policyResultID;
273                 break;
274             case FAILURE_RETRIES:
275                 existingPolicy.failure_retries = policyResultID;
276                 break;
277             case FAILURE_TIMEOUT:
278                 existingPolicy.failure_timeout = policyResultID;
279                 break;
280             case FAILURE_GUARD:
281                 existingPolicy.failure_guard = policyResultID;
282                 break;
283             case SUCCESS:
284                 existingPolicy.success = policyResultID;
285                 break;
286             default:
287                 throw new BuilderException("Invalid PolicyResult " + result);
288             }
289         }
290         return new Policy(this.findPolicy(policyResultID));
291         }
292
293         @Override
294         public boolean removePolicy(String policyID) throws BuilderException {
295                 Policy existingPolicy = this.findPolicy(policyID);
296         if (existingPolicy == null) {
297             throw new BuilderException("Unknown policy " + policyID);
298         }
299         //
300         // Check if the policy to remove is trigger_policy
301         //
302         if (this.policy.controlLoop.trigger_policy.equals(policyID)) {
303             this.policy.controlLoop.trigger_policy = FinalResult.FINAL_OPENLOOP.toString();
304         }
305         else {
306             //
307             // Update policies
308             //
309             for (Policy policy : this.policy.policies) {
310                 int index = this.policy.policies.indexOf(policy);
311                 if (policy.success.equals(policyID)) {
312                     policy.success = FinalResult.FINAL_SUCCESS.toString();
313                 }
314                 if (policy.failure.equals(policyID)) {
315                     policy.failure = FinalResult.FINAL_FAILURE.toString();
316                 }
317                 if (policy.failure_retries.equals(policyID)) {
318                     policy.failure_retries = FinalResult.FINAL_FAILURE_RETRIES.toString();
319                 }
320                 if (policy.failure_timeout.equals(policyID)) {
321                     policy.failure_timeout = FinalResult.FINAL_FAILURE_TIMEOUT.toString();
322                 }
323                 if (policy.failure_exception.equals(policyID)) {
324                     policy.failure_exception = FinalResult.FINAL_FAILURE_EXCEPTION.toString();
325                 }
326                 if (policy.failure_guard.equals(policyID)) {
327                     policy.failure_guard = FinalResult.FINAL_FAILURE_GUARD.toString();
328                 }
329                 this.policy.policies.set(index, policy);
330             }
331         }
332         //
333         // remove the policy
334         //
335         boolean removed = this.policy.policies.remove(existingPolicy);
336         return removed;
337         }
338
339         @Override
340         public Policy resetPolicyResults(String policyID) throws BuilderException {
341         Policy existingPolicy = this.findPolicy(policyID);
342         if (existingPolicy == null) {
343             throw new BuilderException("Unknown policy " + policyID);
344         }
345         //
346         // reset policy results
347         //
348         existingPolicy.success = FinalResult.FINAL_SUCCESS.toString();
349         existingPolicy.failure = FinalResult.FINAL_FAILURE.toString();
350         existingPolicy.failure_retries = FinalResult.FINAL_FAILURE_RETRIES.toString();
351         existingPolicy.failure_timeout = FinalResult.FINAL_FAILURE_TIMEOUT.toString();
352         existingPolicy.failure_exception = FinalResult.FINAL_FAILURE_EXCEPTION.toString();
353         existingPolicy.failure_guard = FinalResult.FINAL_FAILURE_GUARD.toString();
354         return new Policy(existingPolicy);
355         }
356
357         @Override
358         public ControlLoopPolicyBuilder removeAllPolicies() {
359                 //
360         // Remove all existing operational policies
361         //
362         this.policy.policies.clear();
363         //
364         // Revert controlLoop back to an open loop
365         //
366         this.policy.controlLoop.trigger_policy = FinalResult.FINAL_OPENLOOP.toString();
367         return this;
368         }
369         
370         @Override
371         public Policy addOperationsAccumulateParams(String policyID, OperationsAccumulateParams operationsAccumulateParams) throws BuilderException {
372                 Policy existingPolicy = this.findPolicy(policyID);
373         if (existingPolicy == null) {
374             throw new BuilderException("Unknown policy " + policyID);
375         }
376         //
377         // Add operationsAccumulateParams to existingPolicy
378         //
379         existingPolicy.operationsAccumulateParams = operationsAccumulateParams;
380         return new Policy(existingPolicy);
381         }
382
383 }