Search in sources :

Example 1 with ConfigurationFormat

use of org.batfish.datamodel.ConfigurationFormat in project batfish by batfish.

the class VendorConfigurationFormatDetector method identifyConfigurationFormat.

private ConfigurationFormat identifyConfigurationFormat() {
    ConfigurationFormat format;
    format = checkEmpty();
    if (format != null) {
        return format;
    }
    format = checkRancid();
    if (format != null) {
        return format;
    }
    // Heuristics are somewhat brittle. This function adds information about which configuration
    // formats we know this file does not match.
    configureHeuristicBlacklist();
    format = checkCadant();
    if (format != null) {
        return format;
    }
    format = checkF5();
    if (format != null) {
        return format;
    }
    format = checkCiscoXr();
    if (format != null) {
        return format;
    }
    format = checkFlatVyos();
    if (format != null) {
        return format;
    }
    format = checkIpTables();
    if (format != null) {
        return format;
    }
    format = checkMetamako();
    if (format != null) {
        return format;
    }
    format = checkMrv();
    if (format != null) {
        return format;
    }
    format = checkMrvCommands();
    if (format != null) {
        return format;
    }
    format = checkVyos();
    if (format != null) {
        return format;
    }
    format = checkArista();
    if (format != null) {
        return format;
    }
    format = checkBlade();
    if (format != null) {
        return format;
    }
    format = checkVxWorks();
    if (format != null) {
        return format;
    }
    format = checkJuniper();
    if (format != null) {
        return format;
    }
    format = checkAlcatelAos();
    if (format != null) {
        return format;
    }
    format = checkMss();
    if (format != null) {
        return format;
    }
    format = checkCisco();
    if (format != null) {
        return format;
    }
    return ConfigurationFormat.UNKNOWN;
}
Also used : ConfigurationFormat(org.batfish.datamodel.ConfigurationFormat)

Example 2 with ConfigurationFormat

use of org.batfish.datamodel.ConfigurationFormat 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);
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) BatfishException(org.batfish.common.BatfishException) ParserBatfishException(org.batfish.main.ParserBatfishException) FlatVyosControlPlaneExtractor(org.batfish.grammar.flatvyos.FlatVyosControlPlaneExtractor) CiscoControlPlaneExtractor(org.batfish.grammar.cisco.CiscoControlPlaneExtractor) FlatVyosCombinedParser(org.batfish.grammar.flatvyos.FlatVyosCombinedParser) IptablesCombinedParser(org.batfish.grammar.iptables.IptablesCombinedParser) CiscoCombinedParser(org.batfish.grammar.cisco.CiscoCombinedParser) MrvCombinedParser(org.batfish.grammar.mrv.MrvCombinedParser) BatfishException(org.batfish.common.BatfishException) ParserBatfishException(org.batfish.main.ParserBatfishException) ConfigurationFormat(org.batfish.datamodel.ConfigurationFormat) IptablesControlPlaneExtractor(org.batfish.grammar.iptables.IptablesControlPlaneExtractor) ParserBatfishException(org.batfish.main.ParserBatfishException) VendorConfiguration(org.batfish.vendor.VendorConfiguration) FlatJuniperCombinedParser(org.batfish.grammar.flatjuniper.FlatJuniperCombinedParser) FlatVyosControlPlaneExtractor(org.batfish.grammar.flatvyos.FlatVyosControlPlaneExtractor) CiscoControlPlaneExtractor(org.batfish.grammar.cisco.CiscoControlPlaneExtractor) ControlPlaneExtractor(org.batfish.grammar.ControlPlaneExtractor) MrvControlPlaneExtractor(org.batfish.grammar.mrv.MrvControlPlaneExtractor) IptablesControlPlaneExtractor(org.batfish.grammar.iptables.IptablesControlPlaneExtractor) FlatJuniperControlPlaneExtractor(org.batfish.grammar.flatjuniper.FlatJuniperControlPlaneExtractor) MrvControlPlaneExtractor(org.batfish.grammar.mrv.MrvControlPlaneExtractor) FlatJuniperControlPlaneExtractor(org.batfish.grammar.flatjuniper.FlatJuniperControlPlaneExtractor)

Example 3 with ConfigurationFormat

use of org.batfish.datamodel.ConfigurationFormat in project batfish by batfish.

the class FlattenVendorConfigurationJob method call.

