blob: 1f23f2d80eafdbf96da007cdfd295f77bb39790a [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
Yotam Harchol4d634682013-09-26 13:21:06 -07003
4public class IPv4AddressWithMask extends IPAddressWithMask<IPv4Address> {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -07005 public final static IPv4AddressWithMask NONE = of(IPv4Address.NONE, IPv4Address.NONE);
Yotam Harcholf3f11152013-09-05 16:47:16 -07006
Yotam Harchol9a617aa2013-09-16 14:10:17 -07007 private IPv4AddressWithMask(int rawValue, int rawMask) {
Yotam Harchola289d552013-09-16 10:10:40 -07008 super(IPv4Address.of(rawValue), IPv4Address.of(rawMask));
Yotam Harcholf3f11152013-09-05 16:47:16 -07009 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070010
11 private IPv4AddressWithMask(IPv4Address value, IPv4Address mask) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070012 super(value, mask);
13 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070014
Yotam Harchol4d634682013-09-26 13:21:06 -070015 @Override
Yotam Harcholeb023dc2013-09-26 15:45:44 -070016 public IPVersion getIpVersion() {
17 return IPVersion.IPv4;
Yotam Harchol4d634682013-09-26 13:21:06 -070018 }
19
Yotam Harchol9a617aa2013-09-16 14:10:17 -070020 public static IPv4AddressWithMask of(int rawValue, int rawMask) {
21 return new IPv4AddressWithMask(rawValue, rawMask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070022 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070023
24 public static IPv4AddressWithMask of(IPv4Address value, IPv4Address mask) {
25 return new IPv4AddressWithMask(value, mask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070026 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070027
Yotam Harchol9a617aa2013-09-16 14:10:17 -070028 public static IPv4AddressWithMask of(final String string) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070029 int slashPos;
30 String ip = string;
Andreas Wundsame04c86f2013-11-05 17:13:18 -080031 int maskBits = 32;
Yotam Harchola289d552013-09-16 10:10:40 -070032 IPv4Address maskAddress = null;
Yotam Harcholf3f11152013-09-05 16:47:16 -070033
34 // Read mask suffix
35 if ((slashPos = string.indexOf('/')) != -1) {
36 ip = string.substring(0, slashPos);
37 try {
38 String suffix = string.substring(slashPos + 1);
39 if (suffix.length() == 0)
40 throw new IllegalArgumentException("IP Address not well formed: " + string);
41 if (suffix.indexOf('.') != -1) {
42 // Full mask
Yotam Harchola289d552013-09-16 10:10:40 -070043 maskAddress = IPv4Address.of(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -070044 } else {
45 // CIDR Suffix
46 maskBits = Integer.parseInt(suffix);
47 }
48 } catch (NumberFormatException e) {
49 throw new IllegalArgumentException("IP Address not well formed: " + string);
50 }
51 if (maskBits < 0 || maskBits > 32) {
52 throw new IllegalArgumentException("IP Address not well formed: " + string);
53 }
54 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070055
Yotam Harcholf3f11152013-09-05 16:47:16 -070056 // Read IP
Yotam Harchola289d552013-09-16 10:10:40 -070057 IPv4Address ipv4 = IPv4Address.of(ip);
Yotam Harchol9a617aa2013-09-16 14:10:17 -070058
Yotam Harcholf3f11152013-09-05 16:47:16 -070059 if (maskAddress != null) {
60 // Full address mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070061 return IPv4AddressWithMask.of(ipv4, maskAddress);
Andreas Wundsame04c86f2013-11-05 17:13:18 -080062 } else if (maskBits == 32) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070063 // No mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070064 return IPv4AddressWithMask.of(ipv4, IPv4Address.NO_MASK);
Andreas Wundsame04c86f2013-11-05 17:13:18 -080065 } else if (maskBits == 0) {
66 // No mask
67 return IPv4AddressWithMask.of(ipv4, IPv4Address.FULL_MASK);
Yotam Harcholf3f11152013-09-05 16:47:16 -070068 } else {
69 // With mask
70 int mask = (-1) << (32 - maskBits);
Yotam Harchol9a617aa2013-09-16 14:10:17 -070071 return IPv4AddressWithMask.of(ipv4, IPv4Address.of(mask));
Yotam Harcholf3f11152013-09-05 16:47:16 -070072 }
73 }
74
75}