Search in sources :

Example 1 with PcapPages

use of org.apache.metron.pcap.PcapPages in project metron by apache.

the class PcapJobTest method setup.

@BeforeEach
public void setup() throws IOException {
    MockitoAnnotations.initMocks(this);
    basePath = new Path("basepath");
    baseOutPath = new Path("outpath");
    startTime = 100;
    endTime = 200;
    numReducers = 5;
    numRecordsPerFile = 5;
    fixedFields = new HashMap<>();
    fixedFields.put("ip_src_addr", "192.168.1.1");
    hadoopConfig = new Configuration();
    fileSystem = FileSystem.get(hadoopConfig);
    finalOutputPath = new Path("finaloutpath");
    when(jobId.toString()).thenReturn(jobIdVal);
    when(mrStatus.getJobID()).thenReturn(jobId);
    when(mrJob.getJobID()).thenReturn(jobId);
    pageableResult = new PcapPages();
    timer = new TestTimer();
    // handles setting the file name prefix under the hood
    config = new FixedPcapConfig(clock -> "clockprefix");
    PcapOptions.HADOOP_CONF.put(config, hadoopConfig);
    PcapOptions.FILESYSTEM.put(config, FileSystem.get(hadoopConfig));
    PcapOptions.BASE_PATH.put(config, basePath);
    PcapOptions.BASE_INTERIM_RESULT_PATH.put(config, baseOutPath);
    PcapOptions.START_TIME_NS.put(config, startTime);
    PcapOptions.END_TIME_NS.put(config, endTime);
    PcapOptions.NUM_REDUCERS.put(config, numReducers);
    PcapOptions.FIELDS.put(config, fixedFields);
    PcapOptions.FILTER_IMPL.put(config, new FixedPcapFilter.Configurator());
    PcapOptions.NUM_RECORDS_PER_FILE.put(config, numRecordsPerFile);
    PcapOptions.FINAL_OUTPUT_PATH.put(config, finalOutputPath);
    testJob = new TestJob<>(mrJob);
    testJob.setStatusInterval(1);
    testJob.setCompleteCheckInterval(1);
    testJob.setTimer(timer);
}
Also used : Path(org.apache.hadoop.fs.Path) FixedPcapConfig(org.apache.metron.pcap.config.FixedPcapConfig) BeforeEach(org.junit.jupiter.api.BeforeEach) java.util(java.util) FixedPcapFilter(org.apache.metron.pcap.filter.fixed.FixedPcapFilter) FileSystem(org.apache.hadoop.fs.FileSystem) Pageable(org.apache.metron.job.Pageable) Mock(org.mockito.Mock) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) PcapFilterConfigurator(org.apache.metron.pcap.filter.PcapFilterConfigurator) LongWritable(org.apache.hadoop.io.LongWritable) TimestampConverters(org.apache.metron.common.utils.timestamp.TimestampConverters) MockitoAnnotations(org.mockito.MockitoAnnotations) State(org.apache.metron.job.JobStatus.State) PcapOptions(org.apache.metron.pcap.config.PcapOptions) Long.toUnsignedString(java.lang.Long.toUnsignedString) Configuration(org.apache.hadoop.conf.Configuration) Path(org.apache.hadoop.fs.Path) BytesWritable(org.apache.hadoop.io.BytesWritable) Statusable(org.apache.metron.job.Statusable) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) JobID(org.apache.hadoop.mapreduce.JobID) IOException(java.io.IOException) Finalizer(org.apache.metron.job.Finalizer) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) FixedPcapConfig(org.apache.metron.pcap.config.FixedPcapConfig) Job(org.apache.hadoop.mapreduce.Job) JobStatus(org.apache.metron.job.JobStatus) PcapPages(org.apache.metron.pcap.PcapPages) Configuration(org.apache.hadoop.conf.Configuration) PcapPages(org.apache.metron.pcap.PcapPages) FixedPcapFilter(org.apache.metron.pcap.filter.fixed.FixedPcapFilter) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with PcapPages

use of org.apache.metron.pcap.PcapPages in project metron by apache.

the class PcapJobTest method job_succeeds_synchronously.

