131d7d766573d0f4a7e787c66708e31a905bf2f8
[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.builder.impl;
22
23 import java.util.LinkedList;
24 import java.util.Map;
25 import java.util.UUID;
26
27 import org.yaml.snakeyaml.DumperOptions;
28 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
29 import org.yaml.snakeyaml.Yaml;
30
31 import org.onap.policy.aai.PNF;
32 import org.onap.policy.controlloop.compiler.CompilerException;
33 import org.onap.policy.controlloop.compiler.ControlLoopCompiler;
34 import org.onap.policy.controlloop.compiler.ControlLoopCompilerCallback;
35 import org.onap.policy.controlloop.policy.ControlLoop;
36 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
37 import org.onap.policy.controlloop.policy.FinalResult;
38 import org.onap.policy.controlloop.policy.OperationsAccumulateParams;
39 import org.onap.policy.controlloop.policy.Policy;
40 import org.onap.policy.controlloop.policy.PolicyResult;
41 import org.onap.policy.controlloop.policy.Target;
42 import org.onap.policy.controlloop.policy.builder.BuilderException;
43 import org.onap.policy.controlloop.policy.builder.ControlLoopPolicyBuilder;
44 import org.onap.policy.controlloop.policy.builder.MessageLevel;
45 import org.onap.policy.controlloop.policy.builder.Results;
46 import org.onap.policy.sdc.Resource;
47 import org.onap.policy.sdc.Service;
48
49 public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder {
50
51         private ControlLoopPolicy policy;
52         
53         public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout) throws BuilderException {
54                 policy = new ControlLoopPolicy();
55                 policy.controlLoop = new ControlLoop();
56                 policy.controlLoop.controlLoopName = controlLoopName;
57                 policy.controlLoop.timeout = timeout;
58         }
59         
60         public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Resource resource, Service... services) throws BuilderException {
61                 this(controlLoopName, timeout);
62                 this.addResource(resource);
63                 this.addService(services);
64         }
65
66         public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, PNF pnf) throws BuilderException {
67                 this(controlLoopName, timeout);
68                 this.setPNF(pnf);
69         }
70         
71         public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Service service, Resource[] resources) throws BuilderException {
72                 this(controlLoopName, timeout);
73                 this.addService(service);
74                 this.addResource(resources);
75         }
76
77         @Override
78         public ControlLoopPolicyBuilder addService(Service... services) throws BuilderException {
79                 if (services == null) {
80                         throw new BuilderException("Service must not be null");
81                 }
82                 for (Service service : services) {
83                         if (service.serviceUUID == null) {
84                                 if (service.serviceName == null || service.serviceName.length() < 1) {
85                                         throw new BuilderException("Invalid service - need either a serviceUUID or serviceName");
86                                 }
87                         }
88                         if (policy.controlLoop.services == null) {
89                                 policy.controlLoop.services = new LinkedList<Service>();
90                         }
91                         policy.controlLoop.services.add(service);
92                 }
93                 return this;
94         }
95         
96         @Override
97         public ControlLoopPolicyBuilder removeService(Service... services) throws BuilderException {
98                 if (services == null) {
99             throw new BuilderException("Service must not be null");
100         }
101         if (policy.controlLoop.services == null) {
102             throw new BuilderException("No existing services to remove");
103         }
104         for (Service service : services) {
105             if (service.serviceUUID == null) {
106                 if (service.serviceName == null || service.serviceName.length() < 1) {
107                     throw new BuilderException("Invalid service - need either a serviceUUID or serviceName");
108                 }
109             }
110             boolean removed = policy.controlLoop.services.remove(service);    
111             if (!removed) {
112                 throw new BuilderException("Unknown service " + service.serviceName);
113             }
114         }
115         return this;
116         }
117
118         @Override
119         public ControlLoopPolicyBuilder removeAllServices() throws BuilderException {
120                 policy.controlLoop.services.clear();
121         return this;
122         }
123
124         
125         @Override
126         public ControlLoopPolicyBuilder addResource(Resource... resources) throws BuilderException {
127                 if (resources == null) {
128                         throw new BuilderException("resources must not be null");
129                 }
130                 for (Resource resource : resources) {
131                         if (resource.resourceUUID == null) {
132                                 if (resource.resourceName == null || resource.resourceName.length() <= 0) {
133                                         throw new BuilderException("Invalid resource - need either resourceUUID or resourceName");
134                                 }
135                         }
136                         if (policy.controlLoop.resources == null) {
137                                 policy.controlLoop.resources = new LinkedList<Resource>();
138                         }
139                         policy.controlLoop.resources.add(resource);
140                 }
141                 return this;
142         }
143
144         @Override
145         public ControlLoopPolicyBuilder setPNF(PNF pnf) throws BuilderException {
146                 if (pnf == null) {
147                         throw new BuilderException("PNF must not be null");
148                 }
149                 if (pnf.PNFName == null) {
150                         if (pnf.PNFType == null) {
151                                 throw new BuilderException("Invalid PNF - need either pnfName or pnfType");
152                         }
153                 }
154                 policy.controlLoop.pnf = pnf;
155                 return this;
156         }
157         
158         @Override
159         public ControlLoopPolicyBuilder setAbatement(Boolean abatement) throws BuilderException{
160                 if (abatement == null) {
161                         throw new BuilderException("abatement must not be null");
162                 }
163                 policy.controlLoop.abatement = abatement;
164                 return this;
165         }
166         
167         @Override
168         public ControlLoopPolicyBuilder setTimeout(Integer timeout) {
169                 policy.controlLoop.timeout = timeout;
170                 return this;
171         }
172         
173         @Override
174         public Policy setTriggerPolicy(String name, String description, String actor, Target target, String recipe,
175                         Map<String, String> payload, Integer retries, Integer timeout) throws BuilderException {
176                 
177                 Policy trigger = new Policy(UUID.randomUUID().toString(), name, description, actor, payload, target, recipe, retries, timeout);
178                 
179                 policy.controlLoop.trigger_policy = trigger.id;
180                 
181                 this.addNewPolicy(trigger);
182                 //
183                 // Return a copy of the policy
184                 //
185                 return new Policy(trigger);
186         }
187
188         @Override
189         public Policy setPolicyForPolicyResult(String name, String description, String actor,
190                         Target target, String recipe, Map<String, String> payload, Integer retries, Integer timeout, String policyID, PolicyResult... results) throws BuilderException {
191                 //
192                 // Find the existing policy
193                 //
194                 Policy existingPolicy = this.findPolicy(policyID);
195                 if (existingPolicy == null) {
196                         throw new BuilderException("Unknown policy " + policyID);
197                 }
198                 //
199                 // Create the new Policy
200                 //
201                 Policy newPolicy = new Policy(UUID.randomUUID().toString(), name, description, actor, payload, target, recipe, retries, timeout);
202                 //
203                 // Connect the results
204                 //
205                 for (PolicyResult result : results) {
206                         switch (result) {
207                         case FAILURE:
208                                 existingPolicy.failure = newPolicy.id;
209                                 break;
210                         case FAILURE_EXCEPTION:
211                                 existingPolicy.failure_exception = newPolicy.id;
212                                 break;
213                         case FAILURE_RETRIES:
214                                 existingPolicy.failure_retries = newPolicy.id;
215                                 break;
216                         case FAILURE_TIMEOUT:
217                                 existingPolicy.failure_timeout = newPolicy.id;
218                                 break;
219                         case FAILURE_GUARD:
220                                 existingPolicy.failure_guard = newPolicy.id;
221                                 break;
222                         case SUCCESS:
223                                 existingPolicy.success = newPolicy.id;
224                                 break;
225                         default:
226                                 throw new BuilderException("Invalid PolicyResult " + result);
227                         }
228                 }
229                 //
230                 // Add it to our list
231                 //
232                 this.policy.policies.add(newPolicy);
233                 //
234                 // Return a policy to them
235                 //
236                 return new Policy(newPolicy);
237         }
238         
239         private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
240
241                 public ResultsImpl results = new ResultsImpl();
242                 
243                 @Override
244                 public boolean onWarning(String message) {
245                         results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
246                         return false;
247                 }
248
249                 @Override
250                 public boolean onError(String message) {
251                         results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
252                         return false;
253                 }
254         }
255
256         @Override
257         public Results  buildSpecification() {
258                 //
259                 // Dump the specification
260                 //
261                 DumperOptions options = new DumperOptions();
262                 options.setDefaultFlowStyle(FlowStyle.BLOCK);
263                 options.setPrettyFlow(true);
264                 Yaml yaml = new Yaml(options);
265                 String dumpedYaml = yaml.dump(policy);
266                 //
267                 // This is our callback class for our compiler
268                 //
269                 BuilderCompilerCallback callback = new BuilderCompilerCallback();
270                 //
271                 // Compile it
272                 //
273                 try {
274                         ControlLoopCompiler.compile(policy, callback);
275                 } catch (CompilerException e) {
276                         callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
277                 }
278                 //
279                 // Save the spec
280                 //
281                 callback.results.setSpecification(dumpedYaml);
282                 return callback.results;
283         }
284
285         private void addNewPolicy(Policy policy) {
286                 if (this.policy.policies == null) {
287                         this.policy.policies = new LinkedList<Policy>();
288                 }
289                 this.policy.policies.add(policy);
290         }
291         
292         private Policy findPolicy(String id) {
293                 for (Policy policy : this.policy.policies) {
294                         if (policy.id.equals(id)) {
295                                 return policy;
296                         }
297                 }
298                 return null;
299         }
300
301         @Override
302         public ControlLoopPolicyBuilder removeResource(Resource... resources) throws BuilderException {
303             if (resources == null) {
304             throw new BuilderException("Resource must not be null");
305         }
306         if (policy.controlLoop.resources == null) {
307             throw new BuilderException("No existing resources to remove");
308         }
309         for (Resource resource : resources) {
310             if (resource.resourceUUID == null) {
311                 if (resource.resourceName == null || resource.resourceName.length() < 1) {
312                     throw new BuilderException("Invalid resource - need either a resourceUUID or resourceName");
313                 }
314             }
315             boolean removed = policy.controlLoop.resources.remove(resource); 
316             if (!removed) {
317                 throw new BuilderException("Unknown resource " + resource.resourceName);
318             }
319         }
320         return this; 
321     }
322
323         @Override
324         public ControlLoopPolicyBuilder removeAllResources() throws BuilderException {
325             policy.controlLoop.resources.clear();
326         return this;
327     }
328
329         @Override
330         public ControlLoopPolicyBuilder removePNF() throws BuilderException {
331                 policy.controlLoop.pnf = null;
332         return this;
333         }
334
335         @Override
336         public Integer calculateTimeout() {
337                 int sum = 0;
338         for (Policy policy : this.policy.policies) {
339             sum += policy.timeout.intValue();
340         }
341         return new Integer(sum);
342         }
343
344         @Override
345         public ControlLoop setTriggerPolicy(String id) throws BuilderException {
346                 if (id == null) {
347             throw new BuilderException("Id must not be null");
348         }
349             Policy trigger = this.findPolicy(id);
350         if (trigger == null) {
351             throw new BuilderException("Unknown policy " + id);
352         }
353         else {
354             this.policy.controlLoop.trigger_policy = id;
355         }
356         return new ControlLoop(this.policy.controlLoop);
357     }
358
359         @Override
360         public boolean isOpenLoop() {
361         if (this.policy.controlLoop.trigger_policy.equals(FinalResult.FINAL_OPENLOOP.toString())) {
362             return true;
363         }       
364         else {
365             return false;
366         }
367         }
368
369         @Override
370         public Policy getTriggerPolicy() throws BuilderException {
371             if (this.policy.controlLoop.trigger_policy.equals(FinalResult.FINAL_OPENLOOP.toString())) {
372             return null;
373         }
374         else {
375             Policy trigger = new Policy(this.findPolicy(this.policy.controlLoop.trigger_policy));
376             return trigger;
377         }
378     }
379
380         @Override
381         public ControlLoop getControlLoop() {
382                 ControlLoop loop = new ControlLoop(this.policy.controlLoop);
383                 return loop;
384         }
385
386         @Override
387         public Policy setPolicyForPolicyResult(String policyResultID, String policyID, PolicyResult... results)
388                         throws BuilderException {
389                 //
390         // Find the existing policy
391         //
392         Policy existingPolicy = this.findPolicy(policyID);
393         if (existingPolicy == null) {
394             throw new BuilderException(policyID + " does not exist");
395         }
396         if (this.findPolicy(policyResultID) == null) {
397             throw new BuilderException("Operational policy " + policyResultID + " does not exist");
398         }
399         //
400         // Connect the results
401         //
402         for (PolicyResult result : results) {
403             switch (result) {
404             case FAILURE:
405                 existingPolicy.failure = policyResultID;
406                 break;
407             case FAILURE_EXCEPTION:
408                 existingPolicy.failure_exception = policyResultID;
409                 break;
410             case FAILURE_RETRIES:
411                 existingPolicy.failure_retries = policyResultID;
412                 break;
413             case FAILURE_TIMEOUT:
414                 existingPolicy.failure_timeout = policyResultID;
415                 break;
416             case FAILURE_GUARD:
417                 existingPolicy.failure_guard = policyResultID;
418                 break;
419             case SUCCESS:
420                 existingPolicy.success = policyResultID;
421                 break;
422             default:
423                 throw new BuilderException("Invalid PolicyResult " + result);
424             }
425         }
426         return new Policy(this.findPolicy(policyResultID));
427         }
428
429         @Override
430         public boolean removePolicy(String policyID) throws BuilderException {
431                 Policy existingPolicy = this.findPolicy(policyID);
432         if (existingPolicy == null) {
433             throw new BuilderException("Unknown policy " + policyID);
434         }
435         //
436         // Check if the policy to remove is trigger_policy
437         //
438         if (this.policy.controlLoop.trigger_policy.equals(policyID)) {
439             this.policy.controlLoop.trigger_policy = FinalResult.FINAL_OPENLOOP.toString();
440         }
441         else {
442             //
443             // Update policies
444             //
445             for (Policy policy : this.policy.policies) {
446                 int index = this.policy.policies.indexOf(policy);
447                 if (policy.success.equals(policyID)) {
448                     policy.success = FinalResult.FINAL_SUCCESS.toString();
449                 }
450                 if (policy.failure.equals(policyID)) {
451                     policy.failure = FinalResult.FINAL_FAILURE.toString();
452                 }
453                 if (policy.failure_retries.equals(policyID)) {
454                     policy.failure_retries = FinalResult.FINAL_FAILURE_RETRIES.toString();
455                 }
456                 if (policy.failure_timeout.equals(policyID)) {
457                     policy.failure_timeout = FinalResult.FINAL_FAILURE_TIMEOUT.toString();
458                 }
459                 if (policy.failure_exception.equals(policyID)) {
460                     policy.failure_exception = FinalResult.FINAL_FAILURE_EXCEPTION.toString();
461                 }
462                 if (policy.failure_guard.equals(policyID)) {
463                     policy.failure_guard = FinalResult.FINAL_FAILURE_GUARD.toString();
464                 }
465                 this.policy.policies.set(index, policy);
466             }
467         }
468         //
469         // remove the policy
470         //
471         boolean removed = this.policy.policies.remove(existingPolicy);
472         return removed;
473         }
474
475         @Override
476         public Policy resetPolicyResults(String policyID) throws BuilderException {
477         Policy existingPolicy = this.findPolicy(policyID);
478         if (existingPolicy == null) {
479             throw new BuilderException("Unknown policy " + policyID);
480         }
481         //
482         // reset policy results
483         //
484         existingPolicy.success = FinalResult.FINAL_SUCCESS.toString();
485         existingPolicy.failure = FinalResult.FINAL_FAILURE.toString();
486         existingPolicy.failure_retries = FinalResult.FINAL_FAILURE_RETRIES.toString();
487         existingPolicy.failure_timeout = FinalResult.FINAL_FAILURE_TIMEOUT.toString();
488         existingPolicy.failure_exception = FinalResult.FINAL_FAILURE_EXCEPTION.toString();
489         existingPolicy.failure_guard = FinalResult.FINAL_FAILURE_GUARD.toString();
490         return new Policy(existingPolicy);
491         }
492
493         @Override
494         public ControlLoopPolicyBuilder removeAllPolicies() {
495                 //
496         // Remove all existing operational policies
497         //
498         this.policy.policies.clear();
499         //
500         // Revert controlLoop back to an open loop
501         //
502         this.policy.controlLoop.trigger_policy = FinalResult.FINAL_OPENLOOP.toString();
503         return this;
504         }
505         
506         @Override
507         public Policy addOperationsAccumulateParams(String policyID, OperationsAccumulateParams operationsAccumulateParams) throws BuilderException {
508                 Policy existingPolicy = this.findPolicy(policyID);
509         if (existingPolicy == null) {
510             throw new BuilderException("Unknown policy " + policyID);
511         }
512         //
513         // Add operationsAccumulateParams to existingPolicy
514         //
515         existingPolicy.operationsAccumulateParams = operationsAccumulateParams;
516         return new Policy(existingPolicy);
517         }
518
519 }