blob: de84f519d6b255a1d1c4b8eedd4582065b47bad1 [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;
Brian O'Connor9cc799c2015-06-04 19:35:41 -070030import org.onosproject.core.ApplicationId;
Ray Milkey3078fc02015-05-06 16:14:14 -070031import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.Device;
33import org.onosproject.net.DeviceId;
34import org.onosproject.net.device.DeviceService;
35import org.onosproject.net.flow.FlowEntry;
36import org.onosproject.net.flow.FlowEntry.FlowEntryState;
37import org.onosproject.net.flow.FlowRuleService;
alshabib99b8fdc2014-09-25 14:30:22 -070038
Ray Milkey3078fc02015-05-06 16:14:14 -070039import com.fasterxml.jackson.databind.JsonNode;
40import com.fasterxml.jackson.databind.ObjectMapper;
41import com.fasterxml.jackson.databind.node.ArrayNode;
42import com.fasterxml.jackson.databind.node.ObjectNode;
toma6897792014-10-08 22:21:05 -070043
alshabib9290eea2014-09-22 11:58:17 -070044/**
jccf988bf52015-05-05 19:02:50 +080045 * Lists all currently-known flows.
alshabib9290eea2014-09-22 11:58:17 -070046 */
47@Command(scope = "onos", name = "flows",
toma6897792014-10-08 22:21:05 -070048 description = "Lists all currently-known flows.")
alshabib9290eea2014-09-22 11:58:17 -070049public class FlowsListCommand extends AbstractShellCommand {
50
alshabib144a2942014-09-25 18:44:02 -070051 public static final String ANY = "any";
52
alshabib9290eea2014-09-22 11:58:17 -070053 private static final String FMT =
jccf988bf52015-05-05 19:02:50 +080054 " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s, tableId=%s appId=%s, payLoad=%s";
alshabib99b8fdc2014-09-25 14:30:22 -070055 private static final String TFMT = " treatment=%s";
56 private static final String SFMT = " selector=%s";
57
alshabib144a2942014-09-25 18:44:02 -070058 @Argument(index = 1, name = "uri", description = "Device ID",
toma6897792014-10-08 22:21:05 -070059 required = false, multiValued = false)
alshabib99b8fdc2014-09-25 14:30:22 -070060 String uri = null;
alshabib9290eea2014-09-22 11:58:17 -070061
alshabib144a2942014-09-25 18:44:02 -070062 @Argument(index = 0, name = "state", description = "Flow Rule state",
toma6897792014-10-08 22:21:05 -070063 required = false, multiValued = false)
alshabib144a2942014-09-25 18:44:02 -070064 String state = null;
alshabib64231d82014-09-25 18:25:31 -070065
alshabib9290eea2014-09-22 11:58:17 -070066 @Override
tom0872a172014-09-23 11:24:26 -070067 protected void execute() {
toma6897792014-10-08 22:21:05 -070068 CoreService coreService = get(CoreService.class);
tomcaf3bf72014-09-23 13:20:53 -070069 DeviceService deviceService = get(DeviceService.class);
70 FlowRuleService service = get(FlowRuleService.class);
Yuta HIGUCHI8791a812015-02-10 09:43:52 -080071 SortedMap<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service);
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070072
73 if (outputJson()) {
Ray Milkey3078fc02015-05-06 16:14:14 -070074 print("%s", json(flows.keySet(), flows));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070075 } else {
Yuta HIGUCHI8791a812015-02-10 09:43:52 -080076 flows.forEach((device, flow) -> printFlows(device, flow, coreService));
alshabib9290eea2014-09-22 11:58:17 -070077 }
alshabib9290eea2014-09-22 11:58:17 -070078 }
79
alshabib9290eea2014-09-22 11:58:17 -070080 /**
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070081 * Produces a JSON array of flows grouped by the each device.
82 *
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070083 * @param devices collection of devices to group flow by
84 * @param flows collection of flows per each device
85 * @return JSON array
86 */
Ray Milkey3078fc02015-05-06 16:14:14 -070087 private JsonNode json(Iterable<Device> devices,
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070088 Map<Device, List<FlowEntry>> flows) {
89 ObjectMapper mapper = new ObjectMapper();
90 ArrayNode result = mapper.createArrayNode();
91 for (Device device : devices) {
Ray Milkey3078fc02015-05-06 16:14:14 -070092 result.add(json(mapper, device, flows.get(device)));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070093 }
94 return result;
95 }
96
97 // Produces JSON object with the flows of the given device.
Ray Milkey3078fc02015-05-06 16:14:14 -070098 private ObjectNode json(ObjectMapper mapper,
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070099 Device device, List<FlowEntry> flows) {
100 ObjectNode result = mapper.createObjectNode();
101 ArrayNode array = mapper.createArrayNode();
102
Ray Milkey3078fc02015-05-06 16:14:14 -0700103 flows.forEach(flow -> array.add(jsonForEntity(flow, FlowEntry.class)));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700104
105 result.put("device", device.id().toString())
106 .put("flowCount", flows.size())
Thomas Vachuskadfe48a72014-10-16 10:32:08 -0700107 .set("flows", array);
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700108 return result;
109 }
110
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700111 /**
alshabib9290eea2014-09-22 11:58:17 -0700112 * Returns the list of devices sorted using the device ID URIs.
113 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800114 * @param deviceService device service
115 * @param service flow rule service
alshabib9290eea2014-09-22 11:58:17 -0700116 * @return sorted device list
117 */
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800118 protected SortedMap<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService,
tom1dd08e42014-10-07 11:40:00 -0700119 FlowRuleService service) {
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800120 SortedMap<Device, List<FlowEntry>> flows = new TreeMap<>(Comparators.ELEMENT_COMPARATOR);
alshabib1c319ff2014-10-04 20:29:09 -0700121 List<FlowEntry> rules;
122 FlowEntryState s = null;
alshabib144a2942014-09-25 18:44:02 -0700123 if (state != null && !state.equals("any")) {
alshabib1c319ff2014-10-04 20:29:09 -0700124 s = FlowEntryState.valueOf(state.toUpperCase());
alshabib144a2942014-09-25 18:44:02 -0700125 }
tom1dd08e42014-10-07 11:40:00 -0700126 Iterable<Device> devices = uri == null ? deviceService.getDevices() :
toma6897792014-10-08 22:21:05 -0700127 Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri)));
alshabib99b8fdc2014-09-25 14:30:22 -0700128 for (Device d : devices) {
alshabib144a2942014-09-25 18:44:02 -0700129 if (s == null) {
130 rules = newArrayList(service.getFlowEntries(d.id()));
131 } else {
132 rules = newArrayList();
alshabib1c319ff2014-10-04 20:29:09 -0700133 for (FlowEntry f : service.getFlowEntries(d.id())) {
alshabib144a2942014-09-25 18:44:02 -0700134 if (f.state().equals(s)) {
135 rules.add(f);
136 }
137 }
138 }
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800139 rules.sort(Comparators.FLOW_RULE_COMPARATOR);
alshabib9290eea2014-09-22 11:58:17 -0700140 flows.put(d, rules);
141 }
142 return flows;
143 }
144
145 /**
146 * Prints flows.
toma6897792014-10-08 22:21:05 -0700147 *
148 * @param d the device
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800149 * @param flows the set of flows for that device
150 * @param coreService core system service
alshabib9290eea2014-09-22 11:58:17 -0700151 */
toma6897792014-10-08 22:21:05 -0700152 protected void printFlows(Device d, List<FlowEntry> flows,
153 CoreService coreService) {
tom1dd08e42014-10-07 11:40:00 -0700154 boolean empty = flows == null || flows.isEmpty();
155 print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : flows.size());
156 if (!empty) {
157 for (FlowEntry f : flows) {
Brian O'Connor9cc799c2015-06-04 19:35:41 -0700158 ApplicationId appId = coreService.getAppId(f.appId());
toma6897792014-10-08 22:21:05 -0700159 print(FMT, Long.toHexString(f.id().value()), f.state(),
alshabib08d98982015-04-21 16:25:50 -0700160 f.bytes(), f.packets(), f.life(), f.priority(), f.tableId(),
Brian O'Connor9cc799c2015-06-04 19:35:41 -0700161 appId != null ? appId.name() : "<none>",
jccf988bf52015-05-05 19:02:50 +0800162 f.payLoad() == null ? null : f.payLoad().payLoad().toString());
tom1dd08e42014-10-07 11:40:00 -0700163 print(SFMT, f.selector().criteria());
alshabib346b5b32015-03-06 00:42:16 -0800164 print(TFMT, f.treatment());
tom1dd08e42014-10-07 11:40:00 -0700165 }
alshabib99b8fdc2014-09-25 14:30:22 -0700166 }
alshabib9290eea2014-09-22 11:58:17 -0700167 }
168
Yuta HIGUCHIe76a24d2014-09-27 00:48:34 -0700169}