bc94068abed4d240d29ea9ddfe5cf933d55a1b5c
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * controlloop processor
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.processor;
22
23 import org.yaml.snakeyaml.Yaml;
24 import org.yaml.snakeyaml.constructor.Constructor;
25
26 import org.onap.policy.controlloop.ControlLoopException;
27 import org.onap.policy.controlloop.policy.ControlLoop;
28 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
29 import org.onap.policy.controlloop.policy.FinalResult;
30 import org.onap.policy.controlloop.policy.Policy;
31 import org.onap.policy.controlloop.policy.PolicyResult;
32
33 public class ControlLoopProcessor {
34         
35         private final String yaml;
36         private final ControlLoopPolicy policy;
37         private String currentPolicy = null;
38         
39         public ControlLoopProcessor(String yaml) throws ControlLoopException {
40                 this.yaml = yaml;
41                 try {
42                         Yaml y = new Yaml(new Constructor(ControlLoopPolicy.class));
43                         Object obj = y.load(this.yaml);
44                         if (obj instanceof ControlLoopPolicy) {
45                                 this.policy = (ControlLoopPolicy) obj;
46                                 this.currentPolicy = this.policy.controlLoop.trigger_policy;
47                         } else {
48                                 this.policy = null;
49                                 throw new ControlLoopException("Unable to parse yaml into ControlLoopPolicy object");
50                         }
51                 } catch (Exception e) {
52                         //
53                         // Most likely this is a YAML Exception
54                         //
55                         throw new ControlLoopException(e);
56                 }
57         }
58         
59         public ControlLoop getControlLoop() {
60                 return this.policy.controlLoop;
61         }
62         
63         public FinalResult      checkIsCurrentPolicyFinal() {
64                 return FinalResult.toResult(this.currentPolicy);
65         }
66         
67         public Policy   getCurrentPolicy() {
68                 for (Policy policy : this.policy.policies) {
69                         if (policy.id.equals(this.currentPolicy)) {
70                                 return policy;
71                         }
72                 }
73                 return null;
74         }
75         
76         public void     nextPolicyForResult(PolicyResult result) throws ControlLoopException {
77                 Policy policy = this.getCurrentPolicy();
78                 try {
79                         if (this.policy == null) {
80                                 throw new ControlLoopException("There is no current policy to determine where to go to.");
81                         }
82                         switch (result) {
83                         case SUCCESS:
84                                 this.currentPolicy = policy.success;
85                                 break;
86                         case FAILURE:
87                                 this.currentPolicy = policy.failure;
88                                 break;
89                         case FAILURE_TIMEOUT:
90                                 this.currentPolicy = policy.failure_timeout;
91                                 break;
92                         case FAILURE_RETRIES:
93                                 this.currentPolicy = policy.failure_retries;
94                                 break;
95                         case FAILURE_EXCEPTION:
96                                 this.currentPolicy = policy.failure_exception;
97                                 break;
98                         case FAILURE_GUARD:
99                                 this.currentPolicy = policy.failure_guard;
100                                 break;
101                         default:
102                                 throw new ControlLoopException("Bad policy result given: " + result);
103                         }
104                 } catch (ControlLoopException e) {
105                         this.currentPolicy = FinalResult.FINAL_FAILURE_EXCEPTION.toString();
106                         throw e;
107                 }
108         }
109         
110 }