blob: 4cb0fa5379ad5f3c089c6d42a0ecfcb585449a00 [file] [log] [blame]
Yotam Harchol4d634682013-09-26 13:21:06 -07001package org.projectfloodlight.openflow.types;
2
Sovietaced551254f2014-06-13 22:35:40 -07003import java.net.Inet4Address;
4import java.net.Inet6Address;
Sovietacedc6f91212014-06-13 18:36:28 -07005import java.net.InetAddress;
6
Andreas Wundsam3700d162014-03-11 04:43:38 -07007import javax.annotation.Nonnull;
8
Sovietacedc6f91212014-06-13 18:36:28 -07009import com.google.common.base.Preconditions;
10
Yotam Harchol4d634682013-09-26 13:21:06 -070011public abstract class IPAddress<F extends IPAddress<F>> implements OFValueType<F> {
12
Ronald Liaf8d3e72014-07-05 01:26:28 -070013 /**
14 * Returns the Internet Protocol (IP) version of this object
15 *
16 * @return the Internet Protocol (IP) version of this object
17 */
Yotam Harcholeb023dc2013-09-26 15:45:44 -070018 public abstract IPVersion getIpVersion();
Yotam Harchol4d634682013-09-26 13:21:06 -070019
Gregor Maier7f987e62013-12-10 19:34:18 -080020 /**
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 Maier5615b6c2013-12-11 22:29:07 -080026 public abstract boolean isCidrMask();
Gregor Maier7f987e62013-12-10 19:34:18 -080027
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 Maier5615b6c2013-12-11 22:29:07 -080031 * @return length of CIDR mask if this represents a valid CIDR mask
32 * @throws IllegalStateException if isCidrMask() == false
Gregor Maier7f987e62013-12-10 19:34:18 -080033 */
34 public abstract int asCidrMaskLength();
35
Aditya Vaja56b8b182014-03-11 13:13:58 -070036 /**
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 Lee2d686782014-04-23 16:06:23 +090045 * @param other IPAddress<?>
Aditya Vaja56b8b182014-03-11 13:13:58 -070046 * @return new IPAddress<?> object after the AND oper
47 */
Aditya Vaja98c96e72014-03-11 15:19:01 -070048 public abstract F and(F other);
Aditya Vaja56b8b182014-03-11 13:13:58 -070049
50 /**
51 * Perform a low level OR operation on the bits of two IPAddress<?> objects
Byungjoon Lee2d686782014-04-23 16:06:23 +090052 * @param other IPAddress<?>
Aditya Vaja56b8b182014-03-11 13:13:58 -070053 * @return new IPAddress<?> object after the AND oper
54 */
Aditya Vaja98c96e72014-03-11 15:19:01 -070055 public abstract F or(F other);
Aditya Vaja56b8b182014-03-11 13:13:58 -070056
57 /**
58 * Returns a new IPAddress object with the bits inverted
59 * @return IPAddress<?>
60 */
Aditya Vaja98c96e72014-03-11 15:19:01 -070061 public abstract F not();
Aditya Vaja56b8b182014-03-11 13:13:58 -070062
Ronald Liaf8d3e72014-07-05 01:26:28 -070063 /**
Ronald Libbf01942014-07-07 17:00:13 -070064 * 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 Liaf8d3e72014-07-05 01:26:28 -070095 * 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 Liffa80792014-07-04 17:50:49 -0700104 public abstract byte[] getBytes();
105
Gregor Maier7f987e62013-12-10 19:34:18 -0800106 @Override
Ronald Liaf8d3e72014-07-05 01:26:28 -0700107 public abstract String toString();
108
109 @Override
Gregor Maier7f987e62013-12-10 19:34:18 -0800110 public abstract boolean equals(Object other);
111
112 @Override
113 public abstract int hashCode();
114
Andreas Wundsam3700d162014-03-11 04:43:38 -0700115 /** 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) {
Sovietacedc6f91212014-06-13 18:36:28 -0700126 Preconditions.checkNotNull(ip, "ip must not be null");
Yotam Harchol4d634682013-09-26 13:21:06 -0700127 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
Sovietacedc6f91212014-06-13 18:36:28 -0700135 /**
136 * Factory function for InetAddress values.
137 * @param address the InetAddress you wish to parse into an IPAddress object.
138 * @return the IPAddress object.
Sovietaced551254f2014-06-13 22:35:40 -0700139 * @throws NullPointerException if address is null
Sovietacedc6f91212014-06-13 18:36:28 -0700140 */
141 @Nonnull
Ronald Liffa80792014-07-04 17:50:49 -0700142 public static IPAddress<?> of(@Nonnull InetAddress address) {
Sovietacedc6f91212014-06-13 18:36:28 -0700143 Preconditions.checkNotNull(address, "address must not be null");
Sovietaced551254f2014-06-13 22:35:40 -0700144 if(address instanceof Inet4Address)
Ronald Li10929562014-07-07 01:16:44 -0700145 return IPv4Address.of(address);
Sovietaced551254f2014-06-13 22:35:40 -0700146 else if (address instanceof Inet6Address)
Ronald Li10929562014-07-07 01:16:44 -0700147 return IPv6Address.of(address);
Sovietaced551254f2014-06-13 22:35:40 -0700148 else
149 return IPAddress.of(address.getHostAddress());
Sovietacedc6f91212014-06-13 18:36:28 -0700150 }
Ronald Liffa80792014-07-04 17:50:49 -0700151
Ronald Liaf8d3e72014-07-05 01:26:28 -0700152 /**
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 Liffa80792014-07-04 17:50:49 -0700159 @Deprecated
160 @Nonnull
161 public static IPAddress<?> fromInetAddress(@Nonnull InetAddress address) {
162 return of(address);
163 }
Yotam Harchol4d634682013-09-26 13:21:06 -0700164}