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;
24 import java.io.UnsupportedEncodingException;
25 import java.lang.reflect.InvocationTargetException;
26 import java.net.URLDecoder;
27 import java.util.stream.Collectors;
29 import org.apache.commons.beanutils.BeanUtils;
30 import org.onap.policy.common.utils.coder.CoderException;
31 import org.onap.policy.controlloop.ControlLoopException;
32 import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
33 import org.onap.policy.controlloop.policy.ControlLoop;
34 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
35 import org.onap.policy.controlloop.policy.FinalResult;
36 import org.onap.policy.controlloop.policy.Policy;
37 import org.onap.policy.controlloop.policy.PolicyParam;
38 import org.onap.policy.controlloop.policy.PolicyResult;
39 import org.onap.policy.controlloop.policy.Target;
40 import org.onap.policy.controlloop.policy.TargetType;
41 import org.onap.policy.drools.domain.models.DroolsPolicy;
42 import org.onap.policy.drools.models.domain.legacy.LegacyPolicy;
43 import org.onap.policy.drools.models.domain.operational.OperationalPolicy;
44 import org.onap.policy.drools.models.domain.operational.OperationalTarget;
45 import org.onap.policy.drools.system.PolicyEngineConstants;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.yaml.snakeyaml.Yaml;
50 import org.yaml.snakeyaml.constructor.CustomClassLoaderConstructor;
52 public class ControlLoopProcessor implements Serializable {
53 private static final long serialVersionUID = 1L;
54 private static final Logger logger = LoggerFactory.getLogger(ControlLoopProcessor.class);
56 private final ControlLoopPolicy policy;
57 private String currentNestedPolicyId = null;
60 private ToscaPolicy toscaOpPolicy;
63 private DroolsPolicy domainOpPolicy;
66 * Construct an instance from yaml.
68 * @param yaml the yaml
69 * @throws ControlLoopException if an error occurs
71 public ControlLoopProcessor(String yaml) throws ControlLoopException {
73 final Yaml y = new Yaml(new CustomClassLoaderConstructor(ControlLoopPolicy.class,
74 ControlLoopPolicy.class.getClassLoader()));
75 final Object obj = y.load(yaml);
77 this.policy = (ControlLoopPolicy) obj;
78 this.currentNestedPolicyId = this.policy.getControlLoop().getTrigger_policy();
79 } catch (final Exception e) {
81 // Most likely this is a YAML Exception
83 throw new ControlLoopException(e);
88 * Create an instance from a Tosca Policy.
90 public ControlLoopProcessor(ToscaPolicy toscaPolicy) throws ControlLoopException {
92 // TODO: automate policy type to models mapping
94 ("onap.policies.controlloop.Operational".equals(toscaPolicy.getType()))
95 ? buildPolicyFromToscaLegacy(toscaPolicy)
96 : buildPolicyFromToscaCompliant(toscaPolicy);
98 this.currentNestedPolicyId = this.policy.getControlLoop().getTrigger_policy();
99 this.toscaOpPolicy = toscaPolicy;
100 } catch (RuntimeException | CoderException | UnsupportedEncodingException e) {
101 throw new ControlLoopException(e);
105 protected ControlLoopPolicy buildPolicyFromToscaLegacy(ToscaPolicy policy)
106 throws UnsupportedEncodingException, CoderException {
107 LegacyPolicy legacyPolicy =
108 PolicyEngineConstants.getManager().getDomainMaker().convertTo(policy, LegacyPolicy.class);
109 this.domainOpPolicy = legacyPolicy;
110 String decodedPolicy = URLDecoder.decode(legacyPolicy.getProperties().getContent(), "UTF-8");
112 new CustomClassLoaderConstructor(
113 ControlLoopPolicy.class, ControlLoopPolicy.class.getClassLoader())).load(decodedPolicy);
116 private Target toStandardTarget(OperationalTarget opTarget) {
117 Target target = new Target(TargetType.valueOf(opTarget.getTargetType()));
119 BeanUtils.populate(target, opTarget.getEntityIds());
120 } catch (IllegalAccessException | InvocationTargetException e) {
121 logger.warn("target entityIds cannot be mapped (unexpected): {}", opTarget, e);
126 protected ControlLoopPolicy buildPolicyFromToscaCompliant(ToscaPolicy policy) throws CoderException {
127 OperationalPolicy domainPolicy =
128 PolicyEngineConstants.getManager().getDomainMaker().convertTo(policy, OperationalPolicy.class);
130 ControlLoopPolicy backwardsCompatiblePolicy = new ControlLoopPolicy();
133 backwardsCompatiblePolicy.setPolicies(
134 domainPolicy.getProperties().getOperations().stream().map(operation -> new Policy(
135 PolicyParam.builder()
136 .id(operation.getId())
137 .name(operation.getActorOperation().getOperation())
138 .description(operation.getDescription())
139 .actor(operation.getActorOperation().getActor())
140 .payload(operation.getActorOperation().getPayload())
141 .recipe(operation.getActorOperation().getOperation())
142 .retries(operation.getRetries())
143 .timeout(operation.getTimeout())
144 .target(toStandardTarget(operation.getActorOperation().getTarget()))
146 .collect(Collectors.toList()));
149 ControlLoop controlLoop = new ControlLoop();
150 controlLoop.setAbatement(domainPolicy.getProperties().isAbatement());
151 controlLoop.setControlLoopName(domainPolicy.getProperties().getId());
152 controlLoop.setTimeout(domainPolicy.getProperties().getTimeout());
153 controlLoop.setTrigger_policy(domainPolicy.getProperties().getTrigger());
154 controlLoop.setVersion(domainPolicy.getVersion());
156 backwardsCompatiblePolicy.setControlLoop(controlLoop);
157 this.domainOpPolicy = domainPolicy;
158 return backwardsCompatiblePolicy;
162 * Get ControlLoopParams.
164 public ControlLoopParams getControlLoopParams() {
165 ControlLoopParams controlLoopParams = new ControlLoopParams();
167 controlLoopParams.setClosedLoopControlName(this.getControlLoop().getControlLoopName());
168 controlLoopParams.setPolicyScope(domainOpPolicy.getType() + ":" + domainOpPolicy.getTypeVersion());
169 controlLoopParams.setPolicyName(domainOpPolicy.getName());
170 controlLoopParams.setPolicyVersion(domainOpPolicy.getVersion());
171 controlLoopParams.setToscaPolicy(toscaOpPolicy);
173 return controlLoopParams;
176 public ControlLoop getControlLoop() {
177 return this.policy.getControlLoop();
180 public FinalResult checkIsCurrentPolicyFinal() {
181 return FinalResult.toResult(this.currentNestedPolicyId);
185 * Get the current policy.
187 * @return the current policy
188 * @throws ControlLoopException if an error occurs
190 public Policy getCurrentPolicy() throws ControlLoopException {
191 if (this.policy == null || this.policy.getPolicies() == null) {
192 throw new ControlLoopException("There are no policies defined.");
195 for (final Policy nestedPolicy : this.policy.getPolicies()) {
196 if (nestedPolicy.getId().equals(this.currentNestedPolicyId)) {
204 * Get the next policy given a result of the current policy.
206 * @param result the result of the current policy
207 * @throws ControlLoopException if an error occurs
209 public void nextPolicyForResult(PolicyResult result) throws ControlLoopException {
210 final Policy currentPolicy = this.getCurrentPolicy();
212 if (currentPolicy == null) {
213 throw new ControlLoopException("There is no current policy to determine where to go to.");
217 this.currentNestedPolicyId = currentPolicy.getSuccess();
220 this.currentNestedPolicyId = currentPolicy.getFailure();
222 case FAILURE_TIMEOUT:
223 this.currentNestedPolicyId = currentPolicy.getFailure_timeout();
225 case FAILURE_RETRIES:
226 this.currentNestedPolicyId = currentPolicy.getFailure_retries();
228 case FAILURE_EXCEPTION:
229 this.currentNestedPolicyId = currentPolicy.getFailure_exception();
233 this.currentNestedPolicyId = currentPolicy.getFailure_guard();
236 } catch (final ControlLoopException e) {
237 this.currentNestedPolicyId = FinalResult.FINAL_FAILURE_EXCEPTION.toString();