blob: b6246d462e81a4a44aac7d98922193ec288db930 [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 HIGUCHIbf0a8712014-06-30 18:59:46 -070015 * TODO: Rename to match what it is. (Switch/Port/Link/Device)Snapshot?
16 * 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 /**
42 * Create an unfrozen copy of given Object.
43 *
44 * @param original to make copy of.
45 */
46 public SwitchEvent(SwitchEvent original) {
47 super(original);
48 this.dpid = original.dpid;
49 }
50
51 // TODO remove me when ready
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080052 public SwitchEvent(Long dpid) {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -070053 this.dpid = new Dpid(dpid);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080054 }
55
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070056 /**
57 * Gets the DPID identifying this switch.
58 *
59 * @return DPID
60 */
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070061 public Dpid getDpid() {
62 return dpid;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080063 }
64
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080065 @Override
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070066 public boolean equals(Object o) {
67 if (this == o) {
68 return true;
69 }
70
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070071 if (o == null) {
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070072 return false;
73 }
74
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070075 if (getClass() != o.getClass()) {
76 return false;
77 }
78 SwitchEvent other = (SwitchEvent) o;
79
80 // compare attributes
81 if (!super.equals(o)) {
82 return false;
83 }
84
85 return Objects.equals(this.dpid, other.dpid);
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070086 }
87
88 @Override
89 public int hashCode() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070090 return 31 * super.hashCode() + Objects.hashCode(dpid);
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070091 }
92
93 @Override
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080094 public String toString() {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -070095 return "[SwitchEvent 0x" + Long.toHexString(dpid.value()) + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080096 }
97
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080098 public static final int SWITCHID_BYTES = 2 + 8;
99
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700100 public static ByteBuffer getSwitchID(Dpid dpid) {
101 return getSwitchID(dpid.value());
102 }
103
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800104 public static ByteBuffer getSwitchID(Long dpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 if (dpid == null) {
106 throw new IllegalArgumentException("dpid cannot be null");
107 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700108 return (ByteBuffer) ByteBuffer.allocate(SwitchEvent.SWITCHID_BYTES)
109 .putChar('S').putLong(dpid).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800110 }
111
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800112 public byte[] getID() {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -0700113 return getSwitchID(dpid.value()).array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800114 }
115
116 public ByteBuffer getIDasByteBuffer() {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -0700117 return getSwitchID(dpid.value());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800118 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800119}