blob: e0b98ed5877030085f7ce9a4535ed857d4e9bd14 [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;
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;
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;
Yuta HIGUCHI8791a812015-02-10 09:43:52 -080040import java.util.SortedMap;
41import java.util.TreeMap;
toma6897792014-10-08 22:21:05 -070042
43import static com.google.common.collect.Lists.newArrayList;
alshabib9290eea2014-09-22 11:58:17 -070044
45/**
46 * Lists all currently-known hosts.
47 */
48@Command(scope = "onos", name = "flows",
toma6897792014-10-08 22:21:05 -070049 description = "Lists all currently-known flows.")
alshabib9290eea2014-09-22 11:58:17 -070050public class FlowsListCommand extends AbstractShellCommand {
51
alshabib144a2942014-09-25 18:44:02 -070052 public static final String ANY = "any";
53
alshabib9290eea2014-09-22 11:58:17 -070054 private static final String FMT =
toma6897792014-10-08 22:21:05 -070055 " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s, appId=%s";
alshabib99b8fdc2014-09-25 14:30:22 -070056 private static final String TFMT = " treatment=%s";
57 private static final String SFMT = " selector=%s";
58
alshabib144a2942014-09-25 18:44:02 -070059 @Argument(index = 1, name = "uri", description = "Device ID",
toma6897792014-10-08 22:21:05 -070060 required = false, multiValued = false)
alshabib99b8fdc2014-09-25 14:30:22 -070061 String uri = null;
alshabib9290eea2014-09-22 11:58:17 -070062
alshabib144a2942014-09-25 18:44:02 -070063 @Argument(index = 0, name = "state", description = "Flow Rule state",
toma6897792014-10-08 22:21:05 -070064 required = false, multiValued = false)
alshabib144a2942014-09-25 18:44:02 -070065 String state = null;
alshabib64231d82014-09-25 18:25:31 -070066
alshabib9290eea2014-09-22 11:58:17 -070067 @Override
tom0872a172014-09-23 11:24:26 -070068 protected void execute() {
toma6897792014-10-08 22:21:05 -070069 CoreService coreService = get(CoreService.class);
tomcaf3bf72014-09-23 13:20:53 -070070 DeviceService deviceService = get(DeviceService.class);
71 FlowRuleService service = get(FlowRuleService.class);
Yuta HIGUCHI8791a812015-02-10 09:43:52 -080072 SortedMap<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service);
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070073
74 if (outputJson()) {
Yuta HIGUCHI8791a812015-02-10 09:43:52 -080075 print("%s", json(coreService, flows.keySet(), flows));
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070076 } else {
Yuta HIGUCHI8791a812015-02-10 09:43:52 -080077 flows.forEach((device, flow) -> printFlows(device, flow, coreService));
alshabib9290eea2014-09-22 11:58:17 -070078 }
alshabib9290eea2014-09-22 11:58:17 -070079 }
80
alshabib9290eea2014-09-22 11:58:17 -070081 /**
Thomas Vachuskabb0272e2014-10-16 09:32:04 -070082 * Produces a JSON array of flows grouped by the each device.
83 *
84 * @param coreService core service
85 * @param devices collection of devices to group flow by
86 * @param flows collection of flows per each device
87 * @return JSON array
88 */
89 private JsonNode json(CoreService coreService, Iterable<Device> devices,
90 Map<Device, List<FlowEntry>> flows) {
91 ObjectMapper mapper = new ObjectMapper();
92 ArrayNode result = mapper.createArrayNode();
93 for (Device device : devices) {
94 result.add(json(coreService, mapper, device, flows.get(device)));
95 }
96 return result;
97 }
98
99 // Produces JSON object with the flows of the given device.
100 private ObjectNode json(CoreService coreService, ObjectMapper mapper,
101 Device device, List<FlowEntry> flows) {
102 ObjectNode result = mapper.createObjectNode();
103 ArrayNode array = mapper.createArrayNode();
104
105 for (FlowEntry flow : flows) {
106 array.add(json(coreService, mapper, flow));
107 }
108
109 result.put("device", device.id().toString())
110 .put("flowCount", flows.size())
Thomas Vachuskadfe48a72014-10-16 10:32:08 -0700111 .set("flows", array);
Thomas Vachuskabb0272e2014-10-16 09:32:04 -0700112 return result;
113 }
114
115 // Produces JSON structure with the specified flow data.
116 private ObjectNode json(CoreService coreService, ObjectMapper mapper,
117 FlowEntry flow) {
118 ObjectNode result = mapper.createObjectNode();
119 ArrayNode crit = mapper.createArrayNode();
120 for (Criterion c : flow.selector().criteria()) {
121 crit.add(c.toString());
122 }
123
124 ArrayNode instr = mapper.createArrayNode();
125 for (Instruction i : flow.treatment().instructions()) {
126 instr.add(i.toString());
127 }
128
129 result.put("flowId", Long.toHexString(flow.id().value()))
130 .put("state", flow.state().toString())
131 .put("bytes", flow.bytes())
132 .put("packets", flow.packets())
133 .put("life", flow.life())
134 .put("appId", coreService.getAppId(flow.appId()).name());
135 result.set("selector", crit);
136 result.set("treatment", instr);
137 return result;
138 }
139
140 /**
alshabib9290eea2014-09-22 11:58:17 -0700141 * Returns the list of devices sorted using the device ID URIs.
142 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800143 * @param deviceService device service
144 * @param service flow rule service
alshabib9290eea2014-09-22 11:58:17 -0700145 * @return sorted device list
146 */
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800147 protected SortedMap<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService,
tom1dd08e42014-10-07 11:40:00 -0700148 FlowRuleService service) {
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800149 SortedMap<Device, List<FlowEntry>> flows = new TreeMap<>(Comparators.ELEMENT_COMPARATOR);
alshabib1c319ff2014-10-04 20:29:09 -0700150 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 }
Yuta HIGUCHI8791a812015-02-10 09:43:52 -0800168 rules.sort(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
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800178 * @param flows the set of flows for that device
179 * @param coreService core system service
alshabib9290eea2014-09-22 11:58:17 -0700180 */
toma6897792014-10-08 22:21:05 -0700181 protected void printFlows(Device d, List<FlowEntry> flows,
182 CoreService coreService) {
tom1dd08e42014-10-07 11:40:00 -0700183 boolean empty = flows == null || flows.isEmpty();
184 print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : flows.size());
185 if (!empty) {
186 for (FlowEntry f : flows) {
toma6897792014-10-08 22:21:05 -0700187 print(FMT, Long.toHexString(f.id().value()), f.state(),
188 f.bytes(), f.packets(), f.life(), f.priority(),
189 coreService.getAppId(f.appId()).name());
tom1dd08e42014-10-07 11:40:00 -0700190 print(SFMT, f.selector().criteria());
191 print(TFMT, f.treatment().instructions());
192 }
alshabib99b8fdc2014-09-25 14:30:22 -0700193 }
alshabib9290eea2014-09-22 11:58:17 -0700194 }
195
Yuta HIGUCHIe76a24d2014-09-27 00:48:34 -0700196}