Sonar Major issues
[dmaap/messagerouter/msgrtr.git] / src / main / java / com / att / 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 com.att.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 com.att.dmf.mr.backends.ConsumerFactory;
33 import com.att.dmf.mr.backends.MetricsSet;
34 import com.att.dmf.mr.backends.Publisher;
35 import com.att.dmf.mr.backends.kafka.KafkaConsumerCache.KafkaConsumerCacheException;
36 import com.att.dmf.mr.backends.memory.MemoryConsumerFactory;
37 import com.att.dmf.mr.backends.memory.MemoryMetaBroker;
38 import com.att.dmf.mr.backends.memory.MemoryQueue;
39 import com.att.dmf.mr.backends.memory.MemoryQueuePublisher;
40 import com.att.dmf.mr.beans.DMaaPCambriaLimiter;
41 import com.att.dmf.mr.beans.DMaaPKafkaMetaBroker;
42 import com.att.dmf.mr.beans.DMaaPZkConfigDb;
43 import com.att.dmf.mr.constants.CambriaConstants;
44 import com.att.dmf.mr.metabroker.Broker;
45
46 import com.att.dmf.mr.metabroker.Broker1;
47 import com.att.dmf.mr.security.DMaaPAuthenticator;
48 import com.att.dmf.mr.security.impl.DMaaPOriginalUebAuthenticator;
49 import com.att.eelf.configuration.EELFLogger;
50 import com.att.eelf.configuration.EELFManager;
51 import com.att.nsa.configs.ConfigDbException;
52 import com.att.nsa.configs.confimpl.MemConfigDb;
53 import com.att.nsa.drumlin.till.nv.rrNvReadable;
54 import com.att.nsa.drumlin.till.nv.rrNvReadable.invalidSettingValue;
55 import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
56 import com.att.nsa.limits.Blacklist;
57 import com.att.nsa.security.NsaAuthenticatorService;
58
59 import com.att.nsa.security.db.BaseNsaApiDbImpl;
60 import com.att.nsa.security.db.NsaApiDb;
61 import com.att.nsa.security.db.NsaApiDb.KeyExistsException;
62 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
63 import com.att.nsa.security.db.simple.NsaSimpleApiKeyFactory;
64
65 /**
66  * Class is created for all the configuration for rest and service layer
67  * integration.
68  *
69  */
70 @Component
71 public class ConfigurationReader {
72
73
74         private Broker1 fMetaBroker;
75         private ConsumerFactory fConsumerFactory;
76         private Publisher fPublisher;
77         private MetricsSet fMetrics;
78         @Autowired
79         private DMaaPCambriaLimiter fRateLimiter;
80         private NsaApiDb<NsaSimpleApiKey> fApiKeyDb;
81         
82         private DMaaPAuthenticator<NsaSimpleApiKey> fSecurityManager;
83         private NsaAuthenticatorService<NsaSimpleApiKey> nsaSecurityManager;
84         private static CuratorFramework curator;
85         private ZkClient zk;
86         private DMaaPZkConfigDb fConfigDb;
87         private MemoryQueue q;
88         private MemoryMetaBroker mmb;
89         private Blacklist fIpBlackList;
90         private Emailer fEmailer;
91
92         private static final EELFLogger log = EELFManager.getInstance().getLogger(ConfigurationReader.class);
93         
94
95         /**
96          * constructor to initialize all the values
97          * 
98          * @param settings
99          * @param fMetrics
100          * @param zk
101          * @param fConfigDb
102          * @param fPublisher
103          * @param curator
104          * @param fConsumerFactory
105          * @param fMetaBroker
106          * @param q
107          * @param mmb
108          * @param fApiKeyDb
109          * @param fSecurityManager
110          * @throws missingReqdSetting
111          * @throws invalidSettingValue
112          * @throws ServletException
113          * @throws KafkaConsumerCacheException
114          * @throws ConfigDbException 
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 {
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)allowedtimeSkewMs= Long.parseLong(strallowedTimeSkewM);
150                                 
151         
152                 //String strrequireSecureChannel= com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,"aauthentication.requireSecureChannel");
153                 //if(strrequireSecureChannel!=null)requireSecureChannel=Boolean.parseBoolean(strrequireSecureChannel);
154                 //this.nsaSecurityManager = new NsaAuthenticatorService<NsaSimpleApiKey>(this.fApiKeyDb, settings.getLong("authentication.allowedTimeSkewMs", 600000L), settings.getBoolean("authentication.requireSecureChannel", true));
155                 //this.nsaSecurityManager = new NsaAuthenticatorService<NsaSimpleApiKey>(this.fApiKeyDb, allowedtimeSkewMs, requireSecureChannel);
156                 
157                 servletSetup();
158         }
159
160         protected void servletSetup()
161                         throws rrNvReadable.missingReqdSetting, rrNvReadable.invalidSettingValue, ServletException, ConfigDbException {
162                 try {
163
164                         fMetrics.toJson();
165                         fMetrics.setupCambriaSender();
166                         // add the admin authenticator
167                         
168                                                 final String adminSecret = com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,CambriaConstants.kSetting_AdminSecret);
169                                                 
170                                                 if ( adminSecret != null && adminSecret.length () > 0 )
171                                                 {
172                                                         try
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                                                         catch ( KeyExistsException e )
182                                                         {
183                                                                 throw new RuntimeException ( "This key can't exist in a fresh in-memory DB!", e );
184                                                         }
185                                                 }
186                                         
187                         // setup a backend
188                         //final String type = settings.getString(CambriaConstants.kBrokerType, CambriaConstants.kBrokerType_Kafka);
189                          String type = com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,CambriaConstants.kBrokerType);
190                         if (type==null) type = CambriaConstants.kBrokerType_Kafka;
191                         if (CambriaConstants.kBrokerType_Kafka.equalsIgnoreCase(type)) {
192                                 log.info("Broker Type is:" + CambriaConstants.kBrokerType_Kafka);
193                         } else if (CambriaConstants.kBrokerType_Memory.equalsIgnoreCase(type)) {
194                                 log.info("Broker Type is:" + CambriaConstants.kBrokerType_Memory);
195                                 fPublisher = new MemoryQueuePublisher(q, mmb);
196                                 //Ramkumar remove below
197                         //      fMetaBroker = mmb;
198                                 fConsumerFactory = new MemoryConsumerFactory(q);
199                         } else {
200                                 throw new IllegalArgumentException(
201                                                 "Unrecognized type for " + CambriaConstants.kBrokerType + ": " + type + ".");
202                         }
203                         fIpBlackList = new Blacklist ( getfConfigDb(), getfConfigDb().parse ( "/ipBlacklist" ) );
204                         this.fEmailer = new Emailer();
205                         log.info("Broker Type is:" + type);
206
207                 } catch (SecurityException e) {
208                         throw new ServletException(e);
209                 }
210         }
211
212         /**
213          * method returns metaBroker
214          * 
215          * @return
216          */
217         public Broker1 getfMetaBroker() {
218                 return fMetaBroker;
219         }
220
221         /**
222          * method to set the metaBroker
223          * 
224          * @param fMetaBroker
225          */
226         public void setfMetaBroker(Broker1 fMetaBroker) {
227                 this.fMetaBroker = fMetaBroker;
228         }
229
230         /**
231          * method to get ConsumerFactory Object
232          * 
233          * @return
234          */
235         public ConsumerFactory getfConsumerFactory() {
236                 return fConsumerFactory;
237         }
238
239         /**
240          * method to set the consumerfactory object
241          * 
242          * @param fConsumerFactory
243          */
244         public void setfConsumerFactory(ConsumerFactory fConsumerFactory) {
245                 this.fConsumerFactory = fConsumerFactory;
246         }
247
248         /**
249          * method to get Publisher object
250          * 
251          * @return
252          */
253         public Publisher getfPublisher() {
254                 return fPublisher;
255         }
256
257         /**
258          * method to set Publisher object
259          * 
260          * @param fPublisher
261          */
262         public void setfPublisher(Publisher fPublisher) {
263                 this.fPublisher = fPublisher;
264         }
265
266         /**
267          * method to get MetricsSet Object
268          * 
269          * @return
270          */
271         public MetricsSet getfMetrics() {
272                 return fMetrics;
273         }
274
275         /**
276          * method to set MetricsSet Object
277          * 
278          * @param fMetrics
279          */
280         public void setfMetrics(MetricsSet fMetrics) {
281                 this.fMetrics = fMetrics;
282         }
283
284         /**
285          * method to get DMaaPCambriaLimiter object
286          * 
287          * @return
288          */
289         public DMaaPCambriaLimiter getfRateLimiter() {
290                 return fRateLimiter;
291         }
292
293         /**
294          * method to set DMaaPCambriaLimiter object
295          * 
296          * @param fRateLimiter
297          */
298         public void setfRateLimiter(DMaaPCambriaLimiter fRateLimiter) {
299                 this.fRateLimiter = fRateLimiter;
300         }
301
302         /**
303          * Method to get DMaaPAuthenticator object
304          * 
305          * @return
306          */
307         public DMaaPAuthenticator<NsaSimpleApiKey> getfSecurityManager() {
308                 return fSecurityManager;
309         }
310
311         /**
312          * method to set DMaaPAuthenticator object
313          * 
314          * @param fSecurityManager
315          */
316         public void setfSecurityManager(DMaaPAuthenticator<NsaSimpleApiKey> fSecurityManager) {
317                 this.fSecurityManager = fSecurityManager;
318         }
319
320         /**
321          * method to get rrNvReadable object
322          * 
323          * @return
324          */
325         /*public rrNvReadable getSettings() {
326                 return settings;
327         }*/
328
329         /**
330          * method to set rrNvReadable object
331          * 
332          * @param settings
333          */
334         /*public void setSettings(rrNvReadable settings) {
335                 this.settings = settings;
336         }*/
337
338         /**
339          * method to get CuratorFramework object
340          * 
341          * @return
342          */
343         public static CuratorFramework getCurator() {
344                 return curator;
345         }
346
347         /**
348          * method to set CuratorFramework object
349          * 
350          * @param curator
351          */
352         public static void setCurator(CuratorFramework curator) {
353                 ConfigurationReader.curator = curator;
354         }
355
356         /**
357          * method to get ZkClient object
358          * 
359          * @return
360          */
361         public ZkClient getZk() {
362                 return zk;
363         }
364
365         /**
366          * method to set ZkClient object
367          * 
368          * @param zk
369          */
370         public void setZk(ZkClient zk) {
371                 this.zk = zk;
372         }
373
374         /**
375          * method to get DMaaPZkConfigDb object
376          * 
377          * @return
378          */
379         public DMaaPZkConfigDb getfConfigDb() {
380                 return fConfigDb;
381         }
382
383         /**
384          * method to set DMaaPZkConfigDb object
385          * 
386          * @param fConfigDb
387          */
388         public void setfConfigDb(DMaaPZkConfigDb fConfigDb) {
389                 this.fConfigDb = fConfigDb;
390         }
391
392         /**
393          * method to get MemoryQueue object
394          * 
395          * @return
396          */
397         public MemoryQueue getQ() {
398                 return q;
399         }
400
401         /**
402          * method to set MemoryQueue object
403          * 
404          * @param q
405          */
406         public void setQ(MemoryQueue q) {
407                 this.q = q;
408         }
409
410         /**
411          * method to get MemoryMetaBroker object
412          * 
413          * @return
414          */
415         public MemoryMetaBroker getMmb() {
416                 return mmb;
417         }
418
419         /**
420          * method to set MemoryMetaBroker object
421          * 
422          * @param mmb
423          */
424         public void setMmb(MemoryMetaBroker mmb) {
425                 this.mmb = mmb;
426         }
427
428         /**
429          * method to get NsaApiDb object
430          * 
431          * @return
432          */
433         public NsaApiDb<NsaSimpleApiKey> getfApiKeyDb() {
434                 return fApiKeyDb;
435         }
436
437         /**
438          * method to set NsaApiDb object
439          * 
440          * @param fApiKeyDb
441          */
442         public void setfApiKeyDb(NsaApiDb<NsaSimpleApiKey> fApiKeyDb) {
443                 this.fApiKeyDb = fApiKeyDb;
444         }
445
446         /*
447          * public DMaaPTransactionObjDB<DMaaPTransactionObj> getfTranDb() { return
448          * fTranDb; }
449          * 
450          * public void setfTranDb(DMaaPTransactionObjDB<DMaaPTransactionObj>
451          * fTranDb) { this.fTranDb = fTranDb; }
452          */
453         /**
454          * method to get the zookeeper connection String
455          * 
456          * @param settings
457          * @return
458          */
459         public static String getMainZookeeperConnectionString() {
460                 //return settings.getString(CambriaConstants.kSetting_ZkConfigDbServers,                        CambriaConstants.kDefault_ZkConfigDbServers);
461                 
462                  String zkServername = com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,CambriaConstants.kSetting_ZkConfigDbServers);
463                  if (zkServername==null) zkServername=CambriaConstants.kDefault_ZkConfigDbServers;
464                  return zkServername;
465         }
466
467         public static String getMainZookeeperConnectionSRoot(){
468                 String strVal=com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,CambriaConstants.kSetting_ZkConfigDbRoot);
469         
470                 if (null==strVal)
471                         strVal=CambriaConstants.kDefault_ZkConfigDbRoot;
472         
473                 return strVal;
474         }
475         
476         public Blacklist getfIpBlackList() {
477                 return fIpBlackList;
478         }
479
480         public void setfIpBlackList(Blacklist fIpBlackList) {
481                 this.fIpBlackList = fIpBlackList;
482         }
483
484         public NsaAuthenticatorService<NsaSimpleApiKey> getNsaSecurityManager() {
485                 return nsaSecurityManager;
486         }
487
488         public void setNsaSecurityManager(NsaAuthenticatorService<NsaSimpleApiKey> nsaSecurityManager) {
489                 this.nsaSecurityManager = nsaSecurityManager;
490         }
491         
492         public Emailer getSystemEmailer()
493           {
494             return this.fEmailer;
495           }
496
497
498 }