From 9e8023a455633c7daf0dd291c6268986e39352d7 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Mon, 5 Aug 2019 14:17:01 -0400 Subject: 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 --- .../onap/policy/guard/PolicyGuardYamlToXacml.java | 132 ++++++++------------- 1 file changed, 47 insertions(+), 85 deletions(-) (limited to 'controlloop/common/guard') 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 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("" + target - + "" + "\n\t\t\t\t\t\t\\$\\{blackListElement\\}\n"); + result = result.replace("${blackListElement}", + "" + target + + "" + "\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; } } -- cgit 1.2.3-korg