ssyoon90 | a98825a | 2015-08-26 00:48:15 +0900 | [diff] [blame] | 1 | /*
|
| 2 | * Copyright 2015 Open Networking Laboratory
|
| 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 |
|
| 17 | package org.onosproject.cli.net;
|
| 18 |
|
| 19 | import org.apache.karaf.shell.commands.Argument;
|
| 20 | import org.apache.karaf.shell.commands.Command;
|
| 21 | import org.apache.karaf.shell.commands.Option;
|
| 22 | import org.onosproject.cli.AbstractShellCommand;
|
| 23 | import org.onosproject.net.ConnectPoint;
|
| 24 | import org.onosproject.net.Device;
|
| 25 | import org.onosproject.net.DeviceId;
|
| 26 | import org.onosproject.net.Port;
|
| 27 | import org.onosproject.net.PortNumber;
|
| 28 | import org.onosproject.net.device.DeviceService;
|
| 29 | import org.onosproject.net.flow.TypedStoredFlowEntry;
|
| 30 | import org.onosproject.net.flow.instructions.Instruction;
|
| 31 | import org.onosproject.net.statistic.FlowStatisticService;
|
| 32 | import org.onosproject.net.statistic.SummaryFlowEntryWithLoad;
|
| 33 | import org.onosproject.net.statistic.TypedFlowEntryWithLoad;
|
| 34 |
|
| 35 | import java.util.List;
|
| 36 | import java.util.Map;
|
| 37 |
|
| 38 | import static org.onosproject.net.DeviceId.deviceId;
|
| 39 | import static org.onosproject.net.PortNumber.portNumber;
|
| 40 |
|
| 41 | /**
|
| 42 | * Fetches flow statistics with a flow type and instruction type.
|
| 43 | */
|
| 44 | @Command(scope = "onos", name = "get-flow-stats",
|
| 45 | description = "Fetches flow stats for a connection point with given flow type and instruction type")
|
| 46 | public class GetFlowStatistics extends AbstractShellCommand {
|
| 47 | @Argument(index = 0, name = "devicePort",
|
| 48 | description = "Device[/Port] connectPoint Description",
|
| 49 | required = true, multiValued = false)
|
| 50 | String devicePort = null;
|
| 51 |
|
| 52 | @Option(name = "-s", aliases = "--summary",
|
| 53 | description = "Show flow stats summary",
|
| 54 | required = false, multiValued = false)
|
| 55 | boolean showSummary = true; // default summary
|
| 56 |
|
| 57 | @Option(name = "-a", aliases = "--all",
|
| 58 | description = "Show flow stats all",
|
| 59 | required = false, multiValued = false)
|
| 60 | boolean showAll = false;
|
| 61 |
|
| 62 | @Option(name = "-t", aliases = "--topn",
|
| 63 | description = "Show flow stats topn",
|
| 64 | required = false, multiValued = false)
|
| 65 | String showTopn = null;
|
| 66 |
|
| 67 | @Option(name = "-f", aliases = "--flowType",
|
| 68 | description = "Flow live type, It includes IMMEDIATE, SHORT, MID, LONG, UNKNOWN"
|
| 69 | + ", and is valid with -a or -t option only",
|
| 70 | required = false, multiValued = false)
|
| 71 | String flowLiveType = null;
|
| 72 |
|
| 73 | @Option(name = "-i", aliases = "--instructionType",
|
| 74 | description = "Flow instruction type, It includes DROP, OUTPUT, GROUP, L0MODIFICATION, L2MODIFICATION,"
|
| 75 | + " TABLE, L3MODIFICATION, METADATA"
|
| 76 | + ", and is valid with -a or -t option only",
|
| 77 | required = false, multiValued = false)
|
| 78 | String instructionType = null;
|
| 79 |
|
| 80 | @Override
|
| 81 | protected void execute() {
|
| 82 | DeviceService deviceService = get(DeviceService.class);
|
| 83 | FlowStatisticService flowStatsService = get(FlowStatisticService.class);
|
| 84 |
|
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 85 | String deviceUri = getDeviceId(devicePort);
|
| 86 | String portUri = getPortNumber(devicePort);
|
ssyoon90 | a98825a | 2015-08-26 00:48:15 +0900 | [diff] [blame] | 87 |
|
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 88 | DeviceId ingressDeviceId = deviceId(deviceUri);
|
ssyoon90 | a98825a | 2015-08-26 00:48:15 +0900 | [diff] [blame] | 89 | PortNumber ingressPortNumber;
|
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 90 | if (portUri.length() == 0) {
|
ssyoon90 | a98825a | 2015-08-26 00:48:15 +0900 | [diff] [blame] | 91 | ingressPortNumber = null;
|
| 92 | } else {
|
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 93 | ingressPortNumber = portNumber(portUri);
|
ssyoon90 | a98825a | 2015-08-26 00:48:15 +0900 | [diff] [blame] | 94 | }
|
| 95 |
|
| 96 | Device device = deviceService.getDevice(ingressDeviceId);
|
| 97 | if (device == null) {
|
| 98 | error("No such device %s", ingressDeviceId.uri());
|
| 99 | return;
|
| 100 | }
|
| 101 |
|
| 102 | if (ingressPortNumber != null) {
|
| 103 | Port port = deviceService.getPort(ingressDeviceId, ingressPortNumber);
|
| 104 | if (port == null) {
|
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 105 | error("No such port %s on device %s", portUri, ingressDeviceId.uri());
|
ssyoon90 | a98825a | 2015-08-26 00:48:15 +0900 | [diff] [blame] | 106 | return;
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | if (flowLiveType != null) {
|
| 111 | flowLiveType = flowLiveType.toUpperCase();
|
| 112 | }
|
| 113 | if (instructionType != null) {
|
| 114 | instructionType = instructionType.toUpperCase();
|
| 115 | }
|
| 116 |
|
| 117 | // convert String to FlowLiveType and check validity
|
| 118 | TypedStoredFlowEntry.FlowLiveType inLiveType;
|
| 119 | if (flowLiveType == null) {
|
| 120 | inLiveType = null;
|
| 121 | } else {
|
| 122 | inLiveType = getFlowLiveType(flowLiveType);
|
| 123 | if (inLiveType == null) {
|
| 124 | error("Invalid flow live type [%s] error", flowLiveType);
|
| 125 | return;
|
| 126 | }
|
| 127 | }
|
| 128 | // convert String to InstructionType and check validity
|
| 129 | Instruction.Type inInstructionType;
|
| 130 | if (instructionType == null) {
|
| 131 | inInstructionType = null;
|
| 132 | } else {
|
| 133 | inInstructionType = getInstructionType(instructionType);
|
| 134 | if (inInstructionType == null) {
|
| 135 | error("Invalid instruction type [%s] error", instructionType);
|
| 136 | return;
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | if (showTopn != null) {
|
| 141 | int topn = Integer.parseInt(showTopn);
|
| 142 |
|
| 143 | if (topn <= 0) {
|
| 144 | topn = 100; //default value
|
| 145 | } else if (topn > 1000) {
|
| 146 | topn = 1000; //max value
|
| 147 | }
|
| 148 |
|
| 149 | // print show topn head line with type
|
| 150 | print("deviceId=%s, show TOPN=%s flows, live type=%s, instruction type=%s",
|
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 151 | deviceUri,
|
ssyoon90 | a98825a | 2015-08-26 00:48:15 +0900 | [diff] [blame] | 152 | Integer.toString(topn),
|
| 153 | flowLiveType == null ? "ALL" : flowLiveType,
|
| 154 | instructionType == null ? "ALL" : instructionType);
|
| 155 | if (ingressPortNumber == null) {
|
| 156 | Map<ConnectPoint, List<TypedFlowEntryWithLoad>> typedFlowLoadMap =
|
| 157 | flowStatsService.loadTopnByType(device, inLiveType, inInstructionType, topn);
|
| 158 | // print all ports topn flows load for a given device
|
| 159 | for (ConnectPoint cp : typedFlowLoadMap.keySet()) {
|
| 160 | printPortFlowsLoad(cp, typedFlowLoadMap.get(cp));
|
| 161 | }
|
| 162 | } else {
|
| 163 | List<TypedFlowEntryWithLoad> typedFlowLoad =
|
| 164 | flowStatsService.loadTopnByType(device, ingressPortNumber, inLiveType, inInstructionType, topn);
|
| 165 | // print device/port topn flows load
|
| 166 | ConnectPoint cp = new ConnectPoint(ingressDeviceId, ingressPortNumber);
|
| 167 | printPortFlowsLoad(cp, typedFlowLoad);
|
| 168 | }
|
| 169 | } else if (showAll) { // is true?
|
| 170 | // print show all head line with type
|
| 171 | print("deviceId=%s, show ALL flows, live type=%s, instruction type=%s",
|
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 172 | deviceUri,
|
ssyoon90 | a98825a | 2015-08-26 00:48:15 +0900 | [diff] [blame] | 173 | flowLiveType == null ? "ALL" : flowLiveType,
|
| 174 | instructionType == null ? "ALL" : instructionType);
|
| 175 | if (ingressPortNumber == null) {
|
| 176 | Map<ConnectPoint, List<TypedFlowEntryWithLoad>> typedFlowLoadMap =
|
| 177 | flowStatsService.loadAllByType(device, inLiveType, inInstructionType);
|
| 178 | // print all ports all flows load for a given device
|
| 179 | for (ConnectPoint cp : typedFlowLoadMap.keySet()) {
|
| 180 | printPortFlowsLoad(cp, typedFlowLoadMap.get(cp));
|
| 181 | }
|
| 182 | } else {
|
| 183 | List<TypedFlowEntryWithLoad> typedFlowLoad =
|
| 184 | flowStatsService.loadAllByType(device, ingressPortNumber, inLiveType, inInstructionType);
|
| 185 | // print device/port all flows load
|
| 186 | ConnectPoint cp = new ConnectPoint(ingressDeviceId, ingressPortNumber);
|
| 187 | printPortFlowsLoad(cp, typedFlowLoad);
|
| 188 | }
|
| 189 | } else { // if (showSummary == true) //always is true
|
| 190 | // print show summary head line
|
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 191 | print("deviceId=%s, show SUMMARY flows", deviceUri);
|
ssyoon90 | a98825a | 2015-08-26 00:48:15 +0900 | [diff] [blame] | 192 | if (ingressPortNumber == null) {
|
| 193 | Map<ConnectPoint, SummaryFlowEntryWithLoad> summaryFlowLoadMap =
|
| 194 | flowStatsService.loadSummary(device);
|
| 195 | // print all ports flow load summary for a given device
|
| 196 | for (ConnectPoint cp : summaryFlowLoadMap.keySet()) {
|
| 197 | printPortSummaryLoad(cp, summaryFlowLoadMap.get(cp));
|
| 198 | }
|
| 199 | } else {
|
| 200 | SummaryFlowEntryWithLoad summaryFlowLoad =
|
| 201 | flowStatsService.loadSummary(device, ingressPortNumber);
|
| 202 | // print device/port flow load summary
|
| 203 | ConnectPoint cp = new ConnectPoint(ingressDeviceId, ingressPortNumber);
|
| 204 | printPortSummaryLoad(cp, summaryFlowLoad);
|
| 205 | }
|
| 206 | }
|
| 207 | }
|
| 208 |
|
| 209 | /**
|
| 210 | * Extracts the port number portion of the ConnectPoint.
|
| 211 | *
|
| 212 | * @param deviceString string representing the device/port
|
| 213 | * @return port number as a string, empty string if the port is not found
|
| 214 | */
|
| 215 | private String getPortNumber(String deviceString) {
|
| 216 | if (deviceString == null) {
|
| 217 | return "";
|
| 218 | }
|
| 219 |
|
| 220 | int slash = deviceString.indexOf('/');
|
| 221 | if (slash <= 0) {
|
| 222 | return ""; // return when no port number
|
| 223 | }
|
| 224 | return deviceString.substring(slash + 1, deviceString.length());
|
| 225 | }
|
| 226 |
|
| 227 | /**
|
| 228 | * Extracts the device ID portion of the ConnectPoint.
|
| 229 | *
|
| 230 | * @param deviceString string representing the device/port
|
| 231 | * @return device ID string
|
| 232 | */
|
| 233 | private String getDeviceId(String deviceString) {
|
| 234 | if (deviceString == null) {
|
| 235 | return "";
|
| 236 | }
|
| 237 |
|
| 238 | int slash = deviceString.indexOf('/');
|
| 239 | if (slash <= 0) {
|
| 240 | return deviceString; // return only included device ID
|
| 241 | }
|
| 242 | return deviceString.substring(0, slash);
|
| 243 | }
|
| 244 |
|
| 245 | /**
|
| 246 | * converts string of flow live type to FloeLiveType enum.
|
| 247 | *
|
| 248 | * @param liveType string representing the flow live type
|
| 249 | * @return TypedStoredFlowEntry.FlowLiveType
|
| 250 | */
|
| 251 | private TypedStoredFlowEntry.FlowLiveType getFlowLiveType(String liveType) {
|
| 252 | String liveTypeUC = liveType.toUpperCase();
|
| 253 |
|
| 254 | if (liveTypeUC.equals("IMMEDIATE")) {
|
| 255 | return TypedStoredFlowEntry.FlowLiveType.IMMEDIATE_FLOW;
|
| 256 | } else if (liveTypeUC.equals("SHORT")) {
|
| 257 | return TypedStoredFlowEntry.FlowLiveType.SHORT_FLOW;
|
| 258 | } else if (liveTypeUC.equals("MID")) {
|
| 259 | return TypedStoredFlowEntry.FlowLiveType.MID_FLOW;
|
| 260 | } else if (liveTypeUC.equals("LONG")) {
|
| 261 | return TypedStoredFlowEntry.FlowLiveType.LONG_FLOW;
|
| 262 | } else if (liveTypeUC.equals("UNKNOWN")) {
|
| 263 | return TypedStoredFlowEntry.FlowLiveType.UNKNOWN_FLOW;
|
| 264 | } else {
|
| 265 | return null; // flow live type error
|
| 266 | }
|
| 267 | }
|
| 268 |
|
| 269 | /**
|
| 270 | * converts string of instruction type to Instruction type enum.
|
| 271 | *
|
| 272 | * @param instType string representing the instruction type
|
| 273 | * @return Instruction.Type
|
| 274 | */
|
| 275 | private Instruction.Type getInstructionType(String instType) {
|
| 276 | String instTypeUC = instType.toUpperCase();
|
| 277 |
|
| 278 | if (instTypeUC.equals("DROP")) {
|
| 279 | return Instruction.Type.DROP;
|
| 280 | } else if (instTypeUC.equals("OUTPUT")) {
|
| 281 | return Instruction.Type.OUTPUT;
|
| 282 | } else if (instTypeUC.equals("GROUP")) {
|
| 283 | return Instruction.Type.GROUP;
|
| 284 | } else if (instTypeUC.equals("L0MODIFICATION")) {
|
| 285 | return Instruction.Type.L0MODIFICATION;
|
| 286 | } else if (instTypeUC.equals("L2MODIFICATION")) {
|
| 287 | return Instruction.Type.L2MODIFICATION;
|
| 288 | } else if (instTypeUC.equals("TABLE")) {
|
| 289 | return Instruction.Type.TABLE;
|
| 290 | } else if (instTypeUC.equals("L3MODIFICATION")) {
|
| 291 | return Instruction.Type.L3MODIFICATION;
|
| 292 | } else if (instTypeUC.equals("METADATA")) {
|
| 293 | return Instruction.Type.METADATA;
|
| 294 | } else {
|
| 295 | return null; // instruction type error
|
| 296 | }
|
| 297 | }
|
| 298 |
|
| 299 | private void printPortFlowsLoad(ConnectPoint cp, List<TypedFlowEntryWithLoad> typedFlowLoad) {
|
| 300 | print(" deviceId/Port=%s/%s, %s flows", cp.elementId(), cp.port(), typedFlowLoad.size());
|
| 301 | for (TypedFlowEntryWithLoad tfel: typedFlowLoad) {
|
| 302 | TypedStoredFlowEntry tfe = tfel.typedStoredFlowEntry();
|
| 303 | print(" flowId=%s, state=%s, liveType=%s, life=%s -> %s",
|
| 304 | Long.toHexString(tfe.id().value()),
|
| 305 | tfe.state(),
|
| 306 | tfe.flowLiveType(),
|
| 307 | tfe.life(),
|
| 308 | tfel.load().isValid() ? tfel.load() : "Load{rate=0, NOT VALID}");
|
| 309 | }
|
| 310 | }
|
| 311 |
|
| 312 | private void printPortSummaryLoad(ConnectPoint cp, SummaryFlowEntryWithLoad summaryFlowLoad) {
|
| 313 | print(" deviceId/Port=%s/%s, Total=%s, Immediate=%s, Short=%s, Mid=%s, Long=%s, Unknown=%s",
|
| 314 | cp.elementId(),
|
| 315 | cp.port(),
|
| 316 | summaryFlowLoad.totalLoad().isValid() ? summaryFlowLoad.totalLoad() : "Load{rate=0, NOT VALID}",
|
| 317 | summaryFlowLoad.immediateLoad().isValid() ? summaryFlowLoad.immediateLoad() : "Load{rate=0, NOT VALID}",
|
| 318 | summaryFlowLoad.shortLoad().isValid() ? summaryFlowLoad.shortLoad() : "Load{rate=0, NOT VALID}",
|
| 319 | summaryFlowLoad.midLoad().isValid() ? summaryFlowLoad.midLoad() : "Load{rate=0, NOT VALID}",
|
| 320 | summaryFlowLoad.longLoad().isValid() ? summaryFlowLoad.longLoad() : "Load{rate=0, NOT VALID}",
|
| 321 | summaryFlowLoad.unknownLoad().isValid() ? summaryFlowLoad.unknownLoad() : "Load{rate=0, NOT VALID}");
|
| 322 | }
|
| 323 | }
|