Search in sources :

Example 6 with GrokPatternEntity

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

the class InputFacadeTest method resolveForInstallationGrokPattern.

@Test
@MongoDBFixtures("InputFacadeTest.json")
public void resolveForInstallationGrokPattern() throws NotFoundException {
    final Input input = inputService.find("5ae2ebbeef27464477f0fd8b");
    final InputWithExtractors inputWithExtractors = InputWithExtractors.create(input, inputService.getExtractors(input));
    final GrokExtractor grokExtractor = (GrokExtractor) inputWithExtractors.extractors().iterator().next();
    final ExtractorEntity extractorEntity = ExtractorEntity.create(ValueReference.of(grokExtractor.getTitle()), ValueReference.of(grokExtractor.getType()), ValueReference.of(grokExtractor.getCursorStrategy()), ValueReference.of(grokExtractor.getTargetField()), ValueReference.of(grokExtractor.getSourceField()), ReferenceMapUtils.toReferenceMap(grokExtractor.getExtractorConfig()), Collections.emptyList(), ValueReference.of(grokExtractor.getConditionType()), ValueReference.of(grokExtractor.getConditionValue()), ValueReference.of(grokExtractor.getOrder()));
    List<ExtractorEntity> extractorEntities = new ArrayList<>(1);
    extractorEntities.add(extractorEntity);
    InputEntity inputEntity = InputEntity.create(ValueReference.of(input.getTitle()), ReferenceMapUtils.toReferenceMap(input.getConfiguration()), Collections.emptyMap(), ValueReference.of(input.getType()), ValueReference.of(input.isGlobal()), extractorEntities);
    Entity entity = EntityV1.builder().id(ModelId.of(input.getId())).type(ModelTypes.INPUT_V1).data(objectMapper.convertValue(inputEntity, JsonNode.class)).build();
    final GrokPatternEntity grokPatternEntity = GrokPatternEntity.create("GREEDY", ".*");
    final Entity expectedEntity = EntityV1.builder().id(ModelId.of("dead-feed")).data(objectMapper.convertValue(grokPatternEntity, JsonNode.class)).type(ModelTypes.GROK_PATTERN_V1).build();
    final EntityDescriptor entityDescriptor = expectedEntity.toEntityDescriptor();
    final Map<EntityDescriptor, Entity> entities = new HashMap<>(1);
    entities.put(entityDescriptor, expectedEntity);
    Graph<Entity> graph = facade.resolveForInstallation(entity, Collections.emptyMap(), entities);
    assertThat(graph.nodes()).contains(expectedEntity);
}
Also used : NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) ConverterEntity(org.graylog2.contentpacks.model.entities.ConverterEntity) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) ExtractorEntity(org.graylog2.contentpacks.model.entities.ExtractorEntity) LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) RawUDPInput(org.graylog2.inputs.raw.udp.RawUDPInput) Input(org.graylog2.inputs.Input) FakeHttpMessageInput(org.graylog2.inputs.random.FakeHttpMessageInput) MessageInput(org.graylog2.plugin.inputs.MessageInput) GrokExtractor(org.graylog2.inputs.extractors.GrokExtractor) ExtractorEntity(org.graylog2.contentpacks.model.entities.ExtractorEntity) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 7 with GrokPatternEntity

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

the class GrokPatternFacadeTest method notResolveNotMatchingDependecyForInstallation.

