diff options
Diffstat (limited to 'controlloop/common/eventmanager/src')
3 files changed, 182 insertions, 54 deletions
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 bd7f14e6f..bb349591e 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 @@ -25,6 +25,7 @@ import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -33,6 +34,12 @@ import java.util.UUID; import org.onap.policy.aai.AaiGetVnfResponse; import org.onap.policy.aai.AaiGetVserverResponse; import org.onap.policy.aai.AaiManager; +import org.onap.policy.aai.AaiNqInstanceFilters; +import org.onap.policy.aai.AaiNqNamedQuery; +import org.onap.policy.aai.AaiNqQueryParameters; +import org.onap.policy.aai.AaiNqRequest; +import org.onap.policy.aai.AaiNqResponse; +import org.onap.policy.aai.AaiNqResponseWrapper; import org.onap.policy.aai.AaiNqVServer; import org.onap.policy.aai.util.AaiException; import org.onap.policy.controlloop.ControlLoopEventStatus; @@ -51,6 +58,7 @@ import org.onap.policy.guard.PolicyGuard; import org.onap.policy.guard.PolicyGuard.LockResult; import org.onap.policy.guard.TargetLock; import org.onap.policy.rest.RESTManager; +import org.onap.policy.so.util.Serialization; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -95,6 +103,12 @@ public class ControlLoopEventManager implements LockCallback, Serializable { private AaiGetVnfResponse vnfResponse = null; private AaiGetVserverResponse vserverResponse = null; + /** + * Wrapper for AAI vserver named-query response. This is initialized in a lazy + * fashion. + */ + private AaiNqResponseWrapper nqVserverResponse = null; + private static Collection<String> requiredAAIKeys = new ArrayList<>(); static { @@ -871,6 +885,84 @@ public class ControlLoopEventManager implements LockCallback, Serializable { return response; } + /** + * Gets the output from the AAI vserver named-query, using the cache, if appropriate. + * @return output from the AAI vserver named-query + */ + public AaiNqResponseWrapper getNqVserverFromAai() { + if(nqVserverResponse != null) { + // already queried + return nqVserverResponse; + } + + String vserverName = onset.getAai().get(VSERVER_VSERVER_NAME); + if(vserverName == null) { + logger.warn("Missing vserver-name for AAI request {}", onset.getRequestId()); + return null; + } + + // create AAI named-query request with UUID started with "" + AaiNqRequest aaiNqRequest = new AaiNqRequest(); + AaiNqQueryParameters aaiNqQueryParam = new AaiNqQueryParameters(); + AaiNqNamedQuery aaiNqNamedQuery = new AaiNqNamedQuery(); + final AaiNqInstanceFilters aaiNqInstanceFilter = new AaiNqInstanceFilters(); + + // queryParameters + // TODO: UUID.fromString($params.getAaiNamedQueryUUID()) AaiNamedQueryUUID + aaiNqNamedQuery.setNamedQueryUuid(UUID.fromString("4ff56a54-9e3f-46b7-a337-07a1d3c6b469")); + aaiNqQueryParam.setNamedQuery(aaiNqNamedQuery); + aaiNqRequest.setQueryParameters(aaiNqQueryParam); + // + // instanceFilters + // + Map<String, Map<String, String>> aaiNqInstanceFilterMap = new HashMap<>(); + Map<String, String> aaiNqInstanceFilterMapItem = new HashMap<>(); + aaiNqInstanceFilterMapItem.put("vserver-name", vserverName); + aaiNqInstanceFilterMap.put("vserver", aaiNqInstanceFilterMapItem); + aaiNqInstanceFilter.getInstanceFilter().add(aaiNqInstanceFilterMap); + aaiNqRequest.setInstanceFilters(aaiNqInstanceFilter); + + if (logger.isDebugEnabled()) { + logger.debug("AAI Request sent: {}", Serialization.gsonPretty.toJson(aaiNqRequest)); + } + + AaiNqResponse aaiNqResponse = new AaiManager(new RESTManager()).postQuery(getPeManagerEnvProperty("aai.url"), + getPeManagerEnvProperty("aai.username"), getPeManagerEnvProperty("aai.password"), aaiNqRequest, + onset.getRequestId()); + + // Check AAI response + if (aaiNqResponse == null) { + logger.warn("No response received from AAI for request {}", aaiNqRequest); + return null; + } + + // Create AAINQResponseWrapper + nqVserverResponse = new AaiNqResponseWrapper(onset.getRequestId(), aaiNqResponse); + + if (logger.isDebugEnabled()) { + logger.debug("AAI Named Query Response: "); + logger.debug(Serialization.gsonPretty.toJson(nqVserverResponse.getAaiNqResponse())); + } + + return nqVserverResponse; + } + + /** + * This method reads and validates environmental properties coming from the policy engine. Null + * properties cause an {@link IllegalArgumentException} runtime exception to be thrown + * + * @param enginePropertyName the name of the parameter to retrieve + * @return the property value + */ + private static String getPeManagerEnvProperty(String enginePropertyName) { + String enginePropertyValue = PolicyEngine.manager.getEnvironmentProperty(enginePropertyName); + if (enginePropertyValue == null) { + throw new IllegalArgumentException("The value of policy engine manager environment property \"" + + enginePropertyName + "\" may not be null"); + } + return enginePropertyValue; + } + @Override public boolean isActive() { // TODO diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java index 7ef2a2e04..0eb924dd2 100644 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java +++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java @@ -264,8 +264,8 @@ public class ControlLoopOperationManager implements Serializable { return operationRequest; case "SO": SOActorServiceProvider soActorSp = new SOActorServiceProvider(); - this.operationRequest = - soActorSp.constructRequest((VirtualControlLoopEvent) onset, operation.clOperation, this.policy); + this.operationRequest = soActorSp.constructRequest((VirtualControlLoopEvent) onset, + operation.clOperation, this.policy, eventManager.getNqVserverFromAai()); // Save the operation this.currentOperation = operation; 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 1cef78a9e..1b2d8db61 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 @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -46,6 +46,7 @@ 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.AaiNqResponseWrapper; import org.onap.policy.aai.RelatedToProperty; import org.onap.policy.aai.Relationship; import org.onap.policy.aai.RelationshipData; @@ -71,8 +72,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ControlLoopEventManagerTest { + private static final String INVALID_URL = "http://localhost:9999"; + private static final Logger logger = LoggerFactory.getLogger(ControlLoopEventManagerTest.class); - + @Rule public ExpectedException thrown = ExpectedException.none(); @@ -97,7 +100,7 @@ public class ControlLoopEventManagerTest { public static void tearDownSimulator() { HttpServletServer.factory.destroy(); } - + @Before public void setUp() { onset = new VirtualControlLoopEvent(); @@ -110,7 +113,7 @@ public class ControlLoopEventManagerTest { 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"); } @@ -366,7 +369,7 @@ public class ControlLoopEventManagerTest { public static AaiGetVserverResponse getQueryByVserverName2(String urlGet, String username, String password, UUID requestId, String key) { AaiGetVserverResponse response = new AaiGetVserverResponse(); - + AaiNqVServer svr = new AaiNqVServer(); svr.setVserverId("d0668d4f-c25e-4a1b-87c4-83845c01efd8"); @@ -407,7 +410,7 @@ public class ControlLoopEventManagerTest { relationshipList.getRelationships().add(relationship); svr.setRelationshipList(relationshipList); - + response.getVserver().add(svr); return response; @@ -894,7 +897,7 @@ public class ControlLoopEventManagerTest { @Test 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, @@ -915,7 +918,7 @@ public class ControlLoopEventManagerTest { @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"); @@ -938,15 +941,15 @@ public class ControlLoopEventManagerTest { 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()); } @@ -954,22 +957,22 @@ public class ControlLoopEventManagerTest { @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()); } @@ -977,10 +980,10 @@ public class ControlLoopEventManagerTest { @Test public void testQueryAai_QueryVnfById_Disabled() throws AaiException { ControlLoopEventManager mgr = null; - + try { onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_VNF_ID, "disableClosedLoop"); - + mgr = makeManager(onset); mgr.queryAai(onset); @@ -988,7 +991,7 @@ public class ControlLoopEventManagerTest { } catch (AaiException expected) { assertEquals("is-closed-loop-disabled is set to true (query by vnf-id)", expected.getMessage()); - + assertNotNull(mgr.getVnfResponse()); assertNull(mgr.getVserverResponse()); } @@ -1003,15 +1006,15 @@ public class ControlLoopEventManagerTest { 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()); } @@ -1019,11 +1022,11 @@ public class ControlLoopEventManagerTest { @Test public void testQueryAai_QueryVserver_Disabled() throws AaiException { ControlLoopEventManager mgr = null; - + try { onset.getAai().remove(ControlLoopEventManager.GENERIC_VNF_VNF_ID); onset.getAai().put(ControlLoopEventManager.VSERVER_VSERVER_NAME, "disableClosedLoop"); - + mgr = makeManager(onset); mgr.queryAai(onset); @@ -1031,7 +1034,7 @@ public class ControlLoopEventManagerTest { } catch (AaiException expected) { assertEquals("is-closed-loop-disabled is set to true (query by vserver-name)", expected.getMessage()); - + assertNull(mgr.getVnfResponse()); assertNotNull(mgr.getVserverResponse()); } @@ -1040,8 +1043,8 @@ public class ControlLoopEventManagerTest { @Test(expected = AaiException.class) public void testQueryAai_QueryException() throws AaiException { // Force AAI errors - PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:9999"); - + PolicyEngine.manager.setEnvironmentProperty("aai.url", INVALID_URL); + makeManager(onset).queryAai(onset); } @@ -1066,11 +1069,11 @@ public class ControlLoopEventManagerTest { 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); @@ -1080,7 +1083,7 @@ public class ControlLoopEventManagerTest { 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); @@ -1101,10 +1104,10 @@ public class ControlLoopEventManagerTest { @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); @@ -1125,15 +1128,15 @@ public class ControlLoopEventManagerTest { thrown.expectMessage("AAI Responded with a request error (query by vserver-name)"); 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); } @@ -1145,7 +1148,7 @@ public class ControlLoopEventManagerTest { 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); @@ -1159,7 +1162,7 @@ public class ControlLoopEventManagerTest { AaiGetVserverResponse resp = new AaiGetVserverResponse(); AaiNqVServer svr = new AaiNqVServer(); resp.getVserver().add(svr); - + svr.setIsClosedLoopDisabled(false); svr.setProvStatus("inactive1"); Whitebox.invokeMethod(ControlLoopEventManager.class, "processVServerResponse", resp); @@ -1168,63 +1171,63 @@ public class ControlLoopEventManagerTest { @Test public void testIsClosedLoopDisabled() { Map<String, String> 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<String, String> 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")); @@ -1233,11 +1236,44 @@ public class ControlLoopEventManagerTest { assertTrue(ControlLoopEventManager.isAaiTrue("yES")); assertTrue(ControlLoopEventManager.isAaiTrue("Y")); assertTrue(ControlLoopEventManager.isAaiTrue("y")); - + assertFalse(ControlLoopEventManager.isAaiTrue("no")); assertFalse(ControlLoopEventManager.isAaiTrue(null)); } + @Test + public void testGetNqVserverFromAai() throws Exception { + + // empty vserver name + ControlLoopEventManager manager = makeManager(onset); + manager.activate(onset); + assertNull(manager.getNqVserverFromAai()); + + + // re-create manager with a vserver name in the onset + onset.getAai().put(ControlLoopEventManager.VSERVER_VSERVER_NAME, "my-name"); + manager = makeManager(onset); + manager.activate(onset); + + AaiNqResponseWrapper resp = manager.getNqVserverFromAai(); + assertNotNull(resp); + assertEquals(onset.getRequestId(), resp.getRequestId()); + assertNotNull(resp.getAaiNqResponse()); + assertFalse(resp.getAaiNqResponse().getInventoryResponseItems().isEmpty()); + + // re-query should return the same object + assertTrue(resp == manager.getNqVserverFromAai()); + + + // Force AAI error + PolicyEngine.manager.setEnvironmentProperty("aai.url", INVALID_URL); + + // re-create manager + manager = makeManager(onset); + manager.activate(onset); + assertNull(manager.getNqVserverFromAai()); + } + private ControlLoopEventManager makeManager(VirtualControlLoopEvent event) { return new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId()); } |