use of org.batfish.common.BatfishException in project batfish by batfish.
the class Batfish method deserializeEnvironmentBgpTables.
private SortedMap<String, BgpAdvertisementsByVrf> deserializeEnvironmentBgpTables(Path serializeEnvironmentBgpTablesPath) {
_logger.info("\n*** DESERIALIZING ENVIRONMENT BGP TABLES ***\n");
_logger.resetTimer();
Map<Path, String> namesByPath = new TreeMap<>();
try (DirectoryStream<Path> serializedBgpTables = Files.newDirectoryStream(serializeEnvironmentBgpTablesPath)) {
for (Path serializedBgpTable : serializedBgpTables) {
String name = serializedBgpTable.getFileName().toString();
namesByPath.put(serializedBgpTable, name);
}
} catch (IOException e) {
throw new BatfishException("Error reading serialized BGP tables directory", e);
}
SortedMap<String, BgpAdvertisementsByVrf> bgpTables = deserializeObjects(namesByPath, BgpAdvertisementsByVrf.class);
_logger.printElapsedTime();
return bgpTables;
}
use of org.batfish.common.BatfishException in project batfish by batfish.
the class Batfish method parseNodeRoles.
private NodeRoleSpecifier parseNodeRoles(Path nodeRolesPath) {
_logger.infof("Parsing: \"%s\"\n", nodeRolesPath.toAbsolutePath());
String roleFileText = CommonUtil.readFile(nodeRolesPath);
NodeRoleSpecifier specifier;
try {
specifier = BatfishObjectMapper.mapper().readValue(roleFileText, new TypeReference<NodeRoleSpecifier>() {
});
} catch (IOException e) {
throw new BatfishException("Failed to parse node roles", e);
}
return specifier;
}
use of org.batfish.common.BatfishException in project batfish by batfish.
the class Batfish method answer.
public Answer answer() {
Question question = null;
// return right away if we cannot parse the question successfully
try (ActiveSpan parseQuestionSpan = GlobalTracer.get().buildSpan("Parse question").startActive()) {
// avoid not used warning
assert parseQuestionSpan != null;
question = Question.parseQuestion(_settings.getQuestionPath());
} catch (Exception e) {
Answer answer = new Answer();
BatfishException exception = new BatfishException("Could not parse question", e);
answer.setStatus(AnswerStatus.FAILURE);
answer.addAnswerElement(exception.getBatfishStackTrace());
return answer;
}
if (_settings.getDifferential()) {
question.setDifferential(true);
}
boolean dp = question.getDataPlane();
boolean diff = question.getDifferential();
boolean diffActive = _settings.getDiffActive() && !diff;
_settings.setDiffActive(diffActive);
_settings.setDiffQuestion(diff);
try (ActiveSpan loadConfigurationSpan = GlobalTracer.get().buildSpan("Load configurations").startActive()) {
// avoid not used warning
assert loadConfigurationSpan != null;
// Ensures configurations are parsed and ready
loadConfigurations();
}
try (ActiveSpan initQuestionEnvSpan = GlobalTracer.get().buildSpan("Init question environment").startActive()) {
// avoid not used warning
assert initQuestionEnvSpan != null;
initQuestionEnvironments(question, diff, diffActive, dp);
}
AnswerElement answerElement = null;
BatfishException exception = null;
try (ActiveSpan getAnswerSpan = GlobalTracer.get().buildSpan("Get answer").startActive()) {
// avoid not used warning
assert getAnswerSpan != null;
if (question.getDifferential()) {
answerElement = Answerer.create(question, this).answerDiff();
} else {
answerElement = Answerer.create(question, this).answer();
}
} catch (Exception e) {
exception = new BatfishException("Failed to answer question", e);
}
Answer answer = new Answer();
answer.setQuestion(question);
if (exception == null) {
// success
answer.setStatus(AnswerStatus.SUCCESS);
answer.addAnswerElement(answerElement);
} else {
// failure
answer.setStatus(AnswerStatus.FAILURE);
answer.addAnswerElement(exception.getBatfishStackTrace());
}
return answer;
}
use of org.batfish.common.BatfishException 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.BatfishException in project batfish by batfish.
the class Batfish method writeJsonTopology.
private void writeJsonTopology() {
try {
Map<String, Configuration> configs = loadConfigurations();
SortedSet<Edge> textEdges = CommonUtil.synthesizeTopology(configs).getEdges();
JSONArray jEdges = new JSONArray();
for (Edge textEdge : textEdges) {
Configuration node1 = configs.get(textEdge.getNode1());
Configuration node2 = configs.get(textEdge.getNode2());
Interface interface1 = node1.getInterfaces().get(textEdge.getInt1());
Interface interface2 = node2.getInterfaces().get(textEdge.getInt2());
JSONObject jEdge = new JSONObject();
jEdge.put("interface1", interface1.toJSONObject());
jEdge.put("interface2", interface2.toJSONObject());
jEdges.put(jEdge);
}
JSONObject master = new JSONObject();
JSONObject topology = new JSONObject();
topology.put("edges", jEdges);
master.put("topology", topology);
String text = master.toString(3);
_logger.output(text);
} catch (JSONException e) {
throw new BatfishException("Failed to synthesize JSON topology", e);
}
}
Aggregations