blob: 63dba3852ebcf0ec9136e5b6381ebe10bd9e0df8 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
Yotam Harchol4d634682013-09-26 13:21:06 -07003import org.projectfloodlight.openflow.types.IPAddress.IpVersion;
4
5public class IPv4AddressWithMask extends IPAddressWithMask<IPv4Address> {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -07006 public final static IPv4AddressWithMask NONE = of(IPv4Address.NONE, IPv4Address.NONE);
Yotam Harcholf3f11152013-09-05 16:47:16 -07007
Yotam Harchol9a617aa2013-09-16 14:10:17 -07008 private IPv4AddressWithMask(int rawValue, int rawMask) {
Yotam Harchola289d552013-09-16 10:10:40 -07009 super(IPv4Address.of(rawValue), IPv4Address.of(rawMask));
Yotam Harcholf3f11152013-09-05 16:47:16 -070010 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070011
12 private IPv4AddressWithMask(IPv4Address value, IPv4Address mask) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070013 super(value, mask);
14 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070015
Yotam Harchol4d634682013-09-26 13:21:06 -070016 @Override
17 public IpVersion getIpVersion() {
18 return IpVersion.IPv4;
19 }
20
Yotam Harchol9a617aa2013-09-16 14:10:17 -070021 public static IPv4AddressWithMask of(int rawValue, int rawMask) {
22 return new IPv4AddressWithMask(rawValue, rawMask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070023 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070024
25 public static IPv4AddressWithMask of(IPv4Address value, IPv4Address mask) {
26 return new IPv4AddressWithMask(value, mask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070027 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070028
Yotam Harcholf3f11152013-09-05 16:47:16 -070029 @Override
30 public String toString() {
31 StringBuilder res = new StringBuilder();
Yotam Harchol9a617aa2013-09-16 14:10:17 -070032 res.append(value.toString());
33
34 int maskint = mask.getInt();
Yotam Harcholf3f11152013-09-05 16:47:16 -070035 res.append('/');
36 if (Integer.bitCount((~maskint) + 1) == 1) {
37 // CIDR notation
38 res.append(Integer.bitCount(maskint));
39 } else {
40 // Full address mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070041 res.append(mask.toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -070042 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070043
Yotam Harcholf3f11152013-09-05 16:47:16 -070044 return res.toString();
45 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070046
47 public static IPv4AddressWithMask of(final String string) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070048 int slashPos;
49 String ip = string;
50 int maskBits = 0;
Yotam Harchola289d552013-09-16 10:10:40 -070051 IPv4Address maskAddress = null;
Yotam Harcholf3f11152013-09-05 16:47:16 -070052
53 // Read mask suffix
54 if ((slashPos = string.indexOf('/')) != -1) {
55 ip = string.substring(0, slashPos);
56 try {
57 String suffix = string.substring(slashPos + 1);
58 if (suffix.length() == 0)
59 throw new IllegalArgumentException("IP Address not well formed: " + string);
60 if (suffix.indexOf('.') != -1) {
61 // Full mask
Yotam Harchola289d552013-09-16 10:10:40 -070062 maskAddress = IPv4Address.of(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -070063 } else {
64 // CIDR Suffix
65 maskBits = Integer.parseInt(suffix);
66 }
67 } catch (NumberFormatException e) {
68 throw new IllegalArgumentException("IP Address not well formed: " + string);
69 }
70 if (maskBits < 0 || maskBits > 32) {
71 throw new IllegalArgumentException("IP Address not well formed: " + string);
72 }
73 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070074
Yotam Harcholf3f11152013-09-05 16:47:16 -070075 // Read IP
Yotam Harchola289d552013-09-16 10:10:40 -070076 IPv4Address ipv4 = IPv4Address.of(ip);
Yotam Harchol9a617aa2013-09-16 14:10:17 -070077
Yotam Harcholf3f11152013-09-05 16:47:16 -070078 if (maskAddress != null) {
79 // Full address mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070080 return IPv4AddressWithMask.of(ipv4, maskAddress);
Yotam Harcholf3f11152013-09-05 16:47:16 -070081 } else if (maskBits == 0) {
82 // No mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070083 return IPv4AddressWithMask.of(ipv4, IPv4Address.NO_MASK);
Yotam Harcholf3f11152013-09-05 16:47:16 -070084 } else {
85 // With mask
86 int mask = (-1) << (32 - maskBits);
Yotam Harchol9a617aa2013-09-16 14:10:17 -070087 return IPv4AddressWithMask.of(ipv4, IPv4Address.of(mask));
Yotam Harcholf3f11152013-09-05 16:47:16 -070088 }
89 }
90
91}