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