blob: ed8a11b7e410884cac48b60bead71cefcb22e594 [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 */
13 public boolean isCidrMask() {
14 return asCidrMaskLength() != -1;
15 }
16
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).
20 * @return length of CIDR mask or -1 if this is not a CIDR netmask
21 */
22 public abstract int asCidrMaskLength();
23
24 @Override
25 public abstract boolean equals(Object other);
26
27 @Override
28 public abstract int hashCode();
29
Yotam Harchol4d634682013-09-26 13:21:06 -070030 public static IPAddress<?> of(String ip) {
31 if (ip.indexOf('.') != -1)
32 return IPv4Address.of(ip);
33 else if (ip.indexOf(':') != -1)
34 return IPv6Address.of(ip);
35 else
36 throw new IllegalArgumentException("IP Address not well formed: " + ip);
37 }
38
39}