d01c34f1ed645e7f9462a4a83aaec2b7d153a2b7
[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 import org.onap.dmaap.mr.client.ProtocolType;
29
30 public class ValidatorUtil {
31
32     private static final String ID = "id";
33     private static final String AUTH_KEY = "authKey";
34     private static final String AUTH_DATE = "authDate";
35     private static final String PASSWORD = "password";
36     private static final String USERNAME = "username";
37     private static final String HOST = "host";
38     private static final String DME2PREFERRED_ROUTER_FILE_PATH = "DME2preferredRouterFilePath";
39     private static final String TOPIC = "topic";
40     private static final String TRANSPORT_TYPE = "TransportType";
41     private static final String MAX_BATCH_SIZE = "maxBatchSize";
42     private static final String MAX_AGE_MS = "maxAgeMs";
43     private static final String MESSAGE_SENT_THREAD_OCCURRENCE_OLD = "MessageSentThreadOccurance";
44     private static final String MESSAGE_SENT_THREAD_OCCURRENCE = "MessageSentThreadOccurrence";
45     private static final String GROUP = "group";
46     private static final String SERVICE_NAME = "ServiceName";
47     private static final String PARTNER = "Partner";
48     private static final String ROUTE_OFFER = "routeOffer";
49     private static final String PROTOCOL = "Protocol";
50     private static final String METHOD_TYPE = "MethodType";
51     private static final String CONTENT_TYPE = "contenttype";
52     private static final String LATITUDE = "Latitude";
53     private static final String LONGITUDE = "Longitude";
54     private static final String AFT_ENVIRONMENT = "AFT_ENVIRONMENT";
55     private static final String VERSION = "Version";
56     private static final String ENVIRONMENT = "Environment";
57     private static final String SUB_CONTEXT_PATH = "SubContextPath";
58     private static final String SESSION_STICKINESS_REQUIRED = "sessionstickinessrequired";
59
60     public static final String IS_NEEDED = " is needed";
61
62     private ValidatorUtil() {
63
64     }
65
66     public static void validatePublisher(Properties props) {
67         String transportType = props.getProperty(TRANSPORT_TYPE);
68         if (ProtocolType.DME2.getValue().equalsIgnoreCase(transportType)) {
69             validateForDME2(props);
70         } else {
71             validateForNonDME2(props);
72         }
73         String maxBatchSize = props.getProperty(MAX_BATCH_SIZE, "");
74         if (maxBatchSize.isEmpty()) {
75             throw new IllegalArgumentException(MAX_BATCH_SIZE + IS_NEEDED);
76         }
77         String maxAgeMs = props.getProperty(MAX_AGE_MS, "");
78         if (maxAgeMs.isEmpty()) {
79             throw new IllegalArgumentException(MAX_AGE_MS + IS_NEEDED);
80         }
81
82         String messageSentThreadOccurrence = props.getProperty(MESSAGE_SENT_THREAD_OCCURRENCE);
83         if (messageSentThreadOccurrence == null || messageSentThreadOccurrence.isEmpty()) {
84             messageSentThreadOccurrence = props.getProperty(MESSAGE_SENT_THREAD_OCCURRENCE_OLD);
85         }
86         if (messageSentThreadOccurrence == null || messageSentThreadOccurrence.isEmpty()) {
87             throw new IllegalArgumentException(MESSAGE_SENT_THREAD_OCCURRENCE + IS_NEEDED);
88         }
89         try {
90             Integer.parseInt(messageSentThreadOccurrence);
91         } catch (NumberFormatException e) {
92             throw new IllegalArgumentException(MESSAGE_SENT_THREAD_OCCURRENCE + " must be an integer");
93         }
94
95     }
96
97     public static void validateSubscriber(Properties props) {
98         String transportType = props.getProperty(TRANSPORT_TYPE);
99         if (ProtocolType.DME2.getValue().equalsIgnoreCase(transportType)) {
100             validateForDME2(props);
101         } else {
102             validateForNonDME2(props);
103         }
104         String group = props.getProperty(GROUP, "");
105         if (group.isEmpty()) {
106             throw new IllegalArgumentException(GROUP + IS_NEEDED);
107         }
108         String id = props.getProperty(ID, "");
109         if (id.isEmpty()) {
110             throw new IllegalArgumentException("Consumer (" + ID + ")" + IS_NEEDED);
111         }
112     }
113
114     private static void validateForDME2(Properties props) {
115         String serviceName = props.getProperty(SERVICE_NAME, "");
116         if (serviceName.isEmpty()) {
117             throw new IllegalArgumentException(SERVICE_NAME + IS_NEEDED);
118         }
119         String topic = props.getProperty(TOPIC, "");
120         if (topic.isEmpty()) {
121             throw new IllegalArgumentException(TOPIC + IS_NEEDED);
122         }
123         String username = props.getProperty(USERNAME, "");
124         if (username.isEmpty()) {
125             throw new IllegalArgumentException(USERNAME + IS_NEEDED);
126         }
127         String password = props.getProperty(PASSWORD, "");
128         if (password.isEmpty()) {
129             throw new IllegalArgumentException(PASSWORD + IS_NEEDED);
130         }
131         String dme2preferredRouterFilePath = props.getProperty(DME2PREFERRED_ROUTER_FILE_PATH, "");
132         if (dme2preferredRouterFilePath.isEmpty()) {
133             throw new IllegalArgumentException(DME2PREFERRED_ROUTER_FILE_PATH + IS_NEEDED);
134         }
135         String partner = props.getProperty(PARTNER, "");
136         String routeOffer = props.getProperty(ROUTE_OFFER, "");
137         if (partner.isEmpty() && routeOffer.isEmpty()) {
138             throw new IllegalArgumentException(PARTNER + " or " + ROUTE_OFFER + IS_NEEDED);
139         }
140         String protocol = props.getProperty(PROTOCOL, "");
141         if (protocol.isEmpty()) {
142             throw new IllegalArgumentException(PROTOCOL + IS_NEEDED);
143         }
144         String methodType = props.getProperty(METHOD_TYPE, "");
145         if (methodType.isEmpty()) {
146             throw new IllegalArgumentException(METHOD_TYPE + IS_NEEDED);
147         }
148         String contentType = props.getProperty(CONTENT_TYPE, "");
149         if (contentType.isEmpty()) {
150             throw new IllegalArgumentException(CONTENT_TYPE + IS_NEEDED);
151         }
152         String latitude = props.getProperty(LATITUDE, "");
153         if (latitude.isEmpty()) {
154             throw new IllegalArgumentException(LATITUDE + IS_NEEDED);
155         }
156         String longitude = props.getProperty(LONGITUDE, "");
157         if (longitude.isEmpty()) {
158             throw new IllegalArgumentException(LONGITUDE + IS_NEEDED);
159         }
160         String aftEnv = props.getProperty(AFT_ENVIRONMENT, "");
161         if (aftEnv.isEmpty()) {
162             throw new IllegalArgumentException(AFT_ENVIRONMENT + IS_NEEDED);
163         }
164         String version = props.getProperty(VERSION, "");
165         if (version.isEmpty()) {
166             throw new IllegalArgumentException(VERSION + IS_NEEDED);
167         }
168         String environment = props.getProperty(ENVIRONMENT, "");
169         if (environment.isEmpty()) {
170             throw new IllegalArgumentException(ENVIRONMENT + IS_NEEDED);
171         }
172         String subContextPath = props.getProperty(SUB_CONTEXT_PATH, "");
173         if (subContextPath.isEmpty()) {
174             throw new IllegalArgumentException(SUB_CONTEXT_PATH + IS_NEEDED);
175         }
176         String sessionstickinessrequired = props.getProperty(SESSION_STICKINESS_REQUIRED, "");
177         if (sessionstickinessrequired.isEmpty()) {
178             throw new IllegalArgumentException(SESSION_STICKINESS_REQUIRED + IS_NEEDED);
179         }
180     }
181
182     private static void validateForNonDME2(Properties props) {
183         String host = props.getProperty(HOST, "");
184         if (host.isEmpty()) {
185             throw new IllegalArgumentException(HOST + IS_NEEDED);
186         }
187         String topic = props.getProperty(TOPIC, "");
188         if (topic.isEmpty()) {
189             throw new IllegalArgumentException(TOPIC + IS_NEEDED);
190         }
191         String contenttype = props.getProperty(CONTENT_TYPE, "");
192         if (contenttype.isEmpty()) {
193             throw new IllegalArgumentException(CONTENT_TYPE + IS_NEEDED);
194         }
195         String transportType = props.getProperty(TRANSPORT_TYPE);
196         if (!ProtocolType.HTTPNOAUTH.getValue().equalsIgnoreCase(transportType)) {
197             String username = props.getProperty(USERNAME, "");
198             if (username.isEmpty()) {
199                 throw new IllegalArgumentException(USERNAME + IS_NEEDED);
200             }
201             String password = props.getProperty(PASSWORD, "");
202             if (password.isEmpty()) {
203                 throw new IllegalArgumentException(PASSWORD + IS_NEEDED);
204             }
205         }
206         if (ProtocolType.AUTH_KEY.getValue().equalsIgnoreCase(transportType)) {
207             String authKey = props.getProperty(AUTH_KEY, "");
208             if (authKey.isEmpty()) {
209                 throw new IllegalArgumentException(AUTH_KEY + IS_NEEDED);
210             }
211             String authDate = props.getProperty(AUTH_DATE, "");
212             if (authDate.isEmpty()) {
213                 throw new IllegalArgumentException(AUTH_DATE + IS_NEEDED);
214             }
215         }
216     }
217
218 }