diff options
Diffstat (limited to 'utils/src/main/java/org/onap/policy/common/utils/network/NetworkUtil.java')
-rw-r--r-- | utils/src/main/java/org/onap/policy/common/utils/network/NetworkUtil.java | 64 |
1 files changed, 30 insertions, 34 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/network/NetworkUtil.java b/utils/src/main/java/org/onap/policy/common/utils/network/NetworkUtil.java index 14362627..6698d7cf 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/network/NetworkUtil.java +++ b/utils/src/main/java/org/onap/policy/common/utils/network/NetworkUtil.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,16 +26,19 @@ import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; -import java.security.cert.X509Certificate; +import java.util.UUID; import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.apache.commons.net.util.TrustManagerUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Network Utilities. */ -public class NetworkUtil { +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class NetworkUtil { public static final Logger logger = LoggerFactory.getLogger(NetworkUtil.class.getName()); @@ -48,33 +51,7 @@ public class NetworkUtil { /** * A trust manager that always trusts certificates. */ - // @formatter:off - private static final TrustManager[] ALWAYS_TRUST_MANAGER = new TrustManager[] { - new X509TrustManager() { - - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; - } - - @Override - public void checkClientTrusted(final java.security.cert.X509Certificate[] certs, - final String authType) { - // always trust - } - - @Override - public void checkServerTrusted(final java.security.cert.X509Certificate[] certs, - final String authType) { - // always trust - } - } - }; - // @formatter:on - - private NetworkUtil() { - // Empty constructor - } + private static final TrustManager[] ALWAYS_TRUST_MANAGER = { TrustManagerUtils.getAcceptAllTrustManager() }; /** * Allocates an available port on which a server may listen. @@ -105,7 +82,11 @@ public class NetworkUtil { * @throws IOException if a socket cannot be created */ public static int allocPort(InetSocketAddress hostAddr) throws IOException { - try (ServerSocket socket = new ServerSocket()) { + /* + * The socket is only used to find an unused address for a new server. As a + * result, it poses no security risk, thus the sonar issue can be ignored. + */ + try (ServerSocket socket = new ServerSocket()) { // NOSONAR socket.bind(hostAddr); return socket.getLocalPort(); @@ -132,9 +113,13 @@ public class NetworkUtil { */ public static boolean isTcpPortOpen(String host, int port, int retries, long interval) throws InterruptedException { - int retry = 0; + var retry = 0; while (retry < retries) { - try (Socket s = new Socket(host, port)) { + /* + * As with the server socket, this is only used to see if the port is open, + * thus the sonar issue can be ignored. + */ + try (Socket s = new Socket(host, port)) { // NOSONAR logger.debug("{}:{} connected - retries={} interval={}", host, port, retries, interval); return true; } catch (final IOException e) { @@ -186,4 +171,15 @@ public class NetworkUtil { return "127.0.0.1"; } + + /** + * Generates a globally unique name, typically for use in PDP messages, to uniquely + * identify a PDP (or PAP), regardless on what cluster it resides. + * + * @param prefix text to be prepended to the generated value + * @return a globally unique name + */ + public static String genUniqueName(String prefix) { + return prefix + "-" + UUID.randomUUID(); + } } |