alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.net.ConnectPoint; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.PortNumber; |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 26 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.statistic.Load; |
| 28 | import org.onosproject.net.statistic.StatisticService; |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 29 | |
| 30 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import static org.onosproject.net.DeviceId.deviceId; |
| 32 | import static org.onosproject.net.PortNumber.portNumber; |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Fetches statistics. |
| 36 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 37 | @Service |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 38 | @Command(scope = "onos", name = "get-stats", |
| 39 | description = "Fetches stats for a connection point") |
sangyun-han | 483731c | 2016-06-06 12:03:16 +0900 | [diff] [blame] | 40 | public class GetStatisticsCommand extends AbstractShellCommand { |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 41 | |
| 42 | @Argument(index = 0, name = "connectPoint", |
| 43 | description = "Device/Port Description", |
| 44 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 45 | @Completion(ConnectPointCompleter.class) |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 46 | String connectPoint = null; |
| 47 | |
| 48 | |
| 49 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 50 | protected void doExecute() { |
alshabib | 23a8d7c | 2014-10-22 21:53:39 -0700 | [diff] [blame] | 51 | 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 | } |