Added initial implementation of Topology-related event and
event metrics collector. It can be loaded by one of the following two (new)
features: onos-app-metrics, onos-app-metrics-topology
After loading the module, it subscribes for topology-related events
and keeps the following state:
(a) The last 10 events
(b) The timestamp of the last event (ms after epoch) as observed by this
module
(c) The rate of the topology events: count, median rate, average rate
over the last 1, 5 or 15 minutes
The following CLI commands are added:
* onos:topology-events
Shows the last 10 topology events
* onos:topology-events-metrics
Shows the timestamp of the last event, and the rate of the topology
events: see (b) and (c) above
diff --git a/apps/metrics/topology/src/main/java/org/onlab/onos/metrics/topology/cli/TopologyEventsListCommand.java b/apps/metrics/topology/src/main/java/org/onlab/onos/metrics/topology/cli/TopologyEventsListCommand.java
new file mode 100644
index 0000000..8bab4d0
--- /dev/null
+++ b/apps/metrics/topology/src/main/java/org/onlab/onos/metrics/topology/cli/TopologyEventsListCommand.java
@@ -0,0 +1,97 @@
+package org.onlab.onos.metrics.topology.cli;
+
+import java.util.List;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.karaf.shell.commands.Command;
+import org.onlab.onos.cli.AbstractShellCommand;
+import org.onlab.onos.event.Event;
+import org.onlab.onos.metrics.topology.TopologyMetricsService;
+import org.onlab.onos.net.topology.TopologyEvent;
+
+/**
+ * Command to show the list of last topology events.
+ */
+@Command(scope = "onos", name = "topology-events",
+ description = "Lists the last topology events")
+public class TopologyEventsListCommand extends AbstractShellCommand {
+
+ private static final String FORMAT_EVENT =
+ "Topology Event time=%d type=%s subject=%s";
+ private static final String FORMAT_REASON =
+ " Reason time=%d type=%s subject=%s";
+
+ @Override
+ protected void execute() {
+ TopologyMetricsService service = get(TopologyMetricsService.class);
+
+ if (outputJson()) {
+ print("%s", json(service.getEvents()));
+ } else {
+ for (TopologyEvent event : service.getEvents()) {
+ print(FORMAT_EVENT, event.time(), event.type(),
+ event.subject());
+ for (Event reason : event.reasons()) {
+ print(FORMAT_REASON, reason.time(), reason.type(),
+ reason.subject());
+ }
+ print(""); // Extra empty line for clarity
+ }
+ }
+ }
+
+ /**
+ * Produces a JSON array of topology events.
+ *
+ * @param topologyEvents the topology events with the data
+ * @return JSON array with the topology events
+ */
+ private JsonNode json(List<TopologyEvent> topologyEvents) {
+ ObjectMapper mapper = new ObjectMapper();
+ ArrayNode result = mapper.createArrayNode();
+
+ for (TopologyEvent event : topologyEvents) {
+ result.add(json(mapper, event));
+ }
+ return result;
+ }
+
+ /**
+ * Produces JSON object for a topology event.
+ *
+ * @param mapper the JSON object mapper to use
+ * @param topologyEvent the topology event with the data
+ * @return JSON object for the topology event
+ */
+ private ObjectNode json(ObjectMapper mapper, TopologyEvent topologyEvent) {
+ ObjectNode result = mapper.createObjectNode();
+ ArrayNode reasons = mapper.createArrayNode();
+
+ for (Event reason : topologyEvent.reasons()) {
+ reasons.add(json(mapper, reason));
+ }
+ result.put("time", topologyEvent.time())
+ .put("type", topologyEvent.type().toString())
+ .put("subject", topologyEvent.subject().toString())
+ .put("reasons", reasons);
+ return result;
+ }
+
+ /**
+ * Produces JSON object for a generic event.
+ *
+ * @param event the generic event with the data
+ * @return JSON object for the generic event
+ */
+ private ObjectNode json(ObjectMapper mapper, Event event) {
+ ObjectNode result = mapper.createObjectNode();
+
+ result.put("time", event.time())
+ .put("type", event.type().toString())
+ .put("subject", event.subject().toString());
+ return result;
+ }
+}
diff --git a/apps/metrics/topology/src/main/java/org/onlab/onos/metrics/topology/cli/TopologyEventsMetricsCommand.java b/apps/metrics/topology/src/main/java/org/onlab/onos/metrics/topology/cli/TopologyEventsMetricsCommand.java
new file mode 100644
index 0000000..1bc0b57
--- /dev/null
+++ b/apps/metrics/topology/src/main/java/org/onlab/onos/metrics/topology/cli/TopologyEventsMetricsCommand.java
@@ -0,0 +1,69 @@
+package org.onlab.onos.metrics.topology.cli;
+
+import java.io.IOException;
+import java.util.concurrent.TimeUnit;
+
+import com.codahale.metrics.Gauge;
+import com.codahale.metrics.Meter;
+import com.codahale.metrics.json.MetricsModule;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.karaf.shell.commands.Command;
+import org.onlab.onos.cli.AbstractShellCommand;
+import org.onlab.onos.metrics.topology.TopologyMetricsService;
+
+/**
+ * Command to show the topology events metrics.
+ */
+@Command(scope = "onos", name = "topology-events-metrics",
+ description = "Lists topology events metrics")
+public class TopologyEventsMetricsCommand extends AbstractShellCommand {
+
+ private static final String FORMAT_GAUGE =
+ "Last Topology Event Timestamp (ms from epoch)=%d";
+ private static final String FORMAT_METER =
+ "Topology Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f";
+
+ @Override
+ protected void execute() {
+ TopologyMetricsService service = get(TopologyMetricsService.class);
+ Gauge<Long> gauge = service.lastEventTimestampEpochMsGauge();
+ Meter meter = service.eventRateMeter();
+
+ if (outputJson()) {
+ ObjectMapper mapper = new ObjectMapper()
+ .registerModule(new MetricsModule(TimeUnit.SECONDS,
+ TimeUnit.MILLISECONDS,
+ false));
+ ObjectNode result = mapper.createObjectNode();
+ try {
+ //
+ // NOTE: The API for custom serializers is incomplete,
+ // hence we have to parse the JSON string to create JsonNode.
+ //
+ final String gaugeJson = mapper.writeValueAsString(gauge);
+ final String meterJson = mapper.writeValueAsString(meter);
+ JsonNode gaugeNode = mapper.readTree(gaugeJson);
+ JsonNode meterNode = mapper.readTree(meterJson);
+ result.put("lastTopologyEventTimestamp", gaugeNode);
+ result.put("listenerEventRate", meterNode);
+ } catch (JsonProcessingException e) {
+ log.error("Error writing value as JSON string", e);
+ } catch (IOException e) {
+ log.error("Error writing value as JSON string", e);
+ }
+ print("%s", result);
+ } else {
+ TimeUnit rateUnit = TimeUnit.SECONDS;
+ double rateFactor = rateUnit.toSeconds(1);
+ print(FORMAT_GAUGE, gauge.getValue());
+ print(FORMAT_METER, meter.getCount(),
+ meter.getMeanRate() * rateFactor,
+ meter.getOneMinuteRate() * rateFactor,
+ meter.getFiveMinuteRate() * rateFactor,
+ meter.getFifteenMinuteRate() * rateFactor);
+ }
+ }
+}