blob: 5c63de7dc5df4f06bdad15a3d192de82aa9d83e1 [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;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07004import net.onrc.onos.core.util.Dpid;
5import net.onrc.onos.core.util.PortNumber;
6
7import org.apache.commons.lang.Validate;
Ray Milkey2fa6ca42014-06-13 15:38:20 -07008import org.codehaus.jackson.map.annotate.JsonSerialize;
9
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 */
18public class PortEvent {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070019
20 // TODO eliminate this class and use util.SwitchPort if possible
Ray Milkey2fa6ca42014-06-13 15:38:20 -070021 @JsonSerialize(using = SwitchPortSerializer.class)
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080022 public static class SwitchPort {
Ray Milkey269ffb92014-04-03 14:43:30 -070023 public final Long dpid;
24 public final Long number;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080025
Ray Milkey269ffb92014-04-03 14:43:30 -070026 /**
27 * Default constructor for Serializer to use.
28 */
29 @Deprecated
30 public SwitchPort() {
31 dpid = null;
32 number = null;
33 }
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080034
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080035 public SwitchPort(Long dpid, Long number) {
36 this.dpid = dpid;
37 this.number = number;
38 }
39
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070040 public SwitchPort(Dpid dpid, PortNumber number) {
41 this(dpid.value(), (long) number.value());
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080042 }
43
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070044 public Dpid getDpid() {
45 return new Dpid(dpid);
46 }
47
48 public PortNumber getNumber() {
49 return new PortNumber(number.shortValue());
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080050 }
51
52 @Override
53 public String toString() {
54 return "(" + Long.toHexString(dpid) + "@" + number + ")";
55 }
56
Toshio Koide0c9106d2014-02-19 15:26:38 -080057 @Override
58 public int hashCode() {
59 final int prime = 31;
60 int result = 1;
61 result = prime * result + ((dpid == null) ? 0 : dpid.hashCode());
62 result = prime * result
Ray Milkey269ffb92014-04-03 14:43:30 -070063 + ((number == null) ? 0 : number.hashCode());
Toshio Koide0c9106d2014-02-19 15:26:38 -080064 return result;
65 }
66
67 @Override
68 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070069 if (this == obj) {
Ray Milkey269ffb92014-04-03 14:43:30 -070070 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -070071 }
72 if (obj == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070073 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070074 }
75 if (getClass() != obj.getClass()) {
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 SwitchPort other = (SwitchPort) obj;
79 if (dpid == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070080 if (other.dpid != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070081 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070082 }
83 } else if (!dpid.equals(other.dpid)) {
Ray Milkey269ffb92014-04-03 14:43:30 -070084 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070085 }
Toshio Koide0c9106d2014-02-19 15:26:38 -080086 if (number == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070087 if (other.number != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070088 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070089 }
90 } else if (!number.equals(other.number)) {
Ray Milkey269ffb92014-04-03 14:43:30 -070091 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070092 }
Toshio Koide0c9106d2014-02-19 15:26:38 -080093 return true;
94 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080095 }
96
Pavlin Radoslavov26d83402014-02-20 15:24:30 -080097 protected final SwitchPort id;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080098 // TODO Add Hardware Address
99 // TODO Add Description
100
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800101 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -0800102 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800103 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -0800104 @Deprecated
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800105 public PortEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700106 id = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800107 }
108
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800109 public PortEvent(Long dpid, Long number) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700110 this.id = new SwitchPort(dpid, number);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800111 }
112
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700113 public PortEvent(Dpid dpid, PortNumber number) {
114 this.id = new SwitchPort(dpid, number);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800115 }
116
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700117 public Dpid getDpid() {
118 return id.getDpid();
119 }
120
121 public PortNumber getNumber() {
122 return id.getNumber();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800123 }
124
125 @Override
Sho SHIMIZUf34f1502014-06-13 13:48:00 -0700126 public boolean equals(Object o) {
127 if (this == o) {
128 return true;
129 }
130
131 if (!(o instanceof PortEvent)) {
132 return false;
133 }
134
135 PortEvent that = (PortEvent) o;
136 return Objects.equals(this.id, that.id);
137 }
138
139 @Override
140 public int hashCode() {
141 return Objects.hashCode(id);
142 }
143
144 @Override
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800145 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700146 return "[PortEvent 0x" + Long.toHexString(id.dpid) + "@" + id.number + "]";
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800147 }
148
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800149 public static final int PORTID_BYTES = SwitchEvent.SWITCHID_BYTES + 2 + 8;
150
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700151 public static ByteBuffer getPortID(Dpid dpid, PortNumber number) {
152 Validate.notNull(dpid);
153 Validate.notNull(number);
154 return getPortID(dpid.value(), (long) number.value());
155 }
156
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800157 public static ByteBuffer getPortID(Long dpid, Long number) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700158 if (dpid == null) {
159 throw new IllegalArgumentException("dpid cannot be null");
160 }
161 if (number == null) {
162 throw new IllegalArgumentException("number cannot be null");
163 }
164 return (ByteBuffer) ByteBuffer.allocate(PortEvent.PORTID_BYTES).putChar('S').putLong(dpid)
165 .putChar('P').putLong(number).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800166 }
167
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800168 public byte[] getID() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700169 return getPortID(getDpid(), getNumber()).array();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800170 }
171
172 public ByteBuffer getIDasByteBuffer() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700173 return getPortID(getDpid(), getNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800174 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800175}