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