Search in sources :

Example 1 with InstanceData

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);
}
Also used : Path(java.nio.file.Path) BatfishException(org.batfish.common.BatfishException) Variable(org.batfish.datamodel.questions.Question.InstanceData.Variable) JSONException(org.codehaus.jettison.json.JSONException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) WorkItem(org.batfish.common.WorkItem) JSONObject(org.codehaus.jettison.json.JSONObject) InstanceData(org.batfish.datamodel.questions.Question.InstanceData) TypeReference(com.fasterxml.jackson.core.type.TypeReference) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with InstanceData

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

Example 3 with InstanceData

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

Example 4 with InstanceData

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;
}
Also used : InstanceData(org.batfish.datamodel.questions.Question.InstanceData) JsonPathQuery(org.batfish.question.jsonpath.JsonPathQuery) JsonPathQuestion(org.batfish.question.jsonpath.JsonPathQuestionPlugin.JsonPathQuestion)

Aggregations

InstanceData (org.batfish.datamodel.questions.Question.InstanceData)4 JSONObject (org.codehaus.jettison.json.JSONObject)3 IOException (java.io.IOException)2 BatfishException (org.batfish.common.BatfishException)2 JSONException (org.codehaus.jettison.json.JSONException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Path (java.nio.file.Path)1 WorkItem (org.batfish.common.WorkItem)1 Question (org.batfish.datamodel.questions.Question)1 Variable (org.batfish.datamodel.questions.Question.InstanceData.Variable)1 JsonPathQuery (org.batfish.question.jsonpath.JsonPathQuery)1 JsonPathQuestion (org.batfish.question.jsonpath.JsonPathQuestionPlugin.JsonPathQuestion)1