Search in sources :

Example 11 with BatfishException

use of org.batfish.common.BatfishException 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 12 with BatfishException

use of org.batfish.common.BatfishException in project batfish by batfish.

the class Client method validateInstanceData.

private static void validateInstanceData(InstanceData instanceData) {
    String description = instanceData.getDescription();
    String q = "Question: '" + instanceData.getInstanceName() + "'";
    if (description == null || description.length() == 0) {
        throw new BatfishException(q + " is missing question description");
    }
    for (Entry<String, Variable> e : instanceData.getVariables().entrySet()) {
        String variableName = e.getKey();
        Variable variable = e.getValue();
        String v = "Variable: '" + variableName + "' in " + q;
        String variableDescription = variable.getDescription();
        if (variableDescription == null || variableDescription.length() == 0) {
            throw new BatfishException(v + " is missing description");
        }
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) Variable(org.batfish.datamodel.questions.Question.InstanceData.Variable)

Example 13 with BatfishException

use of org.batfish.common.BatfishException in project batfish by batfish.

the class QuestionHelper method getQuestion.

public static Question getQuestion(String questionTypeStr, Map<String, Supplier<Question>> questions) {
    Supplier<Question> supplier = questions.get(questionTypeStr);
    if (supplier == null) {
        throw new BatfishException("No question found of type: " + questionTypeStr + '.');
    }
    Question question = supplier.get();
    return question;
}
Also used : BatfishException(org.batfish.common.BatfishException) Question(org.batfish.datamodel.questions.Question)

Example 14 with BatfishException

use of org.batfish.common.BatfishException in project batfish by batfish.

the class CommonUtil method checkJsonEqual.

public static boolean checkJsonEqual(Object a, Object b) {
    try {
        String aString = BatfishObjectMapper.writePrettyString(a);
        String bString = BatfishObjectMapper.writePrettyString(b);
        JSONAssert.assertEquals(aString, bString, false);
        return true;
    } catch (Exception e) {
        throw new BatfishException("JSON equality check failed", e);
    } catch (AssertionError err) {
        return false;
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) DirectoryIteratorException(java.nio.file.DirectoryIteratorException) IOException(java.io.IOException) NoSuchFileException(java.nio.file.NoSuchFileException) PatternSyntaxException(java.util.regex.PatternSyntaxException) FileNotFoundException(java.io.FileNotFoundException) BatfishException(org.batfish.common.BatfishException) MalformedURLException(java.net.MalformedURLException)

Example 15 with BatfishException

use of org.batfish.common.BatfishException in project batfish by batfish.

the class CommonUtil method getConfig.

public static org.apache.commons.configuration2.Configuration getConfig(String overridePropertyName, String defaultPropertyFilename, Class<?> defaultPropertyLocatorClass) {
    String overriddenPath = System.getProperty(overridePropertyName);
    URL propertiesUrl;
    if (overriddenPath != null) {
        // The user provided an override, so look up that configuration instead.
        try {
            propertiesUrl = new URL(new URL("file://"), overriddenPath);
        } catch (MalformedURLException e) {
            throw new BatfishException("Error treating " + overriddenPath + " as a path to a properties file", e);
        }
    } else {
        // Find the default properties file.
        propertiesUrl = defaultPropertyLocatorClass.getClassLoader().getResource(defaultPropertyFilename);
    }
    try {
        return new Configurations().properties(propertiesUrl);
    } catch (Exception e) {
        throw new BatfishException("Error loading configuration from " + overriddenPath, e);
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) MalformedURLException(java.net.MalformedURLException) Configurations(org.apache.commons.configuration2.builder.fluent.Configurations) URL(java.net.URL) DirectoryIteratorException(java.nio.file.DirectoryIteratorException) IOException(java.io.IOException) NoSuchFileException(java.nio.file.NoSuchFileException) PatternSyntaxException(java.util.regex.PatternSyntaxException) FileNotFoundException(java.io.FileNotFoundException) BatfishException(org.batfish.common.BatfishException) MalformedURLException(java.net.MalformedURLException)

Aggregations

BatfishException (org.batfish.common.BatfishException)264 IOException (java.io.IOException)61 Path (java.nio.file.Path)54 CleanBatfishException (org.batfish.common.CleanBatfishException)35 RedFlagBatfishException (org.batfish.common.RedFlagBatfishException)34 TreeMap (java.util.TreeMap)31 ArrayList (java.util.ArrayList)30 JSONException (org.codehaus.jettison.json.JSONException)30 Ip (org.batfish.datamodel.Ip)25 JSONObject (org.codehaus.jettison.json.JSONObject)25 Configuration (org.batfish.datamodel.Configuration)24 Map (java.util.Map)23 Prefix (org.batfish.datamodel.Prefix)22 HashMap (java.util.HashMap)20 HashSet (java.util.HashSet)20 TreeSet (java.util.TreeSet)20 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)18 Test (org.junit.Test)18 Set (java.util.Set)17 SortedMap (java.util.SortedMap)17