yoonseon | bc0d76f | 2017-01-05 14:52:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.cli.net.vnet; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import org.apache.karaf.shell.commands.Argument; |
| 23 | import org.apache.karaf.shell.commands.Command; |
| 24 | import org.apache.karaf.shell.commands.Option; |
| 25 | import org.onlab.util.StringFilter; |
| 26 | import org.onosproject.cli.AbstractShellCommand; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 30 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 31 | import org.onosproject.net.Device; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.device.DeviceService; |
| 34 | import org.onosproject.net.flow.FlowEntry; |
| 35 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
| 36 | import org.onosproject.net.flow.FlowRuleService; |
| 37 | import org.onosproject.net.flow.TrafficTreatment; |
| 38 | import org.onosproject.utils.Comparators; |
| 39 | |
| 40 | import java.util.ArrayList; |
| 41 | import java.util.Collections; |
| 42 | import java.util.List; |
| 43 | import java.util.Map; |
| 44 | import java.util.SortedMap; |
| 45 | import java.util.TreeMap; |
| 46 | import java.util.function.Predicate; |
| 47 | import java.util.stream.Collectors; |
| 48 | |
| 49 | import static com.google.common.collect.Lists.newArrayList; |
| 50 | |
| 51 | |
| 52 | /** |
| 53 | * Lists all currently-known flows. |
| 54 | */ |
| 55 | @Command(scope = "onos", name = "vnet-flows", |
| 56 | description = "Lists all currently-known flows for a virtual network.") |
| 57 | public class VirtualFlowsListCommand extends AbstractShellCommand { |
| 58 | |
| 59 | private static final Predicate<FlowEntry> TRUE_PREDICATE = f -> true; |
| 60 | |
| 61 | public static final String ANY = "any"; |
| 62 | |
| 63 | private static final String LONG_FORMAT = " id=%s, state=%s, bytes=%s, " |
| 64 | + "packets=%s, duration=%s, liveType=%s, priority=%s, tableId=%s, appId=%s, " |
| 65 | + "payLoad=%s, selector=%s, treatment=%s"; |
| 66 | |
| 67 | private static final String SHORT_FORMAT = " %s, bytes=%s, packets=%s, " |
| 68 | + "table=%s, priority=%s, selector=%s, treatment=%s"; |
| 69 | |
| 70 | @Argument(index = 0, name = "networkId", description = "Network ID", |
| 71 | required = true, multiValued = false) |
| 72 | Long networkId = null; |
| 73 | |
| 74 | @Argument(index = 1, name = "state", description = "Flow Rule state", |
| 75 | required = false, multiValued = false) |
| 76 | String state = null; |
| 77 | |
| 78 | @Argument(index = 2, name = "uri", description = "Device ID", |
| 79 | required = false, multiValued = false) |
| 80 | String uri = null; |
| 81 | |
| 82 | @Argument(index = 3, name = "table", description = "Table ID", |
| 83 | required = false, multiValued = false) |
| 84 | String table = null; |
| 85 | |
| 86 | @Option(name = "-s", aliases = "--short", |
| 87 | description = "Print more succinct output for each flow", |
| 88 | required = false, multiValued = false) |
| 89 | private boolean shortOutput = false; |
| 90 | |
| 91 | @Option(name = "-c", aliases = "--count", |
| 92 | description = "Print flow count only", |
| 93 | required = false, multiValued = false) |
| 94 | private boolean countOnly = false; |
| 95 | |
| 96 | @Option(name = "-f", aliases = "--filter", |
| 97 | description = "Filter flows by specific key", |
| 98 | required = false, multiValued = true) |
| 99 | private List<String> filter = new ArrayList<>(); |
| 100 | |
| 101 | private Predicate<FlowEntry> predicate = TRUE_PREDICATE; |
| 102 | |
| 103 | private StringFilter contentFilter; |
| 104 | |
| 105 | @Override |
| 106 | protected void execute() { |
| 107 | CoreService coreService = get(CoreService.class); |
| 108 | |
| 109 | VirtualNetworkService vnetservice = get(VirtualNetworkService.class); |
| 110 | DeviceService deviceService = vnetservice.get(NetworkId.networkId(networkId), |
| 111 | DeviceService.class); |
| 112 | FlowRuleService service = vnetservice.get(NetworkId.networkId(networkId), |
| 113 | FlowRuleService.class); |
| 114 | contentFilter = new StringFilter(filter, StringFilter.Strategy.AND); |
| 115 | |
| 116 | compilePredicate(); |
| 117 | |
| 118 | SortedMap<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service); |
| 119 | |
| 120 | if (outputJson()) { |
| 121 | print("%s", json(flows.keySet(), flows)); |
| 122 | } else { |
| 123 | flows.forEach((device, flow) -> printFlows(device, flow, coreService)); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Produces a JSON array of flows grouped by the each device. |
| 129 | * |
| 130 | * @param devices collection of devices to group flow by |
| 131 | * @param flows collection of flows per each device |
| 132 | * @return JSON array |
| 133 | */ |
| 134 | private JsonNode json(Iterable<Device> devices, |
| 135 | Map<Device, List<FlowEntry>> flows) { |
| 136 | ObjectMapper mapper = new ObjectMapper(); |
| 137 | ArrayNode result = mapper.createArrayNode(); |
| 138 | for (Device device : devices) { |
| 139 | result.add(json(mapper, device, flows.get(device))); |
| 140 | } |
| 141 | return result; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Compiles a predicate to find matching flows based on the command |
| 146 | * arguments. |
| 147 | */ |
| 148 | private void compilePredicate() { |
| 149 | if (state != null && !state.equals(ANY)) { |
| 150 | final FlowEntryState feState = FlowEntryState.valueOf(state.toUpperCase()); |
| 151 | predicate = predicate.and(f -> f.state().equals(feState)); |
| 152 | } |
| 153 | |
| 154 | if (table != null) { |
| 155 | final int tableId = Integer.parseInt(table); |
| 156 | predicate = predicate.and(f -> f.tableId() == tableId); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // Produces JSON object with the flows of the given device. |
| 161 | private ObjectNode json(ObjectMapper mapper, |
| 162 | Device device, List<FlowEntry> flows) { |
| 163 | ObjectNode result = mapper.createObjectNode(); |
| 164 | ArrayNode array = mapper.createArrayNode(); |
| 165 | |
| 166 | flows.forEach(flow -> array.add(jsonForEntity(flow, FlowEntry.class))); |
| 167 | |
| 168 | result.put("device", device.id().toString()) |
| 169 | .put("flowCount", flows.size()) |
| 170 | .set("flows", array); |
| 171 | return result; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Returns the list of devices sorted using the device ID URIs. |
| 176 | * |
| 177 | * @param deviceService device service |
| 178 | * @param service flow rule service |
| 179 | * @return sorted device list |
| 180 | */ |
| 181 | protected SortedMap<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService, |
| 182 | FlowRuleService service) { |
| 183 | SortedMap<Device, List<FlowEntry>> flows = new TreeMap<>(Comparators.ELEMENT_COMPARATOR); |
| 184 | List<FlowEntry> rules; |
| 185 | |
| 186 | Iterable<Device> devices = null; |
| 187 | if (uri == null) { |
| 188 | devices = deviceService.getDevices(); |
| 189 | } else { |
| 190 | Device dev = deviceService.getDevice(DeviceId.deviceId(uri)); |
| 191 | devices = (dev == null) ? deviceService.getDevices() |
| 192 | : Collections.singletonList(dev); |
| 193 | } |
| 194 | |
| 195 | for (Device d : devices) { |
| 196 | if (predicate.equals(TRUE_PREDICATE)) { |
| 197 | rules = newArrayList(service.getFlowEntries(d.id())); |
| 198 | } else { |
| 199 | rules = newArrayList(); |
| 200 | for (FlowEntry f : service.getFlowEntries(d.id())) { |
| 201 | if (predicate.test(f)) { |
| 202 | rules.add(f); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | rules.sort(Comparators.FLOW_RULE_COMPARATOR); |
| 207 | |
| 208 | flows.put(d, rules); |
| 209 | } |
| 210 | return flows; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Prints flows. |
| 215 | * |
| 216 | * @param d the device |
| 217 | * @param flows the set of flows for that device |
| 218 | * @param coreService core system service |
| 219 | */ |
| 220 | protected void printFlows(Device d, List<FlowEntry> flows, |
| 221 | CoreService coreService) { |
| 222 | List<FlowEntry> filteredFlows = flows.stream(). |
| 223 | filter(f -> contentFilter.filter(f)).collect(Collectors.toList()); |
| 224 | boolean empty = filteredFlows == null || filteredFlows.isEmpty(); |
| 225 | print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : filteredFlows.size()); |
| 226 | if (empty || countOnly) { |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | for (FlowEntry f : filteredFlows) { |
| 231 | if (shortOutput) { |
| 232 | print(SHORT_FORMAT, f.state(), f.bytes(), f.packets(), |
| 233 | f.tableId(), f.priority(), f.selector().criteria(), |
| 234 | printTreatment(f.treatment())); |
| 235 | } else { |
| 236 | ApplicationId appId = coreService.getAppId(f.appId()); |
| 237 | print(LONG_FORMAT, Long.toHexString(f.id().value()), f.state(), |
| 238 | f.bytes(), f.packets(), f.life(), f.liveType(), f.priority(), f.tableId(), |
| 239 | appId != null ? appId.name() : "<none>", |
| 240 | f.payLoad() == null ? null : f.payLoad().payLoad().toString(), |
| 241 | f.selector().criteria(), f.treatment()); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | private String printTreatment(TrafficTreatment treatment) { |
| 247 | final String delimiter = ", "; |
| 248 | StringBuilder builder = new StringBuilder("["); |
| 249 | if (!treatment.immediate().isEmpty()) { |
| 250 | builder.append("immediate=" + treatment.immediate() + delimiter); |
| 251 | } |
| 252 | if (!treatment.deferred().isEmpty()) { |
| 253 | builder.append("deferred=" + treatment.deferred() + delimiter); |
| 254 | } |
| 255 | if (treatment.clearedDeferred()) { |
| 256 | builder.append("clearDeferred" + delimiter); |
| 257 | } |
| 258 | if (treatment.tableTransition() != null) { |
| 259 | builder.append("transition=" + treatment.tableTransition() + delimiter); |
| 260 | } |
| 261 | if (treatment.metered() != null) { |
| 262 | builder.append("meter=" + treatment.metered() + delimiter); |
| 263 | } |
| 264 | if (treatment.writeMetadata() != null) { |
| 265 | builder.append("metadata=" + treatment.writeMetadata() + delimiter); |
| 266 | } |
| 267 | // Chop off last delimiter |
| 268 | builder.replace(builder.length() - delimiter.length(), builder.length(), ""); |
| 269 | builder.append("]"); |
| 270 | return builder.toString(); |
| 271 | } |
| 272 | |
| 273 | } |