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