blob: 2b74c583c3f3c9511daceeac9d95fd1440d7ea18 [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
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070018import 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;
Yuta HIGUCHI8791a812015-02-10 09:43:52 -080022
alshabib99b8fdc2014-09-25 14:30:22 -070023import org.apache.karaf.shell.commands.Argument;
24import org.apache.karaf.shell.commands.Command;
Ray Milkey75228952015-03-02 09:01:59 -080025import org.onosproject.core.ApplicationId;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.core.CoreService;
27import org.onosproject.cli.AbstractShellCommand;
28import org.onosproject.cli.Comparators;
29import 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;
35import org.onosproject.net.flow.criteria.Criterion;
36import org.onosproject.net.flow.instructions.Instruction;
alshabib99b8fdc2014-09-25 14:30:22 -070037
toma6897792014-10-08 22:21:05 -070038import java.util.Collections;
39import java.util.List;
40import java.util.Map;
Yuta HIGUCHI8791a812015-02-10 09:43:52 -080041import java.util.SortedMap;
42import java.util.TreeMap;
toma6897792014-10-08 22:21:05 -070043
44import static com.google.common.collect.Lists.newArrayList;
alshabib9290eea2014-09-22 11:58:17 -070045
46/**
47 * Lists all currently-known hosts.
48 */
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
alshabib144a2942014-09-25 18:44:02 -070053 public static final String ANY = "any";
54
alshabib9290eea2014-09-22 11:58:17 -070055 private static final String FMT =
Saurav Dascbe6de32015-03-01 18:30:46 -080056 " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s, tableId=%s appId=%s";
alshabib99b8fdc2014-09-25 14:30:22 -070057 private static final String TFMT = " treatment=%s";
58 private static final String SFMT = " selector=%s";
59
alshabib144a2942014-09-25 18:44:02 -070060 @Argument(index = 1, name = "uri", description = "Device ID",
toma6897792014-10-08 22:21:05 -070061 required = false, multiValued = false)
alshabib99b8fdc2014-09-25 14:30:22 -070062 String uri = null;
alshabib9290eea2014-09-22 11:58:17 -070063
alshabib144a2942014-09-25 18:44:02 -070064 @Argument(index = 0, name = "state", description = "Flow Rule state",
toma6897792014-10-08 22:21:05 -070065 required = false, multiValued = false)
alshabib144a2942014-09-25 18:44:02 -070066 String state = null;
alshabib64231d82014-09-25 18:25:31 -070067
alshabib9290eea2014-09-22 11:58:17 -070068 @Override
tom0872a172014-09-23 11:24:26 -070069 protected void execute() {
toma6897792014-10-08 22:21:05 -070070 CoreService coreService = get(CoreService.class);
tomcaf3bf72014-09-23 13:20:53 -070071 DeviceService deviceService = get(DeviceService.class);
72 FlowRuleService service = get(FlowRuleService.class);
Yuta HIGUCHI8791a812015-02-10 09:43:52 -080073 SortedMap<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service);
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070074
75 if (outputJson()) {
Yuta HIGUCHI8791a812015-02-10 09:43:52 -080076 print("%s", json(coreService, flows.keySet(), flows));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070077 } else {
Yuta HIGUCHI8791a812015-02-10 09:43:52 -080078 flows.forEach((device, flow) -> printFlows(device, flow, coreService));
alshabib9290eea2014-09-22 11:58:17 -070079 }
alshabib9290eea2014-09-22 11:58:17 -070080 }
81
alshabib9290eea2014-09-22 11:58:17 -070082 /**
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070083 * Produces a JSON array of flows grouped by the each device.
84 *
85 * @param coreService core service
86 * @param devices collection of devices to group flow by
87 * @param flows collection of flows per each device
88 * @return JSON array
89 */
90 private JsonNode json(CoreService coreService, Iterable<Device> devices,
91 Map<Device, List<FlowEntry>> flows) {
92 ObjectMapper mapper = new ObjectMapper();
93 ArrayNode result = mapper.createArrayNode();
94 for (Device device : devices) {
95 result.add(json(coreService, mapper, device, flows.get(device)));
96 }
97 return result;
98 }
99
100 // Produces JSON object with the flows of the given device.
101 private ObjectNode json(CoreService coreService, ObjectMapper mapper,
102 Device device, List<FlowEntry> flows) {
103 ObjectNode result = mapper.createObjectNode();
104 ArrayNode array = mapper.createArrayNode();
105
106 for (FlowEntry flow : flows) {
107 array.add(json(coreService, mapper, flow));
108 }
109
110 result.put("device", device.id().toString())
111 .put("flowCount", flows.size())
Thomas Vachuskadfe48a72014-10-16 10:32:08 -0700112 .set("flows", array);
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700113 return result;
114 }
115
116 // Produces JSON structure with the specified flow data.
117 private ObjectNode json(CoreService coreService, ObjectMapper mapper,
118 FlowEntry flow) {
119 ObjectNode result = mapper.createObjectNode();
120 ArrayNode crit = mapper.createArrayNode();
121 for (Criterion c : flow.selector().criteria()) {
122 crit.add(c.toString());
123 }
124
125 ArrayNode instr = mapper.createArrayNode();
Ray Milkey42507352015-03-20 15:16:10 -0700126 for (Instruction i : flow.treatment().allInstructions()) {
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700127 instr.add(i.toString());
128 }
129
Ray Milkey75228952015-03-02 09:01:59 -0800130 ApplicationId appCoreId = coreService.getAppId(flow.appId());
131 String appName = appCoreId == null ?
132 Short.toString(flow.appId())
133 : appCoreId.name();
134
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700135 result.put("flowId", Long.toHexString(flow.id().value()))
136 .put("state", flow.state().toString())
137 .put("bytes", flow.bytes())
138 .put("packets", flow.packets())
139 .put("life", flow.life())
Saurav Dascbe6de32015-03-01 18:30:46 -0800140 .put("tableId", flow.type().toString())
Ray Milkey75228952015-03-02 09:01:59 -0800141 .put("appId", appName);
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700142 result.set("selector", crit);
143 result.set("treatment", instr);
144 return result;
145 }
146
147 /**
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;
158 FlowEntryState s = null;
alshabib144a2942014-09-25 18:44:02 -0700159 if (state != null && !state.equals("any")) {
alshabib1c319ff2014-10-04 20:29:09 -0700160 s = FlowEntryState.valueOf(state.toUpperCase());
alshabib144a2942014-09-25 18:44:02 -0700161 }
tom1dd08e42014-10-07 11:40:00 -0700162 Iterable<Device> devices = uri == null ? deviceService.getDevices() :
toma6897792014-10-08 22:21:05 -0700163 Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri)));
alshabib99b8fdc2014-09-25 14:30:22 -0700164 for (Device d : devices) {
alshabib144a2942014-09-25 18:44:02 -0700165 if (s == null) {
166 rules = newArrayList(service.getFlowEntries(d.id()));
167 } else {
168 rules = newArrayList();
alshabib1c319ff2014-10-04 20:29:09 -0700169 for (FlowEntry f : service.getFlowEntries(d.id())) {
alshabib144a2942014-09-25 18:44:02 -0700170 if (f.state().equals(s)) {
171 rules.add(f);
172 }
173 }
174 }
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800175 rules.sort(Comparators.FLOW_RULE_COMPARATOR);
alshabib9290eea2014-09-22 11:58:17 -0700176 flows.put(d, rules);
177 }
178 return flows;
179 }
180
181 /**
182 * Prints flows.
toma6897792014-10-08 22:21:05 -0700183 *
184 * @param d the device
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800185 * @param flows the set of flows for that device
186 * @param coreService core system service
alshabib9290eea2014-09-22 11:58:17 -0700187 */
toma6897792014-10-08 22:21:05 -0700188 protected void printFlows(Device d, List<FlowEntry> flows,
189 CoreService coreService) {
tom1dd08e42014-10-07 11:40:00 -0700190 boolean empty = flows == null || flows.isEmpty();
191 print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : flows.size());
192 if (!empty) {
193 for (FlowEntry f : flows) {
toma6897792014-10-08 22:21:05 -0700194 print(FMT, Long.toHexString(f.id().value()), f.state(),
Saurav Dascbe6de32015-03-01 18:30:46 -0800195 f.bytes(), f.packets(), f.life(), f.priority(), f.type(),
toma6897792014-10-08 22:21:05 -0700196 coreService.getAppId(f.appId()).name());
tom1dd08e42014-10-07 11:40:00 -0700197 print(SFMT, f.selector().criteria());
alshabib346b5b32015-03-06 00:42:16 -0800198 print(TFMT, f.treatment());
tom1dd08e42014-10-07 11:40:00 -0700199 }
alshabib99b8fdc2014-09-25 14:30:22 -0700200 }
alshabib9290eea2014-09-22 11:58:17 -0700201 }
202
Yuta HIGUCHIe76a24d2014-09-27 00:48:34 -0700203}