Search in sources :

Example 1 with AnswerSummary

use of org.batfish.datamodel.answers.AnswerSummary 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 2 with AnswerSummary

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

the class JsonPathQuestionPluginTest method updateSummaryTest.

@Test
public void updateSummaryTest() {
    JsonPathAnswerElement answerElement = new JsonPathAnswerElement();
    JsonPathResult result1 = new JsonPathResult();
    result1.setAssertionResult(true);
    result1.setNumResults(2);
    JsonPathResult result2 = new JsonPathResult();
    result2.setAssertionResult(false);
    result2.setNumResults(5);
    JsonPathResult result3 = new JsonPathResult();
    result3.setNumResults(3);
    answerElement.getResults().put(0, result1);
    answerElement.getResults().put(1, result2);
    answerElement.getResults().put(2, result3);
    answerElement.updateSummary();
    AnswerSummary summary = answerElement.getSummary();
    assertThat(summary.getNumFailed(), equalTo(1));
    assertThat(summary.getNumPassed(), equalTo(1));
    assertThat(summary.getNumResults(), equalTo(10));
}
Also used : JsonPathAnswerElement(org.batfish.question.jsonpath.JsonPathQuestionPlugin.JsonPathAnswerElement) AnswerSummary(org.batfish.datamodel.answers.AnswerSummary) Test(org.junit.Test)

Aggregations

AnswerSummary (org.batfish.datamodel.answers.AnswerSummary)2 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 RunAnalysisAnswerElement (org.batfish.datamodel.answers.RunAnalysisAnswerElement)1 JsonPathAnswerElement (org.batfish.question.jsonpath.JsonPathQuestionPlugin.JsonPathAnswerElement)1 Test (org.junit.Test)1