Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 2 | |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 3 | import net.onrc.onos.core.topology.web.serializers.SwitchEventSerializer; |
Sho SHIMIZU | 83e3c1f | 2014-06-13 15:57:26 -0700 | [diff] [blame] | 4 | import net.onrc.onos.core.util.Dpid; |
| 5 | |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 6 | import java.nio.ByteBuffer; |
Sho SHIMIZU | f34f150 | 2014-06-13 13:48:00 -0700 | [diff] [blame] | 7 | import java.util.Objects; |
Yuta HIGUCHI | dac4caa | 2014-02-11 18:51:35 -0800 | [diff] [blame] | 8 | |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 9 | import static com.google.common.base.Preconditions.checkNotNull; |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 10 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
| 11 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 12 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 13 | * Self-contained Switch Object. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 14 | * <p/> |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 15 | * TODO: Rename to match what it is. (Switch/Port/Link/Host)Snapshot? |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 16 | * FIXME: Current implementation directly use this object as |
| 17 | * Replication message, but should be sending update operation info. |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 18 | */ |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 19 | @JsonSerialize(using = SwitchEventSerializer.class) |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 20 | public class SwitchEvent extends TopologyElement<SwitchEvent> { |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 21 | public static final int SWITCHID_BYTES = 2 + 8; |
| 22 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 23 | private final Dpid dpid; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 24 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 25 | /** |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 26 | * Default constructor for Serializer to use. |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 27 | */ |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 28 | @Deprecated |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 29 | protected SwitchEvent() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 30 | dpid = null; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 31 | } |
| 32 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 33 | /** |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 34 | * Constructor for given switch DPID. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 35 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 36 | * @param dpid the switch DPID to identify the switch |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 37 | */ |
| 38 | public SwitchEvent(Dpid dpid) { |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 39 | this.dpid = checkNotNull(dpid); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /** |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 43 | * Copy constructor. |
| 44 | * <p> |
| 45 | * Creates an unfrozen copy of the given SwitchEvent object. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 46 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 47 | * @param original the object to make copy of |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 48 | */ |
| 49 | public SwitchEvent(SwitchEvent original) { |
| 50 | super(original); |
| 51 | this.dpid = original.dpid; |
| 52 | } |
| 53 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Gets the DPID identifying this switch. |
| 56 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 57 | * @return the DPID identifying this switch |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 58 | */ |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 59 | public Dpid getDpid() { |
| 60 | return dpid; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 63 | /** |
| 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 HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 69 | public static ByteBuffer getSwitchID(Dpid dpid) { |
| 70 | return getSwitchID(dpid.value()); |
| 71 | } |
| 72 | |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 73 | /** |
| 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 HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 82 | public static ByteBuffer getSwitchID(Long dpid) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 83 | if (dpid == null) { |
| 84 | throw new IllegalArgumentException("dpid cannot be null"); |
| 85 | } |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 86 | return (ByteBuffer) ByteBuffer.allocate(SwitchEvent.SWITCHID_BYTES) |
| 87 | .putChar('S').putLong(dpid).flip(); |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 90 | @Override |
| 91 | public Dpid getOriginDpid() { |
| 92 | return this.dpid; |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 95 | @Override |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 96 | public ByteBuffer getIDasByteBuffer() { |
Sho SHIMIZU | 83e3c1f | 2014-06-13 15:57:26 -0700 | [diff] [blame] | 97 | return getSwitchID(dpid.value()); |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 98 | } |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 99 | |
| 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 HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 128 | } |