use of org.apache.metron.indexing.dao.search.GetRequest in project metron by apache.
the class ElasticsearchMetaAlertIntegrationTest method shouldCreateMetaAlert.
@Test
public void shouldCreateMetaAlert() throws Exception {
// Load alerts
List<Map<String, Object>> alerts = buildAlerts(3);
elasticsearchAdd(alerts, INDEX, SENSOR_NAME);
// Verify load was successful
findCreatedDocs(Arrays.asList(new GetRequest("message_0", SENSOR_NAME), new GetRequest("message_1", SENSOR_NAME), new GetRequest("message_2", SENSOR_NAME)));
{
MetaAlertCreateRequest metaAlertCreateRequest = new MetaAlertCreateRequest() {
{
setAlerts(new ArrayList<GetRequest>() {
{
add(new GetRequest("message_1", SENSOR_NAME));
add(new GetRequest("message_2", SENSOR_NAME, INDEX));
}
});
setGroups(Collections.singletonList("group"));
}
};
MetaAlertCreateResponse metaAlertCreateResponse = metaDao.createMetaAlert(metaAlertCreateRequest);
{
// Verify metaAlert was created
findCreatedDoc(metaAlertCreateResponse.getGuid(), MetaAlertDao.METAALERT_TYPE);
}
{
// Verify alert 0 was not updated with metaalert field
Document alert = metaDao.getLatest("message_0", SENSOR_NAME);
Assert.assertEquals(4, alert.getDocument().size());
Assert.assertNull(alert.getDocument().get(METAALERT_FIELD));
}
{
// Verify alert 1 was properly updated with metaalert field
Document alert = metaDao.getLatest("message_1", SENSOR_NAME);
Assert.assertEquals(5, alert.getDocument().size());
Assert.assertEquals(1, ((List) alert.getDocument().get(METAALERT_FIELD)).size());
Assert.assertEquals(metaAlertCreateResponse.getGuid(), ((List) alert.getDocument().get(METAALERT_FIELD)).get(0));
}
{
// Verify alert 2 was properly updated with metaalert field
Document alert = metaDao.getLatest("message_2", SENSOR_NAME);
Assert.assertEquals(5, alert.getDocument().size());
Assert.assertEquals(1, ((List) alert.getDocument().get(METAALERT_FIELD)).size());
Assert.assertEquals(metaAlertCreateResponse.getGuid(), ((List) alert.getDocument().get(METAALERT_FIELD)).get(0));
}
}
}
use of org.apache.metron.indexing.dao.search.GetRequest in project metron by apache.
the class HBaseDao method getAllLatest.
@Override
public Iterable<Document> getAllLatest(List<GetRequest> getRequests) throws IOException {
List<Get> gets = new ArrayList<>();
for (GetRequest getRequest : getRequests) {
gets.add(buildGet(getRequest));
}
Result[] results = getTableInterface().get(gets);
List<Document> allLatest = new ArrayList<>();
for (Result result : results) {
Document d = getDocumentFromResult(result);
if (d != null) {
allLatest.add(d);
}
}
return allLatest;
}
use of org.apache.metron.indexing.dao.search.GetRequest in project metron by apache.
the class HBaseDaoIntegrationTest method shouldGetAllLatest.
@Test
public void shouldGetAllLatest() throws Exception {
// Load alerts
List<Document> alerts = buildAlerts(15);
alerts.stream().collect(Collectors.toMap(Document::getGuid, document -> Optional.empty()));
Map<Document, Optional<String>> updates = alerts.stream().collect(Collectors.toMap(document -> document, document -> Optional.empty()));
hbaseDao.batchUpdate(updates);
int expectedCount = 12;
List<GetRequest> getRequests = new ArrayList<>();
for (int i = 1; i < expectedCount + 1; i++) {
getRequests.add(new GetRequest("message_" + i, SENSOR_TYPE));
}
Iterator<Document> results = hbaseDao.getAllLatest(getRequests).iterator();
for (int i = 0; i < expectedCount; i++) {
Document expectedDocument = alerts.get(i + 1);
Document actualDocument = results.next();
Assert.assertEquals(expectedDocument, actualDocument);
}
Assert.assertFalse("Result size should be 12 but was greater", results.hasNext());
}
use of org.apache.metron.indexing.dao.search.GetRequest in project metron by apache.
the class MetaAlertControllerIntegrationTest method shouldUpdateStatus.
@Test
public void shouldUpdateStatus() throws Exception {
MetaAlertCreateRequest metaAlertCreateRequest = new MetaAlertCreateRequest();
metaAlertCreateRequest.setGroups(Arrays.asList("group_one", "group_two"));
metaAlertCreateRequest.setAlerts(new ArrayList<GetRequest>() {
{
add(new GetRequest("bro_1", "bro", "bro_index_2017.01.01.01"));
add(new GetRequest("snort_2", "snort", "snort_index_2017.01.01.01"));
}
});
MetaAlertCreateResponse metaAlertCreateResponse = metaAlertService.create(metaAlertCreateRequest);
ResultActions result = this.mockMvc.perform(post(metaalertUrl + "/update/status/" + metaAlertCreateResponse.getGuid() + "/inactive").with(httpBasic(user, password)).with(csrf()).contentType(MediaType.parseMediaType("application/json;charset=UTF-8")));
result.andExpect(status().isOk()).andExpect(content().string("true"));
result = this.mockMvc.perform(post(metaalertUrl + "/update/status/" + metaAlertCreateResponse.getGuid() + "/inactive").with(httpBasic(user, password)).with(csrf()).contentType(MediaType.parseMediaType("application/json;charset=UTF-8")));
result.andExpect(status().isOk()).andExpect(content().string("false"));
}
use of org.apache.metron.indexing.dao.search.GetRequest in project metron by apache.
the class ElasticsearchDao method getAllLatest.
@Override
public Iterable<Document> getAllLatest(final List<GetRequest> getRequests) throws IOException {
Collection<String> guids = new HashSet<>();
Collection<String> sensorTypes = new HashSet<>();
for (GetRequest getRequest : getRequests) {
guids.add(getRequest.getGuid());
sensorTypes.add(getRequest.getSensorType());
}
List<Document> documents = searchByGuids(guids, sensorTypes, hit -> {
Long ts = 0L;
String doc = hit.getSourceAsString();
String sourceType = Iterables.getFirst(Splitter.on("_doc").split(hit.getType()), null);
try {
return Optional.of(new Document(doc, hit.getId(), sourceType, ts));
} catch (IOException e) {
throw new IllegalStateException("Unable to retrieve latest: " + e.getMessage(), e);
}
});
return documents;
}
Aggregations