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