@Test
public void job_succeeds_synchronously() throws Exception {
    pageableResult = new PcapPages(Arrays.asList(new Path("1.txt"), new Path("2.txt"), new Path("3.txt")));
    when(finalizer.finalizeJob(any())).thenReturn(pageableResult);
    when(mrJob.isComplete()).thenReturn(true);
    when(mrStatus.getState()).thenReturn(org.apache.hadoop.mapreduce.JobStatus.State.SUCCEEDED);
    when(mrJob.getStatus()).thenReturn(mrStatus);
    Statusable<Path> statusable = testJob.submit(finalizer, config);
    timer.updateJobStatus();
    Pageable<Path> results = statusable.get();
    assertThat(results.getSize(), equalTo(3));
    JobStatus status = statusable.getStatus();
    assertThat(status.getState(), equalTo(State.SUCCEEDED));
    assertThat(status.getPercentComplete(), equalTo(100.0));
    assertThat(status.getJobId(), equalTo(jobIdVal));
}
Also used : Path(org.apache.hadoop.fs.Path) JobStatus(org.apache.metron.job.JobStatus) PcapPages(org.apache.metron.pcap.PcapPages) Test(org.junit.jupiter.api.Test)

Example 3 with PcapPages

use of org.apache.metron.pcap.PcapPages in project metron by apache.

the class PcapJobTest method handles_null_values_with_defaults.

@Test
public void handles_null_values_with_defaults() throws Exception {
    PcapOptions.START_TIME_NS.put(config, null);
    PcapOptions.END_TIME_NS.put(config, null);
    PcapOptions.NUM_REDUCERS.put(config, null);
    PcapOptions.NUM_RECORDS_PER_FILE.put(config, null);
    pageableResult = new PcapPages(Arrays.asList(new Path("1.txt"), new Path("2.txt"), new Path("3.txt")));
    when(finalizer.finalizeJob(any())).thenReturn(pageableResult);
    when(mrJob.isComplete()).thenReturn(true);
    when(mrStatus.getState()).thenReturn(org.apache.hadoop.mapreduce.JobStatus.State.SUCCEEDED);
    when(mrJob.getStatus()).thenReturn(mrStatus);
    Statusable<Path> statusable = testJob.submit(finalizer, config);
    timer.updateJobStatus();
    Pageable<Path> results = statusable.get();
    assertThat(results.getSize(), equalTo(3));
    JobStatus status = statusable.getStatus();
    assertThat(status.getState(), equalTo(State.SUCCEEDED));
    assertThat(status.getPercentComplete(), equalTo(100.0));
    assertThat(status.getJobId(), equalTo(jobIdVal));
}
Also used : Path(org.apache.hadoop.fs.Path) JobStatus(org.apache.metron.job.JobStatus) PcapPages(org.apache.metron.pcap.PcapPages) Test(org.junit.jupiter.api.Test)

Example 4 with PcapPages

use of org.apache.metron.pcap.PcapPages in project metron by apache.

the class PcapControllerIntegrationTest method testRawDownload.

@Test
public void testRawDownload() throws Exception {
    String pcapFileContents = "pcap file contents";
    FileUtils.write(new File("./target/pcapFile"), pcapFileContents, "UTF8");
    MockPcapJob mockPcapJob = (MockPcapJob) wac.getBean("mockPcapJob");
    mockPcapJob.setStatus(new JobStatus().withJobId("jobId").withState(JobStatus.State.RUNNING));
    this.mockMvc.perform(post(pcapUrl + "/fixed").with(httpBasic(user, password)).with(csrf()).contentType(MediaType.parseMediaType("application/json;charset=UTF-8")).content(fixedJson)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))).andExpect(jsonPath("$.jobId").value("jobId")).andExpect(jsonPath("$.jobStatus").value("RUNNING"));
    Pageable<Path> pageable = new PcapPages(Arrays.asList(new Path("./target/pcapFile")));
    mockPcapJob.setIsDone(true);
    mockPcapJob.setPageable(pageable);
    this.mockMvc.perform(get(pcapUrl + "/jobId/raw?page=1").with(httpBasic(user, password))).andExpect(status().isOk()).andExpect(header().string("Content-Disposition", "attachment; filename=\"pcap_jobId_1.pcap\"")).andExpect(header().string("Content-Length", Integer.toString(pcapFileContents.length()))).andExpect(content().contentType(MediaType.parseMediaType("application/octet-stream"))).andExpect(content().bytes(pcapFileContents.getBytes(StandardCharsets.UTF_8)));
    this.mockMvc.perform(get(pcapUrl + "/jobId/raw?page=1&fileName=pcapFile.pcap").with(httpBasic(user, password))).andExpect(status().isOk()).andExpect(header().string("Content-Disposition", "attachment; filename=\"pcapFile.pcap\"")).andExpect(header().string("Content-Length", Integer.toString(pcapFileContents.length()))).andExpect(content().contentType(MediaType.parseMediaType("application/octet-stream"))).andExpect(content().bytes(pcapFileContents.getBytes(StandardCharsets.UTF_8)));
    this.mockMvc.perform(get(pcapUrl + "/jobId/raw?page=2").with(httpBasic(user, password))).andExpect(status().isNotFound());
}
Also used : JobStatus(org.apache.metron.job.JobStatus) Path(org.apache.hadoop.fs.Path) MockPcapJob(org.apache.metron.rest.mock.MockPcapJob) PcapPages(org.apache.metron.pcap.PcapPages) File(java.io.File) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with PcapPages

