use of org.batfish.datamodel.Topology in project batfish by batfish.
the class Batfish method computeCompressedDataPlane.
private CompressDataPlaneResult computeCompressedDataPlane(HeaderSpace headerSpace) {
// Since compression mutates the configurations, we must clone them before that happens.
// A simple way to do this is to create a deep clone of each entry using Java serialization.
_logger.info("Computing compressed dataplane");
Map<String, Configuration> clonedConfigs = loadConfigurations().entrySet().parallelStream().collect(toMap(Entry::getKey, entry -> SerializationUtils.clone(entry.getValue())));
Map<String, Configuration> configs = new BatfishCompressor(this, clonedConfigs).compress(headerSpace);
Topology topo = CommonUtil.synthesizeTopology(configs);
DataPlanePlugin dataPlanePlugin = getDataPlanePlugin();
ComputeDataPlaneResult result = dataPlanePlugin.computeDataPlane(false, configs, topo);
_storage.storeCompressedConfigurations(configs, _testrigSettings.getName());
return new CompressDataPlaneResult(configs, result._dataPlane, result._answerElement);
}
use of org.batfish.datamodel.Topology in project batfish by batfish.
the class Batfish method parseTopology.
public Topology parseTopology(Path topologyFilePath) {
_logger.info("*** PARSING TOPOLOGY ***\n");
_logger.resetTimer();
String topologyFileText = CommonUtil.readFile(topologyFilePath);
_logger.infof("Parsing: \"%s\" ...", topologyFilePath.toAbsolutePath());
Topology topology = null;
if (topologyFileText.equals("")) {
throw new BatfishException("ERROR: empty topology\n");
} else if (topologyFileText.startsWith("autostart")) {
BatfishCombinedParser<?, ?> parser = null;
TopologyExtractor extractor = null;
parser = new GNS3TopologyCombinedParser(topologyFileText, _settings);
extractor = new GNS3TopologyExtractor();
ParserRuleContext tree = parse(parser);
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk(extractor, tree);
topology = extractor.getTopology();
} else {
try {
topology = BatfishObjectMapper.mapper().readValue(topologyFileText, Topology.class);
} catch (IOException e) {
_logger.fatal("...ERROR\n");
throw new BatfishException("Topology format error", e);
}
}
_logger.printElapsedTime();
return topology;
}
use of org.batfish.datamodel.Topology in project batfish by batfish.
the class Batfish method serializeIndependentConfigs.
private Answer serializeIndependentConfigs(Path vendorConfigPath) {
Answer answer = new Answer();
ConvertConfigurationAnswerElement answerElement = new ConvertConfigurationAnswerElement();
answerElement.setVersion(Version.getVersion());
if (_settings.getVerboseParse()) {
answer.addAnswerElement(answerElement);
}
Map<String, Configuration> configurations = getConfigurations(vendorConfigPath, answerElement);
Topology testrigTopology = computeTestrigTopology(_testrigSettings.getTestRigPath(), configurations);
serializeAsJson(_testrigSettings.getTopologyPath(), testrigTopology, "testrig topology");
checkTopology(configurations, testrigTopology);
org.batfish.datamodel.pojo.Topology pojoTopology = org.batfish.datamodel.pojo.Topology.create(_testrigSettings.getName(), configurations, testrigTopology);
serializeAsJson(_testrigSettings.getPojoTopologyPath(), pojoTopology, "testrig pojo topology");
_storage.storeConfigurations(configurations, answerElement, _testrigSettings.getName());
applyEnvironment(configurations);
Topology envTopology = computeEnvironmentTopology(configurations);
serializeAsJson(_testrigSettings.getEnvironmentSettings().getSerializedTopologyPath(), envTopology, "environment topology");
NodeRoleSpecifier roleSpecifier = inferNodeRoles(configurations);
serializeAsJson(_testrigSettings.getInferredNodeRolesPath(), roleSpecifier, "inferred node roles");
return answer;
}
use of org.batfish.datamodel.Topology in project batfish by batfish.
the class InferRoles method computeRoleScore.
private double computeRoleScore(String regex) {
SortedMap<String, SortedSet<String>> nodeRolesMap = regexToRoleSpecifier(regex).createNodeRolesMap(new TreeSet<>(_nodes));
Topology topology = _batfish.getEnvironmentTopology();
// produce a role-level topology and the list of nodes in each edge's source role
// that have an edge to some node in the edge's target role
SortedMap<RoleEdge, SortedSet<String>> roleEdges = new TreeMap<>();
for (Edge e : topology.getEdges()) {
String n1 = e.getNode1();
String n2 = e.getNode2();
SortedSet<String> roles1 = nodeRolesMap.get(n1);
SortedSet<String> roles2 = nodeRolesMap.get(n2);
if (roles1 != null && roles2 != null && roles1.size() == 1 && roles2.size() == 1) {
String role1 = roles1.first();
String role2 = roles2.first();
// ignore self-edges
if (role1.equals(role2)) {
continue;
}
RoleEdge redge = new RoleEdge(role1, role2);
SortedSet<String> roleEdgeNodes = roleEdges.getOrDefault(redge, new TreeSet<>());
roleEdgeNodes.add(n1);
roleEdges.put(redge, roleEdgeNodes);
}
}
int numEdges = roleEdges.size();
if (numEdges == 0) {
return 0.0;
}
// compute the "support" of each edge in the role-level topology:
// the percentage of nodes playing the source role that have an edge
// to a node in the target role.
// the score of this regex is then the average support across all role edges
SortedMap<String, SortedSet<String>> roleNodesMap = regexToRoleSpecifier(regex).createRoleNodesMap(_configurations.keySet());
double supportSum = 0.0;
for (Map.Entry<RoleEdge, SortedSet<String>> roleEdgeCount : roleEdges.entrySet()) {
RoleEdge redge = roleEdgeCount.getKey();
int count = roleEdgeCount.getValue().size();
supportSum += (double) count / roleNodesMap.get(redge.getRole1()).size();
}
return supportSum / numEdges;
}
use of org.batfish.datamodel.Topology in project batfish by batfish.
the class BdpDataPlanePluginTest method testStaticInterfaceRoutesWithoutEdge.
@Test
public void testStaticInterfaceRoutesWithoutEdge() {
NetworkFactory nf = new NetworkFactory();
Configuration c = nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS).build();
Vrf vrf = nf.vrfBuilder().setOwner(c).setName(DEFAULT_VRF_NAME).build();
Interface i = nf.interfaceBuilder().setOwner(c).setVrf(vrf).setAddress(new InterfaceAddress("10.0.0.0/24")).setActive(true).build();
StaticRoute srBoth = StaticRoute.builder().setNetwork(Prefix.parse("10.0.1.0/24")).setNextHopInterface(i.getName()).setNextHopIp(new Ip("10.0.0.1")).build();
vrf.getStaticRoutes().add(srBoth);
StaticRoute srJustInterface = StaticRoute.builder().setNetwork(Prefix.parse("10.0.2.0/24")).setNextHopInterface(i.getName()).build();
vrf.getStaticRoutes().add(srJustInterface);
BdpEngine engine = new BdpEngine(new MockBdpSettings(), new BatfishLogger(BatfishLogger.LEVELSTR_DEBUG, false), (a, b) -> new AtomicInteger());
Topology topology = new Topology(Collections.emptySortedSet());
BdpDataPlane dp = engine.computeDataPlane(false, ImmutableMap.of(c.getName(), c), topology, Collections.emptySet(), new BdpAnswerElement());
// generating fibs should not crash
dp.getFibs();
}
Aggregations