diff options
author | vasraz <vasyl.razinkov@est.tech> | 2021-03-27 22:58:29 +0000 |
---|---|---|
committer | Christophe Closset <christophe.closset@intl.att.com> | 2021-03-29 07:15:14 +0000 |
commit | 04c236567737c965545f64c9542a7d75ed6f9046 (patch) | |
tree | 8d2a3ea1bcb80b8d66603bb31a8b6877a98d84f8 /common/onap-common-configuration-management/onap-configuration-management-core/src | |
parent | be66acfea37b8d96cbb1b1e14592de57ec3bc4ae (diff) |
Reformat common
Signed-off-by: Vasyl Razinkov <vasyl.razinkov@est.tech>
Issue-ID: SDC-3449
Change-Id: Ifa1412b8a614f7cdb4c52f05d811ab5e9cf20d83
Diffstat (limited to 'common/onap-common-configuration-management/onap-configuration-management-core/src')
13 files changed, 241 insertions, 453 deletions
diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java index c13a3a458a..71dd457e6f 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java @@ -16,33 +16,16 @@ * Modifications Copyright (c) 2019 Samsung * */ - package org.onap.config; +import static java.util.Optional.ofNullable; +import static org.onap.config.api.Hint.EXTERNAL_LOOKUP; +import static org.onap.config.api.Hint.LATEST_LOOKUP; +import static org.onap.config.api.Hint.NODE_SPECIFIC; + import com.virtlink.commons.configuration2.jackson.JsonConfiguration; import io.github.classgraph.ClassGraph; import io.github.classgraph.ScanResult; -import org.apache.commons.configuration2.Configuration; -import org.apache.commons.configuration2.CompositeConfiguration; -import org.apache.commons.configuration2.FileBasedConfiguration; -import org.apache.commons.configuration2.PropertiesConfiguration; -import org.apache.commons.configuration2.XMLConfiguration; -import org.apache.commons.configuration2.builder.BasicConfigurationBuilder; -import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder; -import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder; -import org.apache.commons.configuration2.builder.fluent.Configurations; -import org.apache.commons.configuration2.builder.fluent.Parameters; -import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler; -import org.apache.commons.configuration2.ex.ConfigurationException; -import org.apache.commons.io.IOUtils; -import org.onap.config.api.Config; -import org.onap.config.api.ConfigurationManager; -import org.onap.config.impl.YamlConfiguration; -import org.onap.config.type.ConfigurationMode; -import org.onap.config.type.ConfigurationType; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.File; import java.io.IOException; import java.lang.reflect.Array; @@ -52,8 +35,8 @@ import java.lang.reflect.Type; import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.Charset; -import java.nio.file.Files; import java.nio.file.FileVisitResult; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; @@ -82,22 +65,33 @@ import java.util.function.Predicate; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; - -import static java.util.Optional.ofNullable; -import static org.onap.config.api.Hint.EXTERNAL_LOOKUP; -import static org.onap.config.api.Hint.LATEST_LOOKUP; -import static org.onap.config.api.Hint.NODE_SPECIFIC; +import org.apache.commons.configuration2.CompositeConfiguration; +import org.apache.commons.configuration2.Configuration; +import org.apache.commons.configuration2.FileBasedConfiguration; +import org.apache.commons.configuration2.PropertiesConfiguration; +import org.apache.commons.configuration2.XMLConfiguration; +import org.apache.commons.configuration2.builder.BasicConfigurationBuilder; +import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder; +import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder; +import org.apache.commons.configuration2.builder.fluent.Configurations; +import org.apache.commons.configuration2.builder.fluent.Parameters; +import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler; +import org.apache.commons.configuration2.ex.ConfigurationException; +import org.apache.commons.io.IOUtils; +import org.onap.config.api.Config; +import org.onap.config.api.ConfigurationManager; +import org.onap.config.impl.YamlConfiguration; +import org.onap.config.type.ConfigurationMode; +import org.onap.config.type.ConfigurationType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ConfigurationUtils { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationUtils.class); - private static final String CONFIGURATION_TYPE_NOT_SUPPORTED = "Configuration type not supported:"; - private static final Map<Class<?>, Class<?>> ARRAY_CLASS_MAP; - private static final String CONFIG_REGEX_TPL_OPT_1 = "CONFIG(-\\w*){0,1}(-(%s|%s|%s)){0,1}\\.(%s|%s|%s|%s)$"; - private static final String CONFIG_REGEX_TPL_OPT_2 = "CONFIG(.)*\\.(%s|%s|%s|%s)$"; static { @@ -125,12 +119,12 @@ public class ConfigurationUtils { Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { - super.preVisitDirectory(dir,attrs); + super.preVisitDirectory(dir, attrs); if (rootPath.equals(dir)) { return FileVisitResult.CONTINUE; } collection.add(dir.toFile()); - return recursive? FileVisitResult.CONTINUE : FileVisitResult.SKIP_SUBTREE; + return recursive ? FileVisitResult.CONTINUE : FileVisitResult.SKIP_SUBTREE; } @Override @@ -153,13 +147,10 @@ public class ConfigurationUtils { } public static String getCommaSeparatedList(List<?> list) { - if ((list == null) || list.isEmpty()) { return ""; } - - return list.stream().filter(o -> o != null && !o.toString().trim().isEmpty()) - .map(o -> o.toString().trim()).collect(Collectors.joining(",")); + return list.stream().filter(o -> o != null && !o.toString().trim().isEmpty()).map(o -> o.toString().trim()).collect(Collectors.joining(",")); } public static boolean isConfig(URL url) { @@ -169,26 +160,11 @@ public class ConfigurationUtils { public static boolean isConfig(String file) { file = file.toUpperCase().substring(file.lastIndexOf('!') + 1); file = file.substring(file.lastIndexOf('/') + 1); - return file.matches( - String.format( - CONFIG_REGEX_TPL_OPT_1, - ConfigurationMode.OVERRIDE, - ConfigurationMode.MERGE, - ConfigurationMode.UNION, - ConfigurationType.PROPERTIES.name(), - ConfigurationType.XML.name(), - ConfigurationType.JSON.name(), - ConfigurationType.YAML.name() - ) - ) || file.matches( - String.format( - CONFIG_REGEX_TPL_OPT_2, - ConfigurationType.PROPERTIES.name(), - ConfigurationType.XML.name(), - ConfigurationType.JSON.name(), - ConfigurationType.YAML.name() - ) - ); + return file.matches(String.format(CONFIG_REGEX_TPL_OPT_1, ConfigurationMode.OVERRIDE, ConfigurationMode.MERGE, ConfigurationMode.UNION, + ConfigurationType.PROPERTIES.name(), ConfigurationType.XML.name(), ConfigurationType.JSON.name(), ConfigurationType.YAML.name())) || file + .matches(String + .format(CONFIG_REGEX_TPL_OPT_2, ConfigurationType.PROPERTIES.name(), ConfigurationType.XML.name(), ConfigurationType.JSON.name(), + ConfigurationType.YAML.name())); } public static boolean isConfig(File file) { @@ -196,45 +172,31 @@ public class ConfigurationUtils { } private static Optional<String> readNamespace(Configuration config) { - return ofNullable(config).flatMap(configuration -> ofNullable(configuration.getString(Constants.NAMESPACE_KEY))) - .map(String::toUpperCase); + return ofNullable(config).flatMap(configuration -> ofNullable(configuration.getString(Constants.NAMESPACE_KEY))).map(String::toUpperCase); } private static Optional<String> readMergeStrategy(Configuration config) { - return ofNullable(config).flatMap(configuration -> ofNullable(configuration.getString(Constants.MODE_KEY))) - .map(String::toUpperCase); + return ofNullable(config).flatMap(configuration -> ofNullable(configuration.getString(Constants.MODE_KEY))).map(String::toUpperCase); } public static ConfigurationMode getMergeStrategy(File file) { - Optional<ConfigurationMode> configurationMode = - getConfiguration(file).flatMap(ConfigurationUtils::readMergeStrategy) - .flatMap(ConfigurationUtils::convertConfigurationMode); + Optional<ConfigurationMode> configurationMode = getConfiguration(file).flatMap(ConfigurationUtils::readMergeStrategy) + .flatMap(ConfigurationUtils::convertConfigurationMode); return configurationMode.orElseGet(() -> getMergeStrategy(file.getName().toUpperCase())); } public static ConfigurationMode getMergeStrategy(URL url) { - Optional<ConfigurationMode> configurationMode = - getConfiguration(url).flatMap(ConfigurationUtils::readMergeStrategy) - .flatMap(ConfigurationUtils::convertConfigurationMode); + Optional<ConfigurationMode> configurationMode = getConfiguration(url).flatMap(ConfigurationUtils::readMergeStrategy) + .flatMap(ConfigurationUtils::convertConfigurationMode); return configurationMode.orElseGet(() -> getMergeStrategy(url.getFile().toUpperCase())); } public static ConfigurationMode getMergeStrategy(String file) { - file = file.toUpperCase().substring(file.lastIndexOf('!') + 1); file = file.substring(file.lastIndexOf('/') + 1); - Pattern pattern = Pattern.compile( - String.format( - CONFIG_REGEX_TPL_OPT_1, - ConfigurationMode.OVERRIDE, - ConfigurationMode.MERGE, - ConfigurationMode.UNION, - ConfigurationType.PROPERTIES.name(), - ConfigurationType.XML.name(), - ConfigurationType.JSON.name(), - ConfigurationType.YAML.name() - ) - ); + Pattern pattern = Pattern.compile(String + .format(CONFIG_REGEX_TPL_OPT_1, ConfigurationMode.OVERRIDE, ConfigurationMode.MERGE, ConfigurationMode.UNION, + ConfigurationType.PROPERTIES.name(), ConfigurationType.XML.name(), ConfigurationType.JSON.name(), ConfigurationType.YAML.name())); Matcher matcher = pattern.matcher(file); boolean b1 = matcher.matches(); if (b1) { @@ -250,14 +212,11 @@ public class ConfigurationUtils { } } } - return null; } public static Optional<FileBasedConfiguration> getConfiguration(URL url) { - try { - ConfigurationType configType = ConfigurationUtils.getConfigType(url); switch (configType) { case PROPERTIES: @@ -274,12 +233,10 @@ public class ConfigurationUtils { } catch (ConfigurationException exception) { LOGGER.error("Error reading configuration at {}.", url.toString(), exception); } - return Optional.empty(); } public static Optional<FileBasedConfiguration> getConfiguration(File file) { - try { return getConfiguration(file.getAbsoluteFile().toURI().toURL()); } catch (MalformedURLException e) { @@ -289,14 +246,12 @@ public class ConfigurationUtils { public static ConfigurationType getConfigType(File file) { Objects.requireNonNull(file, "File cannot be null"); - return Enum.valueOf(ConfigurationType.class, - file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf('.') + 1).toUpperCase()); + return Enum.valueOf(ConfigurationType.class, file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf('.') + 1).toUpperCase()); } public static ConfigurationType getConfigType(URL url) { Objects.requireNonNull(url, "URL cannot be null"); - return Enum.valueOf(ConfigurationType.class, - url.getFile().substring(url.getFile().lastIndexOf('.') + 1).toUpperCase()); + return Enum.valueOf(ConfigurationType.class, url.getFile().substring(url.getFile().lastIndexOf('.') + 1).toUpperCase()); } private static Optional<ConfigurationMode> convertConfigurationMode(String configMode) { @@ -311,9 +266,7 @@ public class ConfigurationUtils { public static Class<?> getCollectionGenericType(Field field) { Type type = field.getGenericType(); - if (type instanceof ParameterizedType) { - ParameterizedType paramType = (ParameterizedType) type; Type[] arr = paramType.getActualTypeArguments(); if (arr.length > 0) { @@ -325,13 +278,12 @@ public class ConfigurationUtils { } } } - return String[].class; } public static boolean isWrapperClass(Class<?> clazz) { - Predicate<Class<?>> predicateWrapper = type -> type == String.class || type == Boolean.class || type == Character.class - || Number.class.isAssignableFrom(type); + Predicate<Class<?>> predicateWrapper = type -> type == String.class || type == Boolean.class || type == Character.class || Number.class + .isAssignableFrom(type); return isA(predicateWrapper, clazz); } @@ -368,10 +320,9 @@ public class ConfigurationUtils { * Check if clazz is array of primitives or array of primitives wrappers */ public static boolean isAPrimitivesOrWrappersArray(Class<?> clazz) { - Predicate<Class<?>> predicatePrimitivesOrWrappersArray = - type -> ConfigurationUtils.isAWrappersArray(type) || ConfigurationUtils.isAPrimitivesArray(type); + Predicate<Class<?>> predicatePrimitivesOrWrappersArray = type -> ConfigurationUtils.isAWrappersArray(type) || ConfigurationUtils + .isAPrimitivesArray(type); return isA(predicatePrimitivesOrWrappersArray, clazz); - } /** @@ -410,23 +361,18 @@ public class ConfigurationUtils { FileBasedConfigurationBuilder<FileBasedConfiguration> builder; ConfigurationType configType = ConfigurationUtils.getConfigType(file); builder = getFileBasedConfigurationBuilder(configType); - builder.configure(new Parameters().fileBased().setFile(file) - .setListDelimiterHandler(new DefaultListDelimiterHandler(','))); + builder.configure(new Parameters().fileBased().setFile(file).setListDelimiterHandler(new DefaultListDelimiterHandler(','))); return builder; } public static BasicConfigurationBuilder<FileBasedConfiguration> getConfigurationBuilder(URL url) { ConfigurationType configType = ConfigurationUtils.getConfigType(url); - ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builder = - getFileBasedConfigurationBuilder(configType); - builder.configure( - new Parameters().fileBased().setURL(url).setListDelimiterHandler(new DefaultListDelimiterHandler(','))); + ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builder = getFileBasedConfigurationBuilder(configType); + builder.configure(new Parameters().fileBased().setURL(url).setListDelimiterHandler(new DefaultListDelimiterHandler(','))); return builder; } - private static ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> getFileBasedConfigurationBuilder( - ConfigurationType configType) { - + private static ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> getFileBasedConfigurationBuilder(ConfigurationType configType) { ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builder; switch (configType) { case PROPERTIES: @@ -490,7 +436,7 @@ public class ConfigurationUtils { public static Object getWrappersArray(Collection<?> collection, Class<?> clazz) { Object array = null; - if (isWrapperClass(clazz)){ + if (isWrapperClass(clazz)) { int collectionSize = collection.size(); array = Array.newInstance(clazz, collection.size()); Object[] objArray = collection.toArray(); @@ -520,11 +466,10 @@ public class ConfigurationUtils { } else if (key.toUpperCase().startsWith("SYS:")) { value = System.getProperty(key.substring(substringStartIndex)); } else { - value = ConfigurationUtils.getCollectionString( - ConfigurationManager.lookup().getAsStringValues(tenant, namespace, key).toString()); + value = ConfigurationUtils.getCollectionString(ConfigurationManager.lookup().getAsStringValues(tenant, namespace, key).toString()); } - return processVariablesIfPresent(tenant, namespace, data.replaceAll("\\$\\{" + key + "}", - value == null ? "" : value.replace("\\", "\\\\"))); + return processVariablesIfPresent(tenant, namespace, + data.replaceAll("\\$\\{" + key + "}", value == null ? "" : value.replace("\\", "\\\\"))); } else { return data; } @@ -594,7 +539,7 @@ public class ConfigurationUtils { return getConcreteCollection(List.class); } throw new IllegalArgumentException("Only corresponding array classes and any are allowed as argument." - + "assignable from TransferQueue, BlockingQueue, Deque, Queue, SortedSet, Set, List class"); + + "assignable from TransferQueue, BlockingQueue, Deque, Queue, SortedSet, Set, List class"); } public static Collection getConcreteCollection(Class<?> clazz) { @@ -616,18 +561,16 @@ public class ConfigurationUtils { return new LinkedBlockingQueue<>(); default: throw new IllegalArgumentException("Only corresponding array classes and any are allowed as argument." - + "assignable from TransferQueue, BlockingQueue, Deque, Queue, SortedSet, Set, List class"); + + "assignable from TransferQueue, BlockingQueue, Deque, Queue, SortedSet, Set, List class"); } } public static String getConfigurationRepositoryKey(File file) { - return getConfigurationRepositoryKey( - ConfigurationUtils.getNamespace(file).split(Constants.TENANT_NAMESPACE_SEPARATOR)); + return getConfigurationRepositoryKey(ConfigurationUtils.getNamespace(file).split(Constants.TENANT_NAMESPACE_SEPARATOR)); } public static String getConfigurationRepositoryKey(URL url) { - return getConfigurationRepositoryKey( - ConfigurationUtils.getNamespace(url).split(Constants.TENANT_NAMESPACE_SEPARATOR)); + return getConfigurationRepositoryKey(ConfigurationUtils.getNamespace(url).split(Constants.TENANT_NAMESPACE_SEPARATOR)); } public static String getConfigurationRepositoryKey(String[] array) { @@ -641,49 +584,34 @@ public class ConfigurationUtils { } public static String getNamespace(File file) { - Optional<String> namespace = - getConfiguration(file).flatMap(ConfigurationUtils::readNamespace).map(String::toUpperCase); + Optional<String> namespace = getConfiguration(file).flatMap(ConfigurationUtils::readNamespace).map(String::toUpperCase); return namespace.orElseGet(() -> getNamespace(file.getName().toUpperCase())); } public static String getNamespace(String file) { file = file.toUpperCase().substring(file.lastIndexOf('!') + 1); file = file.substring(file.lastIndexOf('/') + 1); - Pattern pattern = Pattern.compile( - String.format( - CONFIG_REGEX_TPL_OPT_1, - ConfigurationMode.OVERRIDE, - ConfigurationMode.MERGE, - ConfigurationMode.UNION, - ConfigurationType.PROPERTIES.name(), - ConfigurationType.XML.name(), - ConfigurationType.JSON.name(), - ConfigurationType.YAML.name() - ) - ); + Pattern pattern = Pattern.compile(String + .format(CONFIG_REGEX_TPL_OPT_1, ConfigurationMode.OVERRIDE, ConfigurationMode.MERGE, ConfigurationMode.UNION, + ConfigurationType.PROPERTIES.name(), ConfigurationType.XML.name(), ConfigurationType.JSON.name(), ConfigurationType.YAML.name())); Matcher matcher = pattern.matcher(file); boolean b1 = matcher.matches(); if (b1) { if (matcher.group(1) != null) { String moduleName = matcher.group(1).substring(1); - return moduleName.equalsIgnoreCase(ConfigurationMode.OVERRIDE.name()) || moduleName.equalsIgnoreCase( - ConfigurationMode.UNION.name()) || moduleName.equalsIgnoreCase(ConfigurationMode.MERGE.name()) - ? Constants.DEFAULT_NAMESPACE : moduleName; + return moduleName.equalsIgnoreCase(ConfigurationMode.OVERRIDE.name()) || moduleName.equalsIgnoreCase(ConfigurationMode.UNION.name()) + || moduleName.equalsIgnoreCase(ConfigurationMode.MERGE.name()) ? Constants.DEFAULT_NAMESPACE : moduleName; } else { return Constants.DEFAULT_NAMESPACE; } } else if (isConfig(file)) { return Constants.DEFAULT_NAMESPACE; } - return null; } public static String getNamespace(URL url) { - - Optional<String> namespace = - getConfiguration(url).flatMap(ConfigurationUtils::readNamespace).map(String::toUpperCase); - + Optional<String> namespace = getConfiguration(url).flatMap(ConfigurationUtils::readNamespace).map(String::toUpperCase); return namespace.orElseGet(() -> getNamespace(url.getFile().toUpperCase())); } @@ -738,7 +666,6 @@ public class ConfigurationUtils { return ((Object[]) obj).length == 0; } } - return false; } @@ -747,7 +674,6 @@ public class ConfigurationUtils { } // private methods section starts here - private static int[] getIntsPrimitiveArray(Collection<?> collection) { int collectionSize = collection.size(); int[] array = new int[collectionSize]; @@ -827,5 +753,4 @@ public class ConfigurationUtils { } return array; } - } diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/Constants.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/Constants.java index 97963c4c4e..088d474252 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/Constants.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/Constants.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.config; public class Constants { diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/NonConfigResource.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/NonConfigResource.java index 5e21fbdfb9..5eb09011bf 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/NonConfigResource.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/NonConfigResource.java @@ -13,12 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.config; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.File; import java.net.MalformedURLException; import java.net.URI; @@ -33,20 +29,19 @@ import java.util.List; import java.util.Objects; import java.util.Set; import java.util.function.Function; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class NonConfigResource { - private static final Logger LOGGER = LoggerFactory.getLogger(NonConfigResource.class); static final String NODE_CONFIG_LOCATION = "node.config.location"; static final String CONFIG_LOCATION = "config.location"; - - private final List<Function<String, Path>> lookupFunctions = - Arrays.asList(this::getFromFile, this::findInFiles, this::getForNode, this::getGlobal, this::findInUris); - + private static final Logger LOGGER = LoggerFactory.getLogger(NonConfigResource.class); private final Set<URI> uris = Collections.synchronizedSet(new HashSet<>()); private final Set<File> files = Collections.synchronizedSet(new HashSet<>()); - private final Function<String, String> propertyGetter; + private final List<Function<String, Path>> lookupFunctions = Arrays + .asList(this::getFromFile, this::findInFiles, this::getForNode, this::getGlobal, this::findInUris); NonConfigResource(Function<String, String> propertyGetter) { this.propertyGetter = propertyGetter; @@ -56,6 +51,22 @@ public class NonConfigResource { this(System::getProperty); } + private static URI toUri(URL url) { + try { + return url.toURI(); + } catch (URISyntaxException e) { + throw new IllegalStateException("Unexpected URL syntax: " + url, e); + } + } + + private static URL toUrl(URI uri) { + try { + return uri.toURL(); + } catch (MalformedURLException e) { + throw new IllegalStateException("Unexpected URI syntax: " + uri, e); + } + } + public void add(URL url) { uris.add(toUri(url)); } @@ -65,18 +76,11 @@ public class NonConfigResource { } public Path locate(String resource) { - if (resource == null) { return null; } - try { - - return lookupFunctions.stream() - .map(f -> f.apply(resource)) - .filter(Objects::nonNull) - .findFirst().orElse(null); - + return lookupFunctions.stream().map(f -> f.apply(resource)).filter(Objects::nonNull).findFirst().orElse(null); } catch (Exception exception) { LOGGER.error("Failed to locate resource '{}'.", resource, exception); return null; @@ -84,17 +88,11 @@ public class NonConfigResource { } private Path locate(File root, String resource) { - if (!root.exists()) { return null; } - - return ConfigurationUtils.getAllFiles(root, true, false) - .stream() - .filter(f -> !ConfigurationUtils.isConfig(f)) - .peek(this::add).filter(f -> f.getAbsolutePath().endsWith(resource)) - .findFirst() - .map(file -> Paths.get(file.getAbsolutePath())).orElse(null); + return ConfigurationUtils.getAllFiles(root, true, false).stream().filter(f -> !ConfigurationUtils.isConfig(f)).peek(this::add) + .filter(f -> f.getAbsolutePath().endsWith(resource)).findFirst().map(file -> Paths.get(file.getAbsolutePath())).orElse(null); } private Path getFromFile(String resource) { @@ -111,15 +109,12 @@ public class NonConfigResource { } private Path findInFiles(String resource) { - for (File availableFile : files) { - String absolutePath = availableFile.getAbsolutePath(); if (absolutePath.endsWith(resource) && availableFile.exists()) { return Paths.get(absolutePath); } } - return null; } @@ -135,21 +130,4 @@ public class NonConfigResource { String value = propertyGetter.apply(property); return (value == null) ? null : locate(new File(value), resource); } - - private static URI toUri(URL url) { - - try { - return url.toURI(); - } catch (URISyntaxException e) { - throw new IllegalStateException("Unexpected URL syntax: " + url, e); - } - } - - private static URL toUrl(URI uri) { - try { - return uri.toURL(); - } catch (MalformedURLException e) { - throw new IllegalStateException("Unexpected URI syntax: " + uri, e); - } - } } diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/AggregateConfiguration.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/AggregateConfiguration.java index 07c81e5f5b..0621561dcc 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/AggregateConfiguration.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/AggregateConfiguration.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.config.impl; import static org.onap.config.Constants.LOAD_ORDER_KEY; @@ -44,17 +43,14 @@ public final class AggregateConfiguration { private final Map<String, Configuration> overrideConfig = Collections.synchronizedMap(new LinkedHashMap<>()); public void addConfig(File file) throws ConfigurationException { - addConfig(fileToUrl(file), ConfigurationUtils.getMergeStrategy(file), - ConfigurationUtils.getConfigurationBuilder(file).getConfiguration()); + addConfig(fileToUrl(file), ConfigurationUtils.getMergeStrategy(file), ConfigurationUtils.getConfigurationBuilder(file).getConfiguration()); } public void addConfig(URL url) throws ConfigurationException { - addConfig(url, ConfigurationUtils.getMergeStrategy(url), - ConfigurationUtils.getConfigurationBuilder(url).getConfiguration()); + addConfig(url, ConfigurationUtils.getMergeStrategy(url), ConfigurationUtils.getConfigurationBuilder(url).getConfiguration()); } private void addConfig(URL url, ConfigurationMode configMode, Configuration config) { - String normalizedUrl = normalize(url); if (configMode != null) { switch (configMode) { @@ -76,28 +72,24 @@ public final class AggregateConfiguration { private String normalize(URL url) { // what about Linux where paths are case sensitive? - return url.toString().toUpperCase(); + return url.toString().toUpperCase(); } private URL fileToUrl(File file) { - try { - return file.getAbsoluteFile().toURI().toURL(); + return file.getAbsoluteFile().toURI().toURL(); } catch (MalformedURLException e) { - throw new IllegalStateException("URL produced by JDK and is not expected to be malformed. File: " - + file.getAbsoluteFile()); + throw new IllegalStateException("URL produced by JDK and is not expected to be malformed. File: " + file.getAbsoluteFile()); } } public Configuration getFinalConfiguration() { - CombinedConfiguration ccRoot = new CombinedConfiguration(new MergeCombiner()); ArrayList<Configuration> tempList = new ArrayList<>(rootConfig.values()); tempList.sort(this::sortForMerge); for (Configuration conf : tempList) { ccRoot.addConfiguration(conf); } - CombinedConfiguration ccMergeRoot = new CombinedConfiguration(new MergeCombiner()); ccMergeRoot.addConfiguration(ccRoot); tempList = new ArrayList<>(mergeConfig.values()); @@ -105,13 +97,11 @@ public final class AggregateConfiguration { for (Configuration conf : tempList) { ccMergeRoot.addConfiguration(conf); } - CombinedConfiguration ccUnionRoot = new CombinedConfiguration(new UnionCombiner()); ccUnionRoot.addConfiguration(ccMergeRoot); for (Configuration conf : unionConfig.values()) { ccUnionRoot.addConfiguration(conf); } - ArrayList<Configuration> tempOverrideConfigs = new ArrayList<>(overrideConfig.values()); Collections.reverse(tempOverrideConfigs); tempOverrideConfigs.sort(this::sortForOverride); @@ -119,7 +109,6 @@ public final class AggregateConfiguration { for (Configuration conf : tempOverrideConfigs) { ccOverrideRoot.addConfiguration(conf); } - ccOverrideRoot.addConfiguration(ccUnionRoot); return ccOverrideRoot; } @@ -139,12 +128,10 @@ public final class AggregateConfiguration { } private int readLoadOrder(Configuration conf) { - String order = conf.getString(LOAD_ORDER_KEY); if (ConfigurationUtils.isBlank(order) || !order.trim().matches("\\d+")) { return 0; } - return Integer.parseInt(order.trim()); } } diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/CliConfigurationImpl.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/CliConfigurationImpl.java index f919e234f0..3ddf0a9079 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/CliConfigurationImpl.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/CliConfigurationImpl.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.config.impl; import static org.onap.config.Constants.DEFAULT_NAMESPACE; @@ -59,8 +58,7 @@ public final class CliConfigurationImpl extends ConfigurationImpl implements Con if (queryData.isFallback()) { value = get(queryData.getTenant(), queryData.getNamespace(), queryData.getKey(), String[].class, hints); } else { - value = getInternal(queryData.getTenant(), queryData.getNamespace(), queryData.getKey(), String[].class, - hints); + value = getInternal(queryData.getTenant(), queryData.getNamespace(), queryData.getKey(), String[].class, hints); } return ConfigurationUtils.getCommaSeparatedList(value); } catch (Exception exception) { @@ -130,11 +128,7 @@ public final class CliConfigurationImpl extends ConfigurationImpl implements Con } } } catch (Exception exception) { - LOGGER.warn("Error occurred while searching for in-memory keys for namespace: '{}' and tenant: '{}'", - namespace, - tenant, - exception - ); + LOGGER.warn("Error occurred while searching for in-memory keys for namespace: '{}' and tenant: '{}'", namespace, tenant, exception); } return keys; } diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationFilter.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationFilter.java index 06981e4b3b..ea48d3fe01 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationFilter.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationFilter.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.config.impl; import java.io.IOException; @@ -27,7 +26,6 @@ import javax.servlet.annotation.WebFilter; import org.onap.config.Constants; import org.onap.config.api.Configuration; - @WebFilter("/") public class ConfigurationFilter implements Filter { @@ -37,8 +35,8 @@ public class ConfigurationFilter implements Filter { } @Override - public void doFilter(ServletRequest paramServletRequest, ServletResponse paramServletResponse, - FilterChain paramFilterChain) throws IOException, ServletException { + public void doFilter(ServletRequest paramServletRequest, ServletResponse paramServletResponse, FilterChain paramFilterChain) + throws IOException, ServletException { Configuration.TENANT.set(Constants.DEFAULT_TENANT); try { paramFilterChain.doFilter(paramServletRequest, paramServletResponse); @@ -51,5 +49,4 @@ public class ConfigurationFilter implements Filter { public void destroy() { //Use the default behavior } - } diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationImpl.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationImpl.java index 5d49fa970f..964b8d1a2d 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationImpl.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationImpl.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.config.impl; import static org.onap.config.ConfigurationUtils.isBlank; @@ -36,7 +35,6 @@ import java.util.Set; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Predicate; import java.util.stream.Collectors; - import org.apache.commons.configuration2.ex.ConfigurationException; import org.onap.config.ConfigurationUtils; import org.onap.config.Constants; @@ -49,115 +47,18 @@ import org.slf4j.LoggerFactory; public class ConfigurationImpl implements org.onap.config.api.Configuration { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationImpl.class); - private static final String KEY_CANNOT_BE_NULL = "Key can't be null."; private static final NonConfigResource NON_CONFIG_RESOURCE = new NonConfigResource(); private static final Map<String, AggregateConfiguration> MODULE_CONFIG_STORE = new HashMap<>(); static { - if (!loadClassPathConfigurationsAndResources() - || !loadAdditionalConfigurationsAndResources() - || !loadTenantConfigurations()) { + if (!loadClassPathConfigurationsAndResources() || !loadAdditionalConfigurationsAndResources() || !loadTenantConfigurations()) { throw new IllegalStateException("Failed to initialize configuration"); } populateFinalConfigurationIncrementally(MODULE_CONFIG_STORE); loadNodeSpecificConfigurations(); } - @Override - public <T> T get(String tenant, String namespace, String key, Class<T> clazz, Hint... hints) { - String[] tenantNamespaceArray; - if (tenant == null && namespace != null) { - tenantNamespaceArray = namespace.split(Constants.TENANT_NAMESPACE_SEPARATOR); - if (tenantNamespaceArray.length > 1) { - tenant = tenantNamespaceArray[0]; - namespace = tenantNamespaceArray[1]; - } - } - - tenant = ConfigurationRepository.lookup().isValidTenant(tenant) ? tenant.toUpperCase() : Constants.DEFAULT_TENANT; - namespace = ConfigurationRepository.lookup().isValidNamespace(namespace) ? namespace.toUpperCase() : Constants.DEFAULT_NAMESPACE; - hints = hints == null || hints.length == 0 ? new Hint[]{Hint.EXTERNAL_LOOKUP, Hint.NODE_SPECIFIC} : hints; - T returnValue; - returnValue = getInternal(tenant, namespace, key, clazz, hints); - if ((returnValue == null || ConfigurationUtils.isZeroLengthArray(clazz, returnValue)) - && !Constants.DEFAULT_TENANT.equals(tenant)) { - returnValue = getInternal(Constants.DEFAULT_TENANT, namespace, key, clazz, hints); - } - if ((returnValue == null || ConfigurationUtils.isZeroLengthArray(clazz, returnValue)) - && !Constants.DEFAULT_NAMESPACE.equals(namespace)) { - returnValue = getInternal(tenant, Constants.DEFAULT_NAMESPACE, key, clazz, hints); - } - if ((returnValue == null || ConfigurationUtils.isZeroLengthArray(clazz, returnValue)) - && !Constants.DEFAULT_NAMESPACE.equals(namespace) && !Constants.DEFAULT_TENANT.equals(tenant)) { - returnValue = getInternal(Constants.DEFAULT_TENANT, Constants.DEFAULT_NAMESPACE, key, clazz, hints); - } - if (returnValue == null && ConfigurationUtils.isAPrimitive(clazz)) { - returnValue = (T) ConfigurationUtils.getDefaultFor(clazz); - } - return returnValue; - } - - @Override - public <T> Map<String, T> populateMap(String tenantId, String namespace, String key, Class<T> clazz) { - final String calculatedTenantId = calculateTenant(tenantId); - final String calculatedNamespace = calculateNamespace(namespace); - Map<String, T> map = new HashMap<>(); - Iterator<String> keys; - try { - keys = ConfigurationRepository.lookup().getConfigurationFor(calculatedTenantId, calculatedNamespace).getKeys(key); - keys.forEachRemaining(k -> { - if (k.startsWith(key + ".")) { - k = k.substring(key.length() + 1); - String subkey = k.substring(0, k.indexOf('.')); - if (!map.containsKey(subkey)) { - map.put(subkey, get(calculatedTenantId, calculatedNamespace, key + "." + subkey, clazz)); - } - } - }); - } catch (Exception e) { - LOGGER.warn( - "Couldn't populate map fot tenant: {}, namespace: {}, key: {}, type: {}", - tenantId, - namespace, - key, - clazz.getSimpleName(), - e - ); - } - return map; - } - - @Override - public Map<Object, Object> generateMap(String tenantId, String namespace, String key) { - final String calculatedTenantId = calculateTenant(tenantId); - final String calculatedNamespace = calculateNamespace(namespace); - Map<Object, Object> parentMap = new HashMap<>(); - Iterator<String> configKeys; - try { - if (isBlank(key)) { - configKeys = ConfigurationRepository.lookup().getConfigurationFor(calculatedTenantId, calculatedNamespace).getKeys(); - } else { - configKeys = ConfigurationRepository.lookup().getConfigurationFor(calculatedTenantId, calculatedNamespace).getKeys(key); - } - configKeys.forEachRemaining(subKey -> { - if (!isBlank(key) && !subKey.startsWith(key + ".")) { - configKeys.remove(); - } - parseConfigSubKeys(subKey, key, calculatedTenantId, calculatedNamespace, parentMap); - }); - } catch (Exception e) { - LOGGER.warn( - "Couldn't generate map fot tenant: {}, namespace: {}, key: {}", - tenantId, - namespace, - key, - e - ); - } - return parentMap; - } - private static boolean loadClassPathConfigurationsAndResources() { List<URL> classpathResources = ConfigurationUtils.getAllClassPathResources(); Predicate<URL> predicate = ConfigurationUtils::isConfig; @@ -165,11 +66,8 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { List<URL> configResources = resources.get(true); List<URL> nonConfigResources = resources.get(false); AtomicReference<Boolean> successFlagHolder = new AtomicReference<>(true); - configResources.forEach(url -> successFlagHolder.set(setUpdateModuleConfigStore(url))); - nonConfigResources.forEach(NON_CONFIG_RESOURCE::add); - return successFlagHolder.get(); } @@ -183,9 +81,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { Map<Boolean, List<File>> resources = filesystemResources.stream().collect(Collectors.partitioningBy(filePredicate)); List<File> configResources = resources.get(true); List<File> nonConfigResources = resources.get(false); - configResources.forEach(file -> successFlagHolder.set(setUpdateModuleConfigStore(file))); - nonConfigResources.forEach(NON_CONFIG_RESOURCE::add); } return successFlagHolder.get(); @@ -198,20 +94,17 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { File root = new File(tenantConfigLocation); Collection<File> tenantsRoot = ConfigurationUtils.getAllFiles(root, false, true); Collection<File> filesystemResources = ConfigurationUtils.getAllFiles(root, true, false); - Map<Boolean, List<File>> resources = filesystemResources.stream().collect(Collectors.partitioningBy(ConfigurationUtils::isConfig)); Collection<File> tenantResources = resources.get(true); - tenantResources.forEach(configFile -> { AtomicReference<String> moduleNameHolder = new AtomicReference<>(ConfigurationUtils.getNamespace(configFile)); Predicate<File> startsWithRootPredicate = tenantRoot -> configFile.getAbsolutePath().startsWith(tenantRoot.getAbsolutePath()); - Collection<File> matchesTenantRoot = tenantsRoot.stream().filter(startsWithRootPredicate).collect(Collectors.toCollection(ArrayList::new)); + Collection<File> matchesTenantRoot = tenantsRoot.stream().filter(startsWithRootPredicate) + .collect(Collectors.toCollection(ArrayList::new)); AtomicReference<String[]> altResource = new AtomicReference<>(); matchesTenantRoot.forEach(file -> altResource.set( - (file.getName().toUpperCase() + Constants.TENANT_NAMESPACE_SEPARATOR + moduleNameHolder.get()) - .split(Constants.TENANT_NAMESPACE_SEPARATOR) - ) - ); + (file.getName().toUpperCase() + Constants.TENANT_NAMESPACE_SEPARATOR + moduleNameHolder.get()) + .split(Constants.TENANT_NAMESPACE_SEPARATOR))); successFlagHolder.set(setUpdateModuleConfigStore(configFile, altResource.get())); }); } @@ -223,25 +116,20 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { if (!isBlank(nodeConfigLocation)) { File root = new File(nodeConfigLocation); Collection<File> filesystemResources = ConfigurationUtils.getAllFiles(root, true, false); - filesystemResources.stream().filter(ConfigurationUtils::isConfig).forEach( - file -> ConfigurationRepository.lookup().populateOverrideConfiguration( - ConfigurationUtils.getConfigurationRepositoryKey( - ConfigurationUtils.getNamespace(file).split(Constants.TENANT_NAMESPACE_SEPARATOR)), file) - ); + filesystemResources.stream().filter(ConfigurationUtils::isConfig).forEach(file -> ConfigurationRepository.lookup() + .populateOverrideConfiguration(ConfigurationUtils + .getConfigurationRepositoryKey(ConfigurationUtils.getNamespace(file).split(Constants.TENANT_NAMESPACE_SEPARATOR)), file)); } } private static void populateFinalConfigurationIncrementally(Map<String, AggregateConfiguration> configs) { if (configs.get(Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER + Constants.DB_NAMESPACE) != null) { - ConfigurationRepository.lookup().populateConfiguration( - Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER + Constants.DB_NAMESPACE, - configs.remove(Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER + Constants.DB_NAMESPACE) - .getFinalConfiguration()); + ConfigurationRepository.lookup() + .populateConfiguration(Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER + Constants.DB_NAMESPACE, + configs.remove(Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER + Constants.DB_NAMESPACE).getFinalConfiguration()); } Set<String> modules = configs.keySet(); - modules.forEach( - m -> ConfigurationRepository.lookup().populateConfiguration(m, configs.get(m).getFinalConfiguration()) - ); + modules.forEach(m -> ConfigurationRepository.lookup().populateConfiguration(m, configs.get(m).getFinalConfiguration())); } private static <T> boolean setUpdateModuleConfigStore(T resource, String... namedResources) { @@ -292,6 +180,98 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } } + private static String calculateNamespace(String namespace) { + if (isBlank(namespace)) { + return Constants.DEFAULT_NAMESPACE; + } + return namespace.toUpperCase(); + } + + private static String calculateTenant(String tenant) { + if (isBlank(tenant)) { + return Constants.DEFAULT_TENANT; + } + return tenant.toUpperCase(); + } + + @Override + public <T> T get(String tenant, String namespace, String key, Class<T> clazz, Hint... hints) { + String[] tenantNamespaceArray; + if (tenant == null && namespace != null) { + tenantNamespaceArray = namespace.split(Constants.TENANT_NAMESPACE_SEPARATOR); + if (tenantNamespaceArray.length > 1) { + tenant = tenantNamespaceArray[0]; + namespace = tenantNamespaceArray[1]; + } + } + tenant = ConfigurationRepository.lookup().isValidTenant(tenant) ? tenant.toUpperCase() : Constants.DEFAULT_TENANT; + namespace = ConfigurationRepository.lookup().isValidNamespace(namespace) ? namespace.toUpperCase() : Constants.DEFAULT_NAMESPACE; + hints = hints == null || hints.length == 0 ? new Hint[]{Hint.EXTERNAL_LOOKUP, Hint.NODE_SPECIFIC} : hints; + T returnValue; + returnValue = getInternal(tenant, namespace, key, clazz, hints); + if ((returnValue == null || ConfigurationUtils.isZeroLengthArray(clazz, returnValue)) && !Constants.DEFAULT_TENANT.equals(tenant)) { + returnValue = getInternal(Constants.DEFAULT_TENANT, namespace, key, clazz, hints); + } + if ((returnValue == null || ConfigurationUtils.isZeroLengthArray(clazz, returnValue)) && !Constants.DEFAULT_NAMESPACE.equals(namespace)) { + returnValue = getInternal(tenant, Constants.DEFAULT_NAMESPACE, key, clazz, hints); + } + if ((returnValue == null || ConfigurationUtils.isZeroLengthArray(clazz, returnValue)) && !Constants.DEFAULT_NAMESPACE.equals(namespace) + && !Constants.DEFAULT_TENANT.equals(tenant)) { + returnValue = getInternal(Constants.DEFAULT_TENANT, Constants.DEFAULT_NAMESPACE, key, clazz, hints); + } + if (returnValue == null && ConfigurationUtils.isAPrimitive(clazz)) { + returnValue = (T) ConfigurationUtils.getDefaultFor(clazz); + } + return returnValue; + } + + @Override + public <T> Map<String, T> populateMap(String tenantId, String namespace, String key, Class<T> clazz) { + final String calculatedTenantId = calculateTenant(tenantId); + final String calculatedNamespace = calculateNamespace(namespace); + Map<String, T> map = new HashMap<>(); + Iterator<String> keys; + try { + keys = ConfigurationRepository.lookup().getConfigurationFor(calculatedTenantId, calculatedNamespace).getKeys(key); + keys.forEachRemaining(k -> { + if (k.startsWith(key + ".")) { + k = k.substring(key.length() + 1); + String subkey = k.substring(0, k.indexOf('.')); + if (!map.containsKey(subkey)) { + map.put(subkey, get(calculatedTenantId, calculatedNamespace, key + "." + subkey, clazz)); + } + } + }); + } catch (Exception e) { + LOGGER.warn("Couldn't populate map fot tenant: {}, namespace: {}, key: {}, type: {}", tenantId, namespace, key, clazz.getSimpleName(), e); + } + return map; + } + + @Override + public Map<Object, Object> generateMap(String tenantId, String namespace, String key) { + final String calculatedTenantId = calculateTenant(tenantId); + final String calculatedNamespace = calculateNamespace(namespace); + Map<Object, Object> parentMap = new HashMap<>(); + Iterator<String> configKeys; + try { + if (isBlank(key)) { + configKeys = ConfigurationRepository.lookup().getConfigurationFor(calculatedTenantId, calculatedNamespace).getKeys(); + } else { + configKeys = ConfigurationRepository.lookup().getConfigurationFor(calculatedTenantId, calculatedNamespace).getKeys(key); + } + configKeys.forEachRemaining(subKey -> { + if (!isBlank(key) && !subKey.startsWith(key + ".")) { + configKeys.remove(); + } + parseConfigSubKeys(subKey, key, calculatedTenantId, calculatedNamespace, parentMap); + }); + } catch (Exception e) { + LOGGER.warn("Couldn't generate map fot tenant: {}, namespace: {}, key: {}", tenantId, namespace, key, e); + } + return parentMap; + } + private void parseConfigSubKeys(String subKey, String keyPrefix, String tenantId, String namespace, Map<Object, Object> targetMap) { String value = getAsString(tenantId, namespace, subKey); if (!isBlank(keyPrefix) && subKey.startsWith(keyPrefix + ".")) { @@ -318,18 +298,14 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { processingHints = processingHints | hint.value(); } } - tenant = calculateTenant(tenant); namespace = calculateNamespace(namespace); - if (isBlank(key) && !clazz.isAnnotationPresent(Config.class)) { throw new IllegalArgumentException(KEY_CANNOT_BE_NULL); } - if (clazz == null) { throw new IllegalArgumentException("clazz is null."); } - if (ConfigurationUtils.isAPrimitive(clazz)) { clazz = getWrapperClass(clazz); } @@ -341,43 +317,31 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } else if (clazz.isAnnotationPresent(Config.class)) { return getAnnotatedTypeValue(tenant, namespace, clazz, isBlank(key) ? "" : (key + "."), hints); } else { - throw new IllegalArgumentException( - "Only primitive classes, wrapper classes, corresponding array classes and any " - + "class decorated with @org.openecomp.config.api.Config are allowed as argument."); + throw new IllegalArgumentException("Only primitive classes, wrapper classes, corresponding array classes and any " + + "class decorated with @org.openecomp.config.api.Config are allowed as argument."); } } catch (Exception exception) { - LOGGER.warn( - "Failed to get internal value fot tenant: {}, namespace: {}, key: {}, type: {}", - tenant, - namespace, - key, - clazz.getSimpleName(), - exception - ); + LOGGER + .warn("Failed to get internal value fot tenant: {}, namespace: {}, key: {}, type: {}", tenant, namespace, key, clazz.getSimpleName(), + exception); } return null; } private <T> T getWrapperTypeValue(String tenant, String namespace, String key, Class<T> clazz, int processingHints) throws Exception { - Object obj = ConfigurationUtils.getProperty( - ConfigurationRepository.lookup().getConfigurationFor(tenant, namespace), key, processingHints); + Object obj = ConfigurationUtils.getProperty(ConfigurationRepository.lookup().getConfigurationFor(tenant, namespace), key, processingHints); if (obj != null) { if (ConfigurationUtils.isCollection(obj.toString())) { obj = ConfigurationUtils.getCollectionString(obj.toString()); } - String value = ConfigurationUtils.processVariablesIfPresent( - tenant, - namespace, - obj.toString().split(",")[0] - ); + String value = ConfigurationUtils.processVariablesIfPresent(tenant, namespace, obj.toString().split(",")[0]); return (T) getTypeValue(value, clazz, processingHints); } return null; } private <T> T getArrayTypeValue(String tenant, String namespace, String key, Class<T> clazz, int processingHints) throws Exception { - Object obj = ConfigurationUtils.getProperty( - ConfigurationRepository.lookup().getConfigurationFor(tenant, namespace), key, processingHints); + Object obj = ConfigurationUtils.getProperty(ConfigurationRepository.lookup().getConfigurationFor(tenant, namespace), key, processingHints); if (obj != null) { Class componentType = clazz.getComponentType(); if (ConfigurationUtils.isAPrimitivesArray(clazz)) { @@ -388,11 +352,8 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { for (String itemValue : collString.split(",")) { tempCollection.add(ConfigurationUtils.processVariablesIfPresent(tenant, namespace, itemValue)); } - Collection<T> collection = convert( - ConfigurationUtils.getCollectionString(Arrays.toString(tempCollection.toArray())), - (Class<T>) componentType, - processingHints - ); + Collection<T> collection = convert(ConfigurationUtils.getCollectionString(Arrays.toString(tempCollection.toArray())), + (Class<T>) componentType, processingHints); if (ConfigurationUtils.isAPrimitivesArray(clazz)) { return (T) ConfigurationUtils.getPrimitiveArray(collection, componentType); } else { @@ -403,27 +364,8 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } } - private static String calculateNamespace(String namespace) { - - if (isBlank(namespace)) { - return Constants.DEFAULT_NAMESPACE; - } - - return namespace.toUpperCase(); - } - - private static String calculateTenant(String tenant) { - - if (isBlank(tenant)) { - return Constants.DEFAULT_TENANT; - } - - return tenant.toUpperCase(); - } - private <T> T getAnnotatedTypeValue(String tenant, String namespace, Class<T> clazz, String keyPrefix, Hint... hints) - throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { - + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { Config confAnnotation = clazz.getAnnotation(Config.class); if (confAnnotation != null && confAnnotation.key().length() > 0 && !keyPrefix.endsWith(".")) { keyPrefix += (confAnnotation.key() + "."); @@ -438,8 +380,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { if (fieldConfAnnotation == null) { continue; } - if (ConfigurationUtils.isAPrimitiveOrWrapper(fieldType) || - ConfigurationUtils.isAPrimitivesOrWrappersArray(fieldType)) { + if (ConfigurationUtils.isAPrimitiveOrWrapper(fieldType) || ConfigurationUtils.isAPrimitivesOrWrappersArray(fieldType)) { setPrimitiveField(field, objToReturn, tenant, namespace, keyPrefix, hints); } if (ConfigurationUtils.isACollection(fieldType)) { @@ -453,25 +394,23 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } private void setPrimitiveField(Field field, Object objToReturn, String tenant, String namespace, String keyPrefix, Hint[] hints) - throws IllegalAccessException { + throws IllegalAccessException { String fieldConfAnnotationKey = field.getAnnotation(Config.class).key(); Class<?> fieldType = field.getType(); field.set(objToReturn, get(tenant, namespace, keyPrefix + fieldConfAnnotationKey, fieldType, hints)); } - private void setMapField(Field field, Object objToReturn, String tenant, String namespace, String keyPrefix) - throws IllegalAccessException { + private void setMapField(Field field, Object objToReturn, String tenant, String namespace, String keyPrefix) throws IllegalAccessException { String fieldConfAnnotationKey = field.getAnnotation(Config.class).key(); field.set(objToReturn, generateMap(tenant, namespace, keyPrefix + fieldConfAnnotationKey)); } private void setCollectionField(Field field, Object objToReturn, String tenant, String namespace, String keyPrefix, Hint[] hints) - throws IllegalAccessException, InvocationTargetException, InstantiationException { + throws IllegalAccessException, InvocationTargetException, InstantiationException { String fieldConfAnnotationKey = field.getAnnotation(Config.class).key(); Class<?> fieldType = field.getType(); Object obj = get(tenant, namespace, keyPrefix + fieldConfAnnotationKey, - ConfigurationUtils.getArrayClass(ConfigurationUtils.getCollectionGenericType(field)), - hints); + ConfigurationUtils.getArrayClass(ConfigurationUtils.getCollectionGenericType(field)), hints); if (obj == null) { return; } @@ -480,20 +419,16 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { if (fieldType.isInterface()) { clazzToInstantiate = ConfigurationUtils.getConcreteCollection(fieldType).getClass(); } else if (Modifier.isAbstract(fieldType.getModifiers())) { - clazzToInstantiate = - ConfigurationUtils.getCompatibleCollectionForAbstractDef(fieldType) - .getClass(); + clazzToInstantiate = ConfigurationUtils.getCompatibleCollectionForAbstractDef(fieldType).getClass(); } else { clazzToInstantiate = fieldType; } Constructor construct = getConstructorWithArguments(clazzToInstantiate, Collection.class); - if (construct != null) { construct.setAccessible(true); field.set(objToReturn, construct.newInstance(list)); } else { - construct = getConstructorWithArguments(clazzToInstantiate, Integer.class, - Boolean.class, Collection.class); + construct = getConstructorWithArguments(clazzToInstantiate, Integer.class, Boolean.class, Collection.class); if (construct != null) { construct.setAccessible(true); field.set(objToReturn, construct.newInstance(list.size(), true, list)); @@ -555,8 +490,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { private <T> T getValueForStringType(Object obj, int processingHint) { if (obj.toString().startsWith("@") && ConfigurationUtils.isExternalLookup(processingHint)) { String subString = obj.toString().substring(1).trim(); - String contents = ConfigurationUtils.getFileContents( - NON_CONFIG_RESOURCE.locate(subString)); + String contents = ConfigurationUtils.getFileContents(NON_CONFIG_RESOURCE.locate(subString)); if (contents == null) { contents = ConfigurationUtils.getFileContents(subString); } diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationRepository.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationRepository.java index 7b2e267a14..9be4c262b8 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationRepository.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationRepository.java @@ -14,7 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.config.impl; import java.io.File; @@ -40,26 +39,20 @@ public final class ConfigurationRepository { private static final Logger logger = LoggerFactory.getLogger(ConfigurationRepository.class); private static final ConfigurationRepository REPO = new ConfigurationRepository(); - private final Set<String> tenants = Collections.synchronizedSet(new HashSet<>()); - private final Set<String> namespaces = Collections.synchronizedSet(new HashSet<>()); - - private final Map<String, ConfigurationHolder> store = Collections.synchronizedMap( - - new LinkedHashMap<String, ConfigurationHolder>(16, 0.75f, true) { - - @Override - protected boolean removeEldestEntry(Map.Entry eldest) { - try { - return size() > getConfigurationFor(Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE) - .getInt("config.size.max"); - } catch (Exception exception) { - logger.info("ConfigurationException", exception); - return false; - } + private final Map<String, ConfigurationHolder> store = Collections + .synchronizedMap(new LinkedHashMap<String, ConfigurationHolder>(16, 0.75f, true) { + @Override + protected boolean removeEldestEntry(Map.Entry eldest) { + try { + return size() > getConfigurationFor(Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE).getInt("config.size.max"); + } catch (Exception exception) { + logger.info("ConfigurationException", exception); + return false; } - }); + } + }); private ConfigurationRepository() { tenants.add(Constants.DEFAULT_TENANT); @@ -106,22 +99,18 @@ public final class ConfigurationRepository { } public void populateOverrideConfiguration(String key, File file) { - ConfigurationHolder holder = store.get(key); - if (holder == null) { holder = new ConfigurationHolder(new CombinedConfiguration()); store.put(key, holder); } - holder.addOverrideConfiguration(file.getAbsolutePath(), ConfigurationUtils.getConfigurationBuilder(file)); populateTenantsNamespace(key); } private class ConfigurationHolder { - private final Map<String, FileBasedConfigurationBuilder<FileBasedConfiguration>> overrideConfiguration = - new LinkedHashMap<>(); + private final Map<String, FileBasedConfigurationBuilder<FileBasedConfiguration>> overrideConfiguration = new LinkedHashMap<>(); private BasicConfigurationBuilder<Configuration> builder; private Timestamp lastConfigurationBuildTime; private Configuration config; @@ -160,17 +149,13 @@ public final class ConfigurationRepository { if (config == null) { config = builder.getConfiguration(); lastConfigurationBuildTime = new Timestamp(System.currentTimeMillis()); - } else if (lastConfigurationBuildTime != null - && System.currentTimeMillis() - lastConfigurationBuildTime.getTime() - > getConfigurationFor(Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE) - .getInt("config.refresh.interval")) { + } else if (lastConfigurationBuildTime != null && System.currentTimeMillis() - lastConfigurationBuildTime.getTime() > getConfigurationFor( + Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE).getInt("config.refresh.interval")) { lastConfigurationBuildTime = new Timestamp(System.currentTimeMillis()); } - if (composite == null && overrideConfiguration.size() != 0) { composite = getEffectiveConfiguration(config, overrideConfiguration.values()); } - return overrideConfiguration.size() == 0 ? config : composite; } } diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ContextListener.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ContextListener.java index 520852ee1f..9f9706f622 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ContextListener.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ContextListener.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.config.impl; import static org.onap.config.Constants.MBEAN_NAME; diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/YamlConfiguration.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/YamlConfiguration.java index bdaf21915e..d7b8df1487 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/YamlConfiguration.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/YamlConfiguration.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.config.impl; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; @@ -30,5 +29,4 @@ public class YamlConfiguration extends JacksonConfiguration { public YamlConfiguration() { super(new YAMLFactory()); } - } diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/type/ConfigurationMode.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/type/ConfigurationMode.java index 8293abaf5c..e8a35a08e1 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/type/ConfigurationMode.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/type/ConfigurationMode.java @@ -13,9 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.config.type; -public enum ConfigurationMode { - OVERRIDE, UNION, MERGE -} +public enum ConfigurationMode {OVERRIDE, UNION, MERGE} diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/type/ConfigurationQuery.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/type/ConfigurationQuery.java index e0542369c4..e9fc843b7e 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/type/ConfigurationQuery.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/type/ConfigurationQuery.java @@ -13,12 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.config.type; -import org.onap.config.Constants; import lombok.Getter; import lombok.ToString; +import org.onap.config.Constants; @ToString @Getter @@ -59,7 +58,6 @@ public class ConfigurationQuery { return this; } - public ConfigurationQuery namespace(String id) { if (id != null) { namespace = id; diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/type/ConfigurationType.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/type/ConfigurationType.java index 3fd486522d..90b0feafa4 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/type/ConfigurationType.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/type/ConfigurationType.java @@ -13,9 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.config.type; -public enum ConfigurationType { - PROPERTIES, XML, JSON, YAML -} +public enum ConfigurationType {PROPERTIES, XML, JSON, YAML} |