Fixes NPE in CLI when user enters non-existent DPID in "flows any <dpid>" command.
Sorts flows according to table-id first, and then flow-id, so all flows
from the same table print together.

Change-Id: I4a811a00a1dc0e1e2f2855c06f5f7f9851152c0d
diff --git a/cli/src/main/java/org/onosproject/cli/Comparators.java b/cli/src/main/java/org/onosproject/cli/Comparators.java
index 1df2f04..03d25ce 100644
--- a/cli/src/main/java/org/onosproject/cli/Comparators.java
+++ b/cli/src/main/java/org/onosproject/cli/Comparators.java
@@ -71,7 +71,10 @@
     public static final Comparator<FlowRule> FLOW_RULE_COMPARATOR = new Comparator<FlowRule>() {
         @Override
         public int compare(FlowRule f1, FlowRule f2) {
-            return Long.valueOf(f1.id().value()).compareTo(f2.id().value());
+            int tableCompare = Integer.valueOf(f1.tableId()).compareTo(f2.tableId());
+            return (tableCompare == 0)
+                    ? Long.valueOf(f1.id().value()).compareTo(f2.id().value())
+                    : tableCompare;
         }
     };
 
diff --git a/cli/src/main/java/org/onosproject/cli/net/FlowsListCommand.java b/cli/src/main/java/org/onosproject/cli/net/FlowsListCommand.java
index de84f51..331cca1 100644
--- a/cli/src/main/java/org/onosproject/cli/net/FlowsListCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/FlowsListCommand.java
@@ -123,8 +123,14 @@
         if (state != null && !state.equals("any")) {
             s = FlowEntryState.valueOf(state.toUpperCase());
         }
-        Iterable<Device> devices = uri == null ? deviceService.getDevices() :
-                Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri)));
+        Iterable<Device> devices = null;
+        if (uri == null) {
+            devices = deviceService.getDevices();
+        } else {
+            Device dev = deviceService.getDevice(DeviceId.deviceId(uri));
+            devices = (dev == null) ? deviceService.getDevices()
+                                    : Collections.singletonList(dev);
+        }
         for (Device d : devices) {
             if (s == null) {
                 rules = newArrayList(service.getFlowEntries(d.id()));