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)) { |
| 516 | // Send out notification |
| 517 | TopologyEvent topologyEvent = new TopologyEvent(switchEvent); |
| 518 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 519 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 520 | // Send out notification for each port |
| 521 | for (PortEvent portEvent : portEvents) { |
| 522 | topologyEvent = new TopologyEvent(portEvent); |
| 523 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
| 524 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 525 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 526 | // |
| 527 | // Keep track of the added ports |
| 528 | // |
| 529 | // Get the old Port Events |
| 530 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 531 | discoveredAddedPortEvents.get(switchEvent.getDpid()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 532 | if (oldPortEvents == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 533 | oldPortEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 534 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 535 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 536 | // Store the new Port Events in the local cache |
| 537 | Map<ByteBuffer, PortEvent> newPortEvents = new HashMap<>(); |
| 538 | for (PortEvent portEvent : portEvents) { |
| 539 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 540 | newPortEvents.put(id, portEvent); |
| 541 | } |
| 542 | discoveredAddedPortEvents.put(switchEvent.getDpid(), |
| 543 | newPortEvents); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 544 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 545 | // |
| 546 | // Extract the removed ports |
| 547 | // |
| 548 | List<PortEvent> removedPortEvents = new LinkedList<>(); |
| 549 | for (Map.Entry<ByteBuffer, PortEvent> entry : oldPortEvents.entrySet()) { |
| 550 | ByteBuffer key = entry.getKey(); |
| 551 | PortEvent portEvent = entry.getValue(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 552 | if (!newPortEvents.containsKey(key)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 553 | removedPortEvents.add(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 554 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | // Cleanup old removed ports |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 558 | for (PortEvent portEvent : removedPortEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 559 | removePortDiscoveryEvent(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 560 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 561 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 564 | /** |
| 565 | * Switch removed event. |
| 566 | * |
| 567 | * @param switchEvent the switch event. |
| 568 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 569 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 570 | public void removeSwitchDiscoveryEvent(SwitchEvent switchEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 571 | // Get the old Port Events |
| 572 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 573 | discoveredAddedPortEvents.get(switchEvent.getDpid()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 574 | if (oldPortEvents == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 575 | oldPortEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 576 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 577 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 578 | if (datastore.deactivateSwitch(switchEvent, oldPortEvents.values())) { |
| 579 | // Send out notification |
| 580 | eventChannel.removeEntry(switchEvent.getID()); |
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 | // |
| 583 | // Send out notification for each port. |
| 584 | // |
| 585 | // NOTE: We don't use removePortDiscoveryEvent() for the cleanup, |
| 586 | // because it will attempt to remove the port from the database, |
| 587 | // and the deactiveSwitch() call above already removed all ports. |
| 588 | // |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 589 | for (PortEvent portEvent : oldPortEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 590 | eventChannel.removeEntry(portEvent.getID()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 591 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 592 | discoveredAddedPortEvents.remove(switchEvent.getDpid()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 593 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 594 | // Cleanup for each link |
| 595 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 596 | discoveredAddedLinkEvents.get(switchEvent.getDpid()); |
| 597 | if (oldLinkEvents != null) { |
| 598 | for (LinkEvent linkEvent : new ArrayList<>(oldLinkEvents.values())) { |
| 599 | removeLinkDiscoveryEvent(linkEvent); |
| 600 | } |
| 601 | discoveredAddedLinkEvents.remove(switchEvent.getDpid()); |
| 602 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 603 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 604 | // Cleanup for each device |
| 605 | Map<ByteBuffer, DeviceEvent> oldDeviceEvents = |
| 606 | discoveredAddedDeviceEvents.get(switchEvent.getDpid()); |
| 607 | if (oldDeviceEvents != null) { |
| 608 | for (DeviceEvent deviceEvent : new ArrayList<>(oldDeviceEvents.values())) { |
| 609 | removeDeviceDiscoveryEvent(deviceEvent); |
| 610 | } |
| 611 | discoveredAddedDeviceEvents.remove(switchEvent.getDpid()); |
| 612 | } |
| 613 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 616 | /** |
| 617 | * Port discovered event. |
| 618 | * |
| 619 | * @param portEvent the port event. |
| 620 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 621 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 622 | public void putPortDiscoveryEvent(PortEvent portEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 623 | if (datastore.addPort(portEvent)) { |
| 624 | // Send out notification |
| 625 | TopologyEvent topologyEvent = new TopologyEvent(portEvent); |
| 626 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 627 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 628 | // Store the new Port Event in the local cache |
| 629 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 630 | discoveredAddedPortEvents.get(portEvent.getDpid()); |
| 631 | if (oldPortEvents == null) { |
| 632 | oldPortEvents = new HashMap<>(); |
| 633 | discoveredAddedPortEvents.put(portEvent.getDpid(), |
| 634 | oldPortEvents); |
| 635 | } |
| 636 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 637 | oldPortEvents.put(id, portEvent); |
| 638 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 641 | /** |
| 642 | * Port removed event. |
| 643 | * |
| 644 | * @param portEvent the port event. |
| 645 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 646 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 647 | public void removePortDiscoveryEvent(PortEvent portEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 648 | if (datastore.deactivatePort(portEvent)) { |
| 649 | // Send out notification |
| 650 | eventChannel.removeEntry(portEvent.getID()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 651 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 652 | // Cleanup the Port Event from the local cache |
| 653 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 654 | discoveredAddedPortEvents.get(portEvent.getDpid()); |
| 655 | if (oldPortEvents != null) { |
| 656 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 657 | oldPortEvents.remove(id); |
| 658 | } |
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 for the incoming link |
| 661 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 662 | discoveredAddedLinkEvents.get(portEvent.getDpid()); |
| 663 | if (oldLinkEvents != null) { |
| 664 | for (LinkEvent linkEvent : new ArrayList<>(oldLinkEvents.values())) { |
| 665 | if (linkEvent.getDst().equals(portEvent.id)) { |
| 666 | removeLinkDiscoveryEvent(linkEvent); |
| 667 | // XXX If we change our model to allow multiple Link on |
| 668 | // a Port, this loop must be fixed to allow continuing. |
| 669 | break; |
| 670 | } |
| 671 | } |
| 672 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 673 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 674 | // Cleanup for the connected devices |
| 675 | // TODO: The implementation below is probably wrong |
| 676 | List<DeviceEvent> removedDeviceEvents = new LinkedList<>(); |
| 677 | Map<ByteBuffer, DeviceEvent> oldDeviceEvents = |
| 678 | discoveredAddedDeviceEvents.get(portEvent.getDpid()); |
| 679 | if (oldDeviceEvents != null) { |
| 680 | for (DeviceEvent deviceEvent : new ArrayList<>(oldDeviceEvents.values())) { |
| 681 | for (SwitchPort swp : deviceEvent.getAttachmentPoints()) { |
| 682 | if (swp.equals(portEvent.id)) { |
| 683 | removedDeviceEvents.add(deviceEvent); |
| 684 | } |
| 685 | } |
| 686 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 687 | for (DeviceEvent deviceEvent : removedDeviceEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 688 | removeDeviceDiscoveryEvent(deviceEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 689 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 690 | } |
| 691 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 692 | } |
| 693 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 694 | /** |
| 695 | * Link discovered event. |
| 696 | * |
| 697 | * @param linkEvent the link event. |
| 698 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 699 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 700 | public void putLinkDiscoveryEvent(LinkEvent linkEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 701 | if (datastore.addLink(linkEvent)) { |
| 702 | // Send out notification |
| 703 | TopologyEvent topologyEvent = new TopologyEvent(linkEvent); |
| 704 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 705 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 706 | // Store the new Link Event in the local cache |
| 707 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 708 | discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid()); |
| 709 | if (oldLinkEvents == null) { |
| 710 | oldLinkEvents = new HashMap<>(); |
| 711 | discoveredAddedLinkEvents.put(linkEvent.getDst().getDpid(), |
| 712 | oldLinkEvents); |
| 713 | } |
| 714 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 715 | oldLinkEvents.put(id, linkEvent); |
| 716 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 717 | } |
| 718 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 719 | /** |
| 720 | * Link removed event. |
| 721 | * |
| 722 | * @param linkEvent the link event. |
| 723 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 724 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 725 | public void removeLinkDiscoveryEvent(LinkEvent linkEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 726 | if (datastore.removeLink(linkEvent)) { |
| 727 | // Send out notification |
| 728 | eventChannel.removeEntry(linkEvent.getID()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 729 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 730 | // Cleanup the Link Event from the local cache |
| 731 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 732 | discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid()); |
| 733 | if (oldLinkEvents != null) { |
| 734 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 735 | oldLinkEvents.remove(id); |
| 736 | } |
| 737 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 738 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 739 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 740 | /** |
| 741 | * Device discovered event. |
| 742 | * |
| 743 | * @param deviceEvent the device event. |
| 744 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 745 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 746 | public void putDeviceDiscoveryEvent(DeviceEvent deviceEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 747 | if (datastore.addDevice(deviceEvent)) { |
| 748 | // Send out notification |
| 749 | TopologyEvent topologyEvent = new TopologyEvent(deviceEvent); |
| 750 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 751 | 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] | 752 | |
| 753 | // Store the new Device Event in the local cache |
| 754 | // TODO: The implementation below is probably wrong |
| 755 | for (SwitchPort swp : deviceEvent.getAttachmentPoints()) { |
| 756 | Map<ByteBuffer, DeviceEvent> oldDeviceEvents = |
| 757 | discoveredAddedDeviceEvents.get(swp.getDpid()); |
| 758 | if (oldDeviceEvents == null) { |
| 759 | oldDeviceEvents = new HashMap<>(); |
| 760 | discoveredAddedDeviceEvents.put(swp.getDpid(), |
| 761 | oldDeviceEvents); |
| 762 | } |
| 763 | ByteBuffer id = deviceEvent.getIDasByteBuffer(); |
| 764 | oldDeviceEvents.put(id, deviceEvent); |
| 765 | } |
| 766 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 767 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 768 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 769 | /** |
| 770 | * Device removed event. |
| 771 | * |
| 772 | * @param deviceEvent the device event. |
| 773 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 774 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 775 | public void removeDeviceDiscoveryEvent(DeviceEvent deviceEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 776 | if (datastore.removeDevice(deviceEvent)) { |
| 777 | // Send out notification |
| 778 | eventChannel.removeEntry(deviceEvent.getID()); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 779 | 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] | 780 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 781 | // Cleanup the Device Event from the local cache |
| 782 | // TODO: The implementation below is probably wrong |
| 783 | ByteBuffer id = ByteBuffer.wrap(deviceEvent.getID()); |
| 784 | for (SwitchPort swp : deviceEvent.getAttachmentPoints()) { |
| 785 | Map<ByteBuffer, DeviceEvent> oldDeviceEvents = |
| 786 | discoveredAddedDeviceEvents.get(swp.getDpid()); |
| 787 | if (oldDeviceEvents != null) { |
| 788 | oldDeviceEvents.remove(id); |
| 789 | } |
| 790 | } |
| 791 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 792 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 793 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 794 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 795 | * Add a switch to the topology. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 796 | * |
| 797 | * @param switchEvent the Switch Event with the switch to add. |
| 798 | */ |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 799 | private void addSwitch(SwitchEvent switchEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 800 | Switch sw = topology.getSwitch(switchEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 801 | if (sw == null) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 802 | sw = new SwitchImpl(topology, switchEvent.getDpid()); |
| 803 | topology.putSwitch(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 804 | } else { |
| 805 | // TODO: Update the switch attributes |
| 806 | // TODO: Nothing to do for now |
Ray Milkey | 1aa71f8 | 2014-04-08 16:23:24 -0700 | [diff] [blame] | 807 | log.debug("Update switch attributes"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 808 | } |
| 809 | apiAddedSwitchEvents.add(switchEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 810 | } |
| 811 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 812 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 813 | * Remove a switch from the topology. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 814 | * |
| 815 | * @param switchEvent the Switch Event with the switch to remove. |
| 816 | */ |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 817 | private void removeSwitch(SwitchEvent switchEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 818 | Switch sw = topology.getSwitch(switchEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 819 | if (sw == null) { |
| 820 | log.warn("Switch {} already removed, ignoring", switchEvent); |
| 821 | return; |
| 822 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 823 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 824 | // |
| 825 | // Remove all Ports on the Switch |
| 826 | // |
| 827 | ArrayList<PortEvent> portsToRemove = new ArrayList<>(); |
| 828 | for (Port port : sw.getPorts()) { |
| 829 | log.warn("Port {} on Switch {} should be removed prior to removing Switch. Removing Port now.", |
| 830 | port, switchEvent); |
| 831 | PortEvent portEvent = new PortEvent(port.getDpid(), |
| 832 | port.getNumber()); |
| 833 | portsToRemove.add(portEvent); |
| 834 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 835 | for (PortEvent portEvent : portsToRemove) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 836 | removePort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 837 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 838 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 839 | topology.removeSwitch(switchEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 840 | apiRemovedSwitchEvents.add(switchEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 841 | } |
| 842 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 843 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 844 | * Add a port to the topology. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 845 | * |
| 846 | * @param portEvent the Port Event with the port to add. |
| 847 | */ |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 848 | private void addPort(PortEvent portEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 849 | Switch sw = topology.getSwitch(portEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 850 | if (sw == null) { |
| 851 | // Reordered event: delay the event in local cache |
| 852 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 853 | reorderedAddedPortEvents.put(id, portEvent); |
| 854 | return; |
| 855 | } |
| 856 | SwitchImpl switchImpl = getSwitchImpl(sw); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 857 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 858 | Port port = sw.getPort(portEvent.getNumber()); |
| 859 | if (port == null) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 860 | port = new PortImpl(topology, sw, portEvent.getNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 861 | switchImpl.addPort(port); |
| 862 | } else { |
| 863 | // TODO: Update the port attributes |
Ray Milkey | 1aa71f8 | 2014-04-08 16:23:24 -0700 | [diff] [blame] | 864 | log.debug("Update port attributes"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 865 | } |
| 866 | apiAddedPortEvents.add(portEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 867 | } |
| 868 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 869 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 870 | * Remove a port from the topology. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 871 | * |
| 872 | * @param portEvent the Port Event with the port to remove. |
| 873 | */ |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 874 | private void removePort(PortEvent portEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 875 | Switch sw = topology.getSwitch(portEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 876 | if (sw == null) { |
| 877 | log.warn("Parent Switch for Port {} already removed, ignoring", |
| 878 | portEvent); |
| 879 | return; |
| 880 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 881 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 882 | Port port = sw.getPort(portEvent.getNumber()); |
| 883 | if (port == null) { |
| 884 | log.warn("Port {} already removed, ignoring", portEvent); |
| 885 | return; |
| 886 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 887 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 888 | // |
| 889 | // Remove all Devices attached to the Port |
| 890 | // |
| 891 | ArrayList<DeviceEvent> devicesToRemove = new ArrayList<>(); |
| 892 | for (Device device : port.getDevices()) { |
| 893 | log.debug("Removing Device {} on Port {}", device, portEvent); |
| 894 | DeviceEvent deviceEvent = new DeviceEvent(device.getMacAddress()); |
| 895 | SwitchPort switchPort = new SwitchPort(port.getSwitch().getDpid(), |
| 896 | port.getNumber()); |
| 897 | deviceEvent.addAttachmentPoint(switchPort); |
| 898 | devicesToRemove.add(deviceEvent); |
| 899 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 900 | for (DeviceEvent deviceEvent : devicesToRemove) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 901 | removeDevice(deviceEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 902 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 903 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 904 | // |
| 905 | // Remove all Links connected to the Port |
| 906 | // |
| 907 | Set<Link> links = new HashSet<>(); |
| 908 | links.add(port.getOutgoingLink()); |
| 909 | links.add(port.getIncomingLink()); |
| 910 | ArrayList<LinkEvent> linksToRemove = new ArrayList<>(); |
| 911 | for (Link link : links) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 912 | if (link == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 913 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 914 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 915 | log.debug("Removing Link {} on Port {}", link, portEvent); |
| 916 | LinkEvent linkEvent = new LinkEvent(link.getSrcSwitch().getDpid(), |
| 917 | link.getSrcPort().getNumber(), |
| 918 | link.getDstSwitch().getDpid(), |
| 919 | link.getDstPort().getNumber()); |
| 920 | linksToRemove.add(linkEvent); |
| 921 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 922 | for (LinkEvent linkEvent : linksToRemove) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 923 | removeLink(linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 924 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 925 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 926 | // Remove the Port from the Switch |
| 927 | SwitchImpl switchImpl = getSwitchImpl(sw); |
| 928 | switchImpl.removePort(port); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 929 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 930 | apiRemovedPortEvents.add(portEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 931 | } |
| 932 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 933 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 934 | * Add a link to the topology. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 935 | * |
| 936 | * @param linkEvent the Link Event with the link to add. |
| 937 | */ |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 938 | private void addLink(LinkEvent linkEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 939 | Port srcPort = topology.getPort(linkEvent.getSrc().dpid, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 940 | linkEvent.getSrc().number); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 941 | Port dstPort = topology.getPort(linkEvent.getDst().dpid, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 942 | linkEvent.getDst().number); |
| 943 | if ((srcPort == null) || (dstPort == null)) { |
| 944 | // Reordered event: delay the event in local cache |
| 945 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 946 | reorderedAddedLinkEvents.put(id, linkEvent); |
| 947 | return; |
| 948 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 949 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 950 | // Get the Link instance from the Destination Port Incoming Link |
| 951 | Link link = dstPort.getIncomingLink(); |
| 952 | assert (link == srcPort.getOutgoingLink()); |
| 953 | if (link == null) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 954 | link = new LinkImpl(topology, srcPort, dstPort); |
| 955 | topology.putLink(link); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 956 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 957 | // Remove all Devices attached to the Ports |
| 958 | ArrayList<DeviceEvent> devicesToRemove = new ArrayList<>(); |
| 959 | ArrayList<Port> ports = new ArrayList<>(); |
| 960 | ports.add(srcPort); |
| 961 | ports.add(dstPort); |
| 962 | for (Port port : ports) { |
| 963 | for (Device device : port.getDevices()) { |
| 964 | log.error("Device {} on Port {} should have been removed prior to adding Link {}", |
| 965 | device, port, linkEvent); |
| 966 | DeviceEvent deviceEvent = |
| 967 | new DeviceEvent(device.getMacAddress()); |
| 968 | SwitchPort switchPort = |
| 969 | new SwitchPort(port.getSwitch().getDpid(), |
| 970 | port.getNumber()); |
| 971 | deviceEvent.addAttachmentPoint(switchPort); |
| 972 | devicesToRemove.add(deviceEvent); |
| 973 | } |
| 974 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 975 | for (DeviceEvent deviceEvent : devicesToRemove) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 976 | removeDevice(deviceEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 977 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 978 | } else { |
| 979 | // TODO: Update the link attributes |
Ray Milkey | 1aa71f8 | 2014-04-08 16:23:24 -0700 | [diff] [blame] | 980 | log.debug("Update link attributes"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 981 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 982 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 983 | apiAddedLinkEvents.add(linkEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 984 | } |
| 985 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 986 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 987 | * Remove a link from the topology. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 988 | * |
| 989 | * @param linkEvent the Link Event with the link to remove. |
| 990 | */ |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 991 | private void removeLink(LinkEvent linkEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 992 | Port srcPort = topology.getPort(linkEvent.getSrc().dpid, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 993 | linkEvent.getSrc().number); |
| 994 | if (srcPort == null) { |
| 995 | log.warn("Src Port for Link {} already removed, ignoring", |
| 996 | linkEvent); |
| 997 | return; |
| 998 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 999 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1000 | Port dstPort = topology.getPort(linkEvent.getDst().dpid, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1001 | linkEvent.getDst().number); |
| 1002 | if (dstPort == null) { |
| 1003 | log.warn("Dst Port for Link {} already removed, ignoring", |
| 1004 | linkEvent); |
| 1005 | return; |
| 1006 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1007 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1008 | // |
| 1009 | // Remove the Link instance from the Destination Port Incoming Link |
| 1010 | // and the Source Port Outgoing Link. |
| 1011 | // |
| 1012 | Link link = dstPort.getIncomingLink(); |
| 1013 | if (link == null) { |
| 1014 | log.warn("Link {} already removed on destination Port", linkEvent); |
| 1015 | } |
| 1016 | link = srcPort.getOutgoingLink(); |
| 1017 | if (link == null) { |
| 1018 | log.warn("Link {} already removed on src Port", linkEvent); |
| 1019 | } |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 1020 | |
| 1021 | // TODO should we check that we get the same link from each port? |
| 1022 | if (link != null) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1023 | topology.removeLink(link); |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 1024 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1025 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1026 | apiRemovedLinkEvents.add(linkEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1027 | } |
| 1028 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1029 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1030 | * Add a device to the topology. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1031 | * <p/> |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1032 | * TODO: Device-related work is incomplete. |
| 1033 | * TODO: Eventually, we might need to consider reordering |
| 1034 | * or addLink() and addDevice() events on the same port. |
| 1035 | * |
| 1036 | * @param deviceEvent the Device Event with the device to add. |
| 1037 | */ |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1038 | private void addDevice(DeviceEvent deviceEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1039 | log.debug("Adding a device to the topology with mac {}", deviceEvent.getMac()); |
| 1040 | Device device = topology.getDeviceByMac(deviceEvent.getMac()); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1041 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1042 | if (device == null) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1043 | log.debug("Existing device was not found in the Topology: Adding new device: mac {}", deviceEvent.getMac()); |
| 1044 | device = new DeviceImpl(topology, deviceEvent.getMac()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1045 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1046 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1047 | DeviceImpl deviceImpl = getDeviceImpl(device); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1048 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1049 | // Process each attachment point |
| 1050 | boolean attachmentFound = false; |
| 1051 | for (SwitchPort swp : deviceEvent.getAttachmentPoints()) { |
| 1052 | // Attached Ports must exist |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1053 | Port port = topology.getPort(swp.dpid, swp.number); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1054 | if (port == null) { |
| 1055 | // Reordered event: delay the event in local cache |
| 1056 | ByteBuffer id = deviceEvent.getIDasByteBuffer(); |
| 1057 | reorderedAddedDeviceEvents.put(id, deviceEvent); |
| 1058 | continue; |
| 1059 | } |
| 1060 | // Attached Ports must not have Link |
| 1061 | if (port.getOutgoingLink() != null || |
| 1062 | port.getIncomingLink() != null) { |
| 1063 | log.warn("Link (Out:{},In:{}) exist on the attachment point, skipping mutation.", |
| 1064 | port.getOutgoingLink(), |
| 1065 | port.getIncomingLink()); |
| 1066 | continue; |
| 1067 | } |
| 1068 | |
| 1069 | // Add Device <-> Port attachment |
| 1070 | PortImpl portImpl = getPortImpl(port); |
| 1071 | portImpl.addDevice(device); |
| 1072 | deviceImpl.addAttachmentPoint(port); |
| 1073 | attachmentFound = true; |
| 1074 | } |
| 1075 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1076 | // Update the device in the topology |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1077 | if (attachmentFound) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1078 | log.debug("Storing the device info into the Topology: mac {}", deviceEvent.getMac()); |
| 1079 | topology.putDevice(device); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1080 | apiAddedDeviceEvents.add(deviceEvent); |
| 1081 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1084 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1085 | * Remove a device from the topology. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1086 | * <p/> |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1087 | * TODO: Device-related work is incomplete. |
| 1088 | * |
| 1089 | * @param deviceEvent the Device Event with the device to remove. |
| 1090 | */ |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1091 | private void removeDevice(DeviceEvent deviceEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1092 | log.debug("Removing a device to the topology: mac {}", deviceEvent.getMac()); |
| 1093 | Device device = topology.getDeviceByMac(deviceEvent.getMac()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1094 | if (device == null) { |
| 1095 | log.warn("Device {} already removed, ignoring", deviceEvent); |
| 1096 | return; |
| 1097 | } |
| 1098 | DeviceImpl deviceImpl = getDeviceImpl(device); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1099 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1100 | // Process each attachment point |
| 1101 | for (SwitchPort swp : deviceEvent.getAttachmentPoints()) { |
| 1102 | // Attached Ports must exist |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1103 | Port port = topology.getPort(swp.dpid, swp.number); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1104 | if (port == null) { |
| 1105 | log.warn("Port for the attachment point {} did not exist. skipping attachment point mutation", swp); |
| 1106 | continue; |
| 1107 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1108 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1109 | // Remove Device <-> Port attachment |
| 1110 | PortImpl portImpl = getPortImpl(port); |
| 1111 | portImpl.removeDevice(device); |
| 1112 | deviceImpl.removeAttachmentPoint(port); |
| 1113 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1114 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1115 | log.debug("Removing the device info into the Topology: mac {}", deviceEvent.getMac()); |
| 1116 | topology.removeDevice(device); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1117 | apiRemovedDeviceEvents.add(deviceEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1118 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 1119 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1120 | /** |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1121 | * Get the SwitchImpl-casted switch implementation. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1122 | * |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1123 | * @param sw the Switch to cast. |
| 1124 | * @return the SwitchImpl-casted switch implementation. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1125 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1126 | private SwitchImpl getSwitchImpl(Switch sw) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1127 | if (sw instanceof SwitchImpl) { |
| 1128 | return (SwitchImpl) sw; |
| 1129 | } |
| 1130 | throw new ClassCastException("SwitchImpl expected, but found: " + sw); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1131 | } |
| 1132 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1133 | /** |
| 1134 | * Get the PortImpl-casted port implementation. |
| 1135 | * |
| 1136 | * @param port the Port to cast. |
| 1137 | * @return the PortImpl-casted port implementation. |
| 1138 | */ |
| 1139 | private PortImpl getPortImpl(Port port) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1140 | if (port instanceof PortImpl) { |
| 1141 | return (PortImpl) port; |
| 1142 | } |
| 1143 | throw new ClassCastException("PortImpl expected, but found: " + port); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1144 | } |
| 1145 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1146 | /** |
| 1147 | * Get the LinkImpl-casted link implementation. |
| 1148 | * |
| 1149 | * @param link the Link to cast. |
| 1150 | * @return the LinkImpl-casted link implementation. |
| 1151 | */ |
| 1152 | private LinkImpl getLinkImpl(Link link) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1153 | if (link instanceof LinkImpl) { |
| 1154 | return (LinkImpl) link; |
| 1155 | } |
| 1156 | throw new ClassCastException("LinkImpl expected, but found: " + link); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1157 | } |
| 1158 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1159 | /** |
| 1160 | * Get the DeviceImpl-casted device implementation. |
| 1161 | * |
| 1162 | * @param device the Device to cast. |
| 1163 | * @return the DeviceImpl-casted device implementation. |
| 1164 | */ |
| 1165 | private DeviceImpl getDeviceImpl(Device device) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1166 | if (device instanceof DeviceImpl) { |
| 1167 | return (DeviceImpl) device; |
| 1168 | } |
| 1169 | throw new ClassCastException("DeviceImpl expected, but found: " + device); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1170 | } |
| 1171 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1172 | /** |
| 1173 | * Read the whole topology from the database. |
| 1174 | * |
| 1175 | * @return a collection of EventEntry-encapsulated Topology Events for |
| 1176 | * the whole topology. |
| 1177 | */ |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1178 | private Collection<EventEntry<TopologyEvent>> readWholeTopologyFromDB() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1179 | Collection<EventEntry<TopologyEvent>> collection = |
| 1180 | new LinkedList<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1181 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1182 | // XXX May need to clear whole topology first, depending on |
| 1183 | // how we initially subscribe to replication events |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1184 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1185 | // Add all active switches |
| 1186 | for (KVSwitch sw : KVSwitch.getAllSwitches()) { |
| 1187 | if (sw.getStatus() != KVSwitch.STATUS.ACTIVE) { |
| 1188 | continue; |
| 1189 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1190 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1191 | SwitchEvent switchEvent = new SwitchEvent(sw.getDpid()); |
| 1192 | TopologyEvent topologyEvent = new TopologyEvent(switchEvent); |
| 1193 | EventEntry<TopologyEvent> eventEntry = |
| 1194 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1195 | topologyEvent); |
| 1196 | collection.add(eventEntry); |
| 1197 | } |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 1198 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1199 | // Add all active ports |
| 1200 | for (KVPort p : KVPort.getAllPorts()) { |
| 1201 | if (p.getStatus() != KVPort.STATUS.ACTIVE) { |
| 1202 | continue; |
| 1203 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1204 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1205 | PortEvent portEvent = new PortEvent(p.getDpid(), p.getNumber()); |
| 1206 | TopologyEvent topologyEvent = new TopologyEvent(portEvent); |
| 1207 | EventEntry<TopologyEvent> eventEntry = |
| 1208 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1209 | topologyEvent); |
| 1210 | collection.add(eventEntry); |
| 1211 | } |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 1212 | |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 1213 | for (KVDevice d : KVDevice.getAllDevices()) { |
| 1214 | DeviceEvent devEvent = new DeviceEvent(MACAddress.valueOf(d.getMac())); |
| 1215 | for (byte[] portId : d.getAllPortIds()) { |
| 1216 | devEvent.addAttachmentPoint(new SwitchPort(KVPort.getDpidFromKey(portId), KVPort.getNumberFromKey(portId))); |
| 1217 | } |
| 1218 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1219 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1220 | for (KVLink l : KVLink.getAllLinks()) { |
| 1221 | LinkEvent linkEvent = new LinkEvent(l.getSrc().dpid, |
| 1222 | l.getSrc().number, |
| 1223 | l.getDst().dpid, |
| 1224 | l.getDst().number); |
| 1225 | TopologyEvent topologyEvent = new TopologyEvent(linkEvent); |
| 1226 | EventEntry<TopologyEvent> eventEntry = |
| 1227 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1228 | topologyEvent); |
| 1229 | collection.add(eventEntry); |
| 1230 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1231 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1232 | return collection; |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1233 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 1234 | } |