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.PortEventSerializer; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 4 | import net.onrc.onos.core.util.Dpid; |
| 5 | import net.onrc.onos.core.util.PortNumber; |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 6 | import net.onrc.onos.core.util.SwitchPort; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 7 | |
| 8 | import org.apache.commons.lang.Validate; |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 9 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 10 | import java.nio.ByteBuffer; |
Sho SHIMIZU | f34f150 | 2014-06-13 13:48:00 -0700 | [diff] [blame] | 11 | import java.util.Objects; |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 12 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 13 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 14 | * Self-contained Port event Object. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 15 | * <p/> |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 16 | * TODO: We probably want common base class/interface for Self-Contained Event Object. |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 17 | */ |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 18 | @JsonSerialize(using = PortEventSerializer.class) |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 19 | public class PortEvent { |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 20 | |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 21 | protected final SwitchPort id; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 22 | // TODO Add Hardware Address |
| 23 | // TODO Add Description |
| 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 | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 29 | protected PortEvent() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 30 | id = null; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 31 | } |
| 32 | |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 33 | public PortEvent(SwitchPort switchPort) { |
| 34 | this.id = switchPort; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 37 | public PortEvent(Dpid dpid, PortNumber number) { |
| 38 | this.id = new SwitchPort(dpid, number); |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 41 | public PortEvent(Long dpid, Long number) { |
| 42 | this.id = new SwitchPort(dpid, number); |
| 43 | } |
| 44 | |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 45 | public Dpid getDpid() { |
| 46 | return id.getDpid(); |
| 47 | } |
| 48 | |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 49 | public PortNumber getPortNumber() { |
| 50 | return id.getPortNumber(); |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Override |
Sho SHIMIZU | f34f150 | 2014-06-13 13:48:00 -0700 | [diff] [blame] | 54 | public boolean equals(Object o) { |
| 55 | if (this == o) { |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | if (!(o instanceof PortEvent)) { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | PortEvent that = (PortEvent) o; |
| 64 | return Objects.equals(this.id, that.id); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public int hashCode() { |
| 69 | return Objects.hashCode(id); |
| 70 | } |
| 71 | |
| 72 | @Override |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 73 | public String toString() { |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 74 | return "[PortEvent 0x" + getDpid() + "@" + getPortNumber() + "]"; |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 77 | public static final int PORTID_BYTES = SwitchEvent.SWITCHID_BYTES + 2 + 8; |
| 78 | |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 79 | public static ByteBuffer getPortID(Dpid dpid, PortNumber number) { |
| 80 | Validate.notNull(dpid); |
| 81 | Validate.notNull(number); |
| 82 | return getPortID(dpid.value(), (long) number.value()); |
| 83 | } |
| 84 | |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 85 | public static ByteBuffer getPortID(Long dpid, Long number) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 86 | if (dpid == null) { |
| 87 | throw new IllegalArgumentException("dpid cannot be null"); |
| 88 | } |
| 89 | if (number == null) { |
| 90 | throw new IllegalArgumentException("number cannot be null"); |
| 91 | } |
| 92 | return (ByteBuffer) ByteBuffer.allocate(PortEvent.PORTID_BYTES).putChar('S').putLong(dpid) |
| 93 | .putChar('P').putLong(number).flip(); |
Yuta HIGUCHI | b510728 | 2014-02-14 17:18:24 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 96 | public byte[] getID() { |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 97 | return getIDasByteBuffer().array(); |
Yuta HIGUCHI | a341b11 | 2014-02-23 15:42:00 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | public ByteBuffer getIDasByteBuffer() { |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 101 | return getPortID(getDpid(), getPortNumber()); |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 102 | } |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 103 | |
Yuta HIGUCHI | 54ab8cd | 2014-02-11 09:43:34 -0800 | [diff] [blame] | 104 | } |