blob: 4ea9e3137b253ab591d9c35fb6eead200c94b929 [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 Radoslavova5637c02014-07-30 15:55:11 -07007import net.onrc.onos.core.util.OnosInstanceId;
8
9import static com.google.common.base.Preconditions.checkNotNull;
weibitcef09862014-07-11 17:05:16 -070010
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080011/**
12 * Self-contained Topology event Object
Ray Milkey269ffb92014-04-03 14:43:30 -070013 * <p/>
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080014 * TODO: For now the topology event contains one of the following events:
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -070015 * Switch Mastership, Switch, Port, Link, Host. In the future it will contain
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070016 * multiple events in a single transaction.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070017 * TODO: This class should become immutable after its internals and usage
18 * are finalized.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080019 */
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070020public final class TopologyEvent {
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -070021 private final MastershipEvent mastershipEvent; // Set for Mastership event
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070022 private final SwitchEvent switchEvent; // Set for Switch event
23 private final PortEvent portEvent; // Set for Port event
24 private final LinkEvent linkEvent; // Set for Link event
25 private final HostEvent hostEvent; // Set for Host event
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070026 private final OnosInstanceId onosInstanceId; // The ONOS Instance ID
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080027
28 /**
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070029 * Default constructor for serializer.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080030 */
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070031 @Deprecated
32 protected TopologyEvent() {
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -070033 mastershipEvent = null;
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070034 switchEvent = null;
35 portEvent = null;
36 linkEvent = null;
37 hostEvent = null;
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070038 onosInstanceId = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080039 }
40
41 /**
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070042 * Constructor for given Switch Mastership event.
43 *
44 * @param mastershipEvent the Switch Mastership event to use.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070045 * @param onosInstanceId the ONOS Instance ID that originates the event.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070046 */
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070047 TopologyEvent(MastershipEvent mastershipEvent,
48 OnosInstanceId onosInstanceId) {
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -070049 this.mastershipEvent = checkNotNull(mastershipEvent);
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070050 this.switchEvent = null;
51 this.portEvent = null;
52 this.linkEvent = null;
53 this.hostEvent = null;
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070054 this.onosInstanceId = checkNotNull(onosInstanceId);
55 }
56
57 /**
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -070058 * Constructor for given Switch event.
59 *
60 * @param switchEvent the Switch event to use.
61 * @param onosInstanceId the ONOS Instance ID that originates the event.
62 */
63 TopologyEvent(SwitchEvent switchEvent, OnosInstanceId onosInstanceId) {
64 this.mastershipEvent = null;
65 this.switchEvent = checkNotNull(switchEvent);
66 this.portEvent = null;
67 this.linkEvent = null;
68 this.hostEvent = null;
69 this.onosInstanceId = checkNotNull(onosInstanceId);
70 }
71
72 /**
73 * Constructor for given Port event.
74 *
75 * @param portEvent the Port event to use.
76 * @param onosInstanceId the ONOS Instance ID that originates the event.
77 */
78 TopologyEvent(PortEvent portEvent, OnosInstanceId onosInstanceId) {
79 this.mastershipEvent = null;
80 this.switchEvent = null;
81 this.portEvent = checkNotNull(portEvent);
82 this.linkEvent = null;
83 this.hostEvent = null;
84 this.onosInstanceId = checkNotNull(onosInstanceId);
85 }
86
87 /**
88 * Constructor for given Link event.
89 *
90 * @param linkEvent the Link event to use.
91 * @param onosInstanceId the ONOS Instance ID that originates the event.
92 */
93 TopologyEvent(LinkEvent linkEvent, OnosInstanceId onosInstanceId) {
94 this.mastershipEvent = null;
95 this.switchEvent = null;
96 this.portEvent = null;
97 this.linkEvent = checkNotNull(linkEvent);
98 this.hostEvent = null;
99 this.onosInstanceId = checkNotNull(onosInstanceId);
100 }
101
102 /**
103 * Constructor for given Host event.
104 *
105 * @param hostEvent the Host event to use.
106 * @param onosInstanceId the ONOS Instance ID that originates the event.
107 */
108 TopologyEvent(HostEvent hostEvent, OnosInstanceId onosInstanceId) {
109 this.mastershipEvent = null;
110 this.switchEvent = null;
111 this.portEvent = null;
112 this.linkEvent = null;
113 this.hostEvent = checkNotNull(hostEvent);
114 this.onosInstanceId = checkNotNull(onosInstanceId);
115 }
116
117 /**
118 * Gets the Mastership event.
119 *
120 * @return the Mastership event.
121 */
122 public MastershipEvent getMastershipEvent() {
123 return mastershipEvent;
124 }
125
126 /**
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700127 * Gets the Switch event.
128 *
129 * @return the Switch event.
130 */
131 public SwitchEvent getSwitchEvent() {
132 return switchEvent;
133 }
134
135 /**
136 * Gets the Port event.
137 *
138 * @return the Port event.
139 */
140 public PortEvent getPortEvent() {
141 return portEvent;
142 }
143
144 /**
145 * Gets the Link event.
146 *
147 * @return the Link event.
148 */
149 public LinkEvent getLinkEvent() {
150 return linkEvent;
151 }
152
153 /**
154 * Gets the Host event.
155 *
156 * @return the Host event.
157 */
158 public HostEvent getHostEvent() {
159 return hostEvent;
160 }
161
162 /**
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700163 * Gets the ONOS Instance ID.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700164 *
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700165 * @return the ONOS Instance ID.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700166 */
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700167 public OnosInstanceId getOnosInstanceId() {
168 return onosInstanceId;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700169 }
170
171 /**
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700172 * Checks if all events contained are equal.
Yuta HIGUCHIccab05d2014-07-26 22:42:28 -0700173 *
174 * @param obj TopologyEvent to compare against
weibitcef09862014-07-11 17:05:16 -0700175 */
176 @Override
177 public boolean equals(Object obj) {
178 if (this == obj) {
179 return true;
180 }
181
182 if (obj == null) {
183 return false;
184 }
185
186 if (getClass() != obj.getClass()) {
187 return false;
188 }
189
190 TopologyEvent other = (TopologyEvent) obj;
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700191 return Objects.equals(mastershipEvent, other.mastershipEvent) &&
192 Objects.equals(switchEvent, other.switchEvent) &&
193 Objects.equals(portEvent, other.portEvent) &&
194 Objects.equals(linkEvent, other.linkEvent) &&
195 Objects.equals(hostEvent, other.hostEvent) &&
196 Objects.equals(onosInstanceId, other.onosInstanceId);
weibitcef09862014-07-11 17:05:16 -0700197 }
198
199 @Override
200 public int hashCode() {
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700201 return Objects.hash(mastershipEvent, switchEvent, portEvent,
202 linkEvent, hostEvent, onosInstanceId);
weibitcef09862014-07-11 17:05:16 -0700203 }
204
205 /**
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700206 * Gets the string representation of the event.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800207 *
208 * @return the string representation of the event.
209 */
210 @Override
211 public String toString() {
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700212 String eventStr = null;
213
214 //
215 // Get the Event string
216 //
217 do {
218 if (mastershipEvent != null) {
219 eventStr = mastershipEvent.toString();
220 break;
221 }
222 if (switchEvent != null) {
223 eventStr = switchEvent.toString();
224 break;
225 }
226 if (portEvent != null) {
227 eventStr = portEvent.toString();
228 break;
229 }
230 if (linkEvent != null) {
231 eventStr = linkEvent.toString();
232 break;
233 }
234 if (hostEvent != null) {
235 eventStr = hostEvent.toString();
236 break;
237 }
238 // No event found
239 return "[Empty TopologyEvent]";
240 } while (false);
241
242 return "[TopologyEvent " + eventStr + " from " +
243 onosInstanceId.toString() + "]";
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800244 }
245
246 /**
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700247 * Gets the Topology event ID as a byte array.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800248 *
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700249 * @return the Topology event ID as a byte array.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800250 */
251 public byte[] getID() {
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700252 return getIDasByteBuffer().array();
253 }
254
255 /**
256 * Gets the Topology event ID as a ByteBuffer.
257 *
258 * @return the Topology event ID as a ByteBuffer.
259 */
260 public ByteBuffer getIDasByteBuffer() {
261 ByteBuffer eventId = null;
262
263 //
264 // Get the Event ID
265 //
266 do {
267 if (mastershipEvent != null) {
268 eventId = mastershipEvent.getIDasByteBuffer();
269 break;
270 }
271 if (switchEvent != null) {
272 eventId = switchEvent.getIDasByteBuffer();
273 break;
274 }
275 if (portEvent != null) {
276 eventId = portEvent.getIDasByteBuffer();
277 break;
278 }
279 if (linkEvent != null) {
280 eventId = linkEvent.getIDasByteBuffer();
281 break;
282 }
283 if (hostEvent != null) {
284 eventId = hostEvent.getIDasByteBuffer();
285 break;
286 }
287 // No event found
288 throw new IllegalStateException("Invalid TopologyEvent ID");
289 } while (false);
290
291 //
292 // Prepare the ONOS Instance ID. The '@' separator is needed to avoid
293 // potential key collisions.
294 //
295 byte[] onosId = ("@" + onosInstanceId.toString()).getBytes(StandardCharsets.UTF_8);
296
297 // Concatenate the IDs
298 ByteBuffer buf =
299 ByteBuffer.allocate(eventId.capacity() + onosId.length);
300 buf.put(eventId);
301 buf.put(onosId);
302 buf.flip();
303 return buf;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800304 }
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800305}