blob: 5e4e818b0bf9fdc4e726dfc6121b7a98154a3c1d [file] [log] [blame]
Yotam Harchol4d634682013-09-26 13:21:06 -07001package org.projectfloodlight.openflow.types;
2
Sovietaced551254f2014-06-13 22:35:40 -07003import java.net.Inet4Address;
4import java.net.Inet6Address;
Sovietacedc6f91212014-06-13 18:36:28 -07005import java.net.InetAddress;
6
Andreas Wundsam3700d162014-03-11 04:43:38 -07007import javax.annotation.Nonnull;
8
Sovietacedc6f91212014-06-13 18:36:28 -07009import com.google.common.base.Preconditions;
10
Yotam Harchol4d634682013-09-26 13:21:06 -070011public abstract class IPAddress<F extends IPAddress<F>> implements OFValueType<F> {
12
Yotam Harcholeb023dc2013-09-26 15:45:44 -070013 public abstract IPVersion getIpVersion();
Yotam Harchol4d634682013-09-26 13:21:06 -070014
Gregor Maier7f987e62013-12-10 19:34:18 -080015 /**
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 */
Gregor Maier5615b6c2013-12-11 22:29:07 -080021 public abstract boolean isCidrMask();
Gregor Maier7f987e62013-12-10 19:34:18 -080022
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).
Gregor Maier5615b6c2013-12-11 22:29:07 -080026 * @return length of CIDR mask if this represents a valid CIDR mask
27 * @throws IllegalStateException if isCidrMask() == false
Gregor Maier7f987e62013-12-10 19:34:18 -080028 */
29 public abstract int asCidrMaskLength();
30
Aditya Vaja56b8b182014-03-11 13:13:58 -070031 /**
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
Byungjoon Lee2d686782014-04-23 16:06:23 +090040 * @param other IPAddress<?>
Aditya Vaja56b8b182014-03-11 13:13:58 -070041 * @return new IPAddress<?> object after the AND oper
42 */
Aditya Vaja98c96e72014-03-11 15:19:01 -070043 public abstract F and(F other);
Aditya Vaja56b8b182014-03-11 13:13:58 -070044
45 /**
46 * Perform a low level OR operation on the bits of two IPAddress<?> objects
Byungjoon Lee2d686782014-04-23 16:06:23 +090047 * @param other IPAddress<?>
Aditya Vaja56b8b182014-03-11 13:13:58 -070048 * @return new IPAddress<?> object after the AND oper
49 */
Aditya Vaja98c96e72014-03-11 15:19:01 -070050 public abstract F or(F other);
Aditya Vaja56b8b182014-03-11 13:13:58 -070051
52 /**
53 * Returns a new IPAddress object with the bits inverted
54 * @return IPAddress<?>
55 */
Aditya Vaja98c96e72014-03-11 15:19:01 -070056 public abstract F not();
Aditya Vaja56b8b182014-03-11 13:13:58 -070057
Gregor Maier7f987e62013-12-10 19:34:18 -080058 @Override
59 public abstract boolean equals(Object other);
60
61 @Override
62 public abstract int hashCode();
63
Andreas Wundsam3700d162014-03-11 04:43:38 -070064 /** 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) {
Sovietacedc6f91212014-06-13 18:36:28 -070075 Preconditions.checkNotNull(ip, "ip must not be null");
Yotam Harchol4d634682013-09-26 13:21:06 -070076 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
Sovietacedc6f91212014-06-13 18:36:28 -070084 /**
85 * Factory function for InetAddress values.
86 * @param address the InetAddress you wish to parse into an IPAddress object.
87 * @return the IPAddress object.
Sovietaced551254f2014-06-13 22:35:40 -070088 * @throws NullPointerException if address is null
Sovietacedc6f91212014-06-13 18:36:28 -070089 */
90 @Nonnull
91 public static IPAddress<?> fromInetAddress(@Nonnull InetAddress address) {
92 Preconditions.checkNotNull(address, "address must not be null");
Sovietaced551254f2014-06-13 22:35:40 -070093 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());
Sovietacedc6f91212014-06-13 18:36:28 -0700100 }
Yotam Harchol4d634682013-09-26 13:21:06 -0700101}