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 | 46cc07f | 2014-08-21 18:59:48 -0700 | [diff] [blame] | 3 | import java.util.Objects; |
| 4 | |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 5 | import net.onrc.onos.core.util.serializers.IPv6Deserializer; |
| 6 | import net.onrc.onos.core.util.serializers.IPv6Serializer; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 7 | |
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; |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame] | 10 | import org.projectfloodlight.openflow.util.HexString; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 11 | |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 12 | /** |
| 13 | * The class representing an IPv6 address. |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 14 | * This class is immutable. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 15 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 16 | @JsonDeserialize(using = IPv6Deserializer.class) |
| 17 | @JsonSerialize(using = IPv6Serializer.class) |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 18 | public final class IPv6 { |
| 19 | private final long valueHigh; // The higher (more significant) 64 bits |
| 20 | private final long valueLow; // The lower (less significant) 64 bits |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * Default constructor. |
| 24 | */ |
| 25 | public IPv6() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 26 | this.valueHigh = 0; |
| 27 | this.valueLow = 0; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | /** |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 31 | * Copy constructor. |
| 32 | * |
| 33 | * @param other the object to copy from. |
| 34 | */ |
| 35 | public IPv6(IPv6 other) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 36 | this.valueHigh = other.valueHigh; |
| 37 | this.valueLow = other.valueLow; |
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 from integer values. |
| 42 | * |
| 43 | * @param valueHigh the higher (more significant) 64 bits of the address. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 44 | * @param valueLow the lower (less significant) 64 bits of the address. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 45 | */ |
| 46 | public IPv6(long valueHigh, long valueLow) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 47 | this.valueHigh = valueHigh; |
| 48 | this.valueLow = valueLow; |
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 IPv6(String value) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 57 | // TODO: Implement it! |
| 58 | this.valueHigh = 0; |
| 59 | this.valueLow = 0; |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 63 | * Get the value of the higher (more significant) 64 bits of the address. |
| 64 | * |
| 65 | * @return the value of the higher (more significant) 64 bits of the |
| 66 | * address. |
| 67 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 68 | public long valueHigh() { |
| 69 | return valueHigh; |
| 70 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Get the value of the lower (less significant) 64 bits of the address. |
| 74 | * |
| 75 | * @return the value of the lower (less significant) 64 bits of the |
| 76 | * address. |
| 77 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 78 | public long valueLow() { |
| 79 | return valueLow; |
| 80 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 81 | |
| 82 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 83 | * Convert the IPv6 value to a ':' separated string. |
| 84 | * |
| 85 | * @return the IPv6 value as a ':' separated string. |
| 86 | */ |
| 87 | @Override |
| 88 | public String toString() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 89 | return HexString.toHexString(this.valueHigh) + ":" + |
| 90 | HexString.toHexString(this.valueLow); |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 91 | } |
Jonathan Hart | 46cc07f | 2014-08-21 18:59:48 -0700 | [diff] [blame] | 92 | |
| 93 | @Override |
| 94 | public boolean equals(Object o) { |
| 95 | if (!(o instanceof IPv6)) { |
| 96 | return false; |
| 97 | } |
| 98 | IPv6 other = (IPv6) o; |
| 99 | return this.valueHigh == other.valueHigh |
| 100 | && this.valueLow == other.valueLow; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public int hashCode() { |
| 105 | return Objects.hash(valueHigh, valueLow); |
| 106 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 107 | } |