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