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;
}
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"));
}
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;
}
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;
}
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;
}
Aggregations