Search in sources :

Example 1 with BatfishException

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

the class Client method initEnvironment.

private boolean initEnvironment(String paramsLine, FileWriter outWriter) {
    InitEnvironmentParams params = parseInitEnvironmentParams(paramsLine);
    String newEnvName;
    String paramsLocation = params.getSourcePath();
    String paramsName = params.getNewEnvironmentName();
    String paramsPrefix = params.getNewEnvironmentPrefix();
    String testrigName = params.getDoDelta() ? _currDeltaTestrig : _currTestrig;
    if (paramsName != null) {
        newEnvName = paramsName;
    } else if (paramsPrefix != null) {
        newEnvName = paramsPrefix + UUID.randomUUID();
    } else {
        newEnvName = DEFAULT_DELTA_ENV_PREFIX + UUID.randomUUID();
    }
    String paramsBaseEnv = params.getSourceEnvironmentName();
    String baseEnvName = paramsBaseEnv != null ? paramsBaseEnv : BfConsts.RELPATH_DEFAULT_ENVIRONMENT_NAME;
    String fileToSend;
    SortedSet<String> paramsNodeBlacklist = params.getNodeBlacklist();
    SortedSet<NodeInterfacePair> paramsInterfaceBlacklist = params.getInterfaceBlacklist();
    SortedSet<Edge> paramsEdgeBlacklist = params.getEdgeBlacklist();
    if (paramsLocation == null || Files.isDirectory(Paths.get(paramsLocation)) || !paramsNodeBlacklist.isEmpty() || !paramsInterfaceBlacklist.isEmpty() || !paramsEdgeBlacklist.isEmpty()) {
        Path tempFile = CommonUtil.createTempFile("batfish_client_tmp_env_", ".zip");
        fileToSend = tempFile.toString();
        if (paramsLocation != null && Files.isDirectory(Paths.get(paramsLocation)) && paramsNodeBlacklist.isEmpty() && paramsInterfaceBlacklist.isEmpty() && paramsEdgeBlacklist.isEmpty()) {
            ZipUtility.zipFiles(Paths.get(paramsLocation), tempFile);
        } else {
            Path tempDir = CommonUtil.createTempDirectory("batfish_client_tmp_env_");
            if (paramsLocation != null) {
                if (Files.isDirectory(Paths.get(paramsLocation))) {
                    CommonUtil.copyDirectory(Paths.get(paramsLocation), tempDir);
                } else if (Files.isRegularFile(Paths.get(paramsLocation))) {
                    UnzipUtility.unzip(Paths.get(paramsLocation), tempDir);
                } else {
                    throw new BatfishException("Invalid environment directory or zip: '" + paramsLocation + "'");
                }
            }
            if (!paramsNodeBlacklist.isEmpty()) {
                String nodeBlacklistText;
                try {
                    nodeBlacklistText = BatfishObjectMapper.writePrettyString(paramsNodeBlacklist);
                } catch (JsonProcessingException e) {
                    throw new BatfishException("Failed to write node blacklist to string", e);
                }
                Path nodeBlacklistFilePath = tempDir.resolve(BfConsts.RELPATH_NODE_BLACKLIST_FILE);
                CommonUtil.writeFile(nodeBlacklistFilePath, nodeBlacklistText);
            }
            if (!paramsInterfaceBlacklist.isEmpty()) {
                String interfaceBlacklistText;
                try {
                    interfaceBlacklistText = BatfishObjectMapper.writePrettyString(paramsInterfaceBlacklist);
                } catch (JsonProcessingException e) {
                    throw new BatfishException("Failed to write interface blacklist to string", e);
                }
                Path interfaceBlacklistFilePath = tempDir.resolve(BfConsts.RELPATH_INTERFACE_BLACKLIST_FILE);
                CommonUtil.writeFile(interfaceBlacklistFilePath, interfaceBlacklistText);
            }
            if (!paramsEdgeBlacklist.isEmpty()) {
                String edgeBlacklistText;
                try {
                    edgeBlacklistText = BatfishObjectMapper.writePrettyString(paramsEdgeBlacklist);
                } catch (JsonProcessingException e) {
                    throw new BatfishException("Failed to write edge blacklist to string", e);
                }
                Path edgeBlacklistFilePath = tempDir.resolve(BfConsts.RELPATH_EDGE_BLACKLIST_FILE);
                CommonUtil.writeFile(edgeBlacklistFilePath, edgeBlacklistText);
            }
            ZipUtility.zipFiles(tempDir, tempFile);
        }
    } else if (Files.isRegularFile(Paths.get(paramsLocation))) {
        fileToSend = paramsLocation;
    } else {
        throw new BatfishException("Invalid environment directory or zip: '" + paramsLocation + "'");
    }
    if (!uploadEnv(fileToSend, testrigName, newEnvName, baseEnvName)) {
        return false;
    }
    _currDeltaEnv = newEnvName;
    _currDeltaTestrig = _currTestrig;
    _logger.output("Active delta testrig->environment is set");
    _logger.infof("to %s->%s\n", _currDeltaTestrig, _currDeltaEnv);
    _logger.output("\n");
    WorkItem wItemProcessEnv = WorkItemBuilder.getWorkItemProcessEnvironment(_currContainerName, _currDeltaTestrig, _currDeltaEnv);
    if (!execute(wItemProcessEnv, outWriter)) {
        return false;
    }
    return true;
}
Also used : Path(java.nio.file.Path) BatfishException(org.batfish.common.BatfishException) NodeInterfacePair(org.batfish.datamodel.collections.NodeInterfacePair) InitEnvironmentParams(org.batfish.client.params.InitEnvironmentParams) Edge(org.batfish.datamodel.Edge) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) WorkItem(org.batfish.common.WorkItem)

