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";
46 private static final String CASSANDRA_KEY = "cassandraConfig";
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 = "cassandraHosts";
58 private static final String CASSANDRA_PORT_KEY = "port";
59 private static final String CASSANDRA_USERNAME_KEY = "username";
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 private static final String CASSANDRA_TRUSTSTORE_PASSWORD_KEY = "truststorePassword";
65 private static final String CONSISTENCY_LEVEL = "cassandra.consistencyLevel";
66 private static final String CONSISTENCY_LEVEL_KEY = "consistencyLevel";
67 private static final String LOCAL_DATA_CENTER_KEY = "localDataCenter";
68 private static final String LOCAL_DATA_CENTER = "cassandra.localDataCenter";
69 private static ConfigurationManager instance = null;
70 private final LinkedHashMap<String, Object> cassandraConfiguration;
72 private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
75 private ConfigurationManager() {
77 String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE);
79 Function<InputStream, Map<String, LinkedHashMap<String, Object>>> reader = (is) -> {
80 YamlUtil yamlUtil = new YamlUtil();
81 return yamlUtil.yamlToMap(is);
86 Map<String, LinkedHashMap<String, Object>> configurationMap = configurationYamlFile != null
87 ? readFromFile(configurationYamlFile, reader) // load from file
88 : FileUtils.readViaInputStream(CONFIGURATION_YAML_FILE, reader); // or from resource
89 cassandraConfiguration = configurationMap.get(CASSANDRA_KEY);
91 } catch (IOException e) {
92 throw new RuntimeException("Failed to read configuration", e);
99 * @return the instance
101 public static ConfigurationManager getInstance() {
102 if (Objects.isNull(instance)) {
103 instance = new ConfigurationManager();
109 * Get addresses string [ ].
111 * @return the string [ ]
113 public String[] getAddresses() {
115 String addresses = System.getProperty(CASSANDRA_ADDRESSES);
116 if (Objects.nonNull(addresses)) {
117 return addresses.split(",");
119 List lsAddresses = (ArrayList) cassandraConfiguration.get(CASSANDRA_HOSTS_KEY);
120 if (CollectionUtils.isEmpty(lsAddresses)) {
121 log.info("No Cassandra hosts are defined.");
124 String[] addressesArray;
125 addressesArray = (String[]) lsAddresses.toArray(new String[lsAddresses.size()]);
126 return addressesArray;
133 * @return the key space
135 public String getKeySpace() {
136 String keySpace = System.getProperty(CASSANDRA_DOX_KEY_STORE);
137 if (Objects.isNull(keySpace)) {
138 //keySpace = cassandraConfiguration.get(cassandraKeySpaceKey);
139 //if (keySpace == null)
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;