Initial TCA commit into DCAEGEN2
[dcaegen2/analytics/tca.git] / dcae-analytics-dmaap / src / main / java / org / openecomp / dcae / apod / analytics / dmaap / domain / config / DMaaPMRBaseConfig.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.openecomp.dcae.apod.analytics.dmaap.domain.config;
22
23 import com.google.common.base.Objects;
24 import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import java.util.Locale;
29
30 import static org.openecomp.dcae.apod.analytics.common.utils.HTTPUtils.JSON_APPLICATION_TYPE;
31
32 /**
33  * <p>
34  *      Contains common parameters for both DMaaP Message Router Publisher and Subscriber Configs
35  * <p>
36  * @author Rajiv Singla . Creation Date: 10/12/2016.
37  */
38 public abstract class DMaaPMRBaseConfig implements DMaaPMRConfig {
39
40     protected static final Logger LOG = LoggerFactory.getLogger(DMaaPMRBaseConfig.class);
41
42     protected String hostName;
43     protected Integer portNumber;
44     protected String topicName;
45     protected String protocol;
46     protected String userName;
47     protected String userPassword;
48     protected String contentType;
49
50     /**
51      * Provides host name e.g. mrlocal-mtnjftle01.homer.com
52      *
53      * @return host name
54      */
55     public String getHostName() {
56         return hostName;
57     }
58
59
60     /**
61      * Provides Port Number of DMaaP MR Topic Host. Defaults to 80
62      *
63      * @return host port number
64      */
65     public Integer getPortNumber() {
66         return portNumber;
67     }
68
69     /**
70      * Provides topic name e.g. com.dcae.dmaap.mtnje2.DcaeTestVES
71      *
72      * @return topic name
73      */
74     public String getTopicName() {
75         return topicName;
76     }
77
78     /**
79      * Provides protocol type e.g. http or https
80      *
81      * @return protocol type
82      */
83     public String getProtocol() {
84         return protocol;
85     }
86
87     /**
88      * Provides content type e.g. application/json
89      *
90      * @return content type
91      */
92     public String getContentType() {
93         return contentType;
94     }
95
96
97     /**
98      * Provides User name for the DMaaP MR Topic authentication
99      *
100      * @return user name
101      */
102     public String getUserName() {
103         return userName;
104     }
105
106     /**
107      * Provides User password for the DMaaP MR Topic authentication
108      *
109      * @return user Password
110      */
111     public String getUserPassword() {
112         return userPassword;
113     }
114
115
116     /**
117      * Trims, adjusts casing and validates user input String for protocol selection
118      *
119      * @param protocol - User input for protocol String
120      * @return - network protocol e.g http or https
121      */
122     protected static String normalizeValidateProtocol(final String protocol) {
123         // validate that only http and https are supported protocols are Supported for DMaaP MR
124         String normalizedProtocolString = protocol.trim().toLowerCase(Locale.ENGLISH);
125         if (normalizedProtocolString.isEmpty() ||
126                 !("http".equals(normalizedProtocolString) || "https".equals(normalizedProtocolString))) {
127
128             final String errorMessage =
129                     "Unsupported protocol selection. Only HTTPS and HTTPS are currently supported for DMaaP MR";
130
131             throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, new IllegalArgumentException(errorMessage));
132         }
133         return normalizedProtocolString;
134     }
135
136
137     /**
138      * Trims, adjust casing and validates content type is supported by DMaaP.
139      *
140      * NOTE: DMaaP currently only support application/json content type
141      *
142      * @param contentType content type that needs to checked for DMaaP MR support
143      * @return true if content type is supported by DMaaP MR
144      */
145     protected static String normalizeValidateContentType(final String contentType) {
146         // Current DMaaP MR is only supporting "application/json" content type
147         String normalizedContentType = contentType.trim().toLowerCase(Locale.ENGLISH);
148         final boolean isSupported = contentType.equals(JSON_APPLICATION_TYPE);
149         if (!isSupported) {
150             final String errorMessage =
151                     "Unsupported content type selection. Only application/json is currently supported for DMaaP MR";
152
153             throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, new IllegalArgumentException(errorMessage));
154         }
155         return normalizedContentType;
156     }
157
158
159     @Override
160     public boolean equals(Object o) {
161         if (this == o) {
162             return true;
163         }
164         if (!(o instanceof DMaaPMRBaseConfig)) {
165             return false;
166         }
167         DMaaPMRBaseConfig that = (DMaaPMRBaseConfig) o;
168         return Objects.equal(hostName, that.hostName) &&
169                 Objects.equal(portNumber, that.portNumber) &&
170                 Objects.equal(topicName, that.topicName) &&
171                 Objects.equal(protocol, that.protocol) &&
172                 Objects.equal(userName, that.userName) &&
173                 Objects.equal(userPassword, that.userPassword) &&
174                 Objects.equal(contentType, that.contentType);
175     }
176
177     @Override
178     public int hashCode() {
179         return Objects.hashCode(hostName, portNumber, topicName, protocol, userName, userPassword, contentType);
180     }
181
182     @Override
183     public String toString() {
184         return Objects.toStringHelper(this)
185                 .add("hostName", hostName)
186                 .add("portNumber", portNumber)
187                 .add("topicName", topicName)
188                 .add("protocol", protocol)
189                 .add("userName", userName)
190                 .add("contentType", contentType)
191                 .toString();
192     }
193 }