[Falcon][ONOS-3601] Add REST API for metrics service with unit test
Change-Id: I33ec561d1d83c6f1167e960bc2f684a117e6ea9c
diff --git a/web/api/pom.xml b/web/api/pom.xml
index 99aeab2..6d483d9 100644
--- a/web/api/pom.xml
+++ b/web/api/pom.xml
@@ -102,7 +102,9 @@
org.onlab.osgi.*,
org.onlab.packet.*,
org.onlab.rest.*,
- org.onosproject.*
+ org.onosproject.*,
+ org.onlab.metrics.*,
+ com.codahale.metrics.*
</Import-Package>
<Web-ContextPath>${web.context}</Web-ContextPath>
</instructions>
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/CoreWebApplication.java b/web/api/src/main/java/org/onosproject/rest/resources/CoreWebApplication.java
index 44ad52a..cec1faa 100644
--- a/web/api/src/main/java/org/onosproject/rest/resources/CoreWebApplication.java
+++ b/web/api/src/main/java/org/onosproject/rest/resources/CoreWebApplication.java
@@ -40,7 +40,8 @@
TopologyWebResource.class,
ConfigWebResource.class,
PathsWebResource.class,
- StatisticsWebResource.class
+ StatisticsWebResource.class,
+ MetricsWebResource.class
);
}
}
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/MetricsWebResource.java b/web/api/src/main/java/org/onosproject/rest/resources/MetricsWebResource.java
new file mode 100644
index 0000000..2a221a4
--- /dev/null
+++ b/web/api/src/main/java/org/onosproject/rest/resources/MetricsWebResource.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2014-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.rest.resources;
+
+import com.codahale.metrics.Counter;
+import com.codahale.metrics.Gauge;
+import com.codahale.metrics.Metric;
+import com.codahale.metrics.Histogram;
+import com.codahale.metrics.Meter;
+import com.codahale.metrics.Timer;
+import com.codahale.metrics.MetricFilter;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.Ordering;
+import com.google.common.collect.TreeMultimap;
+import org.onlab.metrics.MetricsService;
+import org.onosproject.rest.AbstractWebResource;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.util.Comparator;
+import java.util.Map;
+
+/**
+ * Query metrics.
+ */
+@Path("metrics")
+public class MetricsWebResource extends AbstractWebResource {
+
+ final MetricsService service = get(MetricsService.class);
+ final ObjectNode root = mapper().createObjectNode();
+
+ /**
+ * Get stats information of all metrics. Returns array of all information for
+ * all metrics.
+ *
+ * @return metric information as array
+ * @onos.rsModel Metrics
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response getAllMetrics() {
+ ArrayNode metricsNode = root.putArray("metrics");
+ service.getMetrics().forEach((name, metric) -> {
+ ObjectNode item = mapper().createObjectNode();
+ item.put("name", name);
+ item.set("metric", codec(Metric.class).encode(metric, this));
+ metricsNode.add(item);
+ });
+
+ return ok(root).build();
+ }
+
+ /**
+ * Get stats information of a metric. Returns array of all information for the
+ * specified metric.
+ *
+ * @param metricName metric name
+ * @return metric information as array
+ * @onos.rsModel Metric
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("{metricName}")
+ public Response getMetricByName(@PathParam("metricName") String metricName) {
+ ObjectNode metricNode = root.putObject("metric");
+ MetricFilter filter = metricName != null ? (name, metric) -> name.equals(metricName) : MetricFilter.ALL;
+ TreeMultimap<String, Metric> matched = listMetrics(service, filter);
+
+ matched.asMap().get(metricName).forEach(m -> {
+ metricNode.set(metricName, codec(Metric.class).encode(m, this));
+ });
+
+ return ok(root).build();
+ }
+
+ private TreeMultimap<String, Metric> listMetrics(MetricsService metricsService, MetricFilter filter) {
+ TreeMultimap<String, Metric> metrics = TreeMultimap.create(Comparator.naturalOrder(), Ordering.arbitrary());
+
+ Map<String, Counter> counters = metricsService.getCounters(filter);
+ for (Map.Entry<String, Counter> entry : counters.entrySet()) {
+ metrics.put(entry.getKey(), entry.getValue());
+ }
+ Map<String, Gauge> gauges = metricsService.getGauges(filter);
+ for (Map.Entry<String, Gauge> entry : gauges.entrySet()) {
+ metrics.put(entry.getKey(), entry.getValue());
+ }
+ Map<String, Histogram> histograms = metricsService.getHistograms(filter);
+ for (Map.Entry<String, Histogram> entry : histograms.entrySet()) {
+ metrics.put(entry.getKey(), entry.getValue());
+ }
+ Map<String, Meter> meters = metricsService.getMeters(filter);
+ for (Map.Entry<String, Meter> entry : meters.entrySet()) {
+ metrics.put(entry.getKey(), entry.getValue());
+ }
+ Map<String, Timer> timers = metricsService.getTimers(filter);
+ for (Map.Entry<String, Timer> entry : timers.entrySet()) {
+ metrics.put(entry.getKey(), entry.getValue());
+ }
+
+ return metrics;
+ }
+}
diff --git a/web/api/src/main/resources/definitions/Metric.json b/web/api/src/main/resources/definitions/Metric.json
new file mode 100644
index 0000000..f712c0b
--- /dev/null
+++ b/web/api/src/main/resources/definitions/Metric.json
@@ -0,0 +1,165 @@
+{
+ "type": "object",
+ "title": "metric",
+ "required": [
+ "name",
+ "metric"
+ ],
+ "properties": {
+ "name": {
+ "type": "string",
+ "example": "cpu"
+ },
+ "metric": {
+ "type": "object",
+ "title": "metric",
+ "optional": [
+ "counter",
+ "gauge",
+ "meter",
+ "histogram",
+ "timer"
+ ],
+ "properties": {
+ "counter": {
+ "type": "object",
+ "required": [
+ "counter"
+ ],
+ "properties": {
+ "counter": {
+ "type": "integer",
+ "example": "1"
+ }
+ }
+ },
+ "gauge": {
+ "type": "object",
+ "required": [
+ "value"
+ ],
+ "properties": {
+ "value": "string",
+ "example": "1"
+ }
+ },
+ "meter": {
+ "type": "object",
+ "required": [
+ "counter",
+ "mean_rate",
+ "1_min_rate",
+ "5_min_rate",
+ "15_min_rate"
+ ],
+ "properties": {
+ "counter": {
+ "type": "integer",
+ "example": "1"
+ },
+ "mean_rate": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "1_min_rate": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "5_min_rate": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "15_min_rate": {
+ "type": "double",
+ "example": "1.0"
+ }
+ }
+ },
+ "histogram": {
+ "type": "object",
+ "required": [
+ "counter",
+ "mean",
+ "min",
+ "max",
+ "stddev"
+ ],
+ "properties": {
+ "counter": {
+ "type": "integer",
+ "example": "1"
+ },
+ "mean": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "min": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "max": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "stddev": {
+ "type": "double",
+ "example": "1.0"
+ }
+ }
+ },
+ "timer": {
+ "type": "object",
+ "required": [
+ "counter",
+ "mean_rate",
+ "1_min_rate",
+ "5_min_rate",
+ "15_min_rate",
+ "mean",
+ "min",
+ "max",
+ "stddev"
+ ],
+ "properties": {
+ "counter": {
+ "type": "integer",
+ "example": "1"
+ },
+ "mean_rate": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "1_min_rate": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "5_min_rate": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "15_min_rate": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "mean": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "min": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "max": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "stddev": {
+ "type": "double",
+ "example": "1.0"
+ }
+ }
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/web/api/src/main/resources/definitions/Metrics.json b/web/api/src/main/resources/definitions/Metrics.json
new file mode 100644
index 0000000..552d07d
--- /dev/null
+++ b/web/api/src/main/resources/definitions/Metrics.json
@@ -0,0 +1,180 @@
+{
+ "type": "object",
+ "title": "metrics",
+ "required": [
+ "metrics"
+ ],
+ "properties": {
+ "metrics": {
+ "type": "array",
+ "xml": {
+ "name": "metrics",
+ "wrapped": true
+ },
+ "items": {
+ "type": "object",
+ "title": "metric",
+ "required": [
+ "name",
+ "metric"
+ ],
+ "properties": {
+ "name": {
+ "type": "string",
+ "example": "cpu"
+ },
+ "metric": {
+ "type": "object",
+ "optional": [
+ "counter",
+ "gauge",
+ "meter",
+ "histogram",
+ "timer"
+ ],
+ "properties": {
+ "counter": {
+ "type": "object",
+ "required": [
+ "counter"
+ ],
+ "properties": {
+ "counter": {
+ "type": "integer",
+ "example": "1"
+ }
+ }
+ },
+ "gauge": {
+ "type": "object",
+ "required": [
+ "value"
+ ],
+ "properties": {
+ "value": "string",
+ "example": "1"
+ }
+ },
+ "meter": {
+ "type": "object",
+ "required": [
+ "counter",
+ "mean_rate",
+ "1_min_rate",
+ "5_min_rate",
+ "15_min_rate"
+ ],
+ "properties": {
+ "counter": {
+ "type": "integer",
+ "example": "1"
+ },
+ "mean_rate": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "1_min_rate": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "5_min_rate": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "15_min_rate": {
+ "type": "double",
+ "example": "1.0"
+ }
+ }
+ },
+ "histogram": {
+ "type": "object",
+ "required": [
+ "counter",
+ "mean",
+ "min",
+ "max",
+ "stddev"
+ ],
+ "properties": {
+ "counter": {
+ "type": "integer",
+ "example": "1"
+ },
+ "mean": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "min": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "max": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "stddev": {
+ "type": "double",
+ "example": "1.0"
+ }
+ }
+ },
+ "timer": {
+ "type": "object",
+ "required": [
+ "counter",
+ "mean_rate",
+ "1_min_rate",
+ "5_min_rate",
+ "15_min_rate",
+ "mean",
+ "min",
+ "max",
+ "stddev"
+ ],
+ "properties": {
+ "counter": {
+ "type": "integer",
+ "example": "1"
+ },
+ "mean_rate": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "1_min_rate": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "5_min_rate": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "15_min_rate": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "mean": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "min": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "max": {
+ "type": "double",
+ "example": "1.0"
+ },
+ "stddev": {
+ "type": "double",
+ "example": "1.0"
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/web/api/src/test/java/org/onosproject/rest/MetricsResourceTest.java b/web/api/src/test/java/org/onosproject/rest/MetricsResourceTest.java
new file mode 100644
index 0000000..089c5fe
--- /dev/null
+++ b/web/api/src/test/java/org/onosproject/rest/MetricsResourceTest.java
@@ -0,0 +1,109 @@
+/*
+ * Copyright 2014-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.rest;
+
+import com.codahale.metrics.Counter;
+import com.codahale.metrics.Meter;
+import com.codahale.metrics.Metric;
+import com.eclipsesource.json.JsonArray;
+import com.eclipsesource.json.JsonObject;
+import com.google.common.collect.ImmutableMap;
+import com.sun.jersey.api.client.WebResource;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.metrics.MetricsService;
+import org.onlab.osgi.ServiceDirectory;
+import org.onlab.osgi.TestServiceDirectory;
+import org.onlab.rest.BaseResource;
+import org.onosproject.codec.CodecService;
+import org.onosproject.codec.impl.CodecManager;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Unit tests for Metrics REST APIs.
+ */
+public class MetricsResourceTest extends ResourceTest {
+ MetricsService mockMetricsService;
+
+ /**
+ * Initializes test mocks and environment.
+ */
+ @Before
+ public void setUpTest() {
+ mockMetricsService = createMock(MetricsService.class);
+
+ // Register the services needed for the test
+ final CodecManager codecService = new CodecManager();
+ codecService.activate();
+ ServiceDirectory testDirectory =
+ new TestServiceDirectory()
+ .add(MetricsService.class, mockMetricsService)
+ .add(CodecService.class, codecService);
+ BaseResource.setServiceDirectory(testDirectory);
+ }
+
+ /**
+ * Verifies mocks.
+ */
+ @After
+ public void tearDownTest() {
+ verify(mockMetricsService);
+ }
+
+ /**
+ * Tests that a fetch of a non-existent object throws an exception.
+ */
+ @Test
+ public void testBadGet() {
+ Counter onosCounter = new Counter();
+ onosCounter.inc();
+
+ Meter onosMeter = new Meter();
+ onosMeter.mark();
+
+ ImmutableMap<String, Metric> metrics =
+ new ImmutableMap.Builder<String, Metric>()
+ .put("onosCounter", onosCounter)
+ .put("onosMeter", onosMeter)
+ .build();
+
+ expect(mockMetricsService.getMetrics())
+ .andReturn(metrics)
+ .anyTimes();
+
+ replay(mockMetricsService);
+
+ WebResource rs = resource();
+ String response = rs.path("metrics").get(String.class);
+ assertThat(response, containsString("{\"metrics\":["));
+
+ JsonObject result = JsonObject.readFrom(response);
+ assertThat(result, notNullValue());
+
+ JsonArray jsonMetrics = result.get("metrics").asArray();
+ assertThat(jsonMetrics, notNullValue());
+ assertThat(jsonMetrics.size(), is(2));
+ }
+}