5723a6fe61c9600cb67185454921ea56b5f2481e
[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.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
46   private static final String CASSANDRA_KEY = "cassandraConfig";
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 = "cassandraHosts";
58   private static final String CASSANDRA_PORT_KEY = "port";
59   private static final String CASSANDRA_USERNAME_KEY = "username";
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   private static final String CASSANDRA_TRUSTSTORE_PASSWORD_KEY = "truststorePassword";
65   private static final String CONSISTENCY_LEVEL = "cassandra.consistencyLevel";
66   private static final String CONSISTENCY_LEVEL_KEY = "consistencyLevel";
67   private static final String LOCAL_DATA_CENTER_KEY = "localDataCenter";
68   private static final String LOCAL_DATA_CENTER = "cassandra.localDataCenter";
69   private static ConfigurationManager instance = null;
70   private final LinkedHashMap<String, Object> cassandraConfiguration;
71
72   private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
73
74
75   private ConfigurationManager() {
76
77     String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE);
78
79     Function<InputStream, Map<String, LinkedHashMap<String, Object>>> reader = (is) -> {
80       YamlUtil yamlUtil = new YamlUtil();
81       return yamlUtil.yamlToMap(is);
82     };
83
84     try {
85
86       Map<String, LinkedHashMap<String, Object>> configurationMap = configurationYamlFile != null
87           ? readFromFile(configurationYamlFile, reader) // load from file
88           : FileUtils.readViaInputStream(CONFIGURATION_YAML_FILE, reader); // or from resource
89       cassandraConfiguration = configurationMap.get(CASSANDRA_KEY);
90
91     } catch (IOException e) {
92       throw new RuntimeException("Failed to read configuration", e);
93     }
94   }
95
96   /**
97    * Gets instance.
98    *
99    * @return the instance
100    */
101   public static ConfigurationManager getInstance() {
102     if (Objects.isNull(instance)) {
103       instance = new ConfigurationManager();
104     }
105     return instance;
106   }
107
108   /**
109    * Get addresses string [ ].
110    *
111    * @return the string [ ]
112    */
113   public String[] getAddresses() {
114
115     String addresses = System.getProperty(CASSANDRA_ADDRESSES);
116     if (Objects.nonNull(addresses)) {
117       return addresses.split(",");
118     }
119     List lsAddresses = (ArrayList) cassandraConfiguration.get(CASSANDRA_HOSTS_KEY);
120     if (CollectionUtils.isEmpty(lsAddresses)) {
121       log.info("No Cassandra hosts are defined.");
122     }
123
124     String[] addressesArray;
125     addressesArray = (String[]) lsAddresses.toArray(new String[lsAddresses.size()]);
126     return addressesArray;
127
128   }
129
130   /**
131    * Gets key space.
132    *
133    * @return the key space
134    */
135   public String getKeySpace() {
136     String keySpace = System.getProperty(CASSANDRA_DOX_KEY_STORE);
137     if (Objects.isNull(keySpace)) {
138       //keySpace = cassandraConfiguration.get(cassandraKeySpaceKey);
139       //if (keySpace == null)
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 }