2 * ============LICENSE_START=======================================================
3 * policy-yaml unit test
4 * ================================================================================
5 * Copyright (C) 2017-2020 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.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
32 import java.io.FileInputStream;
33 import java.io.InputStream;
34 import java.util.UUID;
35 import org.junit.Ignore;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.onap.aai.domain.yang.Pnf;
40 import org.onap.policy.controlloop.policy.builder.BuilderException;
41 import org.onap.policy.controlloop.policy.builder.ControlLoopPolicyBuilder;
42 import org.onap.policy.controlloop.policy.builder.Message;
43 import org.onap.policy.controlloop.policy.builder.MessageLevel;
44 import org.onap.policy.controlloop.policy.builder.Results;
45 import org.onap.policy.sdc.Resource;
46 import org.onap.policy.sdc.ResourceType;
47 import org.onap.policy.sdc.Service;
48 import org.yaml.snakeyaml.Yaml;
49 import org.yaml.snakeyaml.constructor.Constructor;
50 import org.yaml.snakeyaml.error.YAMLException;
53 public class ControlLoopPolicyBuilderTest {
55 private static final String RESOURCE1 = "resource1";
56 private static final String TRIGGER_RESTART = "Upon getting the trigger event, restart the VM";
57 private static final String UNKNOWN_POLICY = "Unknown policy ";
58 private static final String RESTART = "Restart";
59 private static final String RESTART_VM = "Restart the VM";
60 private static final String REBUILD = "Rebuild";
61 private static final String REBUILD_VM = "Rebuild VM";
62 private static final String REBUILD_RESTART = "If the restart fails, rebuild it.";
64 public ExpectedException expectedException = ExpectedException.none();
67 public void testControlLoop() throws BuilderException {
69 // Create a builder for our policy
71 ControlLoopPolicyBuilder builder =
72 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
76 Service scp = new Service("vSCP");
77 Service usp = new Service("vUSP");
78 Service trinity = new Service("Trinity");
79 builder = builder.addService(scp, usp, trinity);
80 assertTrue(builder.getControlLoop().getServices().size() == 3);
82 // Test remove services
84 builder = builder.removeService(scp);
85 assertTrue(builder.getControlLoop().getServices().size() == 2);
86 builder = builder.removeAllServices();
87 assertTrue(builder.getControlLoop().getServices().isEmpty());
91 Resource cts = new Resource("vCTS", ResourceType.VF);
92 Resource com = new Resource("vCTS", ResourceType.VF);
93 Resource rar = new Resource("vCTS", ResourceType.VF);
94 builder = builder.addResource(cts, com, rar);
95 assertTrue(builder.getControlLoop().getResources().size() == 3);
97 // Test remove resources
99 builder = builder.removeResource(cts);
100 assertTrue(builder.getControlLoop().getResources().size() == 2);
101 builder = builder.removeAllResources();
102 assertTrue(builder.getControlLoop().getResources().isEmpty());
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().isEmpty());
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() throws BuilderException {
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);
220 public void testControlLoopWithInitialResourcesAndService() throws BuilderException {
221 Resource cts = new Resource("vCTS", ResourceType.VF);
222 Resource com = new Resource("vCTS", ResourceType.VF);
223 Service scp = new Service("vSCP");
224 ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory
225 .buildControlLoop(UUID.randomUUID().toString(), 2400, scp, cts, com);
226 assertTrue(builder.getControlLoop().getServices().size() == 1);
227 assertTrue(builder.getControlLoop().getResources().size() == 2);
232 // I'VE MARKED THIS TEST CASE AS IGNORE BECAUSE THE TEST CASE FAILS
233 // This test case fails because builder.getControlLoop() returns an instance of ControlLoop
235 // the ControlLoop(ControlLoop controlLoop) constructor.
236 // This constructor does not copy the value of pnf into the newly created object
237 // On the face of it, this looks like a bug, but perhaps there is a reason for this
238 // PLEASE ADVISE IF THE BEHAVIOUR IS INCORRECT OR THE TEST CASE IS INVALID
239 public void testControlLoopForPnf() throws BuilderException {
241 //pnf.setPnfType(PnfType.ENODEB);
242 ControlLoopPolicyBuilder builder =
243 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, pnf);
244 assertEquals(pnf, builder.getControlLoop().getPnf());
247 assertNull(builder.getControlLoop().getPnf());
252 // Fails for the same reason as the above test case
253 public void testSetAndRemovePnf() throws BuilderException {
254 ControlLoopPolicyBuilder builder =
255 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
256 assertNull(builder.getControlLoop().getPnf());
259 //pnf.setPnfType(PnfType.ENODEB);
261 assertEquals(pnf, builder.getControlLoop().getPnf());
264 assertNull(builder.getControlLoop().getPnf());
268 public void testSetNullPnf() throws BuilderException {
269 ControlLoopPolicyBuilder builder =
270 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
271 expectedException.expect(BuilderException.class);
272 expectedException.expectMessage("PNF must not be null");
273 builder.setPnf(null);
277 public void testSetInvalidPnf() throws BuilderException {
278 ControlLoopPolicyBuilder builder =
279 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
280 expectedException.expect(BuilderException.class);
281 expectedException.expectMessage("Invalid PNF - need either pnfName or pnfType");
282 builder.setPnf(new Pnf());
286 public void testSetAbatement() throws BuilderException {
287 ControlLoopPolicyBuilder builder =
288 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
289 assertFalse(builder.getControlLoop().getAbatement());
290 builder = builder.setAbatement(true);
291 assertTrue(builder.getControlLoop().getAbatement());
295 public void testSetNullAbatement() throws BuilderException {
296 ControlLoopPolicyBuilder builder =
297 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
298 expectedException.expect(BuilderException.class);
299 expectedException.expectMessage("abatement must not be null");
300 builder = builder.setAbatement(null);
304 public void testTimeout() throws BuilderException {
306 // Create a builder for our policy
308 ControlLoopPolicyBuilder builder =
309 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
313 assertTrue(builder.getControlLoop().getTimeout() == 2400);
314 builder = builder.setTimeout(800);
315 assertTrue(builder.getControlLoop().getTimeout() == 800);
317 // Test calculateTimeout
320 builder.setTriggerPolicy(PolicyParam.builder().id(UUID.randomUUID().toString())
322 .description(TRIGGER_RESTART)
324 .target(new Target(TargetType.VM))
328 .timeout(300).build());
329 @SuppressWarnings("unused")
330 Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult(
331 PolicyParam.builder()
333 .description("If the restart fails, rebuild it")
335 .target(new Target(TargetType.VM))
340 .id(trigger.getId()).build(),
341 PolicyResult.FAILURE,
342 PolicyResult.FAILURE_RETRIES,
343 PolicyResult.FAILURE_TIMEOUT);
344 assertEquals(Integer.valueOf(300 + 600), builder.calculateTimeout());
348 public void testTriggerPolicyMethods() throws BuilderException {
349 ControlLoopPolicyBuilder builder =
350 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
354 assertTrue(builder.isOpenLoop());
356 // Test set initial trigger policy
358 Policy triggerPolicy1 =
359 builder.setTriggerPolicy(
360 PolicyParam.builder().id(UUID.randomUUID().toString())
362 .description(TRIGGER_RESTART)
364 .target(new Target(TargetType.VM))
368 .timeout(300).build());
369 assertFalse(builder.isOpenLoop());
370 assertEquals(builder.getControlLoop().getTrigger_policy(), triggerPolicy1.getId());
372 // Set trigger policy to a new policy
374 @SuppressWarnings("unused")
375 Policy triggerPolicy2 =
376 builder.setTriggerPolicy(
377 PolicyParam.builder()
378 .id(UUID.randomUUID().toString())
379 .name("Rebuild the VM")
380 .description("Upon getting the trigger event, rebuild the VM")
382 .target(new Target(TargetType.VM))
386 .timeout(300).build());
388 // Test set trigger policy to another existing policy
390 @SuppressWarnings("unused")
391 ControlLoop cl = builder.setExistingTriggerPolicy(triggerPolicy1.getId());
392 assertTrue(builder.getControlLoop().getTrigger_policy().equals(triggerPolicy1.getId()));
394 // Test get trigger policy
396 assertTrue(builder.getTriggerPolicy().equals(triggerPolicy1));
400 public void testSetTriggerPolicyNullPolicyId() throws BuilderException {
401 ControlLoopPolicyBuilder builder =
402 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
403 expectedException.expect(BuilderException.class);
404 expectedException.expectMessage("Id must not be null");
405 builder.setExistingTriggerPolicy(null);
409 public void testSetTriggerPolicyNoPoliciesExist() throws BuilderException {
410 ControlLoopPolicyBuilder builder =
411 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
412 final String unknownPolicyId = "100";
413 expectedException.expect(BuilderException.class);
414 expectedException.expectMessage(UNKNOWN_POLICY + unknownPolicyId);
415 builder.setExistingTriggerPolicy(unknownPolicyId);
419 public void testSetTriggerPolicyUnknownPolicy() throws BuilderException {
420 ControlLoopPolicyBuilder builder =
421 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
422 builder.setTriggerPolicy(
423 PolicyParam.builder()
424 .id(UUID.randomUUID().toString())
426 .description(TRIGGER_RESTART)
428 .target(new Target(TargetType.VM))
432 .timeout(300).build());
433 final String unknownPolicyId = "100";
434 expectedException.expect(BuilderException.class);
435 expectedException.expectMessage(UNKNOWN_POLICY + unknownPolicyId);
436 builder.setExistingTriggerPolicy(unknownPolicyId);
440 public void testAddRemovePolicies() throws BuilderException {
441 ControlLoopPolicyBuilder builder =
442 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
443 Policy triggerPolicy =
444 builder.setTriggerPolicy(
445 PolicyParam.builder()
446 .id(UUID.randomUUID().toString())
448 .description(TRIGGER_RESTART)
450 .target(new Target(TargetType.VM))
454 .timeout(300).build());
456 // Test create a policy and chain it to the results of trigger policy
458 Policy onRestartFailurePolicy1 = builder.setPolicyForPolicyResult(
459 PolicyParam.builder()
461 .description(REBUILD_RESTART)
463 .target(new Target(TargetType.VM))
468 .id(triggerPolicy.getId()).build(),
469 PolicyResult.FAILURE,
470 PolicyResult.FAILURE_EXCEPTION,
471 PolicyResult.FAILURE_RETRIES,
472 PolicyResult.FAILURE_TIMEOUT,
473 PolicyResult.FAILURE_GUARD);
475 assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy1.getId()));
476 assertTrue(builder.getTriggerPolicy().getFailure_exception().equals(onRestartFailurePolicy1.getId()));
477 assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy1.getId()));
478 assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy1.getId()));
479 assertTrue(builder.getTriggerPolicy().getFailure_guard().equals(onRestartFailurePolicy1.getId()));
482 // Test create a policy and chain it to the results of trigger policy success
484 Policy onSuccessPolicy1 = builder.setPolicyForPolicyResult(
485 PolicyParam.builder()
486 .name("Do something")
487 .description("If the restart succeeds, do something else.")
489 .target(new Target(TargetType.VM))
490 .recipe("SomethingElse")
494 .id(triggerPolicy.getId()).build(),
495 PolicyResult.SUCCESS);
497 assertTrue(builder.getTriggerPolicy().getSuccess().equals(onSuccessPolicy1.getId()));
500 // Test remove policy
502 boolean removed = builder.removePolicy(onRestartFailurePolicy1.getId());
504 assertTrue(builder.getTriggerPolicy().getFailure().equals(FinalResult.FINAL_FAILURE.toString()));
505 assertTrue(builder.getTriggerPolicy().getFailure_retries()
506 .equals(FinalResult.FINAL_FAILURE_RETRIES.toString()));
507 assertTrue(builder.getTriggerPolicy().getFailure_timeout()
508 .equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString()));
510 builder.getTriggerPolicy().getFailure_guard().equals(FinalResult.FINAL_FAILURE_GUARD.toString()));
512 // Create another policy and chain it to the results of trigger policy
514 final Policy onRestartFailurePolicy2 =
515 builder.setPolicyForPolicyResult(
516 PolicyParam.builder()
518 .description(REBUILD_RESTART)
520 .target(new Target(TargetType.VM))
525 .id(triggerPolicy.getId()).build(),
526 PolicyResult.FAILURE,
527 PolicyResult.FAILURE_RETRIES,
528 PolicyResult.FAILURE_TIMEOUT);
530 // Test reset policy results
532 triggerPolicy = builder.resetPolicyResults(triggerPolicy.getId());
533 assertTrue(builder.getTriggerPolicy().getFailure().equals(FinalResult.FINAL_FAILURE.toString()));
534 assertTrue(builder.getTriggerPolicy().getFailure_retries()
535 .equals(FinalResult.FINAL_FAILURE_RETRIES.toString()));
536 assertTrue(builder.getTriggerPolicy().getFailure_timeout()
537 .equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString()));
539 // Test set the policy results to an existing operational policy
541 Policy onRestartFailurePolicy3 =
542 builder.setPolicyForPolicyResult(onRestartFailurePolicy2.getId(), triggerPolicy.getId(),
543 PolicyResult.FAILURE, PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT);
544 assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy3.getId()));
545 assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy3.getId()));
546 assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy3.getId()));
548 // Test set the policy result for success to an existing operational policy
550 Policy onRestartFailurePolicy4 =
551 builder.setPolicyForPolicyResult(onRestartFailurePolicy2.getId(), triggerPolicy.getId(),
552 PolicyResult.FAILURE, PolicyResult.FAILURE_EXCEPTION, PolicyResult.FAILURE_GUARD,
553 PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT, PolicyResult.SUCCESS);
554 assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy4.getId()));
555 assertTrue(builder.getTriggerPolicy().getFailure_exception().equals(onRestartFailurePolicy4.getId()));
556 assertTrue(builder.getTriggerPolicy().getFailure_guard().equals(onRestartFailurePolicy4.getId()));
557 assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy4.getId()));
558 assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy4.getId()));
559 assertTrue(builder.getTriggerPolicy().getSuccess().equals(onRestartFailurePolicy4.getId()));
562 // Test remove all existing operational policies
564 builder = builder.removeAllPolicies();
565 assertTrue(builder.getControlLoop().getTrigger_policy().equals(FinalResult.FINAL_OPENLOOP.toString()));
569 public void testAddToUnknownPolicy() throws BuilderException {
570 ControlLoopPolicyBuilder builder =
571 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
572 final String policyId = "100";
573 expectedException.expect(BuilderException.class);
574 expectedException.expectMessage(UNKNOWN_POLICY + policyId);
576 builder.setPolicyForPolicyResult(
577 PolicyParam.builder()
579 .description(REBUILD_RESTART)
581 .target(new Target(TargetType.VM))
586 .id(policyId).build(),
587 PolicyResult.FAILURE,
588 PolicyResult.FAILURE_RETRIES,
589 PolicyResult.FAILURE_TIMEOUT,
590 PolicyResult.FAILURE_GUARD);
594 public void testAddExistingPolicyToUnknownPolicy() throws BuilderException {
595 ControlLoopPolicyBuilder builder =
596 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
597 Policy triggerPolicy =
598 builder.setTriggerPolicy(
599 PolicyParam.builder()
600 .id(UUID.randomUUID().toString())
602 .description(TRIGGER_RESTART)
604 .target(new Target(TargetType.VM))
608 .timeout(300).build());
611 Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult(
612 PolicyParam.builder()
614 .description(REBUILD_RESTART)
616 .target(new Target(TargetType.VM))
621 .id(triggerPolicy.getId()).build(),
622 PolicyResult.FAILURE);
624 final String unknownPolicyId = "100";
625 expectedException.expect(BuilderException.class);
626 expectedException.expectMessage(unknownPolicyId + " does not exist");
628 builder.setPolicyForPolicyResult(onRestartFailurePolicy.getId(), unknownPolicyId, PolicyResult.FAILURE);
632 public void testAddUnknownExistingPolicyToPolicy() throws BuilderException {
633 ControlLoopPolicyBuilder builder =
634 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
635 Policy triggerPolicy =
636 builder.setTriggerPolicy(
637 PolicyParam.builder()
638 .id(UUID.randomUUID().toString())
640 .description(TRIGGER_RESTART)
642 .target(new Target(TargetType.VM))
646 .timeout(300).build());
648 final String unknownPolicyId = "100";
649 expectedException.expect(BuilderException.class);
650 expectedException.expectMessage("Operational policy " + unknownPolicyId + " does not exist");
652 builder.setPolicyForPolicyResult(unknownPolicyId, triggerPolicy.getId(), PolicyResult.FAILURE);
656 public void testAddOperationsAccumulateParams() throws BuilderException {
657 ControlLoopPolicyBuilder builder =
658 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
659 Policy triggerPolicy =
660 builder.setTriggerPolicy(
661 PolicyParam.builder()
662 .id(UUID.randomUUID().toString())
663 .name("Restart the eNodeB")
664 .description("Upon getting the trigger event, restart the eNodeB")
665 .actor("RANController")
666 .target(new Target(TargetType.PNF))
670 .timeout(300).build());
672 // Add the operationsAccumulateParams
674 triggerPolicy = builder.addOperationsAccumulateParams(triggerPolicy.getId(),
675 new OperationsAccumulateParams("15m", 5));
676 assertNotNull(builder.getTriggerPolicy().getOperationsAccumulateParams());
677 assertEquals("15m", builder.getTriggerPolicy().getOperationsAccumulateParams().getPeriod());
678 assertTrue(builder.getTriggerPolicy().getOperationsAccumulateParams().getLimit() == 5);
683 public void testBuildSpecification() throws BuilderException {
685 // Create the builder
687 ControlLoopPolicyBuilder builder =
688 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 800);
690 // Set the first invalid trigger policy
692 final Policy policy1 = builder.setTriggerPolicy(
693 PolicyParam.builder()
694 .id(UUID.randomUUID().toString())
696 .description(TRIGGER_RESTART)
702 .timeout(300).build());
703 Results results = builder.buildSpecification();
705 // Check that ERRORs are in results for invalid policy arguments
707 boolean invalidActor = false;
708 boolean invalidRecipe = false;
709 boolean invalidTarget = false;
710 for (Message m : results.getMessages()) {
711 if ("Policy actor is null".equals(m.getMessage()) && m.getLevel() == MessageLevel.ERROR) {
714 if ("Policy recipe is null".equals(m.getMessage()) && m.getLevel() == MessageLevel.ERROR) {
715 invalidRecipe = true;
717 if ("Policy target is null".equals(m.getMessage()) && m.getLevel() == MessageLevel.ERROR) {
718 invalidTarget = true;
722 assertTrue(invalidActor);
723 assertTrue(invalidRecipe);
724 assertTrue(invalidTarget);
726 // Remove the invalid policy
728 // @SuppressWarnings("unused")
729 boolean removed = builder.removePolicy(policy1.getId());
731 assertTrue(builder.getTriggerPolicy() == null);
733 // Set a valid trigger policy
735 Policy policy1a = builder.setTriggerPolicy(
736 PolicyParam.builder()
737 .id(UUID.randomUUID().toString())
739 .description(REBUILD_RESTART)
741 .target(new Target(TargetType.VM))
745 .timeout(600).build());
747 // Set a second valid trigger policy
749 final Policy policy2 =
750 builder.setTriggerPolicy(
751 PolicyParam.builder()
752 .id(UUID.randomUUID().toString())
754 .description(TRIGGER_RESTART)
756 .target(new Target(TargetType.VM))
760 .timeout(300).build());
762 // Now, we have policy1 unreachable
764 results = builder.buildSpecification();
765 boolean unreachable = false;
766 for (Message m : results.getMessages()) {
767 if (m.getMessage().equals("Policy " + policy1a.getId() + " is not reachable.")
768 && m.getLevel() == MessageLevel.WARNING) {
773 assertTrue(unreachable);
775 // Set policy1a for the failure results of policy2
777 policy1a = builder.setPolicyForPolicyResult(policy1a.getId(), policy2.getId(), PolicyResult.FAILURE,
778 PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT);
779 results = builder.buildSpecification();
780 boolean invalidTimeout = false;
781 for (Message m : results.getMessages()) {
782 if ("controlLoop overall timeout is less than the sum of operational policy timeouts."
783 .equals(m.getMessage()) && m.getLevel() == MessageLevel.ERROR) {
784 invalidTimeout = true;
788 assertTrue(invalidTimeout);
790 // Remove policy2 (revert controlLoop back to open loop)
792 removed = builder.removePolicy(policy2.getId());
794 // ControlLoop is open loop now, but it still has policies (policy1)
796 results = builder.buildSpecification();
798 for (Message m : results.getMessages()) {
799 if ("Open Loop policy contains policies. The policies will never be invoked.".equals(m.getMessage())
800 && m.getLevel() == MessageLevel.WARNING) {
805 assertTrue(unreachable);
810 public void test1() throws Exception {
811 this.test("src/test/resources/v1.0.0/policy_Test.yaml");
815 public void testEvilYaml() throws Exception {
816 try (InputStream is = new FileInputStream(new File("src/test/resources/v1.0.0/test_evil.yaml"))) {
818 // Attempt to read the yaml into our Java Object
820 Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
821 assertThatThrownBy(() -> yaml.load(is)).isInstanceOf(YAMLException.class);
826 * Does the actual test.
828 * @param testFile input file
829 * @throws Exception if an error occurs
831 public void test(String testFile) throws Exception {
832 try (InputStream is = new FileInputStream(new File(testFile))) {
834 // Read the yaml into our Java Object
836 Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
837 Object obj = yaml.load(is);
839 assertTrue(obj instanceof ControlLoopPolicy);
840 ControlLoopPolicy policyTobuild = (ControlLoopPolicy) obj;
842 // Now we're going to try to use the builder to build this.
844 ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(
845 policyTobuild.getControlLoop().getControlLoopName(), policyTobuild.getControlLoop().getTimeout());
849 if (policyTobuild.getControlLoop().getServices() != null) {
850 builder = builder.addService(policyTobuild.getControlLoop().getServices()
851 .toArray(new Service[policyTobuild.getControlLoop().getServices().size()]));
856 if (policyTobuild.getControlLoop().getResources() != null) {
857 builder = builder.addResource(policyTobuild.getControlLoop().getResources()
858 .toArray(new Resource[policyTobuild.getControlLoop().getResources().size()]));
863 if (policyTobuild.getControlLoop().getPnf() != null) {
864 builder = builder.setPnf(policyTobuild.getControlLoop().getPnf());
867 // Add the policies and be sure to set the trigger policy
869 if (policyTobuild.getPolicies() != null) {
870 setTriggerPolicies(policyTobuild, builder);
873 // Question : how to change policy ID and results by using builder ??
875 @SuppressWarnings("unused")
876 Results results = builder.buildSpecification();
881 private void setTriggerPolicies(ControlLoopPolicy policyTobuild, ControlLoopPolicyBuilder builder)
882 throws BuilderException {
883 for (Policy policy : policyTobuild.getPolicies()) {
884 if (policy.getId() == policyTobuild.getControlLoop().getTrigger_policy()) {
885 builder.setTriggerPolicy(
886 PolicyParam.builder()
887 .id(UUID.randomUUID().toString())
888 .name(policy.getName())
889 .description(policy.getDescription())
890 .actor(policy.getActor())
891 .target(policy.getTarget())
892 .recipe(policy.getRecipe())
894 .retries(policy.getRetry())
895 .timeout(policy.getTimeout()).build());