Fixing the BRMS rule generation issue
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / CreateBRMSParamController.java
index 3107950..4dc4542 100644 (file)
 
 package org.onap.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;
@@ -178,7 +173,7 @@ public class CreateBRMSParamController extends RestrictedBaseController {
                                        if (flag) {
                                                params.append(line);
                                        }
-                                       if (line.contains("declare Params")) {
+                                       if (line.contains("declare PapParams")) {
                                                params.append(line);
                                                flag = true;
                                        }
@@ -186,7 +181,7 @@ public class CreateBRMSParamController extends RestrictedBaseController {
                                                break;
                                        }
                                }
-                               params = new StringBuilder(params.toString().replace("declare Params", "").replace("end", "").replaceAll("\\s+", ""));
+                               params = new StringBuilder(params.toString().replace("declare PapParams", "").replace("end", "").replaceAll("\\s+", ""));
                                String[] components = params.toString().split(":");
                                String caption = "";
                                for (int i = 0; i < components.length; i++) {
@@ -342,50 +337,22 @@ public class CreateBRMSParamController extends RestrictedBaseController {
                                policyAdapter.setGuard(value);
                        }
                        if ("TTLDate".equals(attributeId) && !value.contains("NA")){
-                               String newDate = convertDate(value, true);
+                               PolicyController controller = new PolicyController();
+                               String newDate = controller.convertDate(value);
                                policyAdapter.setTtlDate(newDate);
                        }
                }
        }
 
-       private String convertDate(String dateTTL, boolean portalType) {
-               String formateDate = null;
-               String[] date;
-               String[] parts;
-               
-               if (portalType){
-                       parts = dateTTL.split("-");
-                       formateDate = parts[2] + "-" + parts[1] + "-" + parts[0] + "T05:00:00.000Z";
-               } else {
-                       date  = dateTTL.split("T");
-                       parts = date[0].split("-");
-                       formateDate = parts[2] + "-" + parts[1] + "-" + parts[0];
-               }
-               return formateDate;
-       }
        // This method generates the UI from rule configuration
        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 : Files.readAllLines(Paths.get(file.toString()))) {
+                               for (String line : data.split("\n")) {
                                        if (line.isEmpty() || line.startsWith("//")) {
                                                continue;
                                        }
@@ -433,7 +400,7 @@ public class CreateBRMSParamController extends RestrictedBaseController {
                                        if (flag) {
                                                params.append(line);
                                        }
-                                       if (line.contains("rule") && line.contains(".Params\"")) {
+                                       if (line.contains("rule") && line.contains(".PapParams\"")) {
                                                params.append(line);
                                                flag = true;
                                        }
@@ -441,8 +408,8 @@ public class CreateBRMSParamController extends RestrictedBaseController {
                                                break;
                                        }
                                }
-                               params = new StringBuilder(params.substring(params.indexOf(".Params\"")+ 8));
-                               params = new StringBuilder(params.toString().replaceAll("\\s+", "").replace("salience1000whenthenParamsparams=newParams();","")
+                               params = new StringBuilder(params.substring(params.indexOf(".PapParams\"")+ 11));
+                               params = new StringBuilder(params.toString().replaceAll("\\s+", "").replace("salience1000whenthenPapParamsparams=newPapParams();","")
                         .replace("insert(params);end", "")
                         .replace("params.set", ""));
                                String[] components = params.toString().split("\\);");
@@ -495,7 +462,7 @@ public class CreateBRMSParamController extends RestrictedBaseController {
                                        brmsTemplateVlaue + policyData.getRuleName() + "%$> \n */ \n";
                        body = body + findRule((String) policyData.getRuleName()) + "\n";
                        StringBuilder generatedRule = new StringBuilder();
-                       generatedRule.append("rule \""+ policyData.getDomainDir().replace("\\", ".") +".Config_BRMS_Param_" + policyData.getPolicyName()+".Params\" \n\tsalience 1000 \n\twhen\n\tthen\n\t\tParams params = new Params();");
+                       generatedRule.append("rule \""+ policyData.getDomainDir().replace("\\", ".") +".Config_BRMS_Param_" + policyData.getPolicyName()+".PapParams\" \n\tsalience 1000 \n\twhen\n\tthen\n\t\tPapParams params = new PapParams();");
 
                        if(policyData.getRuleData().size() > 0){ 
                                for(Object keyValue: policyData.getRuleData().keySet()){