blob: 3d14acf21169e6a5162adfcf5780e4a1f334192a [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
Yotam Harchol4d634682013-09-26 13:21:06 -07003
4public class IPv4AddressWithMask extends IPAddressWithMask<IPv4Address> {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -07005 public final static IPv4AddressWithMask NONE = of(IPv4Address.NONE, IPv4Address.NONE);
Yotam Harcholf3f11152013-09-05 16:47:16 -07006
Yotam Harchol9a617aa2013-09-16 14:10:17 -07007 private IPv4AddressWithMask(int rawValue, int rawMask) {
Yotam Harchola289d552013-09-16 10:10:40 -07008 super(IPv4Address.of(rawValue), IPv4Address.of(rawMask));
Yotam Harcholf3f11152013-09-05 16:47:16 -07009 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070010
11 private IPv4AddressWithMask(IPv4Address value, IPv4Address mask) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070012 super(value, mask);
13 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070014
Yotam Harchol4d634682013-09-26 13:21:06 -070015 @Override
Yotam Harcholeb023dc2013-09-26 15:45:44 -070016 public IPVersion getIpVersion() {
17 return IPVersion.IPv4;
Yotam Harchol4d634682013-09-26 13:21:06 -070018 }
19
Yotam Harchol9a617aa2013-09-16 14:10:17 -070020 public static IPv4AddressWithMask of(int rawValue, int rawMask) {
21 return new IPv4AddressWithMask(rawValue, rawMask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070022 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070023
24 public static IPv4AddressWithMask of(IPv4Address value, IPv4Address mask) {
Gregor Maier1ff55972013-12-11 02:22:56 -080025 if (value == null) {
26 throw new NullPointerException("Value must not be null");
27 }
28 if (mask == null) {
29 throw new NullPointerException("Mask must not be null");
30 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070031 return new IPv4AddressWithMask(value, mask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070032 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070033
Yotam Harchol9a617aa2013-09-16 14:10:17 -070034 public static IPv4AddressWithMask of(final String string) {
Gregor Maier1ff55972013-12-11 02:22:56 -080035 if (string == null) {
36 throw new NullPointerException("String must not be null");
37 }
Yotam Harcholf3f11152013-09-05 16:47:16 -070038 int slashPos;
39 String ip = string;
Andreas Wundsame04c86f2013-11-05 17:13:18 -080040 int maskBits = 32;
Yotam Harchola289d552013-09-16 10:10:40 -070041 IPv4Address maskAddress = null;
Yotam Harcholf3f11152013-09-05 16:47:16 -070042
43 // Read mask suffix
44 if ((slashPos = string.indexOf('/')) != -1) {
45 ip = string.substring(0, slashPos);
46 try {
47 String suffix = string.substring(slashPos + 1);
48 if (suffix.length() == 0)
49 throw new IllegalArgumentException("IP Address not well formed: " + string);
50 if (suffix.indexOf('.') != -1) {
51 // Full mask
Yotam Harchola289d552013-09-16 10:10:40 -070052 maskAddress = IPv4Address.of(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -070053 } else {
54 // CIDR Suffix
55 maskBits = Integer.parseInt(suffix);
56 }
57 } catch (NumberFormatException e) {
58 throw new IllegalArgumentException("IP Address not well formed: " + string);
59 }
60 if (maskBits < 0 || maskBits > 32) {
61 throw new IllegalArgumentException("IP Address not well formed: " + string);
62 }
63 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070064
Yotam Harcholf3f11152013-09-05 16:47:16 -070065 // Read IP
Yotam Harchola289d552013-09-16 10:10:40 -070066 IPv4Address ipv4 = IPv4Address.of(ip);
Yotam Harchol9a617aa2013-09-16 14:10:17 -070067
Yotam Harcholf3f11152013-09-05 16:47:16 -070068 if (maskAddress != null) {
69 // Full address mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070070 return IPv4AddressWithMask.of(ipv4, maskAddress);
Andreas Wundsame04c86f2013-11-05 17:13:18 -080071 } else if (maskBits == 32) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070072 // No mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070073 return IPv4AddressWithMask.of(ipv4, IPv4Address.NO_MASK);
Andreas Wundsame04c86f2013-11-05 17:13:18 -080074 } else if (maskBits == 0) {
75 // No mask
76 return IPv4AddressWithMask.of(ipv4, IPv4Address.FULL_MASK);
Yotam Harcholf3f11152013-09-05 16:47:16 -070077 } else {
78 // With mask
79 int mask = (-1) << (32 - maskBits);
Yotam Harchol9a617aa2013-09-16 14:10:17 -070080 return IPv4AddressWithMask.of(ipv4, IPv4Address.of(mask));
Yotam Harcholf3f11152013-09-05 16:47:16 -070081 }
82 }
Sovietaced07cc8eb2014-06-24 11:55:26 -070083
84 @Override
85 public boolean contains(IPAddress<?> ip) {
86 if (ip == null) {
87 throw new NullPointerException("ip must not be null");
88 }
89
90 IPv4Address ipv4 = (IPv4Address) ip;
91 return this.matches(ipv4);
92 }
Yotam Harcholf3f11152013-09-05 16:47:16 -070093}