blob: 8e4e8372d0846893a050457d62c0b4a7ed3175dc [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;
Jonathan Hartc78b8f62014-08-07 22:31:09 -07008import org.projectfloodlight.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.
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 = DpidDeserializer.class)
15@JsonSerialize(using = DpidSerializer.class)
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070016public final class Dpid {
Ray Milkeyec838942014-04-09 11:28:43 -070017 private static final long UNKNOWN = 0;
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070018 private final long value;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080019
Jonathan Hart3edb1752013-11-14 13:28:17 -080020 /**
21 * Default constructor.
22 */
Yuta HIGUCHIcd300152014-08-20 21:17:18 -070023 protected 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 /**
Jonathan Hart3edb1752013-11-14 13:28:17 -080055 * Convert the DPID value to a ':' separated hexadecimal string.
56 *
57 * @return the DPID value as a ':' separated hexadecimal string.
58 */
59 @Override
60 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070061 return HexString.toHexString(this.value);
Jonathan Hart3edb1752013-11-14 13:28:17 -080062 }
Jonathan Hartf0a81792013-11-14 11:36:06 -080063
Jonathan Hart3edb1752013-11-14 13:28:17 -080064 @Override
65 public boolean equals(Object other) {
Ray Milkey269ffb92014-04-03 14:43:30 -070066 if (!(other instanceof Dpid)) {
67 return false;
68 }
Jonathan Hartf0a81792013-11-14 11:36:06 -080069
Ray Milkey269ffb92014-04-03 14:43:30 -070070 Dpid otherDpid = (Dpid) other;
Jonathan Hartf0a81792013-11-14 11:36:06 -080071
Ray Milkey269ffb92014-04-03 14:43:30 -070072 return value == otherDpid.value;
Jonathan Hart3edb1752013-11-14 13:28:17 -080073 }
Jonathan Hartf0a81792013-11-14 11:36:06 -080074
Jonathan Hart3edb1752013-11-14 13:28:17 -080075 @Override
76 public int hashCode() {
Ray Milkey269ffb92014-04-03 14:43:30 -070077 int hash = 17;
78 hash += 31 * hash + (int) (value ^ value >>> 32);
79 return hash;
Jonathan Hart3edb1752013-11-14 13:28:17 -080080 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080081}