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; |
tom | 91c7bd0 | 2014-09-25 22:50:44 -0700 | [diff] [blame^] | 12 | import org.onlab.onos.cli.Comparators; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 13 | import org.onlab.onos.net.Device; |
| 14 | import org.onlab.onos.net.DeviceId; |
| 15 | import org.onlab.onos.net.device.DeviceService; |
| 16 | import org.onlab.onos.net.flow.FlowRule; |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.flow.FlowRule.FlowRuleState; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.flow.FlowRuleService; |
| 19 | |
| 20 | import com.google.common.collect.Maps; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * Lists all currently-known hosts. |
| 24 | */ |
| 25 | @Command(scope = "onos", name = "flows", |
| 26 | description = "Lists all currently-known flows.") |
| 27 | public class FlowsListCommand extends AbstractShellCommand { |
| 28 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 29 | public static final String ANY = "any"; |
| 30 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 31 | private static final String FMT = |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 32 | " 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 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 36 | @Argument(index = 1, name = "uri", description = "Device ID", |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 37 | required = false, multiValued = false) |
| 38 | String uri = null; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 39 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 40 | @Argument(index = 0, name = "state", description = "Flow Rule state", |
| 41 | required = false, multiValued = false) |
| 42 | String state = null; |
alshabib | 64231d8 | 2014-09-25 18:25:31 -0700 | [diff] [blame] | 43 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 44 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 45 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 46 | DeviceService deviceService = get(DeviceService.class); |
| 47 | FlowRuleService service = get(FlowRuleService.class); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 48 | Map<Device, List<FlowRule>> flows = getSortedFlows(deviceService, service); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 49 | for (Device d : flows.keySet()) { |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 50 | printFlows(d, flows.get(d)); |
| 51 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 52 | } |
| 53 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 54 | /** |
| 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(); |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 62 | List<FlowRule> rules; |
| 63 | FlowRuleState s = null; |
| 64 | if (state != null && !state.equals("any")) { |
| 65 | s = FlowRuleState.valueOf(state.toUpperCase()); |
| 66 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 67 | Iterable<Device> devices = uri == null ? deviceService.getDevices() : |
| 68 | Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri))); |
| 69 | for (Device d : devices) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 70 | 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 | } |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 80 | Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 81 | 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()); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 93 | if (flows == null | flows.isEmpty()) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 94 | print(" %s", "No flows."); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 95 | return; |
| 96 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 97 | for (FlowRule f : flows) { |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 98 | 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()); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | } |
| 105 | |
| 106 | } |