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