use of org.batfish.datamodel.questions.Question in project batfish by batfish.
the class QuestionHelper method getQuestion.
public static Question getQuestion(String questionTypeStr, Map<String, Supplier<Question>> questions) {
Supplier<Question> supplier = questions.get(questionTypeStr);
if (supplier == null) {
throw new BatfishException("No question found of type: " + questionTypeStr + '.');
}
Question question = supplier.get();
return question;
}
use of org.batfish.datamodel.questions.Question 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.questions.Question in project batfish by batfish.
the class IpsecVpnStatusAnswerer method answer.
@Override
public AnswerElement answer() {
IpsecVpnStatusQuestion question = (IpsecVpnStatusQuestion) _question;
Map<String, Configuration> configurations = _batfish.loadConfigurations();
Set<String> includeNodes1 = question.getNode1Regex().getMatchingNodes(configurations);
Set<String> includeNodes2 = question.getNode2Regex().getMatchingNodes(configurations);
CommonUtil.initRemoteIpsecVpns(configurations);
IpsecVpnStatusAnswerElement answerElement = new IpsecVpnStatusAnswerElement();
for (Configuration c : configurations.values()) {
if (!includeNodes1.contains(c.getHostname())) {
continue;
}
for (IpsecVpn ipsecVpn : c.getIpsecVpns().values()) {
IpsecVpnInfo vpnInfo = analyzeIpsecVpn(ipsecVpn);
if ((vpnInfo.getRemoteEndpoint() == null || includeNodes2.contains(vpnInfo.getRemoteEndpoint().getHostname())) && vpnInfo.getProblems().stream().anyMatch(v -> question.matchesProblem(v))) {
answerElement.getIpsecVpns().add(vpnInfo);
}
}
}
return answerElement;
}
use of org.batfish.datamodel.questions.Question in project batfish by batfish.
the class QuestionPlugin method pluginInitialize.
@Override
protected final void pluginInitialize() {
Question question = createQuestion();
String questionName = question.getName();
String questionClassName = question.getClass().getCanonicalName();
switch(_pluginConsumer.getType()) {
case BATFISH:
{
IBatfish batfish = (IBatfish) _pluginConsumer;
batfish.registerAnswerer(questionName, questionClassName, this::createAnswerer);
break;
}
case CLIENT:
{
IClient client = (IClient) _pluginConsumer;
client.registerQuestion(questionName, this::createQuestion);
break;
}
default:
break;
}
}
use of org.batfish.datamodel.questions.Question in project batfish by batfish.
the class WorkMgrServiceTest method createTestQuestion.
private Question createTestQuestion(String name, String description) {
InstanceData instanceData = new InstanceData();
instanceData.setDescription(description);
instanceData.setInstanceName(name);
Question testQuestion = new Question() {
@Override
public String getName() {
return "test";
}
@Override
public boolean getDataPlane() {
return false;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Question other = (Question) obj;
return Objects.equal(getInstance().getInstanceName(), other.getInstance().getInstanceName()) && Objects.equal(getInstance().getDescription(), other.getInstance().getDescription());
}
@Override
public int hashCode() {
return java.util.Objects.hash(getInstance().getInstanceName(), getInstance().getDescription());
}
};
testQuestion.setInstance(instanceData);
return testQuestion;
}
Aggregations