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