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=========================================================
20 package org.openecomp.core.nosqldb.util;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.ArrayList;
26 import java.util.LinkedHashMap;
27 import java.util.List;
29 import java.util.Objects;
30 import java.util.function.Function;
31 import org.apache.commons.collections4.CollectionUtils;
32 import org.onap.sdc.tosca.services.YamlUtil;
33 import org.openecomp.core.utilities.file.FileUtils;
34 import org.openecomp.sdc.logging.api.Logger;
35 import org.openecomp.sdc.logging.api.LoggerFactory;
38 * The type Configuration manager.
40 public class ConfigurationManager {
42 static final String CONFIGURATION_YAML_FILE = "configuration.yaml";
43 static private final Integer DEFAULT_CASSANDRA_PORT = 9042;
44 private static final String CASSANDRA = "cassandra";
45 private static final String CASSANDRA_KEY = CASSANDRA + "Config";
46 private static final String DEFAULT_KEYSPACE_NAME = "dox";
47 private static final String CASSANDRA_ADDRESSES = CASSANDRA + ".addresses";
48 private static final String CASSANDRA_DOX_KEY_STORE = CASSANDRA + ".dox.keystore";
49 private static final String CASSANDRA_AUTHENTICATE = CASSANDRA + ".authenticate";
50 private static final String CASSANDRA_USER = CASSANDRA + ".user";
51 private static final String CASSANDRA_PASSWORD = CASSANDRA + ".password";
52 private static final String CASSANDRA_SSL = CASSANDRA + ".ssl";
53 private static final String CASSANDRA_TRUSTSTORE = CASSANDRA + ".Truststore";
54 private static final String CASSANDRA_TRUSTSTORE_PASSWORD = CASSANDRA + ".TruststorePassword";
55 private static final String CASSANDRA_HOSTS_KEY = CASSANDRA + "Hosts";
56 private static final String CASSANDRA_PORT_KEY = "cassandraPort";
57 private static final String CASSANDRA_USERNAME_KEY = "username";
58 private static final String CASSANDRA_RECONNECT_TIMEOUT = "reconnectTimeout";
59 @SuppressWarnings("squid:S2068")
60 private static final String CASSANDRA_PASSWORD_KEY = "password";
61 private static final String CASSANDRA_AUTHENTICATE_KEY = "authenticate";
62 private static final String CASSANDRA_SSL_KEY = "ssl";
63 private static final String CASSANDRA_TRUSTSTORE_PATH_KEY = "truststorePath";
64 @SuppressWarnings("squid:S2068")
65 private static final String CASSANDRA_TRUSTSTORE_PASSWORD_KEY = "truststorePassword";
66 private static final String CONSISTENCY_LEVEL = CASSANDRA + ".consistencyLevel";
67 private static final String CONSISTENCY_LEVEL_KEY = "consistencyLevel";
68 private static final String LOCAL_DATA_CENTER_KEY = "localDataCenter";
69 private static final String LOCAL_DATA_CENTER = CASSANDRA + ".localDataCenter";
70 private static final Logger LOG = LoggerFactory.getLogger(ConfigurationManager.class);
71 private static ConfigurationManager instance = null;
72 private final LinkedHashMap<String, Object> cassandraConfiguration;
74 private ConfigurationManager() {
75 String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE);
76 Function<InputStream, Map<String, LinkedHashMap<String, Object>>> reader = is -> {
77 YamlUtil yamlUtil = new YamlUtil();
78 return yamlUtil.yamlToMap(is);
81 Map<String, LinkedHashMap<String, Object>> configurationMap = configurationYamlFile != null
82 ? readFromFile(configurationYamlFile, reader) // load from file
84 : FileUtils.readViaInputStream(CONFIGURATION_YAML_FILE, reader); // or from resource
85 cassandraConfiguration = configurationMap.get(CASSANDRA_KEY);
86 } catch (IOException e) {
87 throw new RuntimeException("Failed to read configuration", e);
94 * @return the instance
96 public static ConfigurationManager getInstance() {
97 if (Objects.isNull(instance)) {
98 instance = new ConfigurationManager();
104 * Get addresses string [ ].
106 * @return the string [ ]
108 public String[] getAddresses() {
109 String addresses = System.getProperty(CASSANDRA_ADDRESSES);
110 if (Objects.nonNull(addresses)) {
111 return addresses.split(",");
113 List lsAddresses = (ArrayList) cassandraConfiguration.get(CASSANDRA_HOSTS_KEY);
114 if (CollectionUtils.isEmpty(lsAddresses)) {
115 LOG.info("No Cassandra hosts are defined.");
117 String[] addressesArray;
118 addressesArray = (String[]) lsAddresses.toArray(new String[lsAddresses.size()]);
119 return addressesArray;
123 * Gets Cassandra port.
127 public int getCassandraPort() {
128 Integer cassandraPort = (Integer) cassandraConfiguration.get(CASSANDRA_PORT_KEY);
129 if (Objects.isNull(cassandraPort)) {
130 cassandraPort = DEFAULT_CASSANDRA_PORT;
132 return cassandraPort;
136 * Gets Cassandra reconnection timeout
140 public Long getReconnectTimeout() {
141 Integer cassandraReconnectTimeout = (Integer) cassandraConfiguration.get(CASSANDRA_RECONNECT_TIMEOUT);
142 if (Objects.isNull(cassandraReconnectTimeout)) {
143 LOG.info("No Cassandra reconnect timeout are defined.");
146 return cassandraReconnectTimeout.longValue();
152 * @return the key space
154 public String getKeySpace() {
155 String keySpace = System.getProperty(CASSANDRA_DOX_KEY_STORE);
156 if (Objects.isNull(keySpace)) {
157 keySpace = DEFAULT_KEYSPACE_NAME;
165 * @return the username
167 public String getUsername() {
168 String username = System.getProperty(CASSANDRA_USER);
169 if (Objects.isNull(username)) {
170 username = (String) cassandraConfiguration.get(CASSANDRA_USERNAME_KEY);
178 * @return the password
180 public String getPassword() {
181 String password = System.getProperty(CASSANDRA_PASSWORD);
182 if (Objects.isNull(password)) {
183 password = (String) cassandraConfiguration.get(CASSANDRA_PASSWORD_KEY);
189 * Gets truststore path.
191 * @return the truststore path
193 public String getTruststorePath() {
194 String truststorePath = System.getProperty(CASSANDRA_TRUSTSTORE);
195 if (Objects.isNull(truststorePath)) {
196 truststorePath = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PATH_KEY);
198 return truststorePath;
202 * Gets truststore password.
204 * @return the truststore password
206 public String getTruststorePassword() {
207 String truststorePassword = System.getProperty(CASSANDRA_TRUSTSTORE_PASSWORD);
208 if (Objects.isNull(truststorePassword)) {
209 truststorePassword = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PASSWORD_KEY);
211 return truststorePassword;
217 * @return the boolean
219 public boolean isSsl() {
220 return getBooleanResult(CASSANDRA_SSL, CASSANDRA_SSL_KEY);
224 * Is authenticate boolean.
226 * @return the boolean
228 public boolean isAuthenticate() {
229 return getBooleanResult(CASSANDRA_AUTHENTICATE, CASSANDRA_AUTHENTICATE_KEY);
232 private Boolean getBooleanResult(String property, String key) {
235 if (System.getProperty(property) == null) {
236 value = String.valueOf(cassandraConfiguration.get(key));
238 value = System.getProperty(property);
240 res = Boolean.valueOf(value);
244 private <T> T readFromFile(String file, Function<InputStream, T> reader) throws IOException {
245 try (InputStream is = new FileInputStream(file)) {
246 return reader.apply(is);
250 public String getConsistencyLevel() {
251 String consistencyLevel = System.getProperty(CONSISTENCY_LEVEL);
252 if (Objects.isNull(consistencyLevel)) {
253 consistencyLevel = (String) cassandraConfiguration.get(CONSISTENCY_LEVEL_KEY);
255 if (Objects.isNull(consistencyLevel)) {
256 consistencyLevel = "LOCAL_QUORUM";
258 return consistencyLevel;
261 public String getLocalDataCenter() {
262 String localDataCenter = System.getProperty(LOCAL_DATA_CENTER);
263 if (Objects.isNull(localDataCenter)) {
264 localDataCenter = (String) cassandraConfiguration.get(LOCAL_DATA_CENTER_KEY);
266 return localDataCenter;