2  * ============LICENSE_START=======================================================
 
   3  * policy-yaml unit test
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017-2019 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.policy.aai.Pnf;
 
  40 import org.onap.policy.aai.PnfType;
 
  41 import org.onap.policy.controlloop.policy.builder.BuilderException;
 
  42 import org.onap.policy.controlloop.policy.builder.ControlLoopPolicyBuilder;
 
  43 import org.onap.policy.controlloop.policy.builder.Message;
 
  44 import org.onap.policy.controlloop.policy.builder.MessageLevel;
 
  45 import org.onap.policy.controlloop.policy.builder.Results;
 
  46 import org.onap.policy.sdc.Resource;
 
  47 import org.onap.policy.sdc.ResourceType;
 
  48 import org.onap.policy.sdc.Service;
 
  49 import org.yaml.snakeyaml.Yaml;
 
  50 import org.yaml.snakeyaml.constructor.Constructor;
 
  51 import org.yaml.snakeyaml.error.YAMLException;
 
  54 public class ControlLoopPolicyBuilderTest {
 
  56     private static final String RESOURCE1 = "resource1";
 
  57     private static final String TRIGGER_RESTART = "Upon getting the trigger event, restart the VM";
 
  58     private static final String UNKNOWN_POLICY = "Unknown policy ";
 
  59     private static final String RESTART = "Restart";
 
  60     private static final String RESTART_VM = "Restart the VM";
 
  61     private static final String REBUILD = "Rebuild";
 
  62     private static final String REBUILD_VM = "Rebuild VM";
 
  63     private static final String REBUILD_RESTART = "If the restart fails, rebuild it.";
 
  65     public ExpectedException expectedException = ExpectedException.none();
 
  68     public void testControlLoop() throws BuilderException {
 
  70         // Create a builder for our policy
 
  72         ControlLoopPolicyBuilder builder =
 
  73                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
  77         Service scp = new Service("vSCP");
 
  78         Service usp = new Service("vUSP");
 
  79         Service trinity = new Service("Trinity");
 
  80         builder = builder.addService(scp, usp, trinity);
 
  81         assertTrue(builder.getControlLoop().getServices().size() == 3);
 
  83         // Test remove services
 
  85         builder = builder.removeService(scp);
 
  86         assertTrue(builder.getControlLoop().getServices().size() == 2);
 
  87         builder = builder.removeAllServices();
 
  88         assertTrue(builder.getControlLoop().getServices().isEmpty());
 
  92         Resource cts = new Resource("vCTS", ResourceType.VF);
 
  93         Resource com = new Resource("vCTS", ResourceType.VF);
 
  94         Resource rar = new Resource("vCTS", ResourceType.VF);
 
  95         builder = builder.addResource(cts, com, rar);
 
  96         assertTrue(builder.getControlLoop().getResources().size() == 3);
 
  98         // Test remove resources
 
 100         builder = builder.removeResource(cts);
 
 101         assertTrue(builder.getControlLoop().getResources().size() == 2);
 
 102         builder = builder.removeAllResources();
 
 103         assertTrue(builder.getControlLoop().getResources().isEmpty());
 
 107     public void testAddNullService() throws BuilderException {
 
 108         ControlLoopPolicyBuilder builder =
 
 109                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 110         expectedException.expect(BuilderException.class);
 
 111         expectedException.expectMessage("Service must not be null");
 
 112         builder.addService((Service) null);
 
 116     public void testAddInvalidService() throws BuilderException {
 
 117         ControlLoopPolicyBuilder builder =
 
 118                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 119         expectedException.expect(BuilderException.class);
 
 120         expectedException.expectMessage("Invalid service - need either a serviceUUID or serviceName");
 
 121         builder.addService(new Service());
 
 125     public void testAddServiceWithUuid() throws BuilderException {
 
 126         ControlLoopPolicyBuilder builder =
 
 127                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 128         UUID uuid = UUID.randomUUID();
 
 129         Service serviceWithUuid = new Service(uuid);
 
 130         builder.addService(serviceWithUuid);
 
 131         assertTrue(builder.getControlLoop().getServices().size() == 1);
 
 135     public void testAddNullResource() throws BuilderException {
 
 136         ControlLoopPolicyBuilder builder =
 
 137                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 138         expectedException.expect(BuilderException.class);
 
 139         expectedException.expectMessage("Resource must not be null");
 
 140         builder.addResource((Resource) null);
 
 145     public void testAddInvalidResource() throws BuilderException {
 
 146         ControlLoopPolicyBuilder builder =
 
 147                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 148         expectedException.expect(BuilderException.class);
 
 149         expectedException.expectMessage("Invalid resource - need either resourceUUID or resourceName");
 
 150         builder.addResource(new Resource());
 
 154     public void testAddAndRemoveResourceWithUuid() throws BuilderException {
 
 155         ControlLoopPolicyBuilder builder =
 
 156                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 157         UUID uuid = UUID.randomUUID();
 
 158         Resource resourceWithUuid = new Resource(uuid);
 
 159         builder.addResource(resourceWithUuid);
 
 160         assertTrue(builder.getControlLoop().getResources().size() == 1);
 
 162         builder.removeResource(resourceWithUuid);
 
 163         assertTrue(builder.getControlLoop().getResources().isEmpty());
 
 167     public void testRemoveNullResource() throws BuilderException {
 
 168         ControlLoopPolicyBuilder builder =
 
 169                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 170         Resource resource = new Resource(RESOURCE1, ResourceType.VF);
 
 171         builder.addResource(resource);
 
 172         expectedException.expect(BuilderException.class);
 
 173         expectedException.expectMessage("Resource must not be null");
 
 174         builder.removeResource((Resource) null);
 
 178     public void testRemoveResourceNoExistingResources() throws BuilderException {
 
 179         ControlLoopPolicyBuilder builder =
 
 180                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 181         expectedException.expect(BuilderException.class);
 
 182         expectedException.expectMessage("No existing resources to remove");
 
 183         builder.removeResource(new Resource(RESOURCE1, ResourceType.VF));
 
 187     public void testRemoveInvalidResource() throws BuilderException {
 
 188         ControlLoopPolicyBuilder builder =
 
 189                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 190         Resource resource = new Resource(RESOURCE1, ResourceType.VF);
 
 191         builder.addResource(resource);
 
 192         expectedException.expect(BuilderException.class);
 
 193         expectedException.expectMessage("Invalid resource - need either a resourceUUID or resourceName");
 
 194         builder.removeResource(new Resource());
 
 198     public void testRemoveUnknownResource() throws BuilderException {
 
 199         ControlLoopPolicyBuilder builder =
 
 200                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 201         Resource resource = new Resource(RESOURCE1, ResourceType.VF);
 
 202         builder.addResource(resource);
 
 203         final String unknownResourceName = "reource2";
 
 204         expectedException.expect(BuilderException.class);
 
 205         expectedException.expectMessage("Unknown resource " + unknownResourceName);
 
 206         builder.removeResource(new Resource(unknownResourceName, ResourceType.VF));
 
 210     public void testControlLoopWithInitialResourceAndServices() throws BuilderException {
 
 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);
 
 221     public void testControlLoopWithInitialResourcesAndService() throws BuilderException {
 
 222         Resource cts = new Resource("vCTS", ResourceType.VF);
 
 223         Resource com = new Resource("vCTS", ResourceType.VF);
 
 224         Service scp = new Service("vSCP");
 
 225         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory
 
 226                 .buildControlLoop(UUID.randomUUID().toString(), 2400, scp, cts, com);
 
 227         assertTrue(builder.getControlLoop().getServices().size() == 1);
 
 228         assertTrue(builder.getControlLoop().getResources().size() == 2);
 
 233     // I'VE MARKED THIS TEST CASE AS IGNORE BECAUSE THE TEST CASE FAILS
 
 234     // This test case fails because builder.getControlLoop() returns an instance of ControlLoop
 
 236     // the ControlLoop(ControlLoop controlLoop) constructor.
 
 237     // This constructor does not copy the value of pnf into the newly created object
 
 238     // On the face of it, this looks like a bug, but perhaps there is a reason for this
 
 239     // PLEASE ADVISE IF THE BEHAVIOUR IS INCORRECT OR THE TEST CASE IS INVALID
 
 240     public void testControlLoopForPnf() throws BuilderException {
 
 242         pnf.setPnfType(PnfType.ENODEB);
 
 243         ControlLoopPolicyBuilder builder =
 
 244                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, pnf);
 
 245         assertEquals(pnf, builder.getControlLoop().getPnf());
 
 248         assertNull(builder.getControlLoop().getPnf());
 
 253     // Fails for the same reason as the above test case
 
 254     public void testSetAndRemovePnf() throws BuilderException {
 
 255         ControlLoopPolicyBuilder builder =
 
 256                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 257         assertNull(builder.getControlLoop().getPnf());
 
 260         pnf.setPnfType(PnfType.ENODEB);
 
 262         assertEquals(pnf, builder.getControlLoop().getPnf());
 
 265         assertNull(builder.getControlLoop().getPnf());
 
 269     public void testSetNullPnf() throws BuilderException {
 
 270         ControlLoopPolicyBuilder builder =
 
 271                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 272         expectedException.expect(BuilderException.class);
 
 273         expectedException.expectMessage("PNF must not be null");
 
 274         builder.setPnf(null);
 
 278     public void testSetInvalidPnf() throws BuilderException {
 
 279         ControlLoopPolicyBuilder builder =
 
 280                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 281         expectedException.expect(BuilderException.class);
 
 282         expectedException.expectMessage("Invalid PNF - need either pnfName or pnfType");
 
 283         builder.setPnf(new Pnf());
 
 287     public void testSetAbatement() throws BuilderException {
 
 288         ControlLoopPolicyBuilder builder =
 
 289                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 290         assertFalse(builder.getControlLoop().getAbatement());
 
 291         builder = builder.setAbatement(true);
 
 292         assertTrue(builder.getControlLoop().getAbatement());
 
 296     public void testSetNullAbatement() throws BuilderException {
 
 297         ControlLoopPolicyBuilder builder =
 
 298                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 299         expectedException.expect(BuilderException.class);
 
 300         expectedException.expectMessage("abatement must not be null");
 
 301         builder = builder.setAbatement(null);
 
 305     public void testTimeout() throws BuilderException {
 
 307         // Create a builder for our policy
 
 309         ControlLoopPolicyBuilder builder =
 
 310                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 314         assertTrue(builder.getControlLoop().getTimeout() == 2400);
 
 315         builder = builder.setTimeout(800);
 
 316         assertTrue(builder.getControlLoop().getTimeout() == 800);
 
 318         // Test calculateTimeout
 
 321                 builder.setTriggerPolicy(PolicyParam.builder().id(UUID.randomUUID().toString())
 
 323                         .description(TRIGGER_RESTART)
 
 325                         .target(new Target(TargetType.VM))
 
 329                         .timeout(300).build());
 
 330         @SuppressWarnings("unused")
 
 331         Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult(
 
 332                 PolicyParam.builder()
 
 334                         .description("If the restart fails, rebuild it")
 
 336                         .target(new Target(TargetType.VM))
 
 341                         .id(trigger.getId()).build(),
 
 342                         PolicyResult.FAILURE,
 
 343                         PolicyResult.FAILURE_RETRIES,
 
 344                         PolicyResult.FAILURE_TIMEOUT);
 
 345         assertTrue(builder.calculateTimeout().equals(new Integer(300 + 600)));
 
 349     public void testTriggerPolicyMethods() throws BuilderException {
 
 350         ControlLoopPolicyBuilder builder =
 
 351                         ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 355         assertTrue(builder.isOpenLoop());
 
 357         // Test set initial trigger policy
 
 359         Policy triggerPolicy1 =
 
 360                 builder.setTriggerPolicy(
 
 361                         PolicyParam.builder().id(UUID.randomUUID().toString())
 
 363                         .description(TRIGGER_RESTART)
 
 365                         .target(new Target(TargetType.VM))
 
 369                         .timeout(300).build());
 
 370         assertFalse(builder.isOpenLoop());
 
 371         assertEquals(builder.getControlLoop().getTrigger_policy(), triggerPolicy1.getId());
 
 373         // Set trigger policy to a new policy
 
 375         @SuppressWarnings("unused")
 
 376         Policy triggerPolicy2 =
 
 377                 builder.setTriggerPolicy(
 
 378                         PolicyParam.builder()
 
 379                         .id(UUID.randomUUID().toString())
 
 380                         .name("Rebuild the VM")
 
 381                         .description("Upon getting the trigger event, rebuild the VM")
 
 383                         .target(new Target(TargetType.VM))
 
 387                         .timeout(300).build());
 
 389         // Test set trigger policy to another existing policy
 
 391         @SuppressWarnings("unused")
 
 392         ControlLoop cl = builder.setExistingTriggerPolicy(triggerPolicy1.getId());
 
 393         assertTrue(builder.getControlLoop().getTrigger_policy().equals(triggerPolicy1.getId()));
 
 395         // Test get trigger policy
 
 397         assertTrue(builder.getTriggerPolicy().equals(triggerPolicy1));
 
 401     public void testSetTriggerPolicyNullPolicyId() throws BuilderException {
 
 402         ControlLoopPolicyBuilder builder =
 
 403                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 404         expectedException.expect(BuilderException.class);
 
 405         expectedException.expectMessage("Id must not be null");
 
 406         builder.setExistingTriggerPolicy(null);
 
 410     public void testSetTriggerPolicyNoPoliciesExist() throws BuilderException {
 
 411         ControlLoopPolicyBuilder builder =
 
 412                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 413         final String unknownPolicyId = "100";
 
 414         expectedException.expect(BuilderException.class);
 
 415         expectedException.expectMessage(UNKNOWN_POLICY + unknownPolicyId);
 
 416         builder.setExistingTriggerPolicy(unknownPolicyId);
 
 420     public void testSetTriggerPolicyUnknownPolicy() throws BuilderException {
 
 421         ControlLoopPolicyBuilder builder =
 
 422                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 423         builder.setTriggerPolicy(
 
 424                 PolicyParam.builder()
 
 425                 .id(UUID.randomUUID().toString())
 
 427                 .description(TRIGGER_RESTART)
 
 429                 .target(new Target(TargetType.VM))
 
 433                 .timeout(300).build());
 
 434         final String unknownPolicyId = "100";
 
 435         expectedException.expect(BuilderException.class);
 
 436         expectedException.expectMessage(UNKNOWN_POLICY + unknownPolicyId);
 
 437         builder.setExistingTriggerPolicy(unknownPolicyId);
 
 441     public void testAddRemovePolicies() throws BuilderException {
 
 442         ControlLoopPolicyBuilder builder =
 
 443                         ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 444         Policy triggerPolicy =
 
 445                 builder.setTriggerPolicy(
 
 446                         PolicyParam.builder()
 
 447                         .id(UUID.randomUUID().toString())
 
 449                         .description(TRIGGER_RESTART)
 
 451                         .target(new Target(TargetType.VM))
 
 455                         .timeout(300).build());
 
 457         // Test create a policy and chain it to the results of trigger policy
 
 459         Policy onRestartFailurePolicy1 = builder.setPolicyForPolicyResult(
 
 460                 PolicyParam.builder()
 
 462                 .description(REBUILD_RESTART)
 
 464                 .target(new Target(TargetType.VM))
 
 469                 .id(triggerPolicy.getId()).build(),
 
 470                 PolicyResult.FAILURE,
 
 471                 PolicyResult.FAILURE_EXCEPTION,
 
 472                 PolicyResult.FAILURE_RETRIES,
 
 473                 PolicyResult.FAILURE_TIMEOUT,
 
 474                 PolicyResult.FAILURE_GUARD);
 
 476         assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy1.getId()));
 
 477         assertTrue(builder.getTriggerPolicy().getFailure_exception().equals(onRestartFailurePolicy1.getId()));
 
 478         assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy1.getId()));
 
 479         assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy1.getId()));
 
 480         assertTrue(builder.getTriggerPolicy().getFailure_guard().equals(onRestartFailurePolicy1.getId()));
 
 483         // Test create a policy and chain it to the results of trigger policy success
 
 485         Policy onSuccessPolicy1 = builder.setPolicyForPolicyResult(
 
 486                 PolicyParam.builder()
 
 487                 .name("Do something")
 
 488                 .description("If the restart succeeds, do something else.")
 
 490                 .target(new Target(TargetType.VM))
 
 491                 .recipe("SomethingElse")
 
 495                 .id(triggerPolicy.getId()).build(),
 
 496                 PolicyResult.SUCCESS);
 
 498         assertTrue(builder.getTriggerPolicy().getSuccess().equals(onSuccessPolicy1.getId()));
 
 501         // Test remove policy
 
 503         boolean removed = builder.removePolicy(onRestartFailurePolicy1.getId());
 
 505         assertTrue(builder.getTriggerPolicy().getFailure().equals(FinalResult.FINAL_FAILURE.toString()));
 
 506         assertTrue(builder.getTriggerPolicy().getFailure_retries()
 
 507                 .equals(FinalResult.FINAL_FAILURE_RETRIES.toString()));
 
 508         assertTrue(builder.getTriggerPolicy().getFailure_timeout()
 
 509                 .equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString()));
 
 511                 builder.getTriggerPolicy().getFailure_guard().equals(FinalResult.FINAL_FAILURE_GUARD.toString()));
 
 513         // Create another policy and chain it to the results of trigger policy
 
 515         final Policy onRestartFailurePolicy2 =
 
 516                 builder.setPolicyForPolicyResult(
 
 517                         PolicyParam.builder()
 
 519                         .description(REBUILD_RESTART)
 
 521                         .target(new Target(TargetType.VM))
 
 526                         .id(triggerPolicy.getId()).build(),
 
 527                         PolicyResult.FAILURE,
 
 528                         PolicyResult.FAILURE_RETRIES,
 
 529                         PolicyResult.FAILURE_TIMEOUT);
 
 531         // Test reset policy results
 
 533         triggerPolicy = builder.resetPolicyResults(triggerPolicy.getId());
 
 534         assertTrue(builder.getTriggerPolicy().getFailure().equals(FinalResult.FINAL_FAILURE.toString()));
 
 535         assertTrue(builder.getTriggerPolicy().getFailure_retries()
 
 536                 .equals(FinalResult.FINAL_FAILURE_RETRIES.toString()));
 
 537         assertTrue(builder.getTriggerPolicy().getFailure_timeout()
 
 538                 .equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString()));
 
 540         // Test set the policy results to an existing operational policy
 
 542         Policy onRestartFailurePolicy3 =
 
 543                 builder.setPolicyForPolicyResult(onRestartFailurePolicy2.getId(), triggerPolicy.getId(),
 
 544                         PolicyResult.FAILURE, PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT);
 
 545         assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy3.getId()));
 
 546         assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy3.getId()));
 
 547         assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy3.getId()));
 
 549         // Test set the policy result for success to an existing operational policy
 
 551         Policy onRestartFailurePolicy4 =
 
 552                 builder.setPolicyForPolicyResult(onRestartFailurePolicy2.getId(), triggerPolicy.getId(),
 
 553                         PolicyResult.FAILURE, PolicyResult.FAILURE_EXCEPTION, PolicyResult.FAILURE_GUARD,
 
 554                         PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT, PolicyResult.SUCCESS);
 
 555         assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy4.getId()));
 
 556         assertTrue(builder.getTriggerPolicy().getFailure_exception().equals(onRestartFailurePolicy4.getId()));
 
 557         assertTrue(builder.getTriggerPolicy().getFailure_guard().equals(onRestartFailurePolicy4.getId()));
 
 558         assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy4.getId()));
 
 559         assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy4.getId()));
 
 560         assertTrue(builder.getTriggerPolicy().getSuccess().equals(onRestartFailurePolicy4.getId()));
 
 563         // Test remove all existing operational policies
 
 565         builder = builder.removeAllPolicies();
 
 566         assertTrue(builder.getControlLoop().getTrigger_policy().equals(FinalResult.FINAL_OPENLOOP.toString()));
 
 570     public void testAddToUnknownPolicy() throws BuilderException {
 
 571         ControlLoopPolicyBuilder builder =
 
 572                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 573         final String policyId = "100";
 
 574         expectedException.expect(BuilderException.class);
 
 575         expectedException.expectMessage(UNKNOWN_POLICY + policyId);
 
 577         builder.setPolicyForPolicyResult(
 
 578                 PolicyParam.builder()
 
 580                 .description(REBUILD_RESTART)
 
 582                 .target(new Target(TargetType.VM))
 
 587                 .id(policyId).build(),
 
 588                 PolicyResult.FAILURE,
 
 589                 PolicyResult.FAILURE_RETRIES,
 
 590                 PolicyResult.FAILURE_TIMEOUT,
 
 591                 PolicyResult.FAILURE_GUARD);
 
 595     public void testAddExistingPolicyToUnknownPolicy() throws BuilderException {
 
 596         ControlLoopPolicyBuilder builder =
 
 597                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 598         Policy triggerPolicy =
 
 599                 builder.setTriggerPolicy(
 
 600                         PolicyParam.builder()
 
 601                         .id(UUID.randomUUID().toString())
 
 603                         .description(TRIGGER_RESTART)
 
 605                         .target(new Target(TargetType.VM))
 
 609                         .timeout(300).build());
 
 612         Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult(
 
 613                 PolicyParam.builder()
 
 615                 .description(REBUILD_RESTART)
 
 617                 .target(new Target(TargetType.VM))
 
 622                 .id(triggerPolicy.getId()).build(),
 
 623                 PolicyResult.FAILURE);
 
 625         final String unknownPolicyId = "100";
 
 626         expectedException.expect(BuilderException.class);
 
 627         expectedException.expectMessage(unknownPolicyId + " does not exist");
 
 629         builder.setPolicyForPolicyResult(onRestartFailurePolicy.getId(), unknownPolicyId, PolicyResult.FAILURE);
 
 633     public void testAddUnknownExistingPolicyToPolicy() throws BuilderException {
 
 634         ControlLoopPolicyBuilder builder =
 
 635                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 636         Policy triggerPolicy =
 
 637                 builder.setTriggerPolicy(
 
 638                         PolicyParam.builder()
 
 639                         .id(UUID.randomUUID().toString())
 
 641                         .description(TRIGGER_RESTART)
 
 643                         .target(new Target(TargetType.VM))
 
 647                         .timeout(300).build());
 
 649         final String unknownPolicyId = "100";
 
 650         expectedException.expect(BuilderException.class);
 
 651         expectedException.expectMessage("Operational policy " + unknownPolicyId + " does not exist");
 
 653         builder.setPolicyForPolicyResult(unknownPolicyId, triggerPolicy.getId(), PolicyResult.FAILURE);
 
 657     public void testAddOperationsAccumulateParams() throws BuilderException {
 
 658         ControlLoopPolicyBuilder builder =
 
 659                         ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
 
 660         Policy triggerPolicy =
 
 661                 builder.setTriggerPolicy(
 
 662                         PolicyParam.builder()
 
 663                         .id(UUID.randomUUID().toString())
 
 664                         .name("Restart the eNodeB")
 
 665                         .description("Upon getting the trigger event, restart the eNodeB")
 
 666                         .actor("RANController")
 
 667                         .target(new Target(TargetType.PNF))
 
 671                         .timeout(300).build());
 
 673         // Add the operationsAccumulateParams
 
 675         triggerPolicy = builder.addOperationsAccumulateParams(triggerPolicy.getId(),
 
 676                 new OperationsAccumulateParams("15m", 5));
 
 677         assertNotNull(builder.getTriggerPolicy().getOperationsAccumulateParams());
 
 678         assertEquals("15m", builder.getTriggerPolicy().getOperationsAccumulateParams().getPeriod());
 
 679         assertTrue(builder.getTriggerPolicy().getOperationsAccumulateParams().getLimit() == 5);
 
 684     public void testBuildSpecification() throws BuilderException {
 
 686         // Create the builder
 
 688         ControlLoopPolicyBuilder builder =
 
 689                 ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 800);
 
 691         // Set the first invalid trigger policy
 
 693         final Policy policy1 = builder.setTriggerPolicy(
 
 694                 PolicyParam.builder()
 
 695                 .id(UUID.randomUUID().toString())
 
 697                 .description(TRIGGER_RESTART)
 
 703                 .timeout(300).build());
 
 704         Results results = builder.buildSpecification();
 
 706         // Check that ERRORs are in results for invalid policy arguments
 
 708         boolean invalidActor = false;
 
 709         boolean invalidRecipe = false;
 
 710         boolean invalidTarget = false;
 
 711         for (Message m : results.getMessages()) {
 
 712             if ("Policy actor is null".equals(m.getMessage()) && m.getLevel() == MessageLevel.ERROR) {
 
 715             if ("Policy recipe is null".equals(m.getMessage()) && m.getLevel() == MessageLevel.ERROR) {
 
 716                 invalidRecipe = true;
 
 718             if ("Policy target is null".equals(m.getMessage()) && m.getLevel() == MessageLevel.ERROR) {
 
 719                 invalidTarget = true;
 
 723         assertTrue(invalidActor);
 
 724         assertTrue(invalidRecipe);
 
 725         assertTrue(invalidTarget);
 
 727         // Remove the invalid policy
 
 729         // @SuppressWarnings("unused")
 
 730         boolean removed = builder.removePolicy(policy1.getId());
 
 732         assertTrue(builder.getTriggerPolicy() == null);
 
 734         // Set a valid trigger policy
 
 736         Policy policy1a = builder.setTriggerPolicy(
 
 737                 PolicyParam.builder()
 
 738                 .id(UUID.randomUUID().toString())
 
 740                 .description(REBUILD_RESTART)
 
 742                 .target(new Target(TargetType.VM))
 
 746                 .timeout(600).build());
 
 748         // Set a second valid trigger policy
 
 750         final Policy policy2 =
 
 751                 builder.setTriggerPolicy(
 
 752                         PolicyParam.builder()
 
 753                         .id(UUID.randomUUID().toString())
 
 755                         .description(TRIGGER_RESTART)
 
 757                         .target(new Target(TargetType.VM))
 
 761                         .timeout(300).build());
 
 763         // Now, we have policy1 unreachable
 
 765         results = builder.buildSpecification();
 
 766         boolean unreachable = false;
 
 767         for (Message m : results.getMessages()) {
 
 768             if (m.getMessage().equals("Policy " + policy1a.getId() + " is not reachable.")
 
 769                     && m.getLevel() == MessageLevel.WARNING) {
 
 774         assertTrue(unreachable);
 
 776         // Set policy1a for the failure results of policy2
 
 778         policy1a = builder.setPolicyForPolicyResult(policy1a.getId(), policy2.getId(), PolicyResult.FAILURE,
 
 779                 PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT);
 
 780         results = builder.buildSpecification();
 
 781         boolean invalidTimeout = false;
 
 782         for (Message m : results.getMessages()) {
 
 783             if ("controlLoop overall timeout is less than the sum of operational policy timeouts."
 
 784                             .equals(m.getMessage()) && m.getLevel() == MessageLevel.ERROR) {
 
 785                 invalidTimeout = true;
 
 789         assertTrue(invalidTimeout);
 
 791         // Remove policy2 (revert controlLoop back to open loop)
 
 793         removed = builder.removePolicy(policy2.getId());
 
 795         // ControlLoop is open loop now, but it still has policies (policy1)
 
 797         results = builder.buildSpecification();
 
 799         for (Message m : results.getMessages()) {
 
 800             if ("Open Loop policy contains policies. The policies will never be invoked.".equals(m.getMessage())
 
 801                     && m.getLevel() == MessageLevel.WARNING) {
 
 806         assertTrue(unreachable);
 
 811     public void test1() throws Exception {
 
 812         this.test("src/test/resources/v1.0.0/policy_Test.yaml");
 
 816     public void testEvilYaml() throws Exception {
 
 817         try (InputStream is = new FileInputStream(new File("src/test/resources/v1.0.0/test_evil.yaml"))) {
 
 819             // Attempt to read the yaml into our Java Object
 
 821             Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
 
 822             assertThatThrownBy(() -> yaml.load(is)).isInstanceOf(YAMLException.class);
 
 827      * Does the actual test.
 
 829      * @param testFile input file
 
 830      * @throws Exception if an error occurs
 
 832     public void test(String testFile) throws Exception {
 
 833         try (InputStream is = new FileInputStream(new File(testFile))) {
 
 835             // Read the yaml into our Java Object
 
 837             Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
 
 838             Object obj = yaml.load(is);
 
 840             assertTrue(obj instanceof ControlLoopPolicy);
 
 841             ControlLoopPolicy policyTobuild = (ControlLoopPolicy) obj;
 
 843             // Now we're going to try to use the builder to build this.
 
 845             ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(
 
 846                     policyTobuild.getControlLoop().getControlLoopName(), policyTobuild.getControlLoop().getTimeout());
 
 850             if (policyTobuild.getControlLoop().getServices() != null) {
 
 851                 builder = builder.addService(policyTobuild.getControlLoop().getServices()
 
 852                         .toArray(new Service[policyTobuild.getControlLoop().getServices().size()]));
 
 857             if (policyTobuild.getControlLoop().getResources() != null) {
 
 858                 builder = builder.addResource(policyTobuild.getControlLoop().getResources()
 
 859                         .toArray(new Resource[policyTobuild.getControlLoop().getResources().size()]));
 
 864             if (policyTobuild.getControlLoop().getPnf() != null) {
 
 865                 builder = builder.setPnf(policyTobuild.getControlLoop().getPnf());
 
 868             // Add the policies and be sure to set the trigger policy
 
 870             if (policyTobuild.getPolicies() != null) {
 
 871                 setTriggerPolicies(policyTobuild, builder);
 
 874             // Question : how to change policy ID and results by using builder ??
 
 876             @SuppressWarnings("unused")
 
 877             Results results = builder.buildSpecification();
 
 882     private void setTriggerPolicies(ControlLoopPolicy policyTobuild, ControlLoopPolicyBuilder builder)
 
 883                     throws BuilderException {
 
 884         for (Policy policy : policyTobuild.getPolicies()) {
 
 885             if (policy.getId() == policyTobuild.getControlLoop().getTrigger_policy()) {
 
 886                 builder.setTriggerPolicy(
 
 887                         PolicyParam.builder()
 
 888                         .id(UUID.randomUUID().toString())
 
 889                         .name(policy.getName())
 
 890                         .description(policy.getDescription())
 
 891                         .actor(policy.getActor())
 
 892                         .target(policy.getTarget())
 
 893                         .recipe(policy.getRecipe())
 
 895                         .retries(policy.getRetry())
 
 896                         .timeout(policy.getTimeout()).build());