Search in sources :

Example 1 with CleanBatfishException

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

the class Batfish method run.

public Answer run() {
    newBatch("Begin job", 0);
    loadPlugins();
    boolean action = false;
    Answer answer = new Answer();
    if (_settings.getPrintSymmetricEdgePairs()) {
        printSymmetricEdgePairs();
        return answer;
    }
    if (_settings.getReport()) {
        answer.addAnswerElement(report());
        return answer;
    }
    if (_settings.getSynthesizeJsonTopology()) {
        writeJsonTopology();
        return answer;
    }
    if (_settings.getHistogram()) {
        histogram(_testrigSettings.getTestRigPath());
        return answer;
    }
    if (_settings.getFlatten()) {
        Path flattenSource = _testrigSettings.getTestRigPath();
        Path flattenDestination = _settings.getFlattenDestination();
        flatten(flattenSource, flattenDestination);
        return answer;
    }
    if (_settings.getGenerateStubs()) {
        String inputRole = _settings.getGenerateStubsInputRole();
        String interfaceDescriptionRegex = _settings.getGenerateStubsInterfaceDescriptionRegex();
        int stubAs = _settings.getGenerateStubsRemoteAs();
        generateStubs(inputRole, stubAs, interfaceDescriptionRegex);
        return answer;
    }
    if (_settings.getSerializeVendor()) {
        Path testRigPath = _testrigSettings.getTestRigPath();
        Path outputPath = _testrigSettings.getSerializeVendorPath();
        answer.append(serializeVendorConfigs(testRigPath, outputPath));
        action = true;
    }
    if (_settings.getSerializeIndependent()) {
        Path inputPath = _testrigSettings.getSerializeVendorPath();
        answer.append(serializeIndependentConfigs(inputPath));
        action = true;
    }
    if (_settings.getInitInfo()) {
        InitInfoAnswerElement initInfoAnswerElement = initInfo(true, false);
        // In this context we can remove parse trees because they will be returned in preceding answer
        // element. Note that parse trees are not removed when asking initInfo as its own question.
        initInfoAnswerElement.setParseTrees(Collections.emptySortedMap());
        answer.addAnswerElement(initInfoAnswerElement);
        action = true;
    }
    if (_settings.getCompileEnvironment()) {
        answer.append(compileEnvironmentConfigurations(_testrigSettings));
        action = true;
    }
    if (_settings.getAnswer()) {
        answer.append(answer());
        action = true;
    }
    if (_settings.getAnalyze()) {
        answer.append(analyze());
        action = true;
    }
    if (_settings.getDataPlane()) {
        answer.addAnswerElement(computeDataPlane(_settings.getDiffActive()));
        action = true;
    }
    if (_settings.getValidateEnvironment()) {
        answer.append(validateEnvironment());
        action = true;
    }
    if (!action) {
        throw new CleanBatfishException("No task performed! Run with -help flag to see usage\n");
    }
    return answer;
}
Also used : Path(java.nio.file.Path) Answer(org.batfish.datamodel.answers.Answer) InitInfoAnswerElement(org.batfish.datamodel.answers.InitInfoAnswerElement) CleanBatfishException(org.batfish.common.CleanBatfishException)

Example 2 with CleanBatfishException

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

the class Driver method runBatfish.

