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