Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 3 | import javax.annotation.Nonnull; |
| 4 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 5 | public abstract class IPAddress<F extends IPAddress<F>> implements OFValueType<F> { |
| 6 | |
Yotam Harchol | eb023dc | 2013-09-26 15:45:44 -0700 | [diff] [blame] | 7 | public abstract IPVersion getIpVersion(); |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 8 | |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 9 | /** |
| 10 | * Checks if this IPAddress represents a valid CIDR style netmask, i.e., |
| 11 | * it has a set of leading "1" bits followed by only "0" bits |
| 12 | * @return true if this represents a valid CIDR style netmask, false |
| 13 | * otherwise |
| 14 | */ |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 15 | public abstract boolean isCidrMask(); |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 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). |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 20 | * @return length of CIDR mask if this represents a valid CIDR mask |
| 21 | * @throws IllegalStateException if isCidrMask() == false |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 22 | */ |
| 23 | public abstract int asCidrMaskLength(); |
| 24 | |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Checks if the IPAddress is the global broadcast address |
| 27 | * 255.255.255.255 in case of IPv4 |
| 28 | * @return boolean true or false |
| 29 | */ |
| 30 | public abstract boolean isBroadcast(); |
| 31 | |
| 32 | /** |
| 33 | * Perform a low level AND operation on the bits of two IPAddress<?> objects |
| 34 | * @param IPAddress<?> other |
| 35 | * @return new IPAddress<?> object after the AND oper |
| 36 | */ |
Aditya Vaja | 98c96e7 | 2014-03-11 15:19:01 -0700 | [diff] [blame] | 37 | public abstract F and(F other); |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Perform a low level OR operation on the bits of two IPAddress<?> objects |
| 41 | * @param IPAddress<?> other |
| 42 | * @return new IPAddress<?> object after the AND oper |
| 43 | */ |
Aditya Vaja | 98c96e7 | 2014-03-11 15:19:01 -0700 | [diff] [blame] | 44 | public abstract F or(F other); |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Returns a new IPAddress object with the bits inverted |
| 48 | * @return IPAddress<?> |
| 49 | */ |
Aditya Vaja | 98c96e7 | 2014-03-11 15:19:01 -0700 | [diff] [blame] | 50 | public abstract F not(); |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 51 | |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 52 | @Override |
| 53 | public abstract boolean equals(Object other); |
| 54 | |
| 55 | @Override |
| 56 | public abstract int hashCode(); |
| 57 | |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 58 | /** parse an IPv4Address or IPv6Address from their conventional string representation. |
| 59 | * For details on supported representations, refer to {@link IPv4Address#of(String)} |
| 60 | * and {@link IPv6Address#of(String)} |
| 61 | * |
| 62 | * @param ip a string representation of an IP address |
| 63 | * @return the parsed IP address |
| 64 | * @throws NullPointerException if ip is null |
| 65 | * @throws IllegalArgumentException if string is not a valid IP address |
| 66 | */ |
| 67 | @Nonnull |
| 68 | public static IPAddress<?> of(@Nonnull String ip) { |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame] | 69 | if (ip == null) { |
| 70 | throw new NullPointerException("String ip must not be null"); |
| 71 | } |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 72 | if (ip.indexOf('.') != -1) |
| 73 | return IPv4Address.of(ip); |
| 74 | else if (ip.indexOf(':') != -1) |
| 75 | return IPv6Address.of(ip); |
| 76 | else |
| 77 | throw new IllegalArgumentException("IP Address not well formed: " + ip); |
| 78 | } |
| 79 | |
| 80 | } |