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