blob: 8d3c9453113b8fba143d218d5e641dad9e0c90a7 [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08002
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07003import net.onrc.onos.core.util.Dpid;
4import net.onrc.onos.core.util.PortNumber;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -07005import net.onrc.onos.core.util.SwitchPort;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07006
7import org.apache.commons.lang.Validate;
Yuta HIGUCHIb5107282014-02-14 17:18:24 -08008import java.nio.ByteBuffer;
Sho SHIMIZUf34f1502014-06-13 13:48:00 -07009import java.util.Objects;
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080010
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080011/**
Ray Milkeyb41100a2014-04-10 10:42:15 -070012 * Self-contained Port event Object.
Ray Milkey269ffb92014-04-03 14:43:30 -070013 * <p/>
Ray Milkeyb41100a2014-04-10 10:42:15 -070014 * TODO: We probably want common base class/interface for Self-Contained Event Object.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080015 */
16public class PortEvent {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070017
Pavlin Radoslavov26d83402014-02-20 15:24:30 -080018 protected final SwitchPort id;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080019 // TODO Add Hardware Address
20 // TODO Add Description
21
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080022 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080023 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080024 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080025 @Deprecated
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070026 protected PortEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -070027 id = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080028 }
29
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070030 public PortEvent(SwitchPort switchPort) {
31 this.id = switchPort;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080032 }
33
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070034 public PortEvent(Dpid dpid, PortNumber number) {
35 this.id = new SwitchPort(dpid, number);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080036 }
37
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070038 public PortEvent(Long dpid, Long number) {
39 this.id = new SwitchPort(dpid, number);
40 }
41
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070042 public Dpid getDpid() {
43 return id.getDpid();
44 }
45
46 public PortNumber getNumber() {
47 return id.getNumber();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080048 }
49
50 @Override
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070051 public boolean equals(Object o) {
52 if (this == o) {
53 return true;
54 }
55
56 if (!(o instanceof PortEvent)) {
57 return false;
58 }
59
60 PortEvent that = (PortEvent) o;
61 return Objects.equals(this.id, that.id);
62 }
63
64 @Override
65 public int hashCode() {
66 return Objects.hashCode(id);
67 }
68
69 @Override
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080070 public String toString() {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070071 return "[PortEvent 0x" + getDpid() + "@" + getNumber() + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080072 }
73
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080074 public static final int PORTID_BYTES = SwitchEvent.SWITCHID_BYTES + 2 + 8;
75
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070076 public static ByteBuffer getPortID(Dpid dpid, PortNumber number) {
77 Validate.notNull(dpid);
78 Validate.notNull(number);
79 return getPortID(dpid.value(), (long) number.value());
80 }
81
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080082 public static ByteBuffer getPortID(Long dpid, Long number) {
Ray Milkey269ffb92014-04-03 14:43:30 -070083 if (dpid == null) {
84 throw new IllegalArgumentException("dpid cannot be null");
85 }
86 if (number == null) {
87 throw new IllegalArgumentException("number cannot be null");
88 }
89 return (ByteBuffer) ByteBuffer.allocate(PortEvent.PORTID_BYTES).putChar('S').putLong(dpid)
90 .putChar('P').putLong(number).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080091 }
92
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080093 public byte[] getID() {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070094 return getIDasByteBuffer().array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -080095 }
96
97 public ByteBuffer getIDasByteBuffer() {
Ray Milkey269ffb92014-04-03 14:43:30 -070098 return getPortID(getDpid(), getNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080099 }
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700100
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800101}