aboutsummaryrefslogtreecommitdiffstats
path: root/event-client-dmaap/src/main/java/org/onap/aai/event/client/DMaaPEventConsumer.java
blob: dab85dc246194b28dd4dbe635d514c2b5138c0e7 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017 European Software Marketing Ltd.
 * ================================================================================
 * 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=========================================================
 *
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */
/**
 *
 */
package org.onap.aai.event.client;

import com.att.nsa.mr.client.impl.MRConsumerImpl;
import java.net.MalformedURLException;
import java.util.List;
import java.util.Properties;
import javax.naming.OperationNotSupportedException;
import org.onap.aai.cl.api.Logger;
import org.onap.aai.cl.eelf.LoggerFactory;
import org.onap.aai.event.api.EventConsumer;
import org.onap.aai.event.api.MessageWithOffset;

/**
 * Event Bus Client consumer API that uses AAF authentication with Username/Password.
 */
public class DMaaPEventConsumer implements EventConsumer {

    public static final String DEFAULT_TRANSPORT_TYPE = "HTTPAAF";
    public static final String DEFAULT_PROTOCOL = "http";
    public static final int DEFAULT_MESSAGE_WAIT_TIMEOUT = 15000;
    public static final int DEFAULT_MESSAGE_LIMIT = 1000;

    private static final String OFFSET_UNSUPPORTED = "DMaaP does not support consuming with offsets.";

    private static Logger log = LoggerFactory.getInstance().getLogger(DMaaPEventConsumer.class);

    public interface MRConsumerFactory {
        public MRConsumerImpl createConsumer(List<String> hosts, String topic, String consumerGroup, String consumerId,
                int timeoutMs, int messageLimit, String filter, String username, String password)
                throws MalformedURLException;
    }

    private static MRConsumerFactory consumerFactory = MRConsumerImpl::new;

    private MRConsumerImpl consumer;

    /**
     * Replace the consumer factory (intended to be used for testing purposes only).
     * 
     * @param consumerFactory
     */
    static void setConsumerFactory(MRConsumerFactory consumerFactory) {
        DMaaPEventConsumer.consumerFactory = consumerFactory;
    }

    /**
     * Creates a new instance of DMaaPEventConsumerAAF using supplied parameters.
     *
     * @param host The host and port of the DMaaP server in the format <b>host:port</b>
     * @param topic The topic name to consume from
     * @param username The username of the client application
     * @param password The password for the username
     * @param consumerGroup The consumer group to consume from
     * @param consumerId The consumer ID of the client
     * @param timeoutMs Time in ms to wait for messages on the server before returning
     * @param messageLimit Maximum number of messages that is returned per fetch
     * @param transportType Specifies the request header type used in the request to DMaaP server.<br>
     *        Valid types:
     *        <li>DME2</li>
     *        <li>HTTPAAF</li>
     *        <li>HTTPAUTH</li>
     *        <li>HTTPNOAUTH</li>
     * @param protocol The http protocol to use (http/https)
     * @param filter A customizable message filter, or null if no filtering is required
     * @throws MalformedURLException
     */
    public DMaaPEventConsumer(String host, String topic, String username, String password, String consumerGroup,
            String consumerId, int timeoutMs, int messageLimit, String transportType, String protocol, String filter)
            throws MalformedURLException {
        consumer = consumerFactory.createConsumer(MRConsumerImpl.stringToList(host), topic, consumerGroup, consumerId,
                timeoutMs, messageLimit, filter, username, password);
        consumer.setHost(host);
        consumer.setUsername(username);
        consumer.setPassword(password);
        consumer.setProtocolFlag(transportType);

        // MRConsumerImpl still needs extra properties from the prop object.
        Properties extraProps = new Properties();
        extraProps.put("Protocol", protocol);
        consumer.setProps(extraProps);
    }

    /**
     * Creates a new instance of DMaaPEventConsumerAAF using supplied parameters and default values.
     *
     * @param host The host and port of the DMaaP server in the format <b>host:port</b>
     * @param topic The topic name to consume from
     * @param username The username of the client application
     * @param password The password for the username
     * @param consumerGroup The consumer group to consume from
     * @param consumerId The consumer ID of the client
     * @param timeoutMs Time in ms to wait for messages on the server before returning
     * @param messageLimit Maximum number of messages that is returned per fetch
     * @param transportType Specifies the request header type used in the request to DMaaP server.<br>
     *        Valid types:
     *        <li>DME2</li>
     *        <li>HTTPAAF</li>
     *        <li>HTTPAUTH</li>
     *        <li>HTTPNOAUTH</li>
     * @throws MalformedURLException
     */
    public DMaaPEventConsumer(String host, String topic, String username, String password, String consumerGroup,
            String consumerId, int timeoutMs, int messageLimit, String transportType) throws MalformedURLException {
        this(host, topic, username, password, consumerGroup, consumerId, timeoutMs, messageLimit, transportType,
                DEFAULT_PROTOCOL, null);
    }

    /**
     * Creates a new instance of DMaaPEventConsumerAAF using supplied parameters and default values.
     *
     * @param host The host and port of the DMaaP server in the format <b>host:port</b>
     * @param topic The topic name to consume from
     * @param username The username of the client application
     * @param password The password for the username
     * @param consumerGroup The consumer group to consume from
     * @param consumerId The consumer ID of the client
     * @throws MalformedURLException
     */
    public DMaaPEventConsumer(String host, String topic, String username, String password, String consumerGroup,
            String consumerId) throws MalformedURLException {
        this(host, topic, username, password, consumerGroup, consumerId, DEFAULT_MESSAGE_WAIT_TIMEOUT,
                DEFAULT_MESSAGE_LIMIT, DEFAULT_TRANSPORT_TYPE, DEFAULT_PROTOCOL, null);
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.aai.event.api.EventConsumer#consumeAndCommit()
     */
    @Override
    public Iterable<String> consumeAndCommit() throws Exception {
        log.debug("Querying Event Bus for messages.");
        return consumer.fetch();
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.aai.event.api.EventConsumer#consume()
     */
    @Override
    public Iterable<String> consume() throws Exception {
        return consumeAndCommit();
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.aai.event.api.EventConsumer#consumeWithOffsets()
     */
    @Override
    public Iterable<MessageWithOffset> consumeWithOffsets() throws Exception {
        throw new OperationNotSupportedException(OFFSET_UNSUPPORTED);
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.aai.event.api.EventConsumer#commitOffsets()
     */
    @Override
    public void commitOffsets() throws Exception {
        throw new OperationNotSupportedException(OFFSET_UNSUPPORTED);
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.aai.event.api.EventConsumer#commitOffsets(long)
     */
    @Override
    public void commitOffsets(long offset) throws Exception {
        throw new OperationNotSupportedException(OFFSET_UNSUPPORTED);
    }

}