blob: f89005dd35551f510e44fd86ee0bada9f232f529 [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> {
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070021 public static final int SWITCHID_BYTES = 2 + 8;
22
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070023 private final Dpid dpid;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080024
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080025 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080026 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080027 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080028 @Deprecated
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070029 protected SwitchEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -070030 dpid = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080031 }
32
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070033 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070034 * Constructor for given switch DPID.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070035 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070036 * @param dpid the switch DPID to identify the switch
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070037 */
38 public SwitchEvent(Dpid dpid) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070039 this.dpid = checkNotNull(dpid);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070040 }
41
42 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070043 * Copy constructor.
44 * <p>
45 * Creates an unfrozen copy of the given SwitchEvent object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070046 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070047 * @param original the object to make copy of
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070048 */
49 public SwitchEvent(SwitchEvent original) {
50 super(original);
51 this.dpid = original.dpid;
52 }
53
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070054 /**
55 * Gets the DPID identifying this switch.
56 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070057 * @return the DPID identifying this switch
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070058 */
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070059 public Dpid getDpid() {
60 return dpid;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080061 }
62
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070063 /**
64 * Computes the switch ID for a given switch DPID.
65 *
66 * @param dpid the switch DPID to use
67 * @return the switch ID as a ByteBuffer
68 */
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070069 public static ByteBuffer getSwitchID(Dpid dpid) {
70 return getSwitchID(dpid.value());
71 }
72
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070073 /**
74 * Computes the switch ID for a given switch DPID.
75 * <p>
76 * TODO: This method should be removed and replaced with the corresponding
77 * getSwitchID(Dpid) method.
78 *
79 * @param dpid the switch DPID to use
80 * @return the switch ID as a ByteBuffer
81 */
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080082 public static ByteBuffer getSwitchID(Long dpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -070083 if (dpid == null) {
84 throw new IllegalArgumentException("dpid cannot be null");
85 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070086 return (ByteBuffer) ByteBuffer.allocate(SwitchEvent.SWITCHID_BYTES)
87 .putChar('S').putLong(dpid).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080088 }
89
Pavlin Radoslavov31f85102014-08-15 13:55:44 -070090 @Override
91 public Dpid getOriginDpid() {
92 return this.dpid;
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080093 }
94
Pavlin Radoslavov31f85102014-08-15 13:55:44 -070095 @Override
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080096 public ByteBuffer getIDasByteBuffer() {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -070097 return getSwitchID(dpid.value());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080098 }
Pavlin Radoslavov31f85102014-08-15 13:55:44 -070099
100 @Override
101 public int hashCode() {
102 return 31 * super.hashCode() + Objects.hashCode(dpid);
103 }
104
105 @Override
106 public boolean equals(Object o) {
107 if (this == o) {
108 return true;
109 }
110
111 if (o == null || getClass() != o.getClass()) {
112 return false;
113 }
114
115 // Compare attributes
116 if (!super.equals(o)) {
117 return false;
118 }
119
120 SwitchEvent other = (SwitchEvent) o;
121 return Objects.equals(this.dpid, other.dpid);
122 }
123
124 @Override
125 public String toString() {
126 return "[SwitchEvent " + dpid + "]";
127 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800128}