* ============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.
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;
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]", ""));
// 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");
}
Map<String, String> mapFieldType= new HashMap<>();
if(rule!=null){
try {
- String params = "";
+ StringBuilder params = new StringBuilder();
Boolean flag = false;
Boolean comment = false;
String lines[] = rule.split("\n");
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 = "";
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);
- 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<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 = 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<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);
+ saveConfigurations(policyName,body.toString());
// Make sure the filename ends with an extension
- if (policyName.endsWith(".xml") == false) {
+ if (!policyName.endsWith(".xml")) {
policyName = policyName + ".xml";
}
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);
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
// Dynamic Field Config Attributes.
Map<String, String> dynamicFieldConfigAttributes = policyAdapter.getDynamicFieldConfigAttributes();
- for (String keyField : dynamicFieldConfigAttributes.keySet()) {
- advice.getAttributeAssignmentExpression().add(createResponseAttributes("key:"+keyField, dynamicFieldConfigAttributes.get(keyField)));
+ for (Entry<String, String> map : dynamicFieldConfigAttributes.entrySet()) {
+ advice.getAttributeAssignmentExpression().add(createResponseAttributes("key:"+map.getKey(), map.getValue()));
}
//Risk Attributes