blob: 0be9bd4d7b47cc0913304136b401f9550b68846b [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
jccf988bf52015-05-05 19:02:50 +080018import static com.google.common.collect.Lists.newArrayList;
19
Ray Milkey3078fc02015-05-06 16:14:14 -070020import java.util.Collections;
21import java.util.List;
22import java.util.Map;
23import java.util.SortedMap;
24import java.util.TreeMap;
Yuta HIGUCHI8791a812015-02-10 09:43:52 -080025
alshabib99b8fdc2014-09-25 14:30:22 -070026import org.apache.karaf.shell.commands.Argument;
27import org.apache.karaf.shell.commands.Command;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.cli.AbstractShellCommand;
29import org.onosproject.cli.Comparators;
Ray Milkey3078fc02015-05-06 16:14:14 -070030import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.Device;
32import org.onosproject.net.DeviceId;
33import org.onosproject.net.device.DeviceService;
34import org.onosproject.net.flow.FlowEntry;
35import org.onosproject.net.flow.FlowEntry.FlowEntryState;
36import org.onosproject.net.flow.FlowRuleService;
alshabib99b8fdc2014-09-25 14:30:22 -070037
Ray Milkey3078fc02015-05-06 16:14:14 -070038import com.fasterxml.jackson.databind.JsonNode;
39import com.fasterxml.jackson.databind.ObjectMapper;
40import com.fasterxml.jackson.databind.node.ArrayNode;
41import com.fasterxml.jackson.databind.node.ObjectNode;
toma6897792014-10-08 22:21:05 -070042
alshabib9290eea2014-09-22 11:58:17 -070043/**
jccf988bf52015-05-05 19:02:50 +080044 * Lists all currently-known flows.
alshabib9290eea2014-09-22 11:58:17 -070045 */
46@Command(scope = "onos", name = "flows",
toma6897792014-10-08 22:21:05 -070047 description = "Lists all currently-known flows.")
alshabib9290eea2014-09-22 11:58:17 -070048public class FlowsListCommand extends AbstractShellCommand {
49
alshabib144a2942014-09-25 18:44:02 -070050 public static final String ANY = "any";
51
alshabib9290eea2014-09-22 11:58:17 -070052 private static final String FMT =
jccf988bf52015-05-05 19:02:50 +080053 " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s, tableId=%s appId=%s, payLoad=%s";
alshabib99b8fdc2014-09-25 14:30:22 -070054 private static final String TFMT = " treatment=%s";
55 private static final String SFMT = " selector=%s";
56
alshabib144a2942014-09-25 18:44:02 -070057 @Argument(index = 1, name = "uri", description = "Device ID",
toma6897792014-10-08 22:21:05 -070058 required = false, multiValued = false)
alshabib99b8fdc2014-09-25 14:30:22 -070059 String uri = null;
alshabib9290eea2014-09-22 11:58:17 -070060
alshabib144a2942014-09-25 18:44:02 -070061 @Argument(index = 0, name = "state", description = "Flow Rule state",
toma6897792014-10-08 22:21:05 -070062 required = false, multiValued = false)
alshabib144a2942014-09-25 18:44:02 -070063 String state = null;
alshabib64231d82014-09-25 18:25:31 -070064
alshabib9290eea2014-09-22 11:58:17 -070065 @Override
tom0872a172014-09-23 11:24:26 -070066 protected void execute() {
toma6897792014-10-08 22:21:05 -070067 CoreService coreService = get(CoreService.class);
tomcaf3bf72014-09-23 13:20:53 -070068 DeviceService deviceService = get(DeviceService.class);
69 FlowRuleService service = get(FlowRuleService.class);
Yuta HIGUCHI8791a812015-02-10 09:43:52 -080070 SortedMap<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service);
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070071
72 if (outputJson()) {
Ray Milkey3078fc02015-05-06 16:14:14 -070073 print("%s", json(flows.keySet(), flows));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070074 } else {
Yuta HIGUCHI8791a812015-02-10 09:43:52 -080075 flows.forEach((device, flow) -> printFlows(device, flow, coreService));
alshabib9290eea2014-09-22 11:58:17 -070076 }
alshabib9290eea2014-09-22 11:58:17 -070077 }
78
alshabib9290eea2014-09-22 11:58:17 -070079 /**
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070080 * Produces a JSON array of flows grouped by the each device.
81 *
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070082 * @param devices collection of devices to group flow by
83 * @param flows collection of flows per each device
84 * @return JSON array
85 */
Ray Milkey3078fc02015-05-06 16:14:14 -070086 private JsonNode json(Iterable<Device> devices,
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070087 Map<Device, List<FlowEntry>> flows) {
88 ObjectMapper mapper = new ObjectMapper();
89 ArrayNode result = mapper.createArrayNode();
90 for (Device device : devices) {
Ray Milkey3078fc02015-05-06 16:14:14 -070091 result.add(json(mapper, device, flows.get(device)));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070092 }
93 return result;
94 }
95
96 // Produces JSON object with the flows of the given device.
Ray Milkey3078fc02015-05-06 16:14:14 -070097 private ObjectNode json(ObjectMapper mapper,
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070098 Device device, List<FlowEntry> flows) {
99 ObjectNode result = mapper.createObjectNode();
100 ArrayNode array = mapper.createArrayNode();
101
Ray Milkey3078fc02015-05-06 16:14:14 -0700102 flows.forEach(flow -> array.add(jsonForEntity(flow, FlowEntry.class)));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700103
104 result.put("device", device.id().toString())
105 .put("flowCount", flows.size())
Thomas Vachuskadfe48a72014-10-16 10:32:08 -0700106 .set("flows", array);
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700107 return result;
108 }
109
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700110 /**
alshabib9290eea2014-09-22 11:58:17 -0700111 * Returns the list of devices sorted using the device ID URIs.
112 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800113 * @param deviceService device service
114 * @param service flow rule service
alshabib9290eea2014-09-22 11:58:17 -0700115 * @return sorted device list
116 */
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800117 protected SortedMap<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService,
tom1dd08e42014-10-07 11:40:00 -0700118 FlowRuleService service) {
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800119 SortedMap<Device, List<FlowEntry>> flows = new TreeMap<>(Comparators.ELEMENT_COMPARATOR);
alshabib1c319ff2014-10-04 20:29:09 -0700120 List<FlowEntry> rules;
121 FlowEntryState s = null;
alshabib144a2942014-09-25 18:44:02 -0700122 if (state != null && !state.equals("any")) {
alshabib1c319ff2014-10-04 20:29:09 -0700123 s = FlowEntryState.valueOf(state.toUpperCase());
alshabib144a2942014-09-25 18:44:02 -0700124 }
tom1dd08e42014-10-07 11:40:00 -0700125 Iterable<Device> devices = uri == null ? deviceService.getDevices() :
toma6897792014-10-08 22:21:05 -0700126 Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri)));
alshabib99b8fdc2014-09-25 14:30:22 -0700127 for (Device d : devices) {
alshabib144a2942014-09-25 18:44:02 -0700128 if (s == null) {
129 rules = newArrayList(service.getFlowEntries(d.id()));
130 } else {
131 rules = newArrayList();
alshabib1c319ff2014-10-04 20:29:09 -0700132 for (FlowEntry f : service.getFlowEntries(d.id())) {
alshabib144a2942014-09-25 18:44:02 -0700133 if (f.state().equals(s)) {
134 rules.add(f);
135 }
136 }
137 }
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800138 rules.sort(Comparators.FLOW_RULE_COMPARATOR);
alshabib9290eea2014-09-22 11:58:17 -0700139 flows.put(d, rules);
140 }
141 return flows;
142 }
143
144 /**
145 * Prints flows.
toma6897792014-10-08 22:21:05 -0700146 *
147 * @param d the device
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800148 * @param flows the set of flows for that device
149 * @param coreService core system service
alshabib9290eea2014-09-22 11:58:17 -0700150 */
toma6897792014-10-08 22:21:05 -0700151 protected void printFlows(Device d, List<FlowEntry> flows,
152 CoreService coreService) {
tom1dd08e42014-10-07 11:40:00 -0700153 boolean empty = flows == null || flows.isEmpty();
154 print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : flows.size());
155 if (!empty) {
156 for (FlowEntry f : flows) {
toma6897792014-10-08 22:21:05 -0700157 print(FMT, Long.toHexString(f.id().value()), f.state(),
alshabib08d98982015-04-21 16:25:50 -0700158 f.bytes(), f.packets(), f.life(), f.priority(), f.tableId(),
jccf988bf52015-05-05 19:02:50 +0800159 coreService.getAppId(f.appId()).name(),
160 f.payLoad() == null ? null : f.payLoad().payLoad().toString());
tom1dd08e42014-10-07 11:40:00 -0700161 print(SFMT, f.selector().criteria());
alshabib346b5b32015-03-06 00:42:16 -0800162 print(TFMT, f.treatment());
tom1dd08e42014-10-07 11:40:00 -0700163 }
alshabib99b8fdc2014-09-25 14:30:22 -0700164 }
alshabib9290eea2014-09-22 11:58:17 -0700165 }
166
Yuta HIGUCHIe76a24d2014-09-27 00:48:34 -0700167}