blob: 81e261d76a3fc93a7fa3f953cdab0d675a81bd68 [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -08002
weibitcef09862014-07-11 17:05:16 -07003import java.util.Objects;
4
5
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -08006/**
7 * Self-contained Topology event Object
Ray Milkey269ffb92014-04-03 14:43:30 -07008 * <p/>
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -08009 * TODO: For now the topology event contains one of the following events:
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070010 * Switch, Port, Link, Host. In the future it will contain multiple events
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080011 * in a single transaction.
12 */
13public class TopologyEvent {
Ray Milkey269ffb92014-04-03 14:43:30 -070014 SwitchEvent switchEvent = null; // Set for Switch event
15 PortEvent portEvent = null; // Set for Port event
16 LinkEvent linkEvent = null; // Set for Link event
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070017 HostEvent hostEvent = null; // Set for Host event
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080018
19 /**
20 * Default constructor.
21 */
22 public TopologyEvent() {
23 }
24
25 /**
26 * Constructor for given Switch event.
27 *
28 * @param switchEvent the Switch event to use.
29 */
Pavlin Radoslavov87dcc262014-02-19 21:13:23 -080030 TopologyEvent(SwitchEvent switchEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -070031 this.switchEvent = switchEvent;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080032 }
33
34 /**
35 * Constructor for given Port event.
36 *
37 * @param portEvent the Port event to use.
38 */
Pavlin Radoslavov87dcc262014-02-19 21:13:23 -080039 TopologyEvent(PortEvent portEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -070040 this.portEvent = portEvent;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080041 }
42
43 /**
44 * Constructor for given Link event.
45 *
46 * @param linkEvent the Link event to use.
47 */
Pavlin Radoslavov87dcc262014-02-19 21:13:23 -080048 TopologyEvent(LinkEvent linkEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -070049 this.linkEvent = linkEvent;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080050 }
51
52 /**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070053 * Constructor for given Host event.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080054 *
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070055 * @param hostEvent the Host event to use.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080056 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070057 TopologyEvent(HostEvent hostEvent) {
58 this.hostEvent = hostEvent;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080059 }
60
61 /**
weibitcef09862014-07-11 17:05:16 -070062 * Check if all events contained are equal.
Yuta HIGUCHIccab05d2014-07-26 22:42:28 -070063 *
64 * @param obj TopologyEvent to compare against
weibitcef09862014-07-11 17:05:16 -070065 */
66 @Override
67 public boolean equals(Object obj) {
68 if (this == obj) {
69 return true;
70 }
71
72 if (obj == null) {
73 return false;
74 }
75
76 if (getClass() != obj.getClass()) {
77 return false;
78 }
79
80 TopologyEvent other = (TopologyEvent) obj;
81 return Objects.equals(switchEvent, other.switchEvent) &&
82 Objects.equals(portEvent, other.portEvent) &&
83 Objects.equals(linkEvent, other.linkEvent) &&
84 Objects.equals(hostEvent, other.hostEvent);
85 }
86
87 @Override
88 public int hashCode() {
89 return Objects.hash(switchEvent, portEvent, linkEvent, hostEvent);
90 }
91
92 /**
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080093 * Get the string representation of the event.
94 *
95 * @return the string representation of the event.
96 */
97 @Override
98 public String toString() {
Ray Milkeyb29e6262014-04-09 16:02:14 -070099 if (switchEvent != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 return switchEvent.toString();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700101 }
102 if (portEvent != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 return portEvent.toString();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700104 }
105 if (linkEvent != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700106 return linkEvent.toString();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700107 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700108 if (hostEvent != null) {
109 return hostEvent.toString();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700110 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 return "[Empty TopologyEvent]";
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800112 }
113
114 /**
115 * Get the Topology event ID.
116 *
117 * @return the Topology event ID.
118 */
119 public byte[] getID() {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700120 if (switchEvent != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700121 return switchEvent.getID();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700122 }
123 if (portEvent != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700124 return portEvent.getID();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700125 }
126 if (linkEvent != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700127 return linkEvent.getID();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700128 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700129 if (hostEvent != null) {
130 return hostEvent.getID();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700131 }
Ray Milkey92897212014-07-21 10:33:16 -0700132 throw new IllegalStateException("Invalid TopologyEvent ID");
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800133 }
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800134}