blob: a244d46c0864f1abbe92f0afc6fd4ee013793de0 [file] [log] [blame]
sangho538108b2015-04-08 14:29:20 -07001package org.onosproject.cli.net;
2
3/*
4 * Copyright 2015 Open Networking Laboratory
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19import org.apache.karaf.shell.commands.Command;
20import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.device.DeviceService;
23import org.onosproject.net.device.PortStatistics;
24
25/**
26 * Lists port statistic of all ports in the system.
27 */
28@Command(scope = "onos", name = "portstats",
29 description = "Lists statistics of all ports in the system")
30public class DevicePortStatsCommand extends AbstractShellCommand {
31
32 private static final String FORMAT =
33 " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s, pktRxDrp=%s, pktTxDrp=%s, Dur=%s";
34
35 @Override
36 protected void execute() {
37 DeviceService deviceService = get(DeviceService.class);
38
39 deviceService.getDevices().forEach(d ->
40 printPortStats(d.id(), deviceService.getPortStatistics(d.id()))
41 );
42 }
43
44 private void printPortStats(DeviceId deviceId, Iterable<PortStatistics> portStats) {
45 print("deviceId=%s", deviceId);
46 for (PortStatistics stat : portStats) {
47 print(FORMAT, stat.port(), stat.packetsReceived(), stat.packetsSent(), stat.bytesReceived(),
48 stat.bytesSent(), stat.packetsRxDropped(), stat.packetsTxDropped(), stat.durationSec());
49 }
50 }
51}