blob: 41f30a7620785cea8cb8c6baa750563402fd6d4e [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;
tom91c7bd02014-09-25 22:50:44 -070012import org.onlab.onos.cli.Comparators;
alshabib99b8fdc2014-09-25 14:30:22 -070013import org.onlab.onos.net.Device;
14import org.onlab.onos.net.DeviceId;
15import org.onlab.onos.net.device.DeviceService;
16import org.onlab.onos.net.flow.FlowRule;
alshabib144a2942014-09-25 18:44:02 -070017import org.onlab.onos.net.flow.FlowRule.FlowRuleState;
alshabib99b8fdc2014-09-25 14:30:22 -070018import org.onlab.onos.net.flow.FlowRuleService;
19
20import com.google.common.collect.Maps;
alshabib9290eea2014-09-22 11:58:17 -070021
22/**
23 * Lists all currently-known hosts.
24 */
25@Command(scope = "onos", name = "flows",
26description = "Lists all currently-known flows.")
27public class FlowsListCommand extends AbstractShellCommand {
28
alshabib144a2942014-09-25 18:44:02 -070029 public static final String ANY = "any";
30
alshabib9290eea2014-09-22 11:58:17 -070031 private static final String FMT =
alshabib99b8fdc2014-09-25 14:30:22 -070032 " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s";
33 private static final String TFMT = " treatment=%s";
34 private static final String SFMT = " selector=%s";
35
alshabib144a2942014-09-25 18:44:02 -070036 @Argument(index = 1, name = "uri", description = "Device ID",
alshabib99b8fdc2014-09-25 14:30:22 -070037 required = false, multiValued = false)
38 String uri = null;
alshabib9290eea2014-09-22 11:58:17 -070039
alshabib144a2942014-09-25 18:44:02 -070040 @Argument(index = 0, name = "state", description = "Flow Rule state",
41 required = false, multiValued = false)
42 String state = null;
alshabib64231d82014-09-25 18:25:31 -070043
alshabib9290eea2014-09-22 11:58:17 -070044 @Override
tom0872a172014-09-23 11:24:26 -070045 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070046 DeviceService deviceService = get(DeviceService.class);
47 FlowRuleService service = get(FlowRuleService.class);
alshabib9290eea2014-09-22 11:58:17 -070048 Map<Device, List<FlowRule>> flows = getSortedFlows(deviceService, service);
alshabib99b8fdc2014-09-25 14:30:22 -070049 for (Device d : flows.keySet()) {
alshabib9290eea2014-09-22 11:58:17 -070050 printFlows(d, flows.get(d));
51 }
alshabib9290eea2014-09-22 11:58:17 -070052 }
53
alshabib9290eea2014-09-22 11:58:17 -070054 /**
55 * Returns the list of devices sorted using the device ID URIs.
56 *
57 * @param service device service
58 * @return sorted device list
59 */
60 protected Map<Device, List<FlowRule>> getSortedFlows(DeviceService deviceService, FlowRuleService service) {
61 Map<Device, List<FlowRule>> flows = Maps.newHashMap();
alshabib144a2942014-09-25 18:44:02 -070062 List<FlowRule> rules;
63 FlowRuleState s = null;
64 if (state != null && !state.equals("any")) {
65 s = FlowRuleState.valueOf(state.toUpperCase());
66 }
alshabib99b8fdc2014-09-25 14:30:22 -070067 Iterable<Device> devices = uri == null ? deviceService.getDevices() :
68 Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri)));
69 for (Device d : devices) {
alshabib144a2942014-09-25 18:44:02 -070070 if (s == null) {
71 rules = newArrayList(service.getFlowEntries(d.id()));
72 } else {
73 rules = newArrayList();
74 for (FlowRule f : service.getFlowEntries(d.id())) {
75 if (f.state().equals(s)) {
76 rules.add(f);
77 }
78 }
79 }
tom1380eee2014-09-24 09:22:02 -070080 Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR);
alshabib9290eea2014-09-22 11:58:17 -070081 flows.put(d, rules);
82 }
83 return flows;
84 }
85
86 /**
87 * Prints flows.
88 * @param d the device
89 * @param flows the set of flows for that device.
90 */
91 protected void printFlows(Device d, List<FlowRule> flows) {
92 print("Device: " + d.id());
alshabib99b8fdc2014-09-25 14:30:22 -070093 if (flows == null | flows.isEmpty()) {
alshabib144a2942014-09-25 18:44:02 -070094 print(" %s", "No flows.");
alshabib99b8fdc2014-09-25 14:30:22 -070095 return;
96 }
alshabib9290eea2014-09-22 11:58:17 -070097 for (FlowRule f : flows) {
alshabib99b8fdc2014-09-25 14:30:22 -070098 print(FMT, Long.toHexString(f.id().value()), f.state(), f.bytes(),
99 f.packets(), f.lifeMillis(), f.priority());
100 print(SFMT, f.selector().criteria());
101 print(TFMT, f.treatment().instructions());
alshabib9290eea2014-09-22 11:58:17 -0700102 }
103
104 }
105
Yuta HIGUCHIe76a24d2014-09-27 00:48:34 -0700106}