blob: f451582b52b0fd77809169293f270ad39eadcc79 [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;
16import org.onlab.onos.net.flow.FlowRule.FlowRuleState;
17import org.onlab.onos.net.flow.FlowRuleService;
18
19import com.google.common.collect.Maps;
alshabib9290eea2014-09-22 11:58:17 -070020
21/**
22 * Lists all currently-known hosts.
23 */
24@Command(scope = "onos", name = "flows",
25description = "Lists all currently-known flows.")
26public class FlowsListCommand extends AbstractShellCommand {
27
28 private static final String FMT =
alshabib99b8fdc2014-09-25 14:30:22 -070029 " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s";
30 private static final String TFMT = " treatment=%s";
31 private static final String SFMT = " selector=%s";
32
33 @Argument(index = 0, name = "state", description = "Flow rule state",
34 required = false, multiValued = false)
35 FlowRuleState state = null;
36
37 @Argument(index = 1, name = "uri", description = "Device ID",
38 required = false, multiValued = false)
39 String uri = null;
alshabib9290eea2014-09-22 11:58:17 -070040
alshabib9290eea2014-09-22 11:58:17 -070041 @Override
tom0872a172014-09-23 11:24:26 -070042 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070043 DeviceService deviceService = get(DeviceService.class);
44 FlowRuleService service = get(FlowRuleService.class);
alshabib9290eea2014-09-22 11:58:17 -070045 Map<Device, List<FlowRule>> flows = getSortedFlows(deviceService, service);
alshabib99b8fdc2014-09-25 14:30:22 -070046 for (Device d : flows.keySet()) {
alshabib9290eea2014-09-22 11:58:17 -070047 printFlows(d, flows.get(d));
48 }
alshabib9290eea2014-09-22 11:58:17 -070049 }
50
alshabib9290eea2014-09-22 11:58:17 -070051 /**
52 * Returns the list of devices sorted using the device ID URIs.
53 *
54 * @param service device service
55 * @return sorted device list
56 */
57 protected Map<Device, List<FlowRule>> getSortedFlows(DeviceService deviceService, FlowRuleService service) {
58 Map<Device, List<FlowRule>> flows = Maps.newHashMap();
alshabib99b8fdc2014-09-25 14:30:22 -070059 List<FlowRule> rules = newArrayList();
60 Iterable<Device> devices = uri == null ? deviceService.getDevices() :
61 Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri)));
62 for (Device d : devices) {
alshabib9290eea2014-09-22 11:58:17 -070063 rules = newArrayList(service.getFlowEntries(d.id()));
tom1380eee2014-09-24 09:22:02 -070064 Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR);
alshabib9290eea2014-09-22 11:58:17 -070065 flows.put(d, rules);
66 }
67 return flows;
68 }
69
70 /**
71 * Prints flows.
72 * @param d the device
73 * @param flows the set of flows for that device.
74 */
75 protected void printFlows(Device d, List<FlowRule> flows) {
76 print("Device: " + d.id());
alshabib99b8fdc2014-09-25 14:30:22 -070077 if (flows == null | flows.isEmpty()) {
78 print(" %s", "No flows installed.");
79 return;
80 }
alshabib9290eea2014-09-22 11:58:17 -070081 for (FlowRule f : flows) {
alshabib99b8fdc2014-09-25 14:30:22 -070082 print(FMT, Long.toHexString(f.id().value()), f.state(), f.bytes(),
83 f.packets(), f.lifeMillis(), f.priority());
84 print(SFMT, f.selector().criteria());
85 print(TFMT, f.treatment().instructions());
alshabib9290eea2014-09-22 11:58:17 -070086 }
87
88 }
89
90}