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.sdc.logging.api.Logger;
24 import org.openecomp.sdc.logging.api.LoggerFactory;
25 import org.openecomp.sdc.tosca.services.YamlUtil;
27 import java.io.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.InputStream;
30 import java.util.ArrayList;
31 import java.util.LinkedHashMap;
32 import java.util.List;
36 * The type Configuration manager.
38 public class ConfigurationManager {
40 private static final String CONFIGURATION_YAML_FILE = "configuration.yaml";
41 private static final String cassandraKey = "cassandraConfig";
42 private static final String DEFAULT_KEYSPACE_NAME = "dox";
43 private static final String CASSANDRA_ADDRESSES = "cassandra.addresses";
44 private static final String CASSANDRA_DOX_KEY_STORE = "cassandra.dox.keystore";
45 private static final String CASSANDRA_AUTHENTICATE = "cassandra.authenticate";
46 private static final String CASSANDRA_USER = "cassandra.user";
47 private static final String CASSANDRA_PASSWORD = "cassandra.password";
48 private static final String CASSANDRA_PORT = "cassandra.port";
49 private static final String CASSANDRA_SSL = "cassandra.ssl";
50 private static final String CASSANDRA_TRUSTSTORE = "cassandra.Truststore";
51 private static final String CASSANDRA_TRUSTSTORE_PASSWORD = "cassandra.TruststorePassword";
52 private static final String cassandraHostsKey = "cassandraHosts";
53 private static final String cassandraPortKey = "port";
54 private static final String cassandraUsernameKey = "username";
55 private static final String cassandraPasswordKey = "password";
56 private static final String cassandraAuthenticateKey = "authenticate";
57 private static final String cassandraSSLKey = "ssl";
58 private static final String cassandraTruststorePathKey = "truststorePath";
59 private static final String cassandraTruststorePasswordKey = "truststorePassword";
60 private static ConfigurationManager instance = null;
61 private final LinkedHashMap<String, Object> cassandraConfiguration;
63 private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
66 private ConfigurationManager() {
67 YamlUtil yamlUtil = new YamlUtil();
68 String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE);
69 InputStream yamlAsString;
70 if (configurationYamlFile != null) {
71 yamlAsString = getConfigFileIs(configurationYamlFile);
74 yamlAsString = yamlUtil.loadYamlFileIs("/" + CONFIGURATION_YAML_FILE);
76 Map<String, LinkedHashMap<String, Object>> configurationMap = yamlUtil.yamlToMap(yamlAsString);
77 cassandraConfiguration = configurationMap.get(cassandraKey);
84 * @return the instance
86 public static ConfigurationManager getInstance() {
87 if (instance == null) {
88 instance = new ConfigurationManager();
94 * Get addresses string [ ].
96 * @return the string [ ]
98 public String[] getAddresses() {
100 String addresses = System.getProperty(CASSANDRA_ADDRESSES);
101 if (addresses != null) {
102 return addresses.split(",");
104 List lsAddresses = (ArrayList) cassandraConfiguration.get(cassandraHostsKey);
105 String[] addressesArray;
106 addressesArray = (String[]) lsAddresses.toArray(new String[lsAddresses.size()]);
107 return addressesArray;
114 * @return the key space
116 public String getKeySpace() {
117 String keySpace = System.getProperty(CASSANDRA_DOX_KEY_STORE);
118 if (keySpace == null) {
119 //keySpace = cassandraConfiguration.get(cassandraKeySpaceKey);
120 //if (keySpace == null)
121 keySpace = DEFAULT_KEYSPACE_NAME;
129 * @return the username
131 public String getUsername() {
132 String username = System.getProperty(CASSANDRA_USER);
133 if (username == null) {
134 username = (String) cassandraConfiguration.get(cassandraUsernameKey);
142 * @return the password
144 public String getPassword() {
145 String password = System.getProperty(CASSANDRA_PASSWORD);
146 if (password == null) {
147 password = (String) cassandraConfiguration.get(cassandraPasswordKey);
153 * Gets truststore path.
155 * @return the truststore path
157 public String getTruststorePath() {
158 String truststorePath = System.getProperty(CASSANDRA_TRUSTSTORE);
159 if (truststorePath == null) {
160 truststorePath = (String) cassandraConfiguration.get(cassandraTruststorePathKey);
162 return truststorePath;
166 * Gets truststore password.
168 * @return the truststore password
170 public String getTruststorePassword() {
171 String truststorePassword = System.getProperty(CASSANDRA_TRUSTSTORE_PASSWORD);
172 if (truststorePassword == null) {
173 truststorePassword = (String) cassandraConfiguration.get(cassandraTruststorePasswordKey);
175 return truststorePassword;
181 * @return the ssl port
183 public int getSslPort() {
185 String sslPort = System.getProperty(CASSANDRA_PORT);
186 if (sslPort == null) {
187 sslPort = (String) cassandraConfiguration.get(cassandraPortKey);
188 if (sslPort == null) {
192 port = Integer.valueOf(sslPort);
200 * @return the boolean
202 public boolean isSsl() {
203 return getBooleanResult(CASSANDRA_SSL, cassandraSSLKey);
207 * Is authenticate boolean.
209 * @return the boolean
211 public boolean isAuthenticate() {
212 return getBooleanResult(CASSANDRA_AUTHENTICATE, cassandraAuthenticateKey);
215 private Boolean getBooleanResult(String property, String key) {
218 if (System.getProperty(property) == null) {
219 value = String.valueOf(cassandraConfiguration.get(key));
221 value = System.getProperty(property);
224 res = Boolean.valueOf(value);
229 private InputStream getConfigFileIs(String file) {
230 InputStream is = null;
232 is = new FileInputStream(file);
233 } catch (FileNotFoundException exception) {
234 log.debug("",exception);