blob: f8d0c1a4e0149333e472943c0d446664c6ba37f8 [file] [log] [blame]
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -07001package org.onlab.onos.metrics.topology.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.event.Event;
12import org.onlab.onos.metrics.topology.TopologyMetricsService;
13import org.onlab.onos.net.topology.TopologyEvent;
14
15/**
16 * Command to show the list of last topology events.
17 */
18@Command(scope = "onos", name = "topology-events",
19 description = "Lists the last topology events")
20public class TopologyEventsListCommand extends AbstractShellCommand {
21
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070022 private static final String FORMAT_EVENT = "Event=%s";
23 private static final String FORMAT_REASON = " Reason=%s";
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070024
25 @Override
26 protected void execute() {
27 TopologyMetricsService service = get(TopologyMetricsService.class);
28
29 if (outputJson()) {
30 print("%s", json(service.getEvents()));
31 } else {
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070032 for (Event event : service.getEvents()) {
33 print(FORMAT_EVENT, event);
34 if (event instanceof TopologyEvent) {
35 TopologyEvent topologyEvent = (TopologyEvent) event;
36 for (Event reason : topologyEvent.reasons()) {
37 print(FORMAT_REASON, reason);
38 }
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070039 }
40 print(""); // Extra empty line for clarity
41 }
42 }
43 }
44
45 /**
46 * Produces a JSON array of topology events.
47 *
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070048 * @param events the topology events with the data
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070049 * @return JSON array with the topology events
50 */
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070051 private JsonNode json(List<Event> events) {
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070052 ObjectMapper mapper = new ObjectMapper();
53 ArrayNode result = mapper.createArrayNode();
54
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070055 for (Event event : events) {
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070056 result.add(json(mapper, event));
57 }
58 return result;
59 }
60
61 /**
62 * Produces JSON object for a topology event.
63 *
64 * @param mapper the JSON object mapper to use
65 * @param topologyEvent the topology event with the data
66 * @return JSON object for the topology event
67 */
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070068 private ObjectNode json(ObjectMapper mapper, Event event) {
69 ObjectNode result = mapper.createObjectNode();
70
71 result.put("time", event.time())
72 .put("type", event.type().toString())
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070073 .put("event", event.toString());
74
75 // Add the reasons if a TopologyEvent
76 if (event instanceof TopologyEvent) {
77 TopologyEvent topologyEvent = (TopologyEvent) event;
78 ArrayNode reasons = mapper.createArrayNode();
79 for (Event reason : topologyEvent.reasons()) {
80 reasons.add(json(mapper, reason));
81 }
82 result.put("reasons", reasons);
83 }
84
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070085 return result;
86 }
87}