2 * Copyright (c) 2013 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
8 package org.opendaylight.protocol.concepts;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Lists;
12 import com.google.common.net.InetAddresses;
13 import com.google.common.primitives.Bytes;
14 import com.google.common.primitives.UnsignedBytes;
15 import java.net.Inet6Address;
16 import java.net.InetAddress;
17 import java.net.UnknownHostException;
18 import java.util.Arrays;
19 import java.util.Collections;
20 import java.util.List;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
26 * Util class for creating generated Ipv6Address.
28 public final class Ipv6Util {
32 public static final int IPV6_LENGTH = 16;
35 * Converts byte array to Inet6Address.
37 * @param bytes to be converted
38 * @return InetAddress instance
39 * @throws IllegalArgumentException if {@link UnknownHostException} is thrown.
41 private static InetAddress getAddress(final byte[] bytes) {
43 return Inet6Address.getByAddress(bytes);
44 } catch (final UnknownHostException e) {
45 throw new IllegalArgumentException("Failed to construct IPv6 address", e);
50 * Converts byte array to Ipv6Address.
52 * @param bytes to be converted to Ipv6Address
55 public static Ipv6Address addressForBytes(final byte[] bytes) {
56 return new Ipv6Address(InetAddresses.toAddrString(getAddress(bytes)));
60 * Converts Ipv6Address to byte array.
62 * @param address Ipv6Address to be converted
65 public static byte[] bytesForAddress(final Ipv6Address address) {
66 final InetAddress a = InetAddresses.forString(address.getValue());
67 Preconditions.checkArgument(a instanceof Inet6Address);
68 return a.getAddress();
73 * Converts Ipv6Prefix to byte array of (prefix bit size/8) size.
75 * @param ipv6Prefix Ipv6Prefix to be converted
78 public static byte[] bytesForPrefixByPrefixLength(Ipv6Prefix ipv6Prefix) {
79 return ByteArray.subByte(Ipv6Util.bytesForPrefix(ipv6Prefix), 0,
80 Ipv4Util.getPrefixLengthBytes(ipv6Prefix.getValue()));
85 * Converts Ipv6Prefix to byte array.
87 * @param prefix Ipv6Prefix to be converted
90 public static byte[] bytesForPrefix(final Ipv6Prefix prefix) {
91 final String p = prefix.getValue();
92 final int sep = p.indexOf('/');
93 final InetAddress a = InetAddresses.forString(p.substring(0, sep));
94 Preconditions.checkArgument(a instanceof Inet6Address);
95 final byte[] bytes = a.getAddress();
96 return Bytes.concat(bytes, new byte[] { Byte.valueOf(p.substring(sep + 1, p.length())) });
100 * Creates an Ipv6Prefix object from given byte array.
102 * @param bytes IPv6 address
103 * @param length prefix length
104 * @return Ipv6Prefix object
106 public static Ipv6Prefix prefixForBytes(final byte[] bytes, final int length) {
107 Preconditions.checkArgument(length <= bytes.length * Byte.SIZE);
108 final byte[] tmp = Arrays.copyOfRange(bytes, 0, IPV6_LENGTH);
109 final InetAddress a = getAddress(tmp);
110 return new Ipv6Prefix(InetAddresses.toAddrString(a) + '/' + length);
114 * Creates a list of Ipv6 Prefixes from given byte array.
116 * @param bytes to be converted to List of Ipv6Prefixes.
117 * @return List<Ipv6Prefix>
119 public static List<Ipv6Prefix> prefixListForBytes(final byte[] bytes) {
120 if (bytes.length == 0) {
121 return Collections.emptyList();
124 final List<Ipv6Prefix> list = Lists.newArrayList();
126 while (byteOffset < bytes.length) {
127 final int bitLength = UnsignedBytes.toInt(ByteArray.subByte(bytes, byteOffset, 1)[0]);
129 final int byteCount = (bitLength % Byte.SIZE != 0) ? (bitLength / Byte.SIZE) + 1 : bitLength / Byte.SIZE;
130 list.add(prefixForBytes(ByteArray.subByte(bytes, byteOffset, byteCount), bitLength));
131 byteOffset += byteCount;