Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.util; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 2 | |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 3 | import net.onrc.onos.core.util.serializers.IPv6NetDeserializer; |
| 4 | import net.onrc.onos.core.util.serializers.IPv6NetSerializer; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 5 | |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 6 | import org.codehaus.jackson.map.annotate.JsonDeserialize; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 7 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 8 | |
| 9 | /** |
| 10 | * The class representing an IPv6 network address. |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 11 | * This class is immutable. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 12 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 13 | @JsonDeserialize(using = IPv6NetDeserializer.class) |
| 14 | @JsonSerialize(using = IPv6NetSerializer.class) |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 15 | public final class IPv6Net { |
| 16 | private final IPv6 address; // The IPv6 address |
| 17 | private final short prefixLen; // The prefix length |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 18 | |
| 19 | /** |
| 20 | * Default constructor. |
| 21 | */ |
| 22 | public IPv6Net() { |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 23 | this.address = null; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 24 | this.prefixLen = 0; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | /** |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 28 | * Copy constructor. |
| 29 | * |
| 30 | * @param other the object to copy from. |
| 31 | */ |
| 32 | public IPv6Net(IPv6Net other) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 33 | if (other.address != null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 34 | this.address = new IPv6(other.address); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 35 | } else { |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 36 | this.address = null; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 37 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 38 | this.prefixLen = other.prefixLen; |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 42 | * Constructor for a given address and prefix length. |
| 43 | * |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 44 | * @param address the address to use. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 45 | * @param prefixLen the prefix length to use. |
| 46 | */ |
| 47 | public IPv6Net(IPv6 address, short prefixLen) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 48 | this.address = address; |
| 49 | this.prefixLen = prefixLen; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /** |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 53 | * Constructor from a string. |
| 54 | * |
| 55 | * @param value the value to use. |
| 56 | */ |
| 57 | public IPv6Net(String value) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 58 | String[] splits = value.split("/"); |
| 59 | if (splits.length != 2) { |
| 60 | throw new IllegalArgumentException("Specified IPv6Net address must contain an IPv6 " + |
| 61 | "address and a prefix length separated by '/'"); |
| 62 | } |
| 63 | this.address = new IPv6(splits[0]); |
| 64 | this.prefixLen = Short.decode(splits[1]); |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 68 | * Get the address value of the IPv6Net address. |
| 69 | * |
| 70 | * @return the address value of the IPv6Net address. |
| 71 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 72 | public IPv6 address() { |
| 73 | return address; |
| 74 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Get the prefix length value of the IPv6Net address. |
| 78 | * |
| 79 | * @return the prefix length value of the IPv6Net address. |
| 80 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 81 | public short prefixLen() { |
| 82 | return prefixLen; |
| 83 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 84 | |
| 85 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 86 | * Convert the IPv6Net value to an "address/prefixLen" string. |
| 87 | * |
| 88 | * @return the IPv6Net value as an "address/prefixLen" string. |
| 89 | */ |
| 90 | @Override |
| 91 | public String toString() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 92 | return this.address.toString() + "/" + this.prefixLen; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 93 | } |
| 94 | } |