2 * ================================================================================
3 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
20 package org.onap.dcae.analytics.web.util.function;
23 import static org.onap.dcae.analytics.web.util.AnalyticsWebUtils.RANDOM_ID_SUPPLIER;
25 import java.net.MalformedURLException;
27 import java.util.LinkedList;
28 import java.util.List;
29 import java.util.function.Function;
30 import java.util.stream.Collectors;
31 import java.util.stream.Stream;
33 import org.onap.dcae.analytics.model.DmaapMrConstants;
34 import org.onap.dcae.analytics.web.dmaap.MrSubscriberPreferences;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.web.util.UriComponentsBuilder;
40 * Creates DMaaP MR Subscriber URLs from {@link MrSubscriberPreferences}
42 * @author Rajiv Singla
44 public class MrSubscriberURLFunction implements Function<MrSubscriberPreferences, List<URL>> {
46 private static final Logger logger = LoggerFactory.getLogger(MrSubscriberURLFunction.class);
47 private static final String URL_PATH_SEPARATOR = "/";
50 public List<URL> apply(final MrSubscriberPreferences subscriberConfig) {
52 final List<URL> subscriberURLs = new LinkedList<>();
54 // if consumer ids is not present generate single random consumer id
55 final List<String> consumerIds = subscriberConfig.getConsumerIds() != null ?
56 subscriberConfig.getConsumerIds() : Stream.of(RANDOM_ID_SUPPLIER.get()).collect(Collectors.toList());
58 for (final String consumerId : consumerIds) {
60 // request url must be present
61 final String requestURL = subscriberConfig.getRequestURL();
63 // generate random consumer group if not present
64 final String consumerGroup = subscriberConfig.getConsumerGroup() != null ?
65 subscriberConfig.getConsumerGroup() :
66 DmaapMrConstants.SUBSCRIBER_RANDOM_CONSUMER_GROUP_PREFIX + RANDOM_ID_SUPPLIER.get();
68 // set default message limit if not present
69 final Integer messageLimit = subscriberConfig.getMessageLimit() != null ?
70 subscriberConfig.getMessageLimit() : DmaapMrConstants.SUBSCRIBER_DEFAULT_MESSAGE_LIMIT;
72 // set default timeout if not present
73 final Integer timeout = subscriberConfig.getTimeout() != null ?
74 subscriberConfig.getTimeout() : DmaapMrConstants.SUBSCRIBER_DEFAULT_TIMEOUT;
76 final UriComponentsBuilder componentsBuilder = UriComponentsBuilder
77 .fromHttpUrl(requestURL)
78 .path(URL_PATH_SEPARATOR + consumerGroup + URL_PATH_SEPARATOR + consumerId);
80 if (messageLimit != null && messageLimit >= 1) {
82 .queryParam(DmaapMrConstants.SUBSCRIBER_MSG_LIMIT_QUERY_PARAM_NAME, messageLimit);
85 if (timeout != null && timeout >= 1) {
87 .queryParam(DmaapMrConstants.SUBSCRIBER_TIMEOUT_QUERY_PARAM_NAME, timeout);
90 subscriberURLs.add(createURL(componentsBuilder));
94 return subscriberURLs;
97 private URL createURL(final UriComponentsBuilder uriComponentsBuilder) {
99 final URL subscriberURL = uriComponentsBuilder.build().toUri().toURL();
100 logger.info("Created DMaaP MR Subscriber URL: {}", subscriberURL);
101 return subscriberURL;
102 } catch (MalformedURLException e) {
103 throw new IllegalStateException("Unable to build DMaaP MR Subscriber URL", e);