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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.processor;
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;
33 public class ControlLoopProcessor implements Serializable {
34 private static final long serialVersionUID = 1L;
36 private final String yaml;
37 private final ControlLoopPolicy policy;
38 private String currentNestedPolicyId = null;
41 * Construct an instance from yaml.
43 * @param yaml the yaml
44 * @throws ControlLoopException if an error occurs
46 public ControlLoopProcessor(String yaml) throws ControlLoopException {
49 final Yaml y = new Yaml(new CustomClassLoaderConstructor(ControlLoopPolicy.class,
50 ControlLoopPolicy.class.getClassLoader()));
51 final Object obj = y.load(this.yaml);
53 this.policy = (ControlLoopPolicy) obj;
54 this.currentNestedPolicyId = this.policy.getControlLoop().getTrigger_policy();
55 } catch (final Exception e) {
57 // Most likely this is a YAML Exception
59 throw new ControlLoopException(e);
63 public ControlLoop getControlLoop() {
64 return this.policy.getControlLoop();
67 public FinalResult checkIsCurrentPolicyFinal() {
68 return FinalResult.toResult(this.currentNestedPolicyId);
72 * Get the current policy.
74 * @return the current policy
75 * @throws ControlLoopException if an error occurs
77 public Policy getCurrentPolicy() throws ControlLoopException {
78 if (this.policy == null || this.policy.getPolicies() == null) {
79 throw new ControlLoopException("There are no policies defined.");
82 for (final Policy nestedPolicy : this.policy.getPolicies()) {
83 if (nestedPolicy.getId().equals(this.currentNestedPolicyId)) {
91 * Get the next policy given a result of the current policy.
93 * @param result the result of the current policy
94 * @throws ControlLoopException if an error occurs
96 public void nextPolicyForResult(PolicyResult result) throws ControlLoopException {
97 final Policy currentPolicy = this.getCurrentPolicy();
99 if (currentPolicy == null) {
100 throw new ControlLoopException("There is no current policy to determine where to go to.");
104 this.currentNestedPolicyId = currentPolicy.getSuccess();
107 this.currentNestedPolicyId = currentPolicy.getFailure();
109 case FAILURE_TIMEOUT:
110 this.currentNestedPolicyId = currentPolicy.getFailure_timeout();
112 case FAILURE_RETRIES:
113 this.currentNestedPolicyId = currentPolicy.getFailure_retries();
115 case FAILURE_EXCEPTION:
116 this.currentNestedPolicyId = currentPolicy.getFailure_exception();
120 this.currentNestedPolicyId = currentPolicy.getFailure_guard();
123 } catch (final ControlLoopException e) {
124 this.currentNestedPolicyId = FinalResult.FINAL_FAILURE_EXCEPTION.toString();