blob: a9de51e25a38766284d676b58b5590d02f3b795e [file] [log] [blame]
alshabib9290eea2014-09-22 11:58:17 -07001package org.onlab.onos.cli.net;
2
alshabib99b8fdc2014-09-25 14:30:22 -07003import static com.google.common.collect.Lists.newArrayList;
alshabib9290eea2014-09-22 11:58:17 -07004
tom1380eee2014-09-24 09:22:02 -07005import java.util.Collections;
6import java.util.List;
7import java.util.Map;
8
alshabib99b8fdc2014-09-25 14:30:22 -07009import org.apache.karaf.shell.commands.Argument;
10import org.apache.karaf.shell.commands.Command;
11import org.onlab.onos.cli.AbstractShellCommand;
12import org.onlab.onos.net.Device;
13import org.onlab.onos.net.DeviceId;
14import org.onlab.onos.net.device.DeviceService;
15import org.onlab.onos.net.flow.FlowRule;
alshabib99b8fdc2014-09-25 14:30:22 -070016import org.onlab.onos.net.flow.FlowRuleService;
17
18import com.google.common.collect.Maps;
alshabib9290eea2014-09-22 11:58:17 -070019
20/**
21 * Lists all currently-known hosts.
22 */
23@Command(scope = "onos", name = "flows",
24description = "Lists all currently-known flows.")
25public class FlowsListCommand extends AbstractShellCommand {
26
27 private static final String FMT =
alshabib99b8fdc2014-09-25 14:30:22 -070028 " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s";
29 private static final String TFMT = " treatment=%s";
30 private static final String SFMT = " selector=%s";
31
alshabib25182652014-09-25 14:34:17 -070032 @Argument(index = 0, name = "uri", description = "Device ID",
alshabib99b8fdc2014-09-25 14:30:22 -070033 required = false, multiValued = false)
34 String uri = null;
alshabib9290eea2014-09-22 11:58:17 -070035
alshabib64231d82014-09-25 18:25:31 -070036
alshabib9290eea2014-09-22 11:58:17 -070037 @Override
tom0872a172014-09-23 11:24:26 -070038 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070039 DeviceService deviceService = get(DeviceService.class);
40 FlowRuleService service = get(FlowRuleService.class);
alshabib9290eea2014-09-22 11:58:17 -070041 Map<Device, List<FlowRule>> flows = getSortedFlows(deviceService, service);
alshabib99b8fdc2014-09-25 14:30:22 -070042 for (Device d : flows.keySet()) {
alshabib9290eea2014-09-22 11:58:17 -070043 printFlows(d, flows.get(d));
44 }
alshabib9290eea2014-09-22 11:58:17 -070045 }
46
alshabib9290eea2014-09-22 11:58:17 -070047 /**
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();
alshabib99b8fdc2014-09-25 14:30:22 -070055 List<FlowRule> rules = newArrayList();
56 Iterable<Device> devices = uri == null ? deviceService.getDevices() :
57 Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri)));
58 for (Device d : devices) {
alshabib9290eea2014-09-22 11:58:17 -070059 rules = newArrayList(service.getFlowEntries(d.id()));
tom1380eee2014-09-24 09:22:02 -070060 Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR);
alshabib9290eea2014-09-22 11:58:17 -070061 flows.put(d, rules);
62 }
63 return flows;
64 }
65
66 /**
67 * Prints flows.
68 * @param d the device
69 * @param flows the set of flows for that device.
70 */
71 protected void printFlows(Device d, List<FlowRule> flows) {
72 print("Device: " + d.id());
alshabib99b8fdc2014-09-25 14:30:22 -070073 if (flows == null | flows.isEmpty()) {
74 print(" %s", "No flows installed.");
75 return;
76 }
alshabib9290eea2014-09-22 11:58:17 -070077 for (FlowRule f : flows) {
alshabib99b8fdc2014-09-25 14:30:22 -070078 print(FMT, Long.toHexString(f.id().value()), f.state(), f.bytes(),
79 f.packets(), f.lifeMillis(), f.priority());
80 print(SFMT, f.selector().criteria());
81 print(TFMT, f.treatment().instructions());
alshabib9290eea2014-09-22 11:58:17 -070082 }
83
84 }
85
86}