Merge "Fix Final Failure on AAI Queries"
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 d320b75..b167420 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
@@ -149,7 +149,7 @@
 			// Syntax check the event
 			//
 			checkEventSyntax(event);
-			checkEventAAISyntax(event);
+			
 			//
 			// At this point we are good to go with this event
 			//
@@ -184,7 +184,6 @@
 			// Syntax check the event
 			//
 			checkEventSyntax(event);
-			checkEventAAISyntax(event);
 	
 			//
 			// Check the YAML
@@ -437,7 +436,7 @@
 		;
 	}
 		
-	public NEW_EVENT_STATUS	onNewEvent(VirtualControlLoopEvent event) {
+	public NEW_EVENT_STATUS	onNewEvent(VirtualControlLoopEvent event) throws AAIException {
 		try {
 			this.checkEventSyntax(event);
 			if (event.closedLoopEventStatus == ControlLoopEventStatus.ONSET) {
@@ -445,6 +444,11 @@
 				// Check if this is our original ONSET
 				//
 				if (event.equals(this.onset)) {
+				    //
+				    // Query A&AI if needed
+				    //
+				    queryAai(event);
+				    
 					//
 					// DO NOT retract it
 					//
@@ -546,58 +550,58 @@
 				! "generic-vnf.vnf-name".equalsIgnoreCase(event.target) ) {
 			throw new ControlLoopException("target field invalid - expecting VM_NAME or VNF_NAME");
 		}
+		if (event.AAI == null) {
+            throw new ControlLoopException("AAI is null");
+        }
+        if (event.AAI.get("generic-vnf.vnf-id") == null && event.AAI.get("vserver.vserver-name") == null &&
+                event.AAI.get("generic-vnf.vnf-name") == null) {
+            throw new ControlLoopException("generic-vnf.vnf-id or generic-vnf.vnf-name or vserver.vserver-name information missing");
+        }
 	}
 	
-	public void checkEventAAISyntax(VirtualControlLoopEvent event) throws ControlLoopException {
-		if (event.AAI == null) {
-			throw new ControlLoopException("AAI is null");
-		}
-		if (event.AAI.get("generic-vnf.vnf-id") == null && event.AAI.get("vserver.vserver-name") == null &&
-				event.AAI.get("generic-vnf.vnf-name") == null) {
-			throw new ControlLoopException("generic-vnf.vnf-id or generic-vnf.vnf-name or vserver.vserver-name information missing");
-		}
+	public void queryAai(VirtualControlLoopEvent event) throws AAIException {
 		if (event.AAI.get("vserver.is-closed-loop-disabled") == null) {
 			try {
 				if (event.AAI.get("generic-vnf.vnf-id") != null) {
 					vnfResponse = getAAIVnfInfo(event); 
 					if (vnfResponse == null) {
-						throw new ControlLoopException("AAI Response is null (query by vnf-id)");
+						throw new AAIException("AAI Response is null (query by vnf-id)");
 					}
 					if (vnfResponse.requestError != null) {
-						throw new ControlLoopException("AAI Responded with a request error (query by vnf-id)");
+						throw new AAIException("AAI Responded with a request error (query by vnf-id)");
 					}
 					if (isClosedLoopDisabled(vnfResponse) == true) {
-						throw new ControlLoopException("is-closed-loop-disabled is set to true");	
+						throw new AAIException("is-closed-loop-disabled is set to true");	
 					}
 				} else if (event.AAI.get("generic-vnf.vnf-name") != null) {
 					vnfResponse = getAAIVnfInfo(event); 
 					if (vnfResponse == null) {
-						throw new ControlLoopException("AAI Response is null (query by vnf-name)");
+						throw new AAIException("AAI Response is null (query by vnf-name)");
 					}
 					if (vnfResponse.requestError != null) {
-						throw new ControlLoopException("AAI Responded with a request error (query by vnf-name)");
+						throw new AAIException("AAI Responded with a request error (query by vnf-name)");
 					}
 					if (isClosedLoopDisabled(vnfResponse) == true) {
-						throw new ControlLoopException("is-closed-loop-disabled is set to true");	
+						throw new AAIException("is-closed-loop-disabled is set to true");	
 					}
 				} else if (event.AAI.get("vserver.vserver-name") != null) {
 					vserverResponse = getAAIVserverInfo(event); 
 					if (vserverResponse == null) {
-						throw new ControlLoopException("AAI Response is null (query by vserver-name)");
+						throw new AAIException("AAI Response is null (query by vserver-name)");
 					}
 					if (vserverResponse.requestError != null) {
-						throw new ControlLoopException("AAI responded with a request error (query by vserver-name)");
+						throw new AAIException("AAI responded with a request error (query by vserver-name)");
 					}
 					if (isClosedLoopDisabled(vserverResponse) == true) {
-						throw new ControlLoopException("is-closed-loop-disabled is set to true");	
+						throw new AAIException("is-closed-loop-disabled is set to true");	
 					}
 				}
 			} catch (Exception e) {
 				logger.error("Exception from getAAIInfo: ", e);
-				throw new ControlLoopException("Exception from getAAIInfo: " + e.toString());
+				throw new AAIException("Exception from getAAIInfo: " + e.toString());
 			}
 		} else if (isClosedLoopDisabled(event)) {
-			throw new ControlLoopException("is-closed-loop-disabled is set to true");
+			throw new AAIException("is-closed-loop-disabled is set to true");
 		}
 	}
 	
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 2233615..be0eb2b 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
@@ -40,6 +40,7 @@
 import org.onap.policy.aai.RelationshipData;
 import org.onap.policy.aai.RelationshipDataItem;
 import org.onap.policy.aai.RelationshipList;
+import org.onap.policy.aai.util.AAIException;
 import org.onap.policy.controlloop.ControlLoopEventStatus;
 import org.onap.policy.controlloop.ControlLoopException;
 import org.onap.policy.controlloop.ControlLoopNotificationType;
@@ -227,6 +228,17 @@
         
         assertNotNull(notification);
         assertEquals(ControlLoopNotificationType.ACTIVE, notification.notification);
+        
+        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());
@@ -240,7 +252,13 @@
         event2.AAI = new HashMap<>();
         event2.AAI.put("generic-vnf.vnf-name", "onsetTwo");
         
-        ControlLoopEventManager.NEW_EVENT_STATUS status = manager.onNewEvent(event2);
+        
+        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);
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 e7a3119..1714d3e 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
@@ -107,6 +107,16 @@
 			assertNotNull(notification);
 			assertEquals(ControlLoopNotificationType.ACTIVE, notification.notification);
 			
+			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);
 			//
@@ -226,6 +236,16 @@
 			assertNotNull(notification);
 			assertEquals(ControlLoopNotificationType.ACTIVE, notification.notification);
 
+			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);
 			//
 			//
