blob: 57c648cd8ef788b782ba537c7a5d521c157fce48 [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08002
Ray Milkey2fa6ca42014-06-13 15:38:20 -07003import net.onrc.onos.core.topology.web.serializers.SwitchPortSerializer;
4import org.codehaus.jackson.map.annotate.JsonSerialize;
5
Yuta HIGUCHIb5107282014-02-14 17:18:24 -08006import java.nio.ByteBuffer;
Sho SHIMIZUf34f1502014-06-13 13:48:00 -07007import java.util.Objects;
Yuta HIGUCHIb5107282014-02-14 17:18:24 -08008
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -08009/**
Ray Milkeyb41100a2014-04-10 10:42:15 -070010 * Self-contained Port event Object.
Ray Milkey269ffb92014-04-03 14:43:30 -070011 * <p/>
Ray Milkeyb41100a2014-04-10 10:42:15 -070012 * TODO: We probably want common base class/interface for Self-Contained Event Object.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080013 */
14public class PortEvent {
Ray Milkey2fa6ca42014-06-13 15:38:20 -070015 @JsonSerialize(using = SwitchPortSerializer.class)
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080016 public static class SwitchPort {
Ray Milkey269ffb92014-04-03 14:43:30 -070017 public final Long dpid;
18 public final Long number;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080019
Ray Milkey269ffb92014-04-03 14:43:30 -070020 /**
21 * Default constructor for Serializer to use.
22 */
23 @Deprecated
24 public SwitchPort() {
25 dpid = null;
26 number = null;
27 }
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080028
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080029 public SwitchPort(Long dpid, Long number) {
30 this.dpid = dpid;
31 this.number = number;
32 }
33
34 public Long getDpid() {
35 return dpid;
36 }
37
38 public Long getNumber() {
39 return number;
40 }
41
42 @Override
43 public String toString() {
44 return "(" + Long.toHexString(dpid) + "@" + number + ")";
45 }
46
Toshio Koide0c9106d2014-02-19 15:26:38 -080047 @Override
48 public int hashCode() {
49 final int prime = 31;
50 int result = 1;
51 result = prime * result + ((dpid == null) ? 0 : dpid.hashCode());
52 result = prime * result
Ray Milkey269ffb92014-04-03 14:43:30 -070053 + ((number == null) ? 0 : number.hashCode());
Toshio Koide0c9106d2014-02-19 15:26:38 -080054 return result;
55 }
56
57 @Override
58 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070059 if (this == obj) {
Ray Milkey269ffb92014-04-03 14:43:30 -070060 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -070061 }
62 if (obj == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070063 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070064 }
65 if (getClass() != obj.getClass()) {
Ray Milkey269ffb92014-04-03 14:43:30 -070066 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070067 }
Toshio Koide0c9106d2014-02-19 15:26:38 -080068 SwitchPort other = (SwitchPort) obj;
69 if (dpid == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070070 if (other.dpid != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070071 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070072 }
73 } else if (!dpid.equals(other.dpid)) {
Ray Milkey269ffb92014-04-03 14:43:30 -070074 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070075 }
Toshio Koide0c9106d2014-02-19 15:26:38 -080076 if (number == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070077 if (other.number != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070078 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070079 }
80 } else if (!number.equals(other.number)) {
Ray Milkey269ffb92014-04-03 14:43:30 -070081 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070082 }
Toshio Koide0c9106d2014-02-19 15:26:38 -080083 return true;
84 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080085 }
86
Pavlin Radoslavov26d83402014-02-20 15:24:30 -080087 protected final SwitchPort id;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080088 // TODO Add Hardware Address
89 // TODO Add Description
90
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080091 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080092 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080093 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080094 @Deprecated
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080095 public PortEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -070096 id = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080097 }
98
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080099 public PortEvent(Long dpid, Long number) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 this.id = new SwitchPort(dpid, number);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800101 }
102
103 public Long getDpid() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700104 return id.dpid;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800105 }
106
107 public Long getNumber() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700108 return id.number;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800109 }
110
111 @Override
Sho SHIMIZUf34f1502014-06-13 13:48:00 -0700112 public boolean equals(Object o) {
113 if (this == o) {
114 return true;
115 }
116
117 if (!(o instanceof PortEvent)) {
118 return false;
119 }
120
121 PortEvent that = (PortEvent) o;
122 return Objects.equals(this.id, that.id);
123 }
124
125 @Override
126 public int hashCode() {
127 return Objects.hashCode(id);
128 }
129
130 @Override
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800131 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700132 return "[PortEvent 0x" + Long.toHexString(id.dpid) + "@" + id.number + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800133 }
134
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800135 public static final int PORTID_BYTES = SwitchEvent.SWITCHID_BYTES + 2 + 8;
136
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800137 public static ByteBuffer getPortID(Long dpid, Long number) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700138 if (dpid == null) {
139 throw new IllegalArgumentException("dpid cannot be null");
140 }
141 if (number == null) {
142 throw new IllegalArgumentException("number cannot be null");
143 }
144 return (ByteBuffer) ByteBuffer.allocate(PortEvent.PORTID_BYTES).putChar('S').putLong(dpid)
145 .putChar('P').putLong(number).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800146 }
147
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800148 public byte[] getID() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700149 return getPortID(getDpid(), getNumber()).array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800150 }
151
152 public ByteBuffer getIDasByteBuffer() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700153 return getPortID(getDpid(), getNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800154 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800155}