blob: fdc4b84cf9712e716f8c2d2449ee08b2ca6e3b4b [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 Radoslavov5cf1fe02014-07-03 22:52:25 -07009import org.codehaus.jackson.map.annotate.JsonSerialize;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070010import org.apache.commons.lang.Validate;
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070011
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) {
37 Validate.notNull(dpid);
38 this.dpid = dpid;
39 }
40
41 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -070042 * Creates an unfrozen copy of given Object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070043 *
44 * @param original to make copy of.
45 */
46 public SwitchEvent(SwitchEvent original) {
47 super(original);
48 this.dpid = original.dpid;
49 }
50
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070051 /**
52 * Gets the DPID identifying this switch.
53 *
54 * @return DPID
55 */
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070056 public Dpid getDpid() {
57 return dpid;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080058 }
59
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080060 @Override
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070061 public boolean equals(Object o) {
62 if (this == o) {
63 return true;
64 }
65
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070066 if (o == null) {
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070067 return false;
68 }
69
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070070 if (getClass() != o.getClass()) {
71 return false;
72 }
73 SwitchEvent other = (SwitchEvent) o;
74
75 // compare attributes
76 if (!super.equals(o)) {
77 return false;
78 }
79
80 return Objects.equals(this.dpid, other.dpid);
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070081 }
82
83 @Override
84 public int hashCode() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070085 return 31 * super.hashCode() + Objects.hashCode(dpid);
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070086 }
87
88 @Override
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080089 public String toString() {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -070090 return "[SwitchEvent 0x" + Long.toHexString(dpid.value()) + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080091 }
92
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080093 public static final int SWITCHID_BYTES = 2 + 8;
94
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070095 public static ByteBuffer getSwitchID(Dpid dpid) {
96 return getSwitchID(dpid.value());
97 }
98
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080099 public static ByteBuffer getSwitchID(Long dpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 if (dpid == null) {
101 throw new IllegalArgumentException("dpid cannot be null");
102 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700103 return (ByteBuffer) ByteBuffer.allocate(SwitchEvent.SWITCHID_BYTES)
104 .putChar('S').putLong(dpid).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800105 }
106
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800107 public byte[] getID() {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -0700108 return getSwitchID(dpid.value()).array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800109 }
110
111 public ByteBuffer getIDasByteBuffer() {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -0700112 return getSwitchID(dpid.value());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800113 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800114}