diff options
author | Krishnajinka <kris.jinka@samsung.com> | 2018-08-16 18:14:51 +0900 |
---|---|---|
committer | Krishnajinka <kris.jinka@samsung.com> | 2018-08-20 14:27:28 +0900 |
commit | b655b67e38d98c3478a3a47407ee90410d4442bb (patch) | |
tree | 017c9782827708b77a87abf8ff0666a8e6fea6b2 /policy-endpoints/src/main | |
parent | 011cdf2d4fa47ab078061d3700a95c3d3764e5e0 (diff) |
Use builder for http and jersey client
Sonar issue with more than 7 params in a method. Use builder object
instead of params. Rework2 for fixing review comments.
Add modifications copyright
Issue-ID: POLICY-1017
Change-Id: Ib0fa692f8da770dcba06158d6e6cafbed2969c27
Signed-off-by: Krishnajinka <kris.jinka@samsung.com>
Diffstat (limited to 'policy-endpoints/src/main')
7 files changed, 175 insertions, 98 deletions
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusConsumer.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusConsumer.java index ecc90139..690a6d0b 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusConsumer.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusConsumer.java @@ -3,7 +3,7 @@ * policy-endpoints * ================================================================================ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. - * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd. + * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,6 +36,7 @@ import java.util.ArrayList; import java.util.Map; import java.util.Properties; +import org.apache.commons.lang3.StringUtils; import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSinkFactory; import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; import org.slf4j.Logger; @@ -294,7 +295,7 @@ public interface BusConsumer { this.fetchTimeout = busTopicParams.getFetchTimeout(); - if (busTopicParams.isTopicNullOrEmpty()) { + if (busTopicParams.isTopicInvalid()) { throw new IllegalArgumentException("No topic for DMaaP"); } @@ -391,7 +392,7 @@ public interface BusConsumer { super(busTopicParams); // super constructor sets servers = {""} if empty to avoid errors when using DME2 - if (busTopicParams.isServersNullOrEmpty()) { + if (busTopicParams.isServersInvalid()) { throw new IllegalArgumentException("Must provide at least one host for HTTP AAF"); } @@ -445,25 +446,25 @@ public interface BusConsumer { final String dme2RouteOffer = busTopicParams.getAdditionalProps() .get(DmaapTopicSinkFactory.DME2_ROUTE_OFFER_PROPERTY); - if (busTopicParams.isEnvironmentNullOrEmpty()) { + if (busTopicParams.isEnvironmentInvalid()) { throw parmException(busTopicParams.getTopic(), PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ENVIRONMENT_SUFFIX); } - if (busTopicParams.isAftEnvironmentNullOrEmpty()) { + if (busTopicParams.isAftEnvironmentInvalid()) { throw parmException(busTopicParams.getTopic(), PolicyEndPointProperties.PROPERTY_DMAAP_DME2_AFT_ENVIRONMENT_SUFFIX); } - if (busTopicParams.isLatitudeNullOrEmpty()) { + if (busTopicParams.isLatitudeInvalid()) { throw parmException(busTopicParams.getTopic(), PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LATITUDE_SUFFIX); } - if (busTopicParams.isLongitudeNullOrEmpty()) { + if (busTopicParams.isLongitudeInvalid()) { throw parmException(busTopicParams.getTopic(), PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LONGITUDE_SUFFIX); } - if ((busTopicParams.isPartnerNullOrEmpty()) - && (dme2RouteOffer == null || dme2RouteOffer.isEmpty())) { + if ((busTopicParams.isPartnerInvalid()) + && StringUtils.isBlank(dme2RouteOffer)) { throw new IllegalArgumentException( "Must provide at least " + PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + busTopicParams.getTopic() diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusPublisher.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusPublisher.java index 2d1d8257..aaf0cfb5 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusPublisher.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusPublisher.java @@ -3,7 +3,7 @@ * policy-endpoints * ================================================================================ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. - * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd. + * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,6 +38,7 @@ import java.util.Map; import java.util.Properties; import java.util.concurrent.TimeUnit; +import org.apache.commons.lang3.StringUtils; import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicSinkFactory; import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; import org.slf4j.Logger; @@ -176,7 +177,7 @@ public interface BusPublisher { String username, String password, boolean useHttps) { - if (topic == null || topic.isEmpty()) { + if (StringUtils.isBlank(topic)) { throw new IllegalArgumentException("No topic for DMaaP"); } @@ -313,25 +314,25 @@ public interface BusPublisher { DmaapTopicSinkFactory.DME2_ROUTE_OFFER_PROPERTY); } - if (busTopicParams.isEnvironmentNullOrEmpty()) { + if (busTopicParams.isEnvironmentInvalid()) { throw parmException(busTopicParams.getTopic(), PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ENVIRONMENT_SUFFIX); } - if (busTopicParams.isAftEnvironmentNullOrEmpty()) { + if (busTopicParams.isAftEnvironmentInvalid()) { throw parmException(busTopicParams.getTopic(), PolicyEndPointProperties.PROPERTY_DMAAP_DME2_AFT_ENVIRONMENT_SUFFIX); } - if (busTopicParams.isLatitudeNullOrEmpty()) { + if (busTopicParams.isLatitudeInvalid()) { throw parmException(busTopicParams.getTopic(), PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LATITUDE_SUFFIX); } - if (busTopicParams.isLongitudeNullOrEmpty()) { + if (busTopicParams.isLongitudeInvalid()) { throw parmException(busTopicParams.getTopic(), PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LONGITUDE_SUFFIX); } - if ((busTopicParams.isPartnerNullOrEmpty()) - && (dme2RouteOffer == null || dme2RouteOffer.trim().isEmpty())) { + if ((busTopicParams.isPartnerInvalid()) + && StringUtils.isBlank(dme2RouteOffer)) { throw new IllegalArgumentException( "Must provide at least " + PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "." + busTopicParams.getTopic() diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusTopicParams.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusTopicParams.java index b133f1c7..7f03286f 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusTopicParams.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusTopicParams.java @@ -20,12 +20,14 @@ package org.onap.policy.common.endpoints.event.comm.bus.internal; +import org.apache.commons.lang3.StringUtils; + import java.util.List; import java.util.Map; /** * Member variables of this Params class are as follows. - * + * * <p>servers DMaaP servers * topic DMaaP Topic to be monitored * apiKey DMaaP API Key (optional) @@ -45,11 +47,9 @@ import java.util.Map; */ public class BusTopicParams { - public static TopicParamsBuilder builder() { - return new TopicParamsBuilder(); - } - + private int port; private List<String> servers; + private Map<String, String> additionalProps; private String topic; private String apiKey; private String apiSecret; @@ -59,6 +59,7 @@ public class BusTopicParams { private int fetchLimit; private boolean useHttps; private boolean allowSelfSignedCerts; + private boolean managed; private String userName; private String password; @@ -67,9 +68,14 @@ public class BusTopicParams { private String partner; private String latitude; private String longitude; - private Map<String, String> additionalProps; private String partitionId; - private boolean managed; + private String clientName; + private String hostname; + private String basePath; + + public static TopicParamsBuilder builder() { + return new TopicParamsBuilder(); + } public String getPartitionId() { return partitionId; @@ -139,6 +145,26 @@ public class BusTopicParams { return fetchLimit; } + public String getClientName() { + return clientName; + } + + public String getHostname() { + return hostname; + } + + public int getPort() { + return port; + } + + public String getBasePath() { + return basePath; + } + + public boolean isManaged() { + return managed; + } + public boolean isUseHttps() { return useHttps; } @@ -147,74 +173,90 @@ public class BusTopicParams { return allowSelfSignedCerts; } - boolean isEnvironmentNullOrEmpty() { - return (environment == null || environment.trim().isEmpty()); + /** + * Methods to Check if the property is INVALID + */ + + boolean isEnvironmentInvalid() { + return StringUtils.isBlank(environment); } - boolean isAftEnvironmentNullOrEmpty() { - return (aftEnvironment == null || aftEnvironment.trim().isEmpty()); + boolean isAftEnvironmentInvalid() { + return StringUtils.isBlank(aftEnvironment); } - boolean isLatitudeNullOrEmpty() { - return (latitude == null || latitude.trim().isEmpty()); + boolean isLatitudeInvalid() { + return StringUtils.isBlank(latitude); } - boolean isLongitudeNullOrEmpty() { - return (longitude == null || longitude.trim().isEmpty()); + boolean isLongitudeInvalid() { + return StringUtils.isBlank(longitude); } - boolean isConsumerInstanceNullOrEmpty() { - return (consumerInstance == null || consumerInstance.trim().isEmpty()); + boolean isConsumerInstanceInvalid() { + return StringUtils.isBlank(consumerInstance); } - boolean isConsumerGroupNullOrEmpty() { - return (consumerGroup == null || consumerGroup.trim().isEmpty()); + boolean isConsumerGroupInvalid() { + return StringUtils.isBlank(consumerGroup); } - boolean isApiKeyValid() { - return !(apiKey == null || apiKey.trim().isEmpty()); + public boolean isClientNameInvalid() { + return StringUtils.isBlank(clientName); } - boolean isApiSecretValid() { - return !(apiSecret == null || apiSecret.trim().isEmpty()); + boolean isPartnerInvalid() { + return StringUtils.isBlank(partner); } - boolean isUserNameValid() { - return !(userName == null || userName.trim().isEmpty()); + boolean isServersInvalid() { + return (servers == null || servers.isEmpty() + || (servers.size() == 1 && ("".equals(servers.get(0))))); } - boolean isPasswordValid() { - return !(password == null || password.trim().isEmpty()); + boolean isTopicInvalid() { + return StringUtils.isBlank(topic); } - boolean isPartnerNullOrEmpty() { - return (partner == null || partner.trim().isEmpty()); + boolean isPartitionIdInvalid() { + return StringUtils.isBlank(partitionId); } - boolean isServersNullOrEmpty() { - return (servers == null || servers.isEmpty() - || (servers.size() == 1 && ("".equals(servers.get(0))))); + public boolean isHostnameInvalid() { + return StringUtils.isBlank(hostname); } - boolean isAdditionalPropsValid() { - return additionalProps != null; + public boolean isPortInvalid() { + return (getPort() <= 0 || getPort() >= 65535); + } + + /** + * Methods to Check if the property is Valid + */ + + boolean isApiKeyValid() { + return StringUtils.isNotBlank(apiKey); } - boolean isTopicNullOrEmpty() { - return (topic == null || topic.trim().isEmpty()); + boolean isApiSecretValid() { + return StringUtils.isNotBlank(apiSecret); } - boolean isPartitionIdNullOrEmpty() { - return (partitionId == null || partitionId.trim().isEmpty()); + boolean isUserNameValid() { + return StringUtils.isNotBlank(userName); } - public boolean isManaged() { - return managed; + boolean isPasswordValid() { + return StringUtils.isNotBlank(password); + } + + boolean isAdditionalPropsValid() { + return additionalProps != null; } public static class TopicParamsBuilder { - - BusTopicParams params = new BusTopicParams(); + + final BusTopicParams params = new BusTopicParams(); private TopicParamsBuilder() { } @@ -322,6 +364,27 @@ public class BusTopicParams { this.params.managed = managed; return this; } + + public TopicParamsBuilder hostname(String hostname) { + this.params.hostname = hostname; + return this; + } + + public TopicParamsBuilder clientName(String clientName) { + this.params.clientName = clientName; + return this; + } + + public TopicParamsBuilder port(int port) { + this.params.port = port; + return this; + } + + public TopicParamsBuilder basePath(String basePath) { + this.params.basePath = basePath; + return this; + } + } } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineBusTopicSink.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineBusTopicSink.java index 043b8673..6e8c4011 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineBusTopicSink.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/InlineBusTopicSink.java @@ -3,7 +3,7 @@ * policy-endpoints * ================================================================================ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. - * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd. + * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -66,7 +66,7 @@ public abstract class InlineBusTopicSink extends BusTopicBase implements BusTopi super(busTopicParams); - if (busTopicParams.isPartitionIdNullOrEmpty()) { + if (busTopicParams.isPartitionIdInvalid()) { this.partitionId = UUID.randomUUID().toString(); } else { this.partitionId = busTopicParams.getPartitionId(); diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedBusTopicSource.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedBusTopicSource.java index f9dd4852..0482e860 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedBusTopicSource.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedBusTopicSource.java @@ -3,7 +3,7 @@ * policy-endpoints * ================================================================================ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. - * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd. + * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -88,13 +88,13 @@ public abstract class SingleThreadedBusTopicSource extends BusTopicBase super(busTopicParams); - if (busTopicParams.isConsumerGroupNullOrEmpty()) { + if (busTopicParams.isConsumerGroupInvalid()) { this.consumerGroup = UUID.randomUUID().toString(); } else { this.consumerGroup = busTopicParams.getConsumerGroup(); } - if (busTopicParams.isConsumerInstanceNullOrEmpty()) { + if (busTopicParams.isConsumerInstanceInvalid()) { this.consumerInstance = NetworkUtil.getHostname(); } else { this.consumerInstance = busTopicParams.getConsumerInstance(); diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java index c002402d..ca10680b 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java @@ -3,6 +3,7 @@ * policy-endpoints * ================================================================================ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +29,8 @@ import java.util.HashMap; import java.util.List; import java.util.Properties; +import org.apache.commons.lang3.StringUtils; +import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; import org.onap.policy.common.endpoints.http.client.internal.JerseyClient; import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; import org.slf4j.Logger; @@ -41,8 +44,7 @@ public interface HttpClientFactory { /** * Build and http client with the following parameters. */ - public HttpClient build(String name, boolean https, boolean selfSignedCerts, String hostname, int port, - String baseUrl, String userName, String password, boolean managed) + public HttpClient build(BusTopicParams busTopicParams) throws KeyManagementException, NoSuchAlgorithmException; /** @@ -89,18 +91,17 @@ class IndexedHttpClientFactory implements HttpClientFactory { protected HashMap<String, HttpClient> clients = new HashMap<>(); @Override - public synchronized HttpClient build(String name, boolean https, boolean selfSignedCerts, String hostname, int port, - String baseUrl, String userName, String password, boolean managed) + public synchronized HttpClient build(BusTopicParams busTopicParams) throws KeyManagementException, NoSuchAlgorithmException { - if (clients.containsKey(name)) { - return clients.get(name); + if (clients.containsKey(busTopicParams.getClientName())) { + return clients.get(busTopicParams.getClientName()); } JerseyClient client = - new JerseyClient(name, https, selfSignedCerts, hostname, port, baseUrl, userName, password); + new JerseyClient(busTopicParams); - if (managed) { - clients.put(name, client); + if (busTopicParams.isManaged()) { + clients.put(busTopicParams.getClientName(), client); } return client; @@ -122,7 +123,7 @@ class IndexedHttpClientFactory implements HttpClientFactory { String httpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + clientName + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX); boolean https = false; - if (httpsString != null && !httpsString.isEmpty()) { + if (StringUtils.isNotBlank(httpsString)) { https = Boolean.parseBoolean(httpsString); } @@ -160,7 +161,17 @@ class IndexedHttpClientFactory implements HttpClientFactory { try { HttpClient client = - this.build(clientName, https, https, hostName, port, baseUrl, userName, password, managed); + this.build(BusTopicParams.builder() + .clientName(clientName) + .useHttps(https) + .allowSelfSignedCerts(https) + .hostname(hostName) + .port(port) + .basePath(baseUrl) + .userName(userName) + .password(password) + .managed(managed) + .build()); clientList.add(client); } catch (Exception e) { logger.error("http-client-factory: cannot build client {}", clientName, e); diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java index 1fc00bfe..c227071c 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java @@ -3,6 +3,7 @@ * policy-endpoints * ================================================================================ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,6 +37,7 @@ import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.core.Response; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; +import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; import org.onap.policy.common.endpoints.http.client.HttpClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -64,43 +66,42 @@ public class JerseyClient implements HttpClient { /** * Constructor. * - * @param name the name - * @param https is it https or not - * @param selfSignedCerts are there self signed certs - * @param hostname the hostname - * @param port port being used - * @param basePath base context - * @param userName user - * @param password password - * + * name the name + * https is it https or not + * selfSignedCerts are there self signed certs + * hostname the hostname + * port port being used + * basePath base context + * userName user + * password password + * @param busTopicParams Input parameters object * @throws KeyManagementException key exception * @throws NoSuchAlgorithmException no algorithm exception */ - public JerseyClient(String name, boolean https, boolean selfSignedCerts, String hostname, int port, String basePath, - String userName, String password) throws KeyManagementException, NoSuchAlgorithmException { + public JerseyClient(BusTopicParams busTopicParams) throws KeyManagementException, NoSuchAlgorithmException { super(); - if (name == null || name.isEmpty()) { + if (busTopicParams.isClientNameInvalid()) { throw new IllegalArgumentException("Name must be provided"); } - if (hostname == null || hostname.isEmpty()) { + if (busTopicParams.isHostnameInvalid()) { throw new IllegalArgumentException("Hostname must be provided"); } - if (port <= 0 && port >= 65535) { - throw new IllegalArgumentException("Invalid Port provided: " + port); + if (busTopicParams.isPortInvalid()) { + throw new IllegalArgumentException("Invalid Port provided: " + busTopicParams.getPort()); } - this.name = name; - this.https = https; - this.hostname = hostname; - this.port = port; - this.basePath = basePath; - this.userName = userName; - this.password = password; - this.selfSignedCerts = selfSignedCerts; + this.name = busTopicParams.getClientName(); + this.https = busTopicParams.isUseHttps(); + this.hostname = busTopicParams.getHostname(); + this.port = busTopicParams.getPort(); + this.basePath = busTopicParams.getBasePath(); + this.userName = busTopicParams.getUserName(); + this.password = busTopicParams.getPassword(); + this.selfSignedCerts = busTopicParams.isAllowSelfSignedCerts(); StringBuilder tmpBaseUrl = new StringBuilder(); if (this.https) { |