HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.util; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 2 | |
HIGUCHI Yuta | f086d8a | 2013-06-12 15:26:35 -0700 | [diff] [blame] | 3 | import net.onrc.onos.ofcontroller.util.serializers.IPv6NetDeserializer; |
| 4 | import net.onrc.onos.ofcontroller.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. |
| 11 | */ |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 12 | @JsonDeserialize(using=IPv6NetDeserializer.class) |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 13 | @JsonSerialize(using=IPv6NetSerializer.class) |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 14 | public class IPv6Net { |
| 15 | private IPv6 address; // The IPv6 address |
| 16 | private short prefixLen; // The prefix length |
| 17 | |
| 18 | /** |
| 19 | * Default constructor. |
| 20 | */ |
| 21 | public IPv6Net() { |
| 22 | this.prefixLen = 0; |
| 23 | } |
| 24 | |
| 25 | /** |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 26 | * Copy constructor. |
| 27 | * |
| 28 | * @param other the object to copy from. |
| 29 | */ |
| 30 | public IPv6Net(IPv6Net other) { |
| 31 | if (other.address != null) |
| 32 | this.address = new IPv6(other.address); |
| 33 | this.prefixLen = other.prefixLen; |
| 34 | } |
| 35 | |
| 36 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 37 | * Constructor for a given address and prefix length. |
| 38 | * |
| 39 | * @param address the address to use. |
| 40 | * @param prefixLen the prefix length to use. |
| 41 | */ |
| 42 | public IPv6Net(IPv6 address, short prefixLen) { |
| 43 | this.address = address; |
| 44 | this.prefixLen = prefixLen; |
| 45 | } |
| 46 | |
| 47 | /** |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 48 | * Constructor from a string. |
| 49 | * |
| 50 | * @param value the value to use. |
| 51 | */ |
| 52 | public IPv6Net(String value) { |
| 53 | String[] splits = value.split("/"); |
| 54 | if (splits.length != 2) { |
| 55 | throw new IllegalArgumentException("Specified IPv6Net address must contain an IPv6 " + |
| 56 | "address and a prefix length separated by '/'"); |
| 57 | } |
| 58 | this.address = new IPv6(splits[0]); |
| 59 | this.prefixLen = Short.decode(splits[1]); |
| 60 | } |
| 61 | |
| 62 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 63 | * Get the address value of the IPv6Net address. |
| 64 | * |
| 65 | * @return the address value of the IPv6Net address. |
| 66 | */ |
| 67 | public IPv6 address() { return address; } |
| 68 | |
| 69 | /** |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 70 | * Set the address value of the IPv6Net address. |
| 71 | * |
| 72 | * @param address the address to use. |
| 73 | */ |
| 74 | public void setAddress(IPv6 address) { |
| 75 | this.address = address; |
| 76 | } |
| 77 | |
| 78 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 79 | * Get the prefix length value of the IPv6Net address. |
| 80 | * |
| 81 | * @return the prefix length value of the IPv6Net address. |
| 82 | */ |
| 83 | public short prefixLen() { return prefixLen; } |
| 84 | |
| 85 | /** |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 86 | * Set the prefix length value of the IPv6Net address. |
| 87 | * |
| 88 | * @param prefixLen the prefix length to use. |
| 89 | */ |
| 90 | public void setPrefixLen(short prefixLen) { |
| 91 | this.prefixLen = prefixLen; |
| 92 | } |
| 93 | |
| 94 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 95 | * Set the value of the IPv6Net address. |
| 96 | * |
| 97 | * @param address the address to use. |
| 98 | * @param prefixLen the prefix length to use. |
| 99 | */ |
| 100 | public void setValue(IPv6 address, short prefixLen) { |
| 101 | this.address = address; |
| 102 | this.prefixLen = prefixLen; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Convert the IPv6Net value to an "address/prefixLen" string. |
| 107 | * |
| 108 | * @return the IPv6Net value as an "address/prefixLen" string. |
| 109 | */ |
| 110 | @Override |
| 111 | public String toString() { |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 112 | return this.address.toString() + "/" + this.prefixLen; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 113 | } |
| 114 | } |