2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2020 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;
25 import org.onap.policy.common.utils.coder.CoderException;
26 import org.onap.policy.common.utils.coder.StandardYamlCoder;
27 import org.onap.policy.controlloop.ControlLoopException;
28 import org.onap.policy.controlloop.actorserviceprovider.OperationFinalResult;
29 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
30 import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
31 import org.onap.policy.drools.domain.models.operational.Operation;
32 import org.onap.policy.drools.domain.models.operational.OperationalPolicy;
33 import org.onap.policy.drools.system.PolicyEngineConstants;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
37 public class ControlLoopProcessor implements Serializable {
38 private static final long serialVersionUID = 1L;
40 private static final StandardYamlCoder coder = new StandardYamlCoder();
43 private final OperationalPolicy policy;
44 private String currentNestedPolicyId;
46 // not serializable, thus must be transient
48 private transient ToscaPolicy toscaOpPolicy;
51 //private ControlLoop controlLoop;
54 * Construct an instance from yaml.
56 * @param yaml the yaml
57 * @throws ControlLoopException if an error occurs
59 public ControlLoopProcessor(String yaml) throws ControlLoopException {
60 this(decodeTosca(yaml));
64 * Create an instance from a Tosca Policy.
66 public ControlLoopProcessor(ToscaPolicy toscaPolicy) throws ControlLoopException {
69 PolicyEngineConstants.getManager().getDomainMaker().convertTo(toscaPolicy, OperationalPolicy.class);
70 this.currentNestedPolicyId = this.policy.getProperties().getTrigger();
71 this.toscaOpPolicy = toscaPolicy;
72 } catch (RuntimeException | CoderException e) {
73 throw new ControlLoopException(e);
77 private static ToscaPolicy decodeTosca(String yaml) throws ControlLoopException {
79 ToscaServiceTemplate template = coder.decode(yaml, ToscaServiceTemplate.class);
80 if (template == null || template.getToscaTopologyTemplate() == null) {
81 throw new IllegalArgumentException("Cannot decode yaml into ToscaServiceTemplate");
84 return template.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next();
86 } catch (final Exception e) {
88 // Most likely this is a YAML Exception
90 throw new ControlLoopException(e);
95 * Get ControlLoopParams.
97 public ControlLoopParams getControlLoopParams() {
98 ControlLoopParams controlLoopParams = new ControlLoopParams();
100 controlLoopParams.setClosedLoopControlName(this.policy.getProperties().getId());
101 controlLoopParams.setPolicyScope(policy.getType() + ":" + policy.getTypeVersion());
102 controlLoopParams.setPolicyName(policy.getName());
103 controlLoopParams.setPolicyVersion(policy.getVersion());
104 controlLoopParams.setToscaPolicy(toscaOpPolicy);
106 return controlLoopParams;
109 public OperationFinalResult checkIsCurrentPolicyFinal() {
110 return OperationFinalResult.toResult(this.currentNestedPolicyId);
114 * Get the current policy.
116 * @return the current policy
117 * @throws ControlLoopException if an error occurs
119 public Operation getCurrentPolicy() throws ControlLoopException {
120 if (this.policy == null || this.policy.getProperties() == null
121 || this.policy.getProperties().getOperations() == null
122 || this.policy.getProperties().getOperations().isEmpty()) {
123 throw new ControlLoopException("There are no policies defined.");
126 for (final Operation nestedPolicy : this.policy.getProperties().getOperations()) {
127 if (nestedPolicy.getId().equals(this.currentNestedPolicyId)) {
135 * Get the next policy given a result of the current policy.
137 * @param result the result of the current policy
138 * @throws ControlLoopException if an error occurs
140 public void nextPolicyForResult(OperationResult result) throws ControlLoopException {
141 final Operation currentPolicy = this.getCurrentPolicy();
143 if (currentPolicy == null) {
144 throw new ControlLoopException("There is no current policy to determine where to go to.");
148 this.currentNestedPolicyId = currentPolicy.getSuccess();
151 this.currentNestedPolicyId = currentPolicy.getFailure();
153 case FAILURE_TIMEOUT:
154 this.currentNestedPolicyId = currentPolicy.getFailureTimeout();
156 case FAILURE_RETRIES:
157 this.currentNestedPolicyId = currentPolicy.getFailureRetries();
159 case FAILURE_EXCEPTION:
160 this.currentNestedPolicyId = currentPolicy.getFailureException();
164 this.currentNestedPolicyId = currentPolicy.getFailureGuard();
167 } catch (final ControlLoopException e) {
168 this.currentNestedPolicyId = OperationFinalResult.FINAL_FAILURE_EXCEPTION.toString();