blob: 1ab404222199630f1453150d91cd2b42dd96458a [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 Harchola289d552013-09-16 10:10:40 -07006public class IPv6WithMask extends Masked<IPv6Address> {
Yotam Harcholf3f11152013-09-05 16:47:16 -07007
Yotam Harchola289d552013-09-16 10:10:40 -07008 private IPv6WithMask(IPv6Address value, IPv6Address mask) {
Yotam Harcholf3f11152013-09-05 16:47:16 -07009 super(value, mask);
10 }
11
Yotam Harchola289d552013-09-16 10:10:40 -070012 public static IPv6WithMask of(IPv6Address value, IPv6Address mask) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070013 return new IPv6WithMask(value, mask);
14 }
15
16 @Override
17 public String toString() {
18 StringBuilder res = new StringBuilder();
Yotam Harchola289d552013-09-16 10:10:40 -070019 res.append(((IPv6Address)value).toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -070020 res.append('/');
21
Yotam Harchola289d552013-09-16 10:10:40 -070022 BigInteger maskint = new BigInteger(((IPv6Address)mask).getBytes());
Yotam Harcholf3f11152013-09-05 16:47:16 -070023 if (maskint.not().add(BigInteger.ONE).bitCount() == 1) {
24 // CIDR notation
25 res.append(maskint.bitCount());
26 } else {
27 // Full address mask
Yotam Harchola289d552013-09-16 10:10:40 -070028 res.append(((IPv6Address)mask).toString());
Yotam Harcholf3f11152013-09-05 16:47:16 -070029 }
30
31 return res.toString();
32 }
33
34 public static IPv6WithMask of(final String string) {
35 int slashPos;
36 String ip = string;
37 int maskBits = 0;
Yotam Harchola289d552013-09-16 10:10:40 -070038 IPv6Address maskAddress = null;
Yotam Harcholf3f11152013-09-05 16:47:16 -070039
40 // Read mask suffix
41 if ((slashPos = string.indexOf('/')) != -1) {
42 ip = string.substring(0, slashPos);
43 try {
44 String suffix = string.substring(slashPos + 1);
45 if (suffix.length() == 0)
46 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
47 if (suffix.indexOf(':') != -1) {
48 // Full mask
Yotam Harchola289d552013-09-16 10:10:40 -070049 maskAddress = IPv6Address.of(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -070050 } else {
51 // CIDR Suffix
52 maskBits = Integer.parseInt(suffix);
53 }
54 } catch (NumberFormatException e) {
55 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
56 }
57 if (maskBits < 0 || maskBits > 128) {
58 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
59 }
60 }
61
62 // Read IP
Yotam Harchola289d552013-09-16 10:10:40 -070063 IPv6Address ipv6 = IPv6Address.of(ip);
Yotam Harcholf3f11152013-09-05 16:47:16 -070064
65 if (maskAddress != null) {
66 // Full address mask
67 return IPv6WithMask.of(ipv6, maskAddress);
68 } else if (maskBits == 0) {
69 // No mask
Yotam Harchola289d552013-09-16 10:10:40 -070070 return IPv6WithMask.of(ipv6, IPv6Address.NO_MASK);
Yotam Harcholf3f11152013-09-05 16:47:16 -070071 } else {
72 // With mask
73 BigInteger mask = BigInteger.ONE.negate().shiftLeft(128 - maskBits);
74 byte[] maskBytesTemp = mask.toByteArray();
75 byte[] maskBytes;
76 if (maskBytesTemp.length < 16) {
77 maskBytes = new byte[16];
78 System.arraycopy(maskBytesTemp, 0, maskBytes, 16 - maskBytesTemp.length, maskBytesTemp.length);
79 Arrays.fill(maskBytes, 0, 16 - maskBytesTemp.length, (byte)(0xFF));
80 } else if (maskBytesTemp.length > 16) {
81 maskBytes = new byte[16];
82 System.arraycopy(maskBytesTemp, 0, maskBytes, 0, maskBytes.length);
83 } else {
84 maskBytes = maskBytesTemp;
85 }
Yotam Harchola289d552013-09-16 10:10:40 -070086 return IPv6WithMask.of(ipv6, IPv6Address.of(maskBytes));
Yotam Harcholf3f11152013-09-05 16:47:16 -070087 }
88 }
89
90
91}