blob: ada0d58f524a15fab6ea72e0499967d6e1536ffb [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
Ronald Liaf8d3e72014-07-05 01:26:28 -07003import javax.annotation.Nonnull;
4
Sovietaced9dfc1ef2014-06-27 11:13:57 -07005import com.google.common.base.Preconditions;
6
Yotam Harchol4d634682013-09-26 13:21:06 -07007
8public class IPv4AddressWithMask extends IPAddressWithMask<IPv4Address> {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -07009 public final static IPv4AddressWithMask NONE = of(IPv4Address.NONE, IPv4Address.NONE);
Yotam Harcholf3f11152013-09-05 16:47:16 -070010
Yotam Harchol9a617aa2013-09-16 14:10:17 -070011 private IPv4AddressWithMask(int rawValue, int rawMask) {
Yotam Harchola289d552013-09-16 10:10:40 -070012 super(IPv4Address.of(rawValue), IPv4Address.of(rawMask));
Yotam Harcholf3f11152013-09-05 16:47:16 -070013 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070014
15 private IPv4AddressWithMask(IPv4Address value, IPv4Address mask) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070016 super(value, mask);
17 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070018
Yotam Harchol4d634682013-09-26 13:21:06 -070019 @Override
Yotam Harcholeb023dc2013-09-26 15:45:44 -070020 public IPVersion getIpVersion() {
21 return IPVersion.IPv4;
Yotam Harchol4d634682013-09-26 13:21:06 -070022 }
23
Ronald Liaf8d3e72014-07-05 01:26:28 -070024 /**
25 * Returns an {@code IPv4AddressWithMask} object that represents the given
26 * raw IP address masked by the given raw IP address mask, with both
27 * represented as 32-bit integers.
28 *
29 * @param rawValue the raw IP address to be masked
30 * @param rawMask the raw IP address mask
31 * @return an {@code IPv4AddressWithMask} object that represents
32 * the given raw IP address masked by the given raw IP
33 * address mask
Ronald Li8a0b53c2014-07-05 02:33:43 -070034 * @deprecated replaced by {@link IPv4Address#of(int)} followed by
35 * {@link IPv4Address#withMask(int)}
Ronald Liaf8d3e72014-07-05 01:26:28 -070036 */
37 @Nonnull
Ronald Li8a0b53c2014-07-05 02:33:43 -070038 @Deprecated
Ronald Liaf8d3e72014-07-05 01:26:28 -070039 public static IPv4AddressWithMask of(final int rawValue, final int rawMask) {
Yotam Harchol9a617aa2013-09-16 14:10:17 -070040 return new IPv4AddressWithMask(rawValue, rawMask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070041 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070042
Ronald Liaf8d3e72014-07-05 01:26:28 -070043 /**
44 * Returns an {@code IPv4AddressWithMask} object that represents the given
45 * IP address masked by the given IP address mask, with both represented
46 * as {@code IPv4Address} objects.
47 *
48 * @param value the IP address to be masked
49 * @param mask the IP address mask
50 * @return an {@code IPv4AddressWithMask} object that represents
51 * the given IP address masked by the given IP address mask
52 * @throws NullPointerException if any of the given {@code IPv4Address}
53 * objects were {@code null}
54 */
55 @Nonnull
56 public static IPv4AddressWithMask of(
57 @Nonnull final IPv4Address value,
58 @Nonnull final IPv4Address mask) {
Sovietaced9dfc1ef2014-06-27 11:13:57 -070059 Preconditions.checkNotNull(value, "value must not be null");
60 Preconditions.checkNotNull(mask, "mask must not be null");
61
Yotam Harchol9a617aa2013-09-16 14:10:17 -070062 return new IPv4AddressWithMask(value, mask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070063 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070064
Ronald Liaf8d3e72014-07-05 01:26:28 -070065 /**
66 * Returns an {@code IPv4AddressWithMask} object that corresponds to
67 * the given string in CIDR notation or other notations as specified
68 * below
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) {
Sovietaced9dfc1ef2014-06-27 11:13:57 -070090 Preconditions.checkNotNull(string, "string must not be null");
91
Yotam Harcholf3f11152013-09-05 16:47:16 -070092 int slashPos;
93 String ip = string;
Ronald Lia780c372014-07-03 17:34:50 -070094 int cidrMaskLength = 32;
Yotam Harchola289d552013-09-16 10:10:40 -070095 IPv4Address maskAddress = null;
Yotam Harcholf3f11152013-09-05 16:47:16 -070096
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
Yotam Harchola289d552013-09-16 10:10:40 -0700106 maskAddress = IPv4Address.of(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700107 } else {
108 // CIDR Suffix
Ronald Lia780c372014-07-03 17:34:50 -0700109 cidrMaskLength = Integer.parseInt(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700110 }
111 } catch (NumberFormatException e) {
112 throw new IllegalArgumentException("IP Address not well formed: " + string);
113 }
Yotam Harcholf3f11152013-09-05 16:47:16 -0700114 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -0700115
Yotam Harcholf3f11152013-09-05 16:47:16 -0700116 // Read IP
Yotam Harchola289d552013-09-16 10:10:40 -0700117 IPv4Address ipv4 = IPv4Address.of(ip);
Yotam Harchol9a617aa2013-09-16 14:10:17 -0700118
Yotam Harcholf3f11152013-09-05 16:47:16 -0700119 if (maskAddress != null) {
120 // Full address mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -0700121 return IPv4AddressWithMask.of(ipv4, maskAddress);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700122 } else {
Ronald Lia780c372014-07-03 17:34:50 -0700123 return IPv4AddressWithMask.of(
124 ipv4, IPv4Address.ofCidrMaskLength(cidrMaskLength));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700125 }
126 }
Sovietaced77d99492014-06-24 12:58:47 -0700127
Sovietaced07cc8eb2014-06-24 11:55:26 -0700128 @Override
129 public boolean contains(IPAddress<?> ip) {
Sovietaced9dfc1ef2014-06-27 11:13:57 -0700130 Preconditions.checkNotNull(ip, "ip must not be null");
Sovietaced77d99492014-06-24 12:58:47 -0700131
132 if(ip.getIpVersion() == IPVersion.IPv4) {
133 IPv4Address ipv4 = (IPv4Address) ip;
134 return this.matches(ipv4);
135 }
136
137 return false;
Sovietaced07cc8eb2014-06-24 11:55:26 -0700138 }
Yotam Harcholf3f11152013-09-05 16:47:16 -0700139}