diff --git a/controlloop/common/simulators/src/main/java/org/onap/policy/simulators/AaiSimulatorJaxRs.java b/controlloop/common/simulators/src/main/java/org/onap/policy/simulators/AaiSimulatorJaxRs.java
index 1a0eb31..8ecc114 100644
--- a/controlloop/common/simulators/src/main/java/org/onap/policy/simulators/AaiSimulatorJaxRs.java
+++ b/controlloop/common/simulators/src/main/java/org/onap/policy/simulators/AaiSimulatorJaxRs.java
@@ -109,7 +109,7 @@
 	@Path("/v11/nodes/vservers")
 	@Consumes(MediaType.APPLICATION_JSON)
 	@Produces("application/json")
-	public String getByVserverName (@QueryParam("vserverName") String vserverName)
+	public String getByVserverName (@QueryParam("vserver-name") String vserverName)
 	{
 		if ("getFail".equals(vserverName)) {
 			return "{\"requestError\":{\"serviceException\":{\"messageId\":\"SVC3001\",\"text\":\"Resource not found for %1 using id %2 (msg=%3) (ec=%4)\",\"variables\":[\"GET\",\"nodes/vservers\",\"Node Not Found:No Node of type generic-vnf found at nodes/vservers\",\"ERR.5.4.6114\"]}}}";
diff --git a/controlloop/templates/archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl b/controlloop/templates/archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl
index da54bbe..93b4e0a 100644
--- a/controlloop/templates/archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl
+++ b/controlloop/templates/archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl
@@ -294,49 +294,50 @@
     logger.info("{}: {}: event={} manager={} clTimer={}", 
                 $params.getClosedLoopControlName(), drools.getRule().getName(),
                 $event, $manager, $clTimer);
-    //
-    // Check which event this is.
-    //
-    ControlLoopEventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
-
-    //
-    // Check what kind of event this is
-    //
-    if (eventStatus == NEW_EVENT_STATUS.SUBSEQUENT_ONSET) {
-        //
-        // We don't care about subsequent onsets
-        //
-        logger.info("{}: {}: subsequent onset", 
-                    $params.getClosedLoopControlName(), drools.getRule().getName());
-        retract($event);
-        return;
-    }
-    if (eventStatus == NEW_EVENT_STATUS.SYNTAX_ERROR) {
-        //
-        // Ignore any bad syntax events
-        //
-        logger.warn("{}: {}: syntax error", 
-                    $params.getClosedLoopControlName(), drools.getRule().getName());
-        retract($event);
-        return;
-    }
-    //
-    // We only want the initial ONSET event in memory,
-    // all the other events need to be retracted to support
-    // cleanup and avoid the other rules being fired for this event.
-    //
-    if (eventStatus != NEW_EVENT_STATUS.FIRST_ONSET) {
-        logger.warn("{}: {}: no first onset", 
-                    $params.getClosedLoopControlName(), drools.getRule().getName());
-        retract($event);
-    }
     
-    logger.debug("{}: {}: target={}", $params.getClosedLoopControlName(), 
-                 drools.getRule().getName(), $event.target);
-    //
-    // Now start seeing if we need to process this event
-    //
     try {
+	    //
+	    // Check which event this is.
+	    //
+	    ControlLoopEventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
+	    //
+	    // Check what kind of event this is
+	    //
+	    if (eventStatus == NEW_EVENT_STATUS.SUBSEQUENT_ONSET) {
+	        //
+	        // We don't care about subsequent onsets
+	        //
+	        logger.info("{}: {}: subsequent onset", 
+	                    $params.getClosedLoopControlName(), drools.getRule().getName());
+	        retract($event);
+	        return;
+	    }
+	    if (eventStatus == NEW_EVENT_STATUS.SYNTAX_ERROR) {
+	        //
+	        // Ignore any bad syntax events
+	        //
+	        logger.warn("{}: {}: syntax error", 
+	                    $params.getClosedLoopControlName(), drools.getRule().getName());
+	        retract($event);
+	        return;
+	    }
+	    //
+	    // We only want the initial ONSET event in memory,
+	    // all the other events need to be retracted to support
+	    // cleanup and avoid the other rules being fired for this event.
+	    //
+	    if (eventStatus != NEW_EVENT_STATUS.FIRST_ONSET) {
+	        logger.warn("{}: {}: no first onset", 
+	                    $params.getClosedLoopControlName(), drools.getRule().getName());
+	        retract($event);
+	    }
+	    
+	    logger.debug("{}: {}: target={}", $params.getClosedLoopControlName(), 
+	                 drools.getRule().getName(), $event.target);
+	    //
+	    // Now start seeing if we need to process this event
+	    //
+
         //
         // Check if this is a Final Event
         //
diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VCPEControlLoopTest.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VCPEControlLoopTest.java
index aaa40d1..55812f1 100644
--- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VCPEControlLoopTest.java
+++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VCPEControlLoopTest.java
@@ -279,13 +279,7 @@
             String policyName = notification.policyName;
             if (policyName.endsWith("EVENT")) {
                 logger.debug("Rule Fired: " + notification.policyName);
-                if ("getFail".equals(notification.AAI.get("generic-vnf.vnf-name"))) {
-                	assertEquals(ControlLoopNotificationType.REJECTED, notification.notification);
-                	kieSession.halt();
-                }
-                else {
-                    assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.notification));
-                }
+                assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.notification));
             }
             else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
                 logger.debug("Rule Fired: " + notification.policyName);
