use of org.batfish.common.WorkItem 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.common.WorkItem in project batfish by batfish.
the class Client method initTestrig.
private boolean initTestrig(@Nullable FileWriter outWriter, List<String> options, List<String> parameters, boolean delta) {
Command command = delta ? Command.INIT_DELTA_TESTRIG : Command.INIT_TESTRIG;
if (!isValidArgument(options, parameters, 1, 1, 2, command)) {
return false;
}
boolean autoAnalyze = false;
if (options.size() == 1) {
if (options.get(0).equals("-autoanalyze")) {
autoAnalyze = true;
} else {
_logger.errorf("Unknown option: %s\n", options.get(0));
printUsage(command);
return false;
}
}
String testrigLocation = parameters.get(0);
String testrigName = (parameters.size() > 1) ? parameters.get(1) : DEFAULT_TESTRIG_PREFIX + UUID.randomUUID();
// initialize the container if it hasn't been init'd before
if (!isSetContainer(false)) {
_currContainerName = _workHelper.initContainer(null, DEFAULT_CONTAINER_PREFIX);
if (_currContainerName == null) {
_logger.errorf("Could not init container\n");
return false;
}
_logger.output("Init'ed and set active container");
_logger.infof(" to %s\n", _currContainerName);
_logger.output("\n");
}
if (!uploadTestrig(testrigLocation, testrigName, autoAnalyze)) {
unsetTestrig(delta);
return false;
}
_logger.output("Uploaded testrig.\n");
if (!autoAnalyze) {
_logger.output("Parsing now.\n");
WorkItem wItemParse = WorkItemBuilder.getWorkItemParse(_currContainerName, testrigName);
if (!execute(wItemParse, outWriter)) {
unsetTestrig(delta);
return false;
}
}
if (!delta) {
_currTestrig = testrigName;
_currEnv = DEFAULT_ENV_NAME;
_logger.infof("Base testrig is now %s\n", _currTestrig);
} else {
_currDeltaTestrig = testrigName;
_currDeltaEnv = DEFAULT_ENV_NAME;
_logger.infof("Delta testrig is now %s\n", _currDeltaTestrig);
}
return true;
}
use of org.batfish.common.WorkItem 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);
}
use of org.batfish.common.WorkItem in project batfish by batfish.
the class WorkItemBuilder method getWorkItemProcessEnvironment.
public static WorkItem getWorkItemProcessEnvironment(String containerName, String testrigName, String envName) {
WorkItem wItem = new WorkItem(containerName, testrigName);
wItem.addRequestParam(BfConsts.COMMAND_COMPILE_DIFF_ENVIRONMENT, "");
wItem.addRequestParam(BfConsts.COMMAND_VALIDATE_ENVIRONMENT, "");
wItem.addRequestParam(BfConsts.ARG_ENVIRONMENT_NAME, envName);
return wItem;
}
use of org.batfish.common.WorkItem in project batfish by batfish.
the class WorkItemBuilder method getWorkItemGenerateDeltaDataPlane.
public static WorkItem getWorkItemGenerateDeltaDataPlane(String containerName, String testrigName, String envName, String deltaTestrigName, String deltaEnvName) {
WorkItem wItem = new WorkItem(containerName, testrigName);
wItem.addRequestParam(BfConsts.COMMAND_DUMP_DP, "");
wItem.addRequestParam(BfConsts.ARG_ENVIRONMENT_NAME, envName);
wItem.addRequestParam(BfConsts.ARG_DELTA_TESTRIG, deltaTestrigName);
wItem.addRequestParam(BfConsts.ARG_DELTA_ENVIRONMENT_NAME, deltaEnvName);
wItem.addRequestParam(BfConsts.ARG_DIFF_ACTIVE, "");
return wItem;
}
Aggregations