Search in sources :

Example 1 with RunAnalysisAnswerElement

use of org.batfish.datamodel.answers.RunAnalysisAnswerElement 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)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ActiveSpan (io.opentracing.ActiveSpan)1 Path (java.nio.file.Path)1 BatfishException (org.batfish.common.BatfishException)1 CleanBatfishException (org.batfish.common.CleanBatfishException)1 Answer (org.batfish.datamodel.answers.Answer)1 AnswerSummary (org.batfish.datamodel.answers.AnswerSummary)1 RunAnalysisAnswerElement (org.batfish.datamodel.answers.RunAnalysisAnswerElement)1