blob: 3745fa9e474cb36e46e70edae36a24afb7f00160 [file] [log] [blame]
alshabib23a8d7c2014-10-22 21:53:39 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. 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,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.onlab.onos.cli.net;
20
21import org.apache.karaf.shell.commands.Argument;
22import org.apache.karaf.shell.commands.Command;
23import org.onlab.onos.cli.AbstractShellCommand;
24import org.onlab.onos.net.ConnectPoint;
25import org.onlab.onos.net.DeviceId;
26import org.onlab.onos.net.PortNumber;
27
28import org.onlab.onos.net.statistic.Load;
29import org.onlab.onos.net.statistic.StatisticService;
30
31
32import static org.onlab.onos.net.DeviceId.deviceId;
33import static org.onlab.onos.net.PortNumber.portNumber;
34
35/**
36 * Fetches statistics.
37 */
38@Command(scope = "onos", name = "get-stats",
39 description = "Fetches stats for a connection point")
40public class GetStatistics extends AbstractShellCommand {
41
42 @Argument(index = 0, name = "connectPoint",
43 description = "Device/Port Description",
44 required = true, multiValued = false)
45 String connectPoint = null;
46
47
48 @Override
49 protected void execute() {
50 StatisticService service = get(StatisticService.class);
51
52 DeviceId ingressDeviceId = deviceId(getDeviceId(connectPoint));
53 PortNumber ingressPortNumber = portNumber(getPortNumber(connectPoint));
54 ConnectPoint cp = new ConnectPoint(ingressDeviceId, ingressPortNumber);
55
56 Load load = service.load(cp);
57
58 print("Load on %s -> %s", cp, load);
59 }
60
61 /**
62 * Extracts the port number portion of the ConnectPoint.
63 *
64 * @param deviceString string representing the device/port
65 * @return port number as a string, empty string if the port is not found
66 */
67 private String getPortNumber(String deviceString) {
68 int slash = deviceString.indexOf('/');
69 if (slash <= 0) {
70 return "";
71 }
72 return deviceString.substring(slash + 1, deviceString.length());
73 }
74
75 /**
76 * Extracts the device ID portion of the ConnectPoint.
77 *
78 * @param deviceString string representing the device/port
79 * @return device ID string
80 */
81 private String getDeviceId(String deviceString) {
82 int slash = deviceString.indexOf('/');
83 if (slash <= 0) {
84 return "";
85 }
86 return deviceString.substring(0, slash);
87 }
88}