Merge branch 'master' of https://github.com/OPENNETWORKINGLAB/ONOS
diff --git a/src/main/java/net/onrc/onos/ofcontroller/core/ILinkStorage.java b/src/main/java/net/onrc/onos/ofcontroller/core/ILinkStorage.java
index c8312d4..8889092 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/core/ILinkStorage.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/core/ILinkStorage.java
@@ -44,6 +44,14 @@
 	 */
 	public List<Link> getLinks(Long dpid, short port);
 
+	/**
+	 * Get list of all reverse links connected to the port specified by given DPID and port number.
+	 * @param dpid DPID of desired port.
+	 * @param port Port number of desired port.
+	 * @return List of reverse links. Empty list if no port was found.
+	 */
+	public List<Link> getReverseLinks(Long dpid, short port);
+
 	public List<Link> getLinks(String dpid);
 
 	/**
diff --git a/src/main/java/net/onrc/onos/ofcontroller/core/ISwitchStorage.java b/src/main/java/net/onrc/onos/ofcontroller/core/ISwitchStorage.java
index b7825f9..1c243c0 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/core/ISwitchStorage.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/core/ISwitchStorage.java
@@ -1,5 +1,7 @@
 package net.onrc.onos.ofcontroller.core;
 
+import java.util.List;
+
 import net.floodlightcontroller.core.IOFSwitch;
 
 import org.openflow.protocol.OFPhysicalPort;
@@ -43,4 +45,12 @@
 	 * Delete a port on a switch by num
 	 */
 	public boolean deletePort(String dpid, short port);
+
+	/**
+	 * Get list of all ports on the switch specified by given DPID.
+	 *
+	 * @param dpid DPID of desired switch.
+	 * @return List of port IDs. Empty list if no port was found.
+	 */
+	public List<Short> getPorts(String dpid);
 }
diff --git a/src/main/java/net/onrc/onos/ofcontroller/core/internal/LinkStorageImpl.java b/src/main/java/net/onrc/onos/ofcontroller/core/internal/LinkStorageImpl.java
index 09e87ca..7a3d43e 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/core/internal/LinkStorageImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/core/internal/LinkStorageImpl.java
@@ -131,6 +131,7 @@
 				op.rollback();
 			}
 		} catch (Exception e) {
+			op.rollback();
 			e.printStackTrace();
 			log.error("LinkStorageImpl:addLink link:{} linfo:{} failed", link, linfo);
 		}
@@ -157,6 +158,7 @@
 			op.commit();
 			success = true;
 		} catch (Exception e) {
+			op.rollback();
 			e.printStackTrace();
 			log.error("LinkStorageImpl:addLinks link:s{} failed", links);
 		}
