blob: 8cea44647623ea5cc41b492c7cdb3c6b4b06b785 [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 */
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;
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.core.CoreService;
26import org.onosproject.cli.AbstractShellCommand;
27import org.onosproject.cli.Comparators;
28import org.onosproject.net.Device;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.device.DeviceService;
31import org.onosproject.net.flow.FlowEntry;
32import org.onosproject.net.flow.FlowEntry.FlowEntryState;
33import org.onosproject.net.flow.FlowRuleService;
34import org.onosproject.net.flow.criteria.Criterion;
35import org.onosproject.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;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import static org.onosproject.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 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800144 * @param deviceService device service
145 * @param service flow rule service
alshabib9290eea2014-09-22 11:58:17 -0700146 * @return sorted device list
147 */
tom1dd08e42014-10-07 11:40:00 -0700148 protected Map<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService,
149 FlowRuleService service) {
alshabib1c319ff2014-10-04 20:29:09 -0700150 Map<Device, List<FlowEntry>> flows = Maps.newHashMap();
151 List<FlowEntry> rules;
152 FlowEntryState s = null;
alshabib144a2942014-09-25 18:44:02 -0700153 if (state != null && !state.equals("any")) {
alshabib1c319ff2014-10-04 20:29:09 -0700154 s = FlowEntryState.valueOf(state.toUpperCase());
alshabib144a2942014-09-25 18:44:02 -0700155 }
tom1dd08e42014-10-07 11:40:00 -0700156 Iterable<Device> devices = uri == null ? deviceService.getDevices() :
toma6897792014-10-08 22:21:05 -0700157 Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri)));
alshabib99b8fdc2014-09-25 14:30:22 -0700158 for (Device d : devices) {
alshabib144a2942014-09-25 18:44:02 -0700159 if (s == null) {
160 rules = newArrayList(service.getFlowEntries(d.id()));
161 } else {
162 rules = newArrayList();
alshabib1c319ff2014-10-04 20:29:09 -0700163 for (FlowEntry f : service.getFlowEntries(d.id())) {
alshabib144a2942014-09-25 18:44:02 -0700164 if (f.state().equals(s)) {
165 rules.add(f);
166 }
167 }
168 }
tom1380eee2014-09-24 09:22:02 -0700169 Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR);
alshabib9290eea2014-09-22 11:58:17 -0700170 flows.put(d, rules);
171 }
172 return flows;
173 }
174
175 /**
176 * Prints flows.
toma6897792014-10-08 22:21:05 -0700177 *
178 * @param d the device
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800179 * @param flows the set of flows for that device
180 * @param coreService core system service
alshabib9290eea2014-09-22 11:58:17 -0700181 */
toma6897792014-10-08 22:21:05 -0700182 protected void printFlows(Device d, List<FlowEntry> flows,
183 CoreService coreService) {
tom1dd08e42014-10-07 11:40:00 -0700184 boolean empty = flows == null || flows.isEmpty();
185 print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : flows.size());
186 if (!empty) {
187 for (FlowEntry f : flows) {
toma6897792014-10-08 22:21:05 -0700188 print(FMT, Long.toHexString(f.id().value()), f.state(),
189 f.bytes(), f.packets(), f.life(), f.priority(),
190 coreService.getAppId(f.appId()).name());
tom1dd08e42014-10-07 11:40:00 -0700191 print(SFMT, f.selector().criteria());
192 print(TFMT, f.treatment().instructions());
193 }
alshabib99b8fdc2014-09-25 14:30:22 -0700194 }
alshabib9290eea2014-09-22 11:58:17 -0700195 }
196
Yuta HIGUCHIe76a24d2014-09-27 00:48:34 -0700197}