X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ONAP-PAP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fpap%2Fxacml%2Frest%2Fcomponents%2FCreateBrmsParamPolicy.java;h=4de65fd1e5a99ee569acd66910508a1237101af5;hb=refs%2Fchanges%2F65%2F33865%2F4;hp=3ed2ee37df22b447ddfb3c7b43f25c7854c3240d;hpb=44a296f53e8faf2b458c59355e5a2506ad46aca5;p=policy%2Fengine.git diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsParamPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsParamPolicy.java index 3ed2ee37d..4de65fd1e 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsParamPolicy.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsParamPolicy.java @@ -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 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 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 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 entry : ruleAndUIValue.entrySet()) { - if(entry.getKey()!="templateName") - { - for(Map.Entry 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 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());