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 java.util.LinkedList;
25 import java.util.UUID;
27 import org.onap.policy.aai.PNF;
28 import org.onap.policy.controlloop.compiler.CompilerException;
29 import org.onap.policy.controlloop.compiler.ControlLoopCompiler;
30 import org.onap.policy.controlloop.compiler.ControlLoopCompilerCallback;
31 import org.onap.policy.controlloop.policy.ControlLoop;
32 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
33 import org.onap.policy.controlloop.policy.FinalResult;
34 import org.onap.policy.controlloop.policy.OperationsAccumulateParams;
35 import org.onap.policy.controlloop.policy.Policy;
36 import org.onap.policy.controlloop.policy.PolicyResult;
37 import org.onap.policy.controlloop.policy.Target;
38 import org.onap.policy.controlloop.policy.builder.BuilderException;
39 import org.onap.policy.controlloop.policy.builder.ControlLoopPolicyBuilder;
40 import org.onap.policy.controlloop.policy.builder.MessageLevel;
41 import org.onap.policy.controlloop.policy.builder.Results;
42 import org.onap.policy.sdc.Resource;
43 import org.onap.policy.sdc.Service;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.yaml.snakeyaml.DumperOptions;
47 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
48 import org.yaml.snakeyaml.Yaml;
50 import com.google.common.base.Strings;
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) throws BuilderException {
66 this(controlLoopName, timeout);
67 this.addResource(resource);
68 this.addService(services);
71 public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, PNF pnf) throws BuilderException {
72 this(controlLoopName, timeout);
76 public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Service service, Resource[] resources) throws BuilderException {
77 this(controlLoopName, timeout);
78 this.addService(service);
79 this.addResource(resources);
83 public ControlLoopPolicyBuilder removePNF() throws BuilderException {
84 controlLoopPolicy.getControlLoop().setPnf(null);
89 public ControlLoopPolicyBuilder addService(Service... services) throws BuilderException {
90 for (Service service : services) {
91 if (service == null) {
92 throw new BuilderException("Service must not be null");
94 if (service.getServiceUUID() == null && Strings.isNullOrEmpty(service.getServiceName())) {
95 throw new BuilderException("Invalid service - need either a serviceUUID or serviceName");
97 if(controlLoopPolicy.getControlLoop().getServices()==null){
98 controlLoopPolicy.getControlLoop().setServices(new LinkedList<>());
100 controlLoopPolicy.getControlLoop().getServices().add(service);
106 public ControlLoopPolicyBuilder removeService(Service... services) throws BuilderException {
107 if (controlLoopPolicy.getControlLoop().getServices() == null) {
108 throw new BuilderException("No existing services to remove");
110 for (Service service : services) {
111 if (service == null) {
112 throw new BuilderException("Service must not be null");
114 if (service.getServiceUUID() == null && Strings.isNullOrEmpty(service.getServiceName())) {
115 throw new BuilderException("Invalid service - need either a serviceUUID or serviceName");
117 boolean removed = controlLoopPolicy.getControlLoop().getServices().remove(service);
119 throw new BuilderException("Unknown service " + service.getServiceName());
126 public ControlLoopPolicyBuilder removeAllServices() throws BuilderException {
127 controlLoopPolicy.getControlLoop().getServices().clear();
133 public ControlLoopPolicyBuilder addResource(Resource... resources) throws BuilderException {
134 for (Resource resource : resources) {
135 if (resource == null) {
136 throw new BuilderException("Resource must not be null");
138 if (resource.getResourceUUID() == null && Strings.isNullOrEmpty(resource.getResourceName())) {
139 throw new BuilderException("Invalid resource - need either resourceUUID or resourceName");
141 if(controlLoopPolicy.getControlLoop().getResources()==null){
142 controlLoopPolicy.getControlLoop().setResources(new LinkedList<>());
144 controlLoopPolicy.getControlLoop().getResources().add(resource);
150 public ControlLoopPolicyBuilder setPNF(PNF pnf) throws BuilderException {
152 throw new BuilderException("PNF must not be null");
154 if (pnf.getPNFName() == null && pnf.getPNFType() == null) {
155 throw new BuilderException("Invalid PNF - need either pnfName or pnfType");
157 controlLoopPolicy.getControlLoop().setPnf(pnf);
162 public ControlLoopPolicyBuilder setAbatement(Boolean abatement) throws BuilderException{
163 if (abatement == null) {
164 throw new BuilderException("abatement must not be null");
166 controlLoopPolicy.getControlLoop().setAbatement(abatement);
171 public ControlLoopPolicyBuilder setTimeout(Integer timeout) {
172 controlLoopPolicy.getControlLoop().setTimeout(timeout);
177 public Policy setTriggerPolicy(String name, String description, String actor, Target target, String recipe,
178 Map<String, String> payload, Integer retries, Integer timeout) throws BuilderException {
180 Policy trigger = new Policy(UUID.randomUUID().toString(), name, description, actor, payload, target, recipe, retries, timeout);
182 controlLoopPolicy.getControlLoop().setTrigger_policy(trigger.getId());
184 this.addNewPolicy(trigger);
186 // Return a copy of the policy
188 return new Policy(trigger);
192 public Policy setPolicyForPolicyResult(String name, String description, String actor,
193 Target target, String recipe, Map<String, String> payload, Integer retries, Integer timeout, String policyID, PolicyResult... results) throws BuilderException {
195 // Find the existing policy
197 Policy existingPolicy = this.findPolicy(policyID);
198 if (existingPolicy == null) {
199 throw new BuilderException(UNKNOWN_POLICY + policyID);
202 // Create the new Policy
204 Policy newPolicy = new Policy(UUID.randomUUID().toString(), name, description, actor, payload, target, recipe, retries, timeout);
206 // Connect the results
208 for (PolicyResult result : results) {
211 existingPolicy.setFailure(newPolicy.getId());
213 case FAILURE_EXCEPTION:
214 existingPolicy.setFailure_exception(newPolicy.getId());
216 case FAILURE_RETRIES:
217 existingPolicy.setFailure_retries(newPolicy.getId());
219 case FAILURE_TIMEOUT:
220 existingPolicy.setFailure_timeout(newPolicy.getId());
223 existingPolicy.setFailure_guard(newPolicy.getId());
226 existingPolicy.setSuccess(newPolicy.getId());
229 throw new BuilderException("Invalid PolicyResult " + result);
233 // Add it to our list
235 this.controlLoopPolicy.getPolicies().add(newPolicy);
237 // Return a policy to them
239 return new Policy(newPolicy);
242 private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
244 private ResultsImpl results = new ResultsImpl();
247 public boolean onWarning(String message) {
248 results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
253 public boolean onError(String message) {
254 results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
260 public Results buildSpecification() {
262 // Dump the specification
264 DumperOptions options = new DumperOptions();
265 options.setDefaultFlowStyle(FlowStyle.BLOCK);
266 options.setPrettyFlow(true);
267 Yaml yaml = new Yaml(options);
268 String dumpedYaml = yaml.dump(controlLoopPolicy);
270 // This is our callback class for our compiler
272 BuilderCompilerCallback callback = new BuilderCompilerCallback();
277 ControlLoopCompiler.compile(controlLoopPolicy, callback);
278 } catch (CompilerException e) {
279 logger.error(e.getMessage() + e);
280 callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
285 callback.results.setSpecification(dumpedYaml);
286 return callback.results;
289 private void addNewPolicy(Policy policy) {
290 if (this.controlLoopPolicy.getPolicies() == null) {
291 this.controlLoopPolicy.setPolicies(new LinkedList<>());
293 this.controlLoopPolicy.getPolicies().add(policy);
296 private Policy findPolicy(String id) {
297 if (this.controlLoopPolicy.getPolicies() != null){
298 for (Policy policy : this.controlLoopPolicy.getPolicies()) {
299 if (policy.getId().equals(id)) {
308 public ControlLoopPolicyBuilder removeResource(Resource... resources) throws BuilderException {
309 if (controlLoopPolicy.getControlLoop().getResources() == null) {
310 throw new BuilderException("No existing resources to remove");
312 for (Resource resource : resources) {
313 if (resource == null) {
314 throw new BuilderException("Resource must not be null");
316 if (resource.getResourceUUID() == null && Strings.isNullOrEmpty(resource.getResourceName())) {
317 throw new BuilderException("Invalid resource - need either a resourceUUID or resourceName");
319 boolean removed = controlLoopPolicy.getControlLoop().getResources().remove(resource);
321 throw new BuilderException("Unknown resource " + resource.getResourceName());
328 public ControlLoopPolicyBuilder removeAllResources() throws BuilderException {
329 controlLoopPolicy.getControlLoop().getResources().clear();
334 public Integer calculateTimeout() {
336 for (Policy policy : this.controlLoopPolicy.getPolicies()) {
337 sum += policy.getTimeout().intValue();
339 return Integer.valueOf(sum);
343 public ControlLoop setTriggerPolicy(String id) throws BuilderException {
345 throw new BuilderException("Id must not be null");
347 Policy trigger = this.findPolicy(id);
348 if (trigger == null) {
349 throw new BuilderException(UNKNOWN_POLICY + id);
352 this.controlLoopPolicy.getControlLoop().setTrigger_policy(id);
354 return new ControlLoop(this.controlLoopPolicy.getControlLoop());
358 public boolean isOpenLoop() {
359 return this.controlLoopPolicy.getControlLoop().getTrigger_policy().equals(FinalResult.FINAL_OPENLOOP.toString());
363 public Policy getTriggerPolicy() throws BuilderException {
364 if (this.controlLoopPolicy.getControlLoop().getTrigger_policy().equals(FinalResult.FINAL_OPENLOOP.toString())) {
368 return new Policy(this.findPolicy(this.controlLoopPolicy.getControlLoop().getTrigger_policy()));
373 public ControlLoop getControlLoop() {
374 return new ControlLoop(this.controlLoopPolicy.getControlLoop());
378 public Policy setPolicyForPolicyResult(String policyResultID, String policyID, PolicyResult... results)
379 throws BuilderException {
381 // Find the existing policy
383 Policy existingPolicy = this.findPolicy(policyID);
384 if (existingPolicy == null) {
385 throw new BuilderException(policyID + " does not exist");
387 if (this.findPolicy(policyResultID) == null) {
388 throw new BuilderException("Operational policy " + policyResultID + " does not exist");
391 // Connect the results
393 for (PolicyResult result : results) {
396 existingPolicy.setFailure(policyResultID);
398 case FAILURE_EXCEPTION:
399 existingPolicy.setFailure_exception(policyResultID);
401 case FAILURE_RETRIES:
402 existingPolicy.setFailure_retries(policyResultID);
404 case FAILURE_TIMEOUT:
405 existingPolicy.setFailure_timeout(policyResultID);
408 existingPolicy.setFailure_guard(policyResultID);
411 existingPolicy.setSuccess(policyResultID);
414 throw new BuilderException("Invalid PolicyResult " + result);
417 return new Policy(this.findPolicy(policyResultID));
421 public boolean removePolicy(String policyID) throws BuilderException {
422 Policy existingPolicy = this.findPolicy(policyID);
423 if (existingPolicy == null) {
424 throw new BuilderException(UNKNOWN_POLICY + policyID);
427 // Check if the policy to remove is trigger_policy
429 if (this.controlLoopPolicy.getControlLoop().getTrigger_policy().equals(policyID)) {
430 this.controlLoopPolicy.getControlLoop().setTrigger_policy(FinalResult.FINAL_OPENLOOP.toString());
433 updateChainedPoliciesForPolicyRemoval(policyID);
438 return this.controlLoopPolicy.getPolicies().remove(existingPolicy);
441 private void updateChainedPoliciesForPolicyRemoval(String idOfPolicyBeingRemoved){
442 for (Policy policy : this.controlLoopPolicy.getPolicies()) {
443 int index = this.controlLoopPolicy.getPolicies().indexOf(policy);
444 if (policy.getSuccess().equals(idOfPolicyBeingRemoved)) {
445 policy.setSuccess(FinalResult.FINAL_SUCCESS.toString());
447 if (policy.getFailure().equals(idOfPolicyBeingRemoved)) {
448 policy.setFailure(FinalResult.FINAL_FAILURE.toString());
450 if (policy.getFailure_retries().equals(idOfPolicyBeingRemoved)) {
451 policy.setFailure_retries(FinalResult.FINAL_FAILURE_RETRIES.toString());
453 if (policy.getFailure_timeout().equals(idOfPolicyBeingRemoved)) {
454 policy.setFailure_timeout(FinalResult.FINAL_FAILURE_TIMEOUT.toString());
456 if (policy.getFailure_exception().equals(idOfPolicyBeingRemoved)) {
457 policy.setFailure_exception(FinalResult.FINAL_FAILURE_EXCEPTION.toString());
459 if (policy.getFailure_guard().equals(idOfPolicyBeingRemoved)) {
460 policy.setFailure_guard(FinalResult.FINAL_FAILURE_GUARD.toString());
462 this.controlLoopPolicy.getPolicies().set(index, policy);
467 public Policy resetPolicyResults(String policyID) throws BuilderException {
468 Policy existingPolicy = this.findPolicy(policyID);
469 if (existingPolicy == null) {
470 throw new BuilderException(UNKNOWN_POLICY + policyID);
473 // reset policy results
475 existingPolicy.setSuccess(FinalResult.FINAL_SUCCESS.toString());
476 existingPolicy.setFailure(FinalResult.FINAL_FAILURE.toString());
477 existingPolicy.setFailure_retries(FinalResult.FINAL_FAILURE_RETRIES.toString());
478 existingPolicy.setFailure_timeout(FinalResult.FINAL_FAILURE_TIMEOUT.toString());
479 existingPolicy.setFailure_exception(FinalResult.FINAL_FAILURE_EXCEPTION.toString());
480 existingPolicy.setFailure_guard(FinalResult.FINAL_FAILURE_GUARD.toString());
481 return new Policy(existingPolicy);
485 public ControlLoopPolicyBuilder removeAllPolicies() {
487 // Remove all existing operational policies
489 this.controlLoopPolicy.getPolicies().clear();
491 // Revert controlLoop back to an open loop
493 this.controlLoopPolicy.getControlLoop().setTrigger_policy(FinalResult.FINAL_OPENLOOP.toString());
498 public Policy addOperationsAccumulateParams(String policyID, OperationsAccumulateParams operationsAccumulateParams) throws BuilderException {
499 Policy existingPolicy = this.findPolicy(policyID);
500 if (existingPolicy == null) {
501 throw new BuilderException(UNKNOWN_POLICY + policyID);
504 // Add operationsAccumulateParams to existingPolicy
506 existingPolicy.setOperationsAccumulateParams(operationsAccumulateParams);
507 return new Policy(existingPolicy);