Search in sources :

Example 1 with LookupTableEntity

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

the class LookupTableFacade method resolveForInstallation.

private Graph<Entity> resolveForInstallation(EntityV1 entity, Map<String, ValueReference> parameters, Map<EntityDescriptor, Entity> entities) {
    final MutableGraph<Entity> mutableGraph = GraphBuilder.directed().build();
    mutableGraph.addNode(entity);
    final LookupTableEntity lookupTableEntity = objectMapper.convertValue(entity.data(), LookupTableEntity.class);
    final String dataAdapterName = lookupTableEntity.dataAdapterName().asString(parameters);
    final EntityDescriptor dataAdapterDescriptor = adapterDescriptor(dataAdapterName);
    final Entity dataAdapterEntity = entities.get(dataAdapterDescriptor);
    if (dataAdapterEntity == null) {
        throw new ContentPackException("Missing data adapter \"" + dataAdapterName + "\" for lookup table " + entity.toEntityDescriptor());
    } else {
        mutableGraph.putEdge(entity, dataAdapterEntity);
    }
    final String cacheName = lookupTableEntity.cacheName().asString(parameters);
    final EntityDescriptor cacheDescriptor = cacheDescriptor(cacheName);
    final Entity cacheEntity = entities.get(cacheDescriptor);
    if (cacheEntity == null) {
        throw new ContentPackException("Missing cache \"" + cacheName + "\" for lookup table " + entity.toEntityDescriptor());
    } else {
        mutableGraph.putEdge(entity, cacheEntity);
    }
    return ImmutableGraph.copyOf(mutableGraph);
}
Also used : ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity)

Example 2 with LookupTableEntity

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

the class LookupTableFacade method decode.

private NativeEntity<LookupTableDto> decode(EntityV1 entity, Map<String, ValueReference> parameters, Map<EntityDescriptor, Object> nativeEntities) {
    final LookupTableEntity lookupTableEntity = objectMapper.convertValue(entity.data(), LookupTableEntity.class);
    final String referencedDataAdapterName = lookupTableEntity.dataAdapterName().asString(parameters);
    final EntityDescriptor dataAdapterDescriptor = adapterDescriptor(referencedDataAdapterName);
    final Object dataAdapter = nativeEntities.get(dataAdapterDescriptor);
    final String dataAdapterId;
    if (dataAdapter instanceof DataAdapterDto) {
        dataAdapterId = ((DataAdapterDto) dataAdapter).id();
    } else {
        throw new MissingNativeEntityException(dataAdapterDescriptor);
    }
    final String referencedCacheName = lookupTableEntity.cacheName().asString(parameters);
    final EntityDescriptor cacheDescriptor = cacheDescriptor(referencedCacheName);
    final Object cache = nativeEntities.get(cacheDescriptor);
    final String cacheId;
    if (cache instanceof CacheDto) {
        cacheId = ((CacheDto) cache).id();
    } else {
        throw new MissingNativeEntityException(cacheDescriptor);
    }
    final LookupTableDto lookupTableDto = LookupTableDto.builder().name(lookupTableEntity.name().asString(parameters)).title(lookupTableEntity.title().asString(parameters)).description(lookupTableEntity.description().asString(parameters)).dataAdapterId(dataAdapterId).cacheId(cacheId).defaultSingleValue(lookupTableEntity.defaultSingleValue().asString(parameters)).defaultSingleValueType(lookupTableEntity.defaultSingleValueType().asEnum(parameters, LookupDefaultSingleValue.Type.class)).defaultMultiValue(lookupTableEntity.defaultMultiValue().asString(parameters)).defaultMultiValueType(lookupTableEntity.defaultMultiValueType().asEnum(parameters, LookupDefaultMultiValue.Type.class)).build();
    final LookupTableDto savedLookupTableDto = lookupTableService.save(lookupTableDto);
    return NativeEntity.create(entity.id(), savedLookupTableDto.id(), TYPE_V1, lookupTableDto.title(), savedLookupTableDto);
}
Also used : EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity) DataAdapterDto(org.graylog2.lookup.dto.DataAdapterDto) LookupDefaultSingleValue(org.graylog2.lookup.LookupDefaultSingleValue) LookupDefaultMultiValue(org.graylog2.lookup.LookupDefaultMultiValue) MissingNativeEntityException(org.graylog2.contentpacks.exceptions.MissingNativeEntityException) LookupTableDto(org.graylog2.lookup.dto.LookupTableDto) CacheDto(org.graylog2.lookup.dto.CacheDto)

