HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.events; |
| 17 | |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 18 | import java.io.PrintWriter; |
| 19 | import java.io.StringWriter; |
| 20 | import java.util.List; |
| 21 | import java.util.function.Predicate; |
| 22 | import java.util.stream.Collector; |
| 23 | import java.util.stream.Stream; |
| 24 | |
| 25 | import org.apache.karaf.shell.commands.Command; |
| 26 | import org.apache.karaf.shell.commands.Option; |
| 27 | import org.joda.time.LocalDateTime; |
| 28 | import org.onosproject.cli.AbstractShellCommand; |
| 29 | import org.onosproject.cluster.ClusterEvent; |
| 30 | import org.onosproject.event.Event; |
| 31 | import org.onosproject.mastership.MastershipEvent; |
| 32 | import org.onosproject.net.Link; |
| 33 | import org.onosproject.net.device.DeviceEvent; |
| 34 | import org.onosproject.net.host.HostEvent; |
| 35 | import org.onosproject.net.link.LinkEvent; |
| 36 | import org.onosproject.net.topology.Topology; |
| 37 | import org.onosproject.net.topology.TopologyEvent; |
| 38 | |
| 39 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 40 | import com.fasterxml.jackson.databind.JsonNode; |
| 41 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 42 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 43 | import com.google.common.base.MoreObjects; |
| 44 | import com.google.common.collect.ImmutableList; |
| 45 | |
Ray Milkey | 9f87e51 | 2016-01-05 10:00:22 -0800 | [diff] [blame] | 46 | import static java.util.stream.Collectors.toList; |
Ray Milkey | 9f87e51 | 2016-01-05 10:00:22 -0800 | [diff] [blame] | 47 | |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 48 | /** |
| 49 | * Command to print history of instance local ONOS Events. |
| 50 | */ |
| 51 | @Command(scope = "onos", name = "events", |
| 52 | description = "Command to print history of instance local ONOS Events") |
| 53 | public class EventsCommand |
| 54 | extends AbstractShellCommand { |
| 55 | |
| 56 | @Option(name = "--all", aliases = "-a", |
| 57 | description = "Include all Events (default behavior)", |
| 58 | required = false) |
| 59 | private boolean all = false; |
| 60 | |
| 61 | @Option(name = "--mastership", aliases = "-m", |
| 62 | description = "Include MastershipEvent", |
| 63 | required = false) |
| 64 | private boolean mastership = false; |
| 65 | |
| 66 | @Option(name = "--device", aliases = "-d", |
| 67 | description = "Include DeviceEvent", |
| 68 | required = false) |
| 69 | private boolean device = false; |
| 70 | |
| 71 | @Option(name = "--link", aliases = "-l", |
| 72 | description = "Include LinkEvent", |
| 73 | required = false) |
| 74 | private boolean link = false; |
| 75 | |
| 76 | @Option(name = "--topology", aliases = "-t", |
| 77 | description = "Include TopologyEvent", |
| 78 | required = false) |
| 79 | private boolean topology = false; |
| 80 | |
| 81 | @Option(name = "--host", aliases = "-t", |
| 82 | description = "Include HostEvent", |
| 83 | required = false) |
| 84 | private boolean host = false; |
| 85 | |
| 86 | @Option(name = "--cluster", aliases = "-c", |
| 87 | description = "Include ClusterEvent", |
| 88 | required = false) |
| 89 | private boolean cluster = false; |
| 90 | |
| 91 | @Option(name = "--max-events", aliases = "-n", |
| 92 | description = "Maximum number of events to print", |
| 93 | required = false, |
| 94 | valueToShowInHelp = "-1 [no limit]") |
| 95 | private long maxSize = -1; |
| 96 | |
| 97 | @Override |
| 98 | protected void execute() { |
| 99 | EventHistoryService eventHistoryService = get(EventHistoryService.class); |
| 100 | |
| 101 | Stream<Event<?, ?>> events = eventHistoryService.history().stream(); |
| 102 | |
| 103 | boolean dumpAll = all || !(mastership || device || link || topology || host); |
| 104 | |
| 105 | if (!dumpAll) { |
| 106 | Predicate<Event<?, ?>> filter = (defaultIs) -> false; |
| 107 | |
| 108 | if (mastership) { |
| 109 | filter = filter.or(evt -> evt instanceof MastershipEvent); |
| 110 | } |
| 111 | if (device) { |
| 112 | filter = filter.or(evt -> evt instanceof DeviceEvent); |
| 113 | } |
| 114 | if (link) { |
| 115 | filter = filter.or(evt -> evt instanceof LinkEvent); |
| 116 | } |
| 117 | if (topology) { |
| 118 | filter = filter.or(evt -> evt instanceof TopologyEvent); |
| 119 | } |
| 120 | if (host) { |
| 121 | filter = filter.or(evt -> evt instanceof HostEvent); |
| 122 | } |
| 123 | if (cluster) { |
| 124 | filter = filter.or(evt -> evt instanceof ClusterEvent); |
| 125 | } |
| 126 | |
| 127 | events = events.filter(filter); |
| 128 | } |
| 129 | |
| 130 | if (maxSize > 0) { |
| 131 | events = events.limit(maxSize); |
| 132 | } |
| 133 | |
| 134 | if (outputJson()) { |
| 135 | ArrayNode jsonEvents = events.map(this::json).collect(toArrayNode()); |
| 136 | printJson(jsonEvents); |
| 137 | } else { |
| 138 | events.forEach(this::printEvent); |
| 139 | } |
| 140 | |
| 141 | } |
| 142 | |
| 143 | private Collector<JsonNode, ArrayNode, ArrayNode> toArrayNode() { |
| 144 | return Collector.of(() -> mapper().createArrayNode(), |
| 145 | ArrayNode::add, |
| 146 | ArrayNode::addAll); |
| 147 | } |
| 148 | |
| 149 | private ObjectNode json(Event<?, ?> event) { |
| 150 | ObjectNode result = mapper().createObjectNode(); |
| 151 | |
| 152 | result.put("time", event.time()) |
| 153 | .put("type", event.type().toString()) |
| 154 | .put("event", event.toString()); |
| 155 | |
| 156 | return result; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Print JsonNode using default pretty printer. |
| 161 | * |
| 162 | * @param json JSON node to print |
| 163 | */ |
Ray Milkey | aef4585 | 2016-01-11 17:13:19 -0800 | [diff] [blame] | 164 | @java.lang.SuppressWarnings("squid:S1148") |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 165 | private void printJson(JsonNode json) { |
| 166 | try { |
| 167 | print("%s", mapper().writerWithDefaultPrettyPrinter().writeValueAsString(json)); |
| 168 | } catch (JsonProcessingException e) { |
| 169 | StringWriter sw = new StringWriter(); |
| 170 | e.printStackTrace(new PrintWriter(sw)); |
| 171 | print("[ERROR] %s\n%s", e.getMessage(), sw.toString()); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | private void printEvent(Event<?, ?> event) { |
| 176 | if (event instanceof DeviceEvent) { |
| 177 | DeviceEvent deviceEvent = (DeviceEvent) event; |
| 178 | if (event.type().toString().startsWith("PORT")) { |
| 179 | // Port event |
| 180 | print("%s %s\t%s/%s [%s]", |
| 181 | new LocalDateTime(event.time()), |
| 182 | event.type(), |
| 183 | deviceEvent.subject().id(), deviceEvent.port().number(), |
| 184 | deviceEvent.port() |
| 185 | ); |
| 186 | } else { |
| 187 | // Device event |
| 188 | print("%s %s\t%s [%s]", |
| 189 | new LocalDateTime(event.time()), |
| 190 | event.type(), |
| 191 | deviceEvent.subject().id(), |
| 192 | deviceEvent.subject() |
| 193 | ); |
| 194 | } |
| 195 | |
| 196 | } else if (event instanceof MastershipEvent) { |
| 197 | print("%s %s\t%s [%s]", |
| 198 | new LocalDateTime(event.time()), |
| 199 | event.type(), |
| 200 | event.subject(), |
| 201 | ((MastershipEvent) event).roleInfo()); |
| 202 | |
| 203 | } else if (event instanceof LinkEvent) { |
| 204 | LinkEvent linkEvent = (LinkEvent) event; |
| 205 | Link link = linkEvent.subject(); |
| 206 | print("%s %s\t%s/%s-%s/%s [%s]", |
| 207 | new LocalDateTime(event.time()), |
| 208 | event.type(), |
| 209 | link.src().deviceId(), link.src().port(), link.dst().deviceId(), link.dst().port(), |
| 210 | link); |
| 211 | |
| 212 | } else if (event instanceof HostEvent) { |
| 213 | HostEvent hostEvent = (HostEvent) event; |
| 214 | print("%s %s\t%s [%s->%s]", |
| 215 | new LocalDateTime(event.time()), |
| 216 | event.type(), |
| 217 | hostEvent.subject().id(), |
| 218 | hostEvent.prevSubject(), hostEvent.subject()); |
| 219 | |
| 220 | } else if (event instanceof TopologyEvent) { |
| 221 | TopologyEvent topoEvent = (TopologyEvent) event; |
Ray Milkey | c18d0e3 | 2016-01-04 16:10:59 -0800 | [diff] [blame] | 222 | List<Event> reasons = MoreObjects.firstNonNull(topoEvent.reasons(), |
| 223 | ImmutableList.<Event>of()); |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 224 | Topology topo = topoEvent.subject(); |
| 225 | String summary = String.format("(d=%d,l=%d,c=%d)", |
| 226 | topo.deviceCount(), |
| 227 | topo.linkCount(), |
| 228 | topo.clusterCount()); |
| 229 | print("%s %s%s [%s]", |
| 230 | new LocalDateTime(event.time()), |
| 231 | event.type(), |
| 232 | summary, |
| 233 | reasons.stream().map(e -> e.type()).collect(toList())); |
| 234 | |
| 235 | } else if (event instanceof ClusterEvent) { |
| 236 | print("%s %s\t%s [%s]", |
| 237 | new LocalDateTime(event.time()), |
| 238 | event.type(), |
| 239 | ((ClusterEvent) event).subject().id(), |
| 240 | event.subject()); |
| 241 | |
| 242 | } else { |
| 243 | // Unknown Event? |
| 244 | print("%s %s\t%s [%s]", |
| 245 | new LocalDateTime(event.time()), |
| 246 | event.type(), |
| 247 | event.subject(), |
| 248 | event); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | } |