ac5085f257919c750c571b94bbf7a0c4888f476f
[bgpcep.git] / util / src / main / java / org / opendaylight / protocol / util / Ipv6Util.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.util;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.net.InetAddresses;
12 import com.google.common.primitives.UnsignedBytes;
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.rev100924.IetfInetUtil;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
22
23 /**
24  * Util class for creating generated Ipv6Address.
25  */
26 public final class Ipv6Util {
27     public static final int IPV6_LENGTH = 16;
28     private static final Ipv6Prefix EMPTY_PREFIX = new Ipv6Prefix("::/0");
29
30     private Ipv6Util() {
31         throw new UnsupportedOperationException();
32     }
33
34     /**
35      * Creates uncompressed IP Address
36      *
37      * @param ip to be uncompressed
38      * @return Ipv6Address with same, but uncompressed, value
39      */
40     public static Ipv6Address getFullForm(final Ipv6Address ip) {
41         return new Ipv6Address(InetAddresses.forString(ip.getValue()).getHostAddress());
42     }
43
44     /**
45      * Reads from ByteBuf buffer and converts bytes to Ipv6Address.
46      *
47      * @param buffer containing Ipv6 address, starting at reader index
48      * @return Ipv6Address
49      */
50     public static Ipv6Address addressForByteBuf(final ByteBuf buffer) {
51         return IetfInetUtil.INSTANCE.ipv6AddressFor(ByteArray.readBytes(buffer, IPV6_LENGTH));
52     }
53
54     /**
55      * From string ipAddress creates an InetAddress and puts it into ByteBuf.
56      * @param ipAddress Ipv6 address
57      * @return ByteBuf with filled in bytes from ipAddress
58      */
59     public static ByteBuf byteBufForAddress(final Ipv6Address ipAddress) {
60         return Unpooled.wrappedBuffer(bytesForAddress(ipAddress));
61     }
62
63     /**
64      * Converts Ipv6Address to byte array.
65      *
66      * @param address Ipv6Address to be converted
67      * @return byte array
68      */
69     public static byte[] bytesForAddress(final Ipv6Address address) {
70         return IetfInetUtil.INSTANCE.ipv6AddressBytes(address);
71     }
72
73     /**
74      * Converts Ipv6Prefix to byte array. Prefix length at the end.
75      *
76      * @param prefix Ipv6Prefix to be converted
77      * @return byte array with prefix length at the end
78      */
79     public static byte[] bytesForPrefix(final Ipv6Prefix prefix) {
80         return IetfInetUtil.INSTANCE.ipv6PrefixToBytes(prefix);
81     }
82
83     /**
84      * Converts Ipv6Prefix to byte array. Prefix length at the beginning.
85      * Prefix bytes are trimmed from the end to match prefix length.
86      *
87      * @param prefix Ipv6Prefix to be converted
88      * @return byte array with the prefix length at the beginning
89      *
90      * @deprecated This is inefficient, refactor code to use {@link #bytesForAddress(Ipv6Address)} or
91      *             {@link ByteBufWriteUtil#writeMinimalPrefix(Ipv6Prefix, ByteBuf)}.
92      */
93     @Deprecated
94     public static byte[] bytesForPrefixBegin(final Ipv6Prefix prefix) {
95         final byte[] addrWithPrefix = bytesForPrefix(prefix);
96         return Ipv4Util.prefixedBytes(addrWithPrefix[IPV6_LENGTH], addrWithPrefix);
97     }
98
99     /**
100      * Creates an Ipv6Prefix object from given byte array.
101      *
102      * @param bytes IPv6 address
103      * @param length prefix length
104      * @return Ipv6Prefix object
105      */
106     public static Ipv6Prefix prefixForBytes(final byte[] bytes, final int length) {
107         Preconditions.checkArgument(length <= bytes.length * Byte.SIZE);
108
109         final byte[] tmp;
110         if (bytes.length != IPV6_LENGTH) {
111             tmp = Arrays.copyOfRange(bytes, 0, IPV6_LENGTH);
112         } else {
113             tmp = bytes;
114         }
115
116         return IetfInetUtil.INSTANCE.ipv6PrefixFor(tmp, length);
117     }
118
119     /**
120      * Creates an Ipv6Prefix object from given ByteBuf. Prefix length is assumed to
121      * be in the left most byte of the buffer.
122      *
123      * @param buf IPv6 address
124      * @return Ipv6Prefix object
125      */
126     public static Ipv6Prefix prefixForByteBuf(final ByteBuf buf) {
127         final int prefixLength = buf.readByte();
128         final int size = prefixLength / Byte.SIZE + ((prefixLength % Byte.SIZE == 0) ? 0 : 1);
129         final int readable = buf.readableBytes();
130         Preconditions.checkArgument(size <= readable, "Illegal length of IP prefix: %s/%s", size, readable);
131
132         final byte[] bytes = new byte[IPV6_LENGTH];
133         buf.readBytes(bytes, 0, size);
134         return IetfInetUtil.INSTANCE.ipv6PrefixFor(bytes, prefixLength);
135     }
136
137     /**
138      * Creates a list of Ipv6 Prefixes from given byte array.
139      *
140      * @param bytes to be converted to List of Ipv6Prefixes.
141      * @return A List of Ipv6Prefixes
142      */
143     public static List<Ipv6Prefix> prefixListForBytes(final byte[] bytes) {
144         if (bytes.length == 0) {
145             return Collections.emptyList();
146         }
147         final List<Ipv6Prefix> list = new ArrayList<>();
148         int byteOffset = 0;
149         while (byteOffset < bytes.length) {
150             final int bitLength = UnsignedBytes.toInt(bytes[byteOffset]);
151             byteOffset += 1;
152             // if length == 0, default route will be added
153             if (bitLength == 0) {
154                 list.add(EMPTY_PREFIX);
155                 continue;
156             }
157             final int byteCount = (bitLength % Byte.SIZE != 0) ? (bitLength / Byte.SIZE) + 1 : bitLength / Byte.SIZE;
158             list.add(prefixForBytes(ByteArray.subByte(bytes, byteOffset, byteCount), bitLength));
159             byteOffset += byteCount;
160
161         }
162         return list;
163     }
164 }