Flavio Castro
Committed by Gerrit Code Review

ONOS-2315 adding auto-complete to the CLI

Change-Id: I7b3056022188d58ca141bfec2870c7f1b44e37c2
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
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
7 + *
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.
15 + */
16 +package org.onosproject.cli;
17 +
18 +import org.onlab.metrics.MetricsService;
19 +
20 +import java.util.ArrayList;
21 +import java.util.List;
22 +
23 +/**
24 + * Metric name completer.
25 + */
26 +public class MetricNameCompleter extends AbstractChoicesCompleter {
27 +
28 + @Override
29 + protected List<String> choices() {
30 + MetricsService metricsService = AbstractShellCommand.get(MetricsService.class);
31 + return new ArrayList<String>(metricsService.getMetrics().keySet());
32 + }
33 +
34 +}
...@@ -22,6 +22,7 @@ import java.util.Comparator; ...@@ -22,6 +22,7 @@ import java.util.Comparator;
22 import java.util.Map; 22 import java.util.Map;
23 import java.util.Map.Entry; 23 import java.util.Map.Entry;
24 24
25 +import org.apache.karaf.shell.commands.Argument;
25 import org.apache.karaf.shell.commands.Command; 26 import org.apache.karaf.shell.commands.Command;
26 import org.joda.time.LocalDateTime; 27 import org.joda.time.LocalDateTime;
27 import org.onlab.metrics.MetricsService; 28 import org.onlab.metrics.MetricsService;
...@@ -45,11 +46,15 @@ import com.google.common.collect.TreeMultimap; ...@@ -45,11 +46,15 @@ import com.google.common.collect.TreeMultimap;
45 description = "Prints metrics in the system") 46 description = "Prints metrics in the system")
46 public class MetricsListCommand extends AbstractShellCommand { 47 public class MetricsListCommand extends AbstractShellCommand {
47 48
49 + @Argument(index = 0, name = "metricName", description = "Name of Metric",
50 + required = false, multiValued = false)
51 + String metricName = null;
52 +
48 @Override 53 @Override
49 protected void execute() { 54 protected void execute() {
50 MetricsService metricsService = get(MetricsService.class); 55 MetricsService metricsService = get(MetricsService.class);
51 56
52 - MetricFilter filter = MetricFilter.ALL; 57 + MetricFilter filter = metricName != null ? (name, metric) -> name.equals(metricName) : MetricFilter.ALL;
53 58
54 TreeMultimap<String, Metric> matched = listMetrics(metricsService, filter); 59 TreeMultimap<String, Metric> matched = listMetrics(metricsService, filter);
55 matched.asMap().forEach((name, metrics) -> { 60 matched.asMap().forEach((name, metrics) -> {
......
...@@ -64,6 +64,9 @@ ...@@ -64,6 +64,9 @@
64 64
65 <command> 65 <command>
66 <action class="org.onosproject.cli.MetricsListCommand"/> 66 <action class="org.onosproject.cli.MetricsListCommand"/>
67 + <completers>
68 + <ref component-id="metricNameCompleter"/>
69 + </completers>
67 </command> 70 </command>
68 71
69 <command> 72 <command>
...@@ -444,6 +447,7 @@ ...@@ -444,6 +447,7 @@
444 <bean id="ExtHeaderCompleter" class="org.onosproject.cli.net.ExtHeaderCompleter"/> 447 <bean id="ExtHeaderCompleter" class="org.onosproject.cli.net.ExtHeaderCompleter"/>
445 448
446 <bean id="startStopCompleter" class="org.onosproject.cli.StartStopCompleter"/> 449 <bean id="startStopCompleter" class="org.onosproject.cli.StartStopCompleter"/>
450 + <bean id="metricNameCompleter" class="org.onosproject.cli.MetricNameCompleter"/>
447 <bean id="upDownCompleter" class="org.onosproject.cli.UpDownCompleter"/> 451 <bean id="upDownCompleter" class="org.onosproject.cli.UpDownCompleter"/>
448 452
449 </blueprint> 453 </blueprint>
......
...@@ -290,4 +290,15 @@ public class MetricsManager implements MetricsService { ...@@ -290,4 +290,15 @@ public class MetricsManager implements MetricsService {
290 public void removeMatching(final MetricFilter filter) { 290 public void removeMatching(final MetricFilter filter) {
291 metricsRegistry.removeMatching(filter); 291 metricsRegistry.removeMatching(filter);
292 } 292 }
293 +
294 + /**
295 + * Fetches the existing Meters.
296 + *
297 + *
298 + * @return a map of all metrics with the key as the
299 + * name String to the Meter.
300 + */
301 + public Map<String, Metric> getMetrics() {
302 + return metricsRegistry.getMetrics();
303 + }
293 } 304 }
......
...@@ -162,6 +162,14 @@ public interface MetricsService { ...@@ -162,6 +162,14 @@ public interface MetricsService {
162 Map<String, Histogram> getHistograms(MetricFilter filter); 162 Map<String, Histogram> getHistograms(MetricFilter filter);
163 163
164 /** 164 /**
165 + * Fetches the existing metrics.
166 + *
167 + * @return a map of the Metrics, with the key as
168 + * the name String to the Histogram.
169 + */
170 + Map<String, Metric> getMetrics();
171 +
172 + /**
165 * Removes all Metrics that match a given filter. 173 * Removes all Metrics that match a given filter.
166 * 174 *
167 * @param filter filter to use to select the Metrics to remove. 175 * @param filter filter to use to select the Metrics to remove.
......