@@ -227,24 +229,54 @@
 	 */
 	@Override
 	public List<Link> getLinks(Long dpid, short port) {
-    	List<Link> links = new ArrayList<Link>();
+	    List<Link> links = new ArrayList<Link>();
+
+	    IPortObject srcPort = op.searchPort(HexString.toHexString(dpid), port);
+	    if (srcPort == null)
+		return links;
+	    ISwitchObject srcSw = srcPort.getSwitch();
+	    if (srcSw == null)
+		return links;
     	
-    	IPortObject srcPort = op.searchPort(HexString.toHexString(dpid), port);
-    	ISwitchObject srcSw = srcPort.getSwitch();
+	    for(IPortObject dstPort : srcPort.getLinkedPorts()) {
+		ISwitchObject dstSw = dstPort.getSwitch();
+		if (dstSw != null) {
+		    Link link = new Link(dpid, port,
+					 HexString.toLong(dstSw.getDPID()),
+					 dstPort.getNumber());
+		    links.add(link);
+		}
+	    }
+	    return links;
+	}
+
+	/**
+	 * Get list of all reverse links connected to the port specified by given DPID and port number.
+	 * @param dpid DPID of desired port.
+	 * @param port Port number of desired port.
+	 * @return List of reverse links. Empty list if no port was found.
+	 */
+	@Override
+	public List<Link> getReverseLinks(Long dpid, short port) {
+	    List<Link> links = new ArrayList<Link>();
     	
-    	if(srcSw != null && srcPort != null) {
-        	for(IPortObject dstPort : srcPort.getLinkedPorts()) {
-        		ISwitchObject dstSw = dstPort.getSwitch();
-        		Link link = new Link(HexString.toLong(srcSw.getDPID()),
-        				srcPort.getNumber(),
-        				HexString.toLong(dstSw.getDPID()),
-        				dstPort.getNumber());
-    		
-        		links.add(link);
-        	}
-    	}
+	    IPortObject srcPort = op.searchPort(HexString.toHexString(dpid), port);
+	    if (srcPort == null)
+		return links;
+	    ISwitchObject srcSw = srcPort.getSwitch();
+	    if (srcSw == null)
+		return links;
     	
-     	return links;
+	    for(IPortObject dstPort : srcPort.getReverseLinkedPorts()) {
+		ISwitchObject dstSw = dstPort.getSwitch();
+		if (dstSw != null) {
+		    Link link = new Link(HexString.toLong(dstSw.getDPID()),
+					 dstPort.getNumber(),
+					 dpid, port);
+		    links.add(link);
+		}
+	    }
+	    return links;
 	}
 	
 	/**
@@ -292,10 +324,10 @@
 				for(IPortObject dstPort : srcPort.getLinkedPorts()) {
 					ISwitchObject dstSw = dstPort.getSwitch();
 					if(dstSw != null) {
-		        		Link link = new Link(HexString.toLong(srcSw.getDPID()),
+					    Link link = new Link(HexString.toLong(dpid),
 		        				srcPort.getNumber(),
 		        				HexString.toLong(dstSw.getDPID()),
-		        				dstPort.getNumber());
+							dstPort.getNumber());
 		        		links.add(link);
 					}
 				}
@@ -326,7 +358,7 @@
 							HexString.toLong(dstSw.getDPID()),
 							dstPort.getNumber(),
 					
-							HexString.toLong(srcSw.getDPID()),
+							HexString.toLong(dpid),
 							srcPort.getNumber());
 		        		links.add(link);
 					}
diff --git a/src/main/java/net/onrc/onos/ofcontroller/core/internal/SwitchStorageImpl.java b/src/main/java/net/onrc/onos/ofcontroller/core/internal/SwitchStorageImpl.java
index c01a3cb..7f0c259 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/core/internal/SwitchStorageImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/core/internal/SwitchStorageImpl.java
@@ -1,5 +1,8 @@
 package net.onrc.onos.ofcontroller.core.internal;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import net.floodlightcontroller.core.IOFSwitch;
 import net.onrc.onos.graph.GraphDBConnection;
 import net.onrc.onos.graph.GraphDBOperation;
@@ -229,8 +232,8 @@
 	        	log.info("SwitchStorage:updatePort dpid:{} port:{}", dpid, portNum);
 	        	if (p != null) {
 	        		setPortStateImpl(p, state, desc);
+				op.commit();
 	        	}
-	        	op.commit();
         		success = true;
 	        } else {
 	    		log.error("SwitchStorage:updatePort dpid:{} port:{} : failed switch does not exist", dpid, portNum);
@@ -316,6 +319,26 @@
 		return success;
 	}
 
+	/**
+	 * Get list of all ports on the switch specified by given DPID.
+	 *
+	 * @param dpid DPID of desired switch.
+	 * @return List of port IDs. Empty list if no port was found.
+	 */
+	@Override
+	public List<Short> getPorts(String dpid) {
+	    List<Short> ports = new ArrayList<Short>();
+
+	    ISwitchObject srcSw = op.searchSwitch(dpid);
+	    if (srcSw != null) {
+		for (IPortObject srcPort : srcSw.getPorts()) {
+		    ports.add(srcPort.getNumber());
+		}
+	    }
+
+	    return ports;
+	}
+
 	private ISwitchObject addSwitchImpl(String dpid) {
 		if (dpid != null) {
 			ISwitchObject sw = op.newSwitch(dpid);
diff --git a/src/main/java/net/onrc/onos/ofcontroller/floodlightlistener/NetworkGraphPublisher.java b/src/main/java/net/onrc/onos/ofcontroller/floodlightlistener/NetworkGraphPublisher.java
index 26292d9..648f6f1 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/floodlightlistener/NetworkGraphPublisher.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/floodlightlistener/NetworkGraphPublisher.java
@@ -100,6 +100,14 @@
 			if (hasControl) {
 				log.debug("got control to set inactive sw {}", HexString.toHexString(dpid));
 				try {
+					// Get the affected ports
+					List<Short> ports = swStore.getPorts(HexString.toHexString(dpid));
+					// Get the affected links
+					List<Link> links = linkStore.getLinks(HexString.toHexString(dpid));
+					// Get the affected reverse links
+					List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(dpid));
+					links.addAll(reverseLinks);
+
 					if (swStore.updateSwitch(HexString.toHexString(dpid), SwitchState.INACTIVE, DM_OPERATION.UPDATE)) {
 					    registryService.releaseControl(dpid);
 					    
@@ -113,6 +121,21 @@
 						new TopologyElement(dpid);
 					    datagridService.notificationSendTopologyElementRemoved(topologyElement);
 
+					    // Publish: remove the affected ports
+					    for (Short port : ports) {
+						TopologyElement topologyElementPort =
+						    new TopologyElement(dpid, port);
+						datagridService.notificationSendTopologyElementRemoved(topologyElementPort);
+					    }
+					    // Publish: remove the affected links
+					    for (Link link : links) {
+						TopologyElement topologyElementLink =
+						    new TopologyElement(link.getSrc(),
+									link.getSrcPort(),
+									link.getDst(),
+									link.getDstPort());
+						datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
+					    }
 					}
 				} catch (Exception e) {
 	                log.error("Error in SwitchCleanup:controlChanged ", e);
@@ -214,7 +237,8 @@
 			    TopologyElement topologyElement =
 				new TopologyElement(sw.getId());
 			    datagridService.notificationSendTopologyElementAdded(topologyElement);
-			    // Add the ports
+
+			    // Publish: add the ports
 			    // TODO: Add only ports that are UP?
 			    for (OFPhysicalPort port : sw.getPorts()) {
 				TopologyElement topologyElementPort =
@@ -228,6 +252,8 @@
 			    // Add all reverse links as well
 			    List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
 			    links.addAll(reverseLinks);
+
+			    // Publish: add the links
 			    for (Link link : links) {
 				TopologyElement topologyElementLink =
 				    new TopologyElement(link.getSrc(),
@@ -243,11 +269,35 @@
 	@Override
 	public void removedSwitch(IOFSwitch sw) {
 		if (registryService.hasControl(sw.getId())) {
+			// Get the affected ports
+			List<Short> ports = swStore.getPorts(HexString.toHexString(sw.getId()));
+			// Get the affected links
+			List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId()));
+			// Get the affected reverse links
+			List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
+			links.addAll(reverseLinks);
+
 			if (swStore.deleteSwitch(sw.getStringId())) {
 			    // TODO publish DELETE_SWITCH event here
 			    TopologyElement topologyElement =
 				new TopologyElement(sw.getId());
 			    datagridService.notificationSendTopologyElementRemoved(topologyElement);
+
+			    // Publish: remove the affected ports
+			    for (Short port : ports) {
+				TopologyElement topologyElementPort =
+				    new TopologyElement(sw.getId(), port);
+				datagridService.notificationSendTopologyElementRemoved(topologyElementPort);
+			    }
+			    // Publish: remove the affected links
+			    for (Link link : links) {
+				TopologyElement topologyElementLink =
+				    new TopologyElement(link.getSrc(),
+							link.getSrcPort(),
+							link.getDst(),
+							link.getDstPort());
+				datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
+			    }
 			}
 		}
 	}
@@ -265,16 +315,48 @@
 		    TopologyElement topologyElement =
 			new TopologyElement(switchId, port.getPortNumber());
 		    datagridService.notificationSendTopologyElementAdded(topologyElement);
+
+		    // Add all links that might be connected already
+		    List<Link> links = linkStore.getLinks(switchId, port.getPortNumber());
+		    // Add all reverse links as well
+		    List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber());
+		    links.addAll(reverseLinks);
+
+		    // Publish: add the links
+		    for (Link link : links) {
+			TopologyElement topologyElementLink =
+			    new TopologyElement(link.getSrc(),
+						link.getSrcPort(),
+						link.getDst(),
+						link.getDstPort());
+			datagridService.notificationSendTopologyElementAdded(topologyElementLink);
+		    }
 		}
 	}
 
 	@Override
 	public void switchPortRemoved(Long switchId, OFPhysicalPort port) {
+		// Remove all links that might be connected already
+		List<Link> links = linkStore.getLinks(switchId, port.getPortNumber());
+		// Remove all reverse links as well
+		List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber());
+		links.addAll(reverseLinks);
+
 		if (swStore.deletePort(HexString.toHexString(switchId), port.getPortNumber())) {
 		    // TODO publish DELETE_PORT event here
 		    TopologyElement topologyElement =
 			new TopologyElement(switchId, port.getPortNumber());
 		    datagridService.notificationSendTopologyElementRemoved(topologyElement);
+
+		    // Publish: remove the links
+		    for (Link link : links) {
+			TopologyElement topologyElementLink =
+			    new TopologyElement(link.getSrc(),
+						link.getSrcPort(),
+						link.getDst(),
+						link.getDstPort());
+			datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
+		    }
 		}
 	}
 
