Search in sources :

Example 1 with BatfishLogger

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

the class ClientTest method testProcessCommandWithValidInput.

private void testProcessCommandWithValidInput(Command command, String[] parameters, String expected) throws Exception {
    Client client = new Client(new String[] { "-runmode", "gendatamodel" });
    File tempFile = _folder.newFile("writer");
    FileWriter writer = new FileWriter(tempFile);
    String[] args = ArrayUtils.addAll(new String[] { command.commandName() }, parameters);
    client._logger = new BatfishLogger("output", false);
    assertTrue(client.processCommand(args, writer));
    assertThat(client.getLogger().getHistory().toString(500), equalTo(expected));
    writer.close();
}
Also used : BatfishLogger(org.batfish.common.BatfishLogger) FileWriter(java.io.FileWriter) File(java.io.File)

Example 2 with BatfishLogger

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

the class ClientTest method testLoadQuestionsNames.

@Test
public void testLoadQuestionsNames() throws Exception {
    Client client = new Client(new String[] { "-runmode", "gendatamodel", "-prettyanswers", "false" });
    JSONObject testQuestion = new JSONObject();
    testQuestion.put("instance", new JSONObject().put("instanceName", "testQuestionName").put("description", "test question description"));
    Path questionJsonPath = _folder.newFile("testquestion.json").toPath();
    CommonUtil.writeFile(questionJsonPath, testQuestion.toString());
    client._logger = new BatfishLogger("output", false);
    client.processCommand(new String[] { LOAD_QUESTIONS.commandName(), questionJsonPath.getParent().toString() }, null);
    // Reading the answer written by load-questions
    Answer answerLoadQuestions = _mapper.readValue(client.getLogger().getHistory().toString(BatfishLogger.LEVEL_OUTPUT), Answer.class);
    LoadQuestionAnswerElement ae = (LoadQuestionAnswerElement) answerLoadQuestions.getAnswerElements().get(0);
    // Checking that question name in answer element matches instanceName in file
    assertEquals("testQuestionName", ae.getAdded().first());
}
Also used : Path(java.nio.file.Path) Answer(org.batfish.datamodel.answers.Answer) LoadQuestionAnswerElement(org.batfish.client.answer.LoadQuestionAnswerElement) JSONObject(org.codehaus.jettison.json.JSONObject) BatfishLogger(org.batfish.common.BatfishLogger) Test(org.junit.Test)

Example 3 with BatfishLogger

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

the class ClientTest method testDefaultCase.

@Test
public void testDefaultCase() throws Exception {
    Client client = new Client(new String[] { "-runmode", "gendatamodel" });
    File tempFile = _folder.newFile("writer");
    FileWriter writer = new FileWriter(tempFile);
    client._logger = new BatfishLogger("output", false);
    String[] args = new String[] { "non-exist command" };
    String expected = "Command failed: Not a valid command: \"non-exist command\"\n";
    assertFalse(client.processCommand(args, writer));
    assertThat(client.getLogger().getHistory().toString(500), equalTo(expected));
}
Also used : BatfishLogger(org.batfish.common.BatfishLogger) FileWriter(java.io.FileWriter) File(java.io.File) Test(org.junit.Test)

Example 4 with BatfishLogger

use of org.batfish.common.BatfishLogger 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 5 with BatfishLogger

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

the class WorkMgrServiceV2Test method initContainerEnvironment.

@Before
public void initContainerEnvironment() throws Exception {
    BatfishLogger logger = new BatfishLogger("debug", false);
    Settings settings = new Settings(new String[] {});
    Main.mainInit(new String[] { "-containerslocation", _folder.getRoot().toString() });
    Main.setLogger(logger);
    Main.initAuthorizer();
    Main.setWorkMgr(new WorkMgr(settings, logger));
}
Also used : BatfishLogger(org.batfish.common.BatfishLogger) Settings(org.batfish.coordinator.config.Settings) Before(org.junit.Before)

Aggregations

BatfishLogger (org.batfish.common.BatfishLogger)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Settings (org.batfish.config.Settings)6 Configuration (org.batfish.datamodel.Configuration)6 Test (org.junit.Test)6 Path (java.nio.file.Path)5 BatfishException (org.batfish.common.BatfishException)5 Topology (org.batfish.datamodel.Topology)5 Vrf (org.batfish.datamodel.Vrf)5 File (java.io.File)4 IOException (java.io.IOException)4 EnvironmentSettings (org.batfish.config.Settings.EnvironmentSettings)4 TestrigSettings (org.batfish.config.Settings.TestrigSettings)4 Ip (org.batfish.datamodel.Ip)4 BdpAnswerElement (org.batfish.datamodel.answers.BdpAnswerElement)4 ActiveSpan (io.opentracing.ActiveSpan)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Nullable (javax.annotation.Nullable)3 CleanBatfishException (org.batfish.common.CleanBatfishException)3