diff options
author | vempo <vitaliy.emporopulo@amdocs.com> | 2018-10-28 14:45:46 +0200 |
---|---|---|
committer | vempo <vitaliy.emporopulo@amdocs.com> | 2018-10-28 14:45:46 +0200 |
commit | 2074ab2e8b4416126542c09205d3ca6646ed6718 (patch) | |
tree | ab4e013ce2d30d8e87221b58d673b6525d952dd9 /common/onap-common-configuration-management/onap-configuration-management-core/src/main | |
parent | 313e73a58a9a01bbceb007b6de617fda521bdd6e (diff) |
Removed support of dynamic configuration
Configuration framework will not poll configuration
for changes, and will not notify client code of them.
Also minor cleanup, added unit tests.
Change-Id: I428b23f7acb13c6610390f46aae6e011d6b0ee80
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')
9 files changed, 26 insertions, 707 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 528941b923..dcdf17a992 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 @@ -48,10 +48,8 @@ import java.util.SortedSet; import java.util.TreeSet; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedTransferQueue; -import java.util.concurrent.ThreadFactory; import java.util.concurrent.TransferQueue; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -102,14 +100,7 @@ public class ConfigurationUtils { } private ConfigurationUtils() { - } - - public static ThreadFactory getThreadFactory() { - return r1 -> { - Thread thread = Executors.privilegedThreadFactory().newThread(r1); - thread.setDaemon(true); - return thread; - }; + // prevent instantiation } public static Collection<File> getAllFiles(File file, boolean recursive, boolean onlyDirectory) { @@ -303,8 +294,9 @@ public class ConfigurationUtils { } public static boolean isWrapperClass(Class clazz) { - return clazz == String.class || clazz == Boolean.class || clazz == Character.class - || Number.class.isAssignableFrom(clazz); + return clazz == String.class || clazz == Boolean.class || clazz == Character.class || Number.class + .isAssignableFrom( + clazz); } public static Class getArrayClass(Class clazz) { @@ -324,8 +316,8 @@ public class ConfigurationUtils { return builder; } - private static ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> - getFileBasedConfigurationReloadingFileBasedConfigurationBuilder(ConfigurationType configType) { + private static ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> getFileBasedConfigurationReloadingFileBasedConfigurationBuilder( + ConfigurationType configType) { ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builder; switch (configType) { @@ -569,7 +561,7 @@ public class ConfigurationUtils { stack.push(element); } String toReturn = stack.pop(); - return stack.pop() + Constants.KEY_ELEMENTS_DELIMETER + toReturn; + return stack.pop() + Constants.KEY_ELEMENTS_DELIMITER + toReturn; } public static String getNamespace(File file) { 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 290a7a5a4b..97963c4c4e 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 @@ -21,7 +21,7 @@ public class Constants { public static final String DEFAULT_TENANT = "GLOBAL"; public static final String DEFAULT_NAMESPACE = "COMMON"; public static final String DB_NAMESPACE = "SYSTEM"; - public static final String KEY_ELEMENTS_DELIMETER = "-"; + public static final String KEY_ELEMENTS_DELIMITER = "-"; public static final String TENANT_NAMESPACE_SEPARATOR = ":"; public static final String NAMESPACE_KEY = "_config.namespace"; public static final String MODE_KEY = "_config.mergeStrategy"; 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 80fbaf8fd0..32a1902151 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 @@ -40,18 +40,6 @@ public final class AggregateConfiguration { private final Map<String, Configuration> mergeConfig = new HashMap<>(); private final Map<String, Configuration> overrideConfig = new LinkedHashMap<>(); - public AggregateConfiguration() { - - try { - Class clazz = Class.forName(Thread.currentThread().getStackTrace()[2].getClassName()); - if (!clazz.getCanonicalName().equals(ConfigurationImpl.class.getCanonicalName())) { - throw new RuntimeException("Illegal access."); - } - } catch (ClassNotFoundException cfe) { - throw new RuntimeException("Class not found while loading change notifier"); - } - } - public void addConfig(File file) throws Exception { addConfig(file.getAbsolutePath().toUpperCase(), ConfigurationUtils.getMergeStrategy(file), ConfigurationUtils.getConfigurationBuilder(file, false).getConfiguration()); 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 7f753fd1fd..77d0c11c9f 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 @@ -16,17 +16,13 @@ package org.onap.config.impl; -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_DELIMETER; 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.lang.management.ManagementFactory; import java.lang.reflect.Method; import java.util.ArrayList; @@ -39,22 +35,12 @@ 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 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; public final class CliConfigurationImpl extends ConfigurationImpl implements ConfigurationManager { @@ -65,27 +51,6 @@ public final class CliConfigurationImpl extends ConfigurationImpl implements Con mbs.unregisterMBean(name); } mbs.registerMBean(new StandardMBean(this, ConfigurationManager.class), name); - mbs.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME, - (notification, handback) -> handleNotification(notification), null, null); - } - - public void handleNotification(Notification notification) { - if (notification instanceof MBeanServerNotification) { - MBeanServerNotification mbs = (MBeanServerNotification) notification; - if (MBeanServerNotification.UNREGISTRATION_NOTIFICATION.equals(mbs.getType())) { - try { - String mbean = ConfigurationRepository.lookup().getConfigurationFor(DEFAULT_TENANT, DB_NAMESPACE) - .getString("shutdown.mbean"); - if (mbs.getMBeanName().equals(mbean == null ? new ObjectName(MBEAN_NAME) : new ObjectName(mbean))) { - changeNotifier.shutdown(); - } - } catch (Exception exception) { - //do nothing. - } - } else if (MBeanServerNotification.REGISTRATION_NOTIFICATION.equals(mbs.getType())) { - mbs.getMBeanName(); - } - } } public String getConfigurationValue(Map<String, Object> input) { @@ -116,7 +81,9 @@ public final class CliConfigurationImpl extends ConfigurationImpl implements Con } private Object getInput(Map<String, Object> input) { + Object toReturn = null; + try { toReturn = Class.forName(input.get("ImplClass").toString()).newInstance(); Method[] methods = toReturn.getClass().getMethods(); @@ -132,111 +99,6 @@ public final class CliConfigurationImpl extends ConfigurationImpl implements Con return toReturn; } - public void updateConfigurationValue(Map<String, Object> input) { - updateConfigurationValue((ConfigurationUpdate) getInput(input)); - } - - private void updateConfigurationValue(ConfigurationUpdate updateData) { - - try { - if (!ConfigurationRepository.lookup().isValidTenant(updateData.getTenant())) { - throw new RuntimeException("Invalid tenantId."); - } - if (!ConfigurationRepository.lookup().isValidNamespace(updateData.getNamespace())) { - throw new RuntimeException("Invalid Namespace."); - } - } catch (NullPointerException e1) { - e1.printStackTrace(); - } - - try { - boolean keyPresent = isKeyExists(updateData.getTenant(), updateData.getNamespace(), updateData.getKey()); - if (keyPresent) { - boolean isUpdated = false; - Object[] paramArray = - new Object[] {updateData.getTenant() + KEY_ELEMENTS_DELIMETER + updateData.getNamespace(), - System.currentTimeMillis(), updateData.getKey(), getConfigurationValue(updateData), - updateData.getValue()}; - Configuration config = ConfigurationRepository.lookup() - .getConfigurationFor(updateData.getTenant(), updateData.getNamespace()); - if (config instanceof AgglomerateConfiguration || config instanceof CombinedConfiguration) { - CompositeConfiguration cc = new CompositeConfiguration(); - cc.addConfiguration(config); - config = cc; - } - CompositeConfiguration configuration = (CompositeConfiguration) config; - int overrideIndex = -1; - for (int i = 0; i < configuration.getNumberOfConfigurations(); i++) { - if (!updateData.isNodeOverride() && ( - configuration.getConfiguration(i) instanceof AgglomerateConfiguration - || configuration.getConfiguration(i) instanceof CombinedConfiguration)) { - configuration.getConfiguration(i).setProperty(updateData.getKey(), updateData.getValue()); - isUpdated = true; - break; - } else if (updateData.isNodeOverride() && configuration.getConfiguration( - i) instanceof FileBasedConfiguration) { - configuration.getConfiguration(i).setProperty(updateData.getKey(), updateData.getValue()); - isUpdated = true; - overrideIndex = i; - break; - } - } - if (!isUpdated) { - if (updateData.isNodeOverride()) { - PropertiesConfiguration pc = new PropertiesConfiguration(); - pc.setProperty(NAMESPACE_KEY, - updateData.getTenant() + Constants.TENANT_NAMESPACE_SEPARATOR - + updateData.getNamespace()); - pc.setProperty(MODE_KEY, "OVERRIDE"); - pc.setProperty(updateData.getKey(), updateData.getValue()); - 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(); - PrintWriter out = new PrintWriter(file); - pc.write(out); - out.close(); - ConfigurationRepository.lookup().populateOverrideConfiguration( - updateData.getTenant() + KEY_ELEMENTS_DELIMETER + updateData.getNamespace(), file); - } - } else { - configuration.getConfiguration(0).setProperty(updateData.getKey(), updateData.getValue()); - } - } - if (updateData.isNodeOverride()) { - ConfigurationRepository.lookup().refreshOverrideConfigurationFor( - updateData.getTenant() + KEY_ELEMENTS_DELIMETER + updateData.getNamespace(), overrideIndex); - } - } - } catch (Exception exception) { - exception.printStackTrace(); - } - } - - private boolean isKeyExists(String tenant, String namespace, String key) { - boolean keyExist = false; - try { - keyExist = ConfigurationRepository.lookup().getConfigurationFor(tenant, namespace).containsKey(key); - if (!keyExist && !DEFAULT_TENANT.equals(tenant)) { - keyExist = ConfigurationRepository.lookup().getConfigurationFor(DEFAULT_TENANT, namespace) - .containsKey(key); - } - if (!keyExist && !DEFAULT_NAMESPACE.equals(namespace)) { - keyExist = ConfigurationRepository.lookup().getConfigurationFor(tenant, DEFAULT_NAMESPACE) - .containsKey(key); - } - if (!keyExist && !DEFAULT_TENANT.equals(tenant) && !DEFAULT_NAMESPACE.equals(namespace)) { - keyExist = ConfigurationRepository.lookup().getConfigurationFor(DEFAULT_TENANT, DEFAULT_NAMESPACE) - .containsKey(key); - } - } catch (Exception exception) { - exception.printStackTrace(); - } - return keyExist; - } - public Map<String, String> listConfiguration(Map<String, Object> input) { return listConfiguration((ConfigurationQuery) getInput(input)); } @@ -256,6 +118,7 @@ public final class CliConfigurationImpl extends ConfigurationImpl implements Con } private ArrayList<String> getInMemoryKeys(String tenant, String namespace) { + ArrayList<String> keys = new ArrayList<>(); try { @@ -274,24 +137,6 @@ public final class CliConfigurationImpl extends ConfigurationImpl implements Con } @Override - public boolean updateConfigurationValues(String tenant, String namespace, Map configKeyValueStore) { - boolean valueToReturn = true; - for (String s : (Iterable<String>) configKeyValueStore.keySet()) { - try { - String key = s; - ConfigurationUpdate updateData = new ConfigurationUpdate(); - updateData.tenant(tenant).namespace(namespace).key(key); - updateData.value(configKeyValueStore.get(key).toString()); - updateConfigurationValue(updateData); - } catch (Exception exception) { - exception.printStackTrace(); - valueToReturn = false; - } - } - return valueToReturn; - } - - @Override public Collection<String> getTenants() { return ConfigurationRepository.lookup().getTenants(); } diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationChangeNotifier.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationChangeNotifier.java deleted file mode 100644 index cd6481f97b..0000000000 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationChangeNotifier.java +++ /dev/null @@ -1,393 +0,0 @@ -/* - * 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.io.IOException; -import java.lang.management.ManagementFactory; -import java.lang.reflect.Method; -import java.nio.file.ClosedWatchServiceException; -import java.nio.file.FileSystems; -import java.nio.file.Path; -import java.nio.file.StandardWatchEventKinds; -import java.nio.file.WatchEvent; -import java.nio.file.WatchKey; -import java.nio.file.WatchService; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import java.util.Vector; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; -import javax.management.JMX; -import javax.management.MBeanServerConnection; -import javax.management.ObjectName; -import org.onap.config.ConfigurationUtils; -import org.onap.config.Constants; -import org.onap.config.api.ConfigurationChangeListener; -import org.onap.config.api.ConfigurationManager; -import org.onap.config.api.Hint; - - -public final class ConfigurationChangeNotifier { - - static { - if (!Thread.currentThread().getStackTrace()[2].getClassName().equals(ConfigurationImpl.class.getName())) { - throw new RuntimeException("Illegal access."); - } - } - - private final HashMap<String, List<NotificationData>> store = new HashMap<>(); - private final ScheduledExecutorService executor = - Executors.newScheduledThreadPool(5, ConfigurationUtils.getThreadFactory()); - private final ExecutorService notificationExecutor = - Executors.newCachedThreadPool(ConfigurationUtils.getThreadFactory()); - private final Map<String, WatchService> watchServiceCollection = Collections.synchronizedMap(new HashMap<>()); - - public ConfigurationChangeNotifier(Map<String, AggregateConfiguration> inMemoryConfig) { - executor.scheduleWithFixedDelay(() -> this.pollFilesystemAndUpdateConfigurationIfRequired(inMemoryConfig, - System.getProperty("config.location"), false), 1, 1, TimeUnit.MILLISECONDS); - executor.scheduleWithFixedDelay(() -> this.pollFilesystemAndUpdateConfigurationIfRequired(inMemoryConfig, - System.getProperty("tenant.config.location"), true), 1, 1, TimeUnit.MILLISECONDS); - executor.scheduleWithFixedDelay(() -> this.pollFilesystemAndUpdateNodeSpecificConfigurationIfRequired( - System.getProperty("node.config.location")), 1, 1, TimeUnit.MILLISECONDS); - } - - public void pollFilesystemAndUpdateConfigurationIfRequired(Map<String, AggregateConfiguration> inMemoryConfig, - String location, boolean isTenantLocation) { - try { - Set<Path> paths = watchForChange(location); - if (paths != null) { - for (Path path : paths) { - File file = path.toAbsolutePath().toFile(); - String repositoryKey = null; - if (ConfigurationUtils.isConfig(file) && file.isFile()) { - if (isTenantLocation) { - Collection<File> tenantsRoot = - ConfigurationUtils.getAllFiles(new File(location), false, true); - for (File tenantRoot : tenantsRoot) { - if (file.getAbsolutePath().startsWith(tenantRoot.getAbsolutePath())) { - repositoryKey = ConfigurationUtils.getConfigurationRepositoryKey( - (tenantRoot.getName() + Constants.TENANT_NAMESPACE_SEPARATOR - + ConfigurationUtils.getNamespace(file)) - .split(Constants.TENANT_NAMESPACE_SEPARATOR)); - } - } - } else { - repositoryKey = ConfigurationUtils.getConfigurationRepositoryKey(file); - } - AggregateConfiguration config = inMemoryConfig.get(repositoryKey); - if (config != null) { - LinkedHashMap origConfig = ConfigurationUtils.toMap(config.getFinalConfiguration()); - config.addConfig(file); - LinkedHashMap latestConfig = ConfigurationUtils.toMap(config.getFinalConfiguration()); - Map map = ConfigurationUtils.diff(origConfig, latestConfig); - String[] tenantNamespaceArray = repositoryKey.split(Constants.KEY_ELEMENTS_DELIMETER); - updateConfigurationValues(tenantNamespaceArray[0], tenantNamespaceArray[1], map); - } - } else { - for (String configKey : inMemoryConfig.keySet()) { - repositoryKey = configKey; - AggregateConfiguration config = inMemoryConfig.get(repositoryKey); - if (config.containsConfig(file)) { - LinkedHashMap origConfig = ConfigurationUtils.toMap(config.getFinalConfiguration()); - config.removeConfig(file); - LinkedHashMap latestConfig = ConfigurationUtils.toMap(config.getFinalConfiguration()); - Map map = ConfigurationUtils.diff(origConfig, latestConfig); - String[] tenantNamespaceArray = repositoryKey.split(Constants.KEY_ELEMENTS_DELIMETER); - updateConfigurationValues(tenantNamespaceArray[0], tenantNamespaceArray[1], map); - } - } - } - } - } - } catch (ClosedWatchServiceException exception) { - // do nothing. - } catch (Exception exception) { - exception.printStackTrace(); - } - } - - public void pollFilesystemAndUpdateNodeSpecificConfigurationIfRequired(String location) { - try { - Set<Path> paths = watchForChange(location); - if (paths != null) { - for (Path path : paths) { - File file = path.toAbsolutePath().toFile(); - - if (ConfigurationUtils.isConfig(file)) { - String repositoryKey = ConfigurationUtils.getConfigurationRepositoryKey(file); - ConfigurationRepository.lookup().populateOverrideConfiguration(repositoryKey, file); - } else { - ConfigurationRepository.lookup().removeOverrideConfiguration(file); - } - } - } - } catch (Exception exception) { - exception.printStackTrace(); - } - } - - private Set<Path> watchForChange(String location) throws Exception { - if (location == null || location.trim().length() == 0) { - return Collections.emptySet(); - } - File file = new File(location); - if (!file.exists()) { - return Collections.emptySet(); - } - Path path = file.toPath(); - Set<Path> toReturn = new HashSet<>(); - try (final WatchService watchService = FileSystems.getDefault().newWatchService()) { - watchServiceCollection.put(location, watchService); - path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_CREATE, - StandardWatchEventKinds.ENTRY_DELETE); - for (File dir : ConfigurationUtils.getAllFiles(file, true, true)) { - dir.toPath().register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, - StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE); - } - while (true) { - final WatchKey wk = watchService.take(); - Thread.sleep(ConfigurationRepository.lookup() - .getConfigurationFor(Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE) - .getLong("event.fetch.delay")); - for (WatchEvent<?> event : wk.pollEvents()) { - Object context = event.context(); - if (context instanceof Path) { - File newFile = new File(((Path) wk.watchable()).toFile(), context.toString()); - if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) { - if (newFile.isDirectory()) { - newFile.toPath().register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, - StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE); - continue; - } - } else if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) { - if (newFile.isDirectory()) { - continue; - } - } - toReturn.add(newFile.toPath()); - } - } - if (toReturn.isEmpty()) { - continue; - } - break; - } - } - return toReturn; - } - - private void updateConfigurationValues(String tenant, String namespace, Map map) throws Exception { - MBeanServerConnection mbsc = ManagementFactory.getPlatformMBeanServer(); - ObjectName mbeanName = new ObjectName(Constants.MBEAN_NAME); - ConfigurationManager conf = JMX.newMBeanProxy(mbsc, mbeanName, ConfigurationManager.class, true); - conf.updateConfigurationValues(tenant, namespace, map); - } - - public void shutdown() { - for (WatchService watch : watchServiceCollection.values()) { - try { - watch.close(); - } catch (IOException exception) { - //do nothing - } - } - executor.shutdownNow(); - } - - public void notifyChangesTowards(String tenant, String component, String key, ConfigurationChangeListener myself) - throws Exception { - List<NotificationData> notificationList = store.get(tenant + Constants.KEY_ELEMENTS_DELIMETER + component); - if (notificationList == null) { - notificationList = Collections.synchronizedList(new ArrayList<>()); - store.put(tenant + Constants.KEY_ELEMENTS_DELIMETER + component, notificationList); - executor.scheduleWithFixedDelay( - () -> triggerScanning(tenant + Constants.KEY_ELEMENTS_DELIMETER + component), 1, 30000, - TimeUnit.MILLISECONDS); - } - notificationList.add(new NotificationData(tenant, component, key, myself)); - } - - private void triggerScanning(String key) { - if (store.get(key) != null) { - notificationExecutor.submit(() -> scanForChanges(key)); - } else { - throw new IllegalArgumentException("Notification service for " + key + " is suspended."); - } - } - - private void scanForChanges(String key) { - List<NotificationData> list = store.get(key); - if (list != null) { - list.stream().filter(NotificationData::isChanged) - .forEach(notificationData -> notificationExecutor.submit(() -> sendNotification(notificationData))); - } - } - - private void sendNotification(NotificationData notificationData) { - try { - notificationData.dispatchNotification(); - } catch (Exception exception) { - exception.printStackTrace(); - } - } - - public void stopNotificationTowards(String tenant, String component, String key, ConfigurationChangeListener myself) - throws Exception { - List<NotificationData> notificationList = store.get(tenant + Constants.KEY_ELEMENTS_DELIMETER + component); - if (notificationList != null) { - boolean removed = notificationList.remove(new NotificationData(tenant, component, key, myself)); - if (removed && notificationList.isEmpty()) { - store.remove(tenant + Constants.KEY_ELEMENTS_DELIMETER + component); - } - } - - } - - static class NotificationData { - - final String tenant; - - final String namespace; - - final String key; - - final ConfigurationChangeListener myself; - - Object currentValue; - - boolean isArray; - - public NotificationData(String tenant, String component, String key, ConfigurationChangeListener myself) - throws Exception { - this.tenant = tenant; - this.namespace = component; - this.key = key; - this.myself = myself; - if (!ConfigurationRepository.lookup().getConfigurationFor(tenant, component).containsKey(key)) { - throw new RuntimeException("Key[" + key + "] not found."); - } - isArray = ConfigurationUtils.isArray(tenant, component, key, Hint.DEFAULT.value()); - if (isArray) { - currentValue = ConfigurationManager.lookup().getAsStringValues(tenant, component, key); - } else { - currentValue = ConfigurationManager.lookup().getAsString(tenant, component, key); - } - } - - @Override - public int hashCode() { - return Objects.hash(tenant, namespace, key, myself, currentValue, isArray); - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof NotificationData)) { - return false; - } - NotificationData nd = (NotificationData) obj; - return Objects.equals(tenant, nd.tenant) && Objects.equals(namespace, nd.namespace) && Objects.equals(key, - nd.key) && Objects.equals(myself, nd.myself) && Objects.equals(currentValue, nd.currentValue) - // it's either String or List<String> - && isArray == nd.isArray; - } - - public boolean isChanged() { - Object latestValue; - try { - if (isArray) { - latestValue = ConfigurationManager.lookup().getAsStringValues(tenant, namespace, key); - } else { - latestValue = ConfigurationManager.lookup().getAsString(tenant, namespace, key); - } - if (!isArray) { - return !currentValue.equals(latestValue); - } else { - Collection<String> oldCollection = (Collection<String>) currentValue; - Collection<String> newCollection = (Collection<String>) latestValue; - for (String val : oldCollection) { - if (!newCollection.remove(val)) { - return true; - } - } - return !newCollection.isEmpty(); - } - } catch (Exception exception) { - return false; - } - } - - public void dispatchNotification() throws Exception { - Method method = null; - Vector<Object> parameters = null; - try { - Object latestValue; - if (isArray) { - latestValue = ConfigurationManager.lookup().getAsStringValues(tenant, namespace, key); - } else { - latestValue = ConfigurationManager.lookup().getAsString(tenant, namespace, key); - } - Method[] methods = myself.getClass().getDeclaredMethods(); - if (methods != null && methods.length > 0) { - method = methods[0]; - int paramCount = method.getParameterCount(); - parameters = new Vector<>(); - if (paramCount > 4) { - if (tenant.equals(Constants.DEFAULT_TENANT)) { - parameters.add(null); - } else { - parameters.add(tenant); - } - } - if (paramCount > 3) { - if (namespace.equals(Constants.DEFAULT_NAMESPACE)) { - parameters.add(null); - } else { - parameters.add(namespace); - } - } - parameters.add(key); - parameters.add(currentValue); - parameters.add(latestValue); - method.setAccessible(true); - } - } catch (Exception exception) { - exception.printStackTrace(); - } finally { - isArray = ConfigurationUtils.isArray(tenant, namespace, key, Hint.DEFAULT.value()); - if (isArray) { - currentValue = ConfigurationManager.lookup().getAsStringValues(tenant, namespace, key); - } else { - currentValue = ConfigurationManager.lookup().getAsString(tenant, namespace, key); - } - if (method != null && parameters != null) { - method.invoke(myself, parameters.toArray()); - } - } - } - } -} 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 58720fc76d..af5ae04104 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 @@ -34,7 +34,6 @@ 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; public class ConfigurationImpl implements org.onap.config.api.Configuration { @@ -44,8 +43,6 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { private static boolean instantiated = false; - ConfigurationChangeNotifier changeNotifier; - public ConfigurationImpl() throws Exception { if (instantiated || !CliConfigurationImpl.class.isAssignableFrom(this.getClass())) { throw new RuntimeException("Illegal access to configuration."); @@ -126,15 +123,14 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } } instantiated = true; - changeNotifier = new ConfigurationChangeNotifier(moduleConfigStore); } private void populateFinalConfigurationIncrementally(Map<String, AggregateConfiguration> configs) { - if (configs.get(Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMETER + Constants.DB_NAMESPACE) != null) { + if (configs.get(Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER + Constants.DB_NAMESPACE) != null) { ConfigurationRepository.lookup().populateConfiguration( - Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMETER + Constants.DB_NAMESPACE, - configs.remove(Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMETER + Constants.DB_NAMESPACE) + Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER + Constants.DB_NAMESPACE, + configs.remove(Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER + Constants.DB_NAMESPACE) .getFinalConfiguration()); } @@ -189,43 +185,6 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } @Override - public void addConfigurationChangeListener(String tenant, String namespace, String key, - ConfigurationChangeListener myself) { - tenant = ConfigurationRepository.lookup().isValidTenant(tenant) ? tenant.toUpperCase() - : Constants.DEFAULT_TENANT; - namespace = ConfigurationRepository.lookup().isValidNamespace(namespace) ? namespace.toUpperCase() - : Constants.DEFAULT_NAMESPACE; - if (key == null || key.trim().length() == 0) { - throw new IllegalArgumentException(KEY_CANNOT_BE_NULL); - } - if (myself == null) { - throw new IllegalArgumentException("ConfigurationChangeListener instance is null."); - } - try { - changeNotifier.notifyChangesTowards(tenant, namespace, key, myself); - } catch (Exception exception) { - exception.printStackTrace(); - } - } - - @Override - public void removeConfigurationChangeListener(String tenant, String namespace, String key, - ConfigurationChangeListener myself) { - tenant = ConfigurationRepository.lookup().isValidTenant(tenant) ? tenant.toUpperCase() - : Constants.DEFAULT_TENANT; - namespace = ConfigurationRepository.lookup().isValidNamespace(namespace) ? namespace.toUpperCase() - : Constants.DEFAULT_NAMESPACE; - if (key == null || key.trim().length() == 0) { - throw new IllegalArgumentException(KEY_CANNOT_BE_NULL); - } - try { - changeNotifier.stopNotificationTowards(tenant, namespace, key, myself); - } catch (Exception exception) { - exception.printStackTrace(); - } - } - - @Override public <T> Map<String, T> populateMap(String tenantId, String namespace, String key, Class<T> clazz) { if (tenantId == null || tenantId.trim().length() == 0) { tenantId = tenant.get(); @@ -574,16 +533,4 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } return collection; } - - public void shutdown() { - if (changeNotifier != null) { - try { - changeNotifier.shutdown(); - } catch (Exception exception) { - exception.printStackTrace(); - } - } - } - - } 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 276dbe2805..132043d5b5 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 @@ -19,9 +19,7 @@ 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.LinkedHashMap; import java.util.Map; @@ -38,16 +36,7 @@ import org.onap.config.Constants; public final class ConfigurationRepository { - private static final ConfigurationRepository repo; - - private static final Set<String> validCallers = Collections.unmodifiableSet(new HashSet<>( - Arrays.asList(ConfigurationChangeNotifier.NotificationData.class.getName(), - ConfigurationUtils.class.getName(), CliConfigurationImpl.class.getName(), - ConfigurationChangeNotifier.class.getName(), ConfigurationImpl.class.getName()))); - - static { - repo = new ConfigurationRepository(); - } + private static final ConfigurationRepository repo = new ConfigurationRepository(); private final Set<String> tenants = new HashSet<>(); private final Set<String> namespaces = new HashSet<>(); @@ -65,18 +54,12 @@ public final class ConfigurationRepository { }; private ConfigurationRepository() { - if (repo != null) { - throw new RuntimeException("Illegal access to configuration."); - } tenants.add(Constants.DEFAULT_TENANT); namespaces.add(Constants.DEFAULT_NAMESPACE); } public static ConfigurationRepository lookup() { - if (validCallers.contains(Thread.currentThread().getStackTrace()[2].getClassName())) { - return repo; - } - return null; + return repo; } public Set<String> getTenants() { @@ -98,22 +81,22 @@ public final class ConfigurationRepository { public Configuration getConfigurationFor(String tenant, String namespace) throws Exception { ConfigurationHolder config; - String module = tenant + Constants.KEY_ELEMENTS_DELIMETER + namespace; + String module = tenant + Constants.KEY_ELEMENTS_DELIMITER + namespace; config = store.get(module); if (config == null) { config = new ConfigurationHolder(new BasicConfigurationBuilder<>(AgglomerateConfiguration.class)); store.put(module, config); } - return config.getConfiguration(tenant + Constants.KEY_ELEMENTS_DELIMETER + namespace); + return config.getConfiguration(tenant + Constants.KEY_ELEMENTS_DELIMITER + namespace); } public void populateConfiguration(String key, Configuration builder) { store.put(key, new ConfigurationHolder(builder)); - populateTenantsNamespace(key, false); + populateTenantsNamespace(key); } - private void populateTenantsNamespace(String key, boolean sourcedFromDb) { - String[] array = key.split(Constants.KEY_ELEMENTS_DELIMETER); + private void populateTenantsNamespace(String key) { + String[] array = key.split(Constants.KEY_ELEMENTS_DELIMITER); if (!array[1].equalsIgnoreCase(Constants.DB_NAMESPACE)) { tenants.add(array[0]); namespaces.add(array[1]); @@ -127,7 +110,7 @@ public final class ConfigurationRepository { store.put(key, holder); } holder.addOverrideConfiguration(file.getAbsolutePath(), ConfigurationUtils.getConfigurationBuilder(file, true)); - populateTenantsNamespace(key, true); + populateTenantsNamespace(key); } public void refreshOverrideConfigurationFor(String key, int index) { @@ -207,6 +190,7 @@ public final class ConfigurationRepository { } public Configuration getConfiguration(String namespace) throws Exception { + if (config == null) { config = builder.getConfiguration(); lastConfigurationBuildTime = new Timestamp(System.currentTimeMillis()); @@ -216,9 +200,11 @@ public final class ConfigurationRepository { .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/type/ConfigurationUpdate.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/type/ConfigurationUpdate.java deleted file mode 100644 index f14b4ec3a6..0000000000 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/type/ConfigurationUpdate.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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.type; - -public class ConfigurationUpdate extends ConfigurationQuery { - - private String value; - private boolean nodeOverride; - - public ConfigurationUpdate value(String val) { - value = val; - return this; - } - - public ConfigurationUpdate nodeOverride(boolean val) { - nodeOverride = val; - return this; - } - - public String getValue() { - if (value != null && value.split(",").length > 1 && !value.matches("^\\[.*\\]$")) { - return "[" + value + "]"; - } - return value; - } - - public boolean isNodeOverride() { - return nodeOverride; - } - -} diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/resources/config-system.properties b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/resources/config-system.properties index f8222a3e0c..785b0f94cc 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/resources/config-system.properties +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/resources/config-system.properties @@ -1,3 +1,2 @@ config.size.max=100 -config.refresh.interval=30000 -event.fetch.delay=5000
\ No newline at end of file +config.refresh.interval=30000
\ No newline at end of file |