diff --git a/src/main/java/net/onrc/onos/ofcontroller/flowmanager/FlowManager.java b/src/main/java/net/onrc/onos/ofcontroller/flowmanager/FlowManager.java
index c22b916..98f9cce 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/flowmanager/FlowManager.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/flowmanager/FlowManager.java
@@ -41,7 +41,8 @@
  */
 public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage {
 
-    protected GraphDBOperation dbHandler;
+    protected GraphDBOperation dbHandlerApi;
+    protected GraphDBOperation dbHandlerInner;
 
     protected volatile IFloodlightProviderService floodlightProvider;
     protected volatile ITopologyNetService topologyNetService;
@@ -82,7 +83,7 @@
 		    runImpl();
 		} catch (Exception e) {
 		    log.debug("Exception processing All Flow Entries from the Network MAP: ", e);
-		    dbHandler.rollback();
+		    dbHandlerInner.rollback();
 		    return;
 		}
 	    }
@@ -113,7 +114,7 @@
 		// switches.
 		//
 		Iterable<IFlowEntry> allFlowEntries =
-		    dbHandler.getAllSwitchNotUpdatedFlowEntries();
+		    dbHandlerInner.getAllSwitchNotUpdatedFlowEntries();
 		for (IFlowEntry flowEntryObj : allFlowEntries) {
 		    counterAllFlowEntries++;
 
@@ -126,7 +127,7 @@
 			continue;	// Ignore the entry: not my switch
 
 		    IFlowPath flowObj =
-			dbHandler.getFlowPathByFlowEntry(flowEntryObj);
+			dbHandlerInner.getFlowPathByFlowEntry(flowEntryObj);
 		    if (flowObj == null)
 			continue;		// Should NOT happen
 		    if (flowObj.getFlowId() == null)
@@ -155,7 +156,7 @@
 		//
 		for (IFlowEntry flowEntryObj : addFlowEntries) {
 		    IFlowPath flowObj =
-			dbHandler.getFlowPathByFlowEntry(flowEntryObj);
+			dbHandlerInner.getFlowPathByFlowEntry(flowEntryObj);
 		    if (flowObj == null)
 			continue;		// Should NOT happen
 		    if (flowObj.getFlowId() == null)
@@ -179,16 +180,16 @@
 		while (! deleteFlowEntries.isEmpty()) {
 		    IFlowEntry flowEntryObj = deleteFlowEntries.poll();
 		    IFlowPath flowObj =
-			dbHandler.getFlowPathByFlowEntry(flowEntryObj);
+			dbHandlerInner.getFlowPathByFlowEntry(flowEntryObj);
 		    if (flowObj == null) {
 			log.debug("Did not find FlowPath to be deleted");
 			continue;
 		    }
 		    flowObj.removeFlowEntry(flowEntryObj);
-		    dbHandler.removeFlowEntry(flowEntryObj);
+		    dbHandlerInner.removeFlowEntry(flowEntryObj);
 		}
 
-		dbHandler.commit();
+		dbHandlerInner.commit();
 
 		long estimatedTime = System.nanoTime() - startTime;
 		double rate = 0.0;
@@ -213,7 +214,7 @@
 		    runImpl();
 		} catch (Exception e) {
 		    log.debug("Exception processing All Flows from the Network MAP: ", e);
-		    dbHandler.rollback();
+		    dbHandlerInner.rollback();
 		    return;
 		}
 	    }
