added stats command
Change-Id: Id6ac04e116858c4a105329ddcf5557fee93dfc8d
Showing
3 changed files
with
102 additions
and
0 deletions
| 1 | +/* | ||
| 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 | + */ | ||
| 19 | +package org.onlab.onos.cli.net; | ||
| 20 | + | ||
| 21 | +import org.apache.karaf.shell.commands.Argument; | ||
| 22 | +import org.apache.karaf.shell.commands.Command; | ||
| 23 | +import org.onlab.onos.cli.AbstractShellCommand; | ||
| 24 | +import org.onlab.onos.net.ConnectPoint; | ||
| 25 | +import org.onlab.onos.net.DeviceId; | ||
| 26 | +import org.onlab.onos.net.PortNumber; | ||
| 27 | + | ||
| 28 | +import org.onlab.onos.net.statistic.Load; | ||
| 29 | +import org.onlab.onos.net.statistic.StatisticService; | ||
| 30 | + | ||
| 31 | + | ||
| 32 | +import static org.onlab.onos.net.DeviceId.deviceId; | ||
| 33 | +import 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") | ||
| 40 | +public 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 | +} |
| ... | @@ -119,6 +119,12 @@ | ... | @@ -119,6 +119,12 @@ |
| 119 | </optional-completers> | 119 | </optional-completers> |
| 120 | </command> | 120 | </command> |
| 121 | <command> | 121 | <command> |
| 122 | + <action class="org.onlab.onos.cli.net.GetStatistics"/> | ||
| 123 | + <completers> | ||
| 124 | + <ref component-id="connectPointCompleter"/> | ||
| 125 | + </completers> | ||
| 126 | + </command> | ||
| 127 | + <command> | ||
| 122 | <action class="org.onlab.onos.cli.net.AddMultiPointToSinglePointIntentCommand"/> | 128 | <action class="org.onlab.onos.cli.net.AddMultiPointToSinglePointIntentCommand"/> |
| 123 | <completers> | 129 | <completers> |
| 124 | <ref component-id="connectPointCompleter"/> | 130 | <ref component-id="connectPointCompleter"/> | ... | ... |
| 1 | package org.onlab.onos.net.statistic; | 1 | package org.onlab.onos.net.statistic; |
| 2 | 2 | ||
| 3 | +import com.google.common.base.MoreObjects; | ||
| 3 | import org.onlab.onos.net.flow.FlowRuleProvider; | 4 | import org.onlab.onos.net.flow.FlowRuleProvider; |
| 4 | 5 | ||
| 5 | /** | 6 | /** |
| ... | @@ -53,4 +54,11 @@ public class DefaultLoad implements Load { | ... | @@ -53,4 +54,11 @@ public class DefaultLoad implements Load { |
| 53 | public long time() { | 54 | public long time() { |
| 54 | return time; | 55 | return time; |
| 55 | } | 56 | } |
| 57 | + | ||
| 58 | + @Override | ||
| 59 | + public String toString() { | ||
| 60 | + return MoreObjects.toStringHelper("Load").add("rate", rate()) | ||
| 61 | + .add("latest", latest()).toString(); | ||
| 62 | + | ||
| 63 | + } | ||
| 56 | } | 64 | } | ... | ... |
-
Please register or login to post a comment