blob: f724fae93f5c68331e635b7c1140d592bc857ea1 [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08002
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -07003import net.onrc.onos.core.topology.web.serializers.SwitchEventSerializer;
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -07004import net.onrc.onos.core.util.Dpid;
5
Yuta HIGUCHIb5107282014-02-14 17:18:24 -08006import java.nio.ByteBuffer;
Sho SHIMIZUf34f1502014-06-13 13:48:00 -07007import java.util.Objects;
Yuta HIGUCHIdac4caa2014-02-11 18:51:35 -08008
Pavlin Radoslavova5637c02014-07-30 15:55:11 -07009import static com.google.common.base.Preconditions.checkNotNull;
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070010import org.codehaus.jackson.map.annotate.JsonSerialize;
11
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080012/**
Ray Milkeyb41100a2014-04-10 10:42:15 -070013 * Self-contained Switch Object.
Ray Milkey269ffb92014-04-03 14:43:30 -070014 * <p/>
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070015 * TODO: Rename to match what it is. (Switch/Port/Link/Host)Snapshot?
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070016 * FIXME: Current implementation directly use this object as
17 * Replication message, but should be sending update operation info.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080018 */
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070019@JsonSerialize(using = SwitchEventSerializer.class)
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070020public class SwitchEvent extends TopologyElement<SwitchEvent> {
21 private final Dpid dpid;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080022
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080023 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080024 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080025 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080026 @Deprecated
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070027 protected SwitchEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -070028 dpid = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080029 }
30
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070031 /**
32 * Creates the switch object.
33 *
34 * @param dpid Dpid to identify this switch
35 */
36 public SwitchEvent(Dpid dpid) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070037 this.dpid = checkNotNull(dpid);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070038 }
39
40 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -070041 * Creates an unfrozen copy of given Object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070042 *
43 * @param original to make copy of.
44 */
45 public SwitchEvent(SwitchEvent original) {
46 super(original);
47 this.dpid = original.dpid;
48 }
49
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070050 /**
51 * Gets the DPID identifying this switch.
52 *
53 * @return DPID
54 */
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070055 public Dpid getDpid() {
56 return dpid;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080057 }
58
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080059 @Override
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070060 public boolean equals(Object o) {
61 if (this == o) {
62 return true;
63 }
64
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070065 if (o == null) {
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070066 return false;
67 }
68
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070069 if (getClass() != o.getClass()) {
70 return false;
71 }
72 SwitchEvent other = (SwitchEvent) o;
73
74 // compare attributes
75 if (!super.equals(o)) {
76 return false;
77 }
78
79 return Objects.equals(this.dpid, other.dpid);
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070080 }
81
82 @Override
83 public int hashCode() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070084 return 31 * super.hashCode() + Objects.hashCode(dpid);
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070085 }
86
87 @Override
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080088 public String toString() {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -070089 return "[SwitchEvent 0x" + Long.toHexString(dpid.value()) + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080090 }
91
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080092 public static final int SWITCHID_BYTES = 2 + 8;
93
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070094 public static ByteBuffer getSwitchID(Dpid dpid) {
95 return getSwitchID(dpid.value());
96 }
97
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080098 public static ByteBuffer getSwitchID(Long dpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -070099 if (dpid == null) {
100 throw new IllegalArgumentException("dpid cannot be null");
101 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700102 return (ByteBuffer) ByteBuffer.allocate(SwitchEvent.SWITCHID_BYTES)
103 .putChar('S').putLong(dpid).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800104 }
105
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800106 public byte[] getID() {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -0700107 return getSwitchID(dpid.value()).array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800108 }
109
110 public ByteBuffer getIDasByteBuffer() {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -0700111 return getSwitchID(dpid.value());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800112 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800113}