Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
| 2 | |
| 3 | import java.nio.ByteBuffer; |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 4 | import java.util.Collections; |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 5 | import java.util.HashMap; |
| 6 | import java.util.LinkedList; |
| 7 | import java.util.List; |
| 8 | import java.util.Map; |
| 9 | |
| 10 | import net.floodlightcontroller.core.IFloodlightProviderService.Role; |
| 11 | import net.onrc.onos.core.registry.IControllerRegistryService; |
| 12 | import net.onrc.onos.core.registry.RegistryException; |
| 13 | import net.onrc.onos.core.util.Dpid; |
| 14 | import net.onrc.onos.core.util.EventEntry; |
| 15 | import net.onrc.onos.core.util.OnosInstanceId; |
| 16 | |
| 17 | import static com.google.common.base.Preconditions.checkNotNull; |
| 18 | |
| 19 | import org.slf4j.Logger; |
| 20 | import org.slf4j.LoggerFactory; |
| 21 | |
| 22 | /** |
| 23 | * Topology Event pre-processor. It is used by the Topology Manager for |
| 24 | * pre-processing Topology events before applying them to the Topology. |
| 25 | * <p/> |
| 26 | * The pre-processor itself keeps internal state about the most recent |
| 27 | * ADD events. It also might keep state about reordered events that cannot |
| 28 | * be applied. |
| 29 | * <p/> |
| 30 | * As part of the pre-processing logic, a previously suppressed event might |
| 31 | * be genenerated later because of some other event. |
| 32 | */ |
| 33 | public class TopologyEventPreprocessor { |
| 34 | private static final Logger log = LoggerFactory |
| 35 | .getLogger(TopologyEventPreprocessor.class); |
| 36 | private final IControllerRegistryService registryService; |
| 37 | |
| 38 | // |
| 39 | // Reordered ADD events that need to be reapplied |
| 40 | // |
| 41 | // TODO: For now, this field is accessed by the TopologyManager as well |
| 42 | // This should be refactored, and change them to private. |
| 43 | // |
| 44 | Map<ByteBuffer, TopologyEvent> reorderedEvents = new HashMap<>(); |
| 45 | |
| 46 | // |
| 47 | // Topology ADD event state per ONOS instance |
| 48 | // |
| 49 | private Map<OnosInstanceId, OnosInstanceLastAddEvents> instanceState = |
| 50 | new HashMap<>(); |
| 51 | |
| 52 | // |
| 53 | // Switch mastership state (updated by the topology events) |
| 54 | // |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 55 | private Map<Dpid, OnosInstanceId> switchMastership = new HashMap<>(); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * Constructor for a given Registry Service. |
| 59 | * |
| 60 | * @param registryService the Registry Service to use. |
| 61 | */ |
| 62 | TopologyEventPreprocessor(IControllerRegistryService registryService) { |
| 63 | this.registryService = registryService; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Class to store the last ADD Topology Events per ONOS Instance. |
| 68 | */ |
| 69 | private final class OnosInstanceLastAddEvents { |
| 70 | private final OnosInstanceId onosInstanceId; |
| 71 | |
| 72 | // The last ADD events received from this ONOS instance |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 73 | private Map<ByteBuffer, TopologyEvent> topologyEvents = new HashMap<>(); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Constructor for a given ONOS Instance ID. |
| 77 | * |
| 78 | * @param onosInstanceId the ONOS Instance ID. |
| 79 | */ |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 80 | private OnosInstanceLastAddEvents(OnosInstanceId onosInstanceId) { |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 81 | this.onosInstanceId = checkNotNull(onosInstanceId); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Processes an event originated by this ONOS instance. |
| 86 | * |
| 87 | * @param event the event to process. |
| 88 | * @return true if the event should be applied to the final Topology |
| 89 | * as well, otherwise false. |
| 90 | */ |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 91 | private boolean processEvent(EventEntry<TopologyEvent> event) { |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 92 | TopologyEvent topologyEvent = event.eventData(); |
| 93 | ByteBuffer id = topologyEvent.getIDasByteBuffer(); |
| 94 | OnosInstanceId masterId = null; |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 95 | boolean isConfigured = false; |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 96 | |
| 97 | // Get the Master of the Origin DPID |
| 98 | Dpid dpid = topologyEvent.getOriginDpid(); |
| 99 | if (dpid != null) { |
| 100 | masterId = switchMastership.get(dpid); |
| 101 | } |
| 102 | |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 103 | if (topologyEvent.getConfigState() == ConfigState.CONFIGURED) { |
| 104 | isConfigured = true; |
| 105 | } |
| 106 | |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 107 | // |
| 108 | // Apply the event based on its type |
| 109 | // |
| 110 | switch (event.eventType()) { |
| 111 | case ENTRY_ADD: |
| 112 | topologyEvents.put(id, topologyEvent); |
| 113 | reorderedEvents.remove(id); |
| 114 | // Allow the ADD only if the event was originated by the Master |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 115 | return isConfigured || onosInstanceId.equals(masterId); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 116 | |
| 117 | case ENTRY_REMOVE: |
| 118 | reorderedEvents.remove(id); |
| 119 | // Don't allow the REMOVE event if there was no ADD before |
| 120 | if (topologyEvents.remove(id) == null) { |
| 121 | return false; |
| 122 | } |
| 123 | // |
| 124 | // Allow the REMOVE if the event was originated by the Master, |
| 125 | // or there is no Master at all. |
| 126 | // |
| 127 | if (masterId == null) { |
| 128 | return true; |
| 129 | } |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 130 | return isConfigured || onosInstanceId.equals(masterId); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 131 | |
| 132 | default: |
| 133 | log.error("Unknown topology event {}", event.eventType()); |
| 134 | } |
| 135 | |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Gets the postponed events for a given DPID. |
| 141 | * Those are the events that couldn't be applied earlier to the |
| 142 | * Topology, because the ONOS Instance originating the events |
| 143 | * was not the Master for the Switch. |
| 144 | * |
| 145 | * @param dpid the DPID to use. |
| 146 | * @return a list of postponed events for the given DPID. |
| 147 | */ |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 148 | private List<EventEntry<TopologyEvent>> getPostponedEvents(Dpid dpid) { |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 149 | List<EventEntry<TopologyEvent>> result = new LinkedList<>(); |
| 150 | |
| 151 | // |
| 152 | // Search all events, and keep only those that match the DPID |
| 153 | // |
| 154 | // TODO: This could be slow, and the code should be optimized |
| 155 | // for speed. The processing complexity is O(N*N) where N is |
| 156 | // the number of Switches: for each Switch Mastership we call |
| 157 | // getPostponedEvents(), and then for each call we |
| 158 | // search all previously added events. |
| 159 | // The code can be optimized by adding additional lookup map: |
| 160 | // Dpid -> List<TopologyEvent> |
| 161 | // |
| 162 | for (TopologyEvent te : topologyEvents.values()) { |
| 163 | if (dpid.equals(te.getOriginDpid())) { |
| 164 | result.add(new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, te)); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return result; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Extracts previously reordered events that should be applied again |
| 174 | * to the Topology. |
| 175 | * |
| 176 | * @return a list of previously reordered events. |
| 177 | */ |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 178 | private List<EventEntry<TopologyEvent>> extractReorderedEvents() { |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 179 | List<EventEntry<TopologyEvent>> result = new LinkedList<>(); |
| 180 | |
| 181 | // |
| 182 | // Search all previously reordered events, and extract only if |
| 183 | // the originator is the Master. |
| 184 | // |
| 185 | List<TopologyEvent> leftoverEvents = new LinkedList<>(); |
| 186 | for (TopologyEvent te : reorderedEvents.values()) { |
| 187 | Dpid dpid = te.getOriginDpid(); |
| 188 | OnosInstanceId masterId = null; |
| 189 | if (dpid != null) { |
| 190 | masterId = switchMastership.get(dpid); |
| 191 | } |
| 192 | if (te.getOnosInstanceId().equals(masterId)) { |
| 193 | result.add(new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, te)); |
| 194 | } else { |
| 195 | leftoverEvents.add(te); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // |
| 200 | // Add back the leftover events |
| 201 | // |
| 202 | reorderedEvents.clear(); |
| 203 | for (TopologyEvent te : leftoverEvents) { |
| 204 | reorderedEvents.put(te.getIDasByteBuffer(), te); |
| 205 | } |
| 206 | |
| 207 | return result; |
| 208 | } |
| 209 | |
| 210 | /** |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 211 | * Processes a Mastership Event. |
| 212 | * |
| 213 | * @param instance the ONOS instance state to use. |
| 214 | * @param event the event to process. |
| 215 | * @return a list of postponed events if the processed event is a |
| 216 | * Mastership Event for a new Master, otherwise an empty list. |
| 217 | */ |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 218 | private List<EventEntry<TopologyEvent>> processMastershipData( |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 219 | OnosInstanceLastAddEvents instance, |
| 220 | EventEntry<TopologyEvent> event) { |
| 221 | TopologyEvent topologyEvent = event.eventData(); |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 222 | MastershipData mastershipData = topologyEvent.getMastershipData(); |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 223 | |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 224 | if (mastershipData == null) { |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 225 | return Collections.emptyList(); // Not a Mastership Event |
| 226 | } |
| 227 | |
| 228 | OnosInstanceId onosInstanceId = topologyEvent.getOnosInstanceId(); |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 229 | Dpid dpid = mastershipData.getDpid(); |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 230 | boolean newMaster = false; |
| 231 | |
| 232 | // |
| 233 | // Update the Switch Mastership state: |
| 234 | // - If ADD a MASTER and the Mastership is confirmed by the |
| 235 | // Registry Service, or if the ADD is explicitly CONFIGURED, |
| 236 | // then add to the Mastership map and fetch the postponed |
| 237 | // events from the originating ONOS Instance. |
| 238 | // - Otherwise, remove from the Mastership map, but only if it is |
| 239 | // the current MASTER. |
| 240 | // |
| 241 | if ((event.eventType() == EventEntry.Type.ENTRY_ADD) && |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 242 | (mastershipData.getRole() == Role.MASTER)) { |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 243 | |
| 244 | // |
| 245 | // Accept if explicitly configured, otherwise check |
| 246 | // with the Registry Service. |
| 247 | // |
| 248 | if (topologyEvent.getConfigState() == ConfigState.CONFIGURED) { |
| 249 | newMaster = true; |
| 250 | } else { |
| 251 | // |
| 252 | // Check with the Registry Service as well |
| 253 | // |
| 254 | try { |
| 255 | String rc = |
| 256 | registryService.getControllerForSwitch(dpid.value()); |
| 257 | if ((rc != null) && |
| 258 | onosInstanceId.equals(new OnosInstanceId(rc))) { |
| 259 | newMaster = true; |
| 260 | } |
| 261 | } catch (RegistryException e) { |
| 262 | log.error("Caught RegistryException while pre-processing Mastership Event", e); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | if (newMaster) { |
| 268 | // Add to the map |
| 269 | switchMastership.put(dpid, onosInstanceId); |
| 270 | return instance.getPostponedEvents(dpid); |
| 271 | } |
| 272 | |
| 273 | // Not a Master: eventually remove from the map |
| 274 | OnosInstanceId oldId = switchMastership.get(dpid); |
| 275 | if (onosInstanceId.equals(oldId)) { |
| 276 | switchMastership.remove(dpid); |
| 277 | } |
| 278 | return Collections.emptyList(); |
| 279 | } |
| 280 | |
| 281 | /** |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 282 | * Pre-processes a list of events. |
| 283 | * |
| 284 | * @param events the events to pre-process. |
| 285 | * @return a list of pre-processed events. |
| 286 | */ |
| 287 | List<EventEntry<TopologyEvent>> processEvents( |
| 288 | List<EventEntry<TopologyEvent>> events) { |
| 289 | List<EventEntry<TopologyEvent>> result = new LinkedList<>(); |
| 290 | |
| 291 | // |
| 292 | // Process the events |
| 293 | // |
| 294 | for (EventEntry<TopologyEvent> event : events) { |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 295 | List<EventEntry<TopologyEvent>> postponedEvents; |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 296 | |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 297 | // Ignore NO-OP events |
| 298 | if (event.isNoop()) { |
| 299 | continue; |
| 300 | } |
| 301 | |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 302 | TopologyEvent topologyEvent = event.eventData(); |
| 303 | OnosInstanceId onosInstanceId = topologyEvent.getOnosInstanceId(); |
| 304 | |
| 305 | log.debug("Topology event {}: {}", event.eventType(), |
| 306 | topologyEvent); |
| 307 | |
| 308 | // Get the ONOS instance state |
| 309 | OnosInstanceLastAddEvents instance = |
| 310 | instanceState.get(onosInstanceId); |
| 311 | if (instance == null) { |
| 312 | instance = new OnosInstanceLastAddEvents(onosInstanceId); |
| 313 | instanceState.put(onosInstanceId, instance); |
| 314 | } |
| 315 | |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 316 | postponedEvents = processMastershipData(instance, event); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 317 | |
| 318 | // |
| 319 | // Process the event and eventually store it in the |
| 320 | // per-Instance state. |
| 321 | // |
| 322 | if (instance.processEvent(event)) { |
| 323 | result.add(event); |
| 324 | } |
| 325 | |
| 326 | // Add the postponed events (if any) |
Pavlin Radoslavov | ae7e848 | 2014-08-11 16:58:19 -0700 | [diff] [blame] | 327 | result.addAll(postponedEvents); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | // Extract and add the previously reordered events |
| 331 | result.addAll(extractReorderedEvents()); |
| 332 | |
| 333 | return reorderEventsForTopology(result); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Classifies and reorders a list of events, and suppresses matching |
| 338 | * events. |
| 339 | * <p/> |
| 340 | * The result events can be applied to the Topology in the following |
| 341 | * order: REMOVE events followed by ADD events. The ADD events are in the |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 342 | * natural order to build a Topology: MastershipData, SwitchData, |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 343 | * PortData, LinkData, HostData. The REMOVE events are in the reverse |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 344 | * order. |
| 345 | * |
| 346 | * @param events the events to classify and reorder. |
| 347 | * @return the classified and reordered events. |
| 348 | */ |
| 349 | private List<EventEntry<TopologyEvent>> reorderEventsForTopology( |
| 350 | List<EventEntry<TopologyEvent>> events) { |
| 351 | // Local state for computing the final set of events |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 352 | Map<ByteBuffer, EventEntry<TopologyEvent>> addedMastershipDataEntries = |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 353 | new HashMap<>(); |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 354 | Map<ByteBuffer, EventEntry<TopologyEvent>> removedMastershipDataEntries = |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 355 | new HashMap<>(); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 356 | Map<ByteBuffer, EventEntry<TopologyEvent>> addedSwitchDataEntries = |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 357 | new HashMap<>(); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 358 | Map<ByteBuffer, EventEntry<TopologyEvent>> removedSwitchDataEntries = |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 359 | new HashMap<>(); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 360 | Map<ByteBuffer, EventEntry<TopologyEvent>> addedPortDataEntries = |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 361 | new HashMap<>(); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 362 | Map<ByteBuffer, EventEntry<TopologyEvent>> removedPortDataEntries = |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 363 | new HashMap<>(); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 364 | Map<ByteBuffer, EventEntry<TopologyEvent>> addedLinkDataEntries = |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 365 | new HashMap<>(); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 366 | Map<ByteBuffer, EventEntry<TopologyEvent>> removedLinkDataEntries = |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 367 | new HashMap<>(); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 368 | Map<ByteBuffer, EventEntry<TopologyEvent>> addedHostDataEntries = |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 369 | new HashMap<>(); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 370 | Map<ByteBuffer, EventEntry<TopologyEvent>> removedHostDataEntries = |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 371 | new HashMap<>(); |
| 372 | |
| 373 | // |
| 374 | // Classify and suppress matching events |
| 375 | // |
| 376 | // NOTE: We intentionally use the event payload as the key ID |
| 377 | // (i.e., we exclude the ONOS Instance ID from the key), |
| 378 | // so we can suppress transient events across multiple ONOS instances. |
| 379 | // |
| 380 | for (EventEntry<TopologyEvent> event : events) { |
| 381 | TopologyEvent topologyEvent = event.eventData(); |
| 382 | |
| 383 | // Get the event itself |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 384 | MastershipData mastershipData = |
| 385 | topologyEvent.getMastershipData(); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 386 | SwitchData switchData = topologyEvent.getSwitchData(); |
| 387 | PortData portData = topologyEvent.getPortData(); |
| 388 | LinkData linkData = topologyEvent.getLinkData(); |
| 389 | HostData hostData = topologyEvent.getHostData(); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 390 | |
| 391 | // |
| 392 | // Extract the events |
| 393 | // |
| 394 | switch (event.eventType()) { |
| 395 | case ENTRY_ADD: |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 396 | if (mastershipData != null) { |
| 397 | ByteBuffer id = mastershipData.getIDasByteBuffer(); |
| 398 | addedMastershipDataEntries.put(id, event); |
| 399 | removedMastershipDataEntries.remove(id); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 400 | } |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 401 | if (switchData != null) { |
| 402 | ByteBuffer id = switchData.getIDasByteBuffer(); |
| 403 | addedSwitchDataEntries.put(id, event); |
| 404 | removedSwitchDataEntries.remove(id); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 405 | } |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 406 | if (portData != null) { |
| 407 | ByteBuffer id = portData.getIDasByteBuffer(); |
| 408 | addedPortDataEntries.put(id, event); |
| 409 | removedPortDataEntries.remove(id); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 410 | } |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 411 | if (linkData != null) { |
| 412 | ByteBuffer id = linkData.getIDasByteBuffer(); |
| 413 | addedLinkDataEntries.put(id, event); |
| 414 | removedLinkDataEntries.remove(id); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 415 | } |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 416 | if (hostData != null) { |
| 417 | ByteBuffer id = hostData.getIDasByteBuffer(); |
| 418 | addedHostDataEntries.put(id, event); |
| 419 | removedHostDataEntries.remove(id); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 420 | } |
| 421 | break; |
| 422 | case ENTRY_REMOVE: |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 423 | if (mastershipData != null) { |
| 424 | ByteBuffer id = mastershipData.getIDasByteBuffer(); |
| 425 | addedMastershipDataEntries.remove(id); |
| 426 | removedMastershipDataEntries.put(id, event); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 427 | } |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 428 | if (switchData != null) { |
| 429 | ByteBuffer id = switchData.getIDasByteBuffer(); |
| 430 | addedSwitchDataEntries.remove(id); |
| 431 | removedSwitchDataEntries.put(id, event); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 432 | } |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 433 | if (portData != null) { |
| 434 | ByteBuffer id = portData.getIDasByteBuffer(); |
| 435 | addedPortDataEntries.remove(id); |
| 436 | removedPortDataEntries.put(id, event); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 437 | } |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 438 | if (linkData != null) { |
| 439 | ByteBuffer id = linkData.getIDasByteBuffer(); |
| 440 | addedLinkDataEntries.remove(id); |
| 441 | removedLinkDataEntries.put(id, event); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 442 | } |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 443 | if (hostData != null) { |
| 444 | ByteBuffer id = hostData.getIDasByteBuffer(); |
| 445 | addedHostDataEntries.remove(id); |
| 446 | removedHostDataEntries.put(id, event); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 447 | } |
| 448 | break; |
| 449 | default: |
| 450 | log.error("Unknown topology event {}", event.eventType()); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | // |
| 455 | // Prepare the result by adding the events in the appropriate order: |
| 456 | // - First REMOVE, then ADD |
| 457 | // - The REMOVE order is: Host, Link, Port, Switch, Mastership |
| 458 | // - The ADD order is the reverse: Mastership, Switch, Port, Link, |
| 459 | // Host |
| 460 | // |
| 461 | List<EventEntry<TopologyEvent>> result = new LinkedList<>(); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 462 | result.addAll(removedHostDataEntries.values()); |
| 463 | result.addAll(removedLinkDataEntries.values()); |
| 464 | result.addAll(removedPortDataEntries.values()); |
| 465 | result.addAll(removedSwitchDataEntries.values()); |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 466 | result.addAll(removedMastershipDataEntries.values()); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 467 | // |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 468 | result.addAll(addedMastershipDataEntries.values()); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 469 | result.addAll(addedSwitchDataEntries.values()); |
| 470 | result.addAll(addedPortDataEntries.values()); |
| 471 | result.addAll(addedLinkDataEntries.values()); |
| 472 | result.addAll(addedHostDataEntries.values()); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 473 | |
| 474 | return result; |
| 475 | } |
| 476 | } |