Merge branch 'master' of https://github.com/OPENNETWORKINGLAB/ONOS

Conflicts:
	src/test/java/net/floodlightcontroller/linkdiscovery/internal/LinkStorageImplTest.java
diff --git a/src/main/java/net/floodlightcontroller/core/INetMapService.java b/src/main/java/net/floodlightcontroller/core/INetMapService.java
new file mode 100644
index 0000000..fcc542a
--- /dev/null
+++ b/src/main/java/net/floodlightcontroller/core/INetMapService.java
@@ -0,0 +1,5 @@
+package net.floodlightcontroller.core;
+
+public interface INetMapService {
+
+}
diff --git a/src/main/java/net/floodlightcontroller/core/INetMapTopologyService.java b/src/main/java/net/floodlightcontroller/core/INetMapTopologyService.java
new file mode 100644
index 0000000..da00e0f
--- /dev/null
+++ b/src/main/java/net/floodlightcontroller/core/INetMapTopologyService.java
@@ -0,0 +1,36 @@
+package net.floodlightcontroller.core;
+
+import java.util.List;
+
+import net.floodlightcontroller.routing.Link;
+import net.floodlightcontroller.topology.NodePortTuple;
+
+public interface INetMapTopologyService extends INetMapService {
+
+	public interface ITopoSwitchService {
+		List<String> GetActiveSwitches();
+		List<String> GetAllSwitches();
+		List<String> GetInactiveSwitches();
+		List<String> GetPortsOnSwitch(String dpid);
+	}
+	
+	public interface ITopoLinkService {
+		List<Link> GetActiveLinks();
+		List<Link> GetLinksOnSwitch(String dpid);
+	}
+	public interface ITopoDeviceService {
+		List<Link> GetActiveDevices();
+		List<Link> GetDevicesOnSwitch(String dpid);
+	}
+	
+	public interface ITopoRouteService {
+		List<NodePortTuple> GetShortestpath(NodePortTuple src, NodePortTuple dest);
+		Boolean RouteExists(NodePortTuple src, NodePortTuple dest);
+	}
+	
+	public interface ITopoFlowService {
+		Boolean FlowExists(NodePortTuple src, NodePortTuple dest);
+		List<NodePortTuple> GetShortestFlowPath(NodePortTuple src, NodePortTuple dest);
+		
+	}
+}
diff --git a/src/main/java/net/floodlightcontroller/core/ISwitchStorage.java b/src/main/java/net/floodlightcontroller/core/ISwitchStorage.java
index 082995f..8060c4d 100644
--- a/src/main/java/net/floodlightcontroller/core/ISwitchStorage.java
+++ b/src/main/java/net/floodlightcontroller/core/ISwitchStorage.java
@@ -50,9 +50,13 @@
 	public void deletePort(String dpid, String portName);
 	
 	public List<String> getActiveSwitches();
+	public List<String> getAllSwitches();
+	public List<String> getInactiveSwitches();
 	
 	/*
 	 * Initialize
 	 */
 	public void init(String conf);
+	
+
 }
diff --git a/src/main/java/net/floodlightcontroller/core/internal/Controller.java b/src/main/java/net/floodlightcontroller/core/internal/Controller.java
index 0cbcfcb..6e816c7 100644
--- a/src/main/java/net/floodlightcontroller/core/internal/Controller.java
+++ b/src/main/java/net/floodlightcontroller/core/internal/Controller.java
@@ -107,6 +107,7 @@
 import org.openflow.protocol.OFPacketIn;
 import org.openflow.protocol.OFPhysicalPort;
 import org.openflow.protocol.OFPortStatus;
+import org.openflow.protocol.OFPhysicalPort.OFPortConfig;
 import org.openflow.protocol.OFPhysicalPort.OFPortState;
 import org.openflow.protocol.OFPortStatus.OFPortReason;
 import org.openflow.protocol.OFSetConfig;
@@ -1145,12 +1146,10 @@
         short portNumber = m.getDesc().getPortNumber();
         OFPhysicalPort port = m.getDesc();
         if (m.getReason() == (byte)OFPortReason.OFPPR_MODIFY.ordinal()) {
-        	int srcPortState = port.getState();
-            boolean portUp = ((srcPortState &
-                    OFPortState.OFPPS_STP_MASK.getValue()) !=
-                    OFPortState.OFPPS_STP_BLOCK.getValue());
+        	boolean portDown = ((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0) ||
+            		((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0);
             sw.setPort(port);
-           if (portUp) {
+           if (!portDown) {
                swStore.addPort(sw.getStringId(), port);
            } else {
         	   swStore.deletePort(sw.getStringId(), port.getPortNumber());
diff --git a/src/main/java/net/floodlightcontroller/core/internal/SwitchStorageImpl.java b/src/main/java/net/floodlightcontroller/core/internal/SwitchStorageImpl.java
index 0636400..be33659 100644
--- a/src/main/java/net/floodlightcontroller/core/internal/SwitchStorageImpl.java
+++ b/src/main/java/net/floodlightcontroller/core/internal/SwitchStorageImpl.java
@@ -1,5 +1,6 @@
 package net.floodlightcontroller.core.internal;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.Set;
@@ -8,6 +9,8 @@
 import org.slf4j.LoggerFactory;
 
 import org.openflow.protocol.OFPhysicalPort;
+import org.openflow.protocol.OFPhysicalPort.OFPortConfig;
+import org.openflow.protocol.OFPhysicalPort.OFPortState;
 
 import com.thinkaurelius.titan.core.TitanException;
 import com.thinkaurelius.titan.core.TitanFactory;
@@ -63,6 +66,13 @@
 	public void addPort(String dpid, OFPhysicalPort port) {
 		// TODO Auto-generated method stub
 		Vertex sw;
+		
+        boolean portDown = ((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0) ||
+        		((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0);
+       if (portDown) {
+             deletePort(dpid, port.getPortNumber());
+             return;
+       }
 		try {
             if ((sw = graph.getVertices("dpid",dpid).iterator().next()) != null) {
             	log.info("SwitchStorage:addPort dpid:{} port:{}", dpid, port.getPortNumber());
@@ -118,7 +128,11 @@
                     /*
                      *  Do nothing or throw exception?
                      */
-            	log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
+            		Vertex sw = graph.getVertices("dpid",dpid).iterator().next();
+            	
+            		log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
+            		sw.setProperty("state",SwitchState.ACTIVE.toString());
+            		graph.stopTransaction(Conclusion.SUCCESS);
             } else {
                     Vertex sw = graph.addVertex(null);
 
@@ -185,8 +199,14 @@
 
 	@Override
 	public List<String> getActiveSwitches() {
-		// TODO Auto-generated method stub
-		return null;
+		// TODO Add unit test
+		List<String> switches = new ArrayList<String>();
+    	for (Vertex V : graph.getVertices("type","switch")) {
+    		if (V.getProperty("state").equals(SwitchState.ACTIVE.toString())) {
+    		     switches.add((String) V.getProperty("dpid"));
+    		}
+    	}
+		return switches;
 	}
 
 	@Override
@@ -206,4 +226,27 @@
         }
 	}
 
+	@Override
+	public List<String> getAllSwitches() {
+		// TODO Auto-generated method stub
+		List<String> switches = new ArrayList<String>();
+    	for (Vertex V : graph.getVertices("type","switch")) {
+    		switches.add((String) V.getProperty("dpid"));
+    	}
+		return switches;
+	}
+
+	@Override
+	public List<String> getInactiveSwitches() {
+		// TODO Auto-generated method stub
+		List<String> switches = new ArrayList<String>();
+    	for (Vertex V : graph.getVertices("type","switch")) {
+    		if (V.getProperty("state").equals(SwitchState.INACTIVE.toString())) {
+    		     switches.add((String) V.getProperty("dpid"));
+    		}
+    	}
+		return switches;
+	}
+
+	
 }
diff --git a/src/main/java/net/floodlightcontroller/core/internal/SwitchStorageImplStubs.java b/src/main/java/net/floodlightcontroller/core/internal/SwitchStorageImplStubs.java
index c281141..88f94db 100644
--- a/src/main/java/net/floodlightcontroller/core/internal/SwitchStorageImplStubs.java
+++ b/src/main/java/net/floodlightcontroller/core/internal/SwitchStorageImplStubs.java
@@ -111,4 +111,16 @@
 		
 	}
 
+	@Override
+	public List<String> getAllSwitches() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public List<String> getInactiveSwitches() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
 }
diff --git a/src/main/java/net/floodlightcontroller/core/internal/TopoSwitchServiceImpl.java b/src/main/java/net/floodlightcontroller/core/internal/TopoSwitchServiceImpl.java
new file mode 100644
index 0000000..99f291d
--- /dev/null
+++ b/src/main/java/net/floodlightcontroller/core/internal/TopoSwitchServiceImpl.java
@@ -0,0 +1,45 @@
+package net.floodlightcontroller.core.internal;
+
+import java.util.List;
+
+import net.floodlightcontroller.core.INetMapTopologyService.ITopoSwitchService;
+
+public class TopoSwitchServiceImpl implements ITopoSwitchService {
+	
+	ThreadLocal<SwitchStorageImpl> store = new ThreadLocal<SwitchStorageImpl>() {
+		@Override
+		protected SwitchStorageImpl initialValue() {
+			SwitchStorageImpl swStore = new SwitchStorageImpl();
+			//TODO: Get the file path from global properties
+			swStore.init("/tmp/cassandra.titan");
+			return swStore;
+		}
+	};
+	
+	SwitchStorageImpl swStore = store.get();
+	
+	@Override
+	public List<String> GetActiveSwitches() {
+		// TODO Auto-generated method stub
+		return swStore.getActiveSwitches();
+	}
+
+	@Override
+	public List<String> GetAllSwitches() {
+		// TODO Auto-generated method stub
+		return swStore.getAllSwitches();
+	}
+
+	@Override
+	public List<String> GetInactiveSwitches() {
+		// TODO Auto-generated method stub
+		return swStore.getInactiveSwitches();
+	}
+
+	@Override
+	public List<String> GetPortsOnSwitch(String dpid) {
+		// TODO Auto-generated method stub
+		return null;
+	}
+    
+}
diff --git a/src/main/java/net/floodlightcontroller/core/web/CoreWebRoutable.java b/src/main/java/net/floodlightcontroller/core/web/CoreWebRoutable.java
index 45ef6e9..0c41259 100644
--- a/src/main/java/net/floodlightcontroller/core/web/CoreWebRoutable.java
+++ b/src/main/java/net/floodlightcontroller/core/web/CoreWebRoutable.java
@@ -60,6 +60,7 @@
         router.attach("/role/json", ControllerRoleResource.class);
         router.attach("/health/json", HealthCheckResource.class);
         router.attach("/system/uptime/json", SystemUptimeResource.class);
+        router.attach("/topology/switches/{filter}/json", TopoSwitchesResource.class);
         return router;
     }
 }
diff --git a/src/main/java/net/floodlightcontroller/core/web/TopoSwitchesResource.java b/src/main/java/net/floodlightcontroller/core/web/TopoSwitchesResource.java
new file mode 100644
index 0000000..a73b0b4
--- /dev/null
+++ b/src/main/java/net/floodlightcontroller/core/web/TopoSwitchesResource.java
@@ -0,0 +1,27 @@
+package net.floodlightcontroller.core.web;
+
+import java.util.Iterator;
+
+import net.floodlightcontroller.core.internal.TopoSwitchServiceImpl;
+
+import org.restlet.resource.Get;
+import org.restlet.resource.ServerResource;
+
+public class TopoSwitchesResource extends ServerResource {
+	
+	@Get("json")
+	public Iterator<String> retrieve() {
+		TopoSwitchServiceImpl impl = new TopoSwitchServiceImpl();
+		
+		String filter = (String) getRequestAttributes().get("filter");
+		
+		if (filter.equals("active")) {
+			return (Iterator<String>) impl.GetActiveSwitches().iterator();
+		}
+		if (filter.equals("inactive")) {
+			return (Iterator<String>) impl.GetInactiveSwitches().iterator();
+		}
+		return (Iterator<String>) impl.GetAllSwitches().iterator();				
+	}
+
+}
diff --git a/src/main/java/net/floodlightcontroller/linkdiscovery/internal/LinkStorageImpl.java b/src/main/java/net/floodlightcontroller/linkdiscovery/internal/LinkStorageImpl.java
index 9c0ee4e..0e9f32b 100644
--- a/src/main/java/net/floodlightcontroller/linkdiscovery/internal/LinkStorageImpl.java
+++ b/src/main/java/net/floodlightcontroller/linkdiscovery/internal/LinkStorageImpl.java
@@ -139,16 +139,16 @@
          	
          	if (vportSrc != null && vportDst != null) {
          		for (Edge e : vportSrc.getEdges(Direction.OUT)) {
-         			log.debug("deleteLink(): {} {}", e.getLabel(), e.getVertex(Direction.IN));
-         			// if (e.getLabel().equals("link") && e.getVertex(Direction.OUT).equals(vportDst)) {
-             		if (e.getLabel().equals("link")) {
+         			log.debug("deleteLink(): {} in {} out {}", 
+         					new Object[]{e.getLabel(), e.getVertex(Direction.IN), e.getVertex(Direction.OUT)});
+         			if (e.getLabel().equals("link") && e.getVertex(Direction.IN).equals(vportDst)) {
          				graph.removeEdge(e);
          				count++;
          			}
          		}
         		graph.stopTransaction(Conclusion.SUCCESS);
-            	log.debug("deleteLink(): {} {} src {} dst {}", new Object[]{
-            			(count > 0) ? "deleted " + count : "failure", lt, vportSrc, vportDst});
+            	log.debug("deleteLink(): deleted {} edges {} src {} dst {}", new Object[]{
+            			count, lt, vportSrc, vportDst});
             	
             } else {
             	log.error("deleteLink(): failed src port vertex not found {} src {} dst {}", new Object[]{lt, vportSrc, vportDst});