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