Example 3 with LookupTableEntity

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

the class LookupTableFacade method findExisting.

private Optional<NativeEntity<LookupTableDto>> findExisting(EntityV1 entity, Map<String, ValueReference> parameters) {
    final LookupTableEntity lookupTableEntity = objectMapper.convertValue(entity.data(), LookupTableEntity.class);
    final String name = lookupTableEntity.name().asString(parameters);
    final String title = lookupTableEntity.title().asString(parameters);
    final Optional<LookupTableDto> lookupTable = lookupTableService.get(name);
    lookupTable.ifPresent(existingLookupTable -> compareLookupTable(name, title, existingLookupTable));
    return lookupTable.map(lt -> NativeEntity.create(entity.id(), lt.id(), TYPE_V1, lt.title(), lt));
}
Also used : LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity) LookupTableDto(org.graylog2.lookup.dto.LookupTableDto)

Example 4 with LookupTableEntity

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

the class LookupTableFacade method exportNativeEntity.

@VisibleForTesting
Entity exportNativeEntity(LookupTableDto lookupTableDto, EntityDescriptorIds entityDescriptorIds) {
    final String tableId = entityDescriptorIds.get(EntityDescriptor.create(lookupTableDto.id(), ModelTypes.LOOKUP_TABLE_V1)).orElseThrow(() -> new ContentPackException("Couldn't find lookup table entity " + lookupTableDto.id()));
    final String cacheId = entityDescriptorIds.get(cacheDescriptor(lookupTableDto.cacheId())).orElseThrow(() -> new ContentPackException("Couldn't find lookup cache entity " + lookupTableDto.cacheId()));
    final String adapterId = entityDescriptorIds.get(adapterDescriptor(lookupTableDto.dataAdapterId())).orElseThrow(() -> new ContentPackException("Couldn't find lookup data adapter entity " + lookupTableDto.dataAdapterId()));
    final LookupTableEntity lookupTableEntity = LookupTableEntity.create(ValueReference.of(lookupTableDto.name()), ValueReference.of(lookupTableDto.title()), ValueReference.of(lookupTableDto.description()), ValueReference.of(cacheId), ValueReference.of(adapterId), ValueReference.of(lookupTableDto.defaultSingleValue()), ValueReference.of(lookupTableDto.defaultSingleValueType()), ValueReference.of(lookupTableDto.defaultMultiValue()), ValueReference.of(lookupTableDto.defaultMultiValueType()));
    final JsonNode data = objectMapper.convertValue(lookupTableEntity, JsonNode.class);
    return EntityV1.builder().id(ModelId.of(tableId)).type(ModelTypes.LOOKUP_TABLE_V1).data(data).build();
}
Also used : ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity) JsonNode(com.fasterxml.jackson.databind.JsonNode) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 5 with LookupTableEntity

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

the class InputFacadeTest method resolveForInstallationLookupTable.

