FIX for ONOS-5296:Wrong MetricName throwing NPE
Change-Id: I625640fba84bd2fabd140d6449cdceb0079ad3f4 (cherry picked from commit f5c17fd7)
Showing
1 changed file
with
7 additions
and
0 deletions
... | @@ -28,6 +28,7 @@ import com.google.common.collect.Ordering; | ... | @@ -28,6 +28,7 @@ import com.google.common.collect.Ordering; |
28 | import com.google.common.collect.TreeMultimap; | 28 | import com.google.common.collect.TreeMultimap; |
29 | import org.onlab.metrics.MetricsService; | 29 | import org.onlab.metrics.MetricsService; |
30 | import org.onosproject.rest.AbstractWebResource; | 30 | import org.onosproject.rest.AbstractWebResource; |
31 | +import org.onlab.util.ItemNotFoundException; | ||
31 | 32 | ||
32 | import javax.ws.rs.GET; | 33 | import javax.ws.rs.GET; |
33 | import javax.ws.rs.Path; | 34 | import javax.ws.rs.Path; |
... | @@ -44,6 +45,8 @@ import java.util.Map; | ... | @@ -44,6 +45,8 @@ import java.util.Map; |
44 | @Path("metrics") | 45 | @Path("metrics") |
45 | public class MetricsWebResource extends AbstractWebResource { | 46 | public class MetricsWebResource extends AbstractWebResource { |
46 | 47 | ||
48 | + private static final String E_METRIC_NAME_NOT_FOUND = "Metric Name is not found"; | ||
49 | + | ||
47 | private final MetricsService service = get(MetricsService.class); | 50 | private final MetricsService service = get(MetricsService.class); |
48 | private final ObjectNode root = mapper().createObjectNode(); | 51 | private final ObjectNode root = mapper().createObjectNode(); |
49 | 52 | ||
... | @@ -84,6 +87,10 @@ public class MetricsWebResource extends AbstractWebResource { | ... | @@ -84,6 +87,10 @@ public class MetricsWebResource extends AbstractWebResource { |
84 | MetricFilter filter = metricName != null ? (name, metric) -> name.equals(metricName) : MetricFilter.ALL; | 87 | MetricFilter filter = metricName != null ? (name, metric) -> name.equals(metricName) : MetricFilter.ALL; |
85 | TreeMultimap<String, Metric> matched = listMetrics(service, filter); | 88 | TreeMultimap<String, Metric> matched = listMetrics(service, filter); |
86 | 89 | ||
90 | + if (matched.isEmpty()) { | ||
91 | + throw new ItemNotFoundException(E_METRIC_NAME_NOT_FOUND); | ||
92 | + } | ||
93 | + | ||
87 | matched.asMap().get(metricName).forEach(m -> { | 94 | matched.asMap().get(metricName).forEach(m -> { |
88 | metricNode.set(metricName, codec(Metric.class).encode(m, this)); | 95 | metricNode.set(metricName, codec(Metric.class).encode(m, this)); |
89 | }); | 96 | }); | ... | ... |
-
Please register or login to post a comment