Search in sources :

Example 11 with Answer

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

the class WorkMgr method getAnalysisAnswers.

public Map<String, String> getAnalysisAnswers(String containerName, String baseTestrig, String baseEnv, String deltaTestrig, String deltaEnv, String analysisName) throws JsonProcessingException {
    Path analysisDir = getdirContainerAnalysis(containerName, analysisName);
    Path testrigDir = getdirTestrig(containerName, baseTestrig);
    SortedSet<String> questions = listAnalysisQuestions(containerName, analysisName);
    Map<String, String> retMap = new TreeMap<>();
    for (String questionName : questions) {
        String answer = "unknown";
        Path questionFile = analysisDir.resolve(Paths.get(BfConsts.RELPATH_QUESTIONS_DIR, questionName, BfConsts.RELPATH_QUESTION_FILE));
        if (!Files.exists(questionFile)) {
            throw new BatfishException("Question file for question " + questionName + "not found");
        }
        Path answerDir = testrigDir.resolve(Paths.get(BfConsts.RELPATH_ANALYSES_DIR, analysisName, BfConsts.RELPATH_QUESTIONS_DIR, questionName, BfConsts.RELPATH_ENVIRONMENTS_DIR, baseEnv));
        if (deltaTestrig != null) {
            answerDir = answerDir.resolve(Paths.get(BfConsts.RELPATH_DELTA, deltaTestrig, deltaEnv));
        }
        Path answerFile = answerDir.resolve(BfConsts.RELPATH_ANSWER_JSON);
        if (!Files.exists(answerFile)) {
            Answer ans = Answer.failureAnswer("Not answered", null);
            ans.setStatus(AnswerStatus.NOTFOUND);
            answer = BatfishObjectMapper.writePrettyString(ans);
        } else {
            boolean answerIsStale;
            answerIsStale = CommonUtil.getLastModifiedTime(questionFile).compareTo(CommonUtil.getLastModifiedTime(answerFile)) > 0;
            if (answerIsStale) {
                Answer ans = Answer.failureAnswer("Not fresh", null);
                ans.setStatus(AnswerStatus.STALE);
                answer = BatfishObjectMapper.writePrettyString(ans);
            } else {
                answer = CommonUtil.readFile(answerFile);
            }
        }
        retMap.put(questionName, answer);
    }
    return retMap;
}
Also used : Path(java.nio.file.Path) BatfishException(org.batfish.common.BatfishException) Answer(org.batfish.datamodel.answers.Answer) TreeMap(java.util.TreeMap)

Example 12 with Answer

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

the class BatfishTest method testAnswerBadQuestion.

@Test
public void testAnswerBadQuestion() throws IOException {
    // missing class field
    String badQuestionStr = "{" + "\"differential\": false," + "\"instance\": {" + "\"description\": \"Outputs cases where undefined structures (e.g., ACL, routemaps) " + "are referenced.\"," + "\"instanceName\": \"undefinedReferences\"," + "\"longDescription\": \"Such occurrences indicate configuration errors and can have" + "serious consequences with some vendors.\"," + "\"tags\": [\"default\"]," + "\"variables\": {\"nodeRegex\": {" + "\"description\": \"Only check nodes whose name matches this regex\"," + "\"type\": \"javaRegex\"," + "\"value\": \".*\"" + "}}" + "}," + "\"nodeRegex\": \"${nodeRegex}\"" + "}";
    Path questionPath = _folder.newFile("testAnswerBadQuestion").toPath();
    Files.write(questionPath, badQuestionStr.getBytes(StandardCharsets.UTF_8));
    Batfish batfish = BatfishTestUtils.getBatfish(new TreeMap<>(), _folder);
    batfish.getSettings().setQuestionPath(questionPath);
    Answer answer = batfish.answer();
    assertThat(answer.getQuestion(), is(nullValue()));
    assertEquals(answer.getStatus(), AnswerStatus.FAILURE);
    assertEquals(answer.getAnswerElements().size(), 1);
    assertThat(answer.getAnswerElements().get(0).prettyPrint(), containsString("Could not parse question"));
}
Also used : Path(java.nio.file.Path) Answer(org.batfish.datamodel.answers.Answer) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 13 with Answer

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

the class Batfish method serializeAwsConfigs.

