2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.openecomp.core.nosqldb.util;
 
  23 import org.apache.commons.collections4.CollectionUtils;
 
  24 import org.openecomp.core.utilities.file.FileUtils;
 
  25 import org.openecomp.sdc.logging.api.Logger;
 
  26 import org.openecomp.sdc.logging.api.LoggerFactory;
 
  27 import org.openecomp.sdc.tosca.services.YamlUtil;
 
  29 import java.io.FileInputStream;
 
  30 import java.io.IOException;
 
  31 import java.io.InputStream;
 
  32 import java.util.ArrayList;
 
  33 import java.util.LinkedHashMap;
 
  34 import java.util.List;
 
  36 import java.util.Objects;
 
  37 import java.util.function.Function;
 
  40  * The type Configuration manager.
 
  42 public class ConfigurationManager {
 
  44   static final String CONFIGURATION_YAML_FILE = "configuration.yaml";
 
  45   private static final String CASSANDRA = "cassandra";
 
  46   private static final String CASSANDRA_KEY = CASSANDRA + "Config";
 
  47   private static final String DEFAULT_KEYSPACE_NAME = "dox";
 
  48   private static final String CASSANDRA_ADDRESSES = CASSANDRA + ".addresses";
 
  49   private static final String CASSANDRA_DOX_KEY_STORE = CASSANDRA + ".dox.keystore";
 
  50   private static final String CASSANDRA_AUTHENTICATE = CASSANDRA + ".authenticate";
 
  51   private static final String CASSANDRA_USER = CASSANDRA + ".user";
 
  52   private static final String CASSANDRA_PASSWORD = CASSANDRA + ".password";
 
  53   private static final String CASSANDRA_PORT = CASSANDRA + ".port";
 
  54   private static final String CASSANDRA_SSL = CASSANDRA + ".ssl";
 
  55   private static final String CASSANDRA_TRUSTSTORE = CASSANDRA + ".Truststore";
 
  56   private static final String CASSANDRA_TRUSTSTORE_PASSWORD = CASSANDRA + ".TruststorePassword";
 
  57   private static final String CASSANDRA_HOSTS_KEY = CASSANDRA + "Hosts";
 
  58   private static final String CASSANDRA_PORT_KEY = "port";
 
  59   private static final String CASSANDRA_USERNAME_KEY = "username";
 
  60   @SuppressWarnings("squid:S2068")
 
  61   private static final String CASSANDRA_PASSWORD_KEY = "password";          
 
  62   private static final String CASSANDRA_AUTHENTICATE_KEY = "authenticate";
 
  63   private static final String CASSANDRA_SSL_KEY = "ssl";
 
  64   private static final String CASSANDRA_TRUSTSTORE_PATH_KEY = "truststorePath";
 
  65   @SuppressWarnings("squid:S2068")
 
  66   private static final String CASSANDRA_TRUSTSTORE_PASSWORD_KEY = "truststorePassword";   
 
  67   private static final String CONSISTENCY_LEVEL = CASSANDRA + ".consistencyLevel";
 
  68   private static final String CONSISTENCY_LEVEL_KEY = "consistencyLevel";
 
  69   private static final String LOCAL_DATA_CENTER_KEY = "localDataCenter";
 
  70   private static final String LOCAL_DATA_CENTER = CASSANDRA + ".localDataCenter";
 
  71   private static ConfigurationManager instance = null;
 
  72   private final LinkedHashMap<String, Object> cassandraConfiguration;
 
  74   private static final Logger LOG = LoggerFactory.getLogger(ConfigurationManager.class);
 
  77   private ConfigurationManager() {
 
  79     String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE);
 
  81     Function<InputStream, Map<String, LinkedHashMap<String, Object>>> reader = is -> {
 
  82       YamlUtil yamlUtil = new YamlUtil();
 
  83       return yamlUtil.yamlToMap(is);
 
  88       Map<String, LinkedHashMap<String, Object>> configurationMap = configurationYamlFile != null
 
  89           ? readFromFile(configurationYamlFile, reader) // load from file
 
  90           : FileUtils.readViaInputStream(CONFIGURATION_YAML_FILE, reader); // or from resource
 
  91       cassandraConfiguration = configurationMap.get(CASSANDRA_KEY);
 
  93     } catch (IOException e) {
 
  94       throw new RuntimeException("Failed to read configuration", e);
 
 101    * @return the instance
 
 103   public static ConfigurationManager getInstance() {
 
 104     if (Objects.isNull(instance)) {
 
 105       instance = new ConfigurationManager();
 
 111    * Get addresses string [ ].
 
 113    * @return the string [ ]
 
 115   public String[] getAddresses() {
 
 117     String addresses = System.getProperty(CASSANDRA_ADDRESSES);
 
 118     if (Objects.nonNull(addresses)) {
 
 119       return addresses.split(",");
 
 121     List lsAddresses = (ArrayList) cassandraConfiguration.get(CASSANDRA_HOSTS_KEY);
 
 122     if (CollectionUtils.isEmpty(lsAddresses)) {
 
 123       LOG.info("No Cassandra hosts are defined.");
 
 126     String[] addressesArray;
 
 127     addressesArray = (String[]) lsAddresses.toArray(new String[lsAddresses.size()]);
 
 128     return addressesArray;
 
 135    * @return the key space
 
 137   public String getKeySpace() {
 
 138     String keySpace = System.getProperty(CASSANDRA_DOX_KEY_STORE);
 
 139     if (Objects.isNull(keySpace)) {
 
 140       keySpace = DEFAULT_KEYSPACE_NAME;
 
 148    * @return the username
 
 150   public String getUsername() {
 
 151     String username = System.getProperty(CASSANDRA_USER);
 
 152     if (Objects.isNull(username)) {
 
 153       username = (String) cassandraConfiguration.get(CASSANDRA_USERNAME_KEY);
 
 161    * @return the password
 
 163   public String getPassword() {
 
 164     String password = System.getProperty(CASSANDRA_PASSWORD);
 
 165     if (Objects.isNull(password)) {
 
 166       password = (String) cassandraConfiguration.get(CASSANDRA_PASSWORD_KEY);
 
 172    * Gets truststore path.
 
 174    * @return the truststore path
 
 176   public String getTruststorePath() {
 
 177     String truststorePath = System.getProperty(CASSANDRA_TRUSTSTORE);
 
 178     if (Objects.isNull(truststorePath)) {
 
 179       truststorePath = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PATH_KEY);
 
 181     return truststorePath;
 
 185    * Gets truststore password.
 
 187    * @return the truststore password
 
 189   public String getTruststorePassword() {
 
 190     String truststorePassword = System.getProperty(CASSANDRA_TRUSTSTORE_PASSWORD);
 
 191     if (Objects.isNull(truststorePassword)) {
 
 192       truststorePassword = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PASSWORD_KEY);
 
 194     return truststorePassword;
 
 200    * @return the ssl port
 
 202   public int getSslPort() {
 
 204     String sslPort = System.getProperty(CASSANDRA_PORT);
 
 205     if (Objects.isNull(sslPort)) {
 
 206       sslPort = (String) cassandraConfiguration.get(CASSANDRA_PORT_KEY);
 
 207       if (Objects.isNull(sslPort)) {
 
 211     port = Integer.valueOf(sslPort);
 
 219    * @return the boolean
 
 221   public boolean isSsl() {
 
 222     return getBooleanResult(CASSANDRA_SSL, CASSANDRA_SSL_KEY);
 
 226    * Is authenticate boolean.
 
 228    * @return the boolean
 
 230   public boolean isAuthenticate() {
 
 231     return getBooleanResult(CASSANDRA_AUTHENTICATE, CASSANDRA_AUTHENTICATE_KEY);
 
 234   private Boolean getBooleanResult(String property, String key) {
 
 237     if (System.getProperty(property) == null) {
 
 238       value = String.valueOf(cassandraConfiguration.get(key));
 
 240       value = System.getProperty(property);
 
 243     res = Boolean.valueOf(value);
 
 248   private <T> T readFromFile(String file, Function<InputStream, T> reader) throws IOException {
 
 249     try (InputStream is = new FileInputStream(file)) {
 
 250       return reader.apply(is);
 
 254   public String getConsistencyLevel() {
 
 255     String consistencyLevel = System.getProperty(CONSISTENCY_LEVEL);
 
 256     if (Objects.isNull(consistencyLevel)) {
 
 257       consistencyLevel = (String) cassandraConfiguration.get(CONSISTENCY_LEVEL_KEY);
 
 260     if (Objects.isNull(consistencyLevel)) {
 
 261       consistencyLevel = "LOCAL_QUORUM";
 
 263     return consistencyLevel;
 
 266   public String getLocalDataCenter() {
 
 267     String localDataCenter = System.getProperty(LOCAL_DATA_CENTER);
 
 268     if (Objects.isNull(localDataCenter)) {
 
 269       localDataCenter = (String) cassandraConfiguration.get(LOCAL_DATA_CENTER_KEY);
 
 271     return localDataCenter;