use of org.apache.metron.pcap.PcapPages in project metron by apache.

the class PcapFinalizer method finalizeJob.

@Override
public Pageable<Path> finalizeJob(Map<String, Object> config) throws JobException {
    Configuration hadoopConfig = PcapOptions.HADOOP_CONF.get(config, Configuration.class);
    int recPerFile = PcapOptions.NUM_RECORDS_PER_FILE.getOrDefault(config, Integer.class, NUM_RECORDS_PER_FILE_DEFAULT);
    Path interimResultPath = PcapOptions.INTERIM_RESULT_PATH.get(config, PcapOptions.STRING_TO_PATH, Path.class);
    FileSystem fs = PcapOptions.FILESYSTEM.get(config, FileSystem.class);
    int parallelism = getNumThreads(PcapOptions.FINALIZER_THREADPOOL_SIZE.get(config, String.class));
    LOG.info("Finalizer running with parallelism set to " + parallelism);
    SequenceFileIterable interimResults = null;
    try {
        interimResults = readInterimResults(interimResultPath, hadoopConfig, fs);
    } catch (IOException e) {
        throw new JobException("Unable to read interim job results while finalizing", e);
    }
    List<Path> outFiles = new ArrayList<>();
    try {
        Iterable<List<byte[]>> partitions = Iterables.partition(interimResults, recPerFile);
        Map<Path, List<byte[]>> toWrite = new HashMap<>();
        int part = 1;
        if (partitions.iterator().hasNext()) {
            for (List<byte[]> data : partitions) {
                Path outputPath = getOutputPath(config, part++);
                toWrite.put(outputPath, data);
            }
            outFiles = writeParallel(hadoopConfig, toWrite, parallelism);
        } else {
            LOG.info("No results returned.");
        }
    } catch (IOException e) {
        throw new JobException("Failed to finalize results", e);
    } finally {
        try {
            interimResults.cleanup();
        } catch (IOException e) {
            LOG.warn("Unable to cleanup files in HDFS", e);
        }
    }
    LOG.info("Done finalizing results");
    return new PcapPages(outFiles);
}
Also used : Path(org.apache.hadoop.fs.Path) Configuration(org.apache.hadoop.conf.Configuration) HashMap(java.util.HashMap) PcapPages(org.apache.metron.pcap.PcapPages) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JobException(org.apache.metron.job.JobException) FileSystem(org.apache.hadoop.fs.FileSystem) SequenceFileIterable(org.apache.metron.common.hadoop.SequenceFileIterable) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Path (org.apache.hadoop.fs.Path)8 PcapPages (org.apache.metron.pcap.PcapPages)8 JobStatus (org.apache.metron.job.JobStatus)6 Test (org.junit.jupiter.api.Test)6 MockPcapJob (org.apache.metron.rest.mock.MockPcapJob)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 IOException (java.io.IOException)2 Configuration (org.apache.hadoop.conf.Configuration)2 FileSystem (org.apache.hadoop.fs.FileSystem)2 JobException (org.apache.metron.job.JobException)2 File (java.io.File)1 Long.toUnsignedString (java.lang.Long.toUnsignedString)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 BytesWritable (org.apache.hadoop.io.BytesWritable)1