2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.util;
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;
18 * Created as a util for test tools
20 public final class InetSocketAddressUtil {
21 private static final String SEPARATOR = ",";
23 private InetSocketAddressUtil() {
24 throw new UnsupportedOperationException();
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));
31 public static List<InetSocketAddress> parseAddresses(final String address) {
32 return Lists.transform(Arrays.asList(address.split(SEPARATOR)), input -> getInetSocketAddress(input, null));
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));
40 return new InetSocketAddress(hostAndPort.getHostText(), hostAndPort.getPort());