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