From 3fdd800133ad5f07aadc90f9731cca28e96896d9 Mon Sep 17 00:00:00 2001 From: sebdet Date: Fri, 4 Jun 2021 17:49:31 +0200 Subject: 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 Change-Id: I44aeb3a4baf45753c3c43f7b4d202253ae4186eb --- .../pom.xml | 5 + .../sdc/common/CommonConfigurationManager.java | 135 +++++++++++ .../util/CassandraConfigurationManager.java | 229 ++++++++++++++++++ .../core/nosqldb/util/CassandraUtils.java | 26 +- .../core/nosqldb/util/ConfigurationManager.java | 268 --------------------- .../util/CassandraConfigurationManagerTest.java | 65 +++++ .../nosqldb/util/ConfigurationManagerTest.java | 96 -------- .../openecomp-sdc-notification-worker/pom.xml | 5 + .../notification/config/ConfigurationManager.java | 93 ------- .../config/NotificationConfigurationManager.java | 40 +++ .../notification/workers/NotificationWorker.java | 4 +- .../impl/NewNotificationsReaderRestImpl.java | 4 +- .../src/test/resources/configuration.yaml | 5 + .../test/resources/onboarding_configuration.yaml | 5 - 14 files changed, 501 insertions(+), 479 deletions(-) create mode 100644 openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/CommonConfigurationManager.java create mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraConfigurationManager.java delete mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/ConfigurationManager.java create mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/CassandraConfigurationManagerTest.java delete mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/ConfigurationManagerTest.java delete mode 100644 openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/config/ConfigurationManager.java create mode 100644 openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/config/NotificationConfigurationManager.java create mode 100644 openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/configuration.yaml delete mode 100644 openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/onboarding_configuration.yaml (limited to 'openecomp-be') 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 @@ -183,6 +183,11 @@ openecomp-tosca-converter-core ${project.version} + + org.openecomp.sdc.core + openecomp-common-lib + ${project.version} + org.bouncycastle bcpkix-jdk15on 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> 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> 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 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 The type of value to be returned + * @return The value found or the default value if not found + */ + public T getConfigValue(String yamlSection, String name, T defaultValue) { + Map 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 The type of value to be returned + * @return The value found or the default value if not found + */ + public 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/CassandraConfigurationManager.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraConfigurationManager.java new file mode 100644 index 0000000000..d9fcf6add4 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraConfigurationManager.java @@ -0,0 +1,229 @@ +/*- + * ============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.core.nosqldb.util; + +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import org.apache.commons.collections4.CollectionUtils; +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 CassandraConfigurationManager extends CommonConfigurationManager { + + private static final String CASSANDRA = "cassandra"; + private static final String CASSANDRA_KEY = CASSANDRA + "Config"; + 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"; + private static final String CASSANDRA_USER = CASSANDRA + ".user"; + private static final String CASSANDRA_PASSWORD = CASSANDRA + ".password"; + private static final String CASSANDRA_SSL = CASSANDRA + ".ssl"; + private static final String CASSANDRA_TRUSTSTORE = CASSANDRA + ".Truststore"; + private static final String CASSANDRA_TRUSTSTORE_PASSWORD = CASSANDRA + ".TruststorePassword"; + private static final String CASSANDRA_HOSTS_KEY = CASSANDRA + "Hosts"; + private static final String CASSANDRA_PORT_KEY = "cassandraPort"; + private static final String CASSANDRA_USERNAME_KEY = "username"; + private static final String CASSANDRA_RECONNECT_TIMEOUT = "reconnectTimeout"; + @SuppressWarnings("squid:S2068") + private static final String CASSANDRA_PASSWORD_KEY = "password"; + private static final String CASSANDRA_AUTHENTICATE_KEY = "authenticate"; + private static final String CASSANDRA_SSL_KEY = "ssl"; + private static final String CASSANDRA_TRUSTSTORE_PATH_KEY = "truststorePath"; + @SuppressWarnings("squid:S2068") + 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(CassandraConfigurationManager.class); + + private static CassandraConfigurationManager singletonInstance; + + private CassandraConfigurationManager() { + super(CASSANDRA_KEY); + } + + public static synchronized CassandraConfigurationManager getInstance() { + if (singletonInstance == null) { + singletonInstance = new CassandraConfigurationManager(); + } + return singletonInstance; + } + + /** + * Get addresses string [ ]. + * + * @return the string [ ] + */ + public String[] getAddresses() { + String addresses = System.getProperty(CASSANDRA_ADDRESSES); + if (Objects.nonNull(addresses)) { + return addresses.split(","); + } + List lsAddresses = this.getConfigValue(CASSANDRA_HOSTS_KEY, Collections.emptyList()); + if (CollectionUtils.isEmpty(lsAddresses)) { + LOG.info("No Cassandra hosts are defined."); + } + String[] addressesArray; + addressesArray = lsAddresses.toArray(new String[lsAddresses.size()]); + return addressesArray; + } + + /** + * Gets Cassandra port. + * + * @return the port + */ + public int getCassandraPort() { + Integer cassandraPort = this.getConfigValue(CASSANDRA_PORT_KEY, null); + if (Objects.isNull(cassandraPort)) { + cassandraPort = DEFAULT_CASSANDRA_PORT; + } + return cassandraPort; + } + + /** + * Gets Cassandra reconnection timeout + * + * @return + */ + public Long getReconnectTimeout() { + Integer cassandraReconnectTimeout = this.getConfigValue(CASSANDRA_RECONNECT_TIMEOUT, null); + if (Objects.isNull(cassandraReconnectTimeout)) { + LOG.info("No Cassandra reconnect timeout are defined."); + return null; + } + return cassandraReconnectTimeout.longValue(); + } + + /** + * Gets key space. + * + * @return the key space + */ + public String getKeySpace() { + String keySpace = System.getProperty(CASSANDRA_DOX_KEY_STORE); + if (Objects.isNull(keySpace)) { + keySpace = DEFAULT_KEYSPACE_NAME; + } + return keySpace; + } + + /** + * Gets username. + * + * @return the username + */ + public String getUsername() { + String username = System.getProperty(CASSANDRA_USER); + if (Objects.isNull(username)) { + username = this.getConfigValue(CASSANDRA_USERNAME_KEY, null); + } + return username; + } + + /** + * Gets password. + * + * @return the password + */ + public String getPassword() { + String password = System.getProperty(CASSANDRA_PASSWORD); + if (Objects.isNull(password)) { + password = this.getConfigValue(CASSANDRA_PASSWORD_KEY, null); + } + return password; + } + + /** + * Gets truststore path. + * + * @return the truststore path + */ + public String getTruststorePath() { + String truststorePath = System.getProperty(CASSANDRA_TRUSTSTORE); + if (Objects.isNull(truststorePath)) { + truststorePath = this.getConfigValue(CASSANDRA_TRUSTSTORE_PATH_KEY, null); + } + return truststorePath; + } + + /** + * Gets truststore password. + * + * @return the truststore password + */ + public String getTruststorePassword() { + String truststorePassword = System.getProperty(CASSANDRA_TRUSTSTORE_PASSWORD); + if (Objects.isNull(truststorePassword)) { + truststorePassword = this.getConfigValue(CASSANDRA_TRUSTSTORE_PASSWORD_KEY, null); + } + return truststorePassword; + } + + /** + * Is ssl boolean. + * + * @return the boolean + */ + public boolean isSsl() { + return getBooleanResult(CASSANDRA_SSL, CASSANDRA_SSL_KEY); + } + + /** + * Is authenticate boolean. + * + * @return the boolean + */ + public boolean isAuthenticate() { + return getBooleanResult(CASSANDRA_AUTHENTICATE, CASSANDRA_AUTHENTICATE_KEY); + } + + private Boolean getBooleanResult(String property, String key) { + 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 = this.getConfigValue(CONSISTENCY_LEVEL_KEY, null); + } + if (Objects.isNull(consistencyLevel)) { + consistencyLevel = "LOCAL_QUORUM"; + } + return consistencyLevel; + } + + public String getLocalDataCenter() { + String localDataCenter = System.getProperty(LOCAL_DATA_CENTER); + if (Objects.isNull(localDataCenter)) { + 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 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/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/ConfigurationManager.java deleted file mode 100644 index 666901edfe..0000000000 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/ConfigurationManager.java +++ /dev/null @@ -1,268 +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.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.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.logging.api.Logger; -import org.openecomp.sdc.logging.api.LoggerFactory; - -/** - * The type Configuration manager. - */ -public class ConfigurationManager { - - 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"; - private static final String CASSANDRA_USER = CASSANDRA + ".user"; - private static final String CASSANDRA_PASSWORD = CASSANDRA + ".password"; - private static final String CASSANDRA_SSL = CASSANDRA + ".ssl"; - private static final String CASSANDRA_TRUSTSTORE = CASSANDRA + ".Truststore"; - private static final String CASSANDRA_TRUSTSTORE_PASSWORD = CASSANDRA + ".TruststorePassword"; - private static final String CASSANDRA_HOSTS_KEY = CASSANDRA + "Hosts"; - private static final String CASSANDRA_PORT_KEY = "cassandraPort"; - private static final String CASSANDRA_USERNAME_KEY = "username"; - private static final String CASSANDRA_RECONNECT_TIMEOUT = "reconnectTimeout"; - @SuppressWarnings("squid:S2068") - private static final String CASSANDRA_PASSWORD_KEY = "password"; - private static final String CASSANDRA_AUTHENTICATE_KEY = "authenticate"; - private static final String CASSANDRA_SSL_KEY = "ssl"; - private static final String CASSANDRA_TRUSTSTORE_PATH_KEY = "truststorePath"; - @SuppressWarnings("squid:S2068") - 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 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 cassandraConfiguration; - - private ConfigurationManager() { - String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE); - Function>> reader = is -> { - YamlUtil yamlUtil = new YamlUtil(); - return yamlUtil.yamlToMap(is); - }; - try { - Map> configurationMap = configurationYamlFile != null - ? readFromFile(configurationYamlFile, reader) // load from file - - : 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); - } - } - - /** - * Gets instance. - * - * @return the instance - */ - public static ConfigurationManager getInstance() { - if (Objects.isNull(instance)) { - instance = new ConfigurationManager(); - } - return instance; - } - - /** - * Get addresses string [ ]. - * - * @return the string [ ] - */ - public String[] getAddresses() { - String addresses = System.getProperty(CASSANDRA_ADDRESSES); - if (Objects.nonNull(addresses)) { - return addresses.split(","); - } - List lsAddresses = (ArrayList) cassandraConfiguration.get(CASSANDRA_HOSTS_KEY); - if (CollectionUtils.isEmpty(lsAddresses)) { - LOG.info("No Cassandra hosts are defined."); - } - String[] addressesArray; - addressesArray = (String[]) lsAddresses.toArray(new String[lsAddresses.size()]); - return addressesArray; - } - - /** - * Gets Cassandra port. - * - * @return the port - */ - public int getCassandraPort() { - Integer cassandraPort = (Integer) cassandraConfiguration.get(CASSANDRA_PORT_KEY); - if (Objects.isNull(cassandraPort)) { - cassandraPort = DEFAULT_CASSANDRA_PORT; - } - return cassandraPort; - } - - /** - * Gets Cassandra reconnection timeout - * - * @return - */ - public Long getReconnectTimeout() { - Integer cassandraReconnectTimeout = (Integer) cassandraConfiguration.get(CASSANDRA_RECONNECT_TIMEOUT); - if (Objects.isNull(cassandraReconnectTimeout)) { - LOG.info("No Cassandra reconnect timeout are defined."); - return null; - } - return cassandraReconnectTimeout.longValue(); - } - - /** - * Gets key space. - * - * @return the key space - */ - public String getKeySpace() { - String keySpace = System.getProperty(CASSANDRA_DOX_KEY_STORE); - if (Objects.isNull(keySpace)) { - keySpace = DEFAULT_KEYSPACE_NAME; - } - return keySpace; - } - - /** - * Gets username. - * - * @return the username - */ - public String getUsername() { - String username = System.getProperty(CASSANDRA_USER); - if (Objects.isNull(username)) { - username = (String) cassandraConfiguration.get(CASSANDRA_USERNAME_KEY); - } - return username; - } - - /** - * Gets password. - * - * @return the password - */ - public String getPassword() { - String password = System.getProperty(CASSANDRA_PASSWORD); - if (Objects.isNull(password)) { - password = (String) cassandraConfiguration.get(CASSANDRA_PASSWORD_KEY); - } - return password; - } - - /** - * Gets truststore path. - * - * @return the truststore path - */ - public String getTruststorePath() { - String truststorePath = System.getProperty(CASSANDRA_TRUSTSTORE); - if (Objects.isNull(truststorePath)) { - truststorePath = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PATH_KEY); - } - return truststorePath; - } - - /** - * Gets truststore password. - * - * @return the truststore password - */ - public String getTruststorePassword() { - String truststorePassword = System.getProperty(CASSANDRA_TRUSTSTORE_PASSWORD); - if (Objects.isNull(truststorePassword)) { - truststorePassword = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PASSWORD_KEY); - } - return truststorePassword; - } - - /** - * Is ssl boolean. - * - * @return the boolean - */ - public boolean isSsl() { - return getBooleanResult(CASSANDRA_SSL, CASSANDRA_SSL_KEY); - } - - /** - * Is authenticate boolean. - * - * @return the boolean - */ - public boolean isAuthenticate() { - return getBooleanResult(CASSANDRA_AUTHENTICATE, CASSANDRA_AUTHENTICATE_KEY); - } - - 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 readFromFile(String file, Function reader) throws IOException { - try (InputStream is = new FileInputStream(file)) { - return reader.apply(is); - } - } - - public String getConsistencyLevel() { - String consistencyLevel = System.getProperty(CONSISTENCY_LEVEL); - if (Objects.isNull(consistencyLevel)) { - consistencyLevel = (String) cassandraConfiguration.get(CONSISTENCY_LEVEL_KEY); - } - if (Objects.isNull(consistencyLevel)) { - consistencyLevel = "LOCAL_QUORUM"; - } - return consistencyLevel; - } - - public String getLocalDataCenter() { - String localDataCenter = System.getProperty(LOCAL_DATA_CENTER); - if (Objects.isNull(localDataCenter)) { - localDataCenter = (String) cassandraConfiguration.get(LOCAL_DATA_CENTER_KEY); - } - return localDataCenter; - } -} 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 @@ -13,6 +13,11 @@ + + org.openecomp.sdc.core + openecomp-common-lib + ${project.version} + org.openecomp.sdc.core openecomp-utilities-lib 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 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> 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 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 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/configuration.yaml b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/configuration.yaml new file mode 100644 index 0000000000..cc04531c7c --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/configuration.yaml @@ -0,0 +1,5 @@ +notifications: + pollingIntervalMsec: 3000 + selectionSize: 5 + beHost: localhost + beHttpPort: 8080 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/onboarding_configuration.yaml deleted file mode 100644 index cc04531c7c..0000000000 --- a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/onboarding_configuration.yaml +++ /dev/null @@ -1,5 +0,0 @@ -notifications: - pollingIntervalMsec: 3000 - selectionSize: 5 - beHost: localhost - beHttpPort: 8080 -- cgit 1.2.3-korg