Example 2 with BatfishException

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

the class Client method loadQuestionsFromServer.

/**
 * Loads questions from a JSON containing the questions
 *
 * @param questionTemplatesJson {@link JSONObject} with question key and question content Json
 * @return loadedQuestions {@link Multimap} containing loaded question names and content, empty if
 *     questionTemplatesJson is null
 * @throws BatfishException if loading of any of the questions is not successful, or if
 *     questionTemplatesJson cannot be deserialized
 */
static Multimap<String, String> loadQuestionsFromServer(JSONObject questionTemplatesJson) {
    try {
        Multimap<String, String> loadedQuestions = HashMultimap.create();
        if (questionTemplatesJson == null) {
            return loadedQuestions;
        }
        Map<String, String> questionsMap = BatfishObjectMapper.mapper().readValue(questionTemplatesJson.toString(), new TypeReference<Map<String, String>>() {
        });
        for (Entry<String, String> question : questionsMap.entrySet()) {
            JSONObject questionJSON = loadQuestionFromText(question.getValue(), question.getKey());
            loadedQuestions.put(getQuestionName(questionJSON, question.getKey()), questionJSON.toString());
        }
        return loadedQuestions;
    } catch (IOException e) {
        throw new BatfishException("Could not load remote questions", e);
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) JSONObject(org.codehaus.jettison.json.JSONObject) IOException(java.io.IOException) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 3 with BatfishException

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

the class Client method validateType.

/**
 * Validate the contents contained in json-encoded {@code value} matches the type required by
 * {@code variable}, and the length of input string meets the requirement of minimum length if
 * specified in {@code variable}. Call {@link Variable#getType()} on {@code variable} gives the
 * expected type.
 *
 * @throws BatfishException if the content encoded in input {@code value} does not satisfy the
 *     requirements specified in {@code variable}.
 */
static void validateType(JsonNode value, Variable variable) throws BatfishException {
    int minLength = variable.getMinLength() == null ? 0 : variable.getMinLength();
    if (value.isTextual() && value.textValue().length() < minLength) {
        throw new BatfishException(String.format("Must be at least %s characters in length", minLength));
    }
    Variable.Type expectedType = variable.getType();
    switch(expectedType) {
        case BOOLEAN:
            if (!value.isBoolean()) {
                throw new BatfishException(String.format("It is not a valid JSON %s value", expectedType.getName()));
            }
            break;
        case COMPARATOR:
            if (!(COMPARATORS.contains(value.textValue()))) {
                throw new BatfishException(String.format("It is not a known %s. Valid options are:" + " %s", expectedType.getName(), COMPARATORS));
            }
            break;
        case DOUBLE:
            if (!value.isDouble()) {
                throw new BatfishException(String.format("It is not a valid JSON %s value", expectedType.getName()));
            }
            break;
        case FLOAT:
            if (!value.isFloat()) {
                throw new BatfishException(String.format("It is not a valid JSON %s value", expectedType.getName()));
            }
            break;
        case INTEGER:
            if (!value.isInt()) {
                throw new BatfishException(String.format("It is not a valid JSON %s value", expectedType.getName()));
            }
            break;
        case LONG:
            if (!value.isLong()) {
                throw new BatfishException(String.format("It is not a valid JSON %s value", expectedType.getName()));
            }
            break;
        case IP:
            // TODO: Need to double check isInetAddress()
            if (!(value.isTextual())) {
                throw new BatfishException(String.format("A Batfish %s must be a JSON string", expectedType.getName()));
            }
            new Ip(value.textValue());
            break;
        case IP_PROTOCOL:
            if (!value.isTextual()) {
                throw new BatfishException(String.format("A Batfish %s must be a JSON string", expectedType.getName()));
            }
            try {
                IpProtocol.fromString(value.textValue());
            } catch (IllegalArgumentException e) {
                throw new BatfishException(String.format("Unknown %s string", expectedType.getName()));
            }
            break;
        case IP_WILDCARD:
            if (!value.isTextual()) {
                throw new BatfishException(String.format("A Batfish %s must be a JSON string", expectedType.getName()));
            }
            new IpWildcard(value.textValue());
            break;
        case JAVA_REGEX:
            if (!value.isTextual()) {
                throw new BatfishException(String.format("A Batfish %s must be a JSON string", expectedType.getName()));
            }
            try {
                Pattern.compile(value.textValue());
            } catch (PatternSyntaxException e) {
                throw new BatfishException("It is not a valid Java regular " + "expression", e);
            }
            break;
        case JSON_PATH_REGEX:
            if (!value.isTextual()) {
                throw new BatfishException(String.format("A Batfish %s must be a JSON string", expectedType.getName()));
            }
            validateJsonPathRegex(value.textValue());
            break;
        case PREFIX:
            if (!value.isTextual()) {
                throw new BatfishException(String.format("A Batfish %s must be a JSON string", expectedType.getName()));
            }
            Prefix.parse(value.textValue());
            break;
        case PREFIX_RANGE:
            if (!value.isTextual()) {
                throw new BatfishException(String.format("A Batfish %s must be a JSON string", expectedType.getName()));
            }
            PrefixRange.fromString(value.textValue());
            break;
        case QUESTION:
            // TODO: Implement
            break;
        case STRING:
            if (!value.isTextual()) {
                throw new BatfishException(String.format("A Batfish %s must be a JSON string", expectedType.getName()));
            }
            break;
        case SUBRANGE:
            if (!(value.isTextual() || value.isInt())) {
                throw new BatfishException(String.format("A Batfish %s must be a JSON string or " + "integer", expectedType.getName()));
            }
            Object actualValue = value.isTextual() ? value.textValue() : value.asInt();
            new SubRange(actualValue);
            break;
        case PROTOCOL:
            if (!value.isTextual()) {
                throw new BatfishException(String.format("A Batfish %s must be a JSON string", expectedType.getName()));
            }
            Protocol.fromString(value.textValue());
            break;
        case JSON_PATH:
            validateJsonPath(value);
            break;
        default:
            throw new BatfishException(String.format("Unsupported parameter type: %s", expectedType));
    }
}
Also used : IpWildcard(org.batfish.datamodel.IpWildcard) BatfishException(org.batfish.common.BatfishException) Variable(org.batfish.datamodel.questions.Question.InstanceData.Variable) Ip(org.batfish.datamodel.Ip) JSONObject(org.codehaus.jettison.json.JSONObject) SubRange(org.batfish.datamodel.SubRange) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 4 with BatfishException

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

the class Client method generateDatamodel.

private void generateDatamodel() {
    try {
        JsonSchemaGenerator schemaGenNew = new JsonSchemaGenerator(BatfishObjectMapper.mapper());
        JsonNode schemaNew = schemaGenNew.generateJsonSchema(Configuration.class);
        _logger.output(BatfishObjectMapper.writePrettyString(schemaNew));
    // Reflections reflections = new Reflections("org.batfish.datamodel");
    // Set<Class<? extends AnswerElement>> classes =
    // reflections.getSubTypesOf(AnswerElement.class);
    // _logger.outputf("Found %d classes that inherit %s\n",
    // classes.toArray().length, "AnswerElement");
    // 
    // File dmDir = Paths.get(_settings.getDatamodelDir()).toFile();
    // if (!dmDir.exists()) {
    // if (!dmDir.mkdirs()) {
    // throw new BatfishException("Could not create directory: " +
    // dmDir.getAbsolutePath());
    // }
    // }
    // 
    // for (Class c : classes) {
    // String className = c.getCanonicalName()
    // .replaceAll("org\\.batfish\\.datamodel\\.", "")
    // .replaceAll("\\.", "-")
    // + ".json";
    // _logger.outputf("%s --> %s\n", c, className);
    // Path file = Paths.get(dmDir.getAbsolutePath(), className);
    // try (PrintWriter out = new
    // PrintWriter(file.toAbsolutePath().toString())) {
    // ObjectMapper mapper = new BatfishObjectMapper();
    // JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
    // JsonNode schema = schemaGen.generateJsonSchema(c);
    // String schemaString = mapper.writeValueAsString(schema);
    // out.println(schemaString);
    // }
    // }
    // JsonSchemaGenerator schemaGenNew = new JsonSchemaGenerator(mapper,
    // true, JsonSchemaConfig.vanillaJsonSchemaDraft4());
    // JsonNode schemaNew =
    // schemaGenNew.generateJsonSchema(Configuration.class);
    // _logger.output(mapper.writeValueAsString(schemaNew));
    // _logger.output("\n");
    // JsonNode schemaNew2 =
    // schemaGenNew.generateJsonSchema(SchemaTest.Parent.class);
    // _logger.output(mapper.writeValueAsString(schemaNew2));
    } catch (Exception e) {
        _logger.errorf("Could not generate data model: %s", e.getMessage());
        e.printStackTrace();
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonSchemaGenerator(com.kjetland.jackson.jsonSchema.JsonSchemaGenerator) EndOfFileException(org.jline.reader.EndOfFileException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) UserInterruptException(org.jline.reader.UserInterruptException) PatternSyntaxException(java.util.regex.PatternSyntaxException) FileNotFoundException(java.io.FileNotFoundException) BatfishException(org.batfish.common.BatfishException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 5 with BatfishException

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

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