blob: da43255681ad8c4043e4a5b698ffa8b9faf76dad [file] [log] [blame]
alshabib9290eea2014-09-22 11:58:17 -07001package org.onlab.onos.cli.net;
2
tom1380eee2014-09-24 09:22:02 -07003import com.google.common.collect.Maps;
alshabib9290eea2014-09-22 11:58:17 -07004import org.apache.karaf.shell.commands.Command;
5import org.onlab.onos.cli.AbstractShellCommand;
6import org.onlab.onos.net.Device;
7import org.onlab.onos.net.device.DeviceService;
8import org.onlab.onos.net.flow.FlowRule;
9import org.onlab.onos.net.flow.FlowRuleService;
10
tom1380eee2014-09-24 09:22:02 -070011import java.util.Collections;
12import java.util.List;
13import java.util.Map;
14
15import static com.google.common.collect.Lists.newArrayList;
alshabib9290eea2014-09-22 11:58:17 -070016
17/**
18 * Lists all currently-known hosts.
19 */
20@Command(scope = "onos", name = "flows",
21description = "Lists all currently-known flows.")
22public class FlowsListCommand extends AbstractShellCommand {
23
24 private static final String FMT =
25 " id=%s, selector=%s, treatment=%s, state=%s";
26
alshabib9290eea2014-09-22 11:58:17 -070027 @Override
tom0872a172014-09-23 11:24:26 -070028 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070029 DeviceService deviceService = get(DeviceService.class);
30 FlowRuleService service = get(FlowRuleService.class);
alshabib9290eea2014-09-22 11:58:17 -070031 Map<Device, List<FlowRule>> flows = getSortedFlows(deviceService, service);
32 for (Device d : deviceService.getDevices()) {
33 printFlows(d, flows.get(d));
34 }
alshabib9290eea2014-09-22 11:58:17 -070035 }
36
alshabib9290eea2014-09-22 11:58:17 -070037 /**
38 * Returns the list of devices sorted using the device ID URIs.
39 *
40 * @param service device service
41 * @return sorted device list
42 */
43 protected Map<Device, List<FlowRule>> getSortedFlows(DeviceService deviceService, FlowRuleService service) {
44 Map<Device, List<FlowRule>> flows = Maps.newHashMap();
45 List<FlowRule> rules;
46 for (Device d : deviceService.getDevices()) {
47 rules = newArrayList(service.getFlowEntries(d.id()));
tom1380eee2014-09-24 09:22:02 -070048 Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR);
alshabib9290eea2014-09-22 11:58:17 -070049 flows.put(d, rules);
50 }
51 return flows;
52 }
53
54 /**
55 * Prints flows.
56 * @param d the device
57 * @param flows the set of flows for that device.
58 */
59 protected void printFlows(Device d, List<FlowRule> flows) {
60 print("Device: " + d.id());
61 for (FlowRule f : flows) {
62 print(FMT, f.id().value(), f.selector(), f.treatment(), f.state());
63 }
64
65 }
66
67}