flow cmd has state selector

Change-Id: Id778277979d538e4addf8ed0052066a6d2e67ac6
diff --git a/cli/src/main/java/org/onlab/onos/cli/net/FlowRuleStatusCompleter.java b/cli/src/main/java/org/onlab/onos/cli/net/FlowRuleStatusCompleter.java
new file mode 100644
index 0000000..134d995
--- /dev/null
+++ b/cli/src/main/java/org/onlab/onos/cli/net/FlowRuleStatusCompleter.java
@@ -0,0 +1,30 @@
+package org.onlab.onos.cli.net;
+
+import java.util.List;
+import java.util.SortedSet;
+
+import org.apache.karaf.shell.console.Completer;
+import org.apache.karaf.shell.console.completer.StringsCompleter;
+import org.onlab.onos.net.flow.FlowRule.FlowRuleState;
+
+/**
+ * Device ID completer.
+ */
+public class FlowRuleStatusCompleter implements Completer {
+    @Override
+    public int complete(String buffer, int cursor, List<String> candidates) {
+        // Delegate string completer
+        StringsCompleter delegate = new StringsCompleter();
+
+        FlowRuleState[] states = FlowRuleState.values();
+        SortedSet<String> strings = delegate.getStrings();
+        for (int i = 0; i < states.length; i++) {
+            strings.add(states[i].toString().toLowerCase());
+        }
+        strings.add(FlowsListCommand.ANY);
+
+        // Now let the completer do the work for figuring out what to offer.
+        return delegate.complete(buffer, cursor, candidates);
+    }
+
+}
diff --git a/cli/src/main/java/org/onlab/onos/cli/net/FlowsListCommand.java b/cli/src/main/java/org/onlab/onos/cli/net/FlowsListCommand.java
index a9de51e..cecd848 100644
--- a/cli/src/main/java/org/onlab/onos/cli/net/FlowsListCommand.java
+++ b/cli/src/main/java/org/onlab/onos/cli/net/FlowsListCommand.java
@@ -13,6 +13,7 @@
 import org.onlab.onos.net.DeviceId;
 import org.onlab.onos.net.device.DeviceService;
 import org.onlab.onos.net.flow.FlowRule;
+import org.onlab.onos.net.flow.FlowRule.FlowRuleState;
 import org.onlab.onos.net.flow.FlowRuleService;
 
 import com.google.common.collect.Maps;
@@ -24,15 +25,20 @@
 description = "Lists all currently-known flows.")
 public class FlowsListCommand extends AbstractShellCommand {
 
+    public static final String ANY = "any";
+
     private static final String FMT =
             "   id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s";
     private static final String TFMT = "      treatment=%s";
     private static final String SFMT = "      selector=%s";
 
-    @Argument(index = 0, name = "uri", description = "Device ID",
+    @Argument(index = 1, name = "uri", description = "Device ID",
             required = false, multiValued = false)
     String uri = null;
 
+    @Argument(index = 0, name = "state", description = "Flow Rule state",
+            required = false, multiValued = false)
+    String state = null;
 
     @Override
     protected void execute() {
@@ -52,11 +58,24 @@
      */
     protected Map<Device, List<FlowRule>> getSortedFlows(DeviceService deviceService, FlowRuleService service) {
         Map<Device, List<FlowRule>> flows = Maps.newHashMap();
-        List<FlowRule> rules = newArrayList();
+        List<FlowRule> rules;
+        FlowRuleState s = null;
+        if (state != null && !state.equals("any")) {
+            s = FlowRuleState.valueOf(state.toUpperCase());
+        }
         Iterable<Device> devices = uri == null ?  deviceService.getDevices() :
             Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri)));
         for (Device d : devices) {
-            rules = newArrayList(service.getFlowEntries(d.id()));
+            if (s == null) {
+                rules = newArrayList(service.getFlowEntries(d.id()));
+            } else {
+                rules = newArrayList();
+                for (FlowRule f : service.getFlowEntries(d.id())) {
+                    if (f.state().equals(s)) {
+                        rules.add(f);
+                    }
+                }
+            }
             Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR);
             flows.put(d, rules);
         }
@@ -71,7 +90,7 @@
     protected void printFlows(Device d, List<FlowRule> flows) {
         print("Device: " + d.id());
         if (flows == null | flows.isEmpty()) {
-            print(" %s", "No flows installed.");
+            print(" %s", "No flows.");
             return;
         }
         for (FlowRule f : flows) {
diff --git a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 6b867b3..30fce6f 100644
--- a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -73,6 +73,7 @@
         <command>
             <action class="org.onlab.onos.cli.net.FlowsListCommand"/>
             <completers>
+                <ref component-id="flowRuleStatusCompleter"/>
                 <ref component-id="deviceIdCompleter"/>
             </completers>
         </command>
@@ -87,5 +88,6 @@
     <bean id="clusterIdCompleter" class="org.onlab.onos.cli.net.ClusterIdCompleter"/>
     <bean id="roleCompleter" class="org.onlab.onos.cli.net.RoleCompleter"/>
     <bean id="hostIdCompleter" class="org.onlab.onos.cli.net.HostIdCompleter"/>
+    <bean id="flowRuleStatusCompleter" class="org.onlab.onos.cli.net.FlowRuleStatusCompleter"/>
 
 </blueprint>