Search in sources :

Example 6 with RoutesByVrf

use of org.batfish.datamodel.collections.RoutesByVrf 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);
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) BatfishException(org.batfish.common.BatfishException) ParserBatfishException(org.batfish.main.ParserBatfishException) IosRoutingTableCombinedParser(org.batfish.grammar.routing_table.ios.IosRoutingTableCombinedParser) IosRoutingTableExtractor(org.batfish.grammar.routing_table.ios.IosRoutingTableExtractor) NxosRoutingTableExtractor(org.batfish.grammar.routing_table.nxos.NxosRoutingTableExtractor) EosRoutingTableCombinedParser(org.batfish.grammar.routing_table.eos.EosRoutingTableCombinedParser) BatfishException(org.batfish.common.BatfishException) ParserBatfishException(org.batfish.main.ParserBatfishException) ParserBatfishException(org.batfish.main.ParserBatfishException) EosRoutingTableExtractor(org.batfish.grammar.routing_table.eos.EosRoutingTableExtractor) NxosRoutingTableCombinedParser(org.batfish.grammar.routing_table.nxos.NxosRoutingTableCombinedParser) EosRoutingTableExtractor(org.batfish.grammar.routing_table.eos.EosRoutingTableExtractor) NxosRoutingTableExtractor(org.batfish.grammar.routing_table.nxos.NxosRoutingTableExtractor) IosRoutingTableExtractor(org.batfish.grammar.routing_table.ios.IosRoutingTableExtractor) RoutingTableExtractor(org.batfish.grammar.RoutingTableExtractor) RoutesByVrf(org.batfish.datamodel.collections.RoutesByVrf) RoutingTableFormat(org.batfish.grammar.RoutingTableFormat)

Example 7 with RoutesByVrf

use of org.batfish.datamodel.collections.RoutesByVrf in project batfish by batfish.

the class BdpDataPlanePluginTest method testIosRtStaticMatchesBdp.

@Test
public void testIosRtStaticMatchesBdp() throws IOException {
    String testrigResourcePrefix = TESTRIGS_PREFIX + "ios-rt-static-ad";
    List<String> configurationNames = ImmutableList.of("r1");
    List<String> routingTableNames = ImmutableList.of("r1");
    Batfish batfish = BatfishTestUtils.getBatfishFromTestrigText(TestrigText.builder().setConfigurationText(testrigResourcePrefix, configurationNames).setRoutingTablesText(testrigResourcePrefix, routingTableNames).build(), _folder);
    BdpDataPlanePlugin dataPlanePlugin = new BdpDataPlanePlugin();
    dataPlanePlugin.initialize(batfish);
    batfish.computeDataPlane(false);
    SortedMap<String, RoutesByVrf> environmentRoutes = batfish.loadEnvironmentRoutingTables();
    SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> routes = dataPlanePlugin.getRoutes(batfish.loadDataPlane());
    Prefix staticRoutePrefix = Prefix.parse("10.0.0.0/8");
    SortedSet<AbstractRoute> r1BdpRoutes = routes.get("r1").get(DEFAULT_VRF_NAME);
    AbstractRoute r1BdpRoute = r1BdpRoutes.stream().filter(r -> r.getNetwork().equals(staticRoutePrefix)).findFirst().get();
    SortedSet<Route> r1EnvironmentRoutes = environmentRoutes.get("r1").get(DEFAULT_VRF_NAME);
    Route r1EnvironmentRoute = r1EnvironmentRoutes.stream().filter(r -> r.getNetwork().equals(staticRoutePrefix)).findFirst().get();
    assertThat(r1BdpRoute.getAdministrativeCost(), equalTo(r1EnvironmentRoute.getAdministrativeCost()));
    assertThat(r1BdpRoute.getMetric(), equalTo(r1EnvironmentRoute.getMetric()));
    assertThat(r1BdpRoute.getProtocol(), equalTo(r1EnvironmentRoute.getProtocol()));
}
Also used : AbstractRoute(org.batfish.datamodel.AbstractRoute) Matchers.containsString(org.hamcrest.Matchers.containsString) Prefix(org.batfish.datamodel.Prefix) SortedMap(java.util.SortedMap) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) RoutesByVrf(org.batfish.datamodel.collections.RoutesByVrf) Batfish(org.batfish.main.Batfish) AbstractRoute(org.batfish.datamodel.AbstractRoute) BgpRoute(org.batfish.datamodel.BgpRoute) StaticRoute(org.batfish.datamodel.StaticRoute) Route(org.batfish.datamodel.Route) Test(org.junit.Test)

Aggregations

RoutesByVrf (org.batfish.datamodel.collections.RoutesByVrf)7 Path (java.nio.file.Path)3 TreeMap (java.util.TreeMap)3 BatfishException (org.batfish.common.BatfishException)3 CleanBatfishException (org.batfish.common.CleanBatfishException)2 ParseEnvironmentRoutingTablesAnswerElement (org.batfish.datamodel.answers.ParseEnvironmentRoutingTablesAnswerElement)2 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 SortedMap (java.util.SortedMap)1 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)1 ImmutableConfiguration (org.apache.commons.configuration2.ImmutableConfiguration)1 Warnings (org.batfish.common.Warnings)1 EnvironmentSettings (org.batfish.config.Settings.EnvironmentSettings)1 AbstractRoute (org.batfish.datamodel.AbstractRoute)1 BgpRoute (org.batfish.datamodel.BgpRoute)1 Configuration (org.batfish.datamodel.Configuration)1 Prefix (org.batfish.datamodel.Prefix)1 Route (org.batfish.datamodel.Route)1 StaticRoute (org.batfish.datamodel.StaticRoute)1