Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 3 | import com.google.common.base.Preconditions; |
| 4 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 5 | public class IPv6AddressWithMask extends IPAddressWithMask<IPv6Address> { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 6 | public final static IPv6AddressWithMask NONE = of(IPv6Address.NONE, IPv6Address.NONE); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 7 | |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 8 | private IPv6AddressWithMask(IPv6Address value, IPv6Address mask) { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 9 | super(value, mask); |
| 10 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 11 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 12 | @Override |
Yotam Harchol | eb023dc | 2013-09-26 15:45:44 -0700 | [diff] [blame] | 13 | public IPVersion getIpVersion() { |
| 14 | return IPVersion.IPv6; |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 15 | } |
| 16 | |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 17 | public static IPv6AddressWithMask of(IPv6Address value, IPv6Address mask) { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 18 | Preconditions.checkNotNull(value, "value must not be null"); |
| 19 | Preconditions.checkNotNull(mask, "mask must not be null"); |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 20 | return new IPv6AddressWithMask(value, mask); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 21 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 22 | |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 23 | public static IPv6AddressWithMask of(final String string) { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 24 | Preconditions.checkNotNull(string, "string must not be null"); |
| 25 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 26 | int slashPos; |
| 27 | String ip = string; |
Ronald Li | 25fb44d | 2014-07-03 18:43:20 -0700 | [diff] [blame] | 28 | int cidrMaskLength = 128; |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 29 | IPv6Address maskAddress = null; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 30 | |
| 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 Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 40 | maskAddress = IPv6Address.of(suffix); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 41 | } else { |
| 42 | // CIDR Suffix |
Ronald Li | 25fb44d | 2014-07-03 18:43:20 -0700 | [diff] [blame] | 43 | cidrMaskLength = Integer.parseInt(suffix); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 44 | } |
| 45 | } catch (NumberFormatException e) { |
| 46 | throw new IllegalArgumentException("IPv6 Address not well formed: " + string); |
| 47 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 48 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 49 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 50 | // Read IP |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 51 | IPv6Address ipv6 = IPv6Address.of(ip); |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 52 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 53 | if (maskAddress != null) { |
| 54 | // Full address mask |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 55 | return IPv6AddressWithMask.of(ipv6, maskAddress); |
Ronald Li | 25fb44d | 2014-07-03 18:43:20 -0700 | [diff] [blame] | 56 | } else { |
| 57 | return IPv6AddressWithMask.of(ipv6, |
| 58 | IPv6Address.ofCidrMaskLength(cidrMaskLength)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Sovietaced | 07cc8eb | 2014-06-24 11:55:26 -0700 | [diff] [blame] | 62 | @Override |
| 63 | public boolean contains(IPAddress<?> ip) { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 64 | Preconditions.checkNotNull(ip, "ip must not be null"); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 65 | |
Sovietaced | 77d9949 | 2014-06-24 12:58:47 -0700 | [diff] [blame] | 66 | if(ip.getIpVersion() == IPVersion.IPv6) { |
| 67 | IPv6Address ipv6 = (IPv6Address) ip; |
| 68 | return this.matches(ipv6); |
| 69 | } |
| 70 | |
| 71 | return false; |
| 72 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 73 | } |