blob: 6ca39fe998ae3091dd65adb031bedec3165b5471 [file] [log] [blame]
Yotam Harchol4d634682013-09-26 13:21:06 -07001package org.projectfloodlight.openflow.types;
2
Andreas Wundsam3700d162014-03-11 04:43:38 -07003import javax.annotation.Nonnull;
4
Yotam Harchol4d634682013-09-26 13:21:06 -07005public abstract class IPAddress<F extends IPAddress<F>> implements OFValueType<F> {
6
Yotam Harcholeb023dc2013-09-26 15:45:44 -07007 public abstract IPVersion getIpVersion();
Yotam Harchol4d634682013-09-26 13:21:06 -07008
Gregor Maier7f987e62013-12-10 19:34:18 -08009 /**
10 * Checks if this IPAddress represents a valid CIDR style netmask, i.e.,
11 * it has a set of leading "1" bits followed by only "0" bits
12 * @return true if this represents a valid CIDR style netmask, false
13 * otherwise
14 */
Gregor Maier5615b6c2013-12-11 22:29:07 -080015 public abstract boolean isCidrMask();
Gregor Maier7f987e62013-12-10 19:34:18 -080016
17 /**
18 * If this IPAddress represents a valid CIDR style netmask (see
19 * isCidrMask()) returns the length of the prefix (the number of "1" bits).
Gregor Maier5615b6c2013-12-11 22:29:07 -080020 * @return length of CIDR mask if this represents a valid CIDR mask
21 * @throws IllegalStateException if isCidrMask() == false
Gregor Maier7f987e62013-12-10 19:34:18 -080022 */
23 public abstract int asCidrMaskLength();
24
Aditya Vaja56b8b182014-03-11 13:13:58 -070025 /**
26 * Checks if the IPAddress is the global broadcast address
27 * 255.255.255.255 in case of IPv4
28 * @return boolean true or false
29 */
30 public abstract boolean isBroadcast();
31
32 /**
33 * Perform a low level AND operation on the bits of two IPAddress<?> objects
34 * @param IPAddress<?> other
35 * @return new IPAddress<?> object after the AND oper
36 */
Aditya Vaja98c96e72014-03-11 15:19:01 -070037 public abstract F and(F other);
Aditya Vaja56b8b182014-03-11 13:13:58 -070038
39 /**
40 * Perform a low level OR operation on the bits of two IPAddress<?> objects
41 * @param IPAddress<?> other
42 * @return new IPAddress<?> object after the AND oper
43 */
Aditya Vaja98c96e72014-03-11 15:19:01 -070044 public abstract F or(F other);
Aditya Vaja56b8b182014-03-11 13:13:58 -070045
46 /**
47 * Returns a new IPAddress object with the bits inverted
48 * @return IPAddress<?>
49 */
Aditya Vaja98c96e72014-03-11 15:19:01 -070050 public abstract F not();
Aditya Vaja56b8b182014-03-11 13:13:58 -070051
Gregor Maier7f987e62013-12-10 19:34:18 -080052 @Override
53 public abstract boolean equals(Object other);
54
55 @Override
56 public abstract int hashCode();
57
Andreas Wundsam3700d162014-03-11 04:43:38 -070058 /** parse an IPv4Address or IPv6Address from their conventional string representation.
59 * For details on supported representations, refer to {@link IPv4Address#of(String)}
60 * and {@link IPv6Address#of(String)}
61 *
62 * @param ip a string representation of an IP address
63 * @return the parsed IP address
64 * @throws NullPointerException if ip is null
65 * @throws IllegalArgumentException if string is not a valid IP address
66 */
67 @Nonnull
68 public static IPAddress<?> of(@Nonnull String ip) {
Gregor Maier1ff55972013-12-11 02:22:56 -080069 if (ip == null) {
70 throw new NullPointerException("String ip must not be null");
71 }
Yotam Harchol4d634682013-09-26 13:21:06 -070072 if (ip.indexOf('.') != -1)
73 return IPv4Address.of(ip);
74 else if (ip.indexOf(':') != -1)
75 return IPv6Address.of(ip);
76 else
77 throw new IllegalArgumentException("IP Address not well formed: " + ip);
78 }
79
80}