blob: 984916ff8320be1fcb8c5e41bd8b4216dd0b62a0 [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;
Ray Milkey0068fd02018-10-11 15:45:39 -070020import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.PortNumber;
alshabib23a8d7c2014-10-22 21:53:39 -070026
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.statistic.Load;
28import org.onosproject.net.statistic.StatisticService;
alshabib23a8d7c2014-10-22 21:53:39 -070029
30
Brian O'Connorabafb502014-12-02 22:26:20 -080031import static org.onosproject.net.DeviceId.deviceId;
32import static org.onosproject.net.PortNumber.portNumber;
alshabib23a8d7c2014-10-22 21:53:39 -070033
34/**
35 * Fetches statistics.
36 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070037@Service
alshabib23a8d7c2014-10-22 21:53:39 -070038@Command(scope = "onos", name = "get-stats",
39 description = "Fetches stats for a connection point")
sangyun-han483731c2016-06-06 12:03:16 +090040public class GetStatisticsCommand extends AbstractShellCommand {
alshabib23a8d7c2014-10-22 21:53:39 -070041
42 @Argument(index = 0, name = "connectPoint",
43 description = "Device/Port Description",
44 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070045 @Completion(ConnectPointCompleter.class)
alshabib23a8d7c2014-10-22 21:53:39 -070046 String connectPoint = null;
47
48
49 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070050 protected void doExecute() {
alshabib23a8d7c2014-10-22 21:53:39 -070051 StatisticService service = get(StatisticService.class);
52
53 DeviceId ingressDeviceId = deviceId(getDeviceId(connectPoint));
54 PortNumber ingressPortNumber = portNumber(getPortNumber(connectPoint));
55 ConnectPoint cp = new ConnectPoint(ingressDeviceId, ingressPortNumber);
56
57 Load load = service.load(cp);
58
59 print("Load on %s -> %s", cp, load);
60 }
61
62 /**
63 * Extracts the port number portion of the ConnectPoint.
64 *
65 * @param deviceString string representing the device/port
66 * @return port number as a string, empty string if the port is not found
67 */
68 private String getPortNumber(String deviceString) {
69 int slash = deviceString.indexOf('/');
70 if (slash <= 0) {
71 return "";
72 }
73 return deviceString.substring(slash + 1, deviceString.length());
74 }
75
76 /**
77 * Extracts the device ID portion of the ConnectPoint.
78 *
79 * @param deviceString string representing the device/port
80 * @return device ID string
81 */
82 private String getDeviceId(String deviceString) {
83 int slash = deviceString.indexOf('/');
84 if (slash <= 0) {
85 return "";
86 }
87 return deviceString.substring(0, slash);
88 }
89}