blob: df38b5e41dc7804ab4b2cc7caa6e85f9073daf30 [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
Pavlin Radoslavova5637c02014-07-30 15:55:11 -07005import net.onrc.onos.core.util.OnosInstanceId;
6
7import static com.google.common.base.Preconditions.checkNotNull;
weibitcef09862014-07-11 17:05:16 -07008
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -08009/**
10 * Self-contained Topology event Object
Ray Milkey269ffb92014-04-03 14:43:30 -070011 * <p/>
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080012 * TODO: For now the topology event contains one of the following events:
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070013 * Switch, Port, Link, Host, Switch Mastership. In the future it will contain
14 * multiple events in a single transaction.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070015 * TODO: This class should become immutable after its internals and usage
16 * are finalized.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080017 */
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070018public final class TopologyEvent {
19 private final SwitchEvent switchEvent; // Set for Switch event
20 private final PortEvent portEvent; // Set for Port event
21 private final LinkEvent linkEvent; // Set for Link event
22 private final HostEvent hostEvent; // Set for Host event
23 private final MastershipEvent mastershipEvent; // Set for Mastership event
24 private final OnosInstanceId onosInstanceId; // The ONOS Instance ID
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080025
26 /**
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070027 * Default constructor for serializer.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080028 */
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070029 @Deprecated
30 protected TopologyEvent() {
31 switchEvent = null;
32 portEvent = null;
33 linkEvent = null;
34 hostEvent = null;
35 mastershipEvent = null;
36 onosInstanceId = null;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080037 }
38
39 /**
40 * Constructor for given Switch event.
41 *
42 * @param switchEvent the Switch event to use.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070043 * @param onosInstanceId the ONOS Instance ID that originates the event.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080044 */
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070045 TopologyEvent(SwitchEvent switchEvent, OnosInstanceId onosInstanceId) {
46 this.switchEvent = checkNotNull(switchEvent);
47 this.portEvent = null;
48 this.linkEvent = null;
49 this.hostEvent = null;
50 this.mastershipEvent = null;
51 this.onosInstanceId = checkNotNull(onosInstanceId);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080052 }
53
54 /**
55 * Constructor for given Port event.
56 *
57 * @param portEvent the Port event to use.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070058 * @param onosInstanceId the ONOS Instance ID that originates the event.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080059 */
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070060 TopologyEvent(PortEvent portEvent, OnosInstanceId onosInstanceId) {
61 this.switchEvent = null;
62 this.portEvent = checkNotNull(portEvent);
63 this.linkEvent = null;
64 this.hostEvent = null;
65 this.mastershipEvent = null;
66 this.onosInstanceId = checkNotNull(onosInstanceId);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080067 }
68
69 /**
70 * Constructor for given Link event.
71 *
72 * @param linkEvent the Link event to use.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070073 * @param onosInstanceId the ONOS Instance ID that originates the event.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080074 */
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070075 TopologyEvent(LinkEvent linkEvent, OnosInstanceId onosInstanceId) {
76 this.switchEvent = null;
77 this.portEvent = null;
78 this.linkEvent = checkNotNull(linkEvent);
79 this.hostEvent = null;
80 this.mastershipEvent = null;
81 this.onosInstanceId = checkNotNull(onosInstanceId);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080082 }
83
84 /**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070085 * Constructor for given Host event.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080086 *
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070087 * @param hostEvent the Host event to use.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070088 * @param onosInstanceId the ONOS Instance ID that originates the event.
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080089 */
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070090 TopologyEvent(HostEvent hostEvent, OnosInstanceId onosInstanceId) {
91 this.switchEvent = null;
92 this.portEvent = null;
93 this.linkEvent = null;
94 this.hostEvent = checkNotNull(hostEvent);
95 this.mastershipEvent = null;
96 this.onosInstanceId = checkNotNull(onosInstanceId);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080097 }
98
99 /**
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700100 * Constructor for given Switch Mastership event.
101 *
102 * @param mastershipEvent the Switch Mastership event to use.
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700103 * @param onosInstanceId the ONOS Instance ID that originates the event.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700104 */
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700105 TopologyEvent(MastershipEvent mastershipEvent,
106 OnosInstanceId onosInstanceId) {
107 this.switchEvent = null;
108 this.portEvent = null;
109 this.linkEvent = null;
110 this.hostEvent = null;
111 this.mastershipEvent = checkNotNull(mastershipEvent);
112 this.onosInstanceId = checkNotNull(onosInstanceId);
113 }
114
115 /**
116 * Gets the Switch event.
117 *
118 * @return the Switch event.
119 */
120 public SwitchEvent getSwitchEvent() {
121 return switchEvent;
122 }
123
124 /**
125 * Gets the Port event.
126 *
127 * @return the Port event.
128 */
129 public PortEvent getPortEvent() {
130 return portEvent;
131 }
132
133 /**
134 * Gets the Link event.
135 *
136 * @return the Link event.
137 */
138 public LinkEvent getLinkEvent() {
139 return linkEvent;
140 }
141
142 /**
143 * Gets the Host event.
144 *
145 * @return the Host event.
146 */
147 public HostEvent getHostEvent() {
148 return hostEvent;
149 }
150
151 /**
152 * Gets the Mastership event.
153 *
154 * @return the Mastership event.
155 */
156 public MastershipEvent getMastershipEvent() {
157 return mastershipEvent;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700158 }
159
160 /**
weibitcef09862014-07-11 17:05:16 -0700161 * Check if all events contained are equal.
Yuta HIGUCHIccab05d2014-07-26 22:42:28 -0700162 *
163 * @param obj TopologyEvent to compare against
weibitcef09862014-07-11 17:05:16 -0700164 */
165 @Override
166 public boolean equals(Object obj) {
167 if (this == obj) {
168 return true;
169 }
170
171 if (obj == null) {
172 return false;
173 }
174
175 if (getClass() != obj.getClass()) {
176 return false;
177 }
178
179 TopologyEvent other = (TopologyEvent) obj;
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700180 // TODO: For now the onosInstanceId is not used
weibitcef09862014-07-11 17:05:16 -0700181 return Objects.equals(switchEvent, other.switchEvent) &&
182 Objects.equals(portEvent, other.portEvent) &&
183 Objects.equals(linkEvent, other.linkEvent) &&
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700184 Objects.equals(hostEvent, other.hostEvent) &&
185 Objects.equals(mastershipEvent, other.mastershipEvent);
weibitcef09862014-07-11 17:05:16 -0700186 }
187
188 @Override
189 public int hashCode() {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700190 // TODO: For now the onosInstanceId is not used
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700191 return Objects.hash(switchEvent, portEvent, linkEvent, hostEvent,
192 mastershipEvent);
weibitcef09862014-07-11 17:05:16 -0700193 }
194
195 /**
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800196 * Get the string representation of the event.
197 *
198 * @return the string representation of the event.
199 */
200 @Override
201 public String toString() {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700202 // TODO: For now the onosInstanceId is not used
Ray Milkeyb29e6262014-04-09 16:02:14 -0700203 if (switchEvent != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700204 return switchEvent.toString();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700205 }
206 if (portEvent != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700207 return portEvent.toString();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700208 }
209 if (linkEvent != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700210 return linkEvent.toString();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700211 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700212 if (hostEvent != null) {
213 return hostEvent.toString();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700214 }
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700215 if (mastershipEvent != null) {
216 return mastershipEvent.toString();
217 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700218 return "[Empty TopologyEvent]";
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800219 }
220
221 /**
222 * Get the Topology event ID.
223 *
224 * @return the Topology event ID.
225 */
226 public byte[] getID() {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700227 // TODO: For now the onosInstanceId is not used
Ray Milkeyb29e6262014-04-09 16:02:14 -0700228 if (switchEvent != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700229 return switchEvent.getID();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700230 }
231 if (portEvent != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700232 return portEvent.getID();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700233 }
234 if (linkEvent != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700235 return linkEvent.getID();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700236 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700237 if (hostEvent != null) {
238 return hostEvent.getID();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700239 }
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700240 if (mastershipEvent != null) {
241 return mastershipEvent.getID();
242 }
Ray Milkey92897212014-07-21 10:33:16 -0700243 throw new IllegalStateException("Invalid TopologyEvent ID");
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800244 }
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800245}