Fixed Issue #207:
Implement remaining API for getting flow state

NOTE: The implementation of the API backend is sub-optimal,
but should be sufficient for now:
  We fetch all flows, and then return only the subset that match
  the query conditions.
  We should use the appropriate Titan/Gremlin query to filter-out
  the flows as appropriate.

Estimated: 2D
Actual: 1D
diff --git a/src/main/java/net/floodlightcontroller/flowcache/FlowManager.java b/src/main/java/net/floodlightcontroller/flowcache/FlowManager.java
index 11519b7..c310ed9 100644
--- a/src/main/java/net/floodlightcontroller/flowcache/FlowManager.java
+++ b/src/main/java/net/floodlightcontroller/flowcache/FlowManager.java
@@ -469,7 +469,162 @@
 	//
 	// Extract the Flow state
 	//
+	FlowPath flowPath = extractFlowPath(flowObj);
+	conn.endTx(Transaction.COMMIT);
+
+	return flowPath;
+    }
+
+    /**
+     * Get all previously added flows by a specific installer for a given
+     * data path endpoints.
+     *
+     * @param installerId the Caller ID of the installer of the flow to get.
+     * @param dataPathEndpoints the data path endpoints of the flow to get.
+     * @return the Flow Paths if found, otherwise null.
+     */
+    @Override
+    public ArrayList<FlowPath> getAllFlows(CallerId installerId,
+					   DataPathEndpoints dataPathEndpoints) {
+	//
+	// TODO: The implementation below is not optimal:
+	// We fetch all flows, and then return only the subset that match
+	// the query conditions.
+	// We should use the appropriate Titan/Gremlin query to filter-out
+	// the flows as appropriate.
+	//
+	ArrayList<FlowPath> allFlows = getAllFlows();
+
+	if (allFlows == null) {
+	    log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints);
+	    return null;
+	}
+
+	ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
+	for (FlowPath flow : allFlows) {
+	    //
+	    // TODO: String-based comparison is sub-optimal.
+	    // We are using it for now to save us the extra work of
+	    // implementing the "equals()" and "haskCode()" methods.
+	    //
+	    if (! flow.installerId().toString().equals(installerId.toString()))
+		continue;
+	    if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) {
+		continue;
+	    }
+	    if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) {
+		continue;
+	    }
+	    flowPaths.add(flow);
+	}
+
+	if (flowPaths.isEmpty()) {
+	    log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints);
+	    flowPaths = null;
+	} else {
+	    log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: FlowPaths are found", installerId, dataPathEndpoints);
+	}
+
+	return flowPaths;
+    }
+
+    /**
+     * Get all installed flows by all installers for given data path endpoints.
+     *
+     * @param dataPathEndpoints the data path endpoints of the flows to get.
+     * @return the Flow Paths if found, otherwise null.
+     */
+    @Override
+    public ArrayList<FlowPath> getAllFlows(DataPathEndpoints dataPathEndpoints) {
+	//
+	// TODO: The implementation below is not optimal:
+	// We fetch all flows, and then return only the subset that match
+	// the query conditions.
+	// We should use the appropriate Titan/Gremlin query to filter-out
+	// the flows as appropriate.
+	//
+	ArrayList<FlowPath> allFlows = getAllFlows();
+
+	if (allFlows == null) {
+	    log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints);
+	    return null;
+	}
+
+	ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
+	for (FlowPath flow : allFlows) {
+	    //
+	    // TODO: String-based comparison is sub-optimal.
+	    // We are using it for now to save us the extra work of
+	    // implementing the "equals()" and "haskCode()" methods.
+	    //
+	    if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) {
+		continue;
+	    }
+	    if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) {
+		continue;
+	    }
+	    flowPaths.add(flow);
+	}
+
+	if (flowPaths.isEmpty()) {
+	    log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints);
+	    flowPaths = null;
+	} else {
+	    log.debug("Get FlowPaths for dataPathEndpoints{}: FlowPaths are found", dataPathEndpoints);
+	}
+
+	return flowPaths;
+    }
+
+    /**
+     * Get all installed flows by all installers.
+     *
+     * @return the Flow Paths if found, otherwise null.
+     */
+    @Override
+    public ArrayList<FlowPath> getAllFlows() {
+	Iterable<IFlowPath> flowPathsObj = null;
+
+	try {
+	    if ((flowPathsObj = conn.utils().getAllFlowPaths(conn)) != null) {
+		log.debug("Get all FlowPaths: found FlowPaths");
+	    } else {
+		log.debug("Get all FlowPaths: no FlowPaths found");
+	    }
+	} catch (Exception e) {
+	    // TODO: handle exceptions
+	    conn.endTx(Transaction.ROLLBACK);
+	    log.error(":getAllFlowPaths failed");
+	}
+	if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false))
+	    return null;	// No Flows found
+
+	ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
+	for (IFlowPath flowObj : flowPathsObj) {
+	    //
+	    // Extract the Flow state
+	    //
+	    FlowPath flowPath = extractFlowPath(flowObj);
+	    flowPaths.add(flowPath);
+	}
+
+	conn.endTx(Transaction.COMMIT);
+
+	return flowPaths;
+    }
+
+    /**
+     * Extract Flow Path State from a Titan Database Object @ref IFlowPath.
+     *
+     * @param flowObj the object to extract the Flow Path State from.
+     * @return the extracted Flow Path State.
+     */
+    private FlowPath extractFlowPath(IFlowPath flowObj) {
 	FlowPath flowPath = new FlowPath();
+
+	//
+	// Extract the Flow state
+	//
 	flowPath.setFlowId(new FlowId(flowObj.getFlowId()));
 	flowPath.setInstallerId(new CallerId(flowObj.getInstallerId()));
 	flowPath.dataPath().srcPort().setDpid(new Dpid(flowObj.getSrcSwitch()));
@@ -497,46 +652,7 @@
 	    //
 	    flowPath.dataPath().flowEntries().add(flowEntry);
 	}
