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