blob: 2df7114dfc2a33c5012610beb9ed264b72a2358a [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 */
53 protected 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 */
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -070065 TopologyEvent(MastershipData mastershipData,
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070066 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 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070078 TopologyEvent(SwitchData switchData, OnosInstanceId onosInstanceId) {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -070079 this.eventType = Type.SWITCH;
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070080 this.event = checkNotNull(switchData);
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -070081 this.onosInstanceId = checkNotNull(onosInstanceId);
82 }
83
84 /**
85 * Constructor for given Port event.
86 *
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070087 * @param portData the Port event to use.
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -070088 * @param onosInstanceId the ONOS Instance ID that originates the event.
89 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070090 TopologyEvent(PortData portData, OnosInstanceId onosInstanceId) {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -070091 this.eventType = Type.PORT;
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070092 this.event = checkNotNull(portData);
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -070093 this.onosInstanceId = checkNotNull(onosInstanceId);
94 }
95
96 /**
97 * Constructor for given Link event.
98 *
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070099 * @param linkData the Link event to use.
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700100 * @param onosInstanceId the ONOS Instance ID that originates the event.
101 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700102 TopologyEvent(LinkData linkData, OnosInstanceId onosInstanceId) {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700103 this.eventType = Type.LINK;
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700104 this.event = checkNotNull(linkData);
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700105 this.onosInstanceId = checkNotNull(onosInstanceId);
106 }
107
108 /**
109 * Constructor for given Host event.
110 *
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700111 * @param hostData the Host event to use.
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700112 * @param onosInstanceId the ONOS Instance ID that originates the event.
113 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700114 TopologyEvent(HostData hostData, OnosInstanceId onosInstanceId) {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700115 this.eventType = Type.HOST;
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700116 this.event = checkNotNull(hostData);
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700117 this.onosInstanceId = checkNotNull(onosInstanceId);
118 }
119
120 /**
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700121 * Gets the Topology Event type.
122 *
123 * @return the Topology Event type.
124 */
125 TopologyEvent.Type getEventType() {
126 return this.eventType;
127 }
128
129 /**
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700130 * Gets the Mastership event.
131 *
132 * @return the Mastership event.
133 */
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -0700134 public MastershipData getMastershipData() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700135 if (eventType != Type.MASTERSHIP) {
136 return null;
137 }
Yuta HIGUCHId8fd2f52014-09-01 23:19:45 -0700138 MastershipData mastershipData = (MastershipData) event;
139 return mastershipData;
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700140 }
141
142 /**
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700143 * Gets the Switch event.
144 *
145 * @return the Switch event.
146 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700147 public SwitchData getSwitchData() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700148 if (eventType != Type.SWITCH) {
149 return null;
150 }
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700151 SwitchData switchData = (SwitchData) event;
152 return switchData;
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700153 }
154
155 /**
156 * Gets the Port event.
157 *
158 * @return the Port event.
159 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700160 public PortData getPortData() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700161 if (eventType != Type.PORT) {
162 return null;
163 }
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700164 PortData portData = (PortData) event;
165 return portData;
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700166 }
167
168 /**
169 * Gets the Link event.
170 *
171 * @return the Link event.
172 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700173 public LinkData getLinkData() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700174 if (eventType != Type.LINK) {
175 return null;
176 }
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700177 LinkData linkData = (LinkData) event;
178 return linkData;
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700179 }
180
181 /**
182 * Gets the Host event.
183 *
184 * @return the Host event.
185 */
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700186 public HostData getHostData() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700187 if (eventType != Type.HOST) {
188 return null;
189 }
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700190 HostData hostData = (HostData) event;
191 return hostData;
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700192 }
193
194 /**
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700195 * Gets the ONOS Instance ID.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700196 *
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700197 * @return the ONOS Instance ID.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700198 */
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700199 public OnosInstanceId getOnosInstanceId() {
200 return onosInstanceId;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700201 }
202
203 /**
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700204 * Tests whether the event origin DPID equals the specified DPID.
205 *
206 * @param dpid the DPID to compare against.
207 * @return true if the event origin Dpid equals the specified DPID.
208 */
209 public boolean equalsOriginDpid(Dpid dpid) {
210 return dpid.equals(getOriginDpid());
211 }
212
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700213 public Dpid getOriginDpid() {
214 if (eventType == Type.NOOP) {
215 return null;
216 }
217 return event.getOriginDpid();
218 }
219
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700220 /**
Pavlin Radoslavovae7e8482014-08-11 16:58:19 -0700221 * Returns the config state of the topology element.
222 *
223 * @return the config state of the topology element.
224 */
225 public ConfigState getConfigState() {
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700226 if (eventType == Type.NOOP) {
227 return ConfigState.NOT_CONFIGURED; // Default: not configured
Pavlin Radoslavovae7e8482014-08-11 16:58:19 -0700228 }
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700229 return event.getConfigState();
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800230 }
231
232 /**
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700233 * Gets the Topology event ID as a byte array.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800234 *
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700235 * @return the Topology event ID as a byte array.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800236 */
237 public byte[] getID() {
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700238 return getIDasByteBuffer().array();
239 }
240
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700241 public ByteBuffer getIDasByteBuffer() {
242 ByteBuffer eventId = null;
243
244 //
245 // Get the Event ID
246 //
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700247 if (eventType == Type.NOOP) {
248 String id = "NO-OP";
249 eventId = ByteBuffer.wrap(id.getBytes(StandardCharsets.UTF_8));
250 } else {
251 eventId = event.getIDasByteBuffer();
Pavlin Radoslavov845ffdd2014-08-06 18:01:26 -0700252 }
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700253
254 //
255 // Prepare the ONOS Instance ID. The '@' separator is needed to avoid
256 // potential key collisions.
257 //
Pavlin Radoslavov845ffdd2014-08-06 18:01:26 -0700258 byte[] onosId =
259 ("@" + onosInstanceId.toString()).getBytes(StandardCharsets.UTF_8);
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700260
261 // Concatenate the IDs
262 ByteBuffer buf =
263 ByteBuffer.allocate(eventId.capacity() + onosId.length);
264 buf.put(eventId);
265 buf.put(onosId);
266 buf.flip();
267 return buf;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800268 }
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700269
270 @Override
271 public boolean equals(Object o) {
272 if (this == o) {
273 return true;
274 }
275 if (o == null || getClass() != o.getClass()) {
276 return false;
277 }
278
279 TopologyEvent other = (TopologyEvent) o;
280 return Objects.equals(eventType, other.eventType) &&
281 Objects.equals(event, other.event) &&
282 Objects.equals(onosInstanceId, other.onosInstanceId);
283 }
284
285 @Override
286 public int hashCode() {
287 return Objects.hash(eventType, event, onosInstanceId);
288 }
289
290 @Override
291 public String toString() {
292 String eventStr = null;
293
294 //
295 // Get the Event string
296 //
297 if (eventType == Type.NOOP) {
298 eventStr = "NO-OP";
299 } else {
300 eventStr = event.toString();
301 }
302 return "[TopologyEvent " + eventStr + " from " +
303 onosInstanceId.toString() + "]";
304 }
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800305}