correct a typo in doc APEX User Manual
[policy/apex-pdp.git] / context / context-test-utils / src / main / java / org / onap / policy / apex / context / test / utils / NetworkUtils.java
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.context.test.utils;
22
23 import java.net.InetAddress;
24 import java.net.NetworkInterface;
25 import java.net.SocketException;
26 import java.util.Collections;
27 import java.util.Enumeration;
28 import java.util.SortedSet;
29 import java.util.TreeSet;
30
31 /**
32  * The Class NetworkUtils contains some utility functions for getting network information for context tests.
33  */
34 public class NetworkUtils {
35
36     /**
37      * Instantiates a new network utils.
38      */
39     private NetworkUtils() {}
40
41     /**
42      * The JGroups IP address must be set to a real (not loopback) IP address for Infinispan to
43      * work. In order to ensure that all the JVMs in a test pick up the same IP address, this
44      * function sets the address to be the first non-loopback IPv4 address on a host
45      * 
46      * @return Set of IPv4 addresses
47      * @throws SocketException throw socket exception if error occurs
48      */
49     public static SortedSet<String> getIPv4NonLoopAddresses() throws SocketException {
50         final TreeSet<String> ipAddressSet = new TreeSet<>();
51
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());
59                 }
60             }
61         }
62         return ipAddressSet;
63     }
64
65
66 }