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.function.Function;
38 * The type Configuration manager.
40 public class ConfigurationManager {
42 static final String CONFIGURATION_YAML_FILE = "configuration.yaml";
44 private static final String CASSANDRA_KEY = "cassandraConfig";
45 private static final String DEFAULT_KEYSPACE_NAME = "dox";
46 private static final String CASSANDRA_ADDRESSES = "cassandra.addresses";
47 private static final String CASSANDRA_DOX_KEY_STORE = "cassandra.dox.keystore";
48 private static final String CASSANDRA_AUTHENTICATE = "cassandra.authenticate";
49 private static final String CASSANDRA_USER = "cassandra.user";
50 private static final String CASSANDRA_PASSWORD = "cassandra.password";
51 private static final String CASSANDRA_PORT = "cassandra.port";
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 = "cassandraHosts";
56 private static final String CASSANDRA_PORT_KEY = "port";
57 private static final String CASSANDRA_USERNAME_KEY = "username";
58 private static final String CASSANDRA_PASSWORD_KEY = "password";
59 private static final String CASSANDRA_AUTHENTICATE_KEY = "authenticate";
60 private static final String CASSANDRA_SSL_KEY = "ssl";
61 private static final String CASSANDRA_TRUSTSTORE_PATH_KEY = "truststorePath";
62 private static final String CASSANDRA_TRUSTSTORE_PASSWORD_KEY = "truststorePassword";
63 private static ConfigurationManager instance = null;
64 private final LinkedHashMap<String, Object> cassandraConfiguration;
66 private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
69 private ConfigurationManager() {
71 String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE);
73 Function<InputStream, Map<String, LinkedHashMap<String, Object>>> reader = (is) -> {
74 YamlUtil yamlUtil = new YamlUtil();
75 return yamlUtil.yamlToMap(is);
80 Map<String, LinkedHashMap<String, Object>> configurationMap = configurationYamlFile != null
81 ? readFromFile(configurationYamlFile, reader) // load from file
82 : FileUtils.readViaInputStream(CONFIGURATION_YAML_FILE, reader); // or from resource
83 cassandraConfiguration = configurationMap.get(CASSANDRA_KEY);
85 } catch (IOException e) {
86 throw new RuntimeException("Failed to read configuration", e);
93 * @return the instance
95 public static ConfigurationManager getInstance() {
96 if (instance == null) {
97 instance = new ConfigurationManager();
103 * Get addresses string [ ].
105 * @return the string [ ]
107 public String[] getAddresses() {
109 String addresses = System.getProperty(CASSANDRA_ADDRESSES);
110 if (addresses != null) {
111 return addresses.split(",");
113 List lsAddresses = (ArrayList) cassandraConfiguration.get(CASSANDRA_HOSTS_KEY);
114 String[] addressesArray;
115 addressesArray = (String[]) lsAddresses.toArray(new String[lsAddresses.size()]);
116 return addressesArray;
123 * @return the key space
125 public String getKeySpace() {
126 String keySpace = System.getProperty(CASSANDRA_DOX_KEY_STORE);
127 if (keySpace == null) {
128 //keySpace = cassandraConfiguration.get(cassandraKeySpaceKey);
129 //if (keySpace == null)
130 keySpace = DEFAULT_KEYSPACE_NAME;
138 * @return the username
140 public String getUsername() {
141 String username = System.getProperty(CASSANDRA_USER);
142 if (username == null) {
143 username = (String) cassandraConfiguration.get(CASSANDRA_USERNAME_KEY);
151 * @return the password
153 public String getPassword() {
154 String password = System.getProperty(CASSANDRA_PASSWORD);
155 if (password == null) {
156 password = (String) cassandraConfiguration.get(CASSANDRA_PASSWORD_KEY);
162 * Gets truststore path.
164 * @return the truststore path
166 public String getTruststorePath() {
167 String truststorePath = System.getProperty(CASSANDRA_TRUSTSTORE);
168 if (truststorePath == null) {
169 truststorePath = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PATH_KEY);
171 return truststorePath;
175 * Gets truststore password.
177 * @return the truststore password
179 public String getTruststorePassword() {
180 String truststorePassword = System.getProperty(CASSANDRA_TRUSTSTORE_PASSWORD);
181 if (truststorePassword == null) {
182 truststorePassword = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PASSWORD_KEY);
184 return truststorePassword;
190 * @return the ssl port
192 public int getSslPort() {
194 String sslPort = System.getProperty(CASSANDRA_PORT);
195 if (sslPort == null) {
196 sslPort = (String) cassandraConfiguration.get(CASSANDRA_PORT_KEY);
197 if (sslPort == null) {
201 port = Integer.valueOf(sslPort);
209 * @return the boolean
211 public boolean isSsl() {
212 return getBooleanResult(CASSANDRA_SSL, CASSANDRA_SSL_KEY);
216 * Is authenticate boolean.
218 * @return the boolean
220 public boolean isAuthenticate() {
221 return getBooleanResult(CASSANDRA_AUTHENTICATE, CASSANDRA_AUTHENTICATE_KEY);
224 private Boolean getBooleanResult(String property, String key) {
227 if (System.getProperty(property) == null) {
228 value = String.valueOf(cassandraConfiguration.get(key));
230 value = System.getProperty(property);
233 res = Boolean.valueOf(value);
238 private <T> T readFromFile(String file, Function<InputStream, T> reader) throws IOException {
239 try (InputStream is = new FileInputStream(file)) {
240 return reader.apply(is);