2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.policy.apex.context.test.utils;
22 import java.net.InetAddress;
23 import java.net.NetworkInterface;
24 import java.net.SocketException;
25 import java.util.Collections;
26 import java.util.Enumeration;
27 import java.util.TreeSet;
29 public class NetworkUtils {
31 private NetworkUtils() {}
34 * The JGroups IP address must be set to a real (not loopback) IP address for Infinispan to
35 * work. In order to ensure that all the JVMs in a test pick up the same IP address, this
36 * function sets the address to be the first non-loopback IPv4 address on a host
38 * @return Set of IPv4 addresses
39 * @throws SocketException
41 public static TreeSet<String> getIPv4NonLoopAddresses() throws SocketException {
42 final TreeSet<String> ipAddressSet = new TreeSet<String>();
44 final Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
45 for (final NetworkInterface netint : Collections.list(nets)) {
46 final Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
47 for (final InetAddress inetAddress : Collections.list(inetAddresses)) {
48 // Look for real IPv4 internet addresses
49 if (!inetAddress.isLoopbackAddress() && inetAddress.getAddress().length == 4) {
50 ipAddressSet.add(inetAddress.getHostAddress());