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