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.Ipv6Address;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
25 * Util class for creating generated Ipv6Address.
27 public final class Ipv6Util {
28 public static final int IPV6_LENGTH = 16;
29 public static final int IPV6_BITS_LENGTH = 128;
31 static final int PREFIX_BYTE_LENGTH = IPV6_LENGTH + 1;
33 private static final Ipv6Prefix EMPTY_PREFIX = new Ipv6Prefix("::/0");
40 * Creates uncompressed IP Address.
42 * @param ip to be uncompressed
43 * @return Ipv6Address with same, but uncompressed, value
45 public static Ipv6Address getFullForm(final Ipv6Address ip) {
46 return new Ipv6Address(InetAddresses.forString(ip.getValue()).getHostAddress());
50 * Reads from ByteBuf buffer and converts bytes to Ipv6Address.
52 * @param buffer containing Ipv6 address, starting at reader index
55 public static Ipv6Address addressForByteBuf(final ByteBuf buffer) {
56 return IetfInetUtil.INSTANCE.ipv6AddressFor(ByteArray.readBytes(buffer, IPV6_LENGTH));
60 * Reads from ByteBuf buffer and converts bytes to Ipv6AddressNoZone.
62 * @param buffer containing Ipv6 address, starting at reader index
63 * @return Ipv6AddressNoZone
65 public static Ipv6AddressNoZone noZoneAddressForByteBuf(final ByteBuf buffer) {
66 return IetfInetUtil.INSTANCE.ipv6AddressNoZoneFor(ByteArray.readBytes(buffer, IPV6_LENGTH));
70 * From string ipAddress creates an InetAddress and puts it into ByteBuf.
71 * @param ipAddress Ipv6 address
72 * @return ByteBuf with filled in bytes from ipAddress
74 public static ByteBuf byteBufForAddress(final Ipv6Address ipAddress) {
75 return Unpooled.wrappedBuffer(bytesForAddress(ipAddress));
79 * Converts Ipv6Address to byte array.
81 * @param address Ipv6Address to be converted
84 public static byte[] bytesForAddress(final Ipv6Address address) {
85 return IetfInetUtil.INSTANCE.ipv6AddressBytes(address);
89 * Converts Ipv6Prefix to byte array. Prefix length at the end.
91 * @param prefix Ipv6Prefix to be converted
92 * @return byte array with prefix length at the end
94 public static byte[] bytesForPrefix(final Ipv6Prefix prefix) {
95 return IetfInetUtil.INSTANCE.ipv6PrefixToBytes(prefix);
99 * Creates an Ipv6Prefix object from given byte array.
101 * @param bytes IPv6 address
102 * @param length prefix length
103 * @return Ipv6Prefix object
105 public static Ipv6Prefix prefixForBytes(final byte[] bytes, final int length) {
106 checkArgument(length <= bytes.length * Byte.SIZE);
109 if (bytes.length != IPV6_LENGTH) {
110 tmp = Arrays.copyOfRange(bytes, 0, IPV6_LENGTH);
115 return IetfInetUtil.INSTANCE.ipv6PrefixFor(tmp, length);
119 * Creates an Ipv6Prefix object from given ByteBuf. Prefix length is assumed to
120 * be in the left most byte of the buffer.
122 * @param buf IPv6 address
123 * @return Ipv6Prefix object
125 public static Ipv6Prefix prefixForByteBuf(final ByteBuf buf) {
126 final int prefixLength = buf.readUnsignedByte();
127 final int size = prefixLength / Byte.SIZE + (prefixLength % Byte.SIZE == 0 ? 0 : 1);
128 final int readable = buf.readableBytes();
129 checkArgument(size <= readable, "Illegal length of IP prefix: %s/%s", size, readable);
131 final byte[] bytes = new byte[IPV6_LENGTH];
132 buf.readBytes(bytes, 0, size);
133 return IetfInetUtil.INSTANCE.ipv6PrefixFor(bytes, prefixLength);
137 * Creates a list of Ipv6 Prefixes from given byte array.
139 * @param bytes to be converted to List of Ipv6Prefixes.
140 * @return A List of Ipv6Prefixes
142 public static List<Ipv6Prefix> prefixListForBytes(final byte[] bytes) {
143 if (bytes.length == 0) {
144 return Collections.emptyList();
146 final List<Ipv6Prefix> list = new ArrayList<>();
148 while (byteOffset < bytes.length) {
149 final int bitLength = Byte.toUnsignedInt(bytes[byteOffset]);
151 // if length == 0, default route will be added
152 if (bitLength == 0) {
153 list.add(EMPTY_PREFIX);
156 list.add(IetfInetUtil.INSTANCE.ipv6PrefixForShort(bytes, byteOffset, bitLength));
157 byteOffset += bitLength / Byte.SIZE;
158 if (bitLength % Byte.SIZE != 0) {
166 * Writes IPv6 address if not null, otherwise writes zeros to the
167 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
170 * IPv6 address to be written to the output.
172 * ByteBuf, where ipv6Address or zeros are written.
174 public static void writeIpv6Address(final Ipv6Address ipv6Address, final ByteBuf output) {
175 if (ipv6Address != null) {
176 output.writeBytes(bytesForAddress(ipv6Address));
178 output.writeZero(IPV6_LENGTH);
183 * Writes IPv6 address if not null, otherwise writes zeros to the
184 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
187 * IPv6 address to be written to the output.
189 * ByteBuf, where ipv6Address or zeros are written.
191 public static void writeIpv6Address(final Ipv6AddressNoZone ipv6Address, final ByteBuf output) {
192 if (ipv6Address != null) {
193 output.writeBytes(IetfInetUtil.INSTANCE.ipv6AddressNoZoneBytes(ipv6Address));
195 output.writeZero(IPV6_LENGTH);
200 * Writes IPv6 prefix if not null, otherwise writes zeros to the
201 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 17.
204 * IPv6 prefix to be written to the output. Prefix is written in
207 * ByteBuf, where ipv6Prefix or zeros are written.
209 public static void writeIpv6Prefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
210 if (ipv6Prefix != null) {
211 output.writeBytes(bytesForPrefix(ipv6Prefix));
213 output.writeZero(PREFIX_BYTE_LENGTH);
217 public static void writeMinimalPrefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
218 final byte[] bytes = IetfInetUtil.INSTANCE.ipv6PrefixToBytes(ipv6Prefix);
219 Ipv4Util.writeMinimalPrefix(output, bytes, bytes[IPV6_LENGTH]);