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