blob: 101f628e5e5f4a762797612d1667fbbf189b0fb3 [file] [log] [blame]
Jonathan Hart062a2e82014-02-03 09:41:57 -08001package net.onrc.onos.ofcontroller.networkgraph;
2
Yuta HIGUCHI1c700102014-02-12 16:30:52 -08003import java.net.InetAddress;
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -08004import java.util.ArrayList;
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -08005import java.util.Collection;
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -08006import java.util.HashSet;
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -08007import java.util.LinkedList;
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -08008import java.util.List;
9import java.util.Set;
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -080010import java.util.concurrent.BlockingQueue;
Yuta HIGUCHIa536e762014-02-17 21:47:28 -080011import java.util.concurrent.CopyOnWriteArrayList;
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -080012import java.util.concurrent.LinkedBlockingQueue;
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080013
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -080014import net.onrc.onos.datagrid.IDatagridService;
15import net.onrc.onos.datagrid.IEventChannel;
16import net.onrc.onos.datagrid.IEventChannelListener;
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -080017import net.onrc.onos.datastore.topology.RCLink;
Jonathan Hart062a2e82014-02-03 09:41:57 -080018import net.onrc.onos.datastore.topology.RCPort;
19import net.onrc.onos.datastore.topology.RCSwitch;
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080020import net.onrc.onos.ofcontroller.networkgraph.PortEvent.SwitchPort;
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -080021import net.onrc.onos.ofcontroller.util.EventEntry;
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -080022import net.onrc.onos.ofcontroller.util.Dpid;
Yuta HIGUCHI170229f2014-02-17 15:47:54 -080023import net.onrc.onos.registry.controller.IControllerRegistryService;
Jonathan Hart062a2e82014-02-03 09:41:57 -080024
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080028/**
29 * The "NB" read-only Network Map.
30 *
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080031 * - Maintain Invariant/Relationships between Topology Objects.
32 *
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -080033 * TODO To be synchronized based on TopologyEvent Notification.
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080034 *
Yuta HIGUCHIcb951982014-02-11 13:31:44 -080035 * TODO TBD: Caller is expected to maintain parent/child calling order. Parent
Yuta HIGUCHI1c700102014-02-12 16:30:52 -080036 * Object must exist before adding sub component(Add Switch -> Port).
Yuta HIGUCHIcb951982014-02-11 13:31:44 -080037 *
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080038 * 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 HIGUCHIcb951982014-02-11 13:31:44 -080040 *
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080041 */
Yuta HIGUCHI928fa682014-02-11 19:07:57 -080042public class NetworkGraphImpl extends AbstractNetworkGraph implements
43 NetworkGraphDiscoveryInterface, NetworkGraphReplicationInterface {
Jonathan Hart062a2e82014-02-03 09:41:57 -080044
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080045 private static final Logger log = LoggerFactory
46 .getLogger(NetworkGraphImpl.class);
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -080047
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -080048 private IEventChannel<byte[], TopologyEvent> eventChannel;
49 private static final String EVENT_CHANNEL_NAME = "onos.topology";
50 private EventHandler eventHandler = new EventHandler();
51
Jonathan Hart22eb9882014-02-11 15:52:59 -080052 private final NetworkGraphDatastore datastore;
Yuta HIGUCHI170229f2014-02-17 15:47:54 -080053 private final IControllerRegistryService registryService;
Yuta HIGUCHIa536e762014-02-17 21:47:28 -080054 private CopyOnWriteArrayList<INetworkGraphListener> networkGraphListeners;
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080055
Yuta HIGUCHIa536e762014-02-17 21:47:28 -080056 public NetworkGraphImpl(IControllerRegistryService registryService, CopyOnWriteArrayList<INetworkGraphListener> networkGraphListeners) {
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080057 super();
Jonathan Hart22eb9882014-02-11 15:52:59 -080058 datastore = new NetworkGraphDatastore(this);
Yuta HIGUCHI170229f2014-02-17 15:47:54 -080059 this.registryService = registryService;
Yuta HIGUCHIa536e762014-02-17 21:47:28 -080060 this.networkGraphListeners = networkGraphListeners;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080061 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080062
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080063 /**
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -080064 * 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 HIGUCHI240bf072014-02-17 10:55:21 -080096 @Override
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -080097 public void run() {
98 Collection<EventEntry<TopologyEvent>> collection =
99 new LinkedList<EventEntry<TopologyEvent>>();
100
101 this.setName("NetworkGraphImpl.EventHandler " + this.getId());
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 HIGUCHI170229f2014-02-17 15:47:54 -0800128 if (event.eventData().getOriginID().equals(registryService.getControllerId())) {
129 // ignore event triggered by myself
130 continue;
131 }
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800132 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 HIGUCHIcb951982014-02-11 13:31:44 -0800212 * 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 Hart22eb9882014-02-11 15:52:59 -0800227
228 /* ******************************
229 * NetworkGraphDiscoveryInterface methods
230 * ******************************/
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800231
Jonathan Hart22eb9882014-02-11 15:52:59 -0800232 @Override
233 public void putSwitchEvent(SwitchEvent switchEvent) {
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800234 if (prepareForAddSwitchEvent(switchEvent)) {
Jonathan Hart22eb9882014-02-11 15:52:59 -0800235 datastore.addSwitch(switchEvent);
236 putSwitch(switchEvent);
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800237 // Send out notification
238 TopologyEvent topologyEvent =
Yuta HIGUCHI170229f2014-02-17 15:47:54 -0800239 new TopologyEvent(switchEvent, registryService.getControllerId());
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800240 eventChannel.addEntry(topologyEvent.getID(),
241 topologyEvent);
Jonathan Hart22eb9882014-02-11 15:52:59 -0800242 }
243 // TODO handle invariant violation
244 }
245
246 @Override
247 public void removeSwitchEvent(SwitchEvent switchEvent) {
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800248 if (prepareForRemoveSwitchEvent(switchEvent)) {
Jonathan Hart22eb9882014-02-11 15:52:59 -0800249 datastore.deactivateSwitch(switchEvent);
250 removeSwitch(switchEvent);
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800251 // Send out notification
252 eventChannel.removeEntry(switchEvent.getID());
Jonathan Hart22eb9882014-02-11 15:52:59 -0800253 }
254 // TODO handle invariant violation
255 }
256
257 @Override
258 public void putPortEvent(PortEvent portEvent) {
Jonathan Hart4c263272014-02-13 17:41:05 -0800259 if (prepareForAddPortEvent(portEvent)) {
260 datastore.addPort(portEvent);
261 putPort(portEvent);
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800262 // Send out notification
263 TopologyEvent topologyEvent =
Yuta HIGUCHI170229f2014-02-17 15:47:54 -0800264 new TopologyEvent(portEvent, registryService.getControllerId());
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800265 eventChannel.addEntry(topologyEvent.getID(),
266 topologyEvent);
Jonathan Hart4c263272014-02-13 17:41:05 -0800267 }
Yuta HIGUCHI75c51ed2014-02-13 17:02:26 -0800268 // TODO handle invariant violation
Jonathan Hart22eb9882014-02-11 15:52:59 -0800269 }
270
271 @Override
272 public void removePortEvent(PortEvent portEvent) {
Jonathan Hart4c263272014-02-13 17:41:05 -0800273 if (prepareForRemovePortEvent(portEvent)) {
274 datastore.deactivatePort(portEvent);
275 removePort(portEvent);
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800276 // Send out notification
277 eventChannel.removeEntry(portEvent.getID());
Jonathan Hart4c263272014-02-13 17:41:05 -0800278 }
Yuta HIGUCHI75c51ed2014-02-13 17:02:26 -0800279 // TODO handle invariant violation
Jonathan Hart22eb9882014-02-11 15:52:59 -0800280 }
281
282 @Override
283 public void putLinkEvent(LinkEvent linkEvent) {
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800284 if (prepareForAddLinkEvent(linkEvent)) {
Jonathan Hart22eb9882014-02-11 15:52:59 -0800285 datastore.addLink(linkEvent);
286 putLink(linkEvent);
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800287 // Send out notification
288 TopologyEvent topologyEvent =
Yuta HIGUCHI170229f2014-02-17 15:47:54 -0800289 new TopologyEvent(linkEvent, registryService.getControllerId());
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800290 eventChannel.addEntry(topologyEvent.getID(),
291 topologyEvent);
Jonathan Hart22eb9882014-02-11 15:52:59 -0800292 }
293 // TODO handle invariant violation
294 }
295
296 @Override
297 public void removeLinkEvent(LinkEvent linkEvent) {
Yuta HIGUCHI71e7a052014-02-17 22:14:15 -0800298 removeLinkEvent(linkEvent, false);
299
300 }
301
302 private void removeLinkEvent(LinkEvent linkEvent, boolean dstCheckBeforeDBmodify) {
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800303 if (prepareForRemoveLinkEvent(linkEvent)) {
Yuta HIGUCHI71e7a052014-02-17 22:14:15 -0800304 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 Hart22eb9882014-02-11 15:52:59 -0800312 removeLink(linkEvent);
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800313 // Send out notification
314 eventChannel.removeEntry(linkEvent.getID());
Jonathan Hart22eb9882014-02-11 15:52:59 -0800315 }
316 // TODO handle invariant violation
317 }
318
319 @Override
Yuta HIGUCHI586d33e2014-02-13 17:05:08 -0800320 public void putDeviceEvent(DeviceEvent deviceEvent) {
321 if (prepareForAddDeviceEvent(deviceEvent)) {
322// datastore.addDevice(deviceEvent);
Yuta HIGUCHId457c052014-02-14 18:33:04 -0800323// putDevice(deviceEvent);
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800324 // Send out notification
325 TopologyEvent topologyEvent =
Yuta HIGUCHI170229f2014-02-17 15:47:54 -0800326 new TopologyEvent(deviceEvent, registryService.getControllerId());
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800327 eventChannel.addEntry(topologyEvent.getID(),
328 topologyEvent);
Yuta HIGUCHI586d33e2014-02-13 17:05:08 -0800329 }
330 // TODO handle invariant violation
331 // XXX if prepareFor~ method returned false, event should be dropped
Jonathan Hart22eb9882014-02-11 15:52:59 -0800332 }
333
334 @Override
335 public void removeDeviceEvent(DeviceEvent deviceEvent) {
Yuta HIGUCHI586d33e2014-02-13 17:05:08 -0800336 if (prepareForRemoveDeviceEvent(deviceEvent)) {
337// datastore.removeDevice(deviceEvent);
Yuta HIGUCHId457c052014-02-14 18:33:04 -0800338// removeDevice(deviceEvent);
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800339 // Send out notification
340 eventChannel.removeEntry(deviceEvent.getID());
Yuta HIGUCHI586d33e2014-02-13 17:05:08 -0800341 }
342 // TODO handle invariant violation
343 // XXX if prepareFor~ method returned false, event should be dropped
Jonathan Hart22eb9882014-02-11 15:52:59 -0800344 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800345
Jonathan Hart22eb9882014-02-11 15:52:59 -0800346 /* *****************
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800347 * Internal methods to maintain invariants of the network graph
Jonathan Hart22eb9882014-02-11 15:52:59 -0800348 * *****************/
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800349
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800350 /**
351 *
352 * @param swEvt
353 * @return true if ready to accept event.
354 */
355 private boolean prepareForAddSwitchEvent(SwitchEvent swEvt) {
Yuta HIGUCHI76df2472014-02-12 22:36:51 -0800356 // No show stopping precondition
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800357 // Prep: remove(deactivate) Ports on Switch, which is not on event
358 removePortsNotOnEvent(swEvt);
359 return true;
Jonathan Hart22eb9882014-02-11 15:52:59 -0800360 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800361
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800362 private boolean prepareForRemoveSwitchEvent(SwitchEvent swEvt) {
Yuta HIGUCHI76df2472014-02-12 22:36:51 -0800363 // No show stopping precondition
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800364 // 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 HIGUCHId02e9282014-02-12 09:24:41 -0800369
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800370 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 Hart480c5572014-02-14 18:28:16 -0800379 List<Port> portsToRemove = new ArrayList<Port>();
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800380 for( Port p : sw.getPorts() ) {
381 if ( !port_noOnEvent.contains(p.getNumber()) ) {
Jonathan Hart480c5572014-02-14 18:28:16 -0800382 //PortEvent rmEvent = new PortEvent(p.getSwitch().getDpid(), p.getNumber());
383 // calling Discovery removePort() API to wipe from DB, etc.
384 //removePortEvent(rmEvent);
Yuta HIGUCHI240bf072014-02-17 10:55:21 -0800385
Jonathan Hart480c5572014-02-14 18:28:16 -0800386 // 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 HIGUCHI8d762e92014-02-12 14:10:25 -0800393 PortEvent rmEvent = new PortEvent(p.getSwitch().getDpid(), p.getNumber());
394 // calling Discovery removePort() API to wipe from DB, etc.
395 removePortEvent(rmEvent);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800396 }
397 }
Jonathan Hart22eb9882014-02-11 15:52:59 -0800398 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800399
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800400 private boolean prepareForAddPortEvent(PortEvent portEvt) {
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800401 // Parent Switch must exist
402 if ( getSwitch(portEvt.getDpid()) == null) {
Jonathan Hart0a4846e2014-02-18 11:03:40 -0800403 log.warn("Dropping add port event because switch doesn't exist: {}",
404 portEvt);
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800405 return false;
406 }
407 // Prep: None
Jonathan Hart22eb9882014-02-11 15:52:59 -0800408 return true;
409 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800410
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800411 private boolean prepareForRemovePortEvent(PortEvent portEvt) {
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800412 // Parent Switch must exist
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800413 Switch sw = getSwitch(portEvt.getDpid());
414 if ( sw == null ) {
Yuta HIGUCHI88be0f22014-02-14 17:20:43 -0800415 log.debug("Switch already removed? {}", portEvt);
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800416 return false;
417 }
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800418 Port port = sw.getPort(portEvt.getNumber());
419 if ( port == null ) {
420 log.debug("Port already removed? {}", portEvt);
421 // let it pass
422 return true;
423 }
424
425 // Prep: Remove Link and Device Attachment
Yuta HIGUCHI240bf072014-02-17 10:55:21 -0800426 ArrayList<DeviceEvent> deviceEvts = new ArrayList<>();
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800427 for (Device device : port.getDevices()) {
Yuta HIGUCHI88be0f22014-02-14 17:20:43 -0800428 log.debug("Removing Device {} on Port {}", device, portEvt);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800429 DeviceEvent devEvt = new DeviceEvent(device.getMacAddress());
430 devEvt.addAttachmentPoint(new SwitchPort(port.getSwitch().getDpid(), port.getNumber()));
Yuta HIGUCHI240bf072014-02-17 10:55:21 -0800431 deviceEvts.add(devEvt);
432 }
433 for (DeviceEvent devEvt : deviceEvts) {
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800434 // calling Discovery API to wipe from DB, etc.
435 removeDeviceEvent(devEvt);
436 }
Yuta HIGUCHI240bf072014-02-17 10:55:21 -0800437
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800438 Set<Link> links = new HashSet<>();
439 links.add(port.getOutgoingLink());
440 links.add(port.getIncomingLink());
441 for ( Link link : links) {
442 if (link == null ) {
443 continue;
444 }
Yuta HIGUCHI88be0f22014-02-14 17:20:43 -0800445 log.debug("Removing Link {} on Port {}", link, portEvt);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800446 LinkEvent linkEvent = new LinkEvent(link.getSourceSwitchDpid(), link.getSourcePortNumber(), link.getDestinationSwitchDpid(), link.getDestinationPortNumber());
447 // calling Discovery API to wipe from DB, etc.
Yuta HIGUCHI71e7a052014-02-17 22:14:15 -0800448
449 // Call internal remove Link, which will check
450 // ownership of DST dpid and modify DB only if it is the owner
451 removeLinkEvent(linkEvent, true);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800452 }
Jonathan Hart22eb9882014-02-11 15:52:59 -0800453 return true;
454 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800455
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800456 private boolean prepareForAddLinkEvent(LinkEvent linkEvt) {
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800457 // Src/Dst Switch must exist
458 Switch srcSw = getSwitch(linkEvt.getSrc().dpid);
459 Switch dstSw = getSwitch(linkEvt.getDst().dpid);
460 if ( srcSw == null || dstSw == null ) {
Jonathan Hart0a4846e2014-02-18 11:03:40 -0800461 log.warn("Dropping add link event because switch doesn't exist: {}",
462 linkEvt);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800463 return false;
464 }
465 // Src/Dst Port must exist
466 Port srcPort = srcSw.getPort(linkEvt.getSrc().number);
Jonathan Hart4c263272014-02-13 17:41:05 -0800467 Port dstPort = dstSw.getPort(linkEvt.getDst().number);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800468 if ( srcPort == null || dstPort == null ) {
Jonathan Hart0a4846e2014-02-18 11:03:40 -0800469 log.warn("Dropping add link event because port doesn't exist: {}",
470 linkEvt);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800471 return false;
472 }
473
474 // Prep: remove Device attachment on both Ports
Yuta HIGUCHI240bf072014-02-17 10:55:21 -0800475 ArrayList<DeviceEvent> deviceEvents = new ArrayList<>();
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800476 for (Device device : srcPort.getDevices()) {
477 DeviceEvent devEvt = new DeviceEvent(device.getMacAddress());
478 devEvt.addAttachmentPoint(new SwitchPort(srcPort.getSwitch().getDpid(), srcPort.getNumber()));
Yuta HIGUCHI240bf072014-02-17 10:55:21 -0800479 deviceEvents.add(devEvt);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800480 }
481 for (Device device : dstPort.getDevices()) {
482 DeviceEvent devEvt = new DeviceEvent(device.getMacAddress());
483 devEvt.addAttachmentPoint(new SwitchPort(dstPort.getSwitch().getDpid(), dstPort.getNumber()));
Yuta HIGUCHI240bf072014-02-17 10:55:21 -0800484 deviceEvents.add(devEvt);
485 }
486 for (DeviceEvent devEvt : deviceEvents) {
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800487 // calling Discovery API to wipe from DB, etc.
488 removeDeviceEvent(devEvt);
489 }
490
491 return true;
Jonathan Hart22eb9882014-02-11 15:52:59 -0800492 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800493
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800494 private boolean prepareForRemoveLinkEvent(LinkEvent linkEvt) {
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800495 // Src/Dst Switch must exist
496 Switch srcSw = getSwitch(linkEvt.getSrc().dpid);
497 Switch dstSw = getSwitch(linkEvt.getDst().dpid);
498 if ( srcSw == null || dstSw == null ) {
Jonathan Hart0a4846e2014-02-18 11:03:40 -0800499 log.warn("Dropping remove link event because switch doesn't exist: {}", linkEvt);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800500 return false;
501 }
502 // Src/Dst Port must exist
503 Port srcPort = srcSw.getPort(linkEvt.getSrc().number);
Yuta HIGUCHI125c7df2014-02-14 12:28:10 -0800504 Port dstPort = dstSw.getPort(linkEvt.getDst().number);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800505 if ( srcPort == null || dstPort == null ) {
Jonathan Hart0a4846e2014-02-18 11:03:40 -0800506 log.warn("Dropping remove link event because port doesn't exist {}", linkEvt);
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800507 return false;
508 }
509
Yuta HIGUCHI125c7df2014-02-14 12:28:10 -0800510 Link link = srcPort.getOutgoingLink();
511
512 // Link is already gone, or different Link exist in memory
513 // XXX Check if we should reject or just accept these cases.
514 // it should be harmless to remove the Link on event from DB anyways
515 if (link == null ||
516 !link.getDestinationPortNumber().equals(linkEvt.getDst().number)
517 || !link.getDestinationSwitchDpid().equals(linkEvt.getDst().dpid)) {
Jonathan Hart0a4846e2014-02-18 11:03:40 -0800518 log.warn("Dropping remove link event because link doesn't exist: {}", linkEvt);
Yuta HIGUCHI125c7df2014-02-14 12:28:10 -0800519 return false;
520 }
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800521 // Prep: None
522 return true;
Jonathan Hart22eb9882014-02-11 15:52:59 -0800523 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800524
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800525 /**
526 *
527 * @param deviceEvt Event will be modified to remove inapplicable attachemntPoints/ipAddress
528 * @return false if this event should be dropped.
529 */
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800530 private boolean prepareForAddDeviceEvent(DeviceEvent deviceEvt) {
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800531 boolean preconditionBroken = false;
532 ArrayList<PortEvent.SwitchPort> failedSwitchPort = new ArrayList<>();
533 for ( PortEvent.SwitchPort swp : deviceEvt.getAttachmentPoints() ) {
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800534 // Attached Ports' Parent Switch must exist
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800535 Switch sw = getSwitch(swp.dpid);
536 if ( sw == null ) {
537 preconditionBroken = true;
538 failedSwitchPort.add(swp);
539 continue;
540 }
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800541 // Attached Ports must exist
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800542 Port port = sw.getPort(swp.number);
543 if ( port == null ) {
544 preconditionBroken = true;
545 failedSwitchPort.add(swp);
546 continue;
547 }
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800548 // Attached Ports must not have Link
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800549 if ( port.getOutgoingLink() != null || port.getIncomingLink() != null ) {
550 preconditionBroken = true;
551 failedSwitchPort.add(swp);
552 continue;
553 }
554 }
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800555
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800556 // Rewriting event to exclude failed attachmentPoint
557 // XXX Assumption behind this is that inapplicable device event should
558 // be dropped, not deferred. If we decide to defer Device event,
559 // rewriting can become a problem
560 List<SwitchPort> attachmentPoints = deviceEvt.getAttachmentPoints();
561 attachmentPoints.removeAll(failedSwitchPort);
562 deviceEvt.setAttachmentPoints(attachmentPoints);
563
564 if ( deviceEvt.getAttachmentPoints().isEmpty() && deviceEvt.getIpAddresses().isEmpty() ) {
Yuta HIGUCHI125c7df2014-02-14 12:28:10 -0800565 // return false to represent: Nothing left to do for this event. Caller should drop event
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -0800566 return false;
567 }
568
569 // Should we return false to tell caller that the event was trimmed?
570 // if ( preconditionBroken ) {
571 // return false;
572 // }
573
574 return true;
Jonathan Hart22eb9882014-02-11 15:52:59 -0800575 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800576
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800577 private boolean prepareForRemoveDeviceEvent(DeviceEvent deviceEvt) {
Yuta HIGUCHId02e9282014-02-12 09:24:41 -0800578 // No show stopping precondition?
579 // Prep: none
Jonathan Hart22eb9882014-02-11 15:52:59 -0800580 return true;
581 }
Yuta HIGUCHI928fa682014-02-11 19:07:57 -0800582
Yuta HIGUCHI76df2472014-02-12 22:36:51 -0800583 /* ******************************
584 * NetworkGraphReplicationInterface methods
585 * ******************************/
586
Yuta HIGUCHI928fa682014-02-11 19:07:57 -0800587 @Override
588 public void putSwitchReplicationEvent(SwitchEvent switchEvent) {
Yuta HIGUCHI9fc10ac2014-02-12 17:18:57 -0800589 if (prepareForAddSwitchEvent(switchEvent)) {
590 putSwitch(switchEvent);
591 }
592 // TODO handle invariant violation
Yuta HIGUCHIa536e762014-02-17 21:47:28 -0800593 // trigger instance local topology event handler
594 dispatchPutSwitchEvent(switchEvent);
Yuta HIGUCHI928fa682014-02-11 19:07:57 -0800595 }
596
597 @Override
598 public void removeSwitchReplicationEvent(SwitchEvent switchEvent) {
Yuta HIGUCHI9fc10ac2014-02-12 17:18:57 -0800599 if (prepareForRemoveSwitchEvent(switchEvent)) {
600 removeSwitch(switchEvent);
601 }
Yuta HIGUCHI125c7df2014-02-14 12:28:10 -0800602 // TODO handle invariant violation
Yuta HIGUCHIa536e762014-02-17 21:47:28 -0800603 // trigger instance local topology event handler
604 dispatchRemoveSwitchEvent(switchEvent);
Yuta HIGUCHI928fa682014-02-11 19:07:57 -0800605 }
606
607 @Override
608 public void putPortReplicationEvent(PortEvent portEvent) {
Yuta HIGUCHI9fc10ac2014-02-12 17:18:57 -0800609 if (prepareForAddPortEvent(portEvent)) {
610 putPort(portEvent);
611 }
Yuta HIGUCHI125c7df2014-02-14 12:28:10 -0800612 // TODO handle invariant violation
Yuta HIGUCHIa536e762014-02-17 21:47:28 -0800613 // trigger instance local topology event handler
614 dispatchPutPortEvent(portEvent);
Yuta HIGUCHI928fa682014-02-11 19:07:57 -0800615 }
616
617 @Override
618 public void removePortReplicationEvent(PortEvent portEvent) {
Yuta HIGUCHI9fc10ac2014-02-12 17:18:57 -0800619 if (prepareForRemovePortEvent(portEvent)) {
620 removePort(portEvent);
621 }
Yuta HIGUCHI125c7df2014-02-14 12:28:10 -0800622 // TODO handle invariant violation
Yuta HIGUCHIa536e762014-02-17 21:47:28 -0800623 // trigger instance local topology event handler
624 dispatchRemovePortEvent(portEvent);
Yuta HIGUCHI928fa682014-02-11 19:07:57 -0800625 }
626
627 @Override
628 public void putLinkReplicationEvent(LinkEvent linkEvent) {
Yuta HIGUCHI9fc10ac2014-02-12 17:18:57 -0800629 if (prepareForAddLinkEvent(linkEvent)) {
630 putLink(linkEvent);
631 }
Yuta HIGUCHI125c7df2014-02-14 12:28:10 -0800632 // TODO handle invariant violation
Yuta HIGUCHIa536e762014-02-17 21:47:28 -0800633 // trigger instance local topology event handler
634 dispatchPutLinkEvent(linkEvent);
Yuta HIGUCHI928fa682014-02-11 19:07:57 -0800635 }
636
637 @Override
638 public void removeLinkReplicationEvent(LinkEvent linkEvent) {
Yuta HIGUCHI9fc10ac2014-02-12 17:18:57 -0800639 if (prepareForRemoveLinkEvent(linkEvent)) {
640 removeLink(linkEvent);
641 }
Yuta HIGUCHI125c7df2014-02-14 12:28:10 -0800642 // TODO handle invariant violation
Yuta HIGUCHIa536e762014-02-17 21:47:28 -0800643 // trigger instance local topology event handler
644 dispatchRemoveLinkEvent(linkEvent);
Yuta HIGUCHI928fa682014-02-11 19:07:57 -0800645 }
646
647 @Override
648 public void putDeviceReplicationEvent(DeviceEvent deviceEvent) {
Yuta HIGUCHI9fc10ac2014-02-12 17:18:57 -0800649 if (prepareForAddDeviceEvent(deviceEvent)) {
650 putDevice(deviceEvent);
651 }
Yuta HIGUCHI125c7df2014-02-14 12:28:10 -0800652 // TODO handle invariant violation
Yuta HIGUCHIa536e762014-02-17 21:47:28 -0800653 // trigger instance local topology event handler
654 dispatchPutDeviceEvent(deviceEvent);
Yuta HIGUCHI928fa682014-02-11 19:07:57 -0800655 }
656
657 @Override
658 public void removeDeviceReplicationEvent(DeviceEvent deviceEvent) {
Yuta HIGUCHI9fc10ac2014-02-12 17:18:57 -0800659 if (prepareForRemoveDeviceEvent(deviceEvent)) {
660 removeDevice(deviceEvent);
661 }
Yuta HIGUCHI125c7df2014-02-14 12:28:10 -0800662 // TODO handle invariant violation
Yuta HIGUCHIa536e762014-02-17 21:47:28 -0800663 // trigger instance local topology event handler
664 dispatchRemoveDeviceEvent(deviceEvent);
Yuta HIGUCHI928fa682014-02-11 19:07:57 -0800665 }
Yuta HIGUCHI76df2472014-02-12 22:36:51 -0800666
667 /* ************************************************
668 * Internal In-memory object mutation methods.
669 * ************************************************/
670
671 void putSwitch(SwitchEvent swEvt) {
672 if (swEvt == null) {
673 throw new IllegalArgumentException("Switch cannot be null");
674 }
675
676 Switch sw = switches.get(swEvt.getDpid());
677
678 if (sw == null) {
679 sw = new SwitchImpl(this, swEvt.getDpid());
680 Switch existing = switches.putIfAbsent(swEvt.getDpid(), sw);
681 if (existing != null) {
682 log.warn(
683 "Concurrent putSwitch not expected. Continuing updating {}",
684 existing);
685 sw = existing;
686 }
687 }
688
689 // Update when more attributes are added to Event object
690 // no attribute to update for now
691
692 // TODO handle child Port event properly for performance
693 for (PortEvent portEvt : swEvt.getPorts() ) {
694 putPort(portEvt);
695 }
696
697 }
698
699 void removeSwitch(SwitchEvent swEvt) {
700 if (swEvt == null) {
701 throw new IllegalArgumentException("Switch cannot be null");
702 }
703
704 // TODO handle child Port event properly for performance
705 for (PortEvent portEvt : swEvt.getPorts() ) {
706 removePort(portEvt);
707 }
708
709 Switch sw = switches.get(swEvt.getDpid());
710
711 if (sw == null) {
712 log.warn("Switch {} already removed, ignoring", swEvt);
713 return;
714 }
715
Yuta HIGUCHI317bf542014-02-17 11:02:39 -0800716 // remove all ports if there still exist
717 ArrayList<PortEvent> portsToRemove = new ArrayList<>();
718 for (Port port : sw.getPorts()) {
Yuta HIGUCHI76df2472014-02-12 22:36:51 -0800719 log.warn(
Yuta HIGUCHI317bf542014-02-17 11:02:39 -0800720 "Port {} on Switch {} should be removed prior to removing Switch. Removing Port now",
721 port, swEvt);
722 PortEvent portEvt = new PortEvent(port.getDpid(), port.getNumber());
723 portsToRemove.add(portEvt);
Yuta HIGUCHI76df2472014-02-12 22:36:51 -0800724 }
Yuta HIGUCHI0a4bd192014-02-17 13:52:34 -0800725 for (PortEvent portEvt : portsToRemove) {
Yuta HIGUCHI317bf542014-02-17 11:02:39 -0800726 // XXX calling removePortEvent() may trigger duplicate event, once at prepare phase, second time here
727 // If event can be squashed, ignored etc. at receiver side it shouldn't be a problem, but if not
728 // need to re-visit this issue.
729
730 // Note: removePortEvent() implies removal of attached Device, etc.
731 // if we decide not to call removePortEvent(), Device needs to be handled properly
732 removePortEvent(portEvt);
Yuta HIGUCHI76df2472014-02-12 22:36:51 -0800733 }
734
735 boolean removed = switches.remove(swEvt.getDpid(), sw);
736 if (removed) {
737 log.warn(
738 "Switch instance was replaced concurrently while removing {}. Something is not right.",
739 sw);
740 }
741 }
742
743 void putPort(PortEvent portEvt) {
744 if (portEvt == null) {
745 throw new IllegalArgumentException("Port cannot be null");
746 }
747 Switch sw = switches.get(portEvt.getDpid());
748 if (sw == null) {
749 throw new BrokenInvariantException(String.format(
750 "Switch with dpid %s did not exist.",
751 new Dpid(portEvt.getDpid())));
752 }
753 Port p = sw.getPort(portEvt.getNumber());
754 PortImpl port = null;
755 if (p != null) {
756 port = getPortImpl(p);
757 }
758
759 if (port == null) {
760 port = new PortImpl(this, sw, portEvt.getNumber());
761 }
762
763 // TODO update attributes
764
765 SwitchImpl s = getSwitchImpl(sw);
766 s.addPort(port);
767 }
768
769 void removePort(PortEvent portEvt) {
770 if (portEvt == null) {
771 throw new IllegalArgumentException("Port cannot be null");
772 }
773
774 Switch sw = switches.get(portEvt.getDpid());
775 if (sw == null) {
776 log.warn("Parent Switch for Port {} already removed, ignoring", portEvt);
777 return;
778 }
779
780 Port p = sw.getPort(portEvt.getNumber());
781 if (p == null) {
782 log.warn("Port {} already removed, ignoring", portEvt);
783 return;
784 }
785
Yuta HIGUCHIde040642014-02-17 11:03:39 -0800786 // Remove Link and Device Attachment
787 for (Device device : p.getDevices()) {
788 log.debug("Removing Device {} on Port {}", device, portEvt);
789 DeviceEvent devEvt = new DeviceEvent(device.getMacAddress());
790 devEvt.addAttachmentPoint(new SwitchPort(p.getSwitch().getDpid(), p.getNumber()));
Yuta HIGUCHI76df2472014-02-12 22:36:51 -0800791
Yuta HIGUCHIde040642014-02-17 11:03:39 -0800792 // XXX calling removeDeviceEvent() may trigger duplicate event, once at prepare phase, second time here
793 // If event can be squashed, ignored etc. at receiver side it shouldn't be a problem, but if not
794 // need to re-visit
795
796 // calling Discovery API to wipe from DB, etc.
797 removeDeviceEvent(devEvt);
Yuta HIGUCHI76df2472014-02-12 22:36:51 -0800798 }
Yuta HIGUCHIde040642014-02-17 11:03:39 -0800799 Set<Link> links = new HashSet<>();
800 links.add(p.getOutgoingLink());
801 links.add(p.getIncomingLink());
802 ArrayList<LinkEvent> linksToRemove = new ArrayList<>();
803 for (Link link : links) {
804 if (link == null) {
805 continue;
806 }
807 log.debug("Removing Link {} on Port {}", link, portEvt);
808 LinkEvent linkEvent = new LinkEvent(link.getSourceSwitchDpid(), link.getSourcePortNumber(), link.getDestinationSwitchDpid(), link.getDestinationPortNumber());
809 linksToRemove.add(linkEvent);
Yuta HIGUCHI76df2472014-02-12 22:36:51 -0800810 }
Yuta HIGUCHIde040642014-02-17 11:03:39 -0800811 for (LinkEvent linkEvent : linksToRemove) {
812 // XXX calling removeLinkEvent() may trigger duplicate event, once at prepare phase, second time here
813 // If event can be squashed, ignored etc. at receiver side it shouldn't be a problem, but if not
814 // need to re-visit
815
816 // calling Discovery API to wipe from DB, etc.
817 removeLinkEvent(linkEvent);
Yuta HIGUCHI76df2472014-02-12 22:36:51 -0800818 }
819
820 // remove Port from Switch
821 SwitchImpl s = getSwitchImpl(sw);
822 s.removePort(p);
823 }
824
825 void putLink(LinkEvent linkEvt) {
826 if (linkEvt == null) {
827 throw new IllegalArgumentException("Link cannot be null");
828 }
829
830 Switch srcSw = switches.get(linkEvt.getSrc().dpid);
831 if (srcSw == null) {
832 throw new BrokenInvariantException(
833 String.format(
834 "Switch with dpid %s did not exist.",
835 new Dpid(linkEvt.getSrc().dpid)));
836 }
837
838 Switch dstSw = switches.get(linkEvt.getDst().dpid);
839 if (dstSw == null) {
840 throw new BrokenInvariantException(
841 String.format(
842 "Switch with dpid %s did not exist.",
843 new Dpid(linkEvt.getDst().dpid)));
844 }
845
846 Port srcPort = srcSw.getPort(linkEvt.getSrc().number);
847 if (srcPort == null) {
848 throw new BrokenInvariantException(
849 String.format(
850 "Src Port %s of a Link did not exist.",
851 linkEvt.getSrc() ));
852 }
853
854 Port dstPort = dstSw.getPort(linkEvt.getDst().number);
855 if (dstPort == null) {
856 throw new BrokenInvariantException(
857 String.format(
858 "Dst Port %s of a Link did not exist.",
859 linkEvt.getDst() ));
860 }
861
862 // getting Link instance from destination port incoming Link
863 Link l = dstPort.getIncomingLink();
864 LinkImpl link = null;
865 assert( l == srcPort.getOutgoingLink() );
866 if (l != null) {
867 link = getLinkImpl(l);
868 }
869
870 if (link == null) {
871 link = new LinkImpl(this, srcPort, dstPort);
872 }
873
874
875 PortImpl dstPortMem = getPortImpl(dstPort);
876 PortImpl srcPortMem = getPortImpl(srcPort);
877
878 // Add Link first to avoid further Device addition
879
880 // add Link to Port
881 dstPortMem.setIncomingLink(link);
882 srcPortMem.setOutgoingLink(link);
883
884 // remove Device Pointing to Port if any
885 for(Device d : dstPortMem.getDevices() ) {
886 log.error("Device {} on Port {} should have been removed prior to adding Link {}", d, dstPort, linkEvt);
887 DeviceImpl dev = getDeviceImpl(d);
888 dev.removeAttachmentPoint(dstPort);
Yuta HIGUCHI407261a2014-02-13 16:34:06 -0800889 // This implies that change is made to Device Object.
890 // sending Device attachment point removed event
891 DeviceEvent rmEvent = new DeviceEvent(d.getMacAddress());
892 rmEvent.addAttachmentPoint(new SwitchPort(dstPort.getDpid(), dstPort.getNumber()));
893 removeDeviceEvent(rmEvent);
Yuta HIGUCHI76df2472014-02-12 22:36:51 -0800894 }
895 dstPortMem.removeAllDevice();
896 for(Device d : srcPortMem.getDevices() ) {
897 log.error("Device {} on Port {} should have been removed prior to adding Link {}", d, srcPort, linkEvt);
898 DeviceImpl dev = getDeviceImpl(d);
899 dev.removeAttachmentPoint(srcPort);
Yuta HIGUCHI407261a2014-02-13 16:34:06 -0800900 // This implies that change is made to Device Object.
901 // sending Device attachment point removed event
902 DeviceEvent rmEvent = new DeviceEvent(d.getMacAddress());
903 rmEvent.addAttachmentPoint(new SwitchPort(dstPort.getDpid(), dstPort.getNumber()));
904 removeDeviceEvent(rmEvent);
Yuta HIGUCHI76df2472014-02-12 22:36:51 -0800905 }
906 srcPortMem.removeAllDevice();
907
908 }
909
910 void removeLink(LinkEvent linkEvt) {
911 if (linkEvt == null) {
912 throw new IllegalArgumentException("Link cannot be null");
913 }
914
915 Switch srcSw = switches.get(linkEvt.getSrc().dpid);
916 if (srcSw == null) {
917 log.warn("Src Switch for Link {} already removed, ignoring", linkEvt);
918 return;
919 }
920
921 Switch dstSw = switches.get(linkEvt.getDst().dpid);
922 if (dstSw == null) {
923 log.warn("Dst Switch for Link {} already removed, ignoring", linkEvt);
924 return;
925 }
926
927 Port srcPort = srcSw.getPort(linkEvt.getSrc().number);
928 if (srcPort == null) {
929 log.warn("Src Port for Link {} already removed, ignoring", linkEvt);
930 return;
931 }
932
933 Port dstPort = dstSw.getPort(linkEvt.getDst().number);
934 if (dstPort == null) {
935 log.warn("Dst Port for Link {} already removed, ignoring", linkEvt);
936 return;
937 }
938
939 Link l = dstPort.getIncomingLink();
940 if ( l == null ) {
941 log.warn("Link {} already removed on destination Port", linkEvt);
942 }
943 l = srcPort.getOutgoingLink();
944 if ( l == null ) {
945 log.warn("Link {} already removed on src Port", linkEvt);
946 }
947
948 getPortImpl(dstPort).setIncomingLink(null);
949 getPortImpl(srcPort).setOutgoingLink(null);
950 }
951
952 // XXX Need to rework Device related
953 void putDevice(DeviceEvent deviceEvt) {
954 if (deviceEvt == null) {
955 throw new IllegalArgumentException("Device cannot be null");
956 }
957
958 Device device = getDeviceByMac(deviceEvt.getMac());
959 if ( device == null ) {
960 device = new DeviceImpl(this, deviceEvt.getMac());
961 Device existing = mac2Device.putIfAbsent(deviceEvt.getMac(), device);
962 if (existing != null) {
963 log.warn(
964 "Concurrent putDevice seems to be in action. Continuing updating {}",
965 existing);
966 device = existing;
967 }
968 }
969 DeviceImpl memDevice = getDeviceImpl(device);
970
971 // for each attachment point
972 for (SwitchPort swp : deviceEvt.getAttachmentPoints() ) {
973 // Attached Ports' Parent Switch must exist
974 Switch sw = getSwitch(swp.dpid);
975 if ( sw == null ) {
Yuta HIGUCHI25719052014-02-13 14:42:06 -0800976 log.warn("Switch for the attachment point {} did not exist. skipping mutation", swp);
Yuta HIGUCHI76df2472014-02-12 22:36:51 -0800977 continue;
978 }
979 // Attached Ports must exist
980 Port port = sw.getPort(swp.number);
981 if ( port == null ) {
Yuta HIGUCHI25719052014-02-13 14:42:06 -0800982 log.warn("Port for the attachment point {} did not exist. skipping mutation", swp);
Yuta HIGUCHI76df2472014-02-12 22:36:51 -0800983 continue;
984 }
985 // Attached Ports must not have Link
986 if ( port.getOutgoingLink() != null || port.getIncomingLink() != null ) {
987 log.warn("Link (Out:{},In:{}) exist on the attachment point, skipping mutation.", port.getOutgoingLink(), port.getIncomingLink());
988 continue;
989 }
990
991 // finally add Device <-> Port on In-memory structure
992 PortImpl memPort = getPortImpl(port);
993 memPort.addDevice(device);
994 memDevice.addAttachmentPoint(port);
995 }
996
997 // for each IP address
998 for( InetAddress ipAddr : deviceEvt.getIpAddresses() ) {
999 // Add Device -> IP
1000 memDevice.addIpAddress(ipAddr);
1001
1002 // Add IP -> Set<Device>
1003 boolean updated = false;
1004 do {
1005 Set<Device> devices = this.addr2Device.get(ipAddr);
1006 if ( devices == null ) {
1007 devices = new HashSet<>();
1008 Set<Device> existing = this.addr2Device.putIfAbsent(ipAddr, devices);
1009 if ( existing == null ) {
1010 // success
1011 updated = true;
1012 }
1013 } else {
1014 Set<Device> updateDevices = new HashSet<>(devices);
1015 updateDevices.add(device);
1016 updated = this.addr2Device.replace(ipAddr, devices, updateDevices);
1017 }
1018 if (!updated) {
1019 log.debug("Collision detected, updating IP to Device mapping retrying.");
1020 }
1021 } while( !updated );
1022 }
1023 }
1024
1025 void removeDevice(DeviceEvent deviceEvt) {
1026 if (deviceEvt == null) {
1027 throw new IllegalArgumentException("Device cannot be null");
1028 }
1029
1030 Device device = getDeviceByMac(deviceEvt.getMac());
1031 if ( device == null ) {
1032 log.warn("Device {} already removed, ignoring", deviceEvt);
1033 return;
1034 }
1035 DeviceImpl memDevice = getDeviceImpl(device);
1036
1037 // for each attachment point
1038 for (SwitchPort swp : deviceEvt.getAttachmentPoints() ) {
1039 // Attached Ports' Parent Switch must exist
1040 Switch sw = getSwitch(swp.dpid);
1041 if ( sw == null ) {
Yuta HIGUCHI25719052014-02-13 14:42:06 -08001042 log.warn("Switch for the attachment point {} did not exist. skipping attachment point mutation", swp);
Yuta HIGUCHI76df2472014-02-12 22:36:51 -08001043 continue;
1044 }
1045 // Attached Ports must exist
1046 Port port = sw.getPort(swp.number);
1047 if ( port == null ) {
Yuta HIGUCHI25719052014-02-13 14:42:06 -08001048 log.warn("Port for the attachment point {} did not exist. skipping attachment point mutation", swp);
Yuta HIGUCHI76df2472014-02-12 22:36:51 -08001049 continue;
1050 }
1051
1052 // finally remove Device <-> Port on In-memory structure
1053 PortImpl memPort = getPortImpl(port);
1054 memPort.removeDevice(device);
1055 memDevice.removeAttachmentPoint(port);
1056 }
1057
1058 // for each IP address
1059 for( InetAddress ipAddr : deviceEvt.getIpAddresses() ) {
1060 // Remove Device -> IP
1061 memDevice.removeIpAddress(ipAddr);
1062
1063 // Remove IP -> Set<Device>
1064 boolean updated = false;
1065 do {
1066 Set<Device> devices = this.addr2Device.get(ipAddr);
1067 if ( devices == null ) {
1068 // already empty set, nothing to do
1069 updated = true;
1070 } else {
1071 Set<Device> updateDevices = new HashSet<>(devices);
1072 updateDevices.remove(device);
1073 updated = this.addr2Device.replace(ipAddr, devices, updateDevices);
1074 }
1075 if (!updated) {
1076 log.debug("Collision detected, updating IP to Device mapping retrying.");
1077 }
1078 } while( !updated );
1079 }
1080 }
1081
Yuta HIGUCHIa536e762014-02-17 21:47:28 -08001082 private void dispatchPutSwitchEvent(SwitchEvent switchEvent) {
1083 for (INetworkGraphListener listener : this.networkGraphListeners) {
1084 // TODO Should copy before handing them over to listener
1085 listener.putSwitchEvent(switchEvent);
1086 }
1087 }
1088
1089 private void dispatchRemoveSwitchEvent(SwitchEvent switchEvent) {
1090 for (INetworkGraphListener listener : this.networkGraphListeners) {
1091 // TODO Should copy before handing them over to listener
1092 listener.removeSwitchEvent(switchEvent);
1093 }
1094 }
1095
1096 private void dispatchPutPortEvent(PortEvent portEvent) {
1097 for (INetworkGraphListener listener : this.networkGraphListeners) {
1098 // TODO Should copy before handing them over to listener
1099 listener.putPortEvent(portEvent);
1100 }
1101 }
1102
1103 private void dispatchRemovePortEvent(PortEvent portEvent) {
1104 for (INetworkGraphListener listener : this.networkGraphListeners) {
1105 // TODO Should copy before handing them over to listener
1106 listener.removePortEvent(portEvent);
1107 }
1108 }
1109
1110 private void dispatchPutLinkEvent(LinkEvent linkEvent) {
1111 for (INetworkGraphListener listener : this.networkGraphListeners) {
1112 // TODO Should copy before handing them over to listener
1113 listener.putLinkEvent(linkEvent);
1114 }
1115 }
1116
1117 private void dispatchRemoveLinkEvent(LinkEvent linkEvent) {
1118 for (INetworkGraphListener listener : this.networkGraphListeners) {
1119 // TODO Should copy before handing them over to listener
1120 listener.removeLinkEvent(linkEvent);
1121 }
1122 }
1123
1124 private void dispatchPutDeviceEvent(DeviceEvent deviceEvent) {
1125 for (INetworkGraphListener listener : this.networkGraphListeners) {
1126 // TODO Should copy before handing them over to listener
1127 listener.putDeviceEvent(deviceEvent);;
1128 }
1129 }
1130
1131 private void dispatchRemoveDeviceEvent(DeviceEvent deviceEvent) {
1132 for (INetworkGraphListener listener : this.networkGraphListeners) {
1133 // TODO Should copy before handing them over to listener
1134 listener.removeDeviceEvent(deviceEvent);
1135 }
1136 }
1137
Yuta HIGUCHI76df2472014-02-12 22:36:51 -08001138 private SwitchImpl getSwitchImpl(Switch sw) {
1139 if (sw instanceof SwitchImpl) {
1140 return (SwitchImpl) sw;
1141 }
1142 throw new ClassCastException("SwitchImpl expected, but found: " + sw);
1143 }
1144
1145 private PortImpl getPortImpl(Port p) {
1146 if (p instanceof PortImpl) {
1147 return (PortImpl) p;
1148 }
1149 throw new ClassCastException("PortImpl expected, but found: " + p);
1150 }
1151
1152 private LinkImpl getLinkImpl(Link l) {
1153 if (l instanceof LinkImpl) {
1154 return (LinkImpl) l;
1155 }
1156 throw new ClassCastException("LinkImpl expected, but found: " + l);
1157 }
1158
1159 private DeviceImpl getDeviceImpl(Device d) {
1160 if (d instanceof DeviceImpl) {
1161 return (DeviceImpl) d;
1162 }
1163 throw new ClassCastException("DeviceImpl expected, but found: " + d);
1164 }
1165
1166 @Deprecated
1167 public void loadWholeTopologyFromDB() {
Yuta HIGUCHI0a4bd192014-02-17 13:52:34 -08001168 // XXX May need to clear whole topology first, depending on
1169 // how we initially subscribe to replication events
Yuta HIGUCHI76df2472014-02-12 22:36:51 -08001170
1171 for (RCSwitch sw : RCSwitch.getAllSwitches()) {
1172 if ( sw.getStatus() != RCSwitch.STATUS.ACTIVE ) {
1173 continue;
1174 }
1175 putSwitchReplicationEvent(new SwitchEvent(sw.getDpid()));
1176 }
1177
1178 for (RCPort p : RCPort.getAllPorts()) {
1179 if (p.getStatus() != RCPort.STATUS.ACTIVE) {
1180 continue;
1181 }
1182 putPortReplicationEvent(new PortEvent(p.getDpid(), p.getNumber() ));
1183 }
1184
1185 // TODO Is Device going to be in DB? If so, read from DB.
1186 // for (RCDevice d : RCDevice.getAllDevices()) {
1187 // DeviceEvent devEvent = new DeviceEvent( MACAddress.valueOf(d.getMac()) );
1188 // for (byte[] portId : d.getAllPortIds() ) {
1189 // devEvent.addAttachmentPoint( new SwitchPort( RCPort.getDpidFromKey(portId), RCPort.getNumberFromKey(portId) ));
1190 // }
1191 // }
1192
1193 for (RCLink l : RCLink.getAllLinks()) {
Yuta HIGUCHIdab94692014-02-18 09:12:02 -08001194 // check if src/dst switch/port exist before triggering event
1195 // Src/Dst Switch must exist
1196 Switch srcSw = getSwitch(l.getSrc().dpid);
1197 Switch dstSw = getSwitch(l.getDst().dpid);
1198 if ( srcSw == null || dstSw == null ) {
1199 continue;
1200 }
1201 // Src/Dst Port must exist
1202 Port srcPort = srcSw.getPort(l.getSrc().number);
1203 Port dstPort = dstSw.getPort(l.getDst().number);
1204 if ( srcPort == null || dstPort == null ) {
1205 continue;
1206 }
Yuta HIGUCHI76df2472014-02-12 22:36:51 -08001207 putLinkReplicationEvent( new LinkEvent(l.getSrc().dpid, l.getSrc().number, l.getDst().dpid, l.getDst().number));
1208 }
1209 }
Jonathan Hart062a2e82014-02-03 09:41:57 -08001210}