Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 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; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Argument; |
| 23 | import org.apache.karaf.shell.commands.Command; |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 24 | import org.apache.karaf.shell.commands.Option; |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 25 | import org.onlab.util.StringFilter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.cli.AbstractShellCommand; |
Brian O'Connor | 9cc799c | 2015-06-04 19:35:41 -0700 | [diff] [blame] | 27 | import org.onosproject.core.ApplicationId; |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 28 | import org.onosproject.core.CoreService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.Device; |
| 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.device.DeviceService; |
| 32 | import org.onosproject.net.flow.FlowEntry; |
| 33 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
| 34 | import org.onosproject.net.flow.FlowRuleService; |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 35 | import org.onosproject.net.flow.TrafficTreatment; |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 36 | import org.onosproject.utils.Comparators; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 37 | |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 38 | import java.io.BufferedReader; |
| 39 | import java.io.IOException; |
| 40 | import java.io.InputStreamReader; |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 41 | import java.util.ArrayList; |
Yuta HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame] | 42 | import java.util.Arrays; |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 43 | import java.util.Collections; |
| 44 | import java.util.List; |
| 45 | import java.util.Map; |
| 46 | import java.util.SortedMap; |
| 47 | import java.util.TreeMap; |
| 48 | import java.util.function.Predicate; |
Mark | 4c96452 | 2016-03-08 11:26:07 -0800 | [diff] [blame] | 49 | import java.util.stream.Collectors; |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 50 | |
| 51 | import static com.google.common.collect.Lists.newArrayList; |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 52 | |
Mark | 4c96452 | 2016-03-08 11:26:07 -0800 | [diff] [blame] | 53 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 54 | /** |
jcc | f988bf5 | 2015-05-05 19:02:50 +0800 | [diff] [blame] | 55 | * Lists all currently-known flows. |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 56 | */ |
| 57 | @Command(scope = "onos", name = "flows", |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 58 | description = "Lists all currently-known flows.") |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 59 | public class FlowsListCommand extends AbstractShellCommand { |
| 60 | |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 61 | private static final Predicate<FlowEntry> TRUE_PREDICATE = f -> true; |
| 62 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 63 | public static final String ANY = "any"; |
| 64 | |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 65 | private static final String LONG_FORMAT = " id=%s, state=%s, bytes=%s, " |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 66 | + "packets=%s, duration=%s, liveType=%s, priority=%s, tableId=%s, appId=%s, " |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 67 | + "payLoad=%s, selector=%s, treatment=%s"; |
| 68 | |
| 69 | private static final String SHORT_FORMAT = " %s, bytes=%s, packets=%s, " |
| 70 | + "table=%s, priority=%s, selector=%s, treatment=%s"; |
| 71 | |
| 72 | @Argument(index = 0, name = "state", description = "Flow Rule state", |
| 73 | required = false, multiValued = false) |
| 74 | String state = null; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 75 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 76 | @Argument(index = 1, name = "uri", description = "Device ID", |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 77 | required = false, multiValued = false) |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 78 | String uri = null; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 79 | |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 80 | @Argument(index = 2, name = "table", description = "Table ID", |
| 81 | required = false, multiValued = false) |
| 82 | String table = null; |
| 83 | |
| 84 | @Option(name = "-s", aliases = "--short", |
| 85 | description = "Print more succinct output for each flow", |
| 86 | required = false, multiValued = false) |
| 87 | private boolean shortOutput = false; |
| 88 | |
Mark | 4c96452 | 2016-03-08 11:26:07 -0800 | [diff] [blame] | 89 | @Option(name = "-n", aliases = "--no-core-flows", |
| 90 | description = "Suppress core flows from output", |
| 91 | required = false, multiValued = false) |
| 92 | private boolean suppressCoreOutput = false; |
| 93 | |
Charles Chan | f9b94ab | 2016-02-23 19:31:41 -0800 | [diff] [blame] | 94 | @Option(name = "-c", aliases = "--count", |
| 95 | description = "Print flow count only", |
| 96 | required = false, multiValued = false) |
| 97 | private boolean countOnly = false; |
| 98 | |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 99 | @Option(name = "-f", aliases = "--filter", |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 100 | description = "Filter flows by specific keyword", |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 101 | required = false, multiValued = true) |
| 102 | private List<String> filter = new ArrayList<>(); |
| 103 | |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 104 | @Option(name = "-r", aliases = "--remove", |
| 105 | description = "Remove flows by specific keyword", |
| 106 | required = false, multiValued = false) |
| 107 | private String remove = null; |
| 108 | |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 109 | private Predicate<FlowEntry> predicate = TRUE_PREDICATE; |
alshabib | 64231d8 | 2014-09-25 18:25:31 -0700 | [diff] [blame] | 110 | |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 111 | private StringFilter contentFilter; |
| 112 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 113 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 114 | protected void execute() { |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 115 | CoreService coreService = get(CoreService.class); |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 116 | DeviceService deviceService = get(DeviceService.class); |
| 117 | FlowRuleService service = get(FlowRuleService.class); |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 118 | contentFilter = new StringFilter(filter, StringFilter.Strategy.AND); |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 119 | |
| 120 | compilePredicate(); |
| 121 | |
Mark | 4c96452 | 2016-03-08 11:26:07 -0800 | [diff] [blame] | 122 | SortedMap<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service, coreService); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 123 | |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 124 | // Remove flows |
| 125 | if (remove != null) { |
| 126 | flows.values().forEach(flowList -> { |
| 127 | if (!remove.isEmpty()) { |
| 128 | filter.add(remove); |
| 129 | contentFilter = new StringFilter(filter, StringFilter.Strategy.AND); |
| 130 | } |
| 131 | if (!filter.isEmpty() || (remove != null && !remove.isEmpty())) { |
| 132 | flowList = filterFlows(flowList); |
| 133 | this.removeFlowsInteractive(flowList, service, coreService); |
| 134 | } |
| 135 | }); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | // Show flows |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 140 | if (outputJson()) { |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 141 | print("%s", json(flows.keySet(), flows)); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 142 | } else { |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 143 | flows.forEach((device, flow) -> printFlows(device, flow, coreService)); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 144 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 145 | } |
| 146 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 147 | /** |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 148 | * Removes the flows passed as argument after confirmation is provided |
| 149 | * for each of them. |
| 150 | * If no explicit confirmation is provided, the flow is not removed. |
| 151 | * |
| 152 | * @param flows list of flows to remove |
| 153 | * @param flowService FlowRuleService object |
| 154 | * @param coreService CoreService object |
| 155 | */ |
| 156 | public void removeFlowsInteractive(Iterable<FlowEntry> flows, |
| 157 | FlowRuleService flowService, CoreService coreService) { |
| 158 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 159 | flows.forEach(flow -> { |
| 160 | ApplicationId appId = coreService.getAppId(flow.appId()); |
| 161 | System.out.print(String.format("Id=%s, AppId=%s. Remove? [y/N]: ", |
| 162 | flow.id(), appId != null ? appId.name() : "<none>")); |
| 163 | String response; |
| 164 | try { |
| 165 | response = br.readLine(); |
| 166 | response = response.trim().replace("\n", ""); |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 167 | if ("y".equals(response)) { |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 168 | flowService.removeFlowRules(flow); |
| 169 | } |
| 170 | } catch (IOException e) { |
| 171 | response = ""; |
| 172 | } |
| 173 | print(response); |
| 174 | }); |
| 175 | } |
| 176 | |
| 177 | /** |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 178 | * Produces a JSON array of flows grouped by the each device. |
| 179 | * |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 180 | * @param devices collection of devices to group flow by |
| 181 | * @param flows collection of flows per each device |
| 182 | * @return JSON array |
| 183 | */ |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 184 | private JsonNode json(Iterable<Device> devices, |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 185 | Map<Device, List<FlowEntry>> flows) { |
| 186 | ObjectMapper mapper = new ObjectMapper(); |
| 187 | ArrayNode result = mapper.createArrayNode(); |
| 188 | for (Device device : devices) { |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 189 | result.add(json(mapper, device, flows.get(device))); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 190 | } |
| 191 | return result; |
| 192 | } |
| 193 | |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 194 | /** |
| 195 | * Compiles a predicate to find matching flows based on the command |
| 196 | * arguments. |
| 197 | */ |
| 198 | private void compilePredicate() { |
| 199 | if (state != null && !state.equals(ANY)) { |
| 200 | final FlowEntryState feState = FlowEntryState.valueOf(state.toUpperCase()); |
| 201 | predicate = predicate.and(f -> f.state().equals(feState)); |
| 202 | } |
| 203 | |
| 204 | if (table != null) { |
| 205 | final int tableId = Integer.parseInt(table); |
| 206 | predicate = predicate.and(f -> f.tableId() == tableId); |
| 207 | } |
| 208 | } |
| 209 | |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 210 | // Produces JSON object with the flows of the given device. |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 211 | private ObjectNode json(ObjectMapper mapper, |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 212 | Device device, List<FlowEntry> flows) { |
| 213 | ObjectNode result = mapper.createObjectNode(); |
| 214 | ArrayNode array = mapper.createArrayNode(); |
| 215 | |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 216 | flows.forEach(flow -> array.add(jsonForEntity(flow, FlowEntry.class))); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 217 | |
| 218 | result.put("device", device.id().toString()) |
| 219 | .put("flowCount", flows.size()) |
Thomas Vachuska | dfe48a7 | 2014-10-16 10:32:08 -0700 | [diff] [blame] | 220 | .set("flows", array); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 221 | return result; |
| 222 | } |
| 223 | |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 224 | /** |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 225 | * Returns the list of devices sorted using the device ID URIs. |
| 226 | * |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 227 | * @param deviceService device service |
| 228 | * @param service flow rule service |
Ray Milkey | d4334db | 2016-04-05 17:39:44 -0700 | [diff] [blame] | 229 | * @param coreService core service |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 230 | * @return sorted device list |
| 231 | */ |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 232 | protected SortedMap<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService, |
Mark | 4c96452 | 2016-03-08 11:26:07 -0800 | [diff] [blame] | 233 | FlowRuleService service, CoreService coreService) { |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 234 | SortedMap<Device, List<FlowEntry>> flows = new TreeMap<>(Comparators.ELEMENT_COMPARATOR); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 235 | List<FlowEntry> rules; |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 236 | |
Saurav Das | 554f5e7 | 2015-10-27 10:28:19 -0700 | [diff] [blame] | 237 | Iterable<Device> devices = null; |
| 238 | if (uri == null) { |
| 239 | devices = deviceService.getDevices(); |
| 240 | } else { |
| 241 | Device dev = deviceService.getDevice(DeviceId.deviceId(uri)); |
| 242 | devices = (dev == null) ? deviceService.getDevices() |
| 243 | : Collections.singletonList(dev); |
| 244 | } |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 245 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 246 | for (Device d : devices) { |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 247 | if (predicate.equals(TRUE_PREDICATE)) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 248 | rules = newArrayList(service.getFlowEntries(d.id())); |
| 249 | } else { |
| 250 | rules = newArrayList(); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 251 | for (FlowEntry f : service.getFlowEntries(d.id())) { |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 252 | if (predicate.test(f)) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 253 | rules.add(f); |
| 254 | } |
| 255 | } |
| 256 | } |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 257 | rules.sort(Comparators.FLOW_RULE_COMPARATOR); |
Mark | 4c96452 | 2016-03-08 11:26:07 -0800 | [diff] [blame] | 258 | |
| 259 | if (suppressCoreOutput) { |
| 260 | short coreAppId = coreService.getAppId("org.onosproject.core").id(); |
| 261 | rules = rules.stream() |
| 262 | .filter(f -> f.appId() != coreAppId) |
| 263 | .collect(Collectors.toList()); |
| 264 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 265 | flows.put(d, rules); |
| 266 | } |
| 267 | return flows; |
| 268 | } |
| 269 | |
| 270 | /** |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 271 | * Filter a given list of flows based on the existing content filter. |
| 272 | * |
| 273 | * @param flows list of flows to filter |
| 274 | * @return further filtered list of flows |
| 275 | */ |
| 276 | private List<FlowEntry> filterFlows(List<FlowEntry> flows) { |
| 277 | return flows.stream(). |
| 278 | filter(f -> contentFilter.filter(f)).collect(Collectors.toList()); |
| 279 | } |
| 280 | |
| 281 | /** |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 282 | * Prints flows. |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 283 | * |
| 284 | * @param d the device |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 285 | * @param flows the set of flows for that device |
| 286 | * @param coreService core system service |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 287 | */ |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 288 | protected void printFlows(Device d, List<FlowEntry> flows, |
| 289 | CoreService coreService) { |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 290 | List<FlowEntry> filteredFlows = filterFlows(flows); |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 291 | boolean empty = filteredFlows == null || filteredFlows.isEmpty(); |
| 292 | print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : filteredFlows.size()); |
Charles Chan | f9b94ab | 2016-02-23 19:31:41 -0800 | [diff] [blame] | 293 | if (empty || countOnly) { |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 294 | return; |
| 295 | } |
| 296 | |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 297 | for (FlowEntry f : filteredFlows) { |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 298 | if (shortOutput) { |
| 299 | print(SHORT_FORMAT, f.state(), f.bytes(), f.packets(), |
Yi Tseng | ac81f5f | 2017-11-14 00:35:43 -0800 | [diff] [blame] | 300 | f.table(), f.priority(), f.selector().criteria(), |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 301 | printTreatment(f.treatment())); |
| 302 | } else { |
Brian O'Connor | 9cc799c | 2015-06-04 19:35:41 -0700 | [diff] [blame] | 303 | ApplicationId appId = coreService.getAppId(f.appId()); |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 304 | print(LONG_FORMAT, Long.toHexString(f.id().value()), f.state(), |
Yi Tseng | ac81f5f | 2017-11-14 00:35:43 -0800 | [diff] [blame] | 305 | f.bytes(), f.packets(), f.life(), f.liveType(), f.priority(), f.table(), |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 306 | appId != null ? appId.name() : "<none>", |
Yuta HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame] | 307 | f.payLoad() == null ? null : Arrays.toString(f.payLoad().payLoad()), |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 308 | f.selector().criteria(), f.treatment()); |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 309 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 310 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 313 | private String printTreatment(TrafficTreatment treatment) { |
| 314 | final String delimiter = ", "; |
| 315 | StringBuilder builder = new StringBuilder("["); |
| 316 | if (!treatment.immediate().isEmpty()) { |
| 317 | builder.append("immediate=" + treatment.immediate() + delimiter); |
| 318 | } |
| 319 | if (!treatment.deferred().isEmpty()) { |
| 320 | builder.append("deferred=" + treatment.deferred() + delimiter); |
| 321 | } |
| 322 | if (treatment.clearedDeferred()) { |
| 323 | builder.append("clearDeferred" + delimiter); |
| 324 | } |
| 325 | if (treatment.tableTransition() != null) { |
| 326 | builder.append("transition=" + treatment.tableTransition() + delimiter); |
| 327 | } |
| 328 | if (treatment.metered() != null) { |
| 329 | builder.append("meter=" + treatment.metered() + delimiter); |
| 330 | } |
| 331 | if (treatment.writeMetadata() != null) { |
| 332 | builder.append("metadata=" + treatment.writeMetadata() + delimiter); |
| 333 | } |
| 334 | // Chop off last delimiter |
| 335 | builder.replace(builder.length() - delimiter.length(), builder.length(), ""); |
| 336 | builder.append("]"); |
| 337 | return builder.toString(); |
| 338 | } |
Yuta HIGUCHI | e76a24d | 2014-09-27 00:48:34 -0700 | [diff] [blame] | 339 | } |