blob: 296a5677fe671fdd879f405df8dfa7c1479c20d9 [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.PortEventSerializer;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07004import net.onrc.onos.core.util.Dpid;
5import net.onrc.onos.core.util.PortNumber;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -07006import net.onrc.onos.core.util.SwitchPort;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07007
8import org.apache.commons.lang.Validate;
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -07009import org.codehaus.jackson.map.annotate.JsonSerialize;
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080010import java.nio.ByteBuffer;
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070011import java.util.Objects;
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080012
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080013/**
Ray Milkeyb41100a2014-04-10 10:42:15 -070014 * Self-contained Port event Object.
Ray Milkey269ffb92014-04-03 14:43:30 -070015 * <p/>
Ray Milkeyb41100a2014-04-10 10:42:15 -070016 * TODO: We probably want common base class/interface for Self-Contained Event Object.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080017 */
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070018@JsonSerialize(using = PortEventSerializer.class)
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080019public class PortEvent {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070020
Pavlin Radoslavov26d83402014-02-20 15:24:30 -080021 protected final SwitchPort id;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080022 // TODO Add Hardware Address
23 // TODO Add Description
24
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080025 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080026 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080027 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080028 @Deprecated
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070029 protected PortEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -070030 id = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080031 }
32
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070033 public PortEvent(SwitchPort switchPort) {
34 this.id = switchPort;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080035 }
36
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070037 public PortEvent(Dpid dpid, PortNumber number) {
38 this.id = new SwitchPort(dpid, number);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080039 }
40
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070041 public PortEvent(Long dpid, Long number) {
42 this.id = new SwitchPort(dpid, number);
43 }
44
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070045 public Dpid getDpid() {
46 return id.getDpid();
47 }
48
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070049 public PortNumber getPortNumber() {
50 return id.getPortNumber();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080051 }
52
53 @Override
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070054 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 HIGUCHI54ab8cd2014-02-11 09:43:34 -080073 public String toString() {
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070074 return "[PortEvent 0x" + getDpid() + "@" + getPortNumber() + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080075 }
76
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080077 public static final int PORTID_BYTES = SwitchEvent.SWITCHID_BYTES + 2 + 8;
78
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070079 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 HIGUCHIa341b112014-02-23 15:42:00 -080085 public static ByteBuffer getPortID(Long dpid, Long number) {
Ray Milkey269ffb92014-04-03 14:43:30 -070086 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 HIGUCHIb5107282014-02-14 17:18:24 -080094 }
95
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080096 public byte[] getID() {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070097 return getIDasByteBuffer().array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080098 }
99
100 public ByteBuffer getIDasByteBuffer() {
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700101 return getPortID(getDpid(), getPortNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800102 }
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700103
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800104}