Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -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 abstract class IPAddressWithMask<F extends IPAddress<F>> extends Masked<F> { |
| 5 | |
| 6 | protected IPAddressWithMask(F value, F mask) { |
| 7 | super(value, mask); |
| 8 | } |
| 9 | |
Yotam Harchol | eb023dc | 2013-09-26 15:45:44 -0700 | [diff] [blame] | 10 | public abstract IPVersion getIpVersion(); |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 11 | |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame^] | 12 | public IPAddress<?> getSubnetBroadcastAddress() { |
| 13 | return value.or(mask.not()); |
| 14 | } |
| 15 | |
| 16 | public boolean isSubnetBroadcastAddress(IPAddress<?> candidate) { |
| 17 | return getSubnetBroadcastAddress().equals(candidate); |
| 18 | } |
| 19 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 20 | public static IPAddressWithMask<?> of(String ip) { |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame] | 21 | if (ip == null) { |
| 22 | throw new NullPointerException("String ip must not be null"); |
| 23 | } |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 24 | if (ip.indexOf('.') != -1) |
| 25 | return IPv4AddressWithMask.of(ip); |
| 26 | else if (ip.indexOf(':') != -1) |
| 27 | return IPv6AddressWithMask.of(ip); |
| 28 | else |
| 29 | throw new IllegalArgumentException("IP Address not well formed: " + ip); |
| 30 | } |
| 31 | |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame] | 32 | @Override |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 33 | public String toString() { |
| 34 | StringBuilder res = new StringBuilder(); |
| 35 | res.append(value.toString()); |
| 36 | |
| 37 | res.append('/'); |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 38 | if (mask.isCidrMask()) { |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 39 | // CIDR notation |
| 40 | res.append(mask.asCidrMaskLength()); |
| 41 | } else { |
| 42 | // Full address mask |
| 43 | res.append(mask.toString()); |
| 44 | } |
| 45 | |
| 46 | return res.toString(); |
| 47 | } |
| 48 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 49 | } |