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.yaml.snakeyaml.DumperOptions;
28 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
29 import org.yaml.snakeyaml.Yaml;
31 import org.onap.policy.aai.PNF;
32 import org.onap.policy.controlloop.compiler.CompilerException;
33 import org.onap.policy.controlloop.compiler.ControlLoopCompiler;
34 import org.onap.policy.controlloop.compiler.ControlLoopCompilerCallback;
35 import org.onap.policy.controlloop.policy.ControlLoop;
36 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
37 import org.onap.policy.controlloop.policy.FinalResult;
38 import org.onap.policy.controlloop.policy.OperationsAccumulateParams;
39 import org.onap.policy.controlloop.policy.Policy;
40 import org.onap.policy.controlloop.policy.PolicyResult;
41 import org.onap.policy.controlloop.policy.Target;
42 import org.onap.policy.controlloop.policy.builder.BuilderException;
43 import org.onap.policy.controlloop.policy.builder.ControlLoopPolicyBuilder;
44 import org.onap.policy.controlloop.policy.builder.MessageLevel;
45 import org.onap.policy.controlloop.policy.builder.Results;
46 import org.onap.policy.sdc.Resource;
47 import org.onap.policy.sdc.Service;
49 public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder {
51 private ControlLoopPolicy policy;
53 public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout) throws BuilderException {
54 policy = new ControlLoopPolicy();
55 policy.controlLoop = new ControlLoop();
56 policy.controlLoop.controlLoopName = controlLoopName;
57 policy.controlLoop.timeout = timeout;
60 public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Resource resource, Service... services) throws BuilderException {
61 this(controlLoopName, timeout);
62 this.addResource(resource);
63 this.addService(services);
66 public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, PNF pnf) throws BuilderException {
67 this(controlLoopName, timeout);
71 public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Service service, Resource[] resources) throws BuilderException {
72 this(controlLoopName, timeout);
73 this.addService(service);
74 this.addResource(resources);
78 public ControlLoopPolicyBuilder addService(Service... services) throws BuilderException {
79 if (services == null) {
80 throw new BuilderException("Service must not be null");
82 for (Service service : services) {
83 if (service.serviceUUID == null) {
84 if (service.serviceName == null || service.serviceName.length() < 1) {
85 throw new BuilderException("Invalid service - need either a serviceUUID or serviceName");
88 if (policy.controlLoop.services == null) {
89 policy.controlLoop.services = new LinkedList<Service>();
91 policy.controlLoop.services.add(service);
97 public ControlLoopPolicyBuilder removeService(Service... services) throws BuilderException {
98 if (services == null) {
99 throw new BuilderException("Service must not be null");
101 if (policy.controlLoop.services == null) {
102 throw new BuilderException("No existing services to remove");
104 for (Service service : services) {
105 if (service.serviceUUID == null) {
106 if (service.serviceName == null || service.serviceName.length() < 1) {
107 throw new BuilderException("Invalid service - need either a serviceUUID or serviceName");
110 boolean removed = policy.controlLoop.services.remove(service);
112 throw new BuilderException("Unknown service " + service.serviceName);
119 public ControlLoopPolicyBuilder removeAllServices() throws BuilderException {
120 policy.controlLoop.services.clear();
126 public ControlLoopPolicyBuilder addResource(Resource... resources) throws BuilderException {
127 if (resources == null) {
128 throw new BuilderException("resources must not be null");
130 for (Resource resource : resources) {
131 if (resource.resourceUUID == null) {
132 if (resource.resourceName == null || resource.resourceName.length() <= 0) {
133 throw new BuilderException("Invalid resource - need either resourceUUID or resourceName");
136 if (policy.controlLoop.resources == null) {
137 policy.controlLoop.resources = new LinkedList<Resource>();
139 policy.controlLoop.resources.add(resource);
145 public ControlLoopPolicyBuilder setPNF(PNF pnf) throws BuilderException {
147 throw new BuilderException("PNF must not be null");
149 if (pnf.PNFName == null) {
150 if (pnf.PNFType == null) {
151 throw new BuilderException("Invalid PNF - need either pnfName or pnfType");
154 policy.controlLoop.pnf = pnf;
159 public ControlLoopPolicyBuilder setAbatement(Boolean abatement) throws BuilderException{
160 if (abatement == null) {
161 throw new BuilderException("abatement must not be null");
163 policy.controlLoop.abatement = abatement;
168 public ControlLoopPolicyBuilder setTimeout(Integer timeout) {
169 policy.controlLoop.timeout = timeout;
174 public Policy setTriggerPolicy(String name, String description, String actor, Target target, String recipe,
175 Map<String, String> payload, Integer retries, Integer timeout) throws BuilderException {
177 Policy trigger = new Policy(UUID.randomUUID().toString(), name, description, actor, payload, target, recipe, retries, timeout);
179 policy.controlLoop.trigger_policy = trigger.id;
181 this.addNewPolicy(trigger);
183 // Return a copy of the policy
185 return new Policy(trigger);
189 public Policy setPolicyForPolicyResult(String name, String description, String actor,
190 Target target, String recipe, Map<String, String> payload, Integer retries, Integer timeout, String policyID, PolicyResult... results) throws BuilderException {
192 // Find the existing policy
194 Policy existingPolicy = this.findPolicy(policyID);
195 if (existingPolicy == null) {
196 throw new BuilderException("Unknown policy " + policyID);
199 // Create the new Policy
201 Policy newPolicy = new Policy(UUID.randomUUID().toString(), name, description, actor, payload, target, recipe, retries, timeout);
203 // Connect the results
205 for (PolicyResult result : results) {
208 existingPolicy.failure = newPolicy.id;
210 case FAILURE_EXCEPTION:
211 existingPolicy.failure_exception = newPolicy.id;
213 case FAILURE_RETRIES:
214 existingPolicy.failure_retries = newPolicy.id;
216 case FAILURE_TIMEOUT:
217 existingPolicy.failure_timeout = newPolicy.id;
220 existingPolicy.failure_guard = newPolicy.id;
223 existingPolicy.success = newPolicy.id;
226 throw new BuilderException("Invalid PolicyResult " + result);
230 // Add it to our list
232 this.policy.policies.add(newPolicy);
234 // Return a policy to them
236 return new Policy(newPolicy);
239 private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
241 public ResultsImpl results = new ResultsImpl();
244 public boolean onWarning(String message) {
245 results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
250 public boolean onError(String message) {
251 results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
257 public Results buildSpecification() {
259 // Dump the specification
261 DumperOptions options = new DumperOptions();
262 options.setDefaultFlowStyle(FlowStyle.BLOCK);
263 options.setPrettyFlow(true);
264 Yaml yaml = new Yaml(options);
265 String dumpedYaml = yaml.dump(policy);
267 // This is our callback class for our compiler
269 BuilderCompilerCallback callback = new BuilderCompilerCallback();
274 ControlLoopCompiler.compile(policy, callback);
275 } catch (CompilerException e) {
276 callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
281 callback.results.setSpecification(dumpedYaml);
282 return callback.results;
285 private void addNewPolicy(Policy policy) {
286 if (this.policy.policies == null) {
287 this.policy.policies = new LinkedList<Policy>();
289 this.policy.policies.add(policy);
292 private Policy findPolicy(String id) {
293 for (Policy policy : this.policy.policies) {
294 if (policy.id.equals(id)) {
302 public ControlLoopPolicyBuilder removeResource(Resource... resources) throws BuilderException {
303 if (resources == null) {
304 throw new BuilderException("Resource must not be null");
306 if (policy.controlLoop.resources == null) {
307 throw new BuilderException("No existing resources to remove");
309 for (Resource resource : resources) {
310 if (resource.resourceUUID == null) {
311 if (resource.resourceName == null || resource.resourceName.length() < 1) {
312 throw new BuilderException("Invalid resource - need either a resourceUUID or resourceName");
315 boolean removed = policy.controlLoop.resources.remove(resource);
317 throw new BuilderException("Unknown resource " + resource.resourceName);
324 public ControlLoopPolicyBuilder removeAllResources() throws BuilderException {
325 policy.controlLoop.resources.clear();
330 public ControlLoopPolicyBuilder removePNF() throws BuilderException {
331 policy.controlLoop.pnf = null;
336 public Integer calculateTimeout() {
338 for (Policy policy : this.policy.policies) {
339 sum += policy.timeout.intValue();
341 return new Integer(sum);
345 public ControlLoop setTriggerPolicy(String id) throws BuilderException {
347 throw new BuilderException("Id must not be null");
349 Policy trigger = this.findPolicy(id);
350 if (trigger == null) {
351 throw new BuilderException("Unknown policy " + id);
354 this.policy.controlLoop.trigger_policy = id;
356 return new ControlLoop(this.policy.controlLoop);
360 public boolean isOpenLoop() {
361 if (this.policy.controlLoop.trigger_policy.equals(FinalResult.FINAL_OPENLOOP.toString())) {
370 public Policy getTriggerPolicy() throws BuilderException {
371 if (this.policy.controlLoop.trigger_policy.equals(FinalResult.FINAL_OPENLOOP.toString())) {
375 Policy trigger = new Policy(this.findPolicy(this.policy.controlLoop.trigger_policy));
381 public ControlLoop getControlLoop() {
382 ControlLoop loop = new ControlLoop(this.policy.controlLoop);
387 public Policy setPolicyForPolicyResult(String policyResultID, String policyID, PolicyResult... results)
388 throws BuilderException {
390 // Find the existing policy
392 Policy existingPolicy = this.findPolicy(policyID);
393 if (existingPolicy == null) {
394 throw new BuilderException(policyID + " does not exist");
396 if (this.findPolicy(policyResultID) == null) {
397 throw new BuilderException("Operational policy " + policyResultID + " does not exist");
400 // Connect the results
402 for (PolicyResult result : results) {
405 existingPolicy.failure = policyResultID;
407 case FAILURE_EXCEPTION:
408 existingPolicy.failure_exception = policyResultID;
410 case FAILURE_RETRIES:
411 existingPolicy.failure_retries = policyResultID;
413 case FAILURE_TIMEOUT:
414 existingPolicy.failure_timeout = policyResultID;
417 existingPolicy.failure_guard = policyResultID;
420 existingPolicy.success = policyResultID;
423 throw new BuilderException("Invalid PolicyResult " + result);
426 return new Policy(this.findPolicy(policyResultID));
430 public boolean removePolicy(String policyID) throws BuilderException {
431 Policy existingPolicy = this.findPolicy(policyID);
432 if (existingPolicy == null) {
433 throw new BuilderException("Unknown policy " + policyID);
436 // Check if the policy to remove is trigger_policy
438 if (this.policy.controlLoop.trigger_policy.equals(policyID)) {
439 this.policy.controlLoop.trigger_policy = FinalResult.FINAL_OPENLOOP.toString();
445 for (Policy policy : this.policy.policies) {
446 int index = this.policy.policies.indexOf(policy);
447 if (policy.success.equals(policyID)) {
448 policy.success = FinalResult.FINAL_SUCCESS.toString();
450 if (policy.failure.equals(policyID)) {
451 policy.failure = FinalResult.FINAL_FAILURE.toString();
453 if (policy.failure_retries.equals(policyID)) {
454 policy.failure_retries = FinalResult.FINAL_FAILURE_RETRIES.toString();
456 if (policy.failure_timeout.equals(policyID)) {
457 policy.failure_timeout = FinalResult.FINAL_FAILURE_TIMEOUT.toString();
459 if (policy.failure_exception.equals(policyID)) {
460 policy.failure_exception = FinalResult.FINAL_FAILURE_EXCEPTION.toString();
462 if (policy.failure_guard.equals(policyID)) {
463 policy.failure_guard = FinalResult.FINAL_FAILURE_GUARD.toString();
465 this.policy.policies.set(index, policy);
471 boolean removed = this.policy.policies.remove(existingPolicy);
476 public Policy resetPolicyResults(String policyID) throws BuilderException {
477 Policy existingPolicy = this.findPolicy(policyID);
478 if (existingPolicy == null) {
479 throw new BuilderException("Unknown policy " + policyID);
482 // reset policy results
484 existingPolicy.success = FinalResult.FINAL_SUCCESS.toString();
485 existingPolicy.failure = FinalResult.FINAL_FAILURE.toString();
486 existingPolicy.failure_retries = FinalResult.FINAL_FAILURE_RETRIES.toString();
487 existingPolicy.failure_timeout = FinalResult.FINAL_FAILURE_TIMEOUT.toString();
488 existingPolicy.failure_exception = FinalResult.FINAL_FAILURE_EXCEPTION.toString();
489 existingPolicy.failure_guard = FinalResult.FINAL_FAILURE_GUARD.toString();
490 return new Policy(existingPolicy);
494 public ControlLoopPolicyBuilder removeAllPolicies() {
496 // Remove all existing operational policies
498 this.policy.policies.clear();
500 // Revert controlLoop back to an open loop
502 this.policy.controlLoop.trigger_policy = FinalResult.FINAL_OPENLOOP.toString();
507 public Policy addOperationsAccumulateParams(String policyID, OperationsAccumulateParams operationsAccumulateParams) throws BuilderException {
508 Policy existingPolicy = this.findPolicy(policyID);
509 if (existingPolicy == null) {
510 throw new BuilderException("Unknown policy " + policyID);
513 // Add operationsAccumulateParams to existingPolicy
515 existingPolicy.operationsAccumulateParams = operationsAccumulateParams;
516 return new Policy(existingPolicy);