blob: dadbac2dd89bec366b415aa089f13de1b11bb8e2 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
Yotam Harchola289d552013-09-16 10:10:40 -07003public class IPv4WithMask extends Masked<IPv4Address> {
Yotam Harcholf3f11152013-09-05 16:47:16 -07004
5 private IPv4WithMask(int rawValue, int rawMask) {
Yotam Harchola289d552013-09-16 10:10:40 -07006 super(IPv4Address.of(rawValue), IPv4Address.of(rawMask));
Yotam Harcholf3f11152013-09-05 16:47:16 -07007 }
8
Yotam Harchola289d552013-09-16 10:10:40 -07009 private IPv4WithMask(IPv4Address value, IPv4Address mask) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070010 super(value, mask);
11 }
12
13 public static IPv4WithMask of(int rawValue, int rawMask) {
14 return new IPv4WithMask(rawValue, rawMask);
15 }
16
Yotam Harchola289d552013-09-16 10:10:40 -070017 public static IPv4WithMask of(IPv4Address value, IPv4Address mask) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070018 return new IPv4WithMask(value, mask);
19 }
20
21 @Override
22 public String toString() {
23 StringBuilder res = new StringBuilder();
Yotam Harchola289d552013-09-16 10:10:40 -070024 res.append(((IPv4Address)value).toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -070025
Yotam Harchola289d552013-09-16 10:10:40 -070026 int maskint = ((IPv4Address)mask).getInt();
Yotam Harcholf3f11152013-09-05 16:47:16 -070027 res.append('/');
28 if (Integer.bitCount((~maskint) + 1) == 1) {
29 // CIDR notation
30 res.append(Integer.bitCount(maskint));
31 } else {
32 // Full address mask
Yotam Harchola289d552013-09-16 10:10:40 -070033 res.append(((IPv4Address)mask).toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -070034 }
35
36 return res.toString();
37 }
38
39 public static IPv4WithMask of(final String string) {
40 int slashPos;
41 String ip = string;
42 int maskBits = 0;
Yotam Harchola289d552013-09-16 10:10:40 -070043 IPv4Address maskAddress = null;
Yotam Harcholf3f11152013-09-05 16:47:16 -070044
45 // Read mask suffix
46 if ((slashPos = string.indexOf('/')) != -1) {
47 ip = string.substring(0, slashPos);
48 try {
49 String suffix = string.substring(slashPos + 1);
50 if (suffix.length() == 0)
51 throw new IllegalArgumentException("IP Address not well formed: " + string);
52 if (suffix.indexOf('.') != -1) {
53 // Full mask
Yotam Harchola289d552013-09-16 10:10:40 -070054 maskAddress = IPv4Address.of(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -070055 } else {
56 // CIDR Suffix
57 maskBits = Integer.parseInt(suffix);
58 }
59 } catch (NumberFormatException e) {
60 throw new IllegalArgumentException("IP Address not well formed: " + string);
61 }
62 if (maskBits < 0 || maskBits > 32) {
63 throw new IllegalArgumentException("IP Address not well formed: " + string);
64 }
65 }
66
67 // Read IP
Yotam Harchola289d552013-09-16 10:10:40 -070068 IPv4Address ipv4 = IPv4Address.of(ip);
Yotam Harcholf3f11152013-09-05 16:47:16 -070069
70 if (maskAddress != null) {
71 // Full address mask
72 return IPv4WithMask.of(ipv4, maskAddress);
73 } else if (maskBits == 0) {
74 // No mask
Yotam Harchola289d552013-09-16 10:10:40 -070075 return IPv4WithMask.of(ipv4, IPv4Address.NO_MASK);
Yotam Harcholf3f11152013-09-05 16:47:16 -070076 } else {
77 // With mask
78 int mask = (-1) << (32 - maskBits);
Yotam Harchola289d552013-09-16 10:10:40 -070079 return IPv4WithMask.of(ipv4, IPv4Address.of(mask));
Yotam Harcholf3f11152013-09-05 16:47:16 -070080 }
81 }
82
83}