Make Cassandra port configurable.
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-nosqldb-lib / openecomp-nosqldb-core / src / main / java / org / openecomp / core / nosqldb / util / ConfigurationManager.java
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.apache.commons.collections4.CollectionUtils;
24 import org.onap.sdc.tosca.services.YamlUtil;
25 import org.openecomp.core.utilities.file.FileUtils;
26 import org.openecomp.sdc.logging.api.Logger;
27 import org.openecomp.sdc.logging.api.LoggerFactory;
28
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;
35 import java.util.Map;
36 import java.util.Objects;
37 import java.util.function.Function;
38
39 /**
40  * The type Configuration manager.
41  */
42 public class ConfigurationManager {
43
44   static final String CONFIGURATION_YAML_FILE = "configuration.yaml";
45   static private final Integer DEFAULT_CASSANDRA_PORT = 9042;
46   private static final String CASSANDRA = "cassandra";
47   private static final String CASSANDRA_KEY = CASSANDRA + "Config";
48   private static final String DEFAULT_KEYSPACE_NAME = "dox";
49   private static final String CASSANDRA_ADDRESSES = CASSANDRA + ".addresses";
50   private static final String CASSANDRA_DOX_KEY_STORE = CASSANDRA + ".dox.keystore";
51   private static final String CASSANDRA_AUTHENTICATE = CASSANDRA + ".authenticate";
52   private static final String CASSANDRA_USER = CASSANDRA + ".user";
53   private static final String CASSANDRA_PASSWORD = CASSANDRA + ".password";
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 = CASSANDRA + "Hosts";
58   private static final String CASSANDRA_PORT_KEY = "cassandraPort";
59   private static final String CASSANDRA_USERNAME_KEY = "username";
60   private static final String CASSANDRA_RECONNECT_TIMEOUT = "reconnectTimeout";
61   @SuppressWarnings("squid:S2068")
62   private static final String CASSANDRA_PASSWORD_KEY = "password";          
63   private static final String CASSANDRA_AUTHENTICATE_KEY = "authenticate";
64   private static final String CASSANDRA_SSL_KEY = "ssl";
65   private static final String CASSANDRA_TRUSTSTORE_PATH_KEY = "truststorePath";
66   @SuppressWarnings("squid:S2068")
67   private static final String CASSANDRA_TRUSTSTORE_PASSWORD_KEY = "truststorePassword";   
68   private static final String CONSISTENCY_LEVEL = CASSANDRA + ".consistencyLevel";
69   private static final String CONSISTENCY_LEVEL_KEY = "consistencyLevel";
70   private static final String LOCAL_DATA_CENTER_KEY = "localDataCenter";
71   private static final String LOCAL_DATA_CENTER = CASSANDRA + ".localDataCenter";
72   private static ConfigurationManager instance = null;
73   private final LinkedHashMap<String, Object> cassandraConfiguration;
74
75   private static final Logger LOG = LoggerFactory.getLogger(ConfigurationManager.class);
76
77
78   private ConfigurationManager() {
79
80     String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE);
81
82     Function<InputStream, Map<String, LinkedHashMap<String, Object>>> reader = is -> {
83       YamlUtil yamlUtil = new YamlUtil();
84       return yamlUtil.yamlToMap(is);
85     };
86
87     try {
88
89       Map<String, LinkedHashMap<String, Object>> configurationMap = configurationYamlFile != null
90           ? readFromFile(configurationYamlFile, reader) // load from file
91           : FileUtils.readViaInputStream(CONFIGURATION_YAML_FILE, reader); // or from resource
92       cassandraConfiguration = configurationMap.get(CASSANDRA_KEY);
93
94     } catch (IOException e) {
95       throw new RuntimeException("Failed to read configuration", e);
96     }
97   }
98
99   /**
100    * Gets instance.
101    *
102    * @return the instance
103    */
104   public static ConfigurationManager getInstance() {
105     if (Objects.isNull(instance)) {
106       instance = new ConfigurationManager();
107     }
108     return instance;
109   }
110
111   /**
112    * Get addresses string [ ].
113    *
114    * @return the string [ ]
115    */
116   public String[] getAddresses() {
117
118     String addresses = System.getProperty(CASSANDRA_ADDRESSES);
119     if (Objects.nonNull(addresses)) {
120       return addresses.split(",");
121     }
122     List lsAddresses = (ArrayList) cassandraConfiguration.get(CASSANDRA_HOSTS_KEY);
123     if (CollectionUtils.isEmpty(lsAddresses)) {
124       LOG.info("No Cassandra hosts are defined.");
125     }
126
127     String[] addressesArray;
128     addressesArray = (String[]) lsAddresses.toArray(new String[lsAddresses.size()]);
129     return addressesArray;
130
131   }
132
133   /**
134    * Gets Cassandra port.
135    *
136    * @return the port
137    */
138   public int getCassandraPort() {
139     Integer cassandraPort = (Integer) cassandraConfiguration.get(CASSANDRA_PORT_KEY);
140     if (Objects.isNull(cassandraPort)) {
141         cassandraPort = DEFAULT_CASSANDRA_PORT;
142     }
143     return cassandraPort;
144   }
145
146   /**
147    * Gets Cassandra reconnection timeout
148    *
149    * @return
150    */
151   public Long getReconnectTimeout() {
152     Integer cassandraReconnectTimeout = (Integer) cassandraConfiguration.get(CASSANDRA_RECONNECT_TIMEOUT);
153     if (Objects.isNull(cassandraReconnectTimeout)) {
154       LOG.info("No Cassandra reconnect timeout are defined.");
155       return null;
156     }
157     return cassandraReconnectTimeout.longValue();
158   }
159
160   /**
161    * Gets key space.
162    *
163    * @return the key space
164    */
165   public String getKeySpace() {
166     String keySpace = System.getProperty(CASSANDRA_DOX_KEY_STORE);
167     if (Objects.isNull(keySpace)) {
168       keySpace = DEFAULT_KEYSPACE_NAME;
169     }
170     return keySpace;
171   }
172
173   /**
174    * Gets username.
175    *
176    * @return the username
177    */
178   public String getUsername() {
179     String username = System.getProperty(CASSANDRA_USER);
180     if (Objects.isNull(username)) {
181       username = (String) cassandraConfiguration.get(CASSANDRA_USERNAME_KEY);
182     }
183     return username;
184   }
185
186   /**
187    * Gets password.
188    *
189    * @return the password
190    */
191   public String getPassword() {
192     String password = System.getProperty(CASSANDRA_PASSWORD);
193     if (Objects.isNull(password)) {
194       password = (String) cassandraConfiguration.get(CASSANDRA_PASSWORD_KEY);
195     }
196     return password;
197   }
198
199   /**
200    * Gets truststore path.
201    *
202    * @return the truststore path
203    */
204   public String getTruststorePath() {
205     String truststorePath = System.getProperty(CASSANDRA_TRUSTSTORE);
206     if (Objects.isNull(truststorePath)) {
207       truststorePath = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PATH_KEY);
208     }
209     return truststorePath;
210   }
211
212   /**
213    * Gets truststore password.
214    *
215    * @return the truststore password
216    */
217   public String getTruststorePassword() {
218     String truststorePassword = System.getProperty(CASSANDRA_TRUSTSTORE_PASSWORD);
219     if (Objects.isNull(truststorePassword)) {
220       truststorePassword = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PASSWORD_KEY);
221     }
222     return truststorePassword;
223   }
224
225   /**
226    * Is ssl boolean.
227    *
228    * @return the boolean
229    */
230   public boolean isSsl() {
231     return getBooleanResult(CASSANDRA_SSL, CASSANDRA_SSL_KEY);
232   }
233
234   /**
235    * Is authenticate boolean.
236    *
237    * @return the boolean
238    */
239   public boolean isAuthenticate() {
240     return getBooleanResult(CASSANDRA_AUTHENTICATE, CASSANDRA_AUTHENTICATE_KEY);
241   }
242
243   private Boolean getBooleanResult(String property, String key) {
244     Boolean res;
245     String value;
246     if (System.getProperty(property) == null) {
247       value = String.valueOf(cassandraConfiguration.get(key));
248     } else {
249       value = System.getProperty(property);
250     }
251
252     res = Boolean.valueOf(value);
253
254     return res;
255   }
256
257   private <T> T readFromFile(String file, Function<InputStream, T> reader) throws IOException {
258     try (InputStream is = new FileInputStream(file)) {
259       return reader.apply(is);
260     }
261   }
262
263   public String getConsistencyLevel() {
264     String consistencyLevel = System.getProperty(CONSISTENCY_LEVEL);
265     if (Objects.isNull(consistencyLevel)) {
266       consistencyLevel = (String) cassandraConfiguration.get(CONSISTENCY_LEVEL_KEY);
267     }
268
269     if (Objects.isNull(consistencyLevel)) {
270       consistencyLevel = "LOCAL_QUORUM";
271     }
272     return consistencyLevel;
273   }
274
275   public String getLocalDataCenter() {
276     String localDataCenter = System.getProperty(LOCAL_DATA_CENTER);
277     if (Objects.isNull(localDataCenter)) {
278       localDataCenter = (String) cassandraConfiguration.get(LOCAL_DATA_CENTER_KEY);
279     }
280     return localDataCenter;
281   }
282 }