blob: 6faf0b813091d353f8a12eb1272c7642475f414b [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
3import java.math.BigInteger;
4import java.util.Arrays;
5
Yotam Harchol4d634682013-09-26 13:21:06 -07006public class IPv6AddressWithMask extends IPAddressWithMask<IPv6Address> {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -07007 public final static IPv6AddressWithMask NONE = of(IPv6Address.NONE, IPv6Address.NONE);
Yotam Harcholf3f11152013-09-05 16:47:16 -07008
Yotam Harchol9a617aa2013-09-16 14:10:17 -07009 private IPv6AddressWithMask(IPv6Address value, IPv6Address mask) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070010 super(value, mask);
11 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070012
Yotam Harchol4d634682013-09-26 13:21:06 -070013 @Override
Yotam Harcholeb023dc2013-09-26 15:45:44 -070014 public IPVersion getIpVersion() {
15 return IPVersion.IPv6;
Yotam Harchol4d634682013-09-26 13:21:06 -070016 }
17
Yotam Harchol9a617aa2013-09-16 14:10:17 -070018 public static IPv6AddressWithMask of(IPv6Address value, IPv6Address mask) {
19 return new IPv6AddressWithMask(value, mask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070020 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070021
Yotam Harcholf3f11152013-09-05 16:47:16 -070022 @Override
23 public String toString() {
24 StringBuilder res = new StringBuilder();
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070025 res.append(value.toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -070026 res.append('/');
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070027
28 BigInteger maskint = new BigInteger(mask.getBytes());
Yotam Harcholf3f11152013-09-05 16:47:16 -070029 if (maskint.not().add(BigInteger.ONE).bitCount() == 1) {
30 // CIDR notation
31 res.append(maskint.bitCount());
32 } else {
33 // Full address mask
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070034 res.append(mask.toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -070035 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070036
Yotam Harcholf3f11152013-09-05 16:47:16 -070037 return res.toString();
38 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070039
Yotam Harchol9a617aa2013-09-16 14:10:17 -070040 public static IPv6AddressWithMask of(final String string) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070041 int slashPos;
42 String ip = string;
Andreas Wundsame04c86f2013-11-05 17:13:18 -080043 int maskBits = 128;
Yotam Harchola289d552013-09-16 10:10:40 -070044 IPv6Address maskAddress = null;
Yotam Harcholf3f11152013-09-05 16:47:16 -070045
46 // Read mask suffix
47 if ((slashPos = string.indexOf('/')) != -1) {
48 ip = string.substring(0, slashPos);
49 try {
50 String suffix = string.substring(slashPos + 1);
51 if (suffix.length() == 0)
52 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
53 if (suffix.indexOf(':') != -1) {
54 // Full mask
Yotam Harchola289d552013-09-16 10:10:40 -070055 maskAddress = IPv6Address.of(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -070056 } else {
57 // CIDR Suffix
58 maskBits = Integer.parseInt(suffix);
59 }
60 } catch (NumberFormatException e) {
61 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
62 }
63 if (maskBits < 0 || maskBits > 128) {
64 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
65 }
66 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070067
Yotam Harcholf3f11152013-09-05 16:47:16 -070068 // Read IP
Yotam Harchola289d552013-09-16 10:10:40 -070069 IPv6Address ipv6 = IPv6Address.of(ip);
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070070
Yotam Harcholf3f11152013-09-05 16:47:16 -070071 if (maskAddress != null) {
72 // Full address mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070073 return IPv6AddressWithMask.of(ipv6, maskAddress);
Andreas Wundsame04c86f2013-11-05 17:13:18 -080074 } else if (maskBits == 128) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070075 // No mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070076 return IPv6AddressWithMask.of(ipv6, IPv6Address.NO_MASK);
Andreas Wundsame04c86f2013-11-05 17:13:18 -080077 } else if (maskBits == 0) {
78 // Entirely masked out
79 return IPv6AddressWithMask.of(ipv6, IPv6Address.FULL_MASK);
80 }else {
Yotam Harcholf3f11152013-09-05 16:47:16 -070081 // With mask
82 BigInteger mask = BigInteger.ONE.negate().shiftLeft(128 - maskBits);
83 byte[] maskBytesTemp = mask.toByteArray();
84 byte[] maskBytes;
85 if (maskBytesTemp.length < 16) {
86 maskBytes = new byte[16];
87 System.arraycopy(maskBytesTemp, 0, maskBytes, 16 - maskBytesTemp.length, maskBytesTemp.length);
88 Arrays.fill(maskBytes, 0, 16 - maskBytesTemp.length, (byte)(0xFF));
89 } else if (maskBytesTemp.length > 16) {
90 maskBytes = new byte[16];
91 System.arraycopy(maskBytesTemp, 0, maskBytes, 0, maskBytes.length);
92 } else {
93 maskBytes = maskBytesTemp;
94 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070095 return IPv6AddressWithMask.of(ipv6, IPv6Address.of(maskBytes));
Yotam Harcholf3f11152013-09-05 16:47:16 -070096 }
97 }
98
99
100}