Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Sovietaced | 88e74ec | 2014-06-16 23:02:45 -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 abstract class IPAddressWithMask<F extends IPAddress<F>> extends Masked<F> { |
| 7 | |
| 8 | protected IPAddressWithMask(F value, F mask) { |
| 9 | super(value, mask); |
| 10 | } |
| 11 | |
Yotam Harchol | eb023dc | 2013-09-26 15:45:44 -0700 | [diff] [blame] | 12 | public abstract IPVersion getIpVersion(); |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 13 | |
Aditya Vaja | 98c96e7 | 2014-03-11 15:19:01 -0700 | [diff] [blame] | 14 | public F getSubnetBroadcastAddress() { |
Aditya Vaja | b21043d | 2014-03-11 13:34:06 -0700 | [diff] [blame] | 15 | if (!mask.isCidrMask()) { |
| 16 | throw new IllegalArgumentException("Mask Invalid " + mask + |
| 17 | " cannot get subnet for non CIDR mask"); |
| 18 | } |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 19 | return value.or(mask.not()); |
| 20 | } |
| 21 | |
Aditya Vaja | 98c96e7 | 2014-03-11 15:19:01 -0700 | [diff] [blame] | 22 | public boolean isSubnetBroadcastAddress(F candidate) { |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 23 | return getSubnetBroadcastAddress().equals(candidate); |
| 24 | } |
| 25 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 26 | public static IPAddressWithMask<?> of(String ip) { |
Sovietaced | 88e74ec | 2014-06-16 23:02:45 -0700 | [diff] [blame] | 27 | Preconditions.checkNotNull(ip, "string ip must not be null"); |
| 28 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 29 | if (ip.indexOf('.') != -1) |
| 30 | return IPv4AddressWithMask.of(ip); |
| 31 | else if (ip.indexOf(':') != -1) |
| 32 | return IPv6AddressWithMask.of(ip); |
| 33 | else |
| 34 | throw new IllegalArgumentException("IP Address not well formed: " + ip); |
| 35 | } |
| 36 | |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame] | 37 | @Override |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 38 | public String toString() { |
| 39 | StringBuilder res = new StringBuilder(); |
| 40 | res.append(value.toString()); |
| 41 | |
| 42 | res.append('/'); |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 43 | if (mask.isCidrMask()) { |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 44 | // CIDR notation |
| 45 | res.append(mask.asCidrMaskLength()); |
| 46 | } else { |
| 47 | // Full address mask |
| 48 | res.append(mask.toString()); |
| 49 | } |
| 50 | |
| 51 | return res.toString(); |
| 52 | } |
| 53 | |
Sovietaced | 88e74ec | 2014-06-16 23:02:45 -0700 | [diff] [blame] | 54 | public boolean contains(IPAddress<?> ip) { |
| 55 | Preconditions.checkNotNull(ip, "ip must not be null"); |
| 56 | |
| 57 | // Ensure mask and IP of the same version |
| 58 | if(getIpVersion() == ip.getIpVersion()) { |
| 59 | if(getIpVersion() == IPVersion.IPv4) { |
| 60 | IPv4AddressWithMask ipv4mask = (IPv4AddressWithMask) this; |
| 61 | IPv4Address ipv4 = (IPv4Address) ip; |
| 62 | return ipv4mask.matches(ipv4); |
| 63 | } else { |
| 64 | IPv6AddressWithMask ipv6mask = (IPv6AddressWithMask) this; |
| 65 | IPv6Address ipv6 = (IPv6Address) ip; |
| 66 | return ipv6mask.matches(ipv6); |
| 67 | } |
| 68 | } |
| 69 | return false; |
| 70 | } |
| 71 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 72 | } |