BUG-49 : updated the rest of ignored tests and fixed more warnings.
[bgpcep.git] / concepts / src / main / java / org / opendaylight / protocol / concepts / 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.concepts;
9
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;
16
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;
21
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;
27
28 /**
29  * Util class for creating generated Ipv4Address.
30  */
31 public final class Ipv4Util {
32
33         public static final int IP4_LENGTH = 4;
34
35         public static Ipv4Address addressForBytes(final byte[] bytes) {
36                 try {
37                         return new Ipv4Address(InetAddresses.toAddrString(Inet4Address.getByAddress(bytes)));
38                 } catch (final UnknownHostException e) {
39                         throw new IllegalArgumentException(e.getMessage());
40                 }
41         }
42
43         public static byte[] bytesForAddress(final Ipv4Address address) {
44                 Inet4Address a;
45                 try {
46                         a = (Inet4Address) InetAddress.getByName(address.getValue());
47                 } catch (final UnknownHostException e) {
48                         throw new IllegalArgumentException(e.getMessage());
49                 }
50                 return a.getAddress();
51         }
52
53         public static byte[] bytesForPrefix(final Ipv4Prefix prefix) {
54                 final String p = prefix.getValue();
55                 final int sep = p.indexOf("/");
56                 try {
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());
61                 }
62         }
63
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);
67                 InetAddress a = null;
68                 try {
69                         a = InetAddress.getByAddress(tmp);
70                 } catch (final UnknownHostException e) {
71                         throw new IllegalArgumentException(e.getMessage());
72                 }
73                 return new Ipv4Prefix(InetAddresses.toAddrString(a) + "/" + length);
74         }
75
76         public static List<Ipv4Prefix> prefixListForBytes(final byte[] bytes) {
77                 if (bytes.length == 0) {
78                         return Collections.emptyList();
79                 }
80
81                 final List<Ipv4Prefix> list = Lists.newArrayList();
82                 int byteOffset = 0;
83                 while (byteOffset < bytes.length) {
84                         final int bitLength = UnsignedBytes.toInt(ByteArray.subByte(bytes, byteOffset, 1)[0]);
85                         byteOffset += 1;
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;
89                 }
90                 return list;
91         }
92
93         public static int getPrefixLength(final IpPrefix prefix) {
94                 String p = "";
95                 if (prefix.getIpv4Prefix() != null) {
96                         p = prefix.getIpv4Prefix().getValue();
97                 } else {
98                         p = prefix.getIpv6Prefix().getValue();
99                 }
100                 final int sep = p.indexOf("/");
101                 return Integer.valueOf(p.substring(sep, p.length() - 1));
102         }
103 }