2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.policy.builder.impl;
24 import com.google.common.base.Strings;
25 import java.util.LinkedList;
26 import java.util.UUID;
27 import org.onap.aai.domain.yang.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.PolicyParam;
37 import org.onap.policy.controlloop.policy.PolicyResult;
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.yaml.snakeyaml.DumperOptions;
45 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
46 import org.yaml.snakeyaml.Yaml;
48 public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder {
49 private static final String UNKNOWN_POLICY = "Unknown policy ";
50 private ControlLoopPolicy controlLoopPolicy;
55 * @param controlLoopName control loop id
56 * @param timeout timeout value
58 public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout) {
59 controlLoopPolicy = new ControlLoopPolicy();
60 ControlLoop controlLoop = new ControlLoop();
61 controlLoop.setControlLoopName(controlLoopName);
62 controlLoop.setTimeout(timeout);
63 controlLoopPolicy.setControlLoop(controlLoop);
69 * @param controlLoopName control loop id
70 * @param timeout timeout value
71 * @param resource resource
72 * @param services services
73 * @throws BuilderException builder exception
75 public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Resource resource,
76 Service... services) throws BuilderException {
77 this(controlLoopName, timeout);
78 this.addResource(resource);
79 this.addService(services);
82 public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Pnf pnf) throws BuilderException {
83 this(controlLoopName, timeout);
90 * @param controlLoopName control loop id
91 * @param timeout timeout
92 * @param service service
93 * @param resources resources
94 * @throws BuilderException builder exception
96 public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Service service,
97 Resource[] resources) throws BuilderException {
98 this(controlLoopName, timeout);
99 this.addService(service);
100 this.addResource(resources);
104 public ControlLoopPolicyBuilder removePnf() throws BuilderException {
105 controlLoopPolicy.getControlLoop().setPnf(null);
110 public ControlLoopPolicyBuilder addService(Service... services) throws BuilderException {
111 for (Service service : services) {
112 if (service == null) {
113 throw new BuilderException("Service must not be null");
115 if (service.getServiceUUID() == null && Strings.isNullOrEmpty(service.getServiceName())) {
116 throw new BuilderException("Invalid service - need either a serviceUUID or serviceName");
118 if (controlLoopPolicy.getControlLoop().getServices() == null) {
119 controlLoopPolicy.getControlLoop().setServices(new LinkedList<>());
121 controlLoopPolicy.getControlLoop().getServices().add(service);
127 public ControlLoopPolicyBuilder removeService(Service... services) throws BuilderException {
128 if (controlLoopPolicy.getControlLoop().getServices() == null) {
129 throw new BuilderException("No existing services to remove");
131 for (Service service : services) {
132 if (service == null) {
133 throw new BuilderException("Service must not be null");
135 if (service.getServiceUUID() == null && Strings.isNullOrEmpty(service.getServiceName())) {
136 throw new BuilderException("Invalid service - need either a serviceUUID or serviceName");
138 boolean removed = controlLoopPolicy.getControlLoop().getServices().remove(service);
140 throw new BuilderException("Unknown service " + service.getServiceName());
147 public ControlLoopPolicyBuilder removeAllServices() throws BuilderException {
148 controlLoopPolicy.getControlLoop().getServices().clear();
153 public ControlLoopPolicyBuilder addResource(Resource... resources) throws BuilderException {
154 for (Resource resource : resources) {
155 if (resource == null) {
156 throw new BuilderException("Resource must not be null");
158 if (resource.getResourceUuid() == null && Strings.isNullOrEmpty(resource.getResourceName())) {
159 throw new BuilderException("Invalid resource - need either resourceUUID or resourceName");
161 if (controlLoopPolicy.getControlLoop().getResources() == null) {
162 controlLoopPolicy.getControlLoop().setResources(new LinkedList<>());
164 controlLoopPolicy.getControlLoop().getResources().add(resource);
170 public ControlLoopPolicyBuilder setPnf(Pnf pnf) throws BuilderException {
172 throw new BuilderException("PNF must not be null");
174 if (pnf.getPnfName() == null && pnf.getEquipType() == null) {
175 throw new BuilderException("Invalid PNF - need either pnfName or pnfType");
177 controlLoopPolicy.getControlLoop().setPnf(pnf);
182 public ControlLoopPolicyBuilder setAbatement(Boolean abatement) throws BuilderException {
183 if (abatement == null) {
184 throw new BuilderException("abatement must not be null");
186 controlLoopPolicy.getControlLoop().setAbatement(abatement);
191 public ControlLoopPolicyBuilder setTimeout(Integer timeout) {
192 controlLoopPolicy.getControlLoop().setTimeout(timeout);
197 public Policy setTriggerPolicy(PolicyParam policyParam) throws BuilderException {
199 Policy trigger = new Policy(policyParam);
201 controlLoopPolicy.getControlLoop().setTrigger_policy(trigger.getId());
203 this.addNewPolicy(trigger);
205 // Return a copy of the policy
207 return new Policy(trigger);
211 public ControlLoop setExistingTriggerPolicy(String id) throws BuilderException {
213 throw new BuilderException("Id must not be null");
215 Policy trigger = this.findPolicy(id);
216 if (trigger == null) {
217 throw new BuilderException(UNKNOWN_POLICY + id);
219 this.controlLoopPolicy.getControlLoop().setTrigger_policy(id);
221 return new ControlLoop(this.controlLoopPolicy.getControlLoop());
225 public Policy setPolicyForPolicyResult(PolicyParam policyParam, PolicyResult... results) throws BuilderException {
227 // Find the existing policy
229 Policy existingPolicy = this.findPolicy(policyParam.getId());
230 if (existingPolicy == null) {
231 throw new BuilderException(UNKNOWN_POLICY + policyParam.getId());
234 // Create the new Policy
237 Policy newPolicy = new Policy(PolicyParam.builder()
238 .id(UUID.randomUUID().toString())
239 .name(policyParam.getName())
240 .description(policyParam.getDescription())
241 .actor(policyParam.getActor())
242 .payload(policyParam.getPayload())
243 .target(policyParam.getTarget())
244 .recipe(policyParam.getRecipe())
245 .retries(policyParam.getRetries())
246 .timeout(policyParam.getTimeout())
250 // Connect the results
252 for (PolicyResult result : results) {
255 existingPolicy.setFailure(newPolicy.getId());
257 case FAILURE_EXCEPTION:
258 existingPolicy.setFailure_exception(newPolicy.getId());
260 case FAILURE_RETRIES:
261 existingPolicy.setFailure_retries(newPolicy.getId());
263 case FAILURE_TIMEOUT:
264 existingPolicy.setFailure_timeout(newPolicy.getId());
267 existingPolicy.setFailure_guard(newPolicy.getId());
270 existingPolicy.setSuccess(newPolicy.getId());
273 throw new BuilderException("Invalid PolicyResult " + result);
277 // Add it to our list
279 this.controlLoopPolicy.getPolicies().add(newPolicy);
281 // Return a policy to them
283 return new Policy(newPolicy);
287 public Policy setPolicyForPolicyResult(String policyResultId, String policyId, PolicyResult... results)
288 throws BuilderException {
290 // Find the existing policy
292 Policy existingPolicy = this.findPolicy(policyId);
293 if (existingPolicy == null) {
294 throw new BuilderException(policyId + " does not exist");
296 if (this.findPolicy(policyResultId) == null) {
297 throw new BuilderException("Operational policy " + policyResultId + " does not exist");
300 // Connect the results
302 for (PolicyResult result : results) {
305 existingPolicy.setFailure(policyResultId);
307 case FAILURE_EXCEPTION:
308 existingPolicy.setFailure_exception(policyResultId);
310 case FAILURE_RETRIES:
311 existingPolicy.setFailure_retries(policyResultId);
313 case FAILURE_TIMEOUT:
314 existingPolicy.setFailure_timeout(policyResultId);
317 existingPolicy.setFailure_guard(policyResultId);
320 existingPolicy.setSuccess(policyResultId);
323 throw new BuilderException("Invalid PolicyResult " + result);
326 return new Policy(this.findPolicy(policyResultId));
329 private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
331 private ResultsImpl results = new ResultsImpl();
334 public boolean onWarning(String message) {
335 results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
340 public boolean onError(String message) {
341 results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
347 public Results buildSpecification() {
349 // Dump the specification
351 DumperOptions options = new DumperOptions();
352 options.setDefaultFlowStyle(FlowStyle.BLOCK);
353 options.setPrettyFlow(true);
354 Yaml yaml = new Yaml(options);
355 String dumpedYaml = yaml.dump(controlLoopPolicy);
357 // This is our callback class for our compiler
359 BuilderCompilerCallback callback = new BuilderCompilerCallback();
364 ControlLoopCompiler.compile(controlLoopPolicy, callback);
365 } catch (CompilerException e) {
366 callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
371 callback.results.setSpecification(dumpedYaml);
372 return callback.results;
375 private void addNewPolicy(Policy policy) {
376 if (this.controlLoopPolicy.getPolicies() == null) {
377 this.controlLoopPolicy.setPolicies(new LinkedList<>());
379 this.controlLoopPolicy.getPolicies().add(policy);
382 private Policy findPolicy(String id) {
383 if (this.controlLoopPolicy.getPolicies() != null) {
384 for (Policy policy : this.controlLoopPolicy.getPolicies()) {
385 if (policy.getId().equals(id)) {
394 public ControlLoopPolicyBuilder removeResource(Resource... resources) throws BuilderException {
395 if (controlLoopPolicy.getControlLoop().getResources() == null) {
396 throw new BuilderException("No existing resources to remove");
398 for (Resource resource : resources) {
399 if (resource == null) {
400 throw new BuilderException("Resource must not be null");
402 if (resource.getResourceUuid() == null && Strings.isNullOrEmpty(resource.getResourceName())) {
403 throw new BuilderException("Invalid resource - need either a resourceUUID or resourceName");
405 boolean removed = controlLoopPolicy.getControlLoop().getResources().remove(resource);
407 throw new BuilderException("Unknown resource " + resource.getResourceName());
414 public ControlLoopPolicyBuilder removeAllResources() throws BuilderException {
415 controlLoopPolicy.getControlLoop().getResources().clear();
420 public Integer calculateTimeout() {
422 for (Policy policy : this.controlLoopPolicy.getPolicies()) {
423 sum += policy.getTimeout().intValue();
425 return Integer.valueOf(sum);
429 public boolean isOpenLoop() {
430 return this.controlLoopPolicy.getControlLoop().getTrigger_policy()
431 .equals(FinalResult.FINAL_OPENLOOP.toString());
435 public Policy getTriggerPolicy() throws BuilderException {
436 if (this.controlLoopPolicy.getControlLoop().getTrigger_policy().equals(FinalResult.FINAL_OPENLOOP.toString())) {
439 return new Policy(this.findPolicy(this.controlLoopPolicy.getControlLoop().getTrigger_policy()));
444 public ControlLoop getControlLoop() {
445 return new ControlLoop(this.controlLoopPolicy.getControlLoop());
449 public boolean removePolicy(String policyId) throws BuilderException {
450 Policy existingPolicy = this.findPolicy(policyId);
451 if (existingPolicy == null) {
452 throw new BuilderException(UNKNOWN_POLICY + policyId);
455 // Check if the policy to remove is trigger_policy
457 if (this.controlLoopPolicy.getControlLoop().getTrigger_policy().equals(policyId)) {
458 this.controlLoopPolicy.getControlLoop().setTrigger_policy(FinalResult.FINAL_OPENLOOP.toString());
460 updateChainedPoliciesForPolicyRemoval(policyId);
465 return this.controlLoopPolicy.getPolicies().remove(existingPolicy);
468 private void updateChainedPoliciesForPolicyRemoval(String idOfPolicyBeingRemoved) {
469 for (Policy policy : this.controlLoopPolicy.getPolicies()) {
470 final int index = this.controlLoopPolicy.getPolicies().indexOf(policy);
471 if (policy.getSuccess().equals(idOfPolicyBeingRemoved)) {
472 policy.setSuccess(FinalResult.FINAL_SUCCESS.toString());
474 if (policy.getFailure().equals(idOfPolicyBeingRemoved)) {
475 policy.setFailure(FinalResult.FINAL_FAILURE.toString());
477 if (policy.getFailure_retries().equals(idOfPolicyBeingRemoved)) {
478 policy.setFailure_retries(FinalResult.FINAL_FAILURE_RETRIES.toString());
480 if (policy.getFailure_timeout().equals(idOfPolicyBeingRemoved)) {
481 policy.setFailure_timeout(FinalResult.FINAL_FAILURE_TIMEOUT.toString());
483 if (policy.getFailure_exception().equals(idOfPolicyBeingRemoved)) {
484 policy.setFailure_exception(FinalResult.FINAL_FAILURE_EXCEPTION.toString());
486 if (policy.getFailure_guard().equals(idOfPolicyBeingRemoved)) {
487 policy.setFailure_guard(FinalResult.FINAL_FAILURE_GUARD.toString());
489 this.controlLoopPolicy.getPolicies().set(index, policy);
494 public Policy resetPolicyResults(String policyId) throws BuilderException {
495 Policy existingPolicy = this.findPolicy(policyId);
496 if (existingPolicy == null) {
497 throw new BuilderException(UNKNOWN_POLICY + policyId);
500 // reset policy results
502 existingPolicy.setSuccess(FinalResult.FINAL_SUCCESS.toString());
503 existingPolicy.setFailure(FinalResult.FINAL_FAILURE.toString());
504 existingPolicy.setFailure_retries(FinalResult.FINAL_FAILURE_RETRIES.toString());
505 existingPolicy.setFailure_timeout(FinalResult.FINAL_FAILURE_TIMEOUT.toString());
506 existingPolicy.setFailure_exception(FinalResult.FINAL_FAILURE_EXCEPTION.toString());
507 existingPolicy.setFailure_guard(FinalResult.FINAL_FAILURE_GUARD.toString());
508 return new Policy(existingPolicy);
512 public ControlLoopPolicyBuilder removeAllPolicies() {
514 // Remove all existing operational policies
516 this.controlLoopPolicy.getPolicies().clear();
518 // Revert controlLoop back to an open loop
520 this.controlLoopPolicy.getControlLoop().setTrigger_policy(FinalResult.FINAL_OPENLOOP.toString());
525 public Policy addOperationsAccumulateParams(String policyId, OperationsAccumulateParams operationsAccumulateParams)
526 throws BuilderException {
527 Policy existingPolicy = this.findPolicy(policyId);
528 if (existingPolicy == null) {
529 throw new BuilderException(UNKNOWN_POLICY + policyId);
532 // Add operationsAccumulateParams to existingPolicy
534 existingPolicy.setOperationsAccumulateParams(operationsAccumulateParams);
535 return new Policy(existingPolicy);