Search in sources :

Example 6 with PcapPages

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

the class PcapControllerIntegrationTest method testGetPdml.

@Test
public void testGetPdml() throws Exception {
    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")));
    mockPcapJob.setIsDone(true);
    mockPcapJob.setPageable(pageable);
    this.mockMvc.perform(get(pcapUrl + "/jobId/pdml?page=1").with(httpBasic(user, password))).andExpect(status().isOk()).andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))).andExpect(jsonPath("$.version").value("0")).andExpect(jsonPath("$.creator").value("wireshark/2.6.1")).andExpect(jsonPath("$.time").value("Thu Jun 28 14:14:38 2018")).andExpect(jsonPath("$.captureFile").value("/tmp/pcap-data-201806272004-289365c53112438ca55ea047e13a12a5+0001.pcap")).andExpect(jsonPath("$.packets[0].protos[0].name").value("geninfo")).andExpect(jsonPath("$.packets[0].protos[0].fields[0].name").value("num")).andExpect(jsonPath("$.packets[0].protos[1].name").value("ip")).andExpect(jsonPath("$.packets[0].protos[1].fields[0].name").value("ip.addr"));
    this.mockMvc.perform(get(pcapUrl + "/jobId/pdml?page=0").with(httpBasic(user, password))).andExpect(status().isNotFound());
    this.mockMvc.perform(get(pcapUrl + "/jobId/pdml?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) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 7 with PcapPages

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

the class PcapControllerIntegrationTest method testGetStatus.

@Test
public void testGetStatus() throws Exception {
    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"));
    mockPcapJob.setStatus(new JobStatus().withJobId("jobId").withState(JobStatus.State.SUCCEEDED));
    Pageable<Path> pageable = new PcapPages(Arrays.asList(new Path("path1"), new Path("path1")));
    mockPcapJob.setIsDone(true);
    mockPcapJob.setPageable(pageable);
    this.mockMvc.perform(get(pcapUrl + "/jobId").with(httpBasic(user, password))).andExpect(status().isOk()).andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))).andExpect(jsonPath("$.jobStatus").value("SUCCEEDED")).andExpect(jsonPath("$.pageTotal").value(2));
    mockPcapJob.setStatus(new JobStatus().withJobId("jobId").withState(JobStatus.State.FINALIZING));
    this.mockMvc.perform(get(pcapUrl + "/jobId").with(httpBasic(user, password))).andExpect(status().isOk()).andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))).andExpect(jsonPath("$.jobStatus").value("FINALIZING"));
    mockPcapJob.setStatus(new JobStatus().withJobId("jobId").withState(JobStatus.State.FAILED));
    this.mockMvc.perform(get(pcapUrl + "/jobId").with(httpBasic(user, password))).andExpect(status().isOk()).andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))).andExpect(jsonPath("$.jobStatus").value("FAILED"));
    mockPcapJob.setStatus(new JobStatus().withJobId("jobId").withState(JobStatus.State.KILLED));
    this.mockMvc.perform(get(pcapUrl + "/jobId").with(httpBasic(user, password))).andExpect(status().isOk()).andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))).andExpect(jsonPath("$.jobStatus").value("KILLED"));
    this.mockMvc.perform(get(pcapUrl + "/someJobId").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) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 8 with PcapPages

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

the class PcapJob method query.

/**
 * Run query asynchronously.
 */
public Statusable<Path> query(Optional<String> jobName, Path basePath, Path baseInterimResultPath, long beginNS, long endNS, int numReducers, T fields, Configuration conf, FileSystem fs, PcapFilterConfigurator<T> filterImpl) throws IOException, ClassNotFoundException, InterruptedException {
    String outputDirName = outputDirFormatter.format(beginNS, endNS, filterImpl.queryToString(fields));
    if (LOG.isDebugEnabled()) {
        DateFormat format = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.LONG, SimpleDateFormat.LONG);
        String from = format.format(new Date(Long.divideUnsigned(beginNS, 1000000)));
        String to = format.format(new Date(Long.divideUnsigned(endNS, 1000000)));
        LOG.debug("Executing query {} on timerange from {} to {}", () -> filterImpl.queryToString(fields), () -> from, () -> to);
    }
    Path interimResultPath = new Path(baseInterimResultPath, outputDirName);
    PcapOptions.INTERIM_RESULT_PATH.put(configuration, interimResultPath);
    mrJob = createJob(jobName, basePath, interimResultPath, beginNS, endNS, numReducers, fields, conf, fs, filterImpl);
    if (mrJob == null) {
        LOG.info("No files to process with specified date range.");
        try {
            setFinalResults(input -> new PcapPages(), configuration);
            jobStatus.withState(State.SUCCEEDED).withDescription("No results in specified date range.").withPercentComplete(100.0);
        } catch (JobException e) {
            // This should not cause an error as we simply set results to an empty result set.
            jobStatus.withState(State.FAILED).withDescription("Unable to finalize empty job.").withFailureException(e);
        }
        return this;
    }
    synchronized (this) {
        // this block synchronized for proper variable visibility across threads once the status timer
        // is started. mrJob and jobStatus need to be synchronized so that their references and internal
        // state are made available to the timer thread. The references to these variables above need
        // not be synchronized because the job will exit when only 1 thread will have had to use them.
        mrJob.submit();
        jobStatus.withState(State.SUBMITTED).withDescription("Job submitted").withJobId(mrJob.getJobID().toString());
    }
    startJobStatusTimerThread(statusInterval);
    return this;
}
Also used : Path(org.apache.hadoop.fs.Path) JobException(org.apache.metron.job.JobException) PcapPages(org.apache.metron.pcap.PcapPages) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

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