aboutsummaryrefslogtreecommitdiffstats
path: root/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsParamPolicy.java
diff options
context:
space:
mode:
Diffstat (limited to 'ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsParamPolicy.java')
-rw-r--r--ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsParamPolicy.java139
1 files changed, 58 insertions, 81 deletions
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 6a0c92650..ffb902bc4 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-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2019 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.
@@ -20,6 +20,9 @@
package org.onap.policy.pap.xacml.rest.components;
+import com.att.research.xacml.api.pap.PAPException;
+import com.att.research.xacml.std.IdentifierImpl;
+import com.google.common.annotations.VisibleForTesting;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
@@ -38,22 +41,7 @@ import java.util.Map.Entry;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-
import javax.script.SimpleBindings;
-
-import org.apache.commons.io.FilenameUtils;
-import org.onap.policy.common.logging.eelf.MessageCodes;
-import org.onap.policy.common.logging.eelf.PolicyLogger;
-import org.onap.policy.common.logging.flexlogger.FlexLogger;
-import org.onap.policy.common.logging.flexlogger.Logger;
-import org.onap.policy.pap.xacml.rest.controller.BRMSDictionaryController;
-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;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
@@ -67,6 +55,15 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
+import org.apache.commons.io.FilenameUtils;
+import org.onap.policy.common.logging.eelf.MessageCodes;
+import org.onap.policy.common.logging.eelf.PolicyLogger;
+import org.onap.policy.common.logging.flexlogger.FlexLogger;
+import org.onap.policy.common.logging.flexlogger.Logger;
+import org.onap.policy.pap.xacml.rest.controller.BRMSDictionaryController;
+import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
+import org.onap.policy.rest.adapter.PolicyRestAdapter;
+import org.onap.policy.rest.jpa.BrmsParamTemplate;
public class CreateBrmsParamPolicy extends Policy {
@@ -91,19 +88,18 @@ public class CreateBrmsParamPolicy extends Policy {
copyMap.put("policyVersion", policyAdapter.getHighestVersion().toString());
copyMap.put("unique", ("p" + policyName + UUID.randomUUID().toString()).replaceAll("[^A-Za-z0-9]", ""));
- //Finding all the keys in the Map data-structure.
+ // Finding all the keys in the Map data-structure.
Iterator<String> iterator = copyMap.keySet().iterator();
Pattern p;
Matcher m;
while (iterator.hasNext()) {
- //Converting the first character of the key into a lower case.
+ // Converting the first character of the key into a lower case.
String input = iterator.next();
- String output = Character.toLowerCase(input.charAt(0)) +
- (input.length() > 1 ? input.substring(1) : "");
- //Searching for a pattern in the String using the key.
+ String output = Character.toLowerCase(input.charAt(0)) + (input.length() > 1 ? input.substring(1) : "");
+ // Searching for a pattern in the String using the key.
p = Pattern.compile("\\$\\{" + output + "\\}");
m = p.matcher(ruleContents);
- //Replacing the value with the inputs provided by the user in the editor.
+ // Replacing the value with the inputs provided by the user in the editor.
String finalInput = copyMap.get(input);
if (finalInput.contains("$")) {
finalInput = finalInput.replace("$", "\\$");
@@ -113,7 +109,6 @@ public class CreateBrmsParamPolicy extends Policy {
return ruleContents;
}
-
// 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));
@@ -133,11 +128,10 @@ public class CreateBrmsParamPolicy extends Policy {
out.close();
} catch (Exception e) {
PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "CreateBrmsParamPolicy",
- "Exception saving configuration file");
+ "Exception saving configuration file");
}
}
-
// Here we are adding the extension for the configurations file based on the
// config type selection for saving.
private String getConfigFile(String filename) {
@@ -151,6 +145,7 @@ public class CreateBrmsParamPolicy extends Policy {
}
// Validations for Config form
+ @Override
public boolean validateConfigForm() {
// Validating mandatory Fields.
@@ -187,12 +182,12 @@ public class CreateBrmsParamPolicy extends Policy {
private String getValueFromDictionary(String templateName) {
String ruleTemplate = null;
CommonClassDaoImpl dbConnection = new CommonClassDaoImpl();
- String queryString = "from BRMSParamTemplate where param_template_name= :templateName";
+ String queryString = "from BrmsParamTemplate where param_template_name= :templateName";
SimpleBindings params = new SimpleBindings();
params.put("templateName", templateName);
List<Object> result = dbConnection.getDataByQuery(queryString, params);
if (!result.isEmpty()) {
- BRMSParamTemplate template = (BRMSParamTemplate) result.get(0);
+ BrmsParamTemplate template = (BrmsParamTemplate) result.get(0);
ruleTemplate = template.getRule();
}
return ruleTemplate;
@@ -222,8 +217,7 @@ public class CreateBrmsParamPolicy extends Policy {
if (line.contains("*/")) {
try {
comment = false;
- line = line.split("\\/\\*")[0]
- + line.split("\\*\\/")[1].replace("*/", "");
+ line = line.split("\\/\\*")[0] + line.split("\\*\\/")[1].replace("*/", "");
} catch (Exception e) {
LOGGER.debug(e);
line = line.split("\\/\\*")[0];
@@ -255,8 +249,8 @@ public class CreateBrmsParamPolicy extends Policy {
break;
}
}
- String param = params.toString().replace("declare Params", "").replace("end", "")
- .replaceAll("\\s+", "");
+ String param =
+ params.toString().replace("declare Params", "").replace("end", "").replaceAll("\\s+", "");
String[] components = param.split(":");
String caption = "";
for (int i = 0; i < components.length; i++) {
@@ -274,7 +268,7 @@ public class CreateBrmsParamPolicy extends Policy {
LOGGER.debug(e);
nextComponent = components[i];
}
- //If the type is of type String then we add the UI Item and type to the map.
+ // If the type is of type String then we add the UI Item and type to the map.
if (nextComponent.startsWith("String")) {
type = "String";
mapFieldType.put(caption, type);
@@ -287,7 +281,7 @@ public class CreateBrmsParamPolicy extends Policy {
}
} catch (Exception e) {
PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, "CreateBrmsParamPolicy",
- "Exception parsing file in findType");
+ "Exception parsing file in findType");
}
}
return mapFieldType;
@@ -330,10 +324,8 @@ public class CreateBrmsParamPolicy extends Policy {
StringBuilder body = new StringBuilder();
try {
- 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("/* 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();
@@ -344,8 +336,8 @@ public class CreateBrmsParamPolicy extends Policy {
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());
@@ -366,12 +358,10 @@ public class CreateBrmsParamPolicy extends Policy {
String fileName = policyAdapter.getNewFileName();
String name = fileName.substring(fileName.lastIndexOf("\\") + 1);
if ((name == null) || (name.equals(""))) {
- name = fileName.substring(fileName.lastIndexOf("/") + 1
- );
+ name = fileName.substring(fileName.lastIndexOf("/") + 1);
}
allOfOne.getMatch().add(createMatch("PolicyName", name));
-
AllOfType allOf = new AllOfType();
// Match for ONAPName
@@ -415,12 +405,11 @@ public class CreateBrmsParamPolicy extends Policy {
accessURI = new URI(ACTION_ID);
} catch (URISyntaxException e) {
PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "CreateBrmsParamPolicy",
- "Exception creating ACCESS URI");
+ "Exception creating ACCESS URI");
}
accessAttributeDesignator.setCategory(CATEGORY_ACTION);
accessAttributeDesignator.setDataType(STRING_DATATYPE);
- accessAttributeDesignator.setAttributeId(new IdentifierImpl(
- accessURI).stringValue());
+ accessAttributeDesignator.setAttributeId(new IdentifierImpl(accessURI).stringValue());
accessMatch.setAttributeDesignator(accessAttributeDesignator);
accessMatch.setMatchId(FUNCTION_STRING_EQUAL_IGNORE);
@@ -438,7 +427,7 @@ public class CreateBrmsParamPolicy extends Policy {
configURI = new URI(RESOURCE_ID);
} catch (URISyntaxException e) {
PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "CreateBrmsParamPolicy",
- "Exception creating Config URI");
+ "Exception creating Config URI");
}
configAttributeDesignator.setCategory(CATEGORY_RESOURCE);
@@ -459,24 +448,21 @@ public class CreateBrmsParamPolicy extends Policy {
rule.setTarget(targetInRule);
rule.setAdviceExpressions(getAdviceExpressions(version, policyName));
- configPolicy
- .getCombinerParametersOrRuleCombinerParametersOrVariableDefinition()
- .add(rule);
+ configPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
policyAdapter.setPolicyData(configPolicy);
} else {
- PolicyLogger.error("Unsupported data object."
- + policyAdapter.getData().getClass().getCanonicalName());
+ PolicyLogger.error("Unsupported data object." + policyAdapter.getData().getClass().getCanonicalName());
}
setPreparedToSave(true);
return true;
}
// Data required for Advice part is setting here.
- private AdviceExpressionsType getAdviceExpressions(int version,
- String fileName) {
+ @VisibleForTesting
+ protected AdviceExpressionsType getAdviceExpressions(int version, String fileName) {
- //Policy Config ID Assignment
+ // Policy Config ID Assignment
AdviceExpressionsType advices = new AdviceExpressionsType();
AdviceExpressionType advice = new AdviceExpressionType();
advice.setAdviceId("BRMSPARAMID");
@@ -489,8 +475,7 @@ public class CreateBrmsParamPolicy extends Policy {
AttributeValueType configNameAttributeValue = new AttributeValueType();
configNameAttributeValue.setDataType(STRING_DATATYPE);
configNameAttributeValue.getContent().add("Configuration");
- assignment1.setExpression(new ObjectFactory()
- .createAttributeValue(configNameAttributeValue));
+ assignment1.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue));
advice.getAttributeAssignmentExpression().add(assignment1);
// For Config file Url if configurations are provided.
@@ -505,8 +490,7 @@ public class CreateBrmsParamPolicy extends Policy {
String content = CONFIG_URL + "/Config/" + getConfigFile(policyName);
attributeValue.getContent().add(content);
- assignment2.setExpression(new ObjectFactory()
- .createAttributeValue(attributeValue));
+ assignment2.setExpression(new ObjectFactory().createAttributeValue(attributeValue));
advice.getAttributeAssignmentExpression().add(assignment2);
// Policy Name Assignment
@@ -518,15 +502,12 @@ public class CreateBrmsParamPolicy extends Policy {
attributeValue3.setDataType(STRING_DATATYPE);
fileName = FilenameUtils.removeExtension(fileName);
fileName = fileName + ".xml";
- String name = fileName.substring(fileName.lastIndexOf("\\") + 1
- );
+ String name = fileName.substring(fileName.lastIndexOf("\\") + 1);
if ((name == null) || (name.equals(""))) {
- name = fileName.substring(fileName.lastIndexOf("/") + 1
- );
+ name = fileName.substring(fileName.lastIndexOf("/") + 1);
}
attributeValue3.getContent().add(name);
- assignment3.setExpression(new ObjectFactory()
- .createAttributeValue(attributeValue3));
+ assignment3.setExpression(new ObjectFactory().createAttributeValue(attributeValue3));
advice.getAttributeAssignmentExpression().add(assignment3);
// Version Number Assignment
@@ -537,8 +518,7 @@ public class CreateBrmsParamPolicy extends Policy {
AttributeValueType configNameAttributeValue4 = new AttributeValueType();
configNameAttributeValue4.setDataType(STRING_DATATYPE);
configNameAttributeValue4.getContent().add(Integer.toString(version));
- assignment4.setExpression(new ObjectFactory()
- .createAttributeValue(configNameAttributeValue4));
+ assignment4.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue4));
advice.getAttributeAssignmentExpression().add(assignment4);
// Onap Name Assignment
@@ -549,12 +529,10 @@ public class CreateBrmsParamPolicy extends Policy {
AttributeValueType configNameAttributeValue5 = new AttributeValueType();
configNameAttributeValue5.setDataType(STRING_DATATYPE);
configNameAttributeValue5.getContent().add(policyAdapter.getOnapName());
- assignment5.setExpression(new ObjectFactory()
- .createAttributeValue(configNameAttributeValue5));
+ assignment5.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue5));
advice.getAttributeAssignmentExpression().add(assignment5);
-
- //Config Name Assignment
+ // Config Name Assignment
AttributeAssignmentExpressionType assignment6 = new AttributeAssignmentExpressionType();
assignment6.setAttributeId("matching:" + CONFIGID);
assignment6.setCategory(CATEGORY_RESOURCE);
@@ -564,16 +542,15 @@ public class CreateBrmsParamPolicy extends Policy {
configNameAttributeValue6.getContent().add(policyAdapter.getConfigName());
assignment6.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue6));
advice.getAttributeAssignmentExpression().add(assignment6);
- // Adding Controller Information.
+ // Adding Controller Information.
if (policyAdapter.getBrmsController() != null) {
BRMSDictionaryController brmsDicitonaryController = new BRMSDictionaryController();
- advice.getAttributeAssignmentExpression().add(
- createResponseAttributes("controller:" + policyAdapter.getBrmsController(),
- brmsDicitonaryController.getControllerDataByID(policyAdapter.getBrmsController())
- .getController()));
+ advice.getAttributeAssignmentExpression()
+ .add(createResponseAttributes("controller:" + policyAdapter.getBrmsController(),
+ brmsDicitonaryController.getControllerDataByID(policyAdapter.getBrmsController()).getController()));
}
- // Adding Dependencies.
+ // Adding Dependencies.
if (policyAdapter.getBrmsDependency() != null) {
BRMSDictionaryController brmsDicitonaryController = new BRMSDictionaryController();
ArrayList<String> dependencies = new ArrayList<>();
@@ -582,18 +559,18 @@ public class CreateBrmsParamPolicy extends Policy {
dependencies.add(brmsDicitonaryController.getDependencyDataByID(dependencyName).getDependency());
key.append(dependencyName + ",");
}
- advice.getAttributeAssignmentExpression().add(
- createResponseAttributes("dependencies:" + key.toString(), dependencies.toString()));
+ advice.getAttributeAssignmentExpression()
+ .add(createResponseAttributes("dependencies:" + key.toString(), dependencies.toString()));
}
- // Dynamic Field Config Attributes.
+ // Dynamic Field Config Attributes.
Map<String, String> dynamicFieldConfigAttributes = policyAdapter.getDynamicFieldConfigAttributes();
for (Entry<String, String> map : dynamicFieldConfigAttributes.entrySet()) {
advice.getAttributeAssignmentExpression()
- .add(createResponseAttributes("key:" + map.getKey(), map.getValue()));
+ .add(createResponseAttributes("key:" + map.getKey(), map.getValue()));
}
- //Risk Attributes
+ // Risk Attributes
AttributeAssignmentExpressionType assignment8 = new AttributeAssignmentExpressionType();
assignment8.setAttributeId("RiskType");
assignment8.setCategory(CATEGORY_RESOURCE);