1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright © 2018 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 *******************************************************************************/
24 package org.onap.dmaap.mr.client;
26 import java.net.MalformedURLException;
27 import java.util.Collection;
28 import java.util.TreeSet;
29 import java.util.UUID;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import org.onap.dmaap.mr.client.impl.MRConsumerImpl;
35 import org.onap.dmaap.mr.client.impl.MRMetaClient;
36 import org.onap.dmaap.mr.client.impl.MRSimplerBatchPublisher;
39 * A collection of builders for various types of MR API clients
43 public class MRClientBuilders
47 * Instantiates MRClientBuilders.
49 private MRClientBuilders() {
50 // prevent instantiation
54 * A builder for a topic Consumer
57 public static class ConsumerBuilder
60 * Construct a consumer builder.
62 public ConsumerBuilder () {}
66 * @param hostList a comma-separated list of hosts to use to connect to MR
67 * @return this builder
69 public ConsumerBuilder usingHosts ( String hostList ) {
70 return usingHosts ( MRConsumerImpl.stringToList(hostList) );
75 * @param hostSet a set of hosts to use to connect to MR
76 * @return this builder
78 public ConsumerBuilder usingHosts ( Collection<String> hostSet ) {
79 fHosts = hostSet; return this;
84 * @param topic the name of the topic to consume
85 * @return this builder
87 public ConsumerBuilder onTopic ( String topic ) {
93 * Set the consumer's group and ID
94 * @param consumerGroup The name of the consumer group this consumer is part of
95 * @param consumerId The unique id of this consumer in its group
96 * @return this builder
98 public ConsumerBuilder knownAs ( String consumerGroup, String consumerId ) {
99 fGroup = consumerGroup;
105 * Set the API key and secret for this client.
108 * @return this builder
110 public ConsumerBuilder authenticatedBy ( String apiKey, String apiSecret ) {
112 fApiSecret = apiSecret;
117 * Set the server side timeout
118 * @param timeoutMs The amount of time in milliseconds that the server should keep the connection open while waiting for message traffic.
119 * @return this builder
121 public ConsumerBuilder waitAtServer ( int timeoutMs ) {
122 fTimeoutMs = timeoutMs;
127 * Set the maximum number of messages to receive per transaction
128 * @param limit The maximum number of messages to receive from the server in one transaction.
129 * @return this builder
131 public ConsumerBuilder receivingAtMost ( int limit ) {
137 * Set a filter to use on the server
138 * @param filter a Highland Park standard library filter encoded in JSON
139 * @return this builder
141 public ConsumerBuilder withServerSideFilter ( String filter ) {
150 public MRConsumer build ()
152 if ( fHosts == null || fHosts.size() == 0 || fTopic == null )
154 throw new IllegalArgumentException ( "You must provide at least one host and a topic name." );
157 if ( fGroup == null )
159 fGroup = UUID.randomUUID ().toString ();
161 log.info ( "Creating non-restartable client with group " + fGroup + " and ID " + fId + "." );
164 if ( sfConsumerMock != null ) return sfConsumerMock;
166 return new MRConsumerImpl ( fHosts, fTopic, fGroup, fId, fTimeoutMs, fLimit, fFilter, fApiKey, fApiSecret );
167 } catch (MalformedURLException e) {
168 throw new IllegalArgumentException(e);
172 private Collection<String> fHosts = null;
173 private String fTopic = null;
174 private String fGroup = null;
175 private String fId = null;
176 private String fApiKey = null;
177 private String fApiSecret = null;
178 private int fTimeoutMs = -1;
179 private int fLimit = -1;
180 private String fFilter = null;
183 /*************************************************************************/
184 /*************************************************************************/
185 /*************************************************************************/
188 * A publisher builder
191 public static class PublisherBuilder
193 public PublisherBuilder () {}
196 * Set the MR/UEB host(s) to use
197 * @param hostlist The host(s) used in the URL to MR. Can be "host:port", can be multiple comma-separated entries.
198 * @return this builder
200 public PublisherBuilder usingHosts ( String hostlist ) {
201 return usingHosts ( MRConsumerImpl.stringToList(hostlist) );
205 * Set the MR/UEB host(s) to use
206 * @param hostSet The host(s) used in the URL to MR. Can be "host:port"
207 * @return this builder
209 public PublisherBuilder usingHosts ( String[] hostSet )
211 final TreeSet<String> hosts = new TreeSet<String> ();
212 for ( String hp : hostSet )
216 return usingHosts ( hosts );
220 * Set the MR/UEB host(s) to use
221 * @param hostlist The host(s) used in the URL to MR. Can be "host:port".
222 * @return this builder
224 public PublisherBuilder usingHosts ( Collection<String> hostlist ) {
230 * Set the topic to publish on
231 * @param topic The topic on which to publish messages.
232 * @return this builder
234 public PublisherBuilder onTopic ( String topic ) {
240 * Batch message sends with the given limits.
241 * @param messageCount The largest set of messages to batch.
242 * @param ageInMs The maximum age of a message waiting in a batch.
243 * @return this builder
245 public PublisherBuilder limitBatch ( int messageCount, int ageInMs ) {
246 fMaxBatchSize = messageCount;
247 fMaxBatchAgeMs = ageInMs;
252 * Compress transactions
253 * @return this builder
255 public PublisherBuilder withCompresion () {
256 return enableCompresion(true);
260 * Do not compress transactions
261 * @return this builder
263 public PublisherBuilder withoutCompresion () {
264 return enableCompresion(false);
268 * Set the compression option
269 * @param compress true to gzip compress transactions
270 * @return this builder
272 public PublisherBuilder enableCompresion ( boolean compress ) {
273 fCompress = compress;
278 * Set the API key and secret for this client.
281 * @return this builder
283 public PublisherBuilder authenticatedBy ( String apiKey, String apiSecret ) {
285 fApiSecret = apiSecret;
290 * Build the publisher
291 * @return a batching publisher
293 public MRBatchingPublisher build ()
295 if ( fHosts == null || fHosts.isEmpty() || fTopic == null )
297 throw new IllegalArgumentException ( "You must provide at least one host and a topic name." );
300 if ( sfPublisherMock != null ) return sfPublisherMock;
302 final MRSimplerBatchPublisher pub = new MRSimplerBatchPublisher.Builder ().
303 againstUrls ( fHosts ).
305 batchTo ( fMaxBatchSize, fMaxBatchAgeMs ).
306 compress ( fCompress ).
308 if ( fApiKey != null )
310 pub.setApiCredentials ( fApiKey, fApiSecret );
315 private Collection<String> fHosts = null;
316 private String fTopic = null;
317 private int fMaxBatchSize = 1;
318 private int fMaxBatchAgeMs = 1;
319 private boolean fCompress = false;
320 private String fApiKey = null;
321 private String fApiSecret = null;
325 * A builder for an identity manager
328 public static class IdentityManagerBuilder extends AbstractAuthenticatedManagerBuilder<MRIdentityManager>
331 * Construct an identity manager builder.
333 public IdentityManagerBuilder () {}
336 protected MRIdentityManager constructClient ( Collection<String> hosts ) { try {
337 return new MRMetaClient ( hosts );
338 } catch (MalformedURLException e) {
339 throw new IllegalArgumentException(e);
344 * A builder for a topic manager
347 public static class TopicManagerBuilder extends AbstractAuthenticatedManagerBuilder<MRTopicManager>
350 * Construct an topic manager builder.
352 public TopicManagerBuilder () {}
355 protected MRTopicManager constructClient ( Collection<String> hosts ) { try {
356 return new MRMetaClient ( hosts );
357 } catch (MalformedURLException e) {
358 throw new IllegalArgumentException(e);
363 * Inject a consumer. Used to support unit tests.
366 public static void $testInject ( MRConsumer cc )
372 * Inject a publisher. Used to support unit tests.
375 public static void $testInject ( MRBatchingPublisher pub )
377 sfPublisherMock = pub;
380 static MRConsumer sfConsumerMock = null;
381 static MRBatchingPublisher sfPublisherMock = null;
384 * A builder for an identity manager
387 public static abstract class AbstractAuthenticatedManagerBuilder<T extends MRClient>
390 * Construct an identity manager builder.
392 public AbstractAuthenticatedManagerBuilder () {}
396 * @param hostList a comma-separated list of hosts to use to connect to MR
397 * @return this builder
399 public AbstractAuthenticatedManagerBuilder<T> usingHosts ( String hostList ) {
400 return usingHosts ( MRConsumerImpl.stringToList(hostList) );
405 * @param hostSet a set of hosts to use to connect to MR
406 * @return this builder
408 public AbstractAuthenticatedManagerBuilder<T> usingHosts ( Collection<String> hostSet ) {
414 * Set the API key and secret for this client.
417 * @return this builder
419 public AbstractAuthenticatedManagerBuilder<T> authenticatedBy ( String apiKey, String apiSecret ) {
421 fApiSecret = apiSecret;
431 if ( fHosts.isEmpty() )
433 throw new IllegalArgumentException ( "You must provide at least one host and a topic name." );
436 final T mgr = constructClient ( fHosts );
437 mgr.setApiCredentials ( fApiKey, fApiSecret );
441 protected abstract T constructClient ( Collection<String> hosts );
443 private Collection<String> fHosts = null;
444 private String fApiKey = null;
445 private String fApiSecret = null;
448 private static final Logger log = LoggerFactory.getLogger ( MRClientBuilders.class );