aboutsummaryrefslogtreecommitdiffstats
path: root/event-client-dmaap/src/main/java/org/onap/aai/event/client/DMaaPEventPublisher.java
blob: 709f5f733f9c25908bfb3255b27076526839dd2c (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 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=========================================================
 */
package org.onap.aai.event.client;

import com.att.nsa.mr.client.MRPublisher.message;
import com.att.nsa.mr.client.impl.MRConsumerImpl;
import com.att.nsa.mr.client.impl.MRSimplerBatchPublisher;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.onap.aai.cl.api.Logger;
import org.onap.aai.cl.eelf.LoggerFactory;
import org.onap.aai.event.api.EventPublisher;

/**
 * Event Bus Client publisher API that uses AAF authentication with Username/Password.
 */
public class DMaaPEventPublisher implements EventPublisher {

    public static final String DEFAULT_TRANSPORT_TYPE = "HTTPAAF";
    public static final String DEFAULT_PROTOCOL = "http";
    public static final String DEFAULT_CONTENT_TYPE = "application/json";
    public static final int DEFAULT_BATCH_SIZE = 100;
    public static final long DEFAULT_BATCH_AGE = 250;
    public static final int DEFAULT_BATCH_DELAY = 50;
    public static final String DEFAULT_PARTITION = "0";
    public static final int CLOSE_TIMEOUT = 20;

    private static final String ASYNC_UNSUPPORTED =
            "This implementation of EventPublisher does not support async mode.";

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

    public interface MRPublisherFactory {
        public MRSimplerBatchPublisher createPublisher(String host, String topic, int maxBatchSize, long maxAgeMs,
                int delayBetweenBatchesMs);
    }

    private static MRPublisherFactory publisherFactory = DMaaPEventPublisher::createMRSimplerBatchPublisher;

    private MRSimplerBatchPublisher publisher;

    /**
     * Replace the publisher factory (intended to be used for testing purposes only).
     * 
     * @param publisherFactory
     */
    static void setPublisherFactory(MRPublisherFactory publisherFactory) {
        DMaaPEventPublisher.publisherFactory = publisherFactory;
    }

    /**
     * Provide the default factory method so that test code is able to restore this functionality.
     * 
     * @return the default publisher factory implementation
     */
    static MRPublisherFactory getPublisherFactory() {
        return publisherFactory;
    }

    /**
     * Creates a new instance of MRSimplerBatchPublisher using the supplied parameters.
     * 
     * @param host The host and port of the DMaaP server in the format <b>host:port</b>
     * @param topic The name of the topic to which messages are published
     * @param maxBatchSize The maximum batch size for each send operation
     * @param maxAgeMs The maximum age of each batch before sending
     * @param delayBetweenBatchesMs Time to wait between sending each batch
     * @return a new MRSimplerBatchPublisher object
     */
    private static MRSimplerBatchPublisher createMRSimplerBatchPublisher(String host, String topic, int maxBatchSize,
            long maxAgeMs, int delayBetweenBatchesMs) {
        return new MRSimplerBatchPublisher.Builder().againstUrls(MRConsumerImpl.stringToList(host)).onTopic(topic)
                .batchTo(maxBatchSize, maxAgeMs).httpThreadTime(delayBetweenBatchesMs).build();
    }

    /**
     * Creates a new instance of DMaaPEventPublisher 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 publish to
     * @param username The username of the client application
     * @param password The password for the username
     * @param maxBatchSize The maximum batch size for each send
     * @param maxAgeMs The max age of each batch in ms before sending
     * @param delayBetweenBatchesMs Time in ms to wait between sending each batch
     * @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 contentType The content-type request header value (e.g. application/json)
     */
    public DMaaPEventPublisher(String host, String topic, String username, String password, int maxBatchSize,
            long maxAgeMs, int delayBetweenBatchesMs, String transportType, String protocol, String contentType) {
        publisher = getPublisherFactory().createPublisher(host, topic, maxBatchSize, maxAgeMs, delayBetweenBatchesMs);
        publisher.setUsername(username);
        publisher.setPassword(password);
        publisher.setProtocolFlag(transportType);

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

    /**
     * Creates a new instance of DMaapEventPublisherAAF 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 publish to
     * @param username The username of the client application
     * @param password The password for the username
     * @param maxBatchSize The maximum batch size for each send
     * @param maxAgeMs The max age of each batch in ms before sending
     * @param delayBetweenBatchesMs Time in ms to wait between sending each batch
     * @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>
     */
    public DMaaPEventPublisher(String host, String topic, String username, String password, int maxBatchSize,
            long maxAgeMs, int delayBetweenBatchesMs, String transportType) {
        this(host, topic, username, password, maxBatchSize, maxAgeMs, delayBetweenBatchesMs, transportType,
                DEFAULT_PROTOCOL, DEFAULT_CONTENT_TYPE);
    }

    /**
     * Creates a new instance of DMaapEventPublisherAAF 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 publish to
     * @param username The username of the client application
     * @param password The password for the username
     */
    public DMaaPEventPublisher(String host, String topic, String username, String password) {
        this(host, topic, username, password, DEFAULT_BATCH_SIZE, DEFAULT_BATCH_AGE, DEFAULT_BATCH_DELAY,
                DEFAULT_TRANSPORT_TYPE, DEFAULT_PROTOCOL, DEFAULT_CONTENT_TYPE);
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.aai.event.api.EventPublisher#close()
     */
    @Override
    public void close() throws Exception {
        publisher.close(CLOSE_TIMEOUT, TimeUnit.SECONDS);
    }

    /**
     * Close the publisher and return a list of unsent messages.
     *
     * @return a list of unsent messages.
     * @throws Exception
     */
    public List<String> closeWithUnsent() throws Exception {
        return publisher.close(CLOSE_TIMEOUT, TimeUnit.SECONDS).stream().map(m -> m.fMsg).collect(Collectors.toList());
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.aai.event.api.EventPublisher#sendAsync(java.lang.String)
     */
    @Override
    public void sendAsync(String message) throws Exception {
        throw new UnsupportedOperationException(ASYNC_UNSUPPORTED);
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.aai.event.api.EventPublisher#sendAsync(java.util.Collection)
     */
    @Override
    public void sendAsync(Collection<String> messages) throws Exception {
        throw new UnsupportedOperationException(ASYNC_UNSUPPORTED);

    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.aai.event.api.EventPublisher#sendAsync(java.lang.String, java.lang.String)
     */
    @Override
    public void sendAsync(String partition, String message) throws Exception {
        throw new UnsupportedOperationException(ASYNC_UNSUPPORTED);
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.aai.event.api.EventPublisher#sendAsync(java.lang.String, java.util.Collection)
     */
    @Override
    public void sendAsync(String partition, Collection<String> messages) throws Exception {
        throw new UnsupportedOperationException(ASYNC_UNSUPPORTED);
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.aai.event.api.EventPublisher#sendSync(java.lang.String)
     */
    @Override
    public int sendSync(String message) throws Exception {
        return sendSync(DEFAULT_PARTITION, message);
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.aai.event.api.EventPublisher#sendSync(java.util.Collection)
     */
    @Override
    public int sendSync(Collection<String> messages) throws Exception {
        return sendSync(DEFAULT_PARTITION, messages);
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.aai.event.api.EventPublisher#sendSync(java.lang.String, java.lang.String)
     */
    @Override
    public int sendSync(String partition, String message) throws Exception {
        log.debug("Publishing message on partition " + partition + ": " + message);

        publisher.getProps().put("partition", partition);
        return publisher.send(partition, message);
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.aai.event.api.EventPublisher#sendSync(java.lang.String, java.util.Collection)
     */
    @Override
    public int sendSync(String partition, Collection<String> messages) throws Exception {
        log.debug("Publishing " + messages.size() + " messages on partition " + partition);

        publisher.getProps().put("partition", partition);

        Collection<message> dmaapMessages = new ArrayList<>();
        for (String message : messages) {
            dmaapMessages.add(new message(partition, message));
        }
        return publisher.send(dmaapMessages);
    }

}