blob: 80ebc15b9a7576e0f463a4e845f88f4d82b9ee45 [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.DpidDeserializer;
4import net.onrc.onos.core.util.serializers.DpidSerializer;
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 a network switch DPID.
12 */
Ray Milkey269ffb92014-04-03 14:43:30 -070013@JsonDeserialize(using = DpidDeserializer.class)
14@JsonSerialize(using = DpidSerializer.class)
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080015public class Dpid {
Naoki Shiotadf051d42014-01-20 16:12:41 -080016 static public final long UNKNOWN = 0;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080017
Jonathan Hart3edb1752013-11-14 13:28:17 -080018 private long value;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080019
Jonathan Hart3edb1752013-11-14 13:28:17 -080020 /**
21 * Default constructor.
22 */
23 public Dpid() {
Ray Milkey269ffb92014-04-03 14:43:30 -070024 this.value = Dpid.UNKNOWN;
Jonathan Hart3edb1752013-11-14 13:28:17 -080025 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080026
Jonathan Hart3edb1752013-11-14 13:28:17 -080027 /**
28 * Constructor from a long value.
29 *
30 * @param value the value to use.
31 */
32 public Dpid(long value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070033 this.value = value;
Jonathan Hart3edb1752013-11-14 13:28:17 -080034 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080035
Jonathan Hart3edb1752013-11-14 13:28:17 -080036 /**
37 * Constructor from a string.
38 *
39 * @param value the value to use.
40 */
41 public Dpid(String value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070042 this.value = HexString.toLong(value);
Jonathan Hart3edb1752013-11-14 13:28:17 -080043 }
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080044
Jonathan Hart3edb1752013-11-14 13:28:17 -080045 /**
46 * Get the value of the DPID.
47 *
48 * @return the value of the DPID.
49 */
Ray Milkey269ffb92014-04-03 14:43:30 -070050 public long value() {
51 return value;
52 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080053
Jonathan Hart3edb1752013-11-14 13:28:17 -080054 /**
55 * Set the value of the DPID.
56 *
57 * @param value the value to set.
58 */
59 public void setValue(long value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070060 this.value = value;
Jonathan Hart3edb1752013-11-14 13:28:17 -080061 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080062
Jonathan Hart3edb1752013-11-14 13:28:17 -080063 /**
64 * Convert the DPID value to a ':' separated hexadecimal string.
65 *
66 * @return the DPID value as a ':' separated hexadecimal string.
67 */
68 @Override
69 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070070 return HexString.toHexString(this.value);
Jonathan Hart3edb1752013-11-14 13:28:17 -080071 }
Jonathan Hartf0a81792013-11-14 11:36:06 -080072
Jonathan Hart3edb1752013-11-14 13:28:17 -080073 @Override
74 public boolean equals(Object other) {
Ray Milkey269ffb92014-04-03 14:43:30 -070075 if (!(other instanceof Dpid)) {
76 return false;
77 }
Jonathan Hartf0a81792013-11-14 11:36:06 -080078
Ray Milkey269ffb92014-04-03 14:43:30 -070079 Dpid otherDpid = (Dpid) other;
Jonathan Hartf0a81792013-11-14 11:36:06 -080080
Ray Milkey269ffb92014-04-03 14:43:30 -070081 return value == otherDpid.value;
Jonathan Hart3edb1752013-11-14 13:28:17 -080082 }
Jonathan Hartf0a81792013-11-14 11:36:06 -080083
Jonathan Hart3edb1752013-11-14 13:28:17 -080084 @Override
85 public int hashCode() {
Ray Milkey269ffb92014-04-03 14:43:30 -070086 int hash = 17;
87 hash += 31 * hash + (int) (value ^ value >>> 32);
88 return hash;
Jonathan Hart3edb1752013-11-14 13:28:17 -080089 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080090}