f92debe3c1a9823760cd63d14a75f2e37fedd831
[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 InetSocketAddress getInetSocketAddress(final String hostPortString, final Integer defaultPort) {
36         final HostAndPort hostAndPort = HostAndPort.fromString(hostPortString);
37         if (defaultPort != null) {
38             return new InetSocketAddress(hostAndPort.getHostText(), hostAndPort.getPortOrDefault(defaultPort));
39         }
40         return new InetSocketAddress(hostAndPort.getHostText(), hostAndPort.getPort());
41     }
42 }