2 * ===============================LICENSE_START======================================
\r
4 * ================================================================================
\r
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============================LICENSE_END===========================================
\r
21 package org.openecomp.dcae.apod.analytics.dmaap.domain.config;
\r
23 import com.google.common.base.Objects;
\r
24 import org.openecomp.dcae.apod.analytics.common.AnalyticsConstants;
\r
26 import java.util.UUID;
\r
28 import javax.annotation.Nonnull;
\r
32 * Immutable DMaaP MR Configuration for Subscriber.
\r
34 * Use {@link DMaaPMRSubscriberConfig.Builder} to construct Subscriber Configuration
\r
37 * @author Rajiv Singla . Creation Date: 10/12/2016.
\r
39 public final class DMaaPMRSubscriberConfig extends DMaaPMRBaseConfig {
\r
41 private final String consumerId;
\r
42 private final String consumerGroup;
\r
43 private final Integer timeoutMS;
\r
44 private final Integer messageLimit;
\r
46 private DMaaPMRSubscriberConfig(@Nonnull String hostName,
\r
47 @Nonnull Integer portNumber,
\r
48 @Nonnull String topicName,
\r
49 @Nonnull String protocol,
\r
51 String userPassword,
\r
52 @Nonnull String contentType,
\r
53 @Nonnull String consumerId,
\r
54 @Nonnull String consumerGroup,
\r
55 @Nonnull Integer timeoutMS,
\r
56 @Nonnull Integer messageLimit) {
\r
57 this.hostName = hostName;
\r
58 this.portNumber = portNumber;
\r
59 this.topicName = topicName;
\r
60 this.protocol = protocol;
\r
61 this.userName = userName;
\r
62 this.userPassword = userPassword;
\r
63 this.contentType = contentType;
\r
64 this.consumerId = consumerId;
\r
65 this.consumerGroup = consumerGroup;
\r
66 this.timeoutMS = timeoutMS;
\r
67 this.messageLimit = messageLimit;
\r
71 * Builder to initialize immutable {@link DMaaPMRSubscriberConfig} object
\r
73 public static class Builder {
\r
75 private String hostName;
\r
76 private Integer portNumber;
\r
77 private String topicName;
\r
78 private String userName;
\r
79 private String userPassword;
\r
80 private String protocol;
\r
81 private String contentType;
\r
82 private String consumerId;
\r
83 private String consumerGroup;
\r
84 private Integer timeoutMS;
\r
85 private Integer messageLimit;
\r
87 public Builder(@Nonnull String hostName,
\r
88 @Nonnull String topicName) {
\r
90 this.hostName = hostName;
\r
91 this.topicName = topicName;
\r
94 this.portNumber = AnalyticsConstants.DEFAULT_PORT_NUMBER;
\r
95 this.userName = AnalyticsConstants.DEFAULT_USER_NAME;
\r
96 this.userPassword = AnalyticsConstants.DEFAULT_USER_PASSWORD;
\r
97 this.protocol = AnalyticsConstants.DEFAULT_PROTOCOL;
\r
98 this.contentType = AnalyticsConstants.DEFAULT_CONTENT_TYPE;
\r
99 this.consumerId = UUID.randomUUID().toString(); // consumer is assigned a random id by default
\r
100 this.consumerGroup = AnalyticsConstants.DEFAULT_SUBSCRIBER_GROUP_PREFIX + consumerId; // random group
\r
101 this.timeoutMS = AnalyticsConstants.DEFAULT_SUBSCRIBER_TIMEOUT_MS; // defaults to 10ms timeout
\r
102 this.messageLimit = AnalyticsConstants.DEFAULT_SUBSCRIBER_MESSAGE_LIMIT; // defaults to 1000 message limit
\r
107 * Setup for custom host port number - Defaults to 80.
\r
109 * @param portNumber custom port number
\r
110 * @return Builder object itself for chaining
\r
112 public Builder setPortNumber(@Nonnull Integer portNumber) {
\r
113 this.portNumber = portNumber;
\r
119 * Setup user name for authentication. If no username is provided authentication will be disabled
\r
121 * @param userName user name for DMaaP Topic Authentication
\r
122 * @return Builder object itself for chaining
\r
124 public Builder setUserName(@Nonnull String userName) {
\r
125 this.userName = userName;
\r
131 * Setup user password for authentication. If no password is provided authentication will be disabled
\r
133 * @param userPassword user password for DMaaP Topic Authentication
\r
134 * @return Builder object itself for chaining
\r
136 public Builder setUserPassword(@Nonnull String userPassword) {
\r
137 this.userPassword = userPassword;
\r
143 * Setup custom Subscriber protocol - Defaults to https.
\r
144 * Note: Only http and https are currently supported.
\r
146 * @param protocol protocol e.g. https or http
\r
147 * @return Builder object itself for chaining
\r
149 public Builder setProtocol(@Nonnull String protocol) {
\r
151 this.protocol = normalizeValidateProtocol(protocol);
\r
156 * Setup custom Subscriber content-type - Defaults to application/json
\r
158 * @param contentType content type e.g. application/json
\r
159 * @return Builder object itself for chaining
\r
161 public Builder setContentType(@Nonnull String contentType) {
\r
162 final String normalizedContentType = normalizeValidateContentType(contentType);
\r
163 this.contentType = normalizedContentType;
\r
169 * Setup custom Consumer Id - Defaults to random Id
\r
171 * @param consumerId - custom consumer ID
\r
172 * @return Builder object itself for chaining
\r
174 public Builder setConsumerId(@Nonnull String consumerId) {
\r
175 this.consumerId = consumerId;
\r
180 * Setup custom Consumer Group - Default to OpenDCAE-DMaaPSub-ConsumerID
\r
182 * @param consumerGroup - custom Consumer Group
\r
183 * @return Builder object itself for chaining
\r
185 public Builder setConsumerGroup(@Nonnull String consumerGroup) {
\r
186 this.consumerGroup = consumerGroup;
\r
191 * Setup Custom Subscriber timeout in ms - Default to no timeout limit
\r
193 * @param timeoutMS timeout in milliseconds
\r
194 * @return Builder object itself for chaining
\r
196 public Builder setTimeoutMS(@Nonnull Integer timeoutMS) {
\r
197 this.timeoutMS = timeoutMS;
\r
202 * Setup custom Subscriber Message Limit - Default to no limit
\r
204 * @param messageLimit message Limit
\r
205 * @return Builder object itself for chaining
\r
207 public Builder setMessageLimit(@Nonnull Integer messageLimit) {
\r
208 this.messageLimit = messageLimit;
\r
213 * Builds Immutable instance of {@link DMaaPMRSubscriberConfig}
\r
215 * @return immutable DMaaP Subscriber Config Object
\r
217 public DMaaPMRSubscriberConfig build() {
\r
218 return new DMaaPMRSubscriberConfig(hostName, portNumber, topicName, protocol, userName, userPassword,
\r
219 contentType, consumerId, consumerGroup, timeoutMS, messageLimit);
\r
226 * DMaaP MR Subscriber Consumer Id
\r
228 * @return consumer Id
\r
230 public String getConsumerId() {
\r
235 * DMaaP MR Subscriber Consumer Group
\r
237 * @return consumer group
\r
239 public String getConsumerGroup() {
\r
240 return consumerGroup;
\r
244 * DMaaP MR Subscriber Timeout in ms
\r
246 * @return subscriber timeout ms
\r
248 public Integer getTimeoutMS() {
\r
253 * DMaaP MR Subscriber message limit
\r
255 * @return subscriber message limit
\r
257 public Integer getMessageLimit() {
\r
258 return messageLimit;
\r
262 public boolean equals(Object o) {
\r
266 if (o == null || getClass() != o.getClass()) {
\r
269 if (!super.equals(o)) {
\r
272 DMaaPMRSubscriberConfig that = (DMaaPMRSubscriberConfig) o;
\r
273 return Objects.equal(consumerId, that.consumerId) &&
\r
274 Objects.equal(consumerGroup, that.consumerGroup) &&
\r
275 Objects.equal(timeoutMS, that.timeoutMS) &&
\r
276 Objects.equal(messageLimit, that.messageLimit);
\r
280 public int hashCode() {
\r
281 return Objects.hashCode(super.hashCode(), consumerId, consumerGroup, timeoutMS, messageLimit);
\r
286 public String toString() {
\r
287 return Objects.toStringHelper(this)
\r
288 .add("baseConfig", super.toString())
\r
289 .add("consumerId", consumerId)
\r
290 .add("consumerGroup", consumerGroup)
\r
291 .add("timeoutMS", timeoutMS)
\r
292 .add("messageLimit", messageLimit)
\r