blob: ddfbecf6eb571a45423beeaec9c7e05554db2e28 [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.util;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08002
Yuta HIGUCHI6d7ee9f2014-08-22 09:56:50 -07003import javax.annotation.concurrent.Immutable;
4
Jonathan Hart23701d12014-04-03 10:45:48 -07005import net.onrc.onos.core.util.serializers.DpidDeserializer;
6import net.onrc.onos.core.util.serializers.DpidSerializer;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08007
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -08008import org.codehaus.jackson.map.annotate.JsonDeserialize;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08009import org.codehaus.jackson.map.annotate.JsonSerialize;
Jonathan Hartc78b8f62014-08-07 22:31:09 -070010import org.projectfloodlight.openflow.util.HexString;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -080011
Yuta HIGUCHI6d7ee9f2014-08-22 09:56:50 -070012import com.google.common.primitives.Longs;
13
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080014/**
15 * The class representing a network switch DPID.
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070016 * This class is immutable.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080017 */
Ray Milkey269ffb92014-04-03 14:43:30 -070018@JsonDeserialize(using = DpidDeserializer.class)
19@JsonSerialize(using = DpidSerializer.class)
Yuta HIGUCHI6d7ee9f2014-08-22 09:56:50 -070020@Immutable
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070021public final class Dpid {
Ray Milkeyec838942014-04-09 11:28:43 -070022 private static final long UNKNOWN = 0;
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070023 private final long value;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080024
Jonathan Hart3edb1752013-11-14 13:28:17 -080025 /**
26 * Default constructor.
27 */
Yuta HIGUCHIcd300152014-08-20 21:17:18 -070028 protected Dpid() {
Ray Milkey269ffb92014-04-03 14:43:30 -070029 this.value = Dpid.UNKNOWN;
Jonathan Hart3edb1752013-11-14 13:28:17 -080030 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080031
Jonathan Hart3edb1752013-11-14 13:28:17 -080032 /**
33 * Constructor from a long value.
34 *
35 * @param value the value to use.
36 */
37 public Dpid(long value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070038 this.value = value;
Jonathan Hart3edb1752013-11-14 13:28:17 -080039 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080040
Jonathan Hart3edb1752013-11-14 13:28:17 -080041 /**
42 * Constructor from a string.
43 *
44 * @param value the value to use.
45 */
46 public Dpid(String value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070047 this.value = HexString.toLong(value);
Jonathan Hart3edb1752013-11-14 13:28:17 -080048 }
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080049
Jonathan Hart3edb1752013-11-14 13:28:17 -080050 /**
51 * Get the value of the DPID.
52 *
53 * @return the value of the DPID.
54 */
Ray Milkey269ffb92014-04-03 14:43:30 -070055 public long value() {
56 return value;
57 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080058
Jonathan Hart3edb1752013-11-14 13:28:17 -080059 /**
Jonathan Hart3edb1752013-11-14 13:28:17 -080060 * 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 Milkey269ffb92014-04-03 14:43:30 -070066 return HexString.toHexString(this.value);
Jonathan Hart3edb1752013-11-14 13:28:17 -080067 }
Jonathan Hartf0a81792013-11-14 11:36:06 -080068
Jonathan Hart3edb1752013-11-14 13:28:17 -080069 @Override
70 public boolean equals(Object other) {
Ray Milkey269ffb92014-04-03 14:43:30 -070071 if (!(other instanceof Dpid)) {
72 return false;
73 }
Jonathan Hartf0a81792013-11-14 11:36:06 -080074
Ray Milkey269ffb92014-04-03 14:43:30 -070075 Dpid otherDpid = (Dpid) other;
Jonathan Hartf0a81792013-11-14 11:36:06 -080076
Ray Milkey269ffb92014-04-03 14:43:30 -070077 return value == otherDpid.value;
Jonathan Hart3edb1752013-11-14 13:28:17 -080078 }
Jonathan Hartf0a81792013-11-14 11:36:06 -080079
Jonathan Hart3edb1752013-11-14 13:28:17 -080080 @Override
81 public int hashCode() {
Yuta HIGUCHI6d7ee9f2014-08-22 09:56:50 -070082 return Longs.hashCode(value);
Jonathan Hart3edb1752013-11-14 13:28:17 -080083 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080084}