Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
| 3 | public abstract class IPAddress<F extends IPAddress<F>> implements OFValueType<F> { |
| 4 | |
Yotam Harchol | eb023dc | 2013-09-26 15:45:44 -0700 | [diff] [blame] | 5 | public abstract IPVersion getIpVersion(); |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 6 | |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame^] | 7 | /** |
| 8 | * Checks if this IPAddress represents a valid CIDR style netmask, i.e., |
| 9 | * it has a set of leading "1" bits followed by only "0" bits |
| 10 | * @return true if this represents a valid CIDR style netmask, false |
| 11 | * otherwise |
| 12 | */ |
| 13 | public boolean isCidrMask() { |
| 14 | return asCidrMaskLength() != -1; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * If this IPAddress represents a valid CIDR style netmask (see |
| 19 | * isCidrMask()) returns the length of the prefix (the number of "1" bits). |
| 20 | * @return length of CIDR mask or -1 if this is not a CIDR netmask |
| 21 | */ |
| 22 | public abstract int asCidrMaskLength(); |
| 23 | |
| 24 | @Override |
| 25 | public abstract boolean equals(Object other); |
| 26 | |
| 27 | @Override |
| 28 | public abstract int hashCode(); |
| 29 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 30 | public static IPAddress<?> of(String ip) { |
| 31 | if (ip.indexOf('.') != -1) |
| 32 | return IPv4Address.of(ip); |
| 33 | else if (ip.indexOf(':') != -1) |
| 34 | return IPv6Address.of(ip); |
| 35 | else |
| 36 | throw new IllegalArgumentException("IP Address not well formed: " + ip); |
| 37 | } |
| 38 | |
| 39 | } |