Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Argument; |
| 23 | import org.apache.karaf.shell.commands.Command; |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 24 | import org.apache.karaf.shell.commands.Option; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.cli.Comparators; |
Brian O'Connor | 9cc799c | 2015-06-04 19:35:41 -0700 | [diff] [blame] | 27 | import org.onosproject.core.ApplicationId; |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 28 | import org.onosproject.core.CoreService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.Device; |
| 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.device.DeviceService; |
| 32 | import org.onosproject.net.flow.FlowEntry; |
| 33 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
| 34 | import org.onosproject.net.flow.FlowRuleService; |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 35 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 36 | |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 37 | import java.util.Collections; |
| 38 | import java.util.List; |
| 39 | import java.util.Map; |
| 40 | import java.util.SortedMap; |
| 41 | import java.util.TreeMap; |
| 42 | import java.util.function.Predicate; |
| 43 | |
| 44 | import static com.google.common.collect.Lists.newArrayList; |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 45 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 46 | /** |
jcc | f988bf5 | 2015-05-05 19:02:50 +0800 | [diff] [blame] | 47 | * Lists all currently-known flows. |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 48 | */ |
| 49 | @Command(scope = "onos", name = "flows", |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 50 | description = "Lists all currently-known flows.") |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 51 | public class FlowsListCommand extends AbstractShellCommand { |
| 52 | |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 53 | private static final Predicate<FlowEntry> TRUE_PREDICATE = f -> true; |
| 54 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 55 | public static final String ANY = "any"; |
| 56 | |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 57 | private static final String LONG_FORMAT = " id=%s, state=%s, bytes=%s, " |
| 58 | + "packets=%s, duration=%s, priority=%s, tableId=%s, appId=%s, " |
| 59 | + "payLoad=%s, selector=%s, treatment=%s"; |
| 60 | |
| 61 | private static final String SHORT_FORMAT = " %s, bytes=%s, packets=%s, " |
| 62 | + "table=%s, priority=%s, selector=%s, treatment=%s"; |
| 63 | |
| 64 | @Argument(index = 0, name = "state", description = "Flow Rule state", |
| 65 | required = false, multiValued = false) |
| 66 | String state = null; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 67 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 68 | @Argument(index = 1, name = "uri", description = "Device ID", |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 69 | required = false, multiValued = false) |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 70 | String uri = null; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 71 | |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 72 | @Argument(index = 2, name = "table", description = "Table ID", |
| 73 | required = false, multiValued = false) |
| 74 | String table = null; |
| 75 | |
| 76 | @Option(name = "-s", aliases = "--short", |
| 77 | description = "Print more succinct output for each flow", |
| 78 | required = false, multiValued = false) |
| 79 | private boolean shortOutput = false; |
| 80 | |
Charles Chan | f9b94ab | 2016-02-23 19:31:41 -0800 | [diff] [blame] | 81 | @Option(name = "-c", aliases = "--count", |
| 82 | description = "Print flow count only", |
| 83 | required = false, multiValued = false) |
| 84 | private boolean countOnly = false; |
| 85 | |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 86 | private Predicate<FlowEntry> predicate = TRUE_PREDICATE; |
alshabib | 64231d8 | 2014-09-25 18:25:31 -0700 | [diff] [blame] | 87 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 88 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 89 | protected void execute() { |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 90 | CoreService coreService = get(CoreService.class); |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 91 | DeviceService deviceService = get(DeviceService.class); |
| 92 | FlowRuleService service = get(FlowRuleService.class); |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 93 | |
| 94 | compilePredicate(); |
| 95 | |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 96 | SortedMap<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 97 | |
| 98 | if (outputJson()) { |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 99 | print("%s", json(flows.keySet(), flows)); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 100 | } else { |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 101 | flows.forEach((device, flow) -> printFlows(device, flow, coreService)); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 102 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 103 | } |
| 104 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 105 | /** |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 106 | * Produces a JSON array of flows grouped by the each device. |
| 107 | * |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 108 | * @param devices collection of devices to group flow by |
| 109 | * @param flows collection of flows per each device |
| 110 | * @return JSON array |
| 111 | */ |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 112 | private JsonNode json(Iterable<Device> devices, |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 113 | Map<Device, List<FlowEntry>> flows) { |
| 114 | ObjectMapper mapper = new ObjectMapper(); |
| 115 | ArrayNode result = mapper.createArrayNode(); |
| 116 | for (Device device : devices) { |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 117 | result.add(json(mapper, device, flows.get(device))); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 118 | } |
| 119 | return result; |
| 120 | } |
| 121 | |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 122 | /** |
| 123 | * Compiles a predicate to find matching flows based on the command |
| 124 | * arguments. |
| 125 | */ |
| 126 | private void compilePredicate() { |
| 127 | if (state != null && !state.equals(ANY)) { |
| 128 | final FlowEntryState feState = FlowEntryState.valueOf(state.toUpperCase()); |
| 129 | predicate = predicate.and(f -> f.state().equals(feState)); |
| 130 | } |
| 131 | |
| 132 | if (table != null) { |
| 133 | final int tableId = Integer.parseInt(table); |
| 134 | predicate = predicate.and(f -> f.tableId() == tableId); |
| 135 | } |
| 136 | } |
| 137 | |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 138 | // Produces JSON object with the flows of the given device. |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 139 | private ObjectNode json(ObjectMapper mapper, |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 140 | Device device, List<FlowEntry> flows) { |
| 141 | ObjectNode result = mapper.createObjectNode(); |
| 142 | ArrayNode array = mapper.createArrayNode(); |
| 143 | |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 144 | flows.forEach(flow -> array.add(jsonForEntity(flow, FlowEntry.class))); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 145 | |
| 146 | result.put("device", device.id().toString()) |
| 147 | .put("flowCount", flows.size()) |
Thomas Vachuska | dfe48a7 | 2014-10-16 10:32:08 -0700 | [diff] [blame] | 148 | .set("flows", array); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 149 | return result; |
| 150 | } |
| 151 | |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 152 | /** |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 153 | * Returns the list of devices sorted using the device ID URIs. |
| 154 | * |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 155 | * @param deviceService device service |
| 156 | * @param service flow rule service |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 157 | * @return sorted device list |
| 158 | */ |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 159 | protected SortedMap<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService, |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 160 | FlowRuleService service) { |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 161 | SortedMap<Device, List<FlowEntry>> flows = new TreeMap<>(Comparators.ELEMENT_COMPARATOR); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 162 | List<FlowEntry> rules; |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 163 | |
Saurav Das | 554f5e7 | 2015-10-27 10:28:19 -0700 | [diff] [blame] | 164 | Iterable<Device> devices = null; |
| 165 | if (uri == null) { |
| 166 | devices = deviceService.getDevices(); |
| 167 | } else { |
| 168 | Device dev = deviceService.getDevice(DeviceId.deviceId(uri)); |
| 169 | devices = (dev == null) ? deviceService.getDevices() |
| 170 | : Collections.singletonList(dev); |
| 171 | } |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 172 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 173 | for (Device d : devices) { |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 174 | if (predicate.equals(TRUE_PREDICATE)) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 175 | rules = newArrayList(service.getFlowEntries(d.id())); |
| 176 | } else { |
| 177 | rules = newArrayList(); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 178 | for (FlowEntry f : service.getFlowEntries(d.id())) { |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 179 | if (predicate.test(f)) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 180 | rules.add(f); |
| 181 | } |
| 182 | } |
| 183 | } |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 184 | rules.sort(Comparators.FLOW_RULE_COMPARATOR); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 185 | flows.put(d, rules); |
| 186 | } |
| 187 | return flows; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Prints flows. |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 192 | * |
| 193 | * @param d the device |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 194 | * @param flows the set of flows for that device |
| 195 | * @param coreService core system service |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 196 | */ |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 197 | protected void printFlows(Device d, List<FlowEntry> flows, |
| 198 | CoreService coreService) { |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 199 | boolean empty = flows == null || flows.isEmpty(); |
| 200 | print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : flows.size()); |
Charles Chan | f9b94ab | 2016-02-23 19:31:41 -0800 | [diff] [blame] | 201 | if (empty || countOnly) { |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | |
| 205 | for (FlowEntry f : flows) { |
| 206 | if (shortOutput) { |
| 207 | print(SHORT_FORMAT, f.state(), f.bytes(), f.packets(), |
| 208 | f.tableId(), f.priority(), f.selector().criteria(), |
| 209 | printTreatment(f.treatment())); |
| 210 | } else { |
Brian O'Connor | 9cc799c | 2015-06-04 19:35:41 -0700 | [diff] [blame] | 211 | ApplicationId appId = coreService.getAppId(f.appId()); |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 212 | print(LONG_FORMAT, Long.toHexString(f.id().value()), f.state(), |
| 213 | f.bytes(), f.packets(), f.life(), f.priority(), f.tableId(), |
| 214 | appId != null ? appId.name() : "<none>", |
| 215 | f.payLoad() == null ? null : f.payLoad().payLoad().toString(), |
| 216 | f.selector().criteria(), f.treatment()); |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 217 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 218 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 221 | private String printTreatment(TrafficTreatment treatment) { |
| 222 | final String delimiter = ", "; |
| 223 | StringBuilder builder = new StringBuilder("["); |
| 224 | if (!treatment.immediate().isEmpty()) { |
| 225 | builder.append("immediate=" + treatment.immediate() + delimiter); |
| 226 | } |
| 227 | if (!treatment.deferred().isEmpty()) { |
| 228 | builder.append("deferred=" + treatment.deferred() + delimiter); |
| 229 | } |
| 230 | if (treatment.clearedDeferred()) { |
| 231 | builder.append("clearDeferred" + delimiter); |
| 232 | } |
| 233 | if (treatment.tableTransition() != null) { |
| 234 | builder.append("transition=" + treatment.tableTransition() + delimiter); |
| 235 | } |
| 236 | if (treatment.metered() != null) { |
| 237 | builder.append("meter=" + treatment.metered() + delimiter); |
| 238 | } |
| 239 | if (treatment.writeMetadata() != null) { |
| 240 | builder.append("metadata=" + treatment.writeMetadata() + delimiter); |
| 241 | } |
| 242 | // Chop off last delimiter |
| 243 | builder.replace(builder.length() - delimiter.length(), builder.length(), ""); |
| 244 | builder.append("]"); |
| 245 | return builder.toString(); |
| 246 | } |
| 247 | |
Yuta HIGUCHI | e76a24d | 2014-09-27 00:48:34 -0700 | [diff] [blame] | 248 | } |