Search in sources :

Example 1 with LookupTableDto

use of org.graylog2.lookup.dto.LookupTableDto in project graylog2-server by Graylog2.

the class InputFacade method resolveNativeEntityLookupTable.

private void resolveNativeEntityLookupTable(EntityDescriptor entityDescriptor, InputWithExtractors inputWithExtractors, MutableGraph<EntityDescriptor> mutableGraph) {
    final Stream<String> extractorLookupNames = inputWithExtractors.extractors().stream().filter(e -> e.getType().equals(Extractor.Type.LOOKUP_TABLE)).map(e -> (String) e.getExtractorConfig().get(LookupTableExtractor.CONFIG_LUT_NAME));
    final Stream<String> converterLookupNames = inputWithExtractors.extractors().stream().flatMap(e -> e.getConverters().stream()).filter(c -> c.getType().equals(Converter.Type.LOOKUP_TABLE)).map(c -> (String) c.getConfig().get("lookup_table_name"));
    Stream.concat(extractorLookupNames, converterLookupNames).map(lookupTableService::get).filter(Optional::isPresent).map(Optional::get).forEach(lookupTableDto -> {
        EntityDescriptor lookupTable = EntityDescriptor.create(ModelId.of(lookupTableDto.id()), ModelTypes.LOOKUP_TABLE_V1);
        mutableGraph.putEdge(entityDescriptor, lookupTable);
    });
}
Also used : Extractor(org.graylog2.plugin.inputs.Extractor) Constraint(org.graylog2.contentpacks.model.constraints.Constraint) ImmutableGraph(com.google.common.graph.ImmutableGraph) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Tools(org.graylog2.plugin.Tools) InputService(org.graylog2.inputs.InputService) NoSuchInputTypeException(org.graylog2.shared.inputs.NoSuchInputTypeException) LoggerFactory(org.slf4j.LoggerFactory) ModelType(org.graylog2.contentpacks.model.ModelType) ConverterEntity(org.graylog2.contentpacks.model.entities.ConverterEntity) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference) InputRegistry(org.graylog2.shared.inputs.InputRegistry) MessageInputFactory(org.graylog2.shared.inputs.MessageInputFactory) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) ConverterFactory(org.graylog2.inputs.converters.ConverterFactory) ImmutableSet(com.google.common.collect.ImmutableSet) ModelId(org.graylog2.contentpacks.model.ModelId) GrokPatternService(org.graylog2.grok.GrokPatternService) ImmutableMap(com.google.common.collect.ImmutableMap) MutableGraph(com.google.common.graph.MutableGraph) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) Collection(java.util.Collection) Set(java.util.Set) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity) ServerStatus(org.graylog2.plugin.ServerStatus) ReferenceMapUtils.toReferenceMap(org.graylog2.contentpacks.model.entities.references.ReferenceMapUtils.toReferenceMap) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) GraphBuilder(com.google.common.graph.GraphBuilder) List(java.util.List) Stream(java.util.stream.Stream) GrokExtractor(org.graylog2.inputs.extractors.GrokExtractor) PluginMetaData(org.graylog2.plugin.PluginMetaData) Optional(java.util.Optional) ModelTypes(org.graylog2.contentpacks.model.ModelTypes) Configuration(org.graylog2.plugin.configuration.Configuration) EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) Converter(org.graylog2.plugin.inputs.Converter) Entity(org.graylog2.contentpacks.model.entities.Entity) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) ExtractorEntity(org.graylog2.contentpacks.model.entities.ExtractorEntity) DBLookupTableService(org.graylog2.lookup.db.DBLookupTableService) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) EntityExcerpt(org.graylog2.contentpacks.model.entities.EntityExcerpt) ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) Input(org.graylog2.inputs.Input) ExtractorFactory(org.graylog2.inputs.extractors.ExtractorFactory) NotFoundException(org.graylog2.database.NotFoundException) ReferenceMap(org.graylog2.contentpacks.model.entities.references.ReferenceMap) PluginVersionConstraint(org.graylog2.contentpacks.model.constraints.PluginVersionConstraint) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity) Ints(com.google.common.primitives.Ints) LookupTableExtractor(org.graylog2.inputs.extractors.LookupTableExtractor) EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) MessageInput(org.graylog2.plugin.inputs.MessageInput) ValidationException(org.graylog2.plugin.database.ValidationException) VisibleForTesting(com.google.common.annotations.VisibleForTesting) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) Message(org.graylog2.plugin.Message) ReferenceMapUtils.toValueMap(org.graylog2.contentpacks.model.entities.references.ReferenceMapUtils.toValueMap) Graph(com.google.common.graph.Graph) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) Optional(java.util.Optional)

Example 2 with LookupTableDto

use of org.graylog2.lookup.dto.LookupTableDto in project graylog2-server by Graylog2.

the class LookupTableFacade method resolveNativeEntity.

@Override
public Graph<EntityDescriptor> resolveNativeEntity(EntityDescriptor entityDescriptor) {
    final MutableGraph<EntityDescriptor> mutableGraph = GraphBuilder.directed().build();
    mutableGraph.addNode(entityDescriptor);
    final ModelId modelId = entityDescriptor.id();
    final Optional<LookupTableDto> lookupTableDto = lookupTableService.get(modelId.id());
    lookupTableDto.map(LookupTableDto::dataAdapterId).map(this::adapterDescriptor).ifPresent(dataAdapter -> mutableGraph.putEdge(entityDescriptor, dataAdapter));
    lookupTableDto.map(LookupTableDto::cacheId).map(this::cacheDescriptor).ifPresent(cache -> mutableGraph.putEdge(entityDescriptor, cache));
    return ImmutableGraph.copyOf(mutableGraph);
}
Also used : EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) LookupTableDto(org.graylog2.lookup.dto.LookupTableDto) ModelId(org.graylog2.contentpacks.model.ModelId)

Example 3 with LookupTableDto

use of org.graylog2.lookup.dto.LookupTableDto 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 4 with LookupTableDto

use of org.graylog2.lookup.dto.LookupTableDto 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 5 with LookupTableDto

use of org.graylog2.lookup.dto.LookupTableDto 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)

Aggregations

LookupTableDto (org.graylog2.lookup.dto.LookupTableDto)14 LookupTableEntity (org.graylog2.contentpacks.model.entities.LookupTableEntity)8 Entity (org.graylog2.contentpacks.model.entities.Entity)5 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)5 NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)5 Test (org.junit.Test)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 ApiOperation (io.swagger.annotations.ApiOperation)4 Path (javax.ws.rs.Path)4 LookupCacheEntity (org.graylog2.contentpacks.model.entities.LookupCacheEntity)4 LookupDataAdapterEntity (org.graylog2.contentpacks.model.entities.LookupDataAdapterEntity)4 NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)3 CacheDto (org.graylog2.lookup.dto.CacheDto)3 DataAdapterDto (org.graylog2.lookup.dto.DataAdapterDto)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Subscribe (com.google.common.eventbus.Subscribe)2 AbstractIdleService (com.google.common.util.concurrent.AbstractIdleService)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 GET (javax.ws.rs.GET)2