alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cli.net; |
| 2 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 3 | import static com.google.common.collect.Lists.newArrayList; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 4 | |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 5 | import java.util.Collections; |
| 6 | import java.util.List; |
| 7 | import java.util.Map; |
| 8 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 9 | import org.apache.karaf.shell.commands.Argument; |
| 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.DeviceId; |
| 14 | import org.onlab.onos.net.device.DeviceService; |
| 15 | import org.onlab.onos.net.flow.FlowRule; |
| 16 | import org.onlab.onos.net.flow.FlowRule.FlowRuleState; |
| 17 | import org.onlab.onos.net.flow.FlowRuleService; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * Lists all currently-known hosts. |
| 23 | */ |
| 24 | @Command(scope = "onos", name = "flows", |
| 25 | description = "Lists all currently-known flows.") |
| 26 | public class FlowsListCommand extends AbstractShellCommand { |
| 27 | |
| 28 | private static final String FMT = |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 29 | " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s"; |
| 30 | private static final String TFMT = " treatment=%s"; |
| 31 | private static final String SFMT = " selector=%s"; |
| 32 | |
| 33 | @Argument(index = 0, name = "state", description = "Flow rule state", |
| 34 | required = false, multiValued = false) |
| 35 | FlowRuleState state = null; |
| 36 | |
| 37 | @Argument(index = 1, name = "uri", description = "Device ID", |
| 38 | required = false, multiValued = false) |
| 39 | String uri = null; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 40 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 41 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 42 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 43 | DeviceService deviceService = get(DeviceService.class); |
| 44 | FlowRuleService service = get(FlowRuleService.class); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 45 | Map<Device, List<FlowRule>> flows = getSortedFlows(deviceService, service); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 46 | for (Device d : flows.keySet()) { |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 47 | printFlows(d, flows.get(d)); |
| 48 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 49 | } |
| 50 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Returns the list of devices sorted using the device ID URIs. |
| 53 | * |
| 54 | * @param service device service |
| 55 | * @return sorted device list |
| 56 | */ |
| 57 | protected Map<Device, List<FlowRule>> getSortedFlows(DeviceService deviceService, FlowRuleService service) { |
| 58 | Map<Device, List<FlowRule>> flows = Maps.newHashMap(); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 59 | List<FlowRule> rules = newArrayList(); |
| 60 | Iterable<Device> devices = uri == null ? deviceService.getDevices() : |
| 61 | Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri))); |
| 62 | for (Device d : devices) { |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 63 | rules = newArrayList(service.getFlowEntries(d.id())); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 64 | Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 65 | flows.put(d, rules); |
| 66 | } |
| 67 | return flows; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Prints flows. |
| 72 | * @param d the device |
| 73 | * @param flows the set of flows for that device. |
| 74 | */ |
| 75 | protected void printFlows(Device d, List<FlowRule> flows) { |
| 76 | print("Device: " + d.id()); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 77 | if (flows == null | flows.isEmpty()) { |
| 78 | print(" %s", "No flows installed."); |
| 79 | return; |
| 80 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 81 | for (FlowRule f : flows) { |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 82 | print(FMT, Long.toHexString(f.id().value()), f.state(), f.bytes(), |
| 83 | f.packets(), f.lifeMillis(), f.priority()); |
| 84 | print(SFMT, f.selector().criteria()); |
| 85 | print(TFMT, f.treatment().instructions()); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | } |
| 89 | |
| 90 | } |