Search in sources :

Example 6 with Answer

use of org.batfish.datamodel.answers.Answer in project batfish by batfish.

the class Batfish method serializeEnvironmentRoutingTables.

private Answer serializeEnvironmentRoutingTables(Path inputPath, Path outputPath) {
    Answer answer = new Answer();
    ParseEnvironmentRoutingTablesAnswerElement answerElement = new ParseEnvironmentRoutingTablesAnswerElement();
    answerElement.setVersion(Version.getVersion());
    answer.addAnswerElement(answerElement);
    SortedMap<String, RoutesByVrf> routingTables = getEnvironmentRoutingTables(inputPath, answerElement);
    serializeEnvironmentRoutingTables(routingTables, outputPath);
    serializeObject(answerElement, _testrigSettings.getEnvironmentSettings().getParseEnvironmentRoutingTablesAnswerPath());
    return answer;
}
Also used : Answer(org.batfish.datamodel.answers.Answer) ParseEnvironmentRoutingTablesAnswerElement(org.batfish.datamodel.answers.ParseEnvironmentRoutingTablesAnswerElement) RoutesByVrf(org.batfish.datamodel.collections.RoutesByVrf)

Example 7 with Answer

use of org.batfish.datamodel.answers.Answer in project batfish by batfish.

the class Batfish method answer.

