alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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. |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.net.ConnectPoint; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.PortNumber; |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 24 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.statistic.Load; |
| 26 | import org.onosproject.net.statistic.StatisticService; |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 27 | |
| 28 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import static org.onosproject.net.DeviceId.deviceId; |
| 30 | import static org.onosproject.net.PortNumber.portNumber; |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Fetches statistics. |
| 34 | */ |
| 35 | @Command(scope = "onos", name = "get-stats", |
| 36 | description = "Fetches stats for a connection point") |
| 37 | public 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 | } |