1f5e20fd0b3dab5ffed933f8c4c39950e83afc1a
[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.sdc.logging.api.Logger;
24 import org.openecomp.sdc.logging.api.LoggerFactory;
25 import org.openecomp.sdc.tosca.services.YamlUtil;
26
27 import java.io.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.InputStream;
30 import java.util.ArrayList;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import java.util.Map;
34
35 /**
36  * The type Configuration manager.
37  */
38 public class ConfigurationManager {
39
40   private static final String CONFIGURATION_YAML_FILE = "configuration.yaml";
41   private static final String cassandraKey = "cassandraConfig";
42   private static final String DEFAULT_KEYSPACE_NAME = "dox";
43   private static final String CASSANDRA_ADDRESSES = "cassandra.addresses";
44   private static final String CASSANDRA_DOX_KEY_STORE = "cassandra.dox.keystore";
45   private static final String CASSANDRA_AUTHENTICATE = "cassandra.authenticate";
46   private static final String CASSANDRA_USER = "cassandra.user";
47   private static final String CASSANDRA_PASSWORD = "cassandra.password";
48   private static final String CASSANDRA_PORT = "cassandra.port";
49   private static final String CASSANDRA_SSL = "cassandra.ssl";
50   private static final String CASSANDRA_TRUSTSTORE = "cassandra.Truststore";
51   private static final String CASSANDRA_TRUSTSTORE_PASSWORD = "cassandra.TruststorePassword";
52   private static final String cassandraHostsKey = "cassandraHosts";
53   private static final String cassandraPortKey = "port";
54   private static final String cassandraUsernameKey = "username";
55   private static final String cassandraPasswordKey = "password";
56   private static final String cassandraAuthenticateKey = "authenticate";
57   private static final String cassandraSSLKey = "ssl";
58   private static final String cassandraTruststorePathKey = "truststorePath";
59   private static final String cassandraTruststorePasswordKey = "truststorePassword";
60   private static ConfigurationManager instance = null;
61   private final LinkedHashMap<String, Object> cassandraConfiguration;
62
63   private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
64
65
66   private ConfigurationManager() {
67     YamlUtil yamlUtil = new YamlUtil();
68     String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE);
69     InputStream yamlAsString;
70     if (configurationYamlFile != null) {
71       yamlAsString = getConfigFileIs(configurationYamlFile);
72     } else {
73       //Load from resources
74       yamlAsString = yamlUtil.loadYamlFileIs("/" + CONFIGURATION_YAML_FILE);
75     }
76     Map<String, LinkedHashMap<String, Object>> configurationMap = yamlUtil.yamlToMap(yamlAsString);
77     cassandraConfiguration = configurationMap.get(cassandraKey);
78
79   }
80
81   /**
82    * Gets instance.
83    *
84    * @return the instance
85    */
86   public static ConfigurationManager getInstance() {
87     if (instance == null) {
88       instance = new ConfigurationManager();
89     }
90     return instance;
91   }
92
93   /**
94    * Get addresses string [ ].
95    *
96    * @return the string [ ]
97    */
98   public String[] getAddresses() {
99
100     String addresses = System.getProperty(CASSANDRA_ADDRESSES);
101     if (addresses != null) {
102       return addresses.split(",");
103     }
104     List lsAddresses = (ArrayList) cassandraConfiguration.get(cassandraHostsKey);
105     String[] addressesArray;
106     addressesArray = (String[]) lsAddresses.toArray(new String[lsAddresses.size()]);
107     return addressesArray;
108
109   }
110
111   /**
112    * Gets key space.
113    *
114    * @return the key space
115    */
116   public String getKeySpace() {
117     String keySpace = System.getProperty(CASSANDRA_DOX_KEY_STORE);
118     if (keySpace == null) {
119       //keySpace = cassandraConfiguration.get(cassandraKeySpaceKey);
120       //if (keySpace == null)
121       keySpace = DEFAULT_KEYSPACE_NAME;
122     }
123     return keySpace;
124   }
125
126   /**
127    * Gets username.
128    *
129    * @return the username
130    */
131   public String getUsername() {
132     String username = System.getProperty(CASSANDRA_USER);
133     if (username == null) {
134       username = (String) cassandraConfiguration.get(cassandraUsernameKey);
135     }
136     return username;
137   }
138
139   /**
140    * Gets password.
141    *
142    * @return the password
143    */
144   public String getPassword() {
145     String password = System.getProperty(CASSANDRA_PASSWORD);
146     if (password == null) {
147       password = (String) cassandraConfiguration.get(cassandraPasswordKey);
148     }
149     return password;
150   }
151
152   /**
153    * Gets truststore path.
154    *
155    * @return the truststore path
156    */
157   public String getTruststorePath() {
158     String truststorePath = System.getProperty(CASSANDRA_TRUSTSTORE);
159     if (truststorePath == null) {
160       truststorePath = (String) cassandraConfiguration.get(cassandraTruststorePathKey);
161     }
162     return truststorePath;
163   }
164
165   /**
166    * Gets truststore password.
167    *
168    * @return the truststore password
169    */
170   public String getTruststorePassword() {
171     String truststorePassword = System.getProperty(CASSANDRA_TRUSTSTORE_PASSWORD);
172     if (truststorePassword == null) {
173       truststorePassword = (String) cassandraConfiguration.get(cassandraTruststorePasswordKey);
174     }
175     return truststorePassword;
176   }
177
178   /**
179    * Gets ssl port.
180    *
181    * @return the ssl port
182    */
183   public int getSslPort() {
184     int port;
185     String sslPort = System.getProperty(CASSANDRA_PORT);
186     if (sslPort == null) {
187       sslPort = (String) cassandraConfiguration.get(cassandraPortKey);
188       if (sslPort == null) {
189         sslPort = "0";
190       }
191     }
192     port = Integer.valueOf(sslPort);
193     return port;
194   }
195
196
197   /**
198    * Is ssl boolean.
199    *
200    * @return the boolean
201    */
202   public boolean isSsl() {
203     return getBooleanResult(CASSANDRA_SSL, cassandraSSLKey);
204   }
205
206   /**
207    * Is authenticate boolean.
208    *
209    * @return the boolean
210    */
211   public boolean isAuthenticate() {
212     return getBooleanResult(CASSANDRA_AUTHENTICATE, cassandraAuthenticateKey);
213   }
214
215   private Boolean getBooleanResult(String property, String key) {
216     Boolean res;
217     String value;
218     if (System.getProperty(property) == null) {
219       value = String.valueOf(cassandraConfiguration.get(key));
220     } else {
221       value = System.getProperty(property);
222     }
223
224     res = Boolean.valueOf(value);
225
226     return res;
227   }
228
229   private InputStream getConfigFileIs(String file) {
230     InputStream is = null;
231     try {
232       is = new FileInputStream(file);
233     } catch (FileNotFoundException exception) {
234       log.debug("",exception);
235     }
236     return is;
237   }
238 }