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.IPv6Deserializer; |
| 4 | import net.onrc.onos.core.util.serializers.IPv6Serializer; |
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; |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame] | 8 | import org.projectfloodlight.openflow.util.HexString; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 9 | |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 10 | /** |
| 11 | * The class representing an IPv6 address. |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 12 | * This class is immutable. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 13 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 14 | @JsonDeserialize(using = IPv6Deserializer.class) |
| 15 | @JsonSerialize(using = IPv6Serializer.class) |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 16 | public final class IPv6 { |
| 17 | private final long valueHigh; // The higher (more significant) 64 bits |
| 18 | private final long valueLow; // The lower (less significant) 64 bits |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * Default constructor. |
| 22 | */ |
| 23 | public IPv6() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 24 | this.valueHigh = 0; |
| 25 | this.valueLow = 0; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | /** |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 29 | * Copy constructor. |
| 30 | * |
| 31 | * @param other the object to copy from. |
| 32 | */ |
| 33 | public IPv6(IPv6 other) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 34 | this.valueHigh = other.valueHigh; |
| 35 | this.valueLow = other.valueLow; |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 39 | * Constructor from integer values. |
| 40 | * |
| 41 | * @param valueHigh the higher (more significant) 64 bits of the address. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 42 | * @param valueLow the lower (less significant) 64 bits of the address. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 43 | */ |
| 44 | public IPv6(long valueHigh, long valueLow) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 45 | this.valueHigh = valueHigh; |
| 46 | this.valueLow = valueLow; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 50 | * Constructor from a string. |
| 51 | * |
| 52 | * @param value the value to use. |
| 53 | */ |
| 54 | public IPv6(String value) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 55 | // TODO: Implement it! |
| 56 | this.valueHigh = 0; |
| 57 | this.valueLow = 0; |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 61 | * Get the value of the higher (more significant) 64 bits of the address. |
| 62 | * |
| 63 | * @return the value of the higher (more significant) 64 bits of the |
| 64 | * address. |
| 65 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | public long valueHigh() { |
| 67 | return valueHigh; |
| 68 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Get the value of the lower (less significant) 64 bits of the address. |
| 72 | * |
| 73 | * @return the value of the lower (less significant) 64 bits of the |
| 74 | * address. |
| 75 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 76 | public long valueLow() { |
| 77 | return valueLow; |
| 78 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 79 | |
| 80 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 81 | * Convert the IPv6 value to a ':' separated string. |
| 82 | * |
| 83 | * @return the IPv6 value as a ':' separated string. |
| 84 | */ |
| 85 | @Override |
| 86 | public String toString() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 87 | return HexString.toHexString(this.valueHigh) + ":" + |
| 88 | HexString.toHexString(this.valueLow); |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 89 | } |
| 90 | } |