Showing
1 changed file
with
72 additions
and
2 deletions
| 1 | package org.onlab.onos.cli.net; | 1 | package org.onlab.onos.cli.net; |
| 2 | 2 | ||
| 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; | ||
| 3 | import com.google.common.collect.Maps; | 7 | import com.google.common.collect.Maps; |
| 4 | import org.apache.karaf.shell.commands.Argument; | 8 | import org.apache.karaf.shell.commands.Argument; |
| 5 | import org.apache.karaf.shell.commands.Command; | 9 | import org.apache.karaf.shell.commands.Command; |
| ... | @@ -12,6 +16,8 @@ import org.onlab.onos.net.device.DeviceService; | ... | @@ -12,6 +16,8 @@ import org.onlab.onos.net.device.DeviceService; |
| 12 | import org.onlab.onos.net.flow.FlowEntry; | 16 | import org.onlab.onos.net.flow.FlowEntry; |
| 13 | import org.onlab.onos.net.flow.FlowEntry.FlowEntryState; | 17 | import org.onlab.onos.net.flow.FlowEntry.FlowEntryState; |
| 14 | import org.onlab.onos.net.flow.FlowRuleService; | 18 | import org.onlab.onos.net.flow.FlowRuleService; |
| 19 | +import org.onlab.onos.net.flow.criteria.Criterion; | ||
| 20 | +import org.onlab.onos.net.flow.instructions.Instruction; | ||
| 15 | 21 | ||
| 16 | import java.util.Collections; | 22 | import java.util.Collections; |
| 17 | import java.util.List; | 23 | import java.util.List; |
| ... | @@ -48,9 +54,73 @@ public class FlowsListCommand extends AbstractShellCommand { | ... | @@ -48,9 +54,73 @@ public class FlowsListCommand extends AbstractShellCommand { |
| 48 | DeviceService deviceService = get(DeviceService.class); | 54 | DeviceService deviceService = get(DeviceService.class); |
| 49 | FlowRuleService service = get(FlowRuleService.class); | 55 | FlowRuleService service = get(FlowRuleService.class); |
| 50 | Map<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service); | 56 | Map<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service); |
| 51 | - for (Device d : getSortedDevices(deviceService)) { | 57 | + |
| 52 | - printFlows(d, flows.get(d), coreService); | 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 | + } | ||
| 64 | + } | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + /** | ||
| 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))); | ||
| 53 | } | 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()) | ||
| 97 | + .put("flows", array); | ||
| 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; | ||
| 54 | } | 124 | } |
| 55 | 125 | ||
| 56 | /** | 126 | /** | ... | ... |
-
Please register or login to post a comment