blob: 996aba2a3f1ebdfba4d9d2e9caa065fb16ff72cc [file] [log] [blame]
Yotam Harchol4d634682013-09-26 13:21:06 -07001package org.projectfloodlight.openflow.types;
2
3public abstract class IPAddress<F extends IPAddress<F>> implements OFValueType<F> {
4
Yotam Harcholeb023dc2013-09-26 15:45:44 -07005 public abstract IPVersion getIpVersion();
Yotam Harchol4d634682013-09-26 13:21:06 -07006
Gregor Maier7f987e62013-12-10 19:34:18 -08007 /**
8 * Checks if this IPAddress represents a valid CIDR style netmask, i.e.,
9 * it has a set of leading "1" bits followed by only "0" bits
10 * @return true if this represents a valid CIDR style netmask, false
11 * otherwise
12 */
Gregor Maier5615b6c2013-12-11 22:29:07 -080013 public abstract boolean isCidrMask();
Gregor Maier7f987e62013-12-10 19:34:18 -080014
15 /**
16 * If this IPAddress represents a valid CIDR style netmask (see
17 * isCidrMask()) returns the length of the prefix (the number of "1" bits).
Gregor Maier5615b6c2013-12-11 22:29:07 -080018 * @return length of CIDR mask if this represents a valid CIDR mask
19 * @throws IllegalStateException if isCidrMask() == false
Gregor Maier7f987e62013-12-10 19:34:18 -080020 */
21 public abstract int asCidrMaskLength();
22
Aditya Vaja56b8b182014-03-11 13:13:58 -070023 /**
24 * Checks if the IPAddress is the global broadcast address
25 * 255.255.255.255 in case of IPv4
26 * @return boolean true or false
27 */
28 public abstract boolean isBroadcast();
29
30 /**
31 * Perform a low level AND operation on the bits of two IPAddress<?> objects
32 * @param IPAddress<?> other
33 * @return new IPAddress<?> object after the AND oper
34 */
35 public abstract IPAddress<?> and(IPAddress<?> other);
36
37 /**
38 * Perform a low level OR operation on the bits of two IPAddress<?> objects
39 * @param IPAddress<?> other
40 * @return new IPAddress<?> object after the AND oper
41 */
42 public abstract IPAddress<?> or(IPAddress<?> other);
43
44 /**
45 * Returns a new IPAddress object with the bits inverted
46 * @return IPAddress<?>
47 */
48 public abstract IPAddress<?> not();
49
Gregor Maier7f987e62013-12-10 19:34:18 -080050 @Override
51 public abstract boolean equals(Object other);
52
53 @Override
54 public abstract int hashCode();
55
Yotam Harchol4d634682013-09-26 13:21:06 -070056 public static IPAddress<?> of(String ip) {
Gregor Maier1ff55972013-12-11 02:22:56 -080057 if (ip == null) {
58 throw new NullPointerException("String ip must not be null");
59 }
Yotam Harchol4d634682013-09-26 13:21:06 -070060 if (ip.indexOf('.') != -1)
61 return IPv4Address.of(ip);
62 else if (ip.indexOf(':') != -1)
63 return IPv6Address.of(ip);
64 else
65 throw new IllegalArgumentException("IP Address not well formed: " + ip);
66 }
67
68}