Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.cli.net; |
| 17 | |
| 18 | import static com.google.common.collect.Lists.newArrayList; |
| 19 | |
| 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; |
| 25 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 26 | import org.apache.karaf.shell.api.action.Argument; |
| 27 | import org.apache.karaf.shell.api.action.Command; |
| 28 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 29 | import org.apache.karaf.shell.api.action.Option; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 30 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | c747729 | 2016-03-11 10:53:43 -0800 | [diff] [blame] | 31 | import org.onosproject.utils.Comparators; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [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.FlowRuleService; |
| 36 | import org.onosproject.net.flow.TableStatisticsEntry; |
| 37 | |
| 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; |
| 42 | |
| 43 | /** |
| 44 | * Lists port statistic of all ports in the system. |
| 45 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 46 | @Service |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 47 | @Command(scope = "onos", name = "tablestats", |
| 48 | description = "Lists statistics of all tables in the device") |
| 49 | public class TableStatisticsCommand extends AbstractShellCommand { |
| 50 | |
| 51 | @Option(name = "-t", aliases = "--table", description = "Show human readable table format for statistics", |
| 52 | required = false, multiValued = false) |
| 53 | private boolean table = false; |
| 54 | |
| 55 | @Argument(index = 0, name = "uri", description = "Device ID", |
| 56 | required = false, multiValued = false) |
| 57 | String uri = null; |
| 58 | |
| 59 | private static final String FORMAT = |
hjtsao | 1a4333c | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 60 | " table=%s, active=%s, lookedup=%s, matched=%s, maxsize=%s"; |
| 61 | private static final String NA = "N/A"; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 62 | |
| 63 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 64 | protected void doExecute() { |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 65 | FlowRuleService flowService = get(FlowRuleService.class); |
| 66 | DeviceService deviceService = get(DeviceService.class); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 67 | SortedMap<Device, List<TableStatisticsEntry>> deviceTableStats = |
| 68 | getSortedTableStats(deviceService, flowService); |
| 69 | |
| 70 | if (outputJson()) { |
| 71 | print("%s", json(deviceTableStats.keySet(), deviceTableStats)); |
| 72 | } else { |
| 73 | deviceTableStats.forEach((device, tableStats) -> printTableStats(device, tableStats)); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Produces a JSON array of table statistics grouped by the each device. |
| 79 | * |
| 80 | * @param devices collection of devices |
| 81 | * @param deviceTableStats collection of table statistics per each device |
| 82 | * @return JSON array |
| 83 | */ |
| 84 | private JsonNode json(Iterable<Device> devices, |
| 85 | Map<Device, List<TableStatisticsEntry>> deviceTableStats) { |
| 86 | ObjectMapper mapper = new ObjectMapper(); |
| 87 | ArrayNode result = mapper.createArrayNode(); |
| 88 | for (Device device : devices) { |
| 89 | result.add(json(mapper, device, deviceTableStats.get(device))); |
| 90 | } |
| 91 | return result; |
| 92 | } |
| 93 | |
| 94 | // Produces JSON object with the table statistics of the given device. |
| 95 | private ObjectNode json(ObjectMapper mapper, |
| 96 | Device device, List<TableStatisticsEntry> tableStats) { |
| 97 | ObjectNode result = mapper.createObjectNode(); |
| 98 | ArrayNode array = mapper.createArrayNode(); |
| 99 | |
| 100 | tableStats.forEach(tableStat -> array.add(jsonForEntity(tableStat, TableStatisticsEntry.class))); |
| 101 | |
| 102 | result.put("device", device.id().toString()) |
| 103 | .put("tableCount", tableStats.size()) |
| 104 | .set("tables", array); |
| 105 | return result; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Prints flow table statistics. |
| 110 | * |
| 111 | * @param d the device |
| 112 | * @param tableStats the set of flow table statistics for that device |
| 113 | */ |
| 114 | protected void printTableStats(Device d, |
| 115 | List<TableStatisticsEntry> tableStats) { |
| 116 | boolean empty = tableStats == null || tableStats.isEmpty(); |
| 117 | print("deviceId=%s, tableCount=%d", d.id(), empty ? 0 : tableStats.size()); |
| 118 | if (!empty) { |
| 119 | for (TableStatisticsEntry t : tableStats) { |
hjtsao | 1a4333c | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 120 | print(FORMAT, t.table(), t.activeFlowEntries(), |
| 121 | t.hasPacketsLookedup() ? t.packetsLookedup() : NA, t.packetsMatched(), |
| 122 | t.hasMaxSize() ? t.maxSize() : NA); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Returns the list of table statistics sorted using the device ID URIs and table IDs. |
| 129 | * |
| 130 | * @param deviceService device service |
| 131 | * @param flowService flow rule service |
| 132 | * @return sorted table statistics list |
| 133 | */ |
| 134 | protected SortedMap<Device, List<TableStatisticsEntry>> getSortedTableStats(DeviceService deviceService, |
hjtsao | 1a4333c | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 135 | FlowRuleService flowService) { |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 136 | SortedMap<Device, List<TableStatisticsEntry>> deviceTableStats = new TreeMap<>(Comparators.ELEMENT_COMPARATOR); |
| 137 | List<TableStatisticsEntry> tableStatsList; |
| 138 | Iterable<Device> devices = uri == null ? deviceService.getDevices() : |
| 139 | Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri))); |
| 140 | for (Device d : devices) { |
| 141 | tableStatsList = newArrayList(flowService.getFlowTableStatistics(d.id())); |
| 142 | tableStatsList.sort((p1, p2) -> Integer.valueOf(p1.tableId()).compareTo(Integer.valueOf(p2.tableId()))); |
| 143 | deviceTableStats.put(d, tableStatsList); |
| 144 | } |
| 145 | return deviceTableStats; |
| 146 | } |
| 147 | |
hjtsao | 1a4333c | 2018-10-22 11:02:00 -0700 | [diff] [blame] | 148 | } |