blob: 2fdb5eb91a08d453c67e92524ee42f0073f8a08b [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.metrics.topology.cli;
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070017
18import java.util.List;
19
20import com.fasterxml.jackson.databind.JsonNode;
21import com.fasterxml.jackson.databind.ObjectMapper;
22import com.fasterxml.jackson.databind.node.ArrayNode;
23import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070024import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070025import org.apache.karaf.shell.api.action.lifecycle.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.cli.AbstractShellCommand;
27import org.onosproject.event.Event;
28import org.onosproject.metrics.topology.TopologyMetricsService;
29import org.onosproject.net.topology.TopologyEvent;
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070030
31/**
32 * Command to show the list of last topology events.
33 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070034@Service
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070035@Command(scope = "onos", name = "topology-events",
36 description = "Lists the last topology events")
37public class TopologyEventsListCommand extends AbstractShellCommand {
38
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070039 private static final String FORMAT_EVENT = "Event=%s";
40 private static final String FORMAT_REASON = " Reason=%s";
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070041
42 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070043 protected void doExecute() {
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070044 TopologyMetricsService service = get(TopologyMetricsService.class);
45
46 if (outputJson()) {
47 print("%s", json(service.getEvents()));
48 } else {
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070049 for (Event event : service.getEvents()) {
50 print(FORMAT_EVENT, event);
51 if (event instanceof TopologyEvent) {
52 TopologyEvent topologyEvent = (TopologyEvent) event;
53 for (Event reason : topologyEvent.reasons()) {
54 print(FORMAT_REASON, reason);
55 }
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070056 }
57 print(""); // Extra empty line for clarity
58 }
59 }
60 }
61
62 /**
63 * Produces a JSON array of topology events.
64 *
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070065 * @param events the topology events with the data
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070066 * @return JSON array with the topology events
67 */
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070068 private JsonNode json(List<Event> events) {
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070069 ObjectMapper mapper = new ObjectMapper();
70 ArrayNode result = mapper.createArrayNode();
71
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070072 for (Event event : events) {
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070073 result.add(json(mapper, event));
74 }
75 return result;
76 }
77
78 /**
79 * Produces JSON object for a topology event.
80 *
81 * @param mapper the JSON object mapper to use
Ray Milkey9d810f62015-02-13 11:20:58 -080082 * @param event the topology event with the data
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070083 * @return JSON object for the topology event
84 */
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070085 private ObjectNode json(ObjectMapper mapper, Event event) {
86 ObjectNode result = mapper.createObjectNode();
87
88 result.put("time", event.time())
89 .put("type", event.type().toString())
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070090 .put("event", event.toString());
91
92 // Add the reasons if a TopologyEvent
93 if (event instanceof TopologyEvent) {
94 TopologyEvent topologyEvent = (TopologyEvent) event;
95 ArrayNode reasons = mapper.createArrayNode();
96 for (Event reason : topologyEvent.reasons()) {
97 reasons.add(json(mapper, reason));
98 }
Ray Milkey9d810f62015-02-13 11:20:58 -080099 result.set("reasons", reasons);
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -0700100 }
101
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -0700102 return result;
103 }
104}