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