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.yaml.YamlUtil;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.InputStream;
28 import java.util.ArrayList;
29 import java.util.LinkedHashMap;
30 import java.util.List;
34 * The type Configuration manager.
36 public class ConfigurationManager {
38 private static final String CONFIGURATION_YAML_FILE = "configuration.yaml";
39 private static final String cassandraKey = "cassandraConfig";
40 private static final String DEFAULT_KEYSPACE_NAME = "dox";
41 private static final String CASSANDRA_ADDRESSES = "cassandra.addresses";
42 private static final String CASSANDRA_DOX_KEY_STORE = "cassandra.dox.keystore";
43 private static final String CASSANDRA_AUTHENTICATE = "cassandra.authenticate";
44 private static final String CASSANDRA_USER = "cassandra.user";
45 private static final String CASSANDRA_PASSWORD = "cassandra.password";
46 private static final String CASSANDRA_PORT = "cassandra.port";
47 private static final String CASSANDRA_SSL = "cassandra.ssl";
48 private static final String CASSANDRA_TRUSTSTORE = "cassandra.Truststore";
49 private static final String CASSANDRA_TRUSTSTORE_PASSWORD = "cassandra.TruststorePassword";
50 private static final String cassandraHostsKey = "cassandraHosts";
51 private static final String cassandraPortKey = "port";
52 private static final String cassandraUsernameKey = "username";
53 private static final String cassandraPasswordKey = "password";
54 private static final String cassandraAuthenticateKey = "authenticate";
55 private static final String cassandraSSLKey = "ssl";
56 private static final String cassandraTruststorePathKey = "truststorePath";
57 private static final String cassandraTruststorePasswordKey = "truststorePassword";
58 private static ConfigurationManager instance = null;
59 private final LinkedHashMap<String, Object> cassandraConfiguration;
62 private ConfigurationManager() {
63 YamlUtil yamlUtil = new YamlUtil();
64 String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE);
65 InputStream yamlAsString;
66 if (configurationYamlFile != null) {
67 yamlAsString = getConfigFileIs(configurationYamlFile);
70 yamlAsString = yamlUtil.loadYamlFileIs("/" + CONFIGURATION_YAML_FILE);
72 Map<String, LinkedHashMap<String, Object>> configurationMap = yamlUtil.yamlToMap(yamlAsString);
73 cassandraConfiguration = configurationMap.get(cassandraKey);
80 * @return the instance
82 public static ConfigurationManager getInstance() {
83 if (instance == null) {
84 instance = new ConfigurationManager();
90 * Get addresses string [ ].
92 * @return the string [ ]
94 public String[] getAddresses() {
96 String addresses = System.getProperty(CASSANDRA_ADDRESSES);
97 if (addresses != null) {
98 return addresses.split(",");
100 List locAddresses = (ArrayList) cassandraConfiguration.get(cassandraHostsKey);
101 String[] addressesArray;
102 addressesArray = (String[]) locAddresses.toArray(new String[locAddresses.size()]);
103 return addressesArray;
110 * @return the key space
112 public String getKeySpace() {
113 String keySpace = System.getProperty(CASSANDRA_DOX_KEY_STORE);
114 if (keySpace == null) {
115 //keySpace = cassandraConfiguration.get(cassandraKeySpaceKey);
116 //if (keySpace == null)
117 keySpace = DEFAULT_KEYSPACE_NAME;
125 * @return the username
127 public String getUsername() {
128 String username = System.getProperty(CASSANDRA_USER);
129 if (username == null) {
130 username = (String) cassandraConfiguration.get(cassandraUsernameKey);
138 * @return the password
140 public String getPassword() {
141 String password = System.getProperty(CASSANDRA_PASSWORD);
142 if (password == null) {
143 password = (String) cassandraConfiguration.get(cassandraPasswordKey);
149 * Gets truststore path.
151 * @return the truststore path
153 public String getTruststorePath() {
154 String truststorePath = System.getProperty(CASSANDRA_TRUSTSTORE);
155 if (truststorePath == null) {
156 truststorePath = (String) cassandraConfiguration.get(cassandraTruststorePathKey);
158 return truststorePath;
162 * Gets truststore password.
164 * @return the truststore password
166 public String getTruststorePassword() {
167 String truststorePassword = System.getProperty(CASSANDRA_TRUSTSTORE_PASSWORD);
168 if (truststorePassword == null) {
169 truststorePassword = (String) cassandraConfiguration.get(cassandraTruststorePasswordKey);
171 return truststorePassword;
177 * @return the ssl port
179 public int getSslPort() {
181 String sslPort = System.getProperty(CASSANDRA_PORT);
182 if (sslPort == null) {
183 sslPort = (String) cassandraConfiguration.get(cassandraPortKey);
184 if (sslPort == null) {
188 port = Integer.valueOf(sslPort);
196 * @return the boolean
198 public boolean isSsl() {
199 return getBooleanResult(CASSANDRA_SSL, cassandraSSLKey);
203 * Is authenticate boolean.
205 * @return the boolean
207 public boolean isAuthenticate() {
208 return getBooleanResult(CASSANDRA_AUTHENTICATE, cassandraAuthenticateKey);
211 private Boolean getBooleanResult(String property, String key) {
214 if (System.getProperty(property) == null) {
215 value = String.valueOf(cassandraConfiguration.get(key));
217 value = System.getProperty(property);
220 res = Boolean.valueOf(value);
225 private InputStream getConfigFileIs(String file) {
226 InputStream is = null;
228 is = new FileInputStream(file);
229 } catch (FileNotFoundException e0) {
230 e0.printStackTrace();