@Test
@MongoDBFixtures("InputFacadeTest.json")
public void resolveForInstallationLookupTable() throws NotFoundException {
    when(lookupuptableBuilder.lookupTable("whois")).thenReturn(lookupuptableBuilder);
    when(lookupuptableBuilder.lookupTable("tor-exit-node-list")).thenReturn(lookupuptableBuilder);
    when(lookupuptableBuilder.build()).thenReturn(lookupTable);
    when(lookupTableService.newBuilder()).thenReturn(lookupuptableBuilder);
    when(lookupTableService.hasTable("whois")).thenReturn(true);
    when(lookupTableService.hasTable("tor-exit-node-list")).thenReturn(true);
    final Input input = inputService.find("5ae2eb0a3d27464477f0fd8b");
    final Map<String, Object> lookupTableConfig = new HashedMap(1);
    lookupTableConfig.put("lookup_table_name", "tor-exit-node-list");
    final ConverterEntity converterEntity = ConverterEntity.create(ValueReference.of(Converter.Type.LOOKUP_TABLE.name()), ReferenceMapUtils.toReferenceMap(lookupTableConfig));
    final List<ConverterEntity> converterEntities = new ArrayList<>(1);
    converterEntities.add(converterEntity);
    final InputWithExtractors inputWithExtractors = InputWithExtractors.create(input, inputService.getExtractors(input));
    final LookupTableExtractor extractor = (LookupTableExtractor) inputWithExtractors.extractors().iterator().next();
    final ExtractorEntity extractorEntity = ExtractorEntity.create(ValueReference.of(extractor.getTitle()), ValueReference.of(extractor.getType()), ValueReference.of(extractor.getCursorStrategy()), ValueReference.of(extractor.getTargetField()), ValueReference.of(extractor.getSourceField()), ReferenceMapUtils.toReferenceMap(extractor.getExtractorConfig()), converterEntities, ValueReference.of(extractor.getConditionType()), ValueReference.of(extractor.getConditionValue()), ValueReference.of(extractor.getOrder()));
    List<ExtractorEntity> extractors = new ArrayList<>();
    extractors.add(extractorEntity);
    InputEntity inputEntity = InputEntity.create(ValueReference.of(input.getTitle()), ReferenceMapUtils.toReferenceMap(input.getConfiguration()), Collections.emptyMap(), ValueReference.of(input.getType()), ValueReference.of(input.isGlobal()), extractors);
    final Entity entity = EntityV1.builder().id(ModelId.of(input.getId())).type(ModelTypes.INPUT_V1).data(objectMapper.convertValue(inputEntity, JsonNode.class)).build();
    final LookupTableEntity whoIsEntity = LookupTableEntity.create(ValueReference.of("whois"), ValueReference.of("title"), ValueReference.of("description"), ValueReference.of("cache_name"), ValueReference.of("dataadapter_name"), ValueReference.of("default_single_value"), ValueReference.of("BOOLEAN"), ValueReference.of("default_multi_value"), ValueReference.of("BOOLEAN"));
    final LookupTableEntity torNodeEntity = LookupTableEntity.create(ValueReference.of("tor-exit-node-list"), ValueReference.of("title"), ValueReference.of("description"), ValueReference.of("cache_name"), ValueReference.of("dataadapter_name"), ValueReference.of("default_single_value"), ValueReference.of("BOOLEAN"), ValueReference.of("default_multi_value"), ValueReference.of("BOOLEAN"));
    final Entity expectedWhoIsEntity = EntityV1.builder().id(ModelId.of("dead-beef")).data(objectMapper.convertValue(whoIsEntity, JsonNode.class)).type(ModelTypes.LOOKUP_TABLE_V1).build();
    final Entity expectedTorEntity = EntityV1.builder().id(ModelId.of("dead-feed")).data(objectMapper.convertValue(torNodeEntity, JsonNode.class)).type(ModelTypes.LOOKUP_TABLE_V1).build();
    final EntityDescriptor whoisDescriptor = expectedWhoIsEntity.toEntityDescriptor();
    final EntityDescriptor torDescriptor = expectedTorEntity.toEntityDescriptor();
    final Map<EntityDescriptor, Entity> entityDescriptorEntityMap = new HashMap<>(2);
    entityDescriptorEntityMap.put(whoisDescriptor, expectedWhoIsEntity);
    entityDescriptorEntityMap.put(torDescriptor, expectedTorEntity);
    Graph<Entity> graph = facade.resolveForInstallation(entity, Collections.emptyMap(), entityDescriptorEntityMap);
    assertThat(graph.nodes()).contains(expectedWhoIsEntity);
    assertThat(graph.nodes()).contains(expectedTorEntity);
}
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) LookupTableExtractor(org.graylog2.inputs.extractors.LookupTableExtractor) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) 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) LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity) ConverterEntity(org.graylog2.contentpacks.model.entities.ConverterEntity) ExtractorEntity(org.graylog2.contentpacks.model.entities.ExtractorEntity) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity) HashedMap(org.apache.commons.collections.map.HashedMap) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Aggregations

LookupTableEntity (org.graylog2.contentpacks.model.entities.LookupTableEntity)8 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)6 Entity (org.graylog2.contentpacks.model.entities.Entity)5 NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)5 Test (org.junit.Test)4 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)3 EntityDescriptorIds (org.graylog2.contentpacks.EntityDescriptorIds)3 EntityV1 (org.graylog2.contentpacks.model.entities.EntityV1)3 LookupCacheEntity (org.graylog2.contentpacks.model.entities.LookupCacheEntity)3 LookupDataAdapterEntity (org.graylog2.contentpacks.model.entities.LookupDataAdapterEntity)3 LookupTableDto (org.graylog2.lookup.dto.LookupTableDto)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ContentPackException (org.graylog2.contentpacks.exceptions.ContentPackException)2 NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashedMap (org.apache.commons.collections.map.HashedMap)1 MissingNativeEntityException (org.graylog2.contentpacks.exceptions.MissingNativeEntityException)1 ConverterEntity (org.graylog2.contentpacks.model.entities.ConverterEntity)1