use of org.batfish.config.Settings in project batfish by batfish.
the class Driver method runBatfishThroughService.
public static List<String> runBatfishThroughService(final String taskId, String[] args) {
final Settings settings;
try {
settings = new Settings(_mainSettings);
settings.setRunMode(RunMode.WORKER);
settings.parseCommandLine(args);
// assign taskId for status updates, termination requests
settings.setTaskId(taskId);
} catch (Exception e) {
return Arrays.asList("failure", "Initialization failed: " + ExceptionUtils.getStackTrace(e));
}
try {
Batfish.initTestrigSettings(settings);
} catch (Exception e) {
return Arrays.asList("failure", "Failed while applying auto basedir. (All arguments are supplied?): " + e.getMessage());
}
if (settings.canExecute()) {
if (claimIdle()) {
try {
final BatfishLogger jobLogger = new BatfishLogger(settings.getLogLevel(), settings.getTimestamp(), settings.getLogFile(), settings.getLogTee(), false);
settings.setLogger(jobLogger);
final Task task = new Task(args);
logTask(taskId, task);
@Nullable SpanContext runTaskSpanContext = GlobalTracer.get().activeSpan() == null ? null : GlobalTracer.get().activeSpan().context();
// run batfish on a new thread and set idle to true when done
Thread thread = new Thread() {
@Override
public void run() {
try (ActiveSpan runBatfishSpan = GlobalTracer.get().buildSpan("Initialize Batfish in a new thread").addReference(References.FOLLOWS_FROM, runTaskSpanContext).startActive()) {
// avoid unused warning
assert runBatfishSpan != null;
task.setStatus(TaskStatus.InProgress);
String errMsg = runBatfish(settings);
if (errMsg == null) {
task.setStatus(TaskStatus.TerminatedNormally);
} else {
task.setStatus(TaskStatus.TerminatedAbnormally);
task.setErrMessage(errMsg);
}
task.setTerminated(new Date());
jobLogger.close();
makeIdle();
}
}
};
thread.start();
return Arrays.asList(BfConsts.SVC_SUCCESS_KEY, "running now");
} catch (Exception e) {
_mainLogger.error("Exception while running task: " + e.getMessage());
makeIdle();
return Arrays.asList(BfConsts.SVC_FAILURE_KEY, e.getMessage());
}
} else {
return Arrays.asList(BfConsts.SVC_FAILURE_KEY, "Not idle");
}
} else {
return Arrays.asList(BfConsts.SVC_FAILURE_KEY, "Non-executable command");
}
}
use of org.batfish.config.Settings in project batfish by batfish.
the class BatfishJobExecutorTest method testHandleJobResultSuccess.
@Test
public void testHandleJobResultSuccess() {
Settings settings = new Settings();
// initializing executor
BatfishJobExecutor executor = BatfishJobExecutor.getBatfishJobExecutor(settings, _logger);
executor.initializeJobsStats(Lists.newArrayList(new BfTestJob(settings, "result1")), TEST_EXECUTOR_DESC);
// Simulating finishing of a job and handling the result
// initiating a separate logger from the Executor logger
BatfishLogger jobLogger = new BatfishLogger(BatfishLogger.LEVELSTR_INFO, false);
BfTestResult bfTestResult = new BfTestResult(TEST_ELAPSED_TIME, jobLogger.getHistory(), "result");
Set<String> output = new HashSet<>();
List<BatfishException> failureCauses = new ArrayList<>();
BfTestAnswerElement ae = new BfTestAnswerElement();
executor.markJobCompleted();
executor.handleJobResult(bfTestResult, output, ae, failureCauses, false);
// checking the log of the executor for the job finished
assertEquals(_logger.getHistory().toString(BatfishLogger.LEVEL_INFO), executor.getSuccessMessage(bfTestResult));
}
use of org.batfish.config.Settings in project batfish by batfish.
the class NodJobTest method getNodJob.
private NodJob getNodJob(HeaderSpace headerSpace, Boolean srcNatted) {
StandardReachabilityQuerySynthesizer querySynthesizer = StandardReachabilityQuerySynthesizer.builder().setActions(ImmutableSet.of(ForwardingAction.ACCEPT)).setFinalNodes(ImmutableSet.of(_dstNode.getHostname())).setHeaderSpace(headerSpace).setIngressNodeVrfs(ImmutableMap.of(_srcNode.getHostname(), ImmutableSet.of(_srcVrf.getName()))).setSrcNatted(srcNatted).setTransitNodes(ImmutableSet.of()).setNonTransitNodes(ImmutableSet.of()).build();
SortedSet<Pair<String, String>> ingressNodes = ImmutableSortedSet.of(new Pair<>(_srcNode.getHostname(), _srcVrf.getName()));
return new NodJob(new Settings(), _synthesizer, querySynthesizer, ingressNodes, "tag", false);
}
use of org.batfish.config.Settings in project batfish by batfish.
the class BatfishTestUtils method getBatfishFromTestrigText.
/**
* Get a new Batfish instance with given configurations, tempFolder should be present for
* non-empty configurations
*
* @param testrigText Structure containing names and content of testrig input files
* @param tempFolder Temporary folder to be used to files required for Batfish
* @return Batfish instance pointing at new testrig comprising testrigText
*/
public static Batfish getBatfishFromTestrigText(TestrigText testrigText, TemporaryFolder tempFolder) throws IOException {
Map<String, String> awsText = testrigText.getAwsText();
Map<String, String> bgpTablesText = testrigText.getBgpTablesText();
Map<String, String> configurationText = testrigText.getConfigurationText();
Map<String, String> hostsText = testrigText.getHostsText();
Map<String, String> iptablesFilesText = testrigText.getIptablesFilesText();
Map<String, String> routingTablesText = testrigText.getRoutingTablesText();
Settings settings = new Settings(new String[] {});
settings.setLogger(new BatfishLogger("debug", false));
settings.setDisableUnrecognized(true);
settings.setHaltOnConvertError(true);
settings.setHaltOnParseError(true);
settings.setThrowOnLexerError(true);
settings.setThrowOnParserError(true);
settings.setVerboseParse(true);
Path containerDir = tempFolder.newFolder().toPath();
settings.setContainerDir(containerDir);
settings.setTestrig("tempTestrig");
settings.setEnvironmentName("tempEnvironment");
settings.setDataplaneEngineName(BdpDataPlanePlugin.PLUGIN_NAME);
Batfish.initTestrigSettings(settings);
Path testrigPath = settings.getBaseTestrigSettings().getTestRigPath();
settings.setActiveTestrigSettings(settings.getBaseTestrigSettings());
EnvironmentSettings envSettings = settings.getBaseTestrigSettings().getEnvironmentSettings();
envSettings.getEnvPath().toFile().mkdirs();
writeTemporaryTestrigFiles(configurationText, testrigPath.resolve(BfConsts.RELPATH_CONFIGURATIONS_DIR));
writeTemporaryTestrigFiles(awsText, testrigPath.resolve(BfConsts.RELPATH_AWS_CONFIGS_DIR));
writeTemporaryTestrigFiles(bgpTablesText, envSettings.getEnvironmentBgpTablesPath());
writeTemporaryTestrigFiles(hostsText, testrigPath.resolve(BfConsts.RELPATH_HOST_CONFIGS_DIR));
writeTemporaryTestrigFiles(iptablesFilesText, testrigPath.resolve("iptables"));
writeTemporaryTestrigFiles(routingTablesText, envSettings.getEnvironmentRoutingTablesPath());
Batfish batfish = new Batfish(settings, makeTestrigCache(), makeTestrigCache(), makeDataPlaneCache(), makeDataPlaneCache(), makeEnvBgpCache(), makeEnvRouteCache(), makeForwardingAnalysisCache());
return batfish;
}
use of org.batfish.config.Settings in project batfish by batfish.
the class BatfishTestUtils method initBatfish.
private static Batfish initBatfish(SortedMap<String, Configuration> configurations, @Nonnull TemporaryFolder tempFolder) throws IOException {
Settings settings = new Settings(new String[] {});
settings.setLogger(new BatfishLogger("debug", false));
final Cache<Snapshot, SortedMap<String, Configuration>> testrigs = makeTestrigCache();
final Cache<Snapshot, SortedMap<String, Configuration>> compressedTestrigs = makeTestrigCache();
Path containerDir = tempFolder.newFolder().toPath();
settings.setContainerDir(containerDir);
if (!configurations.isEmpty()) {
settings.setTestrig("tempTestrig");
settings.setEnvironmentName("tempEnvironment");
Batfish.initTestrigSettings(settings);
settings.getBaseTestrigSettings().getEnvironmentSettings().getEnvPath().toFile().mkdirs();
testrigs.put(new Snapshot("tempTestrig", "tempEnvironment"), configurations);
settings.setActiveTestrigSettings(settings.getBaseTestrigSettings());
}
Batfish batfish = new Batfish(settings, compressedTestrigs, testrigs, makeDataPlaneCache(), makeDataPlaneCache(), makeEnvBgpCache(), makeEnvRouteCache(), makeForwardingAnalysisCache());
if (!configurations.isEmpty()) {
Batfish.serializeAsJson(settings.getBaseTestrigSettings().getEnvironmentSettings().getSerializedTopologyPath(), batfish.computeEnvironmentTopology(configurations), "environment topology");
}
return batfish;
}
Aggregations