2 * ============LICENSE_START=======================================================
3 * policy-yaml unit test
4 * ================================================================================
5 * Copyright (C) 2017-2018 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;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
31 import java.io.FileInputStream;
32 import java.io.FileNotFoundException;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.util.UUID;
37 import org.junit.Ignore;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.onap.policy.aai.Pnf;
42 import org.onap.policy.aai.PnfType;
43 import org.onap.policy.controlloop.policy.builder.BuilderException;
44 import org.onap.policy.controlloop.policy.builder.ControlLoopPolicyBuilder;
45 import org.onap.policy.controlloop.policy.builder.Message;
46 import org.onap.policy.controlloop.policy.builder.MessageLevel;
47 import org.onap.policy.controlloop.policy.builder.Results;
48 import org.onap.policy.sdc.Resource;
49 import org.onap.policy.sdc.ResourceType;
50 import org.onap.policy.sdc.Service;
51 import org.yaml.snakeyaml.Yaml;
52 import org.yaml.snakeyaml.constructor.Constructor;
53 import org.yaml.snakeyaml.error.YAMLException;
56 public class ControlLoopPolicyBuilderTest {
59 public ExpectedException expectedException = ExpectedException.none();
62 public void testControlLoop() {
65 // Create a builder for our policy
67 ControlLoopPolicyBuilder builder =
68 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
72 Service scp = new Service("vSCP");
73 Service usp = new Service("vUSP");
74 Service trinity = new Service("Trinity");
75 builder = builder.addService(scp, usp, trinity);
76 assertTrue(builder.getControlLoop().getServices().size() == 3);
78 // Test remove services
80 builder = builder.removeService(scp);
81 assertTrue(builder.getControlLoop().getServices().size() == 2);
82 builder = builder.removeAllServices();
83 assertTrue(builder.getControlLoop().getServices().size() == 0);
87 Resource cts = new Resource("vCTS", ResourceType.VF);
88 Resource com = new Resource("vCTS", ResourceType.VF);
89 Resource rar = new Resource("vCTS", ResourceType.VF);
90 builder = builder.addResource(cts, com, rar);
91 assertTrue(builder.getControlLoop().getResources().size() == 3);
93 // Test remove resources
95 builder = builder.removeResource(cts);
96 assertTrue(builder.getControlLoop().getResources().size() == 2);
97 builder = builder.removeAllResources();
98 assertTrue(builder.getControlLoop().getResources().size() == 0);
99 } catch (BuilderException e) {
100 fail(e.getMessage());
105 public void testAddNullService() throws BuilderException {
106 ControlLoopPolicyBuilder builder =
107 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
108 expectedException.expect(BuilderException.class);
109 expectedException.expectMessage("Service must not be null");
110 builder.addService((Service) null);
114 public void testAddInvalidService() throws BuilderException {
115 ControlLoopPolicyBuilder builder =
116 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
117 expectedException.expect(BuilderException.class);
118 expectedException.expectMessage("Invalid service - need either a serviceUUID or serviceName");
119 builder.addService(new Service());
123 public void testAddServiceWithUuid() throws BuilderException {
124 ControlLoopPolicyBuilder builder =
125 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
126 UUID uuid = UUID.randomUUID();
127 Service serviceWithUuid = new Service(uuid);
128 builder.addService(serviceWithUuid);
129 assertTrue(builder.getControlLoop().getServices().size() == 1);
133 public void testAddNullResource() throws BuilderException {
134 ControlLoopPolicyBuilder builder =
135 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
136 expectedException.expect(BuilderException.class);
137 expectedException.expectMessage("Resource must not be null");
138 builder.addResource((Resource) null);
143 public void testAddInvalidResource() throws BuilderException {
144 ControlLoopPolicyBuilder builder =
145 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
146 expectedException.expect(BuilderException.class);
147 expectedException.expectMessage("Invalid resource - need either resourceUUID or resourceName");
148 builder.addResource(new Resource());
152 public void testAddAndRemoveResourceWithUuid() throws BuilderException {
153 ControlLoopPolicyBuilder builder =
154 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
155 UUID uuid = UUID.randomUUID();
156 Resource resourceWithUuid = new Resource(uuid);
157 builder.addResource(resourceWithUuid);
158 assertTrue(builder.getControlLoop().getResources().size() == 1);
160 builder.removeResource(resourceWithUuid);
161 assertTrue(builder.getControlLoop().getResources().size() == 0);
165 public void testRemoveNullResource() throws BuilderException {
166 ControlLoopPolicyBuilder builder =
167 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
168 Resource resource = new Resource("resource1", ResourceType.VF);
169 builder.addResource(resource);
170 expectedException.expect(BuilderException.class);
171 expectedException.expectMessage("Resource must not be null");
172 builder.removeResource((Resource) null);
176 public void testRemoveResourceNoExistingResources() throws BuilderException {
177 ControlLoopPolicyBuilder builder =
178 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
179 expectedException.expect(BuilderException.class);
180 expectedException.expectMessage("No existing resources to remove");
181 builder.removeResource(new Resource("resource1", ResourceType.VF));
185 public void testRemoveInvalidResource() throws BuilderException {
186 ControlLoopPolicyBuilder builder =
187 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
188 Resource resource = new Resource("resource1", ResourceType.VF);
189 builder.addResource(resource);
190 expectedException.expect(BuilderException.class);
191 expectedException.expectMessage("Invalid resource - need either a resourceUUID or resourceName");
192 builder.removeResource(new Resource());
196 public void testRemoveUnknownResource() throws BuilderException {
197 ControlLoopPolicyBuilder builder =
198 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
199 Resource resource = new Resource("resource1", ResourceType.VF);
200 builder.addResource(resource);
201 final String unknownResourceName = "reource2";
202 expectedException.expect(BuilderException.class);
203 expectedException.expectMessage("Unknown resource " + unknownResourceName);
204 builder.removeResource(new Resource(unknownResourceName, ResourceType.VF));
208 public void testControlLoopWithInitialResourceAndServices() {
210 Resource cts = new Resource("vCTS", ResourceType.VF);
211 Service scp = new Service("vSCP");
212 Service usp = new Service("vUSP");
213 ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory
214 .buildControlLoop(UUID.randomUUID().toString(), 2400, cts, scp, usp);
215 assertTrue(builder.getControlLoop().getResources().size() == 1);
216 assertTrue(builder.getControlLoop().getServices().size() == 2);
217 } catch (BuilderException e) {
218 fail(e.getMessage());
223 public void testControlLoopWithInitialResourcesAndService() {
225 Resource cts = new Resource("vCTS", ResourceType.VF);
226 Resource com = new Resource("vCTS", ResourceType.VF);
227 Service scp = new Service("vSCP");
228 ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory
229 .buildControlLoop(UUID.randomUUID().toString(), 2400, scp, cts, com);
230 assertTrue(builder.getControlLoop().getServices().size() == 1);
231 assertTrue(builder.getControlLoop().getResources().size() == 2);
232 } catch (BuilderException e) {
233 fail(e.getMessage());
239 // I'VE MARKED THIS TEST CASE AS IGNORE BECAUSE THE TEST CASE FAILS
240 // This test case fails because builder.getControlLoop() returns an instance of ControlLoop
242 // the ControlLoop(ControlLoop controlLoop) constructor.
243 // This constructor does not copy the value of pnf into the newly created object
244 // On the face of it, this looks like a bug, but perhaps there is a reason for this
245 // PLEASE ADVISE IF THE BEHAVIOUR IS INCORRECT OR THE TEST CASE IS INVALID
246 public void testControlLoopForPnf() {
249 pnf.setPnfType(PnfType.ENODEB);
250 ControlLoopPolicyBuilder builder =
251 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, pnf);
252 assertEquals(pnf, builder.getControlLoop().getPnf());
255 assertNull(builder.getControlLoop().getPnf());
256 } catch (BuilderException e) {
257 fail(e.getMessage());
263 // Fails for the same reason as the above test case
264 public void testSetAndRemovePnf() throws BuilderException {
265 ControlLoopPolicyBuilder builder =
266 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
267 assertNull(builder.getControlLoop().getPnf());
270 pnf.setPnfType(PnfType.ENODEB);
272 assertEquals(pnf, builder.getControlLoop().getPnf());
275 assertNull(builder.getControlLoop().getPnf());
279 public void testSetNullPnf() throws BuilderException {
280 ControlLoopPolicyBuilder builder =
281 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
282 expectedException.expect(BuilderException.class);
283 expectedException.expectMessage("PNF must not be null");
284 builder.setPNF(null);
288 public void testSetInvalidPnf() throws BuilderException {
289 ControlLoopPolicyBuilder builder =
290 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
291 expectedException.expect(BuilderException.class);
292 expectedException.expectMessage("Invalid PNF - need either pnfName or pnfType");
293 builder.setPNF(new Pnf());
297 public void testSetAbatement() throws BuilderException {
298 ControlLoopPolicyBuilder builder =
299 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
300 assertFalse(builder.getControlLoop().getAbatement());
301 builder = builder.setAbatement(true);
302 assertTrue(builder.getControlLoop().getAbatement());
306 public void testSetNullAbatement() throws BuilderException {
307 ControlLoopPolicyBuilder builder =
308 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
309 expectedException.expect(BuilderException.class);
310 expectedException.expectMessage("abatement must not be null");
311 builder = builder.setAbatement(null);
315 public void testTimeout() {
318 // Create a builder for our policy
320 ControlLoopPolicyBuilder builder =
321 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
325 assertTrue(builder.getControlLoop().getTimeout() == 2400);
326 builder = builder.setTimeout(800);
327 assertTrue(builder.getControlLoop().getTimeout() == 800);
329 // Test calculateTimeout
332 builder.setTriggerPolicy(PolicyParam.builder().id(UUID.randomUUID().toString())
333 .name("Restart the VM")
334 .description("Upon getting the trigger event, restart the VM")
336 .target(new Target(TargetType.VM))
340 .timeout(300).build());
341 @SuppressWarnings("unused")
342 Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult(
343 PolicyParam.builder()
345 .description("If the restart fails, rebuild it")
347 .target(new Target(TargetType.VM))
352 .id(trigger.getId()).build(),
353 PolicyResult.FAILURE,
354 PolicyResult.FAILURE_RETRIES,
355 PolicyResult.FAILURE_TIMEOUT);
356 assertTrue(builder.calculateTimeout().equals(new Integer(300 + 600)));
358 } catch (BuilderException e) {
359 fail(e.getMessage());
364 public void testTriggerPolicyMethods() {
366 ControlLoopPolicyBuilder builder =
367 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
371 assertTrue(builder.isOpenLoop());
373 // Test set initial trigger policy
375 Policy triggerPolicy1 =
376 builder.setTriggerPolicy(
377 PolicyParam.builder().id(UUID.randomUUID().toString())
378 .name("Restart the VM")
379 .description("Upon getting the trigger event, restart the VM")
381 .target(new Target(TargetType.VM))
385 .timeout(300).build());
386 assertTrue(builder.isOpenLoop() == false);
387 assertTrue(builder.getControlLoop().getTrigger_policy().equals(triggerPolicy1.getId()));
389 // Set trigger policy to a new policy
391 @SuppressWarnings("unused")
392 Policy triggerPolicy2 =
393 builder.setTriggerPolicy(
394 PolicyParam.builder()
395 .id(UUID.randomUUID().toString())
396 .name("Rebuild the VM")
397 .description("Upon getting the trigger event, rebuild the VM")
399 .target(new Target(TargetType.VM))
403 .timeout(300).build());
405 // Test set trigger policy to another existing policy
407 @SuppressWarnings("unused")
408 ControlLoop cl = builder.setExistingTriggerPolicy(triggerPolicy1.getId());
409 assertTrue(builder.getControlLoop().getTrigger_policy().equals(triggerPolicy1.getId()));
411 // Test get trigger policy
413 assertTrue(builder.getTriggerPolicy().equals(triggerPolicy1));
415 } catch (BuilderException e) {
416 fail(e.getMessage());
421 public void testSetTriggerPolicyNullPolicyId() throws BuilderException {
422 ControlLoopPolicyBuilder builder =
423 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
424 expectedException.expect(BuilderException.class);
425 expectedException.expectMessage("Id must not be null");
426 builder.setExistingTriggerPolicy(null);
430 public void testSetTriggerPolicyNoPoliciesExist() throws BuilderException {
431 ControlLoopPolicyBuilder builder =
432 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
433 final String unknownPolicyId = "100";
434 expectedException.expect(BuilderException.class);
435 expectedException.expectMessage("Unknown policy " + unknownPolicyId);
436 builder.setExistingTriggerPolicy(unknownPolicyId);
440 public void testSetTriggerPolicyUnknownPolicy() throws BuilderException {
441 ControlLoopPolicyBuilder builder =
442 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
443 builder.setTriggerPolicy(
444 PolicyParam.builder()
445 .id(UUID.randomUUID().toString())
446 .name("Restart the VM")
447 .description("Upon getting the trigger event, restart the VM")
449 .target(new Target(TargetType.VM))
453 .timeout(300).build());
454 final String unknownPolicyId = "100";
455 expectedException.expect(BuilderException.class);
456 expectedException.expectMessage("Unknown policy " + unknownPolicyId);
457 builder.setExistingTriggerPolicy(unknownPolicyId);
461 public void testAddRemovePolicies() {
463 ControlLoopPolicyBuilder builder =
464 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
465 Policy triggerPolicy =
466 builder.setTriggerPolicy(
467 PolicyParam.builder()
468 .id(UUID.randomUUID().toString())
469 .name("Restart the VM")
470 .description("Upon getting the trigger event, restart the VM")
472 .target(new Target(TargetType.VM))
476 .timeout(300).build());
478 // Test create a policy and chain it to the results of trigger policy
480 Policy onRestartFailurePolicy1 = builder.setPolicyForPolicyResult(
481 PolicyParam.builder()
483 .description("If the restart fails, rebuild it.")
485 .target(new Target(TargetType.VM))
490 .id(triggerPolicy.getId()).build(),
491 PolicyResult.FAILURE,
492 PolicyResult.FAILURE_EXCEPTION,
493 PolicyResult.FAILURE_RETRIES,
494 PolicyResult.FAILURE_TIMEOUT,
495 PolicyResult.FAILURE_GUARD);
497 assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy1.getId()));
498 assertTrue(builder.getTriggerPolicy().getFailure_exception().equals(onRestartFailurePolicy1.getId()));
499 assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy1.getId()));
500 assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy1.getId()));
501 assertTrue(builder.getTriggerPolicy().getFailure_guard().equals(onRestartFailurePolicy1.getId()));
504 // Test create a policy and chain it to the results of trigger policy success
506 Policy onSuccessPolicy1 = builder.setPolicyForPolicyResult(
507 PolicyParam.builder()
508 .name("Do something")
509 .description("If the restart succeeds, do something else.")
511 .target(new Target(TargetType.VM))
512 .recipe("SomethingElse")
516 .id(triggerPolicy.getId()).build(),
517 PolicyResult.SUCCESS);
519 assertTrue(builder.getTriggerPolicy().getSuccess().equals(onSuccessPolicy1.getId()));
522 // Test remove policy
524 boolean removed = builder.removePolicy(onRestartFailurePolicy1.getId());
526 assertTrue(builder.getTriggerPolicy().getFailure().equals(FinalResult.FINAL_FAILURE.toString()));
527 assertTrue(builder.getTriggerPolicy().getFailure_retries()
528 .equals(FinalResult.FINAL_FAILURE_RETRIES.toString()));
529 assertTrue(builder.getTriggerPolicy().getFailure_timeout()
530 .equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString()));
532 builder.getTriggerPolicy().getFailure_guard().equals(FinalResult.FINAL_FAILURE_GUARD.toString()));
534 // Create another policy and chain it to the results of trigger policy
536 final Policy onRestartFailurePolicy2 =
537 builder.setPolicyForPolicyResult(
538 PolicyParam.builder()
540 .description("If the restart fails, rebuild it.")
542 .target(new Target(TargetType.VM))
547 .id(triggerPolicy.getId()).build(),
548 PolicyResult.FAILURE,
549 PolicyResult.FAILURE_RETRIES,
550 PolicyResult.FAILURE_TIMEOUT);
552 // Test reset policy results
554 triggerPolicy = builder.resetPolicyResults(triggerPolicy.getId());
555 assertTrue(builder.getTriggerPolicy().getFailure().equals(FinalResult.FINAL_FAILURE.toString()));
556 assertTrue(builder.getTriggerPolicy().getFailure_retries()
557 .equals(FinalResult.FINAL_FAILURE_RETRIES.toString()));
558 assertTrue(builder.getTriggerPolicy().getFailure_timeout()
559 .equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString()));
561 // Test set the policy results to an existing operational policy
563 Policy onRestartFailurePolicy3 =
564 builder.setPolicyForPolicyResult(onRestartFailurePolicy2.getId(), triggerPolicy.getId(),
565 PolicyResult.FAILURE, PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT);
566 assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy3.getId()));
567 assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy3.getId()));
568 assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy3.getId()));
570 // Test set the policy result for success to an existing operational policy
572 Policy onRestartFailurePolicy4 =
573 builder.setPolicyForPolicyResult(onRestartFailurePolicy2.getId(), triggerPolicy.getId(),
574 PolicyResult.FAILURE, PolicyResult.FAILURE_EXCEPTION, PolicyResult.FAILURE_GUARD,
575 PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT, PolicyResult.SUCCESS);
576 assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy4.getId()));
577 assertTrue(builder.getTriggerPolicy().getFailure_exception().equals(onRestartFailurePolicy4.getId()));
578 assertTrue(builder.getTriggerPolicy().getFailure_guard().equals(onRestartFailurePolicy4.getId()));
579 assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy4.getId()));
580 assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy4.getId()));
581 assertTrue(builder.getTriggerPolicy().getSuccess().equals(onRestartFailurePolicy4.getId()));
584 // Test remove all existing operational policies
586 builder = builder.removeAllPolicies();
587 assertTrue(builder.getControlLoop().getTrigger_policy().equals(FinalResult.FINAL_OPENLOOP.toString()));
589 } catch (BuilderException e) {
590 fail(e.getMessage());
595 public void testAddToUnknownPolicy() throws BuilderException {
596 ControlLoopPolicyBuilder builder =
597 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
598 final String policyId = "100";
599 expectedException.expect(BuilderException.class);
600 expectedException.expectMessage("Unknown policy " + policyId);
602 builder.setPolicyForPolicyResult(
603 PolicyParam.builder()
605 .description("If the restart fails, rebuild it.")
607 .target(new Target(TargetType.VM))
612 .id(policyId).build(),
613 PolicyResult.FAILURE,
614 PolicyResult.FAILURE_RETRIES,
615 PolicyResult.FAILURE_TIMEOUT,
616 PolicyResult.FAILURE_GUARD);
620 public void testAddExistingPolicyToUnknownPolicy() throws BuilderException {
621 ControlLoopPolicyBuilder builder =
622 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
623 Policy triggerPolicy =
624 builder.setTriggerPolicy(
625 PolicyParam.builder()
626 .id(UUID.randomUUID().toString())
627 .name("Restart the VM")
628 .description("Upon getting the trigger event, restart the VM")
630 .target(new Target(TargetType.VM))
634 .timeout(300).build());
637 Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult(
638 PolicyParam.builder()
640 .description("If the restart fails, rebuild it.")
642 .target(new Target(TargetType.VM))
647 .id(triggerPolicy.getId()).build(),
648 PolicyResult.FAILURE);
650 final String unknownPolicyId = "100";
651 expectedException.expect(BuilderException.class);
652 expectedException.expectMessage(unknownPolicyId + " does not exist");
654 builder.setPolicyForPolicyResult(onRestartFailurePolicy.getId(), unknownPolicyId, PolicyResult.FAILURE);
658 public void testAddUnknownExistingPolicyToPolicy() throws BuilderException {
659 ControlLoopPolicyBuilder builder =
660 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
661 Policy triggerPolicy =
662 builder.setTriggerPolicy(
663 PolicyParam.builder()
664 .id(UUID.randomUUID().toString())
665 .name("Restart the VM")
666 .description("Upon getting the trigger event, restart the VM")
668 .target(new Target(TargetType.VM))
672 .timeout(300).build());
674 final String unknownPolicyId = "100";
675 expectedException.expect(BuilderException.class);
676 expectedException.expectMessage("Operational policy " + unknownPolicyId + " does not exist");
678 builder.setPolicyForPolicyResult(unknownPolicyId, triggerPolicy.getId(), PolicyResult.FAILURE);
682 public void testAddOperationsAccumulateParams() {
684 ControlLoopPolicyBuilder builder =
685 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
686 Policy triggerPolicy =
687 builder.setTriggerPolicy(
688 PolicyParam.builder()
689 .id(UUID.randomUUID().toString())
690 .name("Restart the eNodeB")
691 .description("Upon getting the trigger event, restart the eNodeB")
692 .actor("RANController")
693 .target(new Target(TargetType.PNF))
697 .timeout(300).build());
699 // Add the operationsAccumulateParams
701 triggerPolicy = builder.addOperationsAccumulateParams(triggerPolicy.getId(),
702 new OperationsAccumulateParams("15m", 5));
703 assertNotNull(builder.getTriggerPolicy().getOperationsAccumulateParams());
704 assertTrue(builder.getTriggerPolicy().getOperationsAccumulateParams().getPeriod().equals("15m"));
705 assertTrue(builder.getTriggerPolicy().getOperationsAccumulateParams().getLimit() == 5);
707 } catch (BuilderException e) {
708 fail(e.getMessage());
714 public void testBuildSpecification() {
717 // Create the builder
719 ControlLoopPolicyBuilder builder =
720 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 800);
722 // Set the first invalid trigger policy
724 final Policy policy1 = builder.setTriggerPolicy(
725 PolicyParam.builder()
726 .id(UUID.randomUUID().toString())
727 .name("Restart the VM")
728 .description("Upon getting the trigger event, restart the VM")
731 .recipe("Instantiate")
734 .timeout(300).build());
735 Results results = builder.buildSpecification();
737 // Check that ERRORs are in results for invalid policy arguments
739 boolean invalidActor = false;
740 boolean invalidRecipe = false;
741 boolean invalidTarget = false;
742 for (Message m : results.getMessages()) {
743 if (m.getMessage().equals("Policy actor is null") && m.getLevel() == MessageLevel.ERROR) {
746 if (m.getMessage().equals("Policy recipe is invalid") && m.getLevel() == MessageLevel.ERROR) {
747 invalidRecipe = true;
749 if (m.getMessage().equals("Policy target is null") && m.getLevel() == MessageLevel.ERROR) {
750 invalidTarget = true;
754 assertTrue(invalidActor);
755 assertTrue(invalidRecipe);
756 assertTrue(invalidTarget);
758 // Remove the invalid policy
760 // @SuppressWarnings("unused")
761 boolean removed = builder.removePolicy(policy1.getId());
763 assertTrue(builder.getTriggerPolicy() == null);
765 // Set a valid trigger policy
767 Policy policy1a = builder.setTriggerPolicy(
768 PolicyParam.builder()
769 .id(UUID.randomUUID().toString())
771 .description("If the restart fails, rebuild it.")
773 .target(new Target(TargetType.VM))
777 .timeout(600).build());
779 // Set a second valid trigger policy
781 final Policy policy2 =
782 builder.setTriggerPolicy(
783 PolicyParam.builder()
784 .id(UUID.randomUUID().toString())
785 .name("Restart the VM")
786 .description("Upon getting the trigger event, restart the VM")
788 .target(new Target(TargetType.VM))
792 .timeout(300).build());
794 // Now, we have policy1 unreachable
796 results = builder.buildSpecification();
797 boolean unreachable = false;
798 for (Message m : results.getMessages()) {
799 if (m.getMessage().equals("Policy " + policy1a.getId() + " is not reachable.")
800 && m.getLevel() == MessageLevel.WARNING) {
805 assertTrue(unreachable);
807 // Set policy1a for the failure results of policy2
809 policy1a = builder.setPolicyForPolicyResult(policy1a.getId(), policy2.getId(), PolicyResult.FAILURE,
810 PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT);
811 results = builder.buildSpecification();
812 boolean invalidTimeout = false;
813 for (Message m : results.getMessages()) {
815 .equals("controlLoop overall timeout is less than the sum of operational policy timeouts.")
816 && m.getLevel() == MessageLevel.ERROR) {
817 invalidTimeout = true;
821 assertTrue(invalidTimeout);
823 // Remove policy2 (revert controlLoop back to open loop)
825 removed = builder.removePolicy(policy2.getId());
827 // ControlLoop is open loop now, but it still has policies (policy1)
829 results = builder.buildSpecification();
831 for (Message m : results.getMessages()) {
832 if (m.getMessage().equals("Open Loop policy contains policies. The policies will never be invoked.")
833 && m.getLevel() == MessageLevel.WARNING) {
838 assertTrue(unreachable);
840 } catch (BuilderException e) {
841 fail(e.getMessage());
847 public void test1() {
848 this.test("src/test/resources/v1.0.0/policy_Test.yaml");
852 public void testEvilYaml() {
853 try (InputStream is = new FileInputStream(new File("src/test/resources/v1.0.0/test_evil.yaml"))) {
855 // Read the yaml into our Java Object
857 Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
859 } catch (FileNotFoundException e) {
860 fail(e.getLocalizedMessage());
861 } catch (IOException e) {
862 fail(e.getLocalizedMessage());
863 } catch (YAMLException e) {
871 * Does the actual test.
873 * @param testFile input file
875 public void test(String testFile) {
876 try (InputStream is = new FileInputStream(new File(testFile))) {
878 // Read the yaml into our Java Object
880 Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
881 Object obj = yaml.load(is);
883 assertTrue(obj instanceof ControlLoopPolicy);
884 ControlLoopPolicy policyTobuild = (ControlLoopPolicy) obj;
886 // Now we're going to try to use the builder to build this.
888 ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(
889 policyTobuild.getControlLoop().getControlLoopName(), policyTobuild.getControlLoop().getTimeout());
893 if (policyTobuild.getControlLoop().getServices() != null) {
894 builder = builder.addService(policyTobuild.getControlLoop().getServices()
895 .toArray(new Service[policyTobuild.getControlLoop().getServices().size()]));
900 if (policyTobuild.getControlLoop().getResources() != null) {
901 builder = builder.addResource(policyTobuild.getControlLoop().getResources()
902 .toArray(new Resource[policyTobuild.getControlLoop().getResources().size()]));
907 if (policyTobuild.getControlLoop().getPnf() != null) {
908 builder = builder.setPNF(policyTobuild.getControlLoop().getPnf());
911 // Add the policies and be sure to set the trigger policy
913 if (policyTobuild.getPolicies() != null) {
914 for (Policy policy : policyTobuild.getPolicies()) {
915 if (policy.getId() == policyTobuild.getControlLoop().getTrigger_policy()) {
916 builder.setTriggerPolicy(
917 PolicyParam.builder()
918 .id(UUID.randomUUID().toString())
919 .name(policy.getName())
920 .description(policy.getDescription())
921 .actor(policy.getActor())
922 .target(policy.getTarget())
923 .recipe(policy.getRecipe())
925 .retries(policy.getRetry())
926 .timeout(policy.getTimeout()).build());
931 // Question : how to change policy ID and results by using builder ??
933 @SuppressWarnings("unused")
934 Results results = builder.buildSpecification();
936 } catch (FileNotFoundException e) {
937 fail(e.getLocalizedMessage());
938 } catch (IOException e) {
939 fail(e.getLocalizedMessage());
940 } catch (BuilderException e) {
941 fail(e.getLocalizedMessage());