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