@SuppressWarnings("deprecation")
private static String runBatfish(final Settings settings) {
    final BatfishLogger logger = settings.getLogger();
    try {
        final Batfish batfish = new Batfish(settings, CACHED_COMPRESSED_TESTRIGS, CACHED_TESTRIGS, CACHED_COMPRESSED_DATA_PLANES, CACHED_DATA_PLANES, CACHED_ENVIRONMENT_BGP_TABLES, CACHED_ENVIRONMENT_ROUTING_TABLES, CACHED_FORWARDING_ANALYSES);
        @Nullable SpanContext runBatfishSpanContext = GlobalTracer.get().activeSpan() == null ? null : GlobalTracer.get().activeSpan().context();
        Thread thread = new Thread() {

            @Override
            public void run() {
                try (ActiveSpan runBatfishSpan = GlobalTracer.get().buildSpan("Run Batfish job in a new thread and get the answer").addReference(References.FOLLOWS_FROM, runBatfishSpanContext).startActive()) {
                    assert runBatfishSpan != null;
                    Answer answer = null;
                    try {
                        answer = batfish.run();
                        if (answer.getStatus() == null) {
                            answer.setStatus(AnswerStatus.SUCCESS);
                        }
                    } catch (CleanBatfishException e) {
                        String msg = "FATAL ERROR: " + e.getMessage();
                        logger.error(msg);
                        batfish.setTerminatingExceptionMessage(e.getClass().getName() + ": " + e.getMessage());
                        answer = Answer.failureAnswer(msg, null);
                    } catch (QuestionException e) {
                        String stackTrace = ExceptionUtils.getStackTrace(e);
                        logger.error(stackTrace);
                        batfish.setTerminatingExceptionMessage(e.getClass().getName() + ": " + e.getMessage());
                        answer = e.getAnswer();
                        answer.setStatus(AnswerStatus.FAILURE);
                    } catch (BatfishException e) {
                        String stackTrace = ExceptionUtils.getStackTrace(e);
                        logger.error(stackTrace);
                        batfish.setTerminatingExceptionMessage(e.getClass().getName() + ": " + e.getMessage());
                        answer = new Answer();
                        answer.setStatus(AnswerStatus.FAILURE);
                        answer.addAnswerElement(e.getBatfishStackTrace());
                    } catch (Throwable e) {
                        String stackTrace = ExceptionUtils.getStackTrace(e);
                        logger.error(stackTrace);
                        batfish.setTerminatingExceptionMessage(e.getClass().getName() + ": " + e.getMessage());
                        answer = new Answer();
                        answer.setStatus(AnswerStatus.FAILURE);
                        answer.addAnswerElement(new BatfishException("Batfish job failed", e).getBatfishStackTrace());
                    } finally {
                        try (ActiveSpan outputAnswerSpan = GlobalTracer.get().buildSpan("Outputting answer").startActive()) {
                            assert outputAnswerSpan != null;
                            if (settings.getAnswerJsonPath() != null) {
                                batfish.outputAnswerWithLog(answer);
                            }
                        }
                    }
                }
            }
        };
        thread.start();
        thread.join(settings.getMaxRuntimeMs());
        if (thread.isAlive()) {
            // this is deprecated but we should be safe since we don't have
            // locks and such
            // AF: This doesn't do what you think it does, esp. not in Java 8.
            // It needs to be replaced. TODO
            thread.stop();
            logger.error("Batfish worker took too long. Terminated.");
            batfish.setTerminatingExceptionMessage("Batfish worker took too long. Terminated.");
        }
        return batfish.getTerminatingExceptionMessage();
    } catch (Exception e) {
        String stackTrace = ExceptionUtils.getStackTrace(e);
        logger.error(stackTrace);
        return stackTrace;
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) CleanBatfishException(org.batfish.common.CleanBatfishException) SpanContext(io.opentracing.SpanContext) BatfishLogger(org.batfish.common.BatfishLogger) CleanBatfishException(org.batfish.common.CleanBatfishException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ProcessingException(javax.ws.rs.ProcessingException) QuestionException(org.batfish.common.QuestionException) BatfishException(org.batfish.common.BatfishException) IOException(java.io.IOException) CleanBatfishException(org.batfish.common.CleanBatfishException) Answer(org.batfish.datamodel.answers.Answer) ActiveSpan(io.opentracing.ActiveSpan) QuestionException(org.batfish.common.QuestionException) Nullable(javax.annotation.Nullable)

Example 3 with CleanBatfishException

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

the class Batfish method initTestrigSettings.

public static void initTestrigSettings(Settings settings) {
    String testrig = settings.getTestrig();
    String envName = settings.getEnvironmentName();
    Path containerDir = settings.getContainerDir();
    if (testrig != null) {
        applyBaseDir(settings.getBaseTestrigSettings(), containerDir, testrig, envName);
        String deltaTestrig = settings.getDeltaTestrig();
        String deltaEnvName = settings.getDeltaEnvironmentName();
        TestrigSettings deltaTestrigSettings = settings.getDeltaTestrigSettings();
        if (deltaTestrig != null && deltaEnvName == null) {
            deltaEnvName = envName;
            settings.setDeltaEnvironmentName(envName);
        } else if (deltaTestrig == null && deltaEnvName != null) {
            deltaTestrig = testrig;
            settings.setDeltaTestrig(testrig);
        }
        if (deltaTestrig != null) {
            applyBaseDir(deltaTestrigSettings, containerDir, deltaTestrig, deltaEnvName);
        }
        if (settings.getDiffActive()) {
            settings.setActiveTestrigSettings(settings.getDeltaTestrigSettings());
        } else {
            settings.setActiveTestrigSettings(settings.getBaseTestrigSettings());
        }
        initQuestionSettings(settings);
    } else if (containerDir != null) {
        throw new CleanBatfishException("Must supply argument to -" + BfConsts.ARG_TESTRIG);
    }
}
Also used : Path(java.nio.file.Path) CleanBatfishException(org.batfish.common.CleanBatfishException) TestrigSettings(org.batfish.config.Settings.TestrigSettings)

Example 4 with CleanBatfishException

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

the class Batfish method environmentExists.

private boolean environmentExists(TestrigSettings testrigSettings) {
    checkBaseDirExists();
    Path envPath = testrigSettings.getEnvironmentSettings().getEnvPath();
    if (envPath == null) {
        throw new CleanBatfishException("No environment specified for testrig: " + testrigSettings.getName());
    }
    return Files.exists(envPath);
}
Also used : Path(java.nio.file.Path) CleanBatfishException(org.batfish.common.CleanBatfishException)

Example 5 with CleanBatfishException

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

the class Batfish method checkQuestionsDirExists.

private void checkQuestionsDirExists() {
    checkBaseDirExists();
    Path questionsDir = _testrigSettings.getBasePath().resolve(BfConsts.RELPATH_QUESTIONS_DIR);
    if (!Files.exists(questionsDir)) {
        throw new CleanBatfishException("questions dir does not exist: \"" + questionsDir.getFileName() + "\"");
    }
}
Also used : Path(java.nio.file.Path) CleanBatfishException(org.batfish.common.CleanBatfishException)

Aggregations

CleanBatfishException (org.batfish.common.CleanBatfishException)5 Path (java.nio.file.Path)4 Answer (org.batfish.datamodel.answers.Answer)2 ActiveSpan (io.opentracing.ActiveSpan)1 SpanContext (io.opentracing.SpanContext)1 IOException (java.io.IOException)1 Nullable (javax.annotation.Nullable)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 ProcessingException (javax.ws.rs.ProcessingException)1 BatfishException (org.batfish.common.BatfishException)1 BatfishLogger (org.batfish.common.BatfishLogger)1 QuestionException (org.batfish.common.QuestionException)1 TestrigSettings (org.batfish.config.Settings.TestrigSettings)1 InitInfoAnswerElement (org.batfish.datamodel.answers.InitInfoAnswerElement)1