blob: 9de96f924791346011a1fc231352fc6394f38794 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska7d693f52014-10-21 19:17:57 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
alshabib9290eea2014-09-22 11:58:17 -070017
Jonathan Hartc7840bd2016-01-21 23:26:29 -080018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
alshabib99b8fdc2014-09-25 14:30:22 -070022import org.apache.karaf.shell.commands.Argument;
23import org.apache.karaf.shell.commands.Command;
Jonathan Hartc7840bd2016-01-21 23:26:29 -080024import org.apache.karaf.shell.commands.Option;
Carolina Fernandezfa56d142016-11-14 01:13:26 +010025import org.onlab.util.StringFilter;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.cli.AbstractShellCommand;
Ray Milkeyc7477292016-03-11 10:53:43 -080027import org.onosproject.utils.Comparators;
Brian O'Connor9cc799c2015-06-04 19:35:41 -070028import org.onosproject.core.ApplicationId;
Ray Milkey3078fc02015-05-06 16:14:14 -070029import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.Device;
31import org.onosproject.net.DeviceId;
32import org.onosproject.net.device.DeviceService;
33import org.onosproject.net.flow.FlowEntry;
34import org.onosproject.net.flow.FlowEntry.FlowEntryState;
35import org.onosproject.net.flow.FlowRuleService;
Jonathan Hartc7840bd2016-01-21 23:26:29 -080036import org.onosproject.net.flow.TrafficTreatment;
alshabib99b8fdc2014-09-25 14:30:22 -070037
Carolina Fernandezfa56d142016-11-14 01:13:26 +010038import java.util.ArrayList;
Jonathan Hartc7840bd2016-01-21 23:26:29 -080039import java.util.Collections;
40import java.util.List;
41import java.util.Map;
42import java.util.SortedMap;
43import java.util.TreeMap;
44import java.util.function.Predicate;
Mark4c964522016-03-08 11:26:07 -080045import java.util.stream.Collectors;
Jonathan Hartc7840bd2016-01-21 23:26:29 -080046
47import static com.google.common.collect.Lists.newArrayList;
toma6897792014-10-08 22:21:05 -070048
Mark4c964522016-03-08 11:26:07 -080049
alshabib9290eea2014-09-22 11:58:17 -070050/**
jccf988bf52015-05-05 19:02:50 +080051 * Lists all currently-known flows.
alshabib9290eea2014-09-22 11:58:17 -070052 */
53@Command(scope = "onos", name = "flows",
toma6897792014-10-08 22:21:05 -070054 description = "Lists all currently-known flows.")
alshabib9290eea2014-09-22 11:58:17 -070055public class FlowsListCommand extends AbstractShellCommand {
56
Jonathan Hartc7840bd2016-01-21 23:26:29 -080057 private static final Predicate<FlowEntry> TRUE_PREDICATE = f -> true;
58
alshabib144a2942014-09-25 18:44:02 -070059 public static final String ANY = "any";
60
Jonathan Hartc7840bd2016-01-21 23:26:29 -080061 private static final String LONG_FORMAT = " id=%s, state=%s, bytes=%s, "
62 + "packets=%s, duration=%s, priority=%s, tableId=%s, appId=%s, "
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;
alshabib99b8fdc2014-09-25 14:30:22 -070071
alshabib144a2942014-09-25 18:44:02 -070072 @Argument(index = 1, name = "uri", description = "Device ID",
toma6897792014-10-08 22:21:05 -070073 required = false, multiValued = false)
alshabib99b8fdc2014-09-25 14:30:22 -070074 String uri = null;
alshabib9290eea2014-09-22 11:58:17 -070075
Jonathan Hartc7840bd2016-01-21 23:26:29 -080076 @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
Mark4c964522016-03-08 11:26:07 -080085 @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 Chanf9b94ab2016-02-23 19:31:41 -080090 @Option(name = "-c", aliases = "--count",
91 description = "Print flow count only",
92 required = false, multiValued = false)
93 private boolean countOnly = false;
94
Carolina Fernandezfa56d142016-11-14 01:13:26 +010095 @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 Hartc7840bd2016-01-21 23:26:29 -0800100 private Predicate<FlowEntry> predicate = TRUE_PREDICATE;
alshabib64231d82014-09-25 18:25:31 -0700101
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100102 private StringFilter contentFilter;
103
alshabib9290eea2014-09-22 11:58:17 -0700104 @Override
tom0872a172014-09-23 11:24:26 -0700105 protected void execute() {
toma6897792014-10-08 22:21:05 -0700106 CoreService coreService = get(CoreService.class);
tomcaf3bf72014-09-23 13:20:53 -0700107 DeviceService deviceService = get(DeviceService.class);
108 FlowRuleService service = get(FlowRuleService.class);
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100109 contentFilter = new StringFilter(filter, StringFilter.Strategy.AND);
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800110
111 compilePredicate();
112
Mark4c964522016-03-08 11:26:07 -0800113 SortedMap<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service, coreService);
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700114
115 if (outputJson()) {
Ray Milkey3078fc02015-05-06 16:14:14 -0700116 print("%s", json(flows.keySet(), flows));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700117 } else {
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800118 flows.forEach((device, flow) -> printFlows(device, flow, coreService));
alshabib9290eea2014-09-22 11:58:17 -0700119 }
alshabib9290eea2014-09-22 11:58:17 -0700120 }
121
alshabib9290eea2014-09-22 11:58:17 -0700122 /**
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700123 * Produces a JSON array of flows grouped by the each device.
124 *
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700125 * @param devices collection of devices to group flow by
126 * @param flows collection of flows per each device
127 * @return JSON array
128 */
Ray Milkey3078fc02015-05-06 16:14:14 -0700129 private JsonNode json(Iterable<Device> devices,
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700130 Map<Device, List<FlowEntry>> flows) {
131 ObjectMapper mapper = new ObjectMapper();
132 ArrayNode result = mapper.createArrayNode();
133 for (Device device : devices) {
Ray Milkey3078fc02015-05-06 16:14:14 -0700134 result.add(json(mapper, device, flows.get(device)));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700135 }
136 return result;
137 }
138
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800139 /**
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 Vachuskabb0272e2014-10-16 09:32:04 -0700155 // Produces JSON object with the flows of the given device.
Ray Milkey3078fc02015-05-06 16:14:14 -0700156 private ObjectNode json(ObjectMapper mapper,
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700157 Device device, List<FlowEntry> flows) {
158 ObjectNode result = mapper.createObjectNode();
159 ArrayNode array = mapper.createArrayNode();
160
Ray Milkey3078fc02015-05-06 16:14:14 -0700161 flows.forEach(flow -> array.add(jsonForEntity(flow, FlowEntry.class)));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700162
163 result.put("device", device.id().toString())
164 .put("flowCount", flows.size())
Thomas Vachuskadfe48a72014-10-16 10:32:08 -0700165 .set("flows", array);
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700166 return result;
167 }
168
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700169 /**
alshabib9290eea2014-09-22 11:58:17 -0700170 * Returns the list of devices sorted using the device ID URIs.
171 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800172 * @param deviceService device service
173 * @param service flow rule service
Ray Milkeyd4334db2016-04-05 17:39:44 -0700174 * @param coreService core service
alshabib9290eea2014-09-22 11:58:17 -0700175 * @return sorted device list
176 */
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800177 protected SortedMap<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService,
Mark4c964522016-03-08 11:26:07 -0800178 FlowRuleService service, CoreService coreService) {
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800179 SortedMap<Device, List<FlowEntry>> flows = new TreeMap<>(Comparators.ELEMENT_COMPARATOR);
alshabib1c319ff2014-10-04 20:29:09 -0700180 List<FlowEntry> rules;
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800181
Saurav Das554f5e72015-10-27 10:28:19 -0700182 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 Hartc7840bd2016-01-21 23:26:29 -0800190
alshabib99b8fdc2014-09-25 14:30:22 -0700191 for (Device d : devices) {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800192 if (predicate.equals(TRUE_PREDICATE)) {
alshabib144a2942014-09-25 18:44:02 -0700193 rules = newArrayList(service.getFlowEntries(d.id()));
194 } else {
195 rules = newArrayList();
alshabib1c319ff2014-10-04 20:29:09 -0700196 for (FlowEntry f : service.getFlowEntries(d.id())) {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800197 if (predicate.test(f)) {
alshabib144a2942014-09-25 18:44:02 -0700198 rules.add(f);
199 }
200 }
201 }
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800202 rules.sort(Comparators.FLOW_RULE_COMPARATOR);
Mark4c964522016-03-08 11:26:07 -0800203
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 }
alshabib9290eea2014-09-22 11:58:17 -0700210 flows.put(d, rules);
211 }
212 return flows;
213 }
214
215 /**
216 * Prints flows.
toma6897792014-10-08 22:21:05 -0700217 *
218 * @param d the device
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800219 * @param flows the set of flows for that device
220 * @param coreService core system service
alshabib9290eea2014-09-22 11:58:17 -0700221 */
toma6897792014-10-08 22:21:05 -0700222 protected void printFlows(Device d, List<FlowEntry> flows,
223 CoreService coreService) {
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100224 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 Chanf9b94ab2016-02-23 19:31:41 -0800228 if (empty || countOnly) {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800229 return;
230 }
231
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100232 for (FlowEntry f : filteredFlows) {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800233 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'Connor9cc799c2015-06-04 19:35:41 -0700238 ApplicationId appId = coreService.getAppId(f.appId());
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800239 print(LONG_FORMAT, Long.toHexString(f.id().value()), f.state(),
240 f.bytes(), f.packets(), f.life(), f.priority(), f.tableId(),
241 appId != null ? appId.name() : "<none>",
242 f.payLoad() == null ? null : f.payLoad().payLoad().toString(),
243 f.selector().criteria(), f.treatment());
tom1dd08e42014-10-07 11:40:00 -0700244 }
alshabib99b8fdc2014-09-25 14:30:22 -0700245 }
alshabib9290eea2014-09-22 11:58:17 -0700246 }
247
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800248 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 HIGUCHIe76a24d2014-09-27 00:48:34 -0700275}