104e28cd08a6a62e937963ec19898e2fa42244e4
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.processor;
22
23 import java.io.Serializable;
24 import java.lang.reflect.InvocationTargetException;
25 import java.net.URLDecoder;
26 import java.nio.charset.StandardCharsets;
27 import java.util.stream.Collectors;
28 import lombok.Getter;
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.domain.models.legacy.LegacyPolicy;
43 import org.onap.policy.drools.domain.models.operational.Operation;
44 import org.onap.policy.drools.domain.models.operational.OperationalPolicy;
45 import org.onap.policy.drools.domain.models.operational.OperationalTarget;
46 import org.onap.policy.drools.system.PolicyEngineConstants;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.yaml.snakeyaml.Yaml;
51 import org.yaml.snakeyaml.constructor.CustomClassLoaderConstructor;
52
53 public class ControlLoopProcessor implements Serializable {
54     private static final long serialVersionUID = 1L;
55     private static final Logger logger = LoggerFactory.getLogger(ControlLoopProcessor.class);
56
57     private final ControlLoopPolicy policy;
58     private String currentNestedPolicyId;
59
60     // not serializable, thus must be transient
61     @Getter
62     private transient ToscaPolicy toscaOpPolicy;
63
64     // not serializable, thus must be transient
65     @Getter
66     private transient DroolsPolicy domainOpPolicy;
67
68     /**
69      * Construct an instance from yaml.
70      *
71      * @param yaml the yaml
72      * @throws ControlLoopException if an error occurs
73      */
74     public ControlLoopProcessor(String yaml) throws ControlLoopException {
75         try {
76             final Yaml y = new Yaml(new CustomClassLoaderConstructor(ControlLoopPolicy.class,
77                     ControlLoopPolicy.class.getClassLoader()));
78             final Object obj = y.load(yaml);
79
80             this.policy = (ControlLoopPolicy) obj;
81             this.currentNestedPolicyId = this.policy.getControlLoop().getTrigger_policy();
82         } catch (final Exception e) {
83             //
84             // Most likely this is a YAML Exception
85             //
86             throw new ControlLoopException(e);
87         }
88     }
89
90     /**
91      * Create an instance from a Tosca Policy.
92      */
93     public ControlLoopProcessor(ToscaPolicy toscaPolicy) throws ControlLoopException {
94         try {
95             // TODO: automate policy type to models mapping
96             this.policy =
97                 ("onap.policies.controlloop.Operational".equals(toscaPolicy.getType()))
98                     ? buildPolicyFromToscaLegacy(toscaPolicy)
99                         : buildPolicyFromToscaCompliant(toscaPolicy);
100
101             this.currentNestedPolicyId = this.policy.getControlLoop().getTrigger_policy();
102             this.toscaOpPolicy = toscaPolicy;
103         } catch (RuntimeException | CoderException e) {
104             throw new ControlLoopException(e);
105         }
106     }
107
108     protected ControlLoopPolicy buildPolicyFromToscaLegacy(ToscaPolicy policy)
109             throws CoderException {
110         LegacyPolicy legacyPolicy =
111                 PolicyEngineConstants.getManager().getDomainMaker().convertTo(policy, LegacyPolicy.class);
112         this.domainOpPolicy = legacyPolicy;
113         String decodedPolicy = URLDecoder.decode(legacyPolicy.getProperties().getContent(), StandardCharsets.UTF_8);
114         return new Yaml(
115                 new CustomClassLoaderConstructor(
116                         ControlLoopPolicy.class, ControlLoopPolicy.class.getClassLoader())).load(decodedPolicy);
117     }
118
119     private Target toStandardTarget(OperationalTarget opTarget) {
120         Target target = new Target(TargetType.valueOf(opTarget.getTargetType()));
121         try {
122             BeanUtils.populate(target, opTarget.getEntityIds());
123         } catch (IllegalAccessException | InvocationTargetException e) {
124             logger.warn("target entityIds cannot be mapped (unexpected): {}", opTarget, e);
125         }
126         return target;
127     }
128
129     protected ControlLoopPolicy buildPolicyFromToscaCompliant(ToscaPolicy policy) throws CoderException {
130         OperationalPolicy domainPolicy =
131                 PolicyEngineConstants.getManager().getDomainMaker().convertTo(policy, OperationalPolicy.class);
132
133         ControlLoopPolicy backwardsCompatiblePolicy = new ControlLoopPolicy();
134
135         // @formatter:off
136         backwardsCompatiblePolicy.setPolicies(
137             domainPolicy.getProperties().getOperations().stream().map(this::convertPolicy)
138                     .collect(Collectors.toList()));
139         // @formatter:on
140
141         ControlLoop controlLoop = new ControlLoop();
142         controlLoop.setAbatement(domainPolicy.getProperties().isAbatement());
143         controlLoop.setControlLoopName(domainPolicy.getProperties().getId());
144         controlLoop.setTimeout(domainPolicy.getProperties().getTimeout());
145         controlLoop.setTrigger_policy(domainPolicy.getProperties().getTrigger());
146         controlLoop.setVersion(domainPolicy.getVersion());
147
148         backwardsCompatiblePolicy.setControlLoop(controlLoop);
149         this.domainOpPolicy = domainPolicy;
150         return backwardsCompatiblePolicy;
151     }
152
153     private Policy convertPolicy(Operation operation) {
154         // @formatter:off
155         Policy newPolicy = new Policy(PolicyParam.builder()
156                 .id(operation.getId())
157                 .name(operation.getActorOperation().getOperation())
158                 .description(operation.getDescription())
159                 .actor(operation.getActorOperation().getActor())
160                 .payload(operation.getActorOperation().getPayload())
161                 .recipe(operation.getActorOperation().getOperation())
162                 .retries(operation.getRetries())
163                 .timeout(operation.getTimeout())
164                 .target(toStandardTarget(operation.getActorOperation().getTarget()))
165             .build());
166         // @formatter:on
167
168         newPolicy.setSuccess(operation.getSuccess());
169         newPolicy.setFailure(operation.getFailure());
170         newPolicy.setFailure_exception(operation.getFailureException());
171         newPolicy.setFailure_guard(operation.getFailureGuard());
172         newPolicy.setFailure_retries(operation.getFailureRetries());
173         newPolicy.setFailure_timeout(operation.getFailureTimeout());
174
175         return newPolicy;
176     }
177
178     /**
179      * Get ControlLoopParams.
180      */
181     public ControlLoopParams getControlLoopParams() {
182         ControlLoopParams controlLoopParams = new ControlLoopParams();
183
184         controlLoopParams.setClosedLoopControlName(this.getControlLoop().getControlLoopName());
185         controlLoopParams.setPolicyScope(domainOpPolicy.getType() + ":" + domainOpPolicy.getTypeVersion());
186         controlLoopParams.setPolicyName(domainOpPolicy.getName());
187         controlLoopParams.setPolicyVersion(domainOpPolicy.getVersion());
188         controlLoopParams.setToscaPolicy(toscaOpPolicy);
189
190         return controlLoopParams;
191     }
192
193     public ControlLoop getControlLoop() {
194         return this.policy.getControlLoop();
195     }
196
197     public FinalResult checkIsCurrentPolicyFinal() {
198         return FinalResult.toResult(this.currentNestedPolicyId);
199     }
200
201     /**
202      * Get the current policy.
203      *
204      * @return the current policy
205      * @throws ControlLoopException if an error occurs
206      */
207     public Policy getCurrentPolicy() throws ControlLoopException {
208         if (this.policy == null || this.policy.getPolicies() == null) {
209             throw new ControlLoopException("There are no policies defined.");
210         }
211
212         for (final Policy nestedPolicy : this.policy.getPolicies()) {
213             if (nestedPolicy.getId().equals(this.currentNestedPolicyId)) {
214                 return nestedPolicy;
215             }
216         }
217         return null;
218     }
219
220     /**
221      * Get the next policy given a result of the current policy.
222      *
223      * @param result the result of the current policy
224      * @throws ControlLoopException if an error occurs
225      */
226     public void nextPolicyForResult(PolicyResult result) throws ControlLoopException {
227         final Policy currentPolicy = this.getCurrentPolicy();
228         try {
229             if (currentPolicy == null) {
230                 throw new ControlLoopException("There is no current policy to determine where to go to.");
231             }
232             switch (result) {
233                 case SUCCESS:
234                     this.currentNestedPolicyId = currentPolicy.getSuccess();
235                     break;
236                 case FAILURE:
237                     this.currentNestedPolicyId = currentPolicy.getFailure();
238                     break;
239                 case FAILURE_TIMEOUT:
240                     this.currentNestedPolicyId = currentPolicy.getFailure_timeout();
241                     break;
242                 case FAILURE_RETRIES:
243                     this.currentNestedPolicyId = currentPolicy.getFailure_retries();
244                     break;
245                 case FAILURE_EXCEPTION:
246                     this.currentNestedPolicyId = currentPolicy.getFailure_exception();
247                     break;
248                 case FAILURE_GUARD:
249                 default:
250                     this.currentNestedPolicyId = currentPolicy.getFailure_guard();
251                     break;
252             }
253         } catch (final ControlLoopException e) {
254             this.currentNestedPolicyId = FinalResult.FINAL_FAILURE_EXCEPTION.toString();
255             throw e;
256         }
257     }
258 }