Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 3 | import com.google.common.base.Preconditions; |
| 4 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 5 | |
| 6 | public class IPv4AddressWithMask extends IPAddressWithMask<IPv4Address> { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 7 | public final static IPv4AddressWithMask NONE = of(IPv4Address.NONE, IPv4Address.NONE); |
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 | private IPv4AddressWithMask(int rawValue, int rawMask) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 10 | super(IPv4Address.of(rawValue), IPv4Address.of(rawMask)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 11 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 12 | |
| 13 | private IPv4AddressWithMask(IPv4Address value, IPv4Address mask) { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 14 | super(value, mask); |
| 15 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 16 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 17 | @Override |
Yotam Harchol | eb023dc | 2013-09-26 15:45:44 -0700 | [diff] [blame] | 18 | public IPVersion getIpVersion() { |
| 19 | return IPVersion.IPv4; |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 20 | } |
| 21 | |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 22 | public static IPv4AddressWithMask of(int rawValue, int rawMask) { |
| 23 | return new IPv4AddressWithMask(rawValue, rawMask); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 24 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 25 | |
| 26 | public static IPv4AddressWithMask of(IPv4Address value, IPv4Address mask) { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 27 | Preconditions.checkNotNull(value, "value must not be null"); |
| 28 | Preconditions.checkNotNull(mask, "mask must not be null"); |
| 29 | |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 30 | return new IPv4AddressWithMask(value, mask); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 31 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 32 | |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 33 | public static IPv4AddressWithMask of(final String string) { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 34 | Preconditions.checkNotNull(string, "string must not be null"); |
| 35 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 36 | int slashPos; |
| 37 | String ip = string; |
Ronald Li | a780c37 | 2014-07-03 17:34:50 -0700 | [diff] [blame] | 38 | int cidrMaskLength = 32; |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 39 | IPv4Address maskAddress = null; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 40 | |
| 41 | // Read mask suffix |
| 42 | if ((slashPos = string.indexOf('/')) != -1) { |
| 43 | ip = string.substring(0, slashPos); |
| 44 | try { |
| 45 | String suffix = string.substring(slashPos + 1); |
| 46 | if (suffix.length() == 0) |
| 47 | throw new IllegalArgumentException("IP Address not well formed: " + string); |
| 48 | if (suffix.indexOf('.') != -1) { |
| 49 | // Full mask |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 50 | maskAddress = IPv4Address.of(suffix); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 51 | } else { |
| 52 | // CIDR Suffix |
Ronald Li | a780c37 | 2014-07-03 17:34:50 -0700 | [diff] [blame] | 53 | cidrMaskLength = Integer.parseInt(suffix); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 54 | } |
| 55 | } catch (NumberFormatException e) { |
| 56 | throw new IllegalArgumentException("IP Address not well formed: " + string); |
| 57 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 58 | } |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 59 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 60 | // Read IP |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 61 | IPv4Address ipv4 = IPv4Address.of(ip); |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 62 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 63 | if (maskAddress != null) { |
| 64 | // Full address mask |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 65 | return IPv4AddressWithMask.of(ipv4, maskAddress); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 66 | } else { |
Ronald Li | a780c37 | 2014-07-03 17:34:50 -0700 | [diff] [blame] | 67 | return IPv4AddressWithMask.of( |
| 68 | ipv4, IPv4Address.ofCidrMaskLength(cidrMaskLength)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
Sovietaced | 77d9949 | 2014-06-24 12:58:47 -0700 | [diff] [blame] | 71 | |
Sovietaced | 07cc8eb | 2014-06-24 11:55:26 -0700 | [diff] [blame] | 72 | @Override |
| 73 | public boolean contains(IPAddress<?> ip) { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 74 | Preconditions.checkNotNull(ip, "ip must not be null"); |
Sovietaced | 77d9949 | 2014-06-24 12:58:47 -0700 | [diff] [blame] | 75 | |
| 76 | if(ip.getIpVersion() == IPVersion.IPv4) { |
| 77 | IPv4Address ipv4 = (IPv4Address) ip; |
| 78 | return this.matches(ipv4); |
| 79 | } |
| 80 | |
| 81 | return false; |
Sovietaced | 07cc8eb | 2014-06-24 11:55:26 -0700 | [diff] [blame] | 82 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 83 | } |