Sonar Critical Fix
[dcaegen2/analytics/tca.git] / dcae-analytics-dmaap / src / main / java / org / openecomp / dcae / apod / analytics / dmaap / domain / config / DMaaPMRPublisherConfig.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 javax.annotation.Nonnull;\r
27 \r
28 /**\r
29  * <p>\r
30  *      Immutable DMaaP MR Configuration for DMaaP MR Publisher.\r
31  * <p>\r
32  *      Use {@link DMaaPMRPublisherConfig.Builder} to construct Subscriber Configuration\r
33  * </p>\r
34  * <p>\r
35  * @author Rajiv Singla . Creation Date: 10/12/2016.\r
36  */\r
37 public class DMaaPMRPublisherConfig extends DMaaPMRBaseConfig {\r
38 \r
39     /**\r
40      * Publisher batching queue size\r
41      */\r
42     private int maxBatchSize;\r
43 \r
44     /**\r
45      * Publisher Recovery Queue Size\r
46      */\r
47     private int maxRecoveryQueueSize;\r
48 \r
49 \r
50     private DMaaPMRPublisherConfig(@Nonnull String hostName,\r
51                                    @Nonnull Integer portNumber,\r
52                                    @Nonnull String topicName,\r
53                                    @Nonnull String protocol,\r
54                                    String userName,\r
55                                    String userPassword,\r
56                                    @Nonnull String contentType,\r
57                                    int maxBatchSize,\r
58                                    int maxRecoveryQueueSize) {\r
59         this.hostName = hostName;\r
60         this.portNumber = portNumber;\r
61         this.topicName = topicName;\r
62         this.protocol = protocol;\r
63         this.userName = userName;\r
64         this.userPassword = userPassword;\r
65         this.contentType = contentType;\r
66         this.maxBatchSize = maxBatchSize;\r
67         this.maxRecoveryQueueSize = maxRecoveryQueueSize;\r
68     }\r
69 \r
70 \r
71     /**\r
72      * Builder to initialize immutable {@link DMaaPMRPublisherConfig} object\r
73      */\r
74     public static class Builder {\r
75 \r
76         private String hostName;\r
77         private Integer portNumber;\r
78         private String topicName;\r
79         private String userName;\r
80         private String userPassword;\r
81         private String protocol;\r
82         private String contentType;\r
83         private int maxBatchSize;\r
84         private int maxRecoveryQueueSize;\r
85 \r
86         public Builder(@Nonnull String hostName, @Nonnull String topicName) {\r
87             // required values\r
88             this.hostName = hostName;\r
89             this.topicName = topicName;\r
90             // Default values\r
91             this.portNumber = AnalyticsConstants.DEFAULT_PORT_NUMBER;\r
92             this.userName = AnalyticsConstants.DEFAULT_USER_NAME;\r
93             this.userPassword = AnalyticsConstants.DEFAULT_USER_PASSWORD;\r
94             this.protocol = AnalyticsConstants.DEFAULT_PROTOCOL;\r
95             this.contentType = AnalyticsConstants.DEFAULT_CONTENT_TYPE;\r
96             this.maxBatchSize = AnalyticsConstants.DEFAULT_PUBLISHER_MAX_BATCH_SIZE;\r
97             this.maxRecoveryQueueSize = AnalyticsConstants.DEFAULT_PUBLISHER_MAX_RECOVERY_QUEUE_SIZE;\r
98         }\r
99 \r
100         /**\r
101          * Setup for custom host port number - Defaults to 80.\r
102          *\r
103          * @param portNumber custom port number\r
104          * @return Builder object itself for chaining\r
105          */\r
106         public Builder setPortNumber(@Nonnull Integer portNumber) {\r
107             this.portNumber = portNumber;\r
108             return this;\r
109         }\r
110 \r
111 \r
112         /**\r
113          * Setup user name for authentication. If no username is provided authentication will be disabled\r
114          *\r
115          * @param userName user name for DMaaP Topic Authentication\r
116          * @return Builder object itself for chaining\r
117          */\r
118         public Builder setUserName(@Nonnull String userName) {\r
119             this.userName = userName;\r
120             return this;\r
121         }\r
122 \r
123 \r
124         /**\r
125          * Setup user password for authentication. If no password is provided authentication will be disabled\r
126          *\r
127          * @param userPassword user password for DMaaP Topic Authentication\r
128          * @return Builder object itself for chaining\r
129          */\r
130         public Builder setUserPassword(@Nonnull String userPassword) {\r
131             this.userPassword = userPassword;\r
132             return this;\r
133         }\r
134 \r
135 \r
136         /**\r
137          * Setup custom Publisher protocol - Defaults to https.\r
138          * Note: Only http and https are currently supported.\r
139          *\r
140          * @param protocol protocol e.g. https\r
141          * @return Builder object itself for chaining\r
142          */\r
143         public Builder setProtocol(@Nonnull String protocol) {\r
144             this.protocol = normalizeValidateProtocol(protocol);\r
145             return this;\r
146         }\r
147 \r
148 \r
149         /**\r
150          * Setup custom Publisher content-type - Defaults to application/json\r
151          *\r
152          * @param contentType content type e.g. application/json\r
153          * @return Builder object itself for chaining\r
154          */\r
155         public Builder setContentType(@Nonnull String contentType) {\r
156             final String normalizedContentType = normalizeValidateContentType(contentType);\r
157             this.contentType = normalizedContentType;\r
158             return this;\r
159         }\r
160 \r
161 \r
162         /**\r
163          * Setup custom Publisher Max Batch Size - Defaults to 100\r
164          *\r
165          * @param maxBatchSize max Batch Size\r
166          * @return Builder object itself for chaining\r
167          */\r
168         public Builder setMaxBatchSize(int maxBatchSize) {\r
169             this.maxBatchSize = maxBatchSize;\r
170             return this;\r
171         }\r
172 \r
173 \r
174         /**\r
175          * Setup custom Maximum Recovery Queue Size. Recovery Queue is used to hold messages temporarily in case\r
176          * DMaaP MR Publisher topic is not responding for any reason. Defaults to 100,000\r
177          *\r
178          * @param maxRecoveryQueueSize max recovery queue size\r
179          * @return Builder object itself for chaining\r
180          */\r
181         public Builder setMaxRecoveryQueueSize(int maxRecoveryQueueSize) {\r
182             this.maxRecoveryQueueSize = maxRecoveryQueueSize;\r
183             return this;\r
184         }\r
185 \r
186         /**\r
187          * Creates immutable instance of {@link DMaaPMRPublisherConfig}\r
188          *\r
189          * @return Builds and returns thread safe, immutable {@link DMaaPMRPublisherConfig} object\r
190          */\r
191         public DMaaPMRPublisherConfig build() {\r
192             return new DMaaPMRPublisherConfig(hostName, portNumber, topicName, protocol, userName, userPassword,\r
193                     contentType, maxBatchSize, maxRecoveryQueueSize);\r
194         }\r
195 \r
196     }\r
197 \r
198 \r
199     /**\r
200      * Returns max Publisher Batch Queue Size\r
201      *\r
202      * @return max Publisher Batch Queue size\r
203      */\r
204     public int getMaxBatchSize() {\r
205         return maxBatchSize;\r
206     }\r
207 \r
208     /**\r
209      * Returns max Publisher Recovery Queue Size\r
210      *\r
211      * @return max Recovery Queue size\r
212      */\r
213     public int getMaxRecoveryQueueSize() {\r
214         return maxRecoveryQueueSize;\r
215     }\r
216 \r
217 \r
218     @Override\r
219     public boolean equals(Object o) {\r
220         if (this == o) {\r
221             return true;\r
222         }\r
223         if (o == null || getClass() != o.getClass()) {\r
224             return false;\r
225         }\r
226         if (!super.equals(o)) {\r
227             return false;\r
228         }\r
229         DMaaPMRPublisherConfig that = (DMaaPMRPublisherConfig) o;\r
230         return maxBatchSize == that.maxBatchSize &&\r
231                 maxRecoveryQueueSize == that.maxRecoveryQueueSize;\r
232     }\r
233 \r
234     @Override\r
235     public int hashCode() {\r
236         return Objects.hashCode(super.hashCode(), maxBatchSize, maxRecoveryQueueSize);\r
237     }\r
238 \r
239 \r
240     @Override\r
241     public String toString() {\r
242         return Objects.toStringHelper(this)\r
243                 .add("baseConfig", super.toString())\r
244                 .add("maxBatchSize", maxBatchSize)\r
245                 .add("maxRecoveryQueueSize", maxRecoveryQueueSize)\r
246                 .toString();\r
247     }\r
248 }\r