use of org.batfish.vendor.VendorConfiguration in project batfish by batfish.
the class ConvertConfigurationJob method call.
@Override
public ConvertConfigurationResult call() {
long startTime = System.currentTimeMillis();
long elapsedTime;
_logger.infof("Processing: \"%s\"", _name);
Map<String, Configuration> configurations = new HashMap<>();
Map<String, Warnings> warningsByHost = new HashMap<>();
ConvertConfigurationAnswerElement answerElement = new ConvertConfigurationAnswerElement();
try {
// We have only two options: AWS VPCs or router configs
if (VendorConfiguration.class.isInstance(_configObject)) {
Warnings warnings = Batfish.buildWarnings(_settings);
VendorConfiguration vendorConfiguration = ((VendorConfiguration) _configObject);
vendorConfiguration.setWarnings(warnings);
vendorConfiguration.setAnswerElement(answerElement);
Configuration configuration = vendorConfiguration.toVendorIndependentConfiguration();
if (configuration.getDefaultCrossZoneAction() == null) {
throw new BatfishException("Implementation error: missing default cross-zone action for host: '" + configuration.getHostname() + "'");
}
if (configuration.getDefaultInboundAction() == null) {
throw new BatfishException("Implementation error: missing default inbound action for host: '" + configuration.getHostname() + "'");
}
// get iptables if applicable
IptablesVendorConfiguration iptablesConfig = null;
VendorConfiguration ov = vendorConfiguration.getOverlayConfiguration();
if (ov != null) {
// apply overlay
HostConfiguration oh = (HostConfiguration) ov;
iptablesConfig = oh.getIptablesVendorConfig();
} else if (vendorConfiguration instanceof HostConfiguration) {
// TODO: To enable below, we need to reconcile overlay and non-overlay iptables semantics.
// HostConfiguration oh = (HostConfiguration)vendorConfiguration;
// iptablesConfig = oh.getIptablesVendorConfig();
}
if (iptablesConfig != null) {
iptablesConfig.addAsIpAccessLists(configuration, vendorConfiguration, warnings);
iptablesConfig.applyAsOverlay(configuration, warnings);
}
configurations.put(_name, configuration);
warningsByHost.put(_name, warnings);
} else {
configurations = ((AwsConfiguration) _configObject).toConfigurations(_settings, warningsByHost);
}
_logger.info(" ...OK\n");
} catch (Exception e) {
String error = "Conversion error for node with hostname '" + _name + "'";
elapsedTime = System.currentTimeMillis() - startTime;
return new ConvertConfigurationResult(elapsedTime, _logger.getHistory(), _name, new BatfishException(error, e));
} finally {
warningsByHost.forEach((hostname, warnings) -> Batfish.logWarnings(_logger, warnings));
;
}
elapsedTime = System.currentTimeMillis() - startTime;
return new ConvertConfigurationResult(elapsedTime, _logger.getHistory(), warningsByHost, _name, configurations, answerElement);
}
use of org.batfish.vendor.VendorConfiguration in project batfish by batfish.
the class BatfishTest method testReadMissingIptableFile.
@Test
public void testReadMissingIptableFile() throws IOException {
HostConfiguration host1 = new HostConfiguration();
host1.setHostname("host1");
host1.setIptablesFile(Paths.get("iptables").resolve("host1.iptables").toString());
SortedMap<String, VendorConfiguration> hostConfigurations = new TreeMap<>();
hostConfigurations.put("host1", host1);
SortedMap<Path, String> iptablesData = new TreeMap<>();
Path testRigPath = _folder.newFolder("testrig").toPath();
ParseVendorConfigurationAnswerElement answerElement = new ParseVendorConfigurationAnswerElement();
answerElement.getParseStatus().put("host1", ParseStatus.PASSED);
Batfish batfish = BatfishTestUtils.getBatfish(new TreeMap<>(), _folder);
String failureMessage = "Iptables file iptables" + File.separator + "host1.iptables for host host1 is not contained within the testrig";
batfish.readIptableFiles(testRigPath, hostConfigurations, iptablesData, answerElement);
assertThat(answerElement.getParseStatus().get("host1"), equalTo(ParseStatus.FAILED));
assertThat(answerElement.getErrors().get("host1").prettyPrint(), containsString(failureMessage));
// When host file failed, verify that error message contains both failure messages
answerElement.getErrors().clear();
answerElement.getErrors().put("host1", new BatfishException("Failed to parse host file: host1").getBatfishStackTrace());
batfish.readIptableFiles(testRigPath, hostConfigurations, iptablesData, answerElement);
assertThat(answerElement.getErrors().get("host1").prettyPrint(), containsString(failureMessage));
assertThat(answerElement.getErrors().get("host1").prettyPrint(), containsString("Failed to parse host file: host1"));
// When the haltonparseerror flag is set to true
batfish.getSettings().setHaltOnParseError(true);
answerElement.getErrors().clear();
String parseErrorMessage = "Fatal exception due to at least one Iptables file is not contained" + " within the testrig";
_thrown.expect(BatfishException.class);
_thrown.expectMessage(parseErrorMessage);
batfish.readIptableFiles(testRigPath, hostConfigurations, iptablesData, answerElement);
}
use of org.batfish.vendor.VendorConfiguration in project batfish by batfish.
the class BatfishTest method testReadValidIptableFile.
@Test
public void testReadValidIptableFile() throws IOException {
HostConfiguration host1 = new HostConfiguration();
host1.setHostname("host1");
Path iptablePath = Paths.get("iptables").resolve("host1.iptables");
host1.setIptablesFile(iptablePath.toString());
SortedMap<String, VendorConfiguration> hostConfigurations = new TreeMap<>();
hostConfigurations.put("host1", host1);
SortedMap<Path, String> iptablesData = new TreeMap<>();
Path testRigPath = _folder.newFolder("testrig").toPath();
File iptableFile = Paths.get(testRigPath.toString(), iptablePath.toString()).toFile();
iptableFile.getParentFile().mkdir();
assertThat(iptableFile.createNewFile(), is(true));
ParseVendorConfigurationAnswerElement answerElement = new ParseVendorConfigurationAnswerElement();
answerElement.getParseStatus().put("host1", ParseStatus.PASSED);
Batfish batfish = BatfishTestUtils.getBatfish(new TreeMap<>(), _folder);
batfish.readIptableFiles(testRigPath, hostConfigurations, iptablesData, answerElement);
assertThat(answerElement.getParseStatus().get("host1"), equalTo(ParseStatus.PASSED));
assertThat(answerElement.getErrors().size(), is(0));
}
use of org.batfish.vendor.VendorConfiguration in project batfish by batfish.
the class Batfish method serializeVendorConfigs.
Answer serializeVendorConfigs(Path testRigPath, Path outputPath) {
Answer answer = new Answer();
boolean configsFound = false;
// look for network configs
Path networkConfigsPath = testRigPath.resolve(BfConsts.RELPATH_CONFIGURATIONS_DIR);
ParseVendorConfigurationAnswerElement answerElement = new ParseVendorConfigurationAnswerElement();
answerElement.setVersion(Version.getVersion());
if (_settings.getVerboseParse()) {
answer.addAnswerElement(answerElement);
}
// look for host configs and overlay configs
SortedMap<String, VendorConfiguration> overlayHostConfigurations = new TreeMap<>();
Path hostConfigsPath = testRigPath.resolve(BfConsts.RELPATH_HOST_CONFIGS_DIR);
if (Files.exists(hostConfigsPath)) {
overlayHostConfigurations = serializeHostConfigs(testRigPath, outputPath, answerElement);
configsFound = true;
}
if (Files.exists(networkConfigsPath)) {
serializeNetworkConfigs(testRigPath, outputPath, answerElement, overlayHostConfigurations);
configsFound = true;
}
// look for AWS VPC configs
Path awsVpcConfigsPath = testRigPath.resolve(BfConsts.RELPATH_AWS_CONFIGS_DIR);
if (Files.exists(awsVpcConfigsPath)) {
answer.append(serializeAwsConfigs(testRigPath, outputPath));
configsFound = true;
}
if (!configsFound) {
throw new BatfishException("No valid configurations found");
}
// serialize warnings
serializeObject(answerElement, _testrigSettings.getParseAnswerPath());
return answer;
}
use of org.batfish.vendor.VendorConfiguration in project batfish by batfish.
the class Batfish method histogram.
private void histogram(Path testRigPath) {
Map<Path, String> configurationData = readConfigurationFiles(testRigPath, BfConsts.RELPATH_CONFIGURATIONS_DIR);
// todo: either remove histogram function or do something userful with
// answer
Map<String, VendorConfiguration> vendorConfigurations = parseVendorConfigurations(configurationData, new ParseVendorConfigurationAnswerElement(), ConfigurationFormat.UNKNOWN);
_logger.info("Building feature histogram...");
MultiSet<String> histogram = new TreeMultiSet<>();
for (VendorConfiguration vc : vendorConfigurations.values()) {
Set<String> unimplementedFeatures = vc.getUnimplementedFeatures();
histogram.add(unimplementedFeatures);
}
_logger.info("OK\n");
for (String feature : histogram.elements()) {
int count = histogram.count(feature);
_logger.outputf("%s: %s\n", feature, count);
}
}
Aggregations