use of org.batfish.client.answer.LoadQuestionAnswerElement in project batfish by batfish.
the class ClientTest method testMergeQuestions1.
@Test
public void testMergeQuestions1() {
Multimap<String, String> sourceMap = HashMultimap.create();
sourceMap.put("sourceQuestion", "sourcequestionvalue");
sourceMap.put("destinationQuestion", "destinationquestionvalue");
Map<String, String> destMap = new HashMap<>();
destMap.put("destinationquestion", "destinationquestionvalue");
LoadQuestionAnswerElement ae = new LoadQuestionAnswerElement();
Client.mergeQuestions(sourceMap, destMap, ae);
Map<String, String> expectedMap = new HashMap<>();
expectedMap.put("sourcequestion", "sourcequestionvalue");
expectedMap.put("destinationquestion", "destinationquestionvalue");
// Test the merging populates ae and destinationquestion get replaced
assertThat(expectedMap.entrySet(), equalTo(destMap.entrySet()));
assertEquals(Collections.singleton("destinationQuestion"), ae.getReplaced());
assertEquals(Collections.singleton("sourceQuestion"), ae.getAdded());
assertEquals(2, ae.getNumLoaded());
}
use of org.batfish.client.answer.LoadQuestionAnswerElement in project batfish by batfish.
the class ClientTest method testMergeQuestions2.
@Test
public void testMergeQuestions2() {
Multimap<String, String> sourceMap = HashMultimap.create();
sourceMap.put("sourceQuestion", "sourcequestionvalue1");
sourceMap.put("sourceQuestion", "sourcequestionvalue2");
Map<String, String> destMap = new HashMap<>();
LoadQuestionAnswerElement ae = new LoadQuestionAnswerElement();
Client.mergeQuestions(sourceMap, destMap, ae);
// Test the merging populates ae and sourcequestion get replaced
assertEquals(Collections.singleton("sourceQuestion"), ae.getReplaced());
assertEquals(Collections.singleton("sourceQuestion"), ae.getAdded());
assertEquals(2, ae.getNumLoaded());
}
use of org.batfish.client.answer.LoadQuestionAnswerElement in project batfish by batfish.
the class ClientTest method testLoadQuestionsNames.
@Test
public void testLoadQuestionsNames() throws Exception {
Client client = new Client(new String[] { "-runmode", "gendatamodel", "-prettyanswers", "false" });
JSONObject testQuestion = new JSONObject();
testQuestion.put("instance", new JSONObject().put("instanceName", "testQuestionName").put("description", "test question description"));
Path questionJsonPath = _folder.newFile("testquestion.json").toPath();
CommonUtil.writeFile(questionJsonPath, testQuestion.toString());
client._logger = new BatfishLogger("output", false);
client.processCommand(new String[] { LOAD_QUESTIONS.commandName(), questionJsonPath.getParent().toString() }, null);
// Reading the answer written by load-questions
Answer answerLoadQuestions = _mapper.readValue(client.getLogger().getHistory().toString(BatfishLogger.LEVEL_OUTPUT), Answer.class);
LoadQuestionAnswerElement ae = (LoadQuestionAnswerElement) answerLoadQuestions.getAnswerElements().get(0);
// Checking that question name in answer element matches instanceName in file
assertEquals("testQuestionName", ae.getAdded().first());
}
use of org.batfish.client.answer.LoadQuestionAnswerElement 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.client.answer.LoadQuestionAnswerElement 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