2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.testsuites.integration.context.utils;
24 import java.net.InetAddress;
25 import java.net.NetworkInterface;
26 import java.net.SocketException;
27 import java.util.Collections;
28 import java.util.Enumeration;
29 import java.util.SortedSet;
30 import java.util.TreeSet;
33 * The Class NetworkUtils contains some utility functions for getting network information for context tests.
35 public class NetworkUtils {
38 * Instantiates a new network utils.
40 private NetworkUtils() {}
43 * The JGroups IP address must be set to a real (not loopback) IP address for Infinispan to
44 * work. In order to ensure that all the JVMs in a test pick up the same IP address, this
45 * function sets the address to be the first non-loopback IPv4 address on a host
47 * @return Set of IPv4 addresses
48 * @throws SocketException throw socket exception if error occurs
50 public static SortedSet<String> getIPv4NonLoopAddresses() throws SocketException {
51 final TreeSet<String> ipAddressSet = new TreeSet<>();
53 final Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
54 for (final NetworkInterface netint : Collections.list(nets)) {
55 final Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
56 for (final InetAddress inetAddress : Collections.list(inetAddresses)) {
57 // Look for real IPv4 internet addresses
58 if (!inetAddress.isLoopbackAddress() && inetAddress.getAddress().length == 4) {
59 ipAddressSet.add(inetAddress.getHostAddress());