fd7c1043e848a27b3a7bb882e05eb2c07c2f73f0
[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.nonNull(addresses)) {
116       return addresses.split(",");
117     }
118     List lsAddresses = (ArrayList) cassandraConfiguration.get(CASSANDRA_HOSTS_KEY);
119     if (Objects.isNull(addresses) || addresses.length() == 0) {
120       throw new RuntimeException("Missing Cassandra hose.Cassandra host missing is mandatory.");
121     }
122     String[] addressesArray;
123     addressesArray = (String[]) lsAddresses.toArray(new String[lsAddresses.size()]);
124     return addressesArray;
125
126   }
127
128   /**
129    * Gets key space.
130    *
131    * @return the key space
132    */
133   public String getKeySpace() {
134     String keySpace = System.getProperty(CASSANDRA_DOX_KEY_STORE);
135     if (Objects.isNull(keySpace)) {
136       //keySpace = cassandraConfiguration.get(cassandraKeySpaceKey);
137       //if (keySpace == null)
138       keySpace = DEFAULT_KEYSPACE_NAME;
139     }
140     return keySpace;
141   }
142
143   /**
144    * Gets username.
145    *
146    * @return the username
147    */
148   public String getUsername() {
149     String username = System.getProperty(CASSANDRA_USER);
150     if (Objects.isNull(username)) {
151       username = (String) cassandraConfiguration.get(CASSANDRA_USERNAME_KEY);
152     }
153     return username;
154   }
155
156   /**
157    * Gets password.
158    *
159    * @return the password
160    */
161   public String getPassword() {
162     String password = System.getProperty(CASSANDRA_PASSWORD);
163     if (Objects.isNull(password)) {
164       password = (String) cassandraConfiguration.get(CASSANDRA_PASSWORD_KEY);
165     }
166     return password;
167   }
168
169   /**
170    * Gets truststore path.
171    *
172    * @return the truststore path
173    */
174   public String getTruststorePath() {
175     String truststorePath = System.getProperty(CASSANDRA_TRUSTSTORE);
176     if (Objects.isNull(truststorePath)) {
177       truststorePath = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PATH_KEY);
178     }
179     return truststorePath;
180   }
181
182   /**
183    * Gets truststore password.
184    *
185    * @return the truststore password
186    */
187   public String getTruststorePassword() {
188     String truststorePassword = System.getProperty(CASSANDRA_TRUSTSTORE_PASSWORD);
189     if (Objects.isNull(truststorePassword)) {
190       truststorePassword = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PASSWORD_KEY);
191     }
192     return truststorePassword;
193   }
194
195   /**
196    * Gets ssl port.
197    *
198    * @return the ssl port
199    */
200   public int getSslPort() {
201     int port;
202     String sslPort = System.getProperty(CASSANDRA_PORT);
203     if (Objects.isNull(sslPort)) {
204       sslPort = (String) cassandraConfiguration.get(CASSANDRA_PORT_KEY);
205       if (Objects.isNull(sslPort)) {
206         sslPort = "0";
207       }
208     }
209     port = Integer.valueOf(sslPort);
210     return port;
211   }
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
241     res = Boolean.valueOf(value);
242
243     return res;
244   }
245
246   private <T> T readFromFile(String file, Function<InputStream, T> reader) throws IOException {
247     try (InputStream is = new FileInputStream(file)) {
248       return reader.apply(is);
249     }
250   }
251
252   public String getConsistencyLevel() {
253     String consistencyLevel = System.getProperty(CONSISTENCY_LEVEL);
254     if (Objects.isNull(consistencyLevel)) {
255       consistencyLevel = (String) cassandraConfiguration.get(CONSISTENCY_LEVEL_KEY);
256     }
257
258     if (Objects.isNull(consistencyLevel)) {
259       consistencyLevel = "LOCAL_QUORUM";
260     }
261     return consistencyLevel;
262   }
263
264   public String getLocalDataCenter() {
265     String localDataCenter = System.getProperty(LOCAL_DATA_CENTER);
266     if (Objects.isNull(localDataCenter)) {
267       localDataCenter = (String) cassandraConfiguration.get(LOCAL_DATA_CENTER_KEY);
268     }
269     return localDataCenter;
270   }
271 }