private ObjectNode json(Event<?, ?> event) { ObjectNode result = mapper().createObjectNode(); result .put("time", event.time()) .put("type", event.type().toString()) .put("event", event.toString()); return result; }
private void printEvent(Event<?, ?> event) { if (event instanceof DeviceEvent) { DeviceEvent deviceEvent = (DeviceEvent) event; if (event.type().toString().startsWith("PORT")) { // Port event print( "%s %s\t%s/%s [%s]", new LocalDateTime(event.time()), event.type(), deviceEvent.subject().id(), deviceEvent.port().number(), deviceEvent.port()); } else { // Device event print( "%s %s\t%s [%s]", new LocalDateTime(event.time()), event.type(), deviceEvent.subject().id(), deviceEvent.subject()); } } else if (event instanceof MastershipEvent) { print( "%s %s\t%s [%s]", new LocalDateTime(event.time()), event.type(), event.subject(), ((MastershipEvent) event).roleInfo()); } else if (event instanceof LinkEvent) { LinkEvent linkEvent = (LinkEvent) event; Link link = linkEvent.subject(); print( "%s %s\t%s/%s-%s/%s [%s]", new LocalDateTime(event.time()), event.type(), link.src().deviceId(), link.src().port(), link.dst().deviceId(), link.dst().port(), link); } else if (event instanceof HostEvent) { HostEvent hostEvent = (HostEvent) event; print( "%s %s\t%s [%s->%s]", new LocalDateTime(event.time()), event.type(), hostEvent.subject().id(), hostEvent.prevSubject(), hostEvent.subject()); } else if (event instanceof TopologyEvent) { TopologyEvent topoEvent = (TopologyEvent) event; List<Event> reasons = MoreObjects.firstNonNull(topoEvent.reasons(), ImmutableList.<Event>of()); Topology topo = topoEvent.subject(); String summary = String.format( "(d=%d,l=%d,c=%d)", topo.deviceCount(), topo.linkCount(), topo.clusterCount()); print( "%s %s%s [%s]", new LocalDateTime(event.time()), event.type(), summary, reasons.stream().map(e -> e.type()).collect(toList())); } else if (event instanceof ClusterEvent) { print( "%s %s\t%s [%s]", new LocalDateTime(event.time()), event.type(), ((ClusterEvent) event).subject().id(), event.subject()); } else { // Unknown Event? print( "%s %s\t%s [%s]", new LocalDateTime(event.time()), event.type(), event.subject(), event); } }