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