@Test public void testToRow() { Trace t1 = new Trace(ACCEPTED, ImmutableList.of()); Trace t2 = new Trace(EXITS_NETWORK, ImmutableList.of()); { BidirectionalTrace bt1 = new BidirectionalTrace(FORWARD_FLOW, t1, ImmutableSet.of(), REVERSE_FLOW, t2); BidirectionalTrace bt2 = new BidirectionalTrace(FORWARD_FLOW, t2, ImmutableSet.of(), REVERSE_FLOW, t2); Row row = toRow(bt1.getKey(), ImmutableList.of(bt1, bt2)); assertThat( row, allOf( hasColumn(COL_FORWARD_FLOW, equalTo(FORWARD_FLOW), Schema.FLOW), hasColumn(COL_FORWARD_TRACES, contains(t1, t2), Schema.list(Schema.TRACE)), hasColumn(COL_NEW_SESSIONS, empty(), Schema.list(Schema.STRING)), hasColumn(COL_REVERSE_FLOW, equalTo(REVERSE_FLOW), Schema.FLOW), hasColumn(COL_REVERSE_TRACES, contains(t2), Schema.list(Schema.TRACE)))); } { BidirectionalTrace bt1 = new BidirectionalTrace(FORWARD_FLOW, t1, ImmutableSet.of(), REVERSE_FLOW, t1); BidirectionalTrace bt2 = new BidirectionalTrace(FORWARD_FLOW, t1, ImmutableSet.of(), REVERSE_FLOW, t2); Row row = toRow(bt1.getKey(), ImmutableList.of(bt1, bt2)); assertThat( row, allOf( hasColumn(COL_FORWARD_FLOW, equalTo(FORWARD_FLOW), Schema.FLOW), hasColumn(COL_FORWARD_TRACES, contains(t1), Schema.list(Schema.TRACE)), hasColumn(COL_NEW_SESSIONS, empty(), Schema.list(Schema.STRING)), hasColumn(COL_REVERSE_FLOW, equalTo(REVERSE_FLOW), Schema.FLOW), hasColumn(COL_REVERSE_TRACES, contains(t1, t2), Schema.list(Schema.TRACE)))); } { BidirectionalTrace bt1 = new BidirectionalTrace(FORWARD_FLOW, t1, ImmutableSet.of(), REVERSE_FLOW, t1); BidirectionalTrace bt2 = new BidirectionalTrace(FORWARD_FLOW, t1, ImmutableSet.of(), REVERSE_FLOW, t2); BidirectionalTrace bt3 = new BidirectionalTrace(FORWARD_FLOW, t2, ImmutableSet.of(), REVERSE_FLOW, t1); BidirectionalTrace bt4 = new BidirectionalTrace(FORWARD_FLOW, t2, ImmutableSet.of(), REVERSE_FLOW, t2); Row row = toRow(bt1.getKey(), ImmutableList.of(bt1, bt2, bt3, bt4)); assertThat( row, allOf( hasColumn(COL_FORWARD_FLOW, equalTo(FORWARD_FLOW), Schema.FLOW), hasColumn(COL_FORWARD_TRACES, contains(t1, t2), Schema.list(Schema.TRACE)), hasColumn(COL_NEW_SESSIONS, empty(), Schema.list(Schema.STRING)), hasColumn(COL_REVERSE_FLOW, equalTo(REVERSE_FLOW), Schema.FLOW), hasColumn(COL_REVERSE_TRACES, contains(t1, t2), Schema.list(Schema.TRACE)))); } { FirewallSessionTraceInfo session1 = new FirewallSessionTraceInfo( "session1", Accept.INSTANCE, ImmutableSet.of(), DUMMY_SESSION_FLOW, null); FirewallSessionTraceInfo session2 = new FirewallSessionTraceInfo( "session2", Accept.INSTANCE, ImmutableSet.of(), DUMMY_SESSION_FLOW, null); Set<FirewallSessionTraceInfo> sessions = ImmutableSet.of(session1, session2); BidirectionalTrace bt = new BidirectionalTrace(FORWARD_FLOW, t1, sessions, REVERSE_FLOW, t1); Row row = toRow(bt.getKey(), ImmutableList.of(bt)); assertThat( row, allOf( hasColumn(COL_FORWARD_FLOW, equalTo(FORWARD_FLOW), Schema.FLOW), hasColumn(COL_FORWARD_TRACES, contains(t1), Schema.list(Schema.TRACE)), hasColumn( COL_NEW_SESSIONS, contains("session1", "session2"), Schema.list(Schema.STRING)), hasColumn(COL_REVERSE_FLOW, equalTo(REVERSE_FLOW), Schema.FLOW), hasColumn(COL_REVERSE_TRACES, contains(t1), Schema.list(Schema.TRACE)))); } { Trace t = new Trace(DENIED_IN, ImmutableList.of()); BidirectionalTrace bt = new BidirectionalTrace(FORWARD_FLOW, t, ImmutableSet.of(), null, null); Row row = toRow(bt.getKey(), ImmutableList.of(bt)); assertThat( row, allOf( hasColumn(COL_FORWARD_FLOW, equalTo(FORWARD_FLOW), Schema.FLOW), hasColumn(COL_FORWARD_TRACES, contains(t), Schema.list(Schema.TRACE)), hasColumn(COL_NEW_SESSIONS, empty(), Schema.list(Schema.STRING)), hasColumn(COL_REVERSE_FLOW, nullValue(), Schema.FLOW), hasColumn(COL_REVERSE_TRACES, empty(), Schema.list(Schema.TRACE)))); } }
@VisibleForTesting static Row toRow(BidirectionalTrace.Key key, List<BidirectionalTrace> traces) { assert traces.stream().allMatch(trace -> trace.getKey().equals(key)); List<Trace> forwardTraces = traces.stream() .map(BidirectionalTrace::getForwardTrace) .distinct() .collect(ImmutableList.toImmutableList()); List<Trace> reverseTraces = traces.stream() .map(BidirectionalTrace::getReverseTrace) .filter(Objects::nonNull) .distinct() .collect(ImmutableList.toImmutableList()); List<String> sessionHops = key.getNewSessions().stream() .map(FirewallSessionTraceInfo::getHostname) .collect(ImmutableList.toImmutableList()); return Row.of( COL_FORWARD_FLOW, key.getForwardFlow(), COL_FORWARD_TRACES, forwardTraces, COL_NEW_SESSIONS, sessionHops, COL_REVERSE_FLOW, key.getReverseFlow(), COL_REVERSE_TRACES, reverseTraces); }
BidirectionalTracerouteAnswerer extends Answerer { @VisibleForTesting static Row toRow(BidirectionalTrace.Key key, List<BidirectionalTrace> traces) { assert traces.stream().allMatch(trace -> trace.getKey().equals(key)); List<Trace> forwardTraces = traces.stream() .map(BidirectionalTrace::getForwardTrace) .distinct() .collect(ImmutableList.toImmutableList()); List<Trace> reverseTraces = traces.stream() .map(BidirectionalTrace::getReverseTrace) .filter(Objects::nonNull) .distinct() .collect(ImmutableList.toImmutableList()); List<String> sessionHops = key.getNewSessions().stream() .map(FirewallSessionTraceInfo::getHostname) .collect(ImmutableList.toImmutableList()); return Row.of( COL_FORWARD_FLOW, key.getForwardFlow(), COL_FORWARD_TRACES, forwardTraces, COL_NEW_SESSIONS, sessionHops, COL_REVERSE_FLOW, key.getReverseFlow(), COL_REVERSE_TRACES, reverseTraces); } }
BidirectionalTracerouteAnswerer extends Answerer { @VisibleForTesting static Row toRow(BidirectionalTrace.Key key, List<BidirectionalTrace> traces) { assert traces.stream().allMatch(trace -> trace.getKey().equals(key)); List<Trace> forwardTraces = traces.stream() .map(BidirectionalTrace::getForwardTrace) .distinct() .collect(ImmutableList.toImmutableList()); List<Trace> reverseTraces = traces.stream() .map(BidirectionalTrace::getReverseTrace) .filter(Objects::nonNull) .distinct() .collect(ImmutableList.toImmutableList()); List<String> sessionHops = key.getNewSessions().stream() .map(FirewallSessionTraceInfo::getHostname) .collect(ImmutableList.toImmutableList()); return Row.of( COL_FORWARD_FLOW, key.getForwardFlow(), COL_FORWARD_TRACES, forwardTraces, COL_NEW_SESSIONS, sessionHops, COL_REVERSE_FLOW, key.getReverseFlow(), COL_REVERSE_TRACES, reverseTraces); } BidirectionalTracerouteAnswerer(Question question, IBatfish batfish); }
BidirectionalTracerouteAnswerer extends Answerer { @VisibleForTesting static Row toRow(BidirectionalTrace.Key key, List<BidirectionalTrace> traces) { assert traces.stream().allMatch(trace -> trace.getKey().equals(key)); List<Trace> forwardTraces = traces.stream() .map(BidirectionalTrace::getForwardTrace) .distinct() .collect(ImmutableList.toImmutableList()); List<Trace> reverseTraces = traces.stream() .map(BidirectionalTrace::getReverseTrace) .filter(Objects::nonNull) .distinct() .collect(ImmutableList.toImmutableList()); List<String> sessionHops = key.getNewSessions().stream() .map(FirewallSessionTraceInfo::getHostname) .collect(ImmutableList.toImmutableList()); return Row.of( COL_FORWARD_FLOW, key.getForwardFlow(), COL_FORWARD_TRACES, forwardTraces, COL_NEW_SESSIONS, sessionHops, COL_REVERSE_FLOW, key.getReverseFlow(), COL_REVERSE_TRACES, reverseTraces); } BidirectionalTracerouteAnswerer(Question question, IBatfish batfish); @Override AnswerElement answer(NetworkSnapshot snapshot); static AnswerElement bidirectionalTracerouteAnswerElement(
      Question question,
      Set<Flow> flows,
      TracerouteEngine tracerouteEngine,
      boolean ignoreFilters,
      @Nullable Integer maxTraces); static TableMetadata metadata(); }
