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 | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 25 | import net.onrc.onos.ofcontroller.util.Dpid; |
Yuta HIGUCHI | 170229f | 2014-02-17 15:47:54 -0800 | [diff] [blame] | 26 | import net.onrc.onos.registry.controller.IControllerRegistryService; |
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. |
| 33 | * |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 34 | * - Maintain Invariant/Relationships between Topology Objects. |
| 35 | * |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 36 | * TODO To be synchronized based on TopologyEvent Notification. |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 37 | * |
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). |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 40 | * |
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 | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 43 | * |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 44 | */ |
Pavlin Radoslavov | 87dcc26 | 2014-02-19 21:13:23 -0800 | [diff] [blame] | 45 | public class TopologyManager implements NetworkGraphDiscoveryInterface { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 46 | |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 47 | private static final Logger log = LoggerFactory |
Pavlin Radoslavov | db7dbb2 | 2014-02-18 14:45:10 -0800 | [diff] [blame] | 48 | .getLogger(TopologyManager.class); |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 49 | |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 50 | private IEventChannel<byte[], TopologyEvent> eventChannel; |
| 51 | private static final String EVENT_CHANNEL_NAME = "onos.topology"; |
| 52 | private EventHandler eventHandler = new EventHandler(); |
| 53 | |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 54 | private final NetworkGraphDatastore datastore; |
Pavlin Radoslavov | 6d224ee | 2014-02-18 16:43:15 -0800 | [diff] [blame] | 55 | private final NetworkGraphImpl networkGraph = new NetworkGraphImpl(); |
Yuta HIGUCHI | 170229f | 2014-02-17 15:47:54 -0800 | [diff] [blame] | 56 | private final IControllerRegistryService registryService; |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 57 | private CopyOnWriteArrayList<INetworkGraphListener> networkGraphListeners; |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 58 | |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame^] | 59 | // Local state for computing the final set of events |
| 60 | private Map<ByteBuffer, SwitchEvent> addedSwitchEvents = |
| 61 | new HashMap<ByteBuffer, SwitchEvent>(); |
| 62 | private Map<ByteBuffer, SwitchEvent> removedSwitchEvents = |
| 63 | new HashMap<ByteBuffer, SwitchEvent>(); |
| 64 | private Map<ByteBuffer, PortEvent> addedPortEvents = |
| 65 | new HashMap<ByteBuffer, PortEvent>(); |
| 66 | private Map<ByteBuffer, PortEvent> removedPortEvents = |
| 67 | new HashMap<ByteBuffer, PortEvent>(); |
| 68 | private Map<ByteBuffer, LinkEvent> addedLinkEvents = |
| 69 | new HashMap<ByteBuffer, LinkEvent>(); |
| 70 | private Map<ByteBuffer, LinkEvent> removedLinkEvents = |
| 71 | new HashMap<ByteBuffer, LinkEvent>(); |
| 72 | private Map<ByteBuffer, DeviceEvent> addedDeviceEvents = |
| 73 | new HashMap<ByteBuffer, DeviceEvent>(); |
| 74 | private Map<ByteBuffer, DeviceEvent> removedDeviceEvents = |
| 75 | new HashMap<ByteBuffer, DeviceEvent>(); |
| 76 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 77 | /** |
| 78 | * Constructor. |
| 79 | * |
| 80 | * @param registryService the Registry Service to use. |
| 81 | * @param networkGraphListeners the collection of Network Graph Listeners |
| 82 | * to use. |
| 83 | */ |
| 84 | public TopologyManager(IControllerRegistryService registryService, |
| 85 | CopyOnWriteArrayList<INetworkGraphListener> networkGraphListeners) { |
Jonathan Hart | daea86f | 2014-02-19 15:28:42 -0800 | [diff] [blame] | 86 | datastore = new NetworkGraphDatastore(); |
Yuta HIGUCHI | 170229f | 2014-02-17 15:47:54 -0800 | [diff] [blame] | 87 | this.registryService = registryService; |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 88 | this.networkGraphListeners = networkGraphListeners; |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 89 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 90 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 91 | /** |
| 92 | * Get the Network Graph. |
| 93 | * |
| 94 | * @return the Network Graph. |
| 95 | */ |
Pavlin Radoslavov | 6d224ee | 2014-02-18 16:43:15 -0800 | [diff] [blame] | 96 | NetworkGraph getNetworkGraph() { |
| 97 | return networkGraph; |
| 98 | } |
| 99 | |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 100 | /** |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 101 | * Event handler class. |
| 102 | */ |
| 103 | private class EventHandler extends Thread implements |
| 104 | IEventChannelListener<byte[], TopologyEvent> { |
| 105 | private BlockingQueue<EventEntry<TopologyEvent>> topologyEvents = |
| 106 | new LinkedBlockingQueue<EventEntry<TopologyEvent>>(); |
| 107 | |
| 108 | /** |
| 109 | * Startup processing. |
| 110 | */ |
| 111 | private void startup() { |
| 112 | // |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame^] | 113 | // TODO: Read all state from the database: |
| 114 | // |
| 115 | // Collection<EventEntry<TopologyEvent>> collection = |
| 116 | // readWholeTopologyFromDB(); |
| 117 | // |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 118 | // For now, as a shortcut we read it from the datagrid |
| 119 | // |
| 120 | Collection<TopologyEvent> topologyEvents = |
| 121 | eventChannel.getAllEntries(); |
| 122 | Collection<EventEntry<TopologyEvent>> collection = |
| 123 | new LinkedList<EventEntry<TopologyEvent>>(); |
| 124 | |
| 125 | for (TopologyEvent topologyEvent : topologyEvents) { |
| 126 | EventEntry<TopologyEvent> eventEntry = |
| 127 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 128 | topologyEvent); |
| 129 | collection.add(eventEntry); |
| 130 | } |
| 131 | processEvents(collection); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Run the thread. |
| 136 | */ |
Yuta HIGUCHI | 240bf07 | 2014-02-17 10:55:21 -0800 | [diff] [blame] | 137 | @Override |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 138 | public void run() { |
| 139 | Collection<EventEntry<TopologyEvent>> collection = |
| 140 | new LinkedList<EventEntry<TopologyEvent>>(); |
| 141 | |
Pavlin Radoslavov | db7dbb2 | 2014-02-18 14:45:10 -0800 | [diff] [blame] | 142 | this.setName("TopologyManager.EventHandler " + this.getId()); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 143 | startup(); |
| 144 | |
| 145 | // |
| 146 | // The main loop |
| 147 | // |
| 148 | try { |
| 149 | while (true) { |
| 150 | EventEntry<TopologyEvent> eventEntry = topologyEvents.take(); |
| 151 | collection.add(eventEntry); |
| 152 | topologyEvents.drainTo(collection); |
| 153 | |
| 154 | processEvents(collection); |
| 155 | collection.clear(); |
| 156 | } |
| 157 | } catch (Exception exception) { |
| 158 | log.debug("Exception processing Topology Events: ", exception); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Process all topology events. |
| 164 | * |
| 165 | * @param events the events to process. |
| 166 | */ |
| 167 | private void processEvents(Collection<EventEntry<TopologyEvent>> events) { |
| 168 | for (EventEntry<TopologyEvent> event : events) { |
| 169 | TopologyEvent topologyEvent = event.eventData(); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame^] | 170 | SwitchEvent switchEvent = topologyEvent.switchEvent; |
| 171 | PortEvent portEvent = topologyEvent.portEvent; |
| 172 | LinkEvent linkEvent = topologyEvent.linkEvent; |
| 173 | DeviceEvent deviceEvent = topologyEvent.deviceEvent; |
| 174 | |
| 175 | // |
| 176 | // Extract the events by cancelling matching events |
| 177 | // |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 178 | switch (event.eventType()) { |
| 179 | case ENTRY_ADD: |
| 180 | log.debug("Topology event ENTRY_ADD: {}", topologyEvent); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame^] | 181 | if (switchEvent != null) { |
| 182 | ByteBuffer id = ByteBuffer.wrap(switchEvent.getID()); |
| 183 | addedSwitchEvents.put(id, switchEvent); |
| 184 | removedSwitchEvents.remove(id); |
| 185 | } |
| 186 | if (portEvent != null) { |
| 187 | ByteBuffer id = ByteBuffer.wrap(portEvent.getID()); |
| 188 | addedPortEvents.put(id, portEvent); |
| 189 | removedPortEvents.remove(id); |
| 190 | } |
| 191 | if (linkEvent != null) { |
| 192 | ByteBuffer id = ByteBuffer.wrap(linkEvent.getID()); |
| 193 | addedLinkEvents.put(id, linkEvent); |
| 194 | removedLinkEvents.remove(id); |
| 195 | } |
| 196 | if (deviceEvent != null) { |
| 197 | ByteBuffer id = ByteBuffer.wrap(deviceEvent.getID()); |
| 198 | addedDeviceEvents.put(id, deviceEvent); |
| 199 | removedDeviceEvents.remove(id); |
| 200 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 201 | break; |
| 202 | case ENTRY_REMOVE: |
| 203 | log.debug("Topology event ENTRY_REMOVE: {}", topologyEvent); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame^] | 204 | if (switchEvent != null) { |
| 205 | ByteBuffer id = ByteBuffer.wrap(switchEvent.getID()); |
| 206 | addedSwitchEvents.remove(id); |
| 207 | removedSwitchEvents.put(id, switchEvent); |
| 208 | } |
| 209 | if (portEvent != null) { |
| 210 | ByteBuffer id = ByteBuffer.wrap(portEvent.getID()); |
| 211 | addedPortEvents.remove(id); |
| 212 | removedPortEvents.put(id, portEvent); |
| 213 | } |
| 214 | if (linkEvent != null) { |
| 215 | ByteBuffer id = ByteBuffer.wrap(linkEvent.getID()); |
| 216 | addedLinkEvents.remove(id); |
| 217 | removedLinkEvents.put(id, linkEvent); |
| 218 | } |
| 219 | if (deviceEvent != null) { |
| 220 | ByteBuffer id = ByteBuffer.wrap(deviceEvent.getID()); |
| 221 | addedDeviceEvents.remove(id); |
| 222 | removedDeviceEvents.put(id, deviceEvent); |
| 223 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 224 | break; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Receive a notification that an entry is added. |
| 231 | * |
| 232 | * @param value the value for the entry. |
| 233 | */ |
| 234 | @Override |
| 235 | public void entryAdded(TopologyEvent value) { |
| 236 | EventEntry<TopologyEvent> eventEntry = |
| 237 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 238 | value); |
| 239 | topologyEvents.add(eventEntry); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Receive a notification that an entry is removed. |
| 244 | * |
| 245 | * @param value the value for the entry. |
| 246 | */ |
| 247 | @Override |
| 248 | public void entryRemoved(TopologyEvent value) { |
| 249 | EventEntry<TopologyEvent> eventEntry = |
| 250 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_REMOVE, |
| 251 | value); |
| 252 | topologyEvents.add(eventEntry); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Receive a notification that an entry is updated. |
| 257 | * |
| 258 | * @param value the value for the entry. |
| 259 | */ |
| 260 | @Override |
| 261 | public void entryUpdated(TopologyEvent value) { |
| 262 | // NOTE: The ADD and UPDATE events are processed in same way |
| 263 | entryAdded(value); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Startup processing. |
| 269 | * |
| 270 | * @param datagridService the datagrid service to use. |
| 271 | */ |
| 272 | void startup(IDatagridService datagridService) { |
| 273 | eventChannel = datagridService.addListener(EVENT_CHANNEL_NAME, |
| 274 | eventHandler, |
| 275 | byte[].class, |
| 276 | TopologyEvent.class); |
| 277 | eventHandler.start(); |
| 278 | } |
| 279 | |
| 280 | /** |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 281 | * Exception to be thrown when Modification to the Network Graph cannot be |
| 282 | * continued due to broken invariant. |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 283 | * |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 284 | * XXX Should this be checked exception or RuntimeException? |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 285 | */ |
| 286 | public static class BrokenInvariantException extends RuntimeException { |
| 287 | private static final long serialVersionUID = 1L; |
| 288 | |
| 289 | public BrokenInvariantException() { |
| 290 | super(); |
| 291 | } |
| 292 | |
| 293 | public BrokenInvariantException(String message) { |
| 294 | super(message); |
| 295 | } |
| 296 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 297 | |
| 298 | /* ****************************** |
| 299 | * NetworkGraphDiscoveryInterface methods |
| 300 | * ******************************/ |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 301 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 302 | @Override |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame^] | 303 | public void putSwitchDiscoveryEvent(SwitchEvent switchEvent, |
| 304 | Collection<PortEvent> portEvents) { |
| 305 | if (datastore.addSwitch(switchEvent, portEvents)) { |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 306 | // Send out notification |
Pavlin Radoslavov | 87dcc26 | 2014-02-19 21:13:23 -0800 | [diff] [blame] | 307 | TopologyEvent topologyEvent = new TopologyEvent(switchEvent); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 308 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame^] | 309 | |
| 310 | for (PortEvent portEvent : portEvents) { |
| 311 | topologyEvent = new TopologyEvent(portEvent); |
| 312 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
| 313 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 314 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 318 | public void removeSwitchDiscoveryEvent(SwitchEvent switchEvent) { |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame^] | 319 | // TODO: Use a copy of the port events previously added for that switch |
| 320 | Collection<PortEvent> portEvents = new LinkedList<PortEvent>(); |
| 321 | |
| 322 | if (datastore.deactivateSwitch(switchEvent, portEvents)) { |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 323 | // Send out notification |
| 324 | eventChannel.removeEntry(switchEvent.getID()); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame^] | 325 | |
| 326 | for (PortEvent portEvent : portEvents) { |
| 327 | eventChannel.removeEntry(portEvent.getID()); |
| 328 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 329 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 333 | public void putPortDiscoveryEvent(PortEvent portEvent) { |
Pavlin Radoslavov | 50cd148 | 2014-02-19 16:57:03 -0800 | [diff] [blame] | 334 | if (datastore.addPort(portEvent)) { |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 335 | // Send out notification |
Pavlin Radoslavov | 87dcc26 | 2014-02-19 21:13:23 -0800 | [diff] [blame] | 336 | TopologyEvent topologyEvent = new TopologyEvent(portEvent); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame^] | 337 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 338 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 342 | public void removePortDiscoveryEvent(PortEvent portEvent) { |
Pavlin Radoslavov | 50cd148 | 2014-02-19 16:57:03 -0800 | [diff] [blame] | 343 | if (datastore.deactivatePort(portEvent)) { |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 344 | // Send out notification |
| 345 | eventChannel.removeEntry(portEvent.getID()); |
| 346 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 350 | public void putLinkDiscoveryEvent(LinkEvent linkEvent) { |
Pavlin Radoslavov | 50cd148 | 2014-02-19 16:57:03 -0800 | [diff] [blame] | 351 | if (datastore.addLink(linkEvent)) { |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 352 | // Send out notification |
Pavlin Radoslavov | 87dcc26 | 2014-02-19 21:13:23 -0800 | [diff] [blame] | 353 | TopologyEvent topologyEvent = new TopologyEvent(linkEvent); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 354 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
| 355 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 359 | public void removeLinkDiscoveryEvent(LinkEvent linkEvent) { |
Pavlin Radoslavov | 50cd148 | 2014-02-19 16:57:03 -0800 | [diff] [blame] | 360 | if (datastore.removeLink(linkEvent)) { |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 361 | // Send out notification |
| 362 | eventChannel.removeEntry(linkEvent.getID()); |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 363 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 364 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 365 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 366 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 367 | public void putDeviceDiscoveryEvent(DeviceEvent deviceEvent) { |
Pavlin Radoslavov | 50cd148 | 2014-02-19 16:57:03 -0800 | [diff] [blame] | 368 | if (datastore.addDevice(deviceEvent)) { |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 369 | // Send out notification |
Pavlin Radoslavov | 87dcc26 | 2014-02-19 21:13:23 -0800 | [diff] [blame] | 370 | TopologyEvent topologyEvent = new TopologyEvent(deviceEvent); |
Pavlin Radoslavov | 50cd148 | 2014-02-19 16:57:03 -0800 | [diff] [blame] | 371 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 372 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 373 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 374 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 375 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 376 | public void removeDeviceDiscoveryEvent(DeviceEvent deviceEvent) { |
Pavlin Radoslavov | 50cd148 | 2014-02-19 16:57:03 -0800 | [diff] [blame] | 377 | if (datastore.removeDevice(deviceEvent)) { |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 378 | // Send out notification |
| 379 | eventChannel.removeEntry(deviceEvent.getID()); |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 380 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 381 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 382 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 383 | /* ***************** |
| 384 | * Internal methods to maintain invariants of the network graph |
| 385 | * *****************/ |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 386 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 387 | /** |
| 388 | * |
| 389 | * @param swEvent |
| 390 | * @return true if ready to accept event. |
| 391 | */ |
| 392 | private boolean prepareForAddSwitchEvent(SwitchEvent swEvent) { |
| 393 | // No show stopping precondition |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 394 | return true; |
| 395 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 396 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 397 | private boolean prepareForRemoveSwitchEvent(SwitchEvent swEvent) { |
| 398 | // No show stopping precondition |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 399 | return true; |
| 400 | } |
Yuta HIGUCHI | 71e7a05 | 2014-02-17 22:14:15 -0800 | [diff] [blame] | 401 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 402 | private boolean prepareForAddPortEvent(PortEvent portEvent) { |
| 403 | // Parent Switch must exist |
| 404 | if (networkGraph.getSwitch(portEvent.getDpid()) == null) { |
| 405 | log.warn("Dropping add port event because switch doesn't exist: {}", |
| 406 | portEvent); |
| 407 | return false; |
| 408 | } |
| 409 | // Prep: None |
| 410 | return true; |
| 411 | } |
| 412 | |
| 413 | private boolean prepareForRemovePortEvent(PortEvent portEvent) { |
| 414 | Port port = networkGraph.getPort(portEvent.getDpid(), |
| 415 | portEvent.getNumber()); |
| 416 | if (port == null) { |
| 417 | log.debug("Port already removed? {}", portEvent); |
| 418 | // let it pass |
| 419 | return true; |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 420 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 421 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 422 | // Prep: Remove Link and Device Attachment |
| 423 | ArrayList<DeviceEvent> deviceEvents = new ArrayList<>(); |
| 424 | for (Device device : port.getDevices()) { |
| 425 | log.debug("Removing Device {} on Port {}", device, portEvent); |
| 426 | DeviceEvent devEvent = new DeviceEvent(device.getMacAddress()); |
| 427 | devEvent.addAttachmentPoint(new SwitchPort(port.getSwitch().getDpid(), |
| 428 | port.getNumber())); |
| 429 | deviceEvents.add(devEvent); |
| 430 | } |
| 431 | for (DeviceEvent devEvent : deviceEvents) { |
| 432 | // calling Discovery API to wipe from DB, etc. |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 433 | removeDeviceDiscoveryEvent(devEvent); |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 434 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 435 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 436 | Set<Link> links = new HashSet<>(); |
| 437 | links.add(port.getOutgoingLink()); |
| 438 | links.add(port.getIncomingLink()); |
| 439 | for (Link link : links) { |
| 440 | if (link == null) { |
| 441 | continue; |
| 442 | } |
| 443 | log.debug("Removing Link {} on Port {}", link, portEvent); |
| 444 | LinkEvent linkEvent = |
Pavlin Radoslavov | 7c8f69a | 2014-02-19 19:01:45 -0800 | [diff] [blame] | 445 | new LinkEvent(link.getSrcSwitch().getDpid(), |
| 446 | link.getSrcPort().getNumber(), |
| 447 | link.getDstSwitch().getDpid(), |
| 448 | link.getDstPort().getNumber()); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 449 | // calling Discovery API to wipe from DB, etc. |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 450 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 451 | // Call internal remove Link, which will check |
| 452 | // 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] | 453 | removeLinkDiscoveryEvent(linkEvent, true); |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 454 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 455 | return true; |
| 456 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 457 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 458 | private boolean prepareForAddLinkEvent(LinkEvent linkEvent) { |
| 459 | // Src/Dst Port must exist |
| 460 | Port srcPort = networkGraph.getPort(linkEvent.getSrc().dpid, |
| 461 | linkEvent.getSrc().number); |
| 462 | Port dstPort = networkGraph.getPort(linkEvent.getDst().dpid, |
| 463 | linkEvent.getDst().number); |
| 464 | if (srcPort == null || dstPort == null) { |
Jonathan Hart | 0a4846e | 2014-02-18 11:03:40 -0800 | [diff] [blame] | 465 | log.warn("Dropping add link event because port doesn't exist: {}", |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 466 | linkEvent); |
| 467 | return false; |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 468 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 469 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 470 | // Prep: remove Device attachment on both Ports |
| 471 | ArrayList<DeviceEvent> deviceEvents = new ArrayList<>(); |
| 472 | for (Device device : srcPort.getDevices()) { |
| 473 | DeviceEvent devEvent = new DeviceEvent(device.getMacAddress()); |
| 474 | devEvent.addAttachmentPoint(new SwitchPort(srcPort.getSwitch().getDpid(), srcPort.getNumber())); |
| 475 | deviceEvents.add(devEvent); |
| 476 | } |
| 477 | for (Device device : dstPort.getDevices()) { |
| 478 | DeviceEvent devEvent = new DeviceEvent(device.getMacAddress()); |
| 479 | devEvent.addAttachmentPoint(new SwitchPort(dstPort.getSwitch().getDpid(), |
| 480 | dstPort.getNumber())); |
| 481 | deviceEvents.add(devEvent); |
| 482 | } |
| 483 | for (DeviceEvent devEvent : deviceEvents) { |
| 484 | // calling Discovery API to wipe from DB, etc. |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 485 | removeDeviceDiscoveryEvent(devEvent); |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 486 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 487 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 488 | return true; |
| 489 | } |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 490 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 491 | private boolean prepareForRemoveLinkEvent(LinkEvent linkEvent) { |
| 492 | // Src/Dst Port must exist |
| 493 | Port srcPort = networkGraph.getPort(linkEvent.getSrc().dpid, |
| 494 | linkEvent.getSrc().number); |
| 495 | Port dstPort = networkGraph.getPort(linkEvent.getDst().dpid, |
| 496 | linkEvent.getDst().number); |
| 497 | if (srcPort == null || dstPort == null) { |
| 498 | log.warn("Dropping remove link event because port doesn't exist {}", linkEvent); |
| 499 | return false; |
| 500 | } |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 501 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 502 | Link link = srcPort.getOutgoingLink(); |
| 503 | |
| 504 | // Link is already gone, or different Link exist in memory |
| 505 | // XXX Check if we should reject or just accept these cases. |
| 506 | // it should be harmless to remove the Link on event from DB anyways |
| 507 | if (link == null || |
Pavlin Radoslavov | 7c8f69a | 2014-02-19 19:01:45 -0800 | [diff] [blame] | 508 | !link.getDstPort().getNumber().equals(linkEvent.getDst().number) |
| 509 | || !link.getDstSwitch().getDpid().equals(linkEvent.getDst().dpid)) { |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 510 | log.warn("Dropping remove link event because link doesn't exist: {}", linkEvent); |
| 511 | return false; |
| 512 | } |
| 513 | // Prep: None |
| 514 | return true; |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * |
| 519 | * @param deviceEvent Event will be modified to remove inapplicable attachemntPoints/ipAddress |
| 520 | * @return false if this event should be dropped. |
| 521 | */ |
| 522 | private boolean prepareForAddDeviceEvent(DeviceEvent deviceEvent) { |
| 523 | boolean preconditionBroken = false; |
| 524 | ArrayList<PortEvent.SwitchPort> failedSwitchPort = new ArrayList<>(); |
| 525 | for ( PortEvent.SwitchPort swp : deviceEvent.getAttachmentPoints() ) { |
| 526 | // Attached Ports must exist |
| 527 | Port port = networkGraph.getPort(swp.dpid, swp.number); |
| 528 | if (port == null) { |
| 529 | preconditionBroken = true; |
| 530 | failedSwitchPort.add(swp); |
| 531 | continue; |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 532 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 533 | // Attached Ports must not have Link |
| 534 | if (port.getOutgoingLink() != null || |
| 535 | port.getIncomingLink() != null) { |
| 536 | preconditionBroken = true; |
| 537 | failedSwitchPort.add(swp); |
| 538 | continue; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | // Rewriting event to exclude failed attachmentPoint |
| 543 | // XXX Assumption behind this is that inapplicable device event should |
| 544 | // be dropped, not deferred. If we decide to defer Device event, |
| 545 | // rewriting can become a problem |
| 546 | List<SwitchPort> attachmentPoints = deviceEvent.getAttachmentPoints(); |
| 547 | attachmentPoints.removeAll(failedSwitchPort); |
| 548 | deviceEvent.setAttachmentPoints(attachmentPoints); |
| 549 | |
| 550 | if (deviceEvent.getAttachmentPoints().isEmpty() && |
| 551 | deviceEvent.getIpAddresses().isEmpty()) { |
| 552 | // return false to represent: Nothing left to do for this event. |
| 553 | // Caller should drop event |
| 554 | return false; |
| 555 | } |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 556 | |
| 557 | // Should we return false to tell caller that the event was trimmed? |
| 558 | // if ( preconditionBroken ) { |
| 559 | // return false; |
| 560 | // } |
| 561 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 562 | return true; |
| 563 | } |
| 564 | |
| 565 | private boolean prepareForRemoveDeviceEvent(DeviceEvent deviceEvent) { |
| 566 | // No show stopping precondition? |
| 567 | // Prep: none |
| 568 | return true; |
| 569 | } |
| 570 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 571 | /* ************************************************ |
| 572 | * Internal In-memory object mutation methods. |
| 573 | * ************************************************/ |
| 574 | |
| 575 | void putSwitch(SwitchEvent swEvent) { |
| 576 | if (swEvent == null) { |
| 577 | throw new IllegalArgumentException("Switch cannot be null"); |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 578 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 579 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 580 | Switch sw = networkGraph.getSwitch(swEvent.getDpid()); |
| 581 | |
| 582 | if (sw == null) { |
| 583 | sw = new SwitchImpl(networkGraph, swEvent.getDpid()); |
| 584 | networkGraph.putSwitch(sw); |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 585 | } |
Yuta HIGUCHI | 928fa68 | 2014-02-11 19:07:57 -0800 | [diff] [blame] | 586 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 587 | // Update when more attributes are added to Event object |
| 588 | // no attribute to update for now |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | void removeSwitch(SwitchEvent swEvent) { |
| 592 | if (swEvent == null) { |
| 593 | throw new IllegalArgumentException("Switch cannot be null"); |
Yuta HIGUCHI | 928fa68 | 2014-02-11 19:07:57 -0800 | [diff] [blame] | 594 | } |
| 595 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 596 | Switch sw = networkGraph.getSwitch(swEvent.getDpid()); |
| 597 | if (sw == null) { |
| 598 | log.warn("Switch {} already removed, ignoring", swEvent); |
| 599 | return; |
Yuta HIGUCHI | 928fa68 | 2014-02-11 19:07:57 -0800 | [diff] [blame] | 600 | } |
| 601 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 602 | // remove all ports if there still exist |
| 603 | ArrayList<PortEvent> portsToRemove = new ArrayList<>(); |
| 604 | for (Port port : sw.getPorts()) { |
| 605 | log.warn("Port {} on Switch {} should be removed prior to removing Switch. Removing Port now", |
| 606 | port, swEvent); |
| 607 | PortEvent portEvent = new PortEvent(port.getDpid(), |
| 608 | port.getNumber()); |
| 609 | portsToRemove.add(portEvent); |
| 610 | } |
| 611 | for (PortEvent portEvent : portsToRemove) { |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 612 | // XXX calling removePortDiscoveryEvent() may trigger duplicate |
| 613 | // event, once at prepare phase, second time here |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 614 | // If event can be squashed, ignored etc. at receiver side it |
| 615 | // shouldn't be a problem, but if not need to re-visit this issue. |
| 616 | |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 617 | // Note: removePortDiscoveryEvent() implies removal of attached |
| 618 | // Device, etc. if we decide not to call |
| 619 | // removePortDiscoveryEvent(), Device needs to be handled properly. |
| 620 | removePortDiscoveryEvent(portEvent); |
Yuta HIGUCHI | 928fa68 | 2014-02-11 19:07:57 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 623 | networkGraph.removeSwitch(swEvent.getDpid()); |
| 624 | } |
| 625 | |
| 626 | void putPort(PortEvent portEvent) { |
| 627 | if (portEvent == null) { |
| 628 | throw new IllegalArgumentException("Port cannot be null"); |
Yuta HIGUCHI | 928fa68 | 2014-02-11 19:07:57 -0800 | [diff] [blame] | 629 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 630 | Switch sw = networkGraph.getSwitch(portEvent.getDpid()); |
| 631 | if (sw == null) { |
| 632 | throw new BrokenInvariantException(String.format( |
Yuta HIGUCHI | 76df247 | 2014-02-12 22:36:51 -0800 | [diff] [blame] | 633 | "Switch with dpid %s did not exist.", |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 634 | new Dpid(portEvent.getDpid()))); |
| 635 | } |
| 636 | Port p = sw.getPort(portEvent.getNumber()); |
| 637 | PortImpl port = null; |
| 638 | if (p != null) { |
| 639 | port = getPortImpl(p); |
Yuta HIGUCHI | 76df247 | 2014-02-12 22:36:51 -0800 | [diff] [blame] | 640 | } |
| 641 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 642 | if (port == null) { |
| 643 | port = new PortImpl(networkGraph, sw, portEvent.getNumber()); |
Yuta HIGUCHI | 76df247 | 2014-02-12 22:36:51 -0800 | [diff] [blame] | 644 | } |
| 645 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 646 | // TODO update attributes |
Yuta HIGUCHI | 76df247 | 2014-02-12 22:36:51 -0800 | [diff] [blame] | 647 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 648 | SwitchImpl s = getSwitchImpl(sw); |
| 649 | s.addPort(port); |
| 650 | } |
| 651 | |
| 652 | void removePort(PortEvent portEvent) { |
| 653 | if (portEvent == null) { |
| 654 | throw new IllegalArgumentException("Port cannot be null"); |
| 655 | } |
| 656 | |
| 657 | Switch sw = networkGraph.getSwitch(portEvent.getDpid()); |
| 658 | if (sw == null) { |
| 659 | log.warn("Parent Switch for Port {} already removed, ignoring", |
| 660 | portEvent); |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | Port p = sw.getPort(portEvent.getNumber()); |
| 665 | if (p == null) { |
| 666 | log.warn("Port {} already removed, ignoring", portEvent); |
| 667 | return; |
| 668 | } |
| 669 | |
| 670 | // Remove Link and Device Attachment |
| 671 | for (Device device : p.getDevices()) { |
| 672 | log.debug("Removing Device {} on Port {}", device, portEvent); |
| 673 | DeviceEvent devEvent = new DeviceEvent(device.getMacAddress()); |
| 674 | devEvent.addAttachmentPoint(new SwitchPort(p.getSwitch().getDpid(), |
| 675 | p.getNumber())); |
| 676 | |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 677 | // XXX calling removeDeviceDiscoveryEvent() may trigger duplicate |
| 678 | // event, once at prepare phase, second time here. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 679 | // If event can be squashed, ignored etc. at receiver side it |
| 680 | // shouldn't be a problem, but if not need to re-visit |
| 681 | |
| 682 | // calling Discovery API to wipe from DB, etc. |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 683 | removeDeviceDiscoveryEvent(devEvent); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 684 | } |
| 685 | Set<Link> links = new HashSet<>(); |
| 686 | links.add(p.getOutgoingLink()); |
| 687 | links.add(p.getIncomingLink()); |
| 688 | ArrayList<LinkEvent> linksToRemove = new ArrayList<>(); |
| 689 | for (Link link : links) { |
| 690 | if (link == null) { |
| 691 | continue; |
| 692 | } |
| 693 | log.debug("Removing Link {} on Port {}", link, portEvent); |
Pavlin Radoslavov | 7c8f69a | 2014-02-19 19:01:45 -0800 | [diff] [blame] | 694 | LinkEvent linkEvent = new LinkEvent(link.getSrcSwitch().getDpid(), |
| 695 | link.getSrcPort().getNumber(), |
| 696 | link.getDstSwitch().getDpid(), |
| 697 | link.getDstPort().getNumber()); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 698 | linksToRemove.add(linkEvent); |
| 699 | } |
| 700 | for (LinkEvent linkEvent : linksToRemove) { |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 701 | // XXX calling removeLinkDiscoveryEvent() may trigger duplicate |
| 702 | // event, once at prepare phase, second time here. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 703 | // If event can be squashed, ignored etc. at receiver side it |
| 704 | // shouldn't be a problem, but if not need to re-visit |
| 705 | |
| 706 | // calling Discovery API to wipe from DB, etc. |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 707 | removeLinkDiscoveryEvent(linkEvent); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | // remove Port from Switch |
| 711 | SwitchImpl s = getSwitchImpl(sw); |
| 712 | s.removePort(p); |
| 713 | } |
| 714 | |
| 715 | void putLink(LinkEvent linkEvent) { |
| 716 | if (linkEvent == null) { |
| 717 | throw new IllegalArgumentException("Link cannot be null"); |
| 718 | } |
| 719 | |
| 720 | Port srcPort = networkGraph.getPort(linkEvent.getSrc().dpid, |
| 721 | linkEvent.getSrc().number); |
| 722 | if (srcPort == null) { |
| 723 | throw new BrokenInvariantException( |
Yuta HIGUCHI | 76df247 | 2014-02-12 22:36:51 -0800 | [diff] [blame] | 724 | String.format( |
| 725 | "Src Port %s of a Link did not exist.", |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 726 | linkEvent.getSrc() )); |
| 727 | } |
Yuta HIGUCHI | 76df247 | 2014-02-12 22:36:51 -0800 | [diff] [blame] | 728 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 729 | Port dstPort = networkGraph.getPort(linkEvent.getDst().dpid, |
| 730 | linkEvent.getDst().number); |
| 731 | if (dstPort == null) { |
| 732 | throw new BrokenInvariantException( |
Yuta HIGUCHI | 76df247 | 2014-02-12 22:36:51 -0800 | [diff] [blame] | 733 | String.format( |
| 734 | "Dst Port %s of a Link did not exist.", |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 735 | linkEvent.getDst())); |
Yuta HIGUCHI | 76df247 | 2014-02-12 22:36:51 -0800 | [diff] [blame] | 736 | } |
| 737 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 738 | // getting Link instance from destination port incoming Link |
| 739 | Link l = dstPort.getIncomingLink(); |
| 740 | LinkImpl link = null; |
| 741 | assert(l == srcPort.getOutgoingLink()); |
| 742 | if (l != null) { |
| 743 | link = getLinkImpl(l); |
Yuta HIGUCHI | 76df247 | 2014-02-12 22:36:51 -0800 | [diff] [blame] | 744 | } |
| 745 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 746 | if (link == null) { |
| 747 | link = new LinkImpl(networkGraph, srcPort, dstPort); |
Yuta HIGUCHI | 76df247 | 2014-02-12 22:36:51 -0800 | [diff] [blame] | 748 | } |
| 749 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 750 | PortImpl dstPortMem = getPortImpl(dstPort); |
| 751 | PortImpl srcPortMem = getPortImpl(srcPort); |
Yuta HIGUCHI | 76df247 | 2014-02-12 22:36:51 -0800 | [diff] [blame] | 752 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 753 | // Add Link first to avoid further Device addition |
Yuta HIGUCHI | 76df247 | 2014-02-12 22:36:51 -0800 | [diff] [blame] | 754 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 755 | // add Link to Port |
| 756 | dstPortMem.setIncomingLink(link); |
| 757 | srcPortMem.setOutgoingLink(link); |
Yuta HIGUCHI | 76df247 | 2014-02-12 22:36:51 -0800 | [diff] [blame] | 758 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 759 | // remove Device Pointing to Port if any |
| 760 | for (Device d : dstPortMem.getDevices() ) { |
| 761 | log.error("Device {} on Port {} should have been removed prior to adding Link {}", |
| 762 | d, dstPort, linkEvent); |
| 763 | DeviceImpl dev = getDeviceImpl(d); |
| 764 | dev.removeAttachmentPoint(dstPort); |
| 765 | // This implies that change is made to Device Object. |
| 766 | // sending Device attachment point removed event |
| 767 | DeviceEvent rmEvent = new DeviceEvent(d.getMacAddress()); |
| 768 | rmEvent.addAttachmentPoint(new SwitchPort(dstPort.getDpid(), |
| 769 | dstPort.getNumber())); |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 770 | removeDeviceDiscoveryEvent(rmEvent); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 771 | } |
| 772 | dstPortMem.removeAllDevice(); |
| 773 | for (Device d : srcPortMem.getDevices() ) { |
| 774 | log.error("Device {} on Port {} should have been removed prior to adding Link {}", |
| 775 | d, srcPort, linkEvent); |
| 776 | DeviceImpl dev = getDeviceImpl(d); |
| 777 | dev.removeAttachmentPoint(srcPort); |
| 778 | // This implies that change is made to Device Object. |
| 779 | // sending Device attachment point removed event |
| 780 | DeviceEvent rmEvent = new DeviceEvent(d.getMacAddress()); |
| 781 | rmEvent.addAttachmentPoint(new SwitchPort(dstPort.getDpid(), |
| 782 | dstPort.getNumber())); |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 783 | removeDeviceDiscoveryEvent(rmEvent); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 784 | } |
| 785 | srcPortMem.removeAllDevice(); |
| 786 | } |
| 787 | |
| 788 | void removeLink(LinkEvent linkEvent) { |
| 789 | if (linkEvent == null) { |
| 790 | throw new IllegalArgumentException("Link cannot be null"); |
Yuta HIGUCHI | 76df247 | 2014-02-12 22:36:51 -0800 | [diff] [blame] | 791 | } |
| 792 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 793 | Port srcPort = networkGraph.getPort(linkEvent.getSrc().dpid, |
| 794 | linkEvent.getSrc().number); |
| 795 | if (srcPort == null) { |
| 796 | log.warn("Src Port for Link {} already removed, ignoring", |
| 797 | linkEvent); |
| 798 | return; |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 799 | } |
| 800 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 801 | Port dstPort = networkGraph.getPort(linkEvent.getDst().dpid, |
| 802 | linkEvent.getDst().number); |
| 803 | if (dstPort == null) { |
| 804 | log.warn("Dst Port for Link {} already removed, ignoring", |
| 805 | linkEvent); |
| 806 | return; |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 807 | } |
| 808 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 809 | Link l = dstPort.getIncomingLink(); |
| 810 | if ( l == null) { |
| 811 | log.warn("Link {} already removed on destination Port", linkEvent); |
| 812 | } |
| 813 | l = srcPort.getOutgoingLink(); |
| 814 | if (l == null) { |
| 815 | log.warn("Link {} already removed on src Port", linkEvent); |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 816 | } |
| 817 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 818 | getPortImpl(dstPort).setIncomingLink(null); |
| 819 | getPortImpl(srcPort).setOutgoingLink(null); |
| 820 | } |
| 821 | |
| 822 | // XXX Need to rework Device related |
| 823 | void putDevice(DeviceEvent deviceEvent) { |
| 824 | if (deviceEvent == null) { |
| 825 | throw new IllegalArgumentException("Device cannot be null"); |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 826 | } |
| 827 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 828 | Device device = networkGraph.getDeviceByMac(deviceEvent.getMac()); |
| 829 | if (device == null) { |
| 830 | device = new DeviceImpl(networkGraph, deviceEvent.getMac()); |
| 831 | } |
| 832 | DeviceImpl memDevice = getDeviceImpl(device); |
| 833 | |
| 834 | // for each IP address |
| 835 | for (InetAddress ipAddr : deviceEvent.getIpAddresses()) { |
| 836 | memDevice.addIpAddress(ipAddr); |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 837 | } |
| 838 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 839 | networkGraph.putDevice(device); |
| 840 | |
| 841 | // for each attachment point |
| 842 | for (SwitchPort swp : deviceEvent.getAttachmentPoints()) { |
| 843 | // Attached Ports must exist |
| 844 | Port port = networkGraph.getPort(swp.dpid, swp.number); |
| 845 | if (port == null) { |
| 846 | log.warn("Port for the attachment point {} did not exist. skipping mutation", swp); |
| 847 | continue; |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 848 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 849 | // Attached Ports must not have Link |
| 850 | if (port.getOutgoingLink() != null || |
| 851 | port.getIncomingLink() != null) { |
| 852 | log.warn("Link (Out:{},In:{}) exist on the attachment point, skipping mutation.", |
| 853 | port.getOutgoingLink(), |
| 854 | port.getIncomingLink()); |
| 855 | continue; |
| 856 | } |
| 857 | |
| 858 | // finally add Device <-> Port on In-memory structure |
| 859 | PortImpl memPort = getPortImpl(port); |
| 860 | memPort.addDevice(device); |
| 861 | memDevice.addAttachmentPoint(port); |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | void removeDevice(DeviceEvent deviceEvent) { |
| 866 | if (deviceEvent == null) { |
| 867 | throw new IllegalArgumentException("Device cannot be null"); |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 868 | } |
| 869 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 870 | Device device = networkGraph.getDeviceByMac(deviceEvent.getMac()); |
| 871 | if (device == null) { |
| 872 | log.warn("Device {} already removed, ignoring", deviceEvent); |
| 873 | return; |
| 874 | } |
| 875 | DeviceImpl memDevice = getDeviceImpl(device); |
| 876 | |
| 877 | // for each attachment point |
| 878 | for (SwitchPort swp : deviceEvent.getAttachmentPoints()) { |
| 879 | // Attached Ports must exist |
| 880 | Port port = networkGraph.getPort(swp.dpid, swp.number); |
| 881 | if (port == null) { |
| 882 | log.warn("Port for the attachment point {} did not exist. skipping attachment point mutation", swp); |
| 883 | continue; |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 884 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 885 | |
| 886 | // finally remove Device <-> Port on In-memory structure |
| 887 | PortImpl memPort = getPortImpl(port); |
| 888 | memPort.removeDevice(device); |
| 889 | memDevice.removeAttachmentPoint(port); |
| 890 | } |
| 891 | networkGraph.removeDevice(device); |
| 892 | } |
| 893 | |
| 894 | private void dispatchPutSwitchEvent(SwitchEvent switchEvent) { |
| 895 | for (INetworkGraphListener listener : this.networkGraphListeners) { |
| 896 | // TODO Should copy before handing them over to listener |
| 897 | listener.putSwitchEvent(switchEvent); |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | private void dispatchRemoveSwitchEvent(SwitchEvent switchEvent) { |
| 902 | for (INetworkGraphListener listener : this.networkGraphListeners) { |
| 903 | // TODO Should copy before handing them over to listener |
| 904 | listener.removeSwitchEvent(switchEvent); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | private void dispatchPutPortEvent(PortEvent portEvent) { |
| 909 | for (INetworkGraphListener listener : this.networkGraphListeners) { |
| 910 | // TODO Should copy before handing them over to listener |
| 911 | listener.putPortEvent(portEvent); |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | private void dispatchRemovePortEvent(PortEvent portEvent) { |
| 916 | for (INetworkGraphListener listener : this.networkGraphListeners) { |
| 917 | // TODO Should copy before handing them over to listener |
| 918 | listener.removePortEvent(portEvent); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | private void dispatchPutLinkEvent(LinkEvent linkEvent) { |
| 923 | for (INetworkGraphListener listener : this.networkGraphListeners) { |
| 924 | // TODO Should copy before handing them over to listener |
| 925 | listener.putLinkEvent(linkEvent); |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | private void dispatchRemoveLinkEvent(LinkEvent linkEvent) { |
| 930 | for (INetworkGraphListener listener : this.networkGraphListeners) { |
| 931 | // TODO Should copy before handing them over to listener |
| 932 | listener.removeLinkEvent(linkEvent); |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | private void dispatchPutDeviceEvent(DeviceEvent deviceEvent) { |
| 937 | for (INetworkGraphListener listener : this.networkGraphListeners) { |
| 938 | // TODO Should copy before handing them over to listener |
| 939 | listener.putDeviceEvent(deviceEvent);; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | private void dispatchRemoveDeviceEvent(DeviceEvent deviceEvent) { |
| 944 | for (INetworkGraphListener listener : this.networkGraphListeners) { |
| 945 | // TODO Should copy before handing them over to listener |
| 946 | listener.removeDeviceEvent(deviceEvent); |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | private SwitchImpl getSwitchImpl(Switch sw) { |
| 951 | if (sw instanceof SwitchImpl) { |
| 952 | return (SwitchImpl) sw; |
| 953 | } |
| 954 | throw new ClassCastException("SwitchImpl expected, but found: " + sw); |
| 955 | } |
| 956 | |
| 957 | private PortImpl getPortImpl(Port p) { |
| 958 | if (p instanceof PortImpl) { |
| 959 | return (PortImpl) p; |
| 960 | } |
| 961 | throw new ClassCastException("PortImpl expected, but found: " + p); |
| 962 | } |
| 963 | |
| 964 | private LinkImpl getLinkImpl(Link l) { |
| 965 | if (l instanceof LinkImpl) { |
| 966 | return (LinkImpl) l; |
| 967 | } |
| 968 | throw new ClassCastException("LinkImpl expected, but found: " + l); |
| 969 | } |
| 970 | |
| 971 | private DeviceImpl getDeviceImpl(Device d) { |
| 972 | if (d instanceof DeviceImpl) { |
| 973 | return (DeviceImpl) d; |
| 974 | } |
| 975 | throw new ClassCastException("DeviceImpl expected, but found: " + d); |
| 976 | } |
| 977 | |
| 978 | @Deprecated |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame^] | 979 | private Collection<EventEntry<TopologyEvent>> readWholeTopologyFromDB() { |
| 980 | Collection<EventEntry<TopologyEvent>> collection = |
| 981 | new LinkedList<EventEntry<TopologyEvent>>(); |
| 982 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 983 | // XXX May need to clear whole topology first, depending on |
| 984 | // how we initially subscribe to replication events |
| 985 | |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame^] | 986 | // Add all active switches |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 987 | for (RCSwitch sw : RCSwitch.getAllSwitches()) { |
| 988 | if (sw.getStatus() != RCSwitch.STATUS.ACTIVE) { |
| 989 | continue; |
| 990 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame^] | 991 | |
| 992 | SwitchEvent switchEvent = new SwitchEvent(sw.getDpid()); |
| 993 | TopologyEvent topologyEvent = new TopologyEvent(switchEvent); |
| 994 | EventEntry<TopologyEvent> eventEntry = |
| 995 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 996 | topologyEvent); |
| 997 | collection.add(eventEntry); |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 998 | } |
| 999 | |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame^] | 1000 | // Add all active ports |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1001 | for (RCPort p : RCPort.getAllPorts()) { |
| 1002 | if (p.getStatus() != RCPort.STATUS.ACTIVE) { |
| 1003 | continue; |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 1004 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame^] | 1005 | |
| 1006 | PortEvent portEvent = new PortEvent(p.getDpid(), p.getNumber()); |
| 1007 | TopologyEvent topologyEvent = new TopologyEvent(portEvent); |
| 1008 | EventEntry<TopologyEvent> eventEntry = |
| 1009 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1010 | topologyEvent); |
| 1011 | collection.add(eventEntry); |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 1012 | } |
| 1013 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1014 | // TODO Is Device going to be in DB? If so, read from DB. |
| 1015 | // for (RCDevice d : RCDevice.getAllDevices()) { |
| 1016 | // DeviceEvent devEvent = new DeviceEvent( MACAddress.valueOf(d.getMac()) ); |
| 1017 | // for (byte[] portId : d.getAllPortIds() ) { |
| 1018 | // devEvent.addAttachmentPoint( new SwitchPort( RCPort.getDpidFromKey(portId), RCPort.getNumberFromKey(portId) )); |
| 1019 | // } |
| 1020 | // } |
| 1021 | |
| 1022 | for (RCLink l : RCLink.getAllLinks()) { |
| 1023 | // check if src/dst switch/port exist before triggering event |
| 1024 | Port srcPort = networkGraph.getPort(l.getSrc().dpid, |
| 1025 | l.getSrc().number); |
| 1026 | Port dstPort = networkGraph.getPort(l.getDst().dpid, |
| 1027 | l.getDst().number); |
| 1028 | if (srcPort == null || dstPort == null) { |
| 1029 | continue; |
Yuta HIGUCHI | 76df247 | 2014-02-12 22:36:51 -0800 | [diff] [blame] | 1030 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame^] | 1031 | |
| 1032 | LinkEvent linkEvent = new LinkEvent(l.getSrc().dpid, |
| 1033 | l.getSrc().number, |
| 1034 | l.getDst().dpid, |
| 1035 | l.getDst().number); |
| 1036 | TopologyEvent topologyEvent = new TopologyEvent(linkEvent); |
| 1037 | EventEntry<TopologyEvent> eventEntry = |
| 1038 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1039 | topologyEvent); |
| 1040 | collection.add(eventEntry); |
Yuta HIGUCHI | 76df247 | 2014-02-12 22:36:51 -0800 | [diff] [blame] | 1041 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame^] | 1042 | |
| 1043 | return collection; |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1044 | } |
Pavlin Radoslavov | 50cd148 | 2014-02-19 16:57:03 -0800 | [diff] [blame] | 1045 | |
| 1046 | @Deprecated |
| 1047 | private void removeLinkDiscoveryEvent(LinkEvent linkEvent, |
| 1048 | boolean dstCheckBeforeDBmodify) { |
| 1049 | if (prepareForRemoveLinkEvent(linkEvent)) { |
| 1050 | if (dstCheckBeforeDBmodify) { |
| 1051 | // write to DB only if it is owner of the dst dpid |
| 1052 | // XXX this will cause link remove events to be dropped |
| 1053 | // if the dst switch just disconnected |
| 1054 | if (registryService.hasControl(linkEvent.getDst().dpid)) { |
| 1055 | datastore.removeLink(linkEvent); |
| 1056 | } |
| 1057 | } else { |
| 1058 | datastore.removeLink(linkEvent); |
| 1059 | } |
| 1060 | removeLink(linkEvent); |
| 1061 | // Send out notification |
| 1062 | eventChannel.removeEntry(linkEvent.getID()); |
| 1063 | } |
| 1064 | // TODO handle invariant violation |
| 1065 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 1066 | } |