Remove useless UnsupportedOperationExceptions
[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 package org.opendaylight.protocol.util;
9
10 import com.google.common.collect.Lists;
11 import com.google.common.net.HostAndPort;
12 import java.net.InetSocketAddress;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
16 import java.util.stream.Collectors;
17
18 /**
19  * Created as a util for test tools.
20  */
21 public final class InetSocketAddressUtil {
22     private static final String SEPARATOR = ",";
23     private static final List<String> ASSIGNED_IPS = new ArrayList<>();
24     private static final List<Integer> ASSIGNED_PORTS = new ArrayList<>();
25
26     private InetSocketAddressUtil() {
27         // Hidden on purpose
28     }
29
30     public static List<InetSocketAddress> parseAddresses(final String address, final int defaultPort) {
31         return Arrays.asList(address.split(SEPARATOR)).stream()
32                 .map(input -> getInetSocketAddress(input, defaultPort)).collect(Collectors.toList());
33     }
34
35     public static List<InetSocketAddress> parseAddresses(final String address) {
36         return Lists.transform(Arrays.asList(address.split(SEPARATOR)), input -> getInetSocketAddress(input, null));
37     }
38
39     public static HostAndPort toHostAndPort(final InetSocketAddress address) {
40         return HostAndPort.fromParts(address.getHostString(), address.getPort());
41     }
42
43     public static InetSocketAddress getInetSocketAddress(final String hostPortString, final Integer defaultPort) {
44         final HostAndPort hostAndPort = HostAndPort.fromString(hostPortString);
45         if (defaultPort != null) {
46             return new InetSocketAddress(hostAndPort.getHost(), hostAndPort.getPortOrDefault(defaultPort));
47         }
48         return new InetSocketAddress(hostAndPort.getHost(), hostAndPort.getPort());
49     }
50
51     public static synchronized InetSocketAddress getRandomLoopbackInetSocketAddress(final int port) {
52         String newIp;
53         do {
54             newIp = getRandomLoopbackIpAddress();
55         } while (ASSIGNED_IPS.contains(newIp));
56         ASSIGNED_IPS.add(newIp);
57         return new InetSocketAddress(getRandomLoopbackIpAddress(), port);
58     }
59
60     public static InetSocketAddress getRandomLoopbackInetSocketAddress() {
61         return getRandomLoopbackInetSocketAddress(getRandomPort());
62     }
63
64     /**
65      * Generate a random high range port number.
66      *
67      * @return A port number range from 20000 to 60000
68      */
69     public static synchronized int getRandomPort() {
70         int port;
71         do {
72             port = 20000 + (int) Math.round(40000 * Math.random());
73         } while (ASSIGNED_PORTS.contains(port));
74         ASSIGNED_PORTS.add(port);
75         return port;
76     }
77
78     /**
79      * Generate a random loopback ip address.
80      * IP address range: 127.50.50.50 ~ 127.250.250.250
81      * We did not utilize the whole 127./8 range to avoid using common addresses like 127.0.0.1
82      *
83      * @return Generated random loopback IP address
84      */
85     public static String getRandomLoopbackIpAddress() {
86         final StringBuilder sb = new StringBuilder("127");
87         for (int i = 0; i < 3; i++) {
88             sb.append(".").append(50 + (int) Math.round(Math.random() * 200));
89         }
90         return sb.toString();
91     }
92 }