@@ -240,7 +241,7 @@
 		// Flow Paths this controller is responsible for.
 		//
 		Topology topology = topologyNetService.newDatabaseTopology();
-		Iterable<IFlowPath> allFlowPaths = dbHandler.getAllFlowPaths();
+		Iterable<IFlowPath> allFlowPaths = dbHandlerInner.getAllFlowPaths();
 		for (IFlowPath flowPathObj : allFlowPaths) {
 		    counterAllFlowPaths++;
 		    if (flowPathObj == null)
@@ -342,12 +343,12 @@
 		//
 		while (! deleteFlows.isEmpty()) {
 		    IFlowPath flowPathObj = deleteFlows.poll();
-		    dbHandler.removeFlowPath(flowPathObj);
+		    dbHandlerInner.removeFlowPath(flowPathObj);
 		}
 
 		topologyNetService.dropTopology(topology);
 
-		dbHandler.commit();
+		dbHandlerInner.commit();
 
 		long estimatedTime = System.nanoTime() - startTime;
 		double rate = 0.0;
@@ -370,7 +371,8 @@
      */
     @Override
     public void init(String conf) {
-    	dbHandler = new GraphDBOperation(conf);
+    	dbHandlerApi = new GraphDBOperation(conf);
+    	dbHandlerInner = new GraphDBOperation(conf);
     }
 
     /**
@@ -386,7 +388,8 @@
     @Override
     public void close() {
 	datagridService.deregisterFlowEventHandlerService(flowEventHandler);
-    	dbHandler.close();
+    	dbHandlerApi.close();
+    	dbHandlerInner.close();
     }
 
     /**
@@ -531,7 +534,7 @@
 		flowEntry.setFlowId(new FlowId(flowPath.flowId().value()));
 	}
 
-	if (FlowDatabaseOperation.addFlow(this, dbHandler, flowPath, flowId)) {
+	if (FlowDatabaseOperation.addFlow(this, dbHandlerApi, flowPath, flowId)) {
 	    datagridService.notificationSendFlowAdded(flowPath);
 	    return true;
 	}
@@ -546,8 +549,8 @@
      * @return the added Flow Entry object on success, otherwise null.
      */
     private IFlowEntry addFlowEntry(IFlowPath flowObj, FlowEntry flowEntry) {
-	return FlowDatabaseOperation.addFlowEntry(this, dbHandler, flowObj,
-						  flowEntry);
+	return FlowDatabaseOperation.addFlowEntry(this, dbHandlerInner,
+						  flowObj, flowEntry);
     }
 
     /**
@@ -558,8 +561,8 @@
      * @return true on success, otherwise false.
      */
     private boolean deleteFlowEntry(IFlowPath flowObj, FlowEntry flowEntry) {
-	return FlowDatabaseOperation.deleteFlowEntry(dbHandler, flowObj,
-						     flowEntry);
+	return FlowDatabaseOperation.deleteFlowEntry(dbHandlerInner,
+						     flowObj, flowEntry);
     }
 
     /**
@@ -569,7 +572,7 @@
      */
     @Override
     public boolean deleteAllFlows() {
-	if (FlowDatabaseOperation.deleteAllFlows(dbHandler)) {
+	if (FlowDatabaseOperation.deleteAllFlows(dbHandlerApi)) {
 	    datagridService.notificationSendAllFlowsRemoved();
 	    return true;
 	}
@@ -584,7 +587,7 @@
      */
     @Override
     public boolean deleteFlow(FlowId flowId) {
-	if (FlowDatabaseOperation.deleteFlow(dbHandler, flowId)) {
+	if (FlowDatabaseOperation.deleteFlow(dbHandlerApi, flowId)) {
 	    datagridService.notificationSendFlowRemoved(flowId);
 	    return true;
 	}
@@ -598,7 +601,7 @@
      */
     @Override
     public boolean clearAllFlows() {
-	if (FlowDatabaseOperation.clearAllFlows(dbHandler)) {
+	if (FlowDatabaseOperation.clearAllFlows(dbHandlerApi)) {
 	    datagridService.notificationSendAllFlowsRemoved();
 	    return true;
 	}
@@ -613,7 +616,7 @@
      */
     @Override
     public boolean clearFlow(FlowId flowId) {
-	if (FlowDatabaseOperation.clearFlow(dbHandler, flowId)) {
+	if (FlowDatabaseOperation.clearFlow(dbHandlerApi, flowId)) {
 	    datagridService.notificationSendFlowRemoved(flowId);
 	    return true;
 	}
@@ -628,7 +631,7 @@
      */
     @Override
     public FlowPath getFlow(FlowId flowId) {
-	return FlowDatabaseOperation.getFlow(dbHandler, flowId);
+	return FlowDatabaseOperation.getFlow(dbHandlerApi, flowId);
     }
 
     /**
@@ -638,7 +641,7 @@
      */
     @Override
     public ArrayList<FlowPath> getAllFlows() {
-	return FlowDatabaseOperation.getAllFlows(dbHandler);
+	return FlowDatabaseOperation.getAllFlows(dbHandlerApi);
     }
 
     /**
@@ -652,7 +655,7 @@
     @Override
     public ArrayList<FlowPath> getAllFlows(CallerId installerId,
 					   DataPathEndpoints dataPathEndpoints) {
-	return FlowDatabaseOperation.getAllFlows(dbHandler, installerId,
+	return FlowDatabaseOperation.getAllFlows(dbHandlerApi, installerId,
 						 dataPathEndpoints);
     }
 
@@ -664,7 +667,8 @@
      */
     @Override
     public ArrayList<FlowPath> getAllFlows(DataPathEndpoints dataPathEndpoints) {
-	return FlowDatabaseOperation.getAllFlows(dbHandler, dataPathEndpoints);
+	return FlowDatabaseOperation.getAllFlows(dbHandlerApi,
+						 dataPathEndpoints);
     }
 
     /**
@@ -677,7 +681,7 @@
     @Override
     public ArrayList<IFlowPath> getAllFlowsSummary(FlowId flowId,
 						   int maxFlows) {
-	return FlowDatabaseOperation.getAllFlowsSummary(dbHandler, flowId,
+	return FlowDatabaseOperation.getAllFlowsSummary(dbHandlerApi, flowId,
 							maxFlows);
     }
     
@@ -687,7 +691,7 @@
      * @return all Flows information, without the associated Flow Entries.
      */
     public ArrayList<IFlowPath> getAllFlowsWithoutFlowEntries() {
-	return FlowDatabaseOperation.getAllFlowsWithoutFlowEntries(dbHandler);
+	return FlowDatabaseOperation.getAllFlowsWithoutFlowEntries(dbHandlerApi);
     }
 
     /**
@@ -865,6 +869,9 @@
 	if (true)
 	    return;
 
+	if (modifiedFlowPaths.isEmpty())
+	    return;
+
 	Map<Long, IOFSwitch> mySwitches = floodlightProvider.getSwitches();
 
 	for (FlowPath flowPath : modifiedFlowPaths) {
@@ -873,7 +880,7 @@
 	    // NOTE: The Flow Path might not be found if the Flow was just
 	    // removed by some other controller instance.
 	    //
-	    IFlowPath flowObj = dbHandler.searchFlowPath(flowPath.flowId());
+	    IFlowPath flowObj = dbHandlerInner.searchFlowPath(flowPath.flowId());
 
 	    boolean isFlowEntryDeleted = false;
 	    for (FlowEntry flowEntry : flowPath.flowEntries()) {
@@ -932,6 +939,14 @@
 		// instance to handle the cleanup of such orphaned flow
 		// entries.
 		//
+		if (mySwitch == null) {
+		    if (flowEntry.flowEntryUserState() !=
+			FlowEntryUserState.FE_USER_DELETE) {
+			continue;
+		    }
+		    if (! flowEntry.isValidFlowEntryId())
+			continue;
+		}
 
 		//
 		// Write the Flow Entry to the Datagrid
@@ -955,14 +970,6 @@
 		//
 		// Write the Flow Entry to the Network Map
 		//
-		if (mySwitch == null) {
-		    if (flowEntry.flowEntryUserState() !=
-			FlowEntryUserState.FE_USER_DELETE) {
-			continue;
-		    }
-		    if (! flowEntry.isValidFlowEntryId())
-			continue;
-		}
 		if (flowObj == null) {
 		    String logMsg = "Cannot find Network MAP entry for Flow Path " + flowPath.flowId();
 		    continue;
@@ -991,9 +998,8 @@
 			break;
 		    }
 		} catch (Exception e) {
-		    String logMsg = "Exception writing Flow Entry to Network MAP";
-		    log.debug(logMsg);
-		    dbHandler.rollback();
+		    log.debug("Exception writing Flow Entry to Network MAP: ", e);
+		    dbHandlerInner.rollback();
 		    continue;
 		}
 	    }
@@ -1018,7 +1024,12 @@
 		flowPath.dataPath().setFlowEntries(newFlowEntries);
 	    }
 	}
-
-	dbHandler.commit();
+	// Try to commit to the database
+	try {
+	    dbHandlerInner.commit();
+	} catch (Exception e) {
+	    log.debug("Exception during commit of Flow Entries to Network MAP", e);
+	    dbHandlerInner.rollback();
+	}
     }
 }
diff --git a/src/main/java/net/onrc/onos/ofcontroller/topology/Topology.java b/src/main/java/net/onrc/onos/ofcontroller/topology/Topology.java
index 7d7d739..dbf9ada 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/topology/Topology.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/topology/Topology.java
@@ -121,7 +121,7 @@
 	Link reverseLink = reverseLinksMap.get(portId);
 	if (reverseLink != null) {
 	    // NOTE: reverseLink.myPort is the neighbor's outgoing port
-	    reverseLink.neighbor.removeLink(reverseLink.myPort);
+	    reverseLink.me.removeLink(reverseLink.myPort);
 	    removeReverseLink(reverseLink);
 	}