use of org.batfish.datamodel.answers.ParseVendorConfigurationAnswerElement 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.datamodel.answers.ParseVendorConfigurationAnswerElement in project batfish by batfish.
the class Batfish method initInfo.
@Override
public InitInfoAnswerElement initInfo(boolean summary, boolean verboseError) {
ParseVendorConfigurationAnswerElement parseAnswer = loadParseVendorConfigurationAnswerElement();
InitInfoAnswerElement answerElement = mergeParseAnswer(summary, verboseError, parseAnswer);
mergeConvertAnswer(summary, verboseError, answerElement);
_logger.info(answerElement.prettyPrint());
return answerElement;
}
use of org.batfish.datamodel.answers.ParseVendorConfigurationAnswerElement 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.datamodel.answers.ParseVendorConfigurationAnswerElement 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.datamodel.answers.ParseVendorConfigurationAnswerElement 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;
}
Aggregations