Fix Use Case Template

This is a WIP for getting guard to work. Currently
the A&AI GET and named queries are connecting to
the simulator and working in the labs. Guard is not
connecting to simulator and needs further analysis.

Issue-ID: POLICY-259
Change-Id: If9875bfd83cbd82dcae04a876b3818ec9c07b1f7
Signed-off-by: Daniel Cruz <dc443y@att.com>
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 0d4aa93..7aaac9e 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
@@ -629,7 +629,7 @@
 		try {
 	        if (vserverName != null) {
 	           aaiHostURL  = PolicyEngine.manager.getEnvironmentProperty("aai.url"); 
-	           aaiUser     = PolicyEngine.manager.getEnvironmentProperty("aai.user"); 
+	           aaiUser     = PolicyEngine.manager.getEnvironmentProperty("aai.username"); 
 	           aaiPassword = PolicyEngine.manager.getEnvironmentProperty("aai.password");
 	           String aaiGetQueryByVserver = "/aai/v11/nodes/vservers?vserver-name="; 
  	   		   String url = aaiHostURL + aaiGetQueryByVserver; 
@@ -650,19 +650,17 @@
 		String vnfName = event.AAI.get("generic-vnf.vnf-name"); 
 		String vnfID   = event.AAI.get("generic-vnf.vnf-id"); 
  
+		aaiHostURL  = PolicyEngine.manager.getEnvironmentProperty("aai.url"); 
+        aaiUser     = PolicyEngine.manager.getEnvironmentProperty("aai.username"); 
+        aaiPassword = PolicyEngine.manager.getEnvironmentProperty("aai.password");
+		
 		try {
             if (vnfName != null) {
- 	           aaiHostURL  = PolicyEngine.manager.getEnvironmentProperty("aai.url"); 
- 	           aaiUser     = PolicyEngine.manager.getEnvironmentProperty("aai.user"); 
- 	           aaiPassword = PolicyEngine.manager.getEnvironmentProperty("aai.password");
  	           String aaiGetQueryByVnfName = "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name="; 
   	   		   String url = aaiHostURL + aaiGetQueryByVnfName; 
   	   		   logger.info("url: " + url);
 			   response = AAIManager.getQueryByVnfName(url, aaiUser, aaiPassword, requestID, vnfName);	        	
 	        } else if (vnfID != null) {
-	 	       aaiHostURL  = PolicyEngine.manager.getEnvironmentProperty("aai.url"); 
-	 	       aaiUser     = PolicyEngine.manager.getEnvironmentProperty("aai.user"); 
-	 	       aaiPassword = PolicyEngine.manager.getEnvironmentProperty("aai.password");
 	 	       String aaiGetQueryByVnfID = "/aai/v11/network/generic-vnfs/generic-vnf/"; 
 	  	   	   String url = aaiHostURL + aaiGetQueryByVnfID; 
 	  	   	   logger.info("url: " + url);
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 59e97c2..ad106e5 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
@@ -36,6 +36,7 @@
 	
 	@GET
 	@Path("/v8/network/generic-vnfs/generic-vnf/{vnfId}")
+	@Consumes(MediaType.APPLICATION_JSON)
 	@Produces("application/json")
 	public String aaiGetQuery (@PathParam("vnfID") String vnfId)
 	{
@@ -63,6 +64,7 @@
 	
 	@GET
 	@Path("/v11/network/generic-vnfs/generic-vnf?vnf-name={vnfName}")
+	@Consumes(MediaType.APPLICATION_JSON)
 	@Produces("application/json")
 	public String getByVnfName (@PathParam("vnfName") String vnfName)
 	{
@@ -72,6 +74,7 @@
 	
 	@GET
 	@Path("/v11/network/generic-vnfs/generic-vnf/{vnfId}")
+	@Consumes(MediaType.APPLICATION_JSON)
 	@Produces("application/json")
 	public String getByVnfId (@PathParam("vnfId") String vnfId)
 	{
@@ -81,6 +84,7 @@
 	
 	@GET
 	@Path("/v11/nodes/vservers?vserver-name={vserverName}")
+	@Consumes(MediaType.APPLICATION_JSON)
 	@Produces("application/json")
 	public String getByVserverName (@PathParam("vserverName") String vserverName)
 	{
diff --git a/controlloop/common/simulators/src/main/java/org/onap/policy/simulators/VfcSimulatorJaxRs.java b/controlloop/common/simulators/src/main/java/org/onap/policy/simulators/VfcSimulatorJaxRs.java
index 926536e..53ae7bc 100644
--- a/controlloop/common/simulators/src/main/java/org/onap/policy/simulators/VfcSimulatorJaxRs.java
+++ b/controlloop/common/simulators/src/main/java/org/onap/policy/simulators/VfcSimulatorJaxRs.java
@@ -20,6 +20,7 @@
 
 package org.onap.policy.simulators;
 
+import javax.ws.rs.Consumes;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
@@ -27,12 +28,14 @@
 import javax.ws.rs.Produces;
 import javax.servlet.http.HttpServletResponse;
 import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
 
 @Path("/api/nslcm/v1")
 public class VfcSimulatorJaxRs {
 	
 	@POST
 	@Path("/ns/{nsInstanceId}/heal")
+	@Consumes(MediaType.APPLICATION_JSON)
 	@Produces("application/json")
 	public String vfcPostQuery(@PathParam("nsInstanceId") String nsInstanceId,
                                    @Context final HttpServletResponse response)
@@ -47,6 +50,7 @@
 	
 	@GET
 	@Path("/jobs/{jobId}")
+	@Consumes(MediaType.APPLICATION_JSON)
 	@Produces("application/json")
 	public String vfcGetQuery(@PathParam("jobId") String jobId) {
 		return "{\"jobId\" : "+jobId+",\"responseDescriptor\" : {\"progress\" : \"40\",\"status\" : \"finished\",\"statusDescription\" : \"OMC VMs are decommissioned in VIM\",\"errorCode\" : null,\"responseId\": 101 ,\"responseHistoryList\": [{\"progress\" : \"40\",\"status\" : \"proccessing\",\"statusDescription\" : \"OMC VMs are decommissioned in VIM\",\"errorCode\" : null,\"responseId\" : \"1\"}, {\"progress\" : \"41\",\"status\" : \"proccessing\",\"statusDescription\" : \"OMC VMs are decommissioned in VIM\",\"errorCode\" : null,\"responseId\" : \"2\"}]}}";
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 b10cc3b..343221f 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
@@ -506,7 +506,7 @@
         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
         $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
-        $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID, getGuardApprovalStatus() == "Permit" )
+        $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID, "Permit".equalsIgnoreCase(getGuardApprovalStatus()) )
         $lock : TargetLock (requestID == $event.requestID)
     then
 
@@ -620,7 +620,7 @@
     // to the CallGuardTask() and set the first argument to null 
     // (instead of XacmlPdpEngine).
     //
-    boolean guardEnabled = false;
+    boolean guardEnabled = true;
     
     if(guardEnabled){
     
@@ -682,7 +682,7 @@
     
     PolicyEngine.manager.deliver("POLICY-CL-MGT", notification);
     
-    if("Permit".equals($guardResponse.result)){
+    if("Permit".equalsIgnoreCase($guardResponse.result)){
     
         modify($operation){setGuardApprovalStatus($guardResponse.result)};
     }
diff --git a/controlloop/templates/template.demo/src/main/resources/ControlLoop_Template_xacml_guard.drl b/controlloop/templates/template.demo/src/main/resources/ControlLoop_Template_xacml_guard.drl
index 64101ac..6976e99 100644
--- a/controlloop/templates/template.demo/src/main/resources/ControlLoop_Template_xacml_guard.drl
+++ b/controlloop/templates/template.demo/src/main/resources/ControlLoop_Template_xacml_guard.drl
@@ -473,7 +473,7 @@
         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() )
         $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, requestID == $event.requestID )
-        $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID, getGuardApprovalStatus() == "Permit" )
+        $operation : ControlLoopOperationManager( onset.closedLoopControlName == $event.closedLoopControlName, onset.requestID == $event.requestID, "Permit".equalsIgnoreCase(getGuardApprovalStatus()) )
         $lock : TargetLock (requestID == $event.requestID)
 	then
     	//
@@ -657,7 +657,7 @@
 		
 		
 		
-		if("Permit".equals($guardResponse.result)){
+		if("Permit".equalsIgnoreCase($guardResponse.result)){
 		
 			modify($operation){setGuardApprovalStatus($guardResponse.result)};
 		}
diff --git a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/Util.java b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/Util.java
index f8f9068..8e8ac35 100644
--- a/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/Util.java
+++ b/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/Util.java
@@ -126,6 +126,10 @@
 		return org.onap.policy.simulators.Util.buildVfcSim();
 	}
 	
+	public static HttpServletServer buildGuardSim() throws InterruptedException, IOException {
+        return org.onap.policy.simulators.Util.buildGuardSim();
+    }
+	
 	private static String	generatePolicy(String ruleContents, 
 			String closedLoopControlName, 
 			String policyScope, 
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 53c9245..9743f88 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
@@ -28,6 +28,8 @@
 import java.util.HashMap;
 import java.util.UUID;
 
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.kie.api.runtime.KieSession;
 import org.kie.api.runtime.rule.FactHandle;
@@ -41,7 +43,9 @@
 import org.onap.policy.controlloop.VirtualControlLoopNotification;
 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
 import org.onap.policy.controlloop.policy.TargetType;
+import org.onap.policy.drools.http.server.HttpServletServer;
 import org.onap.policy.drools.impl.PolicyEngineJUnitImpl;
+import org.onap.policy.drools.system.PolicyEngine;
 import org.onap.policy.guard.PolicyGuard;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -52,7 +56,33 @@
     
     private KieSession kieSession;
     private Util.Pair<ControlLoopPolicy, String> pair;
-    private PolicyEngineJUnitImpl engine;        
+    private PolicyEngineJUnitImpl engine;    
+    
+    static {
+        /* Set environment properties */
+        PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
+        PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI");
+        PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
+        
+        PolicyEngine.manager.setEnvironmentProperty("guard.url", "http://localhost:6669/pdp/api/getDecision");
+        PolicyEngine.manager.setEnvironmentProperty("guard.username", "GUARD");
+        PolicyEngine.manager.setEnvironmentProperty("guard.password", "GUARD");
+    }
+    
+    @BeforeClass
+    public static void setUpSimulator() {
+        try {
+            Util.buildAaiSim();
+            Util.buildGuardSim();
+        } catch (Exception e) {
+            fail(e.getMessage());
+        }
+    }
+
+    @AfterClass
+    public static void tearDownSimulator() {
+        HttpServletServer.factory.destroy();
+    }
     
     @Test
     public void successTest() {
@@ -168,7 +198,7 @@
          * See if Guard permits this action, if it does 
          * not then the test should fail
          */
-        if (((VirtualControlLoopNotification)obj).message.contains("Guard result: Permit")) {
+        if (((VirtualControlLoopNotification)obj).message.contains("Guard result: PERMIT")) {
             
             /* 
              * A notification should be sent out of the Policy
@@ -395,13 +425,10 @@
         event.target = "generic-vnf.vnf-id";
         event.closedLoopAlarmStart = Instant.now();
         event.AAI = new HashMap<>();
-        event.AAI.put("cloud-region.identity-url", "foo");
-        event.AAI.put("vserver.selflink", "bar");
-        event.AAI.put("vserver.is-closed-loop-disabled", "false");
         event.AAI.put("generic-vnf.vnf-id", "testGenericVnfId");
         event.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
         kieSession.insert(event);
-        Thread.sleep(1000);
+        Thread.sleep(2000);
     }
     
     /**
@@ -421,9 +448,6 @@
         event.closedLoopAlarmStart = Instant.now().minusSeconds(5);
         event.closedLoopAlarmEnd = Instant.now();
         event.AAI = new HashMap<>();
-        event.AAI.put("cloud-region.identity-url", "foo");
-        event.AAI.put("vserver.selflink", "bar");
-        event.AAI.put("vserver.is-closed-loop-disabled", "false");
         event.AAI.put("generic-vnf.vnf-id", "testGenericVnfId");
         event.closedLoopEventStatus = ControlLoopEventStatus.ABATED;
         kieSession.insert(event);
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 4cd0054..a63344d 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
@@ -415,9 +415,6 @@
         event.target = "generic-vnf.vnf-id";
         event.closedLoopAlarmStart = Instant.now();
         event.AAI = new HashMap<>();
-        event.AAI.put("cloud-region.identity-url", "foo");
-        event.AAI.put("vserver.selflink", "bar");
-        event.AAI.put("vserver.is-closed-loop-disabled", "false");
         event.AAI.put("generic-vnf.vnf-id", "testGenericVnfID");
         event.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
         kieSession.insert(event);