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=23c1e146852b43841c666aca584123dec896167e;hp=923e528faafa5ff928ca607f2f2f23241ef8e88d;hpb=59e3ddb0f0698965962a7d5879a6e39a80744648;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 923e528fa..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. @@ -34,6 +34,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -50,6 +51,7 @@ import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl; import org.onap.policy.rest.adapter.PolicyRestAdapter; import org.onap.policy.rest.jpa.BRMSParamTemplate; +import com.att.research.xacml.api.pap.PAPException; import com.att.research.xacml.std.IdentifierImpl; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType; @@ -84,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]", "")); @@ -115,27 +117,21 @@ public class CreateBrmsParamPolicy extends Policy { // Utility to read json data from the existing file to a string static String readFile(String path, Charset encoding) throws IOException { - byte[] encoded = Files.readAllBytes(Paths.get(path)); return new String(encoded, encoding); - } // 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); policyAdapter.setConfigBodyData(expandedBody); out.close(); - - } catch (Exception e) { PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "CreateBrmsParamPolicy", "Exception saving configuration file"); } @@ -164,7 +160,7 @@ public class CreateBrmsParamPolicy extends Policy { } @Override - public Map savePolicies() throws Exception { + public Map savePolicies() throws PAPException { Map successMap = new HashMap<>(); if(isPolicyExists()){ @@ -206,7 +202,7 @@ public class CreateBrmsParamPolicy extends Policy { Map mapFieldType= new HashMap<>(); if(rule!=null){ try { - String params = ""; + StringBuilder params = new StringBuilder(); Boolean flag = false; Boolean comment = false; String lines[] = rule.split("\n"); @@ -231,6 +227,7 @@ public class CreateBrmsParamPolicy extends Policy { line = line.split("\\/\\*")[0] + line.split("\\*\\/")[1].replace("*/", ""); } catch (Exception e) { + LOGGER.debug(e); line = line.split("\\/\\*")[0]; } } else { @@ -242,6 +239,7 @@ public class CreateBrmsParamPolicy extends Policy { try { line = line.split("\\*\\/")[1].replace("*/", ""); } catch (Exception e) { + LOGGER.debug(e); line = ""; } } @@ -249,19 +247,19 @@ public class CreateBrmsParamPolicy extends Policy { continue; } if (flag) { - params = params + line; + params.append(line); } if (line.contains("declare Params")) { - params = params + line; + params.append(line); flag = true; } if (line.contains("end") && flag) { break; } } - params = params.replace("declare Params", "").replace("end", "") + String param = params.toString().replace("declare Params", "").replace("end", "") .replaceAll("\\s+", ""); - String[] components = params.split(":"); + String[] components = param.split(":"); String caption = ""; for (int i = 0; i < components.length; i++) { String type = ""; @@ -275,6 +273,7 @@ public class CreateBrmsParamPolicy extends Policy { try { nextComponent = components[i + 1]; } catch (Exception e) { + LOGGER.debug(e); nextComponent = components[i]; } //If the type is of type String then we add the UI Item and type to the map. @@ -300,7 +299,7 @@ public class CreateBrmsParamPolicy extends Policy { // separately because the fully configured policy is used for multiple // things @Override - public boolean prepareToSave() throws Exception { + public boolean prepareToSave() throws PAPException { if (isPreparedToSave()) { // we have already done this @@ -326,67 +325,32 @@ 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); - String generatedRule=null; - String body = ""; + StringBuilder body = new StringBuilder(); try { - - try { - body = "/* Autogenerated Code Please Don't change/remove this comment section. This is for the UI purpose. \n\t " + - "<$%BRMSParamTemplate=" + tempateValue + "%$> \n */ \n"; - body = body + valueFromDictionary + "\n"; - generatedRule = "rule \"" +policyName.substring(0, policyName.replace(".xml", "").lastIndexOf(".")) +".Params\" \n\tsalience 1000 \n\twhen\n\tthen\n\t\tParams params = new Params();"; - - //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 = generatedRule + "\n\t\tparams.set" - + key + "(\"" - + entry.getValue() + "\");"; - } - else{ - generatedRule = generatedRule + "\n\t\tparams.set" - + key + "(" - + entry.getValue() + ");"; - } - } - } - } - } - - generatedRule = generatedRule - + "\n\t\tinsert(params);\nend"; - LOGGER.info("New rule generated with :" + generatedRule); - body = body + 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); + saveConfigurations(policyName,body.toString()); // Make sure the filename ends with an extension - if (policyName.endsWith(".xml") == false) { + if (!policyName.endsWith(".xml")) { policyName = policyName + ".xml"; } @@ -425,7 +389,7 @@ public class CreateBrmsParamPolicy extends Policy { anyOf.getAllOf().add(allOf); TargetType target = new TargetType(); - ((TargetType) target).getAnyOf().add(anyOf); + target.getAnyOf().add(anyOf); // Adding the target to the policy element configPolicy.setTarget((TargetType) target); @@ -532,14 +496,14 @@ public class CreateBrmsParamPolicy extends Policy { assignment2.setAttributeId("URLID"); assignment2.setCategory(CATEGORY_RESOURCE); assignment2.setIssuer(""); - AttributeValueType AttributeValue = new AttributeValueType(); - AttributeValue.setDataType(URI_DATATYPE); + AttributeValueType attributeValue = new AttributeValueType(); + attributeValue.setDataType(URI_DATATYPE); String content = CONFIG_URL + "/Config/"+ getConfigFile(policyName); - AttributeValue.getContent().add(content); + attributeValue.getContent().add(content); assignment2.setExpression(new ObjectFactory() - .createAttributeValue(AttributeValue)); + .createAttributeValue(attributeValue)); advice.getAttributeAssignmentExpression().add(assignment2); // Policy Name Assignment @@ -620,8 +584,8 @@ public class CreateBrmsParamPolicy extends Policy { // Dynamic Field Config Attributes. Map dynamicFieldConfigAttributes = policyAdapter.getDynamicFieldConfigAttributes(); - for (String keyField : dynamicFieldConfigAttributes.keySet()) { - advice.getAttributeAssignmentExpression().add(createResponseAttributes("key:"+keyField, dynamicFieldConfigAttributes.get(keyField))); + for (Entry map : dynamicFieldConfigAttributes.entrySet()) { + advice.getAttributeAssignmentExpression().add(createResponseAttributes("key:"+map.getKey(), map.getValue())); } //Risk Attributes