80adeb6e2abd2161d770a45f1e7ae2b2722751b7
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.core.nosqldb.util;
22
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;
27
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;
34 import java.util.Map;
35 import java.util.Objects;
36 import java.util.function.Function;
37
38 /**
39  * The type Configuration manager.
40  */
41 public class ConfigurationManager {
42
43   static final String CONFIGURATION_YAML_FILE = "configuration.yaml";
44
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;
70
71   private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
72
73
74   private ConfigurationManager() {
75
76     String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE);
77
78     Function<InputStream, Map<String, LinkedHashMap<String, Object>>> reader = (is) -> {
79       YamlUtil yamlUtil = new YamlUtil();
80       return yamlUtil.yamlToMap(is);
81     };
82
83     try {
84
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);
89
90     } catch (IOException e) {
91       throw new RuntimeException("Failed to read configuration", e);
92     }
93   }
94
95   /**
96    * Gets instance.
97    *
98    * @return the instance
99    */
100   public static ConfigurationManager getInstance() {
101     if (Objects.isNull(instance)) {
102       instance = new ConfigurationManager();
103     }
104     return instance;
105   }
106
107   /**
108    * Get addresses string [ ].
109    *
110    * @return the string [ ]
111    */
112   public String[] getAddresses() {
113
114     String addresses = System.getProperty(CASSANDRA_ADDRESSES);
115     if (Objects.isNull(addresses)) {
116       return addresses.split(",");
117     }
118     List lsAddresses = (ArrayList) cassandraConfiguration.get(CASSANDRA_HOSTS_KEY);
119     String[] addressesArray;
120     addressesArray = (String[]) lsAddresses.toArray(new String[lsAddresses.size()]);
121     return addressesArray;
122
123   }
124
125   /**
126    * Gets key space.
127    *
128    * @return the key space
129    */
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;
136     }
137     return keySpace;
138   }
139
140   /**
141    * Gets username.
142    *
143    * @return the username
144    */
145   public String getUsername() {
146     String username = System.getProperty(CASSANDRA_USER);
147     if (Objects.isNull(username)) {
148       username = (String) cassandraConfiguration.get(CASSANDRA_USERNAME_KEY);
149     }
150     return username;
151   }
152
153   /**
154    * Gets password.
155    *
156    * @return the password
157    */
158   public String getPassword() {
159     String password = System.getProperty(CASSANDRA_PASSWORD);
160     if (Objects.isNull(password)) {
161       password = (String) cassandraConfiguration.get(CASSANDRA_PASSWORD_KEY);
162     }
163     return password;
164   }
165
166   /**
167    * Gets truststore path.
168    *
169    * @return the truststore path
170    */
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);
175     }
176     return truststorePath;
177   }
178
179   /**
180    * Gets truststore password.
181    *
182    * @return the truststore password
183    */
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);
188     }
189     return truststorePassword;
190   }
191
192   /**
193    * Gets ssl port.
194    *
195    * @return the ssl port
196    */
197   public int getSslPort() {
198     int port;
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)) {
203         sslPort = "0";
204       }
205     }
206     port = Integer.valueOf(sslPort);
207     return port;
208   }
209
210
211   /**
212    * Is ssl boolean.
213    *
214    * @return the boolean
215    */
216   public boolean isSsl() {
217     return getBooleanResult(CASSANDRA_SSL, CASSANDRA_SSL_KEY);
218   }
219
220   /**
221    * Is authenticate boolean.
222    *
223    * @return the boolean
224    */
225   public boolean isAuthenticate() {
226     return getBooleanResult(CASSANDRA_AUTHENTICATE, CASSANDRA_AUTHENTICATE_KEY);
227   }
228
229   private Boolean getBooleanResult(String property, String key) {
230     Boolean res;
231     String value;
232     if (System.getProperty(property) == null) {
233       value = String.valueOf(cassandraConfiguration.get(key));
234     } else {
235       value = System.getProperty(property);
236     }
237
238     res = Boolean.valueOf(value);
239
240     return res;
241   }
242
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);
246     }
247   }
248
249   public String getConsistencyLevel() {
250     String consistencyLevel = System.getProperty(CONSISTENCY_LEVEL);
251     if (Objects.isNull(consistencyLevel)) {
252       consistencyLevel = (String) cassandraConfiguration.get(CONSISTENCY_LEVEL_KEY);
253     }
254
255     if (Objects.isNull(consistencyLevel)) {
256       consistencyLevel = "LOCAL_QUORUM";
257     }
258     return consistencyLevel;
259   }
260
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);
265     }
266     return localDataCenter;
267   }
268 }