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.openecomp.core.utilities.file.FileUtils;
24 import org.openecomp.sdc.logging.api.Logger;
25 import org.openecomp.sdc.logging.api.LoggerFactory;
26 import org.openecomp.sdc.tosca.services.YamlUtil;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.ArrayList;
32 import java.util.LinkedHashMap;
33 import java.util.List;
35 import java.util.Objects;
36 import java.util.function.Function;
39 * The type Configuration manager.
41 public class ConfigurationManager {
43 static final String CONFIGURATION_YAML_FILE = "configuration.yaml";
45 private static final String CASSANDRA_KEY = "cassandraConfig";
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_PORT = "cassandra.port";
53 private static final String CASSANDRA_SSL = "cassandra.ssl";
54 private static final String CASSANDRA_TRUSTSTORE = "cassandra.Truststore";
55 private static final String CASSANDRA_TRUSTSTORE_PASSWORD = "cassandra.TruststorePassword";
56 private static final String CASSANDRA_HOSTS_KEY = "cassandraHosts";
57 private static final String CASSANDRA_PORT_KEY = "port";
58 private static final String CASSANDRA_USERNAME_KEY = "username";
59 private static final String CASSANDRA_PASSWORD_KEY = "password";
60 private static final String CASSANDRA_AUTHENTICATE_KEY = "authenticate";
61 private static final String CASSANDRA_SSL_KEY = "ssl";
62 private static final String CASSANDRA_TRUSTSTORE_PATH_KEY = "truststorePath";
63 private static final String CASSANDRA_TRUSTSTORE_PASSWORD_KEY = "truststorePassword";
64 private static final String CONSISTENCY_LEVEL = "cassandra.consistencyLevel";
65 private static final String CONSISTENCY_LEVEL_KEY = "consistencyLevel";
66 private static final String LOCAL_DATA_CENTER_KEY = "localDataCenter";
67 private static final String LOCAL_DATA_CENTER = "cassandra.localDataCenter";
68 private static ConfigurationManager instance = null;
69 private final LinkedHashMap<String, Object> cassandraConfiguration;
71 private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
74 private ConfigurationManager() {
76 String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE);
78 Function<InputStream, Map<String, LinkedHashMap<String, Object>>> reader = (is) -> {
79 YamlUtil yamlUtil = new YamlUtil();
80 return yamlUtil.yamlToMap(is);
85 Map<String, LinkedHashMap<String, Object>> configurationMap = configurationYamlFile != null
86 ? readFromFile(configurationYamlFile, reader) // load from file
87 : FileUtils.readViaInputStream(CONFIGURATION_YAML_FILE, reader); // or from resource
88 cassandraConfiguration = configurationMap.get(CASSANDRA_KEY);
90 } catch (IOException e) {
91 throw new RuntimeException("Failed to read configuration", e);
98 * @return the instance
100 public static ConfigurationManager getInstance() {
101 if (Objects.isNull(instance)) {
102 instance = new ConfigurationManager();
108 * Get addresses string [ ].
110 * @return the string [ ]
112 public String[] getAddresses() {
114 String addresses = System.getProperty(CASSANDRA_ADDRESSES);
115 if (Objects.isNull(addresses)) {
116 return addresses.split(",");
118 List lsAddresses = (ArrayList) cassandraConfiguration.get(CASSANDRA_HOSTS_KEY);
119 String[] addressesArray;
120 addressesArray = (String[]) lsAddresses.toArray(new String[lsAddresses.size()]);
121 return addressesArray;
128 * @return the key space
130 public String getKeySpace() {
131 String keySpace = System.getProperty(CASSANDRA_DOX_KEY_STORE);
132 if (Objects.isNull(keySpace)) {
133 //keySpace = cassandraConfiguration.get(cassandraKeySpaceKey);
134 //if (keySpace == null)
135 keySpace = DEFAULT_KEYSPACE_NAME;
143 * @return the username
145 public String getUsername() {
146 String username = System.getProperty(CASSANDRA_USER);
147 if (Objects.isNull(username)) {
148 username = (String) cassandraConfiguration.get(CASSANDRA_USERNAME_KEY);
156 * @return the password
158 public String getPassword() {
159 String password = System.getProperty(CASSANDRA_PASSWORD);
160 if (Objects.isNull(password)) {
161 password = (String) cassandraConfiguration.get(CASSANDRA_PASSWORD_KEY);
167 * Gets truststore path.
169 * @return the truststore path
171 public String getTruststorePath() {
172 String truststorePath = System.getProperty(CASSANDRA_TRUSTSTORE);
173 if (Objects.isNull(truststorePath)) {
174 truststorePath = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PATH_KEY);
176 return truststorePath;
180 * Gets truststore password.
182 * @return the truststore password
184 public String getTruststorePassword() {
185 String truststorePassword = System.getProperty(CASSANDRA_TRUSTSTORE_PASSWORD);
186 if (Objects.isNull(truststorePassword)) {
187 truststorePassword = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PASSWORD_KEY);
189 return truststorePassword;
195 * @return the ssl port
197 public int getSslPort() {
199 String sslPort = System.getProperty(CASSANDRA_PORT);
200 if (Objects.isNull(sslPort)) {
201 sslPort = (String) cassandraConfiguration.get(CASSANDRA_PORT_KEY);
202 if (Objects.isNull(sslPort)) {
206 port = Integer.valueOf(sslPort);
214 * @return the boolean
216 public boolean isSsl() {
217 return getBooleanResult(CASSANDRA_SSL, CASSANDRA_SSL_KEY);
221 * Is authenticate boolean.
223 * @return the boolean
225 public boolean isAuthenticate() {
226 return getBooleanResult(CASSANDRA_AUTHENTICATE, CASSANDRA_AUTHENTICATE_KEY);
229 private Boolean getBooleanResult(String property, String key) {
232 if (System.getProperty(property) == null) {
233 value = String.valueOf(cassandraConfiguration.get(key));
235 value = System.getProperty(property);
238 res = Boolean.valueOf(value);
243 private <T> T readFromFile(String file, Function<InputStream, T> reader) throws IOException {
244 try (InputStream is = new FileInputStream(file)) {
245 return reader.apply(is);
249 public String getConsistencyLevel() {
250 String consistencyLevel = System.getProperty(CONSISTENCY_LEVEL);
251 if (Objects.isNull(consistencyLevel)) {
252 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;