Search in sources :

Example 1 with PipelineRuleEntity

use of org.graylog2.contentpacks.model.entities.PipelineRuleEntity in project graylog2-server by Graylog2.

the class PipelineRuleFacade method findExisting.

private Optional<NativeEntity<RuleDao>> findExisting(EntityV1 entity, Map<String, ValueReference> parameters) {
    final PipelineRuleEntity ruleEntity = objectMapper.convertValue(entity.data(), PipelineRuleEntity.class);
    final String title = ruleEntity.title().asString(parameters);
    final String source = ruleEntity.source().asString(parameters);
    try {
        final RuleDao ruleDao = ruleService.loadByName(title);
        compareRuleSources(title, source, ruleDao.source());
        return Optional.of(NativeEntity.create(entity.id(), ruleDao.id(), TYPE_V1, ruleDao.title(), ruleDao));
    } catch (NotFoundException e) {
        return Optional.empty();
    }
}
Also used : RuleDao(org.graylog.plugins.pipelineprocessor.db.RuleDao) PipelineRuleEntity(org.graylog2.contentpacks.model.entities.PipelineRuleEntity) NotFoundException(org.graylog2.database.NotFoundException)

Example 2 with PipelineRuleEntity

use of org.graylog2.contentpacks.model.entities.PipelineRuleEntity in project graylog2-server by Graylog2.

the class PipelineRuleFacade method exportNativeEntity.

@VisibleForTesting
Entity exportNativeEntity(RuleDao ruleDao, EntityDescriptorIds entityDescriptorIds) {
    final PipelineRuleEntity ruleEntity = PipelineRuleEntity.create(ValueReference.of(ruleDao.title()), ValueReference.of(ruleDao.description()), ValueReference.of(ruleDao.source()));
    final JsonNode data = objectMapper.convertValue(ruleEntity, JsonNode.class);
    return EntityV1.builder().id(ModelId.of(entityDescriptorIds.getOrThrow(ruleDao.id(), ModelTypes.PIPELINE_RULE_V1))).type(ModelTypes.PIPELINE_RULE_V1).data(data).build();
}
Also used : PipelineRuleEntity(org.graylog2.contentpacks.model.entities.PipelineRuleEntity) JsonNode(com.fasterxml.jackson.databind.JsonNode) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with PipelineRuleEntity

use of org.graylog2.contentpacks.model.entities.PipelineRuleEntity in project graylog2-server by Graylog2.

the class PipelineRuleFacadeTest method exportEntity.

@Test
public void exportEntity() {
    final RuleDao pipelineRule = RuleDao.builder().id("id").title("title").description("description").source("rule \"debug\"\nwhen\n  true\nthen\n  debug($message.message);\nend").build();
    final EntityDescriptor descriptor = EntityDescriptor.create("id", ModelTypes.PIPELINE_RULE_V1);
    final EntityDescriptorIds entityDescriptorIds = EntityDescriptorIds.of(descriptor);
    final Entity entity = facade.exportNativeEntity(pipelineRule, entityDescriptorIds);
    assertThat(entity).isInstanceOf(EntityV1.class);
    assertThat(entity.id()).isEqualTo(ModelId.of(entityDescriptorIds.get(descriptor).orElse(null)));
    assertThat(entity.type()).isEqualTo(ModelTypes.PIPELINE_RULE_V1);
    final EntityV1 entityV1 = (EntityV1) entity;
    final PipelineRuleEntity ruleEntity = objectMapper.convertValue(entityV1.data(), PipelineRuleEntity.class);
    assertThat(ruleEntity.title()).isEqualTo(ValueReference.of("title"));
    assertThat(ruleEntity.description()).isEqualTo(ValueReference.of("description"));
    assertThat(ruleEntity.source().asString(Collections.emptyMap())).startsWith("rule \"debug\"\n");
}
Also used : RuleDao(org.graylog.plugins.pipelineprocessor.db.RuleDao) EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) PipelineEntity(org.graylog2.contentpacks.model.entities.PipelineEntity) PipelineRuleEntity(org.graylog2.contentpacks.model.entities.PipelineRuleEntity) PipelineRuleEntity(org.graylog2.contentpacks.model.entities.PipelineRuleEntity) EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) Test(org.junit.Test)

Example 4 with PipelineRuleEntity

use of org.graylog2.contentpacks.model.entities.PipelineRuleEntity in project graylog2-server by Graylog2.

the class PipelineRuleFacadeTest method collectEntity.

@Test
@MongoDBFixtures("PipelineRuleFacadeTest.json")
public void collectEntity() {
    final EntityDescriptor descriptor = EntityDescriptor.create("5adf25034b900a0fdb4e5338", ModelTypes.PIPELINE_RULE_V1);
    final EntityDescriptorIds entityDescriptorIds = EntityDescriptorIds.of(descriptor);
    final Optional<Entity> collectedEntity = facade.exportEntity(descriptor, entityDescriptorIds);
    assertThat(collectedEntity).isPresent().containsInstanceOf(EntityV1.class);
    final EntityV1 entity = (EntityV1) collectedEntity.orElseThrow(AssertionError::new);
    assertThat(entity.id()).isEqualTo(ModelId.of(entityDescriptorIds.get(descriptor).orElse(null)));
    assertThat(entity.type()).isEqualTo(ModelTypes.PIPELINE_RULE_V1);
    final PipelineRuleEntity pipelineRuleEntity = objectMapper.convertValue(entity.data(), PipelineRuleEntity.class);
    assertThat(pipelineRuleEntity.title()).isEqualTo(ValueReference.of("debug"));
    assertThat(pipelineRuleEntity.description()).isEqualTo(ValueReference.of("Debug"));
    assertThat(pipelineRuleEntity.source().asString(Collections.emptyMap())).startsWith("rule \"debug\"\n");
}
Also used : EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) PipelineEntity(org.graylog2.contentpacks.model.entities.PipelineEntity) PipelineRuleEntity(org.graylog2.contentpacks.model.entities.PipelineRuleEntity) PipelineRuleEntity(org.graylog2.contentpacks.model.entities.PipelineRuleEntity) EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 5 with PipelineRuleEntity

use of org.graylog2.contentpacks.model.entities.PipelineRuleEntity in project graylog2-server by Graylog2.

the class PipelineRuleFacade method decode.

private NativeEntity<RuleDao> decode(EntityV1 entity, Map<String, ValueReference> parameters) {
    final PipelineRuleEntity ruleEntity = objectMapper.convertValue(entity.data(), PipelineRuleEntity.class);
    final String title = ruleEntity.title().asString(parameters);
    final String source = ruleEntity.source().asString(parameters);
    final DateTime now = Tools.nowUTC();
    final ValueReference description = ruleEntity.description();
    final RuleDao ruleDao = RuleDao.builder().title(title).description(description == null ? null : description.asString(parameters)).source(source).createdAt(now).modifiedAt(now).build();
    final RuleDao savedRuleDao = ruleService.save(ruleDao);
    return NativeEntity.create(entity.id(), savedRuleDao.id(), TYPE_V1, savedRuleDao.title(), savedRuleDao);
}
Also used : RuleDao(org.graylog.plugins.pipelineprocessor.db.RuleDao) PipelineRuleEntity(org.graylog2.contentpacks.model.entities.PipelineRuleEntity) DateTime(org.joda.time.DateTime) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference)

Aggregations

PipelineRuleEntity (org.graylog2.contentpacks.model.entities.PipelineRuleEntity)5 RuleDao (org.graylog.plugins.pipelineprocessor.db.RuleDao)3 EntityDescriptorIds (org.graylog2.contentpacks.EntityDescriptorIds)2 Entity (org.graylog2.contentpacks.model.entities.Entity)2 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)2 EntityV1 (org.graylog2.contentpacks.model.entities.EntityV1)2 NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)2 PipelineEntity (org.graylog2.contentpacks.model.entities.PipelineEntity)2 Test (org.junit.Test)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)1 ValueReference (org.graylog2.contentpacks.model.entities.references.ValueReference)1 NotFoundException (org.graylog2.database.NotFoundException)1 DateTime (org.joda.time.DateTime)1