ee988a2ca8b2c2ec5ab6a0f133a874f8de6de284
[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(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;
58     }
59
60     /**
61      * Builder to initialize immutable {@link DMaaPMRSubscriberConfig} object
62      */
63     public static class Builder {
64
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;
76
77         public Builder(@Nonnull String hostName,
78                        @Nonnull String topicName) {
79             // Required Values
80             this.hostName = hostName;
81             this.topicName = topicName;
82
83             // Default values
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
93         }
94
95
96         /**
97          * Setup for custom host port number - Defaults to 80.
98          *
99          * @param portNumber custom port number
100          * @return Builder object itself for chaining
101          */
102         public Builder setPortNumber(@Nonnull Integer portNumber) {
103             this.portNumber = portNumber;
104             return this;
105         }
106
107
108         /**
109          * Setup user name for authentication. If no username is provided authentication will be disabled
110          *
111          * @param userName user name for DMaaP Topic Authentication
112          * @return Builder object itself for chaining
113          */
114         public Builder setUserName(@Nonnull String userName) {
115             this.userName = userName;
116             return this;
117         }
118
119
120         /**
121          * Setup user password for authentication. If no password is provided authentication will be disabled
122          *
123          * @param userPassword user password for DMaaP Topic Authentication
124          * @return Builder object itself for chaining
125          */
126         public Builder setUserPassword(@Nonnull String userPassword) {
127             this.userPassword = userPassword;
128             return this;
129         }
130
131
132         /**
133          * Setup custom Subscriber protocol - Defaults to https.
134          * Note: Only http and https are currently supported.
135          *
136          * @param protocol protocol e.g. https or http
137          * @return Builder object itself for chaining
138          */
139         public Builder setProtocol(@Nonnull String protocol) {
140
141             this.protocol = normalizeValidateProtocol(protocol);
142             return this;
143         }
144
145         /**
146          * Setup custom Subscriber content-type - Defaults to application/json
147          *
148          * @param contentType content type e.g. application/json
149          * @return Builder object itself for chaining
150          */
151         public Builder setContentType(@Nonnull String contentType) {
152             final String normalizedContentType = normalizeValidateContentType(contentType);
153             this.contentType = normalizedContentType;
154             return this;
155         }
156
157
158         /**
159          * Setup custom Consumer Id - Defaults to random Id
160          *
161          * @param consumerId - custom consumer ID
162          * @return Builder object itself for chaining
163          */
164         public Builder setConsumerId(@Nonnull String consumerId) {
165             this.consumerId = consumerId;
166             return this;
167         }
168
169         /**
170          * Setup custom Consumer Group - Default to OpenDCAE-DMaaPSub-ConsumerID
171          *
172          * @param consumerGroup - custom Consumer Group
173          * @return Builder object itself for chaining
174          */
175         public Builder setConsumerGroup(@Nonnull String consumerGroup) {
176             this.consumerGroup = consumerGroup;
177             return this;
178         }
179
180         /**
181          * Setup Custom Subscriber timeout in ms - Default to no timeout limit
182          *
183          * @param timeoutMS timeout in milliseconds
184          * @return Builder object itself for chaining
185          */
186         public Builder setTimeoutMS(@Nonnull Integer timeoutMS) {
187             this.timeoutMS = timeoutMS;
188             return this;
189         }
190
191         /**
192          * Setup custom Subscriber Message Limit - Default to no limit
193          *
194          * @param messageLimit message Limit
195          * @return Builder object itself for chaining
196          */
197         public Builder setMessageLimit(@Nonnull Integer messageLimit) {
198             this.messageLimit = messageLimit;
199             return this;
200         }
201
202         /**
203          * Builds Immutable instance of {@link DMaaPMRSubscriberConfig}
204          *
205          * @return immutable DMaaP Subscriber Config Object
206          */
207         public DMaaPMRSubscriberConfig build() {
208             return new DMaaPMRSubscriberConfig(this);
209         }
210
211     }
212
213
214     /**
215      * DMaaP MR Subscriber Consumer Id
216      *
217      * @return consumer Id
218      */
219     public String getConsumerId() {
220         return consumerId;
221     }
222
223     /**
224      * DMaaP MR Subscriber Consumer Group
225      *
226      * @return consumer group
227      */
228     public String getConsumerGroup() {
229         return consumerGroup;
230     }
231
232     /**
233      * DMaaP MR Subscriber Timeout in ms
234      *
235      * @return subscriber timeout ms
236      */
237     public Integer getTimeoutMS() {
238         return timeoutMS;
239     }
240
241     /**
242      * DMaaP MR Subscriber message limit
243      *
244      * @return subscriber message limit
245      */
246     public Integer getMessageLimit() {
247         return messageLimit;
248     }
249
250     @Override
251     public boolean equals(Object o) {
252         if (this == o) {
253             return true;
254         }
255         if (o == null || getClass() != o.getClass()) {
256             return false;
257         }
258         if (!super.equals(o)) {
259             return false;
260         }
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);
266     }
267
268     @Override
269     public int hashCode() {
270         return Objects.hashCode(super.hashCode(), consumerId, consumerGroup, timeoutMS, messageLimit);
271     }
272
273
274     @Override
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)
282                 .toString();
283     }
284 }