blob: b7f00379f073f54c57a63e80c662f1d7b0912272 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
Sovietaced9dfc1ef2014-06-27 11:13:57 -07003import com.google.common.base.Preconditions;
4
Yotam Harchol4d634682013-09-26 13:21:06 -07005
6public class IPv4AddressWithMask extends IPAddressWithMask<IPv4Address> {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -07007 public final static IPv4AddressWithMask NONE = of(IPv4Address.NONE, IPv4Address.NONE);
Yotam Harcholf3f11152013-09-05 16:47:16 -07008
Yotam Harchol9a617aa2013-09-16 14:10:17 -07009 private IPv4AddressWithMask(int rawValue, int rawMask) {
Yotam Harchola289d552013-09-16 10:10:40 -070010 super(IPv4Address.of(rawValue), IPv4Address.of(rawMask));
Yotam Harcholf3f11152013-09-05 16:47:16 -070011 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070012
13 private IPv4AddressWithMask(IPv4Address value, IPv4Address mask) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070014 super(value, mask);
15 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070016
Yotam Harchol4d634682013-09-26 13:21:06 -070017 @Override
Yotam Harcholeb023dc2013-09-26 15:45:44 -070018 public IPVersion getIpVersion() {
19 return IPVersion.IPv4;
Yotam Harchol4d634682013-09-26 13:21:06 -070020 }
21
Yotam Harchol9a617aa2013-09-16 14:10:17 -070022 public static IPv4AddressWithMask of(int rawValue, int rawMask) {
23 return new IPv4AddressWithMask(rawValue, rawMask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070024 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070025
26 public static IPv4AddressWithMask of(IPv4Address value, IPv4Address mask) {
Sovietaced9dfc1ef2014-06-27 11:13:57 -070027 Preconditions.checkNotNull(value, "value must not be null");
28 Preconditions.checkNotNull(mask, "mask must not be null");
29
Yotam Harchol9a617aa2013-09-16 14:10:17 -070030 return new IPv4AddressWithMask(value, mask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070031 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070032
Yotam Harchol9a617aa2013-09-16 14:10:17 -070033 public static IPv4AddressWithMask of(final String string) {
Sovietaced9dfc1ef2014-06-27 11:13:57 -070034 Preconditions.checkNotNull(string, "string must not be null");
35
Yotam Harcholf3f11152013-09-05 16:47:16 -070036 int slashPos;
37 String ip = string;
Ronald Lia780c372014-07-03 17:34:50 -070038 int cidrMaskLength = 32;
Yotam Harchola289d552013-09-16 10:10:40 -070039 IPv4Address maskAddress = null;
Yotam Harcholf3f11152013-09-05 16:47:16 -070040
41 // Read mask suffix
42 if ((slashPos = string.indexOf('/')) != -1) {
43 ip = string.substring(0, slashPos);
44 try {
45 String suffix = string.substring(slashPos + 1);
46 if (suffix.length() == 0)
47 throw new IllegalArgumentException("IP Address not well formed: " + string);
48 if (suffix.indexOf('.') != -1) {
49 // Full mask
Yotam Harchola289d552013-09-16 10:10:40 -070050 maskAddress = IPv4Address.of(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -070051 } else {
52 // CIDR Suffix
Ronald Lia780c372014-07-03 17:34:50 -070053 cidrMaskLength = Integer.parseInt(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -070054 }
55 } catch (NumberFormatException e) {
56 throw new IllegalArgumentException("IP Address not well formed: " + string);
57 }
Yotam Harcholf3f11152013-09-05 16:47:16 -070058 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070059
Yotam Harcholf3f11152013-09-05 16:47:16 -070060 // Read IP
Yotam Harchola289d552013-09-16 10:10:40 -070061 IPv4Address ipv4 = IPv4Address.of(ip);
Yotam Harchol9a617aa2013-09-16 14:10:17 -070062
Yotam Harcholf3f11152013-09-05 16:47:16 -070063 if (maskAddress != null) {
64 // Full address mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070065 return IPv4AddressWithMask.of(ipv4, maskAddress);
Yotam Harcholf3f11152013-09-05 16:47:16 -070066 } else {
Ronald Lia780c372014-07-03 17:34:50 -070067 return IPv4AddressWithMask.of(
68 ipv4, IPv4Address.ofCidrMaskLength(cidrMaskLength));
Yotam Harcholf3f11152013-09-05 16:47:16 -070069 }
70 }
Sovietaced77d99492014-06-24 12:58:47 -070071
Sovietaced07cc8eb2014-06-24 11:55:26 -070072 @Override
73 public boolean contains(IPAddress<?> ip) {
Sovietaced9dfc1ef2014-06-27 11:13:57 -070074 Preconditions.checkNotNull(ip, "ip must not be null");
Sovietaced77d99492014-06-24 12:58:47 -070075
76 if(ip.getIpVersion() == IPVersion.IPv4) {
77 IPv4Address ipv4 = (IPv4Address) ip;
78 return this.matches(ipv4);
79 }
80
81 return false;
Sovietaced07cc8eb2014-06-24 11:55:26 -070082 }
Yotam Harcholf3f11152013-09-05 16:47:16 -070083}