blob: 67bbce4bb45112102375d38a83f928bce4f990cc [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) {
25 return new IPv4AddressWithMask(value, mask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070026 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070027
Yotam Harcholf3f11152013-09-05 16:47:16 -070028 @Override
29 public String toString() {
30 StringBuilder res = new StringBuilder();
Yotam Harchol9a617aa2013-09-16 14:10:17 -070031 res.append(value.toString());
32
33 int maskint = mask.getInt();
Yotam Harcholf3f11152013-09-05 16:47:16 -070034 res.append('/');
35 if (Integer.bitCount((~maskint) + 1) == 1) {
36 // CIDR notation
37 res.append(Integer.bitCount(maskint));
38 } else {
39 // Full address mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070040 res.append(mask.toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -070041 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070042
Yotam Harcholf3f11152013-09-05 16:47:16 -070043 return res.toString();
44 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070045
46 public static IPv4AddressWithMask of(final String string) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070047 int slashPos;
48 String ip = string;
49 int maskBits = 0;
Yotam Harchola289d552013-09-16 10:10:40 -070050 IPv4Address maskAddress = null;
Yotam Harcholf3f11152013-09-05 16:47:16 -070051
52 // Read mask suffix
53 if ((slashPos = string.indexOf('/')) != -1) {
54 ip = string.substring(0, slashPos);
55 try {
56 String suffix = string.substring(slashPos + 1);
57 if (suffix.length() == 0)
58 throw new IllegalArgumentException("IP Address not well formed: " + string);
59 if (suffix.indexOf('.') != -1) {
60 // Full mask
Yotam Harchola289d552013-09-16 10:10:40 -070061 maskAddress = IPv4Address.of(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -070062 } else {
63 // CIDR Suffix
64 maskBits = Integer.parseInt(suffix);
65 }
66 } catch (NumberFormatException e) {
67 throw new IllegalArgumentException("IP Address not well formed: " + string);
68 }
69 if (maskBits < 0 || maskBits > 32) {
70 throw new IllegalArgumentException("IP Address not well formed: " + string);
71 }
72 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070073
Yotam Harcholf3f11152013-09-05 16:47:16 -070074 // Read IP
Yotam Harchola289d552013-09-16 10:10:40 -070075 IPv4Address ipv4 = IPv4Address.of(ip);
Yotam Harchol9a617aa2013-09-16 14:10:17 -070076
Yotam Harcholf3f11152013-09-05 16:47:16 -070077 if (maskAddress != null) {
78 // Full address mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070079 return IPv4AddressWithMask.of(ipv4, maskAddress);
Yotam Harcholf3f11152013-09-05 16:47:16 -070080 } else if (maskBits == 0) {
81 // No mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070082 return IPv4AddressWithMask.of(ipv4, IPv4Address.NO_MASK);
Yotam Harcholf3f11152013-09-05 16:47:16 -070083 } else {
84 // With mask
85 int mask = (-1) << (32 - maskBits);
Yotam Harchol9a617aa2013-09-16 14:10:17 -070086 return IPv4AddressWithMask.of(ipv4, IPv4Address.of(mask));
Yotam Harcholf3f11152013-09-05 16:47:16 -070087 }
88 }
89
90}