[POLICY-117] Resolve the Policy Critical issues
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / openecomp / policy / controller / CreateBRMSParamController.java
index 68dff19..18ad9fc 100644 (file)
 
 package org.openecomp.policy.controller;
 
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
 import java.io.PrintWriter;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -37,17 +42,18 @@ import javax.servlet.http.HttpServletResponse;
 import javax.xml.bind.JAXBElement;
 
 import org.json.JSONObject;
+import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
+import org.openecomp.policy.common.logging.flexlogger.Logger;
 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
 import org.openecomp.policy.rest.dao.CommonClassDao;
 import org.openecomp.policy.rest.jpa.BRMSParamTemplate;
 import org.openecomp.policy.rest.jpa.PolicyEntity;
+import org.openecomp.policy.xacml.api.XACMLErrorConstants;
 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.servlet.ModelAndView;
 
-import org.openecomp.policy.xacml.api.XACMLErrorConstants;
 import com.fasterxml.jackson.databind.DeserializationFeature;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -64,9 +70,6 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
 
-import org.openecomp.policy.common.logging.flexlogger.FlexLogger; 
-import org.openecomp.policy.common.logging.flexlogger.Logger;
-
 @Controller
 @RequestMapping("/")
 public class CreateBRMSParamController extends RestrictedBaseController {
@@ -74,6 +77,14 @@ public class CreateBRMSParamController extends RestrictedBaseController {
 
        private static CommonClassDao commonClassDao;
 
+       public static CommonClassDao getCommonClassDao() {
+               return commonClassDao;
+       }
+
+       public static void setCommonClassDao(CommonClassDao commonClassDao) {
+               CreateBRMSParamController.commonClassDao = commonClassDao;
+       }
+       
        @Autowired
        private CreateBRMSParamController(CommonClassDao commonClassDao){
                CreateBRMSParamController.commonClassDao = commonClassDao;
@@ -85,35 +96,36 @@ public class CreateBRMSParamController extends RestrictedBaseController {
        private HashMap<String, String> dynamicLayoutMap;
        
        private static String brmsTemplateVlaue = "<$%BRMSParamTemplate=";
-       private static String String = "String";
+       private static String string = "String";
 
 
        @RequestMapping(value={"/policyController/getBRMSTemplateData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
-       public ModelAndView getBRMSParamPolicyRuleData(HttpServletRequest request, HttpServletResponse response) throws Exception{
-               dynamicLayoutMap = new HashMap<>();
-               ObjectMapper mapper = new ObjectMapper();
-               mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
-               JsonNode root = mapper.readTree(request.getReader());
-               String rule = findRule(root.get(PolicyController.getPolicydata()).toString().replaceAll("^\"|\"$", ""));
-               generateUI(rule);
-               response.setCharacterEncoding(PolicyController.getCharacterencoding());
-               response.setContentType(PolicyController.getContenttype());
-               request.setCharacterEncoding(PolicyController.getCharacterencoding());
+       public void getBRMSParamPolicyRuleData(HttpServletRequest request, HttpServletResponse response){
+               try{
+                       dynamicLayoutMap = new HashMap<>();
+                       ObjectMapper mapper = new ObjectMapper();
+                       mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+                       JsonNode root = mapper.readTree(request.getReader());
+                       String rule = findRule(root.get(PolicyController.getPolicydata()).toString().replaceAll("^\"|\"$", ""));
+                       generateUI(rule);
+                       response.setCharacterEncoding(PolicyController.getCharacterencoding());
+                       response.setContentType(PolicyController.getContenttype());
+                       request.setCharacterEncoding(PolicyController.getCharacterencoding());
 
-               PrintWriter out = response.getWriter();
-               String responseString = mapper.writeValueAsString(dynamicLayoutMap);
-               JSONObject j = new JSONObject("{policyData: " + responseString + "}");
-               out.write(j.toString());
-               return null;
+                       PrintWriter out = response.getWriter();
+                       String responseString = mapper.writeValueAsString(dynamicLayoutMap);
+                       JSONObject j = new JSONObject("{policyData: " + responseString + "}");
+                       out.write(j.toString());
+               }catch(Exception e){
+                       policyLogger.error("Exception Occured while getting BRMS Rule data" , e);
+               }
        }
 
        protected String findRule(String ruleTemplate) {
-               List<Object> datas = commonClassDao.getData(BRMSParamTemplate.class);
-               for (Object data: datas){
-                       BRMSParamTemplate  bRMSParamTemplate = (BRMSParamTemplate) data;
-                       if(bRMSParamTemplate.getRuleName().equals(ruleTemplate)){
-                               return bRMSParamTemplate.getRule();
-                       }
+               List<Object> datas = commonClassDao.getDataById(BRMSParamTemplate.class, "ruleName", ruleTemplate);
+               if(datas != null && !datas.isEmpty()){
+                       BRMSParamTemplate  bRMSParamTemplate = (BRMSParamTemplate) datas.get(0);
+                       return bRMSParamTemplate.getRule();
                }
                return null;
        }
@@ -192,10 +204,10 @@ public class CreateBRMSParamController extends RestrictedBaseController {
                                                policyLogger.info("Just for Logging"+e);
                                                nextComponent = components[i];
                                        }
-                                       if (nextComponent.startsWith(String)) {
+                                       if (nextComponent.startsWith(string)) {
                                                type = "String";
                                                createField(caption, type);
-                                               caption = nextComponent.replace(String, "");
+                                               caption = nextComponent.replace(string, "");
                                        } else if (nextComponent.startsWith("int")) {
                                                type = "int";
                                                createField(caption, type);
@@ -352,14 +364,28 @@ public class CreateBRMSParamController extends RestrictedBaseController {
                return formateDate;
        }
        // This method generates the UI from rule configuration
-       private void paramUIGenerate(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
+       public void paramUIGenerate(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
                String data = entity.getConfigurationData().getConfigBody();
                if(data != null){
+                       File file = new File(PolicyController.getConfigHome() +File.separator+ entity.getConfigurationData().getConfigurationName());
+                       if(file.exists()){
+                               try (BufferedReader br = new BufferedReader(new FileReader(file))) {
+                                       StringBuilder sb = new StringBuilder();
+                                       String line = br.readLine();
+                                       while (line != null) {
+                                               sb.append(line);
+                                               sb.append("\n");
+                                               line = br.readLine();
+                                       }
+                               }catch(Exception e){
+                                       policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+ e.getMessage() + e);
+                               }
+                       }
                        try {   
                                StringBuilder params = new StringBuilder("");
                                Boolean flag = false;
                                Boolean comment = false;
-                               for (String line : data.split("\n")) {
+                               for (String line : Files.readAllLines(Paths.get(file.toString()))) {
                                        if (line.isEmpty() || line.startsWith("//")) {
                                                continue;
                                        }
@@ -452,7 +478,7 @@ public class CreateBRMSParamController extends RestrictedBaseController {
        // set View Rule
        @SuppressWarnings("unchecked")
        @RequestMapping(value={"/policyController/ViewBRMSParamPolicyRule.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
-       public ModelAndView setViewRule(HttpServletRequest request, HttpServletResponse response) throws Exception{
+       public void setViewRule(HttpServletRequest request, HttpServletResponse response){
                try {
                        ObjectMapper mapper = new ObjectMapper();
                        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
@@ -474,7 +500,7 @@ public class CreateBRMSParamController extends RestrictedBaseController {
                        if(policyData.getRuleData().size() > 0){ 
                                for(Object keyValue: policyData.getRuleData().keySet()){ 
                                        String key = keyValue.toString().substring(0, 1).toUpperCase() + keyValue.toString().substring(1); 
-                                       if (String.equals(keyValue)) { 
+                                       if (string.equals(keyValue)) { 
                                                generatedRule.append("\n\t\tparams.set" 
                                                                + key + "(\"" 
                                                                + policyData.getRuleData().get(keyValue).toString() + "\");"); 
@@ -518,10 +544,8 @@ public class CreateBRMSParamController extends RestrictedBaseController {
                        String responseString = mapper.writeValueAsString(body);
                        JSONObject j = new JSONObject("{policyData: " + responseString + "}");
                        out.write(j.toString());
-                       return null;
                } catch (Exception e) {
                        policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
-               }
-               return null;    
+               }       
        }
 }