Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | */ |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 16 | package org.onlab.onos.cli.net; |
| 17 | |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [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; |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.commands.Argument; |
| 24 | import org.apache.karaf.shell.commands.Command; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 25 | import org.onlab.onos.core.CoreService; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 26 | import org.onlab.onos.cli.AbstractShellCommand; |
tom | 91c7bd0 | 2014-09-25 22:50:44 -0700 | [diff] [blame] | 27 | import org.onlab.onos.cli.Comparators; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.Device; |
| 29 | import org.onlab.onos.net.DeviceId; |
| 30 | import org.onlab.onos.net.device.DeviceService; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.flow.FlowEntry; |
| 32 | import org.onlab.onos.net.flow.FlowEntry.FlowEntryState; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.flow.FlowRuleService; |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.flow.criteria.Criterion; |
| 35 | import org.onlab.onos.net.flow.instructions.Instruction; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 36 | |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 37 | import java.util.Collections; |
| 38 | import java.util.List; |
| 39 | import java.util.Map; |
| 40 | |
| 41 | import static com.google.common.collect.Lists.newArrayList; |
| 42 | import static org.onlab.onos.cli.net.DevicesListCommand.getSortedDevices; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Lists all currently-known hosts. |
| 46 | */ |
| 47 | @Command(scope = "onos", name = "flows", |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 48 | description = "Lists all currently-known flows.") |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 49 | public class FlowsListCommand extends AbstractShellCommand { |
| 50 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 51 | public static final String ANY = "any"; |
| 52 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 53 | private static final String FMT = |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 54 | " 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] | 55 | private static final String TFMT = " treatment=%s"; |
| 56 | private static final String SFMT = " selector=%s"; |
| 57 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 58 | @Argument(index = 1, name = "uri", description = "Device ID", |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 59 | required = false, multiValued = false) |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 60 | String uri = null; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 61 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 62 | @Argument(index = 0, name = "state", description = "Flow Rule state", |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 63 | required = false, multiValued = false) |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 64 | String state = null; |
alshabib | 64231d8 | 2014-09-25 18:25:31 -0700 | [diff] [blame] | 65 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 66 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 67 | protected void execute() { |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 68 | CoreService coreService = get(CoreService.class); |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 69 | DeviceService deviceService = get(DeviceService.class); |
| 70 | FlowRuleService service = get(FlowRuleService.class); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 71 | Map<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 72 | |
| 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 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 79 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 80 | } |
| 81 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 82 | /** |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 83 | * 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 Vachuska | dfe48a7 | 2014-10-16 10:32:08 -0700 | [diff] [blame] | 112 | .set("flows", array); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 113 | 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 | /** |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 142 | * Returns the list of devices sorted using the device ID URIs. |
| 143 | * |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 144 | * @param deviceService device service |
| 145 | * @param service flow rule service |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 146 | * @return sorted device list |
| 147 | */ |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 148 | protected Map<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService, |
| 149 | FlowRuleService service) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 150 | Map<Device, List<FlowEntry>> flows = Maps.newHashMap(); |
| 151 | List<FlowEntry> rules; |
| 152 | FlowEntryState s = null; |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 153 | if (state != null && !state.equals("any")) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 154 | s = FlowEntryState.valueOf(state.toUpperCase()); |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 155 | } |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 156 | Iterable<Device> devices = uri == null ? deviceService.getDevices() : |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 157 | Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri))); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 158 | for (Device d : devices) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 159 | if (s == null) { |
| 160 | rules = newArrayList(service.getFlowEntries(d.id())); |
| 161 | } else { |
| 162 | rules = newArrayList(); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 163 | for (FlowEntry f : service.getFlowEntries(d.id())) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 164 | if (f.state().equals(s)) { |
| 165 | rules.add(f); |
| 166 | } |
| 167 | } |
| 168 | } |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 169 | Collections.sort(rules, Comparators.FLOW_RULE_COMPARATOR); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 170 | flows.put(d, rules); |
| 171 | } |
| 172 | return flows; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Prints flows. |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 177 | * |
| 178 | * @param d the device |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 179 | * @param flows the set of flows for that device |
| 180 | * @param coreService core system service |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 181 | */ |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 182 | protected void printFlows(Device d, List<FlowEntry> flows, |
| 183 | CoreService coreService) { |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 184 | 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) { |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 188 | print(FMT, Long.toHexString(f.id().value()), f.state(), |
| 189 | f.bytes(), f.packets(), f.life(), f.priority(), |
| 190 | coreService.getAppId(f.appId()).name()); |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 191 | print(SFMT, f.selector().criteria()); |
| 192 | print(TFMT, f.treatment().instructions()); |
| 193 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 194 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Yuta HIGUCHI | e76a24d | 2014-09-27 00:48:34 -0700 | [diff] [blame] | 197 | } |