Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 3 | public class IPv4AddressWithMask extends Masked<IPv4Address> { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame^] | 4 | public final static IPv4AddressWithMask NONE = of(IPv4Address.NONE, IPv4Address.NONE); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 5 | |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 6 | private IPv4AddressWithMask(int rawValue, int rawMask) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 7 | super(IPv4Address.of(rawValue), IPv4Address.of(rawMask)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 8 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 9 | |
| 10 | private IPv4AddressWithMask(IPv4Address value, IPv4Address mask) { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 11 | super(value, mask); |
| 12 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 13 | |
| 14 | public static IPv4AddressWithMask of(int rawValue, int rawMask) { |
| 15 | return new IPv4AddressWithMask(rawValue, rawMask); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 16 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 17 | |
| 18 | public static IPv4AddressWithMask of(IPv4Address value, IPv4Address mask) { |
| 19 | return new IPv4AddressWithMask(value, mask); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 20 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 21 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 22 | @Override |
| 23 | public String toString() { |
| 24 | StringBuilder res = new StringBuilder(); |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 25 | res.append(value.toString()); |
| 26 | |
| 27 | int maskint = mask.getInt(); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 28 | res.append('/'); |
| 29 | if (Integer.bitCount((~maskint) + 1) == 1) { |
| 30 | // CIDR notation |
| 31 | res.append(Integer.bitCount(maskint)); |
| 32 | } else { |
| 33 | // Full address mask |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 34 | res.append(mask.toString()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 35 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 36 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 37 | return res.toString(); |
| 38 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 39 | |
| 40 | public static IPv4AddressWithMask of(final String string) { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 41 | int slashPos; |
| 42 | String ip = string; |
| 43 | int maskBits = 0; |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 44 | IPv4Address maskAddress = null; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 45 | |
| 46 | // Read mask suffix |
| 47 | if ((slashPos = string.indexOf('/')) != -1) { |
| 48 | ip = string.substring(0, slashPos); |
| 49 | try { |
| 50 | String suffix = string.substring(slashPos + 1); |
| 51 | if (suffix.length() == 0) |
| 52 | throw new IllegalArgumentException("IP Address not well formed: " + string); |
| 53 | if (suffix.indexOf('.') != -1) { |
| 54 | // Full mask |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 55 | maskAddress = IPv4Address.of(suffix); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 56 | } else { |
| 57 | // CIDR Suffix |
| 58 | maskBits = Integer.parseInt(suffix); |
| 59 | } |
| 60 | } catch (NumberFormatException e) { |
| 61 | throw new IllegalArgumentException("IP Address not well formed: " + string); |
| 62 | } |
| 63 | if (maskBits < 0 || maskBits > 32) { |
| 64 | throw new IllegalArgumentException("IP Address not well formed: " + string); |
| 65 | } |
| 66 | } |
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 | // Read IP |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 69 | IPv4Address ipv4 = IPv4Address.of(ip); |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 70 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 71 | if (maskAddress != null) { |
| 72 | // Full address mask |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 73 | return IPv4AddressWithMask.of(ipv4, maskAddress); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 74 | } else if (maskBits == 0) { |
| 75 | // No mask |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 76 | return IPv4AddressWithMask.of(ipv4, IPv4Address.NO_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 | } |