blob: 1f20f22495fc84fbe34c4374eb0df2377d4ad86a [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 HIGUCHIb5107282014-02-14 17:18:24 -08003import java.nio.ByteBuffer;
4
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08005/**
Ray Milkeyb41100a2014-04-10 10:42:15 -07006 * Self-contained Port event Object.
Ray Milkey269ffb92014-04-03 14:43:30 -07007 * <p/>
Ray Milkeyb41100a2014-04-10 10:42:15 -07008 * TODO: We probably want common base class/interface for Self-Contained Event Object.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08009 */
10public class PortEvent {
11 public static class SwitchPort {
Ray Milkey269ffb92014-04-03 14:43:30 -070012 public final Long dpid;
13 public final Long number;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080014
Ray Milkey269ffb92014-04-03 14:43:30 -070015 /**
16 * Default constructor for Serializer to use.
17 */
18 @Deprecated
19 public SwitchPort() {
20 dpid = null;
21 number = null;
22 }
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080023
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080024 public SwitchPort(Long dpid, Long number) {
25 this.dpid = dpid;
26 this.number = number;
27 }
28
29 public Long getDpid() {
30 return dpid;
31 }
32
33 public Long getNumber() {
34 return number;
35 }
36
37 @Override
38 public String toString() {
39 return "(" + Long.toHexString(dpid) + "@" + number + ")";
40 }
41
Toshio Koide0c9106d2014-02-19 15:26:38 -080042 @Override
43 public int hashCode() {
44 final int prime = 31;
45 int result = 1;
46 result = prime * result + ((dpid == null) ? 0 : dpid.hashCode());
47 result = prime * result
Ray Milkey269ffb92014-04-03 14:43:30 -070048 + ((number == null) ? 0 : number.hashCode());
Toshio Koide0c9106d2014-02-19 15:26:38 -080049 return result;
50 }
51
52 @Override
53 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070054 if (this == obj) {
Ray Milkey269ffb92014-04-03 14:43:30 -070055 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -070056 }
57 if (obj == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070058 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070059 }
60 if (getClass() != obj.getClass()) {
Ray Milkey269ffb92014-04-03 14:43:30 -070061 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070062 }
Toshio Koide0c9106d2014-02-19 15:26:38 -080063 SwitchPort other = (SwitchPort) obj;
64 if (dpid == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070065 if (other.dpid != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070066 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070067 }
68 } else if (!dpid.equals(other.dpid)) {
Ray Milkey269ffb92014-04-03 14:43:30 -070069 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070070 }
Toshio Koide0c9106d2014-02-19 15:26:38 -080071 if (number == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070072 if (other.number != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070073 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070074 }
75 } else if (!number.equals(other.number)) {
Ray Milkey269ffb92014-04-03 14:43:30 -070076 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070077 }
Toshio Koide0c9106d2014-02-19 15:26:38 -080078 return true;
79 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080080 }
81
Pavlin Radoslavov26d83402014-02-20 15:24:30 -080082 protected final SwitchPort id;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080083 // TODO Add Hardware Address
84 // TODO Add Description
85
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080086 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080087 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080088 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080089 @Deprecated
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080090 public PortEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -070091 id = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080092 }
93
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080094 public PortEvent(Long dpid, Long number) {
Ray Milkey269ffb92014-04-03 14:43:30 -070095 this.id = new SwitchPort(dpid, number);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080096 }
97
98 public Long getDpid() {
Ray Milkey269ffb92014-04-03 14:43:30 -070099 return id.dpid;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800100 }
101
102 public Long getNumber() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 return id.number;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800104 }
105
106 @Override
107 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700108 return "[PortEvent 0x" + Long.toHexString(id.dpid) + "@" + id.number + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800109 }
110
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800111 public static final int PORTID_BYTES = SwitchEvent.SWITCHID_BYTES + 2 + 8;
112
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800113 public static ByteBuffer getPortID(Long dpid, Long number) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700114 if (dpid == null) {
115 throw new IllegalArgumentException("dpid cannot be null");
116 }
117 if (number == null) {
118 throw new IllegalArgumentException("number cannot be null");
119 }
120 return (ByteBuffer) ByteBuffer.allocate(PortEvent.PORTID_BYTES).putChar('S').putLong(dpid)
121 .putChar('P').putLong(number).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800122 }
123
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800124 public byte[] getID() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700125 return getPortID(getDpid(), getNumber()).array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800126 }
127
128 public ByteBuffer getIDasByteBuffer() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700129 return getPortID(getDpid(), getNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800130 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800131}