2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.policy.controlloop.eventmanager;
 
  23 import static org.junit.Assert.assertEquals;
 
  24 import static org.junit.Assert.assertFalse;
 
  25 import static org.junit.Assert.assertNotNull;
 
  26 import static org.junit.Assert.assertNull;
 
  27 import static org.junit.Assert.assertTrue;
 
  28 import static org.junit.Assert.fail;
 
  31 import java.io.FileInputStream;
 
  32 import java.io.IOException;
 
  33 import java.io.InputStream;
 
  34 import java.nio.charset.StandardCharsets;
 
  35 import java.time.Instant;
 
  36 import java.util.HashMap;
 
  37 import java.util.UUID;
 
  39 import org.apache.commons.io.IOUtils;
 
  40 import org.junit.AfterClass;
 
  41 import org.junit.BeforeClass;
 
  42 import org.junit.Test;
 
  43 import org.onap.policy.aai.util.AaiException;
 
  44 import org.onap.policy.appc.CommonHeader;
 
  45 import org.onap.policy.appc.Response;
 
  46 import org.onap.policy.appc.ResponseCode;
 
  47 import org.onap.policy.appc.ResponseStatus;
 
  48 import org.onap.policy.appclcm.LcmCommonHeader;
 
  49 import org.onap.policy.appclcm.LcmRequest;
 
  50 import org.onap.policy.appclcm.LcmRequestWrapper;
 
  51 import org.onap.policy.appclcm.LcmResponse;
 
  52 import org.onap.policy.appclcm.LcmResponseWrapper;
 
  53 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
 
  54 import org.onap.policy.controlloop.ControlLoopEventStatus;
 
  55 import org.onap.policy.controlloop.ControlLoopException;
 
  56 import org.onap.policy.controlloop.ControlLoopNotificationType;
 
  57 import org.onap.policy.controlloop.ControlLoopTargetType;
 
  58 import org.onap.policy.controlloop.Util;
 
  59 import org.onap.policy.controlloop.VirtualControlLoopEvent;
 
  60 import org.onap.policy.controlloop.VirtualControlLoopNotification;
 
  61 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
 
  62 import org.onap.policy.controlloop.policy.Policy;
 
  63 import org.onap.policy.controlloop.policy.PolicyResult;
 
  64 import org.onap.policy.controlloop.policy.Target;
 
  65 import org.onap.policy.controlloop.policy.TargetType;
 
  66 import org.onap.policy.controlloop.processor.ControlLoopProcessor;
 
  67 import org.onap.policy.drools.system.PolicyEngine;
 
  68 import org.onap.policy.so.SOResponse;
 
  69 import org.onap.policy.so.SOResponseWrapper;
 
  70 import org.onap.policy.vfc.VFCResponse;
 
  71 import org.onap.policy.vfc.VFCResponseDescriptor;
 
  72 import org.slf4j.Logger;
 
  73 import org.slf4j.LoggerFactory;
 
  75 public class ControlLoopOperationManagerTest {
 
  76     private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationManagerTest.class);
 
  79     private static VirtualControlLoopEvent onset;
 
  82         onset = new VirtualControlLoopEvent();
 
  83         onset.setRequestId(UUID.randomUUID());
 
  84         onset.setTarget("generic-vnf.vnf-name");
 
  85         onset.setTargetType(ControlLoopTargetType.VNF);
 
  86         onset.setClosedLoopAlarmStart(Instant.now());
 
  87         onset.setAai(new HashMap<>());
 
  88         onset.getAai().put("generic-vnf.vnf-name", "testTriggerSource");
 
  89         onset.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
 
  91         /* Set environment properties */
 
  92         PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
 
  93         PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI");
 
  94         PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
 
 101     public static void setUpSimulator() {
 
 103             org.onap.policy.simulators.Util.buildAaiSim();
 
 104         } catch (Exception e) {
 
 105             fail(e.getMessage());
 
 110     public static void tearDownSimulator() {
 
 111         HttpServletServer.factory.destroy();
 
 115     public void testRetriesFail() {
 
 117         // Load up the policy
 
 119         final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
 
 120         onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName());
 
 123             // Create a processor
 
 125             final ControlLoopProcessor processor = new ControlLoopProcessor(pair.value);
 
 127             // create the manager
 
 129             ControlLoopEventManager eventManager =
 
 130                     new ControlLoopEventManager(onset.getClosedLoopControlName(), onset.getRequestId());
 
 131             VirtualControlLoopNotification notification = eventManager.activate(onset);
 
 133             assertNotNull(notification);
 
 134             assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
 
 136             ControlLoopEventManager.NEW_EVENT_STATUS status = null;
 
 138                 status = eventManager.onNewEvent(onset);
 
 139             } catch (AaiException e) {
 
 140                 logger.warn(e.toString());
 
 141                 fail("A&AI Query Failed");
 
 143             assertNotNull(status);
 
 144             assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.FIRST_ONSET, status);
 
 146             ControlLoopOperationManager manager =
 
 147                     new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
 
 148             logger.debug("{}", manager);
 
 152             assertFalse(manager.isOperationComplete());
 
 153             assertFalse(manager.isOperationRunning());
 
 157             Object request = manager.startOperation(onset);
 
 158             logger.debug("{}", manager);
 
 159             assertNotNull(request);
 
 160             assertTrue(request instanceof LcmRequestWrapper);
 
 161             LcmRequestWrapper dmaapRequest = (LcmRequestWrapper) request;
 
 162             LcmRequest appcRequest = dmaapRequest.getBody();
 
 163             assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("1"));
 
 164             assertFalse(manager.isOperationComplete());
 
 165             assertTrue(manager.isOperationRunning());
 
 169             LcmResponseWrapper dmaapResponse = new LcmResponseWrapper();
 
 170             LcmResponse appcResponse = new LcmResponse(appcRequest);
 
 171             appcResponse.getStatus().setCode(100);
 
 172             appcResponse.getStatus().setMessage("ACCEPT");
 
 173             dmaapResponse.setBody(appcResponse);
 
 177             PolicyResult result = manager.onResponse(dmaapResponse);
 
 178             logger.debug("{}", manager);
 
 179             assertTrue(result == null);
 
 180             assertFalse(manager.isOperationComplete());
 
 181             assertTrue(manager.isOperationRunning());
 
 183             // Now we are going to Fail it
 
 185             appcResponse = new LcmResponse(appcRequest);
 
 186             appcResponse.getStatus().setCode(401);
 
 187             appcResponse.getStatus().setMessage("AppC failed for some reason");
 
 188             dmaapResponse.setBody(appcResponse);
 
 189             result = manager.onResponse(dmaapResponse);
 
 190             logger.debug("{}", manager);
 
 191             assertTrue(result.equals(PolicyResult.FAILURE));
 
 192             assertFalse(manager.isOperationComplete());
 
 193             assertFalse(manager.isOperationRunning());
 
 197             request = manager.startOperation(onset);
 
 198             logger.debug("{}", manager);
 
 199             assertNotNull(request);
 
 200             assertTrue(request instanceof LcmRequestWrapper);
 
 201             dmaapRequest = (LcmRequestWrapper) request;
 
 202             appcRequest = dmaapRequest.getBody();
 
 203             assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("2"));
 
 204             assertFalse(manager.isOperationComplete());
 
 205             assertTrue(manager.isOperationRunning());
 
 209             appcResponse = new LcmResponse(appcRequest);
 
 210             logger.debug("{}", manager);
 
 211             appcResponse.getStatus().setCode(100);
 
 212             appcResponse.getStatus().setMessage("ACCEPT");
 
 213             dmaapResponse.setBody(appcResponse);
 
 217             result = manager.onResponse(dmaapResponse);
 
 218             logger.debug("{}", manager);
 
 219             assertTrue(result == null);
 
 220             assertFalse(manager.isOperationComplete());
 
 221             assertTrue(manager.isOperationRunning());
 
 223             // Now we are going to Fail it
 
 225             appcResponse = new LcmResponse(appcRequest);
 
 226             appcResponse.getStatus().setCode(401);
 
 227             appcResponse.getStatus().setMessage("AppC failed for some reason");
 
 228             dmaapResponse.setBody(appcResponse);
 
 229             result = manager.onResponse(dmaapResponse);
 
 230             logger.debug("{}", manager);
 
 231             assertTrue(result.equals(PolicyResult.FAILURE));
 
 233             // Should be complete now
 
 235             assertTrue(manager.isOperationComplete());
 
 236             assertFalse(manager.isOperationRunning());
 
 237             assertNotNull(manager.getOperationResult());
 
 238             assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_RETRIES));
 
 239             assertTrue(manager.getHistory().size() == 2);
 
 240         } catch (ControlLoopException | AaiException e) {
 
 241             fail(e.getMessage());
 
 246     public void testTimeout() {
 
 248         // Load up the policy
 
 250         final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
 
 251         onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName());
 
 254             // Create a processor
 
 256             final ControlLoopProcessor processor = new ControlLoopProcessor(pair.value);
 
 258             // create the manager
 
 260             ControlLoopEventManager eventManager =
 
 261                     new ControlLoopEventManager(onset.getClosedLoopControlName(), onset.getRequestId());
 
 262             VirtualControlLoopNotification notification = eventManager.activate(onset);
 
 264             assertNotNull(notification);
 
 265             assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
 
 267             ControlLoopEventManager.NEW_EVENT_STATUS status = null;
 
 269                 status = eventManager.onNewEvent(onset);
 
 270             } catch (AaiException e) {
 
 271                 logger.warn(e.toString());
 
 272                 fail("A&AI Query Failed");
 
 274             assertNotNull(status);
 
 275             assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.FIRST_ONSET, status);
 
 277             ControlLoopOperationManager manager =
 
 278                     new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
 
 282             logger.debug("{}", manager);
 
 283             assertFalse(manager.isOperationComplete());
 
 284             assertFalse(manager.isOperationRunning());
 
 288             Object request = manager.startOperation(onset);
 
 289             logger.debug("{}", manager);
 
 290             assertNotNull(request);
 
 291             assertTrue((request) instanceof LcmRequestWrapper);
 
 292             LcmRequestWrapper dmaapRequest = (LcmRequestWrapper) request;
 
 293             LcmRequest appcRequest = dmaapRequest.getBody();
 
 294             assertTrue((appcRequest).getCommonHeader().getSubRequestId().contentEquals("1"));
 
 295             assertFalse(manager.isOperationComplete());
 
 296             assertTrue(manager.isOperationRunning());
 
 300             LcmResponseWrapper dmaapResponse = new LcmResponseWrapper();
 
 301             LcmResponse appcResponse = new LcmResponse(appcRequest);
 
 302             dmaapResponse.setBody(appcResponse);
 
 303             appcResponse.getStatus().setCode(100);
 
 304             appcResponse.getStatus().setMessage("ACCEPT");
 
 308             PolicyResult result = manager.onResponse(dmaapResponse);
 
 309             logger.debug("{}", manager);
 
 310             assertTrue(result == null);
 
 311             assertFalse(manager.isOperationComplete());
 
 312             assertTrue(manager.isOperationRunning());
 
 314             // Now we are going to simulate Timeout
 
 316             manager.setOperationHasTimedOut();
 
 317             logger.debug("{}", manager);
 
 318             assertTrue(manager.isOperationComplete());
 
 319             assertFalse(manager.isOperationRunning());
 
 320             assertTrue(manager.getHistory().size() == 1);
 
 321             assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
 
 323             // Now we are going to Fail the previous request
 
 325             appcResponse = new LcmResponse(appcRequest);
 
 326             appcResponse.getStatus().setCode(401);
 
 327             appcResponse.getStatus().setMessage("AppC failed for some reason");
 
 328             dmaapResponse.setBody(appcResponse);
 
 329             result = manager.onResponse(dmaapResponse);
 
 330             logger.debug("{}", manager);
 
 334             assertTrue(manager.isOperationComplete());
 
 335             assertFalse(manager.isOperationRunning());
 
 336             assertTrue(manager.getHistory().size() == 1);
 
 337             assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
 
 338         } catch (ControlLoopException | AaiException e) {
 
 339             fail(e.getMessage());
 
 344     public void testMethods() throws IOException, ControlLoopException, AaiException {
 
 345         InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml"));
 
 346         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
 
 348         UUID requestId = UUID.randomUUID();
 
 349         VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
 
 350         onsetEvent.setClosedLoopControlName("TwoOnsetTest");
 
 351         onsetEvent.setRequestId(requestId);
 
 352         onsetEvent.setTarget("generic-vnf.vnf-id");
 
 353         onsetEvent.setClosedLoopAlarmStart(Instant.now());
 
 354         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
 
 355         onsetEvent.setAai(new HashMap<>());
 
 356         onsetEvent.getAai().put("generic-vnf.vnf-name", "onsetOne");
 
 358         ControlLoopEventManager manager =
 
 359                 new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestId());
 
 360         VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
 
 361         assertNotNull(notification);
 
 362         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
 
 364         ControlLoopOperationManager clom = manager.processControlLoop();
 
 366         assertNull(clom.getOperationResult());
 
 368         clom.setEventManager(manager);
 
 369         assertEquals(manager, clom.getEventManager());
 
 371         assertNull(clom.getTargetEntity());
 
 373         clom.setGuardApprovalStatus("WizardOKedIt");
 
 374         assertEquals("WizardOKedIt", clom.getGuardApprovalStatus());
 
 376         assertNull(clom.getOperationResult());
 
 378         Policy policy = manager.getProcessor().getCurrentPolicy();
 
 379         clom.getTarget(policy);
 
 381         final Target savedTarget = policy.getTarget();
 
 382         policy.setTarget(null);
 
 384             clom.getTarget(policy);
 
 385             fail("test should throw an exception here");
 
 386         } catch (Exception e) {
 
 387             assertEquals("The target is null", e.getMessage());
 
 390         policy.setTarget(new Target());
 
 392             clom.getTarget(policy);
 
 393             fail("test should throw an exception here");
 
 394         } catch (Exception e) {
 
 395             assertEquals("The target type is null", e.getMessage());
 
 398         policy.setTarget(savedTarget);
 
 400         policy.getTarget().setType(TargetType.PNF);
 
 402             clom.getTarget(policy);
 
 403             fail("test should throw an exception here");
 
 404         } catch (Exception e) {
 
 405             assertEquals("PNF target is not supported", e.getMessage());
 
 408         onsetEvent.setTarget("Oz");
 
 409         onsetEvent.getAai().remove("generic-vnf.vnf-name");
 
 410         onsetEvent.getAai().remove("generic-vnf.vnf-id");
 
 411         onsetEvent.getAai().remove("vserver.vserver-name");
 
 413         policy.getTarget().setType(TargetType.VNF);
 
 415             clom.getTarget(policy);
 
 416             fail("test should throw an exception here");
 
 417         } catch (Exception e) {
 
 418             assertEquals("Target does not match target type", e.getMessage());
 
 421         onsetEvent.setTarget("vserver.vserver-name");
 
 422         onsetEvent.getAai().put("vserver.vserver-name", "OzVServer");
 
 423         assertEquals("OzVServer", clom.getTarget(policy));
 
 425         onsetEvent.getAai().remove("vserver.vserver-name");
 
 426         onsetEvent.setTarget("generic-vnf.vnf-id");
 
 427         onsetEvent.getAai().put("generic-vnf.vnf-id", "OzVNF");
 
 428         assertEquals("OzVNF", clom.getTarget(policy));
 
 430         onsetEvent.setTarget("generic-vnf.vnf-name");
 
 431         assertEquals("OzVNF", clom.getTarget(policy));
 
 433         manager.onNewEvent(onsetEvent);
 
 435         onsetEvent.getAai().remove("generic-vnf.vnf-id");
 
 436         manager.getVnfResponse();
 
 437         clom.getEventManager().getVnfResponse().setVnfId("generic-vnf.vnf-id");
 
 438         assertEquals("generic-vnf.vnf-id", clom.getTarget(policy));
 
 440         policy.getTarget().setType(TargetType.VFC);
 
 442             clom.getTarget(policy);
 
 443             fail("test should throw an exception here");
 
 444         } catch (Exception e) {
 
 445             assertEquals("The target type is not supported", e.getMessage());
 
 448         assertEquals(Integer.valueOf(20), clom.getOperationTimeout());
 
 450         assertEquals("20s", clom.getOperationTimeoutString(100));
 
 452         assertEquals(null, clom.getOperationMessage());
 
 453         assertEquals(null, clom.getOperationMessage("The Wizard Escaped"));
 
 455         clom.startOperation(onsetEvent);
 
 457         assertEquals("actor=SO,operation=Restart,target=Target [type=VFC, resourceId=null],subRequestId=1",
 
 458                 clom.getOperationMessage());
 
 460                 "actor=SO,operation=Restart,target=Target [type=VFC, resourceId=null],subRequestId=1, Guard result: "
 
 461                         + "The Wizard Escaped",
 
 462                 clom.getOperationMessage("The Wizard Escaped"));
 
 464         assertEquals("actor=SO,operation=Restart,tar", clom.getOperationHistory().substring(0, 30));
 
 466         clom.setOperationHasException("The Wizard is gone");
 
 467         clom.setOperationHasGuardDeny();
 
 471     public void testConstructor() throws IOException, ControlLoopException, AaiException {
 
 472         InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
 
 473         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
 
 475         UUID requestId = UUID.randomUUID();
 
 476         VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
 
 477         onsetEvent.setClosedLoopControlName("TwoOnsetTest");
 
 478         onsetEvent.setRequestId(requestId);
 
 479         onsetEvent.setTarget("generic-vnf.vnf-id");
 
 480         onsetEvent.setClosedLoopAlarmStart(Instant.now());
 
 481         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
 
 482         onsetEvent.setAai(new HashMap<>());
 
 483         onsetEvent.getAai().put("generic-vnf.vnf-name", "onsetOne");
 
 485         ControlLoopEventManager manager =
 
 486                 new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestId());
 
 487         VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
 
 488         assertNotNull(notification);
 
 489         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
 
 491         Policy policy = manager.getProcessor().getCurrentPolicy();
 
 492         ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
 
 495         policy.setRecipe("ModifyConfig");
 
 496         policy.getTarget().setResourceID(UUID.randomUUID().toString());
 
 498             new ControlLoopOperationManager(onsetEvent, policy, manager);
 
 499             fail("test should throw an exception here");
 
 500         } catch (Exception e) {
 
 501             assertEquals("Target vnf-id could not be found", e.getMessage());
 
 504         policy.getTarget().setResourceID("82194af1-3c2c-485a-8f44-420e22a9eaa4");
 
 505         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
 
 508         policy.setActor("SO");
 
 509         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
 
 512         policy.setActor("VFC");
 
 513         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
 
 516         policy.setActor("Dorothy");
 
 518             new ControlLoopOperationManager(onsetEvent, policy, manager);
 
 519             fail("test should throw an exception here");
 
 520         } catch (Exception e) {
 
 521             assertEquals("ControlLoopEventManager: policy has an unknown actor.", e.getMessage());
 
 526     public void testStartOperation() throws IOException, ControlLoopException, AaiException {
 
 527         InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
 
 528         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
 
 530         UUID requestId = UUID.randomUUID();
 
 531         VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
 
 532         onsetEvent.setClosedLoopControlName("TwoOnsetTest");
 
 533         onsetEvent.setRequestId(requestId);
 
 534         onsetEvent.setTarget("generic-vnf.vnf-id");
 
 535         onsetEvent.setClosedLoopAlarmStart(Instant.now());
 
 536         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
 
 537         onsetEvent.setAai(new HashMap<>());
 
 538         onsetEvent.getAai().put("generic-vnf.vnf-name", "onsetOne");
 
 540         ControlLoopEventManager manager =
 
 541                 new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestId());
 
 542         VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
 
 543         assertNotNull(notification);
 
 544         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
 
 546         Policy policy = manager.getProcessor().getCurrentPolicy();
 
 547         ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
 
 550         clom.startOperation(onsetEvent);
 
 553             clom.startOperation(onsetEvent);
 
 554             fail("test should throw an exception here");
 
 555         } catch (Exception e) {
 
 556             assertEquals("current operation is not null (an operation is already running)", e.getMessage());
 
 559         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
 
 561         final String savedRecipe = policy.getRecipe();
 
 562         policy.setRecipe("ModifyConfig");
 
 563         policy.getTarget().setResourceID(UUID.randomUUID().toString());
 
 564         clom.startOperation(onsetEvent);
 
 565         policy.setRecipe(savedRecipe);
 
 567         policy.setRetry(null);
 
 568         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
 
 570         clom.startOperation(onsetEvent);
 
 571         clom.setOperationHasTimedOut();
 
 572         assertTrue(clom.isOperationComplete());
 
 574             clom.startOperation(onsetEvent);
 
 575             fail("test should throw an exception here");
 
 576         } catch (Exception e) {
 
 577             assertEquals("current operation failed and retries are not allowed", e.getMessage());
 
 581         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
 
 583         clom.startOperation(onsetEvent);
 
 584         clom.setOperationHasTimedOut();
 
 585         assertTrue(clom.isOperationComplete());
 
 587             clom.startOperation(onsetEvent);
 
 588             fail("test should throw an exception here");
 
 589         } catch (Exception e) {
 
 590             assertEquals("current operation failed and retries are not allowed", e.getMessage());
 
 594         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
 
 596         clom.startOperation(onsetEvent);
 
 597         clom.setOperationHasTimedOut();
 
 598         clom.startOperation(onsetEvent);
 
 599         clom.setOperationHasTimedOut();
 
 600         assertTrue(clom.isOperationComplete());
 
 602             clom.startOperation(onsetEvent);
 
 603             fail("test should throw an exception here");
 
 604         } catch (Exception e) {
 
 605             assertEquals("current oepration has failed after 2 retries", e.getMessage());
 
 608         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
 
 610         policy.setActor("SO");
 
 611         clom.startOperation(onsetEvent);
 
 613         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
 
 615         policy.setActor("VFC");
 
 616         clom.startOperation(onsetEvent);
 
 618         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
 
 620         policy.setActor("Oz");
 
 622             clom.startOperation(onsetEvent);
 
 623             fail("test should throw an exception here");
 
 624         } catch (Exception e) {
 
 625             assertEquals("invalid actor Oz on policy", e.getMessage());
 
 630     public void testOnResponse() throws IOException, ControlLoopException, AaiException {
 
 631         InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
 
 632         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
 
 634         UUID requestId = UUID.randomUUID();
 
 635         VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
 
 636         onsetEvent.setClosedLoopControlName("TwoOnsetTest");
 
 637         onsetEvent.setRequestId(requestId);
 
 638         onsetEvent.setTarget("generic-vnf.vnf-id");
 
 639         onsetEvent.setClosedLoopAlarmStart(Instant.now());
 
 640         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
 
 641         onsetEvent.setAai(new HashMap<>());
 
 642         onsetEvent.getAai().put("generic-vnf.vnf-name", "onsetOne");
 
 644         ControlLoopEventManager manager =
 
 645                 new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestId());
 
 646         VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
 
 647         assertNotNull(notification);
 
 648         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
 
 650         Policy policy = manager.getProcessor().getCurrentPolicy();
 
 651         ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
 
 654         assertNull(clom.onResponse(null));
 
 656         Response appcResponse = new Response();
 
 657         CommonHeader commonHeader = new CommonHeader();
 
 658         appcResponse.setCommonHeader(commonHeader);
 
 659         assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
 
 661         commonHeader.setSubRequestId("12345");
 
 662         appcResponse.setStatus(null);
 
 663         assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
 
 665         ResponseStatus responseStatus = new ResponseStatus();
 
 666         appcResponse.setStatus(responseStatus);
 
 667         assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
 
 669         responseStatus.setCode(0);
 
 670         assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
 
 672         responseStatus.setCode(ResponseCode.ACCEPT.getValue());
 
 673         assertEquals(null, clom.onResponse(appcResponse));
 
 675         responseStatus.setCode(ResponseCode.ERROR.getValue());
 
 676         assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
 
 678         responseStatus.setCode(ResponseCode.FAILURE.getValue());
 
 679         assertEquals(PolicyResult.FAILURE, clom.onResponse(appcResponse));
 
 681         responseStatus.setCode(ResponseCode.REJECT.getValue());
 
 682         assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
 
 684         responseStatus.setCode(ResponseCode.SUCCESS.getValue());
 
 685         assertEquals(PolicyResult.SUCCESS, clom.onResponse(appcResponse));
 
 687         LcmResponseWrapper lrw = new LcmResponseWrapper();
 
 688         LcmResponse body = new LcmResponse();
 
 689         LcmCommonHeader lcmCh = new LcmCommonHeader();
 
 690         body.setCommonHeader(lcmCh);
 
 693         lcmCh.setSubRequestId("NotANumber");
 
 694         assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(lrw));
 
 696         lcmCh.setSubRequestId("12345");
 
 697         assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(lrw));
 
 699         SOResponse soResponse = new SOResponse();
 
 700         SOResponseWrapper soRw = new SOResponseWrapper(soResponse, null);
 
 702         soResponse.setHttpResponseCode(200);
 
 703         assertEquals(PolicyResult.SUCCESS, clom.onResponse(soRw));
 
 705         soResponse.setHttpResponseCode(202);
 
 706         assertEquals(PolicyResult.SUCCESS, clom.onResponse(soRw));
 
 708         soResponse.setHttpResponseCode(500);
 
 709         assertEquals(PolicyResult.FAILURE, clom.onResponse(soRw));
 
 711         VFCResponse vfcResponse = new VFCResponse();
 
 712         VFCResponseDescriptor responseDescriptor = new VFCResponseDescriptor();
 
 713         vfcResponse.setResponseDescriptor(responseDescriptor);
 
 715         responseDescriptor.setStatus("finished");
 
 716         assertEquals(PolicyResult.SUCCESS, clom.onResponse(vfcResponse));
 
 718         responseDescriptor.setStatus("unfinished");
 
 719         assertEquals(PolicyResult.FAILURE, clom.onResponse(vfcResponse));
 
 723     public void testCompleteOperation() throws ControlLoopException, AaiException, IOException {
 
 724         InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
 
 725         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
 
 727         UUID requestId = UUID.randomUUID();
 
 728         VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
 
 729         onsetEvent.setClosedLoopControlName("TwoOnsetTest");
 
 730         onsetEvent.setRequestId(requestId);
 
 731         onsetEvent.setTarget("generic-vnf.vnf-id");
 
 732         onsetEvent.setClosedLoopAlarmStart(Instant.now());
 
 733         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
 
 734         onsetEvent.setAai(new HashMap<>());
 
 735         onsetEvent.getAai().put("generic-vnf.vnf-name", "onsetOne");
 
 737         ControlLoopEventManager manager =
 
 738                 new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestId());
 
 739         VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
 
 740         assertNotNull(notification);
 
 741         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
 
 743         Policy policy = manager.getProcessor().getCurrentPolicy();
 
 744         ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
 
 747         clom.startOperation(onsetEvent);
 
 749         SOResponse soResponse = new SOResponse();
 
 750         final SOResponseWrapper soRw = new SOResponseWrapper(soResponse, null);
 
 752         PolicyEngine.manager.setEnvironmentProperty("guard.disabled", "false");
 
 753         PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.ONAP_KEY_URL,
 
 754                 "http://somewhere.over.the.rainbow");
 
 755         PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.ONAP_KEY_USER, "Dorothy");
 
 756         PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.ONAP_KEY_PASS, "Toto");
 
 758         assertEquals(PolicyResult.FAILURE, clom.onResponse(soRw));
 
 760         System.setProperty("OperationsHistoryPU", "TestOperationsHistoryPU");
 
 761         assertEquals(PolicyResult.FAILURE, clom.onResponse(soRw));