2 * ============LICENSE_START=======================================================
3 * policy-yaml unit test
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 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;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assert.fail;
32 import java.io.FileInputStream;
33 import java.io.FileNotFoundException;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.util.UUID;
38 import org.junit.Ignore;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.ExpectedException;
42 import org.onap.policy.aai.Pnf;
43 import org.onap.policy.aai.PnfType;
44 import org.onap.policy.controlloop.policy.builder.BuilderException;
45 import org.onap.policy.controlloop.policy.builder.ControlLoopPolicyBuilder;
46 import org.onap.policy.controlloop.policy.builder.Message;
47 import org.onap.policy.controlloop.policy.builder.MessageLevel;
48 import org.onap.policy.controlloop.policy.builder.Results;
49 import org.onap.policy.sdc.Resource;
50 import org.onap.policy.sdc.ResourceType;
51 import org.onap.policy.sdc.Service;
52 import org.yaml.snakeyaml.Yaml;
53 import org.yaml.snakeyaml.constructor.Constructor;
54 import org.yaml.snakeyaml.error.YAMLException;
57 public class ControlLoopPolicyBuilderTest {
60 public ExpectedException expectedException = ExpectedException.none();
63 public void testControlLoop() {
66 // Create a builder for our policy
68 ControlLoopPolicyBuilder builder =
69 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
73 Service scp = new Service("vSCP");
74 Service usp = new Service("vUSP");
75 Service trinity = new Service("Trinity");
76 builder = builder.addService(scp, usp, trinity);
77 assertTrue(builder.getControlLoop().getServices().size() == 3);
79 // Test remove services
81 builder = builder.removeService(scp);
82 assertTrue(builder.getControlLoop().getServices().size() == 2);
83 builder = builder.removeAllServices();
84 assertTrue(builder.getControlLoop().getServices().size() == 0);
88 Resource cts = new Resource("vCTS", ResourceType.VF);
89 Resource com = new Resource("vCTS", ResourceType.VF);
90 Resource rar = new Resource("vCTS", ResourceType.VF);
91 builder = builder.addResource(cts, com, rar);
92 assertTrue(builder.getControlLoop().getResources().size() == 3);
94 // Test remove resources
96 builder = builder.removeResource(cts);
97 assertTrue(builder.getControlLoop().getResources().size() == 2);
98 builder = builder.removeAllResources();
99 assertTrue(builder.getControlLoop().getResources().size() == 0);
100 } catch (BuilderException e) {
101 fail(e.getMessage());
106 public void testAddNullService() throws BuilderException {
107 ControlLoopPolicyBuilder builder =
108 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
109 expectedException.expect(BuilderException.class);
110 expectedException.expectMessage("Service must not be null");
111 builder.addService((Service) null);
115 public void testAddInvalidService() throws BuilderException {
116 ControlLoopPolicyBuilder builder =
117 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
118 expectedException.expect(BuilderException.class);
119 expectedException.expectMessage("Invalid service - need either a serviceUUID or serviceName");
120 builder.addService(new Service());
124 public void testAddServiceWithUuid() throws BuilderException {
125 ControlLoopPolicyBuilder builder =
126 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
127 UUID uuid = UUID.randomUUID();
128 Service serviceWithUuid = new Service(uuid);
129 builder.addService(serviceWithUuid);
130 assertTrue(builder.getControlLoop().getServices().size() == 1);
134 public void testAddNullResource() throws BuilderException {
135 ControlLoopPolicyBuilder builder =
136 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
137 expectedException.expect(BuilderException.class);
138 expectedException.expectMessage("Resource must not be null");
139 builder.addResource((Resource) null);
144 public void testAddInvalidResource() throws BuilderException {
145 ControlLoopPolicyBuilder builder =
146 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
147 expectedException.expect(BuilderException.class);
148 expectedException.expectMessage("Invalid resource - need either resourceUUID or resourceName");
149 builder.addResource(new Resource());
153 public void testAddAndRemoveResourceWithUuid() throws BuilderException {
154 ControlLoopPolicyBuilder builder =
155 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
156 UUID uuid = UUID.randomUUID();
157 Resource resourceWithUuid = new Resource(uuid);
158 builder.addResource(resourceWithUuid);
159 assertTrue(builder.getControlLoop().getResources().size() == 1);
161 builder.removeResource(resourceWithUuid);
162 assertTrue(builder.getControlLoop().getResources().size() == 0);
166 public void testRemoveNullResource() throws BuilderException {
167 ControlLoopPolicyBuilder builder =
168 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
169 Resource resource = new Resource("resource1", ResourceType.VF);
170 builder.addResource(resource);
171 expectedException.expect(BuilderException.class);
172 expectedException.expectMessage("Resource must not be null");
173 builder.removeResource((Resource) null);
177 public void testRemoveResourceNoExistingResources() throws BuilderException {
178 ControlLoopPolicyBuilder builder =
179 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
180 expectedException.expect(BuilderException.class);
181 expectedException.expectMessage("No existing resources to remove");
182 builder.removeResource(new Resource("resource1", ResourceType.VF));
186 public void testRemoveInvalidResource() throws BuilderException {
187 ControlLoopPolicyBuilder builder =
188 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
189 Resource resource = new Resource("resource1", ResourceType.VF);
190 builder.addResource(resource);
191 expectedException.expect(BuilderException.class);
192 expectedException.expectMessage("Invalid resource - need either a resourceUUID or resourceName");
193 builder.removeResource(new Resource());
197 public void testRemoveUnknownResource() throws BuilderException {
198 ControlLoopPolicyBuilder builder =
199 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
200 Resource resource = new Resource("resource1", ResourceType.VF);
201 builder.addResource(resource);
202 final String unknownResourceName = "reource2";
203 expectedException.expect(BuilderException.class);
204 expectedException.expectMessage("Unknown resource " + unknownResourceName);
205 builder.removeResource(new Resource(unknownResourceName, ResourceType.VF));
209 public void testControlLoopWithInitialResourceAndServices() {
211 Resource cts = new Resource("vCTS", ResourceType.VF);
212 Service scp = new Service("vSCP");
213 Service usp = new Service("vUSP");
214 ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory
215 .buildControlLoop(UUID.randomUUID().toString(), 2400, cts, scp, usp);
216 assertTrue(builder.getControlLoop().getResources().size() == 1);
217 assertTrue(builder.getControlLoop().getServices().size() == 2);
218 } catch (BuilderException e) {
219 fail(e.getMessage());
224 public void testControlLoopWithInitialResourcesAndService() {
226 Resource cts = new Resource("vCTS", ResourceType.VF);
227 Resource com = new Resource("vCTS", ResourceType.VF);
228 Service scp = new Service("vSCP");
229 ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory
230 .buildControlLoop(UUID.randomUUID().toString(), 2400, scp, cts, com);
231 assertTrue(builder.getControlLoop().getServices().size() == 1);
232 assertTrue(builder.getControlLoop().getResources().size() == 2);
233 } catch (BuilderException e) {
234 fail(e.getMessage());
240 // I'VE MARKED THIS TEST CASE AS IGNORE BECAUSE THE TEST CASE FAILS
241 // This test case fails because builder.getControlLoop() returns an instance of ControlLoop
243 // the ControlLoop(ControlLoop controlLoop) constructor.
244 // This constructor does not copy the value of pnf into the newly created object
245 // On the face of it, this looks like a bug, but perhaps there is a reason for this
246 // PLEASE ADVISE IF THE BEHAVIOUR IS INCORRECT OR THE TEST CASE IS INVALID
247 public void testControlLoopForPnf() {
250 pnf.setPnfType(PnfType.ENODEB);
251 ControlLoopPolicyBuilder builder =
252 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, pnf);
253 assertEquals(pnf, builder.getControlLoop().getPnf());
256 assertNull(builder.getControlLoop().getPnf());
257 } catch (BuilderException e) {
258 fail(e.getMessage());
264 // Fails for the same reason as the above test case
265 public void testSetAndRemovePnf() throws BuilderException {
266 ControlLoopPolicyBuilder builder =
267 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
268 assertNull(builder.getControlLoop().getPnf());
271 pnf.setPnfType(PnfType.ENODEB);
273 assertEquals(pnf, builder.getControlLoop().getPnf());
276 assertNull(builder.getControlLoop().getPnf());
280 public void testSetNullPnf() throws BuilderException {
281 ControlLoopPolicyBuilder builder =
282 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
283 expectedException.expect(BuilderException.class);
284 expectedException.expectMessage("PNF must not be null");
285 builder.setPNF(null);
289 public void testSetInvalidPnf() throws BuilderException {
290 ControlLoopPolicyBuilder builder =
291 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
292 expectedException.expect(BuilderException.class);
293 expectedException.expectMessage("Invalid PNF - need either pnfName or pnfType");
294 builder.setPNF(new Pnf());
298 public void testSetAbatement() throws BuilderException {
299 ControlLoopPolicyBuilder builder =
300 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
301 assertFalse(builder.getControlLoop().getAbatement());
302 builder = builder.setAbatement(true);
303 assertTrue(builder.getControlLoop().getAbatement());
307 public void testSetNullAbatement() throws BuilderException {
308 ControlLoopPolicyBuilder builder =
309 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
310 expectedException.expect(BuilderException.class);
311 expectedException.expectMessage("abatement must not be null");
312 builder = builder.setAbatement(null);
316 public void testTimeout() {
319 // Create a builder for our policy
321 ControlLoopPolicyBuilder builder =
322 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
326 assertTrue(builder.getControlLoop().getTimeout() == 2400);
327 builder = builder.setTimeout(800);
328 assertTrue(builder.getControlLoop().getTimeout() == 800);
330 // Test calculateTimeout
333 builder.setTriggerPolicy(PolicyParam.builder().id(UUID.randomUUID().toString())
334 .name("Restart the VM")
335 .description("Upon getting the trigger event, restart the VM")
337 .target(new Target(TargetType.VM))
341 .timeout(300).build());
342 @SuppressWarnings("unused")
343 Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult(
344 PolicyParam.builder()
346 .description("If the restart fails, rebuild it")
348 .target(new Target(TargetType.VM))
353 .id(trigger.getId()).build(),
354 PolicyResult.FAILURE,
355 PolicyResult.FAILURE_RETRIES,
356 PolicyResult.FAILURE_TIMEOUT);
357 assertTrue(builder.calculateTimeout().equals(new Integer(300 + 600)));
359 } catch (BuilderException e) {
360 fail(e.getMessage());
365 public void testTriggerPolicyMethods() {
367 ControlLoopPolicyBuilder builder =
368 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
372 assertTrue(builder.isOpenLoop());
374 // Test set initial trigger policy
376 Policy triggerPolicy1 =
377 builder.setTriggerPolicy(
378 PolicyParam.builder().id(UUID.randomUUID().toString())
379 .name("Restart the VM")
380 .description("Upon getting the trigger event, restart the VM")
382 .target(new Target(TargetType.VM))
386 .timeout(300).build());
387 assertTrue(builder.isOpenLoop() == false);
388 assertTrue(builder.getControlLoop().getTrigger_policy().equals(triggerPolicy1.getId()));
390 // Set trigger policy to a new policy
392 @SuppressWarnings("unused")
393 Policy triggerPolicy2 =
394 builder.setTriggerPolicy(
395 PolicyParam.builder()
396 .id(UUID.randomUUID().toString())
397 .name("Rebuild the VM")
398 .description("Upon getting the trigger event, rebuild the VM")
400 .target(new Target(TargetType.VM))
404 .timeout(300).build());
406 // Test set trigger policy to another existing policy
408 @SuppressWarnings("unused")
409 ControlLoop cl = builder.setExistingTriggerPolicy(triggerPolicy1.getId());
410 assertTrue(builder.getControlLoop().getTrigger_policy().equals(triggerPolicy1.getId()));
412 // Test get trigger policy
414 assertTrue(builder.getTriggerPolicy().equals(triggerPolicy1));
416 } catch (BuilderException e) {
417 fail(e.getMessage());
422 public void testSetTriggerPolicyNullPolicyId() throws BuilderException {
423 ControlLoopPolicyBuilder builder =
424 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
425 expectedException.expect(BuilderException.class);
426 expectedException.expectMessage("Id must not be null");
427 builder.setExistingTriggerPolicy(null);
431 public void testSetTriggerPolicyNoPoliciesExist() throws BuilderException {
432 ControlLoopPolicyBuilder builder =
433 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
434 final String unknownPolicyId = "100";
435 expectedException.expect(BuilderException.class);
436 expectedException.expectMessage("Unknown policy " + unknownPolicyId);
437 builder.setExistingTriggerPolicy(unknownPolicyId);
441 public void testSetTriggerPolicyUnknownPolicy() throws BuilderException {
442 ControlLoopPolicyBuilder builder =
443 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
444 builder.setTriggerPolicy(
445 PolicyParam.builder()
446 .id(UUID.randomUUID().toString())
447 .name("Restart the VM")
448 .description("Upon getting the trigger event, restart the VM")
450 .target(new Target(TargetType.VM))
454 .timeout(300).build());
455 final String unknownPolicyId = "100";
456 expectedException.expect(BuilderException.class);
457 expectedException.expectMessage("Unknown policy " + unknownPolicyId);
458 builder.setExistingTriggerPolicy(unknownPolicyId);
462 public void testAddRemovePolicies() {
464 ControlLoopPolicyBuilder builder =
465 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
466 Policy triggerPolicy =
467 builder.setTriggerPolicy(
468 PolicyParam.builder()
469 .id(UUID.randomUUID().toString())
470 .name("Restart the VM")
471 .description("Upon getting the trigger event, restart the VM")
473 .target(new Target(TargetType.VM))
477 .timeout(300).build());
479 // Test create a policy and chain it to the results of trigger policy
481 Policy onRestartFailurePolicy1 = builder.setPolicyForPolicyResult(
482 PolicyParam.builder()
484 .description("If the restart fails, rebuild it.")
486 .target(new Target(TargetType.VM))
491 .id(triggerPolicy.getId()).build(),
492 PolicyResult.FAILURE,
493 PolicyResult.FAILURE_EXCEPTION,
494 PolicyResult.FAILURE_RETRIES,
495 PolicyResult.FAILURE_TIMEOUT,
496 PolicyResult.FAILURE_GUARD);
498 assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy1.getId()));
499 assertTrue(builder.getTriggerPolicy().getFailure_exception().equals(onRestartFailurePolicy1.getId()));
500 assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy1.getId()));
501 assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy1.getId()));
502 assertTrue(builder.getTriggerPolicy().getFailure_guard().equals(onRestartFailurePolicy1.getId()));
505 // Test create a policy and chain it to the results of trigger policy success
507 Policy onSuccessPolicy1 = builder.setPolicyForPolicyResult(
508 PolicyParam.builder()
509 .name("Do something")
510 .description("If the restart succeeds, do something else.")
512 .target(new Target(TargetType.VM))
513 .recipe("SomethingElse")
517 .id(triggerPolicy.getId()).build(),
518 PolicyResult.SUCCESS);
520 assertTrue(builder.getTriggerPolicy().getSuccess().equals(onSuccessPolicy1.getId()));
523 // Test remove policy
525 boolean removed = builder.removePolicy(onRestartFailurePolicy1.getId());
527 assertTrue(builder.getTriggerPolicy().getFailure().equals(FinalResult.FINAL_FAILURE.toString()));
528 assertTrue(builder.getTriggerPolicy().getFailure_retries()
529 .equals(FinalResult.FINAL_FAILURE_RETRIES.toString()));
530 assertTrue(builder.getTriggerPolicy().getFailure_timeout()
531 .equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString()));
533 builder.getTriggerPolicy().getFailure_guard().equals(FinalResult.FINAL_FAILURE_GUARD.toString()));
535 // Create another policy and chain it to the results of trigger policy
537 final Policy onRestartFailurePolicy2 =
538 builder.setPolicyForPolicyResult(
539 PolicyParam.builder()
541 .description("If the restart fails, rebuild it.")
543 .target(new Target(TargetType.VM))
548 .id(triggerPolicy.getId()).build(),
549 PolicyResult.FAILURE,
550 PolicyResult.FAILURE_RETRIES,
551 PolicyResult.FAILURE_TIMEOUT);
553 // Test reset policy results
555 triggerPolicy = builder.resetPolicyResults(triggerPolicy.getId());
556 assertTrue(builder.getTriggerPolicy().getFailure().equals(FinalResult.FINAL_FAILURE.toString()));
557 assertTrue(builder.getTriggerPolicy().getFailure_retries()
558 .equals(FinalResult.FINAL_FAILURE_RETRIES.toString()));
559 assertTrue(builder.getTriggerPolicy().getFailure_timeout()
560 .equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString()));
562 // Test set the policy results to an existing operational policy
564 Policy onRestartFailurePolicy3 =
565 builder.setPolicyForPolicyResult(onRestartFailurePolicy2.getId(), triggerPolicy.getId(),
566 PolicyResult.FAILURE, PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT);
567 assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy3.getId()));
568 assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy3.getId()));
569 assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy3.getId()));
571 // Test set the policy result for success to an existing operational policy
573 Policy onRestartFailurePolicy4 =
574 builder.setPolicyForPolicyResult(onRestartFailurePolicy2.getId(), triggerPolicy.getId(),
575 PolicyResult.FAILURE, PolicyResult.FAILURE_EXCEPTION, PolicyResult.FAILURE_GUARD,
576 PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT, PolicyResult.SUCCESS);
577 assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy4.getId()));
578 assertTrue(builder.getTriggerPolicy().getFailure_exception().equals(onRestartFailurePolicy4.getId()));
579 assertTrue(builder.getTriggerPolicy().getFailure_guard().equals(onRestartFailurePolicy4.getId()));
580 assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy4.getId()));
581 assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy4.getId()));
582 assertTrue(builder.getTriggerPolicy().getSuccess().equals(onRestartFailurePolicy4.getId()));
585 // Test remove all existing operational policies
587 builder = builder.removeAllPolicies();
588 assertTrue(builder.getControlLoop().getTrigger_policy().equals(FinalResult.FINAL_OPENLOOP.toString()));
590 } catch (BuilderException e) {
591 fail(e.getMessage());
596 public void testAddToUnknownPolicy() throws BuilderException {
597 ControlLoopPolicyBuilder builder =
598 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
599 final String policyId = "100";
600 expectedException.expect(BuilderException.class);
601 expectedException.expectMessage("Unknown policy " + policyId);
603 builder.setPolicyForPolicyResult(
604 PolicyParam.builder()
606 .description("If the restart fails, rebuild it.")
608 .target(new Target(TargetType.VM))
613 .id(policyId).build(),
614 PolicyResult.FAILURE,
615 PolicyResult.FAILURE_RETRIES,
616 PolicyResult.FAILURE_TIMEOUT,
617 PolicyResult.FAILURE_GUARD);
621 public void testAddExistingPolicyToUnknownPolicy() throws BuilderException {
622 ControlLoopPolicyBuilder builder =
623 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
624 Policy triggerPolicy =
625 builder.setTriggerPolicy(
626 PolicyParam.builder()
627 .id(UUID.randomUUID().toString())
628 .name("Restart the VM")
629 .description("Upon getting the trigger event, restart the VM")
631 .target(new Target(TargetType.VM))
635 .timeout(300).build());
638 Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult(
639 PolicyParam.builder()
641 .description("If the restart fails, rebuild it.")
643 .target(new Target(TargetType.VM))
648 .id(triggerPolicy.getId()).build(),
649 PolicyResult.FAILURE);
651 final String unknownPolicyId = "100";
652 expectedException.expect(BuilderException.class);
653 expectedException.expectMessage(unknownPolicyId + " does not exist");
655 builder.setPolicyForPolicyResult(onRestartFailurePolicy.getId(), unknownPolicyId, PolicyResult.FAILURE);
659 public void testAddUnknownExistingPolicyToPolicy() throws BuilderException {
660 ControlLoopPolicyBuilder builder =
661 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
662 Policy triggerPolicy =
663 builder.setTriggerPolicy(
664 PolicyParam.builder()
665 .id(UUID.randomUUID().toString())
666 .name("Restart the VM")
667 .description("Upon getting the trigger event, restart the VM")
669 .target(new Target(TargetType.VM))
673 .timeout(300).build());
675 final String unknownPolicyId = "100";
676 expectedException.expect(BuilderException.class);
677 expectedException.expectMessage("Operational policy " + unknownPolicyId + " does not exist");
679 builder.setPolicyForPolicyResult(unknownPolicyId, triggerPolicy.getId(), PolicyResult.FAILURE);
683 public void testAddOperationsAccumulateParams() {
685 ControlLoopPolicyBuilder builder =
686 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
687 Policy triggerPolicy =
688 builder.setTriggerPolicy(
689 PolicyParam.builder()
690 .id(UUID.randomUUID().toString())
691 .name("Restart the eNodeB")
692 .description("Upon getting the trigger event, restart the eNodeB")
693 .actor("RANController")
694 .target(new Target(TargetType.PNF))
698 .timeout(300).build());
700 // Add the operationsAccumulateParams
702 triggerPolicy = builder.addOperationsAccumulateParams(triggerPolicy.getId(),
703 new OperationsAccumulateParams("15m", 5));
704 assertNotNull(builder.getTriggerPolicy().getOperationsAccumulateParams());
705 assertTrue(builder.getTriggerPolicy().getOperationsAccumulateParams().getPeriod().equals("15m"));
706 assertTrue(builder.getTriggerPolicy().getOperationsAccumulateParams().getLimit() == 5);
708 } catch (BuilderException e) {
709 fail(e.getMessage());
715 public void testBuildSpecification() {
718 // Create the builder
720 ControlLoopPolicyBuilder builder =
721 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 800);
723 // Set the first invalid trigger policy
725 final Policy policy1 = builder.setTriggerPolicy(
726 PolicyParam.builder()
727 .id(UUID.randomUUID().toString())
728 .name("Restart the VM")
729 .description("Upon getting the trigger event, restart the VM")
732 .recipe("Instantiate")
735 .timeout(300).build());
736 Results results = builder.buildSpecification();
738 // Check that ERRORs are in results for invalid policy arguments
740 boolean invalidActor = false;
741 boolean invalidRecipe = false;
742 boolean invalidTarget = false;
743 for (Message m : results.getMessages()) {
744 if (m.getMessage().equals("Policy actor is null") && m.getLevel() == MessageLevel.ERROR) {
747 if (m.getMessage().equals("Policy recipe is invalid") && m.getLevel() == MessageLevel.ERROR) {
748 invalidRecipe = true;
750 if (m.getMessage().equals("Policy target is null") && m.getLevel() == MessageLevel.ERROR) {
751 invalidTarget = true;
755 assertTrue(invalidActor);
756 assertTrue(invalidRecipe);
757 assertTrue(invalidTarget);
759 // Remove the invalid policy
761 // @SuppressWarnings("unused")
762 boolean removed = builder.removePolicy(policy1.getId());
764 assertTrue(builder.getTriggerPolicy() == null);
766 // Set a valid trigger policy
768 Policy policy1a = builder.setTriggerPolicy(
769 PolicyParam.builder()
770 .id(UUID.randomUUID().toString())
772 .description("If the restart fails, rebuild it.")
774 .target(new Target(TargetType.VM))
778 .timeout(600).build());
780 // Set a second valid trigger policy
782 final Policy policy2 =
783 builder.setTriggerPolicy(
784 PolicyParam.builder()
785 .id(UUID.randomUUID().toString())
786 .name("Restart the VM")
787 .description("Upon getting the trigger event, restart the VM")
789 .target(new Target(TargetType.VM))
793 .timeout(300).build());
795 // Now, we have policy1 unreachable
797 results = builder.buildSpecification();
798 boolean unreachable = false;
799 for (Message m : results.getMessages()) {
800 if (m.getMessage().equals("Policy " + policy1a.getId() + " is not reachable.")
801 && m.getLevel() == MessageLevel.WARNING) {
806 assertTrue(unreachable);
808 // Set policy1a for the failure results of policy2
810 policy1a = builder.setPolicyForPolicyResult(policy1a.getId(), policy2.getId(), PolicyResult.FAILURE,
811 PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT);
812 results = builder.buildSpecification();
813 boolean invalidTimeout = false;
814 for (Message m : results.getMessages()) {
816 .equals("controlLoop overall timeout is less than the sum of operational policy timeouts.")
817 && m.getLevel() == MessageLevel.ERROR) {
818 invalidTimeout = true;
822 assertTrue(invalidTimeout);
824 // Remove policy2 (revert controlLoop back to open loop)
826 removed = builder.removePolicy(policy2.getId());
828 // ControlLoop is open loop now, but it still has policies (policy1)
830 results = builder.buildSpecification();
832 for (Message m : results.getMessages()) {
833 if (m.getMessage().equals("Open Loop policy contains policies. The policies will never be invoked.")
834 && m.getLevel() == MessageLevel.WARNING) {
839 assertTrue(unreachable);
841 } catch (BuilderException e) {
842 fail(e.getMessage());
848 public void test1() {
849 this.test("src/test/resources/v1.0.0/policy_Test.yaml");
853 public void testEvilYaml() {
854 try (InputStream is = new FileInputStream(new File("src/test/resources/v1.0.0/test_evil.yaml"))) {
856 // Read the yaml into our Java Object
858 Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
860 } catch (FileNotFoundException e) {
861 fail(e.getLocalizedMessage());
862 } catch (IOException e) {
863 fail(e.getLocalizedMessage());
864 } catch (YAMLException e) {
872 * Does the actual test.
874 * @param testFile input file
876 public void test(String testFile) {
877 try (InputStream is = new FileInputStream(new File(testFile))) {
879 // Read the yaml into our Java Object
881 Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
882 Object obj = yaml.load(is);
884 assertTrue(obj instanceof ControlLoopPolicy);
885 ControlLoopPolicy policyTobuild = (ControlLoopPolicy) obj;
887 // Now we're going to try to use the builder to build this.
889 ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(
890 policyTobuild.getControlLoop().getControlLoopName(), policyTobuild.getControlLoop().getTimeout());
894 if (policyTobuild.getControlLoop().getServices() != null) {
895 builder = builder.addService(policyTobuild.getControlLoop().getServices()
896 .toArray(new Service[policyTobuild.getControlLoop().getServices().size()]));
901 if (policyTobuild.getControlLoop().getResources() != null) {
902 builder = builder.addResource(policyTobuild.getControlLoop().getResources()
903 .toArray(new Resource[policyTobuild.getControlLoop().getResources().size()]));
908 if (policyTobuild.getControlLoop().getPnf() != null) {
909 builder = builder.setPNF(policyTobuild.getControlLoop().getPnf());
912 // Add the policies and be sure to set the trigger policy
914 if (policyTobuild.getPolicies() != null) {
915 for (Policy policy : policyTobuild.getPolicies()) {
916 if (policy.getId() == policyTobuild.getControlLoop().getTrigger_policy()) {
917 builder.setTriggerPolicy(
918 PolicyParam.builder()
919 .id(UUID.randomUUID().toString())
920 .name(policy.getName())
921 .description(policy.getDescription())
922 .actor(policy.getActor())
923 .target(policy.getTarget())
924 .recipe(policy.getRecipe())
926 .retries(policy.getRetry())
927 .timeout(policy.getTimeout()).build());
932 // Question : how to change policy ID and results by using builder ??
934 @SuppressWarnings("unused")
935 Results results = builder.buildSpecification();
937 } catch (FileNotFoundException e) {
938 fail(e.getLocalizedMessage());
939 } catch (IOException e) {
940 fail(e.getLocalizedMessage());
941 } catch (BuilderException e) {
942 fail(e.getLocalizedMessage());