tom

Merge remote-tracking branch 'origin/master'

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);
}
}
......@@ -13,6 +13,7 @@ import org.onlab.onos.net.Device;
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 @@ import com.google.common.collect.Maps;
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 @@ public class FlowsListCommand extends AbstractShellCommand {
*/
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) {
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 @@ public class FlowsListCommand extends AbstractShellCommand {
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) {
......
......@@ -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>
......