bde9d06ae4b32271ea1b402d8cbbd1b7e5843fa1
[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.function.Function;
36
37 /**
38  * The type Configuration manager.
39  */
40 public class ConfigurationManager {
41
42   static final String CONFIGURATION_YAML_FILE = "configuration.yaml";
43
44   private static final String CASSANDRA_KEY = "cassandraConfig";
45   private static final String DEFAULT_KEYSPACE_NAME = "dox";
46   private static final String CASSANDRA_ADDRESSES = "cassandra.addresses";
47   private static final String CASSANDRA_DOX_KEY_STORE = "cassandra.dox.keystore";
48   private static final String CASSANDRA_AUTHENTICATE = "cassandra.authenticate";
49   private static final String CASSANDRA_USER = "cassandra.user";
50   private static final String CASSANDRA_PASSWORD = "cassandra.password";
51   private static final String CASSANDRA_PORT = "cassandra.port";
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 = "cassandraHosts";
56   private static final String CASSANDRA_PORT_KEY = "port";
57   private static final String CASSANDRA_USERNAME_KEY = "username";
58   private static final String CASSANDRA_PASSWORD_KEY = "password";
59   private static final String CASSANDRA_AUTHENTICATE_KEY = "authenticate";
60   private static final String CASSANDRA_SSL_KEY = "ssl";
61   private static final String CASSANDRA_TRUSTSTORE_PATH_KEY = "truststorePath";
62   private static final String CASSANDRA_TRUSTSTORE_PASSWORD_KEY = "truststorePassword";
63   private static ConfigurationManager instance = null;
64   private final LinkedHashMap<String, Object> cassandraConfiguration;
65
66   private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
67
68
69   private ConfigurationManager() {
70
71     String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE);
72
73     Function<InputStream, Map<String, LinkedHashMap<String, Object>>> reader = (is) -> {
74       YamlUtil yamlUtil = new YamlUtil();
75       return yamlUtil.yamlToMap(is);
76     };
77
78     try {
79
80       Map<String, LinkedHashMap<String, Object>> configurationMap = configurationYamlFile != null
81               ? readFromFile(configurationYamlFile, reader) // load from file
82               : FileUtils.readViaInputStream(CONFIGURATION_YAML_FILE, reader); // or from resource
83       cassandraConfiguration = configurationMap.get(CASSANDRA_KEY);
84
85     } catch (IOException e) {
86       throw new RuntimeException("Failed to read configuration", e);
87     }
88   }
89
90   /**
91    * Gets instance.
92    *
93    * @return the instance
94    */
95   public static ConfigurationManager getInstance() {
96     if (instance == null) {
97       instance = new ConfigurationManager();
98     }
99     return instance;
100   }
101
102   /**
103    * Get addresses string [ ].
104    *
105    * @return the string [ ]
106    */
107   public String[] getAddresses() {
108
109     String addresses = System.getProperty(CASSANDRA_ADDRESSES);
110     if (addresses != null) {
111       return addresses.split(",");
112     }
113     List lsAddresses = (ArrayList) cassandraConfiguration.get(CASSANDRA_HOSTS_KEY);
114     String[] addressesArray;
115     addressesArray = (String[]) lsAddresses.toArray(new String[lsAddresses.size()]);
116     return addressesArray;
117
118   }
119
120   /**
121    * Gets key space.
122    *
123    * @return the key space
124    */
125   public String getKeySpace() {
126     String keySpace = System.getProperty(CASSANDRA_DOX_KEY_STORE);
127     if (keySpace == null) {
128       //keySpace = cassandraConfiguration.get(cassandraKeySpaceKey);
129       //if (keySpace == null)
130       keySpace = DEFAULT_KEYSPACE_NAME;
131     }
132     return keySpace;
133   }
134
135   /**
136    * Gets username.
137    *
138    * @return the username
139    */
140   public String getUsername() {
141     String username = System.getProperty(CASSANDRA_USER);
142     if (username == null) {
143       username = (String) cassandraConfiguration.get(CASSANDRA_USERNAME_KEY);
144     }
145     return username;
146   }
147
148   /**
149    * Gets password.
150    *
151    * @return the password
152    */
153   public String getPassword() {
154     String password = System.getProperty(CASSANDRA_PASSWORD);
155     if (password == null) {
156       password = (String) cassandraConfiguration.get(CASSANDRA_PASSWORD_KEY);
157     }
158     return password;
159   }
160
161   /**
162    * Gets truststore path.
163    *
164    * @return the truststore path
165    */
166   public String getTruststorePath() {
167     String truststorePath = System.getProperty(CASSANDRA_TRUSTSTORE);
168     if (truststorePath == null) {
169       truststorePath = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PATH_KEY);
170     }
171     return truststorePath;
172   }
173
174   /**
175    * Gets truststore password.
176    *
177    * @return the truststore password
178    */
179   public String getTruststorePassword() {
180     String truststorePassword = System.getProperty(CASSANDRA_TRUSTSTORE_PASSWORD);
181     if (truststorePassword == null) {
182       truststorePassword = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PASSWORD_KEY);
183     }
184     return truststorePassword;
185   }
186
187   /**
188    * Gets ssl port.
189    *
190    * @return the ssl port
191    */
192   public int getSslPort() {
193     int port;
194     String sslPort = System.getProperty(CASSANDRA_PORT);
195     if (sslPort == null) {
196       sslPort = (String) cassandraConfiguration.get(CASSANDRA_PORT_KEY);
197       if (sslPort == null) {
198         sslPort = "0";
199       }
200     }
201     port = Integer.valueOf(sslPort);
202     return port;
203   }
204
205
206   /**
207    * Is ssl boolean.
208    *
209    * @return the boolean
210    */
211   public boolean isSsl() {
212     return getBooleanResult(CASSANDRA_SSL, CASSANDRA_SSL_KEY);
213   }
214
215   /**
216    * Is authenticate boolean.
217    *
218    * @return the boolean
219    */
220   public boolean isAuthenticate() {
221     return getBooleanResult(CASSANDRA_AUTHENTICATE, CASSANDRA_AUTHENTICATE_KEY);
222   }
223
224   private Boolean getBooleanResult(String property, String key) {
225     Boolean res;
226     String value;
227     if (System.getProperty(property) == null) {
228       value = String.valueOf(cassandraConfiguration.get(key));
229     } else {
230       value = System.getProperty(property);
231     }
232
233     res = Boolean.valueOf(value);
234
235     return res;
236   }
237
238   private <T> T readFromFile(String file, Function<InputStream, T> reader) throws IOException {
239     try (InputStream is = new FileInputStream(file)) {
240       return reader.apply(is);
241     }
242   }
243 }