blob: bd91daae8e4b5fcf9c84612b0e0f6bb6b03716bd [file] [log] [blame]
HIGUCHI Yuta356086e2013-06-12 15:21:19 -07001package net.onrc.onos.ofcontroller.util;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08002
HIGUCHI Yutaf086d8a2013-06-12 15:26:35 -07003import net.onrc.onos.ofcontroller.util.serializers.DpidDeserializer;
4import net.onrc.onos.ofcontroller.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 */
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080013@JsonDeserialize(using=DpidDeserializer.class)
Pavlin Radoslavovad008e02013-02-21 18:42:42 -080014@JsonSerialize(using=DpidSerializer.class)
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080015public class Dpid {
Jonathan Hart3edb1752013-11-14 13:28:17 -080016 static public 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() {
24 this.value = Dpid.UNKNOWN;
25 }
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) {
33 this.value = value;
34 }
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) {
42 this.value = HexString.toLong(value);
43 }
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 */
50 public long value() { return value; }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080051
Jonathan Hart3edb1752013-11-14 13:28:17 -080052 /**
53 * Set the value of the DPID.
54 *
55 * @param value the value to set.
56 */
57 public void setValue(long value) {
58 this.value = value;
59 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080060
Jonathan Hart3edb1752013-11-14 13:28:17 -080061 /**
62 * Convert the DPID value to a ':' separated hexadecimal string.
63 *
64 * @return the DPID value as a ':' separated hexadecimal string.
65 */
66 @Override
67 public String toString() {
68 return HexString.toHexString(this.value);
69 }
Jonathan Hartf0a81792013-11-14 11:36:06 -080070
Jonathan Hart3edb1752013-11-14 13:28:17 -080071 @Override
72 public boolean equals(Object other) {
73 if (!(other instanceof Dpid)) {
74 return false;
75 }
Jonathan Hartf0a81792013-11-14 11:36:06 -080076
Jonathan Hart3edb1752013-11-14 13:28:17 -080077 Dpid otherDpid = (Dpid) other;
Jonathan Hartf0a81792013-11-14 11:36:06 -080078
Jonathan Hart3edb1752013-11-14 13:28:17 -080079 return value == otherDpid.value;
80 }
Jonathan Hartf0a81792013-11-14 11:36:06 -080081
Jonathan Hart3edb1752013-11-14 13:28:17 -080082 @Override
83 public int hashCode() {
84 int hash = 17;
85 hash += 31 * hash + (int)(value ^ value >>> 32);
86 return hash;
87 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080088}