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