Search in sources :

Example 71 with BatfishException

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

the class Batfish method loadParseEnvironmentBgpTablesAnswerElement.

private ParseEnvironmentBgpTablesAnswerElement loadParseEnvironmentBgpTablesAnswerElement(boolean firstAttempt) {
    Path answerPath = _testrigSettings.getEnvironmentSettings().getParseEnvironmentBgpTablesAnswerPath();
    if (!Files.exists(answerPath)) {
        repairEnvironmentBgpTables();
    }
    ParseEnvironmentBgpTablesAnswerElement ae = deserializeObject(answerPath, ParseEnvironmentBgpTablesAnswerElement.class);
    if (!Version.isCompatibleVersion("Service", "Old processed environment BGP tables", ae.getVersion())) {
        if (firstAttempt) {
            repairEnvironmentRoutingTables();
            return loadParseEnvironmentBgpTablesAnswerElement(false);
        } else {
            throw new BatfishException("Version error repairing environment BGP tables for parse environment BGP tables " + "answer element");
        }
    } else {
        return ae;
    }
}
Also used : Path(java.nio.file.Path) CleanBatfishException(org.batfish.common.CleanBatfishException) BatfishException(org.batfish.common.BatfishException) ParseEnvironmentBgpTablesAnswerElement(org.batfish.datamodel.answers.ParseEnvironmentBgpTablesAnswerElement)

Example 72 with BatfishException

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

the class Batfish method loadDataPlaneAnswerElement.

private DataPlaneAnswerElement loadDataPlaneAnswerElement(boolean compressed, boolean firstAttempt) {
    Path answerPath = compressed ? _testrigSettings.getEnvironmentSettings().getCompressedDataPlaneAnswerPath() : _testrigSettings.getEnvironmentSettings().getDataPlaneAnswerPath();
    DataPlaneAnswerElement bae = deserializeObject(answerPath, DataPlaneAnswerElement.class);
    if (!Version.isCompatibleVersion("Service", "Old data plane", bae.getVersion())) {
        if (firstAttempt) {
            repairDataPlane(compressed);
            return loadDataPlaneAnswerElement(compressed, false);
        } else {
            throw new BatfishException("Version error repairing data plane for data plane answer element");
        }
    } else {
        return bae;
    }
}
Also used : Path(java.nio.file.Path) CleanBatfishException(org.batfish.common.CleanBatfishException) BatfishException(org.batfish.common.BatfishException) DataPlaneAnswerElement(org.batfish.datamodel.answers.DataPlaneAnswerElement)

Example 73 with BatfishException

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

the class Batfish method readIptableFiles.

/**
 * Read Iptable Files for each host in the keyset of {@code hostConfigurations}, and store the
 * contents in {@code iptablesDate}. Each task fails if the Iptable file specified by host is not
 * under {@code testRigPath} or does not exist.
 *
 * @throws BatfishException if there is a failed task and either {@link
 *     Settings#getExitOnFirstError()} or {@link Settings#getHaltOnParseError()} is set.
 */
void readIptableFiles(Path testRigPath, SortedMap<String, VendorConfiguration> hostConfigurations, SortedMap<Path, String> iptablesData, ParseVendorConfigurationAnswerElement answerElement) {
    List<BatfishException> failureCauses = new ArrayList<>();
    for (VendorConfiguration vc : hostConfigurations.values()) {
        HostConfiguration hostConfig = (HostConfiguration) vc;
        if (hostConfig.getIptablesFile() != null) {
            Path path = Paths.get(testRigPath.toString(), hostConfig.getIptablesFile());
            // testrig
            try {
                if (!path.toFile().getCanonicalPath().contains(testRigPath.toFile().getCanonicalPath()) || !path.toFile().exists()) {
                    String failureMessage = String.format("Iptables file %s for host %s is not contained within the testrig", hostConfig.getIptablesFile(), hostConfig.getHostname());
                    BatfishException bfc;
                    if (answerElement.getErrors().containsKey(hostConfig.getHostname())) {
                        bfc = new BatfishException(failureMessage, answerElement.getErrors().get(hostConfig.getHostname()).getException());
                        answerElement.getErrors().put(hostConfig.getHostname(), bfc.getBatfishStackTrace());
                    } else {
                        bfc = new BatfishException(failureMessage);
                        if (_settings.getExitOnFirstError()) {
                            throw bfc;
                        } else {
                            failureCauses.add(bfc);
                            answerElement.getErrors().put(hostConfig.getHostname(), bfc.getBatfishStackTrace());
                            answerElement.getParseStatus().put(hostConfig.getHostname(), ParseStatus.FAILED);
                        }
                    }
                } else {
                    String fileText = CommonUtil.readFile(path);
                    iptablesData.put(path, fileText);
                }
            } catch (IOException e) {
                throw new BatfishException("Could not get canonical path", e);
            }
        }
    }
    if (_settings.getHaltOnParseError() && !failureCauses.isEmpty()) {
        BatfishException e = new BatfishException("Fatal exception due to at least one Iptables file is" + " not contained within the testrig");
        failureCauses.forEach(e::addSuppressed);
        throw e;
    }
}
Also used : Path(java.nio.file.Path) CleanBatfishException(org.batfish.common.CleanBatfishException) BatfishException(org.batfish.common.BatfishException) IptablesVendorConfiguration(org.batfish.representation.iptables.IptablesVendorConfiguration) VendorConfiguration(org.batfish.vendor.VendorConfiguration) Serializable(java.io.Serializable) BatfishStackTrace(org.batfish.common.BatfishException.BatfishStackTrace) AclLine(org.batfish.z3.AclLine) DeviceType(org.batfish.datamodel.DeviceType) InterfaceType(org.batfish.datamodel.InterfaceType) FlowTrace(org.batfish.datamodel.FlowTrace) Edge(org.batfish.datamodel.Edge) IpAccessListLine(org.batfish.datamodel.IpAccessListLine) Cache(com.google.common.cache.Cache) Interface(org.batfish.datamodel.Interface) DataPlane(org.batfish.datamodel.DataPlane) PluginClientType(org.batfish.common.plugin.PluginClientType) Nullable(javax.annotation.Nullable) BgpAdvertisementType(org.batfish.datamodel.BgpAdvertisement.BgpAdvertisementType) File(java.io.File) HeaderSpace(org.batfish.datamodel.HeaderSpace) TypeReference(com.fasterxml.jackson.core.type.TypeReference) AbstractRoute(org.batfish.datamodel.AbstractRoute) SubRange(org.batfish.datamodel.SubRange) HostConfiguration(org.batfish.representation.host.HostConfiguration) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 74 with BatfishException

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

