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