blob: cee9ad1850b989b2abac397cd0a8136ce18c4528 [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -07001package org.projectfloodlight.openflow.types;
2
3import java.net.Inet4Address;
4import java.net.Inet6Address;
5import java.net.InetAddress;
6
7import javax.annotation.Nonnull;
8
9import com.google.common.base.Preconditions;
10
11public abstract class IPAddress<F extends IPAddress<F>> implements OFValueType<F> {
12
13 /**
14 * Returns the Internet Protocol (IP) version of this object
15 *
16 * @return the Internet Protocol (IP) version of this object
17 */
18 public abstract IPVersion getIpVersion();
19
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 */
26 public abstract boolean isCidrMask();
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).
31 * @return length of CIDR mask if this represents a valid CIDR mask
32 * @throws IllegalStateException if isCidrMask() == false
33 */
34 public abstract int asCidrMaskLength();
35
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
45 * @param other IPAddress<?>
46 * @return new IPAddress<?> object after the AND oper
47 */
48 public abstract F and(F other);
49
50 /**
51 * Perform a low level OR operation on the bits of two IPAddress<?> objects
52 * @param other IPAddress<?>
53 * @return new IPAddress<?> object after the AND oper
54 */
55 public abstract F or(F other);
56
57 /**
58 * Returns a new IPAddress object with the bits inverted
59 * @return IPAddress<?>
60 */
61 public abstract F not();
62
63 /**
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 /**
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 */
104 public abstract byte[] getBytes();
105
106 @Override
107 public abstract String toString();
108
109 @Override
110 public abstract boolean equals(Object other);
111
112 @Override
113 public abstract int hashCode();
114
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) {
126 Preconditions.checkNotNull(ip, "ip must not be null");
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
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.
139 * @throws NullPointerException if address is null
140 */
141 @Nonnull
142 public static IPAddress<?> of(@Nonnull InetAddress address) {
143 Preconditions.checkNotNull(address, "address must not be null");
144 if(address instanceof Inet4Address)
145 return IPv4Address.of((Inet4Address) address);
146 else if (address instanceof Inet6Address)
147 return IPv6Address.of((Inet6Address) address);
148 else
149 return IPAddress.of(address.getHostAddress());
150 }
151
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 */
159 @Deprecated
160 @Nonnull
161 public static IPAddress<?> fromInetAddress(@Nonnull InetAddress address) {
162 return of(address);
163 }
164}