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 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 13 | /** |
| 14 | * Returns the Internet Protocol (IP) version of this object |
| 15 | * |
| 16 | * @return the Internet Protocol (IP) version of this object |
| 17 | */ |
Yotam Harchol | eb023dc | 2013-09-26 15:45:44 -0700 | [diff] [blame] | 18 | public abstract IPVersion getIpVersion(); |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 19 | |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 20 | /** |
| 21 | * Checks if this IPAddress represents a valid CIDR style netmask, i.e., |
| 22 | * it has a set of leading "1" bits followed by only "0" bits |
| 23 | * @return true if this represents a valid CIDR style netmask, false |
| 24 | * otherwise |
| 25 | */ |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 26 | public abstract boolean isCidrMask(); |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * If this IPAddress represents a valid CIDR style netmask (see |
| 30 | * isCidrMask()) returns the length of the prefix (the number of "1" bits). |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 31 | * @return length of CIDR mask if this represents a valid CIDR mask |
| 32 | * @throws IllegalStateException if isCidrMask() == false |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 33 | */ |
| 34 | public abstract int asCidrMaskLength(); |
| 35 | |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Checks if the IPAddress is the global broadcast address |
| 38 | * 255.255.255.255 in case of IPv4 |
| 39 | * @return boolean true or false |
| 40 | */ |
| 41 | public abstract boolean isBroadcast(); |
| 42 | |
| 43 | /** |
| 44 | * 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] | 45 | * @param other IPAddress<?> |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 46 | * @return new IPAddress<?> object after the AND oper |
| 47 | */ |
Aditya Vaja | 98c96e7 | 2014-03-11 15:19:01 -0700 | [diff] [blame] | 48 | public abstract F and(F other); |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * 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] | 52 | * @param other IPAddress<?> |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 53 | * @return new IPAddress<?> object after the AND oper |
| 54 | */ |
Aditya Vaja | 98c96e7 | 2014-03-11 15:19:01 -0700 | [diff] [blame] | 55 | public abstract F or(F other); |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * Returns a new IPAddress object with the bits inverted |
| 59 | * @return IPAddress<?> |
| 60 | */ |
Aditya Vaja | 98c96e7 | 2014-03-11 15:19:01 -0700 | [diff] [blame] | 61 | public abstract F not(); |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 62 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 63 | /** |
Ronald Li | bbf0194 | 2014-07-07 17:00:13 -0700 | [diff] [blame] | 64 | * Returns an {@code IPAddressWithMask<F>} object that represents this |
| 65 | * IP address masked by the given IP address mask. |
| 66 | * |
| 67 | * @param mask the {@code F} object that represents the mask |
| 68 | * @return an {@code IPAddressWithMask<F>} object that represents this |
| 69 | * IP address masked by the given mask |
| 70 | * @throws NullPointerException if the given mask was {@code null} |
| 71 | */ |
| 72 | @Nonnull |
| 73 | public abstract IPAddressWithMask<F> withMask(@Nonnull final F mask); |
| 74 | |
| 75 | /** |
| 76 | * Returns an {@code IPAddressWithMask<F>} object that represents this |
| 77 | * IP address masked by the CIDR subnet mask of the given prefix length. |
| 78 | * |
| 79 | * @param cidrMaskLength the prefix length of the CIDR subnet mask |
| 80 | * (i.e. the number of leading one-bits), |
| 81 | * where <code> |
| 82 | * 0 <= cidrMaskLength <= (F.getLength() * 8) |
| 83 | * </code> |
| 84 | * @return an {@code IPAddressWithMask<F>} object that |
| 85 | * represents this IP address masked by the CIDR |
| 86 | * subnet mask of the given prefix length |
| 87 | * @throws IllegalArgumentException if the given prefix length was invalid |
| 88 | * @see #ofCidrMaskLength(int) |
| 89 | */ |
| 90 | @Nonnull |
| 91 | public abstract IPAddressWithMask<F> withMaskOfLength( |
| 92 | final int cidrMaskLength); |
| 93 | |
| 94 | /** |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 95 | * Returns the raw IP address of this {@code IPAddress} object. The result |
| 96 | * is in network byte order: the highest order byte of the address is in |
| 97 | * {@code getBytes()[0]}. |
| 98 | * <p> |
| 99 | * Similar to {@link InetAddress#getAddress()} |
| 100 | * |
| 101 | * @return the raw IP address of this object |
| 102 | * @see InetAddress#getAddress() |
| 103 | */ |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 104 | public abstract byte[] getBytes(); |
| 105 | |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 106 | @Override |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 107 | public abstract String toString(); |
| 108 | |
| 109 | @Override |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 110 | public abstract boolean equals(Object other); |
| 111 | |
| 112 | @Override |
| 113 | public abstract int hashCode(); |
| 114 | |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 115 | /** parse an IPv4Address or IPv6Address from their conventional string representation. |
| 116 | * For details on supported representations, refer to {@link IPv4Address#of(String)} |
| 117 | * and {@link IPv6Address#of(String)} |
| 118 | * |
| 119 | * @param ip a string representation of an IP address |
| 120 | * @return the parsed IP address |
| 121 | * @throws NullPointerException if ip is null |
| 122 | * @throws IllegalArgumentException if string is not a valid IP address |
| 123 | */ |
| 124 | @Nonnull |
| 125 | public static IPAddress<?> of(@Nonnull String ip) { |
Sovietaced | c6f9121 | 2014-06-13 18:36:28 -0700 | [diff] [blame] | 126 | Preconditions.checkNotNull(ip, "ip must not be null"); |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 127 | if (ip.indexOf('.') != -1) |
| 128 | return IPv4Address.of(ip); |
| 129 | else if (ip.indexOf(':') != -1) |
| 130 | return IPv6Address.of(ip); |
| 131 | else |
| 132 | throw new IllegalArgumentException("IP Address not well formed: " + ip); |
| 133 | } |
| 134 | |
Sovietaced | c6f9121 | 2014-06-13 18:36:28 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Factory function for InetAddress values. |
| 137 | * @param address the InetAddress you wish to parse into an IPAddress object. |
| 138 | * @return the IPAddress object. |
Sovietaced | 551254f | 2014-06-13 22:35:40 -0700 | [diff] [blame] | 139 | * @throws NullPointerException if address is null |
Sovietaced | c6f9121 | 2014-06-13 18:36:28 -0700 | [diff] [blame] | 140 | */ |
| 141 | @Nonnull |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 142 | public static IPAddress<?> of(@Nonnull InetAddress address) { |
Sovietaced | c6f9121 | 2014-06-13 18:36:28 -0700 | [diff] [blame] | 143 | Preconditions.checkNotNull(address, "address must not be null"); |
Sovietaced | 551254f | 2014-06-13 22:35:40 -0700 | [diff] [blame] | 144 | if(address instanceof Inet4Address) |
Ronald Li | a87b1fc | 2014-07-16 19:56:03 -0700 | [diff] [blame] | 145 | return IPv4Address.of((Inet4Address) address); |
Sovietaced | 551254f | 2014-06-13 22:35:40 -0700 | [diff] [blame] | 146 | else if (address instanceof Inet6Address) |
Ronald Li | a87b1fc | 2014-07-16 19:56:03 -0700 | [diff] [blame] | 147 | return IPv6Address.of((Inet6Address) address); |
Sovietaced | 551254f | 2014-06-13 22:35:40 -0700 | [diff] [blame] | 148 | else |
| 149 | return IPAddress.of(address.getHostAddress()); |
Sovietaced | c6f9121 | 2014-06-13 18:36:28 -0700 | [diff] [blame] | 150 | } |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 151 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 152 | /** |
| 153 | * Factory function for InetAddress values. |
| 154 | * @param address the InetAddress you wish to parse into an IPAddress object. |
| 155 | * @return the IPAddress object. |
| 156 | * @throws NullPointerException if address is null |
| 157 | * @deprecated replaced by {@link #of(InetAddress)} |
| 158 | */ |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 159 | @Deprecated |
| 160 | @Nonnull |
| 161 | public static IPAddress<?> fromInetAddress(@Nonnull InetAddress address) { |
| 162 | return of(address); |
| 163 | } |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 164 | } |