the class Batfish method analyze.

private Answer analyze() {
    Answer answer = new Answer();
    AnswerSummary summary = new AnswerSummary();
    String analysisName = _settings.getAnalysisName();
    String containerName = _settings.getContainerDir().getFileName().toString();
    Path analysisQuestionsDir = _settings.getContainerDir().resolve(Paths.get(BfConsts.RELPATH_ANALYSES_DIR, analysisName, BfConsts.RELPATH_QUESTIONS_DIR).toString());
    if (!Files.exists(analysisQuestionsDir)) {
        throw new BatfishException("Analysis questions dir does not exist: '" + analysisQuestionsDir + "'");
    }
    RunAnalysisAnswerElement ae = new RunAnalysisAnswerElement();
    try (Stream<Path> questions = CommonUtil.list(analysisQuestionsDir)) {
        questions.forEach(analysisQuestionDir -> {
            String questionName = analysisQuestionDir.getFileName().toString();
            Path analysisQuestionPath = analysisQuestionDir.resolve(BfConsts.RELPATH_QUESTION_FILE);
            _settings.setQuestionPath(analysisQuestionPath);
            Answer currentAnswer;
            try (ActiveSpan analysisQuestionSpan = GlobalTracer.get().buildSpan(String.format("Getting answer to question %s from analysis %s", questionName, analysisName)).startActive()) {
                // make span not show up as unused
                assert analysisQuestionSpan != null;
                currentAnswer = answer();
            }
            // Ensuring that question was parsed successfully
            if (currentAnswer.getQuestion() != null) {
                try {
                    // TODO: This can be represented much cleanly and easily with a Json
                    _logger.infof("Ran question:%s from analysis:%s in container:%s; work-id:%s, status:%s, " + "computed dataplane:%s, parameters:%s\n", questionName, analysisName, containerName, getTaskId(), currentAnswer.getSummary().getNumFailed() > 0 ? "failed" : "passed", currentAnswer.getQuestion().getDataPlane(), BatfishObjectMapper.writeString(currentAnswer.getQuestion().getInstance().getVariables()));
                } catch (JsonProcessingException e) {
                    throw new BatfishException(String.format("Error logging question %s in analysis %s", questionName, analysisName), e);
                }
            }
            initAnalysisQuestionPath(analysisName, questionName);
            outputAnswer(currentAnswer);
            ae.getAnswers().put(questionName, currentAnswer);
            _settings.setQuestionPath(null);
            summary.combine(currentAnswer.getSummary());
        });
    }
    answer.addAnswerElement(ae);
    answer.setSummary(summary);
    return answer;
}
Also used : Path(java.nio.file.Path) RunAnalysisAnswerElement(org.batfish.datamodel.answers.RunAnalysisAnswerElement) Answer(org.batfish.datamodel.answers.Answer) CleanBatfishException(org.batfish.common.CleanBatfishException) BatfishException(org.batfish.common.BatfishException) ActiveSpan(io.opentracing.ActiveSpan) AnswerSummary(org.batfish.datamodel.answers.AnswerSummary) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 75 with BatfishException

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

the class Batfish method loadConvertConfigurationAnswerElementOrReparse.

@Override
public ConvertConfigurationAnswerElement loadConvertConfigurationAnswerElementOrReparse() {
    ConvertConfigurationAnswerElement ccae = _storage.loadConvertConfigurationAnswerElement(_testrigSettings.getName());
    if (ccae != null && Version.isCompatibleVersion("Service", "Old processed configurations", ccae.getVersion())) {
        return ccae;
    }
    repairConfigurations();
    ccae = _storage.loadConvertConfigurationAnswerElement(_testrigSettings.getName());
    if (ccae != null && Version.isCompatibleVersion("Service", "Old processed configurations", ccae.getVersion())) {
        return ccae;
    } else {
        throw new BatfishException("Version error repairing configurations for convert configuration answer element");
    }
}
Also used : CleanBatfishException(org.batfish.common.CleanBatfishException) BatfishException(org.batfish.common.BatfishException) ConvertConfigurationAnswerElement(org.batfish.datamodel.answers.ConvertConfigurationAnswerElement)

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