Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 3 | import javax.annotation.Nonnull; |
| 4 | |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 5 | import com.google.common.base.Preconditions; |
| 6 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 7 | |
| 8 | public class IPv4AddressWithMask extends IPAddressWithMask<IPv4Address> { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 9 | public final static IPv4AddressWithMask NONE = of(IPv4Address.NONE, IPv4Address.NONE); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 10 | |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 11 | private IPv4AddressWithMask(int rawValue, int rawMask) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 12 | super(IPv4Address.of(rawValue), IPv4Address.of(rawMask)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 13 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 14 | |
| 15 | private IPv4AddressWithMask(IPv4Address value, IPv4Address mask) { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 16 | super(value, mask); |
| 17 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 18 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 19 | @Override |
Yotam Harchol | eb023dc | 2013-09-26 15:45:44 -0700 | [diff] [blame] | 20 | public IPVersion getIpVersion() { |
| 21 | return IPVersion.IPv4; |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Returns an {@code IPv4AddressWithMask} object that represents the given |
Ronald Li | 382589f | 2014-07-06 23:58:08 -0700 | [diff] [blame] | 26 | * raw IP address masked by the given raw IP address mask. |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 27 | * |
| 28 | * @param rawValue the raw IP address to be masked |
| 29 | * @param rawMask the raw IP address mask |
| 30 | * @return an {@code IPv4AddressWithMask} object that represents |
| 31 | * the given raw IP address masked by the given raw IP |
| 32 | * address mask |
Ronald Li | 8a0b53c | 2014-07-05 02:33:43 -0700 | [diff] [blame] | 33 | * @deprecated replaced by {@link IPv4Address#of(int)} followed by |
| 34 | * {@link IPv4Address#withMask(int)} |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 35 | */ |
| 36 | @Nonnull |
Ronald Li | 8a0b53c | 2014-07-05 02:33:43 -0700 | [diff] [blame] | 37 | @Deprecated |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 38 | public static IPv4AddressWithMask of(final int rawValue, final int rawMask) { |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 39 | return new IPv4AddressWithMask(rawValue, rawMask); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 40 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 41 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Returns an {@code IPv4AddressWithMask} object that represents the given |
Ronald Li | 382589f | 2014-07-06 23:58:08 -0700 | [diff] [blame] | 44 | * IP address masked by the given IP address mask. Both arguments are given |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 45 | * as {@code IPv4Address} objects. |
| 46 | * |
| 47 | * @param value the IP address to be masked |
| 48 | * @param mask the IP address mask |
| 49 | * @return an {@code IPv4AddressWithMask} object that represents |
| 50 | * the given IP address masked by the given IP address mask |
| 51 | * @throws NullPointerException if any of the given {@code IPv4Address} |
| 52 | * objects were {@code null} |
| 53 | */ |
| 54 | @Nonnull |
| 55 | public static IPv4AddressWithMask of( |
| 56 | @Nonnull final IPv4Address value, |
| 57 | @Nonnull final IPv4Address mask) { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 58 | Preconditions.checkNotNull(value, "value must not be null"); |
| 59 | Preconditions.checkNotNull(mask, "mask must not be null"); |
| 60 | |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 61 | return new IPv4AddressWithMask(value, mask); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 62 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 63 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 64 | /** |
| 65 | * Returns an {@code IPv4AddressWithMask} object that corresponds to |
Ronald Li | 382589f | 2014-07-06 23:58:08 -0700 | [diff] [blame] | 66 | * the given string in CIDR notation or other acceptable notations. |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 67 | * <p> |
| 68 | * The following notations are accepted. |
| 69 | * <table><tr> |
| 70 | * <th>Notation</th><th>Example</th><th>Notes</th> |
| 71 | * </tr><tr> |
| 72 | * <td>IPv4 address only</td><td>{@code 1.2.3.4}</td><td>The subnet mask of |
| 73 | * prefix length 32 (i.e. {@code 255.255.255.255}) is assumed.</td> |
| 74 | * </tr><tr> |
| 75 | * <td>IPv4 address/mask</td><td>{@code 1.2.3.4/255.255.255.0}</td> |
| 76 | * </tr><tr> |
| 77 | * <td>CIDR notation</td><td>{@code 1.2.3.4/24}</td> |
| 78 | * </tr></table> |
| 79 | * |
| 80 | * @param string the string in acceptable notations |
| 81 | * @return an {@code IPv4AddressWithMask} object that corresponds to |
| 82 | * the given string in acceptable notations |
| 83 | * @throws NullPointerException if the given string was {@code null} |
| 84 | * @throws IllegalArgumentException if the given string was malformed |
| 85 | */ |
| 86 | @Nonnull |
| 87 | public static IPv4AddressWithMask of(@Nonnull final String string) { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 88 | Preconditions.checkNotNull(string, "string must not be null"); |
| 89 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 90 | int slashPos; |
| 91 | String ip = string; |
Ronald Li | a780c37 | 2014-07-03 17:34:50 -0700 | [diff] [blame] | 92 | int cidrMaskLength = 32; |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 93 | IPv4Address maskAddress = null; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 94 | |
| 95 | // Read mask suffix |
| 96 | if ((slashPos = string.indexOf('/')) != -1) { |
| 97 | ip = string.substring(0, slashPos); |
| 98 | try { |
| 99 | String suffix = string.substring(slashPos + 1); |
| 100 | if (suffix.length() == 0) |
| 101 | throw new IllegalArgumentException("IP Address not well formed: " + string); |
| 102 | if (suffix.indexOf('.') != -1) { |
| 103 | // Full mask |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 104 | maskAddress = IPv4Address.of(suffix); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 105 | } else { |
| 106 | // CIDR Suffix |
Ronald Li | a780c37 | 2014-07-03 17:34:50 -0700 | [diff] [blame] | 107 | cidrMaskLength = Integer.parseInt(suffix); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 108 | } |
| 109 | } catch (NumberFormatException e) { |
| 110 | throw new IllegalArgumentException("IP Address not well formed: " + string); |
| 111 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 112 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 113 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 114 | // Read IP |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 115 | IPv4Address ipv4 = IPv4Address.of(ip); |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 116 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 117 | if (maskAddress != null) { |
| 118 | // Full address mask |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 119 | return IPv4AddressWithMask.of(ipv4, maskAddress); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 120 | } else { |
Ronald Li | a780c37 | 2014-07-03 17:34:50 -0700 | [diff] [blame] | 121 | return IPv4AddressWithMask.of( |
| 122 | ipv4, IPv4Address.ofCidrMaskLength(cidrMaskLength)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
Sovietaced | 77d9949 | 2014-06-24 12:58:47 -0700 | [diff] [blame] | 125 | |
Sovietaced | 07cc8eb | 2014-06-24 11:55:26 -0700 | [diff] [blame] | 126 | @Override |
| 127 | public boolean contains(IPAddress<?> ip) { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 128 | Preconditions.checkNotNull(ip, "ip must not be null"); |
Sovietaced | 77d9949 | 2014-06-24 12:58:47 -0700 | [diff] [blame] | 129 | |
| 130 | if(ip.getIpVersion() == IPVersion.IPv4) { |
| 131 | IPv4Address ipv4 = (IPv4Address) ip; |
| 132 | return this.matches(ipv4); |
| 133 | } |
| 134 | |
| 135 | return false; |
Sovietaced | 07cc8eb | 2014-06-24 11:55:26 -0700 | [diff] [blame] | 136 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 137 | } |