tom

Merge remote-tracking branch 'origin/master'

1 +package org.onlab.onos.cli.net;
2 +
3 +import java.util.List;
4 +import java.util.SortedSet;
5 +
6 +import org.apache.karaf.shell.console.Completer;
7 +import org.apache.karaf.shell.console.completer.StringsCompleter;
8 +import org.onlab.onos.net.flow.FlowRule.FlowRuleState;
9 +
10 +/**
11 + * Device ID completer.
12 + */
13 +public class FlowRuleStatusCompleter implements Completer {
14 + @Override
15 + public int complete(String buffer, int cursor, List<String> candidates) {
16 + // Delegate string completer
17 + StringsCompleter delegate = new StringsCompleter();
18 +
19 + FlowRuleState[] states = FlowRuleState.values();
20 + SortedSet<String> strings = delegate.getStrings();
21 + for (int i = 0; i < states.length; i++) {
22 + strings.add(states[i].toString().toLowerCase());
23 + }
24 + strings.add(FlowsListCommand.ANY);
25 +
26 + // Now let the completer do the work for figuring out what to offer.
27 + return delegate.complete(buffer, cursor, candidates);
28 + }
29 +
30 +}
...@@ -13,6 +13,7 @@ import org.onlab.onos.net.Device; ...@@ -13,6 +13,7 @@ import org.onlab.onos.net.Device;
13 import org.onlab.onos.net.DeviceId; 13 import org.onlab.onos.net.DeviceId;
14 import org.onlab.onos.net.device.DeviceService; 14 import org.onlab.onos.net.device.DeviceService;
15 import org.onlab.onos.net.flow.FlowRule; 15 import org.onlab.onos.net.flow.FlowRule;
16 +import org.onlab.onos.net.flow.FlowRule.FlowRuleState;
16 import org.onlab.onos.net.flow.FlowRuleService; 17 import org.onlab.onos.net.flow.FlowRuleService;
17 18
18 import com.google.common.collect.Maps; 19 import com.google.common.collect.Maps;
...@@ -24,15 +25,20 @@ import com.google.common.collect.Maps; ...@@ -24,15 +25,20 @@ import com.google.common.collect.Maps;
24 description = "Lists all currently-known flows.") 25 description = "Lists all currently-known flows.")
25 public class FlowsListCommand extends AbstractShellCommand { 26 public class FlowsListCommand extends AbstractShellCommand {
26 27
28 + public static final String ANY = "any";
29 +
27 private static final String FMT = 30 private static final String FMT =
28 " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s"; 31 " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s";
29 private static final String TFMT = " treatment=%s"; 32 private static final String TFMT = " treatment=%s";
30 private static final String SFMT = " selector=%s"; 33 private static final String SFMT = " selector=%s";
31 34
32 - @Argument(index = 0, name = "uri", description = "Device ID", 35 + @Argument(index = 1, name = "uri", description = "Device ID",
33 required = false, multiValued = false) 36 required = false, multiValued = false)
34 String uri = null; 37 String uri = null;
35 38
39 + @Argument(index = 0, name = "state", description = "Flow Rule state",
40 + required = false, multiValued = false)
41 + String state = null;
36 42
37 @Override 43 @Override
38 protected void execute() { 44 protected void execute() {
...@@ -52,11 +58,24 @@ public class FlowsListCommand extends AbstractShellCommand { ...@@ -52,11 +58,24 @@ public class FlowsListCommand extends AbstractShellCommand {
52 */ 58 */
53 protected Map<Device, List<FlowRule>> getSortedFlows(DeviceService deviceService, FlowRuleService service) { 59 protected Map<Device, List<FlowRule>> getSortedFlows(DeviceService deviceService, FlowRuleService service) {
54 Map<Device, List<FlowRule>> flows = Maps.newHashMap(); 60 Map<Device, List<FlowRule>> flows = Maps.newHashMap();
55 - List<FlowRule> rules = newArrayList(); 61 + List<FlowRule> rules;
62 + FlowRuleState s = null;
63 + if (state != null && !state.equals("any")) {
64 + s = FlowRuleState.valueOf(state.toUpperCase());
65 + }
56 Iterable<Device> devices = uri == null ? deviceService.getDevices() : 66 Iterable<Device> devices = uri == null ? deviceService.getDevices() :
57 Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri))); 67 Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri)));
58 for (Device d : devices) { 68 for (Device d : devices) {
59 - rules = newArrayList(service.getFlowEntries(d.id())); 69 + if (s == null) {
70 + rules = newArrayList(service.getFlowEntries(d.id()));
71 + } else {
72 + rules = newArrayList();
73 + for (FlowRule f : service.getFlowEntries(d.id())) {
74 + if (f.state().equals(s)) {
75 + rules.add(f);
76 + }
77 + }
78 + }
60 Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR); 79 Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR);
61 flows.put(d, rules); 80 flows.put(d, rules);
62 } 81 }
...@@ -71,7 +90,7 @@ public class FlowsListCommand extends AbstractShellCommand { ...@@ -71,7 +90,7 @@ public class FlowsListCommand extends AbstractShellCommand {
71 protected void printFlows(Device d, List<FlowRule> flows) { 90 protected void printFlows(Device d, List<FlowRule> flows) {
72 print("Device: " + d.id()); 91 print("Device: " + d.id());
73 if (flows == null | flows.isEmpty()) { 92 if (flows == null | flows.isEmpty()) {
74 - print(" %s", "No flows installed."); 93 + print(" %s", "No flows.");
75 return; 94 return;
76 } 95 }
77 for (FlowRule f : flows) { 96 for (FlowRule f : flows) {
......
...@@ -73,6 +73,7 @@ ...@@ -73,6 +73,7 @@
73 <command> 73 <command>
74 <action class="org.onlab.onos.cli.net.FlowsListCommand"/> 74 <action class="org.onlab.onos.cli.net.FlowsListCommand"/>
75 <completers> 75 <completers>
76 + <ref component-id="flowRuleStatusCompleter"/>
76 <ref component-id="deviceIdCompleter"/> 77 <ref component-id="deviceIdCompleter"/>
77 </completers> 78 </completers>
78 </command> 79 </command>
...@@ -87,5 +88,6 @@ ...@@ -87,5 +88,6 @@
87 <bean id="clusterIdCompleter" class="org.onlab.onos.cli.net.ClusterIdCompleter"/> 88 <bean id="clusterIdCompleter" class="org.onlab.onos.cli.net.ClusterIdCompleter"/>
88 <bean id="roleCompleter" class="org.onlab.onos.cli.net.RoleCompleter"/> 89 <bean id="roleCompleter" class="org.onlab.onos.cli.net.RoleCompleter"/>
89 <bean id="hostIdCompleter" class="org.onlab.onos.cli.net.HostIdCompleter"/> 90 <bean id="hostIdCompleter" class="org.onlab.onos.cli.net.HostIdCompleter"/>
91 + <bean id="flowRuleStatusCompleter" class="org.onlab.onos.cli.net.FlowRuleStatusCompleter"/>
90 92
91 </blueprint> 93 </blueprint>
......