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