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