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
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 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;
32 public class ControlLoopProcessor {
34 private final String yaml;
35 private final ControlLoopPolicy policy;
36 private String currentPolicy = null;
38 public ControlLoopProcessor(String yaml) throws ControlLoopException {
41 final Yaml y = new Yaml(new CustomClassLoaderConstructor(ControlLoopPolicy.class,
42 ControlLoopPolicy.class.getClassLoader()));
43 final Object obj = y.load(this.yaml);
44 if (obj instanceof ControlLoopPolicy) {
45 this.policy = (ControlLoopPolicy) obj;
46 this.currentPolicy = this.policy.getControlLoop().getTrigger_policy();
49 throw new ControlLoopException("Unable to parse yaml into ControlLoopPolicy object");
51 } catch (final Exception e) {
53 // Most likely this is a YAML Exception
55 throw new ControlLoopException(e);
59 public ControlLoop getControlLoop() {
60 return this.policy.getControlLoop();
63 public FinalResult checkIsCurrentPolicyFinal() {
64 return FinalResult.toResult(this.currentPolicy);
67 public Policy getCurrentPolicy() {
68 for (final Policy policy : this.policy.getPolicies()) {
69 if (policy.getId().equals(this.currentPolicy)) {
76 public void nextPolicyForResult(PolicyResult result) throws ControlLoopException {
77 final Policy policy = this.getCurrentPolicy();
79 if (this.policy == null) {
80 throw new ControlLoopException("There is no current policy to determine where to go to.");
84 this.currentPolicy = policy.getSuccess();
87 this.currentPolicy = policy.getFailure();
90 this.currentPolicy = policy.getFailure_timeout();
93 this.currentPolicy = policy.getFailure_retries();
95 case FAILURE_EXCEPTION:
96 this.currentPolicy = policy.getFailure_exception();
99 this.currentPolicy = policy.getFailure_guard();
102 throw new ControlLoopException("Bad policy result given: " + result);
104 } catch (final ControlLoopException e) {
105 this.currentPolicy = FinalResult.FINAL_FAILURE_EXCEPTION.toString();