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 | |
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; |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 22 | |
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; |
Ray Milkey | 7522895 | 2015-03-02 09:01:59 -0800 | [diff] [blame] | 25 | import org.onosproject.core.ApplicationId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.cli.AbstractShellCommand; |
| 28 | import org.onosproject.cli.Comparators; |
| 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; |
| 35 | import org.onosproject.net.flow.criteria.Criterion; |
| 36 | import org.onosproject.net.flow.instructions.Instruction; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 37 | |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 38 | import java.util.Collections; |
| 39 | import java.util.List; |
| 40 | import java.util.Map; |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 41 | import java.util.SortedMap; |
| 42 | import java.util.TreeMap; |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 43 | |
| 44 | import static com.google.common.collect.Lists.newArrayList; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Lists all currently-known hosts. |
| 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 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 53 | public static final String ANY = "any"; |
| 54 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 55 | private static final String FMT = |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 56 | " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s, tableId=%s appId=%s"; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 57 | private static final String TFMT = " treatment=%s"; |
| 58 | private static final String SFMT = " selector=%s"; |
| 59 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 60 | @Argument(index = 1, name = "uri", description = "Device ID", |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 61 | required = false, multiValued = false) |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 62 | String uri = null; |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 63 | |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 64 | @Argument(index = 0, name = "state", description = "Flow Rule state", |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 65 | required = false, multiValued = false) |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 66 | String state = null; |
alshabib | 64231d8 | 2014-09-25 18:25:31 -0700 | [diff] [blame] | 67 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 68 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 69 | protected void execute() { |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 70 | CoreService coreService = get(CoreService.class); |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 71 | DeviceService deviceService = get(DeviceService.class); |
| 72 | FlowRuleService service = get(FlowRuleService.class); |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 73 | SortedMap<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 74 | |
| 75 | if (outputJson()) { |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 76 | print("%s", json(coreService, flows.keySet(), flows)); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 77 | } else { |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 78 | flows.forEach((device, flow) -> printFlows(device, flow, coreService)); |
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(); |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 126 | for (Instruction i : flow.treatment().allInstructions()) { |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 127 | instr.add(i.toString()); |
| 128 | } |
| 129 | |
Ray Milkey | 7522895 | 2015-03-02 09:01:59 -0800 | [diff] [blame] | 130 | ApplicationId appCoreId = coreService.getAppId(flow.appId()); |
| 131 | String appName = appCoreId == null ? |
| 132 | Short.toString(flow.appId()) |
| 133 | : appCoreId.name(); |
| 134 | |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 135 | result.put("flowId", Long.toHexString(flow.id().value())) |
| 136 | .put("state", flow.state().toString()) |
| 137 | .put("bytes", flow.bytes()) |
| 138 | .put("packets", flow.packets()) |
| 139 | .put("life", flow.life()) |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 140 | .put("tableId", flow.type().toString()) |
Ray Milkey | 7522895 | 2015-03-02 09:01:59 -0800 | [diff] [blame] | 141 | .put("appId", appName); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 142 | result.set("selector", crit); |
| 143 | result.set("treatment", instr); |
| 144 | return result; |
| 145 | } |
| 146 | |
| 147 | /** |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 148 | * Returns the list of devices sorted using the device ID URIs. |
| 149 | * |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 150 | * @param deviceService device service |
| 151 | * @param service flow rule service |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 152 | * @return sorted device list |
| 153 | */ |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 154 | protected SortedMap<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService, |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 155 | FlowRuleService service) { |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 156 | SortedMap<Device, List<FlowEntry>> flows = new TreeMap<>(Comparators.ELEMENT_COMPARATOR); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 157 | List<FlowEntry> rules; |
| 158 | FlowEntryState s = null; |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 159 | if (state != null && !state.equals("any")) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 160 | s = FlowEntryState.valueOf(state.toUpperCase()); |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 161 | } |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 162 | Iterable<Device> devices = uri == null ? deviceService.getDevices() : |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 163 | Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri))); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 164 | for (Device d : devices) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 165 | if (s == null) { |
| 166 | rules = newArrayList(service.getFlowEntries(d.id())); |
| 167 | } else { |
| 168 | rules = newArrayList(); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 169 | for (FlowEntry f : service.getFlowEntries(d.id())) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 170 | if (f.state().equals(s)) { |
| 171 | rules.add(f); |
| 172 | } |
| 173 | } |
| 174 | } |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 175 | rules.sort(Comparators.FLOW_RULE_COMPARATOR); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 176 | flows.put(d, rules); |
| 177 | } |
| 178 | return flows; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Prints flows. |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 183 | * |
| 184 | * @param d the device |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 185 | * @param flows the set of flows for that device |
| 186 | * @param coreService core system service |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 187 | */ |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 188 | protected void printFlows(Device d, List<FlowEntry> flows, |
| 189 | CoreService coreService) { |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 190 | boolean empty = flows == null || flows.isEmpty(); |
| 191 | print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : flows.size()); |
| 192 | if (!empty) { |
| 193 | for (FlowEntry f : flows) { |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 194 | print(FMT, Long.toHexString(f.id().value()), f.state(), |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 195 | f.bytes(), f.packets(), f.life(), f.priority(), f.type(), |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 196 | coreService.getAppId(f.appId()).name()); |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 197 | print(SFMT, f.selector().criteria()); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 198 | print(TFMT, f.treatment()); |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 199 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 200 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Yuta HIGUCHI | e76a24d | 2014-09-27 00:48:34 -0700 | [diff] [blame] | 203 | } |