Search in sources :

Example 1 with GrokPatternEntity

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

the class GrokPatternFacade method exportNativeEntity.

@VisibleForTesting
Entity exportNativeEntity(GrokPattern grokPattern, EntityDescriptorIds entityDescriptorIds) {
    final GrokPatternEntity grokPatternEntity = GrokPatternEntity.create(grokPattern.name(), grokPattern.pattern());
    final JsonNode data = objectMapper.convertValue(grokPatternEntity, JsonNode.class);
    return EntityV1.builder().id(ModelId.of(entityDescriptorIds.getOrThrow(grokPattern.id(), ModelTypes.GROK_PATTERN_V1))).type(ModelTypes.GROK_PATTERN_V1).data(data).build();
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with GrokPatternEntity

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

the class GrokPatternFacade method decode.

private NativeEntity<GrokPattern> decode(EntityV1 entity) {
    final GrokPatternEntity grokPatternEntity = objectMapper.convertValue(entity.data(), GrokPatternEntity.class);
    final GrokPattern grokPattern = GrokPattern.create(grokPatternEntity.name(), grokPatternEntity.pattern());
    try {
        final GrokPattern savedGrokPattern = grokPatternService.save(grokPattern);
        return NativeEntity.create(entity.id(), savedGrokPattern.id(), TYPE_V1, savedGrokPattern.name(), savedGrokPattern);
    } catch (ValidationException e) {
        throw new RuntimeException("Couldn't create grok pattern " + grokPattern.name());
    }
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) GrokPattern(org.graylog2.grok.GrokPattern) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity)

Example 3 with GrokPatternEntity

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

the class GrokPatternFacadeTest method createNativeEntity.

@Test
public void createNativeEntity() throws NotFoundException {
    final Entity grokPatternEntity = EntityV1.builder().id(ModelId.of("1")).type(ModelTypes.GROK_PATTERN_V1).data(objectMapper.convertValue(GrokPatternEntity.create("Test", "[a-z]+"), JsonNode.class)).build();
    final NativeEntity<GrokPattern> nativeEntity = facade.createNativeEntity(grokPatternEntity, Collections.emptyMap(), Collections.emptyMap(), "admin");
    final GrokPattern expectedGrokPattern = GrokPattern.create("1", "Test", "[a-z]+", null);
    final NativeEntityDescriptor expectedDescriptor = NativeEntityDescriptor.create("1", "1", ModelTypes.GROK_PATTERN_V1, "Test");
    assertThat(nativeEntity.descriptor().title()).isEqualTo(expectedDescriptor.title());
    assertThat(nativeEntity.descriptor().type()).isEqualTo(expectedDescriptor.type());
    assertThat(nativeEntity.entity()).isEqualTo(expectedGrokPattern);
    assertThat(grokPatternService.load("1")).isEqualTo(expectedGrokPattern);
}
Also used : NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) GrokPattern(org.graylog2.grok.GrokPattern) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) Test(org.junit.Test)

Example 4 with GrokPatternEntity

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

the class GrokPatternFacadeTest method findExisting.

@Test
public void findExisting() throws ValidationException {
    final GrokPattern grokPattern = grokPatternService.save(GrokPattern.create("Test", "[a-z]+"));
    final Entity grokPatternEntity = EntityV1.builder().id(ModelId.of("1")).type(ModelTypes.GROK_PATTERN_V1).data(objectMapper.convertValue(GrokPatternEntity.create("Test", "[a-z]+"), JsonNode.class)).build();
    final Optional<NativeEntity<GrokPattern>> existingGrokPattern = facade.findExisting(grokPatternEntity, Collections.emptyMap());
    final NativeEntityDescriptor expectedDescriptor = NativeEntityDescriptor.create(grokPatternEntity.id(), "1", ModelTypes.GROK_PATTERN_V1, grokPattern.name(), false);
    assertThat(existingGrokPattern).isPresent().get().satisfies(nativeEntity -> {
        assertThat(nativeEntity.descriptor()).isEqualTo(expectedDescriptor);
        assertThat(nativeEntity.entity()).isEqualTo(grokPattern);
    });
}
Also used : NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) GrokPattern(org.graylog2.grok.GrokPattern) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Test(org.junit.Test)

Example 5 with GrokPatternEntity

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

the class GrokPatternFacadeTest method resolveMatchingDependecyForInstallation.

@Test
public void resolveMatchingDependecyForInstallation() {
    final Entity grokPatternEntity = EntityV1.builder().id(ModelId.of("1")).type(ModelTypes.GROK_PATTERN_V1).data(objectMapper.convertValue(GrokPatternEntity.create("Test", "%{PORTAL}"), JsonNode.class)).build();
    final Entity grokPatternEntityDependency = EntityV1.builder().id(ModelId.of("1")).type(ModelTypes.GROK_PATTERN_V1).data(objectMapper.convertValue(GrokPatternEntity.create("PORTAL", "\\d\\d"), JsonNode.class)).build();
    final EntityDescriptor dependencyDescriptor = grokPatternEntityDependency.toEntityDescriptor();
    final Map<EntityDescriptor, Entity> entityDescriptorEntityMap = new HashMap(1);
    entityDescriptorEntityMap.put(dependencyDescriptor, grokPatternEntityDependency);
    final Map<String, ValueReference> parameters = Collections.emptyMap();
    Graph<Entity> graph = facade.resolveForInstallation(grokPatternEntity, parameters, entityDescriptorEntityMap);
    assertThat(graph.nodes().toArray()).contains(grokPatternEntityDependency);
}
Also used : NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) HashMap(java.util.HashMap) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) Test(org.junit.Test)

Aggregations

GrokPatternEntity (org.graylog2.contentpacks.model.entities.GrokPatternEntity)11 Entity (org.graylog2.contentpacks.model.entities.Entity)8 NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)8 Test (org.junit.Test)7 NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)6 GrokPattern (org.graylog2.grok.GrokPattern)6 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 HashMap (java.util.HashMap)3 ValueReference (org.graylog2.contentpacks.model.entities.references.ValueReference)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 EntityDescriptorIds (org.graylog2.contentpacks.EntityDescriptorIds)2 DivergingEntityConfigurationException (org.graylog2.contentpacks.exceptions.DivergingEntityConfigurationException)2 EntityV1 (org.graylog2.contentpacks.model.entities.EntityV1)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Graph (com.google.common.graph.Graph)1 GraphBuilder (com.google.common.graph.GraphBuilder)1 ImmutableGraph (com.google.common.graph.ImmutableGraph)1 MutableGraph (com.google.common.graph.MutableGraph)1 ArrayList (java.util.ArrayList)1