blob: f30a6dac0b1a2451ae51fc59afd57a4ea9bf2dcb [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 */
Yuta HIGUCHI7c28ebb2014-08-19 00:29:30 -070021public final class TopologyEvent implements TopologyBatchTarget {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -070022 private final Type eventType;
23 private final TopologyElement<?> event;
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070024 private final OnosInstanceId onosInstanceId; // The ONOS Instance ID
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080025
26 /**
Pavlin Radoslavov31f85102014-08-15 13:55:44 -070027 * The topology event type.
28 */
29 public enum Type {
30 MASTERSHIP,
31 SWITCH,
32 PORT,
33 LINK,
34 HOST,
35 NOOP, // TODO: temporary type; should be removed
36 }
37
38 /**
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070039 * Default constructor for serializer.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080040 */
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070041 @Deprecated
42 protected TopologyEvent() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -070043 this.eventType = Type.NOOP;
44 this.event = null;
45 this.onosInstanceId = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080046 }
47
48 /**
Pavlin Radoslavov054cd592014-08-07 20:57:16 -070049 * Constructor for creating an empty (NO-OP) Topology Event.
50 *
51 * @param onosInstanceId the ONOS Instance ID that originates the event.
52 */
Pavlin Radoslavov656fdde2014-09-02 17:37:46 -070053 public TopologyEvent(OnosInstanceId onosInstanceId) {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -070054 this.eventType = Type.NOOP;
55 this.event = null;
Pavlin Radoslavov054cd592014-08-07 20:57:16 -070056 this.onosInstanceId = checkNotNull(onosInstanceId);
57 }
58
59 /**
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070060 * Constructor for given Switch Mastership event.
61 *
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -070062 * @param mastershipData the Switch Mastership event to use.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070063 * @param onosInstanceId the ONOS Instance ID that originates the event.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070064 */
Pavlin Radoslavov656fdde2014-09-02 17:37:46 -070065 public TopologyEvent(MastershipData mastershipData,
66 OnosInstanceId onosInstanceId) {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -070067 this.eventType = Type.MASTERSHIP;
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -070068 this.event = checkNotNull(mastershipData);
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070069 this.onosInstanceId = checkNotNull(onosInstanceId);
70 }
71
72 /**
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -070073 * Constructor for given Switch event.
74 *
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070075 * @param switchData the Switch event to use.
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -070076 * @param onosInstanceId the ONOS Instance ID that originates the event.
77 */
Pavlin Radoslavov656fdde2014-09-02 17:37:46 -070078 public TopologyEvent(SwitchData switchData,
79 OnosInstanceId onosInstanceId) {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -070080 this.eventType = Type.SWITCH;
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070081 this.event = checkNotNull(switchData);
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -070082 this.onosInstanceId = checkNotNull(onosInstanceId);
83 }
84
85 /**
86 * Constructor for given Port event.
87 *
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070088 * @param portData the Port event to use.
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -070089 * @param onosInstanceId the ONOS Instance ID that originates the event.
90 */
Pavlin Radoslavov656fdde2014-09-02 17:37:46 -070091 public TopologyEvent(PortData portData, OnosInstanceId onosInstanceId) {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -070092 this.eventType = Type.PORT;
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070093 this.event = checkNotNull(portData);
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -070094 this.onosInstanceId = checkNotNull(onosInstanceId);
95 }
96
97 /**
98 * Constructor for given Link event.
99 *
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700100 * @param linkData the Link event to use.
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700101 * @param onosInstanceId the ONOS Instance ID that originates the event.
102 */
Pavlin Radoslavov656fdde2014-09-02 17:37:46 -0700103 public TopologyEvent(LinkData linkData, OnosInstanceId onosInstanceId) {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700104 this.eventType = Type.LINK;
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700105 this.event = checkNotNull(linkData);
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700106 this.onosInstanceId = checkNotNull(onosInstanceId);
107 }
108
109 /**
110 * Constructor for given Host event.
111 *
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700112 * @param hostData the Host event to use.
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700113 * @param onosInstanceId the ONOS Instance ID that originates the event.
114 */
Pavlin Radoslavov656fdde2014-09-02 17:37:46 -0700115 public TopologyEvent(HostData hostData, OnosInstanceId onosInstanceId) {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700116 this.eventType = Type.HOST;
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700117 this.event = checkNotNull(hostData);
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700118 this.onosInstanceId = checkNotNull(onosInstanceId);
119 }
120
121 /**
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700122 * Gets the Topology Event type.
123 *
124 * @return the Topology Event type.
125 */
Pavlin Radoslavov656fdde2014-09-02 17:37:46 -0700126 public TopologyEvent.Type getEventType() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700127 return this.eventType;
128 }
129
130 /**
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700131 * Gets the Mastership event.
132 *
133 * @return the Mastership event.
134 */
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -0700135 public MastershipData getMastershipData() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700136 if (eventType != Type.MASTERSHIP) {
137 return null;
138 }
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -0700139 MastershipData mastershipData = (MastershipData) event;
140 return mastershipData;
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700141 }
142
143 /**
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700144 * Gets the Switch event.
145 *
146 * @return the Switch event.
147 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700148 public SwitchData getSwitchData() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700149 if (eventType != Type.SWITCH) {
150 return null;
151 }
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700152 SwitchData switchData = (SwitchData) event;
153 return switchData;
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700154 }
155
156 /**
157 * Gets the Port event.
158 *
159 * @return the Port event.
160 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700161 public PortData getPortData() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700162 if (eventType != Type.PORT) {
163 return null;
164 }
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700165 PortData portData = (PortData) event;
166 return portData;
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700167 }
168
169 /**
170 * Gets the Link event.
171 *
172 * @return the Link event.
173 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700174 public LinkData getLinkData() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700175 if (eventType != Type.LINK) {
176 return null;
177 }
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700178 LinkData linkData = (LinkData) event;
179 return linkData;
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700180 }
181
182 /**
183 * Gets the Host event.
184 *
185 * @return the Host event.
186 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700187 public HostData getHostData() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700188 if (eventType != Type.HOST) {
189 return null;
190 }
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700191 HostData hostData = (HostData) event;
192 return hostData;
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700193 }
194
195 /**
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700196 * Gets the ONOS Instance ID.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700197 *
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700198 * @return the ONOS Instance ID.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700199 */
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700200 public OnosInstanceId getOnosInstanceId() {
201 return onosInstanceId;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700202 }
203
204 /**
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700205 * Tests whether the event origin DPID equals the specified DPID.
206 *
207 * @param dpid the DPID to compare against.
208 * @return true if the event origin Dpid equals the specified DPID.
209 */
210 public boolean equalsOriginDpid(Dpid dpid) {
211 return dpid.equals(getOriginDpid());
212 }
213
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700214 public Dpid getOriginDpid() {
215 if (eventType == Type.NOOP) {
216 return null;
217 }
218 return event.getOriginDpid();
219 }
220
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700221 /**
Pavlin Radoslavovae7e8482014-08-11 16:58:19 -0700222 * Returns the config state of the topology element.
223 *
224 * @return the config state of the topology element.
225 */
226 public ConfigState getConfigState() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700227 if (eventType == Type.NOOP) {
228 return ConfigState.NOT_CONFIGURED; // Default: not configured
Pavlin Radoslavovae7e8482014-08-11 16:58:19 -0700229 }
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700230 return event.getConfigState();
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800231 }
232
233 /**
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700234 * Gets the Topology event ID as a byte array.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800235 *
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700236 * @return the Topology event ID as a byte array.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800237 */
238 public byte[] getID() {
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700239 return getIDasByteBuffer().array();
240 }
241
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700242 public ByteBuffer getIDasByteBuffer() {
243 ByteBuffer eventId = null;
244
245 //
246 // Get the Event ID
247 //
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700248 if (eventType == Type.NOOP) {
249 String id = "NO-OP";
250 eventId = ByteBuffer.wrap(id.getBytes(StandardCharsets.UTF_8));
251 } else {
252 eventId = event.getIDasByteBuffer();
Pavlin Radoslavov845ffdd2014-08-06 18:01:26 -0700253 }
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700254
255 //
256 // Prepare the ONOS Instance ID. The '@' separator is needed to avoid
257 // potential key collisions.
258 //
Pavlin Radoslavov845ffdd2014-08-06 18:01:26 -0700259 byte[] onosId =
260 ("@" + onosInstanceId.toString()).getBytes(StandardCharsets.UTF_8);
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700261
262 // Concatenate the IDs
263 ByteBuffer buf =
264 ByteBuffer.allocate(eventId.capacity() + onosId.length);
265 buf.put(eventId);
266 buf.put(onosId);
267 buf.flip();
268 return buf;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800269 }
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700270
271 @Override
272 public boolean equals(Object o) {
273 if (this == o) {
274 return true;
275 }
276 if (o == null || getClass() != o.getClass()) {
277 return false;
278 }
279
280 TopologyEvent other = (TopologyEvent) o;
281 return Objects.equals(eventType, other.eventType) &&
282 Objects.equals(event, other.event) &&
283 Objects.equals(onosInstanceId, other.onosInstanceId);
284 }
285
286 @Override
287 public int hashCode() {
288 return Objects.hash(eventType, event, onosInstanceId);
289 }
290
291 @Override
292 public String toString() {
293 String eventStr = null;
294
295 //
296 // Get the Event string
297 //
298 if (eventType == Type.NOOP) {
299 eventStr = "NO-OP";
300 } else {
301 eventStr = event.toString();
302 }
303 return "[TopologyEvent " + eventStr + " from " +
304 onosInstanceId.toString() + "]";
305 }
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800306}