From af861a9d8e28d49d357ead4a4acad82554510b6f Mon Sep 17 00:00:00 2001 From: mmis Date: Wed, 21 Mar 2018 15:22:10 +0000 Subject: Removed checkstyle warnings Removed checkstyle warnings in: policy/drools-applications/controlloop/common/actors policy/drools-applications/controlloop/common/eventmanager policy/drools-applications/controlloop/common/feature-controlloop-utils Issue-ID: POLICY-705 Change-Id: Iccf99b291bc62bc3ba2082ccdb4c1f9e12107896 Signed-off-by: mmis --- .../controlloop/ControlLoopExceptionTest.java | 18 +- .../policy/controlloop/ControlLoopLoggerTest.java | 32 +- .../controlloop/ControlLoopPublisherTest.java | 39 +- .../java/org/onap/policy/controlloop/Util.java | 62 +- .../eventmanager/ControlLoopEventManagerTest.java | 1915 ++++++++++---------- .../ControlLoopOperationManagerTest.java | 1353 +++++++------- .../eventmanager/OperationsHistoryDbEntryTest.java | 98 +- .../processor/ControlLoopProcessorTest.java | 254 +-- .../onap/policy/drools/DroolsPolicyEngineTest.java | 68 +- 9 files changed, 1957 insertions(+), 1882 deletions(-) (limited to 'controlloop/common/eventmanager/src/test') diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/ControlLoopExceptionTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/ControlLoopExceptionTest.java index 58860435a..f06ea34dc 100644 --- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/ControlLoopExceptionTest.java +++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/ControlLoopExceptionTest.java @@ -20,7 +20,7 @@ package org.onap.policy.controlloop; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; import java.io.IOException; @@ -28,12 +28,12 @@ import org.junit.Test; public class ControlLoopExceptionTest { - @Test - public void testControlLoopException() { - assertNotNull(new ControlLoopException()); - assertNotNull(new ControlLoopException("In Oz")); - assertNotNull(new ControlLoopException(new IOException())); - assertNotNull(new ControlLoopException("In Oz", new IOException())); - assertNotNull(new ControlLoopException("In Oz", new IOException(), false, false)); - } + @Test + public void testControlLoopException() { + assertNotNull(new ControlLoopException()); + assertNotNull(new ControlLoopException("In Oz")); + assertNotNull(new ControlLoopException(new IOException())); + assertNotNull(new ControlLoopException("In Oz", new IOException())); + assertNotNull(new ControlLoopException("In Oz", new IOException(), false, false)); + } } diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/ControlLoopLoggerTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/ControlLoopLoggerTest.java index 25208fe32..6c5264e1c 100644 --- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/ControlLoopLoggerTest.java +++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/ControlLoopLoggerTest.java @@ -25,25 +25,23 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import org.junit.Test; -import org.onap.policy.controlloop.ControlLoopException; -import org.onap.policy.controlloop.ControlLoopLogger; import org.onap.policy.controlloop.impl.ControlLoopLoggerStdOutImpl; public class ControlLoopLoggerTest { - @Test - public void testControlLoopLogger() throws ControlLoopException { - ControlLoopLogger logger = new ControlLoopLogger.Factory().buildLogger(ControlLoopLoggerStdOutImpl.class.getCanonicalName()); - assertNotNull(logger); - logger.info("a log message", "and another", " and another"); - logger.metrics("a metric", "and another", " and another"); - logger.metrics(Double.valueOf(3)); + @Test + public void testControlLoopLogger() throws ControlLoopException { + ControlLoopLogger logger = + new ControlLoopLogger.Factory().buildLogger(ControlLoopLoggerStdOutImpl.class.getCanonicalName()); + assertNotNull(logger); + logger.info("a log message", "and another", " and another"); + logger.metrics("a metric", "and another", " and another"); + logger.metrics(Double.valueOf(3)); - try { - new ControlLoopLogger.Factory().buildLogger("java.lang.String"); - fail("test should throw an exception here"); - } - catch (Exception e) { - assertEquals("Cannot load class java.lang.String as a control loop logger", e.getMessage()); - } - } + try { + new ControlLoopLogger.Factory().buildLogger("java.lang.String"); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("Cannot load class java.lang.String as a control loop logger", e.getMessage()); + } + } } diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/ControlLoopPublisherTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/ControlLoopPublisherTest.java index 3548ab841..309aeb206 100644 --- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/ControlLoopPublisherTest.java +++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/ControlLoopPublisherTest.java @@ -25,29 +25,28 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import org.junit.Test; -import org.onap.policy.controlloop.ControlLoopException; import org.onap.policy.controlloop.impl.ControlLoopPublisherJUnitImpl; public class ControlLoopPublisherTest { - @Test - public void testControlLoopPublisher() throws ControlLoopException { - ControlLoopPublisher publisher = new ControlLoopPublisher.Factory().buildLogger(ControlLoopPublisherJUnitImpl.class.getCanonicalName()); - assertNotNull(publisher); + @Test + public void testControlLoopPublisher() throws ControlLoopException { + ControlLoopPublisher publisher = + new ControlLoopPublisher.Factory().buildLogger(ControlLoopPublisherJUnitImpl.class.getCanonicalName()); + assertNotNull(publisher); - try { - publisher.publish(Double.valueOf(3)); - fail("test should throw an exception here"); - } - catch (Exception e) { - assertEquals("publish() method is not implemented on org.onap.policy.controlloop.impl.ControlLoopPublisherJUnitImpl", e.getMessage()); - } + try { + publisher.publish(Double.valueOf(3)); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("publish() method is not implemented on " + + "org.onap.policy.controlloop.impl.ControlLoopPublisherJUnitImpl", e.getMessage()); + } - try { - new ControlLoopPublisher.Factory().buildLogger("java.lang.String"); - fail("test should throw an exception here"); - } - catch (Exception e) { - assertEquals("Cannot load class java.lang.String as a control loop publisher", e.getMessage()); - } - } + try { + new ControlLoopPublisher.Factory().buildLogger("java.lang.String"); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("Cannot load class java.lang.String as a control loop publisher", e.getMessage()); + } + } } diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/Util.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/Util.java index bf44a8026..ced419a04 100644 --- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/Util.java +++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/Util.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * util * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,44 +24,46 @@ import static org.junit.Assert.fail; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import org.apache.commons.io.IOUtils; +import org.onap.policy.controlloop.policy.ControlLoopPolicy; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; -import org.onap.policy.controlloop.policy.ControlLoopPolicy; - public final class Util { - public static class Pair { - public final A a; - public final B b; - - public Pair(A a, B b) { - this.a = a; - this.b = b; - } - } - - public static Pair loadYaml(String testFile) { - try (InputStream is = new FileInputStream(new File(testFile))) { - String contents = IOUtils.toString(is, StandardCharsets.UTF_8); - // - // Read the yaml into our Java Object - // - Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class)); - Object obj = yaml.load(contents); - return new Pair((ControlLoopPolicy) obj, contents); - } catch (FileNotFoundException e) { - fail(e.getLocalizedMessage()); - } catch (IOException e) { - fail(e.getLocalizedMessage()); - } - return null; - } + public static class Pair { + public final A key; + public final B value; + + public Pair(A key, B value) { + this.key = key; + this.value = value; + } + } + + /** + * Load yaml into a Pair object. + * + * @param testFile the yaml file + * @return a Pair + */ + public static Pair loadYaml(String testFile) { + try (InputStream is = new FileInputStream(new File(testFile))) { + String contents = IOUtils.toString(is, StandardCharsets.UTF_8); + // + // Read the yaml into our Java Object + // + Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class)); + Object obj = yaml.load(contents); + return new Pair((ControlLoopPolicy) obj, contents); + } catch (IOException e) { + fail(e.getLocalizedMessage()); + } + return null; + } } diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java index 1df4a4f27..ea057c9d9 100644 --- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java +++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * unit test * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -67,936 +67,983 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ControlLoopEventManagerTest { - private static final Logger logger = LoggerFactory.getLogger(ControlLoopEventManagerTest.class); - - private static VirtualControlLoopEvent onset; - static { - onset = new VirtualControlLoopEvent(); - onset.setClosedLoopControlName("ControlLoop-vUSP"); - onset.setRequestID(UUID.randomUUID()); - onset.setTarget("VM_NAME"); - onset.setClosedLoopAlarmStart(Instant.now()); - onset.setAAI(new HashMap()); - onset.getAAI().put("cloud-region.identity-url", "foo"); - onset.getAAI().put("vserver.selflink", "bar"); - onset.getAAI().put("generic-vnf.vnf-id", "83f674e8-7555-44d7-9a39-bdc3770b0491"); - onset.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - } - - @BeforeClass - public static void setUpSimulator() { - try { - org.onap.policy.simulators.Util.buildAaiSim(); - } catch (Exception e) { - fail(e.getMessage()); - } - PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI"); - PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI"); - PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666"); - } - - @AfterClass - public static void tearDownSimulator() { - HttpServletServer.factory.destroy(); - } - - @Test - public void testAAIVnfInfo() { - final Util.Pair pair = Util.loadYaml("src/test/resources/test.yaml"); - onset.setClosedLoopControlName(pair.a.getControlLoop().getControlLoopName()); - try { - AAIGETVnfResponse response = getQueryByVnfID2(PolicyEngine.manager.getEnvironmentProperty("aai.url") + "/aai/v11/network/generic-vnfs/generic-vnf/", - PolicyEngine.manager.getEnvironmentProperty("aai.username"), - PolicyEngine.manager.getEnvironmentProperty("aai.password"), - UUID.randomUUID(), "5e49ca06-2972-4532-9ed4-6d071588d792"); - assertNotNull(response); - logger.info("testAAIVnfInfo test result is " + (response == null ? "null" : "not null")); - } catch (Exception e) { - logger.error("testAAIVnfInfo Exception: ", e); - fail(e.getMessage()); - } - } - - @Test - public void testAAIVnfInfo2() { - final Util.Pair pair = Util.loadYaml("src/test/resources/test.yaml"); - onset.setClosedLoopControlName(pair.a.getControlLoop().getControlLoopName()); - try { - AAIGETVnfResponse response = getQueryByVnfName2(PolicyEngine.manager.getEnvironmentProperty("aai.url") + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=", - PolicyEngine.manager.getEnvironmentProperty("aai.username"), - PolicyEngine.manager.getEnvironmentProperty("aai.password"), - UUID.randomUUID(), "lll_vnf_010317"); - assertNotNull(response); - logger.info("testAAIVnfInfo2 test result is " + (response == null ? "null" : "not null")); - } catch (Exception e) { - logger.error("testAAIVnfInfo2 Exception: ", e); - fail(e.getMessage()); - } - } - - @Test - public void testAAIVserver() { - final Util.Pair pair = Util.loadYaml("src/test/resources/test.yaml"); - onset.setClosedLoopControlName(pair.a.getControlLoop().getControlLoopName()); - try { - AAIGETVserverResponse response = getQueryByVserverName2(PolicyEngine.manager.getEnvironmentProperty("aai.url") + "/aai/v11/nodes/vservers?vserver-name=", - PolicyEngine.manager.getEnvironmentProperty("aai.username"), - PolicyEngine.manager.getEnvironmentProperty("aai.password"), - UUID.randomUUID(), "USMSO1SX7NJ0103UJZZ01-vjunos0"); - assertNotNull(response); - logger.info("testAAIVserver test result is " + (response == null ? "null" : "not null")); - } catch (Exception e) { - logger.error("testAAIVserver Exception: ", e); - fail(e.getMessage()); - } - } - - @Test - public void testIsClosedLoopDisabled() { - // - // Load up the policy - // - final Util.Pair pair = Util.loadYaml("src/test/resources/test.yaml"); - onset.setClosedLoopControlName(pair.a.getControlLoop().getControlLoopName()); - - try { - logger.info("testIsClosedLoopDisabled --"); - AAIGETVnfResponse response = getQueryByVnfID2(PolicyEngine.manager.getEnvironmentProperty("aai.url") + "/aai/v11/network/generic-vnfs/generic-vnf/", - PolicyEngine.manager.getEnvironmentProperty("aai.username"), - PolicyEngine.manager.getEnvironmentProperty("aai.password"), - UUID.randomUUID(), "5e49ca06-2972-4532-9ed4-6d071588d792"); - assertNotNull(response); - - // TODO: Handle this - //boolean disabled = ControlLoopEventManager.isClosedLoopDisabled(response); - //logger.info("QueryByVnfID - isClosedLoopDisabled: " + disabled); - - response = getQueryByVnfName2(PolicyEngine.manager.getEnvironmentProperty("aai.url") + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=", - PolicyEngine.manager.getEnvironmentProperty("aai.username"), - PolicyEngine.manager.getEnvironmentProperty("aai.password"), - UUID.randomUUID(), "lll_vnf_010317"); - assertNotNull(response); - // TODO: Handle this - //disabled = ControlLoopEventManager.isClosedLoopDisabled(response); - //logger.info("QueryByVnfName - isClosedLoopDisabled: " + disabled); - - AAIGETVserverResponse response2 = getQueryByVserverName2(PolicyEngine.manager.getEnvironmentProperty("aai.url") + "/aai/v11/nodes/vservers?vserver-name=", - PolicyEngine.manager.getEnvironmentProperty("aai.user"), - PolicyEngine.manager.getEnvironmentProperty("aai.password"), - UUID.randomUUID(), "USMSO1SX7NJ0103UJZZ01-vjunos0"); - assertNotNull(response2); - // TODO: Handle this - //disabled = ControlLoopEventManager.isClosedLoopDisabled(response2); - //logger.info("QueryByVserverName - isClosedLoopDisabled: " + disabled); - } catch (Exception e) { - fail(e.getMessage()); - } - } - - @Test - public void abatemetCheckEventSyntaxTest() { - VirtualControlLoopEvent event = new VirtualControlLoopEvent(); - event.setClosedLoopControlName("abatementAAI"); - event.setRequestID(UUID.randomUUID()); - event.setTarget("generic-vnf.vnf-id"); - event.setClosedLoopAlarmStart(Instant.now()); - event.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED); - ControlLoopEventManager manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); - assertNull(manager.getVnfResponse()); - assertNull(manager.getVserverResponse()); - try { - manager.checkEventSyntax(event); - } catch (ControlLoopException e) { - logger.debug("ControlLoopException in abatemetCheckEventSyntaxTest: "+e.getMessage()); - e.printStackTrace(); - fail("Exception in check event syntax"); - } - assertNull(manager.getVnfResponse()); - assertNull(manager.getVserverResponse()); - - - event.setAAI(new HashMap<>()); - event.getAAI().put("generic-vnf.vnf-name", "abatementTest"); - try { - manager.checkEventSyntax(event); - } catch (ControlLoopException e) { - logger.debug("ControlLoopException in abatemetCheckEventSyntaxTest: "+e.getMessage()); - e.printStackTrace(); - fail("Exception in check event syntax"); - } - assertNull(manager.getVnfResponse()); - assertNull(manager.getVserverResponse()); - } - - @Test - public void subsequentOnsetTest() { - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent event = new VirtualControlLoopEvent(); - event.setClosedLoopControlName("TwoOnsetTest"); - event.setRequestID(requestId); - event.setTarget("generic-vnf.vnf-id"); - event.setClosedLoopAlarmStart(Instant.now()); - event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - event.setAAI(new HashMap<>()); - event.getAAI().put("generic-vnf.vnf-name", "onsetOne"); - - ControlLoopEventManager manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); - VirtualControlLoopNotification notification = manager.activate(event); - - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); - - ControlLoopEventManager.NEW_EVENT_STATUS status = null; - try { - status = manager.onNewEvent(event); - } catch (AAIException e) { - logger.warn(e.toString()); - fail("A&AI Query Failed"); - } - assertNotNull(status); - assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.FIRST_ONSET, status); - - AAIGETVnfResponse response = manager.getVnfResponse(); - assertNotNull(response); - assertNull(manager.getVserverResponse()); - - VirtualControlLoopEvent event2 = new VirtualControlLoopEvent(); - event2.setClosedLoopControlName("TwoOnsetTest"); - event2.setRequestID(requestId); - event2.setTarget("generic-vnf.vnf-id"); - event2.setClosedLoopAlarmStart(Instant.now()); - event2.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - event2.setAAI(new HashMap<>()); - event2.getAAI().put("generic-vnf.vnf-name", "onsetTwo"); - - - try { - status = manager.onNewEvent(event2); - } catch (AAIException e) { - logger.warn(e.toString()); - fail("A&AI Query Failed"); - } - assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.SUBSEQUENT_ONSET, status); - AAIGETVnfResponse response2 = manager.getVnfResponse(); - assertNotNull(response2); - // We should not have queried AAI, so the stored response should be the same - assertEquals(response, response2); - assertNull(manager.getVserverResponse()); - } - - // Simulate a response - public static AAIGETVnfResponse getQueryByVnfID2(String urlGet, String username, String password, UUID requestID, String key) { - AAIGETVnfResponse response = new AAIGETVnfResponse(); - - response.setVnfID("83f674e8-7555-44d7-9a39-bdc3770b0491"); - response.setVnfName("lll_vnf_010317"); - response.setVnfType("Basa-122216-Service/VidVsamp12BaseVolume 1"); - response.setServiceId("a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb"); - response.setOrchestrationStatus("Created"); - response.setInMaint("false"); - response.setIsClosedLoopDisabled("false"); - response.setResourceVersion("1494001988835"); - response.setModelInvariantId("f18be3cd-d446-456e-9109-121d9b62feaa"); - - RelationshipList relationshipList = new RelationshipList(); - Relationship relationship = new Relationship(); - RelationshipData relationshipData = new RelationshipData(); - RelationshipDataItem relationshipDataItem = new RelationshipDataItem(); - - relationshipDataItem.setRelationshipKey ("customer.global-customer-id"); - relationshipDataItem.setRelationshipValue("MSO_1610_ST"); - relationshipData.getRelationshipData().add(relationshipDataItem); - - relationshipDataItem.setRelationshipKey ("service-subscription.service-type"); - relationshipDataItem.setRelationshipValue("MSO-dev-service-type"); - relationshipData.getRelationshipData().add(relationshipDataItem); - - relationshipDataItem.setRelationshipKey ("service-instance.service-instance-id"); - relationshipDataItem.setRelationshipValue("e1e9c97c-02c0-4919-9b4c-eb5d5ef68970"); - relationshipData.getRelationshipData().add(relationshipDataItem); - - RelatedToProperty relatedToProperty = new RelatedToProperty(); - RelatedToPropertyItem item = new RelatedToPropertyItem(); - item.setPropertyKey("service-instance.service-instance-name"); - item.setPropertyValue("lll_svc_010317"); - relatedToProperty.getRelatedTo().add(item); - - relationship.setRelatedTo("service-instance"); - relationship.setRelatedLink("/aai/v11/business/customers/customer/MSO_1610_ST/service-subscriptions/service-subscription/MSO-dev-service-type/service-instances/service-instance/e1e9c97c-02c0-4919-9b4c-eb5d5ef68970"); - relationship.setRelationshipData(relationshipData); - relationship.setRelatedToProperty(relatedToProperty); - - relationshipList.getRelationshipList().add(relationship); - response.setRelationshipList(relationshipList); - - return response; - } - - public static AAIGETVnfResponse getQueryByVnfName2(String urlGet, String username, String password, UUID requestID, String key) { - AAIGETVnfResponse response = new AAIGETVnfResponse(); - - response.setVnfID("83f674e8-7555-44d7-9a39-bdc3770b0491"); - response.setVnfName("lll_vnf_010317"); - response.setVnfType("Basa-122216-Service/VidVsamp12BaseVolume 1"); - response.setServiceId("a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb"); - response.setOrchestrationStatus("Created"); - response.setInMaint("false"); - response.setIsClosedLoopDisabled("false"); - response.setResourceVersion("1494001988835"); - response.setModelInvariantId("f18be3cd-d446-456e-9109-121d9b62feaa"); - - RelationshipList relationshipList = new RelationshipList(); - Relationship relationship = new Relationship(); - RelationshipData relationshipData = new RelationshipData(); - RelationshipDataItem relationshipDataItem = new RelationshipDataItem(); - - relationshipDataItem.setRelationshipKey("customer.global-customer-id"); - relationshipDataItem.setRelationshipValue("MSO_1610_ST"); - relationshipData.getRelationshipData().add(relationshipDataItem); - - relationshipDataItem.setRelationshipKey("service-subscription.service-type"); - relationshipDataItem.setRelationshipValue("MSO-dev-service-type"); - relationshipData.getRelationshipData().add(relationshipDataItem); - - relationshipDataItem.setRelationshipKey("service-instance.service-instance-id"); - relationshipDataItem.setRelationshipValue("e1e9c97c-02c0-4919-9b4c-eb5d5ef68970"); - relationshipData.getRelationshipData().add(relationshipDataItem); - - RelatedToProperty relatedToProperty = new RelatedToProperty(); - RelatedToPropertyItem item = new RelatedToPropertyItem(); - item.setPropertyKey("service-instance.service-instance-name"); - item.setPropertyValue("lll_svc_010317"); - relatedToProperty.getRelatedTo().add(item); - - relationship.setRelatedTo("service-instance"); - relationship.setRelatedLink("/aai/v11/business/customers/customer/MSO_1610_ST/service-subscriptions/service-subscription/MSO-dev-service-type/service-instances/service-instance/e1e9c97c-02c0-4919-9b4c-eb5d5ef68970"); - relationship.setRelationshipData(relationshipData); - relationship.setRelatedToProperty(relatedToProperty); - - relationshipList.getRelationshipList().add(relationship); - response.setRelationshipList(relationshipList); - - return response; - } - - public static AAIGETVserverResponse getQueryByVserverName2(String urlGet, String username, String password, UUID requestID, String key) { - AAIGETVserverResponse response = new AAIGETVserverResponse(); - - response.setVserverID("d0668d4f-c25e-4a1b-87c4-83845c01efd8"); - response.setVserverName("USMSO1SX7NJ0103UJZZ01-vjunos0"); - response.setVserverName2("vjunos0"); - response.setVserverSelflink("https://aai-ext1.test.att.com:8443/aai/v7/cloud-infrastructure/cloud-regions/cloud-region/att-aic/AAIAIC25/tenants/tenant/USMSO1SX7NJ0103UJZZ01%3A%3AuCPE-VMS/vservers/vserver/d0668d4f-c25e-4a1b-87c4-83845c01efd8"); - response.setInMaint("false"); - response.setIsClosedLoopDisabled("false"); - response.setResourceVersion("1494001931513"); - - RelationshipList relationshipList = new RelationshipList(); - Relationship relationship = new Relationship(); - RelationshipData relationshipData = new RelationshipData(); - RelationshipDataItem relationshipDataItem = new RelationshipDataItem(); - - relationshipDataItem.setRelationshipKey("customer.global-customer-id"); - relationshipDataItem.setRelationshipValue("MSO_1610_ST"); - relationshipData.getRelationshipData().add(relationshipDataItem); - - relationshipDataItem.setRelationshipKey("service-subscription.service-type"); - relationshipDataItem.setRelationshipValue("MSO-dev-service-type"); - relationshipData.getRelationshipData().add(relationshipDataItem); - - relationshipDataItem.setRelationshipKey("service-instance.service-instance-id"); - relationshipDataItem.setRelationshipValue("e1e9c97c-02c0-4919-9b4c-eb5d5ef68970"); - relationshipData.getRelationshipData().add(relationshipDataItem); - - RelatedToProperty relatedToProperty = new RelatedToProperty(); - RelatedToPropertyItem item = new RelatedToPropertyItem(); - item.setPropertyKey("service-instance.service-instance-name"); - item.setPropertyValue("lll_svc_010317"); - relatedToProperty.getRelatedTo().add(item); - - relationship.setRelatedTo("service-instance"); - relationship.setRelatedLink("/aai/v11/business/customers/customer/MSO_1610_ST/service-subscriptions/service-subscription/MSO-dev-service-type/service-instances/service-instance/e1e9c97c-02c0-4919-9b4c-eb5d5ef68970"); - relationship.setRelationshipData(relationshipData); - relationship.setRelatedToProperty(relatedToProperty); - - relationshipList.getRelationshipList().add(relationship); - response.setRelationshipList(relationshipList); - - return response; - } - - @Test - public void testMethods() { - ControlLoopEventManager clem = new ControlLoopEventManager("MyClosedLoopName", UUID.randomUUID()); - - clem.setActivated(true); - assertEquals(true, clem.isActivated()); - - clem.setControlLoopResult("SUCCESS"); - assertEquals("SUCCESS", clem.getControlLoopResult()); - - clem.setControlLoopTimedOut(); - assertEquals(true, clem.isControlLoopTimedOut()); - - clem.setNumAbatements(12345); - assertEquals(Integer.valueOf(12345), clem.getNumAbatements()); - - clem.setNumOnsets(54321); - assertEquals(Integer.valueOf(54321), clem.getNumOnsets()); - - assertNull(clem.getOnsetEvent()); - assertNull(clem.getAbatementEvent()); - assertNull(clem.getProcessor()); - - assertEquals(true, clem.isActive()); - assertEquals(false, clem.releaseLock()); - assertEquals(true, clem.isControlLoopTimedOut()); - - assertNull(clem.unlockCurrentOperation()); - } - - @Test - public void testAlreadyActivated() { - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent event = new VirtualControlLoopEvent(); - event.setClosedLoopControlName("TwoOnsetTest"); - event.setRequestID(requestId); - event.setTarget("generic-vnf.vnf-id"); - event.setClosedLoopAlarmStart(Instant.now()); - event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - event.setAAI(new HashMap<>()); - event.getAAI().put("generic-vnf.vnf-name", "onsetOne"); - - ControlLoopEventManager manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); - manager.setActivated(true); - VirtualControlLoopNotification notification = manager.activate(event); - assertEquals(ControlLoopNotificationType.REJECTED, notification.getNotification()); - } - - @Test - public void testActivationYaml() throws IOException { - InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); - String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); - - InputStream isBad = new FileInputStream(new File("src/test/resources/notutf8.yaml")); - String yamlStringBad = IOUtils.toString(isBad, StandardCharsets.UTF_8); - - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent event = new VirtualControlLoopEvent(); - event.setClosedLoopControlName("TwoOnsetTest"); - event.setRequestID(requestId); - event.setTarget("generic-vnf.vnf-id"); - event.setClosedLoopAlarmStart(Instant.now()); - event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - event.setAAI(new HashMap<>()); - event.getAAI().put("generic-vnf.vnf-name", "onsetOne"); - - ControlLoopEventManager manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); - - // Null YAML should fail - VirtualControlLoopNotification notificationNull = manager.activate(null, event); - assertNotNull(notificationNull); - assertEquals(ControlLoopNotificationType.REJECTED, notificationNull.getNotification()); - - // Empty YAML should fail - VirtualControlLoopNotification notificationEmpty = manager.activate("", event); - assertNotNull(notificationEmpty); - assertEquals(ControlLoopNotificationType.REJECTED, notificationEmpty.getNotification()); - - // Bad YAML should fail - VirtualControlLoopNotification notificationBad = manager.activate(yamlStringBad, event); - assertNotNull(notificationBad); - assertEquals(ControlLoopNotificationType.REJECTED, notificationBad.getNotification()); - - VirtualControlLoopNotification notification = manager.activate(yamlString, event); - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); - - // Another activate should fail - VirtualControlLoopNotification notificationActive = manager.activate(yamlString, event); - assertNotNull(notificationActive); - assertEquals(ControlLoopNotificationType.REJECTED, notificationActive.getNotification()); - } - - @Test - public void testControlLoopFinal() throws ControlLoopException, IOException { - InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); - String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); - - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent event = new VirtualControlLoopEvent(); - event.setClosedLoopControlName("TwoOnsetTest"); - event.setRequestID(requestId); - event.setTarget("generic-vnf.vnf-id"); - event.setClosedLoopAlarmStart(Instant.now()); - event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - event.setAAI(new HashMap<>()); - event.getAAI().put("generic-vnf.vnf-name", "onsetOne"); - - ControlLoopEventManager manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); - try { - manager.isControlLoopFinal(); - fail("test should throw an exception here"); - } catch (ControlLoopException e) { - assertEquals("ControlLoopEventManager MUST be activated first.", e.getMessage()); - } - - manager.setActivated(true); - try { - manager.isControlLoopFinal(); - fail("test should throw an exception here"); - } catch (ControlLoopException e) { - assertEquals("No onset event for ControlLoopEventManager.", e.getMessage()); - } - - manager.setActivated(false); - VirtualControlLoopNotification notification = manager.activate(yamlString, event); - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); - - VirtualControlLoopNotification clfNotification = manager.isControlLoopFinal(); - assertNull(clfNotification); - - manager.getProcessor().nextPolicyForResult(PolicyResult.SUCCESS); - clfNotification = manager.isControlLoopFinal(); - assertNotNull(clfNotification); - assertEquals(ControlLoopNotificationType.FINAL_SUCCESS, clfNotification.getNotification()); - - manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); - notification = manager.activate(yamlString, event); - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); - - manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_EXCEPTION); - clfNotification = manager.isControlLoopFinal(); - assertNotNull(clfNotification); - assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification()); - - manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); - notification = manager.activate(yamlString, event); - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); - - manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_GUARD); - clfNotification = manager.isControlLoopFinal(); - assertNotNull(clfNotification); - assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification()); - - manager.setControlLoopTimedOut(); - clfNotification = manager.isControlLoopFinal(); - assertNotNull(clfNotification); - assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification()); - } - - @Test - public void testProcessControlLoop() throws ControlLoopException, IOException, AAIException { - InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); - String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); - - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent event = new VirtualControlLoopEvent(); - event.setClosedLoopControlName("TwoOnsetTest"); - event.setRequestID(requestId); - event.setTarget("generic-vnf.vnf-id"); - event.setClosedLoopAlarmStart(Instant.now()); - event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - event.setAAI(new HashMap<>()); - event.getAAI().put("generic-vnf.vnf-name", "onsetOne"); - - ControlLoopEventManager manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); - try { - manager.processControlLoop(); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("ControlLoopEventManager MUST be activated first.", e.getMessage()); - } - - manager.setActivated(true); - try { - manager.processControlLoop(); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("No onset event for ControlLoopEventManager.", e.getMessage()); - } - - manager.setActivated(false); - VirtualControlLoopNotification notification = manager.activate(yamlString, event); - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); - - ControlLoopOperationManager clom = manager.processControlLoop(); - assertNotNull(clom); - assertNull(clom.getOperationResult()); - - // Test operation in progress - try { - manager.processControlLoop(); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("Already working an Operation, do not call this method.", e.getMessage()); - } - - manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); - notification = manager.activate(yamlString, event); - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); - - manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_GUARD); - VirtualControlLoopNotification clfNotification = manager.isControlLoopFinal(); - assertNotNull(clfNotification); - assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification()); - - // Test operation completed - try { - manager.processControlLoop(); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("Control Loop is in FINAL state, do not call this method.", e.getMessage()); - } - - manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); - notification = manager.activate(yamlString, event); - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); - manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE); - - // Test operation with no next policy defined - try { - manager.processControlLoop(); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("The target type is null", e.getMessage()); - } - } - - @Test - public void testFinishOperation() throws IOException, ControlLoopException, AAIException { - InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml")); - String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); - - InputStream isStd = new FileInputStream(new File("src/test/resources/test.yaml")); - String yamlStringStd = IOUtils.toString(isStd, StandardCharsets.UTF_8); - - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent event = new VirtualControlLoopEvent(); - event.setClosedLoopControlName("TwoOnsetTest"); - event.setRequestID(requestId); - event.setTarget("generic-vnf.vnf-id"); - event.setClosedLoopAlarmStart(Instant.now()); - event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - event.setAAI(new HashMap<>()); - event.getAAI().put("generic-vnf.vnf-name", "onsetOne"); - - ControlLoopEventManager manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); - try { - manager.finishOperation(null); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("No operation to finish.", e.getMessage()); - } - - manager.setActivated(true); - try { - manager.finishOperation(null); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("No operation to finish.", e.getMessage()); - } - - manager.setActivated(false); - VirtualControlLoopNotification notification = manager.activate(yamlString, event); - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); - - try { - manager.lockCurrentOperation(); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("Do not have a current operation.", e.getMessage()); - } - - assertNull(manager.unlockCurrentOperation()); - - ControlLoopOperationManager clom = manager.processControlLoop(); - assertNotNull(clom); - assertNull(clom.getOperationResult()); - - LockResult lockLock = manager.lockCurrentOperation(); - assertNotNull(lockLock); - - LockResult lockLockAgain = manager.lockCurrentOperation(); - assertNotNull(lockLockAgain); - assertEquals(lockLock.getB(), lockLockAgain.getB()); - - assertEquals(lockLock.getB(), manager.unlockCurrentOperation()); - assertNull(manager.unlockCurrentOperation()); - - lockLock = manager.lockCurrentOperation(); - assertNotNull(lockLock); - PolicyGuard.unlockTarget(lockLock.getB()); - assertNull(manager.unlockCurrentOperation()); - - clom.startOperation(event); - - // This call should be exception free - manager.finishOperation(clom); - - ControlLoopEventManager otherManager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); - VirtualControlLoopNotification otherNotification = otherManager.activate(yamlStringStd, event); - assertNotNull(otherNotification); - assertEquals(ControlLoopNotificationType.ACTIVE, otherNotification.getNotification()); - - ControlLoopOperationManager otherClom = otherManager.processControlLoop(); - assertNotNull(otherClom); - assertNull(otherClom.getOperationResult()); - - otherManager.finishOperation(clom); - } - - @Test - public void testOnNewEvent() throws IOException, AAIException { - InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); - String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); - - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent(); - onsetEvent.setClosedLoopControlName("TwoOnsetTest"); - onsetEvent.setRequestID(requestId); - onsetEvent.setTarget("generic-vnf.vnf-id"); - onsetEvent.setClosedLoopAlarmStart(Instant.now()); - onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - onsetEvent.setAAI(new HashMap<>()); - onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); - - VirtualControlLoopEvent abatedEvent = new VirtualControlLoopEvent(); - abatedEvent.setClosedLoopControlName("TwoOnsetTest"); - abatedEvent.setRequestID(requestId); - abatedEvent.setTarget("generic-vnf.vnf-id"); - abatedEvent.setClosedLoopAlarmStart(Instant.now()); - abatedEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED); - abatedEvent.setAAI(new HashMap<>()); - abatedEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); - - ControlLoopEventManager manager = new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID()); - VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent); - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); - - assertEquals(NEW_EVENT_STATUS.FIRST_ONSET, manager.onNewEvent(onsetEvent)); - assertEquals(NEW_EVENT_STATUS.FIRST_ABATEMENT, manager.onNewEvent(abatedEvent)); - assertEquals(NEW_EVENT_STATUS.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent)); - - VirtualControlLoopEvent checkSyntaxEvent = new VirtualControlLoopEvent(); - checkSyntaxEvent.setAAI(null); - checkSyntaxEvent.setClosedLoopAlarmEnd(null); - checkSyntaxEvent.setClosedLoopAlarmStart(null); - checkSyntaxEvent.setClosedLoopControlName(null); - checkSyntaxEvent.setClosedLoopEventClient(null); - checkSyntaxEvent.setClosedLoopEventStatus(null); - checkSyntaxEvent.setFrom(null); - checkSyntaxEvent.setPolicyName(null); - checkSyntaxEvent.setPolicyScope(null); - checkSyntaxEvent.setPolicyVersion(null); - checkSyntaxEvent.setRequestID(null); - checkSyntaxEvent.setTarget(null); - checkSyntaxEvent.setTargetType(null); - checkSyntaxEvent.setVersion(null); - - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.setClosedLoopControlName(null); - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.setClosedLoopControlName(""); - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.setClosedLoopControlName("TwoOnsetTest"); - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.setRequestID(null); - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.setRequestID(requestId); - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.setAAI(null); - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.setAAI(new HashMap<>()); - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.setTarget(""); - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.setTarget(null); - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.setTarget(""); - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.setTarget("OZ"); - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.setTarget("VM_NAME"); - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.setTarget("VNF_NAME"); - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.setTarget("vserver.vserver-name"); - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.setTarget("generic-vnf.vnf-id"); - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.setTarget("generic-vnf.vnf-name"); - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.setAAI(null); - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.setAAI(new HashMap<>()); - assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); - - checkSyntaxEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); - assertEquals(NEW_EVENT_STATUS.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent)); - - checkSyntaxEvent.getAAI().put("vserver.vserver-name", "onsetOne"); - assertEquals(NEW_EVENT_STATUS.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent)); - - checkSyntaxEvent.getAAI().put("generic-vnf.vnf-id", "onsetOne"); - assertEquals(NEW_EVENT_STATUS.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent)); - } - - @Test - public void testControlLoopTimeout() throws IOException { - InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); - String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); - - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent(); - onsetEvent.setClosedLoopControlName("TwoOnsetTest"); - onsetEvent.setRequestID(requestId); - onsetEvent.setTarget("generic-vnf.vnf-id"); - onsetEvent.setClosedLoopAlarmStart(Instant.now()); - onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - onsetEvent.setAAI(new HashMap<>()); - onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); - - ControlLoopEventManager manager = new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID()); - assertTrue(0 == manager.getControlLoopTimeout(null)); - assertTrue(120 == manager.getControlLoopTimeout(120)); - - VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent); - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); - - assertTrue(60 == manager.getControlLoopTimeout(null)); - } - - @Test - public void testQueryAAI() throws IOException, AAIException { - InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); - String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); - - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent(); - onsetEvent.setClosedLoopControlName("TwoOnsetTest"); - onsetEvent.setRequestID(requestId); - onsetEvent.setTarget("generic-vnf.vnf-id"); - onsetEvent.setClosedLoopAlarmStart(Instant.now()); - onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - onsetEvent.setAAI(new HashMap<>()); - onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); - - ControlLoopEventManager manager = new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID()); - manager.queryAai(onsetEvent); - - VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent); - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); - - manager.queryAai(onsetEvent); - - onsetEvent.getAAI().put("generic-vnf.is-closed-loop-disabled", "true"); - try { - manager.queryAai(onsetEvent); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("is-closed-loop-disabled is set to true on VServer or VNF", e.getMessage()); - } - onsetEvent.getAAI().put("vserver.is-closed-loop-disabled", "true"); - try { - manager.queryAai(onsetEvent); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("is-closed-loop-disabled is set to true on VServer or VNF", e.getMessage()); - } - onsetEvent.getAAI().remove("generic-vnf.is-closed-loop-disabled"); - try { - manager.queryAai(onsetEvent); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("is-closed-loop-disabled is set to true on VServer or VNF", e.getMessage()); - } - onsetEvent.getAAI().remove("vserver.is-closed-loop-disabled"); - manager.queryAai(onsetEvent); - - onsetEvent.getAAI().put("generic-vnf.is-closed-loop-disabled", "false"); - manager.queryAai(onsetEvent); - onsetEvent.getAAI().put("vserver.is-closed-loop-disabled", "false"); - manager.queryAai(onsetEvent); - - onsetEvent.getAAI().remove("generic-vnf.vnf-id"); - onsetEvent.getAAI().remove("generic-vnf.vnf-name"); - onsetEvent.getAAI().remove("vserver.vserver-name"); - manager.queryAai(onsetEvent); - - onsetEvent.getAAI().put("vserver.vserver-name", "AVserver"); - manager.queryAai(onsetEvent); - - onsetEvent.getAAI().put("generic-vnf.vnf-name", "AVNFName"); - manager.queryAai(onsetEvent); - - onsetEvent.getAAI().put("generic-vnf.vnf-id", "AVNFID"); - manager.queryAai(onsetEvent); - - onsetEvent.getAAI().remove("vserver.vserver-name"); - manager.queryAai(onsetEvent); - - onsetEvent.getAAI().remove("generic-vnf.vnf-name"); - manager.queryAai(onsetEvent); - - // Force AAI errors - PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:9999"); - - try { - manager.queryAai(onsetEvent); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("Exception from queryAai: org.onap.policy.aai.util.AAIException: AAI Response is null (query by vnf-id)", e.getMessage()); - } - - onsetEvent.getAAI().remove("generic-vnf.vnf-id"); - onsetEvent.getAAI().put("generic-vnf.vnf-name", "AVNFName"); - try { - manager.queryAai(onsetEvent); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("Exception from queryAai: org.onap.policy.aai.util.AAIException: AAI Response is null (query by vnf-name)", e.getMessage()); - } - - onsetEvent.getAAI().remove("generic-vnf.vnf-name"); - onsetEvent.getAAI().put("vserver.vserver-name", "AVserver"); - try { - manager.queryAai(onsetEvent); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("Exception from queryAai: org.onap.policy.aai.util.AAIException: AAI Response is null (query by vserver-name)", e.getMessage()); - } - - PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666"); - } -} \ No newline at end of file + private static final Logger logger = LoggerFactory.getLogger(ControlLoopEventManagerTest.class); + + private static VirtualControlLoopEvent onset; + + { + onset = new VirtualControlLoopEvent(); + onset.setClosedLoopControlName("ControlLoop-vUSP"); + onset.setRequestID(UUID.randomUUID()); + onset.setTarget("VM_NAME"); + onset.setClosedLoopAlarmStart(Instant.now()); + onset.setAAI(new HashMap()); + onset.getAAI().put("cloud-region.identity-url", "foo"); + onset.getAAI().put("vserver.selflink", "bar"); + onset.getAAI().put("generic-vnf.vnf-id", "83f674e8-7555-44d7-9a39-bdc3770b0491"); + onset.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + } + + /** + * Set up test class. + */ + @BeforeClass + public static void setUpSimulator() { + try { + org.onap.policy.simulators.Util.buildAaiSim(); + } catch (Exception e) { + fail(e.getMessage()); + } + PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI"); + PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI"); + PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666"); + } + + @AfterClass + public static void tearDownSimulator() { + HttpServletServer.factory.destroy(); + } + + @Test + public void testAaiVnfInfo() { + final Util.Pair pair = Util.loadYaml("src/test/resources/test.yaml"); + onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName()); + try { + AAIGETVnfResponse response = getQueryByVnfId2( + PolicyEngine.manager.getEnvironmentProperty("aai.url") + + "/aai/v11/network/generic-vnfs/generic-vnf/", + PolicyEngine.manager.getEnvironmentProperty("aai.username"), + PolicyEngine.manager.getEnvironmentProperty("aai.password"), UUID.randomUUID(), + "5e49ca06-2972-4532-9ed4-6d071588d792"); + assertNotNull(response); + logger.info("testAAIVnfInfo test result is " + (response == null ? "null" : "not null")); + } catch (Exception e) { + logger.error("testAAIVnfInfo Exception: ", e); + fail(e.getMessage()); + } + } + + @Test + public void testAaiVnfInfo2() { + final Util.Pair pair = Util.loadYaml("src/test/resources/test.yaml"); + onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName()); + try { + AAIGETVnfResponse response = getQueryByVnfName2( + PolicyEngine.manager.getEnvironmentProperty("aai.url") + + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=", + PolicyEngine.manager.getEnvironmentProperty("aai.username"), + PolicyEngine.manager.getEnvironmentProperty("aai.password"), UUID.randomUUID(), "lll_vnf_010317"); + assertNotNull(response); + logger.info("testAAIVnfInfo2 test result is " + (response == null ? "null" : "not null")); + } catch (Exception e) { + logger.error("testAAIVnfInfo2 Exception: ", e); + fail(e.getMessage()); + } + } + + @Test + public void testAaiVserver() { + final Util.Pair pair = Util.loadYaml("src/test/resources/test.yaml"); + onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName()); + try { + AAIGETVserverResponse response = getQueryByVserverName2( + PolicyEngine.manager.getEnvironmentProperty("aai.url") + "/aai/v11/nodes/vservers?vserver-name=", + PolicyEngine.manager.getEnvironmentProperty("aai.username"), + PolicyEngine.manager.getEnvironmentProperty("aai.password"), UUID.randomUUID(), + "USMSO1SX7NJ0103UJZZ01-vjunos0"); + assertNotNull(response); + logger.info("testAAIVserver test result is " + (response == null ? "null" : "not null")); + } catch (Exception e) { + logger.error("testAAIVserver Exception: ", e); + fail(e.getMessage()); + } + } + + @Test + public void testIsClosedLoopDisabled() { + // + // Load up the policy + // + final Util.Pair pair = Util.loadYaml("src/test/resources/test.yaml"); + onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName()); + + try { + logger.info("testIsClosedLoopDisabled --"); + AAIGETVnfResponse response = getQueryByVnfId2( + PolicyEngine.manager.getEnvironmentProperty("aai.url") + + "/aai/v11/network/generic-vnfs/generic-vnf/", + PolicyEngine.manager.getEnvironmentProperty("aai.username"), + PolicyEngine.manager.getEnvironmentProperty("aai.password"), UUID.randomUUID(), + "5e49ca06-2972-4532-9ed4-6d071588d792"); + assertNotNull(response); + + // TODO: Handle this + // boolean disabled = ControlLoopEventManager.isClosedLoopDisabled(response); + // logger.info("QueryByVnfID - isClosedLoopDisabled: " + disabled); + + response = getQueryByVnfName2( + PolicyEngine.manager.getEnvironmentProperty("aai.url") + + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=", + PolicyEngine.manager.getEnvironmentProperty("aai.username"), + PolicyEngine.manager.getEnvironmentProperty("aai.password"), UUID.randomUUID(), "lll_vnf_010317"); + assertNotNull(response); + // TODO: Handle this + // disabled = ControlLoopEventManager.isClosedLoopDisabled(response); + // logger.info("QueryByVnfName - isClosedLoopDisabled: " + disabled); + + AAIGETVserverResponse response2 = getQueryByVserverName2( + PolicyEngine.manager.getEnvironmentProperty("aai.url") + "/aai/v11/nodes/vservers?vserver-name=", + PolicyEngine.manager.getEnvironmentProperty("aai.user"), + PolicyEngine.manager.getEnvironmentProperty("aai.password"), UUID.randomUUID(), + "USMSO1SX7NJ0103UJZZ01-vjunos0"); + assertNotNull(response2); + // TODO: Handle this + // disabled = ControlLoopEventManager.isClosedLoopDisabled(response2); + // logger.info("QueryByVserverName - isClosedLoopDisabled: " + disabled); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @Test + public void abatemetCheckEventSyntaxTest() { + VirtualControlLoopEvent event = new VirtualControlLoopEvent(); + event.setClosedLoopControlName("abatementAAI"); + event.setRequestID(UUID.randomUUID()); + event.setTarget("generic-vnf.vnf-id"); + event.setClosedLoopAlarmStart(Instant.now()); + event.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED); + ControlLoopEventManager manager = + new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); + assertNull(manager.getVnfResponse()); + assertNull(manager.getVserverResponse()); + try { + manager.checkEventSyntax(event); + } catch (ControlLoopException e) { + logger.debug("ControlLoopException in abatemetCheckEventSyntaxTest: " + e.getMessage()); + e.printStackTrace(); + fail("Exception in check event syntax"); + } + assertNull(manager.getVnfResponse()); + assertNull(manager.getVserverResponse()); + + + event.setAAI(new HashMap<>()); + event.getAAI().put("generic-vnf.vnf-name", "abatementTest"); + try { + manager.checkEventSyntax(event); + } catch (ControlLoopException e) { + logger.debug("ControlLoopException in abatemetCheckEventSyntaxTest: " + e.getMessage()); + e.printStackTrace(); + fail("Exception in check event syntax"); + } + assertNull(manager.getVnfResponse()); + assertNull(manager.getVserverResponse()); + } + + @Test + public void subsequentOnsetTest() { + UUID requestId = UUID.randomUUID(); + VirtualControlLoopEvent event = new VirtualControlLoopEvent(); + event.setClosedLoopControlName("TwoOnsetTest"); + event.setRequestID(requestId); + event.setTarget("generic-vnf.vnf-id"); + event.setClosedLoopAlarmStart(Instant.now()); + event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + event.setAAI(new HashMap<>()); + event.getAAI().put("generic-vnf.vnf-name", "onsetOne"); + + ControlLoopEventManager manager = + new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); + VirtualControlLoopNotification notification = manager.activate(event); + + assertNotNull(notification); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + + ControlLoopEventManager.NEW_EVENT_STATUS status = null; + try { + status = manager.onNewEvent(event); + } catch (AAIException e) { + logger.warn(e.toString()); + fail("A&AI Query Failed"); + } + assertNotNull(status); + assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.FIRST_ONSET, status); + + AAIGETVnfResponse response = manager.getVnfResponse(); + assertNotNull(response); + assertNull(manager.getVserverResponse()); + + VirtualControlLoopEvent event2 = new VirtualControlLoopEvent(); + event2.setClosedLoopControlName("TwoOnsetTest"); + event2.setRequestID(requestId); + event2.setTarget("generic-vnf.vnf-id"); + event2.setClosedLoopAlarmStart(Instant.now()); + event2.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + event2.setAAI(new HashMap<>()); + event2.getAAI().put("generic-vnf.vnf-name", "onsetTwo"); + + + try { + status = manager.onNewEvent(event2); + } catch (AAIException e) { + logger.warn(e.toString()); + fail("A&AI Query Failed"); + } + assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.SUBSEQUENT_ONSET, status); + AAIGETVnfResponse response2 = manager.getVnfResponse(); + assertNotNull(response2); + // We should not have queried AAI, so the stored response should be the same + assertEquals(response, response2); + assertNull(manager.getVserverResponse()); + } + + /** + * Simulate a response. + */ + public static AAIGETVnfResponse getQueryByVnfId2(String urlGet, String username, String password, UUID requestId, + String key) { + AAIGETVnfResponse response = new AAIGETVnfResponse(); + + response.setVnfID("83f674e8-7555-44d7-9a39-bdc3770b0491"); + response.setVnfName("lll_vnf_010317"); + response.setVnfType("Basa-122216-Service/VidVsamp12BaseVolume 1"); + response.setServiceId("a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb"); + response.setOrchestrationStatus("Created"); + response.setInMaint("false"); + response.setIsClosedLoopDisabled("false"); + response.setResourceVersion("1494001988835"); + response.setModelInvariantId("f18be3cd-d446-456e-9109-121d9b62feaa"); + + final RelationshipList relationshipList = new RelationshipList(); + final Relationship relationship = new Relationship(); + RelationshipData relationshipData = new RelationshipData(); + RelationshipDataItem relationshipDataItem = new RelationshipDataItem(); + + relationshipDataItem.setRelationshipKey("customer.global-customer-id"); + relationshipDataItem.setRelationshipValue("MSO_1610_ST"); + relationshipData.getRelationshipData().add(relationshipDataItem); + + relationshipDataItem.setRelationshipKey("service-subscription.service-type"); + relationshipDataItem.setRelationshipValue("MSO-dev-service-type"); + relationshipData.getRelationshipData().add(relationshipDataItem); + + relationshipDataItem.setRelationshipKey("service-instance.service-instance-id"); + relationshipDataItem.setRelationshipValue("e1e9c97c-02c0-4919-9b4c-eb5d5ef68970"); + relationshipData.getRelationshipData().add(relationshipDataItem); + + RelatedToProperty relatedToProperty = new RelatedToProperty(); + RelatedToPropertyItem item = new RelatedToPropertyItem(); + item.setPropertyKey("service-instance.service-instance-name"); + item.setPropertyValue("lll_svc_010317"); + relatedToProperty.getRelatedTo().add(item); + + relationship.setRelatedTo("service-instance"); + relationship.setRelatedLink( + "/aai/v11/business/customers/customer/MSO_1610_ST/service-subscriptions/service-subscription" + + "/MSO-dev-service-type/service-instances/service-instance/" + + "e1e9c97c-02c0-4919-9b4c-eb5d5ef68970"); + relationship.setRelationshipData(relationshipData); + relationship.setRelatedToProperty(relatedToProperty); + + relationshipList.getRelationshipList().add(relationship); + response.setRelationshipList(relationshipList); + + return response; + } + + /** + * Simulate a response. + */ + public static AAIGETVnfResponse getQueryByVnfName2(String urlGet, String username, String password, UUID requestId, + String key) { + AAIGETVnfResponse response = new AAIGETVnfResponse(); + + response.setVnfID("83f674e8-7555-44d7-9a39-bdc3770b0491"); + response.setVnfName("lll_vnf_010317"); + response.setVnfType("Basa-122216-Service/VidVsamp12BaseVolume 1"); + response.setServiceId("a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb"); + response.setOrchestrationStatus("Created"); + response.setInMaint("false"); + response.setIsClosedLoopDisabled("false"); + response.setResourceVersion("1494001988835"); + response.setModelInvariantId("f18be3cd-d446-456e-9109-121d9b62feaa"); + + final RelationshipList relationshipList = new RelationshipList(); + final Relationship relationship = new Relationship(); + RelationshipData relationshipData = new RelationshipData(); + RelationshipDataItem relationshipDataItem = new RelationshipDataItem(); + + relationshipDataItem.setRelationshipKey("customer.global-customer-id"); + relationshipDataItem.setRelationshipValue("MSO_1610_ST"); + relationshipData.getRelationshipData().add(relationshipDataItem); + + relationshipDataItem.setRelationshipKey("service-subscription.service-type"); + relationshipDataItem.setRelationshipValue("MSO-dev-service-type"); + relationshipData.getRelationshipData().add(relationshipDataItem); + + relationshipDataItem.setRelationshipKey("service-instance.service-instance-id"); + relationshipDataItem.setRelationshipValue("e1e9c97c-02c0-4919-9b4c-eb5d5ef68970"); + relationshipData.getRelationshipData().add(relationshipDataItem); + + RelatedToProperty relatedToProperty = new RelatedToProperty(); + RelatedToPropertyItem item = new RelatedToPropertyItem(); + item.setPropertyKey("service-instance.service-instance-name"); + item.setPropertyValue("lll_svc_010317"); + relatedToProperty.getRelatedTo().add(item); + + relationship.setRelatedTo("service-instance"); + relationship.setRelatedLink( + "/aai/v11/business/customers/customer/MSO_1610_ST/service-subscriptions/service-subscription" + + "/MSO-dev-service-type/service-instances/service-instance/" + + "e1e9c97c-02c0-4919-9b4c-eb5d5ef68970"); + relationship.setRelationshipData(relationshipData); + relationship.setRelatedToProperty(relatedToProperty); + + relationshipList.getRelationshipList().add(relationship); + response.setRelationshipList(relationshipList); + + return response; + } + + /** + * Simulate a response. + */ + public static AAIGETVserverResponse getQueryByVserverName2(String urlGet, String username, String password, + UUID requestId, String key) { + AAIGETVserverResponse response = new AAIGETVserverResponse(); + + response.setVserverID("d0668d4f-c25e-4a1b-87c4-83845c01efd8"); + response.setVserverName("USMSO1SX7NJ0103UJZZ01-vjunos0"); + response.setVserverName2("vjunos0"); + response.setVserverSelflink( + "https://aai-ext1.test.att.com:8443/aai/v7/cloud-infrastructure/cloud-regions/cloud-region/att-aic/AAIAIC25/tenants/tenant/USMSO1SX7NJ0103UJZZ01%3A%3AuCPE-VMS/vservers/vserver/d0668d4f-c25e-4a1b-87c4-83845c01efd8"); + response.setInMaint("false"); + response.setIsClosedLoopDisabled("false"); + response.setResourceVersion("1494001931513"); + + final RelationshipList relationshipList = new RelationshipList(); + final Relationship relationship = new Relationship(); + RelationshipData relationshipData = new RelationshipData(); + RelationshipDataItem relationshipDataItem = new RelationshipDataItem(); + + relationshipDataItem.setRelationshipKey("customer.global-customer-id"); + relationshipDataItem.setRelationshipValue("MSO_1610_ST"); + relationshipData.getRelationshipData().add(relationshipDataItem); + + relationshipDataItem.setRelationshipKey("service-subscription.service-type"); + relationshipDataItem.setRelationshipValue("MSO-dev-service-type"); + relationshipData.getRelationshipData().add(relationshipDataItem); + + relationshipDataItem.setRelationshipKey("service-instance.service-instance-id"); + relationshipDataItem.setRelationshipValue("e1e9c97c-02c0-4919-9b4c-eb5d5ef68970"); + relationshipData.getRelationshipData().add(relationshipDataItem); + + RelatedToProperty relatedToProperty = new RelatedToProperty(); + RelatedToPropertyItem item = new RelatedToPropertyItem(); + item.setPropertyKey("service-instance.service-instance-name"); + item.setPropertyValue("lll_svc_010317"); + relatedToProperty.getRelatedTo().add(item); + + relationship.setRelatedTo("service-instance"); + relationship.setRelatedLink( + "/aai/v11/business/customers/customer/MSO_1610_ST/service-subscriptions/service-subscription" + + "/MSO-dev-service-type/service-instances/service-instance/" + + "e1e9c97c-02c0-4919-9b4c-eb5d5ef68970"); + relationship.setRelationshipData(relationshipData); + relationship.setRelatedToProperty(relatedToProperty); + + relationshipList.getRelationshipList().add(relationship); + response.setRelationshipList(relationshipList); + + return response; + } + + @Test + public void testMethods() { + ControlLoopEventManager clem = new ControlLoopEventManager("MyClosedLoopName", UUID.randomUUID()); + + clem.setActivated(true); + assertEquals(true, clem.isActivated()); + + clem.setControlLoopResult("SUCCESS"); + assertEquals("SUCCESS", clem.getControlLoopResult()); + + clem.setControlLoopTimedOut(); + assertEquals(true, clem.isControlLoopTimedOut()); + + clem.setNumAbatements(12345); + assertEquals(Integer.valueOf(12345), clem.getNumAbatements()); + + clem.setNumOnsets(54321); + assertEquals(Integer.valueOf(54321), clem.getNumOnsets()); + + assertNull(clem.getOnsetEvent()); + assertNull(clem.getAbatementEvent()); + assertNull(clem.getProcessor()); + + assertEquals(true, clem.isActive()); + assertEquals(false, clem.releaseLock()); + assertEquals(true, clem.isControlLoopTimedOut()); + + assertNull(clem.unlockCurrentOperation()); + } + + @Test + public void testAlreadyActivated() { + UUID requestId = UUID.randomUUID(); + VirtualControlLoopEvent event = new VirtualControlLoopEvent(); + event.setClosedLoopControlName("TwoOnsetTest"); + event.setRequestID(requestId); + event.setTarget("generic-vnf.vnf-id"); + event.setClosedLoopAlarmStart(Instant.now()); + event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + event.setAAI(new HashMap<>()); + event.getAAI().put("generic-vnf.vnf-name", "onsetOne"); + + ControlLoopEventManager manager = + new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); + manager.setActivated(true); + VirtualControlLoopNotification notification = manager.activate(event); + assertEquals(ControlLoopNotificationType.REJECTED, notification.getNotification()); + } + + @Test + public void testActivationYaml() throws IOException { + InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); + final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); + + InputStream isBad = new FileInputStream(new File("src/test/resources/notutf8.yaml")); + final String yamlStringBad = IOUtils.toString(isBad, StandardCharsets.UTF_8); + + UUID requestId = UUID.randomUUID(); + VirtualControlLoopEvent event = new VirtualControlLoopEvent(); + event.setClosedLoopControlName("TwoOnsetTest"); + event.setRequestID(requestId); + event.setTarget("generic-vnf.vnf-id"); + event.setClosedLoopAlarmStart(Instant.now()); + event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + event.setAAI(new HashMap<>()); + event.getAAI().put("generic-vnf.vnf-name", "onsetOne"); + + ControlLoopEventManager manager = + new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); + + // Null YAML should fail + VirtualControlLoopNotification notificationNull = manager.activate(null, event); + assertNotNull(notificationNull); + assertEquals(ControlLoopNotificationType.REJECTED, notificationNull.getNotification()); + + // Empty YAML should fail + VirtualControlLoopNotification notificationEmpty = manager.activate("", event); + assertNotNull(notificationEmpty); + assertEquals(ControlLoopNotificationType.REJECTED, notificationEmpty.getNotification()); + + // Bad YAML should fail + VirtualControlLoopNotification notificationBad = manager.activate(yamlStringBad, event); + assertNotNull(notificationBad); + assertEquals(ControlLoopNotificationType.REJECTED, notificationBad.getNotification()); + + VirtualControlLoopNotification notification = manager.activate(yamlString, event); + assertNotNull(notification); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + + // Another activate should fail + VirtualControlLoopNotification notificationActive = manager.activate(yamlString, event); + assertNotNull(notificationActive); + assertEquals(ControlLoopNotificationType.REJECTED, notificationActive.getNotification()); + } + + @Test + public void testControlLoopFinal() throws ControlLoopException, IOException { + InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); + final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); + + UUID requestId = UUID.randomUUID(); + VirtualControlLoopEvent event = new VirtualControlLoopEvent(); + event.setClosedLoopControlName("TwoOnsetTest"); + event.setRequestID(requestId); + event.setTarget("generic-vnf.vnf-id"); + event.setClosedLoopAlarmStart(Instant.now()); + event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + event.setAAI(new HashMap<>()); + event.getAAI().put("generic-vnf.vnf-name", "onsetOne"); + + ControlLoopEventManager manager = + new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); + try { + manager.isControlLoopFinal(); + fail("test should throw an exception here"); + } catch (ControlLoopException e) { + assertEquals("ControlLoopEventManager MUST be activated first.", e.getMessage()); + } + + manager.setActivated(true); + try { + manager.isControlLoopFinal(); + fail("test should throw an exception here"); + } catch (ControlLoopException e) { + assertEquals("No onset event for ControlLoopEventManager.", e.getMessage()); + } + + manager.setActivated(false); + VirtualControlLoopNotification notification = manager.activate(yamlString, event); + assertNotNull(notification); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + + VirtualControlLoopNotification clfNotification = manager.isControlLoopFinal(); + assertNull(clfNotification); + + manager.getProcessor().nextPolicyForResult(PolicyResult.SUCCESS); + clfNotification = manager.isControlLoopFinal(); + assertNotNull(clfNotification); + assertEquals(ControlLoopNotificationType.FINAL_SUCCESS, clfNotification.getNotification()); + + manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); + notification = manager.activate(yamlString, event); + assertNotNull(notification); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + + manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_EXCEPTION); + clfNotification = manager.isControlLoopFinal(); + assertNotNull(clfNotification); + assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification()); + + manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); + notification = manager.activate(yamlString, event); + assertNotNull(notification); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + + manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_GUARD); + clfNotification = manager.isControlLoopFinal(); + assertNotNull(clfNotification); + assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification()); + + manager.setControlLoopTimedOut(); + clfNotification = manager.isControlLoopFinal(); + assertNotNull(clfNotification); + assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification()); + } + + @Test + public void testProcessControlLoop() throws ControlLoopException, IOException, AAIException { + InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); + final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); + + UUID requestId = UUID.randomUUID(); + VirtualControlLoopEvent event = new VirtualControlLoopEvent(); + event.setClosedLoopControlName("TwoOnsetTest"); + event.setRequestID(requestId); + event.setTarget("generic-vnf.vnf-id"); + event.setClosedLoopAlarmStart(Instant.now()); + event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + event.setAAI(new HashMap<>()); + event.getAAI().put("generic-vnf.vnf-name", "onsetOne"); + + ControlLoopEventManager manager = + new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); + try { + manager.processControlLoop(); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("ControlLoopEventManager MUST be activated first.", e.getMessage()); + } + + manager.setActivated(true); + try { + manager.processControlLoop(); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("No onset event for ControlLoopEventManager.", e.getMessage()); + } + + manager.setActivated(false); + VirtualControlLoopNotification notification = manager.activate(yamlString, event); + assertNotNull(notification); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + + ControlLoopOperationManager clom = manager.processControlLoop(); + assertNotNull(clom); + assertNull(clom.getOperationResult()); + + // Test operation in progress + try { + manager.processControlLoop(); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("Already working an Operation, do not call this method.", e.getMessage()); + } + + manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); + notification = manager.activate(yamlString, event); + assertNotNull(notification); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + + manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_GUARD); + VirtualControlLoopNotification clfNotification = manager.isControlLoopFinal(); + assertNotNull(clfNotification); + assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification()); + + // Test operation completed + try { + manager.processControlLoop(); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("Control Loop is in FINAL state, do not call this method.", e.getMessage()); + } + + manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); + notification = manager.activate(yamlString, event); + assertNotNull(notification); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE); + + // Test operation with no next policy defined + try { + manager.processControlLoop(); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("The target type is null", e.getMessage()); + } + } + + @Test + public void testFinishOperation() throws IOException, ControlLoopException, AAIException { + InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml")); + final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); + + InputStream isStd = new FileInputStream(new File("src/test/resources/test.yaml")); + final String yamlStringStd = IOUtils.toString(isStd, StandardCharsets.UTF_8); + + UUID requestId = UUID.randomUUID(); + VirtualControlLoopEvent event = new VirtualControlLoopEvent(); + event.setClosedLoopControlName("TwoOnsetTest"); + event.setRequestID(requestId); + event.setTarget("generic-vnf.vnf-id"); + event.setClosedLoopAlarmStart(Instant.now()); + event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + event.setAAI(new HashMap<>()); + event.getAAI().put("generic-vnf.vnf-name", "onsetOne"); + + ControlLoopEventManager manager = + new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); + try { + manager.finishOperation(null); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("No operation to finish.", e.getMessage()); + } + + manager.setActivated(true); + try { + manager.finishOperation(null); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("No operation to finish.", e.getMessage()); + } + + manager.setActivated(false); + VirtualControlLoopNotification notification = manager.activate(yamlString, event); + assertNotNull(notification); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + + try { + manager.lockCurrentOperation(); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("Do not have a current operation.", e.getMessage()); + } + + assertNull(manager.unlockCurrentOperation()); + + ControlLoopOperationManager clom = manager.processControlLoop(); + assertNotNull(clom); + assertNull(clom.getOperationResult()); + + LockResult lockLock = manager.lockCurrentOperation(); + assertNotNull(lockLock); + + LockResult lockLockAgain = manager.lockCurrentOperation(); + assertNotNull(lockLockAgain); + assertEquals(lockLock.getB(), lockLockAgain.getB()); + + assertEquals(lockLock.getB(), manager.unlockCurrentOperation()); + assertNull(manager.unlockCurrentOperation()); + + lockLock = manager.lockCurrentOperation(); + assertNotNull(lockLock); + PolicyGuard.unlockTarget(lockLock.getB()); + assertNull(manager.unlockCurrentOperation()); + + clom.startOperation(event); + + // This call should be exception free + manager.finishOperation(clom); + + ControlLoopEventManager otherManager = + new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestID()); + VirtualControlLoopNotification otherNotification = otherManager.activate(yamlStringStd, event); + assertNotNull(otherNotification); + assertEquals(ControlLoopNotificationType.ACTIVE, otherNotification.getNotification()); + + ControlLoopOperationManager otherClom = otherManager.processControlLoop(); + assertNotNull(otherClom); + assertNull(otherClom.getOperationResult()); + + otherManager.finishOperation(clom); + } + + @Test + public void testOnNewEvent() throws IOException, AAIException { + InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); + final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); + + UUID requestId = UUID.randomUUID(); + VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent(); + onsetEvent.setClosedLoopControlName("TwoOnsetTest"); + onsetEvent.setRequestID(requestId); + onsetEvent.setTarget("generic-vnf.vnf-id"); + onsetEvent.setClosedLoopAlarmStart(Instant.now()); + onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + onsetEvent.setAAI(new HashMap<>()); + onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); + + VirtualControlLoopEvent abatedEvent = new VirtualControlLoopEvent(); + abatedEvent.setClosedLoopControlName("TwoOnsetTest"); + abatedEvent.setRequestID(requestId); + abatedEvent.setTarget("generic-vnf.vnf-id"); + abatedEvent.setClosedLoopAlarmStart(Instant.now()); + abatedEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED); + abatedEvent.setAAI(new HashMap<>()); + abatedEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); + + ControlLoopEventManager manager = + new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID()); + VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent); + assertNotNull(notification); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + + assertEquals(NEW_EVENT_STATUS.FIRST_ONSET, manager.onNewEvent(onsetEvent)); + assertEquals(NEW_EVENT_STATUS.FIRST_ABATEMENT, manager.onNewEvent(abatedEvent)); + assertEquals(NEW_EVENT_STATUS.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent)); + + VirtualControlLoopEvent checkSyntaxEvent = new VirtualControlLoopEvent(); + checkSyntaxEvent.setAAI(null); + checkSyntaxEvent.setClosedLoopAlarmEnd(null); + checkSyntaxEvent.setClosedLoopAlarmStart(null); + checkSyntaxEvent.setClosedLoopControlName(null); + checkSyntaxEvent.setClosedLoopEventClient(null); + checkSyntaxEvent.setClosedLoopEventStatus(null); + checkSyntaxEvent.setFrom(null); + checkSyntaxEvent.setPolicyName(null); + checkSyntaxEvent.setPolicyScope(null); + checkSyntaxEvent.setPolicyVersion(null); + checkSyntaxEvent.setRequestID(null); + checkSyntaxEvent.setTarget(null); + checkSyntaxEvent.setTargetType(null); + checkSyntaxEvent.setVersion(null); + + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.setClosedLoopControlName(null); + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.setClosedLoopControlName(""); + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.setClosedLoopControlName("TwoOnsetTest"); + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.setRequestID(null); + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.setRequestID(requestId); + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.setAAI(null); + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.setAAI(new HashMap<>()); + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.setTarget(""); + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.setTarget(null); + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.setTarget(""); + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.setTarget("OZ"); + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.setTarget("VM_NAME"); + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.setTarget("VNF_NAME"); + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.setTarget("vserver.vserver-name"); + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.setTarget("generic-vnf.vnf-id"); + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.setTarget("generic-vnf.vnf-name"); + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.setAAI(null); + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.setAAI(new HashMap<>()); + assertEquals(NEW_EVENT_STATUS.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent)); + + checkSyntaxEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); + assertEquals(NEW_EVENT_STATUS.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent)); + + checkSyntaxEvent.getAAI().put("vserver.vserver-name", "onsetOne"); + assertEquals(NEW_EVENT_STATUS.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent)); + + checkSyntaxEvent.getAAI().put("generic-vnf.vnf-id", "onsetOne"); + assertEquals(NEW_EVENT_STATUS.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent)); + } + + @Test + public void testControlLoopTimeout() throws IOException { + InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); + final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); + + UUID requestId = UUID.randomUUID(); + VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent(); + onsetEvent.setClosedLoopControlName("TwoOnsetTest"); + onsetEvent.setRequestID(requestId); + onsetEvent.setTarget("generic-vnf.vnf-id"); + onsetEvent.setClosedLoopAlarmStart(Instant.now()); + onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + onsetEvent.setAAI(new HashMap<>()); + onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); + + ControlLoopEventManager manager = + new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID()); + assertTrue(0 == manager.getControlLoopTimeout(null)); + assertTrue(120 == manager.getControlLoopTimeout(120)); + + VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent); + assertNotNull(notification); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + + assertTrue(60 == manager.getControlLoopTimeout(null)); + } + + @Test + public void testQueryAai() throws IOException, AAIException { + InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); + final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); + + UUID requestId = UUID.randomUUID(); + VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent(); + onsetEvent.setClosedLoopControlName("TwoOnsetTest"); + onsetEvent.setRequestID(requestId); + onsetEvent.setTarget("generic-vnf.vnf-id"); + onsetEvent.setClosedLoopAlarmStart(Instant.now()); + onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + onsetEvent.setAAI(new HashMap<>()); + onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); + + ControlLoopEventManager manager = + new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID()); + manager.queryAai(onsetEvent); + + VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent); + assertNotNull(notification); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + + manager.queryAai(onsetEvent); + + onsetEvent.getAAI().put("generic-vnf.is-closed-loop-disabled", "true"); + try { + manager.queryAai(onsetEvent); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("is-closed-loop-disabled is set to true on VServer or VNF", e.getMessage()); + } + onsetEvent.getAAI().put("vserver.is-closed-loop-disabled", "true"); + try { + manager.queryAai(onsetEvent); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("is-closed-loop-disabled is set to true on VServer or VNF", e.getMessage()); + } + onsetEvent.getAAI().remove("generic-vnf.is-closed-loop-disabled"); + try { + manager.queryAai(onsetEvent); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("is-closed-loop-disabled is set to true on VServer or VNF", e.getMessage()); + } + onsetEvent.getAAI().remove("vserver.is-closed-loop-disabled"); + manager.queryAai(onsetEvent); + + onsetEvent.getAAI().put("generic-vnf.is-closed-loop-disabled", "false"); + manager.queryAai(onsetEvent); + onsetEvent.getAAI().put("vserver.is-closed-loop-disabled", "false"); + manager.queryAai(onsetEvent); + + onsetEvent.getAAI().remove("generic-vnf.vnf-id"); + onsetEvent.getAAI().remove("generic-vnf.vnf-name"); + onsetEvent.getAAI().remove("vserver.vserver-name"); + manager.queryAai(onsetEvent); + + onsetEvent.getAAI().put("vserver.vserver-name", "AVserver"); + manager.queryAai(onsetEvent); + + onsetEvent.getAAI().put("generic-vnf.vnf-name", "AVNFName"); + manager.queryAai(onsetEvent); + + onsetEvent.getAAI().put("generic-vnf.vnf-id", "AVNFID"); + manager.queryAai(onsetEvent); + + onsetEvent.getAAI().remove("vserver.vserver-name"); + manager.queryAai(onsetEvent); + + onsetEvent.getAAI().remove("generic-vnf.vnf-name"); + manager.queryAai(onsetEvent); + + // Force AAI errors + PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:9999"); + + try { + manager.queryAai(onsetEvent); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("Exception from queryAai: org.onap.policy.aai.util.AAIException: AAI Response is null " + + "(query by vnf-id)", e.getMessage()); + } + + onsetEvent.getAAI().remove("generic-vnf.vnf-id"); + onsetEvent.getAAI().put("generic-vnf.vnf-name", "AVNFName"); + try { + manager.queryAai(onsetEvent); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("Exception from queryAai: org.onap.policy.aai.util.AAIException: AAI Response is null " + + "(query by vnf-name)", e.getMessage()); + } + + onsetEvent.getAAI().remove("generic-vnf.vnf-name"); + onsetEvent.getAAI().put("vserver.vserver-name", "AVserver"); + try { + manager.queryAai(onsetEvent); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("Exception from queryAai: org.onap.policy.aai.util.AAIException: AAI Response is null " + + "(query by vserver-name)", e.getMessage()); + } + + PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666"); + } +} diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManagerTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManagerTest.java index 4a30a451a..0d811bbd8 100644 --- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManagerTest.java +++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManagerTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * unit test * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -51,11 +51,11 @@ import org.onap.policy.appclcm.LCMRequestWrapper; import org.onap.policy.appclcm.LCMResponse; import org.onap.policy.appclcm.LCMResponseWrapper; import org.onap.policy.controlloop.ControlLoopEventStatus; -import org.onap.policy.controlloop.ControlLoopNotificationType; -import org.onap.policy.controlloop.VirtualControlLoopEvent; import org.onap.policy.controlloop.ControlLoopException; +import org.onap.policy.controlloop.ControlLoopNotificationType; import org.onap.policy.controlloop.ControlLoopTargetType; import org.onap.policy.controlloop.Util; +import org.onap.policy.controlloop.VirtualControlLoopEvent; import org.onap.policy.controlloop.VirtualControlLoopNotification; import org.onap.policy.controlloop.policy.ControlLoopPolicy; import org.onap.policy.controlloop.policy.Policy; @@ -73,674 +73,691 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ControlLoopOperationManagerTest { - private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationManagerTest.class); - - - private static VirtualControlLoopEvent onset; - - static { - onset = new VirtualControlLoopEvent(); - onset.setRequestID(UUID.randomUUID()); - onset.setTarget("generic-vnf.vnf-name"); - onset.setTargetType(ControlLoopTargetType.VNF); - onset.setClosedLoopAlarmStart(Instant.now()); - onset.setAAI(new HashMap<>()); - onset.getAAI().put("generic-vnf.vnf-name", "testTriggerSource"); - onset.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - - /* Set environment properties */ - PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666"); - PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI"); - PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI"); - } - - @BeforeClass - public static void setUpSimulator() { - try { - org.onap.policy.simulators.Util.buildAaiSim(); - } catch (Exception e) { - fail(e.getMessage()); - } - } - - @AfterClass - public static void tearDownSimulator() { - HttpServletServer.factory.destroy(); - } - - @Test - public void testRetriesFail() { - // - // Load up the policy - // - final Util.Pair pair = Util.loadYaml("src/test/resources/test.yaml"); - onset.setClosedLoopControlName(pair.a.getControlLoop().getControlLoopName()); - try { - // - // Create a processor - // - ControlLoopProcessor processor = new ControlLoopProcessor(pair.b); - // - // create the manager - // - ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.getClosedLoopControlName(), onset.getRequestID()); - VirtualControlLoopNotification notification = eventManager.activate(onset); - - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); - - ControlLoopEventManager.NEW_EVENT_STATUS status = null; - try { - status = eventManager.onNewEvent(onset); - } catch (AAIException e) { - logger.warn(e.toString()); - fail("A&AI Query Failed"); - } - assertNotNull(status); - assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.FIRST_ONSET, status); - - ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager); - logger.debug("{}",manager); - // - // - // - assertFalse(manager.isOperationComplete()); - assertFalse(manager.isOperationRunning()); - // - // Start - // - Object request = manager.startOperation(onset); - logger.debug("{}",manager); - assertNotNull(request); - assertTrue(request instanceof LCMRequestWrapper); - LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request; - LCMRequest appcRequest = dmaapRequest.getBody(); - assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("1")); - assertFalse(manager.isOperationComplete()); - assertTrue(manager.isOperationRunning()); - // - // Accept - // - LCMResponseWrapper dmaapResponse = new LCMResponseWrapper(); - LCMResponse appcResponse = new LCMResponse((LCMRequest) appcRequest); - appcResponse.getStatus().setCode(100); - appcResponse.getStatus().setMessage("ACCEPT"); - dmaapResponse.setBody(appcResponse); - // - // - // - PolicyResult result = manager.onResponse(dmaapResponse); - logger.debug("{}",manager); - assertTrue(result == null); - assertFalse(manager.isOperationComplete()); - assertTrue(manager.isOperationRunning()); - // - // Now we are going to Fail it - // - appcResponse = new LCMResponse(appcRequest); - appcResponse.getStatus().setCode(401); - appcResponse.getStatus().setMessage("AppC failed for some reason"); - dmaapResponse.setBody(appcResponse); - result = manager.onResponse(dmaapResponse); - logger.debug("{}",manager); - assertTrue(result.equals(PolicyResult.FAILURE)); - assertFalse(manager.isOperationComplete()); - assertFalse(manager.isOperationRunning()); - // - // Retry it - // - request = manager.startOperation(onset); - logger.debug("{}",manager); - assertNotNull(request); - assertTrue(request instanceof LCMRequestWrapper); - dmaapRequest = (LCMRequestWrapper) request; - appcRequest = dmaapRequest.getBody(); - assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("2")); - assertFalse(manager.isOperationComplete()); - assertTrue(manager.isOperationRunning()); - // - // - // - appcResponse = new LCMResponse((LCMRequest) appcRequest); - logger.debug("{}",manager); - appcResponse.getStatus().setCode(100); - appcResponse.getStatus().setMessage("ACCEPT"); - dmaapResponse.setBody(appcResponse); - // - // - // - result = manager.onResponse(dmaapResponse); - logger.debug("{}",manager); - assertTrue(result == null); - assertFalse(manager.isOperationComplete()); - assertTrue(manager.isOperationRunning()); - // - // Now we are going to Fail it - // - appcResponse = new LCMResponse((LCMRequest) appcRequest); - appcResponse.getStatus().setCode(401); - appcResponse.getStatus().setMessage("AppC failed for some reason"); - dmaapResponse.setBody(appcResponse); - result = manager.onResponse(dmaapResponse); - logger.debug("{}",manager); - assertTrue(result.equals(PolicyResult.FAILURE)); - // - // Should be complete now - // - assertTrue(manager.isOperationComplete()); - assertFalse(manager.isOperationRunning()); - assertNotNull(manager.getOperationResult()); - assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_RETRIES)); - assertTrue(manager.getHistory().size() == 2); - } catch (ControlLoopException | AAIException e) { - fail(e.getMessage()); - } - } - - @Test - public void testTimeout() { - // - // Load up the policy - // - final Util.Pair pair = Util.loadYaml("src/test/resources/test.yaml"); - onset.setClosedLoopControlName(pair.a.getControlLoop().getControlLoopName()); - try { - // - // Create a processor - // - ControlLoopProcessor processor = new ControlLoopProcessor(pair.b); - // - // create the manager - // - ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.getClosedLoopControlName(), onset.getRequestID()); - VirtualControlLoopNotification notification = eventManager.activate(onset); - - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); - - ControlLoopEventManager.NEW_EVENT_STATUS status = null; - try { - status = eventManager.onNewEvent(onset); - } catch (AAIException e) { - logger.warn(e.toString()); - fail("A&AI Query Failed"); - } - assertNotNull(status); - assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.FIRST_ONSET, status); - - ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager); - // - // - // - logger.debug("{}",manager); - assertFalse(manager.isOperationComplete()); - assertFalse(manager.isOperationRunning()); - // - // Start - // - Object request = manager.startOperation(onset); - logger.debug("{}",manager); - assertNotNull(request); - assertTrue((request) instanceof LCMRequestWrapper); - LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request; - LCMRequest appcRequest = dmaapRequest.getBody(); - assertTrue((appcRequest).getCommonHeader().getSubRequestId().contentEquals("1")); - assertFalse(manager.isOperationComplete()); - assertTrue(manager.isOperationRunning()); - // - // Accept - // - LCMResponseWrapper dmaapResponse = new LCMResponseWrapper(); - LCMResponse appcResponse = new LCMResponse(appcRequest); - dmaapResponse.setBody(appcResponse); - appcResponse.getStatus().setCode(100); - appcResponse.getStatus().setMessage("ACCEPT"); - // - // - // - PolicyResult result = manager.onResponse(dmaapResponse); - logger.debug("{}",manager); - assertTrue(result == null); - assertFalse(manager.isOperationComplete()); - assertTrue(manager.isOperationRunning()); - // - // Now we are going to simulate Timeout - // - manager.setOperationHasTimedOut(); - logger.debug("{}",manager); - assertTrue(manager.isOperationComplete()); - assertFalse(manager.isOperationRunning()); - assertTrue(manager.getHistory().size() == 1); - assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT)); - // - // Now we are going to Fail the previous request - // - appcResponse = new LCMResponse(appcRequest); - appcResponse.getStatus().setCode(401); - appcResponse.getStatus().setMessage("AppC failed for some reason"); - dmaapResponse.setBody(appcResponse); - result = manager.onResponse(dmaapResponse); - logger.debug("{}",manager); - // - // - // - assertTrue(manager.isOperationComplete()); - assertFalse(manager.isOperationRunning()); - assertTrue(manager.getHistory().size() == 1); - assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT)); - } catch (ControlLoopException | AAIException e) { - fail(e.getMessage()); - } - } - - @Test - public void testMethods() throws IOException, ControlLoopException, AAIException { - InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml")); - String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); - - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent(); - onsetEvent.setClosedLoopControlName("TwoOnsetTest"); - onsetEvent.setRequestID(requestId); - onsetEvent.setTarget("generic-vnf.vnf-id"); - onsetEvent.setClosedLoopAlarmStart(Instant.now()); - onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - onsetEvent.setAAI(new HashMap<>()); - onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); - - ControlLoopEventManager manager = new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID()); - VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent); - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); - - ControlLoopOperationManager clom = manager.processControlLoop(); - assertNotNull(clom); - assertNull(clom.getOperationResult()); - - clom.setEventManager(manager); - assertEquals(manager, clom.getEventManager()); - - assertNull(clom.getTargetEntity()); - - clom.setGuardApprovalStatus("WizardOKedIt"); - assertEquals("WizardOKedIt", clom.getGuardApprovalStatus()); - - assertNull(clom.getOperationResult()); - - Policy policy = manager.getProcessor().getCurrentPolicy(); - clom.getTarget(policy); - - Target savedTarget = policy.getTarget(); - policy.setTarget(null); - try { - clom.getTarget(policy); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("The target is null", e.getMessage()); - } - - policy.setTarget(new Target()); - try { - clom.getTarget(policy); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("The target type is null", e.getMessage()); - } - - policy.setTarget(savedTarget); - - policy.getTarget().setType(TargetType.PNF); - try { - clom.getTarget(policy); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("PNF target is not supported", e.getMessage()); - } - - onsetEvent.setTarget("Oz"); - onsetEvent.getAAI().remove("generic-vnf.vnf-name"); - onsetEvent.getAAI().remove("generic-vnf.vnf-id"); - onsetEvent.getAAI().remove("vserver.vserver-name"); - - policy.getTarget().setType(TargetType.VNF); - try { - clom.getTarget(policy); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("Target does not match target type", e.getMessage()); - } - - onsetEvent.setTarget("vserver.vserver-name"); - onsetEvent.getAAI().put("vserver.vserver-name", "OzVServer"); - assertEquals("OzVServer", clom.getTarget(policy)); - - onsetEvent.getAAI().remove("vserver.vserver-name"); - onsetEvent.setTarget("generic-vnf.vnf-id"); - onsetEvent.getAAI().put("generic-vnf.vnf-id", "OzVNF"); - assertEquals("OzVNF", clom.getTarget(policy)); - - onsetEvent.setTarget("generic-vnf.vnf-name"); - assertEquals("OzVNF", clom.getTarget(policy)); - - manager.onNewEvent(onsetEvent); - - onsetEvent.getAAI().remove("generic-vnf.vnf-id"); - manager.getVnfResponse(); - clom.getEventManager().getVnfResponse().setVnfID("generic-vnf.vnf-id"); - assertEquals("generic-vnf.vnf-id", clom.getTarget(policy)); - - policy.getTarget().setType(TargetType.VFC); - try { - clom.getTarget(policy); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("The target type is not supported", e.getMessage()); - } - - assertEquals(Integer.valueOf(20), clom.getOperationTimeout()); - - assertEquals("20s", clom.getOperationTimeoutString(100)); - - assertEquals(null, clom.getOperationMessage()); - assertEquals(null, clom.getOperationMessage("The Wizard Escaped")); - - clom.startOperation(onsetEvent); - - assertEquals("actor=SO,operation=Restart,target=Target [type=VFC, resourceID=null],subRequestId=1", clom.getOperationMessage()); - assertEquals("actor=SO,operation=Restart,target=Target [type=VFC, resourceID=null],subRequestId=1, Guard result: The Wizard Escaped", clom.getOperationMessage("The Wizard Escaped")); - - assertEquals("actor=SO,operation=Restart,tar", clom.getOperationHistory().substring(0, 30)); - - clom.setOperationHasException("The Wizard is gone"); - clom.setOperationHasGuardDeny(); - } - - @Test - public void testConstructor() throws IOException, ControlLoopException, AAIException { - InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); - String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); - - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent(); - onsetEvent.setClosedLoopControlName("TwoOnsetTest"); - onsetEvent.setRequestID(requestId); - onsetEvent.setTarget("generic-vnf.vnf-id"); - onsetEvent.setClosedLoopAlarmStart(Instant.now()); - onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - onsetEvent.setAAI(new HashMap<>()); - onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); - - ControlLoopEventManager manager = new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID()); - VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent); - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); - - Policy policy = manager.getProcessor().getCurrentPolicy(); - ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager); - assertNotNull(clom); - - policy.setRecipe("ModifyConfig"); - policy.getTarget().setResourceID(UUID.randomUUID().toString()); - try { - new ControlLoopOperationManager(onsetEvent, policy, manager); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("Target vnf-id could not be found", e.getMessage()); - } - - policy.getTarget().setResourceID("82194af1-3c2c-485a-8f44-420e22a9eaa4"); - clom = new ControlLoopOperationManager(onsetEvent, policy, manager); - assertNotNull(clom); - - policy.setActor("SO"); - clom = new ControlLoopOperationManager(onsetEvent, policy, manager); - assertNotNull(clom); - - policy.setActor("VFC"); - clom = new ControlLoopOperationManager(onsetEvent, policy, manager); - assertNotNull(clom); - - policy.setActor("Dorothy"); - try { - new ControlLoopOperationManager(onsetEvent, policy, manager); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("ControlLoopEventManager: policy has an unknown actor.", e.getMessage()); - } - } - - @Test - public void testStartOperation() throws IOException, ControlLoopException, AAIException { - InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); - String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); - - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent(); - onsetEvent.setClosedLoopControlName("TwoOnsetTest"); - onsetEvent.setRequestID(requestId); - onsetEvent.setTarget("generic-vnf.vnf-id"); - onsetEvent.setClosedLoopAlarmStart(Instant.now()); - onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - onsetEvent.setAAI(new HashMap<>()); - onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); - - ControlLoopEventManager manager = new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID()); - VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent); - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); - - Policy policy = manager.getProcessor().getCurrentPolicy(); - ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager); - assertNotNull(clom); - - clom.startOperation(onsetEvent); - - try { - clom.startOperation(onsetEvent); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("current operation is not null (an operation is already running)", e.getMessage()); - } - - clom = new ControlLoopOperationManager(onsetEvent, policy, manager); - assertNotNull(clom); - String savedRecipe = policy.getRecipe(); - policy.setRecipe("ModifyConfig"); - policy.getTarget().setResourceID(UUID.randomUUID().toString()); - clom.startOperation(onsetEvent); - policy.setRecipe(savedRecipe); - - policy.setRetry(null); - clom = new ControlLoopOperationManager(onsetEvent, policy, manager); - assertNotNull(clom); - clom.startOperation(onsetEvent); - clom.setOperationHasTimedOut(); - assertTrue(clom.isOperationComplete()); - try { - clom.startOperation(onsetEvent); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("current operation failed and retries are not allowed", e.getMessage()); - } - - policy.setRetry(0); - clom = new ControlLoopOperationManager(onsetEvent, policy, manager); - assertNotNull(clom); - clom.startOperation(onsetEvent); - clom.setOperationHasTimedOut(); - assertTrue(clom.isOperationComplete()); - try { - clom.startOperation(onsetEvent); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("current operation failed and retries are not allowed", e.getMessage()); - } - - policy.setRetry(1); - clom = new ControlLoopOperationManager(onsetEvent, policy, manager); - assertNotNull(clom); - clom.startOperation(onsetEvent); - clom.setOperationHasTimedOut(); - clom.startOperation(onsetEvent); - clom.setOperationHasTimedOut(); - assertTrue(clom.isOperationComplete()); - try { - clom.startOperation(onsetEvent); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("current oepration has failed after 2 retries", e.getMessage()); - } - - clom = new ControlLoopOperationManager(onsetEvent, policy, manager); - assertNotNull(clom); - policy.setActor("SO"); - clom.startOperation(onsetEvent); - - clom = new ControlLoopOperationManager(onsetEvent, policy, manager); - assertNotNull(clom); - policy.setActor("VFC"); - clom.startOperation(onsetEvent); - - clom = new ControlLoopOperationManager(onsetEvent, policy, manager); - assertNotNull(clom); - policy.setActor("Oz"); - try { - clom.startOperation(onsetEvent); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("invalid actor Oz on policy", e.getMessage()); - } - } - - @Test - public void testOnResponse() throws IOException, ControlLoopException, AAIException { - InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); - String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); - - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent(); - onsetEvent.setClosedLoopControlName("TwoOnsetTest"); - onsetEvent.setRequestID(requestId); - onsetEvent.setTarget("generic-vnf.vnf-id"); - onsetEvent.setClosedLoopAlarmStart(Instant.now()); - onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - onsetEvent.setAAI(new HashMap<>()); - onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); - - ControlLoopEventManager manager = new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID()); - VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent); - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); - - Policy policy = manager.getProcessor().getCurrentPolicy(); - ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager); - assertNotNull(clom); - - assertNull(clom.onResponse(null)); - - Response appcResponse = new Response(); - CommonHeader commonHeader = new CommonHeader(); - appcResponse.setCommonHeader(commonHeader ); - assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse)); - - commonHeader.setSubRequestID("12345"); - appcResponse.setStatus(null); - assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse)); - - ResponseStatus responseStatus = new ResponseStatus(); - appcResponse.setStatus(responseStatus); - assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse)); - - responseStatus.setCode(0); - assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse)); - - responseStatus.setCode(ResponseCode.ACCEPT.getValue()); - assertEquals(null, clom.onResponse(appcResponse)); - - responseStatus.setCode(ResponseCode.ERROR.getValue()); - assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse)); - - responseStatus.setCode(ResponseCode.FAILURE.getValue()); - assertEquals(PolicyResult.FAILURE, clom.onResponse(appcResponse)); - - responseStatus.setCode(ResponseCode.REJECT.getValue()); - assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse)); - - responseStatus.setCode(ResponseCode.SUCCESS.getValue()); - assertEquals(PolicyResult.SUCCESS, clom.onResponse(appcResponse)); - - LCMResponseWrapper lrw = new LCMResponseWrapper(); - LCMResponse body = new LCMResponse(); - LCMCommonHeader lcmCH = new LCMCommonHeader(); - body.setCommonHeader(lcmCH ); - lrw.setBody(body ); - - lcmCH.setSubRequestId("NotANumber"); - assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(lrw)); - - lcmCH.setSubRequestId("12345"); - assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(lrw)); - - SOResponse soResponse = new SOResponse(); - SOResponseWrapper soRW = new SOResponseWrapper(soResponse , null); - - soResponse.setHttpResponseCode(200); - assertEquals(PolicyResult.SUCCESS, clom.onResponse(soRW)); - - soResponse.setHttpResponseCode(202); - assertEquals(PolicyResult.SUCCESS, clom.onResponse(soRW)); - - soResponse.setHttpResponseCode(500); - assertEquals(PolicyResult.FAILURE, clom.onResponse(soRW)); - - VFCResponse vfcResponse = new VFCResponse(); - VFCResponseDescriptor responseDescriptor = new VFCResponseDescriptor(); - vfcResponse.setResponseDescriptor(responseDescriptor ); - - responseDescriptor.setStatus("finished"); - assertEquals(PolicyResult.SUCCESS, clom.onResponse(vfcResponse)); - - responseDescriptor.setStatus("unfinished"); - assertEquals(PolicyResult.FAILURE, clom.onResponse(vfcResponse)); - } - - @Test - public void testCompleteOperation() throws ControlLoopException, AAIException, IOException { - InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); - String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); - - UUID requestId = UUID.randomUUID(); - VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent(); - onsetEvent.setClosedLoopControlName("TwoOnsetTest"); - onsetEvent.setRequestID(requestId); - onsetEvent.setTarget("generic-vnf.vnf-id"); - onsetEvent.setClosedLoopAlarmStart(Instant.now()); - onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); - onsetEvent.setAAI(new HashMap<>()); - onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); - - ControlLoopEventManager manager = new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID()); - VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent); - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationManagerTest.class); + + + private static VirtualControlLoopEvent onset; + + static { + onset = new VirtualControlLoopEvent(); + onset.setRequestID(UUID.randomUUID()); + onset.setTarget("generic-vnf.vnf-name"); + onset.setTargetType(ControlLoopTargetType.VNF); + onset.setClosedLoopAlarmStart(Instant.now()); + onset.setAAI(new HashMap<>()); + onset.getAAI().put("generic-vnf.vnf-name", "testTriggerSource"); + onset.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + + /* Set environment properties */ + PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666"); + PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI"); + PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI"); + } + + /** + * Set up test class. + */ + @BeforeClass + public static void setUpSimulator() { + try { + org.onap.policy.simulators.Util.buildAaiSim(); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @AfterClass + public static void tearDownSimulator() { + HttpServletServer.factory.destroy(); + } + + @Test + public void testRetriesFail() { + // + // Load up the policy + // + final Util.Pair pair = Util.loadYaml("src/test/resources/test.yaml"); + onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName()); + try { + // + // Create a processor + // + final ControlLoopProcessor processor = new ControlLoopProcessor(pair.value); + // + // create the manager + // + ControlLoopEventManager eventManager = + new ControlLoopEventManager(onset.getClosedLoopControlName(), onset.getRequestID()); + VirtualControlLoopNotification notification = eventManager.activate(onset); + + assertNotNull(notification); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + + ControlLoopEventManager.NEW_EVENT_STATUS status = null; + try { + status = eventManager.onNewEvent(onset); + } catch (AAIException e) { + logger.warn(e.toString()); + fail("A&AI Query Failed"); + } + assertNotNull(status); + assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.FIRST_ONSET, status); + + ControlLoopOperationManager manager = + new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager); + logger.debug("{}", manager); + // + // + // + assertFalse(manager.isOperationComplete()); + assertFalse(manager.isOperationRunning()); + // + // Start + // + Object request = manager.startOperation(onset); + logger.debug("{}", manager); + assertNotNull(request); + assertTrue(request instanceof LCMRequestWrapper); + LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request; + LCMRequest appcRequest = dmaapRequest.getBody(); + assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("1")); + assertFalse(manager.isOperationComplete()); + assertTrue(manager.isOperationRunning()); + // + // Accept + // + LCMResponseWrapper dmaapResponse = new LCMResponseWrapper(); + LCMResponse appcResponse = new LCMResponse(appcRequest); + appcResponse.getStatus().setCode(100); + appcResponse.getStatus().setMessage("ACCEPT"); + dmaapResponse.setBody(appcResponse); + // + // + // + PolicyResult result = manager.onResponse(dmaapResponse); + logger.debug("{}", manager); + assertTrue(result == null); + assertFalse(manager.isOperationComplete()); + assertTrue(manager.isOperationRunning()); + // + // Now we are going to Fail it + // + appcResponse = new LCMResponse(appcRequest); + appcResponse.getStatus().setCode(401); + appcResponse.getStatus().setMessage("AppC failed for some reason"); + dmaapResponse.setBody(appcResponse); + result = manager.onResponse(dmaapResponse); + logger.debug("{}", manager); + assertTrue(result.equals(PolicyResult.FAILURE)); + assertFalse(manager.isOperationComplete()); + assertFalse(manager.isOperationRunning()); + // + // Retry it + // + request = manager.startOperation(onset); + logger.debug("{}", manager); + assertNotNull(request); + assertTrue(request instanceof LCMRequestWrapper); + dmaapRequest = (LCMRequestWrapper) request; + appcRequest = dmaapRequest.getBody(); + assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("2")); + assertFalse(manager.isOperationComplete()); + assertTrue(manager.isOperationRunning()); + // + // + // + appcResponse = new LCMResponse(appcRequest); + logger.debug("{}", manager); + appcResponse.getStatus().setCode(100); + appcResponse.getStatus().setMessage("ACCEPT"); + dmaapResponse.setBody(appcResponse); + // + // + // + result = manager.onResponse(dmaapResponse); + logger.debug("{}", manager); + assertTrue(result == null); + assertFalse(manager.isOperationComplete()); + assertTrue(manager.isOperationRunning()); + // + // Now we are going to Fail it + // + appcResponse = new LCMResponse(appcRequest); + appcResponse.getStatus().setCode(401); + appcResponse.getStatus().setMessage("AppC failed for some reason"); + dmaapResponse.setBody(appcResponse); + result = manager.onResponse(dmaapResponse); + logger.debug("{}", manager); + assertTrue(result.equals(PolicyResult.FAILURE)); + // + // Should be complete now + // + assertTrue(manager.isOperationComplete()); + assertFalse(manager.isOperationRunning()); + assertNotNull(manager.getOperationResult()); + assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_RETRIES)); + assertTrue(manager.getHistory().size() == 2); + } catch (ControlLoopException | AAIException e) { + fail(e.getMessage()); + } + } + + @Test + public void testTimeout() { + // + // Load up the policy + // + final Util.Pair pair = Util.loadYaml("src/test/resources/test.yaml"); + onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName()); + try { + // + // Create a processor + // + final ControlLoopProcessor processor = new ControlLoopProcessor(pair.value); + // + // create the manager + // + ControlLoopEventManager eventManager = + new ControlLoopEventManager(onset.getClosedLoopControlName(), onset.getRequestID()); + VirtualControlLoopNotification notification = eventManager.activate(onset); + + assertNotNull(notification); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + + ControlLoopEventManager.NEW_EVENT_STATUS status = null; + try { + status = eventManager.onNewEvent(onset); + } catch (AAIException e) { + logger.warn(e.toString()); + fail("A&AI Query Failed"); + } + assertNotNull(status); + assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.FIRST_ONSET, status); + + ControlLoopOperationManager manager = + new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager); + // + // + // + logger.debug("{}", manager); + assertFalse(manager.isOperationComplete()); + assertFalse(manager.isOperationRunning()); + // + // Start + // + Object request = manager.startOperation(onset); + logger.debug("{}", manager); + assertNotNull(request); + assertTrue((request) instanceof LCMRequestWrapper); + LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request; + LCMRequest appcRequest = dmaapRequest.getBody(); + assertTrue((appcRequest).getCommonHeader().getSubRequestId().contentEquals("1")); + assertFalse(manager.isOperationComplete()); + assertTrue(manager.isOperationRunning()); + // + // Accept + // + LCMResponseWrapper dmaapResponse = new LCMResponseWrapper(); + LCMResponse appcResponse = new LCMResponse(appcRequest); + dmaapResponse.setBody(appcResponse); + appcResponse.getStatus().setCode(100); + appcResponse.getStatus().setMessage("ACCEPT"); + // + // + // + PolicyResult result = manager.onResponse(dmaapResponse); + logger.debug("{}", manager); + assertTrue(result == null); + assertFalse(manager.isOperationComplete()); + assertTrue(manager.isOperationRunning()); + // + // Now we are going to simulate Timeout + // + manager.setOperationHasTimedOut(); + logger.debug("{}", manager); + assertTrue(manager.isOperationComplete()); + assertFalse(manager.isOperationRunning()); + assertTrue(manager.getHistory().size() == 1); + assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT)); + // + // Now we are going to Fail the previous request + // + appcResponse = new LCMResponse(appcRequest); + appcResponse.getStatus().setCode(401); + appcResponse.getStatus().setMessage("AppC failed for some reason"); + dmaapResponse.setBody(appcResponse); + result = manager.onResponse(dmaapResponse); + logger.debug("{}", manager); + // + // + // + assertTrue(manager.isOperationComplete()); + assertFalse(manager.isOperationRunning()); + assertTrue(manager.getHistory().size() == 1); + assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT)); + } catch (ControlLoopException | AAIException e) { + fail(e.getMessage()); + } + } + + @Test + public void testMethods() throws IOException, ControlLoopException, AAIException { + InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml")); + final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); + + UUID requestId = UUID.randomUUID(); + VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent(); + onsetEvent.setClosedLoopControlName("TwoOnsetTest"); + onsetEvent.setRequestID(requestId); + onsetEvent.setTarget("generic-vnf.vnf-id"); + onsetEvent.setClosedLoopAlarmStart(Instant.now()); + onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + onsetEvent.setAAI(new HashMap<>()); + onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); + + ControlLoopEventManager manager = + new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID()); + VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent); + assertNotNull(notification); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + + ControlLoopOperationManager clom = manager.processControlLoop(); + assertNotNull(clom); + assertNull(clom.getOperationResult()); + + clom.setEventManager(manager); + assertEquals(manager, clom.getEventManager()); + + assertNull(clom.getTargetEntity()); + + clom.setGuardApprovalStatus("WizardOKedIt"); + assertEquals("WizardOKedIt", clom.getGuardApprovalStatus()); + + assertNull(clom.getOperationResult()); + + Policy policy = manager.getProcessor().getCurrentPolicy(); + clom.getTarget(policy); + + final Target savedTarget = policy.getTarget(); + policy.setTarget(null); + try { + clom.getTarget(policy); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("The target is null", e.getMessage()); + } + + policy.setTarget(new Target()); + try { + clom.getTarget(policy); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("The target type is null", e.getMessage()); + } + + policy.setTarget(savedTarget); + + policy.getTarget().setType(TargetType.PNF); + try { + clom.getTarget(policy); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("PNF target is not supported", e.getMessage()); + } + + onsetEvent.setTarget("Oz"); + onsetEvent.getAAI().remove("generic-vnf.vnf-name"); + onsetEvent.getAAI().remove("generic-vnf.vnf-id"); + onsetEvent.getAAI().remove("vserver.vserver-name"); + + policy.getTarget().setType(TargetType.VNF); + try { + clom.getTarget(policy); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("Target does not match target type", e.getMessage()); + } + + onsetEvent.setTarget("vserver.vserver-name"); + onsetEvent.getAAI().put("vserver.vserver-name", "OzVServer"); + assertEquals("OzVServer", clom.getTarget(policy)); + + onsetEvent.getAAI().remove("vserver.vserver-name"); + onsetEvent.setTarget("generic-vnf.vnf-id"); + onsetEvent.getAAI().put("generic-vnf.vnf-id", "OzVNF"); + assertEquals("OzVNF", clom.getTarget(policy)); + + onsetEvent.setTarget("generic-vnf.vnf-name"); + assertEquals("OzVNF", clom.getTarget(policy)); + + manager.onNewEvent(onsetEvent); + + onsetEvent.getAAI().remove("generic-vnf.vnf-id"); + manager.getVnfResponse(); + clom.getEventManager().getVnfResponse().setVnfID("generic-vnf.vnf-id"); + assertEquals("generic-vnf.vnf-id", clom.getTarget(policy)); + + policy.getTarget().setType(TargetType.VFC); + try { + clom.getTarget(policy); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("The target type is not supported", e.getMessage()); + } + + assertEquals(Integer.valueOf(20), clom.getOperationTimeout()); + + assertEquals("20s", clom.getOperationTimeoutString(100)); + + assertEquals(null, clom.getOperationMessage()); + assertEquals(null, clom.getOperationMessage("The Wizard Escaped")); + + clom.startOperation(onsetEvent); + + assertEquals("actor=SO,operation=Restart,target=Target [type=VFC, resourceID=null],subRequestId=1", + clom.getOperationMessage()); + assertEquals( + "actor=SO,operation=Restart,target=Target [type=VFC, resourceID=null],subRequestId=1, Guard result: " + + "The Wizard Escaped", + clom.getOperationMessage("The Wizard Escaped")); + + assertEquals("actor=SO,operation=Restart,tar", clom.getOperationHistory().substring(0, 30)); + + clom.setOperationHasException("The Wizard is gone"); + clom.setOperationHasGuardDeny(); + } + + @Test + public void testConstructor() throws IOException, ControlLoopException, AAIException { + InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); + final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); + + UUID requestId = UUID.randomUUID(); + VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent(); + onsetEvent.setClosedLoopControlName("TwoOnsetTest"); + onsetEvent.setRequestID(requestId); + onsetEvent.setTarget("generic-vnf.vnf-id"); + onsetEvent.setClosedLoopAlarmStart(Instant.now()); + onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + onsetEvent.setAAI(new HashMap<>()); + onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); + + ControlLoopEventManager manager = + new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID()); + VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent); + assertNotNull(notification); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + + Policy policy = manager.getProcessor().getCurrentPolicy(); + ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager); + assertNotNull(clom); + + policy.setRecipe("ModifyConfig"); + policy.getTarget().setResourceID(UUID.randomUUID().toString()); + try { + new ControlLoopOperationManager(onsetEvent, policy, manager); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("Target vnf-id could not be found", e.getMessage()); + } + + policy.getTarget().setResourceID("82194af1-3c2c-485a-8f44-420e22a9eaa4"); + clom = new ControlLoopOperationManager(onsetEvent, policy, manager); + assertNotNull(clom); + + policy.setActor("SO"); + clom = new ControlLoopOperationManager(onsetEvent, policy, manager); + assertNotNull(clom); + + policy.setActor("VFC"); + clom = new ControlLoopOperationManager(onsetEvent, policy, manager); + assertNotNull(clom); + + policy.setActor("Dorothy"); + try { + new ControlLoopOperationManager(onsetEvent, policy, manager); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("ControlLoopEventManager: policy has an unknown actor.", e.getMessage()); + } + } + + @Test + public void testStartOperation() throws IOException, ControlLoopException, AAIException { + InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); + final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); + + UUID requestId = UUID.randomUUID(); + VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent(); + onsetEvent.setClosedLoopControlName("TwoOnsetTest"); + onsetEvent.setRequestID(requestId); + onsetEvent.setTarget("generic-vnf.vnf-id"); + onsetEvent.setClosedLoopAlarmStart(Instant.now()); + onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + onsetEvent.setAAI(new HashMap<>()); + onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); + + ControlLoopEventManager manager = + new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID()); + VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent); + assertNotNull(notification); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + + Policy policy = manager.getProcessor().getCurrentPolicy(); + ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager); + assertNotNull(clom); + + clom.startOperation(onsetEvent); + + try { + clom.startOperation(onsetEvent); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("current operation is not null (an operation is already running)", e.getMessage()); + } + + clom = new ControlLoopOperationManager(onsetEvent, policy, manager); + assertNotNull(clom); + final String savedRecipe = policy.getRecipe(); + policy.setRecipe("ModifyConfig"); + policy.getTarget().setResourceID(UUID.randomUUID().toString()); + clom.startOperation(onsetEvent); + policy.setRecipe(savedRecipe); + + policy.setRetry(null); + clom = new ControlLoopOperationManager(onsetEvent, policy, manager); + assertNotNull(clom); + clom.startOperation(onsetEvent); + clom.setOperationHasTimedOut(); + assertTrue(clom.isOperationComplete()); + try { + clom.startOperation(onsetEvent); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("current operation failed and retries are not allowed", e.getMessage()); + } + + policy.setRetry(0); + clom = new ControlLoopOperationManager(onsetEvent, policy, manager); + assertNotNull(clom); + clom.startOperation(onsetEvent); + clom.setOperationHasTimedOut(); + assertTrue(clom.isOperationComplete()); + try { + clom.startOperation(onsetEvent); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("current operation failed and retries are not allowed", e.getMessage()); + } + + policy.setRetry(1); + clom = new ControlLoopOperationManager(onsetEvent, policy, manager); + assertNotNull(clom); + clom.startOperation(onsetEvent); + clom.setOperationHasTimedOut(); + clom.startOperation(onsetEvent); + clom.setOperationHasTimedOut(); + assertTrue(clom.isOperationComplete()); + try { + clom.startOperation(onsetEvent); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("current oepration has failed after 2 retries", e.getMessage()); + } + + clom = new ControlLoopOperationManager(onsetEvent, policy, manager); + assertNotNull(clom); + policy.setActor("SO"); + clom.startOperation(onsetEvent); + + clom = new ControlLoopOperationManager(onsetEvent, policy, manager); + assertNotNull(clom); + policy.setActor("VFC"); + clom.startOperation(onsetEvent); + + clom = new ControlLoopOperationManager(onsetEvent, policy, manager); + assertNotNull(clom); + policy.setActor("Oz"); + try { + clom.startOperation(onsetEvent); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("invalid actor Oz on policy", e.getMessage()); + } + } + + @Test + public void testOnResponse() throws IOException, ControlLoopException, AAIException { + InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); + final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); + + UUID requestId = UUID.randomUUID(); + VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent(); + onsetEvent.setClosedLoopControlName("TwoOnsetTest"); + onsetEvent.setRequestID(requestId); + onsetEvent.setTarget("generic-vnf.vnf-id"); + onsetEvent.setClosedLoopAlarmStart(Instant.now()); + onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + onsetEvent.setAAI(new HashMap<>()); + onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); + + ControlLoopEventManager manager = + new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID()); + VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent); + assertNotNull(notification); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + + Policy policy = manager.getProcessor().getCurrentPolicy(); + ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager); + assertNotNull(clom); + + assertNull(clom.onResponse(null)); + + Response appcResponse = new Response(); + CommonHeader commonHeader = new CommonHeader(); + appcResponse.setCommonHeader(commonHeader); + assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse)); + + commonHeader.setSubRequestID("12345"); + appcResponse.setStatus(null); + assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse)); + + ResponseStatus responseStatus = new ResponseStatus(); + appcResponse.setStatus(responseStatus); + assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse)); + + responseStatus.setCode(0); + assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse)); + + responseStatus.setCode(ResponseCode.ACCEPT.getValue()); + assertEquals(null, clom.onResponse(appcResponse)); + + responseStatus.setCode(ResponseCode.ERROR.getValue()); + assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse)); + + responseStatus.setCode(ResponseCode.FAILURE.getValue()); + assertEquals(PolicyResult.FAILURE, clom.onResponse(appcResponse)); + + responseStatus.setCode(ResponseCode.REJECT.getValue()); + assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse)); + + responseStatus.setCode(ResponseCode.SUCCESS.getValue()); + assertEquals(PolicyResult.SUCCESS, clom.onResponse(appcResponse)); + + LCMResponseWrapper lrw = new LCMResponseWrapper(); + LCMResponse body = new LCMResponse(); + LCMCommonHeader lcmCh = new LCMCommonHeader(); + body.setCommonHeader(lcmCh); + lrw.setBody(body); + + lcmCh.setSubRequestId("NotANumber"); + assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(lrw)); + + lcmCh.setSubRequestId("12345"); + assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(lrw)); + + SOResponse soResponse = new SOResponse(); + SOResponseWrapper soRw = new SOResponseWrapper(soResponse, null); + + soResponse.setHttpResponseCode(200); + assertEquals(PolicyResult.SUCCESS, clom.onResponse(soRw)); + + soResponse.setHttpResponseCode(202); + assertEquals(PolicyResult.SUCCESS, clom.onResponse(soRw)); + + soResponse.setHttpResponseCode(500); + assertEquals(PolicyResult.FAILURE, clom.onResponse(soRw)); + + VFCResponse vfcResponse = new VFCResponse(); + VFCResponseDescriptor responseDescriptor = new VFCResponseDescriptor(); + vfcResponse.setResponseDescriptor(responseDescriptor); + + responseDescriptor.setStatus("finished"); + assertEquals(PolicyResult.SUCCESS, clom.onResponse(vfcResponse)); + + responseDescriptor.setStatus("unfinished"); + assertEquals(PolicyResult.FAILURE, clom.onResponse(vfcResponse)); + } + + @Test + public void testCompleteOperation() throws ControlLoopException, AAIException, IOException { + InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); + final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); + + UUID requestId = UUID.randomUUID(); + VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent(); + onsetEvent.setClosedLoopControlName("TwoOnsetTest"); + onsetEvent.setRequestID(requestId); + onsetEvent.setTarget("generic-vnf.vnf-id"); + onsetEvent.setClosedLoopAlarmStart(Instant.now()); + onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + onsetEvent.setAAI(new HashMap<>()); + onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne"); + + ControlLoopEventManager manager = + new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID()); + VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent); + assertNotNull(notification); + assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + + Policy policy = manager.getProcessor().getCurrentPolicy(); + ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager); + assertNotNull(clom); - Policy policy = manager.getProcessor().getCurrentPolicy(); - ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager); - assertNotNull(clom); + clom.startOperation(onsetEvent); - clom.startOperation(onsetEvent); + SOResponse soResponse = new SOResponse(); + final SOResponseWrapper soRw = new SOResponseWrapper(soResponse, null); - SOResponse soResponse = new SOResponse(); - SOResponseWrapper soRW = new SOResponseWrapper(soResponse , null); + PolicyEngine.manager.setEnvironmentProperty("guard.disabled", "false"); + PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.ONAP_KEY_URL, + "http://somewhere.over.the.rainbow"); + PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.ONAP_KEY_USER, "Dorothy"); + PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.ONAP_KEY_PASS, "Toto"); - PolicyEngine.manager.setEnvironmentProperty("guard.disabled", "false"); - PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.ONAP_KEY_URL, "http://somewhere.over.the.rainbow"); - PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.ONAP_KEY_USER, "Dorothy"); - PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.ONAP_KEY_PASS, "Toto"); + assertEquals(PolicyResult.FAILURE, clom.onResponse(soRw)); - assertEquals(PolicyResult.FAILURE, clom.onResponse(soRW)); - - System.setProperty("OperationsHistoryPU", "TestOperationsHistoryPU"); - assertEquals(PolicyResult.FAILURE, clom.onResponse(soRW)); - } + System.setProperty("OperationsHistoryPU", "TestOperationsHistoryPU"); + assertEquals(PolicyResult.FAILURE, clom.onResponse(soRw)); + } } diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/OperationsHistoryDbEntryTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/OperationsHistoryDbEntryTest.java index 315565cf0..db22ce1a6 100644 --- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/OperationsHistoryDbEntryTest.java +++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/OperationsHistoryDbEntryTest.java @@ -20,7 +20,7 @@ package org.onap.policy.controlloop.eventmanager; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; import java.sql.Timestamp; import java.util.UUID; @@ -29,52 +29,52 @@ import org.junit.Test; public class OperationsHistoryDbEntryTest { - @Test - public void testOperationsHistoryDbEntry() { - OperationsHistoryDbEntry entry = new OperationsHistoryDbEntry(); - - String actor = "Dorothy"; - entry.setActor(actor); - assertEquals(actor, entry.getActor()); - - String closedLoopName = "GoToOz"; - entry.setClosedLoopName(closedLoopName); - assertEquals(closedLoopName, entry.getClosedLoopName()); - - Timestamp endtime = new Timestamp(System.currentTimeMillis()); - entry.setEndtime(endtime); - assertEquals(endtime, entry.getEndtime()); - - String message = "I Want to go Home"; - entry.setMessage(message); - assertEquals(message, entry.getMessage()); - - String operation = "Get Home"; - entry.setOperation(operation); - assertEquals(operation, entry.getOperation()); - - String outcome = "Back in Kansas"; - entry.setOutcome(outcome); - assertEquals(outcome, entry.getOutcome()); - - String requestId = UUID.randomUUID().toString(); - entry.setRequestId(requestId); - assertEquals(requestId, entry.getRequestId()); - - long rowid = 12345; - entry.setRowid(rowid); - assertEquals(rowid, entry.getRowid()); - - Timestamp starttime = new Timestamp(endtime.getTime() - 100); - entry.setStarttime(starttime); - assertEquals(starttime, entry.getStarttime()); - - String subrequestId = "12321"; - entry.setSubrequestId(subrequestId); - assertEquals(subrequestId, entry.getSubrequestId()); - - String target = "WizardOfOz"; - entry.setTarget(target); - assertEquals(target, entry.getTarget()); - } + @Test + public void testOperationsHistoryDbEntry() { + OperationsHistoryDbEntry entry = new OperationsHistoryDbEntry(); + + String actor = "Dorothy"; + entry.setActor(actor); + assertEquals(actor, entry.getActor()); + + String closedLoopName = "GoToOz"; + entry.setClosedLoopName(closedLoopName); + assertEquals(closedLoopName, entry.getClosedLoopName()); + + Timestamp endtime = new Timestamp(System.currentTimeMillis()); + entry.setEndtime(endtime); + assertEquals(endtime, entry.getEndtime()); + + String message = "I Want to go Home"; + entry.setMessage(message); + assertEquals(message, entry.getMessage()); + + String operation = "Get Home"; + entry.setOperation(operation); + assertEquals(operation, entry.getOperation()); + + String outcome = "Back in Kansas"; + entry.setOutcome(outcome); + assertEquals(outcome, entry.getOutcome()); + + String requestId = UUID.randomUUID().toString(); + entry.setRequestId(requestId); + assertEquals(requestId, entry.getRequestId()); + + long rowid = 12345; + entry.setRowid(rowid); + assertEquals(rowid, entry.getRowid()); + + Timestamp starttime = new Timestamp(endtime.getTime() - 100); + entry.setStarttime(starttime); + assertEquals(starttime, entry.getStarttime()); + + String subrequestId = "12321"; + entry.setSubrequestId(subrequestId); + assertEquals(subrequestId, entry.getSubrequestId()); + + String target = "WizardOfOz"; + entry.setTarget(target); + assertEquals(target, entry.getTarget()); + } } diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/processor/ControlLoopProcessorTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/processor/ControlLoopProcessorTest.java index 87ccdb5b9..2a6dc7bbd 100644 --- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/processor/ControlLoopProcessorTest.java +++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/processor/ControlLoopProcessorTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * unit test * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,10 @@ package org.onap.policy.controlloop.processor; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; import java.io.File; import java.io.FileInputStream; @@ -30,7 +33,6 @@ import java.nio.charset.StandardCharsets; import org.apache.commons.io.IOUtils; import org.junit.Test; - import org.onap.policy.controlloop.ControlLoopException; import org.onap.policy.controlloop.policy.FinalResult; import org.onap.policy.controlloop.policy.Policy; @@ -39,122 +41,132 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ControlLoopProcessorTest { - private static final Logger logger = LoggerFactory.getLogger(ControlLoopProcessorTest.class); - - @Test - public void testControlLoopProcessor() throws IOException, ControlLoopException { - InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); - String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); - this.testSuccess(yamlString); - this.testFailure(yamlString); - } - - @Test - public void testControlLoopProcessorBadYaml() throws IOException { - InputStream is = new FileInputStream(new File("src/test/resources/string.yaml")); - String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); - - try { - new ControlLoopProcessor(yamlString); - fail("test should thrown an exception"); - } - catch (Exception e) { - assertEquals("Cannot create property=string for JavaBean=ControlLoopPolicy", e.getMessage().substring(0, 60)); - } - } - - @Test - public void testControlLoopProcessorBadTriggerYaml() throws IOException, ControlLoopException { - InputStream is = new FileInputStream(new File("src/test/resources/badtriggerpolicy.yaml")); - String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); - - ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString); - assertNull(clProcessor.getCurrentPolicy()); - - try { - clProcessor.nextPolicyForResult(PolicyResult.SUCCESS); - fail("test shold throw an exception here"); - } - catch (ControlLoopException e) { - assertEquals("There is no current policy to determine where to go to.", e.getMessage()); - } - } - - @Test - public void testControlLoopProcessorNoPolicyYaml() throws IOException, ControlLoopException { - InputStream is = new FileInputStream(new File("src/test/resources/nopolicy.yaml")); - String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); - - ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString); - - try { - clProcessor.getCurrentPolicy(); - fail("test shold throw an exception here"); - } - catch (ControlLoopException e) { - assertEquals("There are no policies defined.", e.getMessage()); - } - } - - @Test - public void testControlLoopProcessorNextPolicyForResult() throws IOException, ControlLoopException { - InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); - String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); - - ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString); - clProcessor.getCurrentPolicy(); - clProcessor.nextPolicyForResult(PolicyResult.SUCCESS); - - clProcessor = new ControlLoopProcessor(yamlString); - clProcessor.getCurrentPolicy(); - clProcessor.nextPolicyForResult(PolicyResult.FAILURE); - - clProcessor = new ControlLoopProcessor(yamlString); - clProcessor.getCurrentPolicy(); - clProcessor.nextPolicyForResult(PolicyResult.FAILURE_EXCEPTION); - - clProcessor = new ControlLoopProcessor(yamlString); - clProcessor.getCurrentPolicy(); - clProcessor.nextPolicyForResult(PolicyResult.FAILURE_GUARD); - - clProcessor = new ControlLoopProcessor(yamlString); - clProcessor.getCurrentPolicy(); - clProcessor.nextPolicyForResult(PolicyResult.FAILURE_RETRIES); - - clProcessor = new ControlLoopProcessor(yamlString); - clProcessor.getCurrentPolicy(); - clProcessor.nextPolicyForResult(PolicyResult.FAILURE_TIMEOUT); - } - - public void testSuccess(String yaml) throws ControlLoopException { - ControlLoopProcessor processor = new ControlLoopProcessor(yaml); - logger.debug("testSuccess: {}", processor.getControlLoop()); - while (true) { - FinalResult result = processor.checkIsCurrentPolicyFinal(); - if (result != null) { - logger.debug("{}", result); - break; - } - Policy policy = processor.getCurrentPolicy(); - assertNotNull(policy); - logger.debug("current policy is: {} {}", policy.getId(), policy.getName()); - processor.nextPolicyForResult(PolicyResult.SUCCESS); - } - } - - public void testFailure(String yaml) throws ControlLoopException { - ControlLoopProcessor processor = new ControlLoopProcessor(yaml); - logger.debug("testFailure: {}", processor.getControlLoop()); - while (true) { - FinalResult result = processor.checkIsCurrentPolicyFinal(); - if (result != null) { - logger.debug("{}", result); - break; - } - Policy policy = processor.getCurrentPolicy(); - assertNotNull(policy); - logger.debug("current policy is: {} {}", policy.getId(), policy.getName()); - processor.nextPolicyForResult(PolicyResult.FAILURE); - } - } + private static final Logger logger = LoggerFactory.getLogger(ControlLoopProcessorTest.class); + + @Test + public void testControlLoopProcessor() throws IOException, ControlLoopException { + InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); + String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); + this.testSuccess(yamlString); + this.testFailure(yamlString); + } + + @Test + public void testControlLoopProcessorBadYaml() throws IOException { + InputStream is = new FileInputStream(new File("src/test/resources/string.yaml")); + String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); + + try { + new ControlLoopProcessor(yamlString); + fail("test should thrown an exception"); + } catch (Exception e) { + assertEquals("Cannot create property=string for JavaBean=ControlLoopPolicy", + e.getMessage().substring(0, 60)); + } + } + + @Test + public void testControlLoopProcessorBadTriggerYaml() throws IOException, ControlLoopException { + InputStream is = new FileInputStream(new File("src/test/resources/badtriggerpolicy.yaml")); + String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); + + ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString); + assertNull(clProcessor.getCurrentPolicy()); + + try { + clProcessor.nextPolicyForResult(PolicyResult.SUCCESS); + fail("test shold throw an exception here"); + } catch (ControlLoopException e) { + assertEquals("There is no current policy to determine where to go to.", e.getMessage()); + } + } + + @Test + public void testControlLoopProcessorNoPolicyYaml() throws IOException, ControlLoopException { + InputStream is = new FileInputStream(new File("src/test/resources/nopolicy.yaml")); + String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); + + ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString); + + try { + clProcessor.getCurrentPolicy(); + fail("test shold throw an exception here"); + } catch (ControlLoopException e) { + assertEquals("There are no policies defined.", e.getMessage()); + } + } + + @Test + public void testControlLoopProcessorNextPolicyForResult() throws IOException, ControlLoopException { + InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")); + String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8); + + ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString); + clProcessor.getCurrentPolicy(); + clProcessor.nextPolicyForResult(PolicyResult.SUCCESS); + + clProcessor = new ControlLoopProcessor(yamlString); + clProcessor.getCurrentPolicy(); + clProcessor.nextPolicyForResult(PolicyResult.FAILURE); + + clProcessor = new ControlLoopProcessor(yamlString); + clProcessor.getCurrentPolicy(); + clProcessor.nextPolicyForResult(PolicyResult.FAILURE_EXCEPTION); + + clProcessor = new ControlLoopProcessor(yamlString); + clProcessor.getCurrentPolicy(); + clProcessor.nextPolicyForResult(PolicyResult.FAILURE_GUARD); + + clProcessor = new ControlLoopProcessor(yamlString); + clProcessor.getCurrentPolicy(); + clProcessor.nextPolicyForResult(PolicyResult.FAILURE_RETRIES); + + clProcessor = new ControlLoopProcessor(yamlString); + clProcessor.getCurrentPolicy(); + clProcessor.nextPolicyForResult(PolicyResult.FAILURE_TIMEOUT); + } + + /** + * Test policies in the given yaml following the successfull path. + * + * @param yaml yaml containing the policies to test + * @throws ControlLoopException if an error occurs + */ + public void testSuccess(String yaml) throws ControlLoopException { + ControlLoopProcessor processor = new ControlLoopProcessor(yaml); + logger.debug("testSuccess: {}", processor.getControlLoop()); + while (true) { + FinalResult result = processor.checkIsCurrentPolicyFinal(); + if (result != null) { + logger.debug("{}", result); + break; + } + Policy policy = processor.getCurrentPolicy(); + assertNotNull(policy); + logger.debug("current policy is: {} {}", policy.getId(), policy.getName()); + processor.nextPolicyForResult(PolicyResult.SUCCESS); + } + } + + /** + * Test policies in the given yaml following the failure path. + * + * @param yaml yaml containing the policies to test + * @throws ControlLoopException if an error occurs + */ + public void testFailure(String yaml) throws ControlLoopException { + ControlLoopProcessor processor = new ControlLoopProcessor(yaml); + logger.debug("testFailure: {}", processor.getControlLoop()); + while (true) { + FinalResult result = processor.checkIsCurrentPolicyFinal(); + if (result != null) { + logger.debug("{}", result); + break; + } + Policy policy = processor.getCurrentPolicy(); + assertNotNull(policy); + logger.debug("current policy is: {} {}", policy.getId(), policy.getName()); + processor.nextPolicyForResult(PolicyResult.FAILURE); + } + } } diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/drools/DroolsPolicyEngineTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/drools/DroolsPolicyEngineTest.java index d94230e5e..13d5f3679 100644 --- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/drools/DroolsPolicyEngineTest.java +++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/drools/DroolsPolicyEngineTest.java @@ -34,40 +34,40 @@ import org.onap.policy.controlloop.VirtualControlLoopNotification; import org.onap.policy.drools.impl.PolicyEngineJUnitImpl; public class DroolsPolicyEngineTest { - @Test - public void testDroolsPolicyEngine() { - PolicyEngineJUnitImpl pe = new PolicyEngineJUnitImpl(); - assertNotNull(pe); - - pe.addListener(new TestPolicyEngineListener()); - pe.notifyListeners("TheWizardOfOz"); - - pe.subscribe("OmniBus", "TheWizardOfOz"); + @Test + public void testDroolsPolicyEngine() { + PolicyEngineJUnitImpl pe = new PolicyEngineJUnitImpl(); + assertNotNull(pe); - pe.deliver("OmniBus", "TheWizardOfOz", "Dorothy"); - - pe.subscribe("OmniBus", "TheWizardOfOz"); - pe.subscribe("OmniBus", "ThisTopicDoesNotExist"); + pe.addListener(new TestPolicyEngineListener()); + pe.notifyListeners("TheWizardOfOz"); - ControlLoopNotification notification = new VirtualControlLoopNotification(); - pe.deliver("OmniBus", "TheWizardOfOz", notification); - - Request request = new Request(); - request.setCommonHeader(new CommonHeader()); - request.getCommonHeader().setSubRequestID("12321"); - pe.deliver("OmniBus", "TheWizardOfOz", request); - - LCMRequestWrapper lcmRW = new LCMRequestWrapper(); - lcmRW.setBody(new LCMRequest()); - lcmRW.getBody().setCommonHeader(new LCMCommonHeader()); - lcmRW.getBody().getCommonHeader().setSubRequestId("54321"); - pe.deliver("OmniBus", "TheWizardOfOz", lcmRW); - } - - private class TestPolicyEngineListener implements PolicyEngineListener { - @Override - public void newEventNotification(String topic) { - assertEquals("TheWizardOfOz", topic); - } - } + pe.subscribe("OmniBus", "TheWizardOfOz"); + + pe.deliver("OmniBus", "TheWizardOfOz", "Dorothy"); + + pe.subscribe("OmniBus", "TheWizardOfOz"); + pe.subscribe("OmniBus", "ThisTopicDoesNotExist"); + + ControlLoopNotification notification = new VirtualControlLoopNotification(); + pe.deliver("OmniBus", "TheWizardOfOz", notification); + + Request request = new Request(); + request.setCommonHeader(new CommonHeader()); + request.getCommonHeader().setSubRequestID("12321"); + pe.deliver("OmniBus", "TheWizardOfOz", request); + + LCMRequestWrapper lcmRw = new LCMRequestWrapper(); + lcmRw.setBody(new LCMRequest()); + lcmRw.getBody().setCommonHeader(new LCMCommonHeader()); + lcmRw.getBody().getCommonHeader().setSubRequestId("54321"); + pe.deliver("OmniBus", "TheWizardOfOz", lcmRw); + } + + private class TestPolicyEngineListener implements PolicyEngineListener { + @Override + public void newEventNotification(String topic) { + assertEquals("TheWizardOfOz", topic); + } + } } -- cgit 1.2.3-korg