Search in sources :

Example 1 with InitEnvironmentParams

use of org.batfish.client.params.InitEnvironmentParams 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 InitEnvironmentParams

use of org.batfish.client.params.InitEnvironmentParams in project batfish by batfish.

the class Client method parseInitEnvironmentParams.

static InitEnvironmentParams parseInitEnvironmentParams(String paramsLine) {
    String jsonParamsStr = "{ " + paramsLine + " }";
    InitEnvironmentParams parameters;
    try {
        parameters = BatfishObjectMapper.mapper().readValue(new JSONObject(jsonParamsStr).toString(), new TypeReference<InitEnvironmentParams>() {
        });
        return parameters;
    } catch (JSONException | IOException e) {
        throw new BatfishException("Failed to parse parameters. (Are all key-value pairs separated by commas? Are all " + "values valid JSON?)", e);
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) JSONObject(org.codehaus.jettison.json.JSONObject) JSONException(org.codehaus.jettison.json.JSONException) InitEnvironmentParams(org.batfish.client.params.InitEnvironmentParams) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException)

Aggregations

InitEnvironmentParams (org.batfish.client.params.InitEnvironmentParams)2 BatfishException (org.batfish.common.BatfishException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 WorkItem (org.batfish.common.WorkItem)1 Edge (org.batfish.datamodel.Edge)1 NodeInterfacePair (org.batfish.datamodel.collections.NodeInterfacePair)1 JSONException (org.codehaus.jettison.json.JSONException)1 JSONObject (org.codehaus.jettison.json.JSONObject)1