blob: 45f10d11d0c0e9cd7324b71365b3e0b85a7ff7c5 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
Yotam Harchol9a617aa2013-09-16 14:10:17 -07003public class IPv4AddressWithMask extends Masked<IPv4Address> {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -07004 public final static IPv4AddressWithMask NONE = of(IPv4Address.NONE, IPv4Address.NONE);
Yotam Harcholf3f11152013-09-05 16:47:16 -07005
Yotam Harchol9a617aa2013-09-16 14:10:17 -07006 private IPv4AddressWithMask(int rawValue, int rawMask) {
Yotam Harchola289d552013-09-16 10:10:40 -07007 super(IPv4Address.of(rawValue), IPv4Address.of(rawMask));
Yotam Harcholf3f11152013-09-05 16:47:16 -07008 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -07009
10 private IPv4AddressWithMask(IPv4Address value, IPv4Address mask) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070011 super(value, mask);
12 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070013
14 public static IPv4AddressWithMask of(int rawValue, int rawMask) {
15 return new IPv4AddressWithMask(rawValue, rawMask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070016 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070017
18 public static IPv4AddressWithMask of(IPv4Address value, IPv4Address mask) {
19 return new IPv4AddressWithMask(value, mask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070020 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070021
Yotam Harcholf3f11152013-09-05 16:47:16 -070022 @Override
23 public String toString() {
24 StringBuilder res = new StringBuilder();
Yotam Harchol9a617aa2013-09-16 14:10:17 -070025 res.append(value.toString());
26
27 int maskint = mask.getInt();
Yotam Harcholf3f11152013-09-05 16:47:16 -070028 res.append('/');
29 if (Integer.bitCount((~maskint) + 1) == 1) {
30 // CIDR notation
31 res.append(Integer.bitCount(maskint));
32 } else {
33 // Full address mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070034 res.append(mask.toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -070035 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070036
Yotam Harcholf3f11152013-09-05 16:47:16 -070037 return res.toString();
38 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070039
40 public static IPv4AddressWithMask of(final String string) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070041 int slashPos;
42 String ip = string;
43 int maskBits = 0;
Yotam Harchola289d552013-09-16 10:10:40 -070044 IPv4Address maskAddress = null;
Yotam Harcholf3f11152013-09-05 16:47:16 -070045
46 // Read mask suffix
47 if ((slashPos = string.indexOf('/')) != -1) {
48 ip = string.substring(0, slashPos);
49 try {
50 String suffix = string.substring(slashPos + 1);
51 if (suffix.length() == 0)
52 throw new IllegalArgumentException("IP Address not well formed: " + string);
53 if (suffix.indexOf('.') != -1) {
54 // Full mask
Yotam Harchola289d552013-09-16 10:10:40 -070055 maskAddress = IPv4Address.of(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -070056 } else {
57 // CIDR Suffix
58 maskBits = Integer.parseInt(suffix);
59 }
60 } catch (NumberFormatException e) {
61 throw new IllegalArgumentException("IP Address not well formed: " + string);
62 }
63 if (maskBits < 0 || maskBits > 32) {
64 throw new IllegalArgumentException("IP Address not well formed: " + string);
65 }
66 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070067
Yotam Harcholf3f11152013-09-05 16:47:16 -070068 // Read IP
Yotam Harchola289d552013-09-16 10:10:40 -070069 IPv4Address ipv4 = IPv4Address.of(ip);
Yotam Harchol9a617aa2013-09-16 14:10:17 -070070
Yotam Harcholf3f11152013-09-05 16:47:16 -070071 if (maskAddress != null) {
72 // Full address mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070073 return IPv4AddressWithMask.of(ipv4, maskAddress);
Yotam Harcholf3f11152013-09-05 16:47:16 -070074 } else if (maskBits == 0) {
75 // No mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070076 return IPv4AddressWithMask.of(ipv4, IPv4Address.NO_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 }
83
84}