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 java.net.Inet4Address;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.Arrays;
14 import java.util.Collections;
15 import java.util.List;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
22 import com.google.common.base.Preconditions;
23 import com.google.common.collect.Lists;
24 import com.google.common.net.InetAddresses;
25 import com.google.common.primitives.Bytes;
26 import com.google.common.primitives.UnsignedBytes;
29 * Util class for creating generated Ipv4Address.
31 public final class Ipv4Util {
33 public static final int IP4_LENGTH = 4;
35 public static Ipv4Address addressForBytes(final byte[] bytes) {
37 return new Ipv4Address(InetAddresses.toAddrString(Inet4Address.getByAddress(bytes)));
38 } catch (final UnknownHostException e) {
39 throw new IllegalArgumentException(e.getMessage());
43 public static byte[] bytesForAddress(final Ipv4Address address) {
46 a = (Inet4Address) InetAddress.getByName(address.getValue());
47 } catch (final UnknownHostException e) {
48 throw new IllegalArgumentException(e.getMessage());
50 return a.getAddress();
53 public static byte[] bytesForPrefix(final Ipv4Prefix prefix) {
54 final String p = prefix.getValue();
55 final int sep = p.indexOf("/");
57 final byte[] bytes = Inet4Address.getByName(p.substring(0, sep)).getAddress();
58 return Bytes.concat(bytes, new byte[] { Byte.valueOf(p.substring(sep + 1, p.length() - 1)) });
59 } catch (final UnknownHostException e) {
60 throw new IllegalArgumentException(e.getMessage());
64 public static Ipv4Prefix prefixForBytes(final byte[] bytes, final int length) {
65 Preconditions.checkArgument(length <= bytes.length * 8);
66 final byte[] tmp = Arrays.copyOfRange(bytes, 0, 4);
69 a = InetAddress.getByAddress(tmp);
70 } catch (final UnknownHostException e) {
71 throw new IllegalArgumentException(e.getMessage());
73 return new Ipv4Prefix(InetAddresses.toAddrString(a) + "/" + length);
76 public static List<Ipv4Prefix> prefixListForBytes(final byte[] bytes) {
77 if (bytes.length == 0) {
78 return Collections.emptyList();
81 final List<Ipv4Prefix> list = Lists.newArrayList();
83 while (byteOffset < bytes.length) {
84 final int bitLength = UnsignedBytes.toInt(ByteArray.subByte(bytes, byteOffset, 1)[0]);
86 final int byteCount = (bitLength % 8 != 0) ? (bitLength / 8) + 1 : bitLength / 8;
87 list.add(prefixForBytes(ByteArray.subByte(bytes, byteOffset, byteCount), bitLength));
88 byteOffset += byteCount;
93 public static int getPrefixLength(final IpPrefix prefix) {
95 if (prefix.getIpv4Prefix() != null) {
96 p = prefix.getIpv4Prefix().getValue();
98 p = prefix.getIpv6Prefix().getValue();
100 final int sep = p.indexOf("/");
101 return Integer.valueOf(p.substring(sep, p.length() - 1));