Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 2 | |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 3 | import java.nio.ByteBuffer; |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 4 | import java.util.ArrayList; |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 5 | import java.util.Collection; |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 6 | import java.util.HashMap; |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 7 | import java.util.HashSet; |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 8 | import java.util.LinkedList; |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 9 | import java.util.List; |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 10 | import java.util.Map; |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 11 | import java.util.Set; |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 12 | import java.util.concurrent.BlockingQueue; |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 13 | import java.util.concurrent.CopyOnWriteArrayList; |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 14 | import java.util.concurrent.LinkedBlockingQueue; |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 15 | |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 16 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 17 | import net.onrc.onos.core.datagrid.IDatagridService; |
| 18 | import net.onrc.onos.core.datagrid.IEventChannel; |
| 19 | import net.onrc.onos.core.datagrid.IEventChannelListener; |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 20 | import net.onrc.onos.core.datastore.topology.KVDevice; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 21 | import net.onrc.onos.core.datastore.topology.KVLink; |
| 22 | import net.onrc.onos.core.datastore.topology.KVPort; |
| 23 | import net.onrc.onos.core.datastore.topology.KVSwitch; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 24 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 25 | import net.onrc.onos.core.topology.PortEvent.SwitchPort; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.util.EventEntry; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 27 | |
| 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 31 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 32 | * The TopologyManager receives topology updates from the southbound discovery |
| 33 | * modules and from other ONOS instances. These updates are processed and |
| 34 | * applied to the in-memory topology instance. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 35 | * <p/> |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 36 | * - Maintain Invariant/Relationships between Topology Objects. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 37 | * <p/> |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 38 | * TODO To be synchronized based on TopologyEvent Notification. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 39 | * <p/> |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 40 | * TODO TBD: Caller is expected to maintain parent/child calling order. Parent |
Yuta HIGUCHI | 1c70010 | 2014-02-12 16:30:52 -0800 | [diff] [blame] | 41 | * Object must exist before adding sub component(Add Switch -> Port). |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 42 | * <p/> |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 43 | * TODO TBD: This class may delay the requested change to handle event |
| 44 | * re-ordering. e.g.) Link Add came in, but Switch was not there. |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 45 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 46 | public class TopologyManager implements TopologyDiscoveryInterface { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 47 | |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 48 | private static final Logger log = LoggerFactory |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 49 | .getLogger(TopologyManager.class); |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 50 | |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 51 | private IEventChannel<byte[], TopologyEvent> eventChannel; |
Jonathan Hart | 10a7e2b | 2014-02-21 18:30:08 -0800 | [diff] [blame] | 52 | public static final String EVENT_CHANNEL_NAME = "onos.topology"; |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 53 | private EventHandler eventHandler = new EventHandler(); |
| 54 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 55 | private final TopologyDatastore datastore; |
| 56 | private final TopologyImpl topology = new TopologyImpl(); |
Yuta HIGUCHI | 170229f | 2014-02-17 15:47:54 -0800 | [diff] [blame] | 57 | private final IControllerRegistryService registryService; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 58 | private CopyOnWriteArrayList<ITopologyListener> topologyListeners; |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 59 | |
Pavlin Radoslavov | 706add2 | 2014-02-20 12:15:59 -0800 | [diff] [blame] | 60 | // |
| 61 | // Local state for keeping track of reordered events. |
| 62 | // NOTE: Switch Events are not affected by the event reordering. |
| 63 | // |
| 64 | private Map<ByteBuffer, PortEvent> reorderedAddedPortEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 65 | new HashMap<ByteBuffer, PortEvent>(); |
Pavlin Radoslavov | 706add2 | 2014-02-20 12:15:59 -0800 | [diff] [blame] | 66 | private Map<ByteBuffer, LinkEvent> reorderedAddedLinkEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 67 | new HashMap<ByteBuffer, LinkEvent>(); |
Pavlin Radoslavov | 706add2 | 2014-02-20 12:15:59 -0800 | [diff] [blame] | 68 | private Map<ByteBuffer, DeviceEvent> reorderedAddedDeviceEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 69 | new HashMap<ByteBuffer, DeviceEvent>(); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 70 | |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 71 | // |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 72 | // Local state for keeping track of locally discovered events so we can |
| 73 | // cleanup properly when a Switch or Port is removed. |
| 74 | // |
| 75 | // We keep all Port, Link and Device events per Switch DPID: |
| 76 | // - If a switch goes down, we remove all corresponding Port, Link and |
| 77 | // Device events. |
| 78 | // - If a port on a switch goes down, we remove all corresponding Link |
| 79 | // and Device events. |
| 80 | // |
| 81 | private Map<Long, Map<ByteBuffer, PortEvent>> discoveredAddedPortEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 82 | new HashMap<>(); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 83 | private Map<Long, Map<ByteBuffer, LinkEvent>> discoveredAddedLinkEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 84 | new HashMap<>(); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 85 | private Map<Long, Map<ByteBuffer, DeviceEvent>> discoveredAddedDeviceEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 86 | new HashMap<>(); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 87 | |
| 88 | // |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 89 | // Local state for keeping track of the application event notifications |
| 90 | // |
| 91 | List<SwitchEvent> apiAddedSwitchEvents = new LinkedList<SwitchEvent>(); |
| 92 | List<SwitchEvent> apiRemovedSwitchEvents = new LinkedList<SwitchEvent>(); |
| 93 | List<PortEvent> apiAddedPortEvents = new LinkedList<PortEvent>(); |
| 94 | List<PortEvent> apiRemovedPortEvents = new LinkedList<PortEvent>(); |
| 95 | List<LinkEvent> apiAddedLinkEvents = new LinkedList<LinkEvent>(); |
| 96 | List<LinkEvent> apiRemovedLinkEvents = new LinkedList<LinkEvent>(); |
| 97 | List<DeviceEvent> apiAddedDeviceEvents = new LinkedList<DeviceEvent>(); |
| 98 | List<DeviceEvent> apiRemovedDeviceEvents = new LinkedList<DeviceEvent>(); |
| 99 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 100 | /** |
| 101 | * Constructor. |
| 102 | * |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 103 | * @param registryService the Registry Service to use. |
| 104 | * @param topologyListeners the collection of topology listeners to use. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 105 | */ |
| 106 | public TopologyManager(IControllerRegistryService registryService, |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 107 | CopyOnWriteArrayList<ITopologyListener> topologyListeners) { |
| 108 | datastore = new TopologyDatastore(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 109 | this.registryService = registryService; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 110 | this.topologyListeners = topologyListeners; |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 111 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 112 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 113 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 114 | * Get the Topology. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 115 | * |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 116 | * @return the Topology. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 117 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 118 | Topology getTopology() { |
| 119 | return topology; |
Pavlin Radoslavov | 6d224ee | 2014-02-18 16:43:15 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 122 | /** |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 123 | * Event handler class. |
| 124 | */ |
| 125 | private class EventHandler extends Thread implements |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 126 | IEventChannelListener<byte[], TopologyEvent> { |
| 127 | private BlockingQueue<EventEntry<TopologyEvent>> topologyEvents = |
| 128 | new LinkedBlockingQueue<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 129 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 130 | /** |
| 131 | * Startup processing. |
| 132 | */ |
| 133 | private void startup() { |
| 134 | // |
| 135 | // TODO: Read all state from the database: |
| 136 | // |
| 137 | // Collection<EventEntry<TopologyEvent>> collection = |
| 138 | // readWholeTopologyFromDB(); |
| 139 | // |
| 140 | // For now, as a shortcut we read it from the datagrid |
| 141 | // |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 142 | Collection<TopologyEvent> allTopologyEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 143 | eventChannel.getAllEntries(); |
| 144 | Collection<EventEntry<TopologyEvent>> collection = |
| 145 | new LinkedList<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 146 | |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 147 | for (TopologyEvent topologyEvent : allTopologyEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 148 | EventEntry<TopologyEvent> eventEntry = |
| 149 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 150 | topologyEvent); |
| 151 | collection.add(eventEntry); |
| 152 | } |
| 153 | processEvents(collection); |
| 154 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 155 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 156 | /** |
| 157 | * Run the thread. |
| 158 | */ |
| 159 | @Override |
| 160 | public void run() { |
| 161 | Collection<EventEntry<TopologyEvent>> collection = |
| 162 | new LinkedList<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 163 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 164 | this.setName("TopologyManager.EventHandler " + this.getId()); |
| 165 | startup(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 166 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 167 | // |
| 168 | // The main loop |
| 169 | // |
Pavlin Radoslavov | 8e881a4 | 2014-06-24 16:58:07 -0700 | [diff] [blame] | 170 | while (true) { |
| 171 | try { |
| 172 | EventEntry<TopologyEvent> eventEntry = |
| 173 | topologyEvents.take(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 174 | collection.add(eventEntry); |
| 175 | topologyEvents.drainTo(collection); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 176 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 177 | processEvents(collection); |
| 178 | collection.clear(); |
Pavlin Radoslavov | 8e881a4 | 2014-06-24 16:58:07 -0700 | [diff] [blame] | 179 | } catch (Exception exception) { |
| 180 | log.debug("Exception processing Topology Events: ", |
| 181 | exception); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 182 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 185 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 186 | /** |
| 187 | * Process all topology events. |
| 188 | * |
| 189 | * @param events the events to process. |
| 190 | */ |
| 191 | private void processEvents(Collection<EventEntry<TopologyEvent>> events) { |
| 192 | // Local state for computing the final set of events |
| 193 | Map<ByteBuffer, SwitchEvent> addedSwitchEvents = new HashMap<>(); |
| 194 | Map<ByteBuffer, SwitchEvent> removedSwitchEvents = new HashMap<>(); |
| 195 | Map<ByteBuffer, PortEvent> addedPortEvents = new HashMap<>(); |
| 196 | Map<ByteBuffer, PortEvent> removedPortEvents = new HashMap<>(); |
| 197 | Map<ByteBuffer, LinkEvent> addedLinkEvents = new HashMap<>(); |
| 198 | Map<ByteBuffer, LinkEvent> removedLinkEvents = new HashMap<>(); |
| 199 | Map<ByteBuffer, DeviceEvent> addedDeviceEvents = new HashMap<>(); |
| 200 | Map<ByteBuffer, DeviceEvent> removedDeviceEvents = new HashMap<>(); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 201 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 202 | // |
| 203 | // Classify and suppress matching events |
| 204 | // |
| 205 | for (EventEntry<TopologyEvent> event : events) { |
| 206 | TopologyEvent topologyEvent = event.eventData(); |
| 207 | SwitchEvent switchEvent = topologyEvent.switchEvent; |
| 208 | PortEvent portEvent = topologyEvent.portEvent; |
| 209 | LinkEvent linkEvent = topologyEvent.linkEvent; |
| 210 | DeviceEvent deviceEvent = topologyEvent.deviceEvent; |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 211 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 212 | // |
| 213 | // Extract the events |
| 214 | // |
| 215 | switch (event.eventType()) { |
| 216 | case ENTRY_ADD: |
| 217 | log.debug("Topology event ENTRY_ADD: {}", topologyEvent); |
| 218 | if (switchEvent != null) { |
| 219 | ByteBuffer id = switchEvent.getIDasByteBuffer(); |
| 220 | addedSwitchEvents.put(id, switchEvent); |
| 221 | removedSwitchEvents.remove(id); |
| 222 | // Switch Events are not affected by event reordering |
| 223 | } |
| 224 | if (portEvent != null) { |
| 225 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 226 | addedPortEvents.put(id, portEvent); |
| 227 | removedPortEvents.remove(id); |
| 228 | reorderedAddedPortEvents.remove(id); |
| 229 | } |
| 230 | if (linkEvent != null) { |
| 231 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 232 | addedLinkEvents.put(id, linkEvent); |
| 233 | removedLinkEvents.remove(id); |
| 234 | reorderedAddedLinkEvents.remove(id); |
| 235 | } |
| 236 | if (deviceEvent != null) { |
| 237 | ByteBuffer id = deviceEvent.getIDasByteBuffer(); |
| 238 | addedDeviceEvents.put(id, deviceEvent); |
| 239 | removedDeviceEvents.remove(id); |
| 240 | reorderedAddedDeviceEvents.remove(id); |
| 241 | } |
| 242 | break; |
| 243 | case ENTRY_REMOVE: |
| 244 | log.debug("Topology event ENTRY_REMOVE: {}", topologyEvent); |
| 245 | if (switchEvent != null) { |
| 246 | ByteBuffer id = switchEvent.getIDasByteBuffer(); |
| 247 | addedSwitchEvents.remove(id); |
| 248 | removedSwitchEvents.put(id, switchEvent); |
| 249 | // Switch Events are not affected by event reordering |
| 250 | } |
| 251 | if (portEvent != null) { |
| 252 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 253 | addedPortEvents.remove(id); |
| 254 | removedPortEvents.put(id, portEvent); |
| 255 | reorderedAddedPortEvents.remove(id); |
| 256 | } |
| 257 | if (linkEvent != null) { |
| 258 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 259 | addedLinkEvents.remove(id); |
| 260 | removedLinkEvents.put(id, linkEvent); |
| 261 | reorderedAddedLinkEvents.remove(id); |
| 262 | } |
| 263 | if (deviceEvent != null) { |
| 264 | ByteBuffer id = deviceEvent.getIDasByteBuffer(); |
| 265 | addedDeviceEvents.remove(id); |
| 266 | removedDeviceEvents.put(id, deviceEvent); |
| 267 | reorderedAddedDeviceEvents.remove(id); |
| 268 | } |
| 269 | break; |
Ray Milkey | 0b122ed | 2014-04-14 10:06:03 -0700 | [diff] [blame] | 270 | default: |
| 271 | log.error("Unknown topology event {}", |
| 272 | event.eventType()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 273 | } |
| 274 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 275 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 276 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 277 | // Lock the topology while it is modified |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 278 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 279 | topology.acquireWriteLock(); |
Pavlin Radoslavov | 8ffb8bf | 2014-02-20 15:34:26 -0800 | [diff] [blame] | 280 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 281 | try { |
| 282 | // |
| 283 | // Apply the classified events. |
| 284 | // |
| 285 | // Apply the "add" events in the proper order: |
| 286 | // switch, port, link, device |
| 287 | // |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 288 | for (SwitchEvent switchEvent : addedSwitchEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 289 | addSwitch(switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 290 | } |
| 291 | for (PortEvent portEvent : addedPortEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 292 | addPort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 293 | } |
| 294 | for (LinkEvent linkEvent : addedLinkEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 295 | addLink(linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 296 | } |
| 297 | for (DeviceEvent deviceEvent : addedDeviceEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 298 | addDevice(deviceEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 299 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 300 | // |
| 301 | // Apply the "remove" events in the reverse order: |
| 302 | // device, link, port, switch |
| 303 | // |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 304 | for (DeviceEvent deviceEvent : removedDeviceEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 305 | removeDevice(deviceEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 306 | } |
| 307 | for (LinkEvent linkEvent : removedLinkEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 308 | removeLink(linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 309 | } |
| 310 | for (PortEvent portEvent : removedPortEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 311 | removePort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 312 | } |
| 313 | for (SwitchEvent switchEvent : removedSwitchEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 314 | removeSwitch(switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 315 | } |
Yuta HIGUCHI | 3aca81a | 2014-02-23 12:41:19 -0800 | [diff] [blame] | 316 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 317 | // |
| 318 | // Apply reordered events |
| 319 | // |
| 320 | applyReorderedEvents(!addedSwitchEvents.isEmpty(), |
| 321 | !addedPortEvents.isEmpty()); |
Yuta HIGUCHI | 3aca81a | 2014-02-23 12:41:19 -0800 | [diff] [blame] | 322 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 323 | } finally { |
| 324 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 325 | // Topology modifications completed: Release the lock |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 326 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 327 | topology.releaseWriteLock(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 328 | } |
Yuta HIGUCHI | 3aca81a | 2014-02-23 12:41:19 -0800 | [diff] [blame] | 329 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 330 | // |
| 331 | // Dispatch the Topology Notification Events to the applications |
| 332 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 333 | dispatchTopologyEvents(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 334 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 335 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 336 | /** |
| 337 | * Receive a notification that an entry is added. |
| 338 | * |
| 339 | * @param value the value for the entry. |
| 340 | */ |
| 341 | @Override |
| 342 | public void entryAdded(TopologyEvent value) { |
| 343 | EventEntry<TopologyEvent> eventEntry = |
| 344 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 345 | value); |
| 346 | topologyEvents.add(eventEntry); |
| 347 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 348 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 349 | /** |
| 350 | * Receive a notification that an entry is removed. |
| 351 | * |
| 352 | * @param value the value for the entry. |
| 353 | */ |
| 354 | @Override |
| 355 | public void entryRemoved(TopologyEvent value) { |
| 356 | EventEntry<TopologyEvent> eventEntry = |
| 357 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_REMOVE, |
| 358 | value); |
| 359 | topologyEvents.add(eventEntry); |
| 360 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 361 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 362 | /** |
| 363 | * Receive a notification that an entry is updated. |
| 364 | * |
| 365 | * @param value the value for the entry. |
| 366 | */ |
| 367 | @Override |
| 368 | public void entryUpdated(TopologyEvent value) { |
| 369 | // NOTE: The ADD and UPDATE events are processed in same way |
| 370 | entryAdded(value); |
| 371 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Startup processing. |
| 376 | * |
| 377 | * @param datagridService the datagrid service to use. |
| 378 | */ |
| 379 | void startup(IDatagridService datagridService) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 380 | eventChannel = datagridService.addListener(EVENT_CHANNEL_NAME, |
| 381 | eventHandler, |
| 382 | byte[].class, |
| 383 | TopologyEvent.class); |
| 384 | eventHandler.start(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 387 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 388 | * Dispatch Topology Events to the listeners. |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 389 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 390 | private void dispatchTopologyEvents() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 391 | if (apiAddedSwitchEvents.isEmpty() && |
| 392 | apiRemovedSwitchEvents.isEmpty() && |
| 393 | apiAddedPortEvents.isEmpty() && |
| 394 | apiRemovedPortEvents.isEmpty() && |
| 395 | apiAddedLinkEvents.isEmpty() && |
| 396 | apiRemovedLinkEvents.isEmpty() && |
| 397 | apiAddedDeviceEvents.isEmpty() && |
| 398 | apiRemovedDeviceEvents.isEmpty()) { |
| 399 | return; // No events to dispatch |
| 400 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 401 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 402 | if (log.isDebugEnabled()) { |
| 403 | // |
| 404 | // Debug statements |
| 405 | // TODO: Those statements should be removed in the future |
| 406 | // |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 407 | for (SwitchEvent switchEvent : apiAddedSwitchEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 408 | log.debug("Dispatch Topology Event: ADDED {}", switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 409 | } |
| 410 | for (SwitchEvent switchEvent : apiRemovedSwitchEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 411 | log.debug("Dispatch Topology Event: REMOVED {}", switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 412 | } |
| 413 | for (PortEvent portEvent : apiAddedPortEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 414 | log.debug("Dispatch Topology Event: ADDED {}", portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 415 | } |
| 416 | for (PortEvent portEvent : apiRemovedPortEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 417 | log.debug("Dispatch Topology Event: REMOVED {}", portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 418 | } |
| 419 | for (LinkEvent linkEvent : apiAddedLinkEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 420 | log.debug("Dispatch Topology Event: ADDED {}", linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 421 | } |
| 422 | for (LinkEvent linkEvent : apiRemovedLinkEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 423 | log.debug("Dispatch Topology Event: REMOVED {}", linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 424 | } |
| 425 | for (DeviceEvent deviceEvent : apiAddedDeviceEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 426 | log.debug("Dispatch Topology Event: ADDED {}", deviceEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 427 | } |
| 428 | for (DeviceEvent deviceEvent : apiRemovedDeviceEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 429 | log.debug("Dispatch Topology Event: REMOVED {}", deviceEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 430 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 431 | } |
admin | bc18155 | 2014-02-21 18:36:42 -0800 | [diff] [blame] | 432 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 433 | // Deliver the events |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 434 | for (ITopologyListener listener : this.topologyListeners) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 435 | // TODO: Should copy before handing them over to listener? |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 436 | listener.topologyEvents(apiAddedSwitchEvents, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 437 | apiRemovedSwitchEvents, |
| 438 | apiAddedPortEvents, |
| 439 | apiRemovedPortEvents, |
| 440 | apiAddedLinkEvents, |
| 441 | apiRemovedLinkEvents, |
| 442 | apiAddedDeviceEvents, |
| 443 | apiRemovedDeviceEvents); |
| 444 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 445 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 446 | // |
| 447 | // Cleanup |
| 448 | // |
| 449 | apiAddedSwitchEvents.clear(); |
| 450 | apiRemovedSwitchEvents.clear(); |
| 451 | apiAddedPortEvents.clear(); |
| 452 | apiRemovedPortEvents.clear(); |
| 453 | apiAddedLinkEvents.clear(); |
| 454 | apiRemovedLinkEvents.clear(); |
| 455 | apiAddedDeviceEvents.clear(); |
| 456 | apiRemovedDeviceEvents.clear(); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Apply reordered events. |
| 461 | * |
| 462 | * @param hasAddedSwitchEvents true if there were Added Switch Events. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 463 | * @param hasAddedPortEvents true if there were Added Port Events. |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 464 | */ |
| 465 | private void applyReorderedEvents(boolean hasAddedSwitchEvents, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 466 | boolean hasAddedPortEvents) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 467 | if (!(hasAddedSwitchEvents || hasAddedPortEvents)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 468 | return; // Nothing to do |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 469 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 470 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 471 | // |
| 472 | // Try to apply the reordered events. |
| 473 | // |
| 474 | // NOTE: For simplicity we try to apply all events of a particular |
| 475 | // type if any "parent" type event was processed: |
| 476 | // - Apply reordered Port Events if Switches were added |
| 477 | // - Apply reordered Link and Device Events if Switches or Ports |
| 478 | // were added |
| 479 | // |
admin | bc18155 | 2014-02-21 18:36:42 -0800 | [diff] [blame] | 480 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 481 | // |
| 482 | // Apply reordered Port Events if Switches were added |
| 483 | // |
| 484 | if (hasAddedSwitchEvents) { |
| 485 | Map<ByteBuffer, PortEvent> portEvents = reorderedAddedPortEvents; |
| 486 | reorderedAddedPortEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 487 | for (PortEvent portEvent : portEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 488 | addPort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 489 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 490 | } |
| 491 | // |
| 492 | // Apply reordered Link and Device Events if Switches or Ports |
| 493 | // were added. |
| 494 | // |
| 495 | Map<ByteBuffer, LinkEvent> linkEvents = reorderedAddedLinkEvents; |
| 496 | reorderedAddedLinkEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 497 | for (LinkEvent linkEvent : linkEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 498 | addLink(linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 499 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 500 | // |
| 501 | Map<ByteBuffer, DeviceEvent> deviceEvents = reorderedAddedDeviceEvents; |
| 502 | reorderedAddedDeviceEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 503 | for (DeviceEvent deviceEvent : deviceEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 504 | addDevice(deviceEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 505 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 508 | /** |
| 509 | * Switch discovered event. |
| 510 | * |
| 511 | * @param switchEvent the switch event. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 512 | * @param portEvents the corresponding port events for the switch. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 513 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 514 | @Override |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 515 | public void putSwitchDiscoveryEvent(SwitchEvent switchEvent, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 516 | Collection<PortEvent> portEvents) { |
| 517 | if (datastore.addSwitch(switchEvent, portEvents)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 518 | log.debug("Sending add switch: {}", switchEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 519 | // Send out notification |
| 520 | TopologyEvent topologyEvent = new TopologyEvent(switchEvent); |
| 521 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 522 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 523 | // Send out notification for each port |
| 524 | for (PortEvent portEvent : portEvents) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 525 | log.debug("Sending add port: {}", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 526 | topologyEvent = new TopologyEvent(portEvent); |
| 527 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
| 528 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 529 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 530 | // |
| 531 | // Keep track of the added ports |
| 532 | // |
| 533 | // Get the old Port Events |
| 534 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 535 | discoveredAddedPortEvents.get(switchEvent.getDpid()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 536 | if (oldPortEvents == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 537 | oldPortEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 538 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 539 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 540 | // Store the new Port Events in the local cache |
| 541 | Map<ByteBuffer, PortEvent> newPortEvents = new HashMap<>(); |
| 542 | for (PortEvent portEvent : portEvents) { |
| 543 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 544 | newPortEvents.put(id, portEvent); |
| 545 | } |
| 546 | discoveredAddedPortEvents.put(switchEvent.getDpid(), |
| 547 | newPortEvents); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 548 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 549 | // |
| 550 | // Extract the removed ports |
| 551 | // |
| 552 | List<PortEvent> removedPortEvents = new LinkedList<>(); |
| 553 | for (Map.Entry<ByteBuffer, PortEvent> entry : oldPortEvents.entrySet()) { |
| 554 | ByteBuffer key = entry.getKey(); |
| 555 | PortEvent portEvent = entry.getValue(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 556 | if (!newPortEvents.containsKey(key)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 557 | removedPortEvents.add(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 558 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | // Cleanup old removed ports |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 562 | for (PortEvent portEvent : removedPortEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 563 | removePortDiscoveryEvent(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 564 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 565 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 568 | /** |
| 569 | * Switch removed event. |
| 570 | * |
| 571 | * @param switchEvent the switch event. |
| 572 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 573 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 574 | public void removeSwitchDiscoveryEvent(SwitchEvent switchEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 575 | // Get the old Port Events |
| 576 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 577 | discoveredAddedPortEvents.get(switchEvent.getDpid()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 578 | if (oldPortEvents == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 579 | oldPortEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 580 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 581 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 582 | if (datastore.deactivateSwitch(switchEvent, oldPortEvents.values())) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 583 | log.debug("Sending remove switch: {}", switchEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 584 | // Send out notification |
| 585 | eventChannel.removeEntry(switchEvent.getID()); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 586 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 587 | // |
| 588 | // Send out notification for each port. |
| 589 | // |
| 590 | // NOTE: We don't use removePortDiscoveryEvent() for the cleanup, |
| 591 | // because it will attempt to remove the port from the database, |
| 592 | // and the deactiveSwitch() call above already removed all ports. |
| 593 | // |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 594 | for (PortEvent portEvent : oldPortEvents.values()) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 595 | log.debug("Sending remove port:", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 596 | eventChannel.removeEntry(portEvent.getID()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 597 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 598 | discoveredAddedPortEvents.remove(switchEvent.getDpid()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 599 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 600 | // Cleanup for each link |
| 601 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 602 | discoveredAddedLinkEvents.get(switchEvent.getDpid()); |
| 603 | if (oldLinkEvents != null) { |
| 604 | for (LinkEvent linkEvent : new ArrayList<>(oldLinkEvents.values())) { |
| 605 | removeLinkDiscoveryEvent(linkEvent); |
| 606 | } |
| 607 | discoveredAddedLinkEvents.remove(switchEvent.getDpid()); |
| 608 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 609 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 610 | // Cleanup for each device |
| 611 | Map<ByteBuffer, DeviceEvent> oldDeviceEvents = |
| 612 | discoveredAddedDeviceEvents.get(switchEvent.getDpid()); |
| 613 | if (oldDeviceEvents != null) { |
| 614 | for (DeviceEvent deviceEvent : new ArrayList<>(oldDeviceEvents.values())) { |
| 615 | removeDeviceDiscoveryEvent(deviceEvent); |
| 616 | } |
| 617 | discoveredAddedDeviceEvents.remove(switchEvent.getDpid()); |
| 618 | } |
| 619 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 620 | } |
| 621 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 622 | /** |
| 623 | * Port discovered event. |
| 624 | * |
| 625 | * @param portEvent the port event. |
| 626 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 627 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 628 | public void putPortDiscoveryEvent(PortEvent portEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 629 | if (datastore.addPort(portEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 630 | log.debug("Sending add port: {}", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 631 | // Send out notification |
| 632 | TopologyEvent topologyEvent = new TopologyEvent(portEvent); |
| 633 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 634 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 635 | // Store the new Port Event in the local cache |
| 636 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 637 | discoveredAddedPortEvents.get(portEvent.getDpid()); |
| 638 | if (oldPortEvents == null) { |
| 639 | oldPortEvents = new HashMap<>(); |
| 640 | discoveredAddedPortEvents.put(portEvent.getDpid(), |
| 641 | oldPortEvents); |
| 642 | } |
| 643 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 644 | oldPortEvents.put(id, portEvent); |
| 645 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 646 | } |
| 647 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 648 | /** |
| 649 | * Port removed event. |
| 650 | * |
| 651 | * @param portEvent the port event. |
| 652 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 653 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 654 | public void removePortDiscoveryEvent(PortEvent portEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 655 | if (datastore.deactivatePort(portEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 656 | log.debug("Sending remove port: {}", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 657 | // Send out notification |
| 658 | eventChannel.removeEntry(portEvent.getID()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 659 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 660 | // Cleanup the Port Event from the local cache |
| 661 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 662 | discoveredAddedPortEvents.get(portEvent.getDpid()); |
| 663 | if (oldPortEvents != null) { |
| 664 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 665 | oldPortEvents.remove(id); |
| 666 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 667 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 668 | // Cleanup for the incoming link |
| 669 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 670 | discoveredAddedLinkEvents.get(portEvent.getDpid()); |
| 671 | if (oldLinkEvents != null) { |
| 672 | for (LinkEvent linkEvent : new ArrayList<>(oldLinkEvents.values())) { |
| 673 | if (linkEvent.getDst().equals(portEvent.id)) { |
| 674 | removeLinkDiscoveryEvent(linkEvent); |
| 675 | // XXX If we change our model to allow multiple Link on |
| 676 | // a Port, this loop must be fixed to allow continuing. |
| 677 | break; |
| 678 | } |
| 679 | } |
| 680 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 681 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 682 | // Cleanup for the connected devices |
| 683 | // TODO: The implementation below is probably wrong |
| 684 | List<DeviceEvent> removedDeviceEvents = new LinkedList<>(); |
| 685 | Map<ByteBuffer, DeviceEvent> oldDeviceEvents = |
| 686 | discoveredAddedDeviceEvents.get(portEvent.getDpid()); |
| 687 | if (oldDeviceEvents != null) { |
| 688 | for (DeviceEvent deviceEvent : new ArrayList<>(oldDeviceEvents.values())) { |
| 689 | for (SwitchPort swp : deviceEvent.getAttachmentPoints()) { |
| 690 | if (swp.equals(portEvent.id)) { |
| 691 | removedDeviceEvents.add(deviceEvent); |
| 692 | } |
| 693 | } |
| 694 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 695 | for (DeviceEvent deviceEvent : removedDeviceEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 696 | removeDeviceDiscoveryEvent(deviceEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 697 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 698 | } |
| 699 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 700 | } |
| 701 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 702 | /** |
| 703 | * Link discovered event. |
| 704 | * |
| 705 | * @param linkEvent the link event. |
| 706 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 707 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 708 | public void putLinkDiscoveryEvent(LinkEvent linkEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 709 | if (datastore.addLink(linkEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 710 | log.debug("Sending add link: {}", linkEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 711 | // Send out notification |
| 712 | TopologyEvent topologyEvent = new TopologyEvent(linkEvent); |
| 713 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 714 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 715 | // Store the new Link Event in the local cache |
| 716 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 717 | discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid()); |
| 718 | if (oldLinkEvents == null) { |
| 719 | oldLinkEvents = new HashMap<>(); |
| 720 | discoveredAddedLinkEvents.put(linkEvent.getDst().getDpid(), |
| 721 | oldLinkEvents); |
| 722 | } |
| 723 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 724 | oldLinkEvents.put(id, linkEvent); |
| 725 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 726 | } |
| 727 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 728 | /** |
| 729 | * Link removed event. |
| 730 | * |
| 731 | * @param linkEvent the link event. |
| 732 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 733 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 734 | public void removeLinkDiscoveryEvent(LinkEvent linkEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 735 | if (datastore.removeLink(linkEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 736 | log.debug("Sending remove link: {}", linkEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 737 | // Send out notification |
| 738 | eventChannel.removeEntry(linkEvent.getID()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 739 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 740 | // Cleanup the Link Event from the local cache |
| 741 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 742 | discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid()); |
| 743 | if (oldLinkEvents != null) { |
| 744 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 745 | oldLinkEvents.remove(id); |
| 746 | } |
| 747 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 748 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 749 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 750 | /** |
| 751 | * Device discovered event. |
| 752 | * |
| 753 | * @param deviceEvent the device event. |
| 754 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 755 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 756 | public void putDeviceDiscoveryEvent(DeviceEvent deviceEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 757 | if (datastore.addDevice(deviceEvent)) { |
| 758 | // Send out notification |
| 759 | TopologyEvent topologyEvent = new TopologyEvent(deviceEvent); |
| 760 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 761 | log.debug("Put the device info into the cache of the topology. mac {}", deviceEvent.getMac()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 762 | |
| 763 | // Store the new Device Event in the local cache |
| 764 | // TODO: The implementation below is probably wrong |
| 765 | for (SwitchPort swp : deviceEvent.getAttachmentPoints()) { |
| 766 | Map<ByteBuffer, DeviceEvent> oldDeviceEvents = |
| 767 | discoveredAddedDeviceEvents.get(swp.getDpid()); |
| 768 | if (oldDeviceEvents == null) { |
| 769 | oldDeviceEvents = new HashMap<>(); |
| 770 | discoveredAddedDeviceEvents.put(swp.getDpid(), |
| 771 | oldDeviceEvents); |
| 772 | } |
| 773 | ByteBuffer id = deviceEvent.getIDasByteBuffer(); |
| 774 | oldDeviceEvents.put(id, deviceEvent); |
| 775 | } |
| 776 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 777 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 778 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 779 | /** |
| 780 | * Device removed event. |
| 781 | * |
| 782 | * @param deviceEvent the device event. |
| 783 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 784 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 785 | public void removeDeviceDiscoveryEvent(DeviceEvent deviceEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 786 | if (datastore.removeDevice(deviceEvent)) { |
| 787 | // Send out notification |
| 788 | eventChannel.removeEntry(deviceEvent.getID()); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 789 | log.debug("Remove the device info into the cache of the topology. mac {}", deviceEvent.getMac()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 790 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 791 | // Cleanup the Device Event from the local cache |
| 792 | // TODO: The implementation below is probably wrong |
| 793 | ByteBuffer id = ByteBuffer.wrap(deviceEvent.getID()); |
| 794 | for (SwitchPort swp : deviceEvent.getAttachmentPoints()) { |
| 795 | Map<ByteBuffer, DeviceEvent> oldDeviceEvents = |
| 796 | discoveredAddedDeviceEvents.get(swp.getDpid()); |
| 797 | if (oldDeviceEvents != null) { |
| 798 | oldDeviceEvents.remove(id); |
| 799 | } |
| 800 | } |
| 801 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 802 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 803 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 804 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 805 | * Add a switch to the topology. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 806 | * |
| 807 | * @param switchEvent the Switch Event with the switch to add. |
| 808 | */ |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 809 | private void addSwitch(SwitchEvent switchEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 810 | Switch sw = topology.getSwitch(switchEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 811 | if (sw == null) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 812 | sw = new SwitchImpl(topology, switchEvent.getDpid()); |
| 813 | topology.putSwitch(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 814 | } else { |
| 815 | // TODO: Update the switch attributes |
| 816 | // TODO: Nothing to do for now |
Ray Milkey | 1aa71f8 | 2014-04-08 16:23:24 -0700 | [diff] [blame] | 817 | log.debug("Update switch attributes"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 818 | } |
| 819 | apiAddedSwitchEvents.add(switchEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 820 | } |
| 821 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 822 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 823 | * Remove a switch from the topology. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 824 | * |
| 825 | * @param switchEvent the Switch Event with the switch to remove. |
| 826 | */ |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 827 | private void removeSwitch(SwitchEvent switchEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 828 | Switch sw = topology.getSwitch(switchEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 829 | if (sw == null) { |
| 830 | log.warn("Switch {} already removed, ignoring", switchEvent); |
| 831 | return; |
| 832 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 833 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 834 | // |
| 835 | // Remove all Ports on the Switch |
| 836 | // |
| 837 | ArrayList<PortEvent> portsToRemove = new ArrayList<>(); |
| 838 | for (Port port : sw.getPorts()) { |
| 839 | log.warn("Port {} on Switch {} should be removed prior to removing Switch. Removing Port now.", |
| 840 | port, switchEvent); |
| 841 | PortEvent portEvent = new PortEvent(port.getDpid(), |
| 842 | port.getNumber()); |
| 843 | portsToRemove.add(portEvent); |
| 844 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 845 | for (PortEvent portEvent : portsToRemove) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 846 | removePort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 847 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 848 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 849 | topology.removeSwitch(switchEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 850 | apiRemovedSwitchEvents.add(switchEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 851 | } |
| 852 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 853 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 854 | * Add a port to the topology. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 855 | * |
| 856 | * @param portEvent the Port Event with the port to add. |
| 857 | */ |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 858 | private void addPort(PortEvent portEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 859 | Switch sw = topology.getSwitch(portEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 860 | if (sw == null) { |
Jonathan Hart | f167520 | 2014-05-23 14:59:07 -0700 | [diff] [blame] | 861 | log.debug("{} reordered because switch is null", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 862 | // Reordered event: delay the event in local cache |
| 863 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 864 | reorderedAddedPortEvents.put(id, portEvent); |
| 865 | return; |
| 866 | } |
| 867 | SwitchImpl switchImpl = getSwitchImpl(sw); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 868 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 869 | Port port = sw.getPort(portEvent.getNumber()); |
| 870 | if (port == null) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 871 | port = new PortImpl(topology, sw, portEvent.getNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 872 | switchImpl.addPort(port); |
| 873 | } else { |
| 874 | // TODO: Update the port attributes |
Ray Milkey | 1aa71f8 | 2014-04-08 16:23:24 -0700 | [diff] [blame] | 875 | log.debug("Update port attributes"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 876 | } |
| 877 | apiAddedPortEvents.add(portEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 878 | } |
| 879 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 880 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 881 | * Remove a port from the topology. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 882 | * |
| 883 | * @param portEvent the Port Event with the port to remove. |
| 884 | */ |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 885 | private void removePort(PortEvent portEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 886 | Switch sw = topology.getSwitch(portEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 887 | if (sw == null) { |
| 888 | log.warn("Parent Switch for Port {} already removed, ignoring", |
| 889 | portEvent); |
| 890 | return; |
| 891 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 892 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 893 | Port port = sw.getPort(portEvent.getNumber()); |
| 894 | if (port == null) { |
| 895 | log.warn("Port {} already removed, ignoring", portEvent); |
| 896 | return; |
| 897 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 898 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 899 | // |
| 900 | // Remove all Devices attached to the Port |
| 901 | // |
| 902 | ArrayList<DeviceEvent> devicesToRemove = new ArrayList<>(); |
| 903 | for (Device device : port.getDevices()) { |
| 904 | log.debug("Removing Device {} on Port {}", device, portEvent); |
| 905 | DeviceEvent deviceEvent = new DeviceEvent(device.getMacAddress()); |
| 906 | SwitchPort switchPort = new SwitchPort(port.getSwitch().getDpid(), |
| 907 | port.getNumber()); |
| 908 | deviceEvent.addAttachmentPoint(switchPort); |
| 909 | devicesToRemove.add(deviceEvent); |
| 910 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 911 | for (DeviceEvent deviceEvent : devicesToRemove) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 912 | removeDevice(deviceEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 913 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 914 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 915 | // |
| 916 | // Remove all Links connected to the Port |
| 917 | // |
| 918 | Set<Link> links = new HashSet<>(); |
| 919 | links.add(port.getOutgoingLink()); |
| 920 | links.add(port.getIncomingLink()); |
| 921 | ArrayList<LinkEvent> linksToRemove = new ArrayList<>(); |
| 922 | for (Link link : links) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 923 | if (link == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 924 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 925 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 926 | log.debug("Removing Link {} on Port {}", link, portEvent); |
| 927 | LinkEvent linkEvent = new LinkEvent(link.getSrcSwitch().getDpid(), |
| 928 | link.getSrcPort().getNumber(), |
| 929 | link.getDstSwitch().getDpid(), |
| 930 | link.getDstPort().getNumber()); |
| 931 | linksToRemove.add(linkEvent); |
| 932 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 933 | for (LinkEvent linkEvent : linksToRemove) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 934 | removeLink(linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 935 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 936 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 937 | // Remove the Port from the Switch |
| 938 | SwitchImpl switchImpl = getSwitchImpl(sw); |
| 939 | switchImpl.removePort(port); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 940 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 941 | apiRemovedPortEvents.add(portEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 942 | } |
| 943 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 944 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 945 | * Add a link to the topology. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 946 | * |
| 947 | * @param linkEvent the Link Event with the link to add. |
| 948 | */ |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 949 | private void addLink(LinkEvent linkEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 950 | Port srcPort = topology.getPort(linkEvent.getSrc().dpid, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 951 | linkEvent.getSrc().number); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 952 | Port dstPort = topology.getPort(linkEvent.getDst().dpid, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 953 | linkEvent.getDst().number); |
| 954 | if ((srcPort == null) || (dstPort == null)) { |
Jonathan Hart | f167520 | 2014-05-23 14:59:07 -0700 | [diff] [blame] | 955 | log.debug("{} reordered because {} port is null", linkEvent, |
| 956 | (srcPort == null) ? "src" : "dst"); |
| 957 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 958 | // Reordered event: delay the event in local cache |
| 959 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 960 | reorderedAddedLinkEvents.put(id, linkEvent); |
| 961 | return; |
| 962 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 963 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 964 | // Get the Link instance from the Destination Port Incoming Link |
| 965 | Link link = dstPort.getIncomingLink(); |
| 966 | assert (link == srcPort.getOutgoingLink()); |
| 967 | if (link == null) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 968 | link = new LinkImpl(topology, srcPort, dstPort); |
| 969 | topology.putLink(link); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 970 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 971 | // Remove all Devices attached to the Ports |
| 972 | ArrayList<DeviceEvent> devicesToRemove = new ArrayList<>(); |
| 973 | ArrayList<Port> ports = new ArrayList<>(); |
| 974 | ports.add(srcPort); |
| 975 | ports.add(dstPort); |
| 976 | for (Port port : ports) { |
| 977 | for (Device device : port.getDevices()) { |
| 978 | log.error("Device {} on Port {} should have been removed prior to adding Link {}", |
| 979 | device, port, linkEvent); |
| 980 | DeviceEvent deviceEvent = |
| 981 | new DeviceEvent(device.getMacAddress()); |
| 982 | SwitchPort switchPort = |
| 983 | new SwitchPort(port.getSwitch().getDpid(), |
| 984 | port.getNumber()); |
| 985 | deviceEvent.addAttachmentPoint(switchPort); |
| 986 | devicesToRemove.add(deviceEvent); |
| 987 | } |
| 988 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 989 | for (DeviceEvent deviceEvent : devicesToRemove) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 990 | removeDevice(deviceEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 991 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 992 | } else { |
| 993 | // TODO: Update the link attributes |
Ray Milkey | 1aa71f8 | 2014-04-08 16:23:24 -0700 | [diff] [blame] | 994 | log.debug("Update link attributes"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 995 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 996 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 997 | apiAddedLinkEvents.add(linkEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 998 | } |
| 999 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1000 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1001 | * Remove a link from the topology. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1002 | * |
| 1003 | * @param linkEvent the Link Event with the link to remove. |
| 1004 | */ |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1005 | private void removeLink(LinkEvent linkEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1006 | Port srcPort = topology.getPort(linkEvent.getSrc().dpid, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1007 | linkEvent.getSrc().number); |
| 1008 | if (srcPort == null) { |
| 1009 | log.warn("Src Port for Link {} already removed, ignoring", |
| 1010 | linkEvent); |
| 1011 | return; |
| 1012 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1013 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1014 | Port dstPort = topology.getPort(linkEvent.getDst().dpid, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1015 | linkEvent.getDst().number); |
| 1016 | if (dstPort == null) { |
| 1017 | log.warn("Dst Port for Link {} already removed, ignoring", |
| 1018 | linkEvent); |
| 1019 | return; |
| 1020 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1021 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1022 | // |
| 1023 | // Remove the Link instance from the Destination Port Incoming Link |
| 1024 | // and the Source Port Outgoing Link. |
| 1025 | // |
| 1026 | Link link = dstPort.getIncomingLink(); |
| 1027 | if (link == null) { |
| 1028 | log.warn("Link {} already removed on destination Port", linkEvent); |
| 1029 | } |
| 1030 | link = srcPort.getOutgoingLink(); |
| 1031 | if (link == null) { |
| 1032 | log.warn("Link {} already removed on src Port", linkEvent); |
| 1033 | } |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 1034 | |
| 1035 | // TODO should we check that we get the same link from each port? |
| 1036 | if (link != null) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1037 | topology.removeLink(link); |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 1038 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1039 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1040 | apiRemovedLinkEvents.add(linkEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1041 | } |
| 1042 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1043 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1044 | * Add a device to the topology. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1045 | * <p/> |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1046 | * TODO: Device-related work is incomplete. |
| 1047 | * TODO: Eventually, we might need to consider reordering |
| 1048 | * or addLink() and addDevice() events on the same port. |
| 1049 | * |
| 1050 | * @param deviceEvent the Device Event with the device to add. |
| 1051 | */ |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1052 | private void addDevice(DeviceEvent deviceEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1053 | log.debug("Adding a device to the topology with mac {}", deviceEvent.getMac()); |
| 1054 | Device device = topology.getDeviceByMac(deviceEvent.getMac()); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1055 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1056 | if (device == null) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1057 | log.debug("Existing device was not found in the Topology: Adding new device: mac {}", deviceEvent.getMac()); |
| 1058 | device = new DeviceImpl(topology, deviceEvent.getMac()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1059 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1060 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1061 | DeviceImpl deviceImpl = getDeviceImpl(device); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1062 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1063 | // Process each attachment point |
| 1064 | boolean attachmentFound = false; |
| 1065 | for (SwitchPort swp : deviceEvent.getAttachmentPoints()) { |
| 1066 | // Attached Ports must exist |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1067 | Port port = topology.getPort(swp.dpid, swp.number); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1068 | if (port == null) { |
| 1069 | // Reordered event: delay the event in local cache |
| 1070 | ByteBuffer id = deviceEvent.getIDasByteBuffer(); |
| 1071 | reorderedAddedDeviceEvents.put(id, deviceEvent); |
| 1072 | continue; |
| 1073 | } |
| 1074 | // Attached Ports must not have Link |
| 1075 | if (port.getOutgoingLink() != null || |
| 1076 | port.getIncomingLink() != null) { |
| 1077 | log.warn("Link (Out:{},In:{}) exist on the attachment point, skipping mutation.", |
| 1078 | port.getOutgoingLink(), |
| 1079 | port.getIncomingLink()); |
| 1080 | continue; |
| 1081 | } |
| 1082 | |
| 1083 | // Add Device <-> Port attachment |
| 1084 | PortImpl portImpl = getPortImpl(port); |
| 1085 | portImpl.addDevice(device); |
| 1086 | deviceImpl.addAttachmentPoint(port); |
| 1087 | attachmentFound = true; |
| 1088 | } |
| 1089 | |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 1090 | deviceImpl.setLastSeenTime(deviceEvent.getLastSeenTime()); |
| 1091 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1092 | // Update the device in the topology |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1093 | if (attachmentFound) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1094 | log.debug("Storing the device info into the Topology: mac {}", deviceEvent.getMac()); |
| 1095 | topology.putDevice(device); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1096 | apiAddedDeviceEvents.add(deviceEvent); |
| 1097 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1098 | } |
| 1099 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1100 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1101 | * Remove a device from the topology. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1102 | * <p/> |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1103 | * TODO: Device-related work is incomplete. |
| 1104 | * |
| 1105 | * @param deviceEvent the Device Event with the device to remove. |
| 1106 | */ |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1107 | private void removeDevice(DeviceEvent deviceEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1108 | log.debug("Removing a device to the topology: mac {}", deviceEvent.getMac()); |
| 1109 | Device device = topology.getDeviceByMac(deviceEvent.getMac()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1110 | if (device == null) { |
| 1111 | log.warn("Device {} already removed, ignoring", deviceEvent); |
| 1112 | return; |
| 1113 | } |
| 1114 | DeviceImpl deviceImpl = getDeviceImpl(device); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1115 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1116 | // Process each attachment point |
| 1117 | for (SwitchPort swp : deviceEvent.getAttachmentPoints()) { |
| 1118 | // Attached Ports must exist |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1119 | Port port = topology.getPort(swp.dpid, swp.number); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1120 | if (port == null) { |
| 1121 | log.warn("Port for the attachment point {} did not exist. skipping attachment point mutation", swp); |
| 1122 | continue; |
| 1123 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1124 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1125 | // Remove Device <-> Port attachment |
| 1126 | PortImpl portImpl = getPortImpl(port); |
| 1127 | portImpl.removeDevice(device); |
| 1128 | deviceImpl.removeAttachmentPoint(port); |
| 1129 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1130 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1131 | log.debug("Removing the device info into the Topology: mac {}", deviceEvent.getMac()); |
| 1132 | topology.removeDevice(device); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1133 | apiRemovedDeviceEvents.add(deviceEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1134 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 1135 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1136 | /** |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1137 | * Get the SwitchImpl-casted switch implementation. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1138 | * |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1139 | * @param sw the Switch to cast. |
| 1140 | * @return the SwitchImpl-casted switch implementation. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1141 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1142 | private SwitchImpl getSwitchImpl(Switch sw) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1143 | if (sw instanceof SwitchImpl) { |
| 1144 | return (SwitchImpl) sw; |
| 1145 | } |
| 1146 | throw new ClassCastException("SwitchImpl expected, but found: " + sw); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1147 | } |
| 1148 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1149 | /** |
| 1150 | * Get the PortImpl-casted port implementation. |
| 1151 | * |
| 1152 | * @param port the Port to cast. |
| 1153 | * @return the PortImpl-casted port implementation. |
| 1154 | */ |
| 1155 | private PortImpl getPortImpl(Port port) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1156 | if (port instanceof PortImpl) { |
| 1157 | return (PortImpl) port; |
| 1158 | } |
| 1159 | throw new ClassCastException("PortImpl expected, but found: " + port); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1162 | /** |
| 1163 | * Get the LinkImpl-casted link implementation. |
| 1164 | * |
| 1165 | * @param link the Link to cast. |
| 1166 | * @return the LinkImpl-casted link implementation. |
| 1167 | */ |
| 1168 | private LinkImpl getLinkImpl(Link link) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1169 | if (link instanceof LinkImpl) { |
| 1170 | return (LinkImpl) link; |
| 1171 | } |
| 1172 | throw new ClassCastException("LinkImpl expected, but found: " + link); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1173 | } |
| 1174 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1175 | /** |
| 1176 | * Get the DeviceImpl-casted device implementation. |
| 1177 | * |
| 1178 | * @param device the Device to cast. |
| 1179 | * @return the DeviceImpl-casted device implementation. |
| 1180 | */ |
| 1181 | private DeviceImpl getDeviceImpl(Device device) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1182 | if (device instanceof DeviceImpl) { |
| 1183 | return (DeviceImpl) device; |
| 1184 | } |
| 1185 | throw new ClassCastException("DeviceImpl expected, but found: " + device); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1186 | } |
| 1187 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1188 | /** |
| 1189 | * Read the whole topology from the database. |
| 1190 | * |
| 1191 | * @return a collection of EventEntry-encapsulated Topology Events for |
| 1192 | * the whole topology. |
| 1193 | */ |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1194 | private Collection<EventEntry<TopologyEvent>> readWholeTopologyFromDB() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1195 | Collection<EventEntry<TopologyEvent>> collection = |
| 1196 | new LinkedList<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1197 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1198 | // XXX May need to clear whole topology first, depending on |
| 1199 | // how we initially subscribe to replication events |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1200 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1201 | // Add all active switches |
| 1202 | for (KVSwitch sw : KVSwitch.getAllSwitches()) { |
| 1203 | if (sw.getStatus() != KVSwitch.STATUS.ACTIVE) { |
| 1204 | continue; |
| 1205 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1206 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1207 | SwitchEvent switchEvent = new SwitchEvent(sw.getDpid()); |
| 1208 | TopologyEvent topologyEvent = new TopologyEvent(switchEvent); |
| 1209 | EventEntry<TopologyEvent> eventEntry = |
| 1210 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1211 | topologyEvent); |
| 1212 | collection.add(eventEntry); |
| 1213 | } |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 1214 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1215 | // Add all active ports |
| 1216 | for (KVPort p : KVPort.getAllPorts()) { |
| 1217 | if (p.getStatus() != KVPort.STATUS.ACTIVE) { |
| 1218 | continue; |
| 1219 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1220 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1221 | PortEvent portEvent = new PortEvent(p.getDpid(), p.getNumber()); |
| 1222 | TopologyEvent topologyEvent = new TopologyEvent(portEvent); |
| 1223 | EventEntry<TopologyEvent> eventEntry = |
| 1224 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1225 | topologyEvent); |
| 1226 | collection.add(eventEntry); |
| 1227 | } |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 1228 | |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 1229 | for (KVDevice d : KVDevice.getAllDevices()) { |
| 1230 | DeviceEvent devEvent = new DeviceEvent(MACAddress.valueOf(d.getMac())); |
| 1231 | for (byte[] portId : d.getAllPortIds()) { |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 1232 | devEvent.addAttachmentPoint( |
| 1233 | new SwitchPort(KVPort.getDpidFromKey(portId), |
| 1234 | KVPort.getNumberFromKey(portId))); |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 1235 | } |
| 1236 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1237 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1238 | for (KVLink l : KVLink.getAllLinks()) { |
| 1239 | LinkEvent linkEvent = new LinkEvent(l.getSrc().dpid, |
| 1240 | l.getSrc().number, |
| 1241 | l.getDst().dpid, |
| 1242 | l.getDst().number); |
| 1243 | TopologyEvent topologyEvent = new TopologyEvent(linkEvent); |
| 1244 | EventEntry<TopologyEvent> eventEntry = |
| 1245 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1246 | topologyEvent); |
| 1247 | collection.add(eventEntry); |
| 1248 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1249 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1250 | return collection; |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1251 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 1252 | } |