blob: 3896f274b8dc06358e6155bbc1556f0f0c77fb15 [file] [log] [blame]
HIGUCHI Yuta9092db82016-01-03 18:45:01 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
HIGUCHI Yuta9092db82016-01-03 18:45:01 -08003 *
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 */
16package org.onosproject.events;
17
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080018import java.io.PrintWriter;
19import java.io.StringWriter;
20import java.util.List;
21import java.util.function.Predicate;
22import java.util.stream.Collector;
23import java.util.stream.Stream;
24
25import org.apache.karaf.shell.commands.Command;
26import org.apache.karaf.shell.commands.Option;
27import org.joda.time.LocalDateTime;
28import org.onosproject.cli.AbstractShellCommand;
29import org.onosproject.cluster.ClusterEvent;
30import org.onosproject.event.Event;
31import org.onosproject.mastership.MastershipEvent;
32import org.onosproject.net.Link;
33import org.onosproject.net.device.DeviceEvent;
34import org.onosproject.net.host.HostEvent;
35import org.onosproject.net.link.LinkEvent;
36import org.onosproject.net.topology.Topology;
37import org.onosproject.net.topology.TopologyEvent;
38
39import com.fasterxml.jackson.core.JsonProcessingException;
40import com.fasterxml.jackson.databind.JsonNode;
41import com.fasterxml.jackson.databind.node.ArrayNode;
42import com.fasterxml.jackson.databind.node.ObjectNode;
43import com.google.common.base.MoreObjects;
44import com.google.common.collect.ImmutableList;
45
Ray Milkey9f87e512016-01-05 10:00:22 -080046import static java.util.stream.Collectors.toList;
Ray Milkey9f87e512016-01-05 10:00:22 -080047
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080048/**
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")
53public 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
Jon Hall2eec2c82017-08-02 12:14:32 -0700103 boolean dumpAll = all || !(mastership || device || link || topology || host || cluster);
HIGUCHI Yuta9092db82016-01-03 18:45:01 -0800104
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 Milkeyaef45852016-01-11 17:13:19 -0800164 @java.lang.SuppressWarnings("squid:S1148")
HIGUCHI Yuta9092db82016-01-03 18:45:01 -0800165 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 Milkeyc18d0e32016-01-04 16:10:59 -0800222 List<Event> reasons = MoreObjects.firstNonNull(topoEvent.reasons(),
223 ImmutableList.<Event>of());
HIGUCHI Yuta9092db82016-01-03 18:45:01 -0800224 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}