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