Merge pull request #507 from pingping-lin/master

Fixed the wrong device attachment points
diff --git a/src/main/java/net/onrc/onos/ofcontroller/core/internal/DeviceStorageImpl.java b/src/main/java/net/onrc/onos/ofcontroller/core/internal/DeviceStorageImpl.java
index ba4a3b5..dedf607 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/core/internal/DeviceStorageImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/core/internal/DeviceStorageImpl.java
@@ -328,8 +328,18 @@
 	public void addOnosDevice(OnosDevice onosDevice) {
 		String macAddress = HexString.toHexString(onosDevice.getMacAddress().toBytes());
 		
-		log.debug("addOnosDevice: {}", onosDevice);
+		//if the switch port we try to attach a new device already has a link, then stop adding device
+		IPortObject portObject1 = ope.searchPort(HexString.toHexString(
+				onosDevice.getSwitchDPID()), onosDevice.getSwitchPort());
+
+		if (portObject1.getLinkedPorts().iterator().hasNext()) {
+			log.debug("stop adding OnosDevice: {} due to there is a link to: {}",
+					onosDevice, portObject1.getLinkedPorts().iterator().next().getPortId());
+			return;
+		}
 		
+		log.debug("addOnosDevice: {}", onosDevice);
+
 		try {
 			IDeviceObject device = ope.searchDevice(macAddress);
 			
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 635e24e..7c8541d 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
@@ -6,6 +6,7 @@
 import net.floodlightcontroller.routing.Link;
 import net.onrc.onos.graph.GraphDBOperation;
 import net.onrc.onos.ofcontroller.core.ILinkStorage;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
 import net.onrc.onos.ofcontroller.linkdiscovery.LinkInfo;
@@ -107,11 +108,28 @@
 		return addLink(link, null);
 	}
 
+	private void deleteDeviceOnPort(Long dpid, Short number)
+	{
+		IPortObject srcPortObject = op.searchPort(HexString.toHexString(dpid), number);
+		if (srcPortObject.getDevices().iterator().hasNext()) {
+			for (IDeviceObject deviceObject: srcPortObject.getDevices()) {
+				srcPortObject.removeDevice(deviceObject);
+				log.debug("delete Device "+ deviceObject.getMACAddress() +
+						" from sw: {} port: {} due to a new link added",
+						dpid, number);
+			}
+		}
+	}
+
 	@Override
 	public boolean addLink(Link link, LinkInfo linfo) {
 		boolean success = false;
 		
 		try {
+			//delete the Device attachment points for the related switch and port
+			deleteDeviceOnPort(link.getSrc(),link.getSrcPort());
+			deleteDeviceOnPort(link.getDst(),link.getDstPort());
+
 			if (addLinkImpl(link)) {
 				// Set LinkInfo only if linfo is non-null.
 				if (linfo != null && (! setLinkInfoImpl(link, linfo))) {
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 da5a6f4..d777202 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/floodlightlistener/NetworkGraphPublisher.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/floodlightlistener/NetworkGraphPublisher.java
@@ -173,62 +173,63 @@
     	op.close();
     }
 
-	@Override
-	public void linkDiscoveryUpdate(LDUpdate update) {
-		Link lt = new Link(update.getSrc(),update.getSrcPort(),update.getDst(),update.getDstPort());
-		//log.debug("{}:LinkDicoveryUpdate(): Updating Link {}",this.getClass(), lt);
-		
-		switch (update.getOperation()) {
-			case LINK_REMOVED:
-				log.debug("LinkDiscoveryUpdate(): Removing link {}", lt);
-				
-				if (linkStore.deleteLink(lt)) {
-				    // TODO publish DELETE_LINK event here
-				    TopologyElement topologyElement =
-					new TopologyElement(update.getSrc(),
-							    update.getSrcPort(),
-							    update.getDst(),
-							    update.getDstPort());
-				    datagridService.notificationSendTopologyElementRemoved(topologyElement);
-				}
-				break;
-			case LINK_UPDATED:
-				log.debug("LinkDiscoveryUpdate(): Updating link {}", lt);
-				
-				LinkInfo linfo = linkStore.getLinkInfo(lt);
-				// TODO update "linfo" using portState derived using "update"
-				if (linkStore.update(lt, linfo, DM_OPERATION.UPDATE)) {
-				    // TODO publish UPDATE_LINK event here
-				    //
-				    // TODO NOTE: Here we assume that updated
-				    // link is UP.
-				    //
-				    TopologyElement topologyElement =
-					new TopologyElement(update.getSrc(),
-							    update.getSrcPort(),
-							    update.getDst(),
-							    update.getDstPort());
-				    datagridService.notificationSendTopologyElementUpdated(topologyElement);
-				}
-				break;
-			case LINK_ADDED:
-				log.debug("LinkDiscoveryUpdate(): Adding link {}", lt);
-				
-				if (linkStore.addLink(lt)) {
-				    // TODO publish ADD_LINK event here
-				    TopologyElement topologyElement =
-					new TopologyElement(update.getSrc(),
-							    update.getSrcPort(),
-							    update.getDst(),
-							    update.getDstPort());
-				    datagridService.notificationSendTopologyElementAdded(topologyElement);
-				}
-				break;
-			default:
-				break;
-		}
+    @Override
+    public void linkDiscoveryUpdate(LDUpdate update) {
+    	Link lt = new Link(update.getSrc(),update.getSrcPort(),update.getDst(),update.getDstPort());
+    	//log.debug("{}:LinkDicoveryUpdate(): Updating Link {}",this.getClass(), lt);
 
-	}
+    	switch (update.getOperation()) {
+    	case LINK_REMOVED:
+    		log.debug("LinkDiscoveryUpdate(): Removing link {}", lt);
+
+    		if (linkStore.deleteLink(lt)) {
+    			// TODO publish DELETE_LINK event here
+    			TopologyElement topologyElement =
+    					new TopologyElement(update.getSrc(),
+    							update.getSrcPort(),
+    							update.getDst(),
+    							update.getDstPort());
+    			datagridService.notificationSendTopologyElementRemoved(topologyElement);
+    		}
+    		break;
+    	case LINK_UPDATED:
+    		log.debug("LinkDiscoveryUpdate(): Updating link {}", lt);
+
+    		LinkInfo linfo = linkStore.getLinkInfo(lt);
+    		// TODO update "linfo" using portState derived using "update"
+    		if (linkStore.update(lt, linfo, DM_OPERATION.UPDATE)) {
+    			// TODO publish UPDATE_LINK event here
+    			//
+    			// TODO NOTE: Here we assume that updated
+    			// link is UP.
+    			//
+    			TopologyElement topologyElement =
+    					new TopologyElement(update.getSrc(),
+    							update.getSrcPort(),
+    							update.getDst(),
+    							update.getDstPort());
+    			datagridService.notificationSendTopologyElementUpdated(topologyElement);
+    		}
+    		break;
+    	case LINK_ADDED:
+    		log.debug("LinkDiscoveryUpdate(): Adding link {}", lt);
+
+    		if (linkStore.addLink(lt)) {
+    			// TODO publish ADD_LINK event here
+    			TopologyElement topologyElement =
+    					new TopologyElement(update.getSrc(),
+    							update.getSrcPort(),
+    							update.getDst(),
+    							update.getDstPort());
+    			datagridService.notificationSendTopologyElementAdded(topologyElement);
+    		}
+
+    		break;
+    	default:
+    		break;
+    	}
+
+    }
 
 	@Override
 	public void addedSwitch(IOFSwitch sw) {
diff --git a/src/main/java/net/onrc/onos/ofcontroller/flowprogrammer/FlowProgrammer.java b/src/main/java/net/onrc/onos/ofcontroller/flowprogrammer/FlowProgrammer.java
index a4f0a8c..641faaf 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/flowprogrammer/FlowProgrammer.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/flowprogrammer/FlowProgrammer.java
@@ -139,8 +139,8 @@
 	switch (msg.getType()) {
 	case FLOW_REMOVED:
 	    OFFlowRemoved flowMsg = (OFFlowRemoved) msg;
-	    log.debug("Got flow removed from "+ sw.getId() +": "+ flowMsg.getCookie());
 	    FlowEntryId id = new FlowEntryId(flowMsg.getCookie());
+	    log.debug("Got flow entry removed from " + sw.getId() + ": " + id);
 	    flowManager.flowEntryOnSwitchExpired(sw, id);
 	    break;
 	default: