3dbc25fcf69466204896d5a4c24404638c8929bc
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * controlloop processor
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.onap.policy.controlloop.ControlLoopException;
24 import org.onap.policy.controlloop.policy.ControlLoop;
25 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
26 import org.onap.policy.controlloop.policy.FinalResult;
27 import org.onap.policy.controlloop.policy.Policy;
28 import org.onap.policy.controlloop.policy.PolicyResult;
29 import org.yaml.snakeyaml.Yaml;
30 import org.yaml.snakeyaml.constructor.CustomClassLoaderConstructor;
31
32 public class ControlLoopProcessor {
33
34     private final String yaml;
35     private final ControlLoopPolicy policy;
36     private String currentNestedPolicyId = null;
37
38     /**
39      * Construct an instance from yaml.
40      * 
41      * @param yaml the yaml
42      * @throws ControlLoopException if an error occurs
43      */
44     public ControlLoopProcessor(String yaml) throws ControlLoopException {
45         this.yaml = yaml;
46         try {
47             final Yaml y = new Yaml(new CustomClassLoaderConstructor(ControlLoopPolicy.class,
48                     ControlLoopPolicy.class.getClassLoader()));
49             final Object obj = y.load(this.yaml);
50
51             this.policy = (ControlLoopPolicy) obj;
52             this.currentNestedPolicyId = this.policy.getControlLoop().getTrigger_policy();
53         } catch (final Exception e) {
54             //
55             // Most likely this is a YAML Exception
56             //
57             throw new ControlLoopException(e);
58         }
59     }
60
61     public ControlLoop getControlLoop() {
62         return this.policy.getControlLoop();
63     }
64
65     public FinalResult checkIsCurrentPolicyFinal() {
66         return FinalResult.toResult(this.currentNestedPolicyId);
67     }
68
69     /**
70      * Get the current policy.
71      * 
72      * @return the current policy
73      * @throws ControlLoopException if an error occurs
74      */
75     public Policy getCurrentPolicy() throws ControlLoopException {
76         if (this.policy == null || this.policy.getPolicies() == null) {
77             throw new ControlLoopException("There are no policies defined.");
78         }
79
80         for (final Policy nestedPolicy : this.policy.getPolicies()) {
81             if (nestedPolicy.getId().equals(this.currentNestedPolicyId)) {
82                 return nestedPolicy;
83             }
84         }
85         return null;
86     }
87
88     /**
89      * Get the next policy given a result of the current policy.
90      * 
91      * @param result the result of the current policy
92      * @throws ControlLoopException if an error occurs
93      */
94     public void nextPolicyForResult(PolicyResult result) throws ControlLoopException {
95         final Policy currentPolicy = this.getCurrentPolicy();
96         try {
97             if (currentPolicy == null) {
98                 throw new ControlLoopException("There is no current policy to determine where to go to.");
99             }
100             switch (result) {
101                 case SUCCESS:
102                     this.currentNestedPolicyId = currentPolicy.getSuccess();
103                     break;
104                 case FAILURE:
105                     this.currentNestedPolicyId = currentPolicy.getFailure();
106                     break;
107                 case FAILURE_TIMEOUT:
108                     this.currentNestedPolicyId = currentPolicy.getFailure_timeout();
109                     break;
110                 case FAILURE_RETRIES:
111                     this.currentNestedPolicyId = currentPolicy.getFailure_retries();
112                     break;
113                 case FAILURE_EXCEPTION:
114                     this.currentNestedPolicyId = currentPolicy.getFailure_exception();
115                     break;
116                 case FAILURE_GUARD:
117                 default:
118                     this.currentNestedPolicyId = currentPolicy.getFailure_guard();
119                     break;
120             }
121         } catch (final ControlLoopException e) {
122             this.currentNestedPolicyId = FinalResult.FINAL_FAILURE_EXCEPTION.toString();
123             throw e;
124         }
125     }
126 }