blob: dcaa0b6643443433b5c7316374b6835adee8f212 [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 Harchol9a617aa2013-09-16 14:10:17 -07006public class IPv6AddressWithMask extends Masked<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 Harchol9a617aa2013-09-16 14:10:17 -070013 public static IPv6AddressWithMask of(IPv6Address value, IPv6Address mask) {
14 return new IPv6AddressWithMask(value, mask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070015 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070016
Yotam Harcholf3f11152013-09-05 16:47:16 -070017 @Override
18 public String toString() {
19 StringBuilder res = new StringBuilder();
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070020 res.append(value.toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -070021 res.append('/');
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070022
23 BigInteger maskint = new BigInteger(mask.getBytes());
Yotam Harcholf3f11152013-09-05 16:47:16 -070024 if (maskint.not().add(BigInteger.ONE).bitCount() == 1) {
25 // CIDR notation
26 res.append(maskint.bitCount());
27 } else {
28 // Full address mask
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070029 res.append(mask.toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -070030 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070031
Yotam Harcholf3f11152013-09-05 16:47:16 -070032 return res.toString();
33 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070034
Yotam Harchol9a617aa2013-09-16 14:10:17 -070035 public static IPv6AddressWithMask of(final String string) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070036 int slashPos;
37 String ip = string;
38 int maskBits = 0;
Yotam Harchola289d552013-09-16 10:10:40 -070039 IPv6Address maskAddress = null;
Yotam Harcholf3f11152013-09-05 16:47:16 -070040
41 // Read mask suffix
42 if ((slashPos = string.indexOf('/')) != -1) {
43 ip = string.substring(0, slashPos);
44 try {
45 String suffix = string.substring(slashPos + 1);
46 if (suffix.length() == 0)
47 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
48 if (suffix.indexOf(':') != -1) {
49 // Full mask
Yotam Harchola289d552013-09-16 10:10:40 -070050 maskAddress = IPv6Address.of(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -070051 } else {
52 // CIDR Suffix
53 maskBits = Integer.parseInt(suffix);
54 }
55 } catch (NumberFormatException e) {
56 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
57 }
58 if (maskBits < 0 || maskBits > 128) {
59 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
60 }
61 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070062
Yotam Harcholf3f11152013-09-05 16:47:16 -070063 // Read IP
Yotam Harchola289d552013-09-16 10:10:40 -070064 IPv6Address ipv6 = IPv6Address.of(ip);
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070065
Yotam Harcholf3f11152013-09-05 16:47:16 -070066 if (maskAddress != null) {
67 // Full address mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070068 return IPv6AddressWithMask.of(ipv6, maskAddress);
Yotam Harcholf3f11152013-09-05 16:47:16 -070069 } else if (maskBits == 0) {
70 // No mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070071 return IPv6AddressWithMask.of(ipv6, IPv6Address.NO_MASK);
Yotam Harcholf3f11152013-09-05 16:47:16 -070072 } else {
73 // 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}