@Override
public FlattenVendorConfigurationResult call() {
    long startTime = System.currentTimeMillis();
    long elapsedTime;
    String inputFileAsString = _inputFile.toAbsolutePath().toString();
    ConfigurationFormat format = VendorConfigurationFormatDetector.identifyConfigurationFormat(_fileText);
    if (format == ConfigurationFormat.JUNIPER || format == ConfigurationFormat.VYOS) {
        String header = null;
        if (format == ConfigurationFormat.JUNIPER) {
            header = VendorConfigurationFormatDetector.BATFISH_FLATTENED_JUNIPER_HEADER;
        }
        if (format == ConfigurationFormat.VYOS) {
            header = VendorConfigurationFormatDetector.BATFISH_FLATTENED_VYOS_HEADER;
        }
        _logger.debugf("Flattening config: \"%s\"...", _inputFile);
        String flatConfigText;
        try {
            flatConfigText = Batfish.flatten(_fileText, _logger, _settings, format, header);
        } catch (ParserBatfishException e) {
            String error = "Error parsing configuration file: \"" + inputFileAsString + "\"";
            elapsedTime = System.currentTimeMillis() - startTime;
            return new FlattenVendorConfigurationResult(elapsedTime, _logger.getHistory(), _outputFile, new BatfishException(error, e));
        } catch (Exception e) {
            String error = "Error post-processing parse tree of configuration file: \"" + inputFileAsString + "\"";
            elapsedTime = System.currentTimeMillis() - startTime;
            return new FlattenVendorConfigurationResult(elapsedTime, _logger.getHistory(), _outputFile, new BatfishException(error, e));
        } finally {
            Batfish.logWarnings(_logger, _warnings);
        }
        elapsedTime = System.currentTimeMillis() - startTime;
        return new FlattenVendorConfigurationResult(elapsedTime, _logger.getHistory(), _outputFile, flatConfigText);
    } else if (!_settings.ignoreUnsupported() && format == ConfigurationFormat.UNKNOWN) {
        elapsedTime = System.currentTimeMillis() - startTime;
        return new FlattenVendorConfigurationResult(elapsedTime, _logger.getHistory(), _outputFile, new BatfishException("Unknown configuration format for: \"" + _inputFile + "\""));
    } else {
        _logger.debugf("Skipping: \"%s\"\n", _inputFile);
        String flatConfigText = _fileText;
        elapsedTime = System.currentTimeMillis() - startTime;
        return new FlattenVendorConfigurationResult(elapsedTime, _logger.getHistory(), _outputFile, flatConfigText);
    }
}
Also used : ParserBatfishException(org.batfish.main.ParserBatfishException) BatfishException(org.batfish.common.BatfishException) ParserBatfishException(org.batfish.main.ParserBatfishException) BatfishException(org.batfish.common.BatfishException) ParserBatfishException(org.batfish.main.ParserBatfishException) ConfigurationFormat(org.batfish.datamodel.ConfigurationFormat)

Example 4 with ConfigurationFormat

use of org.batfish.datamodel.ConfigurationFormat in project batfish by batfish.

the class Graph method isLoopback.

/*
   * Check if a graph edge is a loopback address
   */
public boolean isLoopback(GraphEdge ge) {
    Configuration conf = _configurations.get(ge.getRouter());
    ConfigurationFormat format = conf.getConfigurationFormat();
    return ge.getStart().isLoopback(format);
}
Also used : Configuration(org.batfish.datamodel.Configuration) ConfigurationFormat(org.batfish.datamodel.ConfigurationFormat)

Aggregations

ConfigurationFormat (org.batfish.datamodel.ConfigurationFormat)4 BatfishException (org.batfish.common.BatfishException)2 ParserBatfishException (org.batfish.main.ParserBatfishException)2 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)1 Configuration (org.batfish.datamodel.Configuration)1 ControlPlaneExtractor (org.batfish.grammar.ControlPlaneExtractor)1 CiscoCombinedParser (org.batfish.grammar.cisco.CiscoCombinedParser)1 CiscoControlPlaneExtractor (org.batfish.grammar.cisco.CiscoControlPlaneExtractor)1 FlatJuniperCombinedParser (org.batfish.grammar.flatjuniper.FlatJuniperCombinedParser)1 FlatJuniperControlPlaneExtractor (org.batfish.grammar.flatjuniper.FlatJuniperControlPlaneExtractor)1 FlatVyosCombinedParser (org.batfish.grammar.flatvyos.FlatVyosCombinedParser)1 FlatVyosControlPlaneExtractor (org.batfish.grammar.flatvyos.FlatVyosControlPlaneExtractor)1 IptablesCombinedParser (org.batfish.grammar.iptables.IptablesCombinedParser)1 IptablesControlPlaneExtractor (org.batfish.grammar.iptables.IptablesControlPlaneExtractor)1 MrvCombinedParser (org.batfish.grammar.mrv.MrvCombinedParser)1 MrvControlPlaneExtractor (org.batfish.grammar.mrv.MrvControlPlaneExtractor)1 VendorConfiguration (org.batfish.vendor.VendorConfiguration)1