BUG-5619 Enable maven parallel build for bgpcep I
[bgpcep.git] / testtool-util / src / main / java / org / opendaylight / protocol / util / InetSocketAddressUtil.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.protocol.util;
10
11 import com.google.common.collect.Lists;
12 import com.google.common.net.HostAndPort;
13 import java.net.InetSocketAddress;
14 import java.util.Arrays;
15 import java.util.List;
16
17 /**
18  * Created as a util for test tools
19  */
20 public final class InetSocketAddressUtil {
21     private static final String SEPARATOR = ",";
22
23     private InetSocketAddressUtil() {
24         throw new UnsupportedOperationException();
25     }
26
27     public static List<InetSocketAddress> parseAddresses(final String address, final int defaultPort) {
28         return Lists.transform(Arrays.asList(address.split(SEPARATOR)), input -> getInetSocketAddress(input, defaultPort));
29     }
30
31     public static List<InetSocketAddress> parseAddresses(final String address) {
32         return Lists.transform(Arrays.asList(address.split(SEPARATOR)), input -> getInetSocketAddress(input, null));
33     }
34
35     public static HostAndPort toHostAndPort(final InetSocketAddress address) {
36         return HostAndPort.fromParts(address.getHostString(), address.getPort());
37     }
38
39     public static InetSocketAddress getInetSocketAddress(final String hostPortString, final Integer defaultPort) {
40         final HostAndPort hostAndPort = HostAndPort.fromString(hostPortString);
41         if (defaultPort != null) {
42             return new InetSocketAddress(hostAndPort.getHostText(), hostAndPort.getPortOrDefault(defaultPort));
43         }
44         return new InetSocketAddress(hostAndPort.getHostText(), hostAndPort.getPort());
45     }
46
47     public static InetSocketAddress getRandomLoopbackInetSocketAddress(final int port) {
48         return new InetSocketAddress(getRandomLoopbackIpAddress(), port);
49     }
50
51     public static InetSocketAddress getRandomLoopbackInetSocketAddress() {
52         return getRandomLoopbackInetSocketAddress(getRandomPort());
53     }
54
55     /**
56      * Generate a random high range port number
57      *
58      * @return A port number range from 10000 to 50000
59      */
60     public static int getRandomPort() {
61         final int randPort = 10000 + (int) Math.round(40000 * Math.random());
62         return randPort;
63     }
64
65     /**
66      * Generate a random loopback ip address
67      * IP address range: 127.50.50.50 ~ 127.250.250.250
68      * We did not utilize the whole 127./8 range to avoid using common addresses like 127.0.0.1
69      * @return Generated random loopback IP address
70      */
71     public static String getRandomLoopbackIpAddress() {
72         final StringBuilder sb = new StringBuilder("127");
73         for (int i = 0; i < 3; i++) {
74             sb.append(".").append(50 + (int) Math.round(Math.random() * 200));
75         }
76         return sb.toString();
77     }
78 }