Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.util; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 2 | |
Yuta HIGUCHI | 6d7ee9f | 2014-08-22 09:56:50 -0700 | [diff] [blame] | 3 | import javax.annotation.concurrent.Immutable; |
4 | |||||
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 5 | import net.onrc.onos.core.util.serializers.DpidDeserializer; |
6 | import net.onrc.onos.core.util.serializers.DpidSerializer; | ||||
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 7 | |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 8 | import org.codehaus.jackson.map.annotate.JsonDeserialize; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 9 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame] | 10 | import org.projectfloodlight.openflow.util.HexString; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 11 | |
Yuta HIGUCHI | 6d7ee9f | 2014-08-22 09:56:50 -0700 | [diff] [blame] | 12 | import com.google.common.primitives.Longs; |
13 | |||||
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 14 | /** |
15 | * The class representing a network switch DPID. | ||||
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 16 | * This class is immutable. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 17 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 18 | @JsonDeserialize(using = DpidDeserializer.class) |
19 | @JsonSerialize(using = DpidSerializer.class) | ||||
Yuta HIGUCHI | 6d7ee9f | 2014-08-22 09:56:50 -0700 | [diff] [blame] | 20 | @Immutable |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 21 | public final class Dpid { |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 22 | private static final long UNKNOWN = 0; |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 23 | private final long value; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 24 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 25 | /** |
26 | * Default constructor. | ||||
27 | */ | ||||
Yuta HIGUCHI | cd30015 | 2014-08-20 21:17:18 -0700 | [diff] [blame] | 28 | protected Dpid() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 29 | this.value = Dpid.UNKNOWN; |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 30 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 31 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 32 | /** |
33 | * Constructor from a long value. | ||||
34 | * | ||||
35 | * @param value the value to use. | ||||
36 | */ | ||||
37 | public Dpid(long value) { | ||||
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 38 | this.value = value; |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 39 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 40 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 41 | /** |
42 | * Constructor from a string. | ||||
43 | * | ||||
44 | * @param value the value to use. | ||||
45 | */ | ||||
46 | public Dpid(String value) { | ||||
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 47 | this.value = HexString.toLong(value); |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 48 | } |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 49 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 50 | /** |
51 | * Get the value of the DPID. | ||||
52 | * | ||||
53 | * @return the value of the DPID. | ||||
54 | */ | ||||
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 55 | public long value() { |
56 | return value; | ||||
57 | } | ||||
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 58 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 59 | /** |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 60 | * Convert the DPID value to a ':' separated hexadecimal string. |
61 | * | ||||
62 | * @return the DPID value as a ':' separated hexadecimal string. | ||||
63 | */ | ||||
64 | @Override | ||||
65 | public String toString() { | ||||
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | return HexString.toHexString(this.value); |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 67 | } |
Jonathan Hart | f0a8179 | 2013-11-14 11:36:06 -0800 | [diff] [blame] | 68 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 69 | @Override |
70 | public boolean equals(Object other) { | ||||
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 71 | if (!(other instanceof Dpid)) { |
72 | return false; | ||||
73 | } | ||||
Jonathan Hart | f0a8179 | 2013-11-14 11:36:06 -0800 | [diff] [blame] | 74 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 75 | Dpid otherDpid = (Dpid) other; |
Jonathan Hart | f0a8179 | 2013-11-14 11:36:06 -0800 | [diff] [blame] | 76 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 77 | return value == otherDpid.value; |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 78 | } |
Jonathan Hart | f0a8179 | 2013-11-14 11:36:06 -0800 | [diff] [blame] | 79 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 80 | @Override |
81 | public int hashCode() { | ||||
Yuta HIGUCHI | 6d7ee9f | 2014-08-22 09:56:50 -0700 | [diff] [blame] | 82 | return Longs.hashCode(value); |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 83 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 84 | } |