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.util;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.net.InetAddresses;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.Collections;
18 import java.util.List;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
24 * Util class for creating generated Ipv6Address.
26 public final class Ipv6Util {
27 public static final int IPV6_LENGTH = 16;
28 public static final int IPV6_BITS_LENGTH = 128;
30 static final int PREFIX_BYTE_LENGTH = IPV6_LENGTH + 1;
32 private static final Ipv6Prefix EMPTY_PREFIX = new Ipv6Prefix("::/0");
39 * Creates uncompressed IP Address.
41 * @param ip to be uncompressed
42 * @return Ipv6Address with same, but uncompressed, value
44 public static Ipv6AddressNoZone getFullForm(final Ipv6AddressNoZone ip) {
45 return new Ipv6AddressNoZone(InetAddresses.forString(ip.getValue()).getHostAddress());
49 * Reads from ByteBuf buffer and converts bytes to Ipv6Address.
51 * @param buffer containing Ipv6 address, starting at reader index
54 public static Ipv6AddressNoZone addressForByteBuf(final ByteBuf buffer) {
55 return IetfInetUtil.INSTANCE.ipv6AddressNoZoneFor(ByteArray.readBytes(buffer, IPV6_LENGTH));
59 * From string ipAddress creates an InetAddress and puts it into ByteBuf.
60 * @param ipAddress Ipv6 address
61 * @return ByteBuf with filled in bytes from ipAddress
63 public static ByteBuf byteBufForAddress(final Ipv6AddressNoZone ipAddress) {
64 return Unpooled.wrappedBuffer(bytesForAddress(ipAddress));
68 * Converts Ipv6Address to byte array.
70 * @param address Ipv6Address to be converted
73 public static byte[] bytesForAddress(final Ipv6AddressNoZone address) {
74 return IetfInetUtil.INSTANCE.ipv6AddressNoZoneBytes(address);
78 * Converts Ipv6Prefix to byte array. Prefix length at the end.
80 * @param prefix Ipv6Prefix to be converted
81 * @return byte array with prefix length at the end
83 public static byte[] bytesForPrefix(final Ipv6Prefix prefix) {
84 return IetfInetUtil.INSTANCE.ipv6PrefixToBytes(prefix);
88 * Creates an Ipv6Prefix object from given byte array.
90 * @param bytes IPv6 address
91 * @param length prefix length
92 * @return Ipv6Prefix object
94 public static Ipv6Prefix prefixForBytes(final byte[] bytes, final int length) {
95 checkArgument(length <= bytes.length * Byte.SIZE);
98 if (bytes.length != IPV6_LENGTH) {
99 tmp = Arrays.copyOfRange(bytes, 0, IPV6_LENGTH);
104 return IetfInetUtil.INSTANCE.ipv6PrefixFor(tmp, length);
108 * Creates an Ipv6Prefix object from given ByteBuf. Prefix length is assumed to
109 * be in the left most byte of the buffer.
111 * @param buf IPv6 address
112 * @return Ipv6Prefix object
114 public static Ipv6Prefix prefixForByteBuf(final ByteBuf buf) {
115 final int prefixLength = buf.readUnsignedByte();
116 final int size = prefixLength / Byte.SIZE + (prefixLength % Byte.SIZE == 0 ? 0 : 1);
117 final int readable = buf.readableBytes();
118 checkArgument(size <= readable, "Illegal length of IP prefix: %s/%s", size, readable);
120 final byte[] bytes = new byte[IPV6_LENGTH];
121 buf.readBytes(bytes, 0, size);
122 return IetfInetUtil.INSTANCE.ipv6PrefixFor(bytes, prefixLength);
126 * Creates a list of Ipv6 Prefixes from given byte array.
128 * @param bytes to be converted to List of Ipv6Prefixes.
129 * @return A List of Ipv6Prefixes
131 public static List<Ipv6Prefix> prefixListForBytes(final byte[] bytes) {
132 if (bytes.length == 0) {
133 return Collections.emptyList();
135 final List<Ipv6Prefix> list = new ArrayList<>();
137 while (byteOffset < bytes.length) {
138 final int bitLength = Byte.toUnsignedInt(bytes[byteOffset]);
140 // if length == 0, default route will be added
141 if (bitLength == 0) {
142 list.add(EMPTY_PREFIX);
145 list.add(IetfInetUtil.INSTANCE.ipv6PrefixForShort(bytes, byteOffset, bitLength));
146 byteOffset += bitLength / Byte.SIZE;
147 if (bitLength % Byte.SIZE != 0) {
155 * Writes IPv6 address if not null, otherwise writes zeros to the
156 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
159 * IPv6 address to be written to the output.
161 * ByteBuf, where ipv6Address or zeros are written.
163 public static void writeIpv6Address(final Ipv6AddressNoZone ipv6Address, final ByteBuf output) {
164 if (ipv6Address != null) {
165 output.writeBytes(IetfInetUtil.INSTANCE.ipv6AddressNoZoneBytes(ipv6Address));
167 output.writeZero(IPV6_LENGTH);
172 * Writes IPv6 prefix if not null, otherwise writes zeros to the
173 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 17.
176 * IPv6 prefix to be written to the output. Prefix is written in
179 * ByteBuf, where ipv6Prefix or zeros are written.
181 public static void writeIpv6Prefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
182 if (ipv6Prefix != null) {
183 output.writeBytes(bytesForPrefix(ipv6Prefix));
185 output.writeZero(PREFIX_BYTE_LENGTH);
189 public static void writeMinimalPrefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
190 final byte[] bytes = IetfInetUtil.INSTANCE.ipv6PrefixToBytes(ipv6Prefix);
191 Ipv4Util.writeMinimalPrefix(output, bytes, bytes[IPV6_LENGTH]);