Change versions of Zookeeper and C3P0
[policy/apex-pdp.git] / testsuites / integration / integration-context-test / src / test / java / org / onap / policy / apex / testsuites / integration / context / utils / NetworkUtils.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.integration.context.utils;
23
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;
31
32 /**
33  * The Class NetworkUtils contains some utility functions for getting network information for context tests.
34  */
35 public class NetworkUtils {
36
37     /**
38      * Instantiates a new network utils.
39      */
40     private NetworkUtils() {}
41
42     /**
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
46      *
47      * @return Set of IPv4 addresses
48      * @throws SocketException throw socket exception if error occurs
49      */
50     public static SortedSet<String> getIPv4NonLoopAddresses() throws SocketException {
51         final TreeSet<String> ipAddressSet = new TreeSet<>();
52
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());
60                 }
61             }
62         }
63         return ipAddressSet;
64     }
65
66
67 }