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