tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame^] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
| 3 | import com.google.common.base.Preconditions; |
| 4 | |
| 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 | |
| 12 | public abstract IPVersion getIpVersion(); |
| 13 | |
| 14 | public abstract boolean contains(IPAddress<?> ip); |
| 15 | |
| 16 | public F getSubnetBroadcastAddress() { |
| 17 | if (!mask.isCidrMask()) { |
| 18 | throw new IllegalArgumentException("Mask Invalid " + mask + |
| 19 | " cannot get subnet for non CIDR mask"); |
| 20 | } |
| 21 | return value.or(mask.not()); |
| 22 | } |
| 23 | |
| 24 | public boolean isSubnetBroadcastAddress(F candidate) { |
| 25 | return getSubnetBroadcastAddress().equals(candidate); |
| 26 | } |
| 27 | |
| 28 | public static IPAddressWithMask<?> of(String ip) { |
| 29 | Preconditions.checkNotNull(ip, "string ip must not be null"); |
| 30 | |
| 31 | if (ip.indexOf('.') != -1) |
| 32 | return IPv4AddressWithMask.of(ip); |
| 33 | else if (ip.indexOf(':') != -1) |
| 34 | return IPv6AddressWithMask.of(ip); |
| 35 | else |
| 36 | throw new IllegalArgumentException("IP Address not well formed: " + ip); |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public String toString() { |
| 41 | StringBuilder res = new StringBuilder(); |
| 42 | res.append(value.toString()); |
| 43 | |
| 44 | res.append('/'); |
| 45 | if (mask.isCidrMask()) { |
| 46 | // CIDR notation |
| 47 | res.append(mask.asCidrMaskLength()); |
| 48 | } else { |
| 49 | // Full address mask |
| 50 | res.append(mask.toString()); |
| 51 | } |
| 52 | |
| 53 | return res.toString(); |
| 54 | } |
| 55 | } |