[SDNC-5] Split listeners
[sdnc/northbound.git] / dmaap-listener / src / main / java / org / openecomp / sdnc / dmaapclient / DmaapListener.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdnc.dmaapclient;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.util.LinkedList;
27 import java.util.List;
28 import java.util.Properties;
29
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class DmaapListener {
34         
35         private static final String DMAAP_LISTENER_PROPERTIES = "dmaap-listener.properties";
36         private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
37         private static final Logger LOG = LoggerFactory
38                         .getLogger(DmaapListener.class);
39         
40         public static void main(String[] args) {
41                 
42                 Properties properties = new Properties();
43                 
44
45                 String propFileName = DMAAP_LISTENER_PROPERTIES;
46                 
47                 if (args.length > 0) {
48                         propFileName = args[0];
49                 }
50                 
51                 String propPath = null;
52                 String propDir = System.getenv(SDNC_CONFIG_DIR);
53                 
54                 List<SdncDmaapConsumer> consumers = new LinkedList();
55                 
56                 if (propDir == null) {
57                         
58                         propDir = "/opt/sdnc/data/properties";
59                 }
60                 
61                 if (!propFileName.startsWith("/")) {
62                         propPath = propDir + "/" + propFileName;
63                 }
64                 
65                 File propFile = new File(propPath);
66                 
67                 if (!propFile.canRead()) {
68                         LOG.error("Cannot read properties file "+propPath);
69                         System.exit(1);
70                 }
71                 
72                 try {
73                         properties.load(new FileInputStream(propFile));
74                 } catch (Exception e) {
75                         LOG.error("Caught exception loading properties from "+propPath, e);
76                         System.exit(1);
77                 }
78                 
79                 String subscriptionStr = properties.getProperty("subscriptions");
80                 
81                 boolean threadsRunning = false;
82                 
83                 LOG.debug("Dmaap subscriptions : "+subscriptionStr);
84                 
85                 if (subscriptionStr != null) {
86                         String[] subscriptions = subscriptionStr.split(";");
87                         
88                         for (int i = 0; i < subscriptions.length; i++) {
89                                 String[] subscription = subscriptions[i].split(":");
90                                 String consumerClassName = subscription[0];
91                                 String propertyPath = subscription[1];
92
93                                 LOG.debug("Handling subscription [" + consumerClassName + "," + propertyPath + "]");
94
95                                 if (propertyPath == null) {
96                                         LOG.error("Invalid subscription (" + subscriptions[i] + ") property file missing");
97                                         continue;
98                                 }
99
100                                 if (!propertyPath.startsWith("/")) {
101                                         propertyPath = propDir + "/" + propertyPath;
102                                 }
103
104                                 Class<?> consumerClass = null;
105
106                                 try {
107                                         consumerClass = Class.forName(consumerClassName);
108                                 } catch (Exception e) {
109                                         LOG.error("Could not find DMaap consumer class " + consumerClassName);
110                                 }
111
112                                 if (consumerClass != null) {
113
114                                         SdncDmaapConsumer consumer = null;
115
116                                         try {
117                                                 consumer = (SdncDmaapConsumer) consumerClass.newInstance();
118                                         } catch (Exception e) {
119                                                 LOG.error("Could not create consumer from class " + consumerClassName, e);
120                                         }
121
122                                         if (consumer != null) {
123                                                 LOG.debug("Initializing consumer " + consumerClassName + "(" + propertyPath + ")");
124                                                 consumer.init(properties, propertyPath);
125
126                                                 if (consumer.isReady()) {
127                                                         Thread consumerThread = new Thread(consumer);
128                                                         consumerThread.start();
129                                                         consumers.add(consumer);
130                                                         threadsRunning = true;
131                                                         LOG.info("Started consumer thread (" + consumerClassName + " : " + propertyPath + ")");
132                                                 } else {
133                                                         LOG.debug("Consumer " + consumerClassName + " is not ready");
134                                                 }
135                                         }
136
137                                 }
138
139                         }
140                 }
141                 
142                 while (threadsRunning) {
143                         
144                         threadsRunning = false;
145                         for (SdncDmaapConsumer consumer : consumers) {
146                                 if (consumer.isRunning()) {
147                                         threadsRunning = true;
148                                 }
149                         }
150                         
151                         if (!threadsRunning) {
152                                 break;
153                         }
154                         
155                         try {
156                                 Thread.sleep(10000);
157                         } catch (InterruptedException e) {
158                                 
159                         }
160                 }
161                 
162                 LOG.info("No listener threads running - exitting");
163
164         }
165 }