X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=concepts%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fprotocol%2Fconcepts%2FIpv6Util.java;h=e1b0460149c0ba5a17d3fe718625058ea345d9ad;hb=a397aa4828919cc643647c96f5c916ee211f597f;hp=ab7eea5a0155cdf0990e9b65f316e8fcccd7a21f;hpb=92489c5ab6540c805c69610e9ef3b5849a330b5e;p=bgpcep.git diff --git a/concepts/src/main/java/org/opendaylight/protocol/concepts/Ipv6Util.java b/concepts/src/main/java/org/opendaylight/protocol/concepts/Ipv6Util.java index ab7eea5a01..e1b0460149 100644 --- a/concepts/src/main/java/org/opendaylight/protocol/concepts/Ipv6Util.java +++ b/concepts/src/main/java/org/opendaylight/protocol/concepts/Ipv6Util.java @@ -7,6 +7,12 @@ */ package org.opendaylight.protocol.concepts; +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import com.google.common.net.InetAddresses; +import com.google.common.primitives.Bytes; +import com.google.common.primitives.UnsignedBytes; + import java.net.Inet6Address; import java.net.InetAddress; import java.net.UnknownHostException; @@ -18,107 +24,101 @@ import org.opendaylight.protocol.util.ByteArray; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix; -import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; -import com.google.common.net.InetAddresses; -import com.google.common.primitives.Bytes; -import com.google.common.primitives.UnsignedBytes; - /** * Util class for creating generated Ipv6Address. */ public final class Ipv6Util { - private Ipv6Util() { - } + private Ipv6Util() { + } - public static final int IPV6_LENGTH = 16; + public static final int IPV6_LENGTH = 16; - /** - * Converts byte array to Inet6Address. - * - * @param bytes to be converted - * @return InetAddress instance - * @throws IllegalArgumentException if {@link UnknownHostException} is thrown. - */ - private static InetAddress getAddress(final byte[] bytes) { - try { - return Inet6Address.getByAddress(bytes); - } catch (final UnknownHostException e) { - throw new IllegalArgumentException("Failed to construct IPv6 address", e); - } - } + /** + * Converts byte array to Inet6Address. + * + * @param bytes to be converted + * @return InetAddress instance + * @throws IllegalArgumentException if {@link UnknownHostException} is thrown. + */ + private static InetAddress getAddress(final byte[] bytes) { + try { + return Inet6Address.getByAddress(bytes); + } catch (final UnknownHostException e) { + throw new IllegalArgumentException("Failed to construct IPv6 address", e); + } + } - /** - * Converts byte array to Ipv6Address. - * - * @param bytes to be converted to Ipv6Address - * @return Ipv6Address - */ - public static Ipv6Address addressForBytes(final byte[] bytes) { - return new Ipv6Address(InetAddresses.toAddrString(getAddress(bytes))); - } + /** + * Converts byte array to Ipv6Address. + * + * @param bytes to be converted to Ipv6Address + * @return Ipv6Address + */ + public static Ipv6Address addressForBytes(final byte[] bytes) { + return new Ipv6Address(InetAddresses.toAddrString(getAddress(bytes))); + } - /** - * Converts Ipv6Address to byte array. - * - * @param address Ipv6Address to be converted - * @return byte array - */ - public static byte[] bytesForAddress(final Ipv6Address address) { - final InetAddress a = InetAddresses.forString(address.getValue()); - Preconditions.checkArgument(a instanceof Inet6Address); - return a.getAddress(); - } + /** + * Converts Ipv6Address to byte array. + * + * @param address Ipv6Address to be converted + * @return byte array + */ + public static byte[] bytesForAddress(final Ipv6Address address) { + final InetAddress a = InetAddresses.forString(address.getValue()); + Preconditions.checkArgument(a instanceof Inet6Address); + return a.getAddress(); + } - /** - * Converts Ipv6Prefix to byte array. - * - * @param prefix Ipv6Prefix to be converted - * @return byte array - */ - public static byte[] bytesForPrefix(final Ipv6Prefix prefix) { - final String p = prefix.getValue(); - final int sep = p.indexOf('/'); - final InetAddress a = InetAddresses.forString(p.substring(0, sep)); - Preconditions.checkArgument(a instanceof Inet6Address); - final byte[] bytes = a.getAddress(); - return Bytes.concat(bytes, new byte[] { Byte.valueOf(p.substring(sep + 1, p.length())) }); - } + /** + * Converts Ipv6Prefix to byte array. + * + * @param prefix Ipv6Prefix to be converted + * @return byte array + */ + public static byte[] bytesForPrefix(final Ipv6Prefix prefix) { + final String p = prefix.getValue(); + final int sep = p.indexOf('/'); + final InetAddress a = InetAddresses.forString(p.substring(0, sep)); + Preconditions.checkArgument(a instanceof Inet6Address); + final byte[] bytes = a.getAddress(); + return Bytes.concat(bytes, new byte[] { Byte.valueOf(p.substring(sep + 1, p.length())) }); + } - /** - * Creates an Ipv6Prefix object from given byte array. - * - * @param bytes IPv6 address - * @param length prefix length - * @return Ipv6Prefix object - */ - public static Ipv6Prefix prefixForBytes(final byte[] bytes, final int length) { - Preconditions.checkArgument(length <= bytes.length * Byte.SIZE); - final byte[] tmp = Arrays.copyOfRange(bytes, 0, IPV6_LENGTH); - final InetAddress a = getAddress(tmp); - return new Ipv6Prefix(InetAddresses.toAddrString(a) + '/' + length); - } + /** + * Creates an Ipv6Prefix object from given byte array. + * + * @param bytes IPv6 address + * @param length prefix length + * @return Ipv6Prefix object + */ + public static Ipv6Prefix prefixForBytes(final byte[] bytes, final int length) { + Preconditions.checkArgument(length <= bytes.length * Byte.SIZE); + final byte[] tmp = Arrays.copyOfRange(bytes, 0, IPV6_LENGTH); + final InetAddress a = getAddress(tmp); + return new Ipv6Prefix(InetAddresses.toAddrString(a) + '/' + length); + } - /** - * Creates a list of Ipv6 Prefixes from given byte array. - * - * @param bytes to be converted to List of Ipv6Prefixes. - * @return List - */ - public static List prefixListForBytes(final byte[] bytes) { - if (bytes.length == 0) { - return Collections.emptyList(); - } + /** + * Creates a list of Ipv6 Prefixes from given byte array. + * + * @param bytes to be converted to List of Ipv6Prefixes. + * @return List + */ + public static List prefixListForBytes(final byte[] bytes) { + if (bytes.length == 0) { + return Collections.emptyList(); + } - final List list = Lists.newArrayList(); - int byteOffset = 0; - while (byteOffset < bytes.length) { - final int bitLength = UnsignedBytes.toInt(ByteArray.subByte(bytes, byteOffset, 1)[0]); - byteOffset += 1; - final int byteCount = (bitLength % Byte.SIZE != 0) ? (bitLength / Byte.SIZE) + 1 : bitLength / Byte.SIZE; - list.add(prefixForBytes(ByteArray.subByte(bytes, byteOffset, byteCount), bitLength)); - byteOffset += byteCount; - } - return list; - } + final List list = Lists.newArrayList(); + int byteOffset = 0; + while (byteOffset < bytes.length) { + final int bitLength = UnsignedBytes.toInt(ByteArray.subByte(bytes, byteOffset, 1)[0]); + byteOffset += 1; + final int byteCount = (bitLength % Byte.SIZE != 0) ? (bitLength / Byte.SIZE) + 1 : bitLength / Byte.SIZE; + list.add(prefixForBytes(ByteArray.subByte(bytes, byteOffset, byteCount), bitLength)); + byteOffset += byteCount; + } + return list; + } }