Add helper method to populate NetworkGraphImpl from DB for Debugging purpose.

Change-Id: Ib5f2a65d8cdecbfb1f620b67fc462430eb74badb
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 29cc1d7..0debb70 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java
@@ -1,7 +1,9 @@
 package net.onrc.onos.ofcontroller.networkgraph;
 
+import net.onrc.onos.datastore.topology.RCLink;
 import net.onrc.onos.datastore.topology.RCPort;
 import net.onrc.onos.datastore.topology.RCSwitch;
+import net.onrc.onos.ofcontroller.util.Dpid;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -11,8 +13,7 @@
 /**
  * The "NB" read-only Network Map.
  *
- * TODO Current implementation directly read from DB, but eventually, it should
- * read from In-memory shared Network Map instance within ONOS process.
+ * TODO To be synchronized based on TopologyEvent Notification.
  *
  */
 public class NetworkGraphImpl extends AbstractNetworkGraph {
@@ -25,32 +26,34 @@
     }
 
     void addSwitch(Switch sw) {
-	if ( sw == null ) {
+	if (sw == null) {
 	    throw new IllegalArgumentException("Switch cannot be null");
 	}
 	switches.put(sw.getDpid(), sw);
-
     }
 
     /**
-     * Deactivate Switch (and its Ports)
+     * Deactivate Switch (and its Ports?)
+     *
      * @param sw
      */
     void deactivateSwitch(Switch sw) {
-	if ( sw == null ) {
+	if (sw == null) {
 	    throw new IllegalArgumentException("Switch cannot be null");
 	}
 	SwitchImpl s = getSwitchImpl(sw);
+	// XXX When modifying existing object should we change the object itself
+	// or create a modified copy and switch them?
+
 	// TODO Deactivate Switch
 
 	// XXX Are we sure we want to deactivate Ports also?
 
 	// TODO Auto-generated method stub
-
     }
 
     void addPort(Port port) {
-	if ( port == null ) {
+	if (port == null) {
 	    throw new IllegalArgumentException("Port cannot be null");
 	}
 	// TODO Auto-generated method stub
@@ -58,7 +61,7 @@
     }
 
     void deactivatePort(Port port) {
-	if ( port == null ) {
+	if (port == null) {
 	    throw new IllegalArgumentException("Port cannot be null");
 	}
 	// TODO Auto-generated method stub
@@ -66,7 +69,7 @@
     }
 
     void addLink(Link link) {
-	if ( link == null ) {
+	if (link == null) {
 	    throw new IllegalArgumentException("Link cannot be null");
 	}
 	// TODO Auto-generated method stub
@@ -74,7 +77,7 @@
     }
 
     void removeLink(Link link) {
-	if ( link == null ) {
+	if (link == null) {
 	    throw new IllegalArgumentException("Link cannot be null");
 	}
 	// TODO Auto-generated method stub
@@ -82,7 +85,7 @@
     }
 
     void updateDevice(Device device) {
-	if ( device == null ) {
+	if (device == null) {
 	    throw new IllegalArgumentException("Device cannot be null");
 	}
 	// TODO Auto-generated method stub
@@ -90,7 +93,7 @@
     }
 
     void removeDevice(Device device) {
-	if ( device == null ) {
+	if (device == null) {
 	    throw new IllegalArgumentException("Device cannot be null");
 	}
 	// TODO Auto-generated method stub
@@ -98,10 +101,93 @@
     }
 
     private SwitchImpl getSwitchImpl(Switch sw) {
-	if( sw instanceof SwitchImpl ) {
+	if (sw instanceof SwitchImpl) {
 	    return (SwitchImpl) sw;
 	}
-	throw new ClassCastException("SwitchImpl expected, but found:" + sw.getClass().getName()  );
+	throw new ClassCastException("SwitchImpl expected, but found:"
+	        + sw.getClass().getName());
+    }
+
+    public void loadWholeTopologyFromDB() {
+	// XXX clear everything first?
+
+	for (RCSwitch sw : RCSwitch.getAllSwitches()) {
+	    try {
+		sw.read();
+		// TODO SwitchImpl probably should have a constructor from
+		// RCSwitch
+		SwitchImpl memSw = new SwitchImpl(this);
+		memSw.setDpid(sw.getDpid());
+
+		addSwitch(memSw);
+	    } catch (ObjectDoesntExistException e) {
+		log.error("Read Switch Failed, skipping", e);
+	    }
+	}
+
+	for (RCPort p : RCPort.getAllPorts()) {
+	    try {
+		p.read();
+
+		// TODO PortImpl probably should have a constructor from RCPort
+		PortImpl memPort = new PortImpl(this);
+		// FIXME remove shortValue()
+		memPort.setPortNumber(p.getNumber().shortValue());
+		Switch sw = this.getSwitch(p.getDpid());
+		if (sw == null) {
+		    log.error("Switch {} missing when adding Port {}",
+			    new Dpid(p.getDpid()), p);
+		    continue;
+		}
+		memPort.setSwitch(sw);
+
+		addPort(memPort);
+	    } catch (ObjectDoesntExistException e) {
+		log.error("Read Port Failed, skipping", e);
+	    }
+	}
+
+	// TODO Is Device going to be in DB? If so, read from DB.
+	// for (RCDevice d : RCDevice.getAllDevices()) {
+	// try {
+	// d.read();
+	//
+	// } catch (ObjectDoesntExistException e) {
+	// log.debug("Read Device Failed, skipping", e);
+	// }
+	// }
+
+	for (RCLink l : RCLink.getAllLinks()) {
+	    try {
+		l.read();
+
+		LinkImpl memLink = new LinkImpl(this);
+
+		Switch srcSw = this.getSwitch(l.getSrc().dpid);
+		if (srcSw == null) {
+		    log.error("Switch {} missing when adding Link {}",
+			    new Dpid(l.getSrc().dpid), l);
+		    continue;
+		}
+		memLink.setSrcSwitch(srcSw);
+		// FIXME remove shortValue()
+		memLink.setSrcPort(srcSw.getPort(l.getSrc().number.shortValue()));
+
+		Switch dstSw = this.getSwitch(l.getDst().dpid);
+		if (dstSw == null) {
+		    log.error("Switch {} missing when adding Link {}",
+			    new Dpid(l.getDst().dpid), l);
+		    continue;
+		}
+		memLink.setDstSwitch(dstSw);
+		// FIXME remove shortValue()
+		memLink.setDstPort(srcSw.getPort(l.getDst().number.shortValue()));
+
+		addLink(memLink);
+	    } catch (ObjectDoesntExistException e) {
+		log.debug("Delete Link Failed", e);
+	    }
+	}
     }
 
     // FIXME To be removed later this class should never read from DB.