blob: 07d15e2031579a7b90a2dbc992e6f44ff9d61043 [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
37 protected Object doExecute() throws Exception {
38 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 }
44 return null;
45 }
46
47
48 /**
49 * Returns the list of devices sorted using the device ID URIs.
50 *
51 * @param service device service
52 * @return sorted device list
53 */
54 protected Map<Device, List<FlowRule>> getSortedFlows(DeviceService deviceService, FlowRuleService service) {
55 Map<Device, List<FlowRule>> flows = Maps.newHashMap();
56 List<FlowRule> rules;
57 for (Device d : deviceService.getDevices()) {
58 rules = newArrayList(service.getFlowEntries(d.id()));
59 Collections.sort(rules, ID_COMPARATOR);
60 flows.put(d, rules);
61 }
62 return flows;
63 }
64
65 /**
66 * Prints flows.
67 * @param d the device
68 * @param flows the set of flows for that device.
69 */
70 protected void printFlows(Device d, List<FlowRule> flows) {
71 print("Device: " + d.id());
72 for (FlowRule f : flows) {
73 print(FMT, f.id().value(), f.selector(), f.treatment(), f.state());
74 }
75
76 }
77
78}