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