8158b6bfe54eb4faee61ca8b15ad34af9d7a5608
[dcaegen2/analytics/tca.git] / dcae-analytics-dmaap / src / main / java / org / onap / dcae / apod / analytics / dmaap / domain / config / DMaaPMRSubscriberConfig.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
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
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
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===========================================
19  */
20
21 package org.onap.dcae.apod.analytics.dmaap.domain.config;
22
23 import com.google.common.base.Objects;
24 import org.onap.dcae.apod.analytics.common.AnalyticsConstants;
25
26 import java.util.UUID;
27
28 import javax.annotation.Nonnull;
29
30 /**
31  * <p>
32  *      Immutable DMaaP MR Configuration for Subscriber.
33  * <p>
34  *      Use {@link DMaaPMRSubscriberConfig.Builder} to construct Subscriber Configuration
35  * <p>
36  *
37  * @author Rajiv Singla . Creation Date: 10/12/2016.
38  */
39 public final class DMaaPMRSubscriberConfig extends DMaaPMRBaseConfig {
40
41     private final String consumerId;
42     private final String consumerGroup;
43     private final Integer timeoutMS;
44     private final Integer messageLimit;
45
46     private DMaaPMRSubscriberConfig(@Nonnull String hostName,
47                                     @Nonnull Integer portNumber,
48                                     @Nonnull String topicName,
49                                     @Nonnull String protocol,
50                                     String userName,
51                                     String userPassword,
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;
68     }
69
70     /**
71      * Builder to initialize immutable {@link DMaaPMRSubscriberConfig} object
72      */
73     public static class Builder {
74
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;
86
87         public Builder(@Nonnull String hostName,
88                        @Nonnull String topicName) {
89             // Required Values
90             this.hostName = hostName;
91             this.topicName = topicName;
92
93             // Default values
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
103         }
104
105
106         /**
107          * Setup for custom host port number - Defaults to 80.
108          *
109          * @param portNumber custom port number
110          * @return Builder object itself for chaining
111          */
112         public Builder setPortNumber(@Nonnull Integer portNumber) {
113             this.portNumber = portNumber;
114             return this;
115         }
116
117
118         /**
119          * Setup user name for authentication. If no username is provided authentication will be disabled
120          *
121          * @param userName user name for DMaaP Topic Authentication
122          * @return Builder object itself for chaining
123          */
124         public Builder setUserName(@Nonnull String userName) {
125             this.userName = userName;
126             return this;
127         }
128
129
130         /**
131          * Setup user password for authentication. If no password is provided authentication will be disabled
132          *
133          * @param userPassword user password for DMaaP Topic Authentication
134          * @return Builder object itself for chaining
135          */
136         public Builder setUserPassword(@Nonnull String userPassword) {
137             this.userPassword = userPassword;
138             return this;
139         }
140
141
142         /**
143          * Setup custom Subscriber protocol - Defaults to https.
144          * Note: Only http and https are currently supported.
145          *
146          * @param protocol protocol e.g. https or http
147          * @return Builder object itself for chaining
148          */
149         public Builder setProtocol(@Nonnull String protocol) {
150
151             this.protocol = normalizeValidateProtocol(protocol);
152             return this;
153         }
154
155         /**
156          * Setup custom Subscriber content-type - Defaults to application/json
157          *
158          * @param contentType content type e.g. application/json
159          * @return Builder object itself for chaining
160          */
161         public Builder setContentType(@Nonnull String contentType) {
162             final String normalizedContentType = normalizeValidateContentType(contentType);
163             this.contentType = normalizedContentType;
164             return this;
165         }
166
167
168         /**
169          * Setup custom Consumer Id - Defaults to random Id
170          *
171          * @param consumerId - custom consumer ID
172          * @return Builder object itself for chaining
173          */
174         public Builder setConsumerId(@Nonnull String consumerId) {
175             this.consumerId = consumerId;
176             return this;
177         }
178
179         /**
180          * Setup custom Consumer Group - Default to OpenDCAE-DMaaPSub-ConsumerID
181          *
182          * @param consumerGroup - custom Consumer Group
183          * @return Builder object itself for chaining
184          */
185         public Builder setConsumerGroup(@Nonnull String consumerGroup) {
186             this.consumerGroup = consumerGroup;
187             return this;
188         }
189
190         /**
191          * Setup Custom Subscriber timeout in ms - Default to no timeout limit
192          *
193          * @param timeoutMS timeout in milliseconds
194          * @return Builder object itself for chaining
195          */
196         public Builder setTimeoutMS(@Nonnull Integer timeoutMS) {
197             this.timeoutMS = timeoutMS;
198             return this;
199         }
200
201         /**
202          * Setup custom Subscriber Message Limit - Default to no limit
203          *
204          * @param messageLimit message Limit
205          * @return Builder object itself for chaining
206          */
207         public Builder setMessageLimit(@Nonnull Integer messageLimit) {
208             this.messageLimit = messageLimit;
209             return this;
210         }
211
212         /**
213          * Builds Immutable instance of {@link DMaaPMRSubscriberConfig}
214          *
215          * @return immutable DMaaP Subscriber Config Object
216          */
217         public DMaaPMRSubscriberConfig build() {
218             return new DMaaPMRSubscriberConfig(hostName, portNumber, topicName, protocol, userName, userPassword,
219                     contentType, consumerId, consumerGroup, timeoutMS, messageLimit);
220         }
221
222     }
223
224
225     /**
226      * DMaaP MR Subscriber Consumer Id
227      *
228      * @return consumer Id
229      */
230     public String getConsumerId() {
231         return consumerId;
232     }
233
234     /**
235      * DMaaP MR Subscriber Consumer Group
236      *
237      * @return consumer group
238      */
239     public String getConsumerGroup() {
240         return consumerGroup;
241     }
242
243     /**
244      * DMaaP MR Subscriber Timeout in ms
245      *
246      * @return subscriber timeout ms
247      */
248     public Integer getTimeoutMS() {
249         return timeoutMS;
250     }
251
252     /**
253      * DMaaP MR Subscriber message limit
254      *
255      * @return subscriber message limit
256      */
257     public Integer getMessageLimit() {
258         return messageLimit;
259     }
260
261     @Override
262     public boolean equals(Object o) {
263         if (this == o) {
264             return true;
265         }
266         if (o == null || getClass() != o.getClass()) {
267             return false;
268         }
269         if (!super.equals(o)) {
270             return false;
271         }
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);
277     }
278
279     @Override
280     public int hashCode() {
281         return Objects.hashCode(super.hashCode(), consumerId, consumerGroup, timeoutMS, messageLimit);
282     }
283
284
285     @Override
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)
293                 .toString();
294     }
295 }