blob: e434a39405fa4457b0cfaa3cd941ef53c8257e8a [file] [log] [blame]
yoonseonbc0d76f2017-01-05 14:52:05 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
yoonseonbc0d76f2017-01-05 14:52:05 -08003 *
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
7 *
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.
15 */
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080016package org.onosproject.incubator.net.virtual.cli;
yoonseonbc0d76f2017-01-05 14:52:05 -080017
18import 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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.Argument;
23import org.apache.karaf.shell.api.action.Command;
Ray Milkey0068fd02018-10-11 15:45:39 -070024import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070025import org.apache.karaf.shell.api.action.lifecycle.Service;
26import org.apache.karaf.shell.api.action.Option;
yoonseonbc0d76f2017-01-05 14:52:05 -080027import org.onlab.util.StringFilter;
28import org.onosproject.cli.AbstractShellCommand;
Ray Milkey0068fd02018-10-11 15:45:39 -070029import org.onosproject.cli.PlaceholderCompleter;
30import org.onosproject.cli.net.FlowRuleStatusCompleter;
yoonseonbc0d76f2017-01-05 14:52:05 -080031import org.onosproject.core.ApplicationId;
32import org.onosproject.core.CoreService;
33import org.onosproject.incubator.net.virtual.NetworkId;
34import org.onosproject.incubator.net.virtual.VirtualNetworkService;
35import org.onosproject.net.Device;
36import org.onosproject.net.DeviceId;
37import org.onosproject.net.device.DeviceService;
38import org.onosproject.net.flow.FlowEntry;
39import org.onosproject.net.flow.FlowEntry.FlowEntryState;
40import org.onosproject.net.flow.FlowRuleService;
41import org.onosproject.net.flow.TrafficTreatment;
42import org.onosproject.utils.Comparators;
43
44import java.util.ArrayList;
45import java.util.Collections;
46import java.util.List;
47import java.util.Map;
48import java.util.SortedMap;
49import java.util.TreeMap;
50import java.util.function.Predicate;
51import java.util.stream.Collectors;
52
53import static com.google.common.collect.Lists.newArrayList;
54
55
56/**
57 * Lists all currently-known flows.
58 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070059@Service
yoonseonbc0d76f2017-01-05 14:52:05 -080060@Command(scope = "onos", name = "vnet-flows",
61 description = "Lists all currently-known flows for a virtual network.")
62public class VirtualFlowsListCommand extends AbstractShellCommand {
63
64 private static final Predicate<FlowEntry> TRUE_PREDICATE = f -> true;
65
66 public static final String ANY = "any";
67
68 private static final String LONG_FORMAT = " id=%s, state=%s, bytes=%s, "
69 + "packets=%s, duration=%s, liveType=%s, priority=%s, tableId=%s, appId=%s, "
Ray Milkey47f09c52019-02-08 18:51:34 -080070 + "selector=%s, treatment=%s";
yoonseonbc0d76f2017-01-05 14:52:05 -080071
72 private static final String SHORT_FORMAT = " %s, bytes=%s, packets=%s, "
73 + "table=%s, priority=%s, selector=%s, treatment=%s";
74
75 @Argument(index = 0, name = "networkId", description = "Network ID",
76 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070077 @Completion(VirtualNetworkCompleter.class)
yoonseonbc0d76f2017-01-05 14:52:05 -080078 Long networkId = null;
79
80 @Argument(index = 1, name = "state", description = "Flow Rule state",
81 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070082 @Completion(FlowRuleStatusCompleter.class)
yoonseonbc0d76f2017-01-05 14:52:05 -080083 String state = null;
84
85 @Argument(index = 2, name = "uri", description = "Device ID",
86 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070087 @Completion(VirtualDeviceCompleter.class)
yoonseonbc0d76f2017-01-05 14:52:05 -080088 String uri = null;
89
90 @Argument(index = 3, name = "table", description = "Table ID",
91 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070092 @Completion(PlaceholderCompleter.class)
yoonseonbc0d76f2017-01-05 14:52:05 -080093 String table = null;
94
95 @Option(name = "-s", aliases = "--short",
96 description = "Print more succinct output for each flow",
97 required = false, multiValued = false)
98 private boolean shortOutput = false;
99
100 @Option(name = "-c", aliases = "--count",
101 description = "Print flow count only",
102 required = false, multiValued = false)
103 private boolean countOnly = false;
104
105 @Option(name = "-f", aliases = "--filter",
106 description = "Filter flows by specific key",
107 required = false, multiValued = true)
108 private List<String> filter = new ArrayList<>();
109
110 private Predicate<FlowEntry> predicate = TRUE_PREDICATE;
111
112 private StringFilter contentFilter;
113
114 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700115 protected void doExecute() {
yoonseonbc0d76f2017-01-05 14:52:05 -0800116 CoreService coreService = get(CoreService.class);
117
118 VirtualNetworkService vnetservice = get(VirtualNetworkService.class);
119 DeviceService deviceService = vnetservice.get(NetworkId.networkId(networkId),
120 DeviceService.class);
121 FlowRuleService service = vnetservice.get(NetworkId.networkId(networkId),
122 FlowRuleService.class);
123 contentFilter = new StringFilter(filter, StringFilter.Strategy.AND);
124
125 compilePredicate();
126
127 SortedMap<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service);
128
129 if (outputJson()) {
130 print("%s", json(flows.keySet(), flows));
131 } else {
132 flows.forEach((device, flow) -> printFlows(device, flow, coreService));
133 }
134 }
135
136 /**
137 * Produces a JSON array of flows grouped by the each device.
138 *
139 * @param devices collection of devices to group flow by
140 * @param flows collection of flows per each device
141 * @return JSON array
142 */
143 private JsonNode json(Iterable<Device> devices,
144 Map<Device, List<FlowEntry>> flows) {
145 ObjectMapper mapper = new ObjectMapper();
146 ArrayNode result = mapper.createArrayNode();
147 for (Device device : devices) {
148 result.add(json(mapper, device, flows.get(device)));
149 }
150 return result;
151 }
152
153 /**
154 * Compiles a predicate to find matching flows based on the command
155 * arguments.
156 */
157 private void compilePredicate() {
158 if (state != null && !state.equals(ANY)) {
159 final FlowEntryState feState = FlowEntryState.valueOf(state.toUpperCase());
160 predicate = predicate.and(f -> f.state().equals(feState));
161 }
162
163 if (table != null) {
164 final int tableId = Integer.parseInt(table);
165 predicate = predicate.and(f -> f.tableId() == tableId);
166 }
167 }
168
169 // Produces JSON object with the flows of the given device.
170 private ObjectNode json(ObjectMapper mapper,
171 Device device, List<FlowEntry> flows) {
172 ObjectNode result = mapper.createObjectNode();
173 ArrayNode array = mapper.createArrayNode();
174
175 flows.forEach(flow -> array.add(jsonForEntity(flow, FlowEntry.class)));
176
177 result.put("device", device.id().toString())
178 .put("flowCount", flows.size())
179 .set("flows", array);
180 return result;
181 }
182
183 /**
184 * Returns the list of devices sorted using the device ID URIs.
185 *
186 * @param deviceService device service
187 * @param service flow rule service
188 * @return sorted device list
189 */
190 protected SortedMap<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService,
191 FlowRuleService service) {
192 SortedMap<Device, List<FlowEntry>> flows = new TreeMap<>(Comparators.ELEMENT_COMPARATOR);
193 List<FlowEntry> rules;
194
195 Iterable<Device> devices = null;
196 if (uri == null) {
197 devices = deviceService.getDevices();
198 } else {
199 Device dev = deviceService.getDevice(DeviceId.deviceId(uri));
200 devices = (dev == null) ? deviceService.getDevices()
201 : Collections.singletonList(dev);
202 }
203
204 for (Device d : devices) {
205 if (predicate.equals(TRUE_PREDICATE)) {
206 rules = newArrayList(service.getFlowEntries(d.id()));
207 } else {
208 rules = newArrayList();
209 for (FlowEntry f : service.getFlowEntries(d.id())) {
210 if (predicate.test(f)) {
211 rules.add(f);
212 }
213 }
214 }
215 rules.sort(Comparators.FLOW_RULE_COMPARATOR);
216
217 flows.put(d, rules);
218 }
219 return flows;
220 }
221
222 /**
223 * Prints flows.
224 *
225 * @param d the device
226 * @param flows the set of flows for that device
227 * @param coreService core system service
228 */
229 protected void printFlows(Device d, List<FlowEntry> flows,
230 CoreService coreService) {
231 List<FlowEntry> filteredFlows = flows.stream().
232 filter(f -> contentFilter.filter(f)).collect(Collectors.toList());
233 boolean empty = filteredFlows == null || filteredFlows.isEmpty();
234 print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : filteredFlows.size());
235 if (empty || countOnly) {
236 return;
237 }
238
239 for (FlowEntry f : filteredFlows) {
240 if (shortOutput) {
241 print(SHORT_FORMAT, f.state(), f.bytes(), f.packets(),
242 f.tableId(), f.priority(), f.selector().criteria(),
243 printTreatment(f.treatment()));
244 } else {
245 ApplicationId appId = coreService.getAppId(f.appId());
246 print(LONG_FORMAT, Long.toHexString(f.id().value()), f.state(),
247 f.bytes(), f.packets(), f.life(), f.liveType(), f.priority(), f.tableId(),
248 appId != null ? appId.name() : "<none>",
yoonseonbc0d76f2017-01-05 14:52:05 -0800249 f.selector().criteria(), f.treatment());
250 }
251 }
252 }
253
254 private String printTreatment(TrafficTreatment treatment) {
255 final String delimiter = ", ";
256 StringBuilder builder = new StringBuilder("[");
257 if (!treatment.immediate().isEmpty()) {
258 builder.append("immediate=" + treatment.immediate() + delimiter);
259 }
260 if (!treatment.deferred().isEmpty()) {
261 builder.append("deferred=" + treatment.deferred() + delimiter);
262 }
263 if (treatment.clearedDeferred()) {
264 builder.append("clearDeferred" + delimiter);
265 }
266 if (treatment.tableTransition() != null) {
267 builder.append("transition=" + treatment.tableTransition() + delimiter);
268 }
269 if (treatment.metered() != null) {
270 builder.append("meter=" + treatment.metered() + delimiter);
271 }
272 if (treatment.writeMetadata() != null) {
273 builder.append("metadata=" + treatment.writeMetadata() + delimiter);
274 }
275 // Chop off last delimiter
276 builder.replace(builder.length() - delimiter.length(), builder.length(), "");
277 builder.append("]");
278 return builder.toString();
279 }
280
281}