private Answer serializeAwsConfigs(Path testRigPath, Path outputPath) {
    Answer answer = new Answer();
    Map<Path, String> configurationData = readConfigurationFiles(testRigPath, BfConsts.RELPATH_AWS_CONFIGS_DIR);
    AwsConfiguration config;
    try (ActiveSpan parseAwsConfigsSpan = GlobalTracer.get().buildSpan("Parse AWS configs").startActive()) {
        // avoid unused warning
        assert parseAwsConfigsSpan != null;
        config = parseAwsConfigurations(configurationData);
    }
    _logger.info("\n*** SERIALIZING AWS CONFIGURATION STRUCTURES ***\n");
    _logger.resetTimer();
    outputPath.toFile().mkdirs();
    Path currentOutputPath = outputPath.resolve(BfConsts.RELPATH_AWS_CONFIGS_FILE);
    _logger.debugf("Serializing AWS to \"%s\"...", currentOutputPath);
    serializeObject(config, currentOutputPath);
    _logger.debug("OK\n");
    _logger.printElapsedTime();
    return answer;
}
Also used : Path(java.nio.file.Path) Answer(org.batfish.datamodel.answers.Answer) AwsConfiguration(org.batfish.representation.aws.AwsConfiguration) ActiveSpan(io.opentracing.ActiveSpan)

Example 14 with Answer

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

the class Batfish method serializeEnvironmentBgpTables.

private Answer serializeEnvironmentBgpTables(Path inputPath, Path outputPath) {
    Answer answer = new Answer();
    ParseEnvironmentBgpTablesAnswerElement answerElement = new ParseEnvironmentBgpTablesAnswerElement();
    answerElement.setVersion(Version.getVersion());
    answer.addAnswerElement(answerElement);
    SortedMap<String, BgpAdvertisementsByVrf> bgpTables = getEnvironmentBgpTables(inputPath, answerElement);
    serializeEnvironmentBgpTables(bgpTables, outputPath);
    serializeObject(answerElement, _testrigSettings.getEnvironmentSettings().getParseEnvironmentBgpTablesAnswerPath());
    return answer;
}
Also used : Answer(org.batfish.datamodel.answers.Answer) ParseEnvironmentBgpTablesAnswerElement(org.batfish.datamodel.answers.ParseEnvironmentBgpTablesAnswerElement) BgpAdvertisementsByVrf(org.batfish.datamodel.collections.BgpAdvertisementsByVrf)

Example 15 with Answer

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

the class Batfish method serializeVendorConfigs.

Answer serializeVendorConfigs(Path testRigPath, Path outputPath) {
    Answer answer = new Answer();
    boolean configsFound = false;
    // look for network configs
    Path networkConfigsPath = testRigPath.resolve(BfConsts.RELPATH_CONFIGURATIONS_DIR);
    ParseVendorConfigurationAnswerElement answerElement = new ParseVendorConfigurationAnswerElement();
    answerElement.setVersion(Version.getVersion());
    if (_settings.getVerboseParse()) {
        answer.addAnswerElement(answerElement);
    }
    // look for host configs and overlay configs
    SortedMap<String, VendorConfiguration> overlayHostConfigurations = new TreeMap<>();
    Path hostConfigsPath = testRigPath.resolve(BfConsts.RELPATH_HOST_CONFIGS_DIR);
    if (Files.exists(hostConfigsPath)) {
        overlayHostConfigurations = serializeHostConfigs(testRigPath, outputPath, answerElement);
        configsFound = true;
    }
    if (Files.exists(networkConfigsPath)) {
        serializeNetworkConfigs(testRigPath, outputPath, answerElement, overlayHostConfigurations);
        configsFound = true;
    }
    // look for AWS VPC configs
    Path awsVpcConfigsPath = testRigPath.resolve(BfConsts.RELPATH_AWS_CONFIGS_DIR);
    if (Files.exists(awsVpcConfigsPath)) {
        answer.append(serializeAwsConfigs(testRigPath, outputPath));
        configsFound = true;
    }
    if (!configsFound) {
        throw new BatfishException("No valid configurations found");
    }
    // serialize warnings
    serializeObject(answerElement, _testrigSettings.getParseAnswerPath());
    return answer;
}
Also used : Path(java.nio.file.Path) Answer(org.batfish.datamodel.answers.Answer) CleanBatfishException(org.batfish.common.CleanBatfishException) BatfishException(org.batfish.common.BatfishException) IptablesVendorConfiguration(org.batfish.representation.iptables.IptablesVendorConfiguration) VendorConfiguration(org.batfish.vendor.VendorConfiguration) ParseVendorConfigurationAnswerElement(org.batfish.datamodel.answers.ParseVendorConfigurationAnswerElement) TreeMap(java.util.TreeMap)

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