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; |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 4 | import static org.onlab.onos.cli.net.DevicesListCommand.getSortedDevices; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 5 | |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 6 | import java.util.Collections; |
| 7 | import java.util.List; |
| 8 | import java.util.Map; |
| 9 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 10 | import org.apache.karaf.shell.commands.Argument; |
| 11 | import org.apache.karaf.shell.commands.Command; |
| 12 | import org.onlab.onos.cli.AbstractShellCommand; |
tom | 91c7bd0 | 2014-09-25 22:50:44 -0700 | [diff] [blame] | 13 | import org.onlab.onos.cli.Comparators; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.Device; |
| 15 | import org.onlab.onos.net.DeviceId; |
| 16 | import org.onlab.onos.net.device.DeviceService; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.flow.FlowEntry; |
| 18 | import org.onlab.onos.net.flow.FlowEntry.FlowEntryState; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.flow.FlowRuleService; |
| 20 | |
| 21 | import com.google.common.collect.Maps; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Lists all currently-known hosts. |
| 25 | */ |
| 26 | @Command(scope = "onos", name = "flows", |
| 27 | description = "Lists all currently-known flows.") |
| 28 | public class FlowsListCommand extends AbstractShellCommand { |
| 29 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 30 | public static final String ANY = "any"; |
| 31 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 32 | private static final String FMT = |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 33 | " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s"; |
| 34 | private static final String TFMT = " treatment=%s"; |
| 35 | private static final String SFMT = " selector=%s"; |
| 36 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 37 | @Argument(index = 1, name = "uri", description = "Device ID", |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 38 | required = false, multiValued = false) |
| 39 | String uri = null; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 40 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 41 | @Argument(index = 0, name = "state", description = "Flow Rule state", |
| 42 | required = false, multiValued = false) |
| 43 | String state = null; |
alshabib | 64231d8 | 2014-09-25 18:25:31 -0700 | [diff] [blame] | 44 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 45 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 46 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 47 | DeviceService deviceService = get(DeviceService.class); |
| 48 | FlowRuleService service = get(FlowRuleService.class); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 49 | Map<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service); |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame^] | 50 | for (Device d : getSortedDevices(deviceService)) { |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 51 | printFlows(d, flows.get(d)); |
| 52 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 53 | } |
| 54 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Returns the list of devices sorted using the device ID URIs. |
| 57 | * |
| 58 | * @param service device service |
| 59 | * @return sorted device list |
| 60 | */ |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame^] | 61 | protected Map<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService, |
| 62 | FlowRuleService service) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 63 | Map<Device, List<FlowEntry>> flows = Maps.newHashMap(); |
| 64 | List<FlowEntry> rules; |
| 65 | FlowEntryState s = null; |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 66 | if (state != null && !state.equals("any")) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 67 | s = FlowEntryState.valueOf(state.toUpperCase()); |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 68 | } |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame^] | 69 | Iterable<Device> devices = uri == null ? deviceService.getDevices() : |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 70 | Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri))); |
| 71 | for (Device d : devices) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 72 | if (s == null) { |
| 73 | rules = newArrayList(service.getFlowEntries(d.id())); |
| 74 | } else { |
| 75 | rules = newArrayList(); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 76 | for (FlowEntry f : service.getFlowEntries(d.id())) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 77 | if (f.state().equals(s)) { |
| 78 | rules.add(f); |
| 79 | } |
| 80 | } |
| 81 | } |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 82 | Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 83 | flows.put(d, rules); |
| 84 | } |
| 85 | return flows; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Prints flows. |
| 90 | * @param d the device |
| 91 | * @param flows the set of flows for that device. |
| 92 | */ |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 93 | protected void printFlows(Device d, List<FlowEntry> flows) { |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame^] | 94 | boolean empty = flows == null || flows.isEmpty(); |
| 95 | print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : flows.size()); |
| 96 | if (!empty) { |
| 97 | for (FlowEntry f : flows) { |
| 98 | print(FMT, Long.toHexString(f.id().value()), f.state(), f.bytes(), |
| 99 | f.packets(), f.life(), f.priority()); |
| 100 | print(SFMT, f.selector().criteria()); |
| 101 | print(TFMT, f.treatment().instructions()); |
| 102 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 103 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Yuta HIGUCHI | e76a24d | 2014-09-27 00:48:34 -0700 | [diff] [blame] | 106 | } |