blob: e07c3a59327c9de56e99e5b59f92d78548a548e3 [file] [log] [blame]
Pavlin Radoslavov295b2962014-10-23 01:12:41 -07001package org.onlab.onos.metrics.intent.cli;
2
3import java.util.List;
4
5import com.fasterxml.jackson.databind.JsonNode;
6import com.fasterxml.jackson.databind.ObjectMapper;
7import com.fasterxml.jackson.databind.node.ArrayNode;
8import com.fasterxml.jackson.databind.node.ObjectNode;
9import org.apache.karaf.shell.commands.Command;
10import org.onlab.onos.cli.AbstractShellCommand;
11import org.onlab.onos.metrics.intent.IntentMetricsService;
12import org.onlab.onos.net.intent.IntentEvent;
13
14/**
15 * Command to show the list of last intent events.
16 */
17@Command(scope = "onos", name = "intents-events",
18 description = "Lists the last intent events")
19public class IntentEventsListCommand extends AbstractShellCommand {
20
21 private static final String FORMAT_EVENT = "Event=%s";
22
23 @Override
24 protected void execute() {
25 IntentMetricsService service = get(IntentMetricsService.class);
26
27 if (outputJson()) {
28 print("%s", json(service.getEvents()));
29 } else {
30 for (IntentEvent event : service.getEvents()) {
31 print(FORMAT_EVENT, event);
32 print(""); // Extra empty line for clarity
33 }
34 }
35 }
36
37 /**
38 * Produces a JSON array of intent events.
39 *
40 * @param intentEvents the intent events with the data
41 * @return JSON array with the intent events
42 */
43 private JsonNode json(List<IntentEvent> intentEvents) {
44 ObjectMapper mapper = new ObjectMapper();
45 ArrayNode result = mapper.createArrayNode();
46
47 for (IntentEvent event : intentEvents) {
48 result.add(json(mapper, event));
49 }
50 return result;
51 }
52
53 /**
54 * Produces JSON object for a intent event.
55 *
56 * @param mapper the JSON object mapper to use
57 * @param intentEvent the intent event with the data
58 * @return JSON object for the intent event
59 */
60 private ObjectNode json(ObjectMapper mapper, IntentEvent intentEvent) {
61 ObjectNode result = mapper.createObjectNode();
62
63 result.put("time", intentEvent.time())
64 .put("type", intentEvent.type().toString())
65 .put("event", intentEvent.toString());
66 return result;
67 }
68}