blob: a6ec4392c643912ddbd92e898b0bd773fa71d4b7 [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> {
Yotam Harcholf3f11152013-09-05 16:47:16 -07004
Yotam Harchol9a617aa2013-09-16 14:10:17 -07005 private IPv4AddressWithMask(int rawValue, int rawMask) {
Yotam Harchola289d552013-09-16 10:10:40 -07006 super(IPv4Address.of(rawValue), IPv4Address.of(rawMask));
Yotam Harcholf3f11152013-09-05 16:47:16 -07007 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -07008
9 private IPv4AddressWithMask(IPv4Address value, IPv4Address mask) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070010 super(value, mask);
11 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070012
13 public static IPv4AddressWithMask of(int rawValue, int rawMask) {
14 return new IPv4AddressWithMask(rawValue, rawMask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070015 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070016
17 public static IPv4AddressWithMask of(IPv4Address value, IPv4Address mask) {
18 return new IPv4AddressWithMask(value, mask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070019 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070020
Yotam Harcholf3f11152013-09-05 16:47:16 -070021 @Override
22 public String toString() {
23 StringBuilder res = new StringBuilder();
Yotam Harchol9a617aa2013-09-16 14:10:17 -070024 res.append(value.toString());
25
26 int maskint = mask.getInt();
Yotam Harcholf3f11152013-09-05 16:47:16 -070027 res.append('/');
28 if (Integer.bitCount((~maskint) + 1) == 1) {
29 // CIDR notation
30 res.append(Integer.bitCount(maskint));
31 } else {
32 // Full address mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070033 res.append(mask.toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -070034 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070035
Yotam Harcholf3f11152013-09-05 16:47:16 -070036 return res.toString();
37 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070038
39 public static IPv4AddressWithMask of(final String string) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070040 int slashPos;
41 String ip = string;
42 int maskBits = 0;
Yotam Harchola289d552013-09-16 10:10:40 -070043 IPv4Address maskAddress = null;
Yotam Harcholf3f11152013-09-05 16:47:16 -070044
45 // Read mask suffix
46 if ((slashPos = string.indexOf('/')) != -1) {
47 ip = string.substring(0, slashPos);
48 try {
49 String suffix = string.substring(slashPos + 1);
50 if (suffix.length() == 0)
51 throw new IllegalArgumentException("IP Address not well formed: " + string);
52 if (suffix.indexOf('.') != -1) {
53 // Full mask
Yotam Harchola289d552013-09-16 10:10:40 -070054 maskAddress = IPv4Address.of(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -070055 } else {
56 // CIDR Suffix
57 maskBits = Integer.parseInt(suffix);
58 }
59 } catch (NumberFormatException e) {
60 throw new IllegalArgumentException("IP Address not well formed: " + string);
61 }
62 if (maskBits < 0 || maskBits > 32) {
63 throw new IllegalArgumentException("IP Address not well formed: " + string);
64 }
65 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070066
Yotam Harcholf3f11152013-09-05 16:47:16 -070067 // Read IP
Yotam Harchola289d552013-09-16 10:10:40 -070068 IPv4Address ipv4 = IPv4Address.of(ip);
Yotam Harchol9a617aa2013-09-16 14:10:17 -070069
Yotam Harcholf3f11152013-09-05 16:47:16 -070070 if (maskAddress != null) {
71 // Full address mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070072 return IPv4AddressWithMask.of(ipv4, maskAddress);
Yotam Harcholf3f11152013-09-05 16:47:16 -070073 } else if (maskBits == 0) {
74 // No mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070075 return IPv4AddressWithMask.of(ipv4, IPv4Address.NO_MASK);
Yotam Harcholf3f11152013-09-05 16:47:16 -070076 } else {
77 // With mask
78 int mask = (-1) << (32 - maskBits);
Yotam Harchol9a617aa2013-09-16 14:10:17 -070079 return IPv4AddressWithMask.of(ipv4, IPv4Address.of(mask));
Yotam Harcholf3f11152013-09-05 16:47:16 -070080 }
81 }
82
83}