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 | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 33 | if (other.address != null) |
| 34 | this.address = new IPv6(other.address); |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame^] | 35 | else |
| 36 | this.address = null; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 37 | this.prefixLen = other.prefixLen; |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 41 | * Constructor for a given address and prefix length. |
| 42 | * |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 43 | * @param address the address to use. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 44 | * @param prefixLen the prefix length to use. |
| 45 | */ |
| 46 | public IPv6Net(IPv6 address, short prefixLen) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 47 | this.address = address; |
| 48 | this.prefixLen = prefixLen; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 52 | * Constructor from a string. |
| 53 | * |
| 54 | * @param value the value to use. |
| 55 | */ |
| 56 | public IPv6Net(String value) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 57 | String[] splits = value.split("/"); |
| 58 | if (splits.length != 2) { |
| 59 | throw new IllegalArgumentException("Specified IPv6Net address must contain an IPv6 " + |
| 60 | "address and a prefix length separated by '/'"); |
| 61 | } |
| 62 | this.address = new IPv6(splits[0]); |
| 63 | this.prefixLen = Short.decode(splits[1]); |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 67 | * Get the address value of the IPv6Net address. |
| 68 | * |
| 69 | * @return the address value of the IPv6Net address. |
| 70 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 71 | public IPv6 address() { |
| 72 | return address; |
| 73 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Get the prefix length value of the IPv6Net address. |
| 77 | * |
| 78 | * @return the prefix length value of the IPv6Net address. |
| 79 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 80 | public short prefixLen() { |
| 81 | return prefixLen; |
| 82 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 85 | * Convert the IPv6Net value to an "address/prefixLen" string. |
| 86 | * |
| 87 | * @return the IPv6Net value as an "address/prefixLen" string. |
| 88 | */ |
| 89 | @Override |
| 90 | public String toString() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 91 | return this.address.toString() + "/" + this.prefixLen; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 92 | } |
| 93 | } |