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.onap.dcae.apod.analytics.dmaap.domain.config;
23 import com.google.common.base.Objects;
24 import org.onap.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(Builder builder) {
47 this.hostName = builder.hostName;
48 this.portNumber = builder.portNumber;
49 this.topicName = builder.topicName;
50 this.protocol = builder.protocol;
51 this.userName = builder.userName;
52 this.userPassword = builder.userPassword;
53 this.contentType = builder.contentType;
54 this.consumerId = builder.consumerId;
55 this.consumerGroup = builder.consumerGroup;
56 this.timeoutMS = builder.timeoutMS;
57 this.messageLimit = builder.messageLimit;
61 * Builder to initialize immutable {@link DMaaPMRSubscriberConfig} object
63 public static class Builder {
65 private String hostName;
66 private Integer portNumber;
67 private String topicName;
68 private String userName;
69 private String userPassword;
70 private String protocol;
71 private String contentType;
72 private String consumerId;
73 private String consumerGroup;
74 private Integer timeoutMS;
75 private Integer messageLimit;
77 public Builder(@Nonnull String hostName,
78 @Nonnull String topicName) {
80 this.hostName = hostName;
81 this.topicName = topicName;
84 this.portNumber = AnalyticsConstants.DEFAULT_PORT_NUMBER;
85 this.userName = AnalyticsConstants.DEFAULT_USER_NAME;
86 this.userPassword = AnalyticsConstants.DEFAULT_USER_PASSWORD;
87 this.protocol = AnalyticsConstants.DEFAULT_PROTOCOL;
88 this.contentType = AnalyticsConstants.DEFAULT_CONTENT_TYPE;
89 this.consumerId = UUID.randomUUID().toString(); // consumer is assigned a random id by default
90 this.consumerGroup = AnalyticsConstants.DEFAULT_SUBSCRIBER_GROUP_PREFIX + consumerId; // random group
91 this.timeoutMS = AnalyticsConstants.DEFAULT_SUBSCRIBER_TIMEOUT_MS; // defaults to 10ms timeout
92 this.messageLimit = AnalyticsConstants.DEFAULT_SUBSCRIBER_MESSAGE_LIMIT; // defaults to 1000 message limit
97 * Setup for custom host port number - Defaults to 80.
99 * @param portNumber custom port number
100 * @return Builder object itself for chaining
102 public Builder setPortNumber(@Nonnull Integer portNumber) {
103 this.portNumber = portNumber;
109 * Setup user name for authentication. If no username is provided authentication will be disabled
111 * @param userName user name for DMaaP Topic Authentication
112 * @return Builder object itself for chaining
114 public Builder setUserName(@Nonnull String userName) {
115 this.userName = userName;
121 * Setup user password for authentication. If no password is provided authentication will be disabled
123 * @param userPassword user password for DMaaP Topic Authentication
124 * @return Builder object itself for chaining
126 public Builder setUserPassword(@Nonnull String userPassword) {
127 this.userPassword = userPassword;
133 * Setup custom Subscriber protocol - Defaults to https.
134 * Note: Only http and https are currently supported.
136 * @param protocol protocol e.g. https or http
137 * @return Builder object itself for chaining
139 public Builder setProtocol(@Nonnull String protocol) {
141 this.protocol = normalizeValidateProtocol(protocol);
146 * Setup custom Subscriber content-type - Defaults to application/json
148 * @param contentType content type e.g. application/json
149 * @return Builder object itself for chaining
151 public Builder setContentType(@Nonnull String contentType) {
152 final String normalizedContentType = normalizeValidateContentType(contentType);
153 this.contentType = normalizedContentType;
159 * Setup custom Consumer Id - Defaults to random Id
161 * @param consumerId - custom consumer ID
162 * @return Builder object itself for chaining
164 public Builder setConsumerId(@Nonnull String consumerId) {
165 this.consumerId = consumerId;
170 * Setup custom Consumer Group - Default to OpenDCAE-DMaaPSub-ConsumerID
172 * @param consumerGroup - custom Consumer Group
173 * @return Builder object itself for chaining
175 public Builder setConsumerGroup(@Nonnull String consumerGroup) {
176 this.consumerGroup = consumerGroup;
181 * Setup Custom Subscriber timeout in ms - Default to no timeout limit
183 * @param timeoutMS timeout in milliseconds
184 * @return Builder object itself for chaining
186 public Builder setTimeoutMS(@Nonnull Integer timeoutMS) {
187 this.timeoutMS = timeoutMS;
192 * Setup custom Subscriber Message Limit - Default to no limit
194 * @param messageLimit message Limit
195 * @return Builder object itself for chaining
197 public Builder setMessageLimit(@Nonnull Integer messageLimit) {
198 this.messageLimit = messageLimit;
203 * Builds Immutable instance of {@link DMaaPMRSubscriberConfig}
205 * @return immutable DMaaP Subscriber Config Object
207 public DMaaPMRSubscriberConfig build() {
208 return new DMaaPMRSubscriberConfig(this);
215 * DMaaP MR Subscriber Consumer Id
217 * @return consumer Id
219 public String getConsumerId() {
224 * DMaaP MR Subscriber Consumer Group
226 * @return consumer group
228 public String getConsumerGroup() {
229 return consumerGroup;
233 * DMaaP MR Subscriber Timeout in ms
235 * @return subscriber timeout ms
237 public Integer getTimeoutMS() {
242 * DMaaP MR Subscriber message limit
244 * @return subscriber message limit
246 public Integer getMessageLimit() {
251 public boolean equals(Object o) {
255 if (o == null || getClass() != o.getClass()) {
258 if (!super.equals(o)) {
261 DMaaPMRSubscriberConfig that = (DMaaPMRSubscriberConfig) o;
262 return Objects.equal(consumerId, that.consumerId) &&
263 Objects.equal(consumerGroup, that.consumerGroup) &&
264 Objects.equal(timeoutMS, that.timeoutMS) &&
265 Objects.equal(messageLimit, that.messageLimit);
269 public int hashCode() {
270 return Objects.hashCode(super.hashCode(), consumerId, consumerGroup, timeoutMS, messageLimit);
275 public String toString() {
276 return Objects.toStringHelper(this)
277 .add("baseConfig", super.toString())
278 .add("consumerId", consumerId)
279 .add("consumerGroup", consumerGroup)
280 .add("timeoutMS", timeoutMS)
281 .add("messageLimit", messageLimit)