diff options
author | 2021-06-04 17:49:31 +0200 | |
---|---|---|
committer | 2021-06-09 17:48:27 +0000 | |
commit | 3fdd800133ad5f07aadc90f9731cca28e96896d9 (patch) | |
tree | c35caa9fbd5b529fd4e1e12fbf8708eb1e6d2830 /openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src | |
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/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src')
-rw-r--r-- | openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraConfigurationManager.java (renamed from openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/ConfigurationManager.java) | 91 | ||||
-rw-r--r-- | openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraUtils.java | 26 | ||||
-rw-r--r-- | openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/CassandraConfigurationManagerTest.java | 65 | ||||
-rw-r--r-- | openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/ConfigurationManagerTest.java | 96 |
4 files changed, 104 insertions, 174 deletions
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); - } - } - } -} |