Remove auto generated rule to the drl
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / components / CreateBrmsParamPolicy.java
index 3ed2ee3..4de65fd 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP-PAP-REST
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -86,7 +86,7 @@ public class CreateBrmsParamPolicy extends Policy {
 
                Map<String,String> copyMap=new HashMap<>();
                copyMap.putAll(brmsParamBody);
-               copyMap.put("policyName", policyName.substring(0, policyName.replace(".xml", "").lastIndexOf(".")));
+               copyMap.put("policyName", policyName.substring(0, policyName.replace(".xml", "").lastIndexOf('.')));
                copyMap.put("policyScope", policyAdapter.getDomainDir());
                copyMap.put("policyVersion",policyAdapter.getHighestVersion().toString());
                copyMap.put("unique", ("p"+policyName+UUID.randomUUID().toString()).replaceAll("[^A-Za-z0-9]", ""));
@@ -123,12 +123,10 @@ public class CreateBrmsParamPolicy extends Policy {
        
        // Saving the Configurations file at server location for config policy.
        protected void saveConfigurations(String policyName, String ruleBody) {
-               try {
-                       if (policyName.endsWith(".xml")) {
-                               policyName = policyName.substring(0,
-                                               policyName.lastIndexOf(".xml"));
-                       }
-                       PrintWriter out = new PrintWriter(CONFIG_HOME + File.separator + policyName + ".txt");
+           if (policyName.endsWith(".xml")) {
+            policyName = policyName.substring(0, policyName.lastIndexOf(".xml"));
+        }
+               try (PrintWriter out = new PrintWriter(CONFIG_HOME + File.separator + policyName + ".txt")) {
                        String expandedBody=expandConfigBody(ruleBody,policyAdapter.getBrmsParamBody());
                        out.println(expandedBody);
                        policyAdapter.setJsonBody(expandedBody);
@@ -251,7 +249,7 @@ public class CreateBrmsParamPolicy extends Policy {
                                        if (flag) {
                                                params.append(line);
                                        }
-                                       if (line.contains("declare PapParams")) {
+                                       if (line.contains("declare Params")) {
                                                params.append(line);
                                                flag = true;
                                        }
@@ -259,7 +257,7 @@ public class CreateBrmsParamPolicy extends Policy {
                                                break;
                                        }
                                }
-                               String param = params.toString().replace("declare PapParams", "").replace("end", "")
+                               String param = params.toString().replace("declare Params", "").replace("end", "")
                                                .replaceAll("\\s+", "");
                                String[] components = param.split(":");
                                String caption = "";
@@ -327,60 +325,26 @@ public class CreateBrmsParamPolicy extends Policy {
                
                if (policyAdapter.getData() != null) {  
                        Map<String,String> ruleAndUIValue= policyAdapter.getBrmsParamBody();
-                       String tempateValue= ruleAndUIValue.get("templateName");
-                       String valueFromDictionary= getValueFromDictionary(tempateValue);
+                       String templateValue= ruleAndUIValue.get("templateName");
+                       String valueFromDictionary= getValueFromDictionary(templateValue);
                        
-                       //Get the type of the UI Fields. 
-                       Map<String,String> typeOfUIField=findType(valueFromDictionary);
-                       StringBuilder generatedRule = new StringBuilder();
                        StringBuilder body = new StringBuilder();
                        
                        try {
-                               
-                               try {
-                                       body.append("/* Autogenerated Code Please Don't change/remove this comment section. This is for the UI purpose. \n\t " +
-                                                               "<$%BRMSParamTemplate=" + tempateValue + "%$> \n */ \n");
-                                       body.append(valueFromDictionary + "\n");
-                                       generatedRule.append("rule \"" +policyName.substring(0, policyName.replace(".xml", "").lastIndexOf(".")) +".PapParams\" \n\tsalience 1000 \n\twhen\n\tthen\n\t\tPapParams params = new PapParams();");
-                                       
-                                       //We first read the map data structure(ruleAndUIValue) received from the PAP-ADMIN
-                                       //We ignore if the key is "templateName as we are interested only in the UI fields and its value. 
-                                       //We have one more map data structure(typeOfUIField) created by parsing the Drools rule. 
-                                       //From the type of the UI field(String/int) we structure whether to put the "" or not. 
-                                       for (Map.Entry<String, String> entry : ruleAndUIValue.entrySet()) {
-                                               if(entry.getKey()!="templateName")
-                                               {
-                                                       for(Map.Entry<String, String> fieldType:typeOfUIField.entrySet())
-                                                       {
-                                                               if(fieldType.getKey().equalsIgnoreCase(entry.getKey()))
-                                                               {
-                                                                       String key = entry.getKey().substring(0, 1).toUpperCase() + entry.getKey().substring(1); 
-                                                                       if(fieldType.getValue()=="String")
-                                                                       {
-                                                                               //Type is String
-                                                                               generatedRule.append("\n\t\tparams.set"
-                                                                                               + key + "(\""
-                                                                                               + entry.getValue() + "\");");
-                                                                       }
-                                                                       else{
-                                                                           generatedRule.append("\n\t\tparams.set"
-                                                                                               + key  + "("
-                                                                                               +  entry.getValue() + ");");
-                                                                       }
-                                                               }
-                                                       }
-                                               }
-                                       }
-                                       
-                                       generatedRule.append("\n\t\tinsert(params);\nend");
-                                       LOGGER.info("New rule generated with :" + generatedRule);
-                                       body.append(generatedRule);
-                               } catch (Exception e) {
-                                       PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "CreateBrmsParamPolicy", "Exception saving policy");
-                               }
+                           body.append("/* Autogenerated Code Please Don't change/remove this comment section. This is for the UI purpose. \n\t " +
+                                   "<$%BRMSParamTemplate=" + templateValue + "%$> \n");
+                           body.append("<%$Values=");
+                           for (Map.Entry<String, String> entry : ruleAndUIValue.entrySet()) {
+                               String uiKey = entry.getKey();
+                               if(!"templateName".equals(uiKey)) {
+                                   body.append(uiKey+":-:"+entry.getValue()+":|:");
+                               }
+                           }
+                           body.append("$%> \n*/ \n");
+                           body.append(valueFromDictionary + "\n");
                        }
                        catch (Exception e) {
-                               PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "CreateBrmsParamPolicy", "Exception saving policy");
+                           PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "CreateBrmsParamPolicy", "Exception saving policy");
                        }
                        
                        saveConfigurations(policyName,body.toString());