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 | |
| 22 | private static final String FORMAT_EVENT = |
| 23 | "Topology Event time=%d type=%s subject=%s"; |
| 24 | private static final String FORMAT_REASON = |
| 25 | " Reason time=%d type=%s subject=%s"; |
| 26 | |
| 27 | @Override |
| 28 | protected void execute() { |
| 29 | TopologyMetricsService service = get(TopologyMetricsService.class); |
| 30 | |
| 31 | if (outputJson()) { |
| 32 | print("%s", json(service.getEvents())); |
| 33 | } else { |
| 34 | for (TopologyEvent event : service.getEvents()) { |
| 35 | print(FORMAT_EVENT, event.time(), event.type(), |
| 36 | event.subject()); |
| 37 | for (Event reason : event.reasons()) { |
| 38 | print(FORMAT_REASON, reason.time(), reason.type(), |
| 39 | reason.subject()); |
| 40 | } |
| 41 | print(""); // Extra empty line for clarity |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Produces a JSON array of topology events. |
| 48 | * |
| 49 | * @param topologyEvents the topology events with the data |
| 50 | * @return JSON array with the topology events |
| 51 | */ |
| 52 | private JsonNode json(List<TopologyEvent> topologyEvents) { |
| 53 | ObjectMapper mapper = new ObjectMapper(); |
| 54 | ArrayNode result = mapper.createArrayNode(); |
| 55 | |
| 56 | for (TopologyEvent event : topologyEvents) { |
| 57 | result.add(json(mapper, event)); |
| 58 | } |
| 59 | return result; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Produces JSON object for a topology event. |
| 64 | * |
| 65 | * @param mapper the JSON object mapper to use |
| 66 | * @param topologyEvent the topology event with the data |
| 67 | * @return JSON object for the topology event |
| 68 | */ |
| 69 | private ObjectNode json(ObjectMapper mapper, TopologyEvent topologyEvent) { |
| 70 | ObjectNode result = mapper.createObjectNode(); |
| 71 | ArrayNode reasons = mapper.createArrayNode(); |
| 72 | |
| 73 | for (Event reason : topologyEvent.reasons()) { |
| 74 | reasons.add(json(mapper, reason)); |
| 75 | } |
| 76 | result.put("time", topologyEvent.time()) |
| 77 | .put("type", topologyEvent.type().toString()) |
| 78 | .put("subject", topologyEvent.subject().toString()) |
| 79 | .put("reasons", reasons); |
| 80 | return result; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Produces JSON object for a generic event. |
| 85 | * |
| 86 | * @param event the generic event with the data |
| 87 | * @return JSON object for the generic event |
| 88 | */ |
| 89 | private ObjectNode json(ObjectMapper mapper, Event event) { |
| 90 | ObjectNode result = mapper.createObjectNode(); |
| 91 | |
| 92 | result.put("time", event.time()) |
| 93 | .put("type", event.type().toString()) |
| 94 | .put("subject", event.subject().toString()); |
| 95 | return result; |
| 96 | } |
| 97 | } |