@@ -320,8 +314,14 @@
             }
             else if (policyName.endsWith("EVENT.MANAGER")) {
                 logger.debug("Rule Fired: " + notification.policyName);
-                assertTrue(ControlLoopNotificationType.FINAL_SUCCESS.equals(notification.notification));
-                kieSession.halt();
+                if ("getFail".equals(notification.AAI.get("generic-vnf.vnf-name"))) {
+                    assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.notification);
+                    kieSession.halt();
+                }
+                else {
+                    assertTrue(ControlLoopNotificationType.FINAL_SUCCESS.equals(notification.notification));
+                    kieSession.halt();
+                }
             }
             else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
                 logger.debug("Rule Fired: " + notification.policyName);
diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VDNSControlLoopTest.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VDNSControlLoopTest.java
index 3e1785d..f7e2c30 100644
--- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VDNSControlLoopTest.java
+++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VDNSControlLoopTest.java
@@ -322,13 +322,7 @@
             String policyName = notification.policyName;
             if (policyName.endsWith("EVENT")) {
                 logger.debug("Rule Fired: " + notification.policyName);
-                if ("getFail".equals(notification.AAI.get("generic-vnf.vnf-id"))) {
-                	assertEquals(ControlLoopNotificationType.REJECTED, notification.notification);
-                	kieSession.halt();
-                }
-                else {
-                    assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.notification));
-                }
+                assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.notification));
             }
             else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
                 logger.debug("Rule Fired: " + notification.policyName);
@@ -365,6 +359,9 @@
                 if ("error".equals(notification.AAI.get("vserver.vserver-name"))) {
                 	assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.notification);
                 }
+                else if ("getFail".equals(notification.AAI.get("vserver.vserver-name"))) {
+                    assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.notification);
+                }
                 else {
                 	assertTrue(ControlLoopNotificationType.FINAL_SUCCESS.equals(notification.notification));
                 }
diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VFWControlLoopTest.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VFWControlLoopTest.java
index 601eef0..5ef20b5 100644
--- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VFWControlLoopTest.java
+++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/VFWControlLoopTest.java
@@ -325,13 +325,7 @@
             String policyName = notification.policyName;
             if (policyName.endsWith("EVENT")) {
                 logger.debug("Rule Fired: " + notification.policyName);
-                if ("getFail".equals(notification.AAI.get("generic-vnf.vnf-name"))) {
-                	assertEquals(ControlLoopNotificationType.REJECTED, notification.notification);
-                	kieSession.halt();
-                }
-                else {
-                    assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.notification));
-                }
+                assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.notification));
             }
             else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
                 logger.debug("Rule Fired: " + notification.policyName);
@@ -370,6 +364,9 @@
                 	assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.notification);
                 	assertEquals("Target vnf-id could not be found", notification.message);
                 }
+                else if ("getFail".equals(notification.AAI.get("generic-vnf.vnf-name"))) {
+                    assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.notification);
+                }
                 else {
                     assertTrue(ControlLoopNotificationType.FINAL_SUCCESS.equals(notification.notification));
                 }