/** Creates a new TransmissionContext for the specified last-hop node and outgoing interface. */ private TransmissionContext branch( NodeInterfacePair lastHopNodeAndOutgoingInterface, String currentHop) { TransmissionContext transmissionContext = new TransmissionContext( _aclDefinitions, new Node(currentHop), _flowTraces, _hopsSoFar, lastHopNodeAndOutgoingInterface, _newSessions, _namedIpSpaces, _originalFlow, _transformedFlow); return transmissionContext; } }
@Nonnull @VisibleForTesting static Row getRow( String filename, ParseStatus status, @Nullable Collection<String> hostsProduced) { Row.TypedRowBuilder builder = Row.builder(TABLE_METADATA.toColumnMap()); builder.put(COL_FILENAME, filename); builder.put(COL_PARSE_STATUS, status.toString()); ImmutableList.Builder<Node> nodesProduced = ImmutableList.builder(); for (String hostname : firstNonNull(hostsProduced, Collections.<String>emptyList())) { nodesProduced.add(new Node(hostname)); } builder.put(COL_NODES, nodesProduced.build()); return builder.build(); }
@VisibleForTesting static Row getBgpEdgeRow( String node, @Nullable Ip ip, @Nullable Long asNumber, String remoteNode, @Nullable Ip remoteIp, @Nullable Long remoteAsNumber) { RowBuilder row = Row.builder(); row.put(COL_NODE, new Node(node)) .put(COL_IP, ip) .put(COL_AS_NUMBER, asNumber) .put(COL_REMOTE_NODE, new Node(remoteNode)) .put(COL_REMOTE_IP, remoteIp) .put(COL_REMOTE_AS_NUMBER, remoteAsNumber); return row.build(); }
@VisibleForTesting static Row createRow( Map<String, ColumnMetadata> columns, String node, Integer vlan, Map<Integer, Builder<NodeInterfacePair>> switchedVlanInterfaces, Map<Integer, Integer> vlanVnis) { return Row.builder(columns) .put(COL_NODE, new Node(node)) .put(COL_VLAN_ID, vlan) .put(COL_INTERFACES, switchedVlanInterfaces.get(vlan).build()) .put(COL_VXLAN_VNI, vlanVnis.get(vlan)) .build(); }
@VisibleForTesting static Row getRow(String nodeName, Collection<Line> lines) { List<String> exposedLines = new ArrayList<>(); for (Line line : lines) { if (!line.requiresAuthentication()) { exposedLines.add(line.getName()); } } if (exposedLines.isEmpty()) { return null; } RowBuilder row = Row.builder(); row.put(COLUMN_NODE, new Node(nodeName)).put(COLUMN_LINE_NAMES, exposedLines); return row.build(); } }
@Nonnull @VisibleForTesting static Row getRow(String nodeName, Warning warning) { return Row.builder(TABLE_METADATA.toColumnMap()) .put(COL_NODE, new Node(nodeName)) .put(COL_TYPE, ImmutableList.of(warning.getTag())) .put(COL_COMMENT, warning.getText()) .build(); }
@Test public void serialization() { Node node = new Node("testnode", "myId", DeviceType.HOST); Map<String, String> properties = new HashMap<>(); properties.put("key", "value"); node.setProperties(properties); JsonNode jsonNode = BatfishObjectMapper.mapper().valueToTree(node); assertThat(jsonNode.get("id").asText(), equalTo("myId")); assertThat(jsonNode.get("name").asText(), equalTo("testnode")); assertThat(jsonNode.get("type").asText(), equalTo("HOST")); assertThat(jsonNode.get("properties").get("key").asText(), equalTo("value")); } }
@Test public void testFilterMlags() { ImmutableSortedMap<String, Configuration> configs = getConfigs(); TableAnswerElement answer = computeAnswer(ImmutableSet.of("n1", "n2"), Pattern.compile(".*2"), configs); assertThat( answer.getRowsList(), contains( allOf( hasColumn(equalTo(COL_NODE), equalTo(new Node("n2")), Schema.NODE), hasColumn(equalTo(COL_MLAG_ID), equalTo("ID2"), Schema.STRING)))); }
@Test public void testFilterNodes() { ImmutableSortedMap<String, Configuration> configs = getConfigs(); TableAnswerElement answer = computeAnswer(ImmutableSet.of("n1"), Pattern.compile(MATCH_ALL), configs); assertThat( answer.getRowsList(), contains(hasColumn(equalTo(COL_NODE), equalTo(new Node("n1")), Schema.NODE))); }
@Test public void testGetRowWithHost() { Row expected = Row.of( COL_FILENAME, "host", COL_PARSE_STATUS, "EMPTY", COL_NODES, ImmutableList.of(new Node("h1"))); Row row = FileParseStatusAnswerer.getRow("host", ParseStatus.EMPTY, ImmutableList.of("h1")); assertThat(row, equalTo(expected)); }
@VisibleForTesting static TableAnswerElement resolveNode(SpecifiersQuestion question, SpecifierContext context) { List<ColumnMetadata> columns = ImmutableList.of(new ColumnMetadata(COL_NODE, Schema.NODE, "Node", false, false)); TableAnswerElement table = new TableAnswerElement(new TableMetadata(columns)); Map<String, ColumnMetadata> columnMap = table.getMetadata().toColumnMap(); Set<String> nodes = question.getNodeSpecifier().resolve(context); for (String node : nodes) { table.addRow(Row.of(columnMap, COL_NODE, new Node(node))); } return table; } }
@Test public void serialization() { Topology topo = new Topology("testrig"); topo.getAggregates().add(new Aggregate("cloud")); topo.getInterfaces().add(new Interface("node", "name")); topo.getLinks().add(new Link("src", "dst")); topo.getNodes().add(new Node("node")); Map<String, String> properties = new HashMap<>(); properties.put("key", "value"); topo.setProperties(properties); JsonNode jsonNode = BatfishObjectMapper.mapper().valueToTree(topo); assertThat(jsonNode.get("id").asText(), equalTo(Topology.getId("testrig"))); assertThat(jsonNode.get("aggregates").get(0).get("name").asText(), equalTo("cloud")); assertThat(jsonNode.get("interfaces").get(0).get("nodeId").asText(), equalTo("node")); assertThat(jsonNode.get("interfaces").get(0).get("name").asText(), equalTo("name")); assertThat(jsonNode.get("links").get(0).get("srcId").asText(), equalTo("src")); assertThat(jsonNode.get("links").get(0).get("dstId").asText(), equalTo("dst")); assertThat(jsonNode.get("nodes").get(0).get("name").asText(), equalTo("node")); assertThat(jsonNode.get("properties").get("key").asText(), equalTo("value")); } }
@Test public void testBgpToRow() { Row row = getBgpEdgeRow("host1", Ip.parse("1.1.1.1"), 1L, "host2", Ip.parse("2.2.2.2"), 2L); assertThat( row, allOf( hasColumn(COL_NODE, equalTo(new Node("host1")), Schema.NODE), hasColumn(COL_IP, equalTo(Ip.parse("1.1.1.1")), Schema.IP), hasColumn(COL_AS_NUMBER, equalTo("1"), Schema.STRING), hasColumn(COL_REMOTE_NODE, equalTo(new Node("host2")), Schema.NODE), hasColumn(COL_REMOTE_IP, equalTo(Ip.parse("2.2.2.2")), Schema.IP), hasColumn(COL_REMOTE_AS_NUMBER, equalTo("2"), Schema.STRING))); }
@Test public void testColumnComparatorNode() { String col = "col1"; ColumnMetadata columnMetadata = new ColumnMetadata(col, Schema.NODE, "colDesc"); Comparator<Row> comparator = _manager.columnComparator(columnMetadata); Row r1 = Row.of(col, new Node("a")); Row r2 = Row.of(col, new Node("b")); assertThat(comparator.compare(r1, r2), lessThan(0)); }
@Test public void getNodes() throws IOException { _manager.initNetwork("container", null); // create a testrig and write a topology object for it createSnapshotWithMetadata("container", "testrig1"); Topology topology = new Topology("testrig1"); topology.setNodes(ImmutableSet.of(new Node("a1"), new Node("b1"))); CommonUtil.writeFile( _manager .getdirSnapshot("container", "testrig1") .resolve( Paths.get(BfConsts.RELPATH_OUTPUT, BfConsts.RELPATH_TESTRIG_POJO_TOPOLOGY_PATH)), BatfishObjectMapper.mapper().writeValueAsString(topology)); // should get the nodes of the topology when we ask for it assertThat(_manager.getNodes("container", "testrig1"), equalTo(ImmutableSet.of("a1", "b1"))); }
@Test public void testColumnComparatorTrace() { String col = "col1"; ColumnMetadata columnMetadata = new ColumnMetadata(col, Schema.TRACE, "colDesc"); Comparator<Row> comparator = _manager.columnComparator(columnMetadata); Row r1 = Row.of(col, new Trace(FlowDisposition.ACCEPTED, ImmutableList.of())); Row r2 = Row.of( col, new Trace( FlowDisposition.ACCEPTED, ImmutableList.of(new Hop(new Node("a"), ImmutableList.of())))); Row r3 = Row.of(col, new Trace(FlowDisposition.DELIVERED_TO_SUBNET, ImmutableList.of())); assertThat(comparator.compare(r1, r2), lessThan(0)); assertThat(comparator.compare(r1, r3), lessThan(0)); assertThat(comparator.compare(r2, r3), lessThan(0)); }
@VisibleForTesting static Row vxlanEdgeToRow(VxlanEdge edge) { RowBuilder row = Row.builder(); row.put(COL_VNI, edge.getVni()) .put(COL_NODE, new Node(edge.getTail().getHostname())) .put(COL_REMOTE_NODE, new Node(edge.getHead().getHostname())) .put(COL_VTEP_ADDRESS, edge.getTail().getSourceAddress()) .put(COL_REMOTE_VTEP_ADDRESS, edge.getHead().getSourceAddress()) .put(COL_VLAN, edge.getTail().getVlan()) .put(COL_REMOTE_VLAN, edge.getHead().getVlan()) .put(COL_UDP_PORT, edge.getUdpPort()) .put(COL_MULTICAST_GROUP, edge.getMulticastGroup()); return row.build(); }
@Test public void resolveNodeTest() { SpecifiersQuestion question = new SpecifiersQuestion(QueryType.NODE); question.setNodeSpecifierInput(_c1.getHostname()); // only c1 should be present assertThat( resolveNode(question, _context).getRows().getData(), equalTo(ImmutableMultiset.of(Row.of(COL_NODE, new Node(_c1.getHostname()))))); } }
@Test public void resolveFilterTest() { SpecifiersQuestion question = new SpecifiersQuestion(QueryType.FILTER); question.setFilterSpecifierInput(_filter1.getName()); // only filter1 should be present (with a node specifier all nodes are included) assertThat( resolveFilter(question, _context).getRows().getData(), equalTo( ImmutableMultiset.of( Row.of( COL_NODE, new Node(_c1.getHostname()), COL_FILTER_NAME, _filter1.getName())))); // nothing should match since the node specifier does not match anything question.setNodeSpecifierInput("foofoo"); assertThat( resolveFilter(question, _context).getRows().getData(), equalTo(ImmutableMultiset.of())); }