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;
}
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);
}
}
Aggregations