alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cli.net; |
| 2 | |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 3 | import com.fasterxml.jackson.databind.JsonNode; |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 6 | import com.fasterxml.jackson.databind.node.ObjectNode; |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 7 | import com.google.common.collect.Maps; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 8 | import org.apache.karaf.shell.commands.Argument; |
| 9 | import org.apache.karaf.shell.commands.Command; |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 10 | import org.onlab.onos.CoreService; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 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; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 16 | import org.onlab.onos.net.flow.FlowEntry; |
| 17 | import org.onlab.onos.net.flow.FlowEntry.FlowEntryState; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.flow.FlowRuleService; |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.flow.criteria.Criterion; |
| 20 | import org.onlab.onos.net.flow.instructions.Instruction; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 21 | |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 22 | import java.util.Collections; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import static com.google.common.collect.Lists.newArrayList; |
| 27 | import static org.onlab.onos.cli.net.DevicesListCommand.getSortedDevices; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Lists all currently-known hosts. |
| 31 | */ |
| 32 | @Command(scope = "onos", name = "flows", |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 33 | description = "Lists all currently-known flows.") |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 34 | public class FlowsListCommand extends AbstractShellCommand { |
| 35 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 36 | public static final String ANY = "any"; |
| 37 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 38 | private static final String FMT = |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 39 | " 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] | 40 | private static final String TFMT = " treatment=%s"; |
| 41 | private static final String SFMT = " selector=%s"; |
| 42 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 43 | @Argument(index = 1, name = "uri", description = "Device ID", |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 44 | required = false, multiValued = false) |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 45 | String uri = null; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 46 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 47 | @Argument(index = 0, name = "state", description = "Flow Rule state", |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 48 | required = false, multiValued = false) |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 49 | String state = null; |
alshabib | 64231d8 | 2014-09-25 18:25:31 -0700 | [diff] [blame] | 50 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 51 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 52 | protected void execute() { |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 53 | CoreService coreService = get(CoreService.class); |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 54 | DeviceService deviceService = get(DeviceService.class); |
| 55 | FlowRuleService service = get(FlowRuleService.class); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 56 | Map<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 57 | |
| 58 | if (outputJson()) { |
| 59 | print("%s", json(coreService, getSortedDevices(deviceService), flows)); |
| 60 | } else { |
| 61 | for (Device d : getSortedDevices(deviceService)) { |
| 62 | printFlows(d, flows.get(d), coreService); |
| 63 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 64 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 65 | } |
| 66 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 67 | /** |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 68 | * Produces a JSON array of flows grouped by the each device. |
| 69 | * |
| 70 | * @param coreService core service |
| 71 | * @param devices collection of devices to group flow by |
| 72 | * @param flows collection of flows per each device |
| 73 | * @return JSON array |
| 74 | */ |
| 75 | private JsonNode json(CoreService coreService, Iterable<Device> devices, |
| 76 | Map<Device, List<FlowEntry>> flows) { |
| 77 | ObjectMapper mapper = new ObjectMapper(); |
| 78 | ArrayNode result = mapper.createArrayNode(); |
| 79 | for (Device device : devices) { |
| 80 | result.add(json(coreService, mapper, device, flows.get(device))); |
| 81 | } |
| 82 | return result; |
| 83 | } |
| 84 | |
| 85 | // Produces JSON object with the flows of the given device. |
| 86 | private ObjectNode json(CoreService coreService, ObjectMapper mapper, |
| 87 | Device device, List<FlowEntry> flows) { |
| 88 | ObjectNode result = mapper.createObjectNode(); |
| 89 | ArrayNode array = mapper.createArrayNode(); |
| 90 | |
| 91 | for (FlowEntry flow : flows) { |
| 92 | array.add(json(coreService, mapper, flow)); |
| 93 | } |
| 94 | |
| 95 | result.put("device", device.id().toString()) |
| 96 | .put("flowCount", flows.size()) |
Thomas Vachuska | dfe48a7 | 2014-10-16 10:32:08 -0700 | [diff] [blame^] | 97 | .set("flows", array); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 98 | return result; |
| 99 | } |
| 100 | |
| 101 | // Produces JSON structure with the specified flow data. |
| 102 | private ObjectNode json(CoreService coreService, ObjectMapper mapper, |
| 103 | FlowEntry flow) { |
| 104 | ObjectNode result = mapper.createObjectNode(); |
| 105 | ArrayNode crit = mapper.createArrayNode(); |
| 106 | for (Criterion c : flow.selector().criteria()) { |
| 107 | crit.add(c.toString()); |
| 108 | } |
| 109 | |
| 110 | ArrayNode instr = mapper.createArrayNode(); |
| 111 | for (Instruction i : flow.treatment().instructions()) { |
| 112 | instr.add(i.toString()); |
| 113 | } |
| 114 | |
| 115 | result.put("flowId", Long.toHexString(flow.id().value())) |
| 116 | .put("state", flow.state().toString()) |
| 117 | .put("bytes", flow.bytes()) |
| 118 | .put("packets", flow.packets()) |
| 119 | .put("life", flow.life()) |
| 120 | .put("appId", coreService.getAppId(flow.appId()).name()); |
| 121 | result.set("selector", crit); |
| 122 | result.set("treatment", instr); |
| 123 | return result; |
| 124 | } |
| 125 | |
| 126 | /** |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 127 | * Returns the list of devices sorted using the device ID URIs. |
| 128 | * |
| 129 | * @param service device service |
| 130 | * @return sorted device list |
| 131 | */ |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 132 | protected Map<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService, |
| 133 | FlowRuleService service) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 134 | Map<Device, List<FlowEntry>> flows = Maps.newHashMap(); |
| 135 | List<FlowEntry> rules; |
| 136 | FlowEntryState s = null; |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 137 | if (state != null && !state.equals("any")) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 138 | s = FlowEntryState.valueOf(state.toUpperCase()); |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 139 | } |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 140 | Iterable<Device> devices = uri == null ? deviceService.getDevices() : |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 141 | Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri))); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 142 | for (Device d : devices) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 143 | if (s == null) { |
| 144 | rules = newArrayList(service.getFlowEntries(d.id())); |
| 145 | } else { |
| 146 | rules = newArrayList(); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 147 | for (FlowEntry f : service.getFlowEntries(d.id())) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 148 | if (f.state().equals(s)) { |
| 149 | rules.add(f); |
| 150 | } |
| 151 | } |
| 152 | } |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 153 | Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 154 | flows.put(d, rules); |
| 155 | } |
| 156 | return flows; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Prints flows. |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 161 | * |
| 162 | * @param d the device |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 163 | * @param flows the set of flows for that device. |
| 164 | */ |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 165 | protected void printFlows(Device d, List<FlowEntry> flows, |
| 166 | CoreService coreService) { |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 167 | boolean empty = flows == null || flows.isEmpty(); |
| 168 | print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : flows.size()); |
| 169 | if (!empty) { |
| 170 | for (FlowEntry f : flows) { |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 171 | print(FMT, Long.toHexString(f.id().value()), f.state(), |
| 172 | f.bytes(), f.packets(), f.life(), f.priority(), |
| 173 | coreService.getAppId(f.appId()).name()); |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 174 | print(SFMT, f.selector().criteria()); |
| 175 | print(TFMT, f.treatment().instructions()); |
| 176 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 177 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Yuta HIGUCHI | e76a24d | 2014-09-27 00:48:34 -0700 | [diff] [blame] | 180 | } |