Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 2 | |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 3 | import java.nio.ByteBuffer; |
| 4 | import java.nio.charset.StandardCharsets; |
weibit | cef0986 | 2014-07-11 17:05:16 -0700 | [diff] [blame] | 5 | import java.util.Objects; |
| 6 | |
Pavlin Radoslavov | dd08e8c | 2014-08-14 11:02:57 -0700 | [diff] [blame] | 7 | import net.onrc.onos.api.batchoperation.BatchOperationTarget; |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.util.Dpid; |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 9 | import net.onrc.onos.core.util.OnosInstanceId; |
| 10 | |
| 11 | import static com.google.common.base.Preconditions.checkNotNull; |
weibit | cef0986 | 2014-07-11 17:05:16 -0700 | [diff] [blame] | 12 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 13 | /** |
| 14 | * Self-contained Topology event Object |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 15 | * <p/> |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 16 | * TODO: For now the topology event contains one of the following events: |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 17 | * Switch Mastership, Switch, Port, Link, Host. In the future it will contain |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 18 | * multiple events in a single transaction. |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 19 | * TODO: This class should become immutable after its internals and usage |
| 20 | * are finalized. |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 21 | */ |
Pavlin Radoslavov | dd08e8c | 2014-08-14 11:02:57 -0700 | [diff] [blame] | 22 | public final class TopologyEvent implements BatchOperationTarget { |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 23 | private final Type eventType; |
| 24 | private final TopologyElement<?> event; |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 25 | private final OnosInstanceId onosInstanceId; // The ONOS Instance ID |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 26 | |
| 27 | /** |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 28 | * The topology event type. |
| 29 | */ |
| 30 | public enum Type { |
| 31 | MASTERSHIP, |
| 32 | SWITCH, |
| 33 | PORT, |
| 34 | LINK, |
| 35 | HOST, |
| 36 | NOOP, // TODO: temporary type; should be removed |
| 37 | } |
| 38 | |
| 39 | /** |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 40 | * Default constructor for serializer. |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 41 | */ |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 42 | @Deprecated |
| 43 | protected TopologyEvent() { |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 44 | this.eventType = Type.NOOP; |
| 45 | this.event = null; |
| 46 | this.onosInstanceId = null; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 50 | * Constructor for creating an empty (NO-OP) Topology Event. |
| 51 | * |
| 52 | * @param onosInstanceId the ONOS Instance ID that originates the event. |
| 53 | */ |
| 54 | protected TopologyEvent(OnosInstanceId onosInstanceId) { |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 55 | this.eventType = Type.NOOP; |
| 56 | this.event = null; |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 57 | this.onosInstanceId = checkNotNull(onosInstanceId); |
| 58 | } |
| 59 | |
| 60 | /** |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 61 | * Constructor for given Switch Mastership event. |
| 62 | * |
| 63 | * @param mastershipEvent the Switch Mastership event to use. |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 64 | * @param onosInstanceId the ONOS Instance ID that originates the event. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 65 | */ |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 66 | TopologyEvent(MastershipEvent mastershipEvent, |
| 67 | OnosInstanceId onosInstanceId) { |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 68 | this.eventType = Type.MASTERSHIP; |
| 69 | this.event = checkNotNull(mastershipEvent); |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 70 | this.onosInstanceId = checkNotNull(onosInstanceId); |
| 71 | } |
| 72 | |
| 73 | /** |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 74 | * Constructor for given Switch event. |
| 75 | * |
| 76 | * @param switchEvent the Switch event to use. |
| 77 | * @param onosInstanceId the ONOS Instance ID that originates the event. |
| 78 | */ |
| 79 | TopologyEvent(SwitchEvent switchEvent, OnosInstanceId onosInstanceId) { |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 80 | this.eventType = Type.SWITCH; |
| 81 | this.event = checkNotNull(switchEvent); |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 82 | this.onosInstanceId = checkNotNull(onosInstanceId); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Constructor for given Port event. |
| 87 | * |
| 88 | * @param portEvent the Port event to use. |
| 89 | * @param onosInstanceId the ONOS Instance ID that originates the event. |
| 90 | */ |
| 91 | TopologyEvent(PortEvent portEvent, OnosInstanceId onosInstanceId) { |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 92 | this.eventType = Type.PORT; |
| 93 | this.event = checkNotNull(portEvent); |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 94 | this.onosInstanceId = checkNotNull(onosInstanceId); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Constructor for given Link event. |
| 99 | * |
| 100 | * @param linkEvent the Link event to use. |
| 101 | * @param onosInstanceId the ONOS Instance ID that originates the event. |
| 102 | */ |
| 103 | TopologyEvent(LinkEvent linkEvent, OnosInstanceId onosInstanceId) { |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 104 | this.eventType = Type.LINK; |
| 105 | this.event = checkNotNull(linkEvent); |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 106 | this.onosInstanceId = checkNotNull(onosInstanceId); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Constructor for given Host event. |
| 111 | * |
| 112 | * @param hostEvent the Host event to use. |
| 113 | * @param onosInstanceId the ONOS Instance ID that originates the event. |
| 114 | */ |
| 115 | TopologyEvent(HostEvent hostEvent, OnosInstanceId onosInstanceId) { |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 116 | this.eventType = Type.HOST; |
| 117 | this.event = checkNotNull(hostEvent); |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 118 | this.onosInstanceId = checkNotNull(onosInstanceId); |
| 119 | } |
| 120 | |
| 121 | /** |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 122 | * Gets the Topology Event type. |
| 123 | * |
| 124 | * @return the Topology Event type. |
| 125 | */ |
| 126 | TopologyEvent.Type getEventType() { |
| 127 | return this.eventType; |
| 128 | } |
| 129 | |
| 130 | /** |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 131 | * Gets the Mastership event. |
| 132 | * |
| 133 | * @return the Mastership event. |
| 134 | */ |
| 135 | public MastershipEvent getMastershipEvent() { |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 136 | if (eventType != Type.MASTERSHIP) { |
| 137 | return null; |
| 138 | } |
| 139 | MastershipEvent mastershipEvent = (MastershipEvent) event; |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 140 | return mastershipEvent; |
| 141 | } |
| 142 | |
| 143 | /** |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 144 | * Gets the Switch event. |
| 145 | * |
| 146 | * @return the Switch event. |
| 147 | */ |
| 148 | public SwitchEvent getSwitchEvent() { |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 149 | if (eventType != Type.SWITCH) { |
| 150 | return null; |
| 151 | } |
| 152 | SwitchEvent switchEvent = (SwitchEvent) event; |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 153 | return switchEvent; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Gets the Port event. |
| 158 | * |
| 159 | * @return the Port event. |
| 160 | */ |
| 161 | public PortEvent getPortEvent() { |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 162 | if (eventType != Type.PORT) { |
| 163 | return null; |
| 164 | } |
| 165 | PortEvent portEvent = (PortEvent) event; |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 166 | return portEvent; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Gets the Link event. |
| 171 | * |
| 172 | * @return the Link event. |
| 173 | */ |
| 174 | public LinkEvent getLinkEvent() { |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 175 | if (eventType != Type.LINK) { |
| 176 | return null; |
| 177 | } |
| 178 | LinkEvent linkEvent = (LinkEvent) event; |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 179 | return linkEvent; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Gets the Host event. |
| 184 | * |
| 185 | * @return the Host event. |
| 186 | */ |
| 187 | public HostEvent getHostEvent() { |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 188 | if (eventType != Type.HOST) { |
| 189 | return null; |
| 190 | } |
| 191 | HostEvent hostEvent = (HostEvent) event; |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 192 | return hostEvent; |
| 193 | } |
| 194 | |
| 195 | /** |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 196 | * Gets the ONOS Instance ID. |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 197 | * |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 198 | * @return the ONOS Instance ID. |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 199 | */ |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 200 | public OnosInstanceId getOnosInstanceId() { |
| 201 | return onosInstanceId; |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 205 | * 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 Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 214 | public Dpid getOriginDpid() { |
| 215 | if (eventType == Type.NOOP) { |
| 216 | return null; |
| 217 | } |
| 218 | return event.getOriginDpid(); |
| 219 | } |
| 220 | |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 221 | /** |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 222 | * Returns the config state of the topology element. |
| 223 | * |
| 224 | * @return the config state of the topology element. |
| 225 | */ |
| 226 | public ConfigState getConfigState() { |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 227 | if (eventType == Type.NOOP) { |
| 228 | return ConfigState.NOT_CONFIGURED; // Default: not configured |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 229 | } |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 230 | return event.getConfigState(); |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 234 | * Gets the Topology event ID as a byte array. |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 235 | * |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 236 | * @return the Topology event ID as a byte array. |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 237 | */ |
| 238 | public byte[] getID() { |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 239 | return getIDasByteBuffer().array(); |
| 240 | } |
| 241 | |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 242 | public ByteBuffer getIDasByteBuffer() { |
| 243 | ByteBuffer eventId = null; |
| 244 | |
| 245 | // |
| 246 | // Get the Event ID |
| 247 | // |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 248 | 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 Radoslavov | 845ffdd | 2014-08-06 18:01:26 -0700 | [diff] [blame] | 253 | } |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 254 | |
| 255 | // |
| 256 | // Prepare the ONOS Instance ID. The '@' separator is needed to avoid |
| 257 | // potential key collisions. |
| 258 | // |
Pavlin Radoslavov | 845ffdd | 2014-08-06 18:01:26 -0700 | [diff] [blame] | 259 | byte[] onosId = |
| 260 | ("@" + onosInstanceId.toString()).getBytes(StandardCharsets.UTF_8); |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 261 | |
| 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 Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 269 | } |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 270 | |
| 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 Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 306 | } |