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