/** * Gets the set of nodes in this snapshot. Extracts the set based on the topology file that is * generated as part of the testrig initialization. Returns {@code null} if the network or the * snapshot does not exist. * * @throws IOException If the contents of the topology file cannot be mapped to the topology * object */ public @Nullable Set<String> getNodes(String network, String snapshot) throws IOException { Topology topology = getPojoTopology(network, snapshot); if (topology == null) { return null; } return topology.getNodes().stream().map(Node::getName).collect(Collectors.toSet()); }
public Aggregate getOrCreateAggregate(String name, AggregateType aggType) { String aggId = Aggregate.getId(name); for (Aggregate aggregate : _aggregates) { if (aggregate.getId().equals(aggId)) { return aggregate; } } Aggregate aggregate = new Aggregate(name, aggType); getAggregates().add(aggregate); return aggregate; }
public Topology(String testrigName) { this(getId(testrigName), testrigName); }
@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 constructor() throws IOException { String str = "{\"testrigName\" : \"testrig\"," + " \"nodes\" : [{\"name\": \"node\"}], " + " \"links\" : [{\"srcId\" : \"src\", \"dstId\" : \"dst\"}]," + " \"interfaces\" : [{\"nodeId\": \"node\", \"name\" : \"node\"}]," + " \"aggregates\" : [{\"name\": \"cloud\", \"contents\" : [\"id1\"]}]}"; Topology topo = BatfishObjectMapper.mapper().readValue(str, Topology.class); assertThat(topo.getId(), equalTo(Topology.getId("testrig"))); assertThat(topo.getTestrigName(), equalTo("testrig")); assertThat(topo.getAggregates().size(), equalTo(1)); assertThat( topo.getOrCreateAggregate("cloud", AggregateType.CLOUD).getContents().size(), equalTo(1)); assertThat(topo.getInterfaces().size(), equalTo(1)); assertThat(topo.getLinks().size(), equalTo(1)); assertThat(topo.getNodes().size(), equalTo(1)); }
Topology pojoTopology = new Topology(testrigName); pojoTopology.getNodes().add(pojoNode); pojoTopology.getInterfaces().add(pojoInterface); pojoTopology.getLinks().add(pojoLink); String subnetId = configuration.getVendorFamily().getAws().getSubnetId(); Aggregate subnetAggregate = pojoTopology.getOrCreateAggregate(subnetId, AggregateType.SUBNET); subnetAggregate.getContents().add(pojoNode.getId()); Aggregate vpcAggregate = pojoTopology.getOrCreateAggregate(vpcId, AggregateType.VNET); vpcAggregate.getContents().add(subnetAggregate.getId()); } else if (configuration.getVendorFamily().getAws().getVpcId() != null) { String vpcId = configuration.getVendorFamily().getAws().getVpcId(); Aggregate vpcAggregate = pojoTopology.getOrCreateAggregate(vpcId, AggregateType.VNET); vpcAggregate.getContents().add(pojoNode.getId()); pojoTopology.getOrCreateAggregate(region, AggregateType.REGION); regionAggregate.getContents().add(vpcAggregate.getId()); } else if (configuration.getVendorFamily().getAws().getRegion() != null) { String region = configuration.getVendorFamily().getAws().getRegion(); Aggregate regionAggregate = pojoTopology.getOrCreateAggregate(region, AggregateType.REGION); regionAggregate.getContents().add(pojoNode.getId()); Aggregate awsAggregate = pojoTopology.getOrCreateAggregate("aws", AggregateType.CLOUD); awsAggregate.getContents().add(regionAggregate.getId());
@Test public void testNodeSpecAutocomplete() throws IOException { String network = "network"; String snapshot = "snapshot"; _manager.initNetwork(network, null); // create a snapshot and write a topology object for it createSnapshotWithMetadata(network, snapshot); Topology topology = new Topology(snapshot); topology.setNodes(ImmutableSet.of(new Node("a1"), new Node("b1"))); CommonUtil.writeFile( _manager .getdirSnapshot(network, snapshot) .resolve( Paths.get(BfConsts.RELPATH_OUTPUT, BfConsts.RELPATH_TESTRIG_POJO_TOPOLOGY_PATH)), BatfishObjectMapper.mapper().writeValueAsString(topology)); assertThat( _manager.autoComplete(network, snapshot, Type.NODE_SPEC, "a", 5).stream() .map(AutocompleteSuggestion::getText) .collect(Collectors.toSet()), equalTo(ImmutableSet.of("a1", "a.*"))); }
/** Initialize topologies, commit {raw, raw pojo, pruned} layer-3 topologies to storage. */ @VisibleForTesting void initializeTopology(NetworkSnapshot networkSnapshot) { Map<String, Configuration> configurations = loadConfigurations(); Topology rawLayer3Topology = _topologyProvider.getRawLayer3Topology(networkSnapshot); serializeAsJson(_testrigSettings.getTopologyPath(), rawLayer3Topology, "raw layer-3 topology"); checkTopology(configurations, rawLayer3Topology); org.batfish.datamodel.pojo.Topology pojoTopology = org.batfish.datamodel.pojo.Topology.create( _settings.getSnapshotName(), configurations, rawLayer3Topology); serializeAsJson( _testrigSettings.getPojoTopologyPath(), pojoTopology, "raw layer-3 pojo topology"); Topology layer3Topology = _topologyProvider.getLayer3Topology(getNetworkSnapshot()); try { _storage.storeTopology( layer3Topology, networkSnapshot.getNetwork(), networkSnapshot.getSnapshot()); } catch (IOException e) { throw new BatfishException("Could not serialize layer-3 topology", e); } }
@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"))); }
public static void initSnapshotWithTopology(String network, String snapshot, Set<String> nodes) throws IOException { IdManager idManager = Main.getWorkMgr().getIdManager(); NetworkId networkId = idManager.getNetworkId(network); SnapshotId snapshotId = idManager.hasSnapshotId(snapshot, networkId) ? idManager.getSnapshotId(snapshot, networkId) : idManager.generateSnapshotId(); idManager.assignSnapshot(snapshot, networkId, snapshotId); SnapshotMetadataMgr.writeMetadata( new SnapshotMetadata(new Date().toInstant(), null), networkId, snapshotId); Topology pojoTopology = new Topology(snapshot); pojoTopology.setNodes(nodes.stream().map(Node::new).collect(Collectors.toSet())); Main.getWorkMgr().getStorage().storePojoTopology(pojoTopology, networkId, snapshotId); org.batfish.datamodel.Topology envTopology = new org.batfish.datamodel.Topology(ImmutableSortedSet.of()); Main.getWorkMgr().getStorage().storeTopology(envTopology, networkId, snapshotId); }
@JsonCreator public Topology( @JsonProperty(PROP_ID) String topologyId, @JsonProperty(PROP_TESTRIG_NAME) String testrigName) { super(firstNonNull(topologyId, getId(testrigName))); _testrigName = testrigName; _aggregates = new HashSet<>(); _interfaces = new HashSet<>(); _links = new HashSet<>(); _nodes = new HashSet<>(); if (testrigName == null) { throw new IllegalArgumentException("Cannot build Topology: testrigName is null"); } }