blob: 2480131db90a474376dd9995ceb67cff5e512b38 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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;
26import org.onosproject.cli.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;
43
44import static com.google.common.collect.Lists.newArrayList;
toma6897792014-10-08 22:21:05 -070045
alshabib9290eea2014-09-22 11:58:17 -070046/**
jccf988bf52015-05-05 19:02:50 +080047 * Lists all currently-known flows.
alshabib9290eea2014-09-22 11:58:17 -070048 */
49@Command(scope = "onos", name = "flows",
toma6897792014-10-08 22:21:05 -070050 description = "Lists all currently-known flows.")
alshabib9290eea2014-09-22 11:58:17 -070051public class FlowsListCommand extends AbstractShellCommand {
52
Jonathan Hartc7840bd2016-01-21 23:26:29 -080053 private static final Predicate<FlowEntry> TRUE_PREDICATE = f -> true;
54
alshabib144a2942014-09-25 18:44:02 -070055 public static final String ANY = "any";
56
Jonathan Hartc7840bd2016-01-21 23:26:29 -080057 private static final String LONG_FORMAT = " id=%s, state=%s, bytes=%s, "
58 + "packets=%s, duration=%s, priority=%s, tableId=%s, appId=%s, "
59 + "payLoad=%s, selector=%s, treatment=%s";
60
61 private static final String SHORT_FORMAT = " %s, bytes=%s, packets=%s, "
62 + "table=%s, priority=%s, selector=%s, treatment=%s";
63
64 @Argument(index = 0, name = "state", description = "Flow Rule state",
65 required = false, multiValued = false)
66 String state = null;
alshabib99b8fdc2014-09-25 14:30:22 -070067
alshabib144a2942014-09-25 18:44:02 -070068 @Argument(index = 1, name = "uri", description = "Device ID",
toma6897792014-10-08 22:21:05 -070069 required = false, multiValued = false)
alshabib99b8fdc2014-09-25 14:30:22 -070070 String uri = null;
alshabib9290eea2014-09-22 11:58:17 -070071
Jonathan Hartc7840bd2016-01-21 23:26:29 -080072 @Argument(index = 2, name = "table", description = "Table ID",
73 required = false, multiValued = false)
74 String table = null;
75
76 @Option(name = "-s", aliases = "--short",
77 description = "Print more succinct output for each flow",
78 required = false, multiValued = false)
79 private boolean shortOutput = false;
80
Charles Chanf9b94ab2016-02-23 19:31:41 -080081 @Option(name = "-c", aliases = "--count",
82 description = "Print flow count only",
83 required = false, multiValued = false)
84 private boolean countOnly = false;
85
Jonathan Hartc7840bd2016-01-21 23:26:29 -080086 private Predicate<FlowEntry> predicate = TRUE_PREDICATE;
alshabib64231d82014-09-25 18:25:31 -070087
alshabib9290eea2014-09-22 11:58:17 -070088 @Override
tom0872a172014-09-23 11:24:26 -070089 protected void execute() {
toma6897792014-10-08 22:21:05 -070090 CoreService coreService = get(CoreService.class);
tomcaf3bf72014-09-23 13:20:53 -070091 DeviceService deviceService = get(DeviceService.class);
92 FlowRuleService service = get(FlowRuleService.class);
Jonathan Hartc7840bd2016-01-21 23:26:29 -080093
94 compilePredicate();
95
Yuta HIGUCHI8791a812015-02-10 09:43:52 -080096 SortedMap<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service);
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070097
98 if (outputJson()) {
Ray Milkey3078fc02015-05-06 16:14:14 -070099 print("%s", json(flows.keySet(), flows));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700100 } else {
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800101 flows.forEach((device, flow) -> printFlows(device, flow, coreService));
alshabib9290eea2014-09-22 11:58:17 -0700102 }
alshabib9290eea2014-09-22 11:58:17 -0700103 }
104
alshabib9290eea2014-09-22 11:58:17 -0700105 /**
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700106 * Produces a JSON array of flows grouped by the each device.
107 *
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700108 * @param devices collection of devices to group flow by
109 * @param flows collection of flows per each device
110 * @return JSON array
111 */
Ray Milkey3078fc02015-05-06 16:14:14 -0700112 private JsonNode json(Iterable<Device> devices,
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700113 Map<Device, List<FlowEntry>> flows) {
114 ObjectMapper mapper = new ObjectMapper();
115 ArrayNode result = mapper.createArrayNode();
116 for (Device device : devices) {
Ray Milkey3078fc02015-05-06 16:14:14 -0700117 result.add(json(mapper, device, flows.get(device)));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700118 }
119 return result;
120 }
121
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800122 /**
123 * Compiles a predicate to find matching flows based on the command
124 * arguments.
125 */
126 private void compilePredicate() {
127 if (state != null && !state.equals(ANY)) {
128 final FlowEntryState feState = FlowEntryState.valueOf(state.toUpperCase());
129 predicate = predicate.and(f -> f.state().equals(feState));
130 }
131
132 if (table != null) {
133 final int tableId = Integer.parseInt(table);
134 predicate = predicate.and(f -> f.tableId() == tableId);
135 }
136 }
137
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700138 // Produces JSON object with the flows of the given device.
Ray Milkey3078fc02015-05-06 16:14:14 -0700139 private ObjectNode json(ObjectMapper mapper,
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700140 Device device, List<FlowEntry> flows) {
141 ObjectNode result = mapper.createObjectNode();
142 ArrayNode array = mapper.createArrayNode();
143
Ray Milkey3078fc02015-05-06 16:14:14 -0700144 flows.forEach(flow -> array.add(jsonForEntity(flow, FlowEntry.class)));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700145
146 result.put("device", device.id().toString())
147 .put("flowCount", flows.size())
Thomas Vachuskadfe48a72014-10-16 10:32:08 -0700148 .set("flows", array);
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700149 return result;
150 }
151
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700152 /**
alshabib9290eea2014-09-22 11:58:17 -0700153 * Returns the list of devices sorted using the device ID URIs.
154 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800155 * @param deviceService device service
156 * @param service flow rule service
alshabib9290eea2014-09-22 11:58:17 -0700157 * @return sorted device list
158 */
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800159 protected SortedMap<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService,
tom1dd08e42014-10-07 11:40:00 -0700160 FlowRuleService service) {
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800161 SortedMap<Device, List<FlowEntry>> flows = new TreeMap<>(Comparators.ELEMENT_COMPARATOR);
alshabib1c319ff2014-10-04 20:29:09 -0700162 List<FlowEntry> rules;
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800163
Saurav Das554f5e72015-10-27 10:28:19 -0700164 Iterable<Device> devices = null;
165 if (uri == null) {
166 devices = deviceService.getDevices();
167 } else {
168 Device dev = deviceService.getDevice(DeviceId.deviceId(uri));
169 devices = (dev == null) ? deviceService.getDevices()
170 : Collections.singletonList(dev);
171 }
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800172
alshabib99b8fdc2014-09-25 14:30:22 -0700173 for (Device d : devices) {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800174 if (predicate.equals(TRUE_PREDICATE)) {
alshabib144a2942014-09-25 18:44:02 -0700175 rules = newArrayList(service.getFlowEntries(d.id()));
176 } else {
177 rules = newArrayList();
alshabib1c319ff2014-10-04 20:29:09 -0700178 for (FlowEntry f : service.getFlowEntries(d.id())) {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800179 if (predicate.test(f)) {
alshabib144a2942014-09-25 18:44:02 -0700180 rules.add(f);
181 }
182 }
183 }
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800184 rules.sort(Comparators.FLOW_RULE_COMPARATOR);
alshabib9290eea2014-09-22 11:58:17 -0700185 flows.put(d, rules);
186 }
187 return flows;
188 }
189
190 /**
191 * Prints flows.
toma6897792014-10-08 22:21:05 -0700192 *
193 * @param d the device
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800194 * @param flows the set of flows for that device
195 * @param coreService core system service
alshabib9290eea2014-09-22 11:58:17 -0700196 */
toma6897792014-10-08 22:21:05 -0700197 protected void printFlows(Device d, List<FlowEntry> flows,
198 CoreService coreService) {
tom1dd08e42014-10-07 11:40:00 -0700199 boolean empty = flows == null || flows.isEmpty();
200 print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : flows.size());
Charles Chanf9b94ab2016-02-23 19:31:41 -0800201 if (empty || countOnly) {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800202 return;
203 }
204
205 for (FlowEntry f : flows) {
206 if (shortOutput) {
207 print(SHORT_FORMAT, f.state(), f.bytes(), f.packets(),
208 f.tableId(), f.priority(), f.selector().criteria(),
209 printTreatment(f.treatment()));
210 } else {
Brian O'Connor9cc799c2015-06-04 19:35:41 -0700211 ApplicationId appId = coreService.getAppId(f.appId());
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800212 print(LONG_FORMAT, Long.toHexString(f.id().value()), f.state(),
213 f.bytes(), f.packets(), f.life(), f.priority(), f.tableId(),
214 appId != null ? appId.name() : "<none>",
215 f.payLoad() == null ? null : f.payLoad().payLoad().toString(),
216 f.selector().criteria(), f.treatment());
tom1dd08e42014-10-07 11:40:00 -0700217 }
alshabib99b8fdc2014-09-25 14:30:22 -0700218 }
alshabib9290eea2014-09-22 11:58:17 -0700219 }
220
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800221 private String printTreatment(TrafficTreatment treatment) {
222 final String delimiter = ", ";
223 StringBuilder builder = new StringBuilder("[");
224 if (!treatment.immediate().isEmpty()) {
225 builder.append("immediate=" + treatment.immediate() + delimiter);
226 }
227 if (!treatment.deferred().isEmpty()) {
228 builder.append("deferred=" + treatment.deferred() + delimiter);
229 }
230 if (treatment.clearedDeferred()) {
231 builder.append("clearDeferred" + delimiter);
232 }
233 if (treatment.tableTransition() != null) {
234 builder.append("transition=" + treatment.tableTransition() + delimiter);
235 }
236 if (treatment.metered() != null) {
237 builder.append("meter=" + treatment.metered() + delimiter);
238 }
239 if (treatment.writeMetadata() != null) {
240 builder.append("metadata=" + treatment.writeMetadata() + delimiter);
241 }
242 // Chop off last delimiter
243 builder.replace(builder.length() - delimiter.length(), builder.length(), "");
244 builder.append("]");
245 return builder.toString();
246 }
247
Yuta HIGUCHIe76a24d2014-09-27 00:48:34 -0700248}