HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.util; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 2 | |
HIGUCHI Yuta | f086d8a | 2013-06-12 15:26:35 -0700 | [diff] [blame] | 3 | import net.onrc.onos.ofcontroller.util.serializers.DpidDeserializer; |
| 4 | import net.onrc.onos.ofcontroller.util.serializers.DpidSerializer; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 5 | |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 6 | import org.codehaus.jackson.map.annotate.JsonDeserialize; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 7 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
HIGUCHI Yuta | 858c1ea | 2013-06-14 13:10:06 -0700 | [diff] [blame] | 8 | import org.openflow.util.HexString; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 9 | |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 10 | /** |
| 11 | * The class representing a network switch DPID. |
| 12 | */ |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 13 | @JsonDeserialize(using=DpidDeserializer.class) |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 14 | @JsonSerialize(using=DpidSerializer.class) |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 15 | public class Dpid { |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 16 | static public long UNKNOWN = 0; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 17 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 18 | private long value; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 19 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 20 | /** |
| 21 | * Default constructor. |
| 22 | */ |
| 23 | public Dpid() { |
| 24 | this.value = Dpid.UNKNOWN; |
| 25 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 26 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 27 | /** |
| 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 Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 35 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 36 | /** |
| 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 Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 44 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 45 | /** |
| 46 | * Get the value of the DPID. |
| 47 | * |
| 48 | * @return the value of the DPID. |
| 49 | */ |
| 50 | public long value() { return value; } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 51 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 52 | /** |
| 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 Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 60 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 61 | /** |
| 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 Hart | f0a8179 | 2013-11-14 11:36:06 -0800 | [diff] [blame] | 70 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 71 | @Override |
| 72 | public boolean equals(Object other) { |
| 73 | if (!(other instanceof Dpid)) { |
| 74 | return false; |
| 75 | } |
Jonathan Hart | f0a8179 | 2013-11-14 11:36:06 -0800 | [diff] [blame] | 76 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 77 | Dpid otherDpid = (Dpid) other; |
Jonathan Hart | f0a8179 | 2013-11-14 11:36:06 -0800 | [diff] [blame] | 78 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 79 | return value == otherDpid.value; |
| 80 | } |
Jonathan Hart | f0a8179 | 2013-11-14 11:36:06 -0800 | [diff] [blame] | 81 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 82 | @Override |
| 83 | public int hashCode() { |
| 84 | int hash = 17; |
| 85 | hash += 31 * hash + (int)(value ^ value >>> 32); |
| 86 | return hash; |
| 87 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 88 | } |