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