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.concepts;
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;
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;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
29 * Util class for creating generated Ipv4Address.
31 public final class Ipv4Util {
36 public static final int IP4_LENGTH = 4;
39 * Converts byte array to Inet4Address.
41 * @param bytes to be converted
42 * @return InetAddress instance
43 * @throws IllegalArgumentException if {@link UnknownHostException} is thrown.
45 private static InetAddress getAddress(final byte[] bytes) {
47 return Inet4Address.getByAddress(bytes);
48 } catch (final UnknownHostException e) {
49 throw new IllegalArgumentException("Failed to construct IPv4 address", e);
54 * Converts byte array to Ipv4Address.
56 * @param bytes to be converted to Ipv4Address
59 public static Ipv4Address addressForBytes(final byte[] bytes) {
60 return new Ipv4Address(InetAddresses.toAddrString(getAddress(bytes)));
64 * Converts Ipv4Address to byte array.
66 * @param address Ipv4Address to be converted
69 public static byte[] bytesForAddress(final Ipv4Address address) {
70 final InetAddress a = InetAddresses.forString(address.getValue());
71 Preconditions.checkArgument(a instanceof Inet4Address);
72 return a.getAddress();
76 * Returns number of minimum bytes needed to cover all bits of prefix.
81 public static int getPrefixLengthBytes(final String prefix) {
82 int bits = Ipv4Util.getPrefixLength(prefix);
84 return (bits / 8) + 1;
90 * Converts Ipv4Prefix to byte array of length equal to prefix length value.
92 * @param ipv4Prefix Ipv4Prefix to be converted
95 public static byte[] bytesForPrefixByPrefixLength(Ipv4Prefix ipv4Prefix) {
96 return ByteArray.subByte(bytesForPrefix(ipv4Prefix), 0,
97 getPrefixLengthBytes(ipv4Prefix.getValue()));
101 * Converts Ipv4Prefix to byte array.
103 * @param prefix Ipv4Prefix to be converted
106 public static byte[] bytesForPrefix(final Ipv4Prefix prefix) {
107 final String p = prefix.getValue();
108 final int sep = p.indexOf('/');
109 final InetAddress a = InetAddresses.forString(p.substring(0, sep));
110 Preconditions.checkArgument(a instanceof Inet4Address);
111 final byte[] bytes = a.getAddress();
112 return Bytes.concat(bytes, new byte[] { Byte.valueOf(p.substring(sep + 1, p.length())) });
116 * Creates an Ipv4Prefix object from given byte array.
118 * @param bytes IPv4 address
119 * @param length prefix length
120 * @return Ipv4Prefix object
122 public static Ipv4Prefix prefixForBytes(final byte[] bytes, final int length) {
123 Preconditions.checkArgument(length <= bytes.length * Byte.SIZE);
124 final byte[] tmp = Arrays.copyOfRange(bytes, 0, IP4_LENGTH);
125 final InetAddress a = getAddress(tmp);
126 return new Ipv4Prefix(InetAddresses.toAddrString(a) + '/' + length);
130 * Creates a list of Ipv4 Prefixes from given byte array.
132 * @param bytes to be converted to List of Ipv4Prefixes.
133 * @return List<Ipv4Prefix>
135 public static List<Ipv4Prefix> prefixListForBytes(final byte[] bytes) {
136 if (bytes.length == 0) {
137 return Collections.emptyList();
139 final List<Ipv4Prefix> list = Lists.newArrayList();
141 while (byteOffset < bytes.length) {
142 final int bitLength = UnsignedBytes.toInt(ByteArray.subByte(bytes, byteOffset, 1)[0]);
144 final int byteCount = (bitLength % Byte.SIZE != 0) ? (bitLength / Byte.SIZE) + 1 : bitLength / Byte.SIZE;
145 list.add(prefixForBytes(ByteArray.subByte(bytes, byteOffset, byteCount), bitLength));
146 byteOffset += byteCount;
152 * Obtains prefix length from given prefix.
155 * @return prefix length
157 public static int getPrefixLength(final IpPrefix prefix) {
159 if (prefix.getIpv4Prefix() != null) {
160 p = prefix.getIpv4Prefix().getValue();
162 p = prefix.getIpv6Prefix().getValue();
164 return getPrefixLength(p);
168 * Obtains prefix length from given prefix.
170 * @param prefixValue value of prefix
171 * @return prefix length
173 public static int getPrefixLength(final String prefixValue) {
174 final int sep = prefixValue.indexOf('/');
175 return Integer.valueOf(prefixValue.substring(sep + 1, prefixValue.length()));