Pavlin Radoslavov

Added a new module: ONOS intent metrics application

It can be used as an Intent-related event and event metrics collector.
It can be loaded by one of the following two (new) features:
     onos-app-metrics, onos-app-metrics-intent

After loading the module, it subscribes for intent-related events
and keeps the following state:
 (a) The last 100 events
 (b) The timestamp of the last event (ms after epoch) as observed by this
     module for each event type: SUBMITTED, INSTALLED, WITHDRAWN.
     The missing event type is the equivalent of "Withdraw Requested"
 (c) The rate of each intent event type: count, median rate, average rate
      over the last 1, 5 or 15 minutes

The following CLI commands are added:
 * onos:intents-events
   Shows the last 100 intent events
 * onos:intents-events-metrics
   Shows the timestamp of the last event (ms after epoch) as observed by
   this module for each event type, and the rate of the topology
   events (for each event type): see (b) and (c) above

Change-Id: I9f23e9086bbd433b8f24283539abdeb97e199e2e
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.onlab.onos</groupId>
<artifactId>onos-app-metrics</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>onos-app-metrics-intent</artifactId>
<packaging>bundle</packaging>
<description>ONOS intent metrics application</description>
<dependencies>
<dependency>
<groupId>org.onlab.onos</groupId>
<artifactId>onos-cli</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.karaf.shell</groupId>
<artifactId>org.apache.karaf.shell.console</artifactId>
</dependency>
</dependencies>
</project>
package org.onlab.onos.metrics.intent;
import java.util.List;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Meter;
import org.onlab.onos.net.intent.IntentEvent;
/**
* Service interface exported by IntentMetrics.
*/
public interface IntentMetricsService {
/**
* Gets the last saved intent events.
*
* @return the last saved intent events.
*/
public List<IntentEvent> getEvents();
/**
* Gets the Metrics' Gauge for the intent SUBMITTED event timestamp
* (ms from the epoch).
*
* @return the Metrics' Gauge for the intent SUBMITTED event timestamp
* (ms from the epoch)
*/
public Gauge<Long> intentSubmittedTimestampEpochMsGauge();
/**
* Gets the Metrics' Gauge for the intent INSTALLED event timestamp
* (ms from the epoch).
*
* @return the Metrics' Gauge for the intent INSTALLED event timestamp
* (ms from the epoch)
*/
public Gauge<Long> intentInstalledTimestampEpochMsGauge();
/**
* Gets the Metrics' Gauge for the intent WITHDRAW_REQUESTED event
* timestamp (ms from the epoch).
*
* TODO: This intent event is not implemented yet.
*
* @return the Metrics' Gauge for the intent WITHDRAW_REQUESTED event
* timestamp (ms from the epoch)
*/
public Gauge<Long> intentWithdrawRequestedTimestampEpochMsGauge();
/**
* Gets the Metrics' Gauge for the intent WITHDRAWN event timestamp
* (ms from the epoch).
*
* @return the Metrics' Gauge for the intent WITHDRAWN event timestamp
* (ms from the epoch)
*/
public Gauge<Long> intentWithdrawnTimestampEpochMsGauge();
/**
* Gets the Metrics' Meter for the submitted intents event rate.
*
* @return the Metrics' Meter for the submitted intents event rate
*/
public Meter intentSubmittedRateMeter();
/**
* Gets the Metrics' Meter for the installed intents event rate.
*
* @return the Metrics' Meter for the installed intent event rate
*/
public Meter intentInstalledRateMeter();
/**
* Gets the Metrics' Meter for the withdraw requested intents event rate.
*
* @return the Metrics' Meter for the withdraw requested intents event rate
*/
public Meter intentWithdrawRequestedRateMeter();
/**
* Gets the Metrics' Meter for the withdraw completed intents event rate.
*
* @return the Metrics' Meter for the withdraw completed intents event rate
*/
public Meter intentWithdrawnRateMeter();
}
package org.onlab.onos.metrics.intent.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.metrics.intent.IntentMetricsService;
import org.onlab.onos.net.intent.IntentEvent;
/**
* Command to show the list of last intent events.
*/
@Command(scope = "onos", name = "intents-events",
description = "Lists the last intent events")
public class IntentEventsListCommand extends AbstractShellCommand {
private static final String FORMAT_EVENT = "Event=%s";
@Override
protected void execute() {
IntentMetricsService service = get(IntentMetricsService.class);
if (outputJson()) {
print("%s", json(service.getEvents()));
} else {
for (IntentEvent event : service.getEvents()) {
print(FORMAT_EVENT, event);
print(""); // Extra empty line for clarity
}
}
}
/**
* Produces a JSON array of intent events.
*
* @param intentEvents the intent events with the data
* @return JSON array with the intent events
*/
private JsonNode json(List<IntentEvent> intentEvents) {
ObjectMapper mapper = new ObjectMapper();
ArrayNode result = mapper.createArrayNode();
for (IntentEvent event : intentEvents) {
result.add(json(mapper, event));
}
return result;
}
/**
* Produces JSON object for a intent event.
*
* @param mapper the JSON object mapper to use
* @param intentEvent the intent event with the data
* @return JSON object for the intent event
*/
private ObjectNode json(ObjectMapper mapper, IntentEvent intentEvent) {
ObjectNode result = mapper.createObjectNode();
result.put("time", intentEvent.time())
.put("type", intentEvent.type().toString())
.put("event", intentEvent.toString());
return result;
}
}
package org.onlab.onos.metrics.intent.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.intent.IntentMetricsService;
/**
* Command to show the intent events metrics.
*/
@Command(scope = "onos", name = "intents-events-metrics",
description = "Lists intent events metrics")
public class IntentEventsMetricsCommand extends AbstractShellCommand {
private static final String FORMAT_GAUGE =
"Intent %s Event Timestamp (ms from epoch)=%d";
private static final String FORMAT_METER =
"Intent %s Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f";
@Override
protected void execute() {
IntentMetricsService service = get(IntentMetricsService.class);
Gauge<Long> gauge;
Meter meter;
if (outputJson()) {
ObjectMapper mapper = new ObjectMapper()
.registerModule(new MetricsModule(TimeUnit.SECONDS,
TimeUnit.MILLISECONDS,
false));
ObjectNode result = mapper.createObjectNode();
//
gauge = service.intentSubmittedTimestampEpochMsGauge();
result.put("intentSubmittedTimestamp", json(mapper, gauge));
gauge = service.intentInstalledTimestampEpochMsGauge();
result.put("intentInstalledTimestamp", json(mapper, gauge));
gauge = service.intentWithdrawRequestedTimestampEpochMsGauge();
result.put("intentWithdrawRequestedTimestamp",
json(mapper, gauge));
gauge = service.intentWithdrawnTimestampEpochMsGauge();
result.put("intentWithdrawnTimestamp", json(mapper, gauge));
//
meter = service.intentSubmittedRateMeter();
result.put("intentSubmittedRate", json(mapper, meter));
meter = service.intentInstalledRateMeter();
result.put("intentInstalledRate", json(mapper, meter));
meter = service.intentWithdrawRequestedRateMeter();
result.put("intentWithdrawRequestedRate", json(mapper, meter));
meter = service.intentWithdrawnRateMeter();
result.put("intentWithdrawnRate", json(mapper, meter));
//
print("%s", result);
} else {
gauge = service.intentSubmittedTimestampEpochMsGauge();
printGauge("Submitted", gauge);
gauge = service.intentInstalledTimestampEpochMsGauge();
printGauge("Installed", gauge);
gauge = service.intentWithdrawRequestedTimestampEpochMsGauge();
printGauge("Withdraw Requested", gauge);
gauge = service.intentWithdrawnTimestampEpochMsGauge();
printGauge("Withdrawn", gauge);
//
meter = service.intentSubmittedRateMeter();
printMeter("Submitted", meter);
meter = service.intentInstalledRateMeter();
printMeter("Installed", meter);
meter = service.intentWithdrawRequestedRateMeter();
printMeter("Withdraw Requested", meter);
meter = service.intentWithdrawnRateMeter();
printMeter("Withdrawn", meter);
}
}
/**
* Produces JSON node for an Object.
*
* @param mapper the JSON object mapper to use
* @param object the Object with the data
* @return JSON node for the Object
*/
private JsonNode json(ObjectMapper mapper, Object object) {
//
// NOTE: The API for custom serializers is incomplete,
// hence we have to parse the JSON string to create JsonNode.
//
try {
final String objectJson = mapper.writeValueAsString(object);
JsonNode objectNode = mapper.readTree(objectJson);
return objectNode;
} catch (JsonProcessingException e) {
log.error("Error writing value as JSON string", e);
} catch (IOException e) {
log.error("Error writing value as JSON string", e);
}
return null;
}
/**
* Prints a Gauge.
*
* @param operationStr the string with the intent operation to print
* @param gauge the Gauge to print
*/
private void printGauge(String operationStr, Gauge<Long> gauge) {
print(FORMAT_GAUGE, operationStr, gauge.getValue());
}
/**
* Prints a Meter.
*
* @param operationStr the string with the intent operation to print
* @param meter the Meter to print
*/
private void printMeter(String operationStr, Meter meter) {
TimeUnit rateUnit = TimeUnit.SECONDS;
double rateFactor = rateUnit.toSeconds(1);
print(FORMAT_METER, operationStr, meter.getCount(),
meter.getMeanRate() * rateFactor,
meter.getOneMinuteRate() * rateFactor,
meter.getFiveMinuteRate() * rateFactor,
meter.getFifteenMinuteRate() * rateFactor);
}
}
/**
* ONOS Intent Metrics Application that collects intent-related metrics.
*/
package org.onlab.onos.metrics.intent;
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
<command>
<action class="org.onlab.onos.metrics.intent.cli.IntentEventsListCommand"/>
</command>
<command>
<action class="org.onlab.onos.metrics.intent.cli.IntentEventsMetricsCommand"/>
</command>
</command-bundle>
</blueprint>
......@@ -17,6 +17,7 @@
<description>ONOS metrics applications</description>
<modules>
<module>intent</module>
<module>topology</module>
</modules>
......
......@@ -199,9 +199,16 @@
<feature name="onos-app-metrics" version="1.0.0"
description="ONOS metrics applications">
<feature>onos-app-metrics-intent</feature>
<feature>onos-app-metrics-topology</feature>
</feature>
<feature name="onos-app-metrics-intent" version="1.0.0"
description="ONOS intent metrics application">
<feature>onos-api</feature>
<bundle>mvn:org.onlab.onos/onos-app-metrics-intent/1.0.0-SNAPSHOT</bundle>
</feature>
<feature name="onos-app-metrics-topology" version="1.0.0"
description="ONOS topology metrics application">
<feature>onos-api</feature>
......