blob: c96be836ee7b4871d895e78078668190fa2db14d [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
23 @Override
24 public abstract boolean equals(Object other);
25
26 @Override
27 public abstract int hashCode();
28
Yotam Harchol4d634682013-09-26 13:21:06 -070029 public static IPAddress<?> of(String ip) {
Gregor Maier1ff55972013-12-11 02:22:56 -080030 if (ip == null) {
31 throw new NullPointerException("String ip must not be null");
32 }
Yotam Harchol4d634682013-09-26 13:21:06 -070033 if (ip.indexOf('.') != -1)
34 return IPv4Address.of(ip);
35 else if (ip.indexOf(':') != -1)
36 return IPv6Address.of(ip);
37 else
38 throw new IllegalArgumentException("IP Address not well formed: " + ip);
39 }
40
41}