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; |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame^] | 16 | import org.onlab.onos.net.flow.FlowRule.FlowRuleState; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 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 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame^] | 28 | public static final String ANY = "any"; |
| 29 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 30 | private static final String FMT = |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 31 | " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s"; |
| 32 | private static final String TFMT = " treatment=%s"; |
| 33 | private static final String SFMT = " selector=%s"; |
| 34 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame^] | 35 | @Argument(index = 1, name = "uri", description = "Device ID", |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 36 | required = false, multiValued = false) |
| 37 | String uri = null; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 38 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame^] | 39 | @Argument(index = 0, name = "state", description = "Flow Rule state", |
| 40 | required = false, multiValued = false) |
| 41 | String state = null; |
alshabib | 64231d8 | 2014-09-25 18:25:31 -0700 | [diff] [blame] | 42 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 43 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 44 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 45 | DeviceService deviceService = get(DeviceService.class); |
| 46 | FlowRuleService service = get(FlowRuleService.class); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 47 | Map<Device, List<FlowRule>> flows = getSortedFlows(deviceService, service); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 48 | for (Device d : flows.keySet()) { |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 49 | printFlows(d, flows.get(d)); |
| 50 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 51 | } |
| 52 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Returns the list of devices sorted using the device ID URIs. |
| 55 | * |
| 56 | * @param service device service |
| 57 | * @return sorted device list |
| 58 | */ |
| 59 | protected Map<Device, List<FlowRule>> getSortedFlows(DeviceService deviceService, FlowRuleService service) { |
| 60 | Map<Device, List<FlowRule>> flows = Maps.newHashMap(); |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame^] | 61 | List<FlowRule> rules; |
| 62 | FlowRuleState s = null; |
| 63 | if (state != null && !state.equals("any")) { |
| 64 | s = FlowRuleState.valueOf(state.toUpperCase()); |
| 65 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 66 | Iterable<Device> devices = uri == null ? deviceService.getDevices() : |
| 67 | Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri))); |
| 68 | for (Device d : devices) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame^] | 69 | if (s == null) { |
| 70 | rules = newArrayList(service.getFlowEntries(d.id())); |
| 71 | } else { |
| 72 | rules = newArrayList(); |
| 73 | for (FlowRule f : service.getFlowEntries(d.id())) { |
| 74 | if (f.state().equals(s)) { |
| 75 | rules.add(f); |
| 76 | } |
| 77 | } |
| 78 | } |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 79 | Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 80 | flows.put(d, rules); |
| 81 | } |
| 82 | return flows; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Prints flows. |
| 87 | * @param d the device |
| 88 | * @param flows the set of flows for that device. |
| 89 | */ |
| 90 | protected void printFlows(Device d, List<FlowRule> flows) { |
| 91 | print("Device: " + d.id()); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 92 | if (flows == null | flows.isEmpty()) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame^] | 93 | print(" %s", "No flows."); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 94 | return; |
| 95 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 96 | for (FlowRule f : flows) { |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 97 | print(FMT, Long.toHexString(f.id().value()), f.state(), f.bytes(), |
| 98 | f.packets(), f.lifeMillis(), f.priority()); |
| 99 | print(SFMT, f.selector().criteria()); |
| 100 | print(TFMT, f.treatment().instructions()); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | } |
| 104 | |
| 105 | } |