Moved Ipv4Util and Ipv6Util from concepts to util
[bgpcep.git] / util / src / main / java / org / opendaylight / protocol / util / Ipv4Util.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.Inet4Address;
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.IpPrefix;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
25
26 /**
27  * Util class for creating generated Ipv4Address.
28  */
29 public final class Ipv4Util {
30
31     private Ipv4Util() {
32     }
33
34     public static final int IP4_LENGTH = 4;
35
36     /**
37      * Converts byte array to Inet4Address.
38      *
39      * @param bytes to be converted
40      * @return InetAddress instance
41      * @throws IllegalArgumentException if {@link UnknownHostException} is thrown.
42      */
43     private static InetAddress getAddress(final byte[] bytes) {
44         try {
45             return Inet4Address.getByAddress(bytes);
46         } catch (final UnknownHostException e) {
47             throw new IllegalArgumentException("Failed to construct IPv4 address", e);
48         }
49     }
50
51     /**
52      * Converts byte array to Ipv4Address.
53      *
54      * @param bytes to be converted to Ipv4Address
55      * @return Ipv4Address
56      */
57     public static Ipv4Address addressForBytes(final byte[] bytes) {
58         return new Ipv4Address(InetAddresses.toAddrString(getAddress(bytes)));
59     }
60
61     /**
62      * Reads from ByteBuf buffer and converts bytes to Ipv4Address.
63      *
64      * @param buffer containing Ipv4 address, starting at reader index
65      * @return Ipv4Address
66      */
67     public static Ipv4Address addressForByteBuf(final ByteBuf buffer) {
68         return addressForBytes(ByteArray.readBytes(buffer, IP4_LENGTH));
69     }
70
71     /**
72      * Converts Ipv4Address to byte array.
73      *
74      * @param address Ipv4Address to be converted
75      * @return byte array
76      */
77     public static byte[] bytesForAddress(final Ipv4Address address) {
78         final InetAddress a = InetAddresses.forString(address.getValue());
79         Preconditions.checkArgument(a instanceof Inet4Address);
80         return a.getAddress();
81     }
82
83     /**
84      * Returns number of minimum bytes needed to cover all bits of prefix.
85      *
86      * @param prefix
87      * @return
88      */
89     public static int getPrefixLengthBytes(final String prefix) {
90         int bits = Ipv4Util.getPrefixLength(prefix);
91         if (bits % 8 != 0) {
92             return (bits / 8) + 1;
93         }
94         return bits / 8;
95     }
96
97     /**
98      * Converts Ipv4Prefix to byte array of length equal to prefix length value.
99      *
100      * @param ipv4Prefix Ipv4Prefix to be converted
101      * @return byte array
102      */
103     public static byte[] bytesForPrefixByPrefixLength(Ipv4Prefix ipv4Prefix) {
104         return ByteArray.subByte(bytesForPrefix(ipv4Prefix), 0,
105                 getPrefixLengthBytes(ipv4Prefix.getValue()));
106     }
107
108     /**
109      * Converts Ipv4Prefix to byte array.
110      *
111      * @param prefix Ipv4Prefix to be converted
112      * @return byte array
113      */
114     public static byte[] bytesForPrefix(final Ipv4Prefix prefix) {
115         final String p = prefix.getValue();
116         final int sep = p.indexOf('/');
117         final InetAddress a = InetAddresses.forString(p.substring(0, sep));
118         Preconditions.checkArgument(a instanceof Inet4Address);
119         final byte[] bytes = a.getAddress();
120         return Bytes.concat(bytes, new byte[] { Byte.valueOf(p.substring(sep + 1, p.length())) });
121     }
122
123     /**
124      * Creates an Ipv4Prefix object from given byte array.
125      *
126      * @param bytes  IPv4 address
127      * @param length prefix length
128      * @return Ipv4Prefix object
129      */
130     public static Ipv4Prefix prefixForBytes(final byte[] bytes, final int length) {
131         Preconditions.checkArgument(length <= bytes.length * Byte.SIZE);
132         final byte[] tmp = Arrays.copyOfRange(bytes, 0, IP4_LENGTH);
133         final InetAddress a = getAddress(tmp);
134         return new Ipv4Prefix(InetAddresses.toAddrString(a) + '/' + length);
135     }
136
137     /**
138      * Creates a list of Ipv4 Prefixes from given byte array.
139      *
140      * @param bytes to be converted to List of Ipv4Prefixes.
141      * @return List<Ipv4Prefix>
142      */
143     public static List<Ipv4Prefix> prefixListForBytes(final byte[] bytes) {
144         if (bytes.length == 0) {
145             return Collections.emptyList();
146         }
147         final List<Ipv4Prefix> list = Lists.newArrayList();
148         int byteOffset = 0;
149         while (byteOffset < bytes.length) {
150             final int bitLength = UnsignedBytes.toInt(ByteArray.subByte(bytes, byteOffset, 1)[0]);
151             byteOffset += 1;
152             final int byteCount = (bitLength % Byte.SIZE != 0) ? (bitLength / Byte.SIZE) + 1 : bitLength / Byte.SIZE;
153             list.add(prefixForBytes(ByteArray.subByte(bytes, byteOffset, byteCount), bitLength));
154             byteOffset += byteCount;
155         }
156         return list;
157     }
158
159     /**
160      * Obtains prefix length from given prefix.
161      *
162      * @param prefix
163      * @return prefix length
164      */
165     public static int getPrefixLength(final IpPrefix prefix) {
166         String p = "";
167         if (prefix.getIpv4Prefix() != null) {
168             p = prefix.getIpv4Prefix().getValue();
169         } else {
170             p = prefix.getIpv6Prefix().getValue();
171         }
172         return getPrefixLength(p);
173     }
174
175     /**
176      * Obtains prefix length from given prefix.
177      *
178      * @param prefixValue value of prefix
179      * @return prefix length
180      */
181     public static int getPrefixLength(final String prefixValue) {
182         final int sep = prefixValue.indexOf('/');
183         return Integer.valueOf(prefixValue.substring(sep + 1, prefixValue.length()));
184     }
185 }