aboutsummaryrefslogtreecommitdiffstats
path: root/controlloop/common/guard
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-08-05 14:17:01 -0400
committerJim Hahn <jrh3@att.com>2019-08-13 09:24:52 -0400
commit9e8023a455633c7daf0dd291c6268986e39352d7 (patch)
treedc25ae63c4a042a7f16c53d8c50ebcdb4493f5cb /controlloop/common/guard
parent177b67a70f99e0125fec7732f39c18840409f968 (diff)
Fix sonar issues in drools-applications
Addressed sonar issue, "Move constants to a class or enum.", by moving the "manager" object from the ControlLoopMetrics interface into a utility class, ControlLoopMetricsManager. Addressed sonar issue, "duplicated blocks of code must be removed.", by refactoring PolicyGuardYamlToXacml, extracing common functions. Addressed sonar issue, "Remove this unused import", in RestControlLoopManager. Addressed sonar issue, "Refactor this method to throw at most one checked exception", in event manager. Fixed likely new sonar issue with assigning to a parameter. Moved logging line to more appropriate place. Addressed reviewer comment: Use "replace" instead of "replaceAll", thus avoiding escaping Change-Id: I47db957c83c1b3e2bd2330474e261987c6f0aac6 Issue-ID: POLICY-1967 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'controlloop/common/guard')
-rw-r--r--controlloop/common/guard/src/main/java/org/onap/policy/guard/PolicyGuardYamlToXacml.java132
1 files changed, 47 insertions, 85 deletions
diff --git a/controlloop/common/guard/src/main/java/org/onap/policy/guard/PolicyGuardYamlToXacml.java b/controlloop/common/guard/src/main/java/org/onap/policy/guard/PolicyGuardYamlToXacml.java
index a64abcad4..057c3f116 100644
--- a/controlloop/common/guard/src/main/java/org/onap/policy/guard/PolicyGuardYamlToXacml.java
+++ b/controlloop/common/guard/src/main/java/org/onap/policy/guard/PolicyGuardYamlToXacml.java
@@ -2,14 +2,14 @@
* ============LICENSE_START=======================================================
* guard
* ================================================================================
- * Copyright (C) 2017 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.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -25,9 +25,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
+import java.util.function.Consumer;
import org.onap.policy.controlloop.policy.guard.Constraint;
import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
import org.onap.policy.controlloop.policy.guard.GuardPolicy;
@@ -44,7 +42,7 @@ public class PolicyGuardYamlToXacml {
/**
* Convert from Yaml to Xacml.
- *
+ *
* @param yamlFile the Yaml file
* @param xacmlTemplate the Xacml template
* @param xacmlPolicyOutput the Xacml output
@@ -78,7 +76,7 @@ public class PolicyGuardYamlToXacml {
/**
* Generate a Xacml guard.
- *
+ *
* @param xacmlTemplateContent the Xacml template content
* @param matchParameters the paremeters to use
* @param constraint the constraint to use
@@ -86,29 +84,9 @@ public class PolicyGuardYamlToXacml {
*/
private static String generateXacmlGuard(String xacmlTemplateContent, MatchParameters matchParameters,
Constraint constraint) {
- Pattern pattern = Pattern.compile("\\$\\{clname\\}");
- Matcher matcher = pattern.matcher(xacmlTemplateContent);
- if (isNullOrEmpty(matchParameters.getControlLoopName())) {
- matchParameters.setControlLoopName(".*");
- }
- xacmlTemplateContent = matcher.replaceAll(matchParameters.getControlLoopName());
- pattern = Pattern.compile("\\$\\{actor\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- if (isNullOrEmpty(matchParameters.getActor())) {
- matchParameters.setActor(".*");
- }
- xacmlTemplateContent = matcher.replaceAll(matchParameters.getActor());
+ xacmlTemplateContent = doCommonReplacements(xacmlTemplateContent, matchParameters, constraint);
- pattern = Pattern.compile("\\$\\{recipe\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- if (isNullOrEmpty(matchParameters.getRecipe())) {
- matchParameters.setRecipe(".*");
- }
- xacmlTemplateContent = matcher.replaceAll(matchParameters.getRecipe());
-
- pattern = Pattern.compile("\\$\\{targets\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
String targetsRegex = "";
if (isNullOrEmptyList(matchParameters.getTargets())) {
targetsRegex = ".*";
@@ -125,33 +103,47 @@ public class PolicyGuardYamlToXacml {
}
targetsRegex = targetsRegexSb.toString();
}
- xacmlTemplateContent = matcher.replaceAll(targetsRegex);
+ xacmlTemplateContent = xacmlTemplateContent.replace("${targets}", targetsRegex);
+
+ xacmlTemplateContent = xacmlTemplateContent.replace("${limit}",
+ constraint.getFreq_limit_per_target().toString());
+
+ xacmlTemplateContent = xacmlTemplateContent.replace("${twValue}", constraint.getTime_window().get("value"));
- pattern = Pattern.compile("\\$\\{limit\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- xacmlTemplateContent = matcher.replaceAll(constraint.getFreq_limit_per_target().toString());
+ xacmlTemplateContent = xacmlTemplateContent.replace("${twUnits}", constraint.getTime_window().get("units"));
- pattern = Pattern.compile("\\$\\{twValue\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- xacmlTemplateContent = matcher.replaceAll(constraint.getTime_window().get("value"));
+ logger.debug(xacmlTemplateContent);
- pattern = Pattern.compile("\\$\\{twUnits\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- xacmlTemplateContent = matcher.replaceAll(constraint.getTime_window().get("units"));
+ return xacmlTemplateContent;
+ }
+ private static String doCommonReplacements(String xacmlTemplateContent, MatchParameters matchParameters,
+ Constraint constraint) {
- pattern = Pattern.compile("\\$\\{guardActiveStart\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- xacmlTemplateContent = matcher.replaceAll(constraint.getActive_time_range().get("start"));
+ replaceNullOrEmpty(matchParameters.getControlLoopName(), matchParameters::setControlLoopName, ".*");
+ xacmlTemplateContent = xacmlTemplateContent.replace("${clname}", matchParameters.getControlLoopName());
- pattern = Pattern.compile("\\$\\{guardActiveEnd\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- xacmlTemplateContent = matcher.replaceAll(constraint.getActive_time_range().get("end"));
- logger.debug(xacmlTemplateContent);
+ replaceNullOrEmpty(matchParameters.getActor(), matchParameters::setActor, ".*");
+ xacmlTemplateContent = xacmlTemplateContent.replace("${actor}", matchParameters.getActor());
+
+ replaceNullOrEmpty(matchParameters.getRecipe(), matchParameters::setRecipe, ".*");
+ xacmlTemplateContent = xacmlTemplateContent.replace("${recipe}", matchParameters.getRecipe());
+
+ xacmlTemplateContent = xacmlTemplateContent.replace("${guardActiveStart}",
+ constraint.getActive_time_range().get("start"));
+
+ xacmlTemplateContent = xacmlTemplateContent.replace("${guardActiveEnd}",
+ constraint.getActive_time_range().get("end"));
return xacmlTemplateContent;
}
+ private static void replaceNullOrEmpty(String text, Consumer<String> replacer, String newValue) {
+ if (isNullOrEmpty(text)) {
+ replacer.accept(newValue);
+ }
+ }
+
public static boolean isNullOrEmpty(String string) {
return string == null || string.trim().isEmpty();
}
@@ -162,7 +154,7 @@ public class PolicyGuardYamlToXacml {
/**
* Convert from Yaml to Xacml blacklist.
- *
+ *
* @param yamlFile the Yaml file
* @param xacmlTemplate the Xacml template
* @param xacmlPolicyOutput the Xacml output
@@ -185,6 +177,8 @@ public class PolicyGuardYamlToXacml {
String xacmlPolicyContent = generateXacmlGuardBlacklist(xacmlTemplateContent,
guardPolicy.getMatch_parameters(), constraint);
+ logger.debug("{}", xacmlPolicyContent);
+
Files.write(Paths.get(xacmlPolicyOutput), xacmlPolicyContent.getBytes());
} catch (IOException e) {
@@ -194,49 +188,17 @@ public class PolicyGuardYamlToXacml {
private static String generateXacmlGuardBlacklist(String xacmlTemplateContent, MatchParameters matchParameters,
Constraint constraint) {
- Pattern pattern = Pattern.compile("\\$\\{clname\\}");
- Matcher matcher = pattern.matcher(xacmlTemplateContent);
- if (isNullOrEmpty(matchParameters.getControlLoopName())) {
- matchParameters.setControlLoopName(".*");
- }
- xacmlTemplateContent = matcher.replaceAll(matchParameters.getControlLoopName());
- pattern = Pattern.compile("\\$\\{actor\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- if (isNullOrEmpty(matchParameters.getActor())) {
- matchParameters.setActor(".*");
- }
- xacmlTemplateContent = matcher.replaceAll(matchParameters.getActor());
-
- pattern = Pattern.compile("\\$\\{recipe\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- if (isNullOrEmpty(matchParameters.getRecipe())) {
- matchParameters.setRecipe(".*");
- }
- xacmlTemplateContent = matcher.replaceAll(matchParameters.getRecipe());
-
- pattern = Pattern.compile("\\$\\{guardActiveStart\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- xacmlTemplateContent = matcher.replaceAll(constraint.getActive_time_range().get("start"));
-
- pattern = Pattern.compile("\\$\\{guardActiveEnd\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- xacmlTemplateContent = matcher.replaceAll(constraint.getActive_time_range().get("end"));
- logger.debug(xacmlTemplateContent);
+ String result = doCommonReplacements(xacmlTemplateContent, matchParameters, constraint);
for (String target : constraint.getBlacklist()) {
- pattern = Pattern.compile("\\$\\{blackListElement\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- xacmlTemplateContent =
- matcher.replaceAll("<AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">" + target
- + "</AttributeValue>" + "\n\t\t\t\t\t\t\\$\\{blackListElement\\}\n");
+ result = result.replace("${blackListElement}",
+ "<AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">" + target
+ + "</AttributeValue>" + "\n\t\t\t\t\t\t\\${blackListElement}\n");
}
- pattern = Pattern.compile("\t\t\t\t\t\t\\$\\{blackListElement\\}\n");
- matcher = pattern.matcher(xacmlTemplateContent);
- xacmlTemplateContent = matcher.replaceAll("");
-
+ result = result.replace("\t\t\t\t\t\t\\${blackListElement}\n", "");
- return xacmlTemplateContent;
+ return result;
}
}