Flavio Castro
Committed by Gerrit Code Review

ONOS-2315 adding auto-complete to the CLI

Change-Id: I7b3056022188d58ca141bfec2870c7f1b44e37c2
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.cli;
import org.onlab.metrics.MetricsService;
import java.util.ArrayList;
import java.util.List;
/**
* Metric name completer.
*/
public class MetricNameCompleter extends AbstractChoicesCompleter {
@Override
protected List<String> choices() {
MetricsService metricsService = AbstractShellCommand.get(MetricsService.class);
return new ArrayList<String>(metricsService.getMetrics().keySet());
}
}
......@@ -22,6 +22,7 @@ import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.joda.time.LocalDateTime;
import org.onlab.metrics.MetricsService;
......@@ -45,11 +46,15 @@ import com.google.common.collect.TreeMultimap;
description = "Prints metrics in the system")
public class MetricsListCommand extends AbstractShellCommand {
@Argument(index = 0, name = "metricName", description = "Name of Metric",
required = false, multiValued = false)
String metricName = null;
@Override
protected void execute() {
MetricsService metricsService = get(MetricsService.class);
MetricFilter filter = MetricFilter.ALL;
MetricFilter filter = metricName != null ? (name, metric) -> name.equals(metricName) : MetricFilter.ALL;
TreeMultimap<String, Metric> matched = listMetrics(metricsService, filter);
matched.asMap().forEach((name, metrics) -> {
......
......@@ -64,6 +64,9 @@
<command>
<action class="org.onosproject.cli.MetricsListCommand"/>
<completers>
<ref component-id="metricNameCompleter"/>
</completers>
</command>
<command>
......@@ -444,6 +447,7 @@
<bean id="ExtHeaderCompleter" class="org.onosproject.cli.net.ExtHeaderCompleter"/>
<bean id="startStopCompleter" class="org.onosproject.cli.StartStopCompleter"/>
<bean id="metricNameCompleter" class="org.onosproject.cli.MetricNameCompleter"/>
<bean id="upDownCompleter" class="org.onosproject.cli.UpDownCompleter"/>
</blueprint>
......
......@@ -290,4 +290,15 @@ public class MetricsManager implements MetricsService {
public void removeMatching(final MetricFilter filter) {
metricsRegistry.removeMatching(filter);
}
/**
* Fetches the existing Meters.
*
*
* @return a map of all metrics with the key as the
* name String to the Meter.
*/
public Map<String, Metric> getMetrics() {
return metricsRegistry.getMetrics();
}
}
......
......@@ -162,6 +162,14 @@ public interface MetricsService {
Map<String, Histogram> getHistograms(MetricFilter filter);
/**
* Fetches the existing metrics.
*
* @return a map of the Metrics, with the key as
* the name String to the Histogram.
*/
Map<String, Metric> getMetrics();
/**
* Removes all Metrics that match a given filter.
*
* @param filter filter to use to select the Metrics to remove.
......