2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.openecomp.policy.controlloop.policy.builder.impl;
23 import java.util.LinkedList;
25 import java.util.UUID;
27 import org.openecomp.policy.controlloop.compiler.CompilerException;
28 import org.openecomp.policy.controlloop.compiler.ControlLoopCompiler;
29 import org.openecomp.policy.controlloop.compiler.ControlLoopCompilerCallback;
30 import org.openecomp.policy.controlloop.policy.ControlLoop;
31 import org.openecomp.policy.controlloop.policy.ControlLoopPolicy;
32 import org.openecomp.policy.controlloop.policy.FinalResult;
33 import org.openecomp.policy.controlloop.policy.OperationsAccumulateParams;
34 import org.openecomp.policy.controlloop.policy.Policy;
35 import org.openecomp.policy.controlloop.policy.PolicyResult;
36 import org.openecomp.policy.controlloop.policy.Target;
37 import org.openecomp.policy.controlloop.policy.builder.BuilderException;
38 import org.openecomp.policy.controlloop.policy.builder.ControlLoopPolicyBuilder;
39 import org.openecomp.policy.controlloop.policy.builder.MessageLevel;
40 import org.openecomp.policy.controlloop.policy.builder.Results;
41 import org.yaml.snakeyaml.DumperOptions;
42 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
43 import org.yaml.snakeyaml.Yaml;
45 public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder {
47 private ControlLoopPolicy policy;
49 public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout) throws BuilderException {
50 policy = new ControlLoopPolicy();
51 policy.controlLoop = new ControlLoop();
52 policy.controlLoop.controlLoopName = controlLoopName;
53 policy.controlLoop.timeout = timeout;
57 public ControlLoopPolicyBuilder setAbatement(Boolean abatement) throws BuilderException{
58 if (abatement == null) {
59 throw new BuilderException("abatement must not be null");
61 policy.controlLoop.abatement = abatement;
66 public ControlLoopPolicyBuilder setTimeout(Integer timeout) {
67 policy.controlLoop.timeout = timeout;
72 public Policy setTriggerPolicy(String name, String description, String actor, Target target, String recipe,
73 Map<String, String> payload, Integer retries, Integer timeout) throws BuilderException {
75 Policy trigger = new Policy(UUID.randomUUID().toString(), name, description, actor, payload, target, recipe, retries, timeout);
77 policy.controlLoop.trigger_policy = trigger.id;
79 this.addNewPolicy(trigger);
81 // Return a copy of the policy
83 return new Policy(trigger);
87 public Policy setPolicyForPolicyResult(String name, String description, String actor,
88 Target target, String recipe, Map<String, String> payload, Integer retries, Integer timeout, String policyID, PolicyResult... results) throws BuilderException {
90 // Find the existing policy
92 Policy existingPolicy = this.findPolicy(policyID);
93 if (existingPolicy == null) {
94 throw new BuilderException("Unknown policy " + policyID);
97 // Create the new Policy
99 Policy newPolicy = new Policy(UUID.randomUUID().toString(), name, description, actor, payload, target, recipe, retries, timeout);
101 // Connect the results
103 for (PolicyResult result : results) {
106 existingPolicy.failure = newPolicy.id;
108 case FAILURE_EXCEPTION:
109 existingPolicy.failure_exception = newPolicy.id;
111 case FAILURE_RETRIES:
112 existingPolicy.failure_retries = newPolicy.id;
114 case FAILURE_TIMEOUT:
115 existingPolicy.failure_timeout = newPolicy.id;
118 existingPolicy.failure_guard = newPolicy.id;
121 existingPolicy.success = newPolicy.id;
124 throw new BuilderException("Invalid PolicyResult " + result);
128 // Add it to our list
130 this.policy.policies.add(newPolicy);
132 // Return a policy to them
134 return new Policy(newPolicy);
137 private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
139 public ResultsImpl results = new ResultsImpl();
142 public boolean onWarning(String message) {
143 results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
148 public boolean onError(String message) {
149 results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
155 public Results buildSpecification() {
157 // Dump the specification
159 DumperOptions options = new DumperOptions();
160 options.setDefaultFlowStyle(FlowStyle.BLOCK);
161 options.setPrettyFlow(true);
162 Yaml yaml = new Yaml(options);
163 String dumpedYaml = yaml.dump(policy);
165 // This is our callback class for our compiler
167 BuilderCompilerCallback callback = new BuilderCompilerCallback();
172 ControlLoopCompiler.compile(policy, callback);
173 } catch (CompilerException e) {
174 callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
179 callback.results.setSpecification(dumpedYaml);
180 return callback.results;
183 private void addNewPolicy(Policy policy) {
184 if (this.policy.policies == null) {
185 this.policy.policies = new LinkedList<Policy>();
187 this.policy.policies.add(policy);
190 private Policy findPolicy(String id) {
191 for (Policy policy : this.policy.policies) {
192 if (policy.id.equals(id)) {
200 public Integer calculateTimeout() {
202 for (Policy policy : this.policy.policies) {
203 sum += policy.timeout.intValue();
205 return new Integer(sum);
209 public ControlLoop setTriggerPolicy(String id) throws BuilderException {
211 throw new BuilderException("Id must not be null");
213 Policy trigger = this.findPolicy(id);
214 if (trigger == null) {
215 throw new BuilderException("Unknown policy " + id);
218 this.policy.controlLoop.trigger_policy = id;
220 return new ControlLoop(this.policy.controlLoop);
224 public boolean isOpenLoop() {
225 if (this.policy.controlLoop.trigger_policy.equals(FinalResult.FINAL_OPENLOOP.toString())) {
234 public Policy getTriggerPolicy() throws BuilderException {
235 if (this.policy.controlLoop.trigger_policy.equals(FinalResult.FINAL_OPENLOOP.toString())) {
239 Policy trigger = new Policy(this.findPolicy(this.policy.controlLoop.trigger_policy));
245 public ControlLoop getControlLoop() {
246 ControlLoop loop = new ControlLoop(this.policy.controlLoop);
251 public Policy setPolicyForPolicyResult(String policyResultID, String policyID, PolicyResult... results)
252 throws BuilderException {
254 // Find the existing policy
256 Policy existingPolicy = this.findPolicy(policyID);
257 if (existingPolicy == null) {
258 throw new BuilderException(policyID + " does not exist");
260 if (this.findPolicy(policyResultID) == null) {
261 throw new BuilderException("Operational policy " + policyResultID + " does not exist");
264 // Connect the results
266 for (PolicyResult result : results) {
269 existingPolicy.failure = policyResultID;
271 case FAILURE_EXCEPTION:
272 existingPolicy.failure_exception = policyResultID;
274 case FAILURE_RETRIES:
275 existingPolicy.failure_retries = policyResultID;
277 case FAILURE_TIMEOUT:
278 existingPolicy.failure_timeout = policyResultID;
281 existingPolicy.failure_guard = policyResultID;
284 existingPolicy.success = policyResultID;
287 throw new BuilderException("Invalid PolicyResult " + result);
290 return new Policy(this.findPolicy(policyResultID));
294 public boolean removePolicy(String policyID) throws BuilderException {
295 Policy existingPolicy = this.findPolicy(policyID);
296 if (existingPolicy == null) {
297 throw new BuilderException("Unknown policy " + policyID);
300 // Check if the policy to remove is trigger_policy
302 if (this.policy.controlLoop.trigger_policy.equals(policyID)) {
303 this.policy.controlLoop.trigger_policy = FinalResult.FINAL_OPENLOOP.toString();
309 for (Policy policy : this.policy.policies) {
310 int index = this.policy.policies.indexOf(policy);
311 if (policy.success.equals(policyID)) {
312 policy.success = FinalResult.FINAL_SUCCESS.toString();
314 if (policy.failure.equals(policyID)) {
315 policy.failure = FinalResult.FINAL_FAILURE.toString();
317 if (policy.failure_retries.equals(policyID)) {
318 policy.failure_retries = FinalResult.FINAL_FAILURE_RETRIES.toString();
320 if (policy.failure_timeout.equals(policyID)) {
321 policy.failure_timeout = FinalResult.FINAL_FAILURE_TIMEOUT.toString();
323 if (policy.failure_exception.equals(policyID)) {
324 policy.failure_exception = FinalResult.FINAL_FAILURE_EXCEPTION.toString();
326 if (policy.failure_guard.equals(policyID)) {
327 policy.failure_guard = FinalResult.FINAL_FAILURE_GUARD.toString();
329 this.policy.policies.set(index, policy);
335 boolean removed = this.policy.policies.remove(existingPolicy);
340 public Policy resetPolicyResults(String policyID) throws BuilderException {
341 Policy existingPolicy = this.findPolicy(policyID);
342 if (existingPolicy == null) {
343 throw new BuilderException("Unknown policy " + policyID);
346 // reset policy results
348 existingPolicy.success = FinalResult.FINAL_SUCCESS.toString();
349 existingPolicy.failure = FinalResult.FINAL_FAILURE.toString();
350 existingPolicy.failure_retries = FinalResult.FINAL_FAILURE_RETRIES.toString();
351 existingPolicy.failure_timeout = FinalResult.FINAL_FAILURE_TIMEOUT.toString();
352 existingPolicy.failure_exception = FinalResult.FINAL_FAILURE_EXCEPTION.toString();
353 existingPolicy.failure_guard = FinalResult.FINAL_FAILURE_GUARD.toString();
354 return new Policy(existingPolicy);
358 public ControlLoopPolicyBuilder removeAllPolicies() {
360 // Remove all existing operational policies
362 this.policy.policies.clear();
364 // Revert controlLoop back to an open loop
366 this.policy.controlLoop.trigger_policy = FinalResult.FINAL_OPENLOOP.toString();
371 public Policy addOperationsAccumulateParams(String policyID, OperationsAccumulateParams operationsAccumulateParams) throws BuilderException {
372 Policy existingPolicy = this.findPolicy(policyID);
373 if (existingPolicy == null) {
374 throw new BuilderException("Unknown policy " + policyID);
377 // Add operationsAccumulateParams to existingPolicy
379 existingPolicy.operationsAccumulateParams = operationsAccumulateParams;
380 return new Policy(existingPolicy);