blob: 71e7b5474ceb3f4083d4b0125d78640de0653a24 [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;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.event.Event;
27import org.onosproject.metrics.topology.TopologyMetricsService;
28import org.onosproject.net.topology.TopologyEvent;
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070029
30/**
31 * Command to show the list of last topology events.
32 */
33@Command(scope = "onos", name = "topology-events",
34 description = "Lists the last topology events")
35public class TopologyEventsListCommand extends AbstractShellCommand {
36
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070037 private static final String FORMAT_EVENT = "Event=%s";
38 private static final String FORMAT_REASON = " Reason=%s";
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070039
40 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070041 protected void doExecute() {
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070042 TopologyMetricsService service = get(TopologyMetricsService.class);
43
44 if (outputJson()) {
45 print("%s", json(service.getEvents()));
46 } else {
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070047 for (Event event : service.getEvents()) {
48 print(FORMAT_EVENT, event);
49 if (event instanceof TopologyEvent) {
50 TopologyEvent topologyEvent = (TopologyEvent) event;
51 for (Event reason : topologyEvent.reasons()) {
52 print(FORMAT_REASON, reason);
53 }
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070054 }
55 print(""); // Extra empty line for clarity
56 }
57 }
58 }
59
60 /**
61 * Produces a JSON array of topology events.
62 *
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070063 * @param events the topology events with the data
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070064 * @return JSON array with the topology events
65 */
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070066 private JsonNode json(List<Event> events) {
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070067 ObjectMapper mapper = new ObjectMapper();
68 ArrayNode result = mapper.createArrayNode();
69
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070070 for (Event event : events) {
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070071 result.add(json(mapper, event));
72 }
73 return result;
74 }
75
76 /**
77 * Produces JSON object for a topology event.
78 *
79 * @param mapper the JSON object mapper to use
Ray Milkey9d810f62015-02-13 11:20:58 -080080 * @param event the topology event with the data
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070081 * @return JSON object for the topology event
82 */
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -070083 private ObjectNode json(ObjectMapper mapper, Event event) {
84 ObjectNode result = mapper.createObjectNode();
85
86 result.put("time", event.time())
87 .put("type", event.type().toString())
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070088 .put("event", event.toString());
89
90 // Add the reasons if a TopologyEvent
91 if (event instanceof TopologyEvent) {
92 TopologyEvent topologyEvent = (TopologyEvent) event;
93 ArrayNode reasons = mapper.createArrayNode();
94 for (Event reason : topologyEvent.reasons()) {
95 reasons.add(json(mapper, reason));
96 }
Ray Milkey9d810f62015-02-13 11:20:58 -080097 result.set("reasons", reasons);
Pavlin Radoslavov5ba8b282014-10-23 01:03:10 -070098 }
99
Pavlin Radoslavov64d9e472014-10-21 22:01:08 -0700100 return result;
101 }
102}