diff options
author | sebdet <sebastien.determe@intl.att.com> | 2021-06-04 17:49:31 +0200 |
---|---|---|
committer | Andr� Schmid <andre.schmid@est.tech> | 2021-06-09 17:48:27 +0000 |
commit | 3fdd800133ad5f07aadc90f9731cca28e96896d9 (patch) | |
tree | c35caa9fbd5b529fd4e1e12fbf8708eb1e6d2830 /openecomp-be | |
parent | c65578b5c01651975d218d81e0f4856746641298 (diff) |
Create a common ConfigurationManager class
Create a common ConfigurationManager class as some classes have the same logic
Issue-ID: SDC-3616
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
Change-Id: I44aeb3a4baf45753c3c43f7b4d202253ae4186eb
Diffstat (limited to 'openecomp-be')
12 files changed, 293 insertions, 271 deletions
diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/pom.xml b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/pom.xml index 5ca3b8951f..bda54b2de8 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/pom.xml +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/pom.xml @@ -184,6 +184,11 @@ <version>${project.version}</version> </dependency> <dependency> + <groupId>org.openecomp.sdc.core</groupId> + <artifactId>openecomp-common-lib</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcpkix-jdk15on</artifactId> <version>${bouncycastle.version}</version> diff --git a/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/CommonConfigurationManager.java b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/CommonConfigurationManager.java new file mode 100644 index 0000000000..46747fb881 --- /dev/null +++ b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/CommonConfigurationManager.java @@ -0,0 +1,135 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017, 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.openecomp.sdc.common; + +import org.onap.sdc.tosca.services.YamlUtil; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.function.BiConsumer; + +/** + * This is a common class that can access the config file given in input to the JVM with the parameter + * -Dconfiguration.yaml=file.yaml. + */ +public class CommonConfigurationManager { + public static final String JVM_PARAM_CONFIGURATION_FILE = "configuration.yaml"; + private static final Logger LOGGER = LoggerFactory.getLogger(CommonConfigurationManager.class); + private static CommonConfigurationManager singletonInstance; + private Map<String, LinkedHashMap<String, Object>> configuration; + private String configFilename; + private String yamlSection; + + protected CommonConfigurationManager() { + initConfiguration(); + } + + protected CommonConfigurationManager(String yamlSection) { + this(); + this.yamlSection = yamlSection; + } + + public static synchronized CommonConfigurationManager getInstance() { + if (singletonInstance == null) { + singletonInstance = new CommonConfigurationManager(); + } + return singletonInstance; + } + + private void initConfiguration() { + YamlUtil yamlUtil = new YamlUtil(); + readConfigurationFromStream(yamlUtil, (filename, stream) -> { + this.configFilename = filename; + if (stream == null) { + LOGGER.warn("Configuration not found: " + filename + ". Using defaults"); + return; + } + Map<String, LinkedHashMap<String, Object>> configurationMap = yamlUtil.yamlToMap(stream); + if (configurationMap == null) { + LOGGER.warn("Configuration cannot be parsed: " + filename + ". Using defaults"); + return; + } else { + this.configuration = configurationMap; + } + }); + } + + private void readConfigurationFromStream(YamlUtil yamlUtil, BiConsumer<String, InputStream> reader) { + String configurationYamlFile = System.getProperty(JVM_PARAM_CONFIGURATION_FILE); + try { + if (configurationYamlFile == null) { + try (InputStream inputStream = yamlUtil.loadYamlFileIs("/" + JVM_PARAM_CONFIGURATION_FILE)) { + reader.accept(JVM_PARAM_CONFIGURATION_FILE, inputStream); + } + } else { + try (InputStream inputStream = new FileInputStream(configurationYamlFile)) { + reader.accept(configurationYamlFile, inputStream); + } + } + } catch (IOException e) { + LOGGER.error("Failed to read configuration " + configurationYamlFile, e); + throw new RuntimeException("Failed to read configuration " + configurationYamlFile, e); + } + } + + /** + * This method can be used to access any yaml section configuration. + * + * @param yamlSection The yaml section that must be accessed + * @param name The configuration name inside that yaml section + * @param defaultValue A default value + * @param <T> The type of value to be returned + * @return The value found or the default value if not found + */ + public <T> T getConfigValue(String yamlSection, String name, T defaultValue) { + Map<String, Object> section = this.configuration.get(yamlSection); + if (section == null) { + LOGGER.error("Section " + yamlSection + " is missing in configuration file '" + configFilename + + "'. Using defaults"); + return defaultValue; + } + Object value = section.get(name); + try { + return value == null ? defaultValue : (T) value; + } catch (ClassCastException e) { + LOGGER.warn( + String.format("Failed to read configuration property '%s' as requested type. Using default '%s'", + name, defaultValue), e); + return defaultValue; + } + } + + /** + * This method can be used to access a specific configuration parameter in the configuration in the + * yamlSection predefined in the constructor. + * + * @param name The name of the config + * @param defaultValue A default value + * @param <T> The type of value to be returned + * @return The value found or the default value if not found + */ + public <T> T getConfigValue(String name, T defaultValue) { + return this.getConfigValue(yamlSection, name, defaultValue); + } +} diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/ConfigurationManager.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraConfigurationManager.java index 666901edfe..d9fcf6add4 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/ConfigurationManager.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraConfigurationManager.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * SDC * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017, 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,31 +19,21 @@ */ package org.openecomp.core.nosqldb.util; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.LinkedHashMap; +import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.Objects; -import java.util.function.Function; import org.apache.commons.collections4.CollectionUtils; -import org.onap.sdc.tosca.services.YamlUtil; -import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.sdc.common.CommonConfigurationManager; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; /** * The type Configuration manager. */ -public class ConfigurationManager { +public class CassandraConfigurationManager extends CommonConfigurationManager { - static final String CONFIGURATION_YAML_FILE = "configuration.yaml"; - static private final Integer DEFAULT_CASSANDRA_PORT = 9042; private static final String CASSANDRA = "cassandra"; private static final String CASSANDRA_KEY = CASSANDRA + "Config"; - private static final String DEFAULT_KEYSPACE_NAME = "dox"; private static final String CASSANDRA_ADDRESSES = CASSANDRA + ".addresses"; private static final String CASSANDRA_DOX_KEY_STORE = CASSANDRA + ".dox.keystore"; private static final String CASSANDRA_AUTHENTICATE = CASSANDRA + ".authenticate"; @@ -65,39 +55,23 @@ public class ConfigurationManager { private static final String CASSANDRA_TRUSTSTORE_PASSWORD_KEY = "truststorePassword"; private static final String CONSISTENCY_LEVEL = CASSANDRA + ".consistencyLevel"; private static final String CONSISTENCY_LEVEL_KEY = "consistencyLevel"; + private static final String DEFAULT_KEYSPACE_NAME = "dox"; + private static final Integer DEFAULT_CASSANDRA_PORT = 9042; private static final String LOCAL_DATA_CENTER_KEY = "localDataCenter"; private static final String LOCAL_DATA_CENTER = CASSANDRA + ".localDataCenter"; - private static final Logger LOG = LoggerFactory.getLogger(ConfigurationManager.class); - private static ConfigurationManager instance = null; - private final LinkedHashMap<String, Object> cassandraConfiguration; + private static final Logger LOG = LoggerFactory.getLogger(CassandraConfigurationManager.class); - private ConfigurationManager() { - String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE); - Function<InputStream, Map<String, LinkedHashMap<String, Object>>> reader = is -> { - YamlUtil yamlUtil = new YamlUtil(); - return yamlUtil.yamlToMap(is); - }; - try { - Map<String, LinkedHashMap<String, Object>> configurationMap = configurationYamlFile != null - ? readFromFile(configurationYamlFile, reader) // load from file + private static CassandraConfigurationManager singletonInstance; - : FileUtils.readViaInputStream(CONFIGURATION_YAML_FILE, reader); // or from resource - cassandraConfiguration = configurationMap.get(CASSANDRA_KEY); - } catch (IOException e) { - throw new RuntimeException("Failed to read configuration", e); - } + private CassandraConfigurationManager() { + super(CASSANDRA_KEY); } - /** - * Gets instance. - * - * @return the instance - */ - public static ConfigurationManager getInstance() { - if (Objects.isNull(instance)) { - instance = new ConfigurationManager(); + public static synchronized CassandraConfigurationManager getInstance() { + if (singletonInstance == null) { + singletonInstance = new CassandraConfigurationManager(); } - return instance; + return singletonInstance; } /** @@ -110,12 +84,12 @@ public class ConfigurationManager { if (Objects.nonNull(addresses)) { return addresses.split(","); } - List lsAddresses = (ArrayList) cassandraConfiguration.get(CASSANDRA_HOSTS_KEY); + List<String> lsAddresses = this.getConfigValue(CASSANDRA_HOSTS_KEY, Collections.emptyList()); if (CollectionUtils.isEmpty(lsAddresses)) { LOG.info("No Cassandra hosts are defined."); } String[] addressesArray; - addressesArray = (String[]) lsAddresses.toArray(new String[lsAddresses.size()]); + addressesArray = lsAddresses.toArray(new String[lsAddresses.size()]); return addressesArray; } @@ -125,7 +99,7 @@ public class ConfigurationManager { * @return the port */ public int getCassandraPort() { - Integer cassandraPort = (Integer) cassandraConfiguration.get(CASSANDRA_PORT_KEY); + Integer cassandraPort = this.getConfigValue(CASSANDRA_PORT_KEY, null); if (Objects.isNull(cassandraPort)) { cassandraPort = DEFAULT_CASSANDRA_PORT; } @@ -138,7 +112,7 @@ public class ConfigurationManager { * @return */ public Long getReconnectTimeout() { - Integer cassandraReconnectTimeout = (Integer) cassandraConfiguration.get(CASSANDRA_RECONNECT_TIMEOUT); + Integer cassandraReconnectTimeout = this.getConfigValue(CASSANDRA_RECONNECT_TIMEOUT, null); if (Objects.isNull(cassandraReconnectTimeout)) { LOG.info("No Cassandra reconnect timeout are defined."); return null; @@ -167,7 +141,7 @@ public class ConfigurationManager { public String getUsername() { String username = System.getProperty(CASSANDRA_USER); if (Objects.isNull(username)) { - username = (String) cassandraConfiguration.get(CASSANDRA_USERNAME_KEY); + username = this.getConfigValue(CASSANDRA_USERNAME_KEY, null); } return username; } @@ -180,7 +154,7 @@ public class ConfigurationManager { public String getPassword() { String password = System.getProperty(CASSANDRA_PASSWORD); if (Objects.isNull(password)) { - password = (String) cassandraConfiguration.get(CASSANDRA_PASSWORD_KEY); + password = this.getConfigValue(CASSANDRA_PASSWORD_KEY, null); } return password; } @@ -193,7 +167,7 @@ public class ConfigurationManager { public String getTruststorePath() { String truststorePath = System.getProperty(CASSANDRA_TRUSTSTORE); if (Objects.isNull(truststorePath)) { - truststorePath = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PATH_KEY); + truststorePath = this.getConfigValue(CASSANDRA_TRUSTSTORE_PATH_KEY, null); } return truststorePath; } @@ -206,7 +180,7 @@ public class ConfigurationManager { public String getTruststorePassword() { String truststorePassword = System.getProperty(CASSANDRA_TRUSTSTORE_PASSWORD); if (Objects.isNull(truststorePassword)) { - truststorePassword = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PASSWORD_KEY); + truststorePassword = this.getConfigValue(CASSANDRA_TRUSTSTORE_PASSWORD_KEY, null); } return truststorePassword; } @@ -230,27 +204,14 @@ public class ConfigurationManager { } private Boolean getBooleanResult(String property, String key) { - Boolean res; - String value; - if (System.getProperty(property) == null) { - value = String.valueOf(cassandraConfiguration.get(key)); - } else { - value = System.getProperty(property); - } - res = Boolean.valueOf(value); - return res; - } - - private <T> T readFromFile(String file, Function<InputStream, T> reader) throws IOException { - try (InputStream is = new FileInputStream(file)) { - return reader.apply(is); - } + String value = System.getProperty(property); + return Boolean.valueOf(value == null ? String.valueOf(this.getConfigValue(key, false)) : value); } public String getConsistencyLevel() { String consistencyLevel = System.getProperty(CONSISTENCY_LEVEL); if (Objects.isNull(consistencyLevel)) { - consistencyLevel = (String) cassandraConfiguration.get(CONSISTENCY_LEVEL_KEY); + consistencyLevel = this.getConfigValue(CONSISTENCY_LEVEL_KEY, null); } if (Objects.isNull(consistencyLevel)) { consistencyLevel = "LOCAL_QUORUM"; @@ -261,7 +222,7 @@ public class ConfigurationManager { public String getLocalDataCenter() { String localDataCenter = System.getProperty(LOCAL_DATA_CENTER); if (Objects.isNull(localDataCenter)) { - localDataCenter = (String) cassandraConfiguration.get(LOCAL_DATA_CENTER_KEY); + localDataCenter = this.getConfigValue(LOCAL_DATA_CENTER_KEY, null); } return localDataCenter; } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraUtils.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraUtils.java index d6840fedd1..c11c5370e6 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraUtils.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraUtils.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * SDC * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017, 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,15 +30,15 @@ public class CassandraUtils { private static Map<String, String> statementMap = new HashMap<>(); public static String[] getAddresses() { - return ConfigurationManager.getInstance().getAddresses(); + return CassandraConfigurationManager.getInstance().getAddresses(); } public static Long getReconnectTimeout() { - return ConfigurationManager.getInstance().getReconnectTimeout(); + return CassandraConfigurationManager.getInstance().getReconnectTimeout(); } public static String getKeySpace() { - return ConfigurationManager.getInstance().getKeySpace(); + return CassandraConfigurationManager.getInstance().getKeySpace(); } /** @@ -55,38 +55,38 @@ public class CassandraUtils { } public static String getUser() { - return ConfigurationManager.getInstance().getUsername(); + return CassandraConfigurationManager.getInstance().getUsername(); } public static String getPassword() { - return ConfigurationManager.getInstance().getPassword(); + return CassandraConfigurationManager.getInstance().getPassword(); } public static String getTruststore() { - return ConfigurationManager.getInstance().getTruststorePath(); + return CassandraConfigurationManager.getInstance().getTruststorePath(); } public static String getTruststorePassword() { - return ConfigurationManager.getInstance().getTruststorePassword(); + return CassandraConfigurationManager.getInstance().getTruststorePassword(); } public static int getCassandraPort() { - return ConfigurationManager.getInstance().getCassandraPort(); + return CassandraConfigurationManager.getInstance().getCassandraPort(); } public static boolean isSsl() { - return ConfigurationManager.getInstance().isSsl(); + return CassandraConfigurationManager.getInstance().isSsl(); } public static boolean isAuthenticate() { - return ConfigurationManager.getInstance().isAuthenticate(); + return CassandraConfigurationManager.getInstance().isAuthenticate(); } public static String getConsistencyLevel() { - return ConfigurationManager.getInstance().getConsistencyLevel(); + return CassandraConfigurationManager.getInstance().getConsistencyLevel(); } public static String getLocalDataCenter() { - return ConfigurationManager.getInstance().getLocalDataCenter(); + return CassandraConfigurationManager.getInstance().getLocalDataCenter(); } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/CassandraConfigurationManagerTest.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/CassandraConfigurationManagerTest.java new file mode 100644 index 0000000000..021f1c715b --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/CassandraConfigurationManagerTest.java @@ -0,0 +1,65 @@ +/* + * Copyright © 2016-2018 European Support Limited + * ================================================================================ + * Modifications Copyright (C) 2021 AT&T. + * ================================================================================ + * 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.openecomp.core.nosqldb.util; + +import java.io.FileNotFoundException; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * @author EVITALIY + * @since 22 Oct 17 + */ +public class CassandraConfigurationManagerTest { + + private static final String NON_EXISTENT = "unexistentfile"; + + @AfterEach + public void resetSystemVar() { + System.clearProperty(CassandraConfigurationManager.JVM_PARAM_CONFIGURATION_FILE); + } + + @Test + public void testGetInstanceSystemProperty() throws Throwable { + System.setProperty(CassandraConfigurationManager.JVM_PARAM_CONFIGURATION_FILE, NON_EXISTENT); + RuntimeException e = Assertions.assertThrows(RuntimeException.class,()-> { + CassandraConfigurationManager.getInstance(); + }); + Assertions.assertTrue(e.getCause() instanceof FileNotFoundException); + Assertions.assertEquals("Failed to read configuration unexistentfile",e.getMessage()); + } + + @Test() + public void testGetInstanceDefault() { + // Do not set the JVM param for config file, by default code will get it from Resource + CassandraConfigurationManager manager = CassandraConfigurationManager.getInstance(); + Assertions.assertArrayEquals(new String[] {"127.0.0.1"}, manager.getAddresses()); + Assertions.assertEquals(9042, manager.getCassandraPort()); + Assertions.assertEquals("dox", manager.getKeySpace()); + Assertions.assertEquals("LOCAL_QUORUM", manager.getConsistencyLevel()); + Assertions.assertEquals(null, manager.getLocalDataCenter()); + Assertions.assertEquals("Aa1234%^!", manager.getPassword()); + Assertions.assertEquals(30000L, manager.getReconnectTimeout().longValue()); + Assertions.assertEquals("/path/path", manager.getTruststorePath()); + Assertions.assertEquals("Aa123456", manager.getTruststorePassword()); + Assertions.assertFalse(manager.isSsl()); + Assertions.assertFalse(manager.isAuthenticate()); + } +} diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/ConfigurationManagerTest.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/ConfigurationManagerTest.java deleted file mode 100644 index d6e7245450..0000000000 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/ConfigurationManagerTest.java +++ /dev/null @@ -1,96 +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.openecomp.core.nosqldb.util; - -import static org.hamcrest.core.StringContains.containsString; -import static org.junit.Assert.assertNotNull; - -import java.io.Closeable; -import java.io.IOException; -import java.lang.reflect.Field; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -/** - * @author EVITALIY - * @since 22 Oct 17 - */ -public class ConfigurationManagerTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - private static final String NON_EXISTENT = "unexistentfile"; - - @Before - public void resetInstance() throws NoSuchFieldException, IllegalAccessException { - Field field = ConfigurationManager.class.getDeclaredField("instance"); - field.setAccessible(true); - field.set(null, null); - } - - @Test - public void testGetInstanceSystemProperty() throws Throwable { - - expectedException.expect(IOException.class); - expectedException.expectMessage(containsString(NON_EXISTENT)); - - try (ConfigurationSystemPropertyUpdater updater = new ConfigurationSystemPropertyUpdater(NON_EXISTENT)) { - ConfigurationManager.getInstance(); - } catch (RuntimeException e) { - Throwable cause = e.getCause(); - throw cause == null ? e : cause; - } - } - - @Test() - public void testGetInstanceDefault() { - - try (ConfigurationSystemPropertyUpdater property = new ConfigurationSystemPropertyUpdater()) { - ConfigurationManager manager = ConfigurationManager.getInstance(); - assertNotNull(manager.getUsername()); - } - } - - - private static class ConfigurationSystemPropertyUpdater implements Closeable { - - private final String oldValue; - - private ConfigurationSystemPropertyUpdater(String value) { - this.oldValue = System.getProperty(ConfigurationManager.CONFIGURATION_YAML_FILE); - System.setProperty(ConfigurationManager.CONFIGURATION_YAML_FILE, value); - } - - private ConfigurationSystemPropertyUpdater() { - this.oldValue = System.getProperty(ConfigurationManager.CONFIGURATION_YAML_FILE); - System.clearProperty(ConfigurationManager.CONFIGURATION_YAML_FILE); - } - - @Override - public void close() { - - if (oldValue == null) { - System.clearProperty(ConfigurationManager.CONFIGURATION_YAML_FILE); - } else { - System.setProperty(ConfigurationManager.CONFIGURATION_YAML_FILE, oldValue); - } - } - } -} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/pom.xml b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/pom.xml index 5842955fb9..4f56264846 100644 --- a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/pom.xml +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/pom.xml @@ -15,6 +15,11 @@ <dependencies> <dependency> <groupId>org.openecomp.sdc.core</groupId> + <artifactId>openecomp-common-lib</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.openecomp.sdc.core</groupId> <artifactId>openecomp-utilities-lib</artifactId> <version>${project.version}</version> </dependency> diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/config/ConfigurationManager.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/config/ConfigurationManager.java deleted file mode 100644 index e597de0f20..0000000000 --- a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/config/ConfigurationManager.java +++ /dev/null @@ -1,93 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.openecomp.sdc.notification.config; - -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.function.BiConsumer; -import org.onap.sdc.tosca.services.YamlUtil; -import org.openecomp.sdc.logging.api.Logger; -import org.openecomp.sdc.logging.api.LoggerFactory; - -public class ConfigurationManager { - - private static final String CONFIGURATION_YAML_FILE = "onboarding_configuration.yaml"; - private static final String NOTIFICATIONS_CONFIG = "notifications"; - private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationManager.class); - private static final ConfigurationManager SINGLETON = new ConfigurationManager(); - private LinkedHashMap<String, Object> notificationsConfiguration; - - private ConfigurationManager() { - initConfiguration(); - } - - public static ConfigurationManager getInstance() { - return SINGLETON; - } - - private void initConfiguration() { - YamlUtil yamlUtil = new YamlUtil(); - readConfigurationFromStream(yamlUtil, (filename, stream) -> { - if (stream == null) { - LOGGER.warn("Configuration not found: " + filename + ". Using defaults"); - return; - } - Map<String, LinkedHashMap<String, Object>> configurationMap = yamlUtil.yamlToMap(stream); - if (configurationMap == null) { - LOGGER.warn("Configuration cannot be parsed: " + filename + ". Using defaults"); - return; - } - notificationsConfiguration = configurationMap.get(NOTIFICATIONS_CONFIG); - if (notificationsConfiguration == null) { - LOGGER.error(NOTIFICATIONS_CONFIG + " is missing in configuration file '" + filename + "'. Using defaults"); - } - }); - } - - private void readConfigurationFromStream(YamlUtil yamlUtil, BiConsumer<String, InputStream> reader) { - String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE); - try { - if (configurationYamlFile == null) { - try (InputStream inputStream = yamlUtil.loadYamlFileIs("/" + CONFIGURATION_YAML_FILE)) { - reader.accept(CONFIGURATION_YAML_FILE, inputStream); - } - } else { - try (InputStream inputStream = new FileInputStream(configurationYamlFile)) { - reader.accept(configurationYamlFile, inputStream); - } - } - } catch (IOException e) { - LOGGER.error("Failed to read configuration", e); - } - } - - public <T> T getConfigValue(String name, T defaultValue) { - Object value = notificationsConfiguration.get(name); - try { - return value == null ? defaultValue : (T) value; - } catch (ClassCastException e) { - LOGGER.warn(String.format("Failed to read configuration property '%s' as requested type. Using default '%s'", name, defaultValue), e); - return defaultValue; - } - } -} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/config/NotificationConfigurationManager.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/config/NotificationConfigurationManager.java new file mode 100644 index 0000000000..d1ef6ef823 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/config/NotificationConfigurationManager.java @@ -0,0 +1,40 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017, 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.openecomp.sdc.notification.config; + +import org.openecomp.sdc.common.CommonConfigurationManager; + +/** + * This class return a config manager dedicated to notification Yaml section. + * @see org.openecomp.sdc.common.CommonConfigurationManager + */ +public class NotificationConfigurationManager extends CommonConfigurationManager { + + private static NotificationConfigurationManager singletonInstance; + private NotificationConfigurationManager() { + super("notifications"); + } + public static synchronized NotificationConfigurationManager getInstance() { + if (singletonInstance == null) { + singletonInstance = new NotificationConfigurationManager(); + } + return singletonInstance; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/NotificationWorker.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/NotificationWorker.java index c69c4ca524..fcd131aa78 100644 --- a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/NotificationWorker.java +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/NotificationWorker.java @@ -28,7 +28,7 @@ import java.util.function.Consumer; import org.apache.commons.collections4.CollectionUtils; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; -import org.openecomp.sdc.notification.config.ConfigurationManager; +import org.openecomp.sdc.notification.config.NotificationConfigurationManager; import org.openecomp.sdc.notification.types.NotificationsStatusDto; public class NotificationWorker { @@ -45,7 +45,7 @@ public class NotificationWorker { private NewNotificationsReader news = null; public NotificationWorker(NewNotificationsReader news) { - ConfigurationManager cm = ConfigurationManager.getInstance(); + NotificationConfigurationManager cm = NotificationConfigurationManager.getInstance(); pollingSleepInterval = cm.getConfigValue(POLLING_INTERVAL, DEFAULT_POLLING_INTERVAL); selectionLimit = cm.getConfigValue(SELECTION_SIZE, DEFAULT_SELECTION_LIMIT); Objects.requireNonNull(news, "NotificationNews object is not initialized."); diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/impl/NewNotificationsReaderRestImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/impl/NewNotificationsReaderRestImpl.java index 7f2390b38a..4fb7bcd32b 100644 --- a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/impl/NewNotificationsReaderRestImpl.java +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/impl/NewNotificationsReaderRestImpl.java @@ -30,7 +30,7 @@ import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; -import org.openecomp.sdc.notification.config.ConfigurationManager; +import org.openecomp.sdc.notification.config.NotificationConfigurationManager; import org.openecomp.sdc.notification.types.NotificationsStatusDto; import org.openecomp.sdc.notification.workers.NewNotificationsReader; @@ -50,7 +50,7 @@ public class NewNotificationsReaderRestImpl implements NewNotificationsReader { private static int bePort; public NewNotificationsReaderRestImpl() { - ConfigurationManager cm = ConfigurationManager.getInstance(); + NotificationConfigurationManager cm = NotificationConfigurationManager.getInstance(); bePort = cm.getConfigValue(BE_PORT, DEFAULT_BE_PORT); beHost = cm.getConfigValue(BE_HOST, DEFAULT_BE_HOST); } diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/onboarding_configuration.yaml b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/configuration.yaml index cc04531c7c..cc04531c7c 100644 --- a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/onboarding_configuration.yaml +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/configuration.yaml |