public Answer answer() {
    Question question = null;
    // return right away if we cannot parse the question successfully
    try (ActiveSpan parseQuestionSpan = GlobalTracer.get().buildSpan("Parse question").startActive()) {
        // avoid not used warning
        assert parseQuestionSpan != null;
        question = Question.parseQuestion(_settings.getQuestionPath());
    } catch (Exception e) {
        Answer answer = new Answer();
        BatfishException exception = new BatfishException("Could not parse question", e);
        answer.setStatus(AnswerStatus.FAILURE);
        answer.addAnswerElement(exception.getBatfishStackTrace());
        return answer;
    }
    if (_settings.getDifferential()) {
        question.setDifferential(true);
    }
    boolean dp = question.getDataPlane();
    boolean diff = question.getDifferential();
    boolean diffActive = _settings.getDiffActive() && !diff;
    _settings.setDiffActive(diffActive);
    _settings.setDiffQuestion(diff);
    try (ActiveSpan loadConfigurationSpan = GlobalTracer.get().buildSpan("Load configurations").startActive()) {
        // avoid not used warning
        assert loadConfigurationSpan != null;
        // Ensures configurations are parsed and ready
        loadConfigurations();
    }
    try (ActiveSpan initQuestionEnvSpan = GlobalTracer.get().buildSpan("Init question environment").startActive()) {
        // avoid not used warning
        assert initQuestionEnvSpan != null;
        initQuestionEnvironments(question, diff, diffActive, dp);
    }
    AnswerElement answerElement = null;
    BatfishException exception = null;
    try (ActiveSpan getAnswerSpan = GlobalTracer.get().buildSpan("Get answer").startActive()) {
        // avoid not used warning
        assert getAnswerSpan != null;
        if (question.getDifferential()) {
            answerElement = Answerer.create(question, this).answerDiff();
        } else {
            answerElement = Answerer.create(question, this).answer();
        }
    } catch (Exception e) {
        exception = new BatfishException("Failed to answer question", e);
    }
    Answer answer = new Answer();
    answer.setQuestion(question);
    if (exception == null) {
        // success
        answer.setStatus(AnswerStatus.SUCCESS);
        answer.addAnswerElement(answerElement);
    } else {
        // failure
        answer.setStatus(AnswerStatus.FAILURE);
        answer.addAnswerElement(exception.getBatfishStackTrace());
    }
    return answer;
}
Also used : Answer(org.batfish.datamodel.answers.Answer) CleanBatfishException(org.batfish.common.CleanBatfishException) BatfishException(org.batfish.common.BatfishException) ActiveSpan(io.opentracing.ActiveSpan) AnswerElement(org.batfish.datamodel.answers.AnswerElement) InitInfoAnswerElement(org.batfish.datamodel.answers.InitInfoAnswerElement) RunAnalysisAnswerElement(org.batfish.datamodel.answers.RunAnalysisAnswerElement) DataPlaneAnswerElement(org.batfish.datamodel.answers.DataPlaneAnswerElement) ConvertConfigurationAnswerElement(org.batfish.datamodel.answers.ConvertConfigurationAnswerElement) AclLinesAnswerElement(org.batfish.datamodel.answers.AclLinesAnswerElement) ParseEnvironmentRoutingTablesAnswerElement(org.batfish.datamodel.answers.ParseEnvironmentRoutingTablesAnswerElement) NodAnswerElement(org.batfish.datamodel.answers.NodAnswerElement) NodFirstUnsatAnswerElement(org.batfish.datamodel.answers.NodFirstUnsatAnswerElement) InitStepAnswerElement(org.batfish.datamodel.answers.InitStepAnswerElement) NodSatAnswerElement(org.batfish.datamodel.answers.NodSatAnswerElement) ParseAnswerElement(org.batfish.datamodel.answers.ParseAnswerElement) FlattenVendorConfigurationAnswerElement(org.batfish.datamodel.answers.FlattenVendorConfigurationAnswerElement) ValidateEnvironmentAnswerElement(org.batfish.datamodel.answers.ValidateEnvironmentAnswerElement) ParseEnvironmentBgpTablesAnswerElement(org.batfish.datamodel.answers.ParseEnvironmentBgpTablesAnswerElement) ParseVendorConfigurationAnswerElement(org.batfish.datamodel.answers.ParseVendorConfigurationAnswerElement) ReportAnswerElement(org.batfish.datamodel.answers.ReportAnswerElement) HeaderQuestion(org.batfish.datamodel.questions.smt.HeaderQuestion) RoleQuestion(org.batfish.datamodel.questions.smt.RoleQuestion) HeaderLocationQuestion(org.batfish.datamodel.questions.smt.HeaderLocationQuestion) Question(org.batfish.datamodel.questions.Question) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) CleanBatfishException(org.batfish.common.CleanBatfishException) JSONException(org.codehaus.jettison.json.JSONException) BatfishException(org.batfish.common.BatfishException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) InvalidReachabilitySettingsException(org.batfish.datamodel.questions.InvalidReachabilitySettingsException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 8 with Answer

use of org.batfish.datamodel.answers.Answer in project batfish by batfish.

the class Batfish method validateEnvironment.

private Answer validateEnvironment() {
    Answer answer = new Answer();
    ValidateEnvironmentAnswerElement ae = loadValidateEnvironmentAnswerElement();
    answer.addAnswerElement(ae);
    Topology envTopology = computeEnvironmentTopology(loadConfigurations());
    serializeAsJson(_testrigSettings.getEnvironmentSettings().getSerializedTopologyPath(), envTopology, "environment topology");
    return answer;
}
Also used : Answer(org.batfish.datamodel.answers.Answer) ValidateEnvironmentAnswerElement(org.batfish.datamodel.answers.ValidateEnvironmentAnswerElement) Topology(org.batfish.datamodel.Topology)

Example 9 with Answer

use of org.batfish.datamodel.answers.Answer 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;
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) CleanBatfishException(org.batfish.common.CleanBatfishException) SpanContext(io.opentracing.SpanContext) BatfishLogger(org.batfish.common.BatfishLogger) CleanBatfishException(org.batfish.common.CleanBatfishException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ProcessingException(javax.ws.rs.ProcessingException) QuestionException(org.batfish.common.QuestionException) BatfishException(org.batfish.common.BatfishException) IOException(java.io.IOException) CleanBatfishException(org.batfish.common.CleanBatfishException) Answer(org.batfish.datamodel.answers.Answer) ActiveSpan(io.opentracing.ActiveSpan) QuestionException(org.batfish.common.QuestionException) Nullable(javax.annotation.Nullable)

Example 10 with Answer

use of org.batfish.datamodel.answers.Answer in project batfish by batfish.

the class WorkMgr method getAnswer.

public String getAnswer(String containerName, String baseTestrig, String baseEnv, String deltaTestrig, String deltaEnv, String questionName) throws JsonProcessingException {
    Path questionDir = getdirContainerQuestion(containerName, questionName);
    Path questionFile = questionDir.resolve(BfConsts.RELPATH_QUESTION_FILE);
    if (!Files.exists(questionFile)) {
        throw new BatfishException("Question file not found for " + questionName);
    }
    Path testrigDir = getdirTestrig(containerName, baseTestrig);
    Path answerDir = testrigDir.resolve(Paths.get(BfConsts.RELPATH_ANSWERS_DIR, questionName, baseEnv));
    if (deltaTestrig != null) {
        answerDir = answerDir.resolve(Paths.get(BfConsts.RELPATH_DIFF_DIR, deltaTestrig, deltaEnv));
    } else {
        answerDir = answerDir.resolve(Paths.get(BfConsts.RELPATH_STANDARD_DIR));
    }
    Path answerFile = answerDir.resolve(BfConsts.RELPATH_ANSWER_JSON);
    String answer = "unknown";
    if (!Files.exists(answerFile)) {
        Answer ans = Answer.failureAnswer("Not answered", null);
        ans.setStatus(AnswerStatus.NOTFOUND);
        answer = BatfishObjectMapper.writePrettyString(ans);
    } else {
        if (CommonUtil.getLastModifiedTime(questionFile).compareTo(CommonUtil.getLastModifiedTime(answerFile)) > 0) {
            Answer ans = Answer.failureAnswer("Not fresh", null);
            ans.setStatus(AnswerStatus.STALE);
            answer = BatfishObjectMapper.writePrettyString(ans);
        } else {
            answer = CommonUtil.readFile(answerFile);
        }
    }
    return answer;
}
Also used : Path(java.nio.file.Path) BatfishException(org.batfish.common.BatfishException) Answer(org.batfish.datamodel.answers.Answer)

Aggregations

Answer (org.batfish.datamodel.answers.Answer)22 BatfishException (org.batfish.common.BatfishException)14 Path (java.nio.file.Path)13 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)8 CleanBatfishException (org.batfish.common.CleanBatfishException)8 IOException (java.io.IOException)7 JSONException (org.codehaus.jettison.json.JSONException)6 ActiveSpan (io.opentracing.ActiveSpan)5 PatternSyntaxException (java.util.regex.PatternSyntaxException)5 TreeMap (java.util.TreeMap)4 ExecutionException (java.util.concurrent.ExecutionException)3 Nullable (javax.annotation.Nullable)3 LoadQuestionAnswerElement (org.batfish.client.answer.LoadQuestionAnswerElement)3 Topology (org.batfish.datamodel.Topology)3 ConvertConfigurationAnswerElement (org.batfish.datamodel.answers.ConvertConfigurationAnswerElement)3 AwsConfiguration (org.batfish.representation.aws.AwsConfiguration)3 IptablesVendorConfiguration (org.batfish.representation.iptables.IptablesVendorConfiguration)3 VendorConfiguration (org.batfish.vendor.VendorConfiguration)3 JSONObject (org.codehaus.jettison.json.JSONObject)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2