removed code smells
[dmaap/messagerouter/msgrtr.git] / src / main / java / org / onap / dmaap / dmf / mr / utils / ConfigurationReader.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 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  *        http://www.apache.org/licenses/LICENSE-2.0
11 *  
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  *  
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package org.onap.dmaap.dmf.mr.utils;
23
24 import javax.servlet.ServletException;
25
26 import org.I0Itec.zkclient.ZkClient;
27 import org.apache.curator.framework.CuratorFramework;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.beans.factory.annotation.Qualifier;
30 import org.springframework.stereotype.Component;
31
32 import org.onap.dmaap.dmf.mr.backends.ConsumerFactory;
33 import org.onap.dmaap.dmf.mr.backends.MetricsSet;
34 import org.onap.dmaap.dmf.mr.backends.Publisher;
35 import org.onap.dmaap.dmf.mr.backends.kafka.KafkaConsumerCache.KafkaConsumerCacheException;
36 import org.onap.dmaap.dmf.mr.backends.memory.MemoryConsumerFactory;
37 import org.onap.dmaap.dmf.mr.backends.memory.MemoryMetaBroker;
38 import org.onap.dmaap.dmf.mr.backends.memory.MemoryQueue;
39 import org.onap.dmaap.dmf.mr.backends.memory.MemoryQueuePublisher;
40 import org.onap.dmaap.dmf.mr.beans.DMaaPCambriaLimiter;
41 import org.onap.dmaap.dmf.mr.beans.DMaaPZkConfigDb;
42 import org.onap.dmaap.dmf.mr.constants.CambriaConstants;
43 import org.onap.dmaap.dmf.mr.metabroker.Broker;
44
45 import org.onap.dmaap.dmf.mr.metabroker.Broker1;
46 import org.onap.dmaap.dmf.mr.security.DMaaPAuthenticator;
47 import org.onap.dmaap.dmf.mr.security.impl.DMaaPOriginalUebAuthenticator;
48 import com.att.eelf.configuration.EELFLogger;
49 import com.att.eelf.configuration.EELFManager;
50 import com.att.nsa.configs.ConfigDbException;
51 import com.att.nsa.configs.confimpl.MemConfigDb;
52 import com.att.nsa.drumlin.till.nv.rrNvReadable;
53 import com.att.nsa.drumlin.till.nv.rrNvReadable.invalidSettingValue;
54 import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
55 import com.att.nsa.limits.Blacklist;
56 import com.att.nsa.security.NsaAuthenticatorService;
57
58 import com.att.nsa.security.db.BaseNsaApiDbImpl;
59 import com.att.nsa.security.db.NsaApiDb;
60 import com.att.nsa.security.db.NsaApiDb.KeyExistsException;
61 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
62 import com.att.nsa.security.db.simple.NsaSimpleApiKeyFactory;
63
64 /**
65  * Class is created for all the configuration for rest and service layer
66  * integration.
67  *
68  */
69 @Component
70 public class ConfigurationReader {
71
72
73         private Broker1 fMetaBroker;
74         private ConsumerFactory fConsumerFactory;
75         private Publisher fPublisher;
76         private MetricsSet fMetrics;
77         @Autowired
78         private DMaaPCambriaLimiter fRateLimiter;
79         private NsaApiDb<NsaSimpleApiKey> fApiKeyDb;
80         
81         private DMaaPAuthenticator<NsaSimpleApiKey> fSecurityManager;
82         private NsaAuthenticatorService<NsaSimpleApiKey> nsaSecurityManager;
83         private static CuratorFramework curator;
84         private ZkClient zk;
85         private DMaaPZkConfigDb fConfigDb;
86         private MemoryQueue q;
87         private MemoryMetaBroker mmb;
88         private Blacklist fIpBlackList;
89         private Emailer fEmailer;
90
91         private static final EELFLogger log = EELFManager.getInstance().getLogger(ConfigurationReader.class);
92         
93
94         /**
95          * constructor to initialize all the values
96          * 
97          * @param settings
98          * @param fMetrics
99          * @param zk
100          * @param fConfigDb
101          * @param fPublisher
102          * @param curator
103          * @param fConsumerFactory
104          * @param fMetaBroker
105          * @param q
106          * @param mmb
107          * @param fApiKeyDb
108          * @param fSecurityManager
109          * @throws missingReqdSetting
110          * @throws invalidSettingValue
111          * @throws ServletException
112          * @throws KafkaConsumerCacheException
113          * @throws ConfigDbException 
114          * @throws KeyExistsException 
115          */
116         @Autowired
117         public ConfigurationReader(@Qualifier("propertyReader") rrNvReadable settings,
118                         @Qualifier("dMaaPMetricsSet") MetricsSet fMetrics, @Qualifier("dMaaPZkClient") ZkClient zk,
119                         @Qualifier("dMaaPZkConfigDb") DMaaPZkConfigDb fConfigDb, @Qualifier("kafkaPublisher") Publisher fPublisher,
120                         @Qualifier("curator") CuratorFramework curator,
121                         @Qualifier("dMaaPKafkaConsumerFactory") ConsumerFactory fConsumerFactory,
122                         @Qualifier("dMaaPKafkaMetaBroker") Broker1 fMetaBroker,
123                         @Qualifier("q") MemoryQueue q,
124                         @Qualifier("mmb") MemoryMetaBroker mmb, @Qualifier("dMaaPNsaApiDb") NsaApiDb<NsaSimpleApiKey> fApiKeyDb,
125                         /*
126                          * @Qualifier("dMaaPTranDb")
127                          * DMaaPTransactionObjDB<DMaaPTransactionObj> fTranDb,
128                          */
129                         @Qualifier("dMaaPAuthenticatorImpl") DMaaPAuthenticator<NsaSimpleApiKey> fSecurityManager
130                         )
131                                         throws missingReqdSetting, invalidSettingValue, ServletException, KafkaConsumerCacheException, ConfigDbException, KeyExistsException {
132                 
133                 this.fMetrics = fMetrics;
134                 this.zk = zk;
135                 this.fConfigDb = fConfigDb;
136                 this.fPublisher = fPublisher;
137                 ConfigurationReader.curator = curator;
138                 this.fConsumerFactory = fConsumerFactory;
139                 this.fMetaBroker = fMetaBroker;
140                 
141                 this.q = q;
142                 this.mmb = mmb;
143                 this.fApiKeyDb = fApiKeyDb;
144                 
145                 this.fSecurityManager = fSecurityManager;
146                 
147                 long allowedtimeSkewMs=600000L;
148                 String strallowedTimeSkewM= com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,"authentication.allowedTimeSkewMs");
149                 if(null!=strallowedTimeSkewM)
150                 {
151                         allowedtimeSkewMs= Long.parseLong(strallowedTimeSkewM);
152                 }       
153         
154                 //String strrequireSecureChannel= com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,"aauthentication.requireSecureChannel");
155                 //if(strrequireSecureChannel!=null)requireSecureChannel=Boolean.parseBoolean(strrequireSecureChannel);
156                 //this.nsaSecurityManager = new NsaAuthenticatorService<NsaSimpleApiKey>(this.fApiKeyDb, settings.getLong("authentication.allowedTimeSkewMs", 600000L), settings.getBoolean("authentication.requireSecureChannel", true));
157                 //this.nsaSecurityManager = new NsaAuthenticatorService<NsaSimpleApiKey>(this.fApiKeyDb, allowedtimeSkewMs, requireSecureChannel);
158                 
159                 servletSetup();
160         }
161
162         protected void servletSetup()
163                         throws rrNvReadable.missingReqdSetting, rrNvReadable.invalidSettingValue, ServletException, ConfigDbException, KeyExistsException {
164                 try {
165
166                         fMetrics.toJson();
167                         fMetrics.setupCambriaSender();
168                         // add the admin authenticator
169                         
170                                                 final String adminSecret = com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,CambriaConstants.kSetting_AdminSecret);
171                                                 
172                                                 if ( adminSecret != null && adminSecret.length () > 0 )
173                                                 {
174                                                                 
175                                                                 final NsaApiDb<NsaSimpleApiKey> adminDb = new BaseNsaApiDbImpl<NsaSimpleApiKey> ( new MemConfigDb(), new NsaSimpleApiKeyFactory() );
176                                                                 adminDb.createApiKey ( "admin", adminSecret );
177                                                         
178                                                         fSecurityManager.addAuthenticator ( new DMaaPOriginalUebAuthenticator<NsaSimpleApiKey> ( adminDb, 10*60*1000 ) );
179                                                 
180                                                 }
181                                         
182                         // setup a backend
183                         
184                          String type = com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,CambriaConstants.kBrokerType);
185                         if (type==null){
186                                 type = CambriaConstants.kBrokerType_Kafka;
187                         }
188                         if (CambriaConstants.kBrokerType_Kafka.equalsIgnoreCase(type)) {
189                                 log.info("Broker Type is:" + CambriaConstants.kBrokerType_Kafka);
190                         } else if (CambriaConstants.kBrokerType_Memory.equalsIgnoreCase(type)) {
191                                 log.info("Broker Type is:" + CambriaConstants.kBrokerType_Memory);
192                                 fPublisher = new MemoryQueuePublisher(q, mmb);
193                                 //Ramkumar remove below
194                         
195                                 fConsumerFactory = new MemoryConsumerFactory(q);
196                         } else {
197                                 throw new IllegalArgumentException(
198                                                 "Unrecognized type for " + CambriaConstants.kBrokerType + ": " + type + ".");
199                         }
200                         fIpBlackList = new Blacklist ( getfConfigDb(), getfConfigDb().parse ( "/ipBlacklist" ) );
201                         this.fEmailer = new Emailer();
202                         log.info("Broker Type is:" + type);
203
204                 } catch (SecurityException e) {
205                         throw new ServletException(e);
206                 }
207         }
208
209         /**
210          * method returns metaBroker
211          * 
212          * @return
213          */
214         public Broker1 getfMetaBroker() {
215                 return fMetaBroker;
216         }
217
218         /**
219          * method to set the metaBroker
220          * 
221          * @param fMetaBroker
222          */
223         public void setfMetaBroker(Broker1 fMetaBroker) {
224                 this.fMetaBroker = fMetaBroker;
225         }
226
227         /**
228          * method to get ConsumerFactory Object
229          * 
230          * @return
231          */
232         public ConsumerFactory getfConsumerFactory() {
233                 return fConsumerFactory;
234         }
235
236         /**
237          * method to set the consumerfactory object
238          * 
239          * @param fConsumerFactory
240          */
241         public void setfConsumerFactory(ConsumerFactory fConsumerFactory) {
242                 this.fConsumerFactory = fConsumerFactory;
243         }
244
245         /**
246          * method to get Publisher object
247          * 
248          * @return
249          */
250         public Publisher getfPublisher() {
251                 return fPublisher;
252         }
253
254         /**
255          * method to set Publisher object
256          * 
257          * @param fPublisher
258          */
259         public void setfPublisher(Publisher fPublisher) {
260                 this.fPublisher = fPublisher;
261         }
262
263         /**
264          * method to get MetricsSet Object
265          * 
266          * @return
267          */
268         public MetricsSet getfMetrics() {
269                 return fMetrics;
270         }
271
272         /**
273          * method to set MetricsSet Object
274          * 
275          * @param fMetrics
276          */
277         public void setfMetrics(MetricsSet fMetrics) {
278                 this.fMetrics = fMetrics;
279         }
280
281         /**
282          * method to get DMaaPCambriaLimiter object
283          * 
284          * @return
285          */
286         public DMaaPCambriaLimiter getfRateLimiter() {
287                 return fRateLimiter;
288         }
289
290         /**
291          * method to set DMaaPCambriaLimiter object
292          * 
293          * @param fRateLimiter
294          */
295         public void setfRateLimiter(DMaaPCambriaLimiter fRateLimiter) {
296                 this.fRateLimiter = fRateLimiter;
297         }
298
299         /**
300          * Method to get DMaaPAuthenticator object
301          * 
302          * @return
303          */
304         public DMaaPAuthenticator<NsaSimpleApiKey> getfSecurityManager() {
305                 return fSecurityManager;
306         }
307
308         /**
309          * method to set DMaaPAuthenticator object
310          * 
311          * @param fSecurityManager
312          */
313         public void setfSecurityManager(DMaaPAuthenticator<NsaSimpleApiKey> fSecurityManager) {
314                 this.fSecurityManager = fSecurityManager;
315         }
316
317         /**
318          * method to get rrNvReadable object
319          * 
320          * @return
321          */
322         
323                 
324         
325
326         /**
327          * method to set rrNvReadable object
328          * 
329          * @param settings
330          */
331         
332         
333         
334
335         /**
336          * method to get CuratorFramework object
337          * 
338          * @return
339          */
340         public static CuratorFramework getCurator() {
341                 return curator;
342         }
343
344         /**
345          * method to set CuratorFramework object
346          * 
347          * @param curator
348          */
349         public static void setCurator(CuratorFramework curator) {
350                 ConfigurationReader.curator = curator;
351         }
352
353         /**
354          * method to get ZkClient object
355          * 
356          * @return
357          */
358         public ZkClient getZk() {
359                 return zk;
360         }
361
362         /**
363          * method to set ZkClient object
364          * 
365          * @param zk
366          */
367         public void setZk(ZkClient zk) {
368                 this.zk = zk;
369         }
370
371         /**
372          * method to get DMaaPZkConfigDb object
373          * 
374          * @return
375          */
376         public DMaaPZkConfigDb getfConfigDb() {
377                 return fConfigDb;
378         }
379
380         /**
381          * method to set DMaaPZkConfigDb object
382          * 
383          * @param fConfigDb
384          */
385         public void setfConfigDb(DMaaPZkConfigDb fConfigDb) {
386                 this.fConfigDb = fConfigDb;
387         }
388
389         /**
390          * method to get MemoryQueue object
391          * 
392          * @return
393          */
394         public MemoryQueue getQ() {
395                 return q;
396         }
397
398         /**
399          * method to set MemoryQueue object
400          * 
401          * @param q
402          */
403         public void setQ(MemoryQueue q) {
404                 this.q = q;
405         }
406
407         /**
408          * method to get MemoryMetaBroker object
409          * 
410          * @return
411          */
412         public MemoryMetaBroker getMmb() {
413                 return mmb;
414         }
415
416         /**
417          * method to set MemoryMetaBroker object
418          * 
419          * @param mmb
420          */
421         public void setMmb(MemoryMetaBroker mmb) {
422                 this.mmb = mmb;
423         }
424
425         /**
426          * method to get NsaApiDb object
427          * 
428          * @return
429          */
430         public NsaApiDb<NsaSimpleApiKey> getfApiKeyDb() {
431                 return fApiKeyDb;
432         }
433
434         /**
435          * method to set NsaApiDb object
436          * 
437          * @param fApiKeyDb
438          */
439         public void setfApiKeyDb(NsaApiDb<NsaSimpleApiKey> fApiKeyDb) {
440                 this.fApiKeyDb = fApiKeyDb;
441         }
442
443         /*
444          * public DMaaPTransactionObjDB<DMaaPTransactionObj> getfTranDb() { return
445          * fTranDb; }
446          * 
447          * public void setfTranDb(DMaaPTransactionObjDB<DMaaPTransactionObj>
448          * fTranDb) { this.fTranDb = fTranDb; }
449          */
450         /**
451          * method to get the zookeeper connection String
452          * 
453          * @param settings
454          * @return
455          */
456         public static String getMainZookeeperConnectionString() {
457                 
458                 
459                  String zkServername = com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,CambriaConstants.kSetting_ZkConfigDbServers);
460                  if (zkServername==null){
461                          zkServername=CambriaConstants.kDefault_ZkConfigDbServers;
462                  }
463                  return zkServername;
464         }
465
466         public static String getMainZookeeperConnectionSRoot(){
467                 String strVal=com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,CambriaConstants.kSetting_ZkConfigDbRoot);
468         
469                 if (null==strVal)
470                         strVal=CambriaConstants.kDefault_ZkConfigDbRoot;
471         
472                 return strVal;
473         }
474         
475         public Blacklist getfIpBlackList() {
476                 return fIpBlackList;
477         }
478
479         public void setfIpBlackList(Blacklist fIpBlackList) {
480                 this.fIpBlackList = fIpBlackList;
481         }
482
483         public NsaAuthenticatorService<NsaSimpleApiKey> getNsaSecurityManager() {
484                 return nsaSecurityManager;
485         }
486
487         public void setNsaSecurityManager(NsaAuthenticatorService<NsaSimpleApiKey> nsaSecurityManager) {
488                 this.nsaSecurityManager = nsaSecurityManager;
489         }
490         
491         public Emailer getSystemEmailer()
492           {
493             return this.fEmailer;
494           }
495
496
497 }