diff options
author | vempo <vitaliy.emporopulo@amdocs.com> | 2018-10-24 17:18:03 +0300 |
---|---|---|
committer | Avi Gaffa <avi.gaffa@amdocs.com> | 2018-10-25 06:09:31 +0000 |
commit | 9125b48d8813a254d5ae14109d5bd81fd34c2f33 (patch) | |
tree | e958a82b887d4d27ca17d61f3a4445b4f58278ff /common/onap-common-configuration-management/onap-configuration-management-core/src/main | |
parent | 04a5bef1c0c8ac2bd22c416ef224d968279550ee (diff) |
Removed code that stored configuration in DB
Conditionally enabled unit tests, fixed a few
static analysis violations, added copyright headers.
Change-Id: I46ccafc5f4397f82b86d5c51bf0452f2060f6a70
Issue-ID: SDC-1867
Signed-off-by: vempo <vitaliy.emporopulo@amdocs.com>
Diffstat (limited to 'common/onap-common-configuration-management/onap-configuration-management-core/src/main')
5 files changed, 189 insertions, 454 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 db64f7f7fc..953ee155ec 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 @@ -1,33 +1,27 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.onap.config; -import com.google.common.collect.ImmutableMap; +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 net.sf.corn.cps.CPScanner; -import net.sf.corn.cps.ResourceFilter; -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.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.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.onap.config.api.Config; -import org.onap.config.api.ConfigurationManager; -import org.onap.config.impl.ConfigurationRepository; -import org.onap.config.impl.YamlConfiguration; -import org.onap.config.impl.AgglomerateConfiguration; -import org.onap.config.impl.ConfigurationDataSource; -import org.onap.config.type.ConfigurationMode; -import org.onap.config.type.ConfigurationType; - -import javax.sql.DataSource; import java.io.File; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; @@ -35,14 +29,11 @@ import java.lang.reflect.Type; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.Statement; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; @@ -66,42 +57,54 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; - -import java.util.concurrent.Executors; -import java.util.concurrent.ThreadFactory; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static com.google.common.collect.ImmutableMap.builder; - -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 static com.google.common.collect.ImmutableMap.builder; -import static org.onap.config.api.Hint.*; +import net.sf.corn.cps.CPScanner; +import net.sf.corn.cps.ResourceFilter; +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.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.AgglomerateConfiguration; +import org.onap.config.impl.ConfigurationRepository; +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; /** * The type Configuration utils. */ 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 ConfigurationUtils() { } - private static ImmutableMap<Class, Class> arrayClassMap; + private static final Map<Class, Class> ARRAY_CLASS_MAP; static { - ImmutableMap.Builder<Class, Class> builder = builder(); - builder.put(Byte.class, Byte[].class).put(Short.class, Short[].class) - .put(Integer.class, Integer[].class).put(Long.class, Long[].class) - .put(Float.class, Float[].class).put(Double.class, Double[].class) - .put(Boolean.class, Boolean[].class).put(Character.class, Character[].class) - .put(String.class, String[].class); - arrayClassMap = builder.build(); + Map<Class, Class> arrayTypes = new HashMap<>(); + arrayTypes.put(Byte.class, Byte[].class); + arrayTypes.put(Short.class, Short[].class); + arrayTypes.put(Integer.class, Integer[].class); + arrayTypes.put(Long.class, Long[].class); + arrayTypes.put(Float.class, Float[].class); + arrayTypes.put(Double.class, Double[].class); + arrayTypes.put(Boolean.class, Boolean[].class); + arrayTypes.put(Character.class, Character[].class); + arrayTypes.put(String.class, String[].class); + ARRAY_CLASS_MAP = Collections.unmodifiableMap(arrayTypes); } /** @@ -110,7 +113,7 @@ public class ConfigurationUtils { * @return the thread factory */ public static ThreadFactory getThreadFactory() { - return (r1) -> { + return r1 -> { Thread thread = Executors.privilegedThreadFactory().newThread(r1); thread.setDaemon(true); return thread; @@ -386,7 +389,7 @@ public class ConfigurationUtils { builder = new Configurations().fileBased(YamlConfiguration.class, url); break; default: - throw new ConfigurationException("Configuration type not supported:" + configType); + throw new ConfigurationException(CONFIGURATION_TYPE_NOT_SUPPORTED + configType); } } catch (ConfigurationException exception) { exception.printStackTrace(); @@ -418,7 +421,7 @@ public class ConfigurationUtils { builder = new Configurations().fileBased(YamlConfiguration.class, file); break; default: - throw new ConfigurationException("Configuration type not supported:" + configType); + throw new ConfigurationException(CONFIGURATION_TYPE_NOT_SUPPORTED + configType); } } catch (ConfigurationException exception) { exception.printStackTrace(); @@ -460,7 +463,7 @@ public class ConfigurationUtils { * @return the array class */ public static Class getArrayClass(Class clazz) { - return arrayClassMap.getOrDefault(clazz, null); + return ARRAY_CLASS_MAP.getOrDefault(clazz, null); } /** @@ -473,29 +476,6 @@ public class ConfigurationUtils { } /** - * Execute ddlsql boolean. - * - * @param sql the sql - * @return the boolean - * @throws Exception the exception - */ - public static boolean executeDdlSql(String sql) throws Exception { - DataSource datasource = ConfigurationDataSource.lookup(); - if (datasource == null) { - System.err.println("DB configuration not found. Configuration management will be using " - + "in-memory persistence."); - return false; - } - try (Connection con = datasource.getConnection(); Statement stmt = con.createStatement()) { - stmt.executeQuery(sql); - } catch (Exception exception) { - System.err.println("Datasource initialization error. Configuration management will be using in-memory persistence."); - return false; - } - return true; - } - - /** * Gets configuration builder. * * @param url the url @@ -545,88 +525,11 @@ public class ConfigurationUtils { builder = new ReloadingFileBasedConfigurationBuilder<>(YamlConfiguration.class); break; default: - throw new IllegalArgumentException("Configuration type not supported:" + configType); + throw new IllegalArgumentException(CONFIGURATION_TYPE_NOT_SUPPORTED + configType); } return builder; } - - /** - * Execute select sql collection. - * - * @param sql the sql - * @param params the params - * @return the collection - * @throws Exception the exception - */ - public static Collection<String> executeSelectSql(String sql, String[] params) throws Exception { - Collection<String> coll = new ArrayList<>(); - DataSource datasource = ConfigurationDataSource.lookup(); - try (Connection con = datasource.getConnection(); - PreparedStatement stmt = con.prepareStatement(sql)) { - if (params != null) { - for (int i = 0; i < params.length; i++) { - stmt.setString(i + 1, params[i]); - } - } - - try (ResultSet rs = stmt.executeQuery()) { - - while (rs.next()) { - coll.add(rs.getString(1)); - } - } - - } catch (Exception exception) { - //exception.printStackTrace(); - return null; - } - - return coll; - } - - /** - * Execute insert sql boolean. - * - * @param sql the sql - * @param params the params - * @return the boolean - * @throws Exception the exception - */ - public static boolean executeInsertSql(String sql, Object[] params) throws Exception { - DataSource datasource = ConfigurationDataSource.lookup(); - try (Connection con = datasource.getConnection(); - PreparedStatement stmt = con.prepareStatement(sql)) { - if (params != null) { - int counter = 0; - for (Object obj : params) { - if (obj == null) { - obj = ""; - } - switch (obj.getClass().getName()) { - case "java.lang.String": - stmt.setString(++counter, obj.toString()); - break; - case "java.lang.Integer": - stmt.setInt(++counter, ((Integer) obj).intValue()); - break; - case "java.lang.Long": - stmt.setLong(++counter, ((Long) obj).longValue()); - break; - default: - stmt.setString(++counter, obj.toString()); - break; - } - } - } - stmt.executeUpdate(); - return true; - } catch (Exception exception) { - exception.printStackTrace(); - } - return false; - } - /** * Read t. * @@ -659,32 +562,6 @@ public class ConfigurationUtils { } /** - * Gets db configuration builder. - * - * @param configName the config name - * @return the db configuration builder - * @throws Exception the exception - */ - public static BasicConfigurationBuilder<AgglomerateConfiguration> getDbConfigurationBuilder( - String configName) throws Exception { - Configuration dbConfig = ConfigurationRepository.lookup() - .getConfigurationFor(Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE); - BasicConfigurationBuilder<AgglomerateConfiguration> builder = - new BasicConfigurationBuilder<AgglomerateConfiguration>(AgglomerateConfiguration.class); - builder.configure( - new Parameters().database() - .setDataSource(ConfigurationDataSource.lookup()) - .setTable(dbConfig.getString("config.Table")) - .setKeyColumn(dbConfig.getString("configKey")) - .setValueColumn(dbConfig.getString("configValue")) - .setConfigurationNameColumn(dbConfig.getString("configNameColumn")) - .setConfigurationName(configName) - .setAutoCommit(true) - ); - return builder; - } - - /** * Gets property. * * @param config the config @@ -777,8 +654,7 @@ public class ConfigurationUtils { } return array; } - Object obj = null; - return obj; + return null; } /** 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 6663c85805..ada40ce6e8 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 @@ -1,27 +1,59 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.onap.config.impl; -import org.apache.commons.configuration2.*; -import org.onap.config.ConfigurationUtils; -import org.onap.config.Constants; -import org.onap.config.api.ConfigurationManager; -import org.onap.config.api.Hint; -import org.onap.config.type.ConfigurationQuery; -import org.onap.config.type.ConfigurationUpdate; +import static org.onap.config.Constants.DB_NAMESPACE; +import static org.onap.config.Constants.DEFAULT_NAMESPACE; +import static org.onap.config.Constants.DEFAULT_TENANT; +import static org.onap.config.Constants.KEY_ELEMENTS_DELEMETER; +import static org.onap.config.Constants.LOAD_ORDER_KEY; +import static org.onap.config.Constants.MBEAN_NAME; +import static org.onap.config.Constants.MODE_KEY; +import static org.onap.config.Constants.NAMESPACE_KEY; import java.io.File; import java.io.PrintWriter; -import java.util.*; import java.lang.management.ManagementFactory; import java.lang.reflect.Method; - +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; import javax.management.MBeanServer; import javax.management.MBeanServerDelegate; import javax.management.MBeanServerNotification; import javax.management.Notification; import javax.management.ObjectName; import javax.management.StandardMBean; - -import static org.onap.config.Constants.*; +import org.apache.commons.configuration2.CombinedConfiguration; +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.onap.config.ConfigurationUtils; +import org.onap.config.Constants; +import org.onap.config.api.ConfigurationManager; +import org.onap.config.api.Hint; +import org.onap.config.type.ConfigurationQuery; +import org.onap.config.type.ConfigurationUpdate; /** * The type Cli configuration. @@ -62,7 +94,6 @@ public final class CliConfigurationImpl extends ConfigurationImpl implements Con if (mbs.getMBeanName() .equals(mbean == null ? new ObjectName(MBEAN_NAME) : new ObjectName(mbean))) { changeNotifier.shutdown(); - ConfigurationDataSource.lookup().close(); } } catch (Exception exception) { //do nothing. @@ -114,7 +145,6 @@ public final class CliConfigurationImpl extends ConfigurationImpl implements Con throw new RuntimeException("Invalid Namespace."); } } catch (NullPointerException e1) { - // TODO Auto-generated catch block e1.printStackTrace(); } @@ -161,9 +191,9 @@ public final class CliConfigurationImpl extends ConfigurationImpl implements Con + updateData.getNamespace()); pc.setProperty(MODE_KEY, "OVERRIDE"); pc.setProperty(updateData.getKey(), updateData.getValue()); - if (System.getProperty("node.config.location") != null - && System.getProperty("node.config.location").trim().length() > 0) { - File file = new File(System.getProperty("node.config.location"), + String nodeConfigLocation = System.getProperty("node.config.location"); + if (nodeConfigLocation != null && nodeConfigLocation.trim().length() > 0) { + File file = new File(nodeConfigLocation, updateData.getTenant() + File.separator + updateData.getNamespace() + File.separator + "config.properties"); file.getParentFile().mkdirs(); @@ -179,11 +209,7 @@ public final class CliConfigurationImpl extends ConfigurationImpl implements Con .setProperty(updateData.getKey(), updateData.getValue()); } } - if (!updateData.isNodeOverride()) { - ConfigurationUtils.executeInsertSql( - ConfigurationRepository.lookup().getConfigurationFor(DEFAULT_TENANT, DB_NAMESPACE) - .getString("insertconfigurationchangecql"), paramArray); - } else { + if (updateData.isNodeOverride()) { ConfigurationRepository.lookup().refreshOverrideConfigurtaionFor( updateData.getTenant() + KEY_ELEMENTS_DELEMETER + updateData.getNamespace(), overrideIndex); @@ -306,30 +332,10 @@ public final class CliConfigurationImpl extends ConfigurationImpl implements Con @Override public Collection<String> getKeys(String tenant, String namespace) { Set<String> keyCollection = new HashSet<>(); - try { - keyCollection.addAll(ConfigurationUtils.executeSelectSql( - ConfigurationRepository.lookup().getConfigurationFor(DEFAULT_TENANT, DB_NAMESPACE) - .getString("fetchkeysql"), - new String[]{tenant + KEY_ELEMENTS_DELEMETER + DEFAULT_NAMESPACE})); - keyCollection.addAll(ConfigurationUtils.executeSelectSql( - ConfigurationRepository.lookup().getConfigurationFor(DEFAULT_TENANT, DB_NAMESPACE) - .getString("fetchkeysql"), - new String[]{tenant + KEY_ELEMENTS_DELEMETER + namespace})); - keyCollection.addAll(ConfigurationUtils.executeSelectSql( - ConfigurationRepository.lookup().getConfigurationFor(DEFAULT_TENANT, DB_NAMESPACE) - .getString("fetchkeysql"), - new String[]{DEFAULT_TENANT + KEY_ELEMENTS_DELEMETER + namespace})); - keyCollection.addAll(ConfigurationUtils.executeSelectSql( - ConfigurationRepository.lookup().getConfigurationFor(DEFAULT_TENANT, DB_NAMESPACE) - .getString("fetchkeysql"), - new String[]{DEFAULT_TENANT + KEY_ELEMENTS_DELEMETER + DEFAULT_NAMESPACE})); - } catch (Exception exception) { - exception.printStackTrace(); - keyCollection.addAll(getInMemoryKeys(tenant, DEFAULT_NAMESPACE)); - keyCollection.addAll(getInMemoryKeys(tenant, namespace)); - keyCollection.addAll(getInMemoryKeys(DEFAULT_TENANT, namespace)); - keyCollection.addAll(getInMemoryKeys(DEFAULT_TENANT, DEFAULT_NAMESPACE)); - } + keyCollection.addAll(getInMemoryKeys(tenant, DEFAULT_NAMESPACE)); + keyCollection.addAll(getInMemoryKeys(tenant, namespace)); + keyCollection.addAll(getInMemoryKeys(DEFAULT_TENANT, namespace)); + keyCollection.addAll(getInMemoryKeys(DEFAULT_TENANT, DEFAULT_NAMESPACE)); return keyCollection; } } diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationDataSource.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationDataSource.java deleted file mode 100644 index 64fd22e431..0000000000 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationDataSource.java +++ /dev/null @@ -1,100 +0,0 @@ -package org.onap.config.impl; - -import org.apache.commons.configuration2.ImmutableConfiguration; -import org.apache.commons.dbcp2.BasicDataSource; -import org.apache.commons.lang3.StringUtils; -import org.onap.config.ConfigurationUtils; -import org.onap.config.Constants; -import java.util.*; -import java.sql.Driver; - -/** - * The type Configuration data source. - */ -public final class ConfigurationDataSource { - - private static BasicDataSource configDBDatasource = null; - private static Set<String> validCallers = Collections.unmodifiableSet(new HashSet<>( - Arrays.asList(ConfigurationUtils.class.getName(), CliConfigurationImpl.class.getName()))); - - private ConfigurationDataSource() { - //Hide constructor to prevent instantiation using the default implicit constructor - } - - static { - try { - configDBDatasource = initDataSource(); - } catch (Exception exception) { - System.err.println("Datasource initialization error. Configuration management will be using" - + "in-memory persistence."); - } - } - - /** - * Lookup basic data source. - * - * @return the basic data source - * @throws Exception the exception - */ - public static BasicDataSource lookup() throws Exception { - if (validCallers.contains(Thread.currentThread().getStackTrace()[2].getClassName())) { - return configDBDatasource; - } else { - return null; - } - } - - /** - * Init data source basic data source. - * - * @return the basic data source - * @throws Exception the exception - */ - public static BasicDataSource initDataSource() throws Exception { - ImmutableConfiguration dbConfig = ConfigurationRepository.lookup() - .getConfigurationFor(Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE); - if (StringUtils.isEmpty(dbConfig.getString("dbhost"))) { - return null; - } - BasicDataSource datasource = new BasicDataSource(); - String driverClassName = dbConfig.getString("driverClassName"); - String jdbcUrl = dbConfig.getString("jdbcURL"); - if (!isDriverSuitable(driverClassName, jdbcUrl)) { - driverClassName = getDriverFor(jdbcUrl); - } - datasource.setDriverClassName(driverClassName); - datasource.setUrl(jdbcUrl); - String dbuser = dbConfig.getString("dbuser"); - String dbpassword = dbConfig.getString("dbpassword"); - if (dbuser != null && dbuser.trim().length() > 0) { - datasource.setUsername(dbuser); - } - if (dbpassword != null && dbpassword.trim().length() > 0) { - datasource.setPassword(dbpassword); - } - return datasource; - } - - private static boolean isDriverSuitable(String driverClassName, String url) { - if (driverClassName == null || driverClassName.trim().length() == 0) { - return false; - } - try { - Driver driver = Driver.class.cast(Class.forName(driverClassName).newInstance()); - return driver.acceptsURL(url); - } catch (Exception exception) { - return false; - } - } - - private static String getDriverFor(String url) throws Exception { - ServiceLoader<Driver> loader = ServiceLoader.load(Driver.class); - for (Driver driver : loader) { - if (driver.acceptsURL(url)) { - return driver.getClass().getName(); - } - } - throw new RuntimeException("No Suitable driver found for " + url); - } - -} 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 00725b1165..7da0a0feaf 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 @@ -1,13 +1,20 @@ -package org.onap.config.impl; +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -import org.apache.commons.configuration2.Configuration; -import org.apache.commons.configuration2.DatabaseConfiguration; -import org.onap.config.ConfigurationUtils; -import org.onap.config.Constants; -import org.onap.config.NonConfigResource; -import org.onap.config.api.Config; -import org.onap.config.api.ConfigurationChangeListener; -import org.onap.config.api.Hint; +package org.onap.config.impl; import java.io.File; import java.lang.reflect.Constructor; @@ -23,15 +30,22 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.Predicate; - -import static org.onap.config.ConfigurationUtils.getConfigurationRepositoryKey; +import org.onap.config.ConfigurationUtils; +import org.onap.config.Constants; +import org.onap.config.NonConfigResource; +import org.onap.config.api.Config; +import org.onap.config.api.ConfigurationChangeListener; +import org.onap.config.api.Hint; /** * The type Configuration. */ public class ConfigurationImpl implements org.onap.config.api.Configuration { + private static final String KEY_CANNOT_BE_NULL = "Key can't be null."; private static ThreadLocal<String> tenant = new ThreadLocal<String>() { + + @Override protected String initialValue() { return Constants.DEFAULT_TENANT; } @@ -141,7 +155,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { ConfigurationRepository.lookup().isValidNamespace(namespace) ? namespace.toUpperCase() : Constants.DEFAULT_NAMESPACE; if (key == null || key.trim().length() == 0) { - throw new IllegalArgumentException("Key can't be null."); + throw new IllegalArgumentException(KEY_CANNOT_BE_NULL); } if (myself == null) { throw new IllegalArgumentException("ConfigurationChangeListener instance is null."); @@ -153,9 +167,8 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } } - private void populateFinalConfigurationIncrementally(Map<String, AggregateConfiguration> configs) - throws Exception { - boolean isDbAccessible = false; + private void populateFinalConfigurationIncrementally(Map<String, AggregateConfiguration> configs) { + if (configs.get( Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELEMETER + Constants.DB_NAMESPACE) != null) { @@ -164,42 +177,12 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { configs.remove( Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELEMETER + Constants.DB_NAMESPACE) .getFinalConfiguration()); - isDbAccessible = ConfigurationUtils.executeDdlSql(ConfigurationRepository.lookup() - .getConfigurationFor(Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE) - .getString("createtablecql")); - if (isDbAccessible) { - ConfigurationUtils.executeDdlSql(ConfigurationRepository.lookup() - .getConfigurationFor(Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE) - .getString("createmonitoringtablecql")); - } } Set<String> modules = configs.keySet(); for (String module : modules) { - if (isDbAccessible) { - DatabaseConfiguration config = - ConfigurationUtils.getDbConfigurationBuilder(module).getConfiguration(); - Configuration currentConfig = configs.get(module).getFinalConfiguration(); - Iterator<String> keys = currentConfig.getKeys(); - while (keys.hasNext()) { - String currentKey = keys.next(); - if (!(Constants.MODE_KEY.equals(currentKey) - || Constants.NAMESPACE_KEY.equals(currentKey) - || Constants.LOAD_ORDER_KEY.equals(currentKey))) { - if (!config.containsKey(currentKey)) { - Object propValue = currentConfig.getProperty(currentKey); - if (propValue instanceof Collection) { - config.addProperty(currentKey, propValue.toString()); - } else { - config.addProperty(currentKey, propValue); - } - } - } - } - } else { ConfigurationRepository.lookup() .populateConfigurtaion(module, configs.get(module).getFinalConfiguration()); - } } } @@ -276,7 +259,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } if (tenant == null || tenant.trim().length() == 0) { - tenant = this.tenant.get(); + tenant = ConfigurationImpl.tenant.get(); } else { tenant = tenant.toUpperCase(); } @@ -285,10 +268,8 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } else { namespace = namespace.toUpperCase(); } - if (key == null || key.trim().length() == 0) { - if (!clazz.isAnnotationPresent(Config.class)) { - throw new IllegalArgumentException("Key can't be null."); - } + if ((key == null || key.trim().length() == 0) && !clazz.isAnnotationPresent(Config.class)) { + throw new IllegalArgumentException(KEY_CANNOT_BE_NULL); } if (clazz == null) { throw new IllegalArgumentException("clazz is null."); @@ -481,7 +462,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { Float floatVal = doubleValue.floatValue(); return (T) floatVal; case "java.lang.Double": - Double doubleVal = doubleValue.doubleValue(); + Double doubleVal = doubleValue; return (T) doubleVal; default: } @@ -556,7 +537,6 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { if (changeNotifier != null) { try { changeNotifier.shutdown(); - ConfigurationDataSource.lookup().close(); } catch (Exception exception) { exception.printStackTrace(); } @@ -572,7 +552,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { ConfigurationRepository.lookup().isValidNamespace(namespace) ? namespace.toUpperCase() : Constants.DEFAULT_NAMESPACE; if (key == null || key.trim().length() == 0) { - throw new IllegalArgumentException("Key can't be null."); + throw new IllegalArgumentException(KEY_CANNOT_BE_NULL); } try { changeNotifier.stopNotificationTowards(tenant, namespace, key, myself); @@ -584,7 +564,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { @Override public <T> Map<String, T> populateMap(String tenantId, String namespace, String key, Class<T> clazz){ if (tenantId==null || tenantId.trim().length()==0){ - tenantId = this.tenant.get(); + tenantId = tenant.get(); }else{ tenantId = tenantId.toUpperCase(); } @@ -596,12 +576,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { Map<String, T> map = new HashMap<>(); Iterator<String> keys ; try { - if (ConfigurationRepository.lookup().isDBAccessible()){ - keys = ConfigurationUtils.executeSelectSql(ConfigurationRepository.lookup().getConfigurationFor( - Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE).getString("fetchkeysql"), new String[]{tenantId+ Constants.KEY_ELEMENTS_DELEMETER+namespace}).iterator(); - }else{ - keys = ConfigurationRepository.lookup().getConfigurationFor(tenantId, namespace).getKeys(key); - } + keys = ConfigurationRepository.lookup().getConfigurationFor(tenantId, namespace).getKeys(key); while(keys.hasNext()){ String k = keys.next(); if (k.startsWith(key+".")){ @@ -621,7 +596,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { @Override public Map generateMap(String tenantId, String namespace, String key){ if (tenantId==null || tenantId.trim().length()==0){ - tenantId = this.tenant.get(); + tenantId = tenant.get(); }else{ tenantId = tenantId.toUpperCase(); } @@ -633,15 +608,10 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { Map map, parentMap = new HashMap<>(); Iterator<String> keys ; try { - if (ConfigurationRepository.lookup().isDBAccessible()){ - keys = ConfigurationUtils.executeSelectSql(ConfigurationRepository.lookup().getConfigurationFor( - Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE).getString("fetchkeysql"), new String[]{tenantId+ Constants.KEY_ELEMENTS_DELEMETER+namespace}).iterator(); + if (key==null || key.trim().length()==0){ + keys = ConfigurationRepository.lookup().getConfigurationFor(tenantId, namespace).getKeys(); }else{ - if (key==null || key.trim().length()==0){ - keys = ConfigurationRepository.lookup().getConfigurationFor(tenantId, namespace).getKeys(); - }else{ - keys = ConfigurationRepository.lookup().getConfigurationFor(tenantId, namespace).getKeys(key); - } + keys = ConfigurationRepository.lookup().getConfigurationFor(tenantId, namespace).getKeys(key); } while(keys.hasNext()){ map = parentMap; 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 953f793b9d..90346d1ec3 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 @@ -1,5 +1,32 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.onap.config.impl; +import java.io.File; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; import org.apache.commons.configuration2.CombinedConfiguration; import org.apache.commons.configuration2.CompositeConfiguration; import org.apache.commons.configuration2.Configuration; @@ -10,10 +37,6 @@ import org.apache.commons.configuration2.ex.ConfigurationException; import org.onap.config.ConfigurationUtils; import org.onap.config.Constants; -import java.io.File; -import java.sql.Timestamp; -import java.util.*; - /** * The type Configuration repository. */ @@ -26,7 +49,7 @@ public final class ConfigurationRepository { private static Set<String> validCallers = Collections.unmodifiableSet(new HashSet<>(Arrays .asList(ConfigurationChangeNotifier.NotificationData.class.getName(), ConfigurationUtils.class.getName(), CliConfigurationImpl.class.getName(), - ConfigurationChangeNotifier.class.getName(), ConfigurationDataSource.class.getName(), + ConfigurationChangeNotifier.class.getName(), ConfigurationImpl.class.getName()))); static { @@ -102,17 +125,7 @@ public final class ConfigurationRepository { * Init tenants and namespaces. */ public void initTenantsAndNamespaces() { - try { - Collection<String> collection = ConfigurationUtils.executeSelectSql( - getConfigurationFor(Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE) - .getString("fetchnamescql"), new String[]{}); - Iterator<String> iterator = collection.iterator(); - while (iterator.hasNext()) { - populateTenantsNamespace(iterator.next(), true); - } - } catch (Exception exception) { - //Log this later - } + // nothing to do here, left for backward compatibility } /** @@ -122,7 +135,7 @@ public final class ConfigurationRepository { * @return the boolean */ public boolean isValidTenant(String tenant) { - return tenant == null ? false : tenants.contains(tenant.toUpperCase()); + return tenant != null && tenants.contains(tenant.toUpperCase()); } /** @@ -132,7 +145,7 @@ public final class ConfigurationRepository { * @return the boolean */ public boolean isValidNamespace(String namespace) { - return namespace == null ? false : namespaces.contains(namespace.toUpperCase()); + return namespace != null && namespaces.contains(namespace.toUpperCase()); } /** @@ -148,8 +161,7 @@ public final class ConfigurationRepository { String module = tenant + Constants.KEY_ELEMENTS_DELEMETER + namespace; config = store.get(module); if (config == null) { - config = new ConfigurationHolder(ConfigurationUtils - .getDbConfigurationBuilder(tenant + Constants.KEY_ELEMENTS_DELEMETER + namespace)); + config = new ConfigurationHolder(new BasicConfigurationBuilder<>(AgglomerateConfiguration.class)); store.put(module, config); } return config.getConfiguration(tenant + Constants.KEY_ELEMENTS_DELEMETER + namespace); @@ -167,32 +179,16 @@ public final class ConfigurationRepository { } /** - * Populate configurtaion. - * - * @param key the key - * @param builder the builder - * @throws Exception the exception - */ - public void populateConfigurtaion(String key, BasicConfigurationBuilder builder) - throws Exception { - store.put(key, new ConfigurationHolder(builder)); - } - - /** * Populate override configurtaion. * * @param key the key * @param file the file * @throws Exception the exception */ - public void populateOverrideConfigurtaion(String key, File file) throws Exception { + public void populateOverrideConfigurtaion(String key, File file) { ConfigurationHolder holder = store.get(key); if (holder == null) { - if (dbAccessible) { - holder = new ConfigurationHolder(ConfigurationUtils.getDbConfigurationBuilder(key)); - } else { holder = new ConfigurationHolder(new CombinedConfiguration()); - } store.put(key, holder); } holder.addOverrideConfiguration(file.getAbsolutePath(), @@ -207,7 +203,7 @@ public final class ConfigurationRepository { * @param index the index * @throws Exception the exception */ - public void refreshOverrideConfigurtaionFor(String key, int index) throws Exception { + public void refreshOverrideConfigurtaionFor(String key, int index) { ConfigurationHolder holder = store.get(key); if (holder != null) { holder.refreshOverrideConfiguration(index); @@ -380,20 +376,7 @@ public final class ConfigurationRepository { * @return the last update timestamp for */ public Timestamp getLastUpdateTimestampFor(String namespace) { - Timestamp timestamp = null; - - try { - Collection<String> collection = ConfigurationUtils.executeSelectSql( - getConfigurationFor(Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE) - .getString("fetchlastchangecql"), new String[]{namespace}); - if (!collection.isEmpty()) { - timestamp = new Timestamp(Long.valueOf(((ArrayList) collection).get(0).toString())); - } - } catch (Exception exception) { - //Log this later - } - - return timestamp; + return null; } |