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