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