blob: ab79d03cd4021894d7a6f91c2841d0a6e5d2f295 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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 */
alshabib9290eea2014-09-22 11:58:17 -070016package org.onlab.onos.cli.net;
17
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;
toma6897792014-10-08 22:21:05 -070022import com.google.common.collect.Maps;
alshabib99b8fdc2014-09-25 14:30:22 -070023import org.apache.karaf.shell.commands.Argument;
24import org.apache.karaf.shell.commands.Command;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070025import org.onlab.onos.core.CoreService;
alshabib99b8fdc2014-09-25 14:30:22 -070026import org.onlab.onos.cli.AbstractShellCommand;
tom91c7bd02014-09-25 22:50:44 -070027import org.onlab.onos.cli.Comparators;
alshabib99b8fdc2014-09-25 14:30:22 -070028import org.onlab.onos.net.Device;
29import org.onlab.onos.net.DeviceId;
30import org.onlab.onos.net.device.DeviceService;
alshabib1c319ff2014-10-04 20:29:09 -070031import org.onlab.onos.net.flow.FlowEntry;
32import org.onlab.onos.net.flow.FlowEntry.FlowEntryState;
alshabib99b8fdc2014-09-25 14:30:22 -070033import org.onlab.onos.net.flow.FlowRuleService;
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070034import org.onlab.onos.net.flow.criteria.Criterion;
35import org.onlab.onos.net.flow.instructions.Instruction;
alshabib99b8fdc2014-09-25 14:30:22 -070036
toma6897792014-10-08 22:21:05 -070037import java.util.Collections;
38import java.util.List;
39import java.util.Map;
40
41import static com.google.common.collect.Lists.newArrayList;
42import static org.onlab.onos.cli.net.DevicesListCommand.getSortedDevices;
alshabib9290eea2014-09-22 11:58:17 -070043
44/**
45 * Lists all currently-known hosts.
46 */
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 =
toma6897792014-10-08 22:21:05 -070054 " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s, appId=%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);
alshabib1c319ff2014-10-04 20:29:09 -070071 Map<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service);
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070072
73 if (outputJson()) {
74 print("%s", json(coreService, getSortedDevices(deviceService), flows));
75 } else {
76 for (Device d : getSortedDevices(deviceService)) {
77 printFlows(d, flows.get(d), coreService);
78 }
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();
126 for (Instruction i : flow.treatment().instructions()) {
127 instr.add(i.toString());
128 }
129
130 result.put("flowId", Long.toHexString(flow.id().value()))
131 .put("state", flow.state().toString())
132 .put("bytes", flow.bytes())
133 .put("packets", flow.packets())
134 .put("life", flow.life())
135 .put("appId", coreService.getAppId(flow.appId()).name());
136 result.set("selector", crit);
137 result.set("treatment", instr);
138 return result;
139 }
140
141 /**
alshabib9290eea2014-09-22 11:58:17 -0700142 * Returns the list of devices sorted using the device ID URIs.
143 *
144 * @param service device service
145 * @return sorted device list
146 */
tom1dd08e42014-10-07 11:40:00 -0700147 protected Map<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService,
148 FlowRuleService service) {
alshabib1c319ff2014-10-04 20:29:09 -0700149 Map<Device, List<FlowEntry>> flows = Maps.newHashMap();
150 List<FlowEntry> rules;
151 FlowEntryState s = null;
alshabib144a2942014-09-25 18:44:02 -0700152 if (state != null && !state.equals("any")) {
alshabib1c319ff2014-10-04 20:29:09 -0700153 s = FlowEntryState.valueOf(state.toUpperCase());
alshabib144a2942014-09-25 18:44:02 -0700154 }
tom1dd08e42014-10-07 11:40:00 -0700155 Iterable<Device> devices = uri == null ? deviceService.getDevices() :
toma6897792014-10-08 22:21:05 -0700156 Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri)));
alshabib99b8fdc2014-09-25 14:30:22 -0700157 for (Device d : devices) {
alshabib144a2942014-09-25 18:44:02 -0700158 if (s == null) {
159 rules = newArrayList(service.getFlowEntries(d.id()));
160 } else {
161 rules = newArrayList();
alshabib1c319ff2014-10-04 20:29:09 -0700162 for (FlowEntry f : service.getFlowEntries(d.id())) {
alshabib144a2942014-09-25 18:44:02 -0700163 if (f.state().equals(s)) {
164 rules.add(f);
165 }
166 }
167 }
tom1380eee2014-09-24 09:22:02 -0700168 Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR);
alshabib9290eea2014-09-22 11:58:17 -0700169 flows.put(d, rules);
170 }
171 return flows;
172 }
173
174 /**
175 * Prints flows.
toma6897792014-10-08 22:21:05 -0700176 *
177 * @param d the device
alshabib9290eea2014-09-22 11:58:17 -0700178 * @param flows the set of flows for that device.
179 */
toma6897792014-10-08 22:21:05 -0700180 protected void printFlows(Device d, List<FlowEntry> flows,
181 CoreService coreService) {
tom1dd08e42014-10-07 11:40:00 -0700182 boolean empty = flows == null || flows.isEmpty();
183 print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : flows.size());
184 if (!empty) {
185 for (FlowEntry f : flows) {
toma6897792014-10-08 22:21:05 -0700186 print(FMT, Long.toHexString(f.id().value()), f.state(),
187 f.bytes(), f.packets(), f.life(), f.priority(),
188 coreService.getAppId(f.appId()).name());
tom1dd08e42014-10-07 11:40:00 -0700189 print(SFMT, f.selector().criteria());
190 print(TFMT, f.treatment().instructions());
191 }
alshabib99b8fdc2014-09-25 14:30:22 -0700192 }
alshabib9290eea2014-09-22 11:58:17 -0700193 }
194
Yuta HIGUCHIe76a24d2014-09-27 00:48:34 -0700195}