Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 19 | package org.onlab.onos.cli.net; |
| 20 | |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.JsonNode; |
| 22 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 24 | import com.fasterxml.jackson.databind.node.ObjectNode; |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 25 | import com.google.common.collect.Maps; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 26 | import org.apache.karaf.shell.commands.Argument; |
| 27 | import org.apache.karaf.shell.commands.Command; |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 28 | import org.onlab.onos.CoreService; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 29 | import org.onlab.onos.cli.AbstractShellCommand; |
tom | 91c7bd0 | 2014-09-25 22:50:44 -0700 | [diff] [blame] | 30 | import org.onlab.onos.cli.Comparators; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.Device; |
| 32 | import org.onlab.onos.net.DeviceId; |
| 33 | import org.onlab.onos.net.device.DeviceService; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.flow.FlowEntry; |
| 35 | import org.onlab.onos.net.flow.FlowEntry.FlowEntryState; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.flow.FlowRuleService; |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.flow.criteria.Criterion; |
| 38 | import org.onlab.onos.net.flow.instructions.Instruction; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 39 | |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 40 | import java.util.Collections; |
| 41 | import java.util.List; |
| 42 | import java.util.Map; |
| 43 | |
| 44 | import static com.google.common.collect.Lists.newArrayList; |
| 45 | import static org.onlab.onos.cli.net.DevicesListCommand.getSortedDevices; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Lists all currently-known hosts. |
| 49 | */ |
| 50 | @Command(scope = "onos", name = "flows", |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 51 | description = "Lists all currently-known flows.") |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 52 | public class FlowsListCommand extends AbstractShellCommand { |
| 53 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 54 | public static final String ANY = "any"; |
| 55 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 56 | private static final String FMT = |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 57 | " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s, appId=%s"; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 58 | private static final String TFMT = " treatment=%s"; |
| 59 | private static final String SFMT = " selector=%s"; |
| 60 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 61 | @Argument(index = 1, name = "uri", description = "Device ID", |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 62 | required = false, multiValued = false) |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 63 | String uri = null; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 64 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 65 | @Argument(index = 0, name = "state", description = "Flow Rule state", |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 66 | required = false, multiValued = false) |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 67 | String state = null; |
alshabib | 64231d8 | 2014-09-25 18:25:31 -0700 | [diff] [blame] | 68 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 69 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 70 | protected void execute() { |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 71 | CoreService coreService = get(CoreService.class); |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 72 | DeviceService deviceService = get(DeviceService.class); |
| 73 | FlowRuleService service = get(FlowRuleService.class); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 74 | Map<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 75 | |
| 76 | if (outputJson()) { |
| 77 | print("%s", json(coreService, getSortedDevices(deviceService), flows)); |
| 78 | } else { |
| 79 | for (Device d : getSortedDevices(deviceService)) { |
| 80 | printFlows(d, flows.get(d), coreService); |
| 81 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 82 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 83 | } |
| 84 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 85 | /** |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 86 | * Produces a JSON array of flows grouped by the each device. |
| 87 | * |
| 88 | * @param coreService core service |
| 89 | * @param devices collection of devices to group flow by |
| 90 | * @param flows collection of flows per each device |
| 91 | * @return JSON array |
| 92 | */ |
| 93 | private JsonNode json(CoreService coreService, Iterable<Device> devices, |
| 94 | Map<Device, List<FlowEntry>> flows) { |
| 95 | ObjectMapper mapper = new ObjectMapper(); |
| 96 | ArrayNode result = mapper.createArrayNode(); |
| 97 | for (Device device : devices) { |
| 98 | result.add(json(coreService, mapper, device, flows.get(device))); |
| 99 | } |
| 100 | return result; |
| 101 | } |
| 102 | |
| 103 | // Produces JSON object with the flows of the given device. |
| 104 | private ObjectNode json(CoreService coreService, ObjectMapper mapper, |
| 105 | Device device, List<FlowEntry> flows) { |
| 106 | ObjectNode result = mapper.createObjectNode(); |
| 107 | ArrayNode array = mapper.createArrayNode(); |
| 108 | |
| 109 | for (FlowEntry flow : flows) { |
| 110 | array.add(json(coreService, mapper, flow)); |
| 111 | } |
| 112 | |
| 113 | result.put("device", device.id().toString()) |
| 114 | .put("flowCount", flows.size()) |
Thomas Vachuska | dfe48a7 | 2014-10-16 10:32:08 -0700 | [diff] [blame] | 115 | .set("flows", array); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 116 | return result; |
| 117 | } |
| 118 | |
| 119 | // Produces JSON structure with the specified flow data. |
| 120 | private ObjectNode json(CoreService coreService, ObjectMapper mapper, |
| 121 | FlowEntry flow) { |
| 122 | ObjectNode result = mapper.createObjectNode(); |
| 123 | ArrayNode crit = mapper.createArrayNode(); |
| 124 | for (Criterion c : flow.selector().criteria()) { |
| 125 | crit.add(c.toString()); |
| 126 | } |
| 127 | |
| 128 | ArrayNode instr = mapper.createArrayNode(); |
| 129 | for (Instruction i : flow.treatment().instructions()) { |
| 130 | instr.add(i.toString()); |
| 131 | } |
| 132 | |
| 133 | result.put("flowId", Long.toHexString(flow.id().value())) |
| 134 | .put("state", flow.state().toString()) |
| 135 | .put("bytes", flow.bytes()) |
| 136 | .put("packets", flow.packets()) |
| 137 | .put("life", flow.life()) |
| 138 | .put("appId", coreService.getAppId(flow.appId()).name()); |
| 139 | result.set("selector", crit); |
| 140 | result.set("treatment", instr); |
| 141 | return result; |
| 142 | } |
| 143 | |
| 144 | /** |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 145 | * Returns the list of devices sorted using the device ID URIs. |
| 146 | * |
| 147 | * @param service device service |
| 148 | * @return sorted device list |
| 149 | */ |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 150 | protected Map<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService, |
| 151 | FlowRuleService service) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 152 | Map<Device, List<FlowEntry>> flows = Maps.newHashMap(); |
| 153 | List<FlowEntry> rules; |
| 154 | FlowEntryState s = null; |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 155 | if (state != null && !state.equals("any")) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 156 | s = FlowEntryState.valueOf(state.toUpperCase()); |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 157 | } |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 158 | Iterable<Device> devices = uri == null ? deviceService.getDevices() : |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 159 | Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri))); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 160 | for (Device d : devices) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 161 | if (s == null) { |
| 162 | rules = newArrayList(service.getFlowEntries(d.id())); |
| 163 | } else { |
| 164 | rules = newArrayList(); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 165 | for (FlowEntry f : service.getFlowEntries(d.id())) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 166 | if (f.state().equals(s)) { |
| 167 | rules.add(f); |
| 168 | } |
| 169 | } |
| 170 | } |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 171 | Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 172 | flows.put(d, rules); |
| 173 | } |
| 174 | return flows; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Prints flows. |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 179 | * |
| 180 | * @param d the device |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 181 | * @param flows the set of flows for that device. |
| 182 | */ |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 183 | protected void printFlows(Device d, List<FlowEntry> flows, |
| 184 | CoreService coreService) { |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 185 | boolean empty = flows == null || flows.isEmpty(); |
| 186 | print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : flows.size()); |
| 187 | if (!empty) { |
| 188 | for (FlowEntry f : flows) { |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 189 | print(FMT, Long.toHexString(f.id().value()), f.state(), |
| 190 | f.bytes(), f.packets(), f.life(), f.priority(), |
| 191 | coreService.getAppId(f.appId()).name()); |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 192 | print(SFMT, f.selector().criteria()); |
| 193 | print(TFMT, f.treatment().instructions()); |
| 194 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 195 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Yuta HIGUCHI | e76a24d | 2014-09-27 00:48:34 -0700 | [diff] [blame] | 198 | } |