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.onap.sdc.tosca.services.YamlUtil;
25 import org.openecomp.core.utilities.file.FileUtils;
26 import org.openecomp.sdc.logging.api.Logger;
27 import org.openecomp.sdc.logging.api.LoggerFactory;
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 static private final Integer DEFAULT_CASSANDRA_PORT = 9042;
46 private static final String CASSANDRA = "cassandra";
47 private static final String CASSANDRA_KEY = CASSANDRA + "Config";
48 private static final String DEFAULT_KEYSPACE_NAME = "dox";
49 private static final String CASSANDRA_ADDRESSES = CASSANDRA + ".addresses";
50 private static final String CASSANDRA_DOX_KEY_STORE = CASSANDRA + ".dox.keystore";
51 private static final String CASSANDRA_AUTHENTICATE = CASSANDRA + ".authenticate";
52 private static final String CASSANDRA_USER = CASSANDRA + ".user";
53 private static final String CASSANDRA_PASSWORD = CASSANDRA + ".password";
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 = "cassandraPort";
59 private static final String CASSANDRA_USERNAME_KEY = "username";
60 private static final String CASSANDRA_RECONNECT_TIMEOUT = "reconnectTimeout";
61 @SuppressWarnings("squid:S2068")
62 private static final String CASSANDRA_PASSWORD_KEY = "password";
63 private static final String CASSANDRA_AUTHENTICATE_KEY = "authenticate";
64 private static final String CASSANDRA_SSL_KEY = "ssl";
65 private static final String CASSANDRA_TRUSTSTORE_PATH_KEY = "truststorePath";
66 @SuppressWarnings("squid:S2068")
67 private static final String CASSANDRA_TRUSTSTORE_PASSWORD_KEY = "truststorePassword";
68 private static final String CONSISTENCY_LEVEL = CASSANDRA + ".consistencyLevel";
69 private static final String CONSISTENCY_LEVEL_KEY = "consistencyLevel";
70 private static final String LOCAL_DATA_CENTER_KEY = "localDataCenter";
71 private static final String LOCAL_DATA_CENTER = CASSANDRA + ".localDataCenter";
72 private static ConfigurationManager instance = null;
73 private final LinkedHashMap<String, Object> cassandraConfiguration;
75 private static final Logger LOG = LoggerFactory.getLogger(ConfigurationManager.class);
78 private ConfigurationManager() {
80 String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE);
82 Function<InputStream, Map<String, LinkedHashMap<String, Object>>> reader = is -> {
83 YamlUtil yamlUtil = new YamlUtil();
84 return yamlUtil.yamlToMap(is);
89 Map<String, LinkedHashMap<String, Object>> configurationMap = configurationYamlFile != null
90 ? readFromFile(configurationYamlFile, reader) // load from file
91 : FileUtils.readViaInputStream(CONFIGURATION_YAML_FILE, reader); // or from resource
92 cassandraConfiguration = configurationMap.get(CASSANDRA_KEY);
94 } catch (IOException e) {
95 throw new RuntimeException("Failed to read configuration", e);
102 * @return the instance
104 public static ConfigurationManager getInstance() {
105 if (Objects.isNull(instance)) {
106 instance = new ConfigurationManager();
112 * Get addresses string [ ].
114 * @return the string [ ]
116 public String[] getAddresses() {
118 String addresses = System.getProperty(CASSANDRA_ADDRESSES);
119 if (Objects.nonNull(addresses)) {
120 return addresses.split(",");
122 List lsAddresses = (ArrayList) cassandraConfiguration.get(CASSANDRA_HOSTS_KEY);
123 if (CollectionUtils.isEmpty(lsAddresses)) {
124 LOG.info("No Cassandra hosts are defined.");
127 String[] addressesArray;
128 addressesArray = (String[]) lsAddresses.toArray(new String[lsAddresses.size()]);
129 return addressesArray;
134 * Gets Cassandra port.
138 public int getCassandraPort() {
139 Integer cassandraPort = (Integer) cassandraConfiguration.get(CASSANDRA_PORT_KEY);
140 if (Objects.isNull(cassandraPort)) {
141 cassandraPort = DEFAULT_CASSANDRA_PORT;
143 return cassandraPort;
147 * Gets Cassandra reconnection timeout
151 public Long getReconnectTimeout() {
152 Integer cassandraReconnectTimeout = (Integer) cassandraConfiguration.get(CASSANDRA_RECONNECT_TIMEOUT);
153 if (Objects.isNull(cassandraReconnectTimeout)) {
154 LOG.info("No Cassandra reconnect timeout are defined.");
157 return cassandraReconnectTimeout.longValue();
163 * @return the key space
165 public String getKeySpace() {
166 String keySpace = System.getProperty(CASSANDRA_DOX_KEY_STORE);
167 if (Objects.isNull(keySpace)) {
168 keySpace = DEFAULT_KEYSPACE_NAME;
176 * @return the username
178 public String getUsername() {
179 String username = System.getProperty(CASSANDRA_USER);
180 if (Objects.isNull(username)) {
181 username = (String) cassandraConfiguration.get(CASSANDRA_USERNAME_KEY);
189 * @return the password
191 public String getPassword() {
192 String password = System.getProperty(CASSANDRA_PASSWORD);
193 if (Objects.isNull(password)) {
194 password = (String) cassandraConfiguration.get(CASSANDRA_PASSWORD_KEY);
200 * Gets truststore path.
202 * @return the truststore path
204 public String getTruststorePath() {
205 String truststorePath = System.getProperty(CASSANDRA_TRUSTSTORE);
206 if (Objects.isNull(truststorePath)) {
207 truststorePath = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PATH_KEY);
209 return truststorePath;
213 * Gets truststore password.
215 * @return the truststore password
217 public String getTruststorePassword() {
218 String truststorePassword = System.getProperty(CASSANDRA_TRUSTSTORE_PASSWORD);
219 if (Objects.isNull(truststorePassword)) {
220 truststorePassword = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PASSWORD_KEY);
222 return truststorePassword;
228 * @return the boolean
230 public boolean isSsl() {
231 return getBooleanResult(CASSANDRA_SSL, CASSANDRA_SSL_KEY);
235 * Is authenticate boolean.
237 * @return the boolean
239 public boolean isAuthenticate() {
240 return getBooleanResult(CASSANDRA_AUTHENTICATE, CASSANDRA_AUTHENTICATE_KEY);
243 private Boolean getBooleanResult(String property, String key) {
246 if (System.getProperty(property) == null) {
247 value = String.valueOf(cassandraConfiguration.get(key));
249 value = System.getProperty(property);
252 res = Boolean.valueOf(value);
257 private <T> T readFromFile(String file, Function<InputStream, T> reader) throws IOException {
258 try (InputStream is = new FileInputStream(file)) {
259 return reader.apply(is);
263 public String getConsistencyLevel() {
264 String consistencyLevel = System.getProperty(CONSISTENCY_LEVEL);
265 if (Objects.isNull(consistencyLevel)) {
266 consistencyLevel = (String) cassandraConfiguration.get(CONSISTENCY_LEVEL_KEY);
269 if (Objects.isNull(consistencyLevel)) {
270 consistencyLevel = "LOCAL_QUORUM";
272 return consistencyLevel;
275 public String getLocalDataCenter() {
276 String localDataCenter = System.getProperty(LOCAL_DATA_CENTER);
277 if (Objects.isNull(localDataCenter)) {
278 localDataCenter = (String) cassandraConfiguration.get(LOCAL_DATA_CENTER_KEY);
280 return localDataCenter;