blob: cecd848bf9f2289ce5a383085264586679aa06ec [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;
alshabib144a2942014-09-25 18:44:02 -070016import org.onlab.onos.net.flow.FlowRule.FlowRuleState;
alshabib99b8fdc2014-09-25 14:30:22 -070017import 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
alshabib144a2942014-09-25 18:44:02 -070028 public static final String ANY = "any";
29
alshabib9290eea2014-09-22 11:58:17 -070030 private static final String FMT =
alshabib99b8fdc2014-09-25 14:30:22 -070031 " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s";
32 private static final String TFMT = " treatment=%s";
33 private static final String SFMT = " selector=%s";
34
alshabib144a2942014-09-25 18:44:02 -070035 @Argument(index = 1, name = "uri", description = "Device ID",
alshabib99b8fdc2014-09-25 14:30:22 -070036 required = false, multiValued = false)
37 String uri = null;
alshabib9290eea2014-09-22 11:58:17 -070038
alshabib144a2942014-09-25 18:44:02 -070039 @Argument(index = 0, name = "state", description = "Flow Rule state",
40 required = false, multiValued = false)
41 String state = null;
alshabib64231d82014-09-25 18:25:31 -070042
alshabib9290eea2014-09-22 11:58:17 -070043 @Override
tom0872a172014-09-23 11:24:26 -070044 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070045 DeviceService deviceService = get(DeviceService.class);
46 FlowRuleService service = get(FlowRuleService.class);
alshabib9290eea2014-09-22 11:58:17 -070047 Map<Device, List<FlowRule>> flows = getSortedFlows(deviceService, service);
alshabib99b8fdc2014-09-25 14:30:22 -070048 for (Device d : flows.keySet()) {
alshabib9290eea2014-09-22 11:58:17 -070049 printFlows(d, flows.get(d));
50 }
alshabib9290eea2014-09-22 11:58:17 -070051 }
52
alshabib9290eea2014-09-22 11:58:17 -070053 /**
54 * Returns the list of devices sorted using the device ID URIs.
55 *
56 * @param service device service
57 * @return sorted device list
58 */
59 protected Map<Device, List<FlowRule>> getSortedFlows(DeviceService deviceService, FlowRuleService service) {
60 Map<Device, List<FlowRule>> flows = Maps.newHashMap();
alshabib144a2942014-09-25 18:44:02 -070061 List<FlowRule> rules;
62 FlowRuleState s = null;
63 if (state != null && !state.equals("any")) {
64 s = FlowRuleState.valueOf(state.toUpperCase());
65 }
alshabib99b8fdc2014-09-25 14:30:22 -070066 Iterable<Device> devices = uri == null ? deviceService.getDevices() :
67 Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri)));
68 for (Device d : devices) {
alshabib144a2942014-09-25 18:44:02 -070069 if (s == null) {
70 rules = newArrayList(service.getFlowEntries(d.id()));
71 } else {
72 rules = newArrayList();
73 for (FlowRule f : service.getFlowEntries(d.id())) {
74 if (f.state().equals(s)) {
75 rules.add(f);
76 }
77 }
78 }
tom1380eee2014-09-24 09:22:02 -070079 Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR);
alshabib9290eea2014-09-22 11:58:17 -070080 flows.put(d, rules);
81 }
82 return flows;
83 }
84
85 /**
86 * Prints flows.
87 * @param d the device
88 * @param flows the set of flows for that device.
89 */
90 protected void printFlows(Device d, List<FlowRule> flows) {
91 print("Device: " + d.id());
alshabib99b8fdc2014-09-25 14:30:22 -070092 if (flows == null | flows.isEmpty()) {
alshabib144a2942014-09-25 18:44:02 -070093 print(" %s", "No flows.");
alshabib99b8fdc2014-09-25 14:30:22 -070094 return;
95 }
alshabib9290eea2014-09-22 11:58:17 -070096 for (FlowRule f : flows) {
alshabib99b8fdc2014-09-25 14:30:22 -070097 print(FMT, Long.toHexString(f.id().value()), f.state(), f.bytes(),
98 f.packets(), f.lifeMillis(), f.priority());
99 print(SFMT, f.selector().criteria());
100 print(TFMT, f.treatment().instructions());
alshabib9290eea2014-09-22 11:58:17 -0700101 }
102
103 }
104
105}