Merge pull request #446 from jonohart/master

Addressing link discovery bugs
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 a3ab7b9..09e87ca 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
@@ -119,11 +119,15 @@
 			if (addLinkImpl(link)) {
 				// Set LinkInfo only if linfo is non-null.
 				if (linfo != null && (! setLinkInfoImpl(link, linfo))) {
+					log.debug("Adding linkinfo failed: {}", link);
 					op.rollback();
 				}
 				op.commit();
 				success = true;
 			} else {
+				// If we fail here that's because the ports aren't added
+				// before we try to add the link
+				log.debug("Adding link failed: {}", link);
 				op.rollback();
 			}
 		} catch (Exception e) {
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 26d4e40..ac61b51 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
@@ -137,9 +137,13 @@
 			for (OFPhysicalPort port: sw.getPorts()) {
 				IPortObject p = op.searchPort(dpid, port.getPortNumber());
 				if (p != null) {
-		    		log.error("SwitchStorage:addPort dpid:{} port:{} exists", dpid, port.getPortNumber());
+		    		log.debug("SwitchStorage:addPort dpid:{} port:{} exists", dpid, port.getPortNumber());
 		    		setPortStateImpl(p, port.getState(), port.getName());
 		    		p.setState("ACTIVE");
+		    		if (curr.getPort(port.getPortNumber()) == null) {
+		    			// The port exists but the switch has no "on" link to it
+		    			curr.addPort(p);
+		    		}
 				} else {
 					p = addPortImpl(curr, port.getPortNumber());
 					setPortStateImpl(p, port.getState(), port.getName());
@@ -149,8 +153,9 @@
 			success = true;
 		} catch (Exception e) {
 			op.rollback();
-			e.printStackTrace();
-			log.error("SwitchStorage:addSwitch dpid:{} failed", dpid);
+			//e.printStackTrace();
+			log.error("SwitchStorage:addSwitch dpid:{} failed: {}", dpid);
+			log.error("switch write error", e);
 		}
 		
 		return success;
@@ -160,6 +165,10 @@
 	 * This function is for adding the switch into the DB.
 	 * @param dpid The switch dpid you want to add into the DB.
 	 */
+	// This method is only called by tests, so we probably don't need it.
+	// If we need both addSwitch interfaces, one should call the other
+	// rather than implementing the same logic twice.
+	@Deprecated 
 	@Override
 	public boolean addSwitch(String dpid) {
 		boolean success = false;