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