blob: 8831ee17758dbf7d9570dcf824c316edac684400 [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
Ronald Liffa80792014-07-04 17:50:49 -070058 public abstract byte[] getBytes();
59
Gregor Maier7f987e62013-12-10 19:34:18 -080060 @Override
61 public abstract boolean equals(Object other);
62
63 @Override
64 public abstract int hashCode();
65
Andreas Wundsam3700d162014-03-11 04:43:38 -070066 /** parse an IPv4Address or IPv6Address from their conventional string representation.
67 * For details on supported representations, refer to {@link IPv4Address#of(String)}
68 * and {@link IPv6Address#of(String)}
69 *
70 * @param ip a string representation of an IP address
71 * @return the parsed IP address
72 * @throws NullPointerException if ip is null
73 * @throws IllegalArgumentException if string is not a valid IP address
74 */
75 @Nonnull
76 public static IPAddress<?> of(@Nonnull String ip) {
Sovietacedc6f91212014-06-13 18:36:28 -070077 Preconditions.checkNotNull(ip, "ip must not be null");
Yotam Harchol4d634682013-09-26 13:21:06 -070078 if (ip.indexOf('.') != -1)
79 return IPv4Address.of(ip);
80 else if (ip.indexOf(':') != -1)
81 return IPv6Address.of(ip);
82 else
83 throw new IllegalArgumentException("IP Address not well formed: " + ip);
84 }
85
Sovietacedc6f91212014-06-13 18:36:28 -070086 /**
87 * Factory function for InetAddress values.
88 * @param address the InetAddress you wish to parse into an IPAddress object.
89 * @return the IPAddress object.
Sovietaced551254f2014-06-13 22:35:40 -070090 * @throws NullPointerException if address is null
Sovietacedc6f91212014-06-13 18:36:28 -070091 */
92 @Nonnull
Ronald Liffa80792014-07-04 17:50:49 -070093 public static IPAddress<?> of(@Nonnull InetAddress address) {
Sovietacedc6f91212014-06-13 18:36:28 -070094 Preconditions.checkNotNull(address, "address must not be null");
Sovietaced551254f2014-06-13 22:35:40 -070095 byte [] bytes = address.getAddress();
96 if(address instanceof Inet4Address)
97 return IPv4Address.of(bytes);
98 else if (address instanceof Inet6Address)
99 return IPv6Address.of(bytes);
100 else
101 return IPAddress.of(address.getHostAddress());
Sovietacedc6f91212014-06-13 18:36:28 -0700102 }
Ronald Liffa80792014-07-04 17:50:49 -0700103
104 @Deprecated
105 @Nonnull
106 public static IPAddress<?> fromInetAddress(@Nonnull InetAddress address) {
107 return of(address);
108 }
Yotam Harchol4d634682013-09-26 13:21:06 -0700109}