alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cli.net; |
| 2 | |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame^] | 3 | import com.google.common.collect.Maps; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 4 | import org.apache.karaf.shell.commands.Command; |
| 5 | import org.onlab.onos.cli.AbstractShellCommand; |
| 6 | import org.onlab.onos.net.Device; |
| 7 | import org.onlab.onos.net.device.DeviceService; |
| 8 | import org.onlab.onos.net.flow.FlowRule; |
| 9 | import org.onlab.onos.net.flow.FlowRuleService; |
| 10 | |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame^] | 11 | import java.util.Collections; |
| 12 | import java.util.List; |
| 13 | import java.util.Map; |
| 14 | |
| 15 | import static com.google.common.collect.Lists.newArrayList; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * Lists all currently-known hosts. |
| 19 | */ |
| 20 | @Command(scope = "onos", name = "flows", |
| 21 | description = "Lists all currently-known flows.") |
| 22 | public class FlowsListCommand extends AbstractShellCommand { |
| 23 | |
| 24 | private static final String FMT = |
| 25 | " id=%s, selector=%s, treatment=%s, state=%s"; |
| 26 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 27 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 28 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 29 | DeviceService deviceService = get(DeviceService.class); |
| 30 | FlowRuleService service = get(FlowRuleService.class); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 31 | Map<Device, List<FlowRule>> flows = getSortedFlows(deviceService, service); |
| 32 | for (Device d : deviceService.getDevices()) { |
| 33 | printFlows(d, flows.get(d)); |
| 34 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 35 | } |
| 36 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Returns the list of devices sorted using the device ID URIs. |
| 39 | * |
| 40 | * @param service device service |
| 41 | * @return sorted device list |
| 42 | */ |
| 43 | protected Map<Device, List<FlowRule>> getSortedFlows(DeviceService deviceService, FlowRuleService service) { |
| 44 | Map<Device, List<FlowRule>> flows = Maps.newHashMap(); |
| 45 | List<FlowRule> rules; |
| 46 | for (Device d : deviceService.getDevices()) { |
| 47 | rules = newArrayList(service.getFlowEntries(d.id())); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame^] | 48 | Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 49 | flows.put(d, rules); |
| 50 | } |
| 51 | return flows; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Prints flows. |
| 56 | * @param d the device |
| 57 | * @param flows the set of flows for that device. |
| 58 | */ |
| 59 | protected void printFlows(Device d, List<FlowRule> flows) { |
| 60 | print("Device: " + d.id()); |
| 61 | for (FlowRule f : flows) { |
| 62 | print(FMT, f.id().value(), f.selector(), f.treatment(), f.state()); |
| 63 | } |
| 64 | |
| 65 | } |
| 66 | |
| 67 | } |