From a9b2b1d16797fefa4c7be8ba0bf01f0fdab22b7b Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 15 Aug 2018 16:34:02 -0400 Subject: add prov-status check to AAI queries Also had to add prov-status field to AaiGetVnfResponse class. Removed bogus comment from test. Verify response is not null in test. Verify no requery by vnf name in test. Add prov-status to template.demo tests. Add prov-status to sample json for archetype-cl's. Change-Id: I3f36a2bb6de8265ce5b10103690d6d8813c15b1a Issue-ID: POLICY-964 Signed-off-by: Jim Hahn --- controlloop/common/eventmanager/pom.xml | 8 +- .../eventmanager/ControlLoopEventManager.java | 115 +++-- .../eventmanager/ControlLoopEventManagerTest.java | 496 ++++++++++++++------- 3 files changed, 435 insertions(+), 184 deletions(-) (limited to 'controlloop/common/eventmanager') diff --git a/controlloop/common/eventmanager/pom.xml b/controlloop/common/eventmanager/pom.xml index 1f35d1c41..d1fd4e4e0 100644 --- a/controlloop/common/eventmanager/pom.xml +++ b/controlloop/common/eventmanager/pom.xml @@ -2,7 +2,7 @@ ============LICENSE_START======================================================= eventmanager ================================================================================ - 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. @@ -130,6 +130,12 @@ 4.12 test + + org.powermock + powermock-api-mockito + 1.7.3 + test + com.google.code.gson gson diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java index c4affac48..ff1053482 100644 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java +++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java @@ -27,6 +27,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.UUID; import org.onap.policy.aai.AaiGetVnfResponse; @@ -54,13 +55,16 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ControlLoopEventManager implements LockCallback, Serializable { + public static final String PROV_STATUS_ACTIVE = "ACTIVE"; private static final String VM_NAME = "VM_NAME"; private static final String VNF_NAME = "VNF_NAME"; - private static final String GENERIC_VNF_VNF_ID = "generic-vnf.vnf-id"; - private static final String GENERIC_VNF_VNF_NAME = "generic-vnf.vnf-name"; - private static final String VSERVER_VSERVER_NAME = "vserver.vserver-name"; - private static final String GENERIC_VNF_IS_CLOSED_LOOP_DISABLED = "generic-vnf.is-closed-loop-disabled"; - private static final String VSERVER_IS_CLOSED_LOOP_DISABLED = "vserver.is-closed-loop-disabled"; + public static final String GENERIC_VNF_VNF_ID = "generic-vnf.vnf-id"; + public static final String GENERIC_VNF_VNF_NAME = "generic-vnf.vnf-name"; + public static final String VSERVER_VSERVER_NAME = "vserver.vserver-name"; + public static final String GENERIC_VNF_IS_CLOSED_LOOP_DISABLED = "generic-vnf.is-closed-loop-disabled"; + public static final String VSERVER_IS_CLOSED_LOOP_DISABLED = "vserver.is-closed-loop-disabled"; + public static final String GENERIC_VNF_PROV_STATUS = "generic-vnf.prov-status"; + public static final String VSERVER_PROV_STATUS = "vserver.prov-status"; /** * Additional time, in seconds, to add to a "lock" request. This ensures that the lock @@ -661,31 +665,41 @@ public class ControlLoopEventManager implements LockCallback, Serializable { * @param event the event * @throws AaiException if an error occurs retrieving information from A&AI */ - public void queryAai(VirtualControlLoopEvent event) throws AaiException { - if (event.getAai().get(VSERVER_IS_CLOSED_LOOP_DISABLED) != null - || event.getAai().get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED) != null) { - - if (isClosedLoopDisabled(event)) { - throw new AaiException("is-closed-loop-disabled is set to true on VServer or VNF"); - } - + public void queryAai(VirtualControlLoopEvent event) throws AaiException { + + if (isClosedLoopDisabled(event)) { + throw new AaiException("is-closed-loop-disabled is set to true on VServer or VNF"); + } + + if (isProvStatusInactive(event)) { + throw new AaiException("prov-status is not ACTIVE on VServer or VNF"); + } + + Map aai = event.getAai(); + + if ((aai.containsKey(VSERVER_IS_CLOSED_LOOP_DISABLED) || aai.containsKey(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED)) + && (aai.containsKey(VSERVER_PROV_STATUS) || aai.containsKey(GENERIC_VNF_PROV_STATUS))) { + // no need to query, as we already have the data return; } - + if (vnfResponse != null || vserverResponse != null) { // query has already been performed return; } try { - if (event.getAai().get(GENERIC_VNF_VNF_ID) != null || event.getAai().get(GENERIC_VNF_VNF_NAME) != null) { + if (aai.containsKey(GENERIC_VNF_VNF_ID) || aai.containsKey(GENERIC_VNF_VNF_NAME)) { vnfResponse = getAAIVnfInfo(event); - processVNFResponse(vnfResponse, event.getAai().get(GENERIC_VNF_VNF_ID) != null); - } else if (event.getAai().get(VSERVER_VSERVER_NAME) != null) { + processVNFResponse(vnfResponse, aai.containsKey(GENERIC_VNF_VNF_ID)); + } else if (aai.containsKey(VSERVER_VSERVER_NAME)) { vserverResponse = getAAIVserverInfo(event); processVServerResponse(vserverResponse); } + } catch (AaiException e) { + logger.error("Exception from queryAai: ", e); + throw e; } catch (Exception e) { logger.error("Exception from queryAai: ", e); throw new AaiException("Exception from queryAai: " + e.toString()); @@ -696,8 +710,8 @@ public class ControlLoopEventManager implements LockCallback, Serializable { * Process a response from A&AI for a VNF. * * @param aaiResponse the response from A&AI - * @param queryByVnfId true if the query was based on vnf-id, false if - * the query was based on vnf-name + * @param queryByVnfId true if the query was based on vnf-id, + * false if the query was based on vnf-name * @throws AaiException if an error occurs processing the response */ private static void processVNFResponse(AaiGetVnfResponse aaiResponse, boolean queryByVNFID) throws AaiException { @@ -713,44 +727,77 @@ public class ControlLoopEventManager implements LockCallback, Serializable { if (aaiResponse.getIsClosedLoopDisabled()) { throw new AaiException("is-closed-loop-disabled is set to true (query by " + queryTypeString + ")"); } + + if (!PROV_STATUS_ACTIVE.equals(aaiResponse.getProvStatus())) { + throw new AaiException("prov-status is not ACTIVE (query by " + queryTypeString + ")"); + } } + /** + * Process a response from A&AI for a VServer. + * + * @param aaiResponse the response from A&AI + * @throws AaiException if an error occurs processing the response + */ private static void processVServerResponse(AaiGetVserverResponse aaiResponse) throws AaiException { if (aaiResponse == null) { throw new AaiException("AAI Response is null (query by vserver-name)"); } if (aaiResponse.getRequestError() != null) { - throw new AaiException("AAI responded with a request error (query by vserver-name)"); + throw new AaiException("AAI Responded with a request error (query by vserver-name)"); } - + List lst = aaiResponse.getVserver(); - if(lst.isEmpty()) { + if (lst.isEmpty()) { return; } - + AaiNqVServer svr = lst.get(0); if (svr.getIsClosedLoopDisabled()) { throw new AaiException("is-closed-loop-disabled is set to true (query by vserver-name)"); } + + if (!PROV_STATUS_ACTIVE.equals(svr.getProvStatus())) { + throw new AaiException("prov-status is not ACTIVE (query by vserver-name)"); + } } /** * Is closed loop disabled for an event. * * @param event the event - * @return true if the contol loop is disabled, false otherwise + * @return true if the control loop is disabled, false + * otherwise */ public static boolean isClosedLoopDisabled(VirtualControlLoopEvent event) { - if ("true".equalsIgnoreCase(event.getAai().get(VSERVER_IS_CLOSED_LOOP_DISABLED)) - || "T".equalsIgnoreCase(event.getAai().get(VSERVER_IS_CLOSED_LOOP_DISABLED)) - || "yes".equalsIgnoreCase(event.getAai().get(VSERVER_IS_CLOSED_LOOP_DISABLED)) - || "Y".equalsIgnoreCase(event.getAai().get(VSERVER_IS_CLOSED_LOOP_DISABLED))) { - return true; - } - return ("true".equalsIgnoreCase(event.getAai().get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED)) - || "T".equalsIgnoreCase(event.getAai().get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED)) - || "yes".equalsIgnoreCase(event.getAai().get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED)) - || "Y".equalsIgnoreCase(event.getAai().get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED))); + Map aai = event.getAai(); + return (isAaiTrue(aai.get(VSERVER_IS_CLOSED_LOOP_DISABLED)) + || isAaiTrue(aai.get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED))); + } + + /** + * Does provisioning status, for an event, have a value other than ACTIVE? + * + * @param event the event + * @return {@code true} if the provisioning status is neither ACTIVE nor {@code null}, + * {@code false} otherwise + */ + protected static boolean isProvStatusInactive(VirtualControlLoopEvent event) { + Map aai = event.getAai(); + return (!PROV_STATUS_ACTIVE.equals(aai.getOrDefault(VSERVER_PROV_STATUS, PROV_STATUS_ACTIVE)) + || !PROV_STATUS_ACTIVE.equals(aai.getOrDefault(GENERIC_VNF_PROV_STATUS, PROV_STATUS_ACTIVE))); + } + + /** + * Determines the boolean value represented by the given AAI field value. + * + * @param aaiValue value to be examined + * @return the boolean value represented by the field value, or {@code false} if the + * value is {@code null} + */ + protected static boolean isAaiTrue(String aaiValue) { + return ("true".equalsIgnoreCase(aaiValue) || "T".equalsIgnoreCase(aaiValue) || "yes".equalsIgnoreCase(aaiValue) + || "Y".equalsIgnoreCase(aaiValue)); } /** 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 37558474a..1cef78a9e 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 @@ -21,6 +21,7 @@ package org.onap.policy.controlloop.eventmanager; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -32,14 +33,19 @@ import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.time.Instant; import java.util.HashMap; +import java.util.Map; import java.util.UUID; import org.apache.commons.io.IOUtils; import org.junit.AfterClass; +import org.junit.Before; import org.junit.BeforeClass; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.onap.policy.aai.AaiGetVnfResponse; import org.onap.policy.aai.AaiGetVserverResponse; import org.onap.policy.aai.AaiNqVServer; +import org.onap.policy.aai.AaiNqRequestError; import org.onap.policy.aai.RelatedToProperty; import org.onap.policy.aai.Relationship; import org.onap.policy.aai.RelationshipData; @@ -60,26 +66,17 @@ import org.onap.policy.guard.GuardResult; import org.onap.policy.guard.PolicyGuard; import org.onap.policy.guard.PolicyGuard.LockResult; import org.onap.policy.guard.TargetLock; +import org.powermock.reflect.Whitebox; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ControlLoopEventManagerTest { private static final Logger logger = LoggerFactory.getLogger(ControlLoopEventManagerTest.class); + + @Rule + public ExpectedException thrown = ExpectedException.none(); - 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); - } + private VirtualControlLoopEvent onset; /** * Set up test class. @@ -100,6 +97,22 @@ public class ControlLoopEventManagerTest { public static void tearDownSimulator() { HttpServletServer.factory.destroy(); } + + @Before + public void setUp() { + 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); + + PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666"); + } @Test public void testAaiVnfInfo() { @@ -157,53 +170,7 @@ public class ControlLoopEventManagerTest { } @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() { + public void abatementCheckEventSyntaxTest() { VirtualControlLoopEvent event = new VirtualControlLoopEvent(); event.setClosedLoopControlName("abatementAAI"); event.setRequestId(UUID.randomUUID()); @@ -925,119 +892,350 @@ public class ControlLoopEventManagerTest { } @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); + public void testQueryAai_AlreadyDisabled() throws AaiException { + ControlLoopEventManager mgr = null; + + try { + onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.TRUE.toString()); + onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, + ControlLoopEventManager.PROV_STATUS_ACTIVE); - 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"); + mgr = makeManager(onset); + mgr.queryAai(onset); - ControlLoopEventManager manager = makeManager(onsetEvent); - manager.queryAai(onsetEvent); + fail("missing exception"); - VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent); - assertNotNull(notification); - assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification()); + } catch (AaiException expected) { + assertEquals("is-closed-loop-disabled is set to true on VServer or VNF", expected.getMessage()); + assertNull(mgr.getVnfResponse()); + assertNull(mgr.getVserverResponse()); + } + } - // repeat query with same manager - manager.queryAai(onsetEvent); + @Test + public void testQueryAai_AlreadyInactive() throws AaiException { + ControlLoopEventManager mgr = null; + + try { + onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.FALSE.toString()); + onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, "not-active2"); - // remaining queries each use their own manager so they will be re-executed + mgr = makeManager(onset); + mgr.queryAai(onset); - makeManager(onsetEvent).queryAai(onsetEvent); + fail("missing exception"); - onsetEvent.getAai().put("generic-vnf.is-closed-loop-disabled", "true"); - try { - makeManager(onsetEvent).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()); + } catch (AaiException expected) { + assertEquals("prov-status is not ACTIVE on VServer or VNF", expected.getMessage()); + assertNull(mgr.getVnfResponse()); + assertNull(mgr.getVserverResponse()); } - onsetEvent.getAai().put("vserver.is-closed-loop-disabled", "true"); + } + + @Test + public void testQueryAai_QueryVnfById() throws AaiException { + ControlLoopEventManager mgr = null; + + mgr = makeManager(onset); + mgr.queryAai(onset); + + assertNotNull(mgr.getVnfResponse()); + assertNull(mgr.getVserverResponse()); + + AaiGetVnfResponse vnfresp = mgr.getVnfResponse(); + + // should not re-query + mgr.queryAai(onset); + + assertEquals(vnfresp, mgr.getVnfResponse()); + assertNull(mgr.getVserverResponse()); + } + + @Test + public void testQueryAai_QueryVnfByName() throws AaiException { + ControlLoopEventManager mgr = null; + + // vnf query by name + onset.getAai().remove(ControlLoopEventManager.GENERIC_VNF_VNF_ID); + onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_VNF_NAME, "AVNFName"); + + mgr = makeManager(onset); + mgr.queryAai(onset); + + assertNotNull(mgr.getVnfResponse()); + assertNull(mgr.getVserverResponse()); + + AaiGetVnfResponse vnfresp = mgr.getVnfResponse(); + + // should not re-query + mgr.queryAai(onset); + + assertEquals(vnfresp, mgr.getVnfResponse()); + assertNull(mgr.getVserverResponse()); + } + + @Test + public void testQueryAai_QueryVnfById_Disabled() throws AaiException { + ControlLoopEventManager mgr = null; + try { - makeManager(onsetEvent).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()); + onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_VNF_ID, "disableClosedLoop"); + + mgr = makeManager(onset); + mgr.queryAai(onset); + + fail("missing exception"); + + } catch (AaiException expected) { + assertEquals("is-closed-loop-disabled is set to true (query by vnf-id)", expected.getMessage()); + + assertNotNull(mgr.getVnfResponse()); + assertNull(mgr.getVserverResponse()); } - onsetEvent.getAai().remove("generic-vnf.is-closed-loop-disabled"); + } + + @Test + public void testQueryAai_QueryVserver() throws AaiException { + ControlLoopEventManager mgr = null; + + onset.getAai().remove(ControlLoopEventManager.GENERIC_VNF_VNF_ID); + onset.getAai().put(ControlLoopEventManager.VSERVER_VSERVER_NAME, "AVserver"); + + mgr = makeManager(onset); + mgr.queryAai(onset); + + assertNull(mgr.getVnfResponse()); + assertNotNull(mgr.getVserverResponse()); + + AaiGetVserverResponse vsvresp = mgr.getVserverResponse(); + + // should not re-query + mgr.queryAai(onset); + + assertNull(mgr.getVnfResponse()); + assertEquals(vsvresp, mgr.getVserverResponse()); + } + + @Test + public void testQueryAai_QueryVserver_Disabled() throws AaiException { + ControlLoopEventManager mgr = null; + try { - makeManager(onsetEvent).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()); + onset.getAai().remove(ControlLoopEventManager.GENERIC_VNF_VNF_ID); + onset.getAai().put(ControlLoopEventManager.VSERVER_VSERVER_NAME, "disableClosedLoop"); + + mgr = makeManager(onset); + mgr.queryAai(onset); + + fail("missing exception"); + + } catch (AaiException expected) { + assertEquals("is-closed-loop-disabled is set to true (query by vserver-name)", expected.getMessage()); + + assertNull(mgr.getVnfResponse()); + assertNotNull(mgr.getVserverResponse()); } - onsetEvent.getAai().remove("vserver.is-closed-loop-disabled"); - makeManager(onsetEvent).queryAai(onsetEvent); + } - onsetEvent.getAai().put("generic-vnf.is-closed-loop-disabled", "false"); - makeManager(onsetEvent).queryAai(onsetEvent); + @Test(expected = AaiException.class) + public void testQueryAai_QueryException() throws AaiException { + // Force AAI errors + PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:9999"); + + makeManager(onset).queryAai(onset); + } - onsetEvent.getAai().remove("generic-vnf.is-closed-loop-disabled"); - onsetEvent.getAai().put("vserver.is-closed-loop-disabled", "false"); - makeManager(onsetEvent).queryAai(onsetEvent); + @Test + public void testProcessVNFResponse_Success() throws Exception { + AaiGetVnfResponse resp = new AaiGetVnfResponse(); + resp.setIsClosedLoopDisabled(false); + resp.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE); + Whitebox.invokeMethod(ControlLoopEventManager.class, "processVNFResponse", resp, true); + } - onsetEvent.getAai().remove("generic-vnf.vnf-id"); - onsetEvent.getAai().remove("generic-vnf.vnf-name"); - onsetEvent.getAai().remove("vserver.vserver-name"); - onsetEvent.getAai().remove("generic-vnf.is-closed-loop-disabled"); - onsetEvent.getAai().remove("vserver.is-closed-loop-disabled"); - makeManager(onsetEvent).queryAai(onsetEvent); + @Test + public void testProcessVNFResponse_NullResponse() throws Exception { + thrown.expect(AaiException.class); + thrown.expectMessage("AAI Response is null (query by vnf-id)"); - onsetEvent.getAai().put("vserver.vserver-name", "AVserver"); - makeManager(onsetEvent).queryAai(onsetEvent); + AaiGetVnfResponse resp = null; + Whitebox.invokeMethod(ControlLoopEventManager.class, "processVNFResponse", resp, true); + } - onsetEvent.getAai().put("generic-vnf.vnf-name", "AVNFName"); - makeManager(onsetEvent).queryAai(onsetEvent); + @Test + public void testProcessVNFResponse_Error() throws Exception { + thrown.expect(AaiException.class); + thrown.expectMessage("AAI Responded with a request error (query by vnf-name)"); + + AaiGetVnfResponse resp = new AaiGetVnfResponse(); + + resp.setRequestError(new AaiNqRequestError()); + + resp.setIsClosedLoopDisabled(false); + resp.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE); + Whitebox.invokeMethod(ControlLoopEventManager.class, "processVNFResponse", resp, false); + } - onsetEvent.getAai().put("generic-vnf.vnf-id", "AVNFID"); - makeManager(onsetEvent).queryAai(onsetEvent); + @Test + public void testProcessVNFResponse_Disabled() throws Exception { + thrown.expect(AaiException.class); + thrown.expectMessage("is-closed-loop-disabled is set to true (query by vnf-id)"); + + AaiGetVnfResponse resp = new AaiGetVnfResponse(); + resp.setIsClosedLoopDisabled(true); + resp.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE); + Whitebox.invokeMethod(ControlLoopEventManager.class, "processVNFResponse", resp, true); + } - onsetEvent.getAai().remove("vserver.vserver-name"); - makeManager(onsetEvent).queryAai(onsetEvent); + @Test + public void testProcessVNFResponse_Inactive() throws Exception { + thrown.expect(AaiException.class); + thrown.expectMessage("prov-status is not ACTIVE (query by vnf-name)"); + + AaiGetVnfResponse resp = new AaiGetVnfResponse(); + resp.setIsClosedLoopDisabled(false); + resp.setProvStatus("inactive1"); + Whitebox.invokeMethod(ControlLoopEventManager.class, "processVNFResponse", resp, false); + } - onsetEvent.getAai().remove("generic-vnf.vnf-name"); - makeManager(onsetEvent).queryAai(onsetEvent); + @Test + public void testProcessVserverResponse_Success() throws Exception { + AaiGetVserverResponse resp = new AaiGetVserverResponse(); + + AaiNqVServer svr = new AaiNqVServer(); + resp.getVserver().add(svr); + + svr.setIsClosedLoopDisabled(false); + svr.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE); + Whitebox.invokeMethod(ControlLoopEventManager.class, "processVServerResponse", resp); + } - // Force AAI errors - PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:9999"); + @Test + public void testProcessVserverResponse_NullResponse() throws Exception { + thrown.expect(AaiException.class); + thrown.expectMessage("AAI Response is null (query by vserver-name)"); - try { - makeManager(onsetEvent).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()); - } + AaiGetVserverResponse resp = null; + Whitebox.invokeMethod(ControlLoopEventManager.class, "processVServerResponse", resp); + } - onsetEvent.getAai().remove("generic-vnf.vnf-id"); - onsetEvent.getAai().put("generic-vnf.vnf-name", "AVNFName"); - try { - makeManager(onsetEvent).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()); - } + @Test + public void testProcessVserverResponse_Error() throws Exception { + thrown.expect(AaiException.class); + thrown.expectMessage("AAI Responded with a request error (query by vserver-name)"); - onsetEvent.getAai().remove("generic-vnf.vnf-name"); - onsetEvent.getAai().put("vserver.vserver-name", "AVserver"); - try { - makeManager(onsetEvent).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()); - } + AaiGetVserverResponse resp = new AaiGetVserverResponse(); + + resp.setRequestError(new AaiNqRequestError()); + + AaiNqVServer svr = new AaiNqVServer(); + resp.getVserver().add(svr); + + svr.setIsClosedLoopDisabled(false); + svr.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE); + + Whitebox.invokeMethod(ControlLoopEventManager.class, "processVServerResponse", resp); + } - PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666"); + @Test + public void testProcessVserverResponse_Disabled() throws Exception { + thrown.expect(AaiException.class); + thrown.expectMessage("is-closed-loop-disabled is set to true (query by vserver-name)"); + + AaiGetVserverResponse resp = new AaiGetVserverResponse(); + AaiNqVServer svr = new AaiNqVServer(); + resp.getVserver().add(svr); + + svr.setIsClosedLoopDisabled(true); + svr.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE); + Whitebox.invokeMethod(ControlLoopEventManager.class, "processVServerResponse", resp); + } + + @Test + public void testProcessVserverResponse_Inactive() throws Exception { + thrown.expect(AaiException.class); + thrown.expectMessage("prov-status is not ACTIVE (query by vserver-name)"); + + AaiGetVserverResponse resp = new AaiGetVserverResponse(); + AaiNqVServer svr = new AaiNqVServer(); + resp.getVserver().add(svr); + + svr.setIsClosedLoopDisabled(false); + svr.setProvStatus("inactive1"); + Whitebox.invokeMethod(ControlLoopEventManager.class, "processVServerResponse", resp); + } + + @Test + public void testIsClosedLoopDisabled() { + Map aai = onset.getAai(); + + // null, null + aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED); + aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED); + assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset)); + + // null, false + aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED); + aai.put(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED, Boolean.FALSE.toString()); + assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset)); + + // false, null + aai.put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.FALSE.toString()); + aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED); + assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset)); + + // null, true + aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED); + aai.put(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED, Boolean.TRUE.toString()); + assertTrue(ControlLoopEventManager.isClosedLoopDisabled(onset)); + + // true, null + aai.put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.TRUE.toString()); + aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED); + assertTrue(ControlLoopEventManager.isClosedLoopDisabled(onset)); + } + + @Test + public void testIsProvStatusInactive() { + Map aai = onset.getAai(); + + // null, null + aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS); + aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS); + assertFalse(ControlLoopEventManager.isProvStatusInactive(onset)); + + // null, active + aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS); + aai.put(ControlLoopEventManager.VSERVER_PROV_STATUS, ControlLoopEventManager.PROV_STATUS_ACTIVE); + assertFalse(ControlLoopEventManager.isProvStatusInactive(onset)); + + // active, null + aai.put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, ControlLoopEventManager.PROV_STATUS_ACTIVE); + aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS); + assertFalse(ControlLoopEventManager.isProvStatusInactive(onset)); + + // null, inactive + aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS); + aai.put(ControlLoopEventManager.VSERVER_PROV_STATUS, "other1"); + assertTrue(ControlLoopEventManager.isProvStatusInactive(onset)); + + // inactive, null + aai.put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, "other2"); + aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS); + assertTrue(ControlLoopEventManager.isProvStatusInactive(onset)); + } + + @Test + public void testIsAaiTrue() { + assertTrue(ControlLoopEventManager.isAaiTrue("tRuE")); + assertTrue(ControlLoopEventManager.isAaiTrue("T")); + assertTrue(ControlLoopEventManager.isAaiTrue("t")); + assertTrue(ControlLoopEventManager.isAaiTrue("yES")); + assertTrue(ControlLoopEventManager.isAaiTrue("Y")); + assertTrue(ControlLoopEventManager.isAaiTrue("y")); + + assertFalse(ControlLoopEventManager.isAaiTrue("no")); + assertFalse(ControlLoopEventManager.isAaiTrue(null)); } private ControlLoopEventManager makeManager(VirtualControlLoopEvent event) { -- cgit 1.2.3-korg