blob: ad21e06d91a856c8d4e5e2f3a369080945d93b39 [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
nsongd7701e12015-05-13 10:34:26 -070043
44 /**
45 * Checks if the IPAddress is the multicast address
46 * @return boolean true or false
47 */
48 public abstract boolean isMulticast();
49
Aditya Vaja56b8b182014-03-11 13:13:58 -070050 /**
kjwon15270b3632015-02-16 08:41:28 +090051 * Perform a low level AND 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 and(F other);
Aditya Vaja56b8b182014-03-11 13:13:58 -070056
57 /**
kjwon15270b3632015-02-16 08:41:28 +090058 * Perform a low level OR operation on the bits of two IPAddress objects
kjwon157bc85402015-02-12 15:07:42 +090059 * @param other IPAddress
60 * @return new IPAddress object after the AND oper
Aditya Vaja56b8b182014-03-11 13:13:58 -070061 */
Aditya Vaja98c96e72014-03-11 15:19:01 -070062 public abstract F or(F other);
Aditya Vaja56b8b182014-03-11 13:13:58 -070063
64 /**
65 * Returns a new IPAddress object with the bits inverted
kjwon157bc85402015-02-12 15:07:42 +090066 * @return IPAddress
Aditya Vaja56b8b182014-03-11 13:13:58 -070067 */
Aditya Vaja98c96e72014-03-11 15:19:01 -070068 public abstract F not();
Aditya Vaja56b8b182014-03-11 13:13:58 -070069
Ronald Liaf8d3e72014-07-05 01:26:28 -070070 /**
Ronald Libbf01942014-07-07 17:00:13 -070071 * Returns an {@code IPAddressWithMask<F>} object that represents this
72 * IP address masked by the given IP address mask.
73 *
74 * @param mask the {@code F} object that represents the mask
75 * @return an {@code IPAddressWithMask<F>} object that represents this
76 * IP address masked by the given mask
77 * @throws NullPointerException if the given mask was {@code null}
78 */
79 @Nonnull
80 public abstract IPAddressWithMask<F> withMask(@Nonnull final F mask);
81
82 /**
83 * Returns an {@code IPAddressWithMask<F>} object that represents this
84 * IP address masked by the CIDR subnet mask of the given prefix length.
85 *
86 * @param cidrMaskLength the prefix length of the CIDR subnet mask
87 * (i.e. the number of leading one-bits),
88 * where <code>
kjwon157bc85402015-02-12 15:07:42 +090089 * 0 {@literal <=} cidrMaskLength {@literal <=} (F.getLength() * 8)
Ronald Libbf01942014-07-07 17:00:13 -070090 * </code>
91 * @return an {@code IPAddressWithMask<F>} object that
92 * represents this IP address masked by the CIDR
93 * subnet mask of the given prefix length
94 * @throws IllegalArgumentException if the given prefix length was invalid
kjwon157bc85402015-02-12 15:07:42 +090095 * @see org.projectfloodlight.openflow.types.IPv4Address#ofCidrMaskLength(int)
96 * @see org.projectfloodlight.openflow.types.IPv6Address#ofCidrMaskLength(int)
Ronald Libbf01942014-07-07 17:00:13 -070097 */
98 @Nonnull
99 public abstract IPAddressWithMask<F> withMaskOfLength(
100 final int cidrMaskLength);
101
102 /**
Ronald Liaf8d3e72014-07-05 01:26:28 -0700103 * Returns the raw IP address of this {@code IPAddress} object. The result
104 * is in network byte order: the highest order byte of the address is in
105 * {@code getBytes()[0]}.
106 * <p>
107 * Similar to {@link InetAddress#getAddress()}
108 *
109 * @return the raw IP address of this object
110 * @see InetAddress#getAddress()
111 */
Ronald Liffa80792014-07-04 17:50:49 -0700112 public abstract byte[] getBytes();
113
Gregor Maier7f987e62013-12-10 19:34:18 -0800114 @Override
Ronald Liaf8d3e72014-07-05 01:26:28 -0700115 public abstract String toString();
116
117 @Override
Gregor Maier7f987e62013-12-10 19:34:18 -0800118 public abstract boolean equals(Object other);
119
120 @Override
121 public abstract int hashCode();
122
Andreas Wundsam3700d162014-03-11 04:43:38 -0700123 /** parse an IPv4Address or IPv6Address from their conventional string representation.
124 * For details on supported representations, refer to {@link IPv4Address#of(String)}
125 * and {@link IPv6Address#of(String)}
126 *
127 * @param ip a string representation of an IP address
128 * @return the parsed IP address
129 * @throws NullPointerException if ip is null
130 * @throws IllegalArgumentException if string is not a valid IP address
131 */
132 @Nonnull
133 public static IPAddress<?> of(@Nonnull String ip) {
Sovietacedc6f91212014-06-13 18:36:28 -0700134 Preconditions.checkNotNull(ip, "ip must not be null");
Yotam Harchol4d634682013-09-26 13:21:06 -0700135 if (ip.indexOf('.') != -1)
136 return IPv4Address.of(ip);
137 else if (ip.indexOf(':') != -1)
138 return IPv6Address.of(ip);
139 else
140 throw new IllegalArgumentException("IP Address not well formed: " + ip);
141 }
142
Sovietacedc6f91212014-06-13 18:36:28 -0700143 /**
144 * Factory function for InetAddress values.
145 * @param address the InetAddress you wish to parse into an IPAddress object.
146 * @return the IPAddress object.
Sovietaced551254f2014-06-13 22:35:40 -0700147 * @throws NullPointerException if address is null
Sovietacedc6f91212014-06-13 18:36:28 -0700148 */
149 @Nonnull
Ronald Liffa80792014-07-04 17:50:49 -0700150 public static IPAddress<?> of(@Nonnull InetAddress address) {
Sovietacedc6f91212014-06-13 18:36:28 -0700151 Preconditions.checkNotNull(address, "address must not be null");
Sovietaced551254f2014-06-13 22:35:40 -0700152 if(address instanceof Inet4Address)
Ronald Lia87b1fc2014-07-16 19:56:03 -0700153 return IPv4Address.of((Inet4Address) address);
Sovietaced551254f2014-06-13 22:35:40 -0700154 else if (address instanceof Inet6Address)
Ronald Lia87b1fc2014-07-16 19:56:03 -0700155 return IPv6Address.of((Inet6Address) address);
Sovietaced551254f2014-06-13 22:35:40 -0700156 else
157 return IPAddress.of(address.getHostAddress());
Sovietacedc6f91212014-06-13 18:36:28 -0700158 }
Ronald Liffa80792014-07-04 17:50:49 -0700159
Ronald Liaf8d3e72014-07-05 01:26:28 -0700160 /**
161 * Factory function for InetAddress values.
162 * @param address the InetAddress you wish to parse into an IPAddress object.
163 * @return the IPAddress object.
164 * @throws NullPointerException if address is null
165 * @deprecated replaced by {@link #of(InetAddress)}
166 */
Ronald Liffa80792014-07-04 17:50:49 -0700167 @Deprecated
168 @Nonnull
169 public static IPAddress<?> fromInetAddress(@Nonnull InetAddress address) {
170 return of(address);
171 }
Yotam Harchol4d634682013-09-26 13:21:06 -0700172}