blob: 9e21642998276e835923db2dd643f37491abc8ef [file] [log] [blame]
alshabib9290eea2014-09-22 11:58:17 -07001package org.onlab.onos.cli.net;
2
3import static com.google.common.collect.Lists.newArrayList;
4
5import java.util.Collections;
6import java.util.Comparator;
7import java.util.List;
8import java.util.Map;
9
10import org.apache.karaf.shell.commands.Command;
11import org.onlab.onos.cli.AbstractShellCommand;
12import org.onlab.onos.net.Device;
13import org.onlab.onos.net.device.DeviceService;
14import org.onlab.onos.net.flow.FlowRule;
15import org.onlab.onos.net.flow.FlowRuleService;
16
17import com.google.common.collect.Maps;
18
19/**
20 * Lists all currently-known hosts.
21 */
22@Command(scope = "onos", name = "flows",
23description = "Lists all currently-known flows.")
24public class FlowsListCommand extends AbstractShellCommand {
25
26 private static final String FMT =
27 " id=%s, selector=%s, treatment=%s, state=%s";
28
29 protected static final Comparator<FlowRule> ID_COMPARATOR = new Comparator<FlowRule>() {
30 @Override
31 public int compare(FlowRule f1, FlowRule f2) {
32 return Long.valueOf(f1.id().value()).compareTo(f2.id().value());
33 }
34 };
35
36 @Override
tom0872a172014-09-23 11:24:26 -070037 protected void execute() {
alshabib9290eea2014-09-22 11:58:17 -070038 DeviceService deviceService = getService(DeviceService.class);
39 FlowRuleService service = getService(FlowRuleService.class);
40 Map<Device, List<FlowRule>> flows = getSortedFlows(deviceService, service);
41 for (Device d : deviceService.getDevices()) {
42 printFlows(d, flows.get(d));
43 }
alshabib9290eea2014-09-22 11:58:17 -070044 }
45
46
47 /**
48 * Returns the list of devices sorted using the device ID URIs.
49 *
50 * @param service device service
51 * @return sorted device list
52 */
53 protected Map<Device, List<FlowRule>> getSortedFlows(DeviceService deviceService, FlowRuleService service) {
54 Map<Device, List<FlowRule>> flows = Maps.newHashMap();
55 List<FlowRule> rules;
56 for (Device d : deviceService.getDevices()) {
57 rules = newArrayList(service.getFlowEntries(d.id()));
58 Collections.sort(rules, ID_COMPARATOR);
59 flows.put(d, rules);
60 }
61 return flows;
62 }
63
64 /**
65 * Prints flows.
66 * @param d the device
67 * @param flows the set of flows for that device.
68 */
69 protected void printFlows(Device d, List<FlowRule> flows) {
70 print("Device: " + d.id());
71 for (FlowRule f : flows) {
72 print(FMT, f.id().value(), f.selector(), f.treatment(), f.state());
73 }
74
75 }
76
77}