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