BidirectionalTracerouteAnswerer extends Answerer { @VisibleForTesting static Row toRow(BidirectionalTrace.Key key, List<BidirectionalTrace> traces) { assert traces.stream().allMatch(trace -> trace.getKey().equals(key)); List<Trace> forwardTraces = traces.stream() .map(BidirectionalTrace::getForwardTrace) .distinct() .collect(ImmutableList.toImmutableList()); List<Trace> reverseTraces = traces.stream() .map(BidirectionalTrace::getReverseTrace) .filter(Objects::nonNull) .distinct() .collect(ImmutableList.toImmutableList()); List<String> sessionHops = key.getNewSessions().stream() .map(FirewallSessionTraceInfo::getHostname) .collect(ImmutableList.toImmutableList()); return Row.of( COL_FORWARD_FLOW, key.getForwardFlow(), COL_FORWARD_TRACES, forwardTraces, COL_NEW_SESSIONS, sessionHops, COL_REVERSE_FLOW, key.getReverseFlow(), COL_REVERSE_TRACES, reverseTraces); } BidirectionalTracerouteAnswerer(Question question, IBatfish batfish); @Override AnswerElement answer(NetworkSnapshot snapshot); static AnswerElement bidirectionalTracerouteAnswerElement(
      Question question,
      Set<Flow> flows,
      TracerouteEngine tracerouteEngine,
      boolean ignoreFilters,
      @Nullable Integer maxTraces); static TableMetadata metadata();  }