Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.metrics.topology.cli; |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 17 | |
| 18 | import java.util.List; |
| 19 | |
| 20 | import com.fasterxml.jackson.databind.JsonNode; |
| 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 23 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 24 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.event.Event; |
| 27 | import org.onosproject.metrics.topology.TopologyMetricsService; |
| 28 | import org.onosproject.net.topology.TopologyEvent; |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 29 | |
| 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") |
| 35 | public class TopologyEventsListCommand extends AbstractShellCommand { |
| 36 | |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 37 | private static final String FORMAT_EVENT = "Event=%s"; |
| 38 | private static final String FORMAT_REASON = " Reason=%s"; |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 39 | |
| 40 | @Override |
| 41 | protected void execute() { |
| 42 | TopologyMetricsService service = get(TopologyMetricsService.class); |
| 43 | |
| 44 | if (outputJson()) { |
| 45 | print("%s", json(service.getEvents())); |
| 46 | } else { |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 47 | 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 Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 54 | } |
| 55 | print(""); // Extra empty line for clarity |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Produces a JSON array of topology events. |
| 62 | * |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 63 | * @param events the topology events with the data |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 64 | * @return JSON array with the topology events |
| 65 | */ |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 66 | private JsonNode json(List<Event> events) { |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 67 | ObjectMapper mapper = new ObjectMapper(); |
| 68 | ArrayNode result = mapper.createArrayNode(); |
| 69 | |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 70 | for (Event event : events) { |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 71 | 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 Milkey | 9d810f6 | 2015-02-13 11:20:58 -0800 | [diff] [blame] | 80 | * @param event the topology event with the data |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 81 | * @return JSON object for the topology event |
| 82 | */ |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 83 | 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 Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 88 | .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 Milkey | 9d810f6 | 2015-02-13 11:20:58 -0800 | [diff] [blame] | 97 | result.set("reasons", reasons); |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 100 | return result; |
| 101 | } |
| 102 | } |