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