blob: 902b27be72fc240a7636fee1bbb72f703c852bf5 [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;
tom53945d52014-10-07 11:01:36 -07004import static org.onlab.onos.cli.net.DevicesListCommand.getSortedDevices;
alshabib9290eea2014-09-22 11:58:17 -07005
tom1380eee2014-09-24 09:22:02 -07006import java.util.Collections;
7import java.util.List;
8import java.util.Map;
9
alshabib99b8fdc2014-09-25 14:30:22 -070010import org.apache.karaf.shell.commands.Argument;
11import org.apache.karaf.shell.commands.Command;
12import org.onlab.onos.cli.AbstractShellCommand;
tom91c7bd02014-09-25 22:50:44 -070013import org.onlab.onos.cli.Comparators;
alshabib99b8fdc2014-09-25 14:30:22 -070014import org.onlab.onos.net.Device;
15import org.onlab.onos.net.DeviceId;
16import org.onlab.onos.net.device.DeviceService;
alshabib1c319ff2014-10-04 20:29:09 -070017import org.onlab.onos.net.flow.FlowEntry;
18import org.onlab.onos.net.flow.FlowEntry.FlowEntryState;
alshabib99b8fdc2014-09-25 14:30:22 -070019import org.onlab.onos.net.flow.FlowRuleService;
20
21import com.google.common.collect.Maps;
alshabib9290eea2014-09-22 11:58:17 -070022
23/**
24 * Lists all currently-known hosts.
25 */
26@Command(scope = "onos", name = "flows",
27description = "Lists all currently-known flows.")
28public class FlowsListCommand extends AbstractShellCommand {
29
alshabib144a2942014-09-25 18:44:02 -070030 public static final String ANY = "any";
31
alshabib9290eea2014-09-22 11:58:17 -070032 private static final String FMT =
alshabib99b8fdc2014-09-25 14:30:22 -070033 " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s";
34 private static final String TFMT = " treatment=%s";
35 private static final String SFMT = " selector=%s";
36
alshabib144a2942014-09-25 18:44:02 -070037 @Argument(index = 1, name = "uri", description = "Device ID",
alshabib99b8fdc2014-09-25 14:30:22 -070038 required = false, multiValued = false)
39 String uri = null;
alshabib9290eea2014-09-22 11:58:17 -070040
alshabib144a2942014-09-25 18:44:02 -070041 @Argument(index = 0, name = "state", description = "Flow Rule state",
42 required = false, multiValued = false)
43 String state = null;
alshabib64231d82014-09-25 18:25:31 -070044
alshabib9290eea2014-09-22 11:58:17 -070045 @Override
tom0872a172014-09-23 11:24:26 -070046 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070047 DeviceService deviceService = get(DeviceService.class);
48 FlowRuleService service = get(FlowRuleService.class);
alshabib1c319ff2014-10-04 20:29:09 -070049 Map<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service);
tom1dd08e42014-10-07 11:40:00 -070050 for (Device d : getSortedDevices(deviceService)) {
alshabib9290eea2014-09-22 11:58:17 -070051 printFlows(d, flows.get(d));
52 }
alshabib9290eea2014-09-22 11:58:17 -070053 }
54
alshabib9290eea2014-09-22 11:58:17 -070055 /**
56 * Returns the list of devices sorted using the device ID URIs.
57 *
58 * @param service device service
59 * @return sorted device list
60 */
tom1dd08e42014-10-07 11:40:00 -070061 protected Map<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService,
62 FlowRuleService service) {
alshabib1c319ff2014-10-04 20:29:09 -070063 Map<Device, List<FlowEntry>> flows = Maps.newHashMap();
64 List<FlowEntry> rules;
65 FlowEntryState s = null;
alshabib144a2942014-09-25 18:44:02 -070066 if (state != null && !state.equals("any")) {
alshabib1c319ff2014-10-04 20:29:09 -070067 s = FlowEntryState.valueOf(state.toUpperCase());
alshabib144a2942014-09-25 18:44:02 -070068 }
tom1dd08e42014-10-07 11:40:00 -070069 Iterable<Device> devices = uri == null ? deviceService.getDevices() :
alshabib99b8fdc2014-09-25 14:30:22 -070070 Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri)));
71 for (Device d : devices) {
alshabib144a2942014-09-25 18:44:02 -070072 if (s == null) {
73 rules = newArrayList(service.getFlowEntries(d.id()));
74 } else {
75 rules = newArrayList();
alshabib1c319ff2014-10-04 20:29:09 -070076 for (FlowEntry f : service.getFlowEntries(d.id())) {
alshabib144a2942014-09-25 18:44:02 -070077 if (f.state().equals(s)) {
78 rules.add(f);
79 }
80 }
81 }
tom1380eee2014-09-24 09:22:02 -070082 Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR);
alshabib9290eea2014-09-22 11:58:17 -070083 flows.put(d, rules);
84 }
85 return flows;
86 }
87
88 /**
89 * Prints flows.
90 * @param d the device
91 * @param flows the set of flows for that device.
92 */
alshabib1c319ff2014-10-04 20:29:09 -070093 protected void printFlows(Device d, List<FlowEntry> flows) {
tom1dd08e42014-10-07 11:40:00 -070094 boolean empty = flows == null || flows.isEmpty();
95 print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : flows.size());
96 if (!empty) {
97 for (FlowEntry f : flows) {
98 print(FMT, Long.toHexString(f.id().value()), f.state(), f.bytes(),
99 f.packets(), f.life(), f.priority());
100 print(SFMT, f.selector().criteria());
101 print(TFMT, f.treatment().instructions());
102 }
alshabib99b8fdc2014-09-25 14:30:22 -0700103 }
alshabib9290eea2014-09-22 11:58:17 -0700104 }
105
Yuta HIGUCHIe76a24d2014-09-27 00:48:34 -0700106}