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