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