Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame^] | 3 | import org.projectfloodlight.openflow.types.IPAddress.IpVersion; |
| 4 | |
| 5 | public class IPv4AddressWithMask extends IPAddressWithMask<IPv4Address> { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 6 | public final static IPv4AddressWithMask NONE = of(IPv4Address.NONE, IPv4Address.NONE); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 7 | |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 8 | private IPv4AddressWithMask(int rawValue, int rawMask) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 9 | super(IPv4Address.of(rawValue), IPv4Address.of(rawMask)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 10 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 11 | |
| 12 | private IPv4AddressWithMask(IPv4Address value, IPv4Address mask) { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 13 | super(value, mask); |
| 14 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 15 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame^] | 16 | @Override |
| 17 | public IpVersion getIpVersion() { |
| 18 | return IpVersion.IPv4; |
| 19 | } |
| 20 | |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 21 | public static IPv4AddressWithMask of(int rawValue, int rawMask) { |
| 22 | return new IPv4AddressWithMask(rawValue, rawMask); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 23 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 24 | |
| 25 | public static IPv4AddressWithMask of(IPv4Address value, IPv4Address mask) { |
| 26 | return new IPv4AddressWithMask(value, mask); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 27 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 28 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 29 | @Override |
| 30 | public String toString() { |
| 31 | StringBuilder res = new StringBuilder(); |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 32 | res.append(value.toString()); |
| 33 | |
| 34 | int maskint = mask.getInt(); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 35 | 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 Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 41 | res.append(mask.toString()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 42 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 43 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 44 | return res.toString(); |
| 45 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 46 | |
| 47 | public static IPv4AddressWithMask of(final String string) { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 48 | int slashPos; |
| 49 | String ip = string; |
| 50 | int maskBits = 0; |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 51 | IPv4Address maskAddress = null; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 52 | |
| 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 Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 62 | maskAddress = IPv4Address.of(suffix); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 63 | } 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 Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 74 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 75 | // Read IP |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 76 | IPv4Address ipv4 = IPv4Address.of(ip); |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 77 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 78 | if (maskAddress != null) { |
| 79 | // Full address mask |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 80 | return IPv4AddressWithMask.of(ipv4, maskAddress); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 81 | } else if (maskBits == 0) { |
| 82 | // No mask |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 83 | return IPv4AddressWithMask.of(ipv4, IPv4Address.NO_MASK); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 84 | } else { |
| 85 | // With mask |
| 86 | int mask = (-1) << (32 - maskBits); |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 87 | return IPv4AddressWithMask.of(ipv4, IPv4Address.of(mask)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | } |