blob: 3413c96cd74f3deb869a03472df30867cfde2fbd [file] [log] [blame]
alshabib23a8d7c2014-10-22 21:53:39 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
alshabib23a8d7c2014-10-22 21:53:39 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
alshabib23a8d7c2014-10-22 21:53:39 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
alshabib23a8d7c2014-10-22 21:53:39 -070015 */
16package org.onlab.onos.cli.net;
17
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onlab.onos.cli.AbstractShellCommand;
21import org.onlab.onos.net.ConnectPoint;
22import org.onlab.onos.net.DeviceId;
23import org.onlab.onos.net.PortNumber;
24
25import org.onlab.onos.net.statistic.Load;
26import org.onlab.onos.net.statistic.StatisticService;
27
28
29import static org.onlab.onos.net.DeviceId.deviceId;
30import static org.onlab.onos.net.PortNumber.portNumber;
31
32/**
33 * Fetches statistics.
34 */
35@Command(scope = "onos", name = "get-stats",
36 description = "Fetches stats for a connection point")
37public class GetStatistics extends AbstractShellCommand {
38
39 @Argument(index = 0, name = "connectPoint",
40 description = "Device/Port Description",
41 required = true, multiValued = false)
42 String connectPoint = null;
43
44
45 @Override
46 protected void execute() {
47 StatisticService service = get(StatisticService.class);
48
49 DeviceId ingressDeviceId = deviceId(getDeviceId(connectPoint));
50 PortNumber ingressPortNumber = portNumber(getPortNumber(connectPoint));
51 ConnectPoint cp = new ConnectPoint(ingressDeviceId, ingressPortNumber);
52
53 Load load = service.load(cp);
54
55 print("Load on %s -> %s", cp, load);
56 }
57
58 /**
59 * Extracts the port number portion of the ConnectPoint.
60 *
61 * @param deviceString string representing the device/port
62 * @return port number as a string, empty string if the port is not found
63 */
64 private String getPortNumber(String deviceString) {
65 int slash = deviceString.indexOf('/');
66 if (slash <= 0) {
67 return "";
68 }
69 return deviceString.substring(slash + 1, deviceString.length());
70 }
71
72 /**
73 * Extracts the device ID portion of the ConnectPoint.
74 *
75 * @param deviceString string representing the device/port
76 * @return device ID string
77 */
78 private String getDeviceId(String deviceString) {
79 int slash = deviceString.indexOf('/');
80 if (slash <= 0) {
81 return "";
82 }
83 return deviceString.substring(0, slash);
84 }
85}