Couple of fixes for removing links

Change-Id: I604bd3b28d41d2e79285bebfefe9f718a3d5011d
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphDatastore.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphDatastore.java
index 9fd7f86..d34ac46 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphDatastore.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphDatastore.java
@@ -230,6 +230,7 @@
 				//rcSrcPort.update();
 				//rcDstPort.update();
 				rcLink.delete();
+				return;
 			} catch (ObjectDoesntExistException e) {
 				log.error("Remove link failed {}", linkEvent, e);
 				return;
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java
index bab8f4b..8df13e0 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java
@@ -176,12 +176,23 @@
 		}
 		// Existing ports not on event should be removed.
 		// TODO Should batch eventually for performance?
+		List<Port> portsToRemove = new ArrayList<Port>();
 		for( Port p : sw.getPorts() ) {
 		    if ( !port_noOnEvent.contains(p.getNumber()) ) {
+			//PortEvent rmEvent = new PortEvent(p.getSwitch().getDpid(), p.getNumber());
+			// calling Discovery removePort() API to wipe from DB, etc.
+			//removePortEvent(rmEvent);
+		    
+		    // We can't remove ports here because this will trigger a remove
+		    // from the switch's port list, which we are currently iterating
+		    // over.
+		    portsToRemove.add(p);
+		    }
+		}
+		for (Port p : portsToRemove) {
 			PortEvent rmEvent = new PortEvent(p.getSwitch().getDpid(), p.getNumber());
 			// calling Discovery removePort() API to wipe from DB, etc.
 			removePortEvent(rmEvent);
-		    }
 		}
 	    }
 	}