blob: 346d49b2284c5e28436ab2246a0fa93642f9d2d7 [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.util;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08002
Jonathan Hart46cc07f2014-08-21 18:59:48 -07003import java.util.Objects;
4
Jonathan Hart23701d12014-04-03 10:45:48 -07005import net.onrc.onos.core.util.serializers.IPv6Deserializer;
6import net.onrc.onos.core.util.serializers.IPv6Serializer;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08007
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -08008import org.codehaus.jackson.map.annotate.JsonDeserialize;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08009import org.codehaus.jackson.map.annotate.JsonSerialize;
Jonathan Hartc78b8f62014-08-07 22:31:09 -070010import org.projectfloodlight.openflow.util.HexString;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -080011
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080012/**
13 * The class representing an IPv6 address.
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070014 * This class is immutable.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080015 */
Ray Milkey269ffb92014-04-03 14:43:30 -070016@JsonDeserialize(using = IPv6Deserializer.class)
17@JsonSerialize(using = IPv6Serializer.class)
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070018public 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 Radoslavov5363c2a2013-02-18 09:55:42 -080021
22 /**
23 * Default constructor.
24 */
25 public IPv6() {
Ray Milkey269ffb92014-04-03 14:43:30 -070026 this.valueHigh = 0;
27 this.valueLow = 0;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080028 }
29
30 /**
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070031 * Copy constructor.
32 *
33 * @param other the object to copy from.
34 */
35 public IPv6(IPv6 other) {
Ray Milkey269ffb92014-04-03 14:43:30 -070036 this.valueHigh = other.valueHigh;
37 this.valueLow = other.valueLow;
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070038 }
39
40 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080041 * Constructor from integer values.
42 *
43 * @param valueHigh the higher (more significant) 64 bits of the address.
Ray Milkey269ffb92014-04-03 14:43:30 -070044 * @param valueLow the lower (less significant) 64 bits of the address.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080045 */
46 public IPv6(long valueHigh, long valueLow) {
Ray Milkey269ffb92014-04-03 14:43:30 -070047 this.valueHigh = valueHigh;
48 this.valueLow = valueLow;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080049 }
50
51 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080052 * Constructor from a string.
53 *
54 * @param value the value to use.
55 */
56 public IPv6(String value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070057 // TODO: Implement it!
58 this.valueHigh = 0;
59 this.valueLow = 0;
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080060 }
61
62 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080063 * 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 Milkey269ffb92014-04-03 14:43:30 -070068 public long valueHigh() {
69 return valueHigh;
70 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080071
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 Milkey269ffb92014-04-03 14:43:30 -070078 public long valueLow() {
79 return valueLow;
80 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080081
82 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080083 * 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 Milkey269ffb92014-04-03 14:43:30 -070089 return HexString.toHexString(this.valueHigh) + ":" +
90 HexString.toHexString(this.valueLow);
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080091 }
Jonathan Hart46cc07f2014-08-21 18:59:48 -070092
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 Radoslavov5363c2a2013-02-18 09:55:42 -0800107}