Search in sources :

Example 21 with Answer

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

the class Client method loadQuestions.

private boolean loadQuestions(@Nullable FileWriter outWriter, List<String> options, List<String> parameters, Map<String, String> bfq) {
    // checking the arguments and options
    if (!isValidArgument(options, parameters, 1, 0, 1, Command.LOAD_QUESTIONS)) {
        return false;
    }
    boolean loadRemote = false;
    if (options.size() == 1) {
        if (options.get(0).equals("-loadremote")) {
            loadRemote = true;
        } else {
            _logger.errorf("Unknown option: %s\n", options.get(0));
            printUsage(Command.LOAD_QUESTIONS);
            return false;
        }
    }
    // init answer and answer element
    Answer answer = new Answer();
    LoadQuestionAnswerElement ae = new LoadQuestionAnswerElement();
    answer.addAnswerElement(ae);
    // try to load remote questions if no local disk path is passed or loadremote is forced
    if ((parameters.isEmpty() || loadRemote) && _workHelper != null) {
        JSONObject remoteQuestionsJson = _workHelper.getQuestionTemplates();
        Multimap<String, String> remoteQuestions = loadQuestionsFromServer(remoteQuestionsJson);
        // merging remote questions to bfq and updating answer element
        mergeQuestions(remoteQuestions, bfq, ae);
    }
    // try to load local questions whenever local disk path is provided
    if (!parameters.isEmpty()) {
        Multimap<String, String> localQuestions = loadQuestionsFromDir(parameters.get(0));
        // merging local questions to bfq and updating answer element
        mergeQuestions(localQuestions, bfq, ae);
    }
    // outputting the final answer
    String answerStringToPrint;
    if (outWriter == null && _settings.getPrettyPrintAnswers()) {
        answerStringToPrint = answer.prettyPrint();
    } else {
        try {
            answerStringToPrint = BatfishObjectMapper.writePrettyString(answer);
        } catch (JsonProcessingException e) {
            throw new BatfishException("Could not write answer element as string", e);
        }
    }
    logOutput(outWriter, answerStringToPrint);
    return true;
}
Also used : Answer(org.batfish.datamodel.answers.Answer) BatfishException(org.batfish.common.BatfishException) LoadQuestionAnswerElement(org.batfish.client.answer.LoadQuestionAnswerElement) JSONObject(org.codehaus.jettison.json.JSONObject) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 22 with Answer

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

the class Client method initOrAddAnalysis.

private boolean initOrAddAnalysis(@Nullable FileWriter outWriter, List<String> options, List<String> parameters, boolean newAnalysis) {
    Command command = newAnalysis ? Command.INIT_ANALYSIS : Command.ADD_ANALYSIS_QUESTIONS;
    if (!isValidArgument(options, parameters, 0, 2, 2, command)) {
        return false;
    }
    if (!isSetContainer(true)) {
        return false;
    }
    String analysisName = parameters.get(0);
    String questionsPathStr = parameters.get(1);
    Map<String, String> questionMap = new TreeMap<>();
    LoadQuestionAnswerElement ae = new LoadQuestionAnswerElement();
    try {
        // loading questions for the analysis
        Multimap<String, String> analysisQuestions = loadQuestionsFromDir(questionsPathStr);
        Answer answer = new Answer();
        answer.addAnswerElement(ae);
        mergeQuestions(analysisQuestions, questionMap, ae);
        String answerStringToPrint;
        if (_settings.getPrettyPrintAnswers()) {
            answerStringToPrint = answer.prettyPrint();
        } else {
            try {
                answerStringToPrint = BatfishObjectMapper.writePrettyString(answer);
            } catch (JsonProcessingException e) {
                throw new BatfishException("Could not write answer element as string", e);
            }
        }
        _logger.output(answerStringToPrint);
    } catch (BatfishException e) {
        // failure in loading the questions results in failure of loading of analysis
        return false;
    }
    String analysisJsonString = "{}";
    try {
        JSONObject jObject = new JSONObject();
        for (String qName : questionMap.keySet()) {
            jObject.put(qName, new JSONObject(questionMap.get(qName)));
        }
        analysisJsonString = jObject.toString(1);
    } catch (JSONException e) {
        throw new BatfishException("Failed to get JSONObject for analysis", e);
    }
    Path analysisFile = createTempFile("analysis", analysisJsonString);
    boolean result = _workHelper.configureAnalysis(_currContainerName, newAnalysis, analysisName, analysisFile.toAbsolutePath().toString(), null);
    if (analysisFile != null) {
        CommonUtil.deleteIfExists(analysisFile);
    }
    logOutput(outWriter, "Output of configuring analysis " + analysisName + ": " + result + "\n");
    return result;
}
Also used : Path(java.nio.file.Path) BatfishException(org.batfish.common.BatfishException) JSONException(org.codehaus.jettison.json.JSONException) TreeMap(java.util.TreeMap) Answer(org.batfish.datamodel.answers.Answer) LoadQuestionAnswerElement(org.batfish.client.answer.LoadQuestionAnswerElement) JSONObject(org.codehaus.jettison.json.JSONObject) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

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