blob: 28309c5934e84853bd8b090a26859a0ef53727a8 [file] [log] [blame]
alshabib9290eea2014-09-22 11:58:17 -07001package org.onlab.onos.cli.net;
2
toma6897792014-10-08 22:21:05 -07003import com.google.common.collect.Maps;
alshabib99b8fdc2014-09-25 14:30:22 -07004import org.apache.karaf.shell.commands.Argument;
5import org.apache.karaf.shell.commands.Command;
toma6897792014-10-08 22:21:05 -07006import org.onlab.onos.CoreService;
alshabib99b8fdc2014-09-25 14:30:22 -07007import org.onlab.onos.cli.AbstractShellCommand;
tom91c7bd02014-09-25 22:50:44 -07008import org.onlab.onos.cli.Comparators;
alshabib99b8fdc2014-09-25 14:30:22 -07009import org.onlab.onos.net.Device;
10import org.onlab.onos.net.DeviceId;
11import org.onlab.onos.net.device.DeviceService;
alshabib1c319ff2014-10-04 20:29:09 -070012import org.onlab.onos.net.flow.FlowEntry;
13import org.onlab.onos.net.flow.FlowEntry.FlowEntryState;
alshabib99b8fdc2014-09-25 14:30:22 -070014import org.onlab.onos.net.flow.FlowRuleService;
15
toma6897792014-10-08 22:21:05 -070016import java.util.Collections;
17import java.util.List;
18import java.util.Map;
19
20import static com.google.common.collect.Lists.newArrayList;
21import static org.onlab.onos.cli.net.DevicesListCommand.getSortedDevices;
alshabib9290eea2014-09-22 11:58:17 -070022
23/**
24 * Lists all currently-known hosts.
25 */
26@Command(scope = "onos", name = "flows",
toma6897792014-10-08 22:21:05 -070027 description = "Lists all currently-known flows.")
alshabib9290eea2014-09-22 11:58:17 -070028public 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 =
toma6897792014-10-08 22:21:05 -070033 " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s, appId=%s";
alshabib99b8fdc2014-09-25 14:30:22 -070034 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",
toma6897792014-10-08 22:21:05 -070038 required = false, multiValued = false)
alshabib99b8fdc2014-09-25 14:30:22 -070039 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",
toma6897792014-10-08 22:21:05 -070042 required = false, multiValued = false)
alshabib144a2942014-09-25 18:44:02 -070043 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() {
toma6897792014-10-08 22:21:05 -070047 CoreService coreService = get(CoreService.class);
tomcaf3bf72014-09-23 13:20:53 -070048 DeviceService deviceService = get(DeviceService.class);
49 FlowRuleService service = get(FlowRuleService.class);
alshabib1c319ff2014-10-04 20:29:09 -070050 Map<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service);
tom1dd08e42014-10-07 11:40:00 -070051 for (Device d : getSortedDevices(deviceService)) {
toma6897792014-10-08 22:21:05 -070052 printFlows(d, flows.get(d), coreService);
alshabib9290eea2014-09-22 11:58:17 -070053 }
alshabib9290eea2014-09-22 11:58:17 -070054 }
55
alshabib9290eea2014-09-22 11:58:17 -070056 /**
57 * Returns the list of devices sorted using the device ID URIs.
58 *
59 * @param service device service
60 * @return sorted device list
61 */
tom1dd08e42014-10-07 11:40:00 -070062 protected Map<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService,
63 FlowRuleService service) {
alshabib1c319ff2014-10-04 20:29:09 -070064 Map<Device, List<FlowEntry>> flows = Maps.newHashMap();
65 List<FlowEntry> rules;
66 FlowEntryState s = null;
alshabib144a2942014-09-25 18:44:02 -070067 if (state != null && !state.equals("any")) {
alshabib1c319ff2014-10-04 20:29:09 -070068 s = FlowEntryState.valueOf(state.toUpperCase());
alshabib144a2942014-09-25 18:44:02 -070069 }
tom1dd08e42014-10-07 11:40:00 -070070 Iterable<Device> devices = uri == null ? deviceService.getDevices() :
toma6897792014-10-08 22:21:05 -070071 Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri)));
alshabib99b8fdc2014-09-25 14:30:22 -070072 for (Device d : devices) {
alshabib144a2942014-09-25 18:44:02 -070073 if (s == null) {
74 rules = newArrayList(service.getFlowEntries(d.id()));
75 } else {
76 rules = newArrayList();
alshabib1c319ff2014-10-04 20:29:09 -070077 for (FlowEntry f : service.getFlowEntries(d.id())) {
alshabib144a2942014-09-25 18:44:02 -070078 if (f.state().equals(s)) {
79 rules.add(f);
80 }
81 }
82 }
tom1380eee2014-09-24 09:22:02 -070083 Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR);
alshabib9290eea2014-09-22 11:58:17 -070084 flows.put(d, rules);
85 }
86 return flows;
87 }
88
89 /**
90 * Prints flows.
toma6897792014-10-08 22:21:05 -070091 *
92 * @param d the device
alshabib9290eea2014-09-22 11:58:17 -070093 * @param flows the set of flows for that device.
94 */
toma6897792014-10-08 22:21:05 -070095 protected void printFlows(Device d, List<FlowEntry> flows,
96 CoreService coreService) {
tom1dd08e42014-10-07 11:40:00 -070097 boolean empty = flows == null || flows.isEmpty();
98 print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : flows.size());
99 if (!empty) {
100 for (FlowEntry f : flows) {
toma6897792014-10-08 22:21:05 -0700101 print(FMT, Long.toHexString(f.id().value()), f.state(),
102 f.bytes(), f.packets(), f.life(), f.priority(),
103 coreService.getAppId(f.appId()).name());
tom1dd08e42014-10-07 11:40:00 -0700104 print(SFMT, f.selector().criteria());
105 print(TFMT, f.treatment().instructions());
106 }
alshabib99b8fdc2014-09-25 14:30:22 -0700107 }
alshabib9290eea2014-09-22 11:58:17 -0700108 }
109
Yuta HIGUCHIe76a24d2014-09-27 00:48:34 -0700110}