blob: 09613825710db5e76e27c48bf15fb56652ebf3fd [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
Pavlin Radoslavova5637c02014-07-30 15:55:11 -07008import static com.google.common.base.Preconditions.checkNotNull;
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -07009import org.codehaus.jackson.map.annotate.JsonSerialize;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070010
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080011import java.nio.ByteBuffer;
Sho SHIMIZUf34f1502014-06-13 13:48:00 -070012import java.util.Objects;
Yuta HIGUCHIb5107282014-02-14 17:18:24 -080013
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080014/**
Ray Milkeyb41100a2014-04-10 10:42:15 -070015 * Self-contained Port event Object.
Ray Milkey269ffb92014-04-03 14:43:30 -070016 * <p/>
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070017 * TODO: Rename to match what it is. (Switch/Port/Link/Host)Snapshot?
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070018 * FIXME: Current implementation directly use this object as
19 * Replication message, but should be sending update operation info.
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080020 */
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070021@JsonSerialize(using = PortEventSerializer.class)
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070022public class PortEvent extends TopologyElement<PortEvent> {
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070023 public static final int PORTID_BYTES = SwitchEvent.SWITCHID_BYTES + 2 + 8;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070024
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070025 private final SwitchPort id;
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080026 // TODO Add Hardware Address
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070027
28 // TODO: Where should the attribute names be defined?
29 /**
30 * Attribute name for description.
31 */
32 public static final String DESCRIPTION = "description";
33
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080034 /**
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080035 * Default constructor for Serializer to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080036 */
Yuta HIGUCHI9cc421b2014-02-24 15:34:44 -080037 @Deprecated
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070038 protected PortEvent() {
Ray Milkey269ffb92014-04-03 14:43:30 -070039 id = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080040 }
41
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070042 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070043 * Constructor for given SwitchPort.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070044 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070045 * @param switchPort the SwitchPort to identify the port
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070046 */
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070047 public PortEvent(SwitchPort switchPort) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070048 this.id = checkNotNull(switchPort);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080049 }
50
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070051 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070052 * Constructor for given switch DPID and port number.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070053 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070054 * @param dpid the DPID of the switch the port belongs to
55 * @param number the PortNumber to identify the port
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070056 */
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070057 public PortEvent(Dpid dpid, PortNumber number) {
58 this.id = new SwitchPort(dpid, number);
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080059 }
60
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070061 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070062 * Copy constructor.
63 * <p>
64 * Creates an unfrozen copy of the given PortEvent object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070065 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070066 * @param original the object to make copy of
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070067 */
68 public PortEvent(PortEvent original) {
69 super(original);
70 this.id = original.id;
71 }
72
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070073 /**
74 * Gets the SwitchPort identifying this port.
75 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070076 * @return the SwitchPort identifying this port
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070077 */
78 public SwitchPort getSwitchPort() {
79 return id;
80 }
81
82 /**
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070083 * Gets the DPID of the switch this port belongs to.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070084 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070085 * @return the DPID of the switch this port belongs to
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070086 */
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070087 public Dpid getDpid() {
88 return id.getDpid();
89 }
90
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070091 /**
92 * Gets the port number.
93 *
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -070094 * @return the port number
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070095 */
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070096 public PortNumber getPortNumber() {
97 return id.getPortNumber();
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -080098 }
99
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700100 /**
101 * Computes the port ID for a given switch DPID and a port number.
102 *
103 * @param dpid the switch DPID to use
104 * @param number the port number to use
105 * @return the port ID as a ByteBuffer
106 */
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700107 public static ByteBuffer getPortID(Dpid dpid, PortNumber number) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700108 checkNotNull(dpid);
109 checkNotNull(number);
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -0700110 return getPortID(dpid.value(), number.value());
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700111 }
112
Pavlin Radoslavov5317ac92014-08-18 12:59:33 -0700113 /**
114 * Computes the port ID for a given switch DPID and a port number.
115 * <p>
116 * TODO: This method should be removed and replaced with the corresponding
117 * getPortID(Dpid, PortNumber) method.
118 *
119 * @param dpid the switch DPID to use
120 * @param number the port number to use
121 * @return the port ID as a ByteBuffer
122 */
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800123 public static ByteBuffer getPortID(Long dpid, Long number) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700124 if (dpid == null) {
125 throw new IllegalArgumentException("dpid cannot be null");
126 }
127 if (number == null) {
128 throw new IllegalArgumentException("number cannot be null");
129 }
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700130 return (ByteBuffer) ByteBuffer.allocate(PortEvent.PORTID_BYTES)
131 .putChar('S').putLong(dpid)
Ray Milkey269ffb92014-04-03 14:43:30 -0700132 .putChar('P').putLong(number).flip();
Yuta HIGUCHIb5107282014-02-14 17:18:24 -0800133 }
134
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700135 @Override
136 public Dpid getOriginDpid() {
137 return this.id.getDpid();
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800138 }
139
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700140 @Override
Yuta HIGUCHIa341b112014-02-23 15:42:00 -0800141 public ByteBuffer getIDasByteBuffer() {
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700142 return getPortID(getDpid(), getPortNumber());
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800143 }
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700144
145 @Override
146 public int hashCode() {
147 return 31 * super.hashCode() + Objects.hashCode(id);
148 }
149
150 @Override
151 public boolean equals(Object o) {
152 if (this == o) {
153 return true;
154 }
155
156 if (o == null || getClass() != o.getClass()) {
157 return false;
158 }
159
160 // Compare attributes
161 if (!super.equals(o)) {
162 return false;
163 }
164
165 PortEvent other = (PortEvent) o;
166 return Objects.equals(this.id, other.id);
167 }
168
169 @Override
170 public String toString() {
171 return "[PortEvent " + getDpid() + "@" + getPortNumber() + "]";
172 }
Yuta HIGUCHI54ab8cd2014-02-11 09:43:34 -0800173}