blob: 54ddfb2a1b9625d90c80b98fed78168353779917 [file] [log] [blame]
alshabib23a8d7c2014-10-22 21:53:39 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
alshabib23a8d7c2014-10-22 21:53:39 -070017
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.lifecycle.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.net.ConnectPoint;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.PortNumber;
alshabib23a8d7c2014-10-22 21:53:39 -070025
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.statistic.Load;
27import org.onosproject.net.statistic.StatisticService;
alshabib23a8d7c2014-10-22 21:53:39 -070028
29
Brian O'Connorabafb502014-12-02 22:26:20 -080030import static org.onosproject.net.DeviceId.deviceId;
31import static org.onosproject.net.PortNumber.portNumber;
alshabib23a8d7c2014-10-22 21:53:39 -070032
33/**
34 * Fetches statistics.
35 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070036@Service
alshabib23a8d7c2014-10-22 21:53:39 -070037@Command(scope = "onos", name = "get-stats",
38 description = "Fetches stats for a connection point")
sangyun-han483731c2016-06-06 12:03:16 +090039public class GetStatisticsCommand extends AbstractShellCommand {
alshabib23a8d7c2014-10-22 21:53:39 -070040
41 @Argument(index = 0, name = "connectPoint",
42 description = "Device/Port Description",
43 required = true, multiValued = false)
44 String connectPoint = null;
45
46
47 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070048 protected void doExecute() {
alshabib23a8d7c2014-10-22 21:53:39 -070049 StatisticService service = get(StatisticService.class);
50
51 DeviceId ingressDeviceId = deviceId(getDeviceId(connectPoint));
52 PortNumber ingressPortNumber = portNumber(getPortNumber(connectPoint));
53 ConnectPoint cp = new ConnectPoint(ingressDeviceId, ingressPortNumber);
54
55 Load load = service.load(cp);
56
57 print("Load on %s -> %s", cp, load);
58 }
59
60 /**
61 * Extracts the port number portion of the ConnectPoint.
62 *
63 * @param deviceString string representing the device/port
64 * @return port number as a string, empty string if the port is not found
65 */
66 private String getPortNumber(String deviceString) {
67 int slash = deviceString.indexOf('/');
68 if (slash <= 0) {
69 return "";
70 }
71 return deviceString.substring(slash + 1, deviceString.length());
72 }
73
74 /**
75 * Extracts the device ID portion of the ConnectPoint.
76 *
77 * @param deviceString string representing the device/port
78 * @return device ID string
79 */
80 private String getDeviceId(String deviceString) {
81 int slash = deviceString.indexOf('/');
82 if (slash <= 0) {
83 return "";
84 }
85 return deviceString.substring(0, slash);
86 }
87}