blob: e1f739d720a86295544798dae0f5ed97f663a00d [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 /**
kjwon15270b3632015-02-16 08:41:28 +090044 * Perform a low level AND operation on the bits of two IPAddress objects
kjwon157bc85402015-02-12 15:07:42 +090045 * @param other IPAddress
46 * @return new IPAddress object after the AND oper
Aditya Vaja56b8b182014-03-11 13:13:58 -070047 */
Aditya Vaja98c96e72014-03-11 15:19:01 -070048 public abstract F and(F other);
Aditya Vaja56b8b182014-03-11 13:13:58 -070049
50 /**
kjwon15270b3632015-02-16 08:41:28 +090051 * Perform a low level OR operation on the bits of two IPAddress objects
kjwon157bc85402015-02-12 15:07:42 +090052 * @param other IPAddress
53 * @return new IPAddress object after the AND oper
Aditya Vaja56b8b182014-03-11 13:13:58 -070054 */
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
kjwon157bc85402015-02-12 15:07:42 +090059 * @return IPAddress
Aditya Vaja56b8b182014-03-11 13:13:58 -070060 */
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>
kjwon157bc85402015-02-12 15:07:42 +090082 * 0 {@literal <=} cidrMaskLength {@literal <=} (F.getLength() * 8)
Ronald Libbf01942014-07-07 17:00:13 -070083 * </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
kjwon157bc85402015-02-12 15:07:42 +090088 * @see org.projectfloodlight.openflow.types.IPv4Address#ofCidrMaskLength(int)
89 * @see org.projectfloodlight.openflow.types.IPv6Address#ofCidrMaskLength(int)
Ronald Libbf01942014-07-07 17:00:13 -070090 */
91 @Nonnull
92 public abstract IPAddressWithMask<F> withMaskOfLength(
93 final int cidrMaskLength);
94
95 /**
Ronald Liaf8d3e72014-07-05 01:26:28 -070096 * Returns the raw IP address of this {@code IPAddress} object. The result
97 * is in network byte order: the highest order byte of the address is in
98 * {@code getBytes()[0]}.
99 * <p>
100 * Similar to {@link InetAddress#getAddress()}
101 *
102 * @return the raw IP address of this object
103 * @see InetAddress#getAddress()
104 */
Ronald Liffa80792014-07-04 17:50:49 -0700105 public abstract byte[] getBytes();
106
Gregor Maier7f987e62013-12-10 19:34:18 -0800107 @Override
Ronald Liaf8d3e72014-07-05 01:26:28 -0700108 public abstract String toString();
109
110 @Override
Gregor Maier7f987e62013-12-10 19:34:18 -0800111 public abstract boolean equals(Object other);
112
113 @Override
114 public abstract int hashCode();
115
Andreas Wundsam3700d162014-03-11 04:43:38 -0700116 /** parse an IPv4Address or IPv6Address from their conventional string representation.
117 * For details on supported representations, refer to {@link IPv4Address#of(String)}
118 * and {@link IPv6Address#of(String)}
119 *
120 * @param ip a string representation of an IP address
121 * @return the parsed IP address
122 * @throws NullPointerException if ip is null
123 * @throws IllegalArgumentException if string is not a valid IP address
124 */
125 @Nonnull
126 public static IPAddress<?> of(@Nonnull String ip) {
Sovietacedc6f91212014-06-13 18:36:28 -0700127 Preconditions.checkNotNull(ip, "ip must not be null");
Yotam Harchol4d634682013-09-26 13:21:06 -0700128 if (ip.indexOf('.') != -1)
129 return IPv4Address.of(ip);
130 else if (ip.indexOf(':') != -1)
131 return IPv6Address.of(ip);
132 else
133 throw new IllegalArgumentException("IP Address not well formed: " + ip);
134 }
135
Sovietacedc6f91212014-06-13 18:36:28 -0700136 /**
137 * Factory function for InetAddress values.
138 * @param address the InetAddress you wish to parse into an IPAddress object.
139 * @return the IPAddress object.
Sovietaced551254f2014-06-13 22:35:40 -0700140 * @throws NullPointerException if address is null
Sovietacedc6f91212014-06-13 18:36:28 -0700141 */
142 @Nonnull
Ronald Liffa80792014-07-04 17:50:49 -0700143 public static IPAddress<?> of(@Nonnull InetAddress address) {
Sovietacedc6f91212014-06-13 18:36:28 -0700144 Preconditions.checkNotNull(address, "address must not be null");
Sovietaced551254f2014-06-13 22:35:40 -0700145 if(address instanceof Inet4Address)
Ronald Lia87b1fc2014-07-16 19:56:03 -0700146 return IPv4Address.of((Inet4Address) address);
Sovietaced551254f2014-06-13 22:35:40 -0700147 else if (address instanceof Inet6Address)
Ronald Lia87b1fc2014-07-16 19:56:03 -0700148 return IPv6Address.of((Inet6Address) address);
Sovietaced551254f2014-06-13 22:35:40 -0700149 else
150 return IPAddress.of(address.getHostAddress());
Sovietacedc6f91212014-06-13 18:36:28 -0700151 }
Ronald Liffa80792014-07-04 17:50:49 -0700152
Ronald Liaf8d3e72014-07-05 01:26:28 -0700153 /**
154 * Factory function for InetAddress values.
155 * @param address the InetAddress you wish to parse into an IPAddress object.
156 * @return the IPAddress object.
157 * @throws NullPointerException if address is null
158 * @deprecated replaced by {@link #of(InetAddress)}
159 */
Ronald Liffa80792014-07-04 17:50:49 -0700160 @Deprecated
161 @Nonnull
162 public static IPAddress<?> fromInetAddress(@Nonnull InetAddress address) {
163 return of(address);
164 }
Yotam Harchol4d634682013-09-26 13:21:06 -0700165}