blob: ee34923202cdc8c809e0dfdadb9d4d5d838171c6 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
Sovietaced9dfc1ef2014-06-27 11:13:57 -07003import com.google.common.base.Preconditions;
4
Yotam Harchol4d634682013-09-26 13:21:06 -07005public class IPv6AddressWithMask extends IPAddressWithMask<IPv6Address> {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -07006 public final static IPv6AddressWithMask NONE = of(IPv6Address.NONE, IPv6Address.NONE);
Yotam Harcholf3f11152013-09-05 16:47:16 -07007
Yotam Harchol9a617aa2013-09-16 14:10:17 -07008 private IPv6AddressWithMask(IPv6Address value, IPv6Address mask) {
Yotam Harcholf3f11152013-09-05 16:47:16 -07009 super(value, mask);
10 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070011
Yotam Harchol4d634682013-09-26 13:21:06 -070012 @Override
Yotam Harcholeb023dc2013-09-26 15:45:44 -070013 public IPVersion getIpVersion() {
14 return IPVersion.IPv6;
Yotam Harchol4d634682013-09-26 13:21:06 -070015 }
16
Yotam Harchol9a617aa2013-09-16 14:10:17 -070017 public static IPv6AddressWithMask of(IPv6Address value, IPv6Address mask) {
Sovietaced9dfc1ef2014-06-27 11:13:57 -070018 Preconditions.checkNotNull(value, "value must not be null");
19 Preconditions.checkNotNull(mask, "mask must not be null");
Yotam Harchol9a617aa2013-09-16 14:10:17 -070020 return new IPv6AddressWithMask(value, mask);
Yotam Harcholf3f11152013-09-05 16:47:16 -070021 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070022
Yotam Harchol9a617aa2013-09-16 14:10:17 -070023 public static IPv6AddressWithMask of(final String string) {
Sovietaced9dfc1ef2014-06-27 11:13:57 -070024 Preconditions.checkNotNull(string, "string must not be null");
25
Yotam Harcholf3f11152013-09-05 16:47:16 -070026 int slashPos;
27 String ip = string;
Ronald Li25fb44d2014-07-03 18:43:20 -070028 int cidrMaskLength = 128;
Yotam Harchola289d552013-09-16 10:10:40 -070029 IPv6Address maskAddress = null;
Yotam Harcholf3f11152013-09-05 16:47:16 -070030
31 // Read mask suffix
32 if ((slashPos = string.indexOf('/')) != -1) {
33 ip = string.substring(0, slashPos);
34 try {
35 String suffix = string.substring(slashPos + 1);
36 if (suffix.length() == 0)
37 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
38 if (suffix.indexOf(':') != -1) {
39 // Full mask
Yotam Harchola289d552013-09-16 10:10:40 -070040 maskAddress = IPv6Address.of(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -070041 } else {
42 // CIDR Suffix
Ronald Li25fb44d2014-07-03 18:43:20 -070043 cidrMaskLength = Integer.parseInt(suffix);
Yotam Harcholf3f11152013-09-05 16:47:16 -070044 }
45 } catch (NumberFormatException e) {
46 throw new IllegalArgumentException("IPv6 Address not well formed: " + string);
47 }
Yotam Harcholf3f11152013-09-05 16:47:16 -070048 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070049
Yotam Harcholf3f11152013-09-05 16:47:16 -070050 // Read IP
Yotam Harchola289d552013-09-16 10:10:40 -070051 IPv6Address ipv6 = IPv6Address.of(ip);
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070052
Yotam Harcholf3f11152013-09-05 16:47:16 -070053 if (maskAddress != null) {
54 // Full address mask
Yotam Harchol9a617aa2013-09-16 14:10:17 -070055 return IPv6AddressWithMask.of(ipv6, maskAddress);
Ronald Li25fb44d2014-07-03 18:43:20 -070056 } else {
57 return IPv6AddressWithMask.of(ipv6,
58 IPv6Address.ofCidrMaskLength(cidrMaskLength));
Yotam Harcholf3f11152013-09-05 16:47:16 -070059 }
60 }
61
Sovietaced07cc8eb2014-06-24 11:55:26 -070062 @Override
63 public boolean contains(IPAddress<?> ip) {
Sovietaced9dfc1ef2014-06-27 11:13:57 -070064 Preconditions.checkNotNull(ip, "ip must not be null");
Yotam Harcholf3f11152013-09-05 16:47:16 -070065
Sovietaced77d99492014-06-24 12:58:47 -070066 if(ip.getIpVersion() == IPVersion.IPv6) {
67 IPv6Address ipv6 = (IPv6Address) ip;
68 return this.matches(ipv6);
69 }
70
71 return false;
72 }
Yotam Harcholf3f11152013-09-05 16:47:16 -070073}