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.onap.policy.controlloop.policy.builder.impl;
23 import com.google.common.base.Strings;
25 import java.util.LinkedList;
27 import java.util.UUID;
29 import org.onap.policy.aai.Pnf;
30 import org.onap.policy.controlloop.compiler.CompilerException;
31 import org.onap.policy.controlloop.compiler.ControlLoopCompiler;
32 import org.onap.policy.controlloop.compiler.ControlLoopCompilerCallback;
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.OperationsAccumulateParams;
37 import org.onap.policy.controlloop.policy.Policy;
38 import org.onap.policy.controlloop.policy.PolicyResult;
39 import org.onap.policy.controlloop.policy.Target;
40 import org.onap.policy.controlloop.policy.builder.BuilderException;
41 import org.onap.policy.controlloop.policy.builder.ControlLoopPolicyBuilder;
42 import org.onap.policy.controlloop.policy.builder.MessageLevel;
43 import org.onap.policy.controlloop.policy.builder.Results;
44 import org.onap.policy.sdc.Resource;
45 import org.onap.policy.sdc.Service;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.yaml.snakeyaml.DumperOptions;
49 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
50 import org.yaml.snakeyaml.Yaml;
52 public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder {
53 private static final String UNKNOWN_POLICY = "Unknown policy ";
54 private static Logger logger = LoggerFactory.getLogger(ControlLoopPolicyBuilderImpl.class.getName());
55 private ControlLoopPolicy controlLoopPolicy;
57 public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout) {
58 controlLoopPolicy = new ControlLoopPolicy();
59 ControlLoop controlLoop = new ControlLoop();
60 controlLoop.setControlLoopName(controlLoopName);
61 controlLoop.setTimeout(timeout);
62 controlLoopPolicy.setControlLoop(controlLoop);
65 public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Resource resource, Service... services)
66 throws BuilderException {
67 this(controlLoopName, timeout);
68 this.addResource(resource);
69 this.addService(services);
72 public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Pnf pnf) throws BuilderException {
73 this(controlLoopName, timeout);
77 public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Service service, Resource[] resources)
78 throws BuilderException {
79 this(controlLoopName, timeout);
80 this.addService(service);
81 this.addResource(resources);
85 public ControlLoopPolicyBuilder removePNF() throws BuilderException {
86 controlLoopPolicy.getControlLoop().setPnf(null);
91 public ControlLoopPolicyBuilder addService(Service... services) throws BuilderException {
92 for (Service service : services) {
93 if (service == null) {
94 throw new BuilderException("Service must not be null");
96 if (service.getServiceUUID() == null && Strings.isNullOrEmpty(service.getServiceName())) {
97 throw new BuilderException("Invalid service - need either a serviceUUID or serviceName");
99 if (controlLoopPolicy.getControlLoop().getServices() == null) {
100 controlLoopPolicy.getControlLoop().setServices(new LinkedList<>());
102 controlLoopPolicy.getControlLoop().getServices().add(service);
108 public ControlLoopPolicyBuilder removeService(Service... services) throws BuilderException {
109 if (controlLoopPolicy.getControlLoop().getServices() == null) {
110 throw new BuilderException("No existing services to remove");
112 for (Service service : services) {
113 if (service == null) {
114 throw new BuilderException("Service must not be null");
116 if (service.getServiceUUID() == null && Strings.isNullOrEmpty(service.getServiceName())) {
117 throw new BuilderException("Invalid service - need either a serviceUUID or serviceName");
119 boolean removed = controlLoopPolicy.getControlLoop().getServices().remove(service);
121 throw new BuilderException("Unknown service " + service.getServiceName());
128 public ControlLoopPolicyBuilder removeAllServices() throws BuilderException {
129 controlLoopPolicy.getControlLoop().getServices().clear();
135 public ControlLoopPolicyBuilder addResource(Resource... resources) throws BuilderException {
136 for (Resource resource : resources) {
137 if (resource == null) {
138 throw new BuilderException("Resource must not be null");
140 if (resource.getResourceUUID() == null && Strings.isNullOrEmpty(resource.getResourceName())) {
141 throw new BuilderException("Invalid resource - need either resourceUUID or resourceName");
143 if (controlLoopPolicy.getControlLoop().getResources() == null) {
144 controlLoopPolicy.getControlLoop().setResources(new LinkedList<>());
146 controlLoopPolicy.getControlLoop().getResources().add(resource);
152 public ControlLoopPolicyBuilder setPNF(Pnf pnf) throws BuilderException {
154 throw new BuilderException("PNF must not be null");
156 if (pnf.getPnfName() == null && pnf.getPnfType() == null) {
157 throw new BuilderException("Invalid PNF - need either pnfName or pnfType");
159 controlLoopPolicy.getControlLoop().setPnf(pnf);
164 public ControlLoopPolicyBuilder setAbatement(Boolean abatement) throws BuilderException {
165 if (abatement == null) {
166 throw new BuilderException("abatement must not be null");
168 controlLoopPolicy.getControlLoop().setAbatement(abatement);
173 public ControlLoopPolicyBuilder setTimeout(Integer timeout) {
174 controlLoopPolicy.getControlLoop().setTimeout(timeout);
179 public Policy setTriggerPolicy(String name, String description, String actor, Target target, String recipe,
180 Map<String, String> payload, Integer retries, Integer timeout) throws BuilderException {
182 Policy trigger = new Policy(UUID.randomUUID().toString(), name, description, actor, payload, target, recipe,
185 controlLoopPolicy.getControlLoop().setTrigger_policy(trigger.getId());
187 this.addNewPolicy(trigger);
189 // Return a copy of the policy
191 return new Policy(trigger);
195 public Policy setPolicyForPolicyResult(String name, String description, String actor, Target target, String recipe,
196 Map<String, String> payload, Integer retries, Integer timeout, String policyID, PolicyResult... results)
197 throws BuilderException {
199 // Find the existing policy
201 Policy existingPolicy = this.findPolicy(policyID);
202 if (existingPolicy == null) {
203 throw new BuilderException(UNKNOWN_POLICY + policyID);
206 // Create the new Policy
208 Policy newPolicy = new Policy(UUID.randomUUID().toString(), name, description, actor, payload, target, recipe,
211 // Connect the results
213 for (PolicyResult result : results) {
216 existingPolicy.setFailure(newPolicy.getId());
218 case FAILURE_EXCEPTION:
219 existingPolicy.setFailure_exception(newPolicy.getId());
221 case FAILURE_RETRIES:
222 existingPolicy.setFailure_retries(newPolicy.getId());
224 case FAILURE_TIMEOUT:
225 existingPolicy.setFailure_timeout(newPolicy.getId());
228 existingPolicy.setFailure_guard(newPolicy.getId());
231 existingPolicy.setSuccess(newPolicy.getId());
234 throw new BuilderException("Invalid PolicyResult " + result);
238 // Add it to our list
240 this.controlLoopPolicy.getPolicies().add(newPolicy);
242 // Return a policy to them
244 return new Policy(newPolicy);
247 private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
249 private ResultsImpl results = new ResultsImpl();
252 public boolean onWarning(String message) {
253 results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
258 public boolean onError(String message) {
259 results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
265 public Results buildSpecification() {
267 // Dump the specification
269 DumperOptions options = new DumperOptions();
270 options.setDefaultFlowStyle(FlowStyle.BLOCK);
271 options.setPrettyFlow(true);
272 Yaml yaml = new Yaml(options);
273 String dumpedYaml = yaml.dump(controlLoopPolicy);
275 // This is our callback class for our compiler
277 BuilderCompilerCallback callback = new BuilderCompilerCallback();
282 ControlLoopCompiler.compile(controlLoopPolicy, callback);
283 } catch (CompilerException e) {
284 logger.error(e.getMessage() + e);
285 callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
290 callback.results.setSpecification(dumpedYaml);
291 return callback.results;
294 private void addNewPolicy(Policy policy) {
295 if (this.controlLoopPolicy.getPolicies() == null) {
296 this.controlLoopPolicy.setPolicies(new LinkedList<>());
298 this.controlLoopPolicy.getPolicies().add(policy);
301 private Policy findPolicy(String id) {
302 if (this.controlLoopPolicy.getPolicies() != null) {
303 for (Policy policy : this.controlLoopPolicy.getPolicies()) {
304 if (policy.getId().equals(id)) {
313 public ControlLoopPolicyBuilder removeResource(Resource... resources) throws BuilderException {
314 if (controlLoopPolicy.getControlLoop().getResources() == null) {
315 throw new BuilderException("No existing resources to remove");
317 for (Resource resource : resources) {
318 if (resource == null) {
319 throw new BuilderException("Resource must not be null");
321 if (resource.getResourceUUID() == null && Strings.isNullOrEmpty(resource.getResourceName())) {
322 throw new BuilderException("Invalid resource - need either a resourceUUID or resourceName");
324 boolean removed = controlLoopPolicy.getControlLoop().getResources().remove(resource);
326 throw new BuilderException("Unknown resource " + resource.getResourceName());
333 public ControlLoopPolicyBuilder removeAllResources() throws BuilderException {
334 controlLoopPolicy.getControlLoop().getResources().clear();
339 public Integer calculateTimeout() {
341 for (Policy policy : this.controlLoopPolicy.getPolicies()) {
342 sum += policy.getTimeout().intValue();
344 return Integer.valueOf(sum);
348 public ControlLoop setTriggerPolicy(String id) throws BuilderException {
350 throw new BuilderException("Id must not be null");
352 Policy trigger = this.findPolicy(id);
353 if (trigger == null) {
354 throw new BuilderException(UNKNOWN_POLICY + id);
356 this.controlLoopPolicy.getControlLoop().setTrigger_policy(id);
358 return new ControlLoop(this.controlLoopPolicy.getControlLoop());
362 public boolean isOpenLoop() {
363 return this.controlLoopPolicy.getControlLoop().getTrigger_policy()
364 .equals(FinalResult.FINAL_OPENLOOP.toString());
368 public Policy getTriggerPolicy() throws BuilderException {
369 if (this.controlLoopPolicy.getControlLoop().getTrigger_policy().equals(FinalResult.FINAL_OPENLOOP.toString())) {
372 return new Policy(this.findPolicy(this.controlLoopPolicy.getControlLoop().getTrigger_policy()));
377 public ControlLoop getControlLoop() {
378 return new ControlLoop(this.controlLoopPolicy.getControlLoop());
382 public Policy setPolicyForPolicyResult(String policyResultID, String policyID, PolicyResult... results)
383 throws BuilderException {
385 // Find the existing policy
387 Policy existingPolicy = this.findPolicy(policyID);
388 if (existingPolicy == null) {
389 throw new BuilderException(policyID + " does not exist");
391 if (this.findPolicy(policyResultID) == null) {
392 throw new BuilderException("Operational policy " + policyResultID + " does not exist");
395 // Connect the results
397 for (PolicyResult result : results) {
400 existingPolicy.setFailure(policyResultID);
402 case FAILURE_EXCEPTION:
403 existingPolicy.setFailure_exception(policyResultID);
405 case FAILURE_RETRIES:
406 existingPolicy.setFailure_retries(policyResultID);
408 case FAILURE_TIMEOUT:
409 existingPolicy.setFailure_timeout(policyResultID);
412 existingPolicy.setFailure_guard(policyResultID);
415 existingPolicy.setSuccess(policyResultID);
418 throw new BuilderException("Invalid PolicyResult " + result);
421 return new Policy(this.findPolicy(policyResultID));
425 public boolean removePolicy(String policyID) throws BuilderException {
426 Policy existingPolicy = this.findPolicy(policyID);
427 if (existingPolicy == null) {
428 throw new BuilderException(UNKNOWN_POLICY + policyID);
431 // Check if the policy to remove is trigger_policy
433 if (this.controlLoopPolicy.getControlLoop().getTrigger_policy().equals(policyID)) {
434 this.controlLoopPolicy.getControlLoop().setTrigger_policy(FinalResult.FINAL_OPENLOOP.toString());
436 updateChainedPoliciesForPolicyRemoval(policyID);
441 return this.controlLoopPolicy.getPolicies().remove(existingPolicy);
444 private void updateChainedPoliciesForPolicyRemoval(String idOfPolicyBeingRemoved) {
445 for (Policy policy : this.controlLoopPolicy.getPolicies()) {
446 int index = this.controlLoopPolicy.getPolicies().indexOf(policy);
447 if (policy.getSuccess().equals(idOfPolicyBeingRemoved)) {
448 policy.setSuccess(FinalResult.FINAL_SUCCESS.toString());
450 if (policy.getFailure().equals(idOfPolicyBeingRemoved)) {
451 policy.setFailure(FinalResult.FINAL_FAILURE.toString());
453 if (policy.getFailure_retries().equals(idOfPolicyBeingRemoved)) {
454 policy.setFailure_retries(FinalResult.FINAL_FAILURE_RETRIES.toString());
456 if (policy.getFailure_timeout().equals(idOfPolicyBeingRemoved)) {
457 policy.setFailure_timeout(FinalResult.FINAL_FAILURE_TIMEOUT.toString());
459 if (policy.getFailure_exception().equals(idOfPolicyBeingRemoved)) {
460 policy.setFailure_exception(FinalResult.FINAL_FAILURE_EXCEPTION.toString());
462 if (policy.getFailure_guard().equals(idOfPolicyBeingRemoved)) {
463 policy.setFailure_guard(FinalResult.FINAL_FAILURE_GUARD.toString());
465 this.controlLoopPolicy.getPolicies().set(index, policy);
470 public Policy resetPolicyResults(String policyID) throws BuilderException {
471 Policy existingPolicy = this.findPolicy(policyID);
472 if (existingPolicy == null) {
473 throw new BuilderException(UNKNOWN_POLICY + policyID);
476 // reset policy results
478 existingPolicy.setSuccess(FinalResult.FINAL_SUCCESS.toString());
479 existingPolicy.setFailure(FinalResult.FINAL_FAILURE.toString());
480 existingPolicy.setFailure_retries(FinalResult.FINAL_FAILURE_RETRIES.toString());
481 existingPolicy.setFailure_timeout(FinalResult.FINAL_FAILURE_TIMEOUT.toString());
482 existingPolicy.setFailure_exception(FinalResult.FINAL_FAILURE_EXCEPTION.toString());
483 existingPolicy.setFailure_guard(FinalResult.FINAL_FAILURE_GUARD.toString());
484 return new Policy(existingPolicy);
488 public ControlLoopPolicyBuilder removeAllPolicies() {
490 // Remove all existing operational policies
492 this.controlLoopPolicy.getPolicies().clear();
494 // Revert controlLoop back to an open loop
496 this.controlLoopPolicy.getControlLoop().setTrigger_policy(FinalResult.FINAL_OPENLOOP.toString());
501 public Policy addOperationsAccumulateParams(String policyID, OperationsAccumulateParams operationsAccumulateParams)
502 throws BuilderException {
503 Policy existingPolicy = this.findPolicy(policyID);
504 if (existingPolicy == null) {
505 throw new BuilderException(UNKNOWN_POLICY + policyID);
508 // Add operationsAccumulateParams to existingPolicy
510 existingPolicy.setOperationsAccumulateParams(operationsAccumulateParams);
511 return new Policy(existingPolicy);