blob: c51a1573a448b496462bfe9c7fecaab257939d99 [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -08002
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -07003import java.nio.ByteBuffer;
4import java.nio.charset.StandardCharsets;
weibitcef09862014-07-11 17:05:16 -07005import java.util.Objects;
6
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07007import net.onrc.onos.core.util.Dpid;
Pavlin Radoslavova5637c02014-07-30 15:55:11 -07008import net.onrc.onos.core.util.OnosInstanceId;
9
10import static com.google.common.base.Preconditions.checkNotNull;
weibitcef09862014-07-11 17:05:16 -070011
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080012/**
13 * Self-contained Topology event Object
Ray Milkey269ffb92014-04-03 14:43:30 -070014 * <p/>
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080015 * TODO: For now the topology event contains one of the following events:
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -070016 * Switch Mastership, Switch, Port, Link, Host. In the future it will contain
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070017 * multiple events in a single transaction.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070018 * TODO: This class should become immutable after its internals and usage
19 * are finalized.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080020 */
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070021public final class TopologyEvent {
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -070022 private final MastershipEvent mastershipEvent; // Set for Mastership event
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070023 private final SwitchEvent switchEvent; // Set for Switch event
24 private final PortEvent portEvent; // Set for Port event
25 private final LinkEvent linkEvent; // Set for Link event
26 private final HostEvent hostEvent; // Set for Host event
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070027 private final OnosInstanceId onosInstanceId; // The ONOS Instance ID
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080028
29 /**
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070030 * Default constructor for serializer.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080031 */
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070032 @Deprecated
33 protected TopologyEvent() {
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -070034 mastershipEvent = null;
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070035 switchEvent = null;
36 portEvent = null;
37 linkEvent = null;
38 hostEvent = null;
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070039 onosInstanceId = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080040 }
41
42 /**
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070043 * Constructor for given Switch Mastership event.
44 *
45 * @param mastershipEvent the Switch Mastership event to use.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070046 * @param onosInstanceId the ONOS Instance ID that originates the event.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070047 */
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070048 TopologyEvent(MastershipEvent mastershipEvent,
49 OnosInstanceId onosInstanceId) {
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -070050 this.mastershipEvent = checkNotNull(mastershipEvent);
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070051 this.switchEvent = null;
52 this.portEvent = null;
53 this.linkEvent = null;
54 this.hostEvent = null;
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070055 this.onosInstanceId = checkNotNull(onosInstanceId);
56 }
57
58 /**
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -070059 * Constructor for given Switch event.
60 *
61 * @param switchEvent the Switch event to use.
62 * @param onosInstanceId the ONOS Instance ID that originates the event.
63 */
64 TopologyEvent(SwitchEvent switchEvent, OnosInstanceId onosInstanceId) {
65 this.mastershipEvent = null;
66 this.switchEvent = checkNotNull(switchEvent);
67 this.portEvent = null;
68 this.linkEvent = null;
69 this.hostEvent = null;
70 this.onosInstanceId = checkNotNull(onosInstanceId);
71 }
72
73 /**
74 * Constructor for given Port event.
75 *
76 * @param portEvent the Port event to use.
77 * @param onosInstanceId the ONOS Instance ID that originates the event.
78 */
79 TopologyEvent(PortEvent portEvent, OnosInstanceId onosInstanceId) {
80 this.mastershipEvent = null;
81 this.switchEvent = null;
82 this.portEvent = checkNotNull(portEvent);
83 this.linkEvent = null;
84 this.hostEvent = null;
85 this.onosInstanceId = checkNotNull(onosInstanceId);
86 }
87
88 /**
89 * Constructor for given Link event.
90 *
91 * @param linkEvent the Link event to use.
92 * @param onosInstanceId the ONOS Instance ID that originates the event.
93 */
94 TopologyEvent(LinkEvent linkEvent, OnosInstanceId onosInstanceId) {
95 this.mastershipEvent = null;
96 this.switchEvent = null;
97 this.portEvent = null;
98 this.linkEvent = checkNotNull(linkEvent);
99 this.hostEvent = null;
100 this.onosInstanceId = checkNotNull(onosInstanceId);
101 }
102
103 /**
104 * Constructor for given Host event.
105 *
106 * @param hostEvent the Host event to use.
107 * @param onosInstanceId the ONOS Instance ID that originates the event.
108 */
109 TopologyEvent(HostEvent hostEvent, OnosInstanceId onosInstanceId) {
110 this.mastershipEvent = null;
111 this.switchEvent = null;
112 this.portEvent = null;
113 this.linkEvent = null;
114 this.hostEvent = checkNotNull(hostEvent);
115 this.onosInstanceId = checkNotNull(onosInstanceId);
116 }
117
118 /**
119 * Gets the Mastership event.
120 *
121 * @return the Mastership event.
122 */
123 public MastershipEvent getMastershipEvent() {
124 return mastershipEvent;
125 }
126
127 /**
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700128 * Gets the Switch event.
129 *
130 * @return the Switch event.
131 */
132 public SwitchEvent getSwitchEvent() {
133 return switchEvent;
134 }
135
136 /**
137 * Gets the Port event.
138 *
139 * @return the Port event.
140 */
141 public PortEvent getPortEvent() {
142 return portEvent;
143 }
144
145 /**
146 * Gets the Link event.
147 *
148 * @return the Link event.
149 */
150 public LinkEvent getLinkEvent() {
151 return linkEvent;
152 }
153
154 /**
155 * Gets the Host event.
156 *
157 * @return the Host event.
158 */
159 public HostEvent getHostEvent() {
160 return hostEvent;
161 }
162
163 /**
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700164 * Gets the ONOS Instance ID.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700165 *
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700166 * @return the ONOS Instance ID.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700167 */
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700168 public OnosInstanceId getOnosInstanceId() {
169 return onosInstanceId;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700170 }
171
172 /**
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700173 * Gets the event origin DPID.
174 *
175 * @return the event origin DPID.
176 */
177 public Dpid getOriginDpid() {
178 if (mastershipEvent != null) {
179 return mastershipEvent.getOriginDpid();
180 }
181 if (switchEvent != null) {
182 return switchEvent.getOriginDpid();
183 }
184 if (portEvent != null) {
185 return portEvent.getOriginDpid();
186 }
187 if (linkEvent != null) {
188 return linkEvent.getOriginDpid();
189 }
190 if (hostEvent != null) {
191 return hostEvent.getOriginDpid();
192 }
193 return null;
194 }
195
196 /**
197 * Tests whether the event origin DPID equals the specified DPID.
198 *
199 * @param dpid the DPID to compare against.
200 * @return true if the event origin Dpid equals the specified DPID.
201 */
202 public boolean equalsOriginDpid(Dpid dpid) {
203 return dpid.equals(getOriginDpid());
204 }
205
206 /**
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700207 * Checks if all events contained are equal.
Yuta HIGUCHIccab05d2014-07-26 22:42:28 -0700208 *
209 * @param obj TopologyEvent to compare against
weibitcef09862014-07-11 17:05:16 -0700210 */
211 @Override
212 public boolean equals(Object obj) {
213 if (this == obj) {
214 return true;
215 }
216
217 if (obj == null) {
218 return false;
219 }
220
221 if (getClass() != obj.getClass()) {
222 return false;
223 }
224
225 TopologyEvent other = (TopologyEvent) obj;
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700226 return Objects.equals(mastershipEvent, other.mastershipEvent) &&
227 Objects.equals(switchEvent, other.switchEvent) &&
228 Objects.equals(portEvent, other.portEvent) &&
229 Objects.equals(linkEvent, other.linkEvent) &&
230 Objects.equals(hostEvent, other.hostEvent) &&
231 Objects.equals(onosInstanceId, other.onosInstanceId);
weibitcef09862014-07-11 17:05:16 -0700232 }
233
234 @Override
235 public int hashCode() {
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700236 return Objects.hash(mastershipEvent, switchEvent, portEvent,
237 linkEvent, hostEvent, onosInstanceId);
weibitcef09862014-07-11 17:05:16 -0700238 }
239
240 /**
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700241 * Gets the string representation of the event.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800242 *
243 * @return the string representation of the event.
244 */
245 @Override
246 public String toString() {
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700247 String eventStr = null;
248
249 //
250 // Get the Event string
251 //
Pavlin Radoslavov845ffdd2014-08-06 18:01:26 -0700252 stringLabel: {
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700253 if (mastershipEvent != null) {
254 eventStr = mastershipEvent.toString();
Pavlin Radoslavov845ffdd2014-08-06 18:01:26 -0700255 break stringLabel;
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700256 }
257 if (switchEvent != null) {
258 eventStr = switchEvent.toString();
Pavlin Radoslavov845ffdd2014-08-06 18:01:26 -0700259 break stringLabel;
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700260 }
261 if (portEvent != null) {
262 eventStr = portEvent.toString();
Pavlin Radoslavov845ffdd2014-08-06 18:01:26 -0700263 break stringLabel;
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700264 }
265 if (linkEvent != null) {
266 eventStr = linkEvent.toString();
Pavlin Radoslavov845ffdd2014-08-06 18:01:26 -0700267 break stringLabel;
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700268 }
269 if (hostEvent != null) {
270 eventStr = hostEvent.toString();
Pavlin Radoslavov845ffdd2014-08-06 18:01:26 -0700271 break stringLabel;
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700272 }
273 // No event found
274 return "[Empty TopologyEvent]";
Pavlin Radoslavov845ffdd2014-08-06 18:01:26 -0700275 }
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700276
277 return "[TopologyEvent " + eventStr + " from " +
278 onosInstanceId.toString() + "]";
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800279 }
280
281 /**
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700282 * Gets the Topology event ID as a byte array.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800283 *
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700284 * @return the Topology event ID as a byte array.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800285 */
286 public byte[] getID() {
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700287 return getIDasByteBuffer().array();
288 }
289
290 /**
291 * Gets the Topology event ID as a ByteBuffer.
292 *
293 * @return the Topology event ID as a ByteBuffer.
294 */
295 public ByteBuffer getIDasByteBuffer() {
296 ByteBuffer eventId = null;
297
298 //
299 // Get the Event ID
300 //
Pavlin Radoslavov845ffdd2014-08-06 18:01:26 -0700301 idLabel: {
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700302 if (mastershipEvent != null) {
303 eventId = mastershipEvent.getIDasByteBuffer();
Pavlin Radoslavov845ffdd2014-08-06 18:01:26 -0700304 break idLabel;
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700305 }
306 if (switchEvent != null) {
307 eventId = switchEvent.getIDasByteBuffer();
Pavlin Radoslavov845ffdd2014-08-06 18:01:26 -0700308 break idLabel;
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700309 }
310 if (portEvent != null) {
311 eventId = portEvent.getIDasByteBuffer();
Pavlin Radoslavov845ffdd2014-08-06 18:01:26 -0700312 break idLabel;
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700313 }
314 if (linkEvent != null) {
315 eventId = linkEvent.getIDasByteBuffer();
Pavlin Radoslavov845ffdd2014-08-06 18:01:26 -0700316 break idLabel;
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700317 }
318 if (hostEvent != null) {
319 eventId = hostEvent.getIDasByteBuffer();
Pavlin Radoslavov845ffdd2014-08-06 18:01:26 -0700320 break idLabel;
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700321 }
322 // No event found
323 throw new IllegalStateException("Invalid TopologyEvent ID");
Pavlin Radoslavov845ffdd2014-08-06 18:01:26 -0700324 }
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700325
326 //
327 // Prepare the ONOS Instance ID. The '@' separator is needed to avoid
328 // potential key collisions.
329 //
Pavlin Radoslavov845ffdd2014-08-06 18:01:26 -0700330 byte[] onosId =
331 ("@" + onosInstanceId.toString()).getBytes(StandardCharsets.UTF_8);
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700332
333 // Concatenate the IDs
334 ByteBuffer buf =
335 ByteBuffer.allocate(eventId.capacity() + onosId.length);
336 buf.put(eventId);
337 buf.put(onosId);
338 buf.flip();
339 return buf;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800340 }
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800341}