blob: 5e4e818b0bf9fdc4e726dfc6121b7a98154a3c1d [file] [log] [blame]
alshabib1f44e8e2014-08-14 15:19:57 -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 public abstract IPVersion getIpVersion();
14
15 /**
16 * Checks if this IPAddress represents a valid CIDR style netmask, i.e.,
17 * it has a set of leading "1" bits followed by only "0" bits
18 * @return true if this represents a valid CIDR style netmask, false
19 * otherwise
20 */
21 public abstract boolean isCidrMask();
22
23 /**
24 * If this IPAddress represents a valid CIDR style netmask (see
25 * isCidrMask()) returns the length of the prefix (the number of "1" bits).
26 * @return length of CIDR mask if this represents a valid CIDR mask
27 * @throws IllegalStateException if isCidrMask() == false
28 */
29 public abstract int asCidrMaskLength();
30
31 /**
32 * Checks if the IPAddress is the global broadcast address
33 * 255.255.255.255 in case of IPv4
34 * @return boolean true or false
35 */
36 public abstract boolean isBroadcast();
37
38 /**
39 * Perform a low level AND operation on the bits of two IPAddress<?> objects
40 * @param other IPAddress<?>
41 * @return new IPAddress<?> object after the AND oper
42 */
43 public abstract F and(F other);
44
45 /**
46 * Perform a low level OR operation on the bits of two IPAddress<?> objects
47 * @param other IPAddress<?>
48 * @return new IPAddress<?> object after the AND oper
49 */
50 public abstract F or(F other);
51
52 /**
53 * Returns a new IPAddress object with the bits inverted
54 * @return IPAddress<?>
55 */
56 public abstract F not();
57
58 @Override
59 public abstract boolean equals(Object other);
60
61 @Override
62 public abstract int hashCode();
63
64 /** parse an IPv4Address or IPv6Address from their conventional string representation.
65 * For details on supported representations, refer to {@link IPv4Address#of(String)}
66 * and {@link IPv6Address#of(String)}
67 *
68 * @param ip a string representation of an IP address
69 * @return the parsed IP address
70 * @throws NullPointerException if ip is null
71 * @throws IllegalArgumentException if string is not a valid IP address
72 */
73 @Nonnull
74 public static IPAddress<?> of(@Nonnull String ip) {
75 Preconditions.checkNotNull(ip, "ip must not be null");
76 if (ip.indexOf('.') != -1)
77 return IPv4Address.of(ip);
78 else if (ip.indexOf(':') != -1)
79 return IPv6Address.of(ip);
80 else
81 throw new IllegalArgumentException("IP Address not well formed: " + ip);
82 }
83
84 /**
85 * Factory function for InetAddress values.
86 * @param address the InetAddress you wish to parse into an IPAddress object.
87 * @return the IPAddress object.
88 * @throws NullPointerException if address is null
89 */
90 @Nonnull
91 public static IPAddress<?> fromInetAddress(@Nonnull InetAddress address) {
92 Preconditions.checkNotNull(address, "address must not be null");
93 byte [] bytes = address.getAddress();
94 if(address instanceof Inet4Address)
95 return IPv4Address.of(bytes);
96 else if (address instanceof Inet6Address)
97 return IPv6Address.of(bytes);
98 else
99 return IPAddress.of(address.getHostAddress());
100 }
101}