Search in sources :

Example 96 with BatfishException

use of org.batfish.common.BatfishException in project batfish by batfish.

the class CiscoControlPlaneExtractor method toRoutePolicyPrefixSet.

private RoutePolicyPrefixSet toRoutePolicyPrefixSet(Rp_prefix_setContext ctx) {
    if (ctx.name != null) {
        // named
        String name = ctx.name.getText();
        int expressionLine = ctx.name.getStart().getLine();
        return new RoutePolicyPrefixSetName(name, expressionLine);
    } else {
        // inline
        PrefixSpace prefixSpace = new PrefixSpace();
        Prefix6Space prefix6Space = new Prefix6Space();
        boolean ipv6 = false;
        for (Prefix_set_elemContext pctxt : ctx.elems) {
            int lower;
            int upper;
            Prefix prefix = null;
            Prefix6 prefix6 = null;
            if (pctxt.prefix != null) {
                prefix = Prefix.parse(pctxt.prefix.getText());
                lower = prefix.getPrefixLength();
                upper = Prefix.MAX_PREFIX_LENGTH;
            } else if (pctxt.ipa != null) {
                prefix = new Prefix(toIp(pctxt.ipa), Prefix.MAX_PREFIX_LENGTH);
                lower = prefix.getPrefixLength();
                upper = Prefix.MAX_PREFIX_LENGTH;
            } else if (pctxt.ipv6a != null) {
                prefix6 = new Prefix6(toIp6(pctxt.ipv6a), Prefix6.MAX_PREFIX_LENGTH);
                lower = prefix6.getPrefixLength();
                upper = Prefix6.MAX_PREFIX_LENGTH;
            } else if (pctxt.ipv6_prefix != null) {
                prefix6 = new Prefix6(pctxt.ipv6_prefix.getText());
                lower = prefix6.getPrefixLength();
                upper = Prefix6.MAX_PREFIX_LENGTH;
            } else {
                throw new BatfishException("Unhandled alternative");
            }
            if (pctxt.minpl != null) {
                lower = toInteger(pctxt.minpl);
            }
            if (pctxt.maxpl != null) {
                upper = toInteger(pctxt.maxpl);
            }
            if (pctxt.eqpl != null) {
                lower = toInteger(pctxt.eqpl);
                upper = lower;
            }
            if (prefix != null) {
                prefixSpace.addPrefixRange(new PrefixRange(prefix, new SubRange(lower, upper)));
            } else {
                prefix6Space.addPrefix6Range(new Prefix6Range(prefix6, new SubRange(lower, upper)));
                ipv6 = true;
            }
        }
        if (ipv6) {
            return new RoutePolicyInlinePrefix6Set(prefix6Space);
        } else {
            return new RoutePolicyInlinePrefixSet(prefixSpace);
        }
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) RedFlagBatfishException(org.batfish.common.RedFlagBatfishException) PrefixRange(org.batfish.datamodel.PrefixRange) RoutePolicyInlinePrefixSet(org.batfish.representation.cisco.RoutePolicyInlinePrefixSet) RoutePolicyPrefixSetName(org.batfish.representation.cisco.RoutePolicyPrefixSetName) PrefixSpace(org.batfish.datamodel.PrefixSpace) Prefix_set_elemContext(org.batfish.grammar.cisco.CiscoParser.Prefix_set_elemContext) Prefix(org.batfish.datamodel.Prefix) Prefix6Space(org.batfish.datamodel.Prefix6Space) Prefix6Range(org.batfish.datamodel.Prefix6Range) RoutePolicyInlinePrefix6Set(org.batfish.representation.cisco.RoutePolicyInlinePrefix6Set) SubRange(org.batfish.datamodel.SubRange) Prefix6(org.batfish.datamodel.Prefix6)

Example 97 with BatfishException

use of org.batfish.common.BatfishException in project batfish by batfish.

the class CiscoControlPlaneExtractor method enterRo_area.

@Override
public void enterRo_area(Ro_areaContext ctx) {
    long area;
    if (ctx.area_int != null) {
        area = toLong(ctx.area_int);
    } else if (ctx.area_ip != null) {
        area = toIp(ctx.area_ip).asLong();
    } else {
        throw new BatfishException("Missing area");
    }
    _currentOspfArea = area;
}
Also used : BatfishException(org.batfish.common.BatfishException) RedFlagBatfishException(org.batfish.common.RedFlagBatfishException)

Example 98 with BatfishException

use of org.batfish.common.BatfishException in project batfish by batfish.

the class BatfishJobExecutor method executeJobs.

/**
 * @param jobs jobs to be executed, of type {@link JobT}
 * @param output data structure to which the result of the job will be applied, it should be of
 *     generic type {@link OutputT}
 * @param answerElement {@link AnswerElement} containing the detail of the jobs executed
 * @param haltOnProcessingError whether to halt on processing error
 * @param description description of the jobs submitted to the executor
 * @param <JobT> type of job executed in the executor
 * @param <AnswerElementT> type of {@link AnswerElement} to which {@link BatfishJobResult} will be
 *     applied
 * @param <JobResultT> type of {@link BatfishJobResult} which will contain the result of the job
 * @param <OutputT> type of data structure to which job result will be applied
 */
private <JobT extends BatfishJob<JobResultT>, AnswerElementT extends AnswerElement, JobResultT extends BatfishJobResult<OutputT, AnswerElementT>, OutputT> void executeJobs(List<JobT> jobs, OutputT output, AnswerElementT answerElement, boolean haltOnProcessingError, String description) {
    // Initializing executors
    ExecutorService pool = createExecutorService();
    ExecutorCompletionService<JobResultT> completionService = new ExecutorCompletionService<>(pool);
    if (!_settings.getSequential() && _settings.getShuffleJobs()) {
        Collections.shuffle(jobs);
    }
    for (JobT job : jobs) {
        completionService.submit(job);
    }
    initializeJobsStats(jobs, description);
    boolean processingError = false;
    List<BatfishException> failureCauses = new ArrayList<>();
    try {
        for (int i = 0; i < jobs.size(); i++) {
            JobResultT result = null;
            try {
                // getting the result of the job
                result = completionService.take().get();
            } catch (InterruptedException e) {
                throw new BatfishException("Job didn't finish", e);
            } catch (ExecutionException e) {
                throw new BatfishException(String.format("Error executing job: %s", e.getCause().getMessage()), e);
            }
            markJobCompleted();
            boolean jobResultError = handleJobResult(result, output, answerElement, failureCauses, haltOnProcessingError);
            if (jobResultError) {
                processingError = true;
            }
        }
    } finally {
        pool.shutdown();
    }
    if (processingError) {
        handleProcessingError(jobs, failureCauses, haltOnProcessingError);
    } else if (!_logger.isActive(BatfishLogger.LEVEL_INFO)) {
        _logger.info("All jobs executed successfully\n");
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) ExecutionException(java.util.concurrent.ExecutionException)

Example 99 with BatfishException

use of org.batfish.common.BatfishException in project batfish by batfish.

the class BatfishJobExecutor method handleProcessingError.

<JobT> void handleProcessingError(List<JobT> jobs, List<BatfishException> failureCauses, boolean haltOnProcessingError) {
    int numJobs = jobs.size();
    int numFailed = failureCauses.size();
    int numSucceeded = numJobs - numFailed;
    _logger.infof("%d jobs succeeded; %d jobs failed\n", numSucceeded, numFailed);
    if (haltOnProcessingError) {
        BatfishException e = new BatfishException(JOB_FAILURE_MESSAGE);
        failureCauses.forEach(e::addSuppressed);
        throw e;
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) ExecutorService(java.util.concurrent.ExecutorService)

Example 100 with BatfishException

use of org.batfish.common.BatfishException in project batfish by batfish.

the class ConvertConfigurationJob method call.

@Override
public ConvertConfigurationResult call() {
    long startTime = System.currentTimeMillis();
    long elapsedTime;
    _logger.infof("Processing: \"%s\"", _name);
    Map<String, Configuration> configurations = new HashMap<>();
    Map<String, Warnings> warningsByHost = new HashMap<>();
    ConvertConfigurationAnswerElement answerElement = new ConvertConfigurationAnswerElement();
    try {
        // We have only two options: AWS VPCs or router configs
        if (VendorConfiguration.class.isInstance(_configObject)) {
            Warnings warnings = Batfish.buildWarnings(_settings);
            VendorConfiguration vendorConfiguration = ((VendorConfiguration) _configObject);
            vendorConfiguration.setWarnings(warnings);
            vendorConfiguration.setAnswerElement(answerElement);
            Configuration configuration = vendorConfiguration.toVendorIndependentConfiguration();
            if (configuration.getDefaultCrossZoneAction() == null) {
                throw new BatfishException("Implementation error: missing default cross-zone action for host: '" + configuration.getHostname() + "'");
            }
            if (configuration.getDefaultInboundAction() == null) {
                throw new BatfishException("Implementation error: missing default inbound action for host: '" + configuration.getHostname() + "'");
            }
            // get iptables if applicable
            IptablesVendorConfiguration iptablesConfig = null;
            VendorConfiguration ov = vendorConfiguration.getOverlayConfiguration();
            if (ov != null) {
                // apply overlay
                HostConfiguration oh = (HostConfiguration) ov;
                iptablesConfig = oh.getIptablesVendorConfig();
            } else if (vendorConfiguration instanceof HostConfiguration) {
            // TODO: To enable below, we need to reconcile overlay and non-overlay iptables semantics.
            // HostConfiguration oh = (HostConfiguration)vendorConfiguration;
            // iptablesConfig = oh.getIptablesVendorConfig();
            }
            if (iptablesConfig != null) {
                iptablesConfig.addAsIpAccessLists(configuration, vendorConfiguration, warnings);
                iptablesConfig.applyAsOverlay(configuration, warnings);
            }
            configurations.put(_name, configuration);
            warningsByHost.put(_name, warnings);
        } else {
            configurations = ((AwsConfiguration) _configObject).toConfigurations(_settings, warningsByHost);
        }
        _logger.info(" ...OK\n");
    } catch (Exception e) {
        String error = "Conversion error for node with hostname '" + _name + "'";
        elapsedTime = System.currentTimeMillis() - startTime;
        return new ConvertConfigurationResult(elapsedTime, _logger.getHistory(), _name, new BatfishException(error, e));
    } finally {
        warningsByHost.forEach((hostname, warnings) -> Batfish.logWarnings(_logger, warnings));
        ;
    }
    elapsedTime = System.currentTimeMillis() - startTime;
    return new ConvertConfigurationResult(elapsedTime, _logger.getHistory(), warningsByHost, _name, configurations, answerElement);
}
Also used : BatfishException(org.batfish.common.BatfishException) VendorConfiguration(org.batfish.vendor.VendorConfiguration) Configuration(org.batfish.datamodel.Configuration) AwsConfiguration(org.batfish.representation.aws.AwsConfiguration) IptablesVendorConfiguration(org.batfish.representation.iptables.IptablesVendorConfiguration) HostConfiguration(org.batfish.representation.host.HostConfiguration) HashMap(java.util.HashMap) HostConfiguration(org.batfish.representation.host.HostConfiguration) BatfishException(org.batfish.common.BatfishException) VendorConfiguration(org.batfish.vendor.VendorConfiguration) IptablesVendorConfiguration(org.batfish.representation.iptables.IptablesVendorConfiguration) ConvertConfigurationAnswerElement(org.batfish.datamodel.answers.ConvertConfigurationAnswerElement) Warnings(org.batfish.common.Warnings) IptablesVendorConfiguration(org.batfish.representation.iptables.IptablesVendorConfiguration)

Aggregations

BatfishException (org.batfish.common.BatfishException)264 IOException (java.io.IOException)61 Path (java.nio.file.Path)54 CleanBatfishException (org.batfish.common.CleanBatfishException)35 RedFlagBatfishException (org.batfish.common.RedFlagBatfishException)34 TreeMap (java.util.TreeMap)31 ArrayList (java.util.ArrayList)30 JSONException (org.codehaus.jettison.json.JSONException)30 Ip (org.batfish.datamodel.Ip)25 JSONObject (org.codehaus.jettison.json.JSONObject)25 Configuration (org.batfish.datamodel.Configuration)24 Map (java.util.Map)23 Prefix (org.batfish.datamodel.Prefix)22 HashMap (java.util.HashMap)20 HashSet (java.util.HashSet)20 TreeSet (java.util.TreeSet)20 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)18 Test (org.junit.Test)18 Set (java.util.Set)17 SortedMap (java.util.SortedMap)17