blob: 47f8e81b38929a9bfeb4488ab571e3bd2d1e6dc2 [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
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -070059 /**
60 * Gets the event origin DPID.
61 *
62 * @return the event origin DPID.
63 */
64 public Dpid getOriginDpid() {
65 return this.dpid;
66 }
67
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080068 @Override
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070069 public boolean equals(Object o) {
70 if (this == o) {
71 return true;
72 }
73
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070074 if (o == null) {
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070075 return false;
76 }
77
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070078 if (getClass() != o.getClass()) {
79 return false;
80 }
81 SwitchEvent other = (SwitchEvent) o;
82
83 // compare attributes
84 if (!super.equals(o)) {
85 return false;
86 }
87
88 return Objects.equals(this.dpid, other.dpid);
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070089 }
90
91 @Override
92 public int hashCode() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070093 return 31 * super.hashCode() + Objects.hashCode(dpid);
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070094 }
95
96 @Override
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080097 public String toString() {
Pavlin Radoslavovbb17de22014-08-06 15:34:37 -070098 return "[SwitchEvent " + dpid + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080099 }
100
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800101 public static final int SWITCHID_BYTES = 2 + 8;
102
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700103 public static ByteBuffer getSwitchID(Dpid dpid) {
104 return getSwitchID(dpid.value());
105 }
106
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800107 public static ByteBuffer getSwitchID(Long dpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700108 if (dpid == null) {
109 throw new IllegalArgumentException("dpid cannot be null");
110 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700111 return (ByteBuffer) ByteBuffer.allocate(SwitchEvent.SWITCHID_BYTES)
112 .putChar('S').putLong(dpid).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800113 }
114
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800115 public byte[] getID() {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -0700116 return getSwitchID(dpid.value()).array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800117 }
118
119 public ByteBuffer getIDasByteBuffer() {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -0700120 return getSwitchID(dpid.value());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800121 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800122}