@Test
public void notResolveNotMatchingDependecyForInstallation() {
    final Entity grokPatternEntity = EntityV1.builder().id(ModelId.of("1")).type(ModelTypes.GROK_PATTERN_V1).data(objectMapper.convertValue(GrokPatternEntity.create("Test", "%{DOOM}"), 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()).doesNotContain(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)

Example 8 with GrokPatternEntity

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

the class GrokPatternFacadeTest method findExistingFailsWithDivergingPatterns.

@Test
public void findExistingFailsWithDivergingPatterns() throws ValidationException {
    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", "BOOM"), JsonNode.class)).build();
    assertThatThrownBy(() -> facade.findExisting(grokPatternEntity, Collections.emptyMap())).isInstanceOf(DivergingEntityConfigurationException.class).hasMessage("Expected Grok pattern for name \"Test\": <BOOM>; actual Grok pattern: <[a-z]+>");
}
Also used : NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) DivergingEntityConfigurationException(org.graylog2.contentpacks.exceptions.DivergingEntityConfigurationException) Test(org.junit.Test)

Example 9 with GrokPatternEntity

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

the class GrokPatternFacadeTest method exportNativeEntity.

@Test
public void exportNativeEntity() {
    final GrokPattern grokPattern = GrokPattern.create("01234567890", "name", "pattern", null);
    final EntityDescriptor descriptor = EntityDescriptor.create(grokPattern.id(), ModelTypes.GROK_PATTERN_V1);
    final EntityDescriptorIds entityDescriptorIds = EntityDescriptorIds.of(descriptor);
    final Entity entity = facade.exportNativeEntity(grokPattern, entityDescriptorIds);
    assertThat(entity).isInstanceOf(EntityV1.class);
    assertThat(entity.id()).isEqualTo(ModelId.of(entityDescriptorIds.get(descriptor).orElse(null)));
    assertThat(entity.type()).isEqualTo(ModelTypes.GROK_PATTERN_V1);
    final EntityV1 entityV1 = (EntityV1) entity;
    final GrokPatternEntity grokPatternEntity = objectMapper.convertValue(entityV1.data(), GrokPatternEntity.class);
    assertThat(grokPatternEntity.name()).isEqualTo("name");
    assertThat(grokPatternEntity.pattern()).isEqualTo("pattern");
}
Also used : EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) 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) EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) Test(org.junit.Test)

Example 10 with GrokPatternEntity

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

the class GrokPatternFacade method resolveForInstallationV1.

private Graph<Entity> resolveForInstallationV1(EntityV1 entity, Map<EntityDescriptor, Entity> entities) {
    final MutableGraph<Entity> mutableGraph = GraphBuilder.directed().build();
    mutableGraph.addNode(entity);
    final GrokPatternEntity grokPatternEntity = objectMapper.convertValue(entity.data(), GrokPatternEntity.class);
    final String namedPattern = grokPatternEntity.pattern();
    final Set<String> patterns = GrokPatternService.extractPatternNames(namedPattern);
    patterns.stream().forEach(patternName -> {
        entities.entrySet().stream().filter(x -> x.getValue().type().equals(ModelTypes.GROK_PATTERN_V1)).filter(x -> {
            EntityV1 entityV1 = (EntityV1) x.getValue();
            GrokPatternEntity grokPatternEntity1 = objectMapper.convertValue(entityV1.data(), GrokPatternEntity.class);
            return grokPatternEntity1.name().equals(patternName);
        }).forEach(x -> mutableGraph.putEdge(entity, x.getValue()));
    });
    return ImmutableGraph.copyOf(mutableGraph);
}
Also used : EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) ImmutableGraph(com.google.common.graph.ImmutableGraph) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) LoggerFactory(org.slf4j.LoggerFactory) Entity(org.graylog2.contentpacks.model.entities.Entity) ModelType(org.graylog2.contentpacks.model.ModelType) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) Inject(javax.inject.Inject) EntityExcerpt(org.graylog2.contentpacks.model.entities.EntityExcerpt) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) NotFoundException(org.graylog2.database.NotFoundException) Logger(org.slf4j.Logger) ModelId(org.graylog2.contentpacks.model.ModelId) GrokPatternService(org.graylog2.grok.GrokPatternService) MutableGraph(com.google.common.graph.MutableGraph) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) Collectors(java.util.stream.Collectors) GraphBuilder(com.google.common.graph.GraphBuilder) EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) GrokPattern(org.graylog2.grok.GrokPattern) ValidationException(org.graylog2.plugin.database.ValidationException) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) DivergingEntityConfigurationException(org.graylog2.contentpacks.exceptions.DivergingEntityConfigurationException) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) Graph(com.google.common.graph.Graph) ModelTypes(org.graylog2.contentpacks.model.ModelTypes) EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity)

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