Merge "Included Policy GUI Enhancements and validations"
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / AutoPushController.java
index f78a79c..e0388e5 100644 (file)
@@ -38,6 +38,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import javax.script.SimpleBindings;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
@@ -51,13 +52,14 @@ import org.onap.policy.rest.adapter.AutoPushTabAdapter;
 import org.onap.policy.rest.dao.CommonClassDao;
 import org.onap.policy.rest.jpa.PolicyEntity;
 import org.onap.policy.rest.jpa.PolicyVersion;
+import org.onap.policy.utils.PolicyUtils;
 import org.onap.policy.xacml.api.XACMLErrorConstants;
 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
 import org.onap.policy.xacml.std.pap.StdPDPGroup;
 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
-import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
-import org.openecomp.portalsdk.core.web.support.JsonMessage;
-import org.openecomp.portalsdk.core.web.support.UserUtils;
+import org.onap.portalsdk.core.controller.RestrictedBaseController;
+import org.onap.portalsdk.core.web.support.JsonMessage;
+import org.onap.portalsdk.core.web.support.UserUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.MediaType;
 import org.springframework.stereotype.Controller;
@@ -119,7 +121,7 @@ public class AutoPushController extends RestrictedBaseController{
                try{
                        Set<String> scopes = null;
                        List<String> roles = null;
-                       data = new ArrayList<Object>();
+                       data = new ArrayList<>();
                        String userId = UserUtils.getUserSession(request).getOrgUserId();
                        Map<String, Object> model = new HashMap<>();
                        ObjectMapper mapper = new ObjectMapper();
@@ -148,8 +150,11 @@ public class AutoPushController extends RestrictedBaseController{
                        }else{
                                if(!scopes.isEmpty()){
                                        for(String scope : scopes){
-                                               String query = "From PolicyVersion where policy_name like '"+scope+"%' and id > 0";
-                                               List<Object> filterdatas = commonClassDao.getDataByQuery(query);
+                                               scope += "%";
+                                               String query = "From PolicyVersion where policy_name like :scope and id > 0";
+                                               SimpleBindings params = new SimpleBindings();
+                                               params.put("scope", scope);
+                                               List<Object> filterdatas = commonClassDao.getDataByQuery(query, params);
                                                if(filterdatas != null){
                                                        for(int i =0; i < filterdatas.size(); i++){
                                                                data.add(filterdatas.get(i));
@@ -173,7 +178,7 @@ public class AutoPushController extends RestrictedBaseController{
        }
 
        @RequestMapping(value={"/auto_Push/PushPolicyToPDP.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
-       public ModelAndView PushPolicyToPDPGroup(HttpServletRequest request, HttpServletResponse response) throws Exception {
+       public ModelAndView pushPolicyToPDPGroup(HttpServletRequest request, HttpServletResponse response) throws IOException {
                try {
                        ArrayList<Object> selectedPDPS = new ArrayList<>();
                        ArrayList<String> selectedPoliciesInUI = new ArrayList<>();
@@ -183,6 +188,12 @@ public class AutoPushController extends RestrictedBaseController{
                        this.container = new PDPGroupContainer(controller.getPapEngine());
                        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                        JsonNode root = mapper.readTree(request.getReader());
+                       
+                       String userId = UserUtils.getUserSession(request).getOrgUserId();
+                       logger.info("****************************************Logging UserID while Pushing  Policy to PDP Group*****************************************");
+                       logger.info("UserId:  " + userId + "Push Policy Data:  "+ root.get("pushTabData").toString());
+                       logger.info("***********************************************************************************************************************************");
+                       
                        AutoPushTabAdapter adapter = mapper.readValue(root.get("pushTabData").toString(), AutoPushTabAdapter.class);
                        for (Object pdpGroupId :  adapter.getPdpDatas()) {
                                LinkedHashMap<?, ?> selectedPDP = (LinkedHashMap<?, ?>)pdpGroupId;
@@ -220,7 +231,7 @@ public class AutoPushController extends RestrictedBaseController{
                                        String id = name;
                                        if (id.endsWith(".xml")) {
                                                id = id.replace(".xml", "");
-                                               id = id.substring(0, id.lastIndexOf("."));
+                                               id = id.substring(0, id.lastIndexOf('.'));
                                        }
                                        
                                        // Default policy to be Root policy; user can change to deferred
@@ -236,8 +247,11 @@ public class AutoPushController extends RestrictedBaseController{
                                                dbCheckName = dbCheckName.replace(".Decision_", ":Decision_");
                                        }
                                        String[] split = dbCheckName.split(":");
-                                       String query = "FROM PolicyEntity where policyName = '"+split[1]+"' and scope ='"+split[0]+"'";
-                                       List<Object> queryData = controller.getDataByQuery(query);
+                                       String query = "FROM PolicyEntity where policyName = :split_1 and scope = :split_0";
+                                       SimpleBindings policyParams = new SimpleBindings();
+                                       policyParams.put("split_1", split[1]);
+                                       policyParams.put("split_0", split[0]);
+                                       List<Object> queryData = controller.getDataByQuery(query, policyParams);
                                        PolicyEntity policyEntity = (PolicyEntity) queryData.get(0);
                                        File temp = new File(name);
                                        BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
@@ -249,7 +263,7 @@ public class AutoPushController extends RestrictedBaseController{
                                                // Create the policy
                                                selectedPolicy = new StdPDPPolicy(name, true, id, selectedURI);
                                        } catch (IOException e) {
-                                               logger.error("Unable to create policy '" + name + "': "+ e.getMessage());
+                                               logger.error("Unable to create policy '" + name + "': "+ e.getMessage(), e);
                                        }
                                        StdPDPGroup selectedGroup = (StdPDPGroup) pdpDestinationGroupId;
                                        if (selectedPolicy != null) {
@@ -275,6 +289,7 @@ public class AutoPushController extends RestrictedBaseController{
                                updatedGroupObject.setOnapPdps(pdpGroup.getOnapPdps());
                                updatedGroupObject.setPipConfigs(pdpGroup.getPipConfigs());
                                updatedGroupObject.setStatus(pdpGroup.getStatus());
+                               updatedGroupObject.setOperation("push");
 
                                // replace the original set of Policies with the set from the
                                // container (possibly modified by the user)
@@ -312,7 +327,7 @@ public class AutoPushController extends RestrictedBaseController{
                                refreshGroups();
                                JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
                                JSONObject j = new JSONObject(msg);
-                               out.write(j.toString());      
+                               out.write(j.toString());
                                return null;
                        }
                }
@@ -320,27 +335,34 @@ public class AutoPushController extends RestrictedBaseController{
                        response.setCharacterEncoding("UTF-8");
                        request.setCharacterEncoding("UTF-8");
                        PrintWriter out = response.getWriter();
-                       out.write(e.getMessage());
+                       logger.error(e);
+                       out.write(PolicyUtils.CATCH_EXCEPTION);;
                }
                return null;
        }
 
        @SuppressWarnings("unchecked")
        @RequestMapping(value={"/auto_Push/remove_GroupPolicies.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
-       public ModelAndView removePDPGroup(HttpServletRequest request, HttpServletResponse response) throws Exception {
+       public ModelAndView removePDPGroup(HttpServletRequest request, HttpServletResponse response) throws IOException {
                try {
                        PolicyController controller = getPolicyControllerInstance();
                        this.container = new PDPGroupContainer(controller.getPapEngine());
                        ObjectMapper mapper = new ObjectMapper();
                        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                        JsonNode root = mapper.readTree(request.getReader());  
-                       StdPDPGroup group = (StdPDPGroup)mapper.readValue(root.get("activePdpGroup").toString(), StdPDPGroup.class);
+                       StdPDPGroup group = mapper.readValue(root.get("activePdpGroup").toString(), StdPDPGroup.class);
                        JsonNode removePolicyData = root.get("data");
+                       
+                       String userId = UserUtils.getUserSession(request).getOrgUserId();
+                       logger.info("****************************************Logging UserID while Removing Policy from PDP Group*****************************************");
+                       logger.info("UserId:  " + userId + "PDP Group Data:  "+ root.get("activePdpGroup").toString() + "Remove Policy Data: "+root.get("data"));
+                       logger.info("***********************************************************************************************************************************");
+                       
                        policyContainer = new PDPPolicyContainer(group);
                        if(removePolicyData.size() > 0){
                                for(int i = 0 ; i < removePolicyData.size(); i++){
-                                       String data = removePolicyData.get(i).toString();
-                                       this.policyContainer.removeItem(data);
+                                       String polData = removePolicyData.get(i).toString();
+                                       this.policyContainer.removeItem(polData);
                                }
                                Set<PDPPolicy> changedPolicies = new HashSet<>();
                                changedPolicies.addAll((Collection<PDPPolicy>) this.policyContainer.getItemIds());
@@ -349,6 +371,7 @@ public class AutoPushController extends RestrictedBaseController{
                                updatedGroupObject.setOnapPdps(group.getOnapPdps());
                                updatedGroupObject.setPipConfigs(group.getPipConfigs());
                                updatedGroupObject.setStatus(group.getStatus());
+                               updatedGroupObject.setOperation("delete");
                                this.container.updateGroup(updatedGroupObject);
                        }
                        
@@ -369,9 +392,10 @@ public class AutoPushController extends RestrictedBaseController{
                        response.setCharacterEncoding("UTF-8");
                        request.setCharacterEncoding("UTF-8");
                        PrintWriter out = response.getWriter();
-                       out.write(e.getMessage());
+                       logger.error(e);
+                       out.write(PolicyUtils.CATCH_EXCEPTION);;
                }
                return null;
        }
 
-}
\ No newline at end of file
+}