Search in sources :

Example 1 with Question

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;
}
Also used : BatfishException(org.batfish.common.BatfishException) Question(org.batfish.datamodel.questions.Question)

Example 2 with 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;
}
Also used : Answer(org.batfish.datamodel.answers.Answer) CleanBatfishException(org.batfish.common.CleanBatfishException) BatfishException(org.batfish.common.BatfishException) ActiveSpan(io.opentracing.ActiveSpan) AnswerElement(org.batfish.datamodel.answers.AnswerElement) InitInfoAnswerElement(org.batfish.datamodel.answers.InitInfoAnswerElement) RunAnalysisAnswerElement(org.batfish.datamodel.answers.RunAnalysisAnswerElement) DataPlaneAnswerElement(org.batfish.datamodel.answers.DataPlaneAnswerElement) ConvertConfigurationAnswerElement(org.batfish.datamodel.answers.ConvertConfigurationAnswerElement) AclLinesAnswerElement(org.batfish.datamodel.answers.AclLinesAnswerElement) ParseEnvironmentRoutingTablesAnswerElement(org.batfish.datamodel.answers.ParseEnvironmentRoutingTablesAnswerElement) NodAnswerElement(org.batfish.datamodel.answers.NodAnswerElement) NodFirstUnsatAnswerElement(org.batfish.datamodel.answers.NodFirstUnsatAnswerElement) InitStepAnswerElement(org.batfish.datamodel.answers.InitStepAnswerElement) NodSatAnswerElement(org.batfish.datamodel.answers.NodSatAnswerElement) ParseAnswerElement(org.batfish.datamodel.answers.ParseAnswerElement) FlattenVendorConfigurationAnswerElement(org.batfish.datamodel.answers.FlattenVendorConfigurationAnswerElement) ValidateEnvironmentAnswerElement(org.batfish.datamodel.answers.ValidateEnvironmentAnswerElement) ParseEnvironmentBgpTablesAnswerElement(org.batfish.datamodel.answers.ParseEnvironmentBgpTablesAnswerElement) ParseVendorConfigurationAnswerElement(org.batfish.datamodel.answers.ParseVendorConfigurationAnswerElement) ReportAnswerElement(org.batfish.datamodel.answers.ReportAnswerElement) HeaderQuestion(org.batfish.datamodel.questions.smt.HeaderQuestion) RoleQuestion(org.batfish.datamodel.questions.smt.RoleQuestion) HeaderLocationQuestion(org.batfish.datamodel.questions.smt.HeaderLocationQuestion) Question(org.batfish.datamodel.questions.Question) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) CleanBatfishException(org.batfish.common.CleanBatfishException) JSONException(org.codehaus.jettison.json.JSONException) BatfishException(org.batfish.common.BatfishException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) InvalidReachabilitySettingsException(org.batfish.datamodel.questions.InvalidReachabilitySettingsException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 3 with Question

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;
}
Also used : IpsecVpn(org.batfish.datamodel.IpsecVpn) Answerer(org.batfish.common.Answerer) IpsecVpn(org.batfish.datamodel.IpsecVpn) SortedSet(java.util.SortedSet) CommonUtil(org.batfish.common.util.CommonUtil) Set(java.util.Set) TreeSet(java.util.TreeSet) IBatfish(org.batfish.common.plugin.IBatfish) Problem(org.batfish.question.ipsecvpnstatus.IpsecVpnInfo.Problem) Question(org.batfish.datamodel.questions.Question) Map(java.util.Map) Configuration(org.batfish.datamodel.Configuration) AnswerElement(org.batfish.datamodel.answers.AnswerElement) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Configuration(org.batfish.datamodel.Configuration)

Example 4 with Question

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;
    }
}
Also used : Question(org.batfish.datamodel.questions.Question) IClient(org.batfish.common.plugin.IClient) IBatfish(org.batfish.common.plugin.IBatfish)

Example 5 with Question

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;
}
Also used : InstanceData(org.batfish.datamodel.questions.Question.InstanceData) Question(org.batfish.datamodel.questions.Question) JSONObject(org.codehaus.jettison.json.JSONObject)

Aggregations

Question (org.batfish.datamodel.questions.Question)10 BatfishException (org.batfish.common.BatfishException)6 JSONObject (org.codehaus.jettison.json.JSONObject)5 IOException (java.io.IOException)4 Path (java.nio.file.Path)2 Map (java.util.Map)2 IBatfish (org.batfish.common.plugin.IBatfish)2 AnswerElement (org.batfish.datamodel.answers.AnswerElement)2 JSONException (org.codehaus.jettison.json.JSONException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ActiveSpan (io.opentracing.ActiveSpan)1 FileNotFoundException (java.io.FileNotFoundException)1 AccessControlException (java.security.AccessControlException)1 Set (java.util.Set)1 SortedSet (java.util.SortedSet)1 TreeSet (java.util.TreeSet)1 ExecutionException (java.util.concurrent.ExecutionException)1