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