use of org.batfish.datamodel.questions.Question.InstanceData in project batfish by batfish.
the class Client method answer.
private boolean answer(String questionTemplateName, String paramsLine, boolean isDelta, FileWriter outWriter) {
String questionContentUnmodified = _bfq.get(questionTemplateName.toLowerCase());
if (questionContentUnmodified == null) {
throw new BatfishException("Invalid question template name: '" + questionTemplateName + "'");
}
Map<String, JsonNode> parameters = parseParams(paramsLine);
JSONObject questionJson;
try {
questionJson = new JSONObject(questionContentUnmodified);
} catch (JSONException e) {
throw new BatfishException("Question content is not valid JSON", e);
}
String questionName = DEFAULT_QUESTION_PREFIX + "_" + UUID.randomUUID();
if (parameters.containsKey("questionName")) {
questionName = parameters.get("questionName").asText();
parameters.remove("questionName");
}
JSONObject instanceJson;
try {
instanceJson = questionJson.getJSONObject(BfConsts.PROP_INSTANCE);
instanceJson.put(BfConsts.PROP_INSTANCE_NAME, questionName);
} catch (JSONException e) {
throw new BatfishException("Question is missing instance data", e);
}
String instanceDataStr = instanceJson.toString();
InstanceData instanceData;
try {
instanceData = BatfishObjectMapper.mapper().readValue(instanceDataStr, new TypeReference<InstanceData>() {
});
} catch (IOException e) {
throw new BatfishException("Invalid instance data (JSON)", e);
}
Map<String, Variable> variables = instanceData.getVariables();
validateAndSet(parameters, variables);
checkVariableState(variables);
String modifiedInstanceDataStr;
try {
modifiedInstanceDataStr = BatfishObjectMapper.writePrettyString(instanceData);
JSONObject modifiedInstanceData = new JSONObject(modifiedInstanceDataStr);
questionJson.put(BfConsts.PROP_INSTANCE, modifiedInstanceData);
} catch (JSONException | JsonProcessingException e) {
throw new BatfishException("Could not process modified instance data", e);
}
String modifiedQuestionStr = questionJson.toString();
boolean questionJsonDifferential = false;
// check whether question is valid modulo instance data
try {
questionJsonDifferential = questionJson.has(BfConsts.PROP_DIFFERENTIAL) && questionJson.getBoolean(BfConsts.PROP_DIFFERENTIAL);
} catch (JSONException e) {
throw new BatfishException("Could not find whether question is explicitly differential", e);
}
if (questionJsonDifferential && (_currDeltaEnv == null || _currDeltaTestrig == null)) {
_logger.output(DIFF_NOT_READY_MSG);
return false;
}
Path questionFile = createTempFile(BfConsts.RELPATH_QUESTION_FILE, modifiedQuestionStr);
questionFile.toFile().deleteOnExit();
// upload the question
boolean resultUpload = _workHelper.uploadQuestion(_currContainerName, isDelta ? _currDeltaTestrig : _currTestrig, questionName, questionFile.toAbsolutePath().toString());
if (!resultUpload) {
return false;
}
_logger.debug("Uploaded question. Answering now.\n");
// delete the temporary params file
if (questionFile != null) {
CommonUtil.deleteIfExists(questionFile);
}
// answer the question
WorkItem wItemAs = WorkItemBuilder.getWorkItemAnswerQuestion(questionName, _currContainerName, _currTestrig, _currEnv, _currDeltaTestrig, _currDeltaEnv, questionJsonDifferential, isDelta);
return execute(wItemAs, outWriter);
}
use of org.batfish.datamodel.questions.Question.InstanceData in project batfish by batfish.
the class Client method loadQuestionFromText.
/**
* Loads question from a JSON
*
* @param questionText Question JSON Text
* @param questionSource JSON key of question or file path of JSON
* @return question loaded as a {@link JSONObject}
* @throws BatfishException if question does not have instanceName or question cannot be parsed
*/
static JSONObject loadQuestionFromText(String questionText, String questionSource) {
try {
JSONObject questionObj = new JSONObject(questionText);
if (questionObj.has(BfConsts.PROP_INSTANCE) && !questionObj.isNull(BfConsts.PROP_INSTANCE)) {
JSONObject instanceDataObj = questionObj.getJSONObject(BfConsts.PROP_INSTANCE);
String instanceDataStr = instanceDataObj.toString();
InstanceData instanceData = BatfishObjectMapper.mapper().readValue(instanceDataStr, new TypeReference<InstanceData>() {
});
validateInstanceData(instanceData);
return questionObj;
} else {
throw new BatfishException(String.format("Question in %s has no instance data", questionSource));
}
} catch (JSONException | IOException e) {
throw new BatfishException("Failed to process question", e);
}
}
use of org.batfish.datamodel.questions.Question.InstanceData 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;
}
use of org.batfish.datamodel.questions.Question.InstanceData in project batfish by batfish.
the class InferPoliciesQuestionPlugin method makeQuestionWithHints.
private static JsonPathQuestion makeQuestionWithHints(AbstractRoleConsistencyQuestion roleQ) {
JsonPathQuestion ret = new JsonPathQuestion();
ret.setInnerQuestion(roleQ);
JsonPathQuery path = new JsonPathQuery("$.answers[?(@.outliers)]", true);
path.setDisplayHints(getDisplayHintsForQuestion(roleQ));
ret.setPaths(Collections.singletonList(path));
InstanceData instance = new InstanceData();
instance.setInstanceName(getInstanceNameForQuestion(roleQ));
ret.setInstance(instance);
return ret;
}
Aggregations