aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAjith Sreekumar <ajith.sreekumar@bell.ca>2021-06-29 08:28:39 +0000
committerGerrit Code Review <gerrit@onap.org>2021-06-29 08:28:39 +0000
commitfbaec67cb146add874f2aedeb5ba624f74e62d5e (patch)
tree64ed7ab6ce0f15f5cc898be84eb9b8dcd8ec55da
parent3fa5e305fa2d49bfb0614ac7a99d7054d908586a (diff)
parent296fc9c47661e2b9e7d4e5293f1b25fc0cfc7d28 (diff)
Merge "Add name generator for PAP and PDPs"
-rw-r--r--utils/src/main/java/org/onap/policy/common/utils/network/NetworkUtil.java12
-rw-r--r--utils/src/test/java/org/onap/policy/common/utils/network/NetworkUtilTest.java12
2 files changed, 23 insertions, 1 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 e3539fb7..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
@@ -26,6 +26,7 @@ import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
+import java.util.UUID;
import javax.net.ssl.TrustManager;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
@@ -170,4 +171,15 @@ public final 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();
+ }
}
diff --git a/utils/src/test/java/org/onap/policy/common/utils/network/NetworkUtilTest.java b/utils/src/test/java/org/onap/policy/common/utils/network/NetworkUtilTest.java
index 4ae72842..4019ca79 100644
--- a/utils/src/test/java/org/onap/policy/common/utils/network/NetworkUtilTest.java
+++ b/utils/src/test/java/org/onap/policy/common/utils/network/NetworkUtilTest.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* policy-utils
* ================================================================================
- * Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018-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.
@@ -20,6 +20,7 @@
package org.onap.policy.common.utils.network;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
@@ -91,6 +92,15 @@ public class NetworkUtilTest {
}
}
+ @Test
+ public void testGenUniqueName() {
+ String name = NetworkUtil.genUniqueName(LOCALHOST);
+ assertThat(name).isNotBlank().isNotEqualTo(LOCALHOST);
+
+ // second call should generate a different value
+ assertThat(NetworkUtil.genUniqueName(LOCALHOST)).isNotEqualTo(name);
+ }
+
/**
* Thread that accepts a connection on a socket.
*/