42ae1a21a8f4b1c6bf6ec66b04002c8a49637d7e
[sdnc/northbound.git] / dmaap-listener / src / main / java / org / openecomp / sdnc / dmaapclient / SdncDmaapConsumer.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.Properties;
27
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.att.nsa.mr.client.MRClientFactory;
32 import com.att.nsa.mr.client.MRConsumer;
33 import com.att.nsa.mr.client.response.MRConsumerResponse;
34
35 public abstract class SdncDmaapConsumer implements Runnable {
36
37         private static final Logger LOG = LoggerFactory
38                         .getLogger(SdncDmaapConsumer.class);
39
40         private String propertiesPath = "";
41         private Properties properties = null;
42         MRConsumer consumer = null;
43         MRConsumerResponse consumerResponse = null;
44         boolean running = false;
45         boolean ready = false;
46         int fetchPause = 5000; // Default pause between fetchs - 5 seconds
47
48         public boolean isReady() {
49                 return ready;
50         }
51
52         int timeout = 15000; // Default timeout - 15 seconds
53
54         public boolean isRunning() {
55                 return running;
56         }
57
58         public SdncDmaapConsumer() {
59
60         }
61
62         public SdncDmaapConsumer(Properties properties, String propertiesPath) {
63                 init(properties, propertiesPath);
64         }
65
66         public String getProperty(String name) {
67                 return(properties.getProperty(name, ""));
68         }
69
70         public void init(Properties properties, String propertiesPath) {
71
72                 this.propertiesPath = propertiesPath;
73
74                 try {
75
76                         this.properties = (Properties) properties.clone();
77
78                         this.properties.load(new FileInputStream(new File(propertiesPath)));
79
80                         String timeoutStr = properties.getProperty("timeout");
81
82                         if ((timeoutStr != null) && (timeoutStr.length() > 0)) {
83                                 try {
84                                         timeout = Integer.parseInt(timeoutStr);
85                                 } catch (NumberFormatException e) {
86                                         LOG.error("Non-numeric value specified for timeout ("+timeoutStr+")");
87                                 }
88                         }
89
90                         String fetchPauseStr = properties.getProperty("fetchPause");
91                         if ((fetchPauseStr != null) && (fetchPauseStr.length() > 0)) {
92                                 try {
93                                         fetchPause = Integer.parseInt(fetchPauseStr);
94                                 } catch (NumberFormatException e) {
95                                         LOG.error("Non-numeric valud specified for fetchPause ("+fetchPauseStr+")");
96                                 }
97                         }
98
99                         this.consumer = MRClientFactory.createConsumer(propertiesPath);
100                         ready = true;
101                 } catch (Exception e) {
102                         LOG.error("Error initializing DMaaP consumer from file "+propertiesPath, e);
103                 }
104         }
105
106
107         @Override
108         public void run() {
109                 if (ready) {
110
111                         running = true;
112
113                         while (running) {
114
115                                 try {
116                                         boolean noData = true;
117                                         consumerResponse = consumer.fetchWithReturnConsumerResponse(timeout, -1);
118                                         for (String msg : consumerResponse.getActualMessages()) {
119                                                 noData = false;
120                                                 LOG.info("Received message from DMaaP:\n"+msg);
121                                                 processMsg(msg);
122                                         }
123
124                                         if (noData) {
125                                                 if (fetchPause > 0) {
126
127                                                         LOG.info("No data received from fetch.  Pausing "+fetchPause+" ms before retry");
128                                                         Thread.sleep(fetchPause);
129                                                 } else {
130
131                                                         LOG.info("No data received from fetch.  No fetch pause specified - retrying immediately");
132                                                 }
133                                         }
134                                 } catch (Exception e) {
135                                         LOG.error("Caught exception reading from DMaaP", e);
136                                         running = false;
137                                 }
138
139
140                         }
141                 }
142
143         }
144
145         abstract public void processMsg(String msg) throws InvalidMessageException;
146 }