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) { |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame] | 25 | if (value == null) { |
| 26 | throw new NullPointerException("Value must not be null"); |
| 27 | } |
| 28 | if (mask == null) { |
| 29 | throw new NullPointerException("Mask must not be null"); |
| 30 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 31 | return new IPv4AddressWithMask(value, mask); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 32 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 33 | |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 34 | public static IPv4AddressWithMask of(final String string) { |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame] | 35 | if (string == null) { |
| 36 | throw new NullPointerException("String must not be null"); |
| 37 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 38 | int slashPos; |
| 39 | String ip = string; |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 40 | int maskBits = 32; |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 41 | IPv4Address maskAddress = null; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 42 | |
| 43 | // Read mask suffix |
| 44 | if ((slashPos = string.indexOf('/')) != -1) { |
| 45 | ip = string.substring(0, slashPos); |
| 46 | try { |
| 47 | String suffix = string.substring(slashPos + 1); |
| 48 | if (suffix.length() == 0) |
| 49 | throw new IllegalArgumentException("IP Address not well formed: " + string); |
| 50 | if (suffix.indexOf('.') != -1) { |
| 51 | // Full mask |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 52 | maskAddress = IPv4Address.of(suffix); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 53 | } else { |
| 54 | // CIDR Suffix |
| 55 | maskBits = Integer.parseInt(suffix); |
| 56 | } |
| 57 | } catch (NumberFormatException e) { |
| 58 | throw new IllegalArgumentException("IP Address not well formed: " + string); |
| 59 | } |
| 60 | if (maskBits < 0 || maskBits > 32) { |
| 61 | throw new IllegalArgumentException("IP Address not well formed: " + string); |
| 62 | } |
| 63 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 64 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 65 | // Read IP |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 66 | IPv4Address ipv4 = IPv4Address.of(ip); |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 67 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 68 | if (maskAddress != null) { |
| 69 | // Full address mask |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 70 | return IPv4AddressWithMask.of(ipv4, maskAddress); |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 71 | } else if (maskBits == 32) { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 72 | // No mask |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 73 | return IPv4AddressWithMask.of(ipv4, IPv4Address.NO_MASK); |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 74 | } else if (maskBits == 0) { |
| 75 | // No mask |
| 76 | return IPv4AddressWithMask.of(ipv4, IPv4Address.FULL_MASK); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 77 | } else { |
| 78 | // With mask |
| 79 | int mask = (-1) << (32 - maskBits); |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 80 | return IPv4AddressWithMask.of(ipv4, IPv4Address.of(mask)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | } |