2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2021 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 * Construct an instance from yaml.
53 * @param yaml the yaml
54 * @throws ControlLoopException if an error occurs
56 public ControlLoopProcessor(String yaml) throws ControlLoopException {
57 this(decodeTosca(yaml));
61 * Create an instance from a Tosca Policy.
63 public ControlLoopProcessor(ToscaPolicy toscaPolicy) throws ControlLoopException {
66 PolicyEngineConstants.getManager().getDomainMaker().convertTo(toscaPolicy, OperationalPolicy.class);
67 this.currentNestedPolicyId = this.policy.getProperties().getTrigger();
68 this.toscaOpPolicy = toscaPolicy;
69 } catch (RuntimeException | CoderException e) {
70 throw new ControlLoopException(e);
74 private static ToscaPolicy decodeTosca(String yaml) throws ControlLoopException {
76 ToscaServiceTemplate template = coder.decode(yaml, ToscaServiceTemplate.class);
77 if (template == null || template.getToscaTopologyTemplate() == null) {
78 throw new IllegalArgumentException("Cannot decode yaml into ToscaServiceTemplate");
81 return template.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next();
83 } catch (final Exception e) {
85 // Most likely this is a YAML Exception
87 throw new ControlLoopException(e);
92 * Get ControlLoopParams.
94 public ControlLoopParams getControlLoopParams() {
95 ControlLoopParams controlLoopParams = new ControlLoopParams();
97 controlLoopParams.setClosedLoopControlName(this.policy.getProperties().getId());
98 controlLoopParams.setPolicyScope(policy.getType() + ":" + policy.getTypeVersion());
99 controlLoopParams.setPolicyName(policy.getName());
100 controlLoopParams.setPolicyVersion(policy.getVersion());
101 controlLoopParams.setToscaPolicy(toscaOpPolicy);
103 return controlLoopParams;
106 public OperationFinalResult checkIsCurrentPolicyFinal() {
107 return OperationFinalResult.toResult(this.currentNestedPolicyId);
111 * Get the current policy.
113 * @return the current policy
114 * @throws ControlLoopException if an error occurs
116 public Operation getCurrentPolicy() throws ControlLoopException {
117 if (this.policy == null || this.policy.getProperties() == null
118 || this.policy.getProperties().getOperations() == null
119 || this.policy.getProperties().getOperations().isEmpty()) {
120 throw new ControlLoopException("There are no policies defined.");
123 for (final Operation nestedPolicy : this.policy.getProperties().getOperations()) {
124 if (nestedPolicy.getId().equals(this.currentNestedPolicyId)) {
132 * Get the next policy given a result of the current policy.
134 * @param result the result of the current policy
135 * @throws ControlLoopException if an error occurs
137 public void nextPolicyForResult(OperationResult result) throws ControlLoopException {
138 final Operation currentPolicy = this.getCurrentPolicy();
140 if (currentPolicy == null) {
141 throw new ControlLoopException("There is no current policy to determine where to go to.");
145 this.currentNestedPolicyId = currentPolicy.getSuccess();
148 this.currentNestedPolicyId = currentPolicy.getFailure();
150 case FAILURE_TIMEOUT:
151 this.currentNestedPolicyId = currentPolicy.getFailureTimeout();
153 case FAILURE_RETRIES:
154 this.currentNestedPolicyId = currentPolicy.getFailureRetries();
156 case FAILURE_EXCEPTION:
157 this.currentNestedPolicyId = currentPolicy.getFailureException();
161 this.currentNestedPolicyId = currentPolicy.getFailureGuard();
164 } catch (final ControlLoopException e) {
165 this.currentNestedPolicyId = OperationFinalResult.FINAL_FAILURE_EXCEPTION.toString();