diff options
Diffstat (limited to 'common')
8 files changed, 81 insertions, 77 deletions
diff --git a/common/openecomp-common-configuration-management/openecomp-configuration-management-core/src/main/java/org/openecomp/config/impl/ConfigurationChangeNotifier.java b/common/openecomp-common-configuration-management/openecomp-configuration-management-core/src/main/java/org/openecomp/config/impl/ConfigurationChangeNotifier.java index 5c3df4a56d..3922720d48 100644 --- a/common/openecomp-common-configuration-management/openecomp-configuration-management-core/src/main/java/org/openecomp/config/impl/ConfigurationChangeNotifier.java +++ b/common/openecomp-common-configuration-management/openecomp-configuration-management-core/src/main/java/org/openecomp/config/impl/ConfigurationChangeNotifier.java @@ -24,10 +24,10 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; 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; @@ -44,15 +44,15 @@ import javax.management.ObjectName; */ public final class ConfigurationChangeNotifier { - private HashMap<String, List<NotificationData>> store = new HashMap<>(); - private ScheduledExecutorService executor = + private final HashMap<String, List<NotificationData>> store = new HashMap<>(); + private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(5, ConfigurationUtils.getThreadFactory()); - private ExecutorService notificationExcecutor = + private final ExecutorService notificationExecutor = Executors.newCachedThreadPool(ConfigurationUtils.getThreadFactory()); - private Map<String, WatchService> watchServiceCollection = + private final Map<String, WatchService> watchServiceCollection = Collections.synchronizedMap(new HashMap<>()); - { + static { if (!Thread.currentThread().getStackTrace()[2].getClassName() .equals(ConfigurationImpl.class.getName())) { throw new RuntimeException("Illegal access."); @@ -66,13 +66,13 @@ public final class ConfigurationChangeNotifier { */ public ConfigurationChangeNotifier(Map<String, AggregateConfiguration> inMemoryConfig) { executor.scheduleWithFixedDelay(() -> this - .pollFilesystemAndUpdateConfigurationIfREquired(inMemoryConfig, + .pollFilesystemAndUpdateConfigurationIfRequired(inMemoryConfig, System.getProperty("config.location"), false), 1, 1, TimeUnit.MILLISECONDS); executor.scheduleWithFixedDelay(() -> this - .pollFilesystemAndUpdateConfigurationIfREquired(inMemoryConfig, + .pollFilesystemAndUpdateConfigurationIfRequired(inMemoryConfig, System.getProperty("tenant.config.location"), true), 1, 1, TimeUnit.MILLISECONDS); executor.scheduleWithFixedDelay(() -> this - .pollFilesystemAndUpdateNodeSpecificConfigurationIfREquired( + .pollFilesystemAndUpdateNodeSpecificConfigurationIfRequired( System.getProperty("node.config.location")), 1, 1, TimeUnit.MILLISECONDS); } @@ -91,13 +91,13 @@ public final class ConfigurationChangeNotifier { } /** - * Poll filesystem and update configuration if r equired. + * Poll filesystem and update configuration if required. * * @param inMemoryConfig the in memory config * @param location the location * @param isTenantLocation the is tenant location */ - public void pollFilesystemAndUpdateConfigurationIfREquired( + public void pollFilesystemAndUpdateConfigurationIfRequired( Map<String, AggregateConfiguration> inMemoryConfig, String location, boolean isTenantLocation) { try { @@ -132,20 +132,19 @@ public final class ConfigurationChangeNotifier { updateConfigurationValues(tenantNamespaceArray[0], tenantNamespaceArray[1], map); } } else { - Iterator<String> repoKeys = inMemoryConfig.keySet().iterator(); - while (repoKeys.hasNext()) { - repositoryKey = repoKeys.next(); + 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()); + ConfigurationUtils.toMap(config.getFinalConfiguration()); Map map = ConfigurationUtils.diff(origConfig, latestConfig); String[] tenantNamespaceArray = - repositoryKey.split(Constants.KEY_ELEMENTS_DELEMETER); + repositoryKey.split(Constants.KEY_ELEMENTS_DELEMETER); updateConfigurationValues(tenantNamespaceArray[0], tenantNamespaceArray[1], - map); + map); } } } @@ -169,26 +168,26 @@ public final class ConfigurationChangeNotifier { } /** - * Poll filesystem and update node specific configuration if r equired. + * Poll filesystem and update node specific configuration if required. * * @param location the location */ - public void pollFilesystemAndUpdateNodeSpecificConfigurationIfREquired(String location) { + public void pollFilesystemAndUpdateNodeSpecificConfigurationIfRequired(String location) { 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)) { - repositoryKey = ConfigurationUtils.getConfigurationRepositoryKey(file); + String repositoryKey = ConfigurationUtils.getConfigurationRepositoryKey(file); ConfigurationRepository.lookup().populateOverrideConfigurtaion(repositoryKey, file); } else { ConfigurationRepository.lookup().removeOverrideConfigurtaion(file); } } } - } catch (Throwable exception) { + } catch (Exception exception) { exception.printStackTrace(); } } @@ -241,7 +240,7 @@ public final class ConfigurationChangeNotifier { private void triggerScanning(String key) { if (store.get(key) != null) { - notificationExcecutor.submit(() -> scanForChanges(key)); + notificationExecutor.submit(() -> scanForChanges(key)); } else { throw new IllegalArgumentException("Notification service for " + key + " is suspended."); } @@ -250,13 +249,9 @@ public final class ConfigurationChangeNotifier { private void scanForChanges(String key) { List<NotificationData> list = store.get(key); if (list != null) { - int size = list.size(); - for (int i = 0; i < size; i++) { - NotificationData notificationData = list.get(i); - if (notificationData.isChanged()) { - notificationExcecutor.submit(() -> sendNotification(notificationData)); - } - } + list.stream() + .filter(NotificationData::isChanged) + .forEach(notificationData -> notificationExecutor.submit(() -> sendNotification(notificationData))); } } @@ -321,24 +316,24 @@ public final class ConfigurationChangeNotifier { /** * The type Notification data. */ - class NotificationData { + static class NotificationData { /** * The Tenant. */ - String tenant; + final String tenant; /** * The Namespace. */ - String namespace; + final String namespace; /** * The Key. */ - String key; + final String key; /** * The Myself. */ - ConfigurationChangeListener myself; + final ConfigurationChangeListener myself; /** * The Current value. */ @@ -381,8 +376,17 @@ public final class ConfigurationChangeNotifier { return false; } NotificationData nd = (NotificationData) obj; - return tenant.equals(nd.tenant) && namespace.equals(nd.namespace) && key.equals(nd.key) - && myself.equals(nd.myself); + 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; + } + + @Override + public int hashCode() { + return Objects.hash(tenant, namespace, key, myself, currentValue, isArray); } /** @@ -424,7 +428,7 @@ public final class ConfigurationChangeNotifier { Method method = null; Vector<Object> parameters = null; try { - Object latestValue = null; + Object latestValue; if (isArray) { latestValue = ConfigurationManager.lookup().getAsStringValues(tenant, namespace, key); } else { diff --git a/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/ConfigSourceLocationTest.java b/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/ConfigSourceLocationTest.java index 7eb591c6d2..731826b53c 100644 --- a/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/ConfigSourceLocationTest.java +++ b/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/ConfigSourceLocationTest.java @@ -33,10 +33,10 @@ public class ConfigSourceLocationTest { Properties props = new Properties(); props.setProperty("maxCachedBufferSize", "1024"); props.setProperty("artifact.maxsize", "1024"); - File f = new File(TestUtil.jsonSchemaLoc+"config.properties"); - OutputStream out = new FileOutputStream( f ); - props.store(out, "Config Property at Conventional Resource"); - out.close(); + File f = new File(TestUtil.jsonSchemaLoc + "config.properties"); + try (OutputStream out = new FileOutputStream(f)) { + props.store(out, "Config Property at Conventional Resource"); + } } @Test diff --git a/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/DynamicConfigurationTest.java b/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/DynamicConfigurationTest.java index 1ca41f8a81..2140e84873 100644 --- a/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/DynamicConfigurationTest.java +++ b/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/DynamicConfigurationTest.java @@ -39,10 +39,10 @@ public class DynamicConfigurationTest { props.setProperty(ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH, "20"); props.setProperty("_config.namespace",NAMESPACE); props.setProperty("_config.mergeStrategy","override"); - File f = new File(TestUtil.jsonSchemaLoc+"config.properties"); - OutputStream out = new FileOutputStream( f ); - props.store(out, "Override Config Property at Conventional Resource"); - out.close(); + File f = new File(TestUtil.jsonSchemaLoc + "config.properties"); + try (OutputStream out = new FileOutputStream(f)) { + props.store(out, "Override Config Property at Conventional Resource"); + } //Verify configuration with Configuration without wait. This should fetch cached value Assert.assertEquals("14" , config.getAsString(NAMESPACE, ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH)); @@ -60,7 +60,7 @@ public class DynamicConfigurationTest { TestUtil.cleanUp(); File f = new File(TestUtil.jsonSchemaLoc+"config.properties"); if(f.exists()) { - boolean isDeleted = f.delete(); + f.delete(); } } } diff --git a/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/NodeSpecificCLITest.java b/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/NodeSpecificCLITest.java index 795780b83b..43729cbaf2 100644 --- a/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/NodeSpecificCLITest.java +++ b/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/NodeSpecificCLITest.java @@ -51,10 +51,10 @@ public class NodeSpecificCLITest { Properties props = new Properties(); props.setProperty(ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH, "50"); props.setProperty("_config.namespace",NAMESPACE); - File f = new File(TestUtil.jsonSchemaLoc+"config.properties"); - OutputStream out = new FileOutputStream( f ); - props.store(out, "Node Config Property"); - out.close(); + File f = new File(TestUtil.jsonSchemaLoc + "config.properties"); + try (OutputStream out = new FileOutputStream(f)) { + props.store(out, "Node Config Property"); + } Thread.sleep(35000); @@ -86,7 +86,7 @@ public class NodeSpecificCLITest { //Verify maxlength on other nodes by deleting node specific configuration if(f.exists()) { - boolean isDeleted = f.delete(); + f.delete(); } Thread.sleep(35000); @@ -103,7 +103,7 @@ public class NodeSpecificCLITest { TestUtil.cleanUp(); File f = new File(TestUtil.jsonSchemaLoc+"config.properties"); if(f.exists()) { - boolean isDeleted = f.delete(); + f.delete(); } } diff --git a/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/NotificationForNodeConfigTest.java b/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/NotificationForNodeConfigTest.java index 3d1579e25e..9d719103df 100644 --- a/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/NotificationForNodeConfigTest.java +++ b/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/NotificationForNodeConfigTest.java @@ -25,7 +25,7 @@ import java.util.Properties; public class NotificationForNodeConfigTest { public final static String NAMESPACE = "NotificationForNodeConfig"; - public String updatedValue = null; + private String updatedValue = null; @Before public void setUp() throws IOException { @@ -42,10 +42,10 @@ public class NotificationForNodeConfigTest { Properties props = new Properties(); props.setProperty(ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH, "30"); props.setProperty("_config.namespace",NAMESPACE); - File f = new File(TestUtil.jsonSchemaLoc+"config.properties"); - OutputStream out = new FileOutputStream( f ); - props.store(out, "Node Config Property"); - out.close(); + File f = new File(TestUtil.jsonSchemaLoc + "config.properties"); + try (OutputStream out = new FileOutputStream(f)) { + props.store(out, "Node Config Property"); + } Thread.sleep(35000); @@ -55,9 +55,9 @@ public class NotificationForNodeConfigTest { config.addConfigurationChangeListener(NAMESPACE, ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH, new NodePropValListener()); props.setProperty(ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH, "80"); - out = new FileOutputStream( f ); - props.store(out, "Updated Node Config Property"); - out.close(); + try (OutputStream out = new FileOutputStream(f)) { + props.store(out, "Updated Node Config Property"); + } Thread.sleep(35000); @@ -79,7 +79,7 @@ public class NotificationForNodeConfigTest { TestUtil.cleanUp(); File f = new File(TestUtil.jsonSchemaLoc+"config.properties"); if(f.exists()) { - boolean isDeleted = f.delete(); + f.delete(); } } } diff --git a/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/NotificationOnPropValTest.java b/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/NotificationOnPropValTest.java index 8a461503ae..7354cd19f7 100644 --- a/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/NotificationOnPropValTest.java +++ b/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/NotificationOnPropValTest.java @@ -25,7 +25,7 @@ public class NotificationOnPropValTest { public final static String NAMESPACE = "NotificationOnPropVal"; - public String updatedValue = null; + private String updatedValue = null; @Before public void setUp() throws IOException { @@ -45,10 +45,10 @@ public class NotificationOnPropValTest { props.setProperty(ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH, "20"); props.setProperty("_config.namespace",NAMESPACE); props.setProperty("_config.mergeStrategy","override"); - File f = new File(TestUtil.jsonSchemaLoc+"config.properties"); - OutputStream out = new FileOutputStream( f ); - props.store(out, "Override Config Property at Conventional Resource"); - out.close(); + File f = new File(TestUtil.jsonSchemaLoc + "config.properties"); + try (OutputStream out = new FileOutputStream(f)) { + props.store(out, "Override Config Property at Conventional Resource"); + } Thread.sleep(35000); @@ -70,7 +70,7 @@ public class NotificationOnPropValTest { TestUtil.cleanUp(); File f = new File(TestUtil.jsonSchemaLoc+"config.properties"); if(f.exists()) { - boolean isDeleted = f.delete(); + f.delete(); } } } diff --git a/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/UnregisterNotificationTest.java b/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/UnregisterNotificationTest.java index a53f3c2d40..e387f13e93 100644 --- a/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/UnregisterNotificationTest.java +++ b/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/UnregisterNotificationTest.java @@ -25,7 +25,7 @@ import java.util.Properties; public class UnregisterNotificationTest { public final static String NAMESPACE = "UnregisterNotification"; - public String updatedValue = null; + private String updatedValue = null; @Before public void setUp() throws IOException { @@ -70,9 +70,9 @@ public class UnregisterNotificationTest { props.setProperty("_config.namespace",NAMESPACE); props.setProperty("_config.mergeStrategy","override"); File f = new File(TestUtil.jsonSchemaLoc+"config.properties"); - OutputStream out = new FileOutputStream( f ); - props.store(out, "Override Config Property at Conventional Resource"); - out.close(); + try (OutputStream out = new FileOutputStream(f)) { + props.store(out, "Override Config Property at Conventional Resource"); + } } private class PropertyListener implements ConfigurationChangeListener { @@ -88,7 +88,7 @@ public class UnregisterNotificationTest { TestUtil.cleanUp(); File f = new File(TestUtil.jsonSchemaLoc+"config.properties"); if(f.exists()) { - boolean isDeleted = f.delete(); + f.delete(); } } } diff --git a/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/ValidateNodeConfigTest.java b/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/ValidateNodeConfigTest.java index 03a6786275..8f7d703468 100644 --- a/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/ValidateNodeConfigTest.java +++ b/common/openecomp-common-configuration-management/openecomp-configuration-management-test/src/test/java/org/openecomp/config/test/ValidateNodeConfigTest.java @@ -40,9 +40,9 @@ public class ValidateNodeConfigTest { props.setProperty(ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH, "56"); props.setProperty("_config.namespace","ValidateNodeConfig"); File f = new File(TestUtil.jsonSchemaLoc+"config.properties"); - OutputStream out = new FileOutputStream( f ); - props.store(out, "Node Config Property"); - out.close(); + try (OutputStream out = new FileOutputStream(f)) { + props.store(out, "Node Config Property"); + } System.out.println(System.getProperty("node.config.location")); @@ -73,7 +73,7 @@ public class ValidateNodeConfigTest { TestUtil.cleanUp(); File f = new File(TestUtil.jsonSchemaLoc+"config.properties"); if(f.exists()) { - boolean isDeleted = f.delete(); + f.delete(); } } |