Sonar issues review, fix properties constant name.
[dmaap/messagerouter/dmaapclient.git] / src / main / java / org / onap / dmaap / mr / tools / ValidatorUtil.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Modifications Copyright © 2021 Orange.
8  *  ================================================================================
9  *  Licensed under the Apache License, Version 2.0 (the "License");
10  *  you may not use this file except in compliance with the License.
11  *  You may obtain a copy of the License at
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  *  ============LICENSE_END=========================================================
20  *
21  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  *
23  *******************************************************************************/
24
25 package org.onap.dmaap.mr.tools;
26
27 import java.util.Properties;
28
29 import org.onap.dmaap.mr.client.DmaapClientConst;
30 import org.onap.dmaap.mr.client.ProtocolType;
31
32 public class ValidatorUtil {
33
34     public static final String IS_NEEDED = " is needed";
35
36     private ValidatorUtil() {
37
38     }
39
40     public static void validatePublisher(Properties props) {
41         String transportType = props.getProperty(DmaapClientConst.TRANSPORT_TYPE);
42         if (ProtocolType.DME2.getValue().equalsIgnoreCase(transportType)) {
43             validateForDME2(props);
44         } else {
45             validateForNonDME2(props);
46         }
47         String maxBatchSize = props.getProperty(DmaapClientConst.MAX_BATCH_SIZE, "");
48         if (maxBatchSize.isEmpty()) {
49             throw new IllegalArgumentException(DmaapClientConst.MAX_BATCH_SIZE + IS_NEEDED);
50         }
51         String maxAgeMs = props.getProperty(DmaapClientConst.MAX_AGE_MS, "");
52         if (maxAgeMs.isEmpty()) {
53             throw new IllegalArgumentException(DmaapClientConst.MAX_AGE_MS + IS_NEEDED);
54         }
55
56         String messageSentThreadOccurrence = props.getProperty(DmaapClientConst.MESSAGE_SENT_THREAD_OCCURRENCE);
57         if (messageSentThreadOccurrence == null || messageSentThreadOccurrence.isEmpty()) {
58             messageSentThreadOccurrence = props.getProperty(DmaapClientConst.MESSAGE_SENT_THREAD_OCCURRENCE_OLD);
59         }
60         if (messageSentThreadOccurrence == null || messageSentThreadOccurrence.isEmpty()) {
61             throw new IllegalArgumentException(DmaapClientConst.MESSAGE_SENT_THREAD_OCCURRENCE + IS_NEEDED);
62         }
63         try {
64             Integer.parseInt(messageSentThreadOccurrence);
65         } catch (NumberFormatException e) {
66             throw new IllegalArgumentException(DmaapClientConst.MESSAGE_SENT_THREAD_OCCURRENCE + " must be an integer");
67         }
68
69     }
70
71     public static void validateSubscriber(Properties props) {
72         String transportType = props.getProperty(DmaapClientConst.TRANSPORT_TYPE);
73         if (ProtocolType.DME2.getValue().equalsIgnoreCase(transportType)) {
74             validateForDME2(props);
75         } else {
76             validateForNonDME2(props);
77         }
78         String group = props.getProperty(DmaapClientConst.GROUP, "");
79         if (group.isEmpty()) {
80             throw new IllegalArgumentException(DmaapClientConst.GROUP + IS_NEEDED);
81         }
82         String id = props.getProperty(DmaapClientConst.ID, "");
83         if (id.isEmpty()) {
84             throw new IllegalArgumentException("Consumer (" + DmaapClientConst.ID + ")" + IS_NEEDED);
85         }
86     }
87
88     private static void validateForDME2(Properties props) {
89         String serviceName = props.getProperty(DmaapClientConst.SERVICE_NAME, "");
90         if (serviceName.isEmpty()) {
91             throw new IllegalArgumentException(DmaapClientConst.SERVICE_NAME + IS_NEEDED);
92         }
93         String topic = props.getProperty(DmaapClientConst.TOPIC, "");
94         if (topic.isEmpty()) {
95             throw new IllegalArgumentException(DmaapClientConst.TOPIC + IS_NEEDED);
96         }
97         String username = props.getProperty(DmaapClientConst.USERNAME, "");
98         if (username.isEmpty()) {
99             throw new IllegalArgumentException(DmaapClientConst.USERNAME + IS_NEEDED);
100         }
101         String password = props.getProperty(DmaapClientConst.PASSWORD, "");
102         if (password.isEmpty()) {
103             throw new IllegalArgumentException(DmaapClientConst.PASSWORD + IS_NEEDED);
104         }
105         String dme2preferredRouterFilePath = props.getProperty(DmaapClientConst.DME2PREFERRED_ROUTER_FILE_PATH, "");
106         if (dme2preferredRouterFilePath.isEmpty()) {
107             throw new IllegalArgumentException(DmaapClientConst.DME2PREFERRED_ROUTER_FILE_PATH + IS_NEEDED);
108         }
109         String partner = props.getProperty(DmaapClientConst.PARTNER, "");
110         String routeOffer = props.getProperty(DmaapClientConst.ROUTE_OFFER, "");
111         if (partner.isEmpty() && routeOffer.isEmpty()) {
112             throw new IllegalArgumentException(DmaapClientConst.PARTNER + " or " + DmaapClientConst.ROUTE_OFFER + IS_NEEDED);
113         }
114         String protocol = props.getProperty(DmaapClientConst.PROTOCOL, "");
115         if (protocol.isEmpty()) {
116             throw new IllegalArgumentException(DmaapClientConst.PROTOCOL + IS_NEEDED);
117         }
118         String methodType = props.getProperty(DmaapClientConst.METHOD_TYPE, "");
119         if (methodType.isEmpty()) {
120             throw new IllegalArgumentException(DmaapClientConst.METHOD_TYPE + IS_NEEDED);
121         }
122         String contentType = props.getProperty(DmaapClientConst.CONTENT_TYPE, "");
123         if (contentType.isEmpty()) {
124             throw new IllegalArgumentException(DmaapClientConst.CONTENT_TYPE + IS_NEEDED);
125         }
126         String latitude = props.getProperty(DmaapClientConst.LATITUDE, "");
127         if (latitude.isEmpty()) {
128             throw new IllegalArgumentException(DmaapClientConst.LATITUDE + IS_NEEDED);
129         }
130         String longitude = props.getProperty(DmaapClientConst.LONGITUDE, "");
131         if (longitude.isEmpty()) {
132             throw new IllegalArgumentException(DmaapClientConst.LONGITUDE + IS_NEEDED);
133         }
134         String aftEnv = props.getProperty(DmaapClientConst.AFT_ENVIRONMENT, "");
135         if (aftEnv.isEmpty()) {
136             throw new IllegalArgumentException(DmaapClientConst.AFT_ENVIRONMENT + IS_NEEDED);
137         }
138         String version = props.getProperty(DmaapClientConst.VERSION, "");
139         if (version.isEmpty()) {
140             throw new IllegalArgumentException(DmaapClientConst.VERSION + IS_NEEDED);
141         }
142         String environment = props.getProperty(DmaapClientConst.ENVIRONMENT, "");
143         if (environment.isEmpty()) {
144             throw new IllegalArgumentException(DmaapClientConst.ENVIRONMENT + IS_NEEDED);
145         }
146         String subContextPath = props.getProperty(DmaapClientConst.SUB_CONTEXT_PATH, "");
147         if (subContextPath.isEmpty()) {
148             throw new IllegalArgumentException(DmaapClientConst.SUB_CONTEXT_PATH + IS_NEEDED);
149         }
150         String sessionstickinessrequired = props.getProperty(DmaapClientConst.SESSION_STICKINESS_REQUIRED, "");
151         if (sessionstickinessrequired.isEmpty()) {
152             throw new IllegalArgumentException(DmaapClientConst.SESSION_STICKINESS_REQUIRED + IS_NEEDED);
153         }
154     }
155
156     private static void validateForNonDME2(Properties props) {
157         String host = props.getProperty(DmaapClientConst.HOST, "");
158         if (host.isEmpty()) {
159             throw new IllegalArgumentException(DmaapClientConst.HOST + IS_NEEDED);
160         }
161         String topic = props.getProperty(DmaapClientConst.TOPIC, "");
162         if (topic.isEmpty()) {
163             throw new IllegalArgumentException(DmaapClientConst.TOPIC + IS_NEEDED);
164         }
165         String contenttype = props.getProperty(DmaapClientConst.CONTENT_TYPE, "");
166         if (contenttype.isEmpty()) {
167             throw new IllegalArgumentException(DmaapClientConst.CONTENT_TYPE + IS_NEEDED);
168         }
169         String transportType = props.getProperty(DmaapClientConst.TRANSPORT_TYPE);
170         if (!ProtocolType.HTTPNOAUTH.getValue().equalsIgnoreCase(transportType)) {
171             String username = props.getProperty(DmaapClientConst.USERNAME, "");
172             if (username.isEmpty()) {
173                 throw new IllegalArgumentException(DmaapClientConst.USERNAME + IS_NEEDED);
174             }
175             String password = props.getProperty(DmaapClientConst.PASSWORD, "");
176             if (password.isEmpty()) {
177                 throw new IllegalArgumentException(DmaapClientConst.PASSWORD + IS_NEEDED);
178             }
179         }
180         if (ProtocolType.AUTH_KEY.getValue().equalsIgnoreCase(transportType)) {
181             String authKey = props.getProperty(DmaapClientConst.AUTH_KEY, "");
182             if (authKey.isEmpty()) {
183                 throw new IllegalArgumentException(DmaapClientConst.AUTH_KEY + IS_NEEDED);
184             }
185             String authDate = props.getProperty(DmaapClientConst.AUTH_DATE, "");
186             if (authDate.isEmpty()) {
187                 throw new IllegalArgumentException(DmaapClientConst.AUTH_DATE + IS_NEEDED);
188             }
189         }
190     }
191
192 }