use of org.batfish.common.Warnings in project batfish by batfish.
the class ConfigurationTest method testComputeRoutingPolicySources.
@Test
public void testComputeRoutingPolicySources() {
String bgpExportPolicyName = "bgpExportPolicy";
String bgpImportPolicyName = "bgpImportPolicy";
String bgpMissingExportPolicyName = "bgpMissingExportPolicy";
String bgpMissingImportPolicyName = null;
String generatedRouteAttributePolicyName = "generatedRouteAttributePolicy";
String generatedRouteGenerationPolicyName = "generatedRouteGenerationPolicy";
String ospfExportPolicyName = "ospfExportPolicy";
String ospfExportSubPolicyName = "ospfExportSubPolicy";
Prefix neighborPrefix = new Prefix(Ip.ZERO, Prefix.MAX_PREFIX_LENGTH);
Prefix generatedRoutePrefix = neighborPrefix;
Prefix neigborWithMissingPoliciesPrefix = new Prefix(Ip.MAX, Prefix.MAX_PREFIX_LENGTH);
Configuration c = new Configuration("test", ConfigurationFormat.CISCO_IOS);
Vrf vrf = c.getVrfs().computeIfAbsent(Configuration.DEFAULT_VRF_NAME, Vrf::new);
// BGP
BgpProcess bgpProcess = new BgpProcess();
vrf.setBgpProcess(bgpProcess);
BgpNeighbor neighbor = bgpProcess.getNeighbors().computeIfAbsent(neighborPrefix, BgpNeighbor::new);
neighbor.setExportPolicy(c.getRoutingPolicies().computeIfAbsent(bgpExportPolicyName, n -> new RoutingPolicy(n, c)).getName());
neighbor.setImportPolicy(c.getRoutingPolicies().computeIfAbsent(bgpImportPolicyName, n -> new RoutingPolicy(n, c)).getName());
BgpNeighbor neighborWithMissingPolicies = bgpProcess.getNeighbors().computeIfAbsent(neigborWithMissingPoliciesPrefix, BgpNeighbor::new);
neighborWithMissingPolicies.setExportPolicy(bgpMissingExportPolicyName);
neighborWithMissingPolicies.setImportPolicy(bgpMissingImportPolicyName);
// Generated route
GeneratedRoute gr = new GeneratedRoute.Builder().setNetwork(generatedRoutePrefix).setAttributePolicy(c.getRoutingPolicies().computeIfAbsent(generatedRouteAttributePolicyName, n -> new RoutingPolicy(n, c)).getName()).setGenerationPolicy(c.getRoutingPolicies().computeIfAbsent(generatedRouteGenerationPolicyName, n -> new RoutingPolicy(n, c)).getName()).build();
vrf.getGeneratedRoutes().add(gr);
// OSPF
OspfProcess ospfProcess = new OspfProcess();
vrf.setOspfProcess(ospfProcess);
RoutingPolicy ospfExportPolicy = c.getRoutingPolicies().computeIfAbsent(ospfExportPolicyName, n -> new RoutingPolicy(n, c));
ospfProcess.setExportPolicy(ospfExportPolicyName);
ospfExportPolicy.getStatements().add(new CallStatement(c.getRoutingPolicies().computeIfAbsent(ospfExportSubPolicyName, n -> new RoutingPolicy(n, c)).getName()));
// Compute policy sources
Warnings w = new Warnings();
c.computeRoutingPolicySources(w);
// BGP tests
assertThat(neighbor.getExportPolicySources(), equalTo(Collections.singleton(bgpExportPolicyName)));
assertThat(neighbor.getImportPolicySources(), equalTo(Collections.singleton(bgpImportPolicyName)));
assertThat(neighborWithMissingPolicies.getExportPolicySources(), equalTo(Collections.emptySet()));
assertThat(neighborWithMissingPolicies.getImportPolicySources(), equalTo(Collections.emptySet()));
// Generated route tests
assertThat(gr.getAttributePolicySources(), equalTo(Collections.singleton(generatedRouteAttributePolicyName)));
assertThat(gr.getGenerationPolicySources(), equalTo(Collections.singleton(generatedRouteGenerationPolicyName)));
// OSPF tests
assertThat(ospfProcess.getExportPolicySources(), containsInAnyOrder(ospfExportPolicyName, ospfExportSubPolicyName));
}
use of org.batfish.common.Warnings in project batfish by batfish.
the class Batfish method parseVendorConfigurations.
private SortedMap<String, VendorConfiguration> parseVendorConfigurations(Map<Path, String> configurationData, ParseVendorConfigurationAnswerElement answerElement, ConfigurationFormat configurationFormat) {
_logger.info("\n*** PARSING VENDOR CONFIGURATION FILES ***\n");
_logger.resetTimer();
SortedMap<String, VendorConfiguration> vendorConfigurations = new TreeMap<>();
List<ParseVendorConfigurationJob> jobs = new ArrayList<>();
for (Entry<Path, String> vendorFile : configurationData.entrySet()) {
Path currentFile = vendorFile.getKey();
String fileText = vendorFile.getValue();
Warnings warnings = buildWarnings(_settings);
ParseVendorConfigurationJob job = new ParseVendorConfigurationJob(_settings, fileText, currentFile, warnings, configurationFormat);
jobs.add(job);
}
BatfishJobExecutor.runJobsInExecutor(_settings, _logger, jobs, vendorConfigurations, answerElement, _settings.getHaltOnParseError(), "Parse configurations");
_logger.printElapsedTime();
return vendorConfigurations;
}
use of org.batfish.common.Warnings in project batfish by batfish.
the class Batfish method parseEnvironmentRoutingTables.
private SortedMap<String, RoutesByVrf> parseEnvironmentRoutingTables(SortedMap<Path, String> inputData, ParseEnvironmentRoutingTablesAnswerElement answerElement) {
_logger.info("\n*** PARSING ENVIRONMENT ROUTING TABLES ***\n");
_logger.resetTimer();
SortedMap<String, RoutesByVrf> routingTables = new TreeMap<>();
List<ParseEnvironmentRoutingTableJob> jobs = new ArrayList<>();
SortedMap<String, Configuration> configurations = loadConfigurations();
for (Entry<Path, String> routingFile : inputData.entrySet()) {
Path currentFile = routingFile.getKey();
String fileText = routingFile.getValue();
String hostname = currentFile.getFileName().toString();
if (!configurations.containsKey(hostname)) {
continue;
}
Warnings warnings = buildWarnings(_settings);
ParseEnvironmentRoutingTableJob job = new ParseEnvironmentRoutingTableJob(_settings, fileText, currentFile, warnings, this);
jobs.add(job);
}
BatfishJobExecutor.runJobsInExecutor(_settings, _logger, jobs, routingTables, answerElement, _settings.getHaltOnParseError(), "Parse environment routing tables");
_logger.printElapsedTime();
return routingTables;
}
use of org.batfish.common.Warnings in project batfish by batfish.
the class Batfish method serializeHostConfigs.
private SortedMap<String, VendorConfiguration> serializeHostConfigs(Path testRigPath, Path outputPath, ParseVendorConfigurationAnswerElement answerElement) {
SortedMap<Path, String> configurationData = readConfigurationFiles(testRigPath, BfConsts.RELPATH_HOST_CONFIGS_DIR);
// read the host files
SortedMap<String, VendorConfiguration> allHostConfigurations;
try (ActiveSpan parseHostConfigsSpan = GlobalTracer.get().buildSpan("Parse host configs").startActive()) {
// avoid unused warning
assert parseHostConfigsSpan != null;
allHostConfigurations = parseVendorConfigurations(configurationData, answerElement, ConfigurationFormat.HOST);
}
if (allHostConfigurations == null) {
throw new BatfishException("Exiting due to parser errors");
}
_logger.infof("Testrig:%s in container:%s has total number of host configs:%d", getTestrigName(), getContainerName(), allHostConfigurations.size());
// split into hostConfigurations and overlayConfigurations
SortedMap<String, VendorConfiguration> overlayConfigurations = allHostConfigurations.entrySet().stream().filter(e -> ((HostConfiguration) e.getValue()).getOverlay()).collect(toMap(Entry::getKey, Entry::getValue, (v1, v2) -> v1, TreeMap::new));
SortedMap<String, VendorConfiguration> nonOverlayHostConfigurations = allHostConfigurations.entrySet().stream().filter(e -> !((HostConfiguration) e.getValue()).getOverlay()).collect(toMap(Entry::getKey, Entry::getValue, (v1, v2) -> v1, TreeMap::new));
// read and associate iptables files for specified hosts
SortedMap<Path, String> iptablesData = new TreeMap<>();
readIptableFiles(testRigPath, allHostConfigurations, iptablesData, answerElement);
SortedMap<String, VendorConfiguration> iptablesConfigurations = parseVendorConfigurations(iptablesData, answerElement, ConfigurationFormat.IPTABLES);
for (VendorConfiguration vc : allHostConfigurations.values()) {
HostConfiguration hostConfig = (HostConfiguration) vc;
if (hostConfig.getIptablesFile() != null) {
Path path = Paths.get(testRigPath.toString(), hostConfig.getIptablesFile());
String relativePathStr = _testrigSettings.getBasePath().relativize(path).toString();
if (iptablesConfigurations.containsKey(relativePathStr)) {
hostConfig.setIptablesVendorConfig((IptablesVendorConfiguration) iptablesConfigurations.get(relativePathStr));
}
}
}
// now, serialize
_logger.info("\n*** SERIALIZING VENDOR CONFIGURATION STRUCTURES ***\n");
_logger.resetTimer();
CommonUtil.createDirectories(outputPath);
Map<Path, VendorConfiguration> output = new TreeMap<>();
nonOverlayHostConfigurations.forEach((name, vc) -> {
Path currentOutputPath = outputPath.resolve(name);
output.put(currentOutputPath, vc);
});
serializeObjects(output);
// serialize warnings
serializeObject(answerElement, _testrigSettings.getParseAnswerPath());
_logger.printElapsedTime();
return overlayConfigurations;
}
use of org.batfish.common.Warnings in project batfish by batfish.
the class Batfish method flatten.
private void flatten(Path inputPath, Path outputPath) {
Map<Path, String> configurationData = readConfigurationFiles(inputPath, BfConsts.RELPATH_CONFIGURATIONS_DIR);
Map<Path, String> outputConfigurationData = new TreeMap<>();
Path outputConfigDir = outputPath.resolve(BfConsts.RELPATH_CONFIGURATIONS_DIR);
CommonUtil.createDirectories(outputConfigDir);
_logger.info("\n*** FLATTENING TEST RIG ***\n");
_logger.resetTimer();
List<FlattenVendorConfigurationJob> jobs = new ArrayList<>();
for (Entry<Path, String> configFile : configurationData.entrySet()) {
Path inputFile = configFile.getKey();
String fileText = configFile.getValue();
Warnings warnings = buildWarnings(_settings);
String name = inputFile.getFileName().toString();
Path outputFile = outputConfigDir.resolve(name);
FlattenVendorConfigurationJob job = new FlattenVendorConfigurationJob(_settings, fileText, inputFile, outputFile, warnings);
jobs.add(job);
}
BatfishJobExecutor.runJobsInExecutor(_settings, _logger, jobs, outputConfigurationData, new FlattenVendorConfigurationAnswerElement(), _settings.getFlatten() || _settings.getHaltOnParseError(), "Flatten configurations");
_logger.printElapsedTime();
for (Entry<Path, String> e : outputConfigurationData.entrySet()) {
Path outputFile = e.getKey();
String flatConfigText = e.getValue();
String outputFileAsString = outputFile.toString();
_logger.debugf("Writing config to \"%s\"...", outputFileAsString);
CommonUtil.writeFile(outputFile, flatConfigText);
_logger.debug("OK\n");
}
Path inputTopologyPath = inputPath.resolve(TOPOLOGY_FILENAME);
Path outputTopologyPath = outputPath.resolve(TOPOLOGY_FILENAME);
if (Files.isRegularFile(inputTopologyPath)) {
String topologyFileText = CommonUtil.readFile(inputTopologyPath);
CommonUtil.writeFile(outputTopologyPath, topologyFileText);
}
}
Aggregations