-	conn.endTx(Transaction.COMMIT);
 
 	return flowPath;
     }
-
-    /**
-     * Get a previously added flow by a specific installer for given
-     * data path endpoints.
-     *
-     * @param installerId the Caller ID of the installer of the flow to get.
-     * @param dataPathEndpoints the data path endpoints of the flow to get.
-     * @return the Flow Path if found, otherwise null.
-     */
-    @Override
-    public FlowPath getFlow(CallerId installerId,
-			    DataPathEndpoints dataPathEndpoints) {
-	// TODO
-	return null;
-    }
-
-    /**
-     * Get all installed flows by all installers for given data path endpoints.
-     *
-     * @param dataPathEndpoints the data path endpoints of the flows to get.
-     * @return the Flow Paths if found, otherwise null.
-     */
-    @Override
-    public ArrayList<FlowPath> getAllFlows(DataPathEndpoints dataPathEndpoints) {
-	// TODO
-	return null;
-    }
-
-    /**
-     * Get all installed flows by all installers.
-     *
-     * @return the Flow Paths if found, otherwise null.
-     */
-    @Override
-    public ArrayList<FlowPath> getAllFlows() {
-	// TODO
-	return null;
-    }
 }
diff --git a/src/main/java/net/floodlightcontroller/flowcache/IFlowService.java b/src/main/java/net/floodlightcontroller/flowcache/IFlowService.java
index c14c0c1..b159661 100644
--- a/src/main/java/net/floodlightcontroller/flowcache/IFlowService.java
+++ b/src/main/java/net/floodlightcontroller/flowcache/IFlowService.java
@@ -41,15 +41,15 @@
     FlowPath getFlow(FlowId flowId);
 
     /**
-     * Get a previously added flow by a specific installer for given
+     * Get all previously added flows by a specific installer for a given
      * data path endpoints.
      *
      * @param installerId the Caller ID of the installer of the flow to get.
      * @param dataPathEndpoints the data path endpoints of the flow to get.
-     * @return the Flow Path if found, otherwise null.
+     * @return the Flow Paths if found, otherwise null.
      */
