blob: 7259c7f7a04b1fbaa98d63c395bd6b970393037b [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) {
Gregor Maier1ff55972013-12-11 02:22:56 -080019 if (value == null) {
20 throw new NullPointerException("Value must not be null");
21 }
22 if (mask == null) {
23 throw new NullPointerException("Mask must not be null");
24 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070025 return new IPv6AddressWithMask(value, mask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070026 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070027
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070028
Yotam Harchol9a617aa2013-09-16 14:10:17 -070029 public static IPv6AddressWithMask of(final String string) {
Gregor Maier1ff55972013-12-11 02:22:56 -080030 if (string == null) {
31 throw new NullPointerException("String must not be null");
32 }
Yotam Harcholf3f11152013-09-05 16:47:16 -070033 int slashPos;
34 String ip = string;
Andreas Wundsame04c86f2013-11-05 17:13:18 -080035 int maskBits = 128;
Yotam Harchola289d552013-09-16 10:10:40 -070036 IPv6Address maskAddress = null;
Yotam Harcholf3f11152013-09-05 16:47:16 -070037
38 // Read mask suffix
39 if ((slashPos = string.indexOf('/')) != -1) {
40 ip = string.substring(0, slashPos);
41 try {
42 String suffix = string.substring(slashPos + 1);
43 if (suffix.length() == 0)
44 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
45 if (suffix.indexOf(':') != -1) {
46 // Full mask
Yotam Harchola289d552013-09-16 10:10:40 -070047 maskAddress = IPv6Address.of(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -070048 } else {
49 // CIDR Suffix
50 maskBits = Integer.parseInt(suffix);
51 }
52 } catch (NumberFormatException e) {
53 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
54 }
55 if (maskBits < 0 || maskBits > 128) {
56 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
57 }
58 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070059
Yotam Harcholf3f11152013-09-05 16:47:16 -070060 // Read IP
Yotam Harchola289d552013-09-16 10:10:40 -070061 IPv6Address ipv6 = IPv6Address.of(ip);
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -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 IPv6AddressWithMask.of(ipv6, maskAddress);
Andreas Wundsame04c86f2013-11-05 17:13:18 -080066 } else if (maskBits == 128) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070067 // No mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070068 return IPv6AddressWithMask.of(ipv6, IPv6Address.NO_MASK);
Andreas Wundsame04c86f2013-11-05 17:13:18 -080069 } else if (maskBits == 0) {
70 // Entirely masked out
71 return IPv6AddressWithMask.of(ipv6, IPv6Address.FULL_MASK);
72 }else {
Yotam Harcholf3f11152013-09-05 16:47:16 -070073 // With mask
74 BigInteger mask = BigInteger.ONE.negate().shiftLeft(128 - maskBits);
75 byte[] maskBytesTemp = mask.toByteArray();
76 byte[] maskBytes;
77 if (maskBytesTemp.length < 16) {
78 maskBytes = new byte[16];
79 System.arraycopy(maskBytesTemp, 0, maskBytes, 16 - maskBytesTemp.length, maskBytesTemp.length);
80 Arrays.fill(maskBytes, 0, 16 - maskBytesTemp.length, (byte)(0xFF));
81 } else if (maskBytesTemp.length > 16) {
82 maskBytes = new byte[16];
83 System.arraycopy(maskBytesTemp, 0, maskBytes, 0, maskBytes.length);
84 } else {
85 maskBytes = maskBytesTemp;
86 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070087 return IPv6AddressWithMask.of(ipv6, IPv6Address.of(maskBytes));
Yotam Harcholf3f11152013-09-05 16:47:16 -070088 }
89 }
90
91
92}