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; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 24 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 25 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.cli.AbstractShellCommand; |
| 27 | import org.onosproject.event.Event; |
| 28 | import org.onosproject.metrics.topology.TopologyMetricsService; |
| 29 | import org.onosproject.net.topology.TopologyEvent; |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Command to show the list of last topology events. |
| 33 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 34 | @Service |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 35 | @Command(scope = "onos", name = "topology-events", |
| 36 | description = "Lists the last topology events") |
| 37 | public class TopologyEventsListCommand extends AbstractShellCommand { |
| 38 | |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 39 | private static final String FORMAT_EVENT = "Event=%s"; |
| 40 | private static final String FORMAT_REASON = " Reason=%s"; |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 41 | |
| 42 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 43 | protected void doExecute() { |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 44 | TopologyMetricsService service = get(TopologyMetricsService.class); |
| 45 | |
| 46 | if (outputJson()) { |
| 47 | print("%s", json(service.getEvents())); |
| 48 | } else { |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 49 | 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 Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 56 | } |
| 57 | print(""); // Extra empty line for clarity |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Produces a JSON array of topology events. |
| 64 | * |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 65 | * @param events the topology events with the data |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 66 | * @return JSON array with the topology events |
| 67 | */ |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 68 | private JsonNode json(List<Event> events) { |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 69 | ObjectMapper mapper = new ObjectMapper(); |
| 70 | ArrayNode result = mapper.createArrayNode(); |
| 71 | |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 72 | for (Event event : events) { |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 73 | 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 Milkey | 9d810f6 | 2015-02-13 11:20:58 -0800 | [diff] [blame] | 82 | * @param event the topology event with the data |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 83 | * @return JSON object for the topology event |
| 84 | */ |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 85 | 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 Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 90 | .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 Milkey | 9d810f6 | 2015-02-13 11:20:58 -0800 | [diff] [blame] | 99 | result.set("reasons", reasons); |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 102 | return result; |
| 103 | } |
| 104 | } |