Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 1 | package org.onlab.onos.metrics.topology.cli; |
| 2 | |
| 3 | import java.util.List; |
| 4 | |
| 5 | import com.fasterxml.jackson.databind.JsonNode; |
| 6 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 8 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 9 | import org.apache.karaf.shell.commands.Command; |
| 10 | import org.onlab.onos.cli.AbstractShellCommand; |
| 11 | import org.onlab.onos.event.Event; |
| 12 | import org.onlab.onos.metrics.topology.TopologyMetricsService; |
| 13 | import 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") |
| 20 | public class TopologyEventsListCommand extends AbstractShellCommand { |
| 21 | |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 22 | private static final String FORMAT_EVENT = "Event=%s"; |
| 23 | private static final String FORMAT_REASON = " Reason=%s"; |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 24 | |
| 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 Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 32 | 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 Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 39 | } |
| 40 | print(""); // Extra empty line for clarity |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Produces a JSON array of topology events. |
| 47 | * |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 48 | * @param events the topology events with the data |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 49 | * @return JSON array with the topology events |
| 50 | */ |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 51 | private JsonNode json(List<Event> events) { |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 52 | ObjectMapper mapper = new ObjectMapper(); |
| 53 | ArrayNode result = mapper.createArrayNode(); |
| 54 | |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 55 | for (Event event : events) { |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 56 | 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 Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 68 | 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 Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 73 | .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 Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 85 | return result; |
| 86 | } |
| 87 | } |