blob: f918d1c6c76a790bc3f0d0609a4373836643e412 [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 -07006import org.projectfloodlight.openflow.types.IPAddress.IpVersion;
7
8public class IPv6AddressWithMask extends IPAddressWithMask<IPv6Address> {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -07009 public final static IPv6AddressWithMask NONE = of(IPv6Address.NONE, IPv6Address.NONE);
Yotam Harcholf3f11152013-09-05 16:47:16 -070010
Yotam Harchol9a617aa2013-09-16 14:10:17 -070011 private IPv6AddressWithMask(IPv6Address value, IPv6Address mask) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070012 super(value, mask);
13 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070014
Yotam Harchol4d634682013-09-26 13:21:06 -070015 @Override
16 public IpVersion getIpVersion() {
17 return IpVersion.IPv6;
18 }
19
Yotam Harchol9a617aa2013-09-16 14:10:17 -070020 public static IPv6AddressWithMask of(IPv6Address value, IPv6Address mask) {
21 return new IPv6AddressWithMask(value, mask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070022 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070023
Yotam Harcholf3f11152013-09-05 16:47:16 -070024 @Override
25 public String toString() {
26 StringBuilder res = new StringBuilder();
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070027 res.append(value.toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -070028 res.append('/');
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070029
30 BigInteger maskint = new BigInteger(mask.getBytes());
Yotam Harcholf3f11152013-09-05 16:47:16 -070031 if (maskint.not().add(BigInteger.ONE).bitCount() == 1) {
32 // CIDR notation
33 res.append(maskint.bitCount());
34 } else {
35 // Full address mask
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070036 res.append(mask.toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -070037 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070038
Yotam Harcholf3f11152013-09-05 16:47:16 -070039 return res.toString();
40 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070041
Yotam Harchol9a617aa2013-09-16 14:10:17 -070042 public static IPv6AddressWithMask of(final String string) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070043 int slashPos;
44 String ip = string;
45 int maskBits = 0;
Yotam Harchola289d552013-09-16 10:10:40 -070046 IPv6Address maskAddress = null;
Yotam Harcholf3f11152013-09-05 16:47:16 -070047
48 // Read mask suffix
49 if ((slashPos = string.indexOf('/')) != -1) {
50 ip = string.substring(0, slashPos);
51 try {
52 String suffix = string.substring(slashPos + 1);
53 if (suffix.length() == 0)
54 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
55 if (suffix.indexOf(':') != -1) {
56 // Full mask
Yotam Harchola289d552013-09-16 10:10:40 -070057 maskAddress = IPv6Address.of(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -070058 } else {
59 // CIDR Suffix
60 maskBits = Integer.parseInt(suffix);
61 }
62 } catch (NumberFormatException e) {
63 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
64 }
65 if (maskBits < 0 || maskBits > 128) {
66 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
67 }
68 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070069
Yotam Harcholf3f11152013-09-05 16:47:16 -070070 // Read IP
Yotam Harchola289d552013-09-16 10:10:40 -070071 IPv6Address ipv6 = IPv6Address.of(ip);
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070072
Yotam Harcholf3f11152013-09-05 16:47:16 -070073 if (maskAddress != null) {
74 // Full address mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070075 return IPv6AddressWithMask.of(ipv6, maskAddress);
Yotam Harcholf3f11152013-09-05 16:47:16 -070076 } else if (maskBits == 0) {
77 // No mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070078 return IPv6AddressWithMask.of(ipv6, IPv6Address.NO_MASK);
Yotam Harcholf3f11152013-09-05 16:47:16 -070079 } else {
80 // With mask
81 BigInteger mask = BigInteger.ONE.negate().shiftLeft(128 - maskBits);
82 byte[] maskBytesTemp = mask.toByteArray();
83 byte[] maskBytes;
84 if (maskBytesTemp.length < 16) {
85 maskBytes = new byte[16];
86 System.arraycopy(maskBytesTemp, 0, maskBytes, 16 - maskBytesTemp.length, maskBytesTemp.length);
87 Arrays.fill(maskBytes, 0, 16 - maskBytesTemp.length, (byte)(0xFF));
88 } else if (maskBytesTemp.length > 16) {
89 maskBytes = new byte[16];
90 System.arraycopy(maskBytesTemp, 0, maskBytes, 0, maskBytes.length);
91 } else {
92 maskBytes = maskBytesTemp;
93 }
Yotam Harchol9a617aa2013-09-16 14:10:17 -070094 return IPv6AddressWithMask.of(ipv6, IPv6Address.of(maskBytes));
Yotam Harcholf3f11152013-09-05 16:47:16 -070095 }
96 }
97
98
99}