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 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 3 | import net.onrc.onos.core.topology.web.serializers.SwitchDataSerializer; |
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 | /** |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 13 | * Self-contained Switch object. |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 14 | */ |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 15 | @JsonSerialize(using = SwitchDataSerializer.class) |
| 16 | public class SwitchData extends TopologyElement<SwitchData> { |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 17 | public static final int SWITCHID_BYTES = 2 + 8; |
| 18 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 19 | private final Dpid dpid; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 20 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 21 | /** |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 22 | * Default constructor for Serializer to use. |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 23 | */ |
Yuta HIGUCHI | 9cc421b | 2014-02-24 15:34:44 -0800 | [diff] [blame] | 24 | @Deprecated |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 25 | protected SwitchData() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 26 | dpid = null; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 27 | } |
| 28 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 29 | /** |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 30 | * Constructor for given switch DPID. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 31 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 32 | * @param dpid the switch DPID to identify the switch |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 33 | */ |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 34 | public SwitchData(Dpid dpid) { |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 35 | this.dpid = checkNotNull(dpid); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /** |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 39 | * Copy constructor. |
| 40 | * <p> |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 41 | * Creates an unfrozen copy of the given SwitchData object. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 42 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 43 | * @param original the object to make copy of |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 44 | */ |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 45 | public SwitchData(SwitchData original) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 46 | super(original); |
| 47 | this.dpid = original.dpid; |
| 48 | } |
| 49 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Gets the DPID identifying this switch. |
| 52 | * |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 53 | * @return the DPID identifying this switch |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 54 | */ |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 55 | public Dpid getDpid() { |
| 56 | return dpid; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 59 | /** |
| 60 | * Computes the switch ID for a given switch DPID. |
| 61 | * |
| 62 | * @param dpid the switch DPID to use |
| 63 | * @return the switch ID as a ByteBuffer |
| 64 | */ |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 65 | public static ByteBuffer getSwitchID(Dpid dpid) { |
| 66 | return getSwitchID(dpid.value()); |
| 67 | } |
| 68 | |
Pavlin Radoslavov | 5317ac9 | 2014-08-18 12:59:33 -0700 | [diff] [blame] | 69 | /** |
| 70 | * Computes the switch ID for a given switch DPID. |
| 71 | * <p> |
| 72 | * TODO: This method should be removed and replaced with the corresponding |
| 73 | * getSwitchID(Dpid) method. |
| 74 | * |
| 75 | * @param dpid the switch DPID to use |
| 76 | * @return the switch ID as a ByteBuffer |
| 77 | */ |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 78 | public static ByteBuffer getSwitchID(Long dpid) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 79 | if (dpid == null) { |
| 80 | throw new IllegalArgumentException("dpid cannot be null"); |
| 81 | } |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 82 | return (ByteBuffer) ByteBuffer.allocate(SwitchData.SWITCHID_BYTES) |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 83 | .putChar('S').putLong(dpid).flip(); |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 86 | @Override |
| 87 | public Dpid getOriginDpid() { |
| 88 | return this.dpid; |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 91 | @Override |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 92 | public ByteBuffer getIDasByteBuffer() { |
Sho SHIMIZU | 83e3c1f | 2014-06-13 15:57:26 -0700 | [diff] [blame] | 93 | return getSwitchID(dpid.value()); |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 94 | } |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 95 | |
| 96 | @Override |
| 97 | public int hashCode() { |
| 98 | return 31 * super.hashCode() + Objects.hashCode(dpid); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public boolean equals(Object o) { |
| 103 | if (this == o) { |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | if (o == null || getClass() != o.getClass()) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | // Compare attributes |
| 112 | if (!super.equals(o)) { |
| 113 | return false; |
| 114 | } |
| 115 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 116 | SwitchData other = (SwitchData) o; |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 117 | return Objects.equals(this.dpid, other.dpid); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public String toString() { |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 122 | return "[SwitchData " + dpid + "]"; |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 123 | } |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 124 | } |