53a6ab663891b1d1b308a90240c88041b681b05a
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
6  * Modifications Copyright (C) 2018-2019, 2021 AT&T Intellectual Property. All rights reserved.
7  * Modifications Copyright (C) 2019, 2023-2024 Nordix Foundation.
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  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.common.endpoints.event.comm.bus.internal;
24
25 import java.util.List;
26 import java.util.Map;
27 import lombok.AccessLevel;
28 import lombok.Getter;
29 import lombok.NoArgsConstructor;
30 import lombok.Setter;
31 import org.apache.commons.lang3.StringUtils;
32
33 /**
34  * Member variables of this Params class are as follows.
35  *
36  * <p>servers Kafka servers
37  * topic Kafka Topic to be monitored
38  * apiKey Kafka API Key (optional)
39  * apiSecret Kafka API Secret (optional)
40  * consumerGroup kafka Reader Consumer Group
41  * consumerInstance Kafka Reader Instance
42  * fetchTimeout kafka fetch timeout
43  * fetchLimit Kafka fetch limit
44  * environment DME2 Environment
45  * aftEnvironment DME2 AFT Environment
46  * partner DME2 Partner
47  * latitude DME2 Latitude
48  * longitude DME2 Longitude
49  * additionalProps Additional properties to pass to DME2
50  * useHttps does connection use HTTPS?
51  * allowTracing is message tracing allowed?
52  * allowSelfSignedCerts are self-signed certificates allow
53  */
54 @Getter
55 @Setter
56 public class BusTopicParams {
57
58     private int port;
59     private List<String> servers;
60     private Map<String, String> additionalProps;
61     private String topic;
62     private String effectiveTopic;
63     private String apiKey;
64     private String apiSecret;
65     private String consumerGroup;
66     private String consumerInstance;
67     private int fetchTimeout;
68     private int fetchLimit;
69     private boolean useHttps;
70     private boolean allowTracing;
71     private boolean allowSelfSignedCerts;
72     private boolean managed;
73
74     private String userName;
75     private String password;
76     private String environment;
77     private String aftEnvironment;
78     private String partner;
79     private String latitude;
80     private String longitude;
81     private String partitionId;
82     private String clientName;
83     private String hostname;
84     private String basePath;
85     @Getter
86     private String serializationProvider;
87
88     public static TopicParamsBuilder builder() {
89         return new TopicParamsBuilder();
90     }
91
92     /**
93      * Methods to Check if the property is INVALID.
94      */
95
96     boolean isEnvironmentInvalid() {
97         return StringUtils.isBlank(environment);
98     }
99
100     boolean isAftEnvironmentInvalid() {
101         return StringUtils.isBlank(aftEnvironment);
102     }
103
104     boolean isLatitudeInvalid() {
105         return StringUtils.isBlank(latitude);
106     }
107
108     boolean isLongitudeInvalid() {
109         return StringUtils.isBlank(longitude);
110     }
111
112     boolean isConsumerInstanceInvalid() {
113         return StringUtils.isBlank(consumerInstance);
114     }
115
116     boolean isConsumerGroupInvalid() {
117         return StringUtils.isBlank(consumerGroup);
118     }
119
120     public boolean isClientNameInvalid() {
121         return StringUtils.isBlank(clientName);
122     }
123
124     boolean isPartnerInvalid() {
125         return StringUtils.isBlank(partner);
126     }
127
128     boolean isServersInvalid() {
129         return (servers == null || servers.isEmpty()
130                 || (servers.size() == 1 && ("".equals(servers.get(0)))));
131     }
132
133     boolean isTopicInvalid() {
134         return StringUtils.isBlank(topic);
135     }
136
137     boolean isPartitionIdInvalid() {
138         return StringUtils.isBlank(partitionId);
139     }
140
141     public boolean isHostnameInvalid() {
142         return StringUtils.isBlank(hostname);
143     }
144
145     public boolean isPortInvalid() {
146         return  (getPort() <= 0 || getPort() >= 65535);
147     }
148
149     /**
150      * Methods to Check if the property is Valid.
151      */
152
153     boolean isApiKeyValid() {
154         return StringUtils.isNotBlank(apiKey);
155     }
156
157     boolean isApiSecretValid() {
158         return StringUtils.isNotBlank(apiSecret);
159     }
160
161     boolean isUserNameValid() {
162         return StringUtils.isNotBlank(userName);
163     }
164
165     boolean isPasswordValid() {
166         return StringUtils.isNotBlank(password);
167     }
168
169     boolean isAdditionalPropsValid() {
170         return additionalProps != null;
171     }
172
173     public void setEffectiveTopic(String effectiveTopic) {
174         this.effectiveTopic = topicToLowerCase(effectiveTopic);
175     }
176
177     public void setTopic(String topic) {
178         this.topic = topicToLowerCase(topic);
179     }
180
181     public String getEffectiveTopic() {
182         return topicToLowerCase(effectiveTopic);
183     }
184
185     public String getTopic() {
186         return topicToLowerCase(topic);
187     }
188
189     private String topicToLowerCase(String topic) {
190         return (topic == null || topic.isEmpty()) ? topic : topic.toLowerCase();
191     }
192
193     @NoArgsConstructor(access = AccessLevel.PRIVATE)
194     public static class TopicParamsBuilder {
195
196         final BusTopicParams params = new BusTopicParams();
197
198         public TopicParamsBuilder servers(List<String> servers) {
199             this.params.servers = servers;
200             return this;
201         }
202
203         public TopicParamsBuilder topic(String topic) {
204             this.params.setTopic(topic);
205             return this;
206         }
207
208         public TopicParamsBuilder effectiveTopic(String effectiveTopic) {
209             this.params.setEffectiveTopic(effectiveTopic);
210             return this;
211         }
212
213         public TopicParamsBuilder apiKey(String apiKey) {
214             this.params.apiKey = apiKey;
215             return this;
216         }
217
218         public TopicParamsBuilder apiSecret(String apiSecret) {
219             this.params.apiSecret = apiSecret;
220             return this;
221         }
222
223         public TopicParamsBuilder consumerGroup(String consumerGroup) {
224             this.params.consumerGroup = consumerGroup;
225             return this;
226         }
227
228         public TopicParamsBuilder consumerInstance(String consumerInstance) {
229             this.params.consumerInstance = consumerInstance;
230             return this;
231         }
232
233         public TopicParamsBuilder fetchTimeout(int fetchTimeout) {
234             this.params.fetchTimeout = fetchTimeout;
235             return this;
236         }
237
238         public TopicParamsBuilder fetchLimit(int fetchLimit) {
239             this.params.fetchLimit = fetchLimit;
240             return this;
241         }
242
243         public TopicParamsBuilder useHttps(boolean useHttps) {
244             this.params.useHttps = useHttps;
245             return this;
246         }
247
248         public TopicParamsBuilder allowTracing(boolean allowTracing) {
249             this.params.allowTracing = allowTracing;
250             return this;
251         }
252
253         public TopicParamsBuilder allowSelfSignedCerts(boolean allowSelfSignedCerts) {
254             this.params.allowSelfSignedCerts = allowSelfSignedCerts;
255             return this;
256         }
257
258         public TopicParamsBuilder userName(String userName) {
259             this.params.userName = userName;
260             return this;
261         }
262
263         public TopicParamsBuilder password(String password) {
264             this.params.password = password;
265             return this;
266         }
267
268         public TopicParamsBuilder environment(String environment) {
269             this.params.environment = environment;
270             return this;
271         }
272
273         public TopicParamsBuilder aftEnvironment(String aftEnvironment) {
274             this.params.aftEnvironment = aftEnvironment;
275             return this;
276         }
277
278         public TopicParamsBuilder partner(String partner) {
279             this.params.partner = partner;
280             return this;
281         }
282
283         public TopicParamsBuilder latitude(String latitude) {
284             this.params.latitude = latitude;
285             return this;
286         }
287
288         public TopicParamsBuilder longitude(String longitude) {
289             this.params.longitude = longitude;
290             return this;
291         }
292
293         public TopicParamsBuilder additionalProps(Map<String, String> additionalProps) {
294             this.params.additionalProps = additionalProps;
295             return this;
296         }
297
298         public TopicParamsBuilder partitionId(String partitionId) {
299             this.params.partitionId = partitionId;
300             return this;
301         }
302
303         public BusTopicParams build() {
304             return params;
305         }
306
307         public TopicParamsBuilder managed(boolean managed) {
308             this.params.managed = managed;
309             return this;
310         }
311
312         public TopicParamsBuilder hostname(String hostname) {
313             this.params.hostname = hostname;
314             return this;
315         }
316
317         public TopicParamsBuilder clientName(String clientName) {
318             this.params.clientName = clientName;
319             return this;
320         }
321
322         public TopicParamsBuilder port(int port) {
323             this.params.port = port;
324             return this;
325         }
326
327         public TopicParamsBuilder basePath(String basePath) {
328             this.params.basePath = basePath;
329             return this;
330         }
331
332         public TopicParamsBuilder serializationProvider(String serializationProvider) {
333             this.params.serializationProvider = serializationProvider;
334             return this;
335         }
336     }
337 }
338