blob: 986fd11d7a17a90dc510d6f29ff3f032a08eb789 [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 Hart23701d12014-04-03 10:45:48 -07003import net.onrc.onos.core.util.serializers.IPv6Deserializer;
4import net.onrc.onos.core.util.serializers.IPv6Serializer;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08005
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -08006import org.codehaus.jackson.map.annotate.JsonDeserialize;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08007import org.codehaus.jackson.map.annotate.JsonSerialize;
HIGUCHI Yuta858c1ea2013-06-14 13:10:06 -07008import org.openflow.util.HexString;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08009
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080010/**
11 * The class representing an IPv6 address.
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070012 * This class is immutable.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080013 */
Ray Milkey269ffb92014-04-03 14:43:30 -070014@JsonDeserialize(using = IPv6Deserializer.class)
15@JsonSerialize(using = IPv6Serializer.class)
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070016public 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 Radoslavov5363c2a2013-02-18 09:55:42 -080019
20 /**
21 * Default constructor.
22 */
23 public IPv6() {
Ray Milkey269ffb92014-04-03 14:43:30 -070024 this.valueHigh = 0;
25 this.valueLow = 0;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080026 }
27
28 /**
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070029 * Copy constructor.
30 *
31 * @param other the object to copy from.
32 */
33 public IPv6(IPv6 other) {
Ray Milkey269ffb92014-04-03 14:43:30 -070034 this.valueHigh = other.valueHigh;
35 this.valueLow = other.valueLow;
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070036 }
37
38 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080039 * Constructor from integer values.
40 *
41 * @param valueHigh the higher (more significant) 64 bits of the address.
Ray Milkey269ffb92014-04-03 14:43:30 -070042 * @param valueLow the lower (less significant) 64 bits of the address.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080043 */
44 public IPv6(long valueHigh, long valueLow) {
Ray Milkey269ffb92014-04-03 14:43:30 -070045 this.valueHigh = valueHigh;
46 this.valueLow = valueLow;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080047 }
48
49 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080050 * Constructor from a string.
51 *
52 * @param value the value to use.
53 */
54 public IPv6(String value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070055 // TODO: Implement it!
56 this.valueHigh = 0;
57 this.valueLow = 0;
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080058 }
59
60 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080061 * 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 Milkey269ffb92014-04-03 14:43:30 -070066 public long valueHigh() {
67 return valueHigh;
68 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080069
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 Milkey269ffb92014-04-03 14:43:30 -070076 public long valueLow() {
77 return valueLow;
78 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080079
80 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080081 * 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 Milkey269ffb92014-04-03 14:43:30 -070087 return HexString.toHexString(this.valueHigh) + ":" +
88 HexString.toHexString(this.valueLow);
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080089 }
90}