use of org.batfish.common.BatfishException in project batfish by batfish.
the class BatfishStorage method storeCompressedConfigurations.
/**
* Stores the configurations into the compressed config path for the given testrig. Will replace
* any previously-stored compressed configurations.
*/
public void storeCompressedConfigurations(Map<String, Configuration> configurations, String testrig) {
Path testrigDir = getTestrigDir(testrig);
if (!testrigDir.toFile().exists() && !testrigDir.toFile().mkdirs()) {
throw new BatfishException(String.format("Unable to create testrig directory '%s'", testrigDir));
}
Path outputDir = testrigDir.resolve(BfConsts.RELPATH_COMPRESSED_CONFIG_DIR);
String batchName = String.format("Serializing %s compressed configuration structures for testrig %s", configurations.size(), testrig);
storeConfigurations(outputDir, batchName, configurations);
}
use of org.batfish.common.BatfishException in project batfish by batfish.
the class BatfishStorage method storeConfigurations.
/**
* Stores the configuration information into the given testrig. Will replace any previously-stored
* configurations.
*/
public void storeConfigurations(Map<String, Configuration> configurations, ConvertConfigurationAnswerElement convertAnswerElement, String testrig) {
Path testrigDir = getTestrigDir(testrig);
if (!testrigDir.toFile().exists() && !testrigDir.toFile().mkdirs()) {
throw new BatfishException(String.format("Unable to create testrig directory '%s'", testrigDir));
}
// Save the convert configuration answer element.
Path ccaePath = testrigDir.resolve(BfConsts.RELPATH_CONVERT_ANSWER_PATH);
CommonUtil.deleteIfExists(ccaePath);
serializeObject(convertAnswerElement, ccaePath);
Path outputDir = testrigDir.resolve(BfConsts.RELPATH_VENDOR_INDEPENDENT_CONFIG_DIR);
String batchName = String.format("Serializing %s vendor-independent configuration structures for testrig %s", configurations.size(), testrig);
storeConfigurations(outputDir, batchName, configurations);
}
use of org.batfish.common.BatfishException in project batfish by batfish.
the class Driver method runBatfish.
@SuppressWarnings("deprecation")
private static String runBatfish(final Settings settings) {
final BatfishLogger logger = settings.getLogger();
try {
final Batfish batfish = new Batfish(settings, CACHED_COMPRESSED_TESTRIGS, CACHED_TESTRIGS, CACHED_COMPRESSED_DATA_PLANES, CACHED_DATA_PLANES, CACHED_ENVIRONMENT_BGP_TABLES, CACHED_ENVIRONMENT_ROUTING_TABLES, CACHED_FORWARDING_ANALYSES);
@Nullable SpanContext runBatfishSpanContext = GlobalTracer.get().activeSpan() == null ? null : GlobalTracer.get().activeSpan().context();
Thread thread = new Thread() {
@Override
public void run() {
try (ActiveSpan runBatfishSpan = GlobalTracer.get().buildSpan("Run Batfish job in a new thread and get the answer").addReference(References.FOLLOWS_FROM, runBatfishSpanContext).startActive()) {
assert runBatfishSpan != null;
Answer answer = null;
try {
answer = batfish.run();
if (answer.getStatus() == null) {
answer.setStatus(AnswerStatus.SUCCESS);
}
} catch (CleanBatfishException e) {
String msg = "FATAL ERROR: " + e.getMessage();
logger.error(msg);
batfish.setTerminatingExceptionMessage(e.getClass().getName() + ": " + e.getMessage());
answer = Answer.failureAnswer(msg, null);
} catch (QuestionException e) {
String stackTrace = ExceptionUtils.getStackTrace(e);
logger.error(stackTrace);
batfish.setTerminatingExceptionMessage(e.getClass().getName() + ": " + e.getMessage());
answer = e.getAnswer();
answer.setStatus(AnswerStatus.FAILURE);
} catch (BatfishException e) {
String stackTrace = ExceptionUtils.getStackTrace(e);
logger.error(stackTrace);
batfish.setTerminatingExceptionMessage(e.getClass().getName() + ": " + e.getMessage());
answer = new Answer();
answer.setStatus(AnswerStatus.FAILURE);
answer.addAnswerElement(e.getBatfishStackTrace());
} catch (Throwable e) {
String stackTrace = ExceptionUtils.getStackTrace(e);
logger.error(stackTrace);
batfish.setTerminatingExceptionMessage(e.getClass().getName() + ": " + e.getMessage());
answer = new Answer();
answer.setStatus(AnswerStatus.FAILURE);
answer.addAnswerElement(new BatfishException("Batfish job failed", e).getBatfishStackTrace());
} finally {
try (ActiveSpan outputAnswerSpan = GlobalTracer.get().buildSpan("Outputting answer").startActive()) {
assert outputAnswerSpan != null;
if (settings.getAnswerJsonPath() != null) {
batfish.outputAnswerWithLog(answer);
}
}
}
}
}
};
thread.start();
thread.join(settings.getMaxRuntimeMs());
if (thread.isAlive()) {
// this is deprecated but we should be safe since we don't have
// locks and such
// AF: This doesn't do what you think it does, esp. not in Java 8.
// It needs to be replaced. TODO
thread.stop();
logger.error("Batfish worker took too long. Terminated.");
batfish.setTerminatingExceptionMessage("Batfish worker took too long. Terminated.");
}
return batfish.getTerminatingExceptionMessage();
} catch (Exception e) {
String stackTrace = ExceptionUtils.getStackTrace(e);
logger.error(stackTrace);
return stackTrace;
}
}
use of org.batfish.common.BatfishException in project batfish by batfish.
the class ParseEnvironmentRoutingTableJob method call.
@Override
public ParseEnvironmentRoutingTableResult call() {
long startTime = System.currentTimeMillis();
long elapsedTime;
String currentPath = _file.toAbsolutePath().toString();
BatfishCombinedParser<?, ?> combinedParser = null;
ParserRuleContext tree = null;
RoutingTableExtractor extractor = null;
_logger.infof("Processing: '%s'\n", currentPath);
// String relativePathStr =
// _settings.getActiveTestrigSettings().getEnvironmentSettings().getEnvPath()
// .relativize(_file).toString();
RoutingTableFormat format = RoutingTableFormatDetector.identifyRoutingTableFormat(_fileText);
switch(format) {
case EMPTY:
_warnings.redFlag("Empty file: '" + currentPath + "'\n");
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseEnvironmentRoutingTableResult(elapsedTime, _logger.getHistory(), _file, _warnings, ParseStatus.EMPTY);
case EOS:
EosRoutingTableCombinedParser eosRoutingTableParser = new EosRoutingTableCombinedParser(_fileText, _settings);
combinedParser = eosRoutingTableParser;
extractor = new EosRoutingTableExtractor(_hostname, _fileText, eosRoutingTableParser, _warnings, _batfish);
break;
case IOS:
IosRoutingTableCombinedParser iosRoutingTableParser = new IosRoutingTableCombinedParser(_fileText, _settings);
combinedParser = iosRoutingTableParser;
extractor = new IosRoutingTableExtractor(_hostname, _fileText, iosRoutingTableParser, _warnings, _batfish);
break;
case NXOS:
NxosRoutingTableCombinedParser nxosRoutingTableParser = new NxosRoutingTableCombinedParser(_fileText, _settings);
combinedParser = nxosRoutingTableParser;
extractor = new NxosRoutingTableExtractor(_hostname, _fileText, nxosRoutingTableParser, _warnings, _batfish);
break;
case UNKNOWN:
default:
String unknownError = "Unknown routing-table format for file: '" + currentPath + "'\n";
if (!_settings.ignoreUnknown()) {
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseEnvironmentRoutingTableResult(elapsedTime, _logger.getHistory(), _file, new BatfishException(unknownError));
} else {
_warnings.unimplemented(unknownError);
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseEnvironmentRoutingTableResult(elapsedTime, _logger.getHistory(), _file, _warnings, ParseStatus.UNKNOWN);
}
}
try {
_logger.info("\tParsing...");
tree = Batfish.parse(combinedParser, _logger, _settings);
if (_settings.getPrintParseTree()) {
_ptSentences = ParseTreePrettyPrinter.getParseTreeSentences(tree, combinedParser);
}
_logger.info("\tPost-processing...");
extractor.processParseTree(tree);
_logger.info("OK\n");
} catch (ParserBatfishException e) {
String error = "Error parsing configuration file: '" + currentPath + "'";
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseEnvironmentRoutingTableResult(elapsedTime, _logger.getHistory(), _file, new BatfishException(error, e));
} catch (Exception e) {
String error = "Error post-processing parse tree of configuration file: '" + currentPath + "'";
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseEnvironmentRoutingTableResult(elapsedTime, _logger.getHistory(), _file, new BatfishException(error, e));
} finally {
Batfish.logWarnings(_logger, _warnings);
}
RoutesByVrf routesByVrf = extractor.getRoutesByVrf();
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseEnvironmentRoutingTableResult(elapsedTime, _logger.getHistory(), _file, _hostname, routesByVrf, _warnings, _ptSentences);
}
use of org.batfish.common.BatfishException in project batfish by batfish.
the class ParseVendorConfigurationJob method call.
@SuppressWarnings("fallthrough")
@Override
public ParseVendorConfigurationResult call() throws Exception {
long startTime = System.currentTimeMillis();
long elapsedTime;
String currentPath = _file.toAbsolutePath().toString();
VendorConfiguration vc = null;
BatfishCombinedParser<?, ?> combinedParser = null;
ParserRuleContext tree = null;
ControlPlaneExtractor extractor = null;
ConfigurationFormat format = _format;
_logger.infof("Processing: '%s'\n", currentPath);
for (String s : _settings.ignoreFilesWithStrings()) {
if (_fileText.contains(s)) {
format = ConfigurationFormat.IGNORED;
break;
}
}
String relativePathStr = _settings.getActiveTestrigSettings().getBasePath().relativize(_file).toString();
if (format == ConfigurationFormat.UNKNOWN) {
format = VendorConfigurationFormatDetector.identifyConfigurationFormat(_fileText);
}
switch(format) {
case EMPTY:
_warnings.redFlag("Empty file: '" + currentPath + "'\n");
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseVendorConfigurationResult(elapsedTime, _logger.getHistory(), _file, _warnings, ParseStatus.EMPTY);
case IGNORED:
_warnings.pedantic("Ignored file: '" + currentPath + "'\n");
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseVendorConfigurationResult(elapsedTime, _logger.getHistory(), _file, _warnings, ParseStatus.IGNORED);
case ARISTA:
case CADANT:
case CISCO_ASA:
case CISCO_IOS:
case CISCO_IOS_XR:
case CISCO_NX:
case FORCE10:
case FOUNDRY:
String newFileText = _fileText;
String fileText;
_logger.info("\tPreprocessing...");
do {
fileText = newFileText;
try {
newFileText = preprocessBanner(fileText, format);
} catch (BatfishException e) {
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseVendorConfigurationResult(elapsedTime, _logger.getHistory(), _file, new BatfishException("Error preprocessing banner", e));
}
} while (!newFileText.equals(fileText));
_logger.info("OK\n");
CiscoCombinedParser ciscoParser = new CiscoCombinedParser(newFileText, _settings, format);
combinedParser = ciscoParser;
extractor = new CiscoControlPlaneExtractor(newFileText, ciscoParser, format, _warnings, _settings.getUnrecognizedAsRedFlag());
break;
case HOST:
vc = HostConfiguration.fromJson(_fileText, _warnings);
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseVendorConfigurationResult(elapsedTime, _logger.getHistory(), _file, vc, _warnings, _ptSentences);
case VYOS:
if (_settings.flattenOnTheFly()) {
String msg = "Flattening: '" + currentPath + "' on-the-fly; line-numbers reported for this file will be spurious\n";
_warnings.pedantic(msg);
// _logger
// .warn("Flattening: \""
// + currentPath
// +
// "\" on-the-fly; line-numbers reported for this file will be
// spurious\n");
_fileText = Batfish.flatten(_fileText, _logger, _settings, ConfigurationFormat.VYOS, VendorConfigurationFormatDetector.BATFISH_FLATTENED_VYOS_HEADER);
} else {
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseVendorConfigurationResult(elapsedTime, _logger.getHistory(), _file, new BatfishException("Vyos configurations must be flattened prior to this stage: '" + relativePathStr + "'"));
}
// fall through
case FLAT_VYOS:
FlatVyosCombinedParser flatVyosParser = new FlatVyosCombinedParser(_fileText, _settings);
combinedParser = flatVyosParser;
extractor = new FlatVyosControlPlaneExtractor(_fileText, flatVyosParser, _warnings);
break;
case JUNIPER:
if (_settings.flattenOnTheFly()) {
String msg = "Flattening: '" + currentPath + "' on-the-fly; line-numbers reported for this file will be spurious\n";
_warnings.pedantic(msg);
// spurious\n");
try {
_fileText = Batfish.flatten(_fileText, _logger, _settings, ConfigurationFormat.JUNIPER, VendorConfigurationFormatDetector.BATFISH_FLATTENED_JUNIPER_HEADER);
} catch (BatfishException e) {
String error = "Error flattening configuration file: '" + currentPath + "'";
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseVendorConfigurationResult(elapsedTime, _logger.getHistory(), _file, new BatfishException(error, e));
}
} else {
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseVendorConfigurationResult(elapsedTime, _logger.getHistory(), _file, new BatfishException("Juniper configurations must be flattened prior to this stage: '" + relativePathStr + "'"));
}
// fall through
case FLAT_JUNIPER:
FlatJuniperCombinedParser flatJuniperParser = new FlatJuniperCombinedParser(_fileText, _settings);
combinedParser = flatJuniperParser;
extractor = new FlatJuniperControlPlaneExtractor(_fileText, flatJuniperParser, _warnings, _settings.getUnrecognizedAsRedFlag());
break;
case IPTABLES:
IptablesCombinedParser iptablesParser = new IptablesCombinedParser(_fileText, _settings);
combinedParser = iptablesParser;
extractor = new IptablesControlPlaneExtractor(_fileText, iptablesParser, _warnings, relativePathStr);
break;
case MRV:
MrvCombinedParser mrvParser = new MrvCombinedParser(_fileText, _settings);
combinedParser = mrvParser;
extractor = new MrvControlPlaneExtractor(_fileText, mrvParser, _warnings);
break;
case ALCATEL_AOS:
case AWS:
case BLADENETWORK:
case F5:
case JUNIPER_SWITCH:
case METAMAKO:
case MRV_COMMANDS:
case MSS:
case VXWORKS:
String unsupportedError = "Unsupported configuration format: '" + format + "' for file: '" + currentPath + "'\n";
if (!_settings.ignoreUnsupported()) {
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseVendorConfigurationResult(elapsedTime, _logger.getHistory(), _file, new BatfishException(unsupportedError));
} else {
_warnings.unimplemented(unsupportedError);
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseVendorConfigurationResult(elapsedTime, _logger.getHistory(), _file, _warnings, ParseStatus.UNSUPPORTED);
}
case UNKNOWN:
default:
String unknownError = "Unknown configuration format for file: '" + currentPath + "'\n";
if (!_settings.ignoreUnknown()) {
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseVendorConfigurationResult(elapsedTime, _logger.getHistory(), _file, new BatfishException(unknownError));
} else {
_warnings.unimplemented(unknownError);
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseVendorConfigurationResult(elapsedTime, _logger.getHistory(), _file, _warnings, ParseStatus.UNKNOWN);
}
}
try {
_logger.info("\tParsing...");
tree = Batfish.parse(combinedParser, _logger, _settings);
if (_settings.getPrintParseTree()) {
_ptSentences = ParseTreePrettyPrinter.getParseTreeSentences(tree, combinedParser);
}
_logger.info("\tPost-processing...");
extractor.processParseTree(tree);
if (!combinedParser.getErrors().isEmpty()) {
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseVendorConfigurationResult(elapsedTime, _logger.getHistory(), _file, new BatfishException(String.format("Configuration file: '%s' contains unrecognized lines:\n%s", _file.getFileName().toString(), String.join("\n", combinedParser.getErrors()))));
}
_logger.info("OK\n");
} catch (ParserBatfishException e) {
String error = "Error parsing configuration file: '" + currentPath + "'";
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseVendorConfigurationResult(elapsedTime, _logger.getHistory(), _file, new BatfishException(error, e));
} catch (Exception e) {
String error = "Error post-processing parse tree of configuration file: '" + currentPath + "'";
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseVendorConfigurationResult(elapsedTime, _logger.getHistory(), _file, new BatfishException(error, e));
} finally {
Batfish.logWarnings(_logger, _warnings);
}
vc = extractor.getVendorConfiguration();
vc.setVendor(format);
vc.setFilename(_file.getFileName().toString());
// at this point we should have a VendorConfiguration vc
String hostname = vc.getHostname();
if (hostname == null) {
String error = "No hostname set in file: '" + relativePathStr.replace("\\", "/") + "'\n";
try {
_warnings.redFlag(error);
} catch (BatfishException e) {
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseVendorConfigurationResult(elapsedTime, _logger.getHistory(), _file, e);
}
String filename = _file.getFileName().toString();
String guessedHostname = filename.replaceAll("\\.(cfg|conf)$", "");
_logger.redflag("\tNo hostname set! Guessing hostname from filename: '" + filename + "' ==> '" + guessedHostname + "'\n");
vc.setHostname(guessedHostname);
}
elapsedTime = System.currentTimeMillis() - startTime;
return new ParseVendorConfigurationResult(elapsedTime, _logger.getHistory(), _file, vc, _warnings, _ptSentences);
}
Aggregations