blob: b6477c33b6cfc817bb162d3aaaed6f4fe1f408d1 [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
81 private Predicate<FlowEntry> predicate = TRUE_PREDICATE;
alshabib64231d82014-09-25 18:25:31 -070082
alshabib9290eea2014-09-22 11:58:17 -070083 @Override
tom0872a172014-09-23 11:24:26 -070084 protected void execute() {
toma6897792014-10-08 22:21:05 -070085 CoreService coreService = get(CoreService.class);
tomcaf3bf72014-09-23 13:20:53 -070086 DeviceService deviceService = get(DeviceService.class);
87 FlowRuleService service = get(FlowRuleService.class);
Jonathan Hartc7840bd2016-01-21 23:26:29 -080088
89 compilePredicate();
90
Yuta HIGUCHI8791a812015-02-10 09:43:52 -080091 SortedMap<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service);
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070092
93 if (outputJson()) {
Ray Milkey3078fc02015-05-06 16:14:14 -070094 print("%s", json(flows.keySet(), flows));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070095 } else {
Yuta HIGUCHI8791a812015-02-10 09:43:52 -080096 flows.forEach((device, flow) -> printFlows(device, flow, coreService));
alshabib9290eea2014-09-22 11:58:17 -070097 }
alshabib9290eea2014-09-22 11:58:17 -070098 }
99
alshabib9290eea2014-09-22 11:58:17 -0700100 /**
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700101 * Produces a JSON array of flows grouped by the each device.
102 *
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700103 * @param devices collection of devices to group flow by
104 * @param flows collection of flows per each device
105 * @return JSON array
106 */
Ray Milkey3078fc02015-05-06 16:14:14 -0700107 private JsonNode json(Iterable<Device> devices,
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700108 Map<Device, List<FlowEntry>> flows) {
109 ObjectMapper mapper = new ObjectMapper();
110 ArrayNode result = mapper.createArrayNode();
111 for (Device device : devices) {
Ray Milkey3078fc02015-05-06 16:14:14 -0700112 result.add(json(mapper, device, flows.get(device)));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700113 }
114 return result;
115 }
116
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800117 /**
118 * Compiles a predicate to find matching flows based on the command
119 * arguments.
120 */
121 private void compilePredicate() {
122 if (state != null && !state.equals(ANY)) {
123 final FlowEntryState feState = FlowEntryState.valueOf(state.toUpperCase());
124 predicate = predicate.and(f -> f.state().equals(feState));
125 }
126
127 if (table != null) {
128 final int tableId = Integer.parseInt(table);
129 predicate = predicate.and(f -> f.tableId() == tableId);
130 }
131 }
132
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700133 // Produces JSON object with the flows of the given device.
Ray Milkey3078fc02015-05-06 16:14:14 -0700134 private ObjectNode json(ObjectMapper mapper,
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700135 Device device, List<FlowEntry> flows) {
136 ObjectNode result = mapper.createObjectNode();
137 ArrayNode array = mapper.createArrayNode();
138
Ray Milkey3078fc02015-05-06 16:14:14 -0700139 flows.forEach(flow -> array.add(jsonForEntity(flow, FlowEntry.class)));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700140
141 result.put("device", device.id().toString())
142 .put("flowCount", flows.size())
Thomas Vachuskadfe48a72014-10-16 10:32:08 -0700143 .set("flows", array);
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700144 return result;
145 }
146
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700147 /**
alshabib9290eea2014-09-22 11:58:17 -0700148 * Returns the list of devices sorted using the device ID URIs.
149 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800150 * @param deviceService device service
151 * @param service flow rule service
alshabib9290eea2014-09-22 11:58:17 -0700152 * @return sorted device list
153 */
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800154 protected SortedMap<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService,
tom1dd08e42014-10-07 11:40:00 -0700155 FlowRuleService service) {
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800156 SortedMap<Device, List<FlowEntry>> flows = new TreeMap<>(Comparators.ELEMENT_COMPARATOR);
alshabib1c319ff2014-10-04 20:29:09 -0700157 List<FlowEntry> rules;
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800158
Saurav Das554f5e72015-10-27 10:28:19 -0700159 Iterable<Device> devices = null;
160 if (uri == null) {
161 devices = deviceService.getDevices();
162 } else {
163 Device dev = deviceService.getDevice(DeviceId.deviceId(uri));
164 devices = (dev == null) ? deviceService.getDevices()
165 : Collections.singletonList(dev);
166 }
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800167
alshabib99b8fdc2014-09-25 14:30:22 -0700168 for (Device d : devices) {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800169 if (predicate.equals(TRUE_PREDICATE)) {
alshabib144a2942014-09-25 18:44:02 -0700170 rules = newArrayList(service.getFlowEntries(d.id()));
171 } else {
172 rules = newArrayList();
alshabib1c319ff2014-10-04 20:29:09 -0700173 for (FlowEntry f : service.getFlowEntries(d.id())) {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800174 if (predicate.test(f)) {
alshabib144a2942014-09-25 18:44:02 -0700175 rules.add(f);
176 }
177 }
178 }
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800179 rules.sort(Comparators.FLOW_RULE_COMPARATOR);
alshabib9290eea2014-09-22 11:58:17 -0700180 flows.put(d, rules);
181 }
182 return flows;
183 }
184
185 /**
186 * Prints flows.
toma6897792014-10-08 22:21:05 -0700187 *
188 * @param d the device
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800189 * @param flows the set of flows for that device
190 * @param coreService core system service
alshabib9290eea2014-09-22 11:58:17 -0700191 */
toma6897792014-10-08 22:21:05 -0700192 protected void printFlows(Device d, List<FlowEntry> flows,
193 CoreService coreService) {
tom1dd08e42014-10-07 11:40:00 -0700194 boolean empty = flows == null || flows.isEmpty();
195 print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : flows.size());
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800196 if (empty) {
197 return;
198 }
199
200 for (FlowEntry f : flows) {
201 if (shortOutput) {
202 print(SHORT_FORMAT, f.state(), f.bytes(), f.packets(),
203 f.tableId(), f.priority(), f.selector().criteria(),
204 printTreatment(f.treatment()));
205 } else {
Brian O'Connor9cc799c2015-06-04 19:35:41 -0700206 ApplicationId appId = coreService.getAppId(f.appId());
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800207 print(LONG_FORMAT, Long.toHexString(f.id().value()), f.state(),
208 f.bytes(), f.packets(), f.life(), f.priority(), f.tableId(),
209 appId != null ? appId.name() : "<none>",
210 f.payLoad() == null ? null : f.payLoad().payLoad().toString(),
211 f.selector().criteria(), f.treatment());
tom1dd08e42014-10-07 11:40:00 -0700212 }
alshabib99b8fdc2014-09-25 14:30:22 -0700213 }
alshabib9290eea2014-09-22 11:58:17 -0700214 }
215
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800216 private String printTreatment(TrafficTreatment treatment) {
217 final String delimiter = ", ";
218 StringBuilder builder = new StringBuilder("[");
219 if (!treatment.immediate().isEmpty()) {
220 builder.append("immediate=" + treatment.immediate() + delimiter);
221 }
222 if (!treatment.deferred().isEmpty()) {
223 builder.append("deferred=" + treatment.deferred() + delimiter);
224 }
225 if (treatment.clearedDeferred()) {
226 builder.append("clearDeferred" + delimiter);
227 }
228 if (treatment.tableTransition() != null) {
229 builder.append("transition=" + treatment.tableTransition() + delimiter);
230 }
231 if (treatment.metered() != null) {
232 builder.append("meter=" + treatment.metered() + delimiter);
233 }
234 if (treatment.writeMetadata() != null) {
235 builder.append("metadata=" + treatment.writeMetadata() + delimiter);
236 }
237 // Chop off last delimiter
238 builder.replace(builder.length() - delimiter.length(), builder.length(), "");
239 builder.append("]");
240 return builder.toString();
241 }
242
Yuta HIGUCHIe76a24d2014-09-27 00:48:34 -0700243}