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