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