-    FlowPath getFlow(CallerId installerId,
-		     DataPathEndpoints dataPathEndpoints);
+    ArrayList<FlowPath> getAllFlows(CallerId installerId,
+				 DataPathEndpoints dataPathEndpoints);
 
     /**
      * Get all installed flows by all installers for given data path endpoints.
diff --git a/src/main/java/net/floodlightcontroller/flowcache/web/FlowWebRoutable.java b/src/main/java/net/floodlightcontroller/flowcache/web/FlowWebRoutable.java
index a885bdb..cfd3505 100644
--- a/src/main/java/net/floodlightcontroller/flowcache/web/FlowWebRoutable.java
+++ b/src/main/java/net/floodlightcontroller/flowcache/web/FlowWebRoutable.java
@@ -16,8 +16,8 @@
         router.attach("/add/json", AddFlowResource.class);
         router.attach("/delete/{flow-id}/json", DeleteFlowResource.class);
         router.attach("/get/{flow-id}/json", GetFlowByIdResource.class);
-        router.attach("/get/{installer-id}/{src-dpid}/{src-port}/{dst-dpid}/{dst-port}/json", GetFlowByInstallerIdResource.class);
-        router.attach("/getall/{src-dpid}/{src-port}/{dst-dpid}/{dst-port}/json", GetAllFlowsByEndpointsResource.class);
+        router.attach("/getall-by-installer-id/{installer-id}/{src-dpid}/{src-port}/{dst-dpid}/{dst-port}/json", GetAllFlowsByInstallerIdResource.class);
+        router.attach("/getall-by-endpoints/{src-dpid}/{src-port}/{dst-dpid}/{dst-port}/json", GetAllFlowsByEndpointsResource.class);
         router.attach("/getall/json", GetAllFlowsResource.class);
         return router;
     }
diff --git a/src/main/java/net/floodlightcontroller/flowcache/web/GetAllFlowsByInstallerIdResource.java b/src/main/java/net/floodlightcontroller/flowcache/web/GetAllFlowsByInstallerIdResource.java
index 33eca0b..e3043dc 100644
--- a/src/main/java/net/floodlightcontroller/flowcache/web/GetAllFlowsByInstallerIdResource.java
+++ b/src/main/java/net/floodlightcontroller/flowcache/web/GetAllFlowsByInstallerIdResource.java
@@ -1,5 +1,7 @@
 package net.floodlightcontroller.flowcache.web;
 
+import java.util.ArrayList;
+
 import net.floodlightcontroller.flowcache.IFlowService;
 import net.floodlightcontroller.util.CallerId;
 import net.floodlightcontroller.util.DataPathEndpoints;
@@ -13,12 +15,12 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class GetFlowByInstallerIdResource extends ServerResource {
-    protected static Logger log = LoggerFactory.getLogger(GetFlowByInstallerIdResource.class);
+public class GetAllFlowsByInstallerIdResource extends ServerResource {
+    protected static Logger log = LoggerFactory.getLogger(GetAllFlowsByInstallerIdResource.class);
 
     @Get("json")
-    public FlowPath retrieve() {
-	FlowPath result = null;
+    public ArrayList<FlowPath> retrieve() {
+	ArrayList<FlowPath> result = null;
 
         IFlowService flowService =
                 (IFlowService)getContext().getAttributes().
@@ -36,7 +38,8 @@
         String dstDpidStr = (String) getRequestAttributes().get("dst-dpid");
         String dstPortStr = (String) getRequestAttributes().get("dst-port");
 
-	log.debug("Get Flow By Installer: " + installerIdStr + " Endpoints: " +
+	log.debug("Get All Flow By Installer: " + installerIdStr +
+		  " Endpoints: " +
 		  srcDpidStr + "--" + srcPortStr + "--" +
 		  dstDpidStr + "--" + dstPortStr);
 
@@ -50,7 +53,7 @@
 	DataPathEndpoints dataPathEndpoints =
 	    new DataPathEndpoints(srcSwitchPort, dstSwitchPort);
 
-	result = flowService.getFlow(installerId, dataPathEndpoints);
+	result = flowService.getAllFlows(installerId, dataPathEndpoints);
 
         return result;
     }
diff --git a/src/main/java/net/onrc/onos/util/GraphDBUtils.java b/src/main/java/net/onrc/onos/util/GraphDBUtils.java
index ebc5942..097cfa0 100644
--- a/src/main/java/net/onrc/onos/util/GraphDBUtils.java
+++ b/src/main/java/net/onrc/onos/util/GraphDBUtils.java
@@ -100,6 +100,13 @@
 	}
 
 	@Override
+        public Iterable<IFlowPath> getAllFlowPaths(GraphDBConnection conn) {
+		FramedGraph<TitanGraph> fg = conn.getFramedGraph();
+		
+		return fg.getVertices("type", "flow", IFlowPath.class);
+	}
+
+	@Override
 	public IFlowEntry searchFlowEntry(GraphDBConnection conn,
 					  FlowEntryId flowEntryId) {
 		FramedGraph<TitanGraph> fg = conn.getFramedGraph();
diff --git a/src/main/java/net/onrc/onos/util/IDBUtils.java b/src/main/java/net/onrc/onos/util/IDBUtils.java
index 4ff70f3..48d5946 100644
--- a/src/main/java/net/onrc/onos/util/IDBUtils.java
+++ b/src/main/java/net/onrc/onos/util/IDBUtils.java
@@ -20,6 +20,7 @@
 	public void removeFlowPath(GraphDBConnection conn, IFlowPath flowPath);
         public IFlowPath getFlowPathByFlowEntry(GraphDBConnection conn,
 						IFlowEntry flowEntry);
+	public Iterable<IFlowPath> getAllFlowPaths(GraphDBConnection conn);
 	public IFlowEntry searchFlowEntry(GraphDBConnection conn,
 					  FlowEntryId flowEntryId);
 	public IFlowEntry newFlowEntry(GraphDBConnection conn);
diff --git a/web/add_flow.py b/web/add_flow.py
index 5c3b3ab..18846b7 100755
--- a/web/add_flow.py
+++ b/web/add_flow.py
@@ -84,7 +84,7 @@
     exit(1)
 
 if __name__ == "__main__":
-  usage_msg = "Usage: %s <flow-id> <src-dpid> <src-port> <dest-dpid> <dest-port>" % (sys.argv[0])
+  usage_msg = "Usage: %s <flow-id> <installer-id> <src-dpid> <src-port> <dest-dpid> <dest-port>" % (sys.argv[0])
 
   # app.debug = False;
 
@@ -94,14 +94,14 @@
     exit(0)
 
   # Check arguments
-  if len(sys.argv) < 6:
+  if len(sys.argv) < 7:
     log_error(usage_msg)
     exit(1)
 
   # Do the work
-  data_path = shortest_path(sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]);
-  my_installer_id = 'ONOS-Path-Computation-Python'
-  my_flow_id = sys.argv[1];
+  my_flow_id = sys.argv[1]
+  my_installer_id = sys.argv[2];	# 'ONOS-Path-Computation-Python'
+  data_path = shortest_path(sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6])
 
   debug("Data Path: %s" % data_path)
 
diff --git a/web/get_flow.py b/web/get_flow.py
index 5988978..b20e134 100755
--- a/web/get_flow.py
+++ b/web/get_flow.py
@@ -32,6 +32,25 @@
 # @app.route("/wm/flow/get/<flow-id>/json")
 # Sample output:
 # {"flowId":{"value":"0x5"},"installerId":{"value":"FOOBAR"},"dataPath":{"srcPort":{"dpid":{"value":"00:00:00:00:00:00:00:01"},"port":{"value":0}},"dstPort":{"dpid":{"value":"00:00:00:00:00:00:00:02"},"port":{"value":0}},"flowEntries":[{"flowEntryId":"0x1389","flowEntryMatch":null,"flowEntryActions":null,"dpid":{"value":"00:00:00:00:00:00:00:01"},"inPort":{"value":0},"outPort":{"value":1},"flowEntryUserState":"FE_USER_DELETE","flowEntrySwitchState":"FE_SWITCH_NOT_UPDATED","flowEntryErrorState":null},{"flowEntryId":"0x138a","flowEntryMatch":null,"flowEntryActions":null,"dpid":{"value":"00:00:00:00:00:00:00:02"},"inPort":{"value":9},"outPort":{"value":0},"flowEntryUserState":"FE_USER_DELETE","flowEntrySwitchState":"FE_SWITCH_NOT_UPDATED","flowEntryErrorState":null}]}}
+
+def print_flow_path(parsedResult):
+  flowId = parsedResult['flowId']['value'];
+  installerId = parsedResult['installerId']['value'];
+  srcSwitch = parsedResult['dataPath']['srcPort']['dpid']['value'];
+  srcPort = parsedResult['dataPath']['srcPort']['port']['value'];
+  dstSwitch = parsedResult['dataPath']['dstPort']['dpid']['value'];
+  dstPort = parsedResult['dataPath']['dstPort']['port']['value'];
+
+  print "FlowPath: (flowId = %s installerId = %s src = %s/%s dst = %s/%s)" % (flowId, installerId, srcSwitch, srcPort, dstSwitch, dstPort)
+
+  for f in parsedResult['dataPath']['flowEntries']:
+    inPort = f['inPort']['value'];
+    outPort = f['outPort']['value'];
+    dpid = f['dpid']['value']
+    userState = f['flowEntryUserState']
+    switchState = f['flowEntrySwitchState']
+    print "  FlowEntry: (%s, %s, %s, %s, %s)" % (inPort, dpid, outPort, userState, switchState)
+
 def get_flow_path(flow_id):
   try:
     command = "curl -s \"http://%s:%s/wm/flow/get/%s/json\"" % (ControllerIP, ControllerPort, flow_id)
@@ -49,26 +68,78 @@
     log_error("Controller IF has issue")
     exit(1)
 
-  flowId = parsedResult['flowId']['value'];
-  installerId = parsedResult['installerId']['value'];
-  srcSwitch = parsedResult['dataPath']['srcPort']['dpid']['value'];
-  srcPort = parsedResult['dataPath']['srcPort']['port']['value'];
-  dstSwitch = parsedResult['dataPath']['dstPort']['dpid']['value'];
-  dstPort = parsedResult['dataPath']['dstPort']['port']['value'];
+  print_flow_path(parsedResult)
 
-  print "FlowPath: (flowId = %s installerId = %s src = %s/%s dst = %s/%s)" % (flowId, installerId, srcSwitch, srcPort, dstSwitch, dstPort)
 
-  for f in parsedResult['dataPath']['flowEntries']:
-    inPort = f['inPort']['value'];
-    outPort = f['outPort']['value'];
-    dpid = f['dpid']['value']
-    userState = f['flowEntryUserState']
-    switchState = f['flowEntrySwitchState']
-    print "FlowEntry: (%s, %s, %s, %s, %s)" % (inPort, dpid, outPort, userState, switchState)
+def get_installer_flow_paths(installer_id, v1, p1, v2, p2):
+  try:
+    command = "curl -s \"http://%s:%s/wm/flow/getall-by-installer-id/%s/%s/%s/%s/%s/json\"" % (ControllerIP, ControllerPort, installer_id, v1, p1, v2, p2)
+    debug("get_installer_flow_paths %s" % command)
 
+    result = os.popen(command).read()
+    debug("result %s" % result)
+    if len(result) == 0:
+	print "No Flows found"
+	return;
+
+    parsedResult = json.loads(result)
+    debug("parsed %s" % parsedResult)
+  except:
+    log_error("Controller IF has issue")
+    exit(1)
+
+  for flowPath in parsedResult:
+    print_flow_path(flowPath)
+
+
+def get_endpoints_flow_paths(v1, p1, v2, p2):
+  try:
+    command = "curl -s \"http://%s:%s/wm/flow/getall-by-endpoints/%s/%s/%s/%s/json\"" % (ControllerIP, ControllerPort, v1, p1, v2, p2)
+    debug("get_endpoints_flow_paths %s" % command)
+
+    result = os.popen(command).read()
+    debug("result %s" % result)
+    if len(result) == 0:
+	print "No Flows found"
+	return;
+
+    parsedResult = json.loads(result)
+    debug("parsed %s" % parsedResult)
+  except:
+    log_error("Controller IF has issue")
+    exit(1)
+
+  for flowPath in parsedResult:
+    print_flow_path(flowPath)
+
+
+def get_all_flow_paths():
+  try:
+    command = "curl -s \"http://%s:%s/wm/flow/getall/json\"" % (ControllerIP, ControllerPort)
+    debug("get_all_flow_paths %s" % command)
+
+    result = os.popen(command).read()
+    debug("result %s" % result)
+    if len(result) == 0:
+	print "No Flows found"
+	return;
+
+    parsedResult = json.loads(result)
+    debug("parsed %s" % parsedResult)
+  except:
+    log_error("Controller IF has issue")
+    exit(1)
+
+  for flowPath in parsedResult:
+    print_flow_path(flowPath)
 
 if __name__ == "__main__":
-  usage_msg = "Usage: %s <flow_id>" % (sys.argv[0])
+  usage_msg1 = "Usage:\n"
+  usage_msg2 = "%s <flow_id> : Print flow with Flow ID of <flow_id>\n" % (sys.argv[0])
+  usage_msg3 = "                   all    : Print all flows\n"
+  usage_msg4 = "                   installer <installer-id> <src-dpid> <src-port> <dest-dpid> <dest-port>\n"
+  usage_msg5 = "                   endpoints <src-dpid> <src-port> <dest-dpid> <dest-port>"
+  usage_msg = usage_msg1 + usage_msg2 + usage_msg3 + usage_msg4 + usage_msg5;
 
   # app.debug = False;
 
@@ -83,4 +154,19 @@
     exit(1)
 
   # Do the work
-  get_flow_path(sys.argv[1]);
+  if sys.argv[1] == "all":
+    get_all_flow_paths()
+  elif sys.argv[1] == "installer":
+    if len(sys.argv) < 7:
+      log_error(usage_msg)
+      exit(1)
+    get_installer_flow_paths(sys.argv[2], sys.argv[3], sys.argv[4],
+			     sys.argv[5], sys.argv[6])
+  elif sys.argv[1] == "endpoints":
+    if len(sys.argv) < 6:
+      log_error(usage_msg)
+      exit(1)
+    get_endpoints_flow_paths(sys.argv[2], sys.argv[3], sys.argv[4],
+			     sys.argv[5])
+  else:
+    get_flow_path(sys.argv[1])