blob: 659dd9604c73b39809783de66e21b633dc6824b7 [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;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.cli.AbstractShellCommand;
Ray Milkeyc7477292016-03-11 10:53:43 -080026import org.onosproject.utils.Comparators;
Brian O'Connor9cc799c2015-06-04 19:35:41 -070027import org.onosproject.core.ApplicationId;
Ray Milkey3078fc02015-05-06 16:14:14 -070028import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.Device;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.device.DeviceService;
32import org.onosproject.net.flow.FlowEntry;
33import org.onosproject.net.flow.FlowEntry.FlowEntryState;
34import org.onosproject.net.flow.FlowRuleService;
Jonathan Hartc7840bd2016-01-21 23:26:29 -080035import org.onosproject.net.flow.TrafficTreatment;
alshabib99b8fdc2014-09-25 14:30:22 -070036
Jonathan Hartc7840bd2016-01-21 23:26:29 -080037import java.util.Collections;
38import java.util.List;
39import java.util.Map;
40import java.util.SortedMap;
41import java.util.TreeMap;
42import java.util.function.Predicate;
Mark4c964522016-03-08 11:26:07 -080043import java.util.stream.Collectors;
Jonathan Hartc7840bd2016-01-21 23:26:29 -080044
45import static com.google.common.collect.Lists.newArrayList;
toma6897792014-10-08 22:21:05 -070046
Mark4c964522016-03-08 11:26:07 -080047
alshabib9290eea2014-09-22 11:58:17 -070048/**
jccf988bf52015-05-05 19:02:50 +080049 * Lists all currently-known flows.
alshabib9290eea2014-09-22 11:58:17 -070050 */
51@Command(scope = "onos", name = "flows",
toma6897792014-10-08 22:21:05 -070052 description = "Lists all currently-known flows.")
alshabib9290eea2014-09-22 11:58:17 -070053public class FlowsListCommand extends AbstractShellCommand {
54
Jonathan Hartc7840bd2016-01-21 23:26:29 -080055 private static final Predicate<FlowEntry> TRUE_PREDICATE = f -> true;
56
alshabib144a2942014-09-25 18:44:02 -070057 public static final String ANY = "any";
58
Jonathan Hartc7840bd2016-01-21 23:26:29 -080059 private static final String LONG_FORMAT = " id=%s, state=%s, bytes=%s, "
60 + "packets=%s, duration=%s, priority=%s, tableId=%s, appId=%s, "
61 + "payLoad=%s, selector=%s, treatment=%s";
62
63 private static final String SHORT_FORMAT = " %s, bytes=%s, packets=%s, "
64 + "table=%s, priority=%s, selector=%s, treatment=%s";
65
66 @Argument(index = 0, name = "state", description = "Flow Rule state",
67 required = false, multiValued = false)
68 String state = null;
alshabib99b8fdc2014-09-25 14:30:22 -070069
alshabib144a2942014-09-25 18:44:02 -070070 @Argument(index = 1, name = "uri", description = "Device ID",
toma6897792014-10-08 22:21:05 -070071 required = false, multiValued = false)
alshabib99b8fdc2014-09-25 14:30:22 -070072 String uri = null;
alshabib9290eea2014-09-22 11:58:17 -070073
Jonathan Hartc7840bd2016-01-21 23:26:29 -080074 @Argument(index = 2, name = "table", description = "Table ID",
75 required = false, multiValued = false)
76 String table = null;
77
78 @Option(name = "-s", aliases = "--short",
79 description = "Print more succinct output for each flow",
80 required = false, multiValued = false)
81 private boolean shortOutput = false;
82
Mark4c964522016-03-08 11:26:07 -080083 @Option(name = "-n", aliases = "--no-core-flows",
84 description = "Suppress core flows from output",
85 required = false, multiValued = false)
86 private boolean suppressCoreOutput = false;
87
Charles Chanf9b94ab2016-02-23 19:31:41 -080088 @Option(name = "-c", aliases = "--count",
89 description = "Print flow count only",
90 required = false, multiValued = false)
91 private boolean countOnly = false;
92
Jonathan Hartc7840bd2016-01-21 23:26:29 -080093 private Predicate<FlowEntry> predicate = TRUE_PREDICATE;
alshabib64231d82014-09-25 18:25:31 -070094
alshabib9290eea2014-09-22 11:58:17 -070095 @Override
tom0872a172014-09-23 11:24:26 -070096 protected void execute() {
toma6897792014-10-08 22:21:05 -070097 CoreService coreService = get(CoreService.class);
tomcaf3bf72014-09-23 13:20:53 -070098 DeviceService deviceService = get(DeviceService.class);
99 FlowRuleService service = get(FlowRuleService.class);
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800100
101 compilePredicate();
102
Mark4c964522016-03-08 11:26:07 -0800103 SortedMap<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service, coreService);
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700104
105 if (outputJson()) {
Ray Milkey3078fc02015-05-06 16:14:14 -0700106 print("%s", json(flows.keySet(), flows));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700107 } else {
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800108 flows.forEach((device, flow) -> printFlows(device, flow, coreService));
alshabib9290eea2014-09-22 11:58:17 -0700109 }
alshabib9290eea2014-09-22 11:58:17 -0700110 }
111
alshabib9290eea2014-09-22 11:58:17 -0700112 /**
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700113 * Produces a JSON array of flows grouped by the each device.
114 *
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700115 * @param devices collection of devices to group flow by
116 * @param flows collection of flows per each device
117 * @return JSON array
118 */
Ray Milkey3078fc02015-05-06 16:14:14 -0700119 private JsonNode json(Iterable<Device> devices,
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700120 Map<Device, List<FlowEntry>> flows) {
121 ObjectMapper mapper = new ObjectMapper();
122 ArrayNode result = mapper.createArrayNode();
123 for (Device device : devices) {
Ray Milkey3078fc02015-05-06 16:14:14 -0700124 result.add(json(mapper, device, flows.get(device)));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700125 }
126 return result;
127 }
128
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800129 /**
130 * Compiles a predicate to find matching flows based on the command
131 * arguments.
132 */
133 private void compilePredicate() {
134 if (state != null && !state.equals(ANY)) {
135 final FlowEntryState feState = FlowEntryState.valueOf(state.toUpperCase());
136 predicate = predicate.and(f -> f.state().equals(feState));
137 }
138
139 if (table != null) {
140 final int tableId = Integer.parseInt(table);
141 predicate = predicate.and(f -> f.tableId() == tableId);
142 }
143 }
144
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700145 // Produces JSON object with the flows of the given device.
Ray Milkey3078fc02015-05-06 16:14:14 -0700146 private ObjectNode json(ObjectMapper mapper,
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700147 Device device, List<FlowEntry> flows) {
148 ObjectNode result = mapper.createObjectNode();
149 ArrayNode array = mapper.createArrayNode();
150
Ray Milkey3078fc02015-05-06 16:14:14 -0700151 flows.forEach(flow -> array.add(jsonForEntity(flow, FlowEntry.class)));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700152
153 result.put("device", device.id().toString())
154 .put("flowCount", flows.size())
Thomas Vachuskadfe48a72014-10-16 10:32:08 -0700155 .set("flows", array);
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700156 return result;
157 }
158
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700159 /**
alshabib9290eea2014-09-22 11:58:17 -0700160 * Returns the list of devices sorted using the device ID URIs.
161 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800162 * @param deviceService device service
163 * @param service flow rule service
Ray Milkeyd4334db2016-04-05 17:39:44 -0700164 * @param coreService core service
alshabib9290eea2014-09-22 11:58:17 -0700165 * @return sorted device list
166 */
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800167 protected SortedMap<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService,
Mark4c964522016-03-08 11:26:07 -0800168 FlowRuleService service, CoreService coreService) {
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800169 SortedMap<Device, List<FlowEntry>> flows = new TreeMap<>(Comparators.ELEMENT_COMPARATOR);
alshabib1c319ff2014-10-04 20:29:09 -0700170 List<FlowEntry> rules;
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800171
Saurav Das554f5e72015-10-27 10:28:19 -0700172 Iterable<Device> devices = null;
173 if (uri == null) {
174 devices = deviceService.getDevices();
175 } else {
176 Device dev = deviceService.getDevice(DeviceId.deviceId(uri));
177 devices = (dev == null) ? deviceService.getDevices()
178 : Collections.singletonList(dev);
179 }
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800180
alshabib99b8fdc2014-09-25 14:30:22 -0700181 for (Device d : devices) {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800182 if (predicate.equals(TRUE_PREDICATE)) {
alshabib144a2942014-09-25 18:44:02 -0700183 rules = newArrayList(service.getFlowEntries(d.id()));
184 } else {
185 rules = newArrayList();
alshabib1c319ff2014-10-04 20:29:09 -0700186 for (FlowEntry f : service.getFlowEntries(d.id())) {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800187 if (predicate.test(f)) {
alshabib144a2942014-09-25 18:44:02 -0700188 rules.add(f);
189 }
190 }
191 }
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800192 rules.sort(Comparators.FLOW_RULE_COMPARATOR);
Mark4c964522016-03-08 11:26:07 -0800193
194 if (suppressCoreOutput) {
195 short coreAppId = coreService.getAppId("org.onosproject.core").id();
196 rules = rules.stream()
197 .filter(f -> f.appId() != coreAppId)
198 .collect(Collectors.toList());
199 }
alshabib9290eea2014-09-22 11:58:17 -0700200 flows.put(d, rules);
201 }
202 return flows;
203 }
204
205 /**
206 * Prints flows.
toma6897792014-10-08 22:21:05 -0700207 *
208 * @param d the device
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800209 * @param flows the set of flows for that device
210 * @param coreService core system service
alshabib9290eea2014-09-22 11:58:17 -0700211 */
toma6897792014-10-08 22:21:05 -0700212 protected void printFlows(Device d, List<FlowEntry> flows,
213 CoreService coreService) {
tom1dd08e42014-10-07 11:40:00 -0700214 boolean empty = flows == null || flows.isEmpty();
215 print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : flows.size());
Charles Chanf9b94ab2016-02-23 19:31:41 -0800216 if (empty || countOnly) {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800217 return;
218 }
219
220 for (FlowEntry f : flows) {
221 if (shortOutput) {
222 print(SHORT_FORMAT, f.state(), f.bytes(), f.packets(),
223 f.tableId(), f.priority(), f.selector().criteria(),
224 printTreatment(f.treatment()));
225 } else {
Brian O'Connor9cc799c2015-06-04 19:35:41 -0700226 ApplicationId appId = coreService.getAppId(f.appId());
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800227 print(LONG_FORMAT, Long.toHexString(f.id().value()), f.state(),
228 f.bytes(), f.packets(), f.life(), f.priority(), f.tableId(),
229 appId != null ? appId.name() : "<none>",
230 f.payLoad() == null ? null : f.payLoad().payLoad().toString(),
231 f.selector().criteria(), f.treatment());
tom1dd08e42014-10-07 11:40:00 -0700232 }
alshabib99b8fdc2014-09-25 14:30:22 -0700233 }
alshabib9290eea2014-09-22 11:58:17 -0700234 }
235
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800236 private String printTreatment(TrafficTreatment treatment) {
237 final String delimiter = ", ";
238 StringBuilder builder = new StringBuilder("[");
239 if (!treatment.immediate().isEmpty()) {
240 builder.append("immediate=" + treatment.immediate() + delimiter);
241 }
242 if (!treatment.deferred().isEmpty()) {
243 builder.append("deferred=" + treatment.deferred() + delimiter);
244 }
245 if (treatment.clearedDeferred()) {
246 builder.append("clearDeferred" + delimiter);
247 }
248 if (treatment.tableTransition() != null) {
249 builder.append("transition=" + treatment.tableTransition() + delimiter);
250 }
251 if (treatment.metered() != null) {
252 builder.append("meter=" + treatment.metered() + delimiter);
253 }
254 if (treatment.writeMetadata() != null) {
255 builder.append("metadata=" + treatment.writeMetadata() + delimiter);
256 }
257 // Chop off last delimiter
258 builder.replace(builder.length() - delimiter.length(), builder.length(), "");
259 builder.append("]");
260 return builder.toString();
261 }
262
Yuta HIGUCHIe76a24d2014-09-27 00:48:34 -0700263}