aboutsummaryrefslogtreecommitdiffstats
path: root/dmaap-listener/src/main/java/org/onap/ccsdk/sli/northbound/dmaapclient/SdncDmaapConsumer.java
blob: 2b416e7dbc7d1196dcdca5cc7ac1d5a939bccbcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : SDN-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 			reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.ccsdk.sli.northbound.dmaapclient;

import com.att.nsa.mr.client.MRClientFactory;
import com.att.nsa.mr.client.MRConsumer;
import com.att.nsa.mr.client.response.MRConsumerResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class SdncDmaapConsumer implements Runnable {

    private static final Logger LOG = LoggerFactory
        .getLogger(SdncDmaapConsumer.class);

    private Properties properties = null;
    private MRConsumer consumer = null;
    private MRConsumerResponse consumerResponse = null;
    private boolean running = false;
    private boolean ready = false;
    private int fetchPause = 5000; // Default pause between fetch - 5 seconds
    private int timeout = 15000; // Default timeout - 15 seconds

    public SdncDmaapConsumer() {

    }

    public SdncDmaapConsumer(Properties properties, String propertiesPath) {
        init(properties, propertiesPath);
    }

    public boolean isReady() {
        return ready;
    }

    public boolean isRunning() {
        return running;
    }

    public String getProperty(String name) {
        return properties.getProperty(name, "");
    }

    public void init(Properties properties, String propertiesPath) {

        try (FileInputStream in = new FileInputStream(new File(propertiesPath))) {

	    LOG.debug("propertiesPath: " + propertiesPath);
            this.properties = (Properties) properties.clone();
            this.properties.load(in);


            String timeoutStr = this.properties.getProperty("timeout");
	    LOG.debug("timeoutStr: " + timeoutStr);

            if ((timeoutStr != null) && (timeoutStr.length() > 0)) {
                timeout = parseTimeOutValue(timeoutStr);
            }

            String fetchPauseStr = this.properties.getProperty("fetchPause");
	    LOG.debug("fetchPause(Str): " + fetchPauseStr);
            if ((fetchPauseStr != null) && (fetchPauseStr.length() > 0)) {
                fetchPause = parseFetchPause(fetchPauseStr);
            }
	    LOG.debug("fetchPause: " + fetchPause);


            this.consumer = MRClientFactory.createConsumer(propertiesPath);
            ready = true;
        } catch (Exception e) {
            LOG.error("Error initializing DMaaP consumer from file " + propertiesPath, e);
        }
    }

    private int parseTimeOutValue(String timeoutStr) {
        try {
            return Integer.parseInt(timeoutStr);
        } catch (NumberFormatException e) {
            LOG.error("Non-numeric value specified for timeout (" + timeoutStr + ")");
        }
        return timeout;
    }

    private int parseFetchPause(String fetchPauseStr) {
        try {
            return Integer.parseInt(fetchPauseStr);
        } catch (NumberFormatException e) {
            LOG.error("Non-numeric value specified for fetchPause (" + fetchPauseStr + ")");
        }
        return fetchPause;
    }


    @Override
    public void run() {
        if (ready) {

            running = true;

            while (running) {

                try {
                    boolean noData = true;
                    consumerResponse = consumer.fetchWithReturnConsumerResponse(timeout, -1);
                    for (String msg : consumerResponse.getActualMessages()) {
                        noData = false;
                        LOG.info("Received message from DMaaP:\n" + msg);
                        processMsg(msg);
                    }

                    if (noData) {
                        pauseThread();
                    }
                } catch (Exception e) {
                    LOG.error("Caught exception reading from DMaaP", e);
                    running = false;
                }
            }
        }
    }

    private void pauseThread() throws InterruptedException {
        if (fetchPause > 0) {
            LOG.info(String.format("No data received from fetch.  Pausing %d ms before retry", fetchPause));
            Thread.sleep(fetchPause);
        } else {
            LOG.info("No data received from fetch.  No fetch pause specified - retrying immediately");
        }
    }

    abstract public void processMsg(String msg) throws InvalidMessageException;
}