4512443926bf273f901b4664e170b9b70497cf95
[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.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         public ControlLoopProcessor(String yaml) throws ControlLoopException {
39                 this.yaml = yaml;
40                 try {
41                         final Yaml y = new Yaml(new CustomClassLoaderConstructor(ControlLoopPolicy.class,       ControlLoopPolicy.class.getClassLoader()));
42                         final Object obj = y.load(this.yaml);
43
44                         this.policy = (ControlLoopPolicy) obj;
45                         this.currentNestedPolicyID = this.policy.getControlLoop().getTrigger_policy();
46                 } catch (final Exception e) {
47                         //
48                         // Most likely this is a YAML Exception
49                         //
50                         throw new ControlLoopException(e);
51                 }
52         }
53
54         public ControlLoop getControlLoop() {
55                 return this.policy.getControlLoop();
56         }
57
58         public FinalResult checkIsCurrentPolicyFinal() {
59                 return FinalResult.toResult(this.currentNestedPolicyID);
60         }
61
62         public Policy getCurrentPolicy() throws ControlLoopException {
63                 if (this.policy == null || this.policy.getPolicies() == null) {
64                         throw new ControlLoopException("There are no policies defined.");
65                 }
66
67                 for (final Policy nestedPolicy : this.policy.getPolicies()) {
68                         if (nestedPolicy.getId().equals(this.currentNestedPolicyID)) {
69                                 return nestedPolicy;
70                         }
71                 }
72                 return null;
73         }
74
75         public void nextPolicyForResult(PolicyResult result) throws ControlLoopException {
76                 final Policy currentPolicy = this.getCurrentPolicy();
77                 try {
78                         if (currentPolicy == null) {
79                                 throw new ControlLoopException("There is no current policy to determine where to go to.");
80                         }
81                         switch (result) {
82                         case SUCCESS:
83                                 this.currentNestedPolicyID = currentPolicy.getSuccess();
84                                 break;
85                         case FAILURE:
86                                 this.currentNestedPolicyID = currentPolicy.getFailure();
87                                 break;
88                         case FAILURE_TIMEOUT:
89                                 this.currentNestedPolicyID = currentPolicy.getFailure_timeout();
90                                 break;
91                         case FAILURE_RETRIES:
92                                 this.currentNestedPolicyID = currentPolicy.getFailure_retries();
93                                 break;
94                         case FAILURE_EXCEPTION:
95                                 this.currentNestedPolicyID = currentPolicy.getFailure_exception();
96                                 break;
97                         case FAILURE_GUARD:
98                                 this.currentNestedPolicyID = currentPolicy.getFailure_guard();
99                                 break;
100                         }
101                 } catch (final ControlLoopException e) {
102                         this.currentNestedPolicyID = FinalResult.FINAL_FAILURE_EXCEPTION.toString();
103                         throw e;
104                 }
105         }
106 }