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 | |
jcc | f988bf5 | 2015-05-05 19:02:50 +0800 | [diff] [blame] | 18 | import static com.google.common.collect.Lists.newArrayList; |
| 19 | |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 20 | import java.util.Collections; |
| 21 | import java.util.List; |
| 22 | import java.util.Map; |
| 23 | import java.util.SortedMap; |
| 24 | import java.util.TreeMap; |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 25 | |
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; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.cli.AbstractShellCommand; |
| 29 | import org.onosproject.cli.Comparators; |
Brian O'Connor | 9cc799c | 2015-06-04 19:35:41 -0700 | [diff] [blame] | 30 | import org.onosproject.core.ApplicationId; |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 31 | import org.onosproject.core.CoreService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.Device; |
| 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.device.DeviceService; |
| 35 | import org.onosproject.net.flow.FlowEntry; |
| 36 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
| 37 | import org.onosproject.net.flow.FlowRuleService; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 38 | |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 39 | import com.fasterxml.jackson.databind.JsonNode; |
| 40 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 41 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 42 | import com.fasterxml.jackson.databind.node.ObjectNode; |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 43 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 44 | /** |
jcc | f988bf5 | 2015-05-05 19:02:50 +0800 | [diff] [blame] | 45 | * Lists all currently-known flows. |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 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 = |
jcc | f988bf5 | 2015-05-05 19:02:50 +0800 | [diff] [blame] | 54 | " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s, tableId=%s appId=%s, payLoad=%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); |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 71 | SortedMap<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 72 | |
| 73 | if (outputJson()) { |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 74 | print("%s", json(flows.keySet(), flows)); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 75 | } else { |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 76 | flows.forEach((device, flow) -> printFlows(device, flow, coreService)); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 77 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 78 | } |
| 79 | |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 80 | /** |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 81 | * Produces a JSON array of flows grouped by the each device. |
| 82 | * |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 83 | * @param devices collection of devices to group flow by |
| 84 | * @param flows collection of flows per each device |
| 85 | * @return JSON array |
| 86 | */ |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 87 | private JsonNode json(Iterable<Device> devices, |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 88 | Map<Device, List<FlowEntry>> flows) { |
| 89 | ObjectMapper mapper = new ObjectMapper(); |
| 90 | ArrayNode result = mapper.createArrayNode(); |
| 91 | for (Device device : devices) { |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 92 | result.add(json(mapper, device, flows.get(device))); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 93 | } |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | // Produces JSON object with the flows of the given device. |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 98 | private ObjectNode json(ObjectMapper mapper, |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 99 | Device device, List<FlowEntry> flows) { |
| 100 | ObjectNode result = mapper.createObjectNode(); |
| 101 | ArrayNode array = mapper.createArrayNode(); |
| 102 | |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 103 | flows.forEach(flow -> array.add(jsonForEntity(flow, FlowEntry.class))); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 104 | |
| 105 | result.put("device", device.id().toString()) |
| 106 | .put("flowCount", flows.size()) |
Thomas Vachuska | dfe48a7 | 2014-10-16 10:32:08 -0700 | [diff] [blame] | 107 | .set("flows", array); |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 108 | return result; |
| 109 | } |
| 110 | |
Thomas Vachuska | bb0272e | 2014-10-16 09:32:04 -0700 | [diff] [blame] | 111 | /** |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 112 | * Returns the list of devices sorted using the device ID URIs. |
| 113 | * |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 114 | * @param deviceService device service |
| 115 | * @param service flow rule service |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 116 | * @return sorted device list |
| 117 | */ |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 118 | protected SortedMap<Device, List<FlowEntry>> getSortedFlows(DeviceService deviceService, |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 119 | FlowRuleService service) { |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 120 | SortedMap<Device, List<FlowEntry>> flows = new TreeMap<>(Comparators.ELEMENT_COMPARATOR); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 121 | List<FlowEntry> rules; |
| 122 | FlowEntryState s = null; |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 123 | if (state != null && !state.equals("any")) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 124 | s = FlowEntryState.valueOf(state.toUpperCase()); |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 125 | } |
Saurav Das | 554f5e7 | 2015-10-27 10:28:19 -0700 | [diff] [blame] | 126 | Iterable<Device> devices = null; |
| 127 | if (uri == null) { |
| 128 | devices = deviceService.getDevices(); |
| 129 | } else { |
| 130 | Device dev = deviceService.getDevice(DeviceId.deviceId(uri)); |
| 131 | devices = (dev == null) ? deviceService.getDevices() |
| 132 | : Collections.singletonList(dev); |
| 133 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 134 | for (Device d : devices) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 135 | if (s == null) { |
| 136 | rules = newArrayList(service.getFlowEntries(d.id())); |
| 137 | } else { |
| 138 | rules = newArrayList(); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 139 | for (FlowEntry f : service.getFlowEntries(d.id())) { |
alshabib | 144a294 | 2014-09-25 18:44:02 -0700 | [diff] [blame] | 140 | if (f.state().equals(s)) { |
| 141 | rules.add(f); |
| 142 | } |
| 143 | } |
| 144 | } |
Yuta HIGUCHI | 8791a81 | 2015-02-10 09:43:52 -0800 | [diff] [blame] | 145 | rules.sort(Comparators.FLOW_RULE_COMPARATOR); |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 146 | flows.put(d, rules); |
| 147 | } |
| 148 | return flows; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Prints flows. |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 153 | * |
| 154 | * @param d the device |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 155 | * @param flows the set of flows for that device |
| 156 | * @param coreService core system service |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 157 | */ |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 158 | protected void printFlows(Device d, List<FlowEntry> flows, |
| 159 | CoreService coreService) { |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 160 | boolean empty = flows == null || flows.isEmpty(); |
| 161 | print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : flows.size()); |
| 162 | if (!empty) { |
| 163 | for (FlowEntry f : flows) { |
Brian O'Connor | 9cc799c | 2015-06-04 19:35:41 -0700 | [diff] [blame] | 164 | ApplicationId appId = coreService.getAppId(f.appId()); |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 165 | print(FMT, Long.toHexString(f.id().value()), f.state(), |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 166 | f.bytes(), f.packets(), f.life(), f.priority(), f.tableId(), |
Brian O'Connor | 9cc799c | 2015-06-04 19:35:41 -0700 | [diff] [blame] | 167 | appId != null ? appId.name() : "<none>", |
jcc | f988bf5 | 2015-05-05 19:02:50 +0800 | [diff] [blame] | 168 | f.payLoad() == null ? null : f.payLoad().payLoad().toString()); |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 169 | print(SFMT, f.selector().criteria()); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 170 | print(TFMT, f.treatment()); |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 171 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 172 | } |
alshabib | 9290eea | 2014-09-22 11:58:17 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Yuta HIGUCHI | e76a24d | 2014-09-27 00:48:34 -0700 | [diff] [blame] | 175 | } |