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;
16 import java.net.Inet6Address;
17 import java.net.InetAddress;
18 import java.net.UnknownHostException;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.List;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
28 * Util class for creating generated Ipv6Address.
30 public final class Ipv6Util {
34 public static final int IPV6_LENGTH = 16;
37 * Converts byte array to Inet6Address.
39 * @param bytes to be converted
40 * @return InetAddress instance
41 * @throws IllegalArgumentException if {@link UnknownHostException} is thrown.
43 private static InetAddress getAddress(final byte[] bytes) {
45 return Inet6Address.getByAddress(bytes);
46 } catch (final UnknownHostException e) {
47 throw new IllegalArgumentException("Failed to construct IPv6 address", e);
52 * Converts byte array to Ipv6Address.
54 * @param bytes to be converted to Ipv6Address
57 public static Ipv6Address addressForBytes(final byte[] bytes) {
58 return new Ipv6Address(InetAddresses.toAddrString(getAddress(bytes)));
62 * Converts Ipv6Address to byte array.
64 * @param address Ipv6Address to be converted
67 public static byte[] bytesForAddress(final Ipv6Address address) {
68 final InetAddress a = InetAddresses.forString(address.getValue());
69 Preconditions.checkArgument(a instanceof Inet6Address);
70 return a.getAddress();
74 * Converts Ipv6Prefix to byte array.
76 * @param prefix Ipv6Prefix to be converted
79 public static byte[] bytesForPrefix(final Ipv6Prefix prefix) {
80 final String p = prefix.getValue();
81 final int sep = p.indexOf('/');
82 final InetAddress a = InetAddresses.forString(p.substring(0, sep));
83 Preconditions.checkArgument(a instanceof Inet6Address);
84 final byte[] bytes = a.getAddress();
85 return Bytes.concat(bytes, new byte[] { Byte.valueOf(p.substring(sep + 1, p.length())) });
89 * Creates an Ipv6Prefix object from given byte array.
91 * @param bytes IPv6 address
92 * @param length prefix length
93 * @return Ipv6Prefix object
95 public static Ipv6Prefix prefixForBytes(final byte[] bytes, final int length) {
96 Preconditions.checkArgument(length <= bytes.length * Byte.SIZE);
97 final byte[] tmp = Arrays.copyOfRange(bytes, 0, IPV6_LENGTH);
98 final InetAddress a = getAddress(tmp);
99 return new Ipv6Prefix(InetAddresses.toAddrString(a) + '/' + length);
103 * Creates a list of Ipv6 Prefixes from given byte array.
105 * @param bytes to be converted to List of Ipv6Prefixes.
106 * @return List<Ipv6Prefix>
108 public static List<Ipv6Prefix> prefixListForBytes(final byte[] bytes) {
109 if (bytes.length == 0) {
110 return Collections.emptyList();
113 final List<Ipv6Prefix> list = Lists.newArrayList();
115 while (byteOffset < bytes.length) {
116 final int bitLength = UnsignedBytes.toInt(ByteArray.subByte(bytes, byteOffset, 1)[0]);
118 final int byteCount = (bitLength % Byte.SIZE != 0) ? (bitLength / Byte.SIZE) + 1 : bitLength / Byte.SIZE;
119 list.add(prefixForBytes(ByteArray.subByte(bytes, byteOffset, byteCount), bitLength));
120 byteOffset += byteCount;