Jayasree Ghosh

FIX for ONOS-5296:Wrong MetricName throwing NPE

Change-Id: I625640fba84bd2fabd140d6449cdceb0079ad3f4
(cherry picked from commit f5c17fd7)
......@@ -28,6 +28,7 @@ import com.google.common.collect.Ordering;
import com.google.common.collect.TreeMultimap;
import org.onlab.metrics.MetricsService;
import org.onosproject.rest.AbstractWebResource;
import org.onlab.util.ItemNotFoundException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
......@@ -44,6 +45,8 @@ import java.util.Map;
@Path("metrics")
public class MetricsWebResource extends AbstractWebResource {
private static final String E_METRIC_NAME_NOT_FOUND = "Metric Name is not found";
private final MetricsService service = get(MetricsService.class);
private final ObjectNode root = mapper().createObjectNode();
......@@ -84,6 +87,10 @@ public class MetricsWebResource extends AbstractWebResource {
MetricFilter filter = metricName != null ? (name, metric) -> name.equals(metricName) : MetricFilter.ALL;
TreeMultimap<String, Metric> matched = listMetrics(service, filter);
if (matched.isEmpty()) {
throw new ItemNotFoundException(E_METRIC_NAME_NOT_FOUND);
}
matched.asMap().get(metricName).forEach(m -> {
metricNode.set(metricName, codec(Metric.class).encode(m, this));
});
......