Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Sovietaced | 551254f | 2014-06-13 22:35:40 -0700 | [diff] [blame] | 3 | import java.net.Inet4Address; |
| 4 | import java.net.Inet6Address; |
Sovietaced | c6f9121 | 2014-06-13 18:36:28 -0700 | [diff] [blame] | 5 | import java.net.InetAddress; |
| 6 | |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 7 | import javax.annotation.Nonnull; |
| 8 | |
Sovietaced | c6f9121 | 2014-06-13 18:36:28 -0700 | [diff] [blame] | 9 | import com.google.common.base.Preconditions; |
| 10 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 11 | public abstract class IPAddress<F extends IPAddress<F>> implements OFValueType<F> { |
| 12 | |
Yotam Harchol | eb023dc | 2013-09-26 15:45:44 -0700 | [diff] [blame] | 13 | public abstract IPVersion getIpVersion(); |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 14 | |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 15 | /** |
| 16 | * Checks if this IPAddress represents a valid CIDR style netmask, i.e., |
| 17 | * it has a set of leading "1" bits followed by only "0" bits |
| 18 | * @return true if this represents a valid CIDR style netmask, false |
| 19 | * otherwise |
| 20 | */ |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 21 | public abstract boolean isCidrMask(); |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * If this IPAddress represents a valid CIDR style netmask (see |
| 25 | * isCidrMask()) returns the length of the prefix (the number of "1" bits). |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 26 | * @return length of CIDR mask if this represents a valid CIDR mask |
| 27 | * @throws IllegalStateException if isCidrMask() == false |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 28 | */ |
| 29 | public abstract int asCidrMaskLength(); |
| 30 | |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Checks if the IPAddress is the global broadcast address |
| 33 | * 255.255.255.255 in case of IPv4 |
| 34 | * @return boolean true or false |
| 35 | */ |
| 36 | public abstract boolean isBroadcast(); |
| 37 | |
| 38 | /** |
| 39 | * Perform a low level AND operation on the bits of two IPAddress<?> objects |
Byungjoon Lee | 2d68678 | 2014-04-23 16:06:23 +0900 | [diff] [blame] | 40 | * @param other IPAddress<?> |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 41 | * @return new IPAddress<?> object after the AND oper |
| 42 | */ |
Aditya Vaja | 98c96e7 | 2014-03-11 15:19:01 -0700 | [diff] [blame] | 43 | public abstract F and(F other); |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Perform a low level OR operation on the bits of two IPAddress<?> objects |
Byungjoon Lee | 2d68678 | 2014-04-23 16:06:23 +0900 | [diff] [blame] | 47 | * @param other IPAddress<?> |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 48 | * @return new IPAddress<?> object after the AND oper |
| 49 | */ |
Aditya Vaja | 98c96e7 | 2014-03-11 15:19:01 -0700 | [diff] [blame] | 50 | public abstract F or(F other); |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Returns a new IPAddress object with the bits inverted |
| 54 | * @return IPAddress<?> |
| 55 | */ |
Aditya Vaja | 98c96e7 | 2014-03-11 15:19:01 -0700 | [diff] [blame] | 56 | public abstract F not(); |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 57 | |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame^] | 58 | public abstract byte[] getBytes(); |
| 59 | |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 60 | @Override |
| 61 | public abstract boolean equals(Object other); |
| 62 | |
| 63 | @Override |
| 64 | public abstract int hashCode(); |
| 65 | |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 66 | /** parse an IPv4Address or IPv6Address from their conventional string representation. |
| 67 | * For details on supported representations, refer to {@link IPv4Address#of(String)} |
| 68 | * and {@link IPv6Address#of(String)} |
| 69 | * |
| 70 | * @param ip a string representation of an IP address |
| 71 | * @return the parsed IP address |
| 72 | * @throws NullPointerException if ip is null |
| 73 | * @throws IllegalArgumentException if string is not a valid IP address |
| 74 | */ |
| 75 | @Nonnull |
| 76 | public static IPAddress<?> of(@Nonnull String ip) { |
Sovietaced | c6f9121 | 2014-06-13 18:36:28 -0700 | [diff] [blame] | 77 | Preconditions.checkNotNull(ip, "ip must not be null"); |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 78 | if (ip.indexOf('.') != -1) |
| 79 | return IPv4Address.of(ip); |
| 80 | else if (ip.indexOf(':') != -1) |
| 81 | return IPv6Address.of(ip); |
| 82 | else |
| 83 | throw new IllegalArgumentException("IP Address not well formed: " + ip); |
| 84 | } |
| 85 | |
Sovietaced | c6f9121 | 2014-06-13 18:36:28 -0700 | [diff] [blame] | 86 | /** |
| 87 | * Factory function for InetAddress values. |
| 88 | * @param address the InetAddress you wish to parse into an IPAddress object. |
| 89 | * @return the IPAddress object. |
Sovietaced | 551254f | 2014-06-13 22:35:40 -0700 | [diff] [blame] | 90 | * @throws NullPointerException if address is null |
Sovietaced | c6f9121 | 2014-06-13 18:36:28 -0700 | [diff] [blame] | 91 | */ |
| 92 | @Nonnull |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame^] | 93 | public static IPAddress<?> of(@Nonnull InetAddress address) { |
Sovietaced | c6f9121 | 2014-06-13 18:36:28 -0700 | [diff] [blame] | 94 | Preconditions.checkNotNull(address, "address must not be null"); |
Sovietaced | 551254f | 2014-06-13 22:35:40 -0700 | [diff] [blame] | 95 | byte [] bytes = address.getAddress(); |
| 96 | if(address instanceof Inet4Address) |
| 97 | return IPv4Address.of(bytes); |
| 98 | else if (address instanceof Inet6Address) |
| 99 | return IPv6Address.of(bytes); |
| 100 | else |
| 101 | return IPAddress.of(address.getHostAddress()); |
Sovietaced | c6f9121 | 2014-06-13 18:36:28 -0700 | [diff] [blame] | 102 | } |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame^] | 103 | |
| 104 | @Deprecated |
| 105 | @Nonnull |
| 106 | public static IPAddress<?> fromInetAddress(@Nonnull InetAddress address) { |
| 107 | return of(address); |
| 108 | } |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 109 | } |