blob: 2d08e1a356f0ef98ac56e41b5791f119428a1eb2 [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;
Andreas Wundsame04c86f2013-11-05 17:13:18 -080038 int maskBits = 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
53 maskBits = Integer.parseInt(suffix);
54 }
55 } catch (NumberFormatException e) {
56 throw new IllegalArgumentException("IP Address not well formed: " + string);
57 }
58 if (maskBits < 0 || maskBits > 32) {
59 throw new IllegalArgumentException("IP Address not well formed: " + string);
60 }
61 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070062
Yotam Harcholf3f11152013-09-05 16:47:16 -070063 // Read IP
Yotam Harchola289d552013-09-16 10:10:40 -070064 IPv4Address ipv4 = IPv4Address.of(ip);
Yotam Harchol9a617aa2013-09-16 14:10:17 -070065
Yotam Harcholf3f11152013-09-05 16:47:16 -070066 if (maskAddress != null) {
67 // Full address mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070068 return IPv4AddressWithMask.of(ipv4, maskAddress);
Andreas Wundsame04c86f2013-11-05 17:13:18 -080069 } else if (maskBits == 32) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070070 // No mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070071 return IPv4AddressWithMask.of(ipv4, IPv4Address.NO_MASK);
Andreas Wundsame04c86f2013-11-05 17:13:18 -080072 } else if (maskBits == 0) {
73 // No mask
74 return IPv4AddressWithMask.of(ipv4, IPv4Address.FULL_MASK);
Yotam Harcholf3f11152013-09-05 16:47:16 -070075 } else {
76 // With mask
77 int mask = (-1) << (32 - maskBits);
Yotam Harchol9a617aa2013-09-16 14:10:17 -070078 return IPv4AddressWithMask.of(ipv4, IPv4Address.of(mask));
Yotam Harcholf3f11152013-09-05 16:47:16 -070079 }
80 }
Sovietaced77d99492014-06-24 12:58:47 -070081
Sovietaced07cc8eb2014-06-24 11:55:26 -070082 @Override
83 public boolean contains(IPAddress<?> ip) {
Sovietaced9dfc1ef2014-06-27 11:13:57 -070084 Preconditions.checkNotNull(ip, "ip must not be null");
Sovietaced77d99492014-06-24 12:58:47 -070085
86 if(ip.getIpVersion() == IPVersion.IPv4) {
87 IPv4Address ipv4 = (IPv4Address) ip;
88 return this.matches(ipv4);
89 }
90
91 return false;
Sovietaced07cc8eb2014-06-24 11:55:26 -070092 }
Yotam Harcholf3f11152013-09-05 16:47:16 -070093}