blob: 29db7033b2a4205edf5d7b2a82a24a4834ada947 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070019package org.onlab.onos.metrics.topology.cli;
20
21import java.util.List;
22
23import com.fasterxml.jackson.databind.JsonNode;
24import com.fasterxml.jackson.databind.ObjectMapper;
25import com.fasterxml.jackson.databind.node.ArrayNode;
26import com.fasterxml.jackson.databind.node.ObjectNode;
27import org.apache.karaf.shell.commands.Command;
28import org.onlab.onos.cli.AbstractShellCommand;
29import org.onlab.onos.event.Event;
30import org.onlab.onos.metrics.topology.TopologyMetricsService;
31import org.onlab.onos.net.topology.TopologyEvent;
32
33/**
34 * Command to show the list of last topology events.
35 */
36@Command(scope = "onos", name = "topology-events",
37 description = "Lists the last topology events")
38public class TopologyEventsListCommand extends AbstractShellCommand {
39
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070040 private static final String FORMAT_EVENT = "Event=%s";
41 private static final String FORMAT_REASON = " Reason=%s";
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070042
43 @Override
44 protected void execute() {
45 TopologyMetricsService service = get(TopologyMetricsService.class);
46
47 if (outputJson()) {
48 print("%s", json(service.getEvents()));
49 } else {
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070050 for (Event event : service.getEvents()) {
51 print(FORMAT_EVENT, event);
52 if (event instanceof TopologyEvent) {
53 TopologyEvent topologyEvent = (TopologyEvent) event;
54 for (Event reason : topologyEvent.reasons()) {
55 print(FORMAT_REASON, reason);
56 }
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070057 }
58 print(""); // Extra empty line for clarity
59 }
60 }
61 }
62
63 /**
64 * Produces a JSON array of topology events.
65 *
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070066 * @param events the topology events with the data
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070067 * @return JSON array with the topology events
68 */
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070069 private JsonNode json(List<Event> events) {
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070070 ObjectMapper mapper = new ObjectMapper();
71 ArrayNode result = mapper.createArrayNode();
72
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070073 for (Event event : events) {
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070074 result.add(json(mapper, event));
75 }
76 return result;
77 }
78
79 /**
80 * Produces JSON object for a topology event.
81 *
82 * @param mapper the JSON object mapper to use
83 * @param topologyEvent the topology event with the data
84 * @return JSON object for the topology event
85 */
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070086 private ObjectNode json(ObjectMapper mapper, Event event) {
87 ObjectNode result = mapper.createObjectNode();
88
89 result.put("time", event.time())
90 .put("type", event.type().toString())
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070091 .put("event", event.toString());
92
93 // Add the reasons if a TopologyEvent
94 if (event instanceof TopologyEvent) {
95 TopologyEvent topologyEvent = (TopologyEvent) event;
96 ArrayNode reasons = mapper.createArrayNode();
97 for (Event reason : topologyEvent.reasons()) {
98 reasons.add(json(mapper, reason));
99 }
100 result.put("reasons", reasons);
101 }
102
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -0700103 return result;
104 }
105}