Default Cipher to empty string
[music.git] / music-rest / src / main / java / org / onap / music / main / PropertiesLoader.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
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  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22
23 package org.onap.music.main;
24
25 import java.util.HashSet;
26 import java.util.Properties;
27
28 import org.onap.music.eelf.logging.EELFLoggerDelegate;
29 import org.springframework.beans.factory.InitializingBean;
30 import org.springframework.beans.factory.annotation.Value;
31 import org.springframework.context.annotation.Bean;
32 import org.springframework.context.annotation.PropertySource;
33 import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
34 import org.springframework.stereotype.Component;
35
36 @PropertySource(value = {"file:/opt/app/music/etc/music.properties", "classpath:/project.properties"})
37 //"file:/opt/app/music/etc/key.properties"
38 @Component
39 public class PropertiesLoader implements InitializingBean {
40
41     @Value("${cassandra.host}")
42     public String cassandraHost;
43
44     @Value("${debug}")
45     public String debug;
46
47     @Value("${version}")
48     public String version;
49
50     @Value("${build}")
51     public String build;
52
53     @Value("${music.properties}")
54     public String musicProperties;
55
56     @Value("${lock.lease.period}")
57     public String lockLeasePeriod;
58
59     @Value("${cassandra.user}")
60     public String cassandraUser;
61
62     @Value("${cassandra.password}")
63     public String cassandraPassword;
64
65     @Value("${cassandra.port}")
66     public String cassandraPort;
67
68     @Value("${cassandra.connecttimeoutms}")
69     public String cassandraConnectTimeOutMS;
70
71     @Value("${cassandra.readtimeoutms}")
72     public String cassandraReadTimeOutMS;
73
74     @Value("${cadi}")
75     public String isCadi;
76
77     @Value("${keyspace.active}")
78     public String isKeyspaceActive;
79
80     @Value("${retry.count}")
81     public String rertryCount;
82     
83     @Value("${lock.daemon.sleeptime.ms}")
84     public String lockDaemonSleeptimems;
85
86     @Value("${keyspaces.for.lock.cleanup}")
87     public String keyspacesForLockCleanup;
88
89     @Value("${create.lock.wait.period.ms}")
90     private long createLockWaitPeriod;
91
92     @Value("${create.lock.wait.increment.ms}")
93     private int createLockWaitIncrement;
94
95     @Value("${transId.header.prefix}")
96     private String transIdPrefix;
97
98     @Value("${conversation.header.prefix}")
99     private String conversationIdPrefix;
100
101     @Value("${clientId.header.prefix}")
102     private String clientIdPrefix;
103
104     @Value("${messageId.header.prefix}")
105     private String messageIdPrefix;    
106
107     @Value("${transId.header.required}")
108     private Boolean transIdRequired;
109
110     @Value("${conversation.header.required}")
111     private Boolean conversationIdRequired;
112
113     @Value("${clientId.header.required}")
114     private Boolean clientIdRequired;
115
116     @Value("${messageId.header.required}")
117     private Boolean messageIdRequired;
118
119     @Value("${music.aaf.ns}")
120     private String musicAafNs;
121
122     @Value("${cipher.enc.key:}")
123     private String cipherEncKey;
124
125     @SuppressWarnings("unused")
126     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PropertiesLoader.class);
127
128     @Bean
129     public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
130
131         PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
132         pspc.setIgnoreResourceNotFound(true);
133         pspc.setIgnoreUnresolvablePlaceholders(true);
134         return pspc;
135     }
136
137     /**
138      * .
139      */
140     public void loadProperties() {
141         if(cipherEncKey != null) {
142             MusicUtil.setCipherEncKey(cipherEncKey);
143         }
144         if (musicAafNs != null) {
145             MusicUtil.setMusicAafNs(musicAafNs);
146         }
147         if (cassandraPort != null && !cassandraPort.equals("${cassandra.port}")) {
148             MusicUtil.setCassandraPort(Integer.parseInt(cassandraPort));
149         }
150         if (cassandraUser != null && !cassandraUser.equals("${cassandra.user}")) {
151             MusicUtil.setCassName(cassandraUser);
152         }
153         if (cassandraPassword != null && !cassandraPassword.equals("${cassandra.password}")) {
154             MusicUtil.setCassPwd(cassandraPassword);
155         }
156         if (cassandraConnectTimeOutMS != null && !cassandraConnectTimeOutMS.equals("${cassandra.connecttimeoutms}")) {
157             MusicUtil.setCassandraConnectTimeOutMS(Integer.parseInt(cassandraConnectTimeOutMS));
158         }
159         if (cassandraReadTimeOutMS != null && !cassandraReadTimeOutMS.equals("${cassandra.readtimeoutms}")) {
160             MusicUtil.setCassandraReadTimeOutMS(Integer.parseInt(cassandraReadTimeOutMS));
161         }
162         if (debug != null && !debug.equals("${debug}")) {
163             MusicUtil.setDebug(Boolean.parseBoolean(debug));
164         }
165         if (lockLeasePeriod != null && !lockLeasePeriod.equals("${lock.lease.period}")) {
166             MusicUtil.setDefaultLockLeasePeriod(Long.parseLong(lockLeasePeriod));
167         }
168         if (musicProperties != null && !musicProperties.equals("${music.properties}")) {
169             MusicUtil.setMusicPropertiesFilePath(musicProperties);
170         }
171         if (cassandraHost != null && !cassandraHost.equals("${cassandra.host}")) {
172             MusicUtil.setMyCassaHost(cassandraHost);
173         }
174         if (version != null && !version.equals("${version}")) {
175             MusicUtil.setVersion(version);
176         }
177         if (build != null && !version.equals("${build}")) {
178             MusicUtil.setBuild(build);
179         }
180         if (isCadi != null && !isCadi.equals("${cadi}")) {
181             MusicUtil.setIsCadi(Boolean.parseBoolean(isCadi));
182         }
183         if (rertryCount != null && !rertryCount.equals("${retry.count}")) {
184             MusicUtil.setRetryCount(Integer.parseInt(rertryCount));
185         }
186         if (isKeyspaceActive != null && !isKeyspaceActive.equals("${keyspace.active}")) {
187             MusicUtil.setKeyspaceActive(Boolean.parseBoolean(isKeyspaceActive));
188         }
189         if (lockDaemonSleeptimems != null && !lockDaemonSleeptimems.equals("${lock.daemon.sleeptime.ms}")) {
190             MusicUtil.setLockDaemonSleepTimeMs(Long.parseLong(lockDaemonSleeptimems));
191         }
192         if (keyspacesForLockCleanup !=null && !keyspacesForLockCleanup.equals("${keyspaces.for.lock.cleanup}")) {
193             HashSet<String> keyspaces = new HashSet<>();
194             for (String keyspace: keyspacesForLockCleanup.split(",")) {
195                 keyspaces.add(keyspace);
196             }
197             MusicUtil.setKeyspacesToCleanLocks(keyspaces);
198         }
199         if(transIdPrefix!=null) {
200             MusicUtil.setTransIdPrefix(transIdPrefix);
201         }
202
203         if(conversationIdPrefix!=null) {
204             MusicUtil.setConversationIdPrefix(conversationIdPrefix);
205         }
206
207         if(clientIdPrefix!=null) {
208             MusicUtil.setClientIdPrefix(clientIdPrefix);
209         }
210
211         if(messageIdPrefix!=null) {
212             MusicUtil.setMessageIdPrefix(messageIdPrefix);
213         }
214
215         if(transIdRequired!=null) {
216             MusicUtil.setTransIdRequired(transIdRequired);
217         }
218
219         if(conversationIdRequired!=null) {
220             MusicUtil.setConversationIdRequired(conversationIdRequired);
221         }
222
223         if(clientIdRequired!=null) {
224             MusicUtil.setClientIdRequired(clientIdRequired);
225         }
226
227         if(messageIdRequired!=null) {
228             MusicUtil.setMessageIdRequired(messageIdRequired);
229         }
230         
231         if(createLockWaitPeriod!=0) {
232             MusicUtil.setCreateLockWaitPeriod(createLockWaitPeriod);
233         }
234
235         if(createLockWaitIncrement!=0) {
236             MusicUtil.setCreateLockWaitIncrement(createLockWaitIncrement);
237         }
238     }
239
240     public static void loadProperties(Properties properties) {
241         if (properties.getProperty("cassandra.host")!=null) {
242             MusicUtil.setMyCassaHost(properties.getProperty("cassandra.host"));
243         }
244
245         if (properties.getProperty("cassandra.port")!=null) {
246             MusicUtil.setCassandraPort(Integer.parseInt(properties.getProperty("cassandra.port")));
247         }
248
249         if (properties.getProperty("cassandra.user")!=null) {
250             MusicUtil.setCassName(properties.getProperty("cassandra.user"));
251         }
252
253         if (properties.getProperty("cassandra.password")!=null) {
254             MusicUtil.setCassPwd(properties.getProperty("cassandra.password"));
255         }
256
257         if(properties.getProperty("cassandra.connectTimeOutMS")!=null) {
258             MusicUtil.setCassandraConnectTimeOutMS(Integer.parseInt(properties.getProperty("cassandra.connecttimeoutms")));
259         }
260
261         if(properties.getProperty("cassandra.readTimeOutMS")!=null) {
262             MusicUtil.setCassandraReadTimeOutMS(Integer.parseInt(properties.getProperty("cassandra.readtimeoutms")));
263         }
264
265         if (properties.getProperty("music.properties")!=null) {
266             MusicUtil.setMusicPropertiesFilePath(properties.getProperty("music.properties"));
267         }
268
269         if (properties.getProperty("debug")!=null) {
270             MusicUtil.setDebug(Boolean.parseBoolean(properties.getProperty("debug")));
271         }
272
273         if (properties.getProperty("version")!=null) {
274             MusicUtil.setVersion(properties.getProperty("version"));
275         }
276
277         if (properties.getProperty("build")!=null) {
278             MusicUtil.setBuild(properties.getProperty("build"));
279         }
280
281         if (properties.getProperty("lock.lease.period")!=null) {
282             MusicUtil.setDefaultLockLeasePeriod(Long.parseLong(properties.getProperty("lock.lease.period")));
283         }
284
285         if (properties.getProperty("cadi")!=null) {
286             MusicUtil.setIsCadi(Boolean.parseBoolean(properties.getProperty("cadi")));
287         }
288
289         if (properties.getProperty("keyspace.active")!=null) {
290             MusicUtil.setKeyspaceActive(Boolean.parseBoolean(properties.getProperty("keyspace.active")));
291         }
292
293         if (properties.getProperty("retry.count")!=null) {
294             MusicUtil.setRetryCount(Integer.parseInt(properties.getProperty("retry.count")));
295         }
296
297         if (properties.getProperty("transId.header.prefix")!=null) {
298             MusicUtil.setTransIdPrefix(properties.getProperty("transId.header.prefix"));
299         }
300
301         if (properties.getProperty("conversation.header.prefix")!=null) {
302             MusicUtil.setConversationIdPrefix(properties.getProperty("conversation.header.prefix"));
303         }
304
305         if (properties.getProperty("clientId.header.prefix")!=null) {
306             MusicUtil.setClientIdPrefix(properties.getProperty("clientId.header.prefix"));
307         }
308
309         if (properties.getProperty("messageId.header.prefix")!=null) {
310             MusicUtil.setMessageIdPrefix(properties.getProperty("messageId.header.prefix"));
311         }
312
313         if (properties.getProperty("transId.header.required")!=null) {
314             MusicUtil.setTransIdRequired(Boolean.parseBoolean(properties.getProperty("transId.header.required")));
315         }
316
317         if (properties.getProperty("conversation.header.required")!=null) {
318             MusicUtil.setConversationIdRequired(Boolean.parseBoolean(properties.getProperty("conversation.header.required")));
319         }
320
321         if (properties.getProperty("clientId.header.required")!=null) {
322             MusicUtil.setClientIdRequired(Boolean.parseBoolean(properties.getProperty("clientId.header.required")));
323         }
324
325         if (properties.getProperty("messageId.header.required")!=null) {
326             MusicUtil.setMessageIdRequired(Boolean.parseBoolean(properties.getProperty("messageId.header.required")));
327         }
328
329         if (properties.getProperty("music.aaf.ns")!=null) {
330             MusicUtil.setMusicAafNs(properties.getProperty("music.aaf.ns"));
331         }
332
333         if (properties.getProperty("cipher.enc.key")!=null) {
334             MusicUtil.setCipherEncKey(properties.getProperty("cipher.enc.key"));
335         }
336
337     }
338
339     @Override
340     public void afterPropertiesSet() throws Exception {
341         // TODO Auto-generated method stub
342
343     }
344     
345     /* For unit testing purpose only*/
346     protected void setProperties() {
347         cassandraHost = "127.0.0.1";
348         debug = "true";
349         version = "x.x.x";
350         build = "y.y";
351         musicProperties = "property";
352         lockLeasePeriod = "5000";
353         cassandraUser = "user";
354         cassandraPassword = "password";
355         cassandraPort = "8007";
356         cassandraConnectTimeOutMS = "1000";
357         cassandraReadTimeOutMS = "1000";
358         isCadi = "true";
359         isKeyspaceActive = "true";
360         rertryCount = "20";
361         transIdPrefix = "transId-";
362         conversationIdPrefix = "conversation-";
363         clientIdPrefix = "clientId-";
364         messageIdPrefix = "messageId-";    
365         transIdRequired = true;
366         conversationIdRequired = true;
367         clientIdRequired = true;
368         messageIdRequired = true;
369         musicAafNs = "ns";
370         cipherEncKey = "key";
371     }
372
373 }