Merge "Bug 611 - NlriReg supports serialization"
[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.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 io.netty.buffer.ByteBuf;
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;
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;
24
25 /**
26  * Util class for creating generated Ipv6Address.
27  */
28 public final class Ipv6Util {
29     private Ipv6Util() {
30     }
31
32     public static final int IPV6_LENGTH = 16;
33
34     /**
35      * Converts byte array to Inet6Address.
36      *
37      * @param bytes to be converted
38      * @return InetAddress instance
39      * @throws IllegalArgumentException if {@link UnknownHostException} is thrown.
40      */
41     private static InetAddress getAddress(final byte[] bytes) {
42         try {
43             return Inet6Address.getByAddress(bytes);
44         } catch (final UnknownHostException e) {
45             throw new IllegalArgumentException("Failed to construct IPv6 address", e);
46         }
47     }
48
49     /**
50      * Converts byte array to Ipv6Address.
51      *
52      * @param bytes to be converted to Ipv6Address
53      * @return Ipv6Address
54      */
55     public static Ipv6Address addressForBytes(final byte[] bytes) {
56         return new Ipv6Address(InetAddresses.toAddrString(getAddress(bytes)));
57     }
58
59     /**
60      * Reads from ByteBuf buffer and converts bytes to Ipv6Address.
61      *
62      * @param buffer containing Ipv6 address, starting at reader index
63      * @return Ipv4Address
64      */
65     public static Ipv6Address addressForByteBuf(final ByteBuf buffer) {
66         return addressForBytes(ByteArray.readBytes(buffer, IPV6_LENGTH));
67     }
68
69     /**
70      * Converts Ipv6Address to byte array.
71      *
72      * @param address Ipv6Address to be converted
73      * @return byte array
74      */
75     public static byte[] bytesForAddress(final Ipv6Address address) {
76         final InetAddress a = InetAddresses.forString(address.getValue());
77         Preconditions.checkArgument(a instanceof Inet6Address);
78         return a.getAddress();
79     }
80
81
82     /**
83      * Converts Ipv6Prefix to byte array of (prefix bit size/8) size.
84      *
85      * @param ipv6Prefix Ipv6Prefix to be converted
86      * @return byte array
87      */
88     public static byte[] bytesForPrefixByPrefixLength(Ipv6Prefix ipv6Prefix) {
89         return ByteArray.subByte(Ipv6Util.bytesForPrefix(ipv6Prefix), 0,
90                 Ipv4Util.getPrefixLengthBytes(ipv6Prefix.getValue()));
91     }
92
93
94     /**
95      * Converts Ipv6Prefix to byte array.
96      *
97      * @param prefix Ipv6Prefix to be converted
98      * @return byte array
99      */
100     public static byte[] bytesForPrefix(final Ipv6Prefix prefix) {
101         final String p = prefix.getValue();
102         final int sep = p.indexOf('/');
103         final InetAddress a = InetAddresses.forString(p.substring(0, sep));
104         Preconditions.checkArgument(a instanceof Inet6Address);
105         final byte[] bytes = a.getAddress();
106         return Bytes.concat(bytes, new byte[] { Byte.valueOf(p.substring(sep + 1, p.length())) });
107     }
108
109     /**
110      * Creates an Ipv6Prefix object from given byte array.
111      *
112      * @param bytes IPv6 address
113      * @param length prefix length
114      * @return Ipv6Prefix object
115      */
116     public static Ipv6Prefix prefixForBytes(final byte[] bytes, final int length) {
117         Preconditions.checkArgument(length <= bytes.length * Byte.SIZE);
118         final byte[] tmp = Arrays.copyOfRange(bytes, 0, IPV6_LENGTH);
119         final InetAddress a = getAddress(tmp);
120         return new Ipv6Prefix(InetAddresses.toAddrString(a) + '/' + length);
121     }
122
123     /**
124      * Creates a list of Ipv6 Prefixes from given byte array.
125      *
126      * @param bytes to be converted to List of Ipv6Prefixes.
127      * @return List<Ipv6Prefix>
128      */
129     public static List<Ipv6Prefix> prefixListForBytes(final byte[] bytes) {
130         if (bytes.length == 0) {
131             return Collections.emptyList();
132         }
133
134         final List<Ipv6Prefix> list = Lists.newArrayList();
135         int byteOffset = 0;
136         while (byteOffset < bytes.length) {
137             final int bitLength = UnsignedBytes.toInt(ByteArray.subByte(bytes, byteOffset, 1)[0]);
138             byteOffset += 1;
139             final int byteCount = (bitLength % Byte.SIZE != 0) ? (bitLength / Byte.SIZE) + 1 : bitLength / Byte.SIZE;
140             list.add(prefixForBytes(ByteArray.subByte(bytes, byteOffset, byteCount), bitLength));
141             byteOffset += byteCount